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
, const char *local_var_prefix
)
346 if (decl_indirect(t
))
348 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
349 local_var_prefix
, n
, local_var_prefix
, n
);
350 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
352 else if (is_ptr(t
) || is_array(t
))
353 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
356 void write_parameters_init(FILE *file
, int indent
, const func_t
*func
, const char *local_var_prefix
)
360 if (!is_void(get_func_return_type(func
)))
361 write_var_init(file
, indent
, get_func_return_type(func
), "_RetVal", local_var_prefix
);
366 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
367 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
372 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
374 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
375 print_file(f
, indent
, "{\n");
376 print_file(f
, indent
+ 1, "short Pad;\n");
377 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
378 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
379 print_file(f
, indent
, "\n");
382 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
384 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
385 get_size_typeformatstring(stmts
, pred
));
387 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
388 get_size_procformatstring(stmts
, pred
));
391 write_formatdesc(f
, indent
, "TYPE");
392 write_formatdesc(f
, indent
, "PROC");
394 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
395 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
396 print_file(f
, indent
, "\n");
399 static inline int is_base_type(unsigned char type
)
418 case RPC_FC_ERROR_STATUS_T
:
419 case RPC_FC_BIND_PRIMITIVE
:
427 int decl_indirect(const type_t
*t
)
429 return is_user_type(t
)
430 || (!is_base_type(t
->type
)
435 static size_t write_procformatstring_type(FILE *file
, int indent
,
438 const attr_list_t
*attrs
,
443 int is_in
= is_attr(attrs
, ATTR_IN
);
444 int is_out
= is_attr(attrs
, ATTR_OUT
);
446 if (!is_in
&& !is_out
) is_in
= TRUE
;
448 if (!type
->declarray
&& is_base_type(type
->type
))
451 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
453 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
455 if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
457 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
458 size
= 2; /* includes param type prefix */
460 else if (is_base_type(type
->type
))
462 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
463 size
= 2; /* includes param type prefix */
467 error("Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
474 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
475 else if (is_in
&& is_out
)
476 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
478 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
480 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
482 print_file(file
, indent
, "0x01,\n");
483 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
484 size
= 4; /* includes param type prefix */
489 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
491 const statement_t
*stmt
;
492 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
494 if (stmt
->type
== STMT_TYPE
&& stmt
->u
.type
->type
== RPC_FC_IP
)
497 if (!pred(stmt
->u
.type
))
499 if (stmt
->u
.type
->funcs
) LIST_FOR_EACH_ENTRY( func
, stmt
->u
.type
->funcs
, const func_t
, entry
)
501 if (is_local(func
->def
->attrs
)) continue;
502 /* emit argument data */
506 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
507 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
510 /* emit return value data */
511 if (is_void(get_func_return_type(func
)))
513 print_file(file
, indent
, "0x5b, /* FC_END */\n");
514 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
517 write_procformatstring_type(file
, indent
, "return value", get_func_return_type(func
), NULL
, TRUE
);
520 else if (stmt
->type
== STMT_LIBRARY
)
521 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
525 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
529 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
530 print_file(file
, indent
, "{\n");
532 print_file(file
, indent
, "0,\n");
533 print_file(file
, indent
, "{\n");
536 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
538 print_file(file
, indent
, "0x0\n");
540 print_file(file
, indent
, "}\n");
542 print_file(file
, indent
, "};\n");
543 print_file(file
, indent
, "\n");
546 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
548 if (is_base_type(type
->type
))
550 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
551 *typestring_offset
+= 1;
558 /* write conformance / variance descriptor */
559 static size_t write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
560 unsigned int baseoff
, const type_t
*type
,
563 unsigned char operator_type
= 0;
564 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
565 const char *conftype_string
= "";
566 const char *operator_string
= "no operators";
567 const expr_t
*subexpr
;
571 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
577 /* Top-level conformance calculations are done inline. */
578 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
579 RPC_FC_TOP_LEVEL_CONFORMANCE
);
580 print_file (file
, 2, "0x0,\n");
581 print_file (file
, 2, "NdrFcShort(0x0),\n");
587 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
588 error("write_conf_or_var_desc: constant value %ld is greater than "
589 "the maximum constant size of %d\n", expr
->cval
,
590 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
592 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
593 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
594 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
595 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
600 if (is_ptr(type
) || (is_array(type
) && !type
->declarray
))
602 conftype
= RPC_FC_POINTER_CONFORMANCE
;
603 conftype_string
= "field pointer, ";
607 switch (subexpr
->type
)
610 subexpr
= subexpr
->ref
;
611 operator_type
= RPC_FC_DEREFERENCE
;
612 operator_string
= "FC_DEREFERENCE";
615 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
617 subexpr
= subexpr
->ref
;
618 operator_type
= RPC_FC_DIV_2
;
619 operator_string
= "FC_DIV_2";
623 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
625 subexpr
= subexpr
->ref
;
626 operator_type
= RPC_FC_MULT_2
;
627 operator_string
= "FC_MULT_2";
631 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
633 subexpr
= subexpr
->ref
;
634 operator_type
= RPC_FC_SUB_1
;
635 operator_string
= "FC_SUB_1";
639 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
641 subexpr
= subexpr
->ref
;
642 operator_type
= RPC_FC_ADD_1
;
643 operator_string
= "FC_ADD_1";
650 if (subexpr
->type
== EXPR_IDENTIFIER
)
652 const type_t
*correlation_variable
= NULL
;
653 unsigned char correlation_variable_type
;
654 unsigned char param_type
= 0;
658 if (structure
->fields_or_args
) LIST_FOR_EACH_ENTRY( var
, structure
->fields_or_args
, const var_t
, entry
)
660 unsigned int align
= 0;
661 /* FIXME: take alignment into account */
662 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
664 correlation_variable
= var
->type
;
667 offset
+= type_memsize(var
->type
, &align
);
669 if (!correlation_variable
)
670 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
673 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
676 correlation_variable_type
= correlation_variable
->type
;
678 switch (correlation_variable_type
)
682 param_type
= RPC_FC_SMALL
;
686 param_type
= RPC_FC_USMALL
;
691 param_type
= RPC_FC_SHORT
;
694 param_type
= RPC_FC_USHORT
;
698 param_type
= RPC_FC_LONG
;
701 param_type
= RPC_FC_ULONG
;
704 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
705 correlation_variable_type
);
708 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
709 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
710 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
711 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
716 unsigned int callback_offset
= 0;
717 struct expr_eval_routine
*eval
;
720 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
722 if (!strcmp (eval
->structure
->name
, structure
->name
)
723 && !compare_expr (eval
->expr
, expr
))
733 eval
= xmalloc (sizeof(*eval
));
734 eval
->structure
= structure
;
735 eval
->baseoff
= baseoff
;
737 list_add_tail (&expr_eval_routines
, &eval
->entry
);
740 if (callback_offset
> USHRT_MAX
)
741 error("Maximum number of callback routines reached\n");
743 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
744 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
745 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
750 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
752 int have_align
= FALSE
;
756 if (!fields
) return 0;
757 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
759 unsigned int falign
= 0;
760 size_t fsize
= type_memsize(v
->type
, &falign
);
766 size
= ROUND_SIZE(size
, falign
);
770 size
= ROUND_SIZE(size
, *align
);
774 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
776 size_t size
, maxs
= 0;
777 unsigned int align
= *pmaxa
;
780 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
782 /* we could have an empty default field with NULL type */
785 size
= type_memsize(v
->type
, &align
);
786 if (maxs
< size
) maxs
= size
;
787 if (*pmaxa
< align
) *pmaxa
= align
;
794 int get_padding(const var_list_t
*fields
)
796 unsigned short offset
= 0;
803 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
805 type_t
*ft
= f
->type
;
806 unsigned int align
= 0;
807 size_t size
= type_memsize(ft
, &align
);
810 offset
= ROUND_SIZE(offset
, align
);
814 return ROUNDING(offset
, salign
);
817 size_t type_memsize(const type_t
*t
, unsigned int *align
)
821 if (t
->kind
== TKIND_ALIAS
)
822 size
= type_memsize(t
->orig
, align
);
823 else if (t
->declarray
&& is_conformant_array(t
))
825 type_memsize(t
->ref
, align
);
828 else if (is_ptr(t
) || is_conformant_array(t
))
830 size
= sizeof(void *);
831 if (size
> *align
) *align
= size
;
833 else switch (t
->type
)
840 if (size
> *align
) *align
= size
;
847 if (size
> *align
) *align
= size
;
851 case RPC_FC_ERROR_STATUS_T
:
855 if (size
> *align
) *align
= size
;
860 if (size
> *align
) *align
= size
;
863 case RPC_FC_CVSTRUCT
:
864 case RPC_FC_CPSTRUCT
:
867 case RPC_FC_BOGUS_STRUCT
:
868 size
= fields_memsize(t
->fields_or_args
, align
);
870 case RPC_FC_ENCAPSULATED_UNION
:
871 case RPC_FC_NON_ENCAPSULATED_UNION
:
872 size
= union_memsize(t
->fields_or_args
, align
);
874 case RPC_FC_SMFARRAY
:
875 case RPC_FC_LGFARRAY
:
876 case RPC_FC_SMVARRAY
:
877 case RPC_FC_LGVARRAY
:
878 case RPC_FC_BOGUS_ARRAY
:
879 size
= t
->dim
* type_memsize(t
->ref
, align
);
882 error("type_memsize: Unknown type %d\n", t
->type
);
889 int is_full_pointer_function(const func_t
*func
)
892 if (type_has_full_pointer(get_func_return_type(func
)))
896 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
897 if (type_has_full_pointer( var
->type
))
902 void write_full_pointer_init(FILE *file
, int indent
, const func_t
*func
, int is_server
)
904 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
905 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
909 void write_full_pointer_free(FILE *file
, int indent
, const func_t
*func
)
911 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
915 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
917 short absoff
= type
->ref
->typestring_offset
;
918 short reloff
= absoff
- (offset
+ 2);
919 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
921 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
922 type
->type
, ptr_attr
, string_of_type(type
->type
));
923 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
924 reloff
, reloff
, absoff
);
928 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
930 unsigned char fc
= type
->ref
->type
;
931 /* for historical reasons, write_simple_pointer also handled string types,
932 * but no longer does. catch bad uses of the function with this check */
933 if (is_string_type(type
->attrs
, type
))
934 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
935 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
936 type
->type
, string_of_type(type
->type
));
937 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
938 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
942 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
944 print_file(file
, 0, "/* %u (", tfsoff
);
945 write_type_decl(file
, t
, NULL
);
946 print_file(file
, 0, ") */\n");
949 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
951 unsigned int offset
= *typestring_offset
;
953 print_start_tfs_comment(file
, type
, offset
);
954 update_tfsoff(type
, offset
, file
);
956 if (type
->ref
->typestring_offset
)
957 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
958 else if (is_base_type(type
->ref
->type
))
959 *typestring_offset
+= write_simple_pointer(file
, type
);
964 static int processed(const type_t
*type
)
966 return type
->typestring_offset
&& !type
->tfswrite
;
969 static int user_type_has_variable_size(const type_t
*t
)
978 case RPC_FC_CPSTRUCT
:
979 case RPC_FC_CVSTRUCT
:
982 /* Note: Since this only applies to user types, we can't have a conformant
983 array here, and strings should get filed under pointer in this case. */
987 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
989 unsigned int start
, absoff
, flags
;
990 unsigned int align
= 0, ualign
= 0;
991 const char *name
= NULL
;
992 type_t
*utype
= get_user_type(type
, &name
);
993 size_t usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
994 size_t size
= type_memsize(type
, &align
);
995 unsigned short funoff
= user_type_offset(name
);
1000 if (is_base_type(utype
->type
))
1003 print_start_tfs_comment(file
, utype
, absoff
);
1004 print_file(file
, 2, "0x%x,\t/* %s */\n", utype
->type
, string_of_type(utype
->type
));
1005 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1010 if (!processed(utype
))
1011 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1012 absoff
= utype
->typestring_offset
;
1015 if (utype
->type
== RPC_FC_RP
)
1017 else if (utype
->type
== RPC_FC_UP
)
1023 update_tfsoff(type
, start
, file
);
1024 print_start_tfs_comment(file
, type
, start
);
1025 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1026 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1027 flags
| (align
- 1), align
- 1, flags
);
1028 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1029 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size
, size
);
1030 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize
, usize
);
1032 reloff
= absoff
- *tfsoff
;
1033 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff
, reloff
, absoff
);
1037 static void write_member_type(FILE *file
, const type_t
*cont
,
1038 const attr_list_t
*attrs
, const type_t
*type
,
1039 unsigned int *corroff
, unsigned int *tfsoff
)
1041 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1046 if (is_union(type
->type
) && is_attr(attrs
, ATTR_SWITCHIS
))
1053 absoff
= type
->typestring_offset
;
1055 reloff
= absoff
- (*tfsoff
+ 2);
1057 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1058 /* FIXME: actually compute necessary padding */
1059 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1060 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1061 reloff
, reloff
, absoff
);
1064 else if (is_ptr(type
) || is_conformant_array(type
))
1066 unsigned char fc
= (cont
->type
== RPC_FC_BOGUS_STRUCT
1069 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1072 else if (!write_base_type(file
, type
, tfsoff
))
1073 error("Unsupported member type 0x%x\n", type
->type
);
1076 static void write_end(FILE *file
, unsigned int *tfsoff
)
1078 if (*tfsoff
% 2 == 0)
1080 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1083 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1087 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1089 unsigned int offset
= 0;
1090 var_list_t
*fs
= type
->fields_or_args
;
1093 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1095 unsigned int align
= 0;
1096 type_t
*ft
= f
->type
;
1097 if (is_union(ft
->type
) && is_attr(f
->attrs
, ATTR_SWITCHIS
))
1099 unsigned int absoff
= ft
->typestring_offset
;
1100 short reloff
= absoff
- (*tfsoff
+ 6);
1101 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1102 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
1103 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1104 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1105 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1106 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1107 reloff
, reloff
, absoff
);
1111 /* FIXME: take alignment into account */
1112 offset
+= type_memsize(ft
, &align
);
1116 static int write_no_repeat_pointer_descriptions(
1117 FILE *file
, type_t
*type
,
1118 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1119 unsigned int *typestring_offset
)
1124 if (is_ptr(type
) || (!type
->declarray
&& is_conformant_array(type
)))
1128 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1129 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1131 /* pointer instance */
1132 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1133 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1134 *typestring_offset
+= 6;
1138 if (is_string_type(type
->attrs
, type
))
1139 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1141 write_pointer_tfs(file
, type
, typestring_offset
);
1145 unsigned absoff
= type
->typestring_offset
;
1146 short reloff
= absoff
- (*typestring_offset
+ 2);
1147 /* FIXME: get pointer attributes from field */
1148 print_file(file
, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP
, "FC_UP");
1149 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1150 reloff
, reloff
, absoff
);
1151 *typestring_offset
+= 4;
1155 memsize
= type_memsize(type
, &align
);
1156 *offset_in_memory
+= memsize
;
1157 /* increment these separately as in the case of conformant (varying)
1158 * structures these start at different values */
1159 *offset_in_buffer
+= memsize
;
1164 if (is_non_complex_struct(type
))
1167 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1169 if (offset_in_memory
&& offset_in_buffer
)
1173 type_memsize(v
->type
, &align
);
1174 padding
= ROUNDING(*offset_in_memory
, align
);
1175 *offset_in_memory
+= padding
;
1176 *offset_in_buffer
+= padding
;
1178 written
+= write_no_repeat_pointer_descriptions(
1180 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1187 memsize
= type_memsize(type
, &align
);
1188 *offset_in_memory
+= memsize
;
1189 /* increment these separately as in the case of conformant (varying)
1190 * structures these start at different values */
1191 *offset_in_buffer
+= memsize
;
1197 static int write_pointer_description_offsets(
1198 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1199 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1200 unsigned int *typestring_offset
)
1205 if (is_ptr(type
) && type
->ref
->type
!= RPC_FC_IP
)
1207 if (offset_in_memory
&& offset_in_buffer
)
1211 /* pointer instance */
1212 /* FIXME: sometimes from end of structure, sometimes from beginning */
1213 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1214 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1217 memsize
= type_memsize(type
, &align
);
1218 *offset_in_memory
+= memsize
;
1219 /* increment these separately as in the case of conformant (varying)
1220 * structures these start at different values */
1221 *offset_in_buffer
+= memsize
;
1223 *typestring_offset
+= 4;
1225 if (is_string_type(attrs
, type
))
1226 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1227 else if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1228 write_pointer_tfs(file
, type
, typestring_offset
);
1230 error("write_pointer_description_offsets: type format string unknown\n");
1237 return write_pointer_description_offsets(
1238 file
, attrs
, type
->ref
, offset_in_memory
, offset_in_buffer
,
1241 else if (is_non_complex_struct(type
))
1243 /* otherwise search for interesting fields to parse */
1245 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1247 if (offset_in_memory
&& offset_in_buffer
)
1251 type_memsize(v
->type
, &align
);
1252 padding
= ROUNDING(*offset_in_memory
, align
);
1253 *offset_in_memory
+= padding
;
1254 *offset_in_buffer
+= padding
;
1256 written
+= write_pointer_description_offsets(
1257 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1263 if (offset_in_memory
&& offset_in_buffer
)
1267 memsize
= type_memsize(type
, &align
);
1268 *offset_in_memory
+= memsize
;
1269 /* increment these separately as in the case of conformant (varying)
1270 * structures these start at different values */
1271 *offset_in_buffer
+= memsize
;
1278 /* Note: if file is NULL return value is number of pointers to write, else
1279 * it is the number of type format characters written */
1280 static int write_fixed_array_pointer_descriptions(
1281 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1282 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1283 unsigned int *typestring_offset
)
1286 int pointer_count
= 0;
1288 if (type
->type
== RPC_FC_SMFARRAY
|| type
->type
== RPC_FC_LGFARRAY
)
1290 unsigned int temp
= 0;
1291 /* unfortunately, this needs to be done in two passes to avoid
1292 * writing out redundant FC_FIXED_REPEAT descriptions */
1293 pointer_count
= write_pointer_description_offsets(
1294 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1295 if (pointer_count
> 0)
1297 unsigned int increment_size
;
1298 size_t offset_of_array_pointer_mem
= 0;
1299 size_t offset_of_array_pointer_buf
= 0;
1302 increment_size
= type_memsize(type
->ref
, &align
);
1304 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1305 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1306 print_file(file
, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type
->dim
, type
->dim
);
1307 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1308 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1309 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1310 *typestring_offset
+= 10;
1312 pointer_count
= write_pointer_description_offsets(
1313 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1314 &offset_of_array_pointer_buf
, typestring_offset
);
1317 else if (is_struct(type
->type
))
1320 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1322 if (offset_in_memory
&& offset_in_buffer
)
1326 type_memsize(v
->type
, &align
);
1327 padding
= ROUNDING(*offset_in_memory
, align
);
1328 *offset_in_memory
+= padding
;
1329 *offset_in_buffer
+= padding
;
1331 pointer_count
+= write_fixed_array_pointer_descriptions(
1332 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1338 if (offset_in_memory
&& offset_in_buffer
)
1342 memsize
= type_memsize(type
, &align
);
1343 *offset_in_memory
+= memsize
;
1344 /* increment these separately as in the case of conformant (varying)
1345 * structures these start at different values */
1346 *offset_in_buffer
+= memsize
;
1350 return pointer_count
;
1353 /* Note: if file is NULL return value is number of pointers to write, else
1354 * it is the number of type format characters written */
1355 static int write_conformant_array_pointer_descriptions(
1356 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1357 size_t offset_in_memory
, unsigned int *typestring_offset
)
1360 int pointer_count
= 0;
1362 if (is_conformant_array(type
) && !type
->length_is
)
1364 unsigned int temp
= 0;
1365 /* unfortunately, this needs to be done in two passes to avoid
1366 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1367 pointer_count
= write_pointer_description_offsets(
1368 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1369 if (pointer_count
> 0)
1371 unsigned int increment_size
;
1372 size_t offset_of_array_pointer_mem
= offset_in_memory
;
1373 size_t offset_of_array_pointer_buf
= offset_in_memory
;
1376 increment_size
= type_memsize(type
->ref
, &align
);
1378 if (increment_size
> USHRT_MAX
)
1379 error("array size of %u bytes is too large\n", increment_size
);
1381 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1382 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1383 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1384 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1385 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1386 *typestring_offset
+= 8;
1388 pointer_count
= write_pointer_description_offsets(
1389 file
, attrs
, type
->ref
, &offset_of_array_pointer_mem
,
1390 &offset_of_array_pointer_buf
, typestring_offset
);
1394 return pointer_count
;
1397 /* Note: if file is NULL return value is number of pointers to write, else
1398 * it is the number of type format characters written */
1399 static int write_varying_array_pointer_descriptions(
1400 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1401 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1402 unsigned int *typestring_offset
)
1405 int pointer_count
= 0;
1407 if (is_array(type
) && type
->length_is
)
1409 unsigned int temp
= 0;
1410 /* unfortunately, this needs to be done in two passes to avoid
1411 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1412 pointer_count
= write_pointer_description_offsets(
1413 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1414 if (pointer_count
> 0)
1416 unsigned int increment_size
;
1419 increment_size
= type_memsize(type
->ref
, &align
);
1421 if (increment_size
> USHRT_MAX
)
1422 error("array size of %u bytes is too large\n", increment_size
);
1424 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1425 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1426 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1427 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1428 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1429 *typestring_offset
+= 8;
1431 pointer_count
= write_pointer_description_offsets(
1432 file
, attrs
, type
, offset_in_memory
,
1433 offset_in_buffer
, typestring_offset
);
1436 else if (is_struct(type
->type
))
1439 LIST_FOR_EACH_ENTRY( v
, type
->fields_or_args
, const var_t
, entry
)
1441 if (offset_in_memory
&& offset_in_buffer
)
1445 if (is_array(v
->type
) && v
->type
->length_is
)
1447 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1448 /* skip over variance and offset in buffer */
1449 *offset_in_buffer
+= 8;
1453 type_memsize(v
->type
, &align
);
1454 padding
= ROUNDING(*offset_in_memory
, align
);
1455 *offset_in_memory
+= padding
;
1456 *offset_in_buffer
+= padding
;
1458 pointer_count
+= write_varying_array_pointer_descriptions(
1459 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1465 if (offset_in_memory
&& offset_in_buffer
)
1469 memsize
= type_memsize(type
, &align
);
1470 *offset_in_memory
+= memsize
;
1471 /* increment these separately as in the case of conformant (varying)
1472 * structures these start at different values */
1473 *offset_in_buffer
+= memsize
;
1477 return pointer_count
;
1480 static void write_pointer_description(FILE *file
, type_t
*type
,
1481 unsigned int *typestring_offset
)
1483 size_t offset_in_buffer
;
1484 size_t offset_in_memory
;
1486 /* pass 1: search for single instance of a pointer (i.e. don't descend
1488 if (!is_array(type
))
1490 offset_in_memory
= 0;
1491 offset_in_buffer
= 0;
1492 write_no_repeat_pointer_descriptions(
1494 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1497 /* pass 2: search for pointers in fixed arrays */
1498 offset_in_memory
= 0;
1499 offset_in_buffer
= 0;
1500 write_fixed_array_pointer_descriptions(
1502 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1504 /* pass 3: search for pointers in conformant only arrays (but don't descend
1505 * into conformant varying or varying arrays) */
1506 if ((!type
->declarray
|| !current_structure
) && is_conformant_array(type
))
1507 write_conformant_array_pointer_descriptions(
1508 file
, NULL
, type
, 0, typestring_offset
);
1509 else if (type
->type
== RPC_FC_CPSTRUCT
)
1511 unsigned int align
= 0;
1512 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1513 write_conformant_array_pointer_descriptions(
1515 type_memsize(type
, &align
),
1519 /* pass 4: search for pointers in varying arrays */
1520 offset_in_memory
= 0;
1521 offset_in_buffer
= 0;
1522 write_varying_array_pointer_descriptions(
1524 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1527 int is_declptr(const type_t
*t
)
1529 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1532 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1534 const char *name
, unsigned int *typestring_offset
)
1536 size_t start_offset
;
1537 unsigned char rtype
;
1539 if (is_declptr(type
))
1541 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1542 int pointer_type
= is_ptr(type
) ? type
->type
: get_attrv(attrs
, ATTR_POINTERTYPE
);
1544 pointer_type
= RPC_FC_RP
;
1545 print_start_tfs_comment(file
, type
, *typestring_offset
);
1546 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1547 pointer_type
, flag
, string_of_type(pointer_type
),
1548 flag
? " [simple_pointer]" : "");
1549 *typestring_offset
+= 2;
1552 print_file(file
, 2, "NdrFcShort(0x2),\n");
1553 *typestring_offset
+= 2;
1557 start_offset
= *typestring_offset
;
1558 update_tfsoff(type
, start_offset
, file
);
1560 rtype
= type
->ref
->type
;
1562 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1564 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1565 return start_offset
;
1568 if (type
->declarray
&& !is_conformant_array(type
))
1570 /* FIXME: multi-dimensional array */
1571 if (0xffffuL
< type
->dim
)
1572 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1573 name
, 0xffffu
, type
->dim
- 0xffffu
);
1575 if (rtype
== RPC_FC_CHAR
)
1576 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1578 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1579 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1580 *typestring_offset
+= 2;
1582 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", type
->dim
, type
->dim
);
1583 *typestring_offset
+= 2;
1585 return start_offset
;
1587 else if (type
->size_is
)
1589 unsigned int align
= 0;
1591 if (rtype
== RPC_FC_CHAR
)
1592 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1594 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1595 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1596 *typestring_offset
+= 2;
1598 *typestring_offset
+= write_conf_or_var_desc(
1599 file
, current_structure
,
1600 (type
->declarray
&& current_structure
1601 ? type_memsize(current_structure
, &align
)
1603 type
, type
->size_is
);
1605 return start_offset
;
1609 if (rtype
== RPC_FC_WCHAR
)
1610 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1612 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1613 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1614 *typestring_offset
+= 2;
1616 return start_offset
;
1620 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1621 const char *name
, unsigned int *typestring_offset
)
1623 const expr_t
*length_is
= type
->length_is
;
1624 const expr_t
*size_is
= type
->size_is
;
1625 unsigned int align
= 0;
1627 size_t start_offset
;
1629 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1630 unsigned int baseoff
1631 = type
->declarray
&& current_structure
1632 ? type_memsize(current_structure
, &align
)
1636 pointer_type
= RPC_FC_RP
;
1638 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1641 has_pointer
= type_has_pointers(type
->ref
);
1644 size
= type_memsize((is_conformant_array(type
) ? type
->ref
: type
), &align
);
1646 start_offset
= *typestring_offset
;
1647 update_tfsoff(type
, start_offset
, file
);
1648 print_start_tfs_comment(file
, type
, start_offset
);
1649 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1650 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1651 *typestring_offset
+= 2;
1654 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1656 unsigned char tc
= type
->type
;
1658 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1660 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1661 *typestring_offset
+= 4;
1665 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1666 *typestring_offset
+= 2;
1669 if (is_conformant_array(type
))
1671 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1674 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1676 unsigned int elalign
= 0;
1677 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1679 if (type
->type
== RPC_FC_LGVARRAY
)
1681 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1682 *typestring_offset
+= 4;
1686 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1687 *typestring_offset
+= 2;
1690 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1691 *typestring_offset
+= 2;
1696 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1699 if (has_pointer
&& (!type
->declarray
|| !current_structure
))
1701 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1702 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1703 *typestring_offset
+= 2;
1704 write_pointer_description(file
, type
, typestring_offset
);
1705 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1706 *typestring_offset
+= 1;
1709 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1710 write_end(file
, typestring_offset
);
1714 unsigned int dim
= size_is
? 0 : type
->dim
;
1715 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim
, dim
);
1716 *typestring_offset
+= 2;
1718 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1721 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1723 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1724 write_end(file
, typestring_offset
);
1727 return start_offset
;
1730 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1732 const var_t
*last_field
;
1735 if (!type
->fields_or_args
|| list_empty(type
->fields_or_args
))
1738 last_field
= LIST_ENTRY( list_tail(type
->fields_or_args
), const var_t
, entry
);
1739 ft
= last_field
->type
;
1741 if (ft
->declarray
&& is_conformant_array(ft
))
1744 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1745 return find_array_or_string_in_struct(ft
);
1750 static void write_struct_members(FILE *file
, const type_t
*type
,
1751 unsigned int *corroff
, unsigned int *typestring_offset
)
1754 unsigned short offset
= 0;
1758 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
1760 type_t
*ft
= field
->type
;
1761 if (!ft
->declarray
|| !is_conformant_array(ft
))
1763 unsigned int align
= 0;
1764 size_t size
= type_memsize(ft
, &align
);
1767 if ((align
- 1) & offset
)
1769 unsigned char fc
= 0;
1773 fc
= RPC_FC_ALIGNM4
;
1776 fc
= RPC_FC_ALIGNM8
;
1779 error("write_struct_members: cannot align type %d\n", ft
->type
);
1781 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1782 offset
= ROUND_SIZE(offset
, align
);
1783 *typestring_offset
+= 1;
1785 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1791 padding
= ROUNDING(offset
, salign
);
1794 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1795 RPC_FC_STRUCTPAD1
+ padding
- 1,
1797 *typestring_offset
+= 1;
1800 write_end(file
, typestring_offset
);
1803 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1804 const char *name
, unsigned int *tfsoff
)
1806 const type_t
*save_current_structure
= current_structure
;
1807 unsigned int total_size
;
1809 size_t start_offset
;
1810 size_t array_offset
;
1811 int has_pointers
= 0;
1812 unsigned int align
= 0;
1813 unsigned int corroff
;
1817 current_structure
= type
;
1819 total_size
= type_memsize(type
, &align
);
1820 if (total_size
> USHRT_MAX
)
1821 error("structure size for %s exceeds %d bytes by %d bytes\n",
1822 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1824 if (type
->fields_or_args
) LIST_FOR_EACH_ENTRY(f
, type
->fields_or_args
, var_t
, entry
)
1825 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1827 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
1829 array
= find_array_or_string_in_struct(type
);
1830 if (array
&& !processed(array
->type
))
1832 = is_attr(array
->attrs
, ATTR_STRING
)
1833 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1834 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1837 write_descriptors(file
, type
, tfsoff
);
1839 start_offset
= *tfsoff
;
1840 update_tfsoff(type
, start_offset
, file
);
1841 print_start_tfs_comment(file
, type
, start_offset
);
1842 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1843 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1844 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1849 unsigned int absoff
= array
->type
->typestring_offset
;
1850 short reloff
= absoff
- *tfsoff
;
1851 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1852 reloff
, reloff
, absoff
);
1855 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1857 print_file(file
, 2, "NdrFcShort(0x0),\n");
1861 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1863 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1864 nothing is written to file yet. On the actual writing pass,
1865 this will have been updated. */
1866 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
1867 short reloff
= absoff
- *tfsoff
;
1868 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1869 reloff
, reloff
, absoff
);
1872 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1873 (type
->type
== RPC_FC_CPSTRUCT
) ||
1874 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1876 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1877 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1879 write_pointer_description(file
, type
, tfsoff
);
1880 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1884 write_struct_members(file
, type
, &corroff
, tfsoff
);
1886 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1888 const var_list_t
*fs
= type
->fields_or_args
;
1891 type
->ptrdesc
= *tfsoff
;
1892 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1894 type_t
*ft
= f
->type
;
1897 if (is_string_type(f
->attrs
, ft
))
1898 write_string_tfs(file
, f
->attrs
, ft
, f
->name
, tfsoff
);
1900 write_pointer_tfs(file
, ft
, tfsoff
);
1902 else if (!ft
->declarray
&& is_conformant_array(ft
))
1904 unsigned int absoff
= ft
->typestring_offset
;
1905 short reloff
= absoff
- (*tfsoff
+ 2);
1906 int ptr_type
= get_attrv(f
->attrs
, ATTR_POINTERTYPE
);
1907 /* FIXME: We need to store pointer attributes for arrays
1908 so we don't lose pointer_default info. */
1910 ptr_type
= RPC_FC_UP
;
1911 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1912 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
1913 string_of_type(ptr_type
));
1914 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1915 reloff
, reloff
, absoff
);
1919 if (type
->ptrdesc
== *tfsoff
)
1923 current_structure
= save_current_structure
;
1924 return start_offset
;
1927 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1928 unsigned char flags
, size_t offset
,
1929 unsigned int *typeformat_offset
)
1931 size_t start_offset
= *typeformat_offset
;
1932 short reloff
= offset
- (*typeformat_offset
+ 2);
1933 int in_attr
, out_attr
;
1934 in_attr
= is_attr(attrs
, ATTR_IN
);
1935 out_attr
= is_attr(attrs
, ATTR_OUT
);
1936 if (!in_attr
&& !out_attr
) in_attr
= 1;
1938 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1941 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1944 string_of_type(pointer_type
));
1948 fprintf(file
, " [allocated_on_stack]");
1950 fprintf(file
, " [pointer_deref]");
1951 fprintf(file
, " */\n");
1954 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1955 *typeformat_offset
+= 4;
1957 return start_offset
;
1960 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1964 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1966 else if (is_base_type(t
->type
))
1968 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1969 t
->type
, string_of_type(t
->type
));
1971 else if (t
->typestring_offset
)
1973 short reloff
= t
->typestring_offset
- *tfsoff
;
1974 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1975 reloff
, reloff
, t
->typestring_offset
);
1978 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1983 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1985 unsigned int align
= 0;
1986 unsigned int start_offset
;
1987 size_t size
= type_memsize(type
, &align
);
1990 type_t
*deftype
= NULL
;
1991 short nodeftype
= 0xffff;
1996 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1998 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields_or_args
), const var_t
, entry
);
1999 fields
= uv
->type
->fields_or_args
;
2002 fields
= type
->fields_or_args
;
2004 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2006 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2008 nbranch
+= list_count(cases
);
2010 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2013 start_offset
= *tfsoff
;
2014 update_tfsoff(type
, start_offset
, file
);
2015 print_start_tfs_comment(file
, type
, start_offset
);
2016 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
2018 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields_or_args
), const var_t
, entry
);
2019 const type_t
*st
= sv
->type
;
2032 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
2033 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2034 0x40 | st
->type
, string_of_type(st
->type
));
2038 error("union switch type must be an integer, char, or enum\n");
2041 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2043 static const expr_t dummy_expr
; /* FIXME */
2044 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2057 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
2058 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2059 st
->type
, string_of_type(st
->type
));
2063 error("union switch type must be an integer, char, or enum\n");
2066 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2069 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
2070 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
2073 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2075 type_t
*ft
= f
->type
;
2076 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2077 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2080 if (cases
== NULL
&& !deflt
)
2081 error("union field %s with neither case nor default attribute\n", f
->name
);
2083 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2085 /* MIDL doesn't check for duplicate cases, even though that seems
2086 like a reasonable thing to do, it just dumps them to the TFS
2087 like we're going to do here. */
2088 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
2090 write_branch_type(file
, ft
, tfsoff
);
2093 /* MIDL allows multiple default branches, even though that seems
2094 illogical, it just chooses the last one, which is what we will
2105 write_branch_type(file
, deftype
, tfsoff
);
2109 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
2113 return start_offset
;
2116 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2117 unsigned int *typeformat_offset
)
2120 size_t start_offset
= *typeformat_offset
;
2121 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2125 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2126 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2128 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2132 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
2133 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2136 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2138 update_tfsoff(type
, start_offset
, file
);
2139 print_start_tfs_comment(file
, type
, start_offset
);
2140 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2141 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2142 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
2143 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2144 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2145 for (i
= 0; i
< 8; ++i
)
2146 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2149 fprintf(file
, "\n");
2151 *typeformat_offset
+= 18;
2153 return start_offset
;
2156 static size_t write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2158 unsigned int *typeformat_offset
)
2160 size_t start_offset
= *typeformat_offset
;
2161 unsigned char flags
= 0;
2163 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2164 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2168 if (is_attr(var
->attrs
, ATTR_IN
))
2171 if (!is_attr(var
->attrs
, ATTR_OUT
))
2172 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2174 if (is_attr(var
->attrs
, ATTR_OUT
))
2177 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2178 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2179 /* return and can't be null values overlap */
2180 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2181 print_file(file
, 0, "can't be null, ");
2182 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2183 print_file(file
, 0, "serialize, ");
2184 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2185 print_file(file
, 0, "no serialize, ");
2186 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2187 print_file(file
, 0, "strict, ");
2188 if ((flags
& 0x21) == 0x20)
2189 print_file(file
, 0, "out, ");
2190 if ((flags
& 0x21) == 0x21)
2191 print_file(file
, 0, "return, ");
2193 print_file(file
, 0, "in, ");
2195 print_file(file
, 0, "via ptr, ");
2196 print_file(file
, 0, "*/\n");
2197 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2198 print_file(file
, 2, "0, /* FIXME: param num */\n");
2199 *typeformat_offset
+= 4;
2201 return start_offset
;
2204 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
2205 type_t
*type
, const var_t
*var
,
2206 unsigned int *typeformat_offset
)
2210 if (is_context_handle(type
))
2211 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2213 if (is_user_type(type
))
2215 write_user_tfs(file
, type
, typeformat_offset
);
2216 return type
->typestring_offset
;
2219 if (is_string_type(var
->attrs
, type
))
2220 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2226 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2227 ptr_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2228 /* Top level pointers to conformant arrays may be handled specially
2229 since we can bypass the pointer, but if the array is buried
2230 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2231 always need to write the pointer. */
2232 if (!ptr_type
&& var
->type
!= type
)
2233 /* FIXME: This should use pointer_default, but the information
2234 isn't kept around for arrays. */
2235 ptr_type
= RPC_FC_UP
;
2236 if (ptr_type
&& ptr_type
!= RPC_FC_RP
)
2238 unsigned int absoff
= type
->typestring_offset
;
2239 short reloff
= absoff
- (*typeformat_offset
+ 2);
2240 off
= *typeformat_offset
;
2241 print_file(file
, 0, "/* %d */\n", off
);
2242 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2243 string_of_type(ptr_type
));
2244 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2245 reloff
, reloff
, absoff
);
2246 *typeformat_offset
+= 4;
2253 /* basic types don't need a type format string */
2254 if (is_base_type(type
->type
))
2260 case RPC_FC_PSTRUCT
:
2261 case RPC_FC_CSTRUCT
:
2262 case RPC_FC_CPSTRUCT
:
2263 case RPC_FC_CVSTRUCT
:
2264 case RPC_FC_BOGUS_STRUCT
:
2265 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2266 case RPC_FC_ENCAPSULATED_UNION
:
2267 case RPC_FC_NON_ENCAPSULATED_UNION
:
2268 return write_union_tfs(file
, type
, typeformat_offset
);
2270 case RPC_FC_BIND_PRIMITIVE
:
2274 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
2277 else if (last_ptr(type
))
2279 size_t start_offset
= *typeformat_offset
;
2280 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2281 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2282 const type_t
*base
= type
->ref
;
2284 if (base
->type
== RPC_FC_IP
2286 && is_attr(var
->attrs
, ATTR_IIDIS
)))
2288 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2291 /* special case for pointers to base types */
2292 if (is_base_type(base
->type
))
2294 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2295 type
->type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2296 string_of_type(type
->type
),
2297 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2298 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2299 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2300 *typeformat_offset
+= 4;
2301 return start_offset
;
2305 assert(is_ptr(type
));
2307 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2309 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2310 return write_pointer_only_tfs(file
, var
->attrs
, type
->type
,
2311 !last_ptr(type
) ? 0x10 : 0,
2312 offset
, typeformat_offset
);
2315 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2316 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2320 if (is_user_type(type
))
2322 write_user_tfs(file
, type
, tfsoff
);
2324 else if (is_string_type(attrs
, type
))
2326 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2328 else if (is_ptr(type
))
2330 type_t
*ref
= type
->ref
;
2332 if (ref
->type
== RPC_FC_IP
2334 && is_attr(attrs
, ATTR_IIDIS
)))
2336 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2340 if (!processed(ref
) && !is_base_type(ref
->type
))
2341 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2344 write_pointer_tfs(file
, type
, tfsoff
);
2349 else if (type
->declarray
&& is_conformant_array(type
))
2350 ; /* conformant arrays and strings are handled specially */
2351 else if (is_array(type
))
2353 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2354 if (is_conformant_array(type
))
2357 else if (is_struct(type
->type
))
2359 if (!processed(type
))
2360 write_struct_tfs(file
, type
, name
, tfsoff
);
2362 else if (is_union(type
->type
))
2364 if (!processed(type
))
2365 write_union_tfs(file
, type
, tfsoff
);
2367 else if (!is_base_type(type
->type
))
2368 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2374 static size_t process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2375 type_pred_t pred
, unsigned int *typeformat_offset
)
2378 const statement_t
*stmt
;
2380 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2382 const type_t
*iface
;
2383 if (stmt
->type
== STMT_LIBRARY
)
2385 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2388 else if (stmt
->type
!= STMT_TYPE
|| stmt
->u
.type
->type
!= RPC_FC_IP
)
2391 iface
= stmt
->u
.type
;
2398 current_iface
= iface
;
2399 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
2401 if (is_local(func
->def
->attrs
)) continue;
2403 if (!is_void(get_func_return_type(func
)))
2405 var_t v
= *func
->def
;
2406 v
.type
= get_func_return_type(func
);
2407 update_tfsoff(get_func_return_type(func
),
2408 write_typeformatstring_var(
2409 file
, 2, NULL
, get_func_return_type(func
),
2410 &v
, typeformat_offset
),
2414 current_func
= func
;
2416 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2419 write_typeformatstring_var(
2420 file
, 2, func
, var
->type
, var
,
2427 return *typeformat_offset
+ 1;
2430 static size_t process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2432 unsigned int typeformat_offset
= 2;
2434 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2438 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2442 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2443 print_file(file
, indent
, "{\n");
2445 print_file(file
, indent
, "0,\n");
2446 print_file(file
, indent
, "{\n");
2448 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2450 set_all_tfswrite(TRUE
);
2451 process_tfs(file
, stmts
, pred
);
2453 print_file(file
, indent
, "0x0\n");
2455 print_file(file
, indent
, "}\n");
2457 print_file(file
, indent
, "};\n");
2458 print_file(file
, indent
, "\n");
2461 static unsigned int get_required_buffer_size_type(
2462 const type_t
*type
, const char *name
, unsigned int *alignment
)
2465 const type_t
*utype
;
2468 if ((utype
= get_user_type(type
, &uname
)))
2470 return get_required_buffer_size_type(utype
, uname
, alignment
);
2494 case RPC_FC_ERROR_STATUS_T
:
2504 case RPC_FC_BIND_PRIMITIVE
:
2508 case RPC_FC_PSTRUCT
:
2512 if (!type
->fields_or_args
) return 0;
2513 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2515 unsigned int alignment
;
2516 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2524 is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
2525 ? get_required_buffer_size_type( type
->ref
, name
, alignment
)
2528 case RPC_FC_SMFARRAY
:
2529 case RPC_FC_LGFARRAY
:
2530 return type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2538 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2540 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2541 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2544 if (!in_attr
&& !out_attr
)
2549 for (t
= var
->type
; is_ptr(t
); t
= t
->ref
)
2550 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
2556 if (pass
== PASS_OUT
)
2558 if (out_attr
&& is_ptr(var
->type
))
2560 type_t
*type
= var
->type
;
2562 if (type
->type
== RPC_FC_STRUCT
)
2565 unsigned int size
= 36;
2567 if (!type
->fields_or_args
) return size
;
2568 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2571 size
+= get_required_buffer_size_type(
2572 field
->type
, field
->name
, &align
);
2581 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2582 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2584 if (is_ptr(var
->type
))
2586 type_t
*type
= var
->type
;
2588 if (is_base_type(type
->type
))
2592 else if (type
->type
== RPC_FC_STRUCT
)
2594 unsigned int size
= 36;
2597 if (!type
->fields_or_args
) return size
;
2598 LIST_FOR_EACH_ENTRY( field
, type
->fields_or_args
, const var_t
, entry
)
2601 size
+= get_required_buffer_size_type(
2602 field
->type
, field
->name
, &align
);
2609 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2613 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2616 unsigned int total_size
= 0, alignment
;
2620 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2622 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2623 total_size
+= alignment
;
2627 if (pass
== PASS_OUT
&& !is_void(get_func_return_type(func
)))
2629 var_t v
= *func
->def
;
2630 v
.type
= get_func_return_type(func
);
2631 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
2632 total_size
+= alignment
;
2637 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2638 const char *local_var_prefix
, enum remoting_phase phase
,
2639 const var_t
*var
, unsigned int type_offset
)
2641 const char *function
;
2644 case PHASE_BUFFERSIZE
:
2645 function
= "BufferSize";
2648 function
= "Marshall";
2650 case PHASE_UNMARSHAL
:
2651 function
= "Unmarshall";
2661 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2663 print_file(file
, indent
, "&__frame->_StubMsg,\n");
2664 print_file(file
, indent
, "%s%s%s%s%s,\n",
2665 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2666 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2668 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2670 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2671 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2672 if (phase
== PHASE_UNMARSHAL
)
2673 print_file(file
, indent
, "0);\n");
2677 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
2678 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
2679 const char *varname
)
2681 type_t
*type
= var
->type
;
2683 unsigned int alignment
= 0;
2684 unsigned char rtype
;
2686 /* no work to do for other phases, buffer sizing is done elsewhere */
2687 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2690 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2714 case RPC_FC_ERROR_STATUS_T
:
2726 case RPC_FC_BIND_PRIMITIVE
:
2727 /* no marshalling needed */
2731 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2735 if (phase
== PHASE_MARSHAL
)
2736 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (long)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
2737 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((long)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2738 alignment
- 1, alignment
- 1);
2740 if (phase
== PHASE_MARSHAL
)
2742 print_file(file
, indent
, "*(");
2743 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2745 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
2747 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
2748 fprintf(file
, "%s%s", local_var_prefix
, varname
);
2749 fprintf(file
, ";\n");
2751 else if (phase
== PHASE_UNMARSHAL
)
2753 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
2754 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2755 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
2756 print_file(file
, indent
, "{\n");
2757 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2758 print_file(file
, indent
, "}\n");
2759 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2760 print_file(file
, indent
, "");
2762 print_file(file
, indent
, "*");
2763 fprintf(file
, "%s%s", local_var_prefix
, varname
);
2764 if (pass
== PASS_IN
&& is_ptr(type
))
2765 fprintf(file
, " = (");
2767 fprintf(file
, " = *(");
2768 write_type_decl(file
, is_ptr(type
) ? type
->ref
: type
, NULL
);
2769 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
2772 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
2773 write_type_decl(file
, var
->type
, NULL
);
2774 fprintf(file
, ");\n");
2777 /* returns whether the MaxCount, Offset or ActualCount members need to be
2778 * filled in for the specified phase */
2779 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
2781 return (phase
!= PHASE_UNMARSHAL
);
2784 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
2788 for ( ; is_ptr(t
) || is_array(t
); t
= t
->ref
)
2794 error("%s: multidimensional conformant"
2795 " arrays not supported at the top level\n",
2802 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
2803 enum remoting_phase phase
, const var_t
*var
)
2805 const type_t
*type
= var
->type
;
2806 /* get fundamental type for the argument */
2809 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
2811 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
2813 else if (is_array(type
) || is_string_type(var
->attrs
, type
))
2815 if (is_conformance_needed_for_phase(phase
))
2819 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (unsigned long)");
2820 write_expr(file
, type
->size_is
, 1, 1, NULL
, NULL
, local_var_prefix
);
2821 fprintf(file
, ";\n\n");
2823 if (type
->length_is
)
2825 print_file(file
, indent
, "__frame->_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2826 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (unsigned long)");
2827 write_expr(file
, type
->length_is
, 1, 1, NULL
, NULL
, local_var_prefix
);
2828 fprintf(file
, ";\n\n");
2833 else if (type
->type
== RPC_FC_NON_ENCAPSULATED_UNION
)
2835 if (is_conformance_needed_for_phase(phase
))
2837 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (unsigned long)");
2838 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
2839 fprintf(file
, ";\n\n");
2843 else if (type
->type
== RPC_FC_IP
)
2847 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2849 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (unsigned long) " );
2850 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
2851 fprintf( file
, ";\n\n" );
2855 else if (is_ptr(type
))
2862 static void write_remoting_arg(FILE *file
, int indent
, const func_t
*func
, const char *local_var_prefix
,
2863 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
2865 int in_attr
, out_attr
, pointer_type
;
2866 const type_t
*type
= var
->type
;
2867 unsigned char rtype
;
2868 size_t start_offset
= type
->typestring_offset
;
2870 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2872 pointer_type
= RPC_FC_RP
;
2874 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2875 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2876 if (!in_attr
&& !out_attr
)
2879 if (phase
!= PHASE_FREE
)
2883 if (!in_attr
) return;
2886 if (!out_attr
) return;
2892 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
2895 if (is_context_handle(type
))
2897 if (phase
== PHASE_MARSHAL
)
2899 if (pass
== PASS_IN
)
2901 /* if the context_handle attribute appears in the chain of types
2902 * without pointers being followed, then the context handle must
2903 * be direct, otherwise it is a pointer */
2904 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
2905 print_file(file
, indent
, "NdrClientContextMarshall(\n");
2906 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
2907 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
2908 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
2912 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
2913 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
2914 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
2915 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
2916 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2919 else if (phase
== PHASE_UNMARSHAL
)
2921 if (pass
== PASS_OUT
)
2924 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
2925 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
2926 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
2927 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
2928 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
2932 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
2933 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
2934 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
2938 else if (is_user_type(var
->type
))
2940 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
2942 else if (is_string_type(var
->attrs
, var
->type
))
2944 if (is_array(type
) && !is_conformant_array(type
))
2945 print_phase_function(file
, indent
, "NonConformantString", local_var_prefix
,
2946 phase
, var
, start_offset
);
2949 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
|| pointer_type
== RPC_FC_UP
)
2950 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
,
2951 start_offset
- (type
->size_is
? 4 : 2));
2953 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
2954 phase
, var
, start_offset
);
2957 else if (is_array(type
))
2959 unsigned char tc
= type
->type
;
2960 const char *array_type
= "FixedArray";
2962 /* We already have the size_is expression since it's at the
2963 top level, but do checks for multidimensional conformant
2964 arrays. When we handle them, we'll need to extend this
2965 function to return a list, and then we'll actually use
2966 the return value. */
2967 get_size_is_expr(type
, var
->name
);
2969 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2971 array_type
= "VaryingArray";
2973 else if (tc
== RPC_FC_CARRAY
)
2975 array_type
= "ConformantArray";
2977 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
2979 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
2981 : "ConformantVaryingArray");
2984 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
2985 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
2986 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
2988 /* these are all unmarshalled by allocating memory */
2989 if (type
->type
== RPC_FC_BOGUS_ARRAY
||
2990 type
->type
== RPC_FC_CVARRAY
||
2991 ((type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
) && in_attr
) ||
2992 (type
->type
== RPC_FC_CARRAY
&& !in_attr
))
2994 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
2996 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3000 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
3002 if (phase
!= PHASE_FREE
)
3003 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3005 else if (!is_ptr(var
->type
))
3010 case RPC_FC_PSTRUCT
:
3011 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3013 case RPC_FC_CSTRUCT
:
3014 case RPC_FC_CPSTRUCT
:
3015 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3017 case RPC_FC_CVSTRUCT
:
3018 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3020 case RPC_FC_BOGUS_STRUCT
:
3021 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3024 if (is_base_type( var
->type
->ref
->type
))
3026 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3028 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
3030 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
3031 print_phase_function(file
, indent
, local_var_prefix
, "SimpleStruct", phase
, var
, start_offset
+ 4);
3036 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3038 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (unsigned long) " );
3039 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3040 fprintf( file
, ";\n\n" );
3042 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3046 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
3051 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
3053 if (phase
!= PHASE_FREE
)
3054 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3056 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
3058 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
3059 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
+ 4);
3063 if (var
->type
->ref
->type
== RPC_FC_IP
)
3064 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3066 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3069 fprintf(file
, "\n");
3072 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
, const char *local_var_prefix
,
3073 enum pass pass
, enum remoting_phase phase
)
3075 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3077 unsigned int size
= get_function_buffer_size( func
, pass
);
3078 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3081 if (pass
== PASS_RETURN
)
3085 var
.type
= get_func_return_type(func
);
3086 var
.name
= xstrdup( "_RetVal" );
3087 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3095 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3096 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3101 size_t get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3103 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3107 size_t get_size_procformatstring_func(const func_t
*func
)
3112 /* argument list size */
3114 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3115 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3117 /* return value size */
3118 if (is_void(get_func_return_type(func
)))
3119 size
+= 2; /* FC_END and FC_PAD */
3121 size
+= get_size_procformatstring_type("return value", get_func_return_type(func
), NULL
);
3126 size_t get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3128 const statement_t
*stmt
;
3132 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3134 const type_t
*iface
;
3135 if (stmt
->type
== STMT_LIBRARY
)
3137 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3140 else if (stmt
->type
!= STMT_TYPE
|| stmt
->u
.type
->type
!= RPC_FC_IP
)
3143 iface
= stmt
->u
.type
;
3148 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
3149 if (!is_local(func
->def
->attrs
))
3150 size
+= get_size_procformatstring_func( func
);
3155 size_t get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3157 set_all_tfswrite(FALSE
);
3158 return process_tfs(NULL
, stmts
, pred
);
3161 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
3163 int in_attr
, out_attr
;
3167 /* declare return value '_RetVal' */
3168 if (!is_void(get_func_return_type(func
)))
3170 print_file(file
, indent
, "");
3171 write_type_decl_left(file
, get_func_return_type(func
));
3172 fprintf(file
, " _RetVal;\n");
3178 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3180 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3182 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3183 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3184 if (!out_attr
&& !in_attr
)
3187 if (is_context_handle(var
->type
))
3188 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3191 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
3193 print_file(file
, indent
, "");
3194 write_type_decl(file
, var
->type
->declarray
? var
->type
: var
->type
->ref
,
3196 fprintf(file
, ";\n");
3199 print_file(file
, indent
, "");
3200 write_type_decl_left(file
, var
->type
);
3202 if (var
->type
->declarray
) {
3203 fprintf(file
, "(*%s)", var
->name
);
3205 fprintf(file
, "%s", var
->name
);
3206 write_type_right(file
, var
->type
, FALSE
);
3207 fprintf(file
, ";\n");
3209 if (decl_indirect(var
->type
))
3210 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3216 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
, const char *local_var_prefix
)
3218 int in_attr
, out_attr
;
3225 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
3227 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
3228 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3229 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3230 if (!out_attr
&& !in_attr
)
3235 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3237 if (is_context_handle(var
->type
))
3239 fprintf(file
, " = NdrContextHandleInitialize(\n");
3240 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3241 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3242 var
->type
->typestring_offset
);
3244 else if (var
->type
->size_is
)
3246 unsigned int size
, align
= 0;
3247 type_t
*type
= var
->type
;
3249 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3250 for ( ; type
->size_is
; type
= type
->ref
)
3252 write_expr(file
, type
->size_is
, TRUE
, TRUE
, NULL
, NULL
, local_var_prefix
);
3253 fprintf(file
, " * ");
3255 size
= type_memsize(type
, &align
);
3256 fprintf(file
, "%u);\n", size
);
3258 else if (!is_string
)
3260 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3261 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
3262 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3270 fprintf(file
, "\n");
3274 int write_expr_eval_routines(FILE *file
, const char *iface
)
3276 static const char *var_name
= "pS";
3277 static const char *var_name_expr
= "pS->";
3279 struct expr_eval_routine
*eval
;
3280 unsigned short callback_offset
= 0;
3282 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3284 const char *name
= eval
->structure
->name
;
3287 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3288 iface
, name
, callback_offset
);
3289 print_file(file
, 0, "{\n");
3290 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3291 name
, var_name
, name
, eval
->baseoff
);
3292 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3293 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
3294 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
3295 fprintf(file
, ";\n");
3296 print_file(file
, 0, "}\n\n");
3302 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3304 struct expr_eval_routine
*eval
;
3305 struct expr_eval_routine
*cursor
;
3306 unsigned short callback_offset
= 0;
3308 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3309 fprintf(file
, "{\n");
3311 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3313 const char *name
= eval
->structure
->name
;
3314 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3316 list_remove(&eval
->entry
);
3320 fprintf(file
, "};\n\n");
3323 void write_user_quad_list(FILE *file
)
3327 if (list_empty(&user_type_list
))
3330 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3331 fprintf(file
, "{\n");
3332 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3334 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3335 print_file(file
, 1, "{\n");
3336 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3337 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3338 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3339 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3340 print_file(file
, 1, "}%s\n", sep
);
3342 fprintf(file
, "};\n\n");
3345 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3347 const struct str_list_entry_t
*endpoint
;
3350 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3351 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3352 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3354 print_file( f
, 1, "{ (const unsigned char *)\"" );
3355 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3357 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3360 if (!*p
) goto error
;
3361 if (p
[1] != '[') goto error
;
3363 fprintf( f
, "\", (const unsigned char *)\"" );
3364 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3366 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3369 if (*p
!= ']') goto error
;
3370 fprintf( f
, "\" },\n" );
3372 print_file( f
, 0, "};\n\n" );
3376 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
3379 void write_exceptions( FILE *file
)
3381 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3382 fprintf( file
, "\n");
3383 fprintf( file
, "#include \"wine/exception.h\"\n");
3384 fprintf( file
, "#undef RpcTryExcept\n");
3385 fprintf( file
, "#undef RpcExcept\n");
3386 fprintf( file
, "#undef RpcEndExcept\n");
3387 fprintf( file
, "#undef RpcTryFinally\n");
3388 fprintf( file
, "#undef RpcFinally\n");
3389 fprintf( file
, "#undef RpcEndFinally\n");
3390 fprintf( file
, "#undef RpcExceptionCode\n");
3391 fprintf( file
, "#undef RpcAbnormalTermination\n");
3392 fprintf( file
, "\n");
3393 fprintf( file
, "struct __exception_frame;\n");
3394 fprintf( file
, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3395 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3396 fprintf( file
, "\n");
3397 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
3398 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3399 fprintf( file
, " __filter_func filter; \\\n");
3400 fprintf( file
, " __finally_func finally; \\\n");
3401 fprintf( file
, " sigjmp_buf jmp; \\\n");
3402 fprintf( file
, " DWORD code; \\\n");
3403 fprintf( file
, " unsigned char abnormal_termination; \\\n");
3404 fprintf( file
, " unsigned char filter_level; \\\n");
3405 fprintf( file
, " unsigned char finally_level;\n");
3406 fprintf( file
, "\n");
3407 fprintf( file
, "struct __exception_frame\n{\n");
3408 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
3409 fprintf( file
, "};\n");
3410 fprintf( file
, "\n");
3411 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3412 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3413 fprintf( file
, " CONTEXT *context,\n");
3414 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3415 fprintf( file
, "{\n");
3416 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3417 fprintf( file
, "\n");
3418 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3419 fprintf( file
, " {\n" );
3420 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3421 fprintf( file
, " {\n" );
3422 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
3423 fprintf( file
, " exc_frame->finally( exc_frame );\n");
3424 fprintf( file
, " }\n" );
3425 fprintf( file
, " return ExceptionContinueSearch;\n");
3426 fprintf( file
, " }\n" );
3427 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
3428 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3429 fprintf( file
, " {\n");
3430 fprintf( file
, " __wine_rtl_unwind( frame, record );\n");
3431 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3432 fprintf( file
, " {\n");
3433 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
3434 fprintf( file
, " exc_frame->finally( exc_frame );\n");
3435 fprintf( file
, " __wine_pop_frame( frame );\n");
3436 fprintf( file
, " }\n");
3437 fprintf( file
, " exc_frame->filter_level = 0;\n");
3438 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
3439 fprintf( file
, " }\n");
3440 fprintf( file
, " return ExceptionContinueSearch;\n");
3441 fprintf( file
, "}\n");
3442 fprintf( file
, "\n");
3443 fprintf( file
, "#define RpcTryExcept \\\n");
3444 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3445 fprintf( file
, " { \\\n");
3446 fprintf( file
, " if (!__frame->finally_level) \\\n" );
3447 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
3448 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
3449 fprintf( file
, "\n");
3450 fprintf( file
, "#define RpcExcept(expr) \\\n");
3451 fprintf( file
, " if (!__frame->finally_level) \\\n" );
3452 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
3453 fprintf( file
, " __frame->filter_level = 0; \\\n" );
3454 fprintf( file
, " } \\\n");
3455 fprintf( file
, " else \\\n");
3456 fprintf( file
, "\n");
3457 fprintf( file
, "#define RpcEndExcept\n");
3458 fprintf( file
, "\n");
3459 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
3460 fprintf( file
, "\n");
3461 fprintf( file
, "#define RpcTryFinally \\\n");
3462 fprintf( file
, " if (!__frame->filter_level) \\\n");
3463 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
3464 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
3465 fprintf( file
, "\n");
3466 fprintf( file
, "#define RpcFinally \\\n");
3467 fprintf( file
, " if (!__frame->filter_level) \\\n");
3468 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
3469 fprintf( file
, " __frame->finally_level = 0;\n");
3470 fprintf( file
, "\n");
3471 fprintf( file
, "#define RpcEndFinally\n");
3472 fprintf( file
, "\n");
3473 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3474 fprintf( file
, "\n");
3475 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3476 fprintf( file
, " do { \\\n");
3477 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3478 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3479 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
3480 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
3481 fprintf( file
, " __frame->filter_level = 0; \\\n");
3482 fprintf( file
, " __frame->finally_level = 0; \\\n");
3483 fprintf( file
, " } while (0)\n");
3484 fprintf( file
, "\n");
3485 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3486 fprintf( file
, "\n");
3487 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3488 fprintf( file
, "#define __DECL_EXCEPTION_FRAME\n");
3489 fprintf( file
, "\n");
3490 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");