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"
39 #include "wine/list.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const func_t
*current_func
;
50 static const type_t
*current_structure
;
51 static const type_t
*current_iface
;
53 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
54 struct expr_eval_routine
57 const type_t
*structure
;
62 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
);
63 static size_t write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
64 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
65 const char *name
, int write_ptr
, unsigned int *tfsoff
);
66 static const var_t
*find_array_or_string_in_struct(const type_t
*type
);
67 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
69 const char *name
, unsigned int *typestring_offset
);
71 const char *string_of_type(unsigned char type
)
75 case RPC_FC_BYTE
: return "FC_BYTE";
76 case RPC_FC_CHAR
: return "FC_CHAR";
77 case RPC_FC_SMALL
: return "FC_SMALL";
78 case RPC_FC_USMALL
: return "FC_USMALL";
79 case RPC_FC_WCHAR
: return "FC_WCHAR";
80 case RPC_FC_SHORT
: return "FC_SHORT";
81 case RPC_FC_USHORT
: return "FC_USHORT";
82 case RPC_FC_LONG
: return "FC_LONG";
83 case RPC_FC_ULONG
: return "FC_ULONG";
84 case RPC_FC_FLOAT
: return "FC_FLOAT";
85 case RPC_FC_HYPER
: return "FC_HYPER";
86 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
87 case RPC_FC_ENUM16
: return "FC_ENUM16";
88 case RPC_FC_ENUM32
: return "FC_ENUM32";
89 case RPC_FC_IGNORE
: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP
: return "FC_RP";
92 case RPC_FC_UP
: return "FC_UP";
93 case RPC_FC_OP
: return "FC_OP";
94 case RPC_FC_FP
: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT
: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY
: return "FC_CARRAY";
108 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
112 case RPC_FC_POINTER
: return "FC_POINTER";
113 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING
: return "FC_CSTRING";
116 case RPC_FC_WSTRING
: return "FC_WSTRING";
118 error("string_of_type: unknown type 0x%02x\n", type
);
123 int is_struct(unsigned char type
)
130 case RPC_FC_CPSTRUCT
:
131 case RPC_FC_CVSTRUCT
:
132 case RPC_FC_BOGUS_STRUCT
:
139 static int is_non_complex_struct(const type_t
*type
)
146 case RPC_FC_CPSTRUCT
:
147 case RPC_FC_CVSTRUCT
:
154 int is_union(unsigned char type
)
158 case RPC_FC_ENCAPSULATED_UNION
:
159 case RPC_FC_NON_ENCAPSULATED_UNION
:
166 static int type_has_pointers(const type_t
*type
)
168 if (is_user_type(type
))
170 else if (is_ptr(type
))
172 else if (is_array(type
))
173 return type_has_pointers(type
->ref
);
174 else if (is_struct(type
->type
))
177 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
179 if (type_has_pointers(field
->type
))
183 else if (is_union(type
->type
))
187 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
189 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
190 fields
= uv
->type
->fields_or_args
;
193 fields
= type
->fields_or_args
;
194 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
196 if (field
->type
&& type_has_pointers(field
->type
))
204 static int type_has_full_pointer(const type_t
*type
)
206 if (is_user_type(type
))
208 else if (type
->type
== RPC_FC_FP
)
210 else if (is_ptr(type
))
212 else if (is_array(type
))
213 return type_has_full_pointer(type
->ref
);
214 else if (is_struct(type
->type
))
217 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
219 if (type_has_full_pointer(field
->type
))
223 else if (is_union(type
->type
))
227 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
229 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
230 fields
= uv
->type
->fields_or_args
;
233 fields
= type
->fields_or_args
;
234 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
236 if (field
->type
&& type_has_full_pointer(field
->type
))
244 static unsigned short user_type_offset(const char *name
)
247 unsigned short off
= 0;
248 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
250 if (strcmp(name
, ut
->name
) == 0)
254 error("user_type_offset: couldn't find type (%s)\n", name
);
258 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
260 type
->typestring_offset
= offset
;
261 if (file
) type
->tfswrite
= FALSE
;
264 static void guard_rec(type_t
*type
)
266 /* types that contain references to themselves (like a linked list),
267 need to be shielded from infinite recursion when writing embedded
269 if (type
->typestring_offset
)
270 type
->tfswrite
= FALSE
;
272 type
->typestring_offset
= 1;
275 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
279 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
287 if (t
->kind
== TKIND_ALIAS
)
294 int is_user_type(const type_t
*t
)
296 return get_user_type(t
, NULL
) != NULL
;
299 static int is_embedded_complex(const type_t
*type
)
301 unsigned char tc
= type
->type
;
302 return is_struct(tc
) || is_union(tc
) || is_array(type
) || is_user_type(type
)
303 || (is_ptr(type
) && type
->ref
->type
== RPC_FC_IP
);
306 static const char *get_context_handle_type_name(const type_t
*type
)
309 for (t
= type
; is_ptr(t
); t
= t
->ref
)
310 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
316 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
319 fprintf(file, "/* %2u */\n", typestring_offset); \
320 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
324 static void print_file(FILE *file
, int indent
, const char *format
, ...)
327 va_start(va
, format
);
328 print(file
, indent
, format
, va
);
332 void print(FILE *file
, int indent
, const char *format
, va_list va
)
336 if (format
[0] != '\n')
339 vfprintf(file
, format
, va
);
344 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
)
346 if (decl_indirect(t
))
347 print_file(file
, indent
, "MIDL_memset(&%s, 0, sizeof(%s));\n", n
, n
);
348 else if (is_ptr(t
) || is_array(t
))
349 print_file(file
, indent
, "%s = 0;\n", n
);
352 void write_parameters_init(FILE *file
, int indent
, const func_t
*func
)
356 if (!is_void(get_func_return_type(func
)))
357 write_var_init(file
, indent
, get_func_return_type(func
), "_RetVal");
362 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
363 write_var_init(file
, indent
, var
->type
, var
->name
);
368 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
370 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
371 print_file(f
, indent
, "{\n");
372 print_file(f
, indent
+ 1, "short Pad;\n");
373 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
374 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
375 print_file(f
, indent
, "\n");
378 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
380 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
381 get_size_typeformatstring(stmts
, pred
));
383 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
384 get_size_procformatstring(stmts
, pred
));
387 write_formatdesc(f
, indent
, "TYPE");
388 write_formatdesc(f
, indent
, "PROC");
390 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
391 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
392 print_file(f
, indent
, "\n");
395 static inline int is_base_type(unsigned char type
)
414 case RPC_FC_ERROR_STATUS_T
:
415 case RPC_FC_BIND_PRIMITIVE
:
423 int decl_indirect(const type_t
*t
)
425 return is_user_type(t
)
426 || (!is_base_type(t
->type
)
431 static size_t write_procformatstring_type(FILE *file
, int indent
,
434 const attr_list_t
*attrs
,
439 int is_in
= is_attr(attrs
, ATTR_IN
);
440 int is_out
= is_attr(attrs
, ATTR_OUT
);
442 if (!is_in
&& !is_out
) is_in
= TRUE
;
444 if (!type
->declarray
&& is_base_type(type
->type
))
447 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
449 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
451 if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
453 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
454 size
= 2; /* includes param type prefix */
456 else if (is_base_type(type
->type
))
458 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
459 size
= 2; /* includes param type prefix */
463 error("Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
470 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
471 else if (is_in
&& is_out
)
472 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
474 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
476 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
478 print_file(file
, indent
, "0x01,\n");
479 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
480 size
= 4; /* includes param type prefix */
485 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
487 const statement_t
*stmt
;
488 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
490 if (stmt
->type
== STMT_TYPE
&& stmt
->u
.type
->type
== RPC_FC_IP
)
493 if (!pred(stmt
->u
.type
))
495 if (stmt
->u
.type
->funcs
) LIST_FOR_EACH_ENTRY( func
, stmt
->u
.type
->funcs
, const func_t
, entry
)
497 if (is_local(func
->def
->attrs
)) continue;
498 /* emit argument data */
502 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
503 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
506 /* emit return value data */
507 if (is_void(get_func_return_type(func
)))
509 print_file(file
, indent
, "0x5b, /* FC_END */\n");
510 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
513 write_procformatstring_type(file
, indent
, "return value", get_func_return_type(func
), NULL
, TRUE
);
516 else if (stmt
->type
== STMT_LIBRARY
)
517 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
521 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
525 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
526 print_file(file
, indent
, "{\n");
528 print_file(file
, indent
, "0,\n");
529 print_file(file
, indent
, "{\n");
532 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
534 print_file(file
, indent
, "0x0\n");
536 print_file(file
, indent
, "}\n");
538 print_file(file
, indent
, "};\n");
539 print_file(file
, indent
, "\n");
542 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
544 if (is_base_type(type
->type
))
546 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
547 *typestring_offset
+= 1;
554 /* write conformance / variance descriptor */
555 static size_t write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
556 unsigned int baseoff
, const type_t
*type
,
559 unsigned char operator_type
= 0;
560 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
561 const char *conftype_string
= "";
562 const char *operator_string
= "no operators";
563 const expr_t
*subexpr
;
567 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
573 /* Top-level conformance calculations are done inline. */
574 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
575 RPC_FC_TOP_LEVEL_CONFORMANCE
);
576 print_file (file
, 2, "0x0,\n");
577 print_file (file
, 2, "NdrFcShort(0x0),\n");
583 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
584 error("write_conf_or_var_desc: constant value %ld is greater than "
585 "the maximum constant size of %d\n", expr
->cval
,
586 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
588 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
589 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
590 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
591 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
596 if (is_ptr(type
) || (is_array(type
) && !type
->declarray
))
598 conftype
= RPC_FC_POINTER_CONFORMANCE
;
599 conftype_string
= "field pointer, ";
603 switch (subexpr
->type
)
606 subexpr
= subexpr
->ref
;
607 operator_type
= RPC_FC_DEREFERENCE
;
608 operator_string
= "FC_DEREFERENCE";
611 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
613 subexpr
= subexpr
->ref
;
614 operator_type
= RPC_FC_DIV_2
;
615 operator_string
= "FC_DIV_2";
619 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
621 subexpr
= subexpr
->ref
;
622 operator_type
= RPC_FC_MULT_2
;
623 operator_string
= "FC_MULT_2";
627 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
629 subexpr
= subexpr
->ref
;
630 operator_type
= RPC_FC_SUB_1
;
631 operator_string
= "FC_SUB_1";
635 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
637 subexpr
= subexpr
->ref
;
638 operator_type
= RPC_FC_ADD_1
;
639 operator_string
= "FC_ADD_1";
646 if (subexpr
->type
== EXPR_IDENTIFIER
)
648 const type_t
*correlation_variable
= NULL
;
649 unsigned char correlation_variable_type
;
650 unsigned char param_type
= 0;
654 if (structure
->fields_or_args
) LIST_FOR_EACH_ENTRY( var
, structure
->fields_or_args
, const var_t
, entry
)
656 unsigned int align
= 0;
657 /* FIXME: take alignment into account */
658 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
660 correlation_variable
= var
->type
;
663 offset
+= type_memsize(var
->type
, &align
);
665 if (!correlation_variable
)
666 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
669 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
672 correlation_variable_type
= correlation_variable
->type
;
674 switch (correlation_variable_type
)
678 param_type
= RPC_FC_SMALL
;
682 param_type
= RPC_FC_USMALL
;
687 param_type
= RPC_FC_SHORT
;
690 param_type
= RPC_FC_USHORT
;
694 param_type
= RPC_FC_LONG
;
697 param_type
= RPC_FC_ULONG
;
700 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
701 correlation_variable_type
);
704 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
705 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
706 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
707 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
712 unsigned int callback_offset
= 0;
713 struct expr_eval_routine
*eval
;
716 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
718 if (!strcmp (eval
->structure
->name
, structure
->name
)
719 && !compare_expr (eval
->expr
, expr
))
729 eval
= xmalloc (sizeof(*eval
));
730 eval
->structure
= structure
;
731 eval
->baseoff
= baseoff
;
733 list_add_tail (&expr_eval_routines
, &eval
->entry
);
736 if (callback_offset
> USHRT_MAX
)
737 error("Maximum number of callback routines reached\n");
739 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
740 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
741 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
746 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
748 int have_align
= FALSE
;
752 if (!fields
) return 0;
753 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
755 unsigned int falign
= 0;
756 size_t fsize
= type_memsize(v
->type
, &falign
);
762 size
= ROUND_SIZE(size
, falign
);
766 size
= ROUND_SIZE(size
, *align
);
770 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
772 size_t size
, maxs
= 0;
773 unsigned int align
= *pmaxa
;
776 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
778 /* we could have an empty default field with NULL type */
781 size
= type_memsize(v
->type
, &align
);
782 if (maxs
< size
) maxs
= size
;
783 if (*pmaxa
< align
) *pmaxa
= align
;
790 int get_padding(const var_list_t
*fields
)
792 unsigned short offset
= 0;
799 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
801 type_t
*ft
= f
->type
;
802 unsigned int align
= 0;
803 size_t size
= type_memsize(ft
, &align
);
806 offset
= ROUND_SIZE(offset
, align
);
810 return ROUNDING(offset
, salign
);
813 size_t type_memsize(const type_t
*t
, unsigned int *align
)
817 if (t
->kind
== TKIND_ALIAS
)
818 size
= type_memsize(t
->orig
, align
);
819 else if (t
->declarray
&& is_conformant_array(t
))
821 type_memsize(t
->ref
, align
);
824 else if (is_ptr(t
) || is_conformant_array(t
))
826 size
= sizeof(void *);
827 if (size
> *align
) *align
= size
;
829 else switch (t
->type
)
836 if (size
> *align
) *align
= size
;
843 if (size
> *align
) *align
= size
;
847 case RPC_FC_ERROR_STATUS_T
:
851 if (size
> *align
) *align
= size
;
856 if (size
> *align
) *align
= size
;
859 case RPC_FC_CVSTRUCT
:
860 case RPC_FC_CPSTRUCT
:
863 case RPC_FC_BOGUS_STRUCT
:
864 size
= fields_memsize(t
->fields_or_args
, align
);
866 case RPC_FC_ENCAPSULATED_UNION
:
867 case RPC_FC_NON_ENCAPSULATED_UNION
:
868 size
= union_memsize(t
->fields_or_args
, align
);
870 case RPC_FC_SMFARRAY
:
871 case RPC_FC_LGFARRAY
:
872 case RPC_FC_SMVARRAY
:
873 case RPC_FC_LGVARRAY
:
874 case RPC_FC_BOGUS_ARRAY
:
875 size
= t
->dim
* type_memsize(t
->ref
, align
);
878 error("type_memsize: Unknown type %d\n", t
->type
);
885 int is_full_pointer_function(const func_t
*func
)
888 if (type_has_full_pointer(get_func_return_type(func
)))
892 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
893 if (type_has_full_pointer( var
->type
))
898 void write_full_pointer_init(FILE *file
, int indent
, const func_t
*func
, int is_server
)
900 print_file(file
, indent
, "_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
901 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
905 void write_full_pointer_free(FILE *file
, int indent
, const func_t
*func
)
907 print_file(file
, indent
, "NdrFullPointerXlatFree(_StubMsg.FullPtrXlatTables);\n");
911 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
913 short absoff
= type
->ref
->typestring_offset
;
914 short reloff
= absoff
- (offset
+ 2);
915 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
917 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
918 type
->type
, ptr_attr
, string_of_type(type
->type
));
919 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
920 reloff
, reloff
, absoff
);
924 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
926 unsigned char fc
= type
->ref
->type
;
927 /* for historical reasons, write_simple_pointer also handled string types,
928 * but no longer does. catch bad uses of the function with this check */
929 if (is_string_type(type
->attrs
, type
))
930 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
931 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
932 type
->type
, string_of_type(type
->type
));
933 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
934 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
938 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
940 print_file(file
, 0, "/* %u (", tfsoff
);
941 write_type_decl(file
, t
, NULL
);
942 print_file(file
, 0, ") */\n");
945 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
947 unsigned int offset
= *typestring_offset
;
949 print_start_tfs_comment(file
, type
, offset
);
950 update_tfsoff(type
, offset
, file
);
952 if (type
->ref
->typestring_offset
)
953 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
954 else if (is_base_type(type
->ref
->type
))
955 *typestring_offset
+= write_simple_pointer(file
, type
);
960 static int processed(const type_t
*type
)
962 return type
->typestring_offset
&& !type
->tfswrite
;
965 static int user_type_has_variable_size(const type_t
*t
)
974 case RPC_FC_CPSTRUCT
:
975 case RPC_FC_CVSTRUCT
:
978 /* Note: Since this only applies to user types, we can't have a conformant
979 array here, and strings should get filed under pointer in this case. */
983 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
985 unsigned int start
, absoff
, flags
;
986 unsigned int align
= 0, ualign
= 0;
988 type_t
*utype
= get_user_type(type
, &name
);
989 size_t usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
990 size_t size
= type_memsize(type
, &align
);
991 unsigned short funoff
= user_type_offset(name
);
996 if (is_base_type(utype
->type
))
999 print_start_tfs_comment(file
, utype
, absoff
);
1000 print_file(file
, 2, "0x%x,\t/* %s */\n", utype
->type
, string_of_type(utype
->type
));
1001 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1006 if (!processed(utype
))
1007 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1008 absoff
= utype
->typestring_offset
;
1011 if (utype
->type
== RPC_FC_RP
)
1013 else if (utype
->type
== RPC_FC_UP
)
1019 update_tfsoff(type
, start
, file
);
1020 print_start_tfs_comment(file
, type
, start
);
1021 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1022 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1023 flags
| (align
- 1), align
- 1, flags
);
1024 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1025 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size
, size
);
1026 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize
, usize
);
1028 reloff
= absoff
- *tfsoff
;
1029 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff
, reloff
, absoff
);
1033 static void write_member_type(FILE *file
, const type_t
*cont
,
1034 const attr_list_t
*attrs
, const type_t
*type
,
1035 unsigned int *corroff
, unsigned int *tfsoff
)
1037 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1042 if (is_union(type
->type
) && is_attr(attrs
, ATTR_SWITCHIS
))
1049 absoff
= type
->typestring_offset
;
1051 reloff
= absoff
- (*tfsoff
+ 2);
1053 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1054 /* FIXME: actually compute necessary padding */
1055 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1056 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1057 reloff
, reloff
, absoff
);
1060 else if (is_ptr(type
) || is_conformant_array(type
))
1062 unsigned char fc
= (cont
->type
== RPC_FC_BOGUS_STRUCT
1065 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1068 else if (!write_base_type(file
, type
, tfsoff
))
1069 error("Unsupported member type 0x%x\n", type
->type
);
1072 static void write_end(FILE *file
, unsigned int *tfsoff
)
1074 if (*tfsoff
% 2 == 0)
1076 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1079 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1083 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1085 unsigned int offset
= 0;
1086 var_list_t
*fs
= type
->fields_or_args
;
1089 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1091 unsigned int align
= 0;
1092 type_t
*ft
= f
->type
;
1093 if (is_union(ft
->type
) && is_attr(f
->attrs
, ATTR_SWITCHIS
))
1095 unsigned int absoff
= ft
->typestring_offset
;
1096 short reloff
= absoff
- (*tfsoff
+ 6);
1097 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1098 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
1099 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1100 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1101 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1102 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1103 reloff
, reloff
, absoff
);
1107 /* FIXME: take alignment into account */
1108 offset
+= type_memsize(ft
, &align
);
1112 static int write_no_repeat_pointer_descriptions(
1113 FILE *file
, type_t
*type
,
1114 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1115 unsigned int *typestring_offset
)
1120 if (is_ptr(type
) || (!type
->declarray
&& is_conformant_array(type
)))
1124 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1125 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1127 /* pointer instance */
1128 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1129 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1130 *typestring_offset
+= 6;
1134 if (is_string_type(type
->attrs
, type
))
1135 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1137 write_pointer_tfs(file
, type
, typestring_offset
);
1141 unsigned absoff
= type
->typestring_offset
;
1142 short reloff
= absoff
- (*typestring_offset
+ 2);
1143 /* FIXME: get pointer attributes from field */
1144 print_file(file
, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP
, "FC_UP");
1145 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1146 reloff
, reloff
, absoff
);
1147 *typestring_offset
+= 4;
1151 memsize
= type_memsize(type
, &align
);
1152 *offset_in_memory
+= memsize
;
1153 /* increment these separately as in the case of conformant (varying)
1154 * structures these start at different values */
1155 *offset_in_buffer
+= memsize
;
1160 if (is_non_complex_struct(type
))
1163 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1165 if (offset_in_memory
&& offset_in_buffer
)
1169 type_memsize(v
->type
, &align
);
1170 padding
= ROUNDING(*offset_in_memory
, align
);
1171 *offset_in_memory
+= padding
;
1172 *offset_in_buffer
+= padding
;
1174 written
+= write_no_repeat_pointer_descriptions(
1176 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1183 memsize
= type_memsize(type
, &align
);
1184 *offset_in_memory
+= memsize
;
1185 /* increment these separately as in the case of conformant (varying)
1186 * structures these start at different values */
1187 *offset_in_buffer
+= memsize
;
1193 static int write_pointer_description_offsets(
1194 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1195 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1196 unsigned int *typestring_offset
)
1201 if (is_ptr(type
) && type
->ref
->type
!= RPC_FC_IP
)
1203 if (offset_in_memory
&& offset_in_buffer
)
1207 /* pointer instance */
1208 /* FIXME: sometimes from end of structure, sometimes from beginning */
1209 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1210 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1213 memsize
= type_memsize(type
, &align
);
1214 *offset_in_memory
+= memsize
;
1215 /* increment these separately as in the case of conformant (varying)
1216 * structures these start at different values */
1217 *offset_in_buffer
+= memsize
;
1219 *typestring_offset
+= 4;
1221 if (is_string_type(attrs
, type
))
1222 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1223 else if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1224 write_pointer_tfs(file
, type
, typestring_offset
);
1226 error("write_pointer_description_offsets: type format string unknown\n");
1233 return write_pointer_description_offsets(
1234 file
, attrs
, type
->ref
, offset_in_memory
, offset_in_buffer
,
1237 else if (is_non_complex_struct(type
))
1239 /* otherwise search for interesting fields to parse */
1241 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1243 if (offset_in_memory
&& offset_in_buffer
)
1247 type_memsize(v
->type
, &align
);
1248 padding
= ROUNDING(*offset_in_memory
, align
);
1249 *offset_in_memory
+= padding
;
1250 *offset_in_buffer
+= padding
;
1252 written
+= write_pointer_description_offsets(
1253 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1259 if (offset_in_memory
&& offset_in_buffer
)
1263 memsize
= type_memsize(type
, &align
);
1264 *offset_in_memory
+= memsize
;
1265 /* increment these separately as in the case of conformant (varying)
1266 * structures these start at different values */
1267 *offset_in_buffer
+= memsize
;
1274 /* Note: if file is NULL return value is number of pointers to write, else
1275 * it is the number of type format characters written */
1276 static int write_fixed_array_pointer_descriptions(
1277 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1278 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1279 unsigned int *typestring_offset
)
1282 int pointer_count
= 0;
1284 if (type
->type
== RPC_FC_SMFARRAY
|| type
->type
== RPC_FC_LGFARRAY
)
1286 unsigned int temp
= 0;
1287 /* unfortunately, this needs to be done in two passes to avoid
1288 * writing out redundant FC_FIXED_REPEAT descriptions */
1289 pointer_count
= write_pointer_description_offsets(
1290 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1291 if (pointer_count
> 0)
1293 unsigned int increment_size
;
1294 size_t offset_of_array_pointer_mem
= 0;
1295 size_t offset_of_array_pointer_buf
= 0;
1298 increment_size
= type_memsize(type
->ref
, &align
);
1300 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1301 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1302 print_file(file
, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type
->dim
, type
->dim
);
1303 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1304 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1305 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1306 *typestring_offset
+= 10;
1308 pointer_count
= write_pointer_description_offsets(
1309 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1310 &offset_of_array_pointer_buf
, typestring_offset
);
1313 else if (is_struct(type
->type
))
1316 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1318 if (offset_in_memory
&& offset_in_buffer
)
1322 type_memsize(v
->type
, &align
);
1323 padding
= ROUNDING(*offset_in_memory
, align
);
1324 *offset_in_memory
+= padding
;
1325 *offset_in_buffer
+= padding
;
1327 pointer_count
+= write_fixed_array_pointer_descriptions(
1328 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1334 if (offset_in_memory
&& offset_in_buffer
)
1338 memsize
= type_memsize(type
, &align
);
1339 *offset_in_memory
+= memsize
;
1340 /* increment these separately as in the case of conformant (varying)
1341 * structures these start at different values */
1342 *offset_in_buffer
+= memsize
;
1346 return pointer_count
;
1349 /* Note: if file is NULL return value is number of pointers to write, else
1350 * it is the number of type format characters written */
1351 static int write_conformant_array_pointer_descriptions(
1352 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1353 size_t offset_in_memory
, unsigned int *typestring_offset
)
1356 int pointer_count
= 0;
1358 if (is_conformant_array(type
) && !type
->length_is
)
1360 unsigned int temp
= 0;
1361 /* unfortunately, this needs to be done in two passes to avoid
1362 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1363 pointer_count
= write_pointer_description_offsets(
1364 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1365 if (pointer_count
> 0)
1367 unsigned int increment_size
;
1368 size_t offset_of_array_pointer_mem
= offset_in_memory
;
1369 size_t offset_of_array_pointer_buf
= offset_in_memory
;
1372 increment_size
= type_memsize(type
->ref
, &align
);
1374 if (increment_size
> USHRT_MAX
)
1375 error("array size of %u bytes is too large\n", increment_size
);
1377 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1378 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1379 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1380 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1381 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1382 *typestring_offset
+= 8;
1384 pointer_count
= write_pointer_description_offsets(
1385 file
, attrs
, type
->ref
, &offset_of_array_pointer_mem
,
1386 &offset_of_array_pointer_buf
, typestring_offset
);
1390 return pointer_count
;
1393 /* Note: if file is NULL return value is number of pointers to write, else
1394 * it is the number of type format characters written */
1395 static int write_varying_array_pointer_descriptions(
1396 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1397 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1398 unsigned int *typestring_offset
)
1401 int pointer_count
= 0;
1403 if (is_array(type
) && type
->length_is
)
1405 unsigned int temp
= 0;
1406 /* unfortunately, this needs to be done in two passes to avoid
1407 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1408 pointer_count
= write_pointer_description_offsets(
1409 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1410 if (pointer_count
> 0)
1412 unsigned int increment_size
;
1415 increment_size
= type_memsize(type
->ref
, &align
);
1417 if (increment_size
> USHRT_MAX
)
1418 error("array size of %u bytes is too large\n", increment_size
);
1420 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1421 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1422 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1423 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1424 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1425 *typestring_offset
+= 8;
1427 pointer_count
= write_pointer_description_offsets(
1428 file
, attrs
, type
, offset_in_memory
,
1429 offset_in_buffer
, typestring_offset
);
1432 else if (is_struct(type
->type
))
1435 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1437 if (offset_in_memory
&& offset_in_buffer
)
1441 type_memsize(v
->type
, &align
);
1442 padding
= ROUNDING(*offset_in_memory
, align
);
1443 *offset_in_memory
+= padding
;
1444 *offset_in_buffer
+= padding
;
1446 pointer_count
+= write_varying_array_pointer_descriptions(
1447 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1453 if (offset_in_memory
&& offset_in_buffer
)
1457 memsize
= type_memsize(type
, &align
);
1458 *offset_in_memory
+= memsize
;
1459 /* increment these separately as in the case of conformant (varying)
1460 * structures these start at different values */
1461 *offset_in_buffer
+= memsize
;
1465 return pointer_count
;
1468 static void write_pointer_description(FILE *file
, type_t
*type
,
1469 unsigned int *typestring_offset
)
1471 size_t offset_in_buffer
;
1472 size_t offset_in_memory
;
1473 size_t conformance
= 0;
1475 if (type
->type
== RPC_FC_CVSTRUCT
)
1477 else if (type
->type
== RPC_FC_CSTRUCT
|| type
->type
== RPC_FC_CPSTRUCT
)
1480 /* pass 1: search for single instance of a pointer (i.e. don't descend
1482 if (!is_array(type
))
1484 offset_in_memory
= 0;
1485 offset_in_buffer
= conformance
;
1486 write_no_repeat_pointer_descriptions(
1488 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1491 /* pass 2: search for pointers in fixed arrays */
1492 offset_in_memory
= 0;
1493 offset_in_buffer
= conformance
;
1494 write_fixed_array_pointer_descriptions(
1496 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1498 /* pass 3: search for pointers in conformant only arrays (but don't descend
1499 * into conformant varying or varying arrays) */
1500 if ((!type
->declarray
|| !current_structure
) && is_conformant_array(type
))
1501 write_conformant_array_pointer_descriptions(
1502 file
, NULL
, type
, 0, typestring_offset
);
1503 else if (type
->type
== RPC_FC_CPSTRUCT
)
1505 unsigned int align
= 0;
1506 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1507 write_conformant_array_pointer_descriptions(
1509 type_memsize(type
, &align
),
1513 /* pass 4: search for pointers in varying arrays */
1514 offset_in_memory
= 0;
1515 offset_in_buffer
= conformance
;
1516 write_varying_array_pointer_descriptions(
1518 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1521 int is_declptr(const type_t
*t
)
1523 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1526 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1528 const char *name
, unsigned int *typestring_offset
)
1530 size_t start_offset
;
1531 unsigned char rtype
;
1533 if (is_declptr(type
))
1535 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1536 int pointer_type
= is_ptr(type
) ? type
->type
: get_attrv(attrs
, ATTR_POINTERTYPE
);
1538 pointer_type
= RPC_FC_RP
;
1539 print_start_tfs_comment(file
, type
, *typestring_offset
);
1540 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1541 pointer_type
, flag
, string_of_type(pointer_type
),
1542 flag
? " [simple_pointer]" : "");
1543 *typestring_offset
+= 2;
1546 print_file(file
, 2, "NdrFcShort(0x2),\n");
1547 *typestring_offset
+= 2;
1551 start_offset
= *typestring_offset
;
1552 update_tfsoff(type
, start_offset
, file
);
1554 rtype
= type
->ref
->type
;
1556 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1558 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1559 return start_offset
;
1562 if (type
->declarray
&& !is_conformant_array(type
))
1564 /* FIXME: multi-dimensional array */
1565 if (0xffffuL
< type
->dim
)
1566 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1567 name
, 0xffffu
, type
->dim
- 0xffffu
);
1569 if (rtype
== RPC_FC_CHAR
)
1570 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1572 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1573 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1574 *typestring_offset
+= 2;
1576 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", type
->dim
, type
->dim
);
1577 *typestring_offset
+= 2;
1579 return start_offset
;
1581 else if (type
->size_is
)
1583 unsigned int align
= 0;
1585 if (rtype
== RPC_FC_CHAR
)
1586 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1588 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1589 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1590 *typestring_offset
+= 2;
1592 *typestring_offset
+= write_conf_or_var_desc(
1593 file
, current_structure
,
1594 (type
->declarray
&& current_structure
1595 ? type_memsize(current_structure
, &align
)
1597 type
, type
->size_is
);
1599 return start_offset
;
1603 if (rtype
== RPC_FC_WCHAR
)
1604 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1606 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1607 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1608 *typestring_offset
+= 2;
1610 return start_offset
;
1614 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1615 const char *name
, unsigned int *typestring_offset
)
1617 const expr_t
*length_is
= type
->length_is
;
1618 const expr_t
*size_is
= type
->size_is
;
1619 unsigned int align
= 0;
1621 size_t start_offset
;
1623 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1624 unsigned int baseoff
1625 = type
->declarray
&& current_structure
1626 ? type_memsize(current_structure
, &align
)
1630 pointer_type
= RPC_FC_RP
;
1632 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1635 has_pointer
= type_has_pointers(type
->ref
);
1638 size
= type_memsize((is_conformant_array(type
) ? type
->ref
: type
), &align
);
1640 start_offset
= *typestring_offset
;
1641 update_tfsoff(type
, start_offset
, file
);
1642 print_start_tfs_comment(file
, type
, start_offset
);
1643 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1644 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1645 *typestring_offset
+= 2;
1648 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1650 unsigned char tc
= type
->type
;
1652 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1654 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1655 *typestring_offset
+= 4;
1659 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1660 *typestring_offset
+= 2;
1663 if (is_conformant_array(type
))
1665 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1668 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1670 unsigned int elalign
= 0;
1671 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1673 if (type
->type
== RPC_FC_LGVARRAY
)
1675 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1676 *typestring_offset
+= 4;
1680 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1681 *typestring_offset
+= 2;
1684 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1685 *typestring_offset
+= 2;
1690 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1693 if (has_pointer
&& (!type
->declarray
|| !current_structure
))
1695 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1696 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1697 *typestring_offset
+= 2;
1698 write_pointer_description(file
, type
, typestring_offset
);
1699 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1700 *typestring_offset
+= 1;
1703 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1704 write_end(file
, typestring_offset
);
1708 unsigned int dim
= size_is
? 0 : type
->dim
;
1709 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim
, dim
);
1710 *typestring_offset
+= 2;
1712 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1715 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1717 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1718 write_end(file
, typestring_offset
);
1721 return start_offset
;
1724 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1726 const var_t
*last_field
;
1729 if (!type
->fields_or_args
|| list_empty(type
->fields_or_args
))
1732 last_field
= LIST_ENTRY( list_tail(type
->fields_or_args
), const var_t
, entry
);
1733 ft
= last_field
->type
;
1735 if (ft
->declarray
&& is_conformant_array(ft
))
1738 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1739 return find_array_or_string_in_struct(ft
);
1744 static void write_struct_members(FILE *file
, const type_t
*type
,
1745 unsigned int *corroff
, unsigned int *typestring_offset
)
1748 unsigned short offset
= 0;
1752 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
1754 type_t
*ft
= field
->type
;
1755 if (!ft
->declarray
|| !is_conformant_array(ft
))
1757 unsigned int align
= 0;
1758 size_t size
= type_memsize(ft
, &align
);
1761 if ((align
- 1) & offset
)
1763 unsigned char fc
= 0;
1767 fc
= RPC_FC_ALIGNM4
;
1770 fc
= RPC_FC_ALIGNM8
;
1773 error("write_struct_members: cannot align type %d\n", ft
->type
);
1775 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1776 offset
= ROUND_SIZE(offset
, align
);
1777 *typestring_offset
+= 1;
1779 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1785 padding
= ROUNDING(offset
, salign
);
1788 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1789 RPC_FC_STRUCTPAD1
+ padding
- 1,
1791 *typestring_offset
+= 1;
1794 write_end(file
, typestring_offset
);
1797 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1798 const char *name
, unsigned int *tfsoff
)
1800 const type_t
*save_current_structure
= current_structure
;
1801 unsigned int total_size
;
1803 size_t start_offset
;
1804 size_t array_offset
;
1805 int has_pointers
= 0;
1806 unsigned int align
= 0;
1807 unsigned int corroff
;
1811 current_structure
= type
;
1813 total_size
= type_memsize(type
, &align
);
1814 if (total_size
> USHRT_MAX
)
1815 error("structure size for %s exceeds %d bytes by %d bytes\n",
1816 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1818 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY(f
, type
->fields_or_args
, var_t
, entry
)
1819 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1821 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
1823 array
= find_array_or_string_in_struct(type
);
1824 if (array
&& !processed(array
->type
))
1826 = is_attr(array
->attrs
, ATTR_STRING
)
1827 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1828 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1831 write_descriptors(file
, type
, tfsoff
);
1833 start_offset
= *tfsoff
;
1834 update_tfsoff(type
, start_offset
, file
);
1835 print_start_tfs_comment(file
, type
, start_offset
);
1836 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1837 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1838 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1843 unsigned int absoff
= array
->type
->typestring_offset
;
1844 short reloff
= absoff
- *tfsoff
;
1845 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1846 reloff
, reloff
, absoff
);
1849 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1851 print_file(file
, 2, "NdrFcShort(0x0),\n");
1855 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1857 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1858 nothing is written to file yet. On the actual writing pass,
1859 this will have been updated. */
1860 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
1861 short reloff
= absoff
- *tfsoff
;
1862 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1863 reloff
, reloff
, absoff
);
1866 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1867 (type
->type
== RPC_FC_CPSTRUCT
) ||
1868 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1870 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1871 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1873 write_pointer_description(file
, type
, tfsoff
);
1874 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1878 write_struct_members(file
, type
, &corroff
, tfsoff
);
1880 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1882 const var_list_t
*fs
= type
->fields_or_args
;
1885 type
->ptrdesc
= *tfsoff
;
1886 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1888 type_t
*ft
= f
->type
;
1891 if (is_string_type(f
->attrs
, ft
))
1892 write_string_tfs(file
, f
->attrs
, ft
, f
->name
, tfsoff
);
1894 write_pointer_tfs(file
, ft
, tfsoff
);
1896 else if (!ft
->declarray
&& is_conformant_array(ft
))
1898 unsigned int absoff
= ft
->typestring_offset
;
1899 short reloff
= absoff
- (*tfsoff
+ 2);
1900 int ptr_type
= get_attrv(f
->attrs
, ATTR_POINTERTYPE
);
1901 /* FIXME: We need to store pointer attributes for arrays
1902 so we don't lose pointer_default info. */
1904 ptr_type
= RPC_FC_UP
;
1905 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1906 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
1907 string_of_type(ptr_type
));
1908 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1909 reloff
, reloff
, absoff
);
1913 if (type
->ptrdesc
== *tfsoff
)
1917 current_structure
= save_current_structure
;
1918 return start_offset
;
1921 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1922 unsigned char flags
, size_t offset
,
1923 unsigned int *typeformat_offset
)
1925 size_t start_offset
= *typeformat_offset
;
1926 short reloff
= offset
- (*typeformat_offset
+ 2);
1927 int in_attr
, out_attr
;
1928 in_attr
= is_attr(attrs
, ATTR_IN
);
1929 out_attr
= is_attr(attrs
, ATTR_OUT
);
1930 if (!in_attr
&& !out_attr
) in_attr
= 1;
1932 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1935 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1938 string_of_type(pointer_type
));
1942 fprintf(file
, " [allocated_on_stack]");
1944 fprintf(file
, " [pointer_deref]");
1945 fprintf(file
, " */\n");
1948 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1949 *typeformat_offset
+= 4;
1951 return start_offset
;
1954 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1958 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1960 else if (is_base_type(t
->type
))
1962 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1963 t
->type
, string_of_type(t
->type
));
1965 else if (t
->typestring_offset
)
1967 short reloff
= t
->typestring_offset
- *tfsoff
;
1968 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1969 reloff
, reloff
, t
->typestring_offset
);
1972 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1977 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1979 unsigned int align
= 0;
1980 unsigned int start_offset
;
1981 size_t size
= type_memsize(type
, &align
);
1984 type_t
*deftype
= NULL
;
1985 short nodeftype
= 0xffff;
1990 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1992 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
1993 fields
= uv
->type
->fields_or_args
;
1996 fields
= type
->fields_or_args
;
1998 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2000 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2002 nbranch
+= list_count(cases
);
2004 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2007 start_offset
= *tfsoff
;
2008 update_tfsoff(type
, start_offset
, file
);
2009 print_start_tfs_comment(file
, type
, start_offset
);
2010 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
2012 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields_or_args
), const var_t
, entry
);
2013 const type_t
*st
= sv
->type
;
2026 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
2027 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2028 0x40 | st
->type
, string_of_type(st
->type
));
2032 error("union switch type must be an integer, char, or enum\n");
2035 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
2036 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
2039 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2041 type_t
*ft
= f
->type
;
2042 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2043 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2046 if (cases
== NULL
&& !deflt
)
2047 error("union field %s with neither case nor default attribute\n", f
->name
);
2049 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2051 /* MIDL doesn't check for duplicate cases, even though that seems
2052 like a reasonable thing to do, it just dumps them to the TFS
2053 like we're going to do here. */
2054 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
2056 write_branch_type(file
, ft
, tfsoff
);
2059 /* MIDL allows multiple default branches, even though that seems
2060 illogical, it just chooses the last one, which is what we will
2071 write_branch_type(file
, deftype
, tfsoff
);
2075 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
2079 return start_offset
;
2082 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2083 unsigned int *typeformat_offset
)
2086 size_t start_offset
= *typeformat_offset
;
2087 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2091 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2092 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2094 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2098 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
2099 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2102 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2104 update_tfsoff(type
, start_offset
, file
);
2105 print_start_tfs_comment(file
, type
, start_offset
);
2106 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2107 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2108 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
2109 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2110 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2111 for (i
= 0; i
< 8; ++i
)
2112 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2115 fprintf(file
, "\n");
2117 *typeformat_offset
+= 18;
2119 return start_offset
;
2122 static size_t write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2124 unsigned int *typeformat_offset
)
2126 size_t start_offset
= *typeformat_offset
;
2127 unsigned char flags
= 0;
2129 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2130 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2134 if (is_attr(var
->attrs
, ATTR_IN
))
2137 if (!is_attr(var
->attrs
, ATTR_OUT
))
2138 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2140 if (is_attr(var
->attrs
, ATTR_OUT
))
2143 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2144 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2145 /* return and can't be null values overlap */
2146 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2147 print_file(file
, 0, "can't be null, ");
2148 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2149 print_file(file
, 0, "serialize, ");
2150 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2151 print_file(file
, 0, "no serialize, ");
2152 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2153 print_file(file
, 0, "strict, ");
2154 if ((flags
& 0x21) == 0x20)
2155 print_file(file
, 0, "out, ");
2156 if ((flags
& 0x21) == 0x21)
2157 print_file(file
, 0, "return, ");
2159 print_file(file
, 0, "in, ");
2161 print_file(file
, 0, "via ptr, ");
2162 print_file(file
, 0, "*/\n");
2163 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2164 print_file(file
, 2, "0, /* FIXME: param num */\n");
2165 *typeformat_offset
+= 4;
2167 return start_offset
;
2170 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
2171 type_t
*type
, const var_t
*var
,
2172 unsigned int *typeformat_offset
)
2176 if (is_context_handle(type
))
2177 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2179 if (is_user_type(type
))
2181 write_user_tfs(file
, type
, typeformat_offset
);
2182 return type
->typestring_offset
;
2185 if (is_string_type(var
->attrs
, type
))
2186 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2192 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2193 ptr_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2194 /* Top level pointers to conformant arrays may be handled specially
2195 since we can bypass the pointer, but if the array is buried
2196 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2197 always need to write the pointer. */
2198 if (!ptr_type
&& var
->type
!= type
)
2199 /* FIXME: This should use pointer_default, but the information
2200 isn't kept around for arrays. */
2201 ptr_type
= RPC_FC_UP
;
2202 if (ptr_type
&& ptr_type
!= RPC_FC_RP
)
2204 unsigned int absoff
= type
->typestring_offset
;
2205 short reloff
= absoff
- (*typeformat_offset
+ 2);
2206 off
= *typeformat_offset
;
2207 print_file(file
, 0, "/* %d */\n", off
);
2208 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2209 string_of_type(ptr_type
));
2210 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2211 reloff
, reloff
, absoff
);
2212 *typeformat_offset
+= 4;
2219 /* basic types don't need a type format string */
2220 if (is_base_type(type
->type
))
2226 case RPC_FC_PSTRUCT
:
2227 case RPC_FC_CSTRUCT
:
2228 case RPC_FC_CPSTRUCT
:
2229 case RPC_FC_CVSTRUCT
:
2230 case RPC_FC_BOGUS_STRUCT
:
2231 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2232 case RPC_FC_ENCAPSULATED_UNION
:
2233 case RPC_FC_NON_ENCAPSULATED_UNION
:
2234 return write_union_tfs(file
, type
, typeformat_offset
);
2236 case RPC_FC_BIND_PRIMITIVE
:
2240 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
2243 else if (last_ptr(type
))
2245 size_t start_offset
= *typeformat_offset
;
2246 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2247 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2248 const type_t
*base
= type
->ref
;
2250 if (base
->type
== RPC_FC_IP
2252 && is_attr(var
->attrs
, ATTR_IIDIS
)))
2254 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2257 /* special case for pointers to base types */
2258 if (is_base_type(base
->type
))
2260 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2261 type
->type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2262 string_of_type(type
->type
),
2263 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2264 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2265 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2266 *typeformat_offset
+= 4;
2267 return start_offset
;
2271 assert(is_ptr(type
));
2273 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2275 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2276 return write_pointer_only_tfs(file
, var
->attrs
, type
->type
,
2277 !last_ptr(type
) ? 0x10 : 0,
2278 offset
, typeformat_offset
);
2281 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2282 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2286 if (is_user_type(type
))
2288 write_user_tfs(file
, type
, tfsoff
);
2290 else if (is_string_type(attrs
, type
))
2292 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2294 else if (is_ptr(type
))
2296 type_t
*ref
= type
->ref
;
2298 if (ref
->type
== RPC_FC_IP
2300 && is_attr(attrs
, ATTR_IIDIS
)))
2302 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2306 if (!processed(ref
) && !is_base_type(ref
->type
))
2307 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2310 write_pointer_tfs(file
, type
, tfsoff
);
2315 else if (type
->declarray
&& is_conformant_array(type
))
2316 ; /* conformant arrays and strings are handled specially */
2317 else if (is_array(type
))
2319 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2320 if (is_conformant_array(type
))
2323 else if (is_struct(type
->type
))
2325 if (!processed(type
))
2326 write_struct_tfs(file
, type
, name
, tfsoff
);
2328 else if (is_union(type
->type
))
2330 if (!processed(type
))
2331 write_union_tfs(file
, type
, tfsoff
);
2333 else if (!is_base_type(type
->type
))
2334 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2340 static size_t process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2341 type_pred_t pred
, unsigned int *typeformat_offset
)
2344 const statement_t
*stmt
;
2346 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2348 const type_t
*iface
;
2349 if (stmt
->type
== STMT_LIBRARY
)
2351 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2354 else if (stmt
->type
!= STMT_TYPE
|| stmt
->u
.type
->type
!= RPC_FC_IP
)
2357 iface
= stmt
->u
.type
;
2364 current_iface
= iface
;
2365 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
2367 if (is_local(func
->def
->attrs
)) continue;
2369 if (!is_void(get_func_return_type(func
)))
2371 var_t v
= *func
->def
;
2372 v
.type
= get_func_return_type(func
);
2373 update_tfsoff(get_func_return_type(func
),
2374 write_typeformatstring_var(
2375 file
, 2, NULL
, get_func_return_type(func
),
2376 &v
, typeformat_offset
),
2380 current_func
= func
;
2382 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2385 write_typeformatstring_var(
2386 file
, 2, func
, var
->type
, var
,
2393 return *typeformat_offset
+ 1;
2396 static size_t process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2398 unsigned int typeformat_offset
= 2;
2400 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2404 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2408 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2409 print_file(file
, indent
, "{\n");
2411 print_file(file
, indent
, "0,\n");
2412 print_file(file
, indent
, "{\n");
2414 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2416 set_all_tfswrite(TRUE
);
2417 process_tfs(file
, stmts
, pred
);
2419 print_file(file
, indent
, "0x0\n");
2421 print_file(file
, indent
, "}\n");
2423 print_file(file
, indent
, "};\n");
2424 print_file(file
, indent
, "\n");
2427 static unsigned int get_required_buffer_size_type(
2428 const type_t
*type
, const char *name
, unsigned int *alignment
)
2431 if (is_user_type(type
))
2434 const type_t
*utype
= get_user_type(type
, &uname
);
2435 return get_required_buffer_size_type(utype
, uname
, alignment
);
2459 case RPC_FC_ERROR_STATUS_T
:
2469 case RPC_FC_BIND_PRIMITIVE
:
2473 case RPC_FC_PSTRUCT
:
2477 if (!type
->fields_or_args
) return 0;
2478 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2480 unsigned int alignment
;
2481 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2489 is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
2490 ? get_required_buffer_size_type( type
->ref
, name
, alignment
)
2493 case RPC_FC_SMFARRAY
:
2494 case RPC_FC_LGFARRAY
:
2495 return type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2503 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2505 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2506 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2509 if (!in_attr
&& !out_attr
)
2514 for (t
= var
->type
; is_ptr(t
); t
= t
->ref
)
2515 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
2521 if (pass
== PASS_OUT
)
2523 if (out_attr
&& is_ptr(var
->type
))
2525 type_t
*type
= var
->type
;
2527 if (type
->type
== RPC_FC_STRUCT
)
2530 unsigned int size
= 36;
2532 if (!type
->fields_or_args
) return size
;
2533 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2536 size
+= get_required_buffer_size_type(
2537 field
->type
, field
->name
, &align
);
2546 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2547 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2549 if (is_ptr(var
->type
))
2551 type_t
*type
= var
->type
;
2553 if (is_base_type(type
->type
))
2557 else if (type
->type
== RPC_FC_STRUCT
)
2559 unsigned int size
= 36;
2562 if (!type
->fields_or_args
) return size
;
2563 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2566 size
+= get_required_buffer_size_type(
2567 field
->type
, field
->name
, &align
);
2574 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2578 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2581 unsigned int total_size
= 0, alignment
;
2585 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2587 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2588 total_size
+= alignment
;
2592 if (pass
== PASS_OUT
&& !is_void(get_func_return_type(func
)))
2594 var_t v
= *func
->def
;
2595 v
.type
= get_func_return_type(func
);
2596 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
2597 total_size
+= alignment
;
2602 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2603 enum remoting_phase phase
,
2604 const var_t
*var
, unsigned int type_offset
)
2606 const char *function
;
2609 case PHASE_BUFFERSIZE
:
2610 function
= "BufferSize";
2613 function
= "Marshall";
2615 case PHASE_UNMARSHAL
:
2616 function
= "Unmarshall";
2626 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2628 print_file(file
, indent
, "&_StubMsg,\n");
2629 print_file(file
, indent
, "%s%s%s%s,\n",
2630 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2631 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2632 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2634 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2635 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2636 if (phase
== PHASE_UNMARSHAL
)
2637 print_file(file
, indent
, "0);\n");
2641 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2642 enum pass pass
, const var_t
*var
,
2643 const char *varname
)
2645 type_t
*type
= var
->type
;
2647 unsigned int alignment
= 0;
2648 unsigned char rtype
;
2650 /* no work to do for other phases, buffer sizing is done elsewhere */
2651 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2654 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2678 case RPC_FC_ERROR_STATUS_T
:
2690 case RPC_FC_BIND_PRIMITIVE
:
2691 /* no marshalling needed */
2695 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2699 if (phase
== PHASE_MARSHAL
)
2700 print_file(file
, indent
, "MIDL_memset(_StubMsg.Buffer, 0, (0x%x - (long)_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
2701 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2702 alignment
- 1, alignment
- 1);
2704 if (phase
== PHASE_MARSHAL
)
2706 print_file(file
, indent
, "*(");
2707 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2709 fprintf(file
, " *)_StubMsg.Buffer = *");
2711 fprintf(file
, " *)_StubMsg.Buffer = ");
2712 fprintf(file
, "%s", varname
);
2713 fprintf(file
, ";\n");
2715 else if (phase
== PHASE_UNMARSHAL
)
2717 print_file(file
, indent
, "if (_StubMsg.Buffer + sizeof(");
2718 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2719 fprintf(file
, ") > _StubMsg.BufferEnd)\n");
2720 print_file(file
, indent
, "{\n");
2721 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2722 print_file(file
, indent
, "}\n");
2723 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2724 print_file(file
, indent
, "");
2726 print_file(file
, indent
, "*");
2727 fprintf(file
, "%s", varname
);
2728 if (pass
== PASS_IN
&& is_ptr(type
))
2729 fprintf(file
, " = (");
2731 fprintf(file
, " = *(");
2732 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2733 fprintf(file
, " *)_StubMsg.Buffer;\n");
2736 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2737 write_type_decl(file
, var
->type
, NULL
);
2738 fprintf(file
, ");\n");
2741 /* returns whether the MaxCount, Offset or ActualCount members need to be
2742 * filled in for the specified phase */
2743 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2745 return (phase
!= PHASE_UNMARSHAL
);
2748 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
2752 for ( ; is_ptr(t
) || is_array(t
); t
= t
->ref
)
2758 error("%s: multidimensional conformant"
2759 " arrays not supported at the top level\n",
2766 static void write_remoting_arg(FILE *file
, int indent
, const func_t
*func
,
2767 enum pass pass
, enum remoting_phase phase
,
2770 int in_attr
, out_attr
, pointer_type
;
2771 const type_t
*type
= var
->type
;
2772 unsigned char rtype
;
2773 size_t start_offset
= type
->typestring_offset
;
2775 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2777 pointer_type
= RPC_FC_RP
;
2779 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2780 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2781 if (!in_attr
&& !out_attr
)
2784 if (phase
!= PHASE_FREE
)
2788 if (!in_attr
) return;
2791 if (!out_attr
) return;
2799 if (is_context_handle(type
))
2801 if (phase
== PHASE_MARSHAL
)
2803 if (pass
== PASS_IN
)
2805 /* if the context_handle attribute appears in the chain of types
2806 * without pointers being followed, then the context handle must
2807 * be direct, otherwise it is a pointer */
2808 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
2809 print_file(file
, indent
, "NdrClientContextMarshall(\n");
2810 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2811 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s,\n", is_ch_ptr
? "*" : "", var
->name
);
2812 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
2816 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
2817 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2818 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s,\n", var
->name
);
2819 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
2820 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2823 else if (phase
== PHASE_UNMARSHAL
)
2825 if (pass
== PASS_OUT
)
2828 print_file(file
, indent
, "*%s = 0;\n", var
->name
);
2829 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
2830 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2831 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s,\n", var
->name
);
2832 print_file(file
, indent
+ 1, "_Handle);\n");
2836 print_file(file
, indent
, "%s = NdrServerContextNewUnmarshall(\n", var
->name
);
2837 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2838 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2842 else if (is_user_type(var
->type
))
2844 print_phase_function(file
, indent
, "UserMarshal", phase
, var
, start_offset
);
2846 else if (is_string_type(var
->attrs
, var
->type
))
2848 if (is_array(type
) && !is_conformant_array(type
))
2849 print_phase_function(file
, indent
, "NonConformantString", phase
, var
, start_offset
);
2852 if (type
->size_is
&& is_size_needed_for_phase(phase
))
2854 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2855 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2856 fprintf(file
, ";\n");
2859 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
|| pointer_type
== RPC_FC_UP
)
2860 print_phase_function(file
, indent
, "Pointer", phase
, var
,
2861 start_offset
- (type
->size_is
? 4 : 2));
2863 print_phase_function(file
, indent
, "ConformantString", phase
, var
,
2867 else if (is_array(type
))
2869 unsigned char tc
= type
->type
;
2870 const char *array_type
= "FixedArray";
2872 /* We already have the size_is expression since it's at the
2873 top level, but do checks for multidimensional conformant
2874 arrays. When we handle them, we'll need to extend this
2875 function to return a list, and then we'll actually use
2876 the return value. */
2877 get_size_is_expr(type
, var
->name
);
2879 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2881 if (is_size_needed_for_phase(phase
))
2883 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2884 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2885 write_expr(file
, type
->length_is
, 1, 1, NULL
, NULL
);
2886 fprintf(file
, ";\n\n");
2888 array_type
= "VaryingArray";
2890 else if (tc
== RPC_FC_CARRAY
)
2892 if (is_size_needed_for_phase(phase
))
2894 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2895 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2896 fprintf(file
, ";\n\n");
2898 array_type
= "ConformantArray";
2900 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
2902 if (is_size_needed_for_phase(phase
))
2906 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2907 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2908 fprintf(file
, ";\n");
2910 if (type
->length_is
)
2912 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2913 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2914 write_expr(file
, type
->length_is
, 1, 1, NULL
, NULL
);
2915 fprintf(file
, ";\n\n");
2918 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
2920 : "ConformantVaryingArray");
2923 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
2924 print_phase_function(file
, indent
, array_type
, phase
, var
, start_offset
);
2925 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
2927 /* these are all unmarshalled by allocating memory */
2928 if (type
->type
== RPC_FC_BOGUS_ARRAY
||
2929 type
->type
== RPC_FC_CVARRAY
||
2930 ((type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
) && in_attr
) ||
2931 (type
->type
== RPC_FC_CARRAY
&& !in_attr
))
2933 print_file(file
, indent
, "if (%s)\n", var
->name
);
2935 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2939 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2941 if (phase
!= PHASE_FREE
)
2942 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2944 else if (!is_ptr(var
->type
))
2949 case RPC_FC_PSTRUCT
:
2950 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
);
2952 case RPC_FC_CSTRUCT
:
2953 case RPC_FC_CPSTRUCT
:
2954 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
, start_offset
);
2956 case RPC_FC_CVSTRUCT
:
2957 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
, start_offset
);
2959 case RPC_FC_BOGUS_STRUCT
:
2960 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
, start_offset
);
2963 if (is_base_type( var
->type
->ref
->type
))
2965 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2967 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2969 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2970 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2975 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2977 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long) " );
2978 write_expr( file
, iid
, 1, 1, NULL
, NULL
);
2979 fprintf( file
, ";\n\n" );
2981 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2985 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2990 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2992 if (phase
!= PHASE_FREE
)
2993 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2995 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2997 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2998 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
3003 expr_t
*sx
= get_size_is_expr(type
, var
->name
);
3005 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3007 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long) " );
3008 write_expr( file
, iid
, 1, 1, NULL
, NULL
);
3009 fprintf( file
, ";\n\n" );
3013 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long) ");
3014 write_expr(file
, sx
, 1, 1, NULL
, NULL
);
3015 fprintf(file
, ";\n\n");
3017 if (var
->type
->ref
->type
== RPC_FC_IP
)
3018 print_phase_function(file
, indent
, "InterfacePointer", phase
, var
, start_offset
);
3020 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
3023 fprintf(file
, "\n");
3026 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
3027 enum pass pass
, enum remoting_phase phase
)
3029 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3031 unsigned int size
= get_function_buffer_size( func
, pass
);
3032 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
3035 if (pass
== PASS_RETURN
)
3039 var
.type
= get_func_return_type(func
);
3040 var
.name
= xstrdup( "_RetVal" );
3041 write_remoting_arg( file
, indent
, func
, pass
, phase
, &var
);
3049 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3050 write_remoting_arg( file
, indent
, func
, pass
, phase
, var
);
3055 size_t get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3057 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3061 size_t get_size_procformatstring_func(const func_t
*func
)
3066 /* argument list size */
3068 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3069 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3071 /* return value size */
3072 if (is_void(get_func_return_type(func
)))
3073 size
+= 2; /* FC_END and FC_PAD */
3075 size
+= get_size_procformatstring_type("return value", get_func_return_type(func
), NULL
);
3080 size_t get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3082 const statement_t
*stmt
;
3086 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3088 const type_t
*iface
;
3089 if (stmt
->type
== STMT_LIBRARY
)
3091 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3094 else if (stmt
->type
!= STMT_TYPE
|| stmt
->u
.type
->type
!= RPC_FC_IP
)
3097 iface
= stmt
->u
.type
;
3102 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
3103 if (!is_local(func
->def
->attrs
))
3104 size
+= get_size_procformatstring_func( func
);
3109 size_t get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3111 set_all_tfswrite(FALSE
);
3112 return process_tfs(NULL
, stmts
, pred
);
3115 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
3117 int in_attr
, out_attr
;
3121 /* declare return value '_RetVal' */
3122 if (!is_void(get_func_return_type(func
)))
3124 print_file(file
, indent
, "");
3125 write_type_decl_left(file
, get_func_return_type(func
));
3126 fprintf(file
, " _RetVal;\n");
3132 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3134 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3136 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3137 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3138 if (!out_attr
&& !in_attr
)
3141 if (is_context_handle(var
->type
))
3142 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3145 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
3147 print_file(file
, indent
, "");
3148 write_type_decl(file
, var
->type
->declarray
? var
->type
: var
->type
->ref
,
3150 fprintf(file
, ";\n");
3153 print_file(file
, indent
, "");
3154 write_type_decl_left(file
, var
->type
);
3156 if (var
->type
->declarray
) {
3157 fprintf(file
, "( *");
3158 write_name(file
, var
);
3159 fprintf(file
, " )");
3161 write_name(file
, var
);
3162 write_type_right(file
, var
->type
, FALSE
);
3163 fprintf(file
, ";\n");
3165 if (decl_indirect(var
->type
))
3166 print_file(file
, indent
, "void *_p_%s = &%s;\n",
3167 var
->name
, var
->name
);
3173 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
3175 int in_attr
, out_attr
;
3182 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3184 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3185 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3186 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3187 if (!out_attr
&& !in_attr
)
3192 print_file(file
, indent
, "");
3193 write_name(file
, var
);
3195 if (is_context_handle(var
->type
))
3197 fprintf(file
, " = NdrContextHandleInitialize(\n");
3198 print_file(file
, indent
+ 1, "&_StubMsg,\n");
3199 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3200 var
->type
->typestring_offset
);
3202 else if (var
->type
->size_is
)
3204 unsigned int size
, align
= 0;
3205 type_t
*type
= var
->type
;
3207 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
3208 for ( ; type
->size_is
; type
= type
->ref
)
3210 write_expr(file
, type
->size_is
, TRUE
, TRUE
, NULL
, NULL
);
3211 fprintf(file
, " * ");
3213 size
= type_memsize(type
, &align
);
3214 fprintf(file
, "%u);\n", size
);
3216 else if (!is_string
)
3218 fprintf(file
, " = &_W%u;\n", i
);
3219 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
3220 print_file(file
, indent
, "_W%u = 0;\n", i
);
3228 fprintf(file
, "\n");
3232 int write_expr_eval_routines(FILE *file
, const char *iface
)
3234 static const char *var_name
= "pS";
3235 static const char *var_name_expr
= "pS->";
3237 struct expr_eval_routine
*eval
;
3238 unsigned short callback_offset
= 0;
3240 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3242 const char *name
= eval
->structure
->name
;
3245 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3246 iface
, name
, callback_offset
);
3247 print_file(file
, 0, "{\n");
3248 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3249 name
, var_name
, name
, eval
->baseoff
);
3250 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3251 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
3252 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
);
3253 fprintf(file
, ";\n");
3254 print_file(file
, 0, "}\n\n");
3260 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3262 struct expr_eval_routine
*eval
;
3263 struct expr_eval_routine
*cursor
;
3264 unsigned short callback_offset
= 0;
3266 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3267 fprintf(file
, "{\n");
3269 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3271 const char *name
= eval
->structure
->name
;
3272 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3274 list_remove(&eval
->entry
);
3278 fprintf(file
, "};\n\n");
3281 void write_user_quad_list(FILE *file
)
3285 if (list_empty(&user_type_list
))
3288 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3289 fprintf(file
, "{\n");
3290 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3292 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3293 print_file(file
, 1, "{\n");
3294 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3295 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3296 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3297 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3298 print_file(file
, 1, "}%s\n", sep
);
3300 fprintf(file
, "};\n\n");
3303 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3305 const struct str_list_entry_t
*endpoint
;
3308 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3309 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3310 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3312 print_file( f
, 1, "{ (const unsigned char *)\"" );
3313 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3315 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3318 if (!*p
) goto error
;
3319 if (p
[1] != '[') goto error
;
3321 fprintf( f
, "\", (const unsigned char *)\"" );
3322 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3324 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3327 if (*p
!= ']') goto error
;
3328 fprintf( f
, "\" },\n" );
3330 print_file( f
, 0, "};\n\n" );
3334 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);