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 static const func_t
*current_func
;
45 static const type_t
*current_structure
;
46 static const type_t
*current_iface
;
48 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
49 struct expr_eval_routine
52 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
);
61 static const var_t
*find_array_or_string_in_struct(const type_t
*type
);
62 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
64 const char *name
, unsigned int *typestring_offset
);
66 const char *string_of_type(unsigned char type
)
70 case RPC_FC_BYTE
: return "FC_BYTE";
71 case RPC_FC_CHAR
: return "FC_CHAR";
72 case RPC_FC_SMALL
: return "FC_SMALL";
73 case RPC_FC_USMALL
: return "FC_USMALL";
74 case RPC_FC_WCHAR
: return "FC_WCHAR";
75 case RPC_FC_SHORT
: return "FC_SHORT";
76 case RPC_FC_USHORT
: return "FC_USHORT";
77 case RPC_FC_LONG
: return "FC_LONG";
78 case RPC_FC_ULONG
: return "FC_ULONG";
79 case RPC_FC_FLOAT
: return "FC_FLOAT";
80 case RPC_FC_HYPER
: return "FC_HYPER";
81 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
82 case RPC_FC_ENUM16
: return "FC_ENUM16";
83 case RPC_FC_ENUM32
: return "FC_ENUM32";
84 case RPC_FC_IGNORE
: return "FC_IGNORE";
85 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
86 case RPC_FC_RP
: return "FC_RP";
87 case RPC_FC_UP
: return "FC_UP";
88 case RPC_FC_OP
: return "FC_OP";
89 case RPC_FC_FP
: return "FC_FP";
90 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
91 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
92 case RPC_FC_STRUCT
: return "FC_STRUCT";
93 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
94 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
95 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
96 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
97 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
98 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
99 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
100 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
101 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
102 case RPC_FC_CARRAY
: return "FC_CARRAY";
103 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
104 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
105 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
106 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
107 case RPC_FC_POINTER
: return "FC_POINTER";
108 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
109 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
110 case RPC_FC_CSTRING
: return "FC_CSTRING";
111 case RPC_FC_WSTRING
: return "FC_WSTRING";
113 error("string_of_type: unknown type 0x%02x\n", type
);
118 int is_struct(unsigned char type
)
125 case RPC_FC_CPSTRUCT
:
126 case RPC_FC_CVSTRUCT
:
127 case RPC_FC_BOGUS_STRUCT
:
134 static int is_non_complex_struct(const type_t
*type
)
141 case RPC_FC_CPSTRUCT
:
142 case RPC_FC_CVSTRUCT
:
149 int is_union(unsigned char type
)
153 case RPC_FC_ENCAPSULATED_UNION
:
154 case RPC_FC_NON_ENCAPSULATED_UNION
:
161 static int type_has_pointers(const type_t
*type
)
163 if (is_user_type(type
))
165 else if (is_ptr(type
))
167 else if (is_array(type
))
168 return type_has_pointers(type
->ref
);
169 else if (is_struct(type
->type
))
172 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
174 if (type_has_pointers(field
->type
))
178 else if (is_union(type
->type
))
182 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
184 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
185 fields
= uv
->type
->fields_or_args
;
188 fields
= type
->fields_or_args
;
189 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
191 if (field
->type
&& type_has_pointers(field
->type
))
199 static int type_has_full_pointer(const type_t
*type
)
201 if (is_user_type(type
))
203 else if (type
->type
== RPC_FC_FP
)
205 else if (is_ptr(type
))
207 else if (is_array(type
))
208 return type_has_full_pointer(type
->ref
);
209 else if (is_struct(type
->type
))
212 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
214 if (type_has_full_pointer(field
->type
))
218 else if (is_union(type
->type
))
222 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
224 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
225 fields
= uv
->type
->fields_or_args
;
228 fields
= type
->fields_or_args
;
229 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
231 if (field
->type
&& type_has_full_pointer(field
->type
))
239 static unsigned short user_type_offset(const char *name
)
242 unsigned short off
= 0;
243 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
245 if (strcmp(name
, ut
->name
) == 0)
249 error("user_type_offset: couldn't find type (%s)\n", name
);
253 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
255 type
->typestring_offset
= offset
;
256 if (file
) type
->tfswrite
= FALSE
;
259 static void guard_rec(type_t
*type
)
261 /* types that contain references to themselves (like a linked list),
262 need to be shielded from infinite recursion when writing embedded
264 if (type
->typestring_offset
)
265 type
->tfswrite
= FALSE
;
267 type
->typestring_offset
= 1;
270 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
274 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
282 if (t
->kind
== TKIND_ALIAS
)
289 int is_user_type(const type_t
*t
)
291 return get_user_type(t
, NULL
) != NULL
;
294 static int is_embedded_complex(const type_t
*type
)
296 unsigned char tc
= type
->type
;
297 return is_struct(tc
) || is_union(tc
) || is_array(type
) || is_user_type(type
)
298 || (is_ptr(type
) && type
->ref
->type
== RPC_FC_IP
);
301 static const char *get_context_handle_type_name(const type_t
*type
)
304 for (t
= type
; is_ptr(t
); t
= t
->ref
)
305 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
311 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
314 fprintf(file, "/* %2u */\n", typestring_offset); \
315 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
319 static void print_file(FILE *file
, int indent
, const char *format
, ...)
322 va_start(va
, format
);
323 print(file
, indent
, format
, va
);
327 void print(FILE *file
, int indent
, const char *format
, va_list va
)
331 if (format
[0] != '\n')
334 vfprintf(file
, format
, va
);
339 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
)
341 if (decl_indirect(t
))
342 print_file(file
, indent
, "MIDL_memset(&%s, 0, sizeof(%s));\n", n
, n
);
343 else if (is_ptr(t
) || is_array(t
))
344 print_file(file
, indent
, "%s = 0;\n", n
);
347 void write_parameters_init(FILE *file
, int indent
, const func_t
*func
)
351 if (!is_void(get_func_return_type(func
)))
352 write_var_init(file
, indent
, get_func_return_type(func
), "_RetVal");
357 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
358 write_var_init(file
, indent
, var
->type
, var
->name
);
363 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
365 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
366 print_file(f
, indent
, "{\n");
367 print_file(f
, indent
+ 1, "short Pad;\n");
368 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
369 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
370 print_file(f
, indent
, "\n");
373 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
375 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
376 get_size_typeformatstring(stmts
, pred
));
378 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
379 get_size_procformatstring(stmts
, pred
));
382 write_formatdesc(f
, indent
, "TYPE");
383 write_formatdesc(f
, indent
, "PROC");
385 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
386 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
387 print_file(f
, indent
, "\n");
390 static inline int is_base_type(unsigned char type
)
409 case RPC_FC_ERROR_STATUS_T
:
410 case RPC_FC_BIND_PRIMITIVE
:
418 int decl_indirect(const type_t
*t
)
420 return is_user_type(t
)
421 || (!is_base_type(t
->type
)
426 static size_t write_procformatstring_type(FILE *file
, int indent
,
429 const attr_list_t
*attrs
,
434 int is_in
= is_attr(attrs
, ATTR_IN
);
435 int is_out
= is_attr(attrs
, ATTR_OUT
);
437 if (!is_in
&& !is_out
) is_in
= TRUE
;
439 if (!type
->declarray
&& is_base_type(type
->type
))
442 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
444 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
446 if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
448 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
449 size
= 2; /* includes param type prefix */
451 else if (is_base_type(type
->type
))
453 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
454 size
= 2; /* includes param type prefix */
458 error("Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
465 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
466 else if (is_in
&& is_out
)
467 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
469 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
471 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
473 print_file(file
, indent
, "0x01,\n");
474 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
475 size
= 4; /* includes param type prefix */
480 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
482 const statement_t
*stmt
;
483 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
485 if (stmt
->type
== STMT_TYPE
&& stmt
->u
.type
->type
== RPC_FC_IP
)
488 if (!pred(stmt
->u
.type
))
490 if (stmt
->u
.type
->funcs
) LIST_FOR_EACH_ENTRY( func
, stmt
->u
.type
->funcs
, const func_t
, entry
)
492 if (is_local(func
->def
->attrs
)) continue;
493 /* emit argument data */
497 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
498 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
501 /* emit return value data */
502 if (is_void(get_func_return_type(func
)))
504 print_file(file
, indent
, "0x5b, /* FC_END */\n");
505 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
508 write_procformatstring_type(file
, indent
, "return value", get_func_return_type(func
), NULL
, TRUE
);
511 else if (stmt
->type
== STMT_LIBRARY
)
512 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
516 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
520 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
521 print_file(file
, indent
, "{\n");
523 print_file(file
, indent
, "0,\n");
524 print_file(file
, indent
, "{\n");
527 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
529 print_file(file
, indent
, "0x0\n");
531 print_file(file
, indent
, "}\n");
533 print_file(file
, indent
, "};\n");
534 print_file(file
, indent
, "\n");
537 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
539 if (is_base_type(type
->type
))
541 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
542 *typestring_offset
+= 1;
549 /* write conformance / variance descriptor */
550 static size_t write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
551 unsigned int baseoff
, const type_t
*type
,
554 unsigned char operator_type
= 0;
555 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
556 const char *conftype_string
= "";
557 const char *operator_string
= "no operators";
558 const expr_t
*subexpr
;
562 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
568 /* Top-level conformance calculations are done inline. */
569 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
570 RPC_FC_TOP_LEVEL_CONFORMANCE
);
571 print_file (file
, 2, "0x0,\n");
572 print_file (file
, 2, "NdrFcShort(0x0),\n");
578 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
579 error("write_conf_or_var_desc: constant value %ld is greater than "
580 "the maximum constant size of %d\n", expr
->cval
,
581 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
583 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
584 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
585 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
586 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
591 if (is_ptr(type
) || (is_array(type
) && !type
->declarray
))
593 conftype
= RPC_FC_POINTER_CONFORMANCE
;
594 conftype_string
= "field pointer, ";
598 switch (subexpr
->type
)
601 subexpr
= subexpr
->ref
;
602 operator_type
= RPC_FC_DEREFERENCE
;
603 operator_string
= "FC_DEREFERENCE";
606 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
608 subexpr
= subexpr
->ref
;
609 operator_type
= RPC_FC_DIV_2
;
610 operator_string
= "FC_DIV_2";
614 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
616 subexpr
= subexpr
->ref
;
617 operator_type
= RPC_FC_MULT_2
;
618 operator_string
= "FC_MULT_2";
622 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
624 subexpr
= subexpr
->ref
;
625 operator_type
= RPC_FC_SUB_1
;
626 operator_string
= "FC_SUB_1";
630 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
632 subexpr
= subexpr
->ref
;
633 operator_type
= RPC_FC_ADD_1
;
634 operator_string
= "FC_ADD_1";
641 if (subexpr
->type
== EXPR_IDENTIFIER
)
643 const type_t
*correlation_variable
= NULL
;
644 unsigned char correlation_variable_type
;
645 unsigned char param_type
= 0;
649 if (structure
->fields_or_args
) LIST_FOR_EACH_ENTRY( var
, structure
->fields_or_args
, const var_t
, entry
)
651 unsigned int align
= 0;
652 /* FIXME: take alignment into account */
653 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
655 correlation_variable
= var
->type
;
658 offset
+= type_memsize(var
->type
, &align
);
660 if (!correlation_variable
)
661 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
664 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
667 correlation_variable_type
= correlation_variable
->type
;
669 switch (correlation_variable_type
)
673 param_type
= RPC_FC_SMALL
;
677 param_type
= RPC_FC_USMALL
;
682 param_type
= RPC_FC_SHORT
;
685 param_type
= RPC_FC_USHORT
;
689 param_type
= RPC_FC_LONG
;
692 param_type
= RPC_FC_ULONG
;
695 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
696 correlation_variable_type
);
699 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
700 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
701 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
702 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
707 unsigned int callback_offset
= 0;
708 struct expr_eval_routine
*eval
;
711 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
713 if (!strcmp (eval
->structure
->name
, structure
->name
)
714 && !compare_expr (eval
->expr
, expr
))
724 eval
= xmalloc (sizeof(*eval
));
725 eval
->structure
= structure
;
726 eval
->baseoff
= baseoff
;
728 list_add_tail (&expr_eval_routines
, &eval
->entry
);
731 if (callback_offset
> USHRT_MAX
)
732 error("Maximum number of callback routines reached\n");
734 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
735 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
736 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
741 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
743 int have_align
= FALSE
;
747 if (!fields
) return 0;
748 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
750 unsigned int falign
= 0;
751 size_t fsize
= type_memsize(v
->type
, &falign
);
757 size
= (size
+ (falign
- 1)) & ~(falign
- 1);
761 size
= (size
+ (*align
- 1)) & ~(*align
- 1);
765 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
767 size_t size
, maxs
= 0;
768 unsigned int align
= *pmaxa
;
771 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
773 /* we could have an empty default field with NULL type */
776 size
= type_memsize(v
->type
, &align
);
777 if (maxs
< size
) maxs
= size
;
778 if (*pmaxa
< align
) *pmaxa
= align
;
785 int get_padding(const var_list_t
*fields
)
787 unsigned short offset
= 0;
794 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
796 type_t
*ft
= f
->type
;
797 unsigned int align
= 0;
798 size_t size
= type_memsize(ft
, &align
);
801 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
805 return ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
808 size_t type_memsize(const type_t
*t
, unsigned int *align
)
812 if (t
->declarray
&& is_conformant_array(t
))
814 type_memsize(t
->ref
, align
);
817 else if (is_ptr(t
) || is_conformant_array(t
))
819 size
= sizeof(void *);
820 if (size
> *align
) *align
= size
;
822 else switch (t
->type
)
829 if (size
> *align
) *align
= size
;
836 if (size
> *align
) *align
= size
;
840 case RPC_FC_ERROR_STATUS_T
:
844 if (size
> *align
) *align
= size
;
849 if (size
> *align
) *align
= size
;
852 case RPC_FC_CVSTRUCT
:
853 case RPC_FC_CPSTRUCT
:
856 case RPC_FC_BOGUS_STRUCT
:
857 size
= fields_memsize(t
->fields_or_args
, align
);
859 case RPC_FC_ENCAPSULATED_UNION
:
860 case RPC_FC_NON_ENCAPSULATED_UNION
:
861 size
= union_memsize(t
->fields_or_args
, align
);
863 case RPC_FC_SMFARRAY
:
864 case RPC_FC_LGFARRAY
:
865 case RPC_FC_SMVARRAY
:
866 case RPC_FC_LGVARRAY
:
867 case RPC_FC_BOGUS_ARRAY
:
868 size
= t
->dim
* type_memsize(t
->ref
, align
);
871 error("type_memsize: Unknown type %d\n", t
->type
);
878 int is_full_pointer_function(const func_t
*func
)
881 if (type_has_full_pointer(get_func_return_type(func
)))
885 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
886 if (type_has_full_pointer( var
->type
))
891 void write_full_pointer_init(FILE *file
, int indent
, const func_t
*func
, int is_server
)
893 print_file(file
, indent
, "_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
894 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
898 void write_full_pointer_free(FILE *file
, int indent
, const func_t
*func
)
900 print_file(file
, indent
, "NdrFullPointerXlatFree(_StubMsg.FullPtrXlatTables);\n");
904 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
906 short absoff
= type
->ref
->typestring_offset
;
907 short reloff
= absoff
- (offset
+ 2);
908 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
910 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
911 type
->type
, ptr_attr
, string_of_type(type
->type
));
912 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
913 reloff
, reloff
, absoff
);
917 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
919 unsigned char fc
= type
->ref
->type
;
920 /* for historical reasons, write_simple_pointer also handled string types,
921 * but no longer does. catch bad uses of the function with this check */
922 if (is_string_type(type
->attrs
, type
))
923 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
924 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
925 type
->type
, string_of_type(type
->type
));
926 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
927 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
931 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
933 print_file(file
, 0, "/* %u (", tfsoff
);
934 write_type_decl(file
, t
, NULL
);
935 print_file(file
, 0, ") */\n");
938 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
940 unsigned int offset
= *typestring_offset
;
942 print_start_tfs_comment(file
, type
, offset
);
943 update_tfsoff(type
, offset
, file
);
945 if (type
->ref
->typestring_offset
)
946 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
947 else if (is_base_type(type
->ref
->type
))
948 *typestring_offset
+= write_simple_pointer(file
, type
);
953 static int processed(const type_t
*type
)
955 return type
->typestring_offset
&& !type
->tfswrite
;
958 static int user_type_has_variable_size(const type_t
*t
)
967 case RPC_FC_CPSTRUCT
:
968 case RPC_FC_CVSTRUCT
:
971 /* Note: Since this only applies to user types, we can't have a conformant
972 array here, and strings should get filed under pointer in this case. */
976 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
978 unsigned int start
, absoff
, flags
;
979 unsigned int align
= 0, ualign
= 0;
981 type_t
*utype
= get_user_type(type
, &name
);
982 size_t usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
983 size_t size
= type_memsize(type
, &align
);
984 unsigned short funoff
= user_type_offset(name
);
989 if (is_base_type(utype
->type
))
992 print_start_tfs_comment(file
, utype
, absoff
);
993 print_file(file
, 2, "0x%x,\t/* %s */\n", utype
->type
, string_of_type(utype
->type
));
994 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
999 if (!processed(utype
))
1000 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1001 absoff
= utype
->typestring_offset
;
1004 if (utype
->type
== RPC_FC_RP
)
1006 else if (utype
->type
== RPC_FC_UP
)
1012 update_tfsoff(type
, start
, file
);
1013 print_start_tfs_comment(file
, type
, start
);
1014 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1015 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1016 flags
| (align
- 1), align
- 1, flags
);
1017 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1018 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size
, size
);
1019 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize
, usize
);
1021 reloff
= absoff
- *tfsoff
;
1022 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff
, reloff
, absoff
);
1026 static void write_member_type(FILE *file
, const type_t
*cont
,
1027 const attr_list_t
*attrs
, const type_t
*type
,
1028 unsigned int *corroff
, unsigned int *tfsoff
)
1030 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1035 if (is_union(type
->type
) && is_attr(attrs
, ATTR_SWITCHIS
))
1042 absoff
= type
->typestring_offset
;
1044 reloff
= absoff
- (*tfsoff
+ 2);
1046 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1047 /* FIXME: actually compute necessary padding */
1048 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1049 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1050 reloff
, reloff
, absoff
);
1053 else if (is_ptr(type
) || is_conformant_array(type
))
1055 unsigned char fc
= (cont
->type
== RPC_FC_BOGUS_STRUCT
1058 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1061 else if (!write_base_type(file
, type
, tfsoff
))
1062 error("Unsupported member type 0x%x\n", type
->type
);
1065 static void write_end(FILE *file
, unsigned int *tfsoff
)
1067 if (*tfsoff
% 2 == 0)
1069 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1072 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1076 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1078 unsigned int offset
= 0;
1079 var_list_t
*fs
= type
->fields_or_args
;
1082 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1084 unsigned int align
= 0;
1085 type_t
*ft
= f
->type
;
1086 if (is_union(ft
->type
) && is_attr(f
->attrs
, ATTR_SWITCHIS
))
1088 unsigned int absoff
= ft
->typestring_offset
;
1089 short reloff
= absoff
- (*tfsoff
+ 6);
1090 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1091 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
1092 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1093 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1094 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1095 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1096 reloff
, reloff
, absoff
);
1100 /* FIXME: take alignment into account */
1101 offset
+= type_memsize(ft
, &align
);
1105 static int write_no_repeat_pointer_descriptions(
1106 FILE *file
, type_t
*type
,
1107 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1108 unsigned int *typestring_offset
)
1113 if (is_ptr(type
) || (!type
->declarray
&& is_conformant_array(type
)))
1115 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1116 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1118 /* pointer instance */
1119 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1120 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1121 *typestring_offset
+= 6;
1125 if (is_string_type(type
->attrs
, type
))
1126 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1128 write_pointer_tfs(file
, type
, typestring_offset
);
1132 unsigned absoff
= type
->typestring_offset
;
1133 short reloff
= absoff
- (*typestring_offset
+ 2);
1134 /* FIXME: get pointer attributes from field */
1135 print_file(file
, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP
, "FC_UP");
1136 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1137 reloff
, reloff
, absoff
);
1138 *typestring_offset
+= 4;
1142 *offset_in_memory
+= type_memsize(type
, &align
);
1143 /* FIXME: is there a case where these two are different? */
1145 *offset_in_buffer
+= type_memsize(type
, &align
);
1150 if (is_non_complex_struct(type
))
1153 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1154 written
+= write_no_repeat_pointer_descriptions(
1156 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1161 *offset_in_memory
+= type_memsize(type
, &align
);
1162 /* FIXME: is there a case where these two are different? */
1164 *offset_in_buffer
+= type_memsize(type
, &align
);
1170 static int write_pointer_description_offsets(
1171 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1172 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1173 unsigned int *typestring_offset
)
1178 if (is_ptr(type
) && type
->ref
->type
!= RPC_FC_IP
)
1180 if (offset_in_memory
&& offset_in_buffer
)
1182 /* pointer instance */
1183 /* FIXME: sometimes from end of structure, sometimes from beginning */
1184 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1185 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1188 *offset_in_memory
+= type_memsize(type
, &align
);
1189 /* FIXME: is there a case where these two are different? */
1191 *offset_in_buffer
+= type_memsize(type
, &align
);
1193 *typestring_offset
+= 4;
1195 if (is_string_type(attrs
, type
))
1196 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1197 else if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1198 write_pointer_tfs(file
, type
, typestring_offset
);
1200 error("write_pointer_description_offsets: type format string unknown\n");
1207 return write_pointer_description_offsets(
1208 file
, attrs
, type
->ref
, offset_in_memory
, offset_in_buffer
,
1211 else if (is_non_complex_struct(type
))
1213 /* otherwise search for interesting fields to parse */
1215 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1217 written
+= write_pointer_description_offsets(
1218 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1225 if (offset_in_memory
)
1226 *offset_in_memory
+= type_memsize(type
, &align
);
1227 /* FIXME: is there a case where these two are different? */
1229 if (offset_in_buffer
)
1230 *offset_in_buffer
+= type_memsize(type
, &align
);
1236 /* Note: if file is NULL return value is number of pointers to write, else
1237 * it is the number of type format characters written */
1238 static int write_fixed_array_pointer_descriptions(
1239 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1240 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1241 unsigned int *typestring_offset
)
1244 int pointer_count
= 0;
1246 if (type
->type
== RPC_FC_SMFARRAY
|| type
->type
== RPC_FC_LGFARRAY
)
1248 unsigned int temp
= 0;
1249 /* unfortunately, this needs to be done in two passes to avoid
1250 * writing out redundant FC_FIXED_REPEAT descriptions */
1251 pointer_count
= write_pointer_description_offsets(
1252 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1253 if (pointer_count
> 0)
1255 unsigned int increment_size
;
1256 size_t offset_of_array_pointer_mem
= 0;
1257 size_t offset_of_array_pointer_buf
= 0;
1260 increment_size
= type_memsize(type
->ref
, &align
);
1262 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1263 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1264 print_file(file
, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type
->dim
, type
->dim
);
1265 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1266 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1267 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1268 *typestring_offset
+= 10;
1270 pointer_count
= write_pointer_description_offsets(
1271 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1272 &offset_of_array_pointer_buf
, typestring_offset
);
1275 else if (is_struct(type
->type
))
1278 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1280 pointer_count
+= write_fixed_array_pointer_descriptions(
1281 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1288 if (offset_in_memory
)
1289 *offset_in_memory
+= type_memsize(type
, &align
);
1290 /* FIXME: is there a case where these two are different? */
1292 if (offset_in_buffer
)
1293 *offset_in_buffer
+= type_memsize(type
, &align
);
1296 return pointer_count
;
1299 /* Note: if file is NULL return value is number of pointers to write, else
1300 * it is the number of type format characters written */
1301 static int write_conformant_array_pointer_descriptions(
1302 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1303 size_t offset_in_memory
, unsigned int *typestring_offset
)
1306 int pointer_count
= 0;
1308 if (is_conformant_array(type
) && !type
->length_is
)
1310 unsigned int temp
= 0;
1311 /* unfortunately, this needs to be done in two passes to avoid
1312 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1313 pointer_count
= write_pointer_description_offsets(
1314 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1315 if (pointer_count
> 0)
1317 unsigned int increment_size
;
1318 size_t offset_of_array_pointer_mem
= offset_in_memory
;
1319 size_t offset_of_array_pointer_buf
= offset_in_memory
;
1322 increment_size
= type_memsize(type
->ref
, &align
);
1324 if (increment_size
> USHRT_MAX
)
1325 error("array size of %u bytes is too large\n", increment_size
);
1327 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1328 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1329 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1330 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1331 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1332 *typestring_offset
+= 8;
1334 pointer_count
= write_pointer_description_offsets(
1335 file
, attrs
, type
->ref
, &offset_of_array_pointer_mem
,
1336 &offset_of_array_pointer_buf
, typestring_offset
);
1340 return pointer_count
;
1343 /* Note: if file is NULL return value is number of pointers to write, else
1344 * it is the number of type format characters written */
1345 static int write_varying_array_pointer_descriptions(
1346 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1347 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1348 unsigned int *typestring_offset
)
1351 int pointer_count
= 0;
1353 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1355 if (is_array(type
) && type
->length_is
)
1357 unsigned int temp
= 0;
1358 /* unfortunately, this needs to be done in two passes to avoid
1359 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1360 pointer_count
= write_pointer_description_offsets(
1361 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1362 if (pointer_count
> 0)
1364 unsigned int increment_size
;
1365 size_t offset_of_array_pointer_mem
= 0;
1366 size_t offset_of_array_pointer_buf
= 0;
1369 increment_size
= type_memsize(type
->ref
, &align
);
1371 if (increment_size
> USHRT_MAX
)
1372 error("array size of %u bytes is too large\n", increment_size
);
1374 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1375 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1376 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1377 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1378 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1379 *typestring_offset
+= 8;
1381 pointer_count
= write_pointer_description_offsets(
1382 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1383 &offset_of_array_pointer_buf
, typestring_offset
);
1386 else if (is_struct(type
->type
))
1389 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1391 pointer_count
+= write_varying_array_pointer_descriptions(
1392 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1399 if (offset_in_memory
)
1400 *offset_in_memory
+= type_memsize(type
, &align
);
1401 /* FIXME: is there a case where these two are different? */
1403 if (offset_in_buffer
)
1404 *offset_in_buffer
+= type_memsize(type
, &align
);
1407 return pointer_count
;
1410 static void write_pointer_description(FILE *file
, type_t
*type
,
1411 unsigned int *typestring_offset
)
1413 size_t offset_in_buffer
;
1414 size_t offset_in_memory
;
1416 /* pass 1: search for single instance of a pointer (i.e. don't descend
1418 if (!is_array(type
))
1420 offset_in_memory
= 0;
1421 offset_in_buffer
= 0;
1422 write_no_repeat_pointer_descriptions(
1424 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1427 /* pass 2: search for pointers in fixed arrays */
1428 offset_in_memory
= 0;
1429 offset_in_buffer
= 0;
1430 write_fixed_array_pointer_descriptions(
1432 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1434 /* pass 3: search for pointers in conformant only arrays (but don't descend
1435 * into conformant varying or varying arrays) */
1436 if ((!type
->declarray
|| !current_structure
) && is_conformant_array(type
))
1437 write_conformant_array_pointer_descriptions(
1438 file
, NULL
, type
, 0, typestring_offset
);
1439 else if (type
->type
== RPC_FC_CPSTRUCT
)
1441 unsigned int align
= 0;
1442 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1443 write_conformant_array_pointer_descriptions(
1445 type_memsize(type
, &align
),
1449 /* pass 4: search for pointers in varying arrays */
1450 offset_in_memory
= 0;
1451 offset_in_buffer
= 0;
1452 write_varying_array_pointer_descriptions(
1454 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1457 int is_declptr(const type_t
*t
)
1459 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1462 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1464 const char *name
, unsigned int *typestring_offset
)
1466 size_t start_offset
;
1467 unsigned char rtype
;
1469 if (is_declptr(type
))
1471 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1472 int pointer_type
= is_ptr(type
) ? type
->type
: get_attrv(attrs
, ATTR_POINTERTYPE
);
1474 pointer_type
= RPC_FC_RP
;
1475 print_start_tfs_comment(file
, type
, *typestring_offset
);
1476 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1477 pointer_type
, flag
, string_of_type(pointer_type
),
1478 flag
? " [simple_pointer]" : "");
1479 *typestring_offset
+= 2;
1482 print_file(file
, 2, "NdrFcShort(0x2),\n");
1483 *typestring_offset
+= 2;
1487 start_offset
= *typestring_offset
;
1488 update_tfsoff(type
, start_offset
, file
);
1490 rtype
= type
->ref
->type
;
1492 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1494 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1495 return start_offset
;
1498 if (type
->declarray
&& !is_conformant_array(type
))
1500 /* FIXME: multi-dimensional array */
1501 if (0xffffuL
< type
->dim
)
1502 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1503 name
, 0xffffu
, type
->dim
- 0xffffu
);
1505 if (rtype
== RPC_FC_CHAR
)
1506 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1508 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1509 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1510 *typestring_offset
+= 2;
1512 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", type
->dim
, type
->dim
);
1513 *typestring_offset
+= 2;
1515 return start_offset
;
1517 else if (type
->size_is
)
1519 unsigned int align
= 0;
1521 if (rtype
== RPC_FC_CHAR
)
1522 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1524 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1525 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1526 *typestring_offset
+= 2;
1528 *typestring_offset
+= write_conf_or_var_desc(
1529 file
, current_structure
,
1530 (type
->declarray
&& current_structure
1531 ? type_memsize(current_structure
, &align
)
1533 type
, type
->size_is
);
1535 return start_offset
;
1539 if (rtype
== RPC_FC_WCHAR
)
1540 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1542 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1543 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1544 *typestring_offset
+= 2;
1546 return start_offset
;
1550 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1551 const char *name
, unsigned int *typestring_offset
)
1553 const expr_t
*length_is
= type
->length_is
;
1554 const expr_t
*size_is
= type
->size_is
;
1555 unsigned int align
= 0;
1557 size_t start_offset
;
1559 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1560 unsigned int baseoff
1561 = type
->declarray
&& current_structure
1562 ? type_memsize(current_structure
, &align
)
1566 pointer_type
= RPC_FC_RP
;
1568 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1571 has_pointer
= type_has_pointers(type
->ref
);
1574 size
= type_memsize((is_conformant_array(type
) ? type
->ref
: type
), &align
);
1576 start_offset
= *typestring_offset
;
1577 update_tfsoff(type
, start_offset
, file
);
1578 print_start_tfs_comment(file
, type
, start_offset
);
1579 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1580 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1581 *typestring_offset
+= 2;
1584 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1586 unsigned char tc
= type
->type
;
1588 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1590 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1591 *typestring_offset
+= 4;
1595 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1596 *typestring_offset
+= 2;
1599 if (is_conformant_array(type
))
1601 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1604 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1606 unsigned int elalign
= 0;
1607 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1609 if (type
->type
== RPC_FC_LGVARRAY
)
1611 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1612 *typestring_offset
+= 4;
1616 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1617 *typestring_offset
+= 2;
1620 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1621 *typestring_offset
+= 2;
1626 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1629 if (has_pointer
&& (!type
->declarray
|| !current_structure
))
1631 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1632 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1633 *typestring_offset
+= 2;
1634 write_pointer_description(file
, type
, typestring_offset
);
1635 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1636 *typestring_offset
+= 1;
1639 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1640 write_end(file
, typestring_offset
);
1644 unsigned int dim
= size_is
? 0 : type
->dim
;
1645 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim
, dim
);
1646 *typestring_offset
+= 2;
1648 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1651 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1653 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1654 write_end(file
, typestring_offset
);
1657 return start_offset
;
1660 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1662 const var_t
*last_field
;
1665 if (!type
->fields_or_args
|| list_empty(type
->fields_or_args
))
1668 last_field
= LIST_ENTRY( list_tail(type
->fields_or_args
), const var_t
, entry
);
1669 ft
= last_field
->type
;
1671 if (ft
->declarray
&& is_conformant_array(ft
))
1674 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1675 return find_array_or_string_in_struct(ft
);
1680 static void write_struct_members(FILE *file
, const type_t
*type
,
1681 unsigned int *corroff
, unsigned int *typestring_offset
)
1684 unsigned short offset
= 0;
1688 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
1690 type_t
*ft
= field
->type
;
1691 if (!ft
->declarray
|| !is_conformant_array(ft
))
1693 unsigned int align
= 0;
1694 size_t size
= type_memsize(ft
, &align
);
1697 if ((align
- 1) & offset
)
1699 unsigned char fc
= 0;
1703 fc
= RPC_FC_ALIGNM4
;
1706 fc
= RPC_FC_ALIGNM8
;
1709 error("write_struct_members: cannot align type %d\n", ft
->type
);
1711 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1712 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
1713 *typestring_offset
+= 1;
1715 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1721 padding
= ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
1724 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1725 RPC_FC_STRUCTPAD1
+ padding
- 1,
1727 *typestring_offset
+= 1;
1730 write_end(file
, typestring_offset
);
1733 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1734 const char *name
, unsigned int *tfsoff
)
1736 const type_t
*save_current_structure
= current_structure
;
1737 unsigned int total_size
;
1739 size_t start_offset
;
1740 size_t array_offset
;
1741 int has_pointers
= 0;
1742 unsigned int align
= 0;
1743 unsigned int corroff
;
1747 current_structure
= type
;
1749 total_size
= type_memsize(type
, &align
);
1750 if (total_size
> USHRT_MAX
)
1751 error("structure size for %s exceeds %d bytes by %d bytes\n",
1752 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1754 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY(f
, type
->fields_or_args
, var_t
, entry
)
1755 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1757 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
1759 array
= find_array_or_string_in_struct(type
);
1760 if (array
&& !processed(array
->type
))
1762 = is_attr(array
->attrs
, ATTR_STRING
)
1763 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1764 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1767 write_descriptors(file
, type
, tfsoff
);
1769 start_offset
= *tfsoff
;
1770 update_tfsoff(type
, start_offset
, file
);
1771 print_start_tfs_comment(file
, type
, start_offset
);
1772 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1773 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1774 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1779 unsigned int absoff
= array
->type
->typestring_offset
;
1780 short reloff
= absoff
- *tfsoff
;
1781 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1782 reloff
, reloff
, absoff
);
1785 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1787 print_file(file
, 2, "NdrFcShort(0x0),\n");
1791 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1793 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1794 nothing is written to file yet. On the actual writing pass,
1795 this will have been updated. */
1796 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
1797 short reloff
= absoff
- *tfsoff
;
1798 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1799 reloff
, reloff
, absoff
);
1802 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1803 (type
->type
== RPC_FC_CPSTRUCT
) ||
1804 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1806 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1807 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1809 write_pointer_description(file
, type
, tfsoff
);
1810 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1814 write_struct_members(file
, type
, &corroff
, tfsoff
);
1816 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1818 const var_list_t
*fs
= type
->fields_or_args
;
1821 type
->ptrdesc
= *tfsoff
;
1822 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1824 type_t
*ft
= f
->type
;
1827 if (is_string_type(f
->attrs
, ft
))
1828 write_string_tfs(file
, f
->attrs
, ft
, f
->name
, tfsoff
);
1830 write_pointer_tfs(file
, ft
, tfsoff
);
1832 else if (!ft
->declarray
&& is_conformant_array(ft
))
1834 unsigned int absoff
= ft
->typestring_offset
;
1835 short reloff
= absoff
- (*tfsoff
+ 2);
1836 int ptr_type
= get_attrv(f
->attrs
, ATTR_POINTERTYPE
);
1837 /* FIXME: We need to store pointer attributes for arrays
1838 so we don't lose pointer_default info. */
1840 ptr_type
= RPC_FC_UP
;
1841 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1842 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
1843 string_of_type(ptr_type
));
1844 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1845 reloff
, reloff
, absoff
);
1849 if (type
->ptrdesc
== *tfsoff
)
1853 current_structure
= save_current_structure
;
1854 return start_offset
;
1857 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1858 unsigned char flags
, size_t offset
,
1859 unsigned int *typeformat_offset
)
1861 size_t start_offset
= *typeformat_offset
;
1862 short reloff
= offset
- (*typeformat_offset
+ 2);
1863 int in_attr
, out_attr
;
1864 in_attr
= is_attr(attrs
, ATTR_IN
);
1865 out_attr
= is_attr(attrs
, ATTR_OUT
);
1866 if (!in_attr
&& !out_attr
) in_attr
= 1;
1868 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1871 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1874 string_of_type(pointer_type
));
1878 fprintf(file
, " [allocated_on_stack]");
1880 fprintf(file
, " [pointer_deref]");
1881 fprintf(file
, " */\n");
1884 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1885 *typeformat_offset
+= 4;
1887 return start_offset
;
1890 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1894 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1896 else if (is_base_type(t
->type
))
1898 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1899 t
->type
, string_of_type(t
->type
));
1901 else if (t
->typestring_offset
)
1903 short reloff
= t
->typestring_offset
- *tfsoff
;
1904 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1905 reloff
, reloff
, t
->typestring_offset
);
1908 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1913 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1915 unsigned int align
= 0;
1916 unsigned int start_offset
;
1917 size_t size
= type_memsize(type
, &align
);
1920 type_t
*deftype
= NULL
;
1921 short nodeftype
= 0xffff;
1926 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1928 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
1929 fields
= uv
->type
->fields_or_args
;
1932 fields
= type
->fields_or_args
;
1934 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1936 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1938 nbranch
+= list_count(cases
);
1940 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
1943 start_offset
= *tfsoff
;
1944 update_tfsoff(type
, start_offset
, file
);
1945 print_start_tfs_comment(file
, type
, start_offset
);
1946 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1948 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields_or_args
), const var_t
, entry
);
1949 const type_t
*st
= sv
->type
;
1962 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1963 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
1964 0x40 | st
->type
, string_of_type(st
->type
));
1968 error("union switch type must be an integer, char, or enum\n");
1971 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1972 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1975 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1977 type_t
*ft
= f
->type
;
1978 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1979 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1982 if (cases
== NULL
&& !deflt
)
1983 error("union field %s with neither case nor default attribute\n", f
->name
);
1985 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1987 /* MIDL doesn't check for duplicate cases, even though that seems
1988 like a reasonable thing to do, it just dumps them to the TFS
1989 like we're going to do here. */
1990 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1992 write_branch_type(file
, ft
, tfsoff
);
1995 /* MIDL allows multiple default branches, even though that seems
1996 illogical, it just chooses the last one, which is what we will
2007 write_branch_type(file
, deftype
, tfsoff
);
2011 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
2015 return start_offset
;
2018 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2019 unsigned int *typeformat_offset
)
2022 size_t start_offset
= *typeformat_offset
;
2023 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2027 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2028 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2030 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2034 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
2035 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2038 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2040 update_tfsoff(type
, start_offset
, file
);
2041 print_start_tfs_comment(file
, type
, start_offset
);
2042 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2043 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2044 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
2045 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2046 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2047 for (i
= 0; i
< 8; ++i
)
2048 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2051 fprintf(file
, "\n");
2053 *typeformat_offset
+= 18;
2055 return start_offset
;
2058 static size_t write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2060 unsigned int *typeformat_offset
)
2062 size_t start_offset
= *typeformat_offset
;
2063 unsigned char flags
= 0;
2065 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2066 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2070 if (is_attr(var
->attrs
, ATTR_IN
))
2073 if (!is_attr(var
->attrs
, ATTR_OUT
))
2074 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2076 if (is_attr(var
->attrs
, ATTR_OUT
))
2079 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2080 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2081 /* return and can't be null values overlap */
2082 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2083 print_file(file
, 0, "can't be null, ");
2084 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2085 print_file(file
, 0, "serialize, ");
2086 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2087 print_file(file
, 0, "no serialize, ");
2088 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2089 print_file(file
, 0, "strict, ");
2090 if ((flags
& 0x21) == 0x20)
2091 print_file(file
, 0, "out, ");
2092 if ((flags
& 0x21) == 0x21)
2093 print_file(file
, 0, "return, ");
2095 print_file(file
, 0, "in, ");
2097 print_file(file
, 0, "via ptr, ");
2098 print_file(file
, 0, "*/\n");
2099 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2100 print_file(file
, 2, "0, /* FIXME: param num */\n");
2101 *typeformat_offset
+= 4;
2103 return start_offset
;
2106 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
2107 type_t
*type
, const var_t
*var
,
2108 unsigned int *typeformat_offset
)
2112 if (is_context_handle(type
))
2113 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2115 if (is_user_type(type
))
2117 write_user_tfs(file
, type
, typeformat_offset
);
2118 return type
->typestring_offset
;
2121 if (is_string_type(var
->attrs
, type
))
2122 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2128 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2129 ptr_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2130 /* Top level pointers to conformant arrays may be handled specially
2131 since we can bypass the pointer, but if the array is buried
2132 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2133 always need to write the pointer. */
2134 if (!ptr_type
&& var
->type
!= type
)
2135 /* FIXME: This should use pointer_default, but the information
2136 isn't kept around for arrays. */
2137 ptr_type
= RPC_FC_UP
;
2138 if (ptr_type
&& ptr_type
!= RPC_FC_RP
)
2140 unsigned int absoff
= type
->typestring_offset
;
2141 short reloff
= absoff
- (*typeformat_offset
+ 2);
2142 off
= *typeformat_offset
;
2143 print_file(file
, 0, "/* %d */\n", off
);
2144 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2145 string_of_type(ptr_type
));
2146 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2147 reloff
, reloff
, absoff
);
2148 *typeformat_offset
+= 4;
2155 /* basic types don't need a type format string */
2156 if (is_base_type(type
->type
))
2162 case RPC_FC_PSTRUCT
:
2163 case RPC_FC_CSTRUCT
:
2164 case RPC_FC_CPSTRUCT
:
2165 case RPC_FC_CVSTRUCT
:
2166 case RPC_FC_BOGUS_STRUCT
:
2167 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2168 case RPC_FC_ENCAPSULATED_UNION
:
2169 case RPC_FC_NON_ENCAPSULATED_UNION
:
2170 return write_union_tfs(file
, type
, typeformat_offset
);
2172 case RPC_FC_BIND_PRIMITIVE
:
2176 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
2179 else if (last_ptr(type
))
2181 size_t start_offset
= *typeformat_offset
;
2182 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2183 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2184 const type_t
*base
= type
->ref
;
2186 if (base
->type
== RPC_FC_IP
2188 && is_attr(var
->attrs
, ATTR_IIDIS
)))
2190 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2193 /* special case for pointers to base types */
2194 if (is_base_type(base
->type
))
2196 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2197 type
->type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2198 string_of_type(type
->type
),
2199 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2200 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2201 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2202 *typeformat_offset
+= 4;
2203 return start_offset
;
2207 assert(is_ptr(type
));
2209 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2211 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2212 return write_pointer_only_tfs(file
, var
->attrs
, type
->type
,
2213 !last_ptr(type
) ? 0x10 : 0,
2214 offset
, typeformat_offset
);
2217 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2218 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2222 if (is_user_type(type
))
2224 write_user_tfs(file
, type
, tfsoff
);
2226 else if (is_string_type(attrs
, type
))
2228 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2230 else if (is_ptr(type
))
2232 type_t
*ref
= type
->ref
;
2234 if (ref
->type
== RPC_FC_IP
2236 && is_attr(attrs
, ATTR_IIDIS
)))
2238 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2242 if (!processed(ref
) && !is_base_type(ref
->type
))
2243 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2246 write_pointer_tfs(file
, type
, tfsoff
);
2251 else if (type
->declarray
&& is_conformant_array(type
))
2252 ; /* conformant arrays and strings are handled specially */
2253 else if (is_array(type
))
2255 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2256 if (is_conformant_array(type
))
2259 else if (is_struct(type
->type
))
2261 if (!processed(type
))
2262 write_struct_tfs(file
, type
, name
, tfsoff
);
2264 else if (is_union(type
->type
))
2266 if (!processed(type
))
2267 write_union_tfs(file
, type
, tfsoff
);
2269 else if (!is_base_type(type
->type
))
2270 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2276 static size_t process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2277 type_pred_t pred
, unsigned int *typeformat_offset
)
2280 const statement_t
*stmt
;
2282 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2284 const type_t
*iface
;
2285 if (stmt
->type
== STMT_LIBRARY
)
2287 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2290 else if (stmt
->type
!= STMT_TYPE
|| stmt
->u
.type
->type
!= RPC_FC_IP
)
2293 iface
= stmt
->u
.type
;
2300 current_iface
= iface
;
2301 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
2303 if (is_local(func
->def
->attrs
)) continue;
2305 if (!is_void(get_func_return_type(func
)))
2307 var_t v
= *func
->def
;
2308 v
.type
= get_func_return_type(func
);
2309 update_tfsoff(get_func_return_type(func
),
2310 write_typeformatstring_var(
2311 file
, 2, NULL
, get_func_return_type(func
),
2312 &v
, typeformat_offset
),
2316 current_func
= func
;
2318 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2321 write_typeformatstring_var(
2322 file
, 2, func
, var
->type
, var
,
2329 return *typeformat_offset
+ 1;
2332 static size_t process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2334 unsigned int typeformat_offset
= 2;
2336 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2340 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2344 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2345 print_file(file
, indent
, "{\n");
2347 print_file(file
, indent
, "0,\n");
2348 print_file(file
, indent
, "{\n");
2350 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2352 set_all_tfswrite(TRUE
);
2353 process_tfs(file
, stmts
, pred
);
2355 print_file(file
, indent
, "0x0\n");
2357 print_file(file
, indent
, "}\n");
2359 print_file(file
, indent
, "};\n");
2360 print_file(file
, indent
, "\n");
2363 static unsigned int get_required_buffer_size_type(
2364 const type_t
*type
, const char *name
, unsigned int *alignment
)
2367 if (is_user_type(type
))
2370 const type_t
*utype
= get_user_type(type
, &uname
);
2371 return get_required_buffer_size_type(utype
, uname
, alignment
);
2395 case RPC_FC_ERROR_STATUS_T
:
2405 case RPC_FC_BIND_PRIMITIVE
:
2409 case RPC_FC_PSTRUCT
:
2413 if (!type
->fields_or_args
) return 0;
2414 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2416 unsigned int alignment
;
2417 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2425 is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
2426 ? get_required_buffer_size_type( type
->ref
, name
, alignment
)
2429 case RPC_FC_SMFARRAY
:
2430 case RPC_FC_LGFARRAY
:
2431 return type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2439 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2441 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2442 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2445 if (!in_attr
&& !out_attr
)
2450 for (t
= var
->type
; is_ptr(t
); t
= t
->ref
)
2451 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
2457 if (pass
== PASS_OUT
)
2459 if (out_attr
&& is_ptr(var
->type
))
2461 type_t
*type
= var
->type
;
2463 if (type
->type
== RPC_FC_STRUCT
)
2466 unsigned int size
= 36;
2468 if (!type
->fields_or_args
) return size
;
2469 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2472 size
+= get_required_buffer_size_type(
2473 field
->type
, field
->name
, &align
);
2482 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2483 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2485 if (is_ptr(var
->type
))
2487 type_t
*type
= var
->type
;
2489 if (is_base_type(type
->type
))
2493 else if (type
->type
== RPC_FC_STRUCT
)
2495 unsigned int size
= 36;
2498 if (!type
->fields_or_args
) return size
;
2499 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2502 size
+= get_required_buffer_size_type(
2503 field
->type
, field
->name
, &align
);
2510 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2514 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2517 unsigned int total_size
= 0, alignment
;
2521 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2523 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2524 total_size
+= alignment
;
2528 if (pass
== PASS_OUT
&& !is_void(get_func_return_type(func
)))
2530 var_t v
= *func
->def
;
2531 v
.type
= get_func_return_type(func
);
2532 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
2533 total_size
+= alignment
;
2538 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2539 enum remoting_phase phase
,
2540 const var_t
*var
, unsigned int type_offset
)
2542 const char *function
;
2545 case PHASE_BUFFERSIZE
:
2546 function
= "BufferSize";
2549 function
= "Marshall";
2551 case PHASE_UNMARSHAL
:
2552 function
= "Unmarshall";
2562 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2564 print_file(file
, indent
, "&_StubMsg,\n");
2565 print_file(file
, indent
, "%s%s%s%s,\n",
2566 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2567 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2568 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2570 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2571 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2572 if (phase
== PHASE_UNMARSHAL
)
2573 print_file(file
, indent
, "0);\n");
2577 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2578 enum pass pass
, const var_t
*var
,
2579 const char *varname
)
2581 type_t
*type
= var
->type
;
2583 unsigned int alignment
= 0;
2584 unsigned char rtype
;
2586 /* no work to do for other phases, buffer sizing is done elsewhere */
2587 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2590 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2614 case RPC_FC_ERROR_STATUS_T
:
2626 case RPC_FC_BIND_PRIMITIVE
:
2627 /* no marshalling needed */
2631 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2635 if (phase
== PHASE_MARSHAL
)
2636 print_file(file
, indent
, "MIDL_memset(_StubMsg.Buffer, 0, (0x%x - (long)_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
2637 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2638 alignment
- 1, alignment
- 1);
2640 if (phase
== PHASE_MARSHAL
)
2642 print_file(file
, indent
, "*(");
2643 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2645 fprintf(file
, " *)_StubMsg.Buffer = *");
2647 fprintf(file
, " *)_StubMsg.Buffer = ");
2648 fprintf(file
, "%s", varname
);
2649 fprintf(file
, ";\n");
2651 else if (phase
== PHASE_UNMARSHAL
)
2653 print_file(file
, indent
, "if (_StubMsg.Buffer + sizeof(");
2654 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2655 fprintf(file
, ") > _StubMsg.BufferEnd)\n");
2656 print_file(file
, indent
, "{\n");
2657 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2658 print_file(file
, indent
, "}\n");
2659 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2660 print_file(file
, indent
, "");
2662 print_file(file
, indent
, "*");
2663 fprintf(file
, "%s", varname
);
2664 if (pass
== PASS_IN
&& is_ptr(type
))
2665 fprintf(file
, " = (");
2667 fprintf(file
, " = *(");
2668 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2669 fprintf(file
, " *)_StubMsg.Buffer;\n");
2672 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2673 write_type_decl(file
, var
->type
, NULL
);
2674 fprintf(file
, ");\n");
2677 /* returns whether the MaxCount, Offset or ActualCount members need to be
2678 * filled in for the specified phase */
2679 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2681 return (phase
!= PHASE_UNMARSHAL
);
2684 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
2688 for ( ; is_ptr(t
) || is_array(t
); t
= t
->ref
)
2694 error("%s: multidimensional conformant"
2695 " arrays not supported at the top level\n",
2702 static void write_remoting_arg(FILE *file
, int indent
, const func_t
*func
,
2703 enum pass pass
, enum remoting_phase phase
,
2706 int in_attr
, out_attr
, pointer_type
;
2707 const type_t
*type
= var
->type
;
2708 unsigned char rtype
;
2709 size_t start_offset
= type
->typestring_offset
;
2711 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2713 pointer_type
= RPC_FC_RP
;
2715 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2716 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2717 if (!in_attr
&& !out_attr
)
2720 if (phase
!= PHASE_FREE
)
2724 if (!in_attr
) return;
2727 if (!out_attr
) return;
2735 if (is_context_handle(type
))
2737 if (phase
== PHASE_MARSHAL
)
2739 if (pass
== PASS_IN
)
2741 /* if the context_handle attribute appears in the chain of types
2742 * without pointers being followed, then the context handle must
2743 * be direct, otherwise it is a pointer */
2744 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
2745 print_file(file
, indent
, "NdrClientContextMarshall(\n");
2746 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2747 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s,\n", is_ch_ptr
? "*" : "", var
->name
);
2748 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
2752 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
2753 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2754 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s,\n", var
->name
);
2755 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
2756 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2759 else if (phase
== PHASE_UNMARSHAL
)
2761 if (pass
== PASS_OUT
)
2764 print_file(file
, indent
, "*%s = 0;\n", var
->name
);
2765 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
2766 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2767 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s,\n", var
->name
);
2768 print_file(file
, indent
+ 1, "_Handle);\n");
2772 print_file(file
, indent
, "%s = NdrServerContextNewUnmarshall(\n", var
->name
);
2773 print_file(file
, indent
+ 1, "&_StubMsg,\n");
2774 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2778 else if (is_user_type(var
->type
))
2780 print_phase_function(file
, indent
, "UserMarshal", phase
, var
, start_offset
);
2782 else if (is_string_type(var
->attrs
, var
->type
))
2784 if (is_array(type
) && !is_conformant_array(type
))
2785 print_phase_function(file
, indent
, "NonConformantString", phase
, var
, start_offset
);
2788 if (type
->size_is
&& is_size_needed_for_phase(phase
))
2790 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2791 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2792 fprintf(file
, ";\n");
2795 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
|| pointer_type
== RPC_FC_UP
)
2796 print_phase_function(file
, indent
, "Pointer", phase
, var
,
2797 start_offset
- (type
->size_is
? 4 : 2));
2799 print_phase_function(file
, indent
, "ConformantString", phase
, var
,
2803 else if (is_array(type
))
2805 unsigned char tc
= type
->type
;
2806 const char *array_type
= "FixedArray";
2808 /* We already have the size_is expression since it's at the
2809 top level, but do checks for multidimensional conformant
2810 arrays. When we handle them, we'll need to extend this
2811 function to return a list, and then we'll actually use
2812 the return value. */
2813 get_size_is_expr(type
, var
->name
);
2815 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2817 if (is_size_needed_for_phase(phase
))
2819 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2820 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2821 write_expr(file
, type
->length_is
, 1, 1, NULL
, NULL
);
2822 fprintf(file
, ";\n\n");
2824 array_type
= "VaryingArray";
2826 else if (tc
== RPC_FC_CARRAY
)
2828 if (is_size_needed_for_phase(phase
))
2830 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2831 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2832 fprintf(file
, ";\n\n");
2834 array_type
= "ConformantArray";
2836 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
2838 if (is_size_needed_for_phase(phase
))
2842 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2843 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
);
2844 fprintf(file
, ";\n");
2846 if (type
->length_is
)
2848 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2849 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2850 write_expr(file
, type
->length_is
, 1, 1, NULL
, NULL
);
2851 fprintf(file
, ";\n\n");
2854 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
2856 : "ConformantVaryingArray");
2859 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
2860 print_phase_function(file
, indent
, array_type
, phase
, var
, start_offset
);
2861 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
2863 /* these are all unmarshalled by allocating memory */
2864 if (type
->type
== RPC_FC_BOGUS_ARRAY
||
2865 type
->type
== RPC_FC_CVARRAY
||
2866 ((type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
) && in_attr
) ||
2867 (type
->type
== RPC_FC_CARRAY
&& !in_attr
))
2869 print_file(file
, indent
, "if (%s)\n", var
->name
);
2871 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2875 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2877 if (phase
!= PHASE_FREE
)
2878 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2880 else if (!is_ptr(var
->type
))
2885 case RPC_FC_PSTRUCT
:
2886 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
);
2888 case RPC_FC_CSTRUCT
:
2889 case RPC_FC_CPSTRUCT
:
2890 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
, start_offset
);
2892 case RPC_FC_CVSTRUCT
:
2893 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
, start_offset
);
2895 case RPC_FC_BOGUS_STRUCT
:
2896 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
, start_offset
);
2899 if (is_base_type( var
->type
->ref
->type
))
2901 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2903 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2905 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2906 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2911 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2913 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long) " );
2914 write_expr( file
, iid
, 1, 1, NULL
, NULL
);
2915 fprintf( file
, ";\n\n" );
2917 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2921 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2926 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2928 if (phase
!= PHASE_FREE
)
2929 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2931 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2933 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2934 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2939 expr_t
*sx
= get_size_is_expr(type
, var
->name
);
2941 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2943 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long) " );
2944 write_expr( file
, iid
, 1, 1, NULL
, NULL
);
2945 fprintf( file
, ";\n\n" );
2949 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long) ");
2950 write_expr(file
, sx
, 1, 1, NULL
, NULL
);
2951 fprintf(file
, ";\n\n");
2953 if (var
->type
->ref
->type
== RPC_FC_IP
)
2954 print_phase_function(file
, indent
, "InterfacePointer", phase
, var
, start_offset
);
2956 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2959 fprintf(file
, "\n");
2962 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2963 enum pass pass
, enum remoting_phase phase
)
2965 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
2967 unsigned int size
= get_function_buffer_size( func
, pass
);
2968 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2971 if (pass
== PASS_RETURN
)
2975 var
.type
= get_func_return_type(func
);
2976 var
.name
= xstrdup( "_RetVal" );
2977 write_remoting_arg( file
, indent
, func
, pass
, phase
, &var
);
2985 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2986 write_remoting_arg( file
, indent
, func
, pass
, phase
, var
);
2991 size_t get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
2993 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
2997 size_t get_size_procformatstring_func(const func_t
*func
)
3002 /* argument list size */
3004 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3005 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3007 /* return value size */
3008 if (is_void(get_func_return_type(func
)))
3009 size
+= 2; /* FC_END and FC_PAD */
3011 size
+= get_size_procformatstring_type("return value", get_func_return_type(func
), NULL
);
3016 size_t get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3018 const statement_t
*stmt
;
3022 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3024 const type_t
*iface
;
3025 if (stmt
->type
== STMT_LIBRARY
)
3027 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3030 else if (stmt
->type
!= STMT_TYPE
&& stmt
->u
.type
->type
!= RPC_FC_IP
)
3033 iface
= stmt
->u
.type
;
3038 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
3039 if (!is_local(func
->def
->attrs
))
3040 size
+= get_size_procformatstring_func( func
);
3045 size_t get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3047 set_all_tfswrite(FALSE
);
3048 return process_tfs(NULL
, stmts
, pred
);
3051 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
3053 int in_attr
, out_attr
;
3057 /* declare return value '_RetVal' */
3058 if (!is_void(get_func_return_type(func
)))
3060 print_file(file
, indent
, "");
3061 write_type_decl_left(file
, get_func_return_type(func
));
3062 fprintf(file
, " _RetVal;\n");
3068 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3070 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3072 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3073 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3074 if (!out_attr
&& !in_attr
)
3077 if (is_context_handle(var
->type
))
3078 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3081 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
3083 print_file(file
, indent
, "");
3084 write_type_decl(file
, var
->type
->declarray
? var
->type
: var
->type
->ref
,
3086 fprintf(file
, ";\n");
3089 print_file(file
, indent
, "");
3090 write_type_decl_left(file
, var
->type
);
3092 if (var
->type
->declarray
) {
3093 fprintf(file
, "( *");
3094 write_name(file
, var
);
3095 fprintf(file
, " )");
3097 write_name(file
, var
);
3098 write_type_right(file
, var
->type
, FALSE
);
3099 fprintf(file
, ";\n");
3101 if (decl_indirect(var
->type
))
3102 print_file(file
, indent
, "void *_p_%s = &%s;\n",
3103 var
->name
, var
->name
);
3109 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
3111 int in_attr
, out_attr
;
3118 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3120 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3121 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3122 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3123 if (!out_attr
&& !in_attr
)
3128 print_file(file
, indent
, "");
3129 write_name(file
, var
);
3131 if (is_context_handle(var
->type
))
3133 fprintf(file
, " = NdrContextHandleInitialize(\n");
3134 print_file(file
, indent
+ 1, "&_StubMsg,\n");
3135 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3136 var
->type
->typestring_offset
);
3138 else if (var
->type
->size_is
)
3140 unsigned int size
, align
= 0;
3141 type_t
*type
= var
->type
;
3143 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
3144 for ( ; type
->size_is
; type
= type
->ref
)
3146 write_expr(file
, type
->size_is
, TRUE
, TRUE
, NULL
, NULL
);
3147 fprintf(file
, " * ");
3149 size
= type_memsize(type
, &align
);
3150 fprintf(file
, "%u);\n", size
);
3152 else if (!is_string
)
3154 fprintf(file
, " = &_W%u;\n", i
);
3155 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
3156 print_file(file
, indent
, "_W%u = 0;\n", i
);
3164 fprintf(file
, "\n");
3168 int write_expr_eval_routines(FILE *file
, const char *iface
)
3170 static const char *var_name
= "pS";
3171 static const char *var_name_expr
= "pS->";
3173 struct expr_eval_routine
*eval
;
3174 unsigned short callback_offset
= 0;
3176 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3178 const char *name
= eval
->structure
->name
;
3181 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3182 iface
, name
, callback_offset
);
3183 print_file(file
, 0, "{\n");
3184 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3185 name
, var_name
, name
, eval
->baseoff
);
3186 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3187 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
3188 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
);
3189 fprintf(file
, ";\n");
3190 print_file(file
, 0, "}\n\n");
3196 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3198 struct expr_eval_routine
*eval
;
3199 struct expr_eval_routine
*cursor
;
3200 unsigned short callback_offset
= 0;
3202 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3203 fprintf(file
, "{\n");
3205 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3207 const char *name
= eval
->structure
->name
;
3208 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3210 list_remove(&eval
->entry
);
3214 fprintf(file
, "};\n\n");
3217 void write_user_quad_list(FILE *file
)
3221 if (list_empty(&user_type_list
))
3224 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3225 fprintf(file
, "{\n");
3226 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3228 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3229 print_file(file
, 1, "{\n");
3230 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3231 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3232 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3233 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3234 print_file(file
, 1, "}%s\n", sep
);
3236 fprintf(file
, "};\n\n");
3239 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3241 const struct str_list_entry_t
*endpoint
;
3244 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3245 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3246 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3248 print_file( f
, 1, "{ (const unsigned char *)\"" );
3249 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3251 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3254 if (!*p
) goto error
;
3255 if (p
[1] != '[') goto error
;
3257 fprintf( f
, "\", (const unsigned char *)\"" );
3258 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3260 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3263 if (*p
!= ']') goto error
;
3264 fprintf( f
, "\" },\n" );
3266 print_file( f
, 0, "};\n\n" );
3270 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);