2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
41 #include "wine/list.h"
46 static const func_t
*current_func
;
47 static const type_t
*current_structure
;
49 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
50 struct expr_eval_routine
53 const type_t
*structure
;
57 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
);
58 static size_t write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
59 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
60 const char *name
, int write_ptr
, unsigned int *tfsoff
);
62 const char *string_of_type(unsigned char type
)
66 case RPC_FC_BYTE
: return "FC_BYTE";
67 case RPC_FC_CHAR
: return "FC_CHAR";
68 case RPC_FC_SMALL
: return "FC_SMALL";
69 case RPC_FC_USMALL
: return "FC_USMALL";
70 case RPC_FC_WCHAR
: return "FC_WCHAR";
71 case RPC_FC_SHORT
: return "FC_SHORT";
72 case RPC_FC_USHORT
: return "FC_USHORT";
73 case RPC_FC_LONG
: return "FC_LONG";
74 case RPC_FC_ULONG
: return "FC_ULONG";
75 case RPC_FC_FLOAT
: return "FC_FLOAT";
76 case RPC_FC_HYPER
: return "FC_HYPER";
77 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
78 case RPC_FC_ENUM16
: return "FC_ENUM16";
79 case RPC_FC_ENUM32
: return "FC_ENUM32";
80 case RPC_FC_IGNORE
: return "FC_IGNORE";
81 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
82 case RPC_FC_RP
: return "FC_RP";
83 case RPC_FC_UP
: return "FC_UP";
84 case RPC_FC_OP
: return "FC_OP";
85 case RPC_FC_FP
: return "FC_FP";
86 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
87 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
88 case RPC_FC_STRUCT
: return "FC_STRUCT";
89 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
90 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
91 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
92 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
93 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
94 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
95 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
96 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
97 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
98 case RPC_FC_CARRAY
: return "FC_CARRAY";
99 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
100 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
101 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
102 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
103 case RPC_FC_POINTER
: return "FC_POINTER";
104 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
105 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
106 case RPC_FC_CSTRING
: return "FC_CSTRING";
107 case RPC_FC_WSTRING
: return "FC_WSTRING";
109 error("string_of_type: unknown type 0x%02x\n", type
);
114 int is_struct(unsigned char type
)
121 case RPC_FC_CPSTRUCT
:
122 case RPC_FC_CVSTRUCT
:
123 case RPC_FC_BOGUS_STRUCT
:
130 static int is_non_complex_struct(const type_t
*type
)
137 case RPC_FC_CPSTRUCT
:
138 case RPC_FC_CVSTRUCT
:
145 int is_union(unsigned char type
)
149 case RPC_FC_ENCAPSULATED_UNION
:
150 case RPC_FC_NON_ENCAPSULATED_UNION
:
157 static unsigned short user_type_offset(const char *name
)
160 unsigned short off
= 0;
161 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
163 if (strcmp(name
, ut
->name
) == 0)
167 error("user_type_offset: couldn't find type (%s)\n", name
);
171 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
173 type
->typestring_offset
= offset
;
174 if (file
) type
->tfswrite
= FALSE
;
177 static void guard_rec(type_t
*type
)
179 /* types that contain references to themselves (like a linked list),
180 need to be shielded from infinite recursion when writing embedded
182 if (type
->typestring_offset
)
183 type
->tfswrite
= FALSE
;
185 type
->typestring_offset
= 1;
188 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
192 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
200 if (t
->kind
== TKIND_ALIAS
)
207 int is_user_type(const type_t
*t
)
209 return get_user_type(t
, NULL
) != NULL
;
212 static int is_embedded_complex(const type_t
*type
)
214 unsigned char tc
= type
->type
;
215 return is_struct(tc
) || is_union(tc
) || is_array(type
) || is_user_type(type
)
216 || (is_ptr(type
) && type
->ref
->type
== RPC_FC_IP
);
219 static int compare_expr(const expr_t
*a
, const expr_t
*b
)
223 if (a
->type
!= b
->type
)
224 return a
->type
- b
->type
;
231 return a
->u
.lval
- b
->u
.lval
;
233 return a
->u
.dval
- b
->u
.dval
;
234 case EXPR_IDENTIFIER
:
235 return strcmp(a
->u
.sval
, b
->u
.sval
);
237 ret
= compare_expr(a
->ref
, b
->ref
);
240 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
243 return compare_expr(a
->ext2
, b
->ext2
);
252 ret
= compare_expr(a
->ref
, b
->ref
);
255 return compare_expr(a
->u
.ext
, b
->u
.ext
);
261 return compare_expr(a
->ref
, b
->ref
);
268 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
271 fprintf(file, "/* %2u */\n", typestring_offset); \
272 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
276 static void print_file(FILE *file
, int indent
, const char *format
, ...)
279 va_start(va
, format
);
280 print(file
, indent
, format
, va
);
284 void print(FILE *file
, int indent
, const char *format
, va_list va
)
288 if (format
[0] != '\n')
291 vfprintf(file
, format
, va
);
295 void write_parameters_init(FILE *file
, int indent
, const func_t
*func
)
302 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
304 const type_t
*t
= var
->type
;
305 const char *n
= var
->name
;
306 if (decl_indirect(t
))
307 print_file(file
, indent
, "MIDL_memset(&%s, 0, sizeof %s);\n", n
, n
);
308 else if (is_ptr(t
) || is_array(t
))
309 print_file(file
, indent
, "%s = 0;\n", n
);
315 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
317 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
318 print_file(f
, indent
, "{\n");
319 print_file(f
, indent
+ 1, "short Pad;\n");
320 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
321 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
322 print_file(f
, indent
, "\n");
325 void write_formatstringsdecl(FILE *f
, int indent
, ifref_list_t
*ifaces
, int for_objects
)
327 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
328 get_size_typeformatstring(ifaces
, for_objects
));
330 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
331 get_size_procformatstring(ifaces
, for_objects
));
334 write_formatdesc(f
, indent
, "TYPE");
335 write_formatdesc(f
, indent
, "PROC");
337 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
338 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
339 print_file(f
, indent
, "\n");
342 static inline int is_base_type(unsigned char type
)
361 case RPC_FC_ERROR_STATUS_T
:
362 case RPC_FC_BIND_PRIMITIVE
:
370 int decl_indirect(const type_t
*t
)
372 return is_user_type(t
)
373 || (!is_base_type(t
->type
)
378 static size_t write_procformatstring_var(FILE *file
, int indent
,
379 const var_t
*var
, int is_return
)
382 const type_t
*type
= var
->type
;
384 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
385 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
387 if (!is_in
&& !is_out
) is_in
= TRUE
;
389 if (!type
->declarray
&& is_base_type(type
->type
))
392 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
394 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
396 if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
398 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
399 size
= 2; /* includes param type prefix */
401 else if (is_base_type(type
->type
))
403 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
404 size
= 2; /* includes param type prefix */
408 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
415 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
416 else if (is_in
&& is_out
)
417 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
419 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
421 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
423 print_file(file
, indent
, "0x01,\n");
424 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
425 size
= 4; /* includes param type prefix */
430 void write_procformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
432 const ifref_t
*iface
;
436 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
437 print_file(file
, indent
, "{\n");
439 print_file(file
, indent
, "0,\n");
440 print_file(file
, indent
, "{\n");
443 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
445 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
448 if (iface
->iface
->funcs
)
451 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
453 if (is_local(func
->def
->attrs
)) continue;
454 /* emit argument data */
457 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
458 write_procformatstring_var(file
, indent
, var
, FALSE
);
461 /* emit return value data */
463 if (is_void(var
->type
))
465 print_file(file
, indent
, "0x5b, /* FC_END */\n");
466 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
469 write_procformatstring_var(file
, indent
, var
, TRUE
);
474 print_file(file
, indent
, "0x0\n");
476 print_file(file
, indent
, "}\n");
478 print_file(file
, indent
, "};\n");
479 print_file(file
, indent
, "\n");
482 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
484 if (is_base_type(type
->type
))
486 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
487 *typestring_offset
+= 1;
494 /* write conformance / variance descriptor */
495 static size_t write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
496 unsigned int baseoff
, const expr_t
*expr
)
498 unsigned char operator_type
= 0;
499 const char *operator_string
= "no operators";
500 const expr_t
*subexpr
;
504 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
510 /* Top-level conformance calculations are done inline. */
511 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
512 RPC_FC_TOP_LEVEL_CONFORMANCE
);
513 print_file (file
, 2, "0x0,\n");
514 print_file (file
, 2, "NdrFcShort(0x0),\n");
520 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
521 error("write_conf_or_var_desc: constant value %ld is greater than "
522 "the maximum constant size of %d\n", expr
->cval
,
523 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
525 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
526 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
527 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
528 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
534 switch (subexpr
->type
)
537 subexpr
= subexpr
->ref
;
538 operator_type
= RPC_FC_DEREFERENCE
;
539 operator_string
= "FC_DEREFERENCE";
542 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
544 subexpr
= subexpr
->ref
;
545 operator_type
= RPC_FC_DIV_2
;
546 operator_string
= "FC_DIV_2";
550 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
552 subexpr
= subexpr
->ref
;
553 operator_type
= RPC_FC_MULT_2
;
554 operator_string
= "FC_MULT_2";
558 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
560 subexpr
= subexpr
->ref
;
561 operator_type
= RPC_FC_SUB_1
;
562 operator_string
= "FC_SUB_1";
566 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
568 subexpr
= subexpr
->ref
;
569 operator_type
= RPC_FC_ADD_1
;
570 operator_string
= "FC_ADD_1";
577 if (subexpr
->type
== EXPR_IDENTIFIER
)
579 const type_t
*correlation_variable
= NULL
;
580 unsigned char correlation_variable_type
;
581 unsigned char param_type
= 0;
585 if (structure
->fields
) LIST_FOR_EACH_ENTRY( var
, structure
->fields
, const var_t
, entry
)
587 unsigned int align
= 0;
588 /* FIXME: take alignment into account */
589 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
591 correlation_variable
= var
->type
;
594 offset
+= type_memsize(var
->type
, &align
);
596 if (!correlation_variable
)
597 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
601 correlation_variable_type
= correlation_variable
->type
;
603 switch (correlation_variable_type
)
607 param_type
= RPC_FC_SMALL
;
611 param_type
= RPC_FC_USMALL
;
616 param_type
= RPC_FC_SHORT
;
619 param_type
= RPC_FC_USHORT
;
623 param_type
= RPC_FC_LONG
;
626 param_type
= RPC_FC_ULONG
;
632 if (sizeof(void *) == 4) /* FIXME */
633 param_type
= RPC_FC_LONG
;
635 param_type
= RPC_FC_HYPER
;
638 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
639 correlation_variable_type
);
642 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
643 RPC_FC_NORMAL_CONFORMANCE
| param_type
, string_of_type(param_type
));
644 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
645 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
650 unsigned int callback_offset
= 0;
651 struct expr_eval_routine
*eval
;
654 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
656 if (!strcmp (eval
->structure
->name
, structure
->name
)
657 && !compare_expr (eval
->expr
, expr
))
667 eval
= xmalloc (sizeof(*eval
));
668 eval
->structure
= structure
;
670 list_add_tail (&expr_eval_routines
, &eval
->entry
);
673 if (callback_offset
> USHRT_MAX
)
674 error("Maximum number of callback routines reached\n");
676 print_file(file
, 2, "0x%x, /* Corr desc: */\n", RPC_FC_NORMAL_CONFORMANCE
);
677 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
678 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
683 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
685 int have_align
= FALSE
;
689 if (!fields
) return 0;
690 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
692 unsigned int falign
= 0;
693 size_t fsize
= type_memsize(v
->type
, &falign
);
699 size
= (size
+ (falign
- 1)) & ~(falign
- 1);
703 size
= (size
+ (*align
- 1)) & ~(*align
- 1);
707 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
709 size_t size
, maxs
= 0;
710 unsigned int align
= *pmaxa
;
713 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
715 /* we could have an empty default field with NULL type */
718 size
= type_memsize(v
->type
, &align
);
719 if (maxs
< size
) maxs
= size
;
720 if (*pmaxa
< align
) *pmaxa
= align
;
727 int get_padding(const var_list_t
*fields
)
729 unsigned short offset
= 0;
736 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
738 type_t
*ft
= f
->type
;
739 unsigned int align
= 0;
740 size_t size
= type_memsize(ft
, &align
);
743 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
747 return ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
750 size_t type_memsize(const type_t
*t
, unsigned int *align
)
754 if (t
->declarray
&& is_conformant_array(t
))
756 type_memsize(t
->ref
, align
);
759 else if (is_ptr(t
) || is_conformant_array(t
))
761 size
= sizeof(void *);
762 if (size
> *align
) *align
= size
;
764 else switch (t
->type
)
771 if (size
> *align
) *align
= size
;
778 if (size
> *align
) *align
= size
;
782 case RPC_FC_ERROR_STATUS_T
:
786 if (size
> *align
) *align
= size
;
791 if (size
> *align
) *align
= size
;
794 case RPC_FC_CVSTRUCT
:
795 case RPC_FC_CPSTRUCT
:
798 case RPC_FC_BOGUS_STRUCT
:
799 size
= fields_memsize(t
->fields
, align
);
801 case RPC_FC_ENCAPSULATED_UNION
:
802 case RPC_FC_NON_ENCAPSULATED_UNION
:
803 size
= union_memsize(t
->fields
, align
);
805 case RPC_FC_SMFARRAY
:
806 case RPC_FC_LGFARRAY
:
807 case RPC_FC_SMVARRAY
:
808 case RPC_FC_LGVARRAY
:
809 case RPC_FC_BOGUS_ARRAY
:
810 size
= t
->dim
* type_memsize(t
->ref
, align
);
813 error("type_memsize: Unknown type %d\n", t
->type
);
820 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
822 short absoff
= type
->ref
->typestring_offset
;
823 short reloff
= absoff
- (offset
+ 2);
824 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
826 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
827 type
->type
, ptr_attr
, string_of_type(type
->type
));
828 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
829 reloff
, reloff
, absoff
);
833 static unsigned char conf_string_type_of_char_type(unsigned char t
)
839 return RPC_FC_C_CSTRING
;
841 return RPC_FC_C_WSTRING
;
844 error("string_type_of_char_type: unrecognized type %d", t
);
848 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
851 = is_string_type(type
->attrs
, type
)
852 ? conf_string_type_of_char_type(type
->ref
->type
)
854 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
855 type
->type
, string_of_type(type
->type
));
856 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
857 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
861 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
863 unsigned int offset
= *typestring_offset
;
865 print_file(file
, 0, "/* %d */\n", offset
);
866 update_tfsoff(type
, offset
, file
);
868 if (type
->ref
->typestring_offset
)
869 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
870 else if (is_base_type(type
->ref
->type
))
871 *typestring_offset
+= write_simple_pointer(file
, type
);
876 static int processed(const type_t
*type
)
878 return type
->typestring_offset
&& !type
->tfswrite
;
881 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
883 unsigned int start
, absoff
, flags
;
884 unsigned int align
= 0, ualign
= 0;
886 type_t
*utype
= get_user_type(type
, &name
);
887 size_t usize
= type_memsize(utype
, &ualign
);
888 size_t size
= type_memsize(type
, &align
);
889 unsigned short funoff
= user_type_offset(name
);
894 if (is_base_type(utype
->type
))
897 print_file(file
, 0, "/* %d */\n", absoff
);
898 print_file(file
, 2, "0x%x,\t/* %s */\n", utype
->type
, string_of_type(utype
->type
));
899 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
904 if (!processed(utype
))
905 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
906 absoff
= utype
->typestring_offset
;
909 if (utype
->type
== RPC_FC_RP
)
911 else if (utype
->type
== RPC_FC_UP
)
917 update_tfsoff(type
, start
, file
);
918 print_file(file
, 0, "/* %d */\n", start
);
919 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
920 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
921 flags
| (align
- 1), align
- 1, flags
);
922 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
923 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize
, usize
);
924 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size
, size
);
926 reloff
= absoff
- *tfsoff
;
927 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff
, reloff
, absoff
);
931 static void write_member_type(FILE *file
, const type_t
*cont
,
932 const attr_list_t
*attrs
, const type_t
*type
,
933 unsigned int *corroff
, unsigned int *tfsoff
)
935 if (is_embedded_complex(type
))
940 if (is_union(type
->type
) && is_attr(attrs
, ATTR_SWITCHIS
))
947 absoff
= type
->typestring_offset
;
949 reloff
= absoff
- (*tfsoff
+ 2);
951 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
952 /* FIXME: actually compute necessary padding */
953 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
954 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
955 reloff
, reloff
, absoff
);
958 else if (is_ptr(type
))
960 unsigned char fc
= (cont
->type
== RPC_FC_BOGUS_STRUCT
963 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
966 else if (!write_base_type(file
, type
, tfsoff
))
967 error("Unsupported member type 0x%x\n", type
->type
);
970 static void write_end(FILE *file
, unsigned int *tfsoff
)
972 if (*tfsoff
% 2 == 0)
974 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
977 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
981 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
983 unsigned int offset
= 0;
984 var_list_t
*fs
= type
->fields
;
987 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
989 unsigned int align
= 0;
990 type_t
*ft
= f
->type
;
991 if (is_union(ft
->type
) && is_attr(f
->attrs
, ATTR_SWITCHIS
))
993 unsigned int absoff
= ft
->typestring_offset
;
994 short reloff
= absoff
- (*tfsoff
+ 6);
995 print_file(file
, 0, "/* %d */\n", *tfsoff
);
996 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
997 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
998 write_conf_or_var_desc(file
, current_structure
, offset
,
999 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1000 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1001 reloff
, reloff
, absoff
);
1005 /* FIXME: take alignment into account */
1006 offset
+= type_memsize(ft
, &align
);
1010 static int write_no_repeat_pointer_descriptions(
1011 FILE *file
, type_t
*type
,
1012 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1013 unsigned int *typestring_offset
)
1020 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1021 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1023 /* pointer instance */
1024 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1025 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1026 *typestring_offset
+= 6;
1028 if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1029 write_pointer_tfs(file
, type
, typestring_offset
);
1031 error("write_pointer_description: type format string unknown\n");
1034 *offset_in_memory
+= type_memsize(type
, &align
);
1035 /* FIXME: is there a case where these two are different? */
1037 *offset_in_buffer
+= type_memsize(type
, &align
);
1042 if (is_non_complex_struct(type
))
1045 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1046 written
+= write_no_repeat_pointer_descriptions(
1048 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1053 *offset_in_memory
+= type_memsize(type
, &align
);
1054 /* FIXME: is there a case where these two are different? */
1056 *offset_in_buffer
+= type_memsize(type
, &align
);
1062 static int write_pointer_description_offsets(
1063 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1064 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1065 unsigned int *typestring_offset
)
1070 if (is_ptr(type
) && type
->ref
->type
!= RPC_FC_IP
)
1072 if (offset_in_memory
&& offset_in_buffer
)
1074 /* pointer instance */
1075 /* FIXME: sometimes from end of structure, sometimes from beginning */
1076 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1077 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1080 *offset_in_memory
+= type_memsize(type
, &align
);
1081 /* FIXME: is there a case where these two are different? */
1083 *offset_in_buffer
+= type_memsize(type
, &align
);
1085 *typestring_offset
+= 4;
1087 if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1088 write_pointer_tfs(file
, type
, typestring_offset
);
1090 error("write_pointer_description_offsets: type format string unknown\n");
1097 return write_pointer_description_offsets(
1098 file
, attrs
, type
->ref
, offset_in_memory
, offset_in_buffer
,
1101 else if (is_non_complex_struct(type
))
1103 /* otherwise search for interesting fields to parse */
1105 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1107 written
+= write_pointer_description_offsets(
1108 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1115 if (offset_in_memory
)
1116 *offset_in_memory
+= type_memsize(type
, &align
);
1117 /* FIXME: is there a case where these two are different? */
1119 if (offset_in_buffer
)
1120 *offset_in_buffer
+= type_memsize(type
, &align
);
1126 /* Note: if file is NULL return value is number of pointers to write, else
1127 * it is the number of type format characters written */
1128 static int write_fixed_array_pointer_descriptions(
1129 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1130 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1131 unsigned int *typestring_offset
)
1134 int pointer_count
= 0;
1136 if (type
->type
== RPC_FC_SMFARRAY
|| type
->type
== RPC_FC_LGFARRAY
)
1138 unsigned int temp
= 0;
1139 /* unfortunately, this needs to be done in two passes to avoid
1140 * writing out redundant FC_FIXED_REPEAT descriptions */
1141 pointer_count
= write_pointer_description_offsets(
1142 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1143 if (pointer_count
> 0)
1145 unsigned int increment_size
;
1146 size_t offset_of_array_pointer_mem
= 0;
1147 size_t offset_of_array_pointer_buf
= 0;
1150 increment_size
= type_memsize(type
->ref
, &align
);
1152 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1153 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1154 print_file(file
, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type
->dim
, type
->dim
);
1155 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1156 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1157 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1158 *typestring_offset
+= 10;
1160 pointer_count
= write_pointer_description_offsets(
1161 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1162 &offset_of_array_pointer_buf
, typestring_offset
);
1165 else if (is_struct(type
->type
))
1168 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1170 pointer_count
+= write_fixed_array_pointer_descriptions(
1171 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1178 if (offset_in_memory
)
1179 *offset_in_memory
+= type_memsize(type
, &align
);
1180 /* FIXME: is there a case where these two are different? */
1182 if (offset_in_buffer
)
1183 *offset_in_buffer
+= type_memsize(type
, &align
);
1186 return pointer_count
;
1189 /* Note: if file is NULL return value is number of pointers to write, else
1190 * it is the number of type format characters written */
1191 static int write_conformant_array_pointer_descriptions(
1192 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1193 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1194 unsigned int *typestring_offset
)
1197 int pointer_count
= 0;
1199 if (is_conformant_array(type
) && !type
->length_is
)
1201 unsigned int temp
= 0;
1202 /* unfortunately, this needs to be done in two passes to avoid
1203 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1204 pointer_count
= write_pointer_description_offsets(
1205 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1206 if (pointer_count
> 0)
1208 unsigned int increment_size
;
1209 size_t offset_of_array_pointer_mem
= 0;
1210 size_t offset_of_array_pointer_buf
= 0;
1213 increment_size
= type_memsize(type
->ref
, &align
);
1215 if (increment_size
> USHRT_MAX
)
1216 error("array size of %u bytes is too large\n", increment_size
);
1218 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1219 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1220 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1221 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1222 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1223 *typestring_offset
+= 8;
1225 pointer_count
= write_pointer_description_offsets(
1226 file
, attrs
, type
->ref
, &offset_of_array_pointer_mem
,
1227 &offset_of_array_pointer_buf
, typestring_offset
);
1230 else if (is_struct(type
->type
))
1233 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1235 pointer_count
+= write_conformant_array_pointer_descriptions(
1236 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1243 if (offset_in_memory
)
1244 *offset_in_memory
+= type_memsize(type
, &align
);
1245 /* FIXME: is there a case where these two are different? */
1247 if (offset_in_buffer
)
1248 *offset_in_buffer
+= type_memsize(type
, &align
);
1251 return pointer_count
;
1254 /* Note: if file is NULL return value is number of pointers to write, else
1255 * it is the number of type format characters written */
1256 static int write_varying_array_pointer_descriptions(
1257 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1258 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1259 unsigned int *typestring_offset
)
1262 int pointer_count
= 0;
1264 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1266 if (is_array(type
) && type
->length_is
)
1268 unsigned int temp
= 0;
1269 /* unfortunately, this needs to be done in two passes to avoid
1270 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1271 pointer_count
= write_pointer_description_offsets(
1272 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1273 if (pointer_count
> 0)
1275 unsigned int increment_size
;
1276 size_t offset_of_array_pointer_mem
= 0;
1277 size_t offset_of_array_pointer_buf
= 0;
1280 increment_size
= type_memsize(type
->ref
, &align
);
1282 if (increment_size
> USHRT_MAX
)
1283 error("array size of %u bytes is too large\n", increment_size
);
1285 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1286 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1287 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1288 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1289 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1290 *typestring_offset
+= 8;
1292 pointer_count
= write_pointer_description_offsets(
1293 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1294 &offset_of_array_pointer_buf
, typestring_offset
);
1297 else if (is_struct(type
->type
))
1300 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1302 pointer_count
+= write_varying_array_pointer_descriptions(
1303 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1310 if (offset_in_memory
)
1311 *offset_in_memory
+= type_memsize(type
, &align
);
1312 /* FIXME: is there a case where these two are different? */
1314 if (offset_in_buffer
)
1315 *offset_in_buffer
+= type_memsize(type
, &align
);
1318 return pointer_count
;
1321 static void write_pointer_description(FILE *file
, type_t
*type
,
1322 unsigned int *typestring_offset
)
1324 size_t offset_in_buffer
;
1325 size_t offset_in_memory
;
1327 /* pass 1: search for single instance of a pointer (i.e. don't descend
1329 offset_in_memory
= 0;
1330 offset_in_buffer
= 0;
1331 write_no_repeat_pointer_descriptions(
1333 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1335 /* pass 2: search for pointers in fixed arrays */
1336 offset_in_memory
= 0;
1337 offset_in_buffer
= 0;
1338 write_fixed_array_pointer_descriptions(
1340 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1342 /* pass 3: search for pointers in conformant only arrays (but don't descend
1343 * into conformant varying or varying arrays) */
1344 offset_in_memory
= 0;
1345 offset_in_buffer
= 0;
1346 write_conformant_array_pointer_descriptions(
1348 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1350 /* pass 4: search for pointers in varying arrays */
1351 offset_in_memory
= 0;
1352 offset_in_buffer
= 0;
1353 write_varying_array_pointer_descriptions(
1355 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1358 static int is_declptr(const type_t
*t
)
1360 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1363 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1365 const char *name
, unsigned int *typestring_offset
)
1367 size_t start_offset
= *typestring_offset
;
1368 unsigned char rtype
;
1370 update_tfsoff(type
, start_offset
, file
);
1372 if (is_declptr(type
))
1374 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1375 int pointer_type
= is_ptr(type
) ? type
->type
: get_attrv(attrs
, ATTR_POINTERTYPE
);
1377 pointer_type
= RPC_FC_RP
;
1378 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1379 pointer_type
, flag
, string_of_type(pointer_type
),
1380 flag
? " [simple_pointer]" : "");
1381 *typestring_offset
+= 2;
1384 print_file(file
, 2, "NdrFcShort(0x2),\n");
1385 *typestring_offset
+= 2;
1387 rtype
= type
->ref
->type
;
1390 rtype
= type
->ref
->type
;
1392 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1394 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1395 return start_offset
;
1398 if (type
->declarray
&& !is_conformant_array(type
))
1400 /* FIXME: multi-dimensional array */
1401 if (0xffffuL
< type
->dim
)
1402 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1403 name
, 0xffffu
, type
->dim
- 0xffffu
);
1405 if (rtype
== RPC_FC_CHAR
)
1406 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1408 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1409 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1410 *typestring_offset
+= 2;
1412 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", type
->dim
, type
->dim
);
1413 *typestring_offset
+= 2;
1415 return start_offset
;
1417 else if (type
->size_is
)
1419 unsigned int align
= 0;
1421 if (rtype
== RPC_FC_CHAR
)
1422 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1424 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1425 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1426 *typestring_offset
+= 2;
1428 *typestring_offset
+= write_conf_or_var_desc(
1429 file
, current_structure
,
1430 (type
->declarray
&& current_structure
1431 ? type_memsize(current_structure
, &align
)
1435 return start_offset
;
1439 if (rtype
== RPC_FC_WCHAR
)
1440 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1442 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1443 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1444 *typestring_offset
+= 2;
1446 return start_offset
;
1450 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1451 const char *name
, unsigned int *typestring_offset
)
1453 const expr_t
*length_is
= type
->length_is
;
1454 const expr_t
*size_is
= type
->size_is
;
1455 unsigned int align
= 0;
1457 size_t start_offset
;
1459 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1460 unsigned int baseoff
1461 = type
->declarray
&& current_structure
1462 ? type_memsize(current_structure
, &align
)
1466 pointer_type
= RPC_FC_RP
;
1468 has_pointer
= FALSE
;
1469 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1473 size
= type_memsize(type
, &align
);
1474 if (size
== 0) /* conformant array */
1475 size
= type_memsize(type
->ref
, &align
);
1477 start_offset
= *typestring_offset
;
1478 update_tfsoff(type
, start_offset
, file
);
1479 print_file(file
, 0, "/* %lu */\n", start_offset
);
1480 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1481 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1482 *typestring_offset
+= 2;
1485 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1487 unsigned char tc
= type
->type
;
1489 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1491 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1492 *typestring_offset
+= 4;
1496 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1497 *typestring_offset
+= 2;
1500 if (is_conformant_array(type
))
1502 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1505 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1507 unsigned int elalign
= 0;
1508 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1510 if (type
->type
== RPC_FC_LGVARRAY
)
1512 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1513 *typestring_offset
+= 4;
1517 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1518 *typestring_offset
+= 2;
1521 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1522 *typestring_offset
+= 2;
1527 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1532 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1533 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1534 *typestring_offset
+= 2;
1535 write_pointer_description(file
, type
, typestring_offset
);
1536 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1537 *typestring_offset
+= 1;
1540 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1541 write_end(file
, typestring_offset
);
1545 unsigned int dim
= size_is
? 0 : type
->dim
;
1546 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim
, dim
);
1547 *typestring_offset
+= 2;
1549 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1552 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1554 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1555 write_end(file
, typestring_offset
);
1558 return start_offset
;
1561 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1563 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1564 const type_t
*ft
= last_field
->type
;
1566 if (ft
->declarray
&& is_conformant_array(ft
))
1569 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1570 return find_array_or_string_in_struct(last_field
->type
);
1575 static void write_struct_members(FILE *file
, const type_t
*type
,
1576 unsigned int *corroff
, unsigned int *typestring_offset
)
1579 unsigned short offset
= 0;
1583 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1585 type_t
*ft
= field
->type
;
1586 if (!ft
->declarray
|| !is_conformant_array(ft
))
1588 unsigned int align
= 0;
1589 size_t size
= type_memsize(ft
, &align
);
1592 if ((align
- 1) & offset
)
1594 unsigned char fc
= 0;
1598 fc
= RPC_FC_ALIGNM4
;
1601 fc
= RPC_FC_ALIGNM8
;
1604 error("write_struct_members: cannot align type %d", ft
->type
);
1606 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1607 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
1608 *typestring_offset
+= 1;
1610 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1616 padding
= ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
1619 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1620 RPC_FC_STRUCTPAD1
+ padding
- 1,
1622 *typestring_offset
+= 1;
1625 write_end(file
, typestring_offset
);
1628 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1629 const char *name
, unsigned int *tfsoff
)
1631 const type_t
*save_current_structure
= current_structure
;
1632 unsigned int total_size
;
1634 size_t start_offset
;
1635 size_t array_offset
;
1636 int has_pointers
= 0;
1637 unsigned int align
= 0;
1638 unsigned int corroff
;
1642 current_structure
= type
;
1644 total_size
= type_memsize(type
, &align
);
1645 if (total_size
> USHRT_MAX
)
1646 error("structure size for %s exceeds %d bytes by %d bytes\n",
1647 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1649 if (type
->fields
) LIST_FOR_EACH_ENTRY(f
, type
->fields
, var_t
, entry
)
1650 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1653 array
= find_array_or_string_in_struct(type
);
1654 if (array
&& !processed(array
->type
))
1656 = is_attr(array
->attrs
, ATTR_STRING
)
1657 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1658 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1661 write_descriptors(file
, type
, tfsoff
);
1663 start_offset
= *tfsoff
;
1664 update_tfsoff(type
, start_offset
, file
);
1665 print_file(file
, 0, "/* %d */\n", start_offset
);
1666 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1667 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1668 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1673 unsigned int absoff
= array
->type
->typestring_offset
;
1674 short reloff
= absoff
- *tfsoff
;
1675 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1676 reloff
, reloff
, absoff
);
1679 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1681 print_file(file
, 2, "NdrFcShort(0x0),\n");
1685 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1687 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1688 nothing is written to file yet. On the actual writing pass,
1689 this will have been updated. */
1690 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
1691 short reloff
= absoff
- *tfsoff
;
1692 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1693 reloff
, reloff
, absoff
);
1696 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1697 (type
->type
== RPC_FC_CPSTRUCT
) ||
1698 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1700 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1701 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1703 write_pointer_description(file
, type
, tfsoff
);
1704 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1708 write_struct_members(file
, type
, &corroff
, tfsoff
);
1710 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1712 const var_list_t
*fs
= type
->fields
;
1715 type
->ptrdesc
= *tfsoff
;
1716 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1718 type_t
*ft
= f
->type
;
1720 write_pointer_tfs(file
, ft
, tfsoff
);
1722 if (type
->ptrdesc
== *tfsoff
)
1726 current_structure
= save_current_structure
;
1727 return start_offset
;
1730 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1731 unsigned char flags
, size_t offset
,
1732 unsigned int *typeformat_offset
)
1734 size_t start_offset
= *typeformat_offset
;
1735 short reloff
= offset
- (*typeformat_offset
+ 2);
1736 int in_attr
, out_attr
;
1737 in_attr
= is_attr(attrs
, ATTR_IN
);
1738 out_attr
= is_attr(attrs
, ATTR_OUT
);
1739 if (!in_attr
&& !out_attr
) in_attr
= 1;
1741 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1744 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1747 string_of_type(pointer_type
));
1751 fprintf(file
, " [allocated_on_stack]");
1753 fprintf(file
, " [pointer_deref]");
1754 fprintf(file
, " */\n");
1757 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1758 *typeformat_offset
+= 4;
1760 return start_offset
;
1763 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1767 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1769 else if (is_base_type(t
->type
))
1771 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1772 t
->type
, string_of_type(t
->type
));
1774 else if (t
->typestring_offset
)
1776 short reloff
= t
->typestring_offset
- *tfsoff
;
1777 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1778 reloff
, reloff
, t
->typestring_offset
);
1781 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1786 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1788 unsigned int align
= 0;
1789 unsigned int start_offset
;
1790 size_t size
= type_memsize(type
, &align
);
1793 type_t
*deftype
= NULL
;
1794 short nodeftype
= 0xffff;
1799 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1801 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields
), const var_t
, entry
);
1802 fields
= uv
->type
->fields
;
1805 fields
= type
->fields
;
1807 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1809 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1811 nbranch
+= list_count(cases
);
1813 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
1816 start_offset
= *tfsoff
;
1817 update_tfsoff(type
, start_offset
, file
);
1818 print_file(file
, 0, "/* %d */\n", start_offset
);
1819 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1821 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields
), const var_t
, entry
);
1822 const type_t
*st
= sv
->type
;
1835 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1836 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
1837 0x40 | st
->type
, string_of_type(st
->type
));
1841 error("union switch type must be an integer, char, or enum\n");
1844 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1845 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1848 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1850 type_t
*ft
= f
->type
;
1851 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1852 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1855 if (cases
== NULL
&& !deflt
)
1856 error("union field %s with neither case nor default attribute\n", f
->name
);
1858 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1860 /* MIDL doesn't check for duplicate cases, even though that seems
1861 like a reasonable thing to do, it just dumps them to the TFS
1862 like we're going to do here. */
1863 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1865 write_branch_type(file
, ft
, tfsoff
);
1868 /* MIDL allows multiple default branches, even though that seems
1869 illogical, it just chooses the last one, which is what we will
1880 write_branch_type(file
, deftype
, tfsoff
);
1884 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
1888 return start_offset
;
1891 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1892 unsigned int *typeformat_offset
)
1895 size_t start_offset
= *typeformat_offset
;
1896 const var_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
1902 expr
.type
= EXPR_IDENTIFIER
;
1904 expr
.u
.sval
= iid
->name
;
1905 expr
.is_const
= FALSE
;
1906 print_file(file
, 2, "0x2f, /* FC_IP */\n");
1907 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
1908 *typeformat_offset
+= write_conf_or_var_desc(file
, NULL
, 0, &expr
) + 2;
1912 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1913 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
1916 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
1918 update_tfsoff(type
, start_offset
, file
);
1919 print_file(file
, 0, "/* %d */\n", start_offset
);
1920 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1921 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1922 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1923 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1924 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1925 for (i
= 0; i
< 8; ++i
)
1926 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1929 fprintf(file
, "\n");
1931 *typeformat_offset
+= 18;
1933 return start_offset
;
1936 static int get_ptr_attr(const type_t
*t
, int def_type
)
1940 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1943 if (t
->kind
!= TKIND_ALIAS
)
1949 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
1950 type_t
*type
, const var_t
*var
,
1951 unsigned int *typeformat_offset
)
1956 if (is_user_type(type
))
1958 write_user_tfs(file
, type
, typeformat_offset
);
1959 return type
->typestring_offset
;
1962 if (type
== var
->type
) /* top-level pointers */
1964 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1965 if (pointer_attr
!= 0 && !is_ptr(type
) && !is_array(type
))
1966 error("'%s': pointer attribute applied to non-pointer type\n", var
->name
);
1968 if (pointer_attr
== 0)
1969 pointer_attr
= get_ptr_attr(type
, RPC_FC_RP
);
1971 pointer_type
= pointer_attr
;
1974 pointer_type
= get_ptr_attr(type
, RPC_FC_UP
);
1976 if ((last_ptr(type
) || last_array(type
)) && is_ptrchain_attr(var
, ATTR_STRING
))
1977 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1982 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1983 if (pointer_type
!= RPC_FC_RP
)
1985 unsigned int absoff
= type
->typestring_offset
;
1986 short reloff
= absoff
- (*typeformat_offset
+ 2);
1987 off
= *typeformat_offset
;
1988 print_file(file
, 0, "/* %d */\n", off
);
1989 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", pointer_type
,
1990 string_of_type(pointer_type
));
1991 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1992 reloff
, reloff
, absoff
);
1993 *typeformat_offset
+= 4;
2000 /* basic types don't need a type format string */
2001 if (is_base_type(type
->type
))
2007 case RPC_FC_PSTRUCT
:
2008 case RPC_FC_CSTRUCT
:
2009 case RPC_FC_CPSTRUCT
:
2010 case RPC_FC_CVSTRUCT
:
2011 case RPC_FC_BOGUS_STRUCT
:
2012 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2013 case RPC_FC_ENCAPSULATED_UNION
:
2014 case RPC_FC_NON_ENCAPSULATED_UNION
:
2015 return write_union_tfs(file
, type
, typeformat_offset
);
2017 case RPC_FC_BIND_PRIMITIVE
:
2021 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
2024 else if (last_ptr(type
))
2026 size_t start_offset
= *typeformat_offset
;
2027 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2028 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2029 const type_t
*base
= type
->ref
;
2031 if (base
->type
== RPC_FC_IP
)
2033 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2036 /* special case for pointers to base types */
2037 if (is_base_type(base
->type
))
2039 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2040 pointer_type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2041 string_of_type(pointer_type
),
2042 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2043 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2044 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2045 *typeformat_offset
+= 4;
2046 return start_offset
;
2050 assert(is_ptr(type
));
2052 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2054 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2055 return write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
2056 !last_ptr(type
) ? 0x10 : 0,
2057 offset
, typeformat_offset
);
2060 static void set_tfswrite(type_t
*type
, int val
)
2062 while (type
->tfswrite
!= val
)
2064 type_t
*utype
= get_user_type(type
, NULL
);
2066 type
->tfswrite
= val
;
2069 set_tfswrite(utype
, val
);
2071 if (type
->kind
== TKIND_ALIAS
)
2073 else if (is_ptr(type
) || is_array(type
))
2080 LIST_FOR_EACH_ENTRY( v
, type
->fields
, var_t
, entry
)
2082 set_tfswrite(v
->type
, val
);
2090 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2091 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2095 if (is_user_type(type
))
2097 write_user_tfs(file
, type
, tfsoff
);
2099 else if (is_ptr(type
))
2101 type_t
*ref
= type
->ref
;
2103 if (ref
->type
== RPC_FC_IP
)
2105 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2109 if (!processed(ref
) && !is_base_type(ref
->type
))
2110 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2113 write_pointer_tfs(file
, type
, tfsoff
);
2118 else if (last_array(type
) && is_attr(attrs
, ATTR_STRING
))
2120 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2122 else if (type
->declarray
&& is_conformant_array(type
))
2123 ; /* conformant arrays and strings are handled specially */
2124 else if (is_array(type
))
2126 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2128 else if (is_struct(type
->type
))
2130 if (!processed(type
))
2131 write_struct_tfs(file
, type
, name
, tfsoff
);
2133 else if (is_union(type
->type
))
2135 if (!processed(type
))
2136 write_union_tfs(file
, type
, tfsoff
);
2138 else if (!is_base_type(type
->type
))
2139 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2145 static void set_all_tfswrite(const ifref_list_t
*ifaces
, int val
)
2147 const ifref_t
* iface
;
2152 LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2153 if (iface
->iface
->funcs
)
2154 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2156 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2157 set_tfswrite(var
->type
, val
);
2160 static size_t process_tfs(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2163 const ifref_t
*iface
;
2164 unsigned int typeformat_offset
= 2;
2166 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2168 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2171 if (iface
->iface
->funcs
)
2174 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2176 if (is_local(func
->def
->attrs
)) continue;
2178 current_func
= func
;
2180 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2183 write_typeformatstring_var(
2184 file
, 2, func
, var
->type
, var
,
2185 &typeformat_offset
),
2191 return typeformat_offset
+ 1;
2195 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2199 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2200 print_file(file
, indent
, "{\n");
2202 print_file(file
, indent
, "0,\n");
2203 print_file(file
, indent
, "{\n");
2205 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2207 set_all_tfswrite(ifaces
, TRUE
);
2208 process_tfs(file
, ifaces
, for_objects
);
2210 print_file(file
, indent
, "0x0\n");
2212 print_file(file
, indent
, "}\n");
2214 print_file(file
, indent
, "};\n");
2215 print_file(file
, indent
, "\n");
2218 static unsigned int get_required_buffer_size_type(
2219 const type_t
*type
, const char *name
, unsigned int *alignment
)
2222 if (is_user_type(type
))
2225 const type_t
*utype
= get_user_type(type
, &uname
);
2226 return get_required_buffer_size_type(utype
, uname
, alignment
);
2250 case RPC_FC_ERROR_STATUS_T
:
2260 case RPC_FC_BIND_PRIMITIVE
:
2264 case RPC_FC_PSTRUCT
:
2268 if (!type
->fields
) return 0;
2269 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2271 unsigned int alignment
;
2272 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2280 is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
2281 ? get_required_buffer_size_type( type
->ref
, name
, alignment
)
2284 case RPC_FC_SMFARRAY
:
2285 case RPC_FC_LGFARRAY
:
2286 return type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2294 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2296 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2297 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2299 if (!in_attr
&& !out_attr
)
2304 if (pass
== PASS_OUT
)
2306 if (out_attr
&& is_ptr(var
->type
))
2308 type_t
*type
= var
->type
;
2310 if (type
->type
== RPC_FC_STRUCT
)
2313 unsigned int size
= 36;
2315 if (!type
->fields
) return size
;
2316 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2319 size
+= get_required_buffer_size_type(
2320 field
->type
, field
->name
, &align
);
2329 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2330 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2332 if (is_ptr(var
->type
))
2334 type_t
*type
= var
->type
;
2336 if (is_base_type(type
->type
))
2340 else if (type
->type
== RPC_FC_STRUCT
)
2342 unsigned int size
= 36;
2345 if (!type
->fields
) return size
;
2346 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2349 size
+= get_required_buffer_size_type(
2350 field
->type
, field
->name
, &align
);
2357 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2361 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2364 unsigned int total_size
= 0, alignment
;
2368 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2370 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2371 total_size
+= alignment
;
2375 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
))
2377 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
2378 total_size
+= alignment
;
2383 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2384 enum remoting_phase phase
,
2385 const var_t
*var
, unsigned int type_offset
)
2387 const char *function
;
2390 case PHASE_BUFFERSIZE
:
2391 function
= "BufferSize";
2394 function
= "Marshall";
2396 case PHASE_UNMARSHAL
:
2397 function
= "Unmarshall";
2407 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2409 print_file(file
, indent
, "&_StubMsg,\n");
2410 print_file(file
, indent
, "%s%s%s%s,\n",
2411 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2412 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2413 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2415 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2416 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2417 if (phase
== PHASE_UNMARSHAL
)
2418 print_file(file
, indent
, "0);\n");
2422 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2423 enum pass pass
, const var_t
*var
,
2424 const char *varname
)
2426 type_t
*type
= var
->type
;
2428 unsigned int alignment
= 0;
2429 unsigned char rtype
;
2431 /* no work to do for other phases, buffer sizing is done elsewhere */
2432 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2435 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2459 case RPC_FC_ERROR_STATUS_T
:
2471 case RPC_FC_BIND_PRIMITIVE
:
2472 /* no marshalling needed */
2476 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2480 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2481 alignment
- 1, alignment
- 1);
2483 if (phase
== PHASE_MARSHAL
)
2485 print_file(file
, indent
, "*(");
2486 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2488 fprintf(file
, " *)_StubMsg.Buffer = *");
2490 fprintf(file
, " *)_StubMsg.Buffer = ");
2491 fprintf(file
, varname
);
2492 fprintf(file
, ";\n");
2494 else if (phase
== PHASE_UNMARSHAL
)
2496 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2497 print_file(file
, indent
, "");
2499 print_file(file
, indent
, "*");
2500 fprintf(file
, varname
);
2501 if (pass
== PASS_IN
&& is_ptr(type
))
2502 fprintf(file
, " = (");
2504 fprintf(file
, " = *(");
2505 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2506 fprintf(file
, " *)_StubMsg.Buffer;\n");
2509 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2510 write_type(file
, var
->type
, FALSE
, NULL
);
2511 fprintf(file
, ");\n");
2514 /* returns whether the MaxCount, Offset or ActualCount members need to be
2515 * filled in for the specified phase */
2516 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2518 return (phase
!= PHASE_UNMARSHAL
);
2521 static int needs_freeing(const attr_list_t
*attrs
, const type_t
*t
, int out
)
2526 && (t
->ref
->type
== RPC_FC_IP
2527 || is_ptr(t
->ref
))))
2528 || (out
&& (is_string_type(attrs
, t
)
2532 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2533 enum pass pass
, enum remoting_phase phase
)
2535 int in_attr
, out_attr
, pointer_type
;
2541 if (phase
== PHASE_BUFFERSIZE
)
2543 unsigned int size
= get_function_buffer_size( func
, pass
);
2544 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2547 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2549 const type_t
*type
= var
->type
;
2550 unsigned char rtype
;
2551 size_t start_offset
= type
->typestring_offset
;
2553 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2555 pointer_type
= RPC_FC_RP
;
2557 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2558 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2559 if (!in_attr
&& !out_attr
)
2562 if (phase
== PHASE_FREE
)
2564 if (!needs_freeing(var
->attrs
, type
, out_attr
))
2571 if (!in_attr
) continue;
2574 if (!out_attr
) continue;
2582 if (is_user_type(var
->type
))
2584 print_phase_function(file
, indent
, "UserMarshal", phase
, var
, start_offset
);
2586 else if (is_string_type(var
->attrs
, var
->type
))
2588 if (is_array(type
) && !is_conformant_array(type
))
2589 print_phase_function(file
, indent
, "NonConformantString", phase
, var
, start_offset
);
2592 if (type
->size_is
&& is_size_needed_for_phase(phase
))
2594 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2595 write_expr(file
, type
->size_is
, 1);
2596 fprintf(file
, ";\n");
2599 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
2600 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2602 print_phase_function(file
, indent
, "ConformantString", phase
, var
,
2603 start_offset
+ (type
->size_is
? 4 : 2));
2606 else if (is_array(type
))
2608 unsigned char tc
= type
->type
;
2609 const char *array_type
= "FixedArray";
2611 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2613 if (is_size_needed_for_phase(phase
))
2615 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2616 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2617 write_expr(file
, type
->length_is
, 1);
2618 fprintf(file
, ";\n\n");
2620 array_type
= "VaryingArray";
2622 else if (tc
== RPC_FC_CARRAY
)
2624 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
2626 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2627 write_expr(file
, type
->size_is
, 1);
2628 fprintf(file
, ";\n\n");
2630 array_type
= "ConformantArray";
2632 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
2634 if (is_size_needed_for_phase(phase
))
2638 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2639 write_expr(file
, type
->size_is
, 1);
2640 fprintf(file
, ";\n");
2642 if (type
->length_is
)
2644 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2645 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2646 write_expr(file
, type
->length_is
, 1);
2647 fprintf(file
, ";\n\n");
2650 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
2652 : "ConformantVaryingArray");
2655 if (!in_attr
&& phase
== PHASE_FREE
)
2657 print_file(file
, indent
, "if (%s)\n", var
->name
);
2659 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2661 else if (phase
!= PHASE_FREE
)
2663 const char *t
= pointer_type
== RPC_FC_RP
? array_type
: "Pointer";
2664 print_phase_function(file
, indent
, t
, phase
, var
, start_offset
);
2667 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2669 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2671 else if (!is_ptr(var
->type
))
2676 case RPC_FC_PSTRUCT
:
2677 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
);
2679 case RPC_FC_CSTRUCT
:
2680 case RPC_FC_CPSTRUCT
:
2681 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
, start_offset
);
2683 case RPC_FC_CVSTRUCT
:
2684 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
, start_offset
);
2686 case RPC_FC_BOGUS_STRUCT
:
2687 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
, start_offset
);
2690 if (is_base_type( var
->type
->ref
->type
))
2692 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2694 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2696 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2697 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2702 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2703 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2704 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2708 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2713 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2715 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2717 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2719 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2720 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2725 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2726 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2727 if (var
->type
->ref
->type
== RPC_FC_IP
)
2728 print_phase_function(file
, indent
, "InterfacePointer", phase
, var
, start_offset
);
2730 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2733 fprintf(file
, "\n");
2738 size_t get_size_procformatstring_var(const var_t
*var
)
2740 return write_procformatstring_var(NULL
, 0, var
, FALSE
);
2744 size_t get_size_procformatstring_func(const func_t
*func
)
2749 /* argument list size */
2751 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2752 size
+= get_size_procformatstring_var(var
);
2754 /* return value size */
2755 if (is_void(func
->def
->type
))
2756 size
+= 2; /* FC_END and FC_PAD */
2758 size
+= get_size_procformatstring_var(func
->def
);
2763 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2765 const ifref_t
*iface
;
2769 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2771 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2774 if (iface
->iface
->funcs
)
2775 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2776 if (!is_local(func
->def
->attrs
))
2777 size
+= get_size_procformatstring_func( func
);
2782 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2784 set_all_tfswrite(ifaces
, FALSE
);
2785 return process_tfs(NULL
, ifaces
, for_objects
);
2788 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2789 const var_list_t
*fields
, const char *structvar
)
2795 fprintf(h
, "%lu", e
->u
.lval
);
2798 fprintf(h
, "0x%lx", e
->u
.lval
);
2801 fprintf(h
, "%#.15g", e
->u
.dval
);
2803 case EXPR_TRUEFALSE
:
2805 fprintf(h
, "FALSE");
2809 case EXPR_IDENTIFIER
:
2812 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2813 if (!strcmp(e
->u
.sval
, field
->name
))
2815 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2819 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2824 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2828 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2832 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2836 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2838 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2841 fprintf(h
, "sizeof(");
2842 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2853 if (brackets
) fprintf(h
, "(");
2854 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2856 case EXPR_SHL
: fprintf(h
, " << "); break;
2857 case EXPR_SHR
: fprintf(h
, " >> "); break;
2858 case EXPR_MUL
: fprintf(h
, " * "); break;
2859 case EXPR_DIV
: fprintf(h
, " / "); break;
2860 case EXPR_ADD
: fprintf(h
, " + "); break;
2861 case EXPR_SUB
: fprintf(h
, " - "); break;
2862 case EXPR_AND
: fprintf(h
, " & "); break;
2863 case EXPR_OR
: fprintf(h
, " | "); break;
2866 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2867 if (brackets
) fprintf(h
, ")");
2870 if (brackets
) fprintf(h
, "(");
2871 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2873 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2875 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2876 if (brackets
) fprintf(h
, ")");
2882 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2884 int in_attr
, out_attr
;
2886 const var_t
*def
= func
->def
;
2889 /* declare return value '_RetVal' */
2890 if (!is_void(def
->type
))
2892 print_file(file
, indent
, "");
2893 write_type_left(file
, def
->type
);
2894 fprintf(file
, " _RetVal;\n");
2900 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2902 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2904 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2905 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2906 if (!out_attr
&& !in_attr
)
2909 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
2911 print_file(file
, indent
, "");
2912 write_type(file
, var
->type
->ref
, FALSE
, "_W%u", i
++);
2913 fprintf(file
, ";\n");
2916 print_file(file
, indent
, "");
2917 write_type_left(file
, var
->type
);
2919 if (var
->type
->declarray
) {
2920 fprintf(file
, "( *");
2921 write_name(file
, var
);
2922 fprintf(file
, " )");
2924 write_name(file
, var
);
2925 write_type_right(file
, var
->type
, FALSE
);
2926 fprintf(file
, ";\n");
2928 if (decl_indirect(var
->type
))
2929 print_file(file
, indent
, "void *_p_%s = &%s;\n",
2930 var
->name
, var
->name
);
2935 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2937 int in_attr
, out_attr
;
2944 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2946 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2947 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2948 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2949 if (!out_attr
&& !in_attr
)
2954 print_file(file
, indent
, "");
2955 write_name(file
, var
);
2957 if (var
->type
->size_is
)
2959 unsigned int size
, align
= 0;
2960 type_t
*type
= var
->type
;
2962 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2963 for ( ; type
->size_is
; type
= type
->ref
)
2965 write_expr(file
, type
->size_is
, TRUE
);
2966 fprintf(file
, " * ");
2968 size
= type_memsize(type
, &align
);
2969 fprintf(file
, "%u);\n", size
);
2971 else if (!is_string
)
2973 fprintf(file
, " = &_W%u;\n", i
);
2974 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
2975 print_file(file
, indent
, "_W%u = 0;\n", i
);
2983 fprintf(file
, "\n");
2987 int write_expr_eval_routines(FILE *file
, const char *iface
)
2989 static const char *var_name
= "pS";
2991 struct expr_eval_routine
*eval
;
2992 unsigned short callback_offset
= 0;
2994 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2996 const char *name
= eval
->structure
->name
;
2997 const var_list_t
*fields
= eval
->structure
->fields
;
3001 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3002 iface
, name
, callback_offset
);
3003 print_file(file
, 0, "{\n");
3004 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3005 name
, var_name
, name
, type_memsize (eval
->structure
, &align
));
3006 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3007 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
3008 write_struct_expr(file
, eval
->expr
, 1, fields
, var_name
);
3009 fprintf(file
, ";\n");
3010 print_file(file
, 0, "}\n\n");
3016 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3018 struct expr_eval_routine
*eval
;
3019 struct expr_eval_routine
*cursor
;
3020 unsigned short callback_offset
= 0;
3022 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3023 fprintf(file
, "{\n");
3025 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3027 const char *name
= eval
->structure
->name
;
3028 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3030 list_remove(&eval
->entry
);
3034 fprintf(file
, "};\n\n");
3037 void write_user_quad_list(FILE *file
)
3041 if (list_empty(&user_type_list
))
3044 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3045 fprintf(file
, "{\n");
3046 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3048 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3049 print_file(file
, 1, "{\n");
3050 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3051 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3052 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3053 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3054 print_file(file
, 1, "}%s\n", sep
);
3056 fprintf(file
, "};\n\n");
3059 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3061 const struct str_list_entry_t
*endpoint
;
3064 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3065 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3066 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3068 print_file( f
, 1, "{ (const unsigned char *)\"" );
3069 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3071 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3074 if (!*p
) goto error
;
3075 if (p
[1] != '[') goto error
;
3077 fprintf( f
, "\", (const unsigned char *)\"" );
3078 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3080 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3083 if (*p
!= ']') goto error
;
3084 fprintf( f
, "\" },\n" );
3086 print_file( f
, 0, "};\n\n" );
3090 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);