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 static int get_struct_type(const type_t *type)
126 int has_conformance = 0;
127 int has_variance = 0;
130 if (type->type != RPC_FC_STRUCT) return type->type;
132 if (get_padding(type->fields_or_args))
133 return RPC_FC_BOGUS_STRUCT;
135 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, var_t, entry )
137 type_t *t = field->type;
140 return RPC_FC_BOGUS_STRUCT;
148 switch (get_struct_type(t))
151 case RPC_FC_ENCAPSULATED_UNION:
152 case RPC_FC_NON_ENCAPSULATED_UNION:
153 case RPC_FC_BOGUS_STRUCT:
154 return RPC_FC_BOGUS_STRUCT;
161 if (field->type->declarray)
163 if (is_string_type(field->attrs, field->type))
165 if (is_conformant_array(field->type))
171 if (is_array(field->type->ref))
172 return RPC_FC_BOGUS_STRUCT;
174 if (is_conformant_array(field->type))
177 if (field->type->declarray && list_next(type->fields_or_args, &field->entry))
178 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
181 if (field->type->length_is)
184 t = field->type->ref;
187 switch (get_struct_type(t))
190 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
191 * Simple types don't effect the type of struct.
192 * A struct containing a simple struct is still a simple struct.
193 * So long as we can block copy the data, we return RPC_FC_STRUCT.
195 case 0: /* void pointer */
206 case RPC_FC_UINT3264:
215 return RPC_FC_BOGUS_STRUCT;
220 if (pointer_size != 4)
221 return RPC_FC_BOGUS_STRUCT;
227 case RPC_FC_BOGUS_ARRAY:
229 unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
230 if (!ptr_type || ptr_type == RPC_FC_RP)
231 return RPC_FC_BOGUS_STRUCT;
232 else if (pointer_size != 4)
233 return RPC_FC_BOGUS_STRUCT;
239 * Propagate member attributes
240 * a struct should be at least as complex as its member
242 case RPC_FC_CVSTRUCT:
248 case RPC_FC_CPSTRUCT:
250 if (list_next( type->fields_or_args, &field->entry ))
251 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
258 if (list_next( type->fields_or_args, &field->entry ))
259 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
268 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
269 /* fallthru - treat it as complex */
271 /* as soon as we see one of these these members, it's bogus... */
272 case RPC_FC_ENCAPSULATED_UNION:
273 case RPC_FC_NON_ENCAPSULATED_UNION:
274 case RPC_FC_BOGUS_STRUCT:
276 return RPC_FC_BOGUS_STRUCT;
282 if ( has_conformance )
283 return RPC_FC_CVSTRUCT;
285 return RPC_FC_BOGUS_STRUCT;
287 if( has_conformance && has_pointer )
288 return RPC_FC_CPSTRUCT;
289 if( has_conformance )
290 return RPC_FC_CSTRUCT;
292 return RPC_FC_PSTRUCT;
293 return RPC_FC_STRUCT;
296 static int get_array_type(const type_t *type)
300 const type_t *rt = type->ref;
301 if (is_user_type(rt))
302 return RPC_FC_BOGUS_ARRAY;
303 switch (get_struct_type(rt))
305 case RPC_FC_BOGUS_STRUCT:
306 case RPC_FC_NON_ENCAPSULATED_UNION:
307 case RPC_FC_ENCAPSULATED_UNION:
309 return RPC_FC_BOGUS_ARRAY;
310 /* FC_RP should be above, but widl overuses these, and will break things. */
313 if (rt->ref->type == RPC_FC_IP) return RPC_FC_BOGUS_ARRAY;
317 if (type->type == RPC_FC_LGFARRAY || type->type == RPC_FC_LGVARRAY)
319 unsigned int align = 0;
320 size_t size = type_memsize(type, &align);
321 if (size * type->dim <= 0xffff)
322 return (type->type == RPC_FC_LGFARRAY) ? RPC_FC_SMFARRAY : RPC_FC_SMVARRAY;
328 int is_struct(unsigned char type)
335 case RPC_FC_CPSTRUCT:
336 case RPC_FC_CVSTRUCT:
337 case RPC_FC_BOGUS_STRUCT:
344 static int is_non_complex_struct(const type_t *type)
346 switch (get_struct_type(type))
351 case RPC_FC_CPSTRUCT:
352 case RPC_FC_CVSTRUCT:
359 int is_union(unsigned char type)
363 case RPC_FC_ENCAPSULATED_UNION:
364 case RPC_FC_NON_ENCAPSULATED_UNION:
371 static int type_has_pointers(const type_t *type)
373 if (is_user_type(type))
375 else if (is_ptr(type))
377 else if (is_array(type))
378 return type_has_pointers(type->ref);
379 else if (is_struct(type->type))
382 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
384 if (type_has_pointers(field->type))
388 else if (is_union(type->type))
392 if (type->type == RPC_FC_ENCAPSULATED_UNION)
394 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
395 fields = uv->type->fields_or_args;
398 fields = type->fields_or_args;
399 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
401 if (field->type && type_has_pointers(field->type))
409 static int type_has_full_pointer(const type_t *type)
411 if (is_user_type(type))
413 else if (type->type == RPC_FC_FP)
415 else if (is_ptr(type))
417 else if (is_array(type))
418 return type_has_full_pointer(type->ref);
419 else if (is_struct(type->type))
422 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
424 if (type_has_full_pointer(field->type))
428 else if (is_union(type->type))
432 if (type->type == RPC_FC_ENCAPSULATED_UNION)
434 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
435 fields = uv->type->fields_or_args;
438 fields = type->fields_or_args;
439 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
441 if (field->type && type_has_full_pointer(field->type))
449 static unsigned short user_type_offset(const char *name)
452 unsigned short off = 0;
453 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
455 if (strcmp(name, ut->name) == 0)
459 error("user_type_offset: couldn't find type (%s)\n", name);
463 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
465 type->typestring_offset = offset;
466 if (file) type->tfswrite = FALSE;
469 static void guard_rec(type_t *type)
471 /* types that contain references to themselves (like a linked list),
472 need to be shielded from infinite recursion when writing embedded
474 if (type->typestring_offset)
475 type->tfswrite = FALSE;
477 type->typestring_offset = 1;
480 static type_t *get_user_type(const type_t *t, const char **pname)
484 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
492 if (t->kind == TKIND_ALIAS)
499 int is_user_type(const type_t *t)
501 return get_user_type(t, NULL) != NULL;
504 static int is_embedded_complex(const type_t *type)
506 unsigned char tc = type->type;
507 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
508 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
511 static const char *get_context_handle_type_name(const type_t *type)
514 for (t = type; is_ptr(t); t = t->ref)
515 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
521 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
524 fprintf(file, "/* %2u */\n", typestring_offset); \
525 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
529 static void print_file(FILE *file, int indent, const char *format, ...)
532 va_start(va, format);
533 print(file, indent, format, va);
537 void print(FILE *file, int indent, const char *format, va_list va)
541 if (format[0] != '\n')
544 vfprintf(file, format, va);
549 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
551 if (decl_indirect(t))
553 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
554 local_var_prefix, n, local_var_prefix, n);
555 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
557 else if (is_ptr(t) || is_array(t))
558 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
561 void write_parameters_init(FILE *file, int indent, const func_t *func, const char *local_var_prefix)
565 if (!is_void(get_func_return_type(func)))
566 write_var_init(file, indent, get_func_return_type(func), "_RetVal", local_var_prefix);
571 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
572 write_var_init(file, indent, var->type, var->name, local_var_prefix);
577 static void write_formatdesc(FILE *f, int indent, const char *str)
579 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
580 print_file(f, indent, "{\n");
581 print_file(f, indent + 1, "short Pad;\n");
582 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
583 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
584 print_file(f, indent, "\n");
587 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
591 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
592 get_size_typeformatstring(stmts, pred));
594 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
595 get_size_procformatstring(stmts, pred));
598 write_formatdesc(f, indent, "TYPE");
599 write_formatdesc(f, indent, "PROC");
601 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
602 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
603 print_file(f, indent, "\n");
606 static inline int is_base_type(unsigned char type)
625 case RPC_FC_ERROR_STATUS_T:
626 case RPC_FC_BIND_PRIMITIVE:
634 int decl_indirect(const type_t *t)
636 return is_user_type(t)
637 || (!is_base_type(t->type)
642 static size_t write_procformatstring_type(FILE *file, int indent,
645 const attr_list_t *attrs,
650 int is_in = is_attr(attrs, ATTR_IN);
651 int is_out = is_attr(attrs, ATTR_OUT);
653 if (!is_in && !is_out) is_in = TRUE;
655 if (!type->declarray && is_base_type(type->type))
658 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
660 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
662 if (type->type == RPC_FC_BIND_PRIMITIVE)
664 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
665 size = 2; /* includes param type prefix */
667 else if (is_base_type(type->type))
669 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
670 size = 2; /* includes param type prefix */
674 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
681 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
682 else if (is_in && is_out)
683 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
685 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
687 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
689 print_file(file, indent, "0x01,\n");
690 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
691 size = 4; /* includes param type prefix */
696 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
698 const statement_t *stmt;
699 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
701 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
704 if (!pred(stmt->u.type))
706 if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
708 if (is_local(func->def->attrs)) continue;
709 /* emit argument data */
713 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
714 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
717 /* emit return value data */
718 if (is_void(get_func_return_type(func)))
720 print_file(file, indent, "0x5b, /* FC_END */\n");
721 print_file(file, indent, "0x5c, /* FC_PAD */\n");
724 write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
727 else if (stmt->type == STMT_LIBRARY)
728 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
732 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
736 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
737 print_file(file, indent, "{\n");
739 print_file(file, indent, "0,\n");
740 print_file(file, indent, "{\n");
743 write_procformatstring_stmts(file, indent, stmts, pred);
745 print_file(file, indent, "0x0\n");
747 print_file(file, indent, "}\n");
749 print_file(file, indent, "};\n");
750 print_file(file, indent, "\n");
753 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
755 if (is_base_type(type->type))
757 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
758 *typestring_offset += 1;
765 /* write conformance / variance descriptor */
766 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
767 unsigned int baseoff, const type_t *type,
770 unsigned char operator_type = 0;
771 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
772 const char *conftype_string = "";
773 const char *operator_string = "no operators";
774 const expr_t *subexpr;
778 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
784 /* Top-level conformance calculations are done inline. */
785 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
786 RPC_FC_TOP_LEVEL_CONFORMANCE);
787 print_file (file, 2, "0x0,\n");
788 print_file (file, 2, "NdrFcShort(0x0),\n");
794 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
795 error("write_conf_or_var_desc: constant value %ld is greater than "
796 "the maximum constant size of %d\n", expr->cval,
797 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
799 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
800 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
801 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
802 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
807 if (is_ptr(type) || (is_array(type) && !type->declarray))
809 conftype = RPC_FC_POINTER_CONFORMANCE;
810 conftype_string = "field pointer, ";
814 switch (subexpr->type)
817 subexpr = subexpr->ref;
818 operator_type = RPC_FC_DEREFERENCE;
819 operator_string = "FC_DEREFERENCE";
822 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
824 subexpr = subexpr->ref;
825 operator_type = RPC_FC_DIV_2;
826 operator_string = "FC_DIV_2";
830 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
832 subexpr = subexpr->ref;
833 operator_type = RPC_FC_MULT_2;
834 operator_string = "FC_MULT_2";
838 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
840 subexpr = subexpr->ref;
841 operator_type = RPC_FC_SUB_1;
842 operator_string = "FC_SUB_1";
846 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
848 subexpr = subexpr->ref;
849 operator_type = RPC_FC_ADD_1;
850 operator_string = "FC_ADD_1";
857 if (subexpr->type == EXPR_IDENTIFIER)
859 const type_t *correlation_variable = NULL;
860 unsigned char correlation_variable_type;
861 unsigned char param_type = 0;
865 if (structure->fields_or_args) LIST_FOR_EACH_ENTRY( var, structure->fields_or_args, const var_t, entry )
867 unsigned int align = 0;
868 /* FIXME: take alignment into account */
869 if (var->name && !strcmp(var->name, subexpr->u.sval))
871 correlation_variable = var->type;
874 offset += type_memsize(var->type, &align);
876 if (!correlation_variable)
877 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
880 correlation_variable = expr_resolve_type(NULL, structure, expr);
883 correlation_variable_type = correlation_variable->type;
885 switch (correlation_variable_type)
889 param_type = RPC_FC_SMALL;
893 param_type = RPC_FC_USMALL;
898 param_type = RPC_FC_SHORT;
901 param_type = RPC_FC_USHORT;
905 param_type = RPC_FC_LONG;
908 param_type = RPC_FC_ULONG;
911 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
912 correlation_variable_type);
915 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
916 conftype | param_type, conftype_string, string_of_type(param_type));
917 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
918 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
923 unsigned int callback_offset = 0;
924 struct expr_eval_routine *eval;
927 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
929 if (!strcmp (eval->structure->name, structure->name)
930 && !compare_expr (eval->expr, expr))
940 eval = xmalloc (sizeof(*eval));
941 eval->structure = structure;
942 eval->baseoff = baseoff;
944 list_add_tail (&expr_eval_routines, &eval->entry);
947 if (callback_offset > USHRT_MAX)
948 error("Maximum number of callback routines reached\n");
950 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
951 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
952 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
957 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
959 int have_align = FALSE;
963 if (!fields) return 0;
964 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
966 unsigned int falign = 0;
967 size_t fsize = type_memsize(v->type, &falign);
973 size = ROUND_SIZE(size, falign);
977 size = ROUND_SIZE(size, *align);
981 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
983 size_t size, maxs = 0;
984 unsigned int align = *pmaxa;
987 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
989 /* we could have an empty default field with NULL type */
992 size = type_memsize(v->type, &align);
993 if (maxs < size) maxs = size;
994 if (*pmaxa < align) *pmaxa = align;
1001 int get_padding(const var_list_t *fields)
1003 unsigned short offset = 0;
1010 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1012 type_t *ft = f->type;
1013 unsigned int align = 0;
1014 size_t size = type_memsize(ft, &align);
1017 offset = ROUND_SIZE(offset, align);
1021 return ROUNDING(offset, salign);
1024 size_t type_memsize(const type_t *t, unsigned int *align)
1028 if (t->kind == TKIND_ALIAS)
1029 size = type_memsize(t->orig, align);
1030 else if (t->declarray && is_conformant_array(t))
1032 type_memsize(t->ref, align);
1035 else if (is_ptr(t) || is_conformant_array(t))
1037 assert( pointer_size );
1038 size = pointer_size;
1039 if (size > *align) *align = size;
1041 else switch (t->type)
1048 if (size > *align) *align = size;
1055 if (size > *align) *align = size;
1059 case RPC_FC_ERROR_STATUS_T:
1063 if (size > *align) *align = size;
1068 if (size > *align) *align = size;
1071 case RPC_FC_CVSTRUCT:
1072 case RPC_FC_CPSTRUCT:
1073 case RPC_FC_CSTRUCT:
1074 case RPC_FC_PSTRUCT:
1075 case RPC_FC_BOGUS_STRUCT:
1076 size = fields_memsize(t->fields_or_args, align);
1078 case RPC_FC_ENCAPSULATED_UNION:
1079 case RPC_FC_NON_ENCAPSULATED_UNION:
1080 size = union_memsize(t->fields_or_args, align);
1082 case RPC_FC_SMFARRAY:
1083 case RPC_FC_LGFARRAY:
1084 case RPC_FC_SMVARRAY:
1085 case RPC_FC_LGVARRAY:
1086 case RPC_FC_BOGUS_ARRAY:
1087 size = t->dim * type_memsize(t->ref, align);
1090 error("type_memsize: Unknown type %d\n", t->type);
1097 int is_full_pointer_function(const func_t *func)
1100 if (type_has_full_pointer(get_func_return_type(func)))
1104 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1105 if (type_has_full_pointer( var->type ))
1110 void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
1112 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1113 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1114 fprintf(file, "\n");
1117 void write_full_pointer_free(FILE *file, int indent, const func_t *func)
1119 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1120 fprintf(file, "\n");
1123 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
1125 short absoff = type->ref->typestring_offset;
1126 short reloff = absoff - (offset + 2);
1127 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
1129 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1130 type->type, ptr_attr, string_of_type(type->type));
1131 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1132 reloff, reloff, absoff);
1136 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1138 unsigned char fc = type->ref->type;
1139 /* for historical reasons, write_simple_pointer also handled string types,
1140 * but no longer does. catch bad uses of the function with this check */
1141 if (is_string_type(type->attrs, type))
1142 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1143 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1144 type->type, string_of_type(type->type));
1145 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1146 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1150 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1152 print_file(file, 0, "/* %u (", tfsoff);
1153 write_type_decl(file, t, NULL);
1154 print_file(file, 0, ") */\n");
1157 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1159 unsigned int offset = *typestring_offset;
1161 print_start_tfs_comment(file, type, offset);
1162 update_tfsoff(type, offset, file);
1164 if (type->ref->typestring_offset)
1165 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1166 else if (is_base_type(type->ref->type))
1167 *typestring_offset += write_simple_pointer(file, type);
1172 static int processed(const type_t *type)
1174 return type->typestring_offset && !type->tfswrite;
1177 static int user_type_has_variable_size(const type_t *t)
1182 switch (get_struct_type(t))
1184 case RPC_FC_PSTRUCT:
1185 case RPC_FC_CSTRUCT:
1186 case RPC_FC_CPSTRUCT:
1187 case RPC_FC_CVSTRUCT:
1190 /* Note: Since this only applies to user types, we can't have a conformant
1191 array here, and strings should get filed under pointer in this case. */
1195 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1197 unsigned int start, absoff, flags;
1198 unsigned int align = 0, ualign = 0;
1199 const char *name = NULL;
1200 type_t *utype = get_user_type(type, &name);
1201 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1202 size_t size = type_memsize(type, &align);
1203 unsigned short funoff = user_type_offset(name);
1208 if (is_base_type(utype->type))
1211 print_start_tfs_comment(file, utype, absoff);
1212 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1213 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1218 if (!processed(utype))
1219 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1220 absoff = utype->typestring_offset;
1223 if (utype->type == RPC_FC_RP)
1225 else if (utype->type == RPC_FC_UP)
1231 update_tfsoff(type, start, file);
1232 print_start_tfs_comment(file, type, start);
1233 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1234 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1235 flags | (align - 1), align - 1, flags);
1236 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1237 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1238 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1240 reloff = absoff - *tfsoff;
1241 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1245 static void write_member_type(FILE *file, const type_t *cont,
1246 const attr_list_t *attrs, const type_t *type,
1247 unsigned int *corroff, unsigned int *tfsoff)
1249 if (is_embedded_complex(type) && !is_conformant_array(type))
1254 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1261 absoff = type->typestring_offset;
1263 reloff = absoff - (*tfsoff + 2);
1265 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1266 /* FIXME: actually compute necessary padding */
1267 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1268 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1269 reloff, reloff, absoff);
1272 else if (is_ptr(type) || is_conformant_array(type))
1274 unsigned char fc = (get_struct_type(cont) == RPC_FC_BOGUS_STRUCT
1277 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1280 else if (!write_base_type(file, type, tfsoff))
1281 error("Unsupported member type 0x%x\n", type->type);
1284 static void write_end(FILE *file, unsigned int *tfsoff)
1286 if (*tfsoff % 2 == 0)
1288 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1291 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1295 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1297 unsigned int offset = 0;
1298 var_list_t *fs = type->fields_or_args;
1301 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1303 unsigned int align = 0;
1304 type_t *ft = f->type;
1305 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1307 unsigned int absoff = ft->typestring_offset;
1308 short reloff = absoff - (*tfsoff + 6);
1309 print_file(file, 0, "/* %d */\n", *tfsoff);
1310 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1311 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1312 write_conf_or_var_desc(file, current_structure, offset, ft,
1313 get_attrp(f->attrs, ATTR_SWITCHIS));
1314 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1315 reloff, reloff, absoff);
1319 /* FIXME: take alignment into account */
1320 offset += type_memsize(ft, &align);
1324 static int write_no_repeat_pointer_descriptions(
1325 FILE *file, type_t *type,
1326 size_t *offset_in_memory, size_t *offset_in_buffer,
1327 unsigned int *typestring_offset)
1332 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1336 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1337 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1339 /* pointer instance */
1340 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1341 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1342 *typestring_offset += 6;
1346 if (is_string_type(type->attrs, type))
1347 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1349 write_pointer_tfs(file, type, typestring_offset);
1353 unsigned absoff = type->typestring_offset;
1354 short reloff = absoff - (*typestring_offset + 2);
1355 /* FIXME: get pointer attributes from field */
1356 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1357 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1358 reloff, reloff, absoff);
1359 *typestring_offset += 4;
1363 memsize = type_memsize(type, &align);
1364 *offset_in_memory += memsize;
1365 /* increment these separately as in the case of conformant (varying)
1366 * structures these start at different values */
1367 *offset_in_buffer += memsize;
1372 if (is_non_complex_struct(type))
1375 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1377 if (offset_in_memory && offset_in_buffer)
1381 type_memsize(v->type, &align);
1382 padding = ROUNDING(*offset_in_memory, align);
1383 *offset_in_memory += padding;
1384 *offset_in_buffer += padding;
1386 written += write_no_repeat_pointer_descriptions(
1388 offset_in_memory, offset_in_buffer, typestring_offset);
1395 memsize = type_memsize(type, &align);
1396 *offset_in_memory += memsize;
1397 /* increment these separately as in the case of conformant (varying)
1398 * structures these start at different values */
1399 *offset_in_buffer += memsize;
1405 static int write_pointer_description_offsets(
1406 FILE *file, const attr_list_t *attrs, type_t *type,
1407 size_t *offset_in_memory, size_t *offset_in_buffer,
1408 unsigned int *typestring_offset)
1413 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1415 if (offset_in_memory && offset_in_buffer)
1419 /* pointer instance */
1420 /* FIXME: sometimes from end of structure, sometimes from beginning */
1421 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1422 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1425 memsize = type_memsize(type, &align);
1426 *offset_in_memory += memsize;
1427 /* increment these separately as in the case of conformant (varying)
1428 * structures these start at different values */
1429 *offset_in_buffer += memsize;
1431 *typestring_offset += 4;
1433 if (is_string_type(attrs, type))
1434 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1435 else if (processed(type->ref) || is_base_type(type->ref->type))
1436 write_pointer_tfs(file, type, typestring_offset);
1438 error("write_pointer_description_offsets: type format string unknown\n");
1445 return write_pointer_description_offsets(
1446 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1449 else if (is_non_complex_struct(type))
1451 /* otherwise search for interesting fields to parse */
1453 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1455 if (offset_in_memory && offset_in_buffer)
1459 type_memsize(v->type, &align);
1460 padding = ROUNDING(*offset_in_memory, align);
1461 *offset_in_memory += padding;
1462 *offset_in_buffer += padding;
1464 written += write_pointer_description_offsets(
1465 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1471 if (offset_in_memory && offset_in_buffer)
1475 memsize = type_memsize(type, &align);
1476 *offset_in_memory += memsize;
1477 /* increment these separately as in the case of conformant (varying)
1478 * structures these start at different values */
1479 *offset_in_buffer += memsize;
1486 /* Note: if file is NULL return value is number of pointers to write, else
1487 * it is the number of type format characters written */
1488 static int write_fixed_array_pointer_descriptions(
1489 FILE *file, const attr_list_t *attrs, type_t *type,
1490 size_t *offset_in_memory, size_t *offset_in_buffer,
1491 unsigned int *typestring_offset)
1494 int pointer_count = 0;
1495 int real_type = get_array_type( type );
1497 if (real_type == RPC_FC_SMFARRAY || real_type == RPC_FC_LGFARRAY)
1499 unsigned int temp = 0;
1500 /* unfortunately, this needs to be done in two passes to avoid
1501 * writing out redundant FC_FIXED_REPEAT descriptions */
1502 pointer_count = write_pointer_description_offsets(
1503 NULL, attrs, type->ref, NULL, NULL, &temp);
1504 if (pointer_count > 0)
1506 unsigned int increment_size;
1507 size_t offset_of_array_pointer_mem = 0;
1508 size_t offset_of_array_pointer_buf = 0;
1511 increment_size = type_memsize(type->ref, &align);
1513 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1514 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1515 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1516 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1517 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1518 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1519 *typestring_offset += 10;
1521 pointer_count = write_pointer_description_offsets(
1522 file, attrs, type, &offset_of_array_pointer_mem,
1523 &offset_of_array_pointer_buf, typestring_offset);
1526 else if (is_struct(type->type))
1529 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1531 if (offset_in_memory && offset_in_buffer)
1535 type_memsize(v->type, &align);
1536 padding = ROUNDING(*offset_in_memory, align);
1537 *offset_in_memory += padding;
1538 *offset_in_buffer += padding;
1540 pointer_count += write_fixed_array_pointer_descriptions(
1541 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1547 if (offset_in_memory && offset_in_buffer)
1551 memsize = type_memsize(type, &align);
1552 *offset_in_memory += memsize;
1553 /* increment these separately as in the case of conformant (varying)
1554 * structures these start at different values */
1555 *offset_in_buffer += memsize;
1559 return pointer_count;
1562 /* Note: if file is NULL return value is number of pointers to write, else
1563 * it is the number of type format characters written */
1564 static int write_conformant_array_pointer_descriptions(
1565 FILE *file, const attr_list_t *attrs, type_t *type,
1566 size_t offset_in_memory, unsigned int *typestring_offset)
1569 int pointer_count = 0;
1571 if (is_conformant_array(type) && !type->length_is)
1573 unsigned int temp = 0;
1574 /* unfortunately, this needs to be done in two passes to avoid
1575 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1576 pointer_count = write_pointer_description_offsets(
1577 NULL, attrs, type->ref, NULL, NULL, &temp);
1578 if (pointer_count > 0)
1580 unsigned int increment_size;
1581 size_t offset_of_array_pointer_mem = offset_in_memory;
1582 size_t offset_of_array_pointer_buf = offset_in_memory;
1585 increment_size = type_memsize(type->ref, &align);
1587 if (increment_size > USHRT_MAX)
1588 error("array size of %u bytes is too large\n", increment_size);
1590 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1591 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1592 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1593 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1594 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1595 *typestring_offset += 8;
1597 pointer_count = write_pointer_description_offsets(
1598 file, attrs, type->ref, &offset_of_array_pointer_mem,
1599 &offset_of_array_pointer_buf, typestring_offset);
1603 return pointer_count;
1606 /* Note: if file is NULL return value is number of pointers to write, else
1607 * it is the number of type format characters written */
1608 static int write_varying_array_pointer_descriptions(
1609 FILE *file, const attr_list_t *attrs, type_t *type,
1610 size_t *offset_in_memory, size_t *offset_in_buffer,
1611 unsigned int *typestring_offset)
1614 int pointer_count = 0;
1616 if (is_array(type) && type->length_is)
1618 unsigned int temp = 0;
1619 /* unfortunately, this needs to be done in two passes to avoid
1620 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1621 pointer_count = write_pointer_description_offsets(
1622 NULL, attrs, type->ref, NULL, NULL, &temp);
1623 if (pointer_count > 0)
1625 unsigned int increment_size;
1628 increment_size = type_memsize(type->ref, &align);
1630 if (increment_size > USHRT_MAX)
1631 error("array size of %u bytes is too large\n", increment_size);
1633 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1634 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1635 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1636 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1637 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1638 *typestring_offset += 8;
1640 pointer_count = write_pointer_description_offsets(
1641 file, attrs, type, offset_in_memory,
1642 offset_in_buffer, typestring_offset);
1645 else if (is_struct(type->type))
1648 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1650 if (offset_in_memory && offset_in_buffer)
1654 if (is_array(v->type) && v->type->length_is)
1656 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1657 /* skip over variance and offset in buffer */
1658 *offset_in_buffer += 8;
1662 type_memsize(v->type, &align);
1663 padding = ROUNDING(*offset_in_memory, align);
1664 *offset_in_memory += padding;
1665 *offset_in_buffer += padding;
1667 pointer_count += write_varying_array_pointer_descriptions(
1668 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1674 if (offset_in_memory && offset_in_buffer)
1678 memsize = type_memsize(type, &align);
1679 *offset_in_memory += memsize;
1680 /* increment these separately as in the case of conformant (varying)
1681 * structures these start at different values */
1682 *offset_in_buffer += memsize;
1686 return pointer_count;
1689 static void write_pointer_description(FILE *file, type_t *type,
1690 unsigned int *typestring_offset)
1692 size_t offset_in_buffer;
1693 size_t offset_in_memory;
1695 /* pass 1: search for single instance of a pointer (i.e. don't descend
1697 if (!is_array(type))
1699 offset_in_memory = 0;
1700 offset_in_buffer = 0;
1701 write_no_repeat_pointer_descriptions(
1703 &offset_in_memory, &offset_in_buffer, typestring_offset);
1706 /* pass 2: search for pointers in fixed arrays */
1707 offset_in_memory = 0;
1708 offset_in_buffer = 0;
1709 write_fixed_array_pointer_descriptions(
1711 &offset_in_memory, &offset_in_buffer, typestring_offset);
1713 /* pass 3: search for pointers in conformant only arrays (but don't descend
1714 * into conformant varying or varying arrays) */
1715 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1716 write_conformant_array_pointer_descriptions(
1717 file, NULL, type, 0, typestring_offset);
1718 else if (get_struct_type(type) == RPC_FC_CPSTRUCT)
1720 unsigned int align = 0;
1721 type_t *carray = find_array_or_string_in_struct(type)->type;
1722 write_conformant_array_pointer_descriptions(
1724 type_memsize(type, &align),
1728 /* pass 4: search for pointers in varying arrays */
1729 offset_in_memory = 0;
1730 offset_in_buffer = 0;
1731 write_varying_array_pointer_descriptions(
1733 &offset_in_memory, &offset_in_buffer, typestring_offset);
1736 int is_declptr(const type_t *t)
1738 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1741 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1743 const char *name, unsigned int *typestring_offset)
1745 size_t start_offset;
1746 unsigned char rtype;
1748 if (is_declptr(type))
1750 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1751 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1753 pointer_type = RPC_FC_RP;
1754 print_start_tfs_comment(file, type, *typestring_offset);
1755 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1756 pointer_type, flag, string_of_type(pointer_type),
1757 flag ? " [simple_pointer]" : "");
1758 *typestring_offset += 2;
1761 print_file(file, 2, "NdrFcShort(0x2),\n");
1762 *typestring_offset += 2;
1766 start_offset = *typestring_offset;
1767 update_tfsoff(type, start_offset, file);
1769 rtype = type->ref->type;
1771 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1773 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1774 return start_offset;
1777 if (type->declarray && !is_conformant_array(type))
1779 /* FIXME: multi-dimensional array */
1780 if (0xffffuL < type->dim)
1781 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1782 name, 0xffffu, type->dim - 0xffffu);
1784 if (rtype == RPC_FC_CHAR)
1785 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1787 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1788 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1789 *typestring_offset += 2;
1791 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1792 *typestring_offset += 2;
1794 return start_offset;
1796 else if (type->size_is)
1798 unsigned int align = 0;
1800 if (rtype == RPC_FC_CHAR)
1801 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1803 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1804 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1805 *typestring_offset += 2;
1807 *typestring_offset += write_conf_or_var_desc(
1808 file, current_structure,
1809 (type->declarray && current_structure
1810 ? type_memsize(current_structure, &align)
1812 type, type->size_is);
1814 return start_offset;
1818 if (rtype == RPC_FC_WCHAR)
1819 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1821 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1822 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1823 *typestring_offset += 2;
1825 return start_offset;
1829 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1830 const char *name, unsigned int *typestring_offset)
1832 const expr_t *length_is = type->length_is;
1833 const expr_t *size_is = type->size_is;
1834 unsigned int align = 0;
1836 size_t start_offset;
1839 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1840 unsigned int baseoff
1841 = type->declarray && current_structure
1842 ? type_memsize(current_structure, &align)
1846 pointer_type = RPC_FC_RP;
1848 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1851 has_pointer = type_has_pointers(type->ref);
1854 size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1855 real_type = get_array_type( type );
1857 start_offset = *typestring_offset;
1858 update_tfsoff(type, start_offset, file);
1859 print_start_tfs_comment(file, type, start_offset);
1860 print_file(file, 2, "0x%02x,\t/* %s */\n", real_type, string_of_type(real_type));
1861 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1862 *typestring_offset += 2;
1865 if (real_type != RPC_FC_BOGUS_ARRAY)
1867 if (real_type == RPC_FC_LGFARRAY || real_type == RPC_FC_LGVARRAY)
1869 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1870 *typestring_offset += 4;
1874 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1875 *typestring_offset += 2;
1878 if (is_conformant_array(type))
1880 += write_conf_or_var_desc(file, current_structure, baseoff,
1883 if (real_type == RPC_FC_SMVARRAY || real_type == RPC_FC_LGVARRAY)
1885 unsigned int elalign = 0;
1886 size_t elsize = type_memsize(type->ref, &elalign);
1888 if (real_type == RPC_FC_LGVARRAY)
1890 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1891 *typestring_offset += 4;
1895 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1896 *typestring_offset += 2;
1899 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1900 *typestring_offset += 2;
1905 += write_conf_or_var_desc(file, current_structure, baseoff,
1908 if (has_pointer && (!type->declarray || !current_structure))
1910 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1911 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1912 *typestring_offset += 2;
1913 write_pointer_description(file, type, typestring_offset);
1914 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1915 *typestring_offset += 1;
1918 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1919 write_end(file, typestring_offset);
1923 unsigned int dim = size_is ? 0 : type->dim;
1924 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1925 *typestring_offset += 2;
1927 += write_conf_or_var_desc(file, current_structure, baseoff,
1930 += write_conf_or_var_desc(file, current_structure, baseoff,
1932 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1933 write_end(file, typestring_offset);
1936 return start_offset;
1939 static const var_t *find_array_or_string_in_struct(const type_t *type)
1941 const var_t *last_field;
1945 if (!type->fields_or_args || list_empty(type->fields_or_args))
1948 last_field = LIST_ENTRY( list_tail(type->fields_or_args), const var_t, entry );
1949 ft = last_field->type;
1951 if (ft->declarray && is_conformant_array(ft))
1954 real_type = get_struct_type( type );
1955 if (real_type == RPC_FC_CSTRUCT || real_type == RPC_FC_CPSTRUCT || real_type == RPC_FC_CVSTRUCT)
1956 return find_array_or_string_in_struct(ft);
1961 static void write_struct_members(FILE *file, const type_t *type,
1962 unsigned int *corroff, unsigned int *typestring_offset)
1965 unsigned short offset = 0;
1969 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
1971 type_t *ft = field->type;
1972 if (!ft->declarray || !is_conformant_array(ft))
1974 unsigned int align = 0;
1975 size_t size = type_memsize(ft, &align);
1978 if ((align - 1) & offset)
1980 unsigned char fc = 0;
1984 fc = RPC_FC_ALIGNM4;
1987 fc = RPC_FC_ALIGNM8;
1990 error("write_struct_members: cannot align type %d\n", ft->type);
1992 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1993 offset = ROUND_SIZE(offset, align);
1994 *typestring_offset += 1;
1996 write_member_type(file, type, field->attrs, field->type, corroff,
2002 padding = ROUNDING(offset, salign);
2005 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2006 RPC_FC_STRUCTPAD1 + padding - 1,
2008 *typestring_offset += 1;
2011 write_end(file, typestring_offset);
2014 static size_t write_struct_tfs(FILE *file, type_t *type,
2015 const char *name, unsigned int *tfsoff)
2017 const type_t *save_current_structure = current_structure;
2018 unsigned int total_size;
2020 size_t start_offset;
2021 size_t array_offset;
2022 int has_pointers = 0;
2023 unsigned int align = 0;
2024 unsigned int corroff;
2026 int real_type = get_struct_type( type );
2029 current_structure = type;
2031 total_size = type_memsize(type, &align);
2032 if (total_size > USHRT_MAX)
2033 error("structure size for %s exceeds %d bytes by %d bytes\n",
2034 name, USHRT_MAX, total_size - USHRT_MAX);
2036 if (type->fields_or_args) LIST_FOR_EACH_ENTRY(f, type->fields_or_args, var_t, entry)
2037 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2039 if (!has_pointers) has_pointers = type_has_pointers(type);
2041 array = find_array_or_string_in_struct(type);
2042 if (array && !processed(array->type))
2044 = is_attr(array->attrs, ATTR_STRING)
2045 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2046 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2049 write_descriptors(file, type, tfsoff);
2051 start_offset = *tfsoff;
2052 update_tfsoff(type, start_offset, file);
2053 print_start_tfs_comment(file, type, start_offset);
2054 print_file(file, 2, "0x%x,\t/* %s */\n", real_type, string_of_type(real_type));
2055 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2056 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
2061 unsigned int absoff = array->type->typestring_offset;
2062 short reloff = absoff - *tfsoff;
2063 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
2064 reloff, reloff, absoff);
2067 else if (real_type == RPC_FC_BOGUS_STRUCT)
2069 print_file(file, 2, "NdrFcShort(0x0),\n");
2073 if (real_type == RPC_FC_BOGUS_STRUCT)
2075 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2076 nothing is written to file yet. On the actual writing pass,
2077 this will have been updated. */
2078 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2079 int reloff = absoff - *tfsoff;
2080 assert( reloff >= 0 );
2081 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%u) */\n",
2082 reloff, reloff, absoff);
2085 else if ((real_type == RPC_FC_PSTRUCT) ||
2086 (real_type == RPC_FC_CPSTRUCT) ||
2087 (real_type == RPC_FC_CVSTRUCT && has_pointers))
2089 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2090 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2092 write_pointer_description(file, type, tfsoff);
2093 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2097 write_struct_members(file, type, &corroff, tfsoff);
2099 if (real_type == RPC_FC_BOGUS_STRUCT)
2101 const var_list_t *fs = type->fields_or_args;
2104 type->ptrdesc = *tfsoff;
2105 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
2107 type_t *ft = f->type;
2110 if (is_string_type(f->attrs, ft))
2111 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2113 write_pointer_tfs(file, ft, tfsoff);
2115 else if (!ft->declarray && is_conformant_array(ft))
2117 unsigned int absoff = ft->typestring_offset;
2118 short reloff = absoff - (*tfsoff + 2);
2119 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2120 /* FIXME: We need to store pointer attributes for arrays
2121 so we don't lose pointer_default info. */
2123 ptr_type = RPC_FC_UP;
2124 print_file(file, 0, "/* %d */\n", *tfsoff);
2125 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2126 string_of_type(ptr_type));
2127 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2128 reloff, reloff, absoff);
2132 if (type->ptrdesc == *tfsoff)
2136 current_structure = save_current_structure;
2137 return start_offset;
2140 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2141 unsigned char flags, size_t offset,
2142 unsigned int *typeformat_offset)
2144 size_t start_offset = *typeformat_offset;
2145 short reloff = offset - (*typeformat_offset + 2);
2146 int in_attr, out_attr;
2147 in_attr = is_attr(attrs, ATTR_IN);
2148 out_attr = is_attr(attrs, ATTR_OUT);
2149 if (!in_attr && !out_attr) in_attr = 1;
2151 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2154 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2157 string_of_type(pointer_type));
2161 fprintf(file, " [allocated_on_stack]");
2163 fprintf(file, " [pointer_deref]");
2164 fprintf(file, " */\n");
2167 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
2168 *typeformat_offset += 4;
2170 return start_offset;
2173 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2177 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2179 else if (is_base_type(t->type))
2181 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2182 t->type, string_of_type(t->type));
2184 else if (t->typestring_offset)
2186 short reloff = t->typestring_offset - *tfsoff;
2187 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
2188 reloff, reloff, t->typestring_offset);
2191 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
2196 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2198 unsigned int align = 0;
2199 unsigned int start_offset;
2200 size_t size = type_memsize(type, &align);
2203 type_t *deftype = NULL;
2204 short nodeftype = 0xffff;
2209 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2211 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
2212 fields = uv->type->fields_or_args;
2215 fields = type->fields_or_args;
2217 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2219 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2221 nbranch += list_count(cases);
2223 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2226 start_offset = *tfsoff;
2227 update_tfsoff(type, start_offset, file);
2228 print_start_tfs_comment(file, type, start_offset);
2229 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2231 const var_t *sv = LIST_ENTRY(list_head(type->fields_or_args), const var_t, entry);
2232 const type_t *st = sv->type;
2245 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2246 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2247 0x40 | st->type, string_of_type(st->type));
2251 error("union switch type must be an integer, char, or enum\n");
2254 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2256 static const expr_t dummy_expr; /* FIXME */
2257 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2270 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2271 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2272 st->type, string_of_type(st->type));
2276 error("union switch type must be an integer, char, or enum\n");
2279 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2282 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
2283 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
2286 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2288 type_t *ft = f->type;
2289 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2290 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2293 if (cases == NULL && !deflt)
2294 error("union field %s with neither case nor default attribute\n", f->name);
2296 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2298 /* MIDL doesn't check for duplicate cases, even though that seems
2299 like a reasonable thing to do, it just dumps them to the TFS
2300 like we're going to do here. */
2301 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2303 write_branch_type(file, ft, tfsoff);
2306 /* MIDL allows multiple default branches, even though that seems
2307 illogical, it just chooses the last one, which is what we will
2318 write_branch_type(file, deftype, tfsoff);
2322 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2326 return start_offset;
2329 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2330 unsigned int *typeformat_offset)
2333 size_t start_offset = *typeformat_offset;
2334 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2338 print_file(file, 2, "0x2f, /* FC_IP */\n");
2339 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2341 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2345 const type_t *base = is_ptr(type) ? type->ref : type;
2346 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2349 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2351 update_tfsoff(type, start_offset, file);
2352 print_start_tfs_comment(file, type, start_offset);
2353 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2354 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2355 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2356 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2357 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2358 for (i = 0; i < 8; ++i)
2359 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2362 fprintf(file, "\n");
2364 *typeformat_offset += 18;
2366 return start_offset;
2369 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2371 unsigned int *typeformat_offset)
2373 size_t start_offset = *typeformat_offset;
2374 unsigned char flags = 0;
2376 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2377 flags |= NDR_STRICT_CONTEXT_HANDLE;
2381 if (is_attr(var->attrs, ATTR_IN))
2384 if (!is_attr(var->attrs, ATTR_OUT))
2385 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2387 if (is_attr(var->attrs, ATTR_OUT))
2390 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2391 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2392 /* return and can't be null values overlap */
2393 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2394 print_file(file, 0, "can't be null, ");
2395 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2396 print_file(file, 0, "serialize, ");
2397 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2398 print_file(file, 0, "no serialize, ");
2399 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2400 print_file(file, 0, "strict, ");
2401 if ((flags & 0x21) == 0x20)
2402 print_file(file, 0, "out, ");
2403 if ((flags & 0x21) == 0x21)
2404 print_file(file, 0, "return, ");
2406 print_file(file, 0, "in, ");
2408 print_file(file, 0, "via ptr, ");
2409 print_file(file, 0, "*/\n");
2410 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2411 print_file(file, 2, "0, /* FIXME: param num */\n");
2412 *typeformat_offset += 4;
2414 return start_offset;
2417 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2418 type_t *type, const var_t *var,
2419 unsigned int *typeformat_offset)
2423 if (is_context_handle(type))
2424 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2426 if (is_user_type(type))
2428 write_user_tfs(file, type, typeformat_offset);
2429 return type->typestring_offset;
2432 if (is_string_type(var->attrs, type))
2433 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2439 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2440 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2441 /* Top level pointers to conformant arrays may be handled specially
2442 since we can bypass the pointer, but if the array is buried
2443 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2444 always need to write the pointer. */
2445 if (!ptr_type && var->type != type)
2446 /* FIXME: This should use pointer_default, but the information
2447 isn't kept around for arrays. */
2448 ptr_type = RPC_FC_UP;
2449 if (ptr_type && ptr_type != RPC_FC_RP)
2451 unsigned int absoff = type->typestring_offset;
2452 short reloff = absoff - (*typeformat_offset + 2);
2453 off = *typeformat_offset;
2454 print_file(file, 0, "/* %d */\n", off);
2455 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2456 string_of_type(ptr_type));
2457 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2458 reloff, reloff, absoff);
2459 *typeformat_offset += 4;
2466 /* basic types don't need a type format string */
2467 if (is_base_type(type->type))
2470 if (processed(type)) return type->typestring_offset;
2475 case RPC_FC_PSTRUCT:
2476 case RPC_FC_CSTRUCT:
2477 case RPC_FC_CPSTRUCT:
2478 case RPC_FC_CVSTRUCT:
2479 case RPC_FC_BOGUS_STRUCT:
2480 return write_struct_tfs(file, type, var->name, typeformat_offset);
2481 case RPC_FC_ENCAPSULATED_UNION:
2482 case RPC_FC_NON_ENCAPSULATED_UNION:
2483 return write_union_tfs(file, type, typeformat_offset);
2485 case RPC_FC_BIND_PRIMITIVE:
2489 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2492 else if (last_ptr(type))
2494 size_t start_offset = *typeformat_offset;
2495 int in_attr = is_attr(var->attrs, ATTR_IN);
2496 int out_attr = is_attr(var->attrs, ATTR_OUT);
2497 const type_t *base = type->ref;
2499 if (base->type == RPC_FC_IP
2501 && is_attr(var->attrs, ATTR_IIDIS)))
2503 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2506 /* special case for pointers to base types */
2507 if (is_base_type(base->type))
2509 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2510 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2511 string_of_type(type->type),
2512 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2513 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2514 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2515 *typeformat_offset += 4;
2516 return start_offset;
2520 assert(is_ptr(type));
2522 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2524 fprintf(file, "/* %2u */\n", *typeformat_offset);
2525 return write_pointer_only_tfs(file, var->attrs, type->type,
2526 !last_ptr(type) ? 0x10 : 0,
2527 offset, typeformat_offset);
2530 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2531 const char *name, int write_ptr, unsigned int *tfsoff)
2535 if (is_user_type(type))
2537 write_user_tfs(file, type, tfsoff);
2539 else if (is_string_type(attrs, type))
2541 write_string_tfs(file, attrs, type, name, tfsoff);
2543 else if (is_ptr(type))
2545 type_t *ref = type->ref;
2547 if (ref->type == RPC_FC_IP
2549 && is_attr(attrs, ATTR_IIDIS)))
2551 write_ip_tfs(file, attrs, type, tfsoff);
2555 if (!processed(ref) && !is_base_type(ref->type))
2556 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2559 write_pointer_tfs(file, type, tfsoff);
2564 else if (type->declarray && is_conformant_array(type))
2565 ; /* conformant arrays and strings are handled specially */
2566 else if (is_array(type))
2568 write_array_tfs(file, attrs, type, name, tfsoff);
2569 if (is_conformant_array(type))
2572 else if (is_struct(type->type))
2574 if (!processed(type))
2575 write_struct_tfs(file, type, name, tfsoff);
2577 else if (is_union(type->type))
2579 if (!processed(type))
2580 write_union_tfs(file, type, tfsoff);
2582 else if (!is_base_type(type->type))
2583 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2589 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2590 type_pred_t pred, unsigned int *typeformat_offset)
2593 const statement_t *stmt;
2595 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2597 const type_t *iface;
2598 if (stmt->type == STMT_LIBRARY)
2600 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2603 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2606 iface = stmt->u.type;
2613 current_iface = iface;
2614 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2616 if (is_local(func->def->attrs)) continue;
2618 if (!is_void(get_func_return_type(func)))
2620 var_t v = *func->def;
2621 v.type = get_func_return_type(func);
2622 update_tfsoff(get_func_return_type(func),
2623 write_typeformatstring_var(
2624 file, 2, NULL, get_func_return_type(func),
2625 &v, typeformat_offset),
2629 current_func = func;
2631 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2634 write_typeformatstring_var(
2635 file, 2, func, var->type, var,
2642 return *typeformat_offset + 1;
2645 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2647 unsigned int typeformat_offset = 2;
2649 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2653 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2657 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2658 print_file(file, indent, "{\n");
2660 print_file(file, indent, "0,\n");
2661 print_file(file, indent, "{\n");
2663 print_file(file, indent, "NdrFcShort(0x0),\n");
2665 set_all_tfswrite(TRUE);
2666 process_tfs(file, stmts, pred);
2668 print_file(file, indent, "0x0\n");
2670 print_file(file, indent, "}\n");
2672 print_file(file, indent, "};\n");
2673 print_file(file, indent, "\n");
2676 static unsigned int get_required_buffer_size_type(
2677 const type_t *type, const char *name, unsigned int *alignment)
2680 const type_t *utype;
2683 if ((utype = get_user_type(type, &uname)))
2685 return get_required_buffer_size_type(utype, uname, alignment);
2689 switch (get_struct_type(type))
2709 case RPC_FC_ERROR_STATUS_T:
2719 case RPC_FC_BIND_PRIMITIVE:
2723 if (!type->fields_or_args) return 0;
2724 return fields_memsize(type->fields_or_args, alignment);
2728 is_base_type( type->ref->type ) || get_struct_type(type->ref) == RPC_FC_STRUCT
2729 ? get_required_buffer_size_type( type->ref, name, alignment )
2732 case RPC_FC_SMFARRAY:
2733 case RPC_FC_LGFARRAY:
2734 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2742 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2744 int in_attr = is_attr(var->attrs, ATTR_IN);
2745 int out_attr = is_attr(var->attrs, ATTR_OUT);
2747 if (!in_attr && !out_attr)
2752 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2753 pass == PASS_RETURN)
2755 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2761 if (!is_string_type(var->attrs, var->type))
2762 return get_required_buffer_size_type(var->type, var->name,
2768 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2771 unsigned int total_size = 0, alignment;
2775 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2777 total_size += get_required_buffer_size(var, &alignment, pass);
2778 total_size += alignment;
2782 if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2784 var_t v = *func->def;
2785 v.type = get_func_return_type(func);
2786 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2787 total_size += alignment;
2792 static void print_phase_function(FILE *file, int indent, const char *type,
2793 const char *local_var_prefix, enum remoting_phase phase,
2794 const var_t *var, unsigned int type_offset)
2796 const char *function;
2799 case PHASE_BUFFERSIZE:
2800 function = "BufferSize";
2803 function = "Marshall";
2805 case PHASE_UNMARSHAL:
2806 function = "Unmarshall";
2816 print_file(file, indent, "Ndr%s%s(\n", type, function);
2818 print_file(file, indent, "&__frame->_StubMsg,\n");
2819 print_file(file, indent, "%s%s%s%s%s,\n",
2820 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2821 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2823 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2825 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2826 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2827 if (phase == PHASE_UNMARSHAL)
2828 print_file(file, indent, "0);\n");
2832 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2833 enum remoting_phase phase, enum pass pass, const var_t *var,
2834 const char *varname)
2836 type_t *type = var->type;
2838 unsigned int alignment = 0;
2839 unsigned char rtype;
2841 /* no work to do for other phases, buffer sizing is done elsewhere */
2842 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2845 rtype = is_ptr(type) ? type->ref->type : type->type;
2869 case RPC_FC_ERROR_STATUS_T:
2881 case RPC_FC_BIND_PRIMITIVE:
2882 /* no marshalling needed */
2886 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2890 if (phase == PHASE_MARSHAL)
2891 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2892 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2893 alignment - 1, alignment - 1);
2895 if (phase == PHASE_MARSHAL)
2897 print_file(file, indent, "*(");
2898 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2900 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2902 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2903 fprintf(file, "%s%s", local_var_prefix, varname);
2904 fprintf(file, ";\n");
2906 else if (phase == PHASE_UNMARSHAL)
2908 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2909 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2910 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2911 print_file(file, indent, "{\n");
2912 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2913 print_file(file, indent, "}\n");
2914 if (pass == PASS_IN || pass == PASS_RETURN)
2915 print_file(file, indent, "");
2917 print_file(file, indent, "*");
2918 fprintf(file, "%s%s", local_var_prefix, varname);
2919 if (pass == PASS_IN && is_ptr(type))
2920 fprintf(file, " = (");
2922 fprintf(file, " = *(");
2923 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2924 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2927 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2928 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2929 fprintf(file, ");\n");
2932 /* returns whether the MaxCount, Offset or ActualCount members need to be
2933 * filled in for the specified phase */
2934 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2936 return (phase != PHASE_UNMARSHAL);
2939 expr_t *get_size_is_expr(const type_t *t, const char *name)
2943 for ( ; is_ptr(t) || is_array(t); t = t->ref)
2949 error("%s: multidimensional conformant"
2950 " arrays not supported at the top level\n",
2957 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
2958 enum remoting_phase phase, const var_t *var)
2960 const type_t *type = var->type;
2961 /* get fundamental type for the argument */
2964 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2966 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2968 else if (is_array(type) || is_string_type(var->attrs, type))
2970 if (is_conformance_needed_for_phase(phase))
2974 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2975 write_expr(file, type->size_is, 1, 1, NULL, NULL, local_var_prefix);
2976 fprintf(file, ";\n\n");
2978 if (type->length_is)
2980 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
2981 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
2982 write_expr(file, type->length_is, 1, 1, NULL, NULL, local_var_prefix);
2983 fprintf(file, ";\n\n");
2988 else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
2990 if (is_conformance_needed_for_phase(phase))
2992 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2993 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
2994 fprintf(file, ";\n\n");
2998 else if (type->type == RPC_FC_IP)
3002 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3004 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3005 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3006 fprintf( file, ";\n\n" );
3010 else if (is_ptr(type))
3017 static void write_remoting_arg(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3018 enum pass pass, enum remoting_phase phase, const var_t *var)
3020 int in_attr, out_attr, pointer_type;
3021 const type_t *type = var->type;
3022 unsigned char rtype;
3023 size_t start_offset = type->typestring_offset;
3025 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3027 pointer_type = RPC_FC_RP;
3029 in_attr = is_attr(var->attrs, ATTR_IN);
3030 out_attr = is_attr(var->attrs, ATTR_OUT);
3031 if (!in_attr && !out_attr)
3034 if (phase != PHASE_FREE)
3038 if (!in_attr) return;
3041 if (!out_attr) return;
3047 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3048 rtype = get_struct_type(type);
3050 if (is_context_handle(type))
3052 if (phase == PHASE_MARSHAL)
3054 if (pass == PASS_IN)
3056 /* if the context_handle attribute appears in the chain of types
3057 * without pointers being followed, then the context handle must
3058 * be direct, otherwise it is a pointer */
3059 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3060 print_file(file, indent, "NdrClientContextMarshall(\n");
3061 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3062 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3063 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3067 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3068 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3069 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3070 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3071 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3074 else if (phase == PHASE_UNMARSHAL)
3076 if (pass == PASS_OUT)
3079 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3080 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3081 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3082 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3083 print_file(file, indent + 1, "__frame->_Handle);\n");
3087 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3088 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3089 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3093 else if (is_user_type(var->type))
3095 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3097 else if (is_string_type(var->attrs, var->type))
3099 if (is_array(type) && !is_conformant_array(type))
3100 print_phase_function(file, indent, "NonConformantString", local_var_prefix,
3101 phase, var, start_offset);
3104 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
3105 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
3106 start_offset - (type->size_is ? 4 : 2));
3108 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3109 phase, var, start_offset);
3112 else if (is_array(type))
3114 unsigned char tc = get_array_type( type );
3115 const char *array_type = "FixedArray";
3117 /* We already have the size_is expression since it's at the
3118 top level, but do checks for multidimensional conformant
3119 arrays. When we handle them, we'll need to extend this
3120 function to return a list, and then we'll actually use
3121 the return value. */
3122 get_size_is_expr(type, var->name);
3124 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3126 array_type = "VaryingArray";
3128 else if (tc == RPC_FC_CARRAY)
3130 array_type = "ConformantArray";
3132 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3134 array_type = (tc == RPC_FC_BOGUS_ARRAY
3136 : "ConformantVaryingArray");
3139 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3140 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3141 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3143 /* these are all unmarshalled by allocating memory */
3144 if (tc == RPC_FC_BOGUS_ARRAY ||
3145 tc == RPC_FC_CVARRAY ||
3146 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3147 (tc == RPC_FC_CARRAY && !in_attr))
3149 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3151 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3155 else if (!is_ptr(var->type) && is_base_type(rtype))
3157 if (phase != PHASE_FREE)
3158 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3160 else if (!is_ptr(var->type))
3165 case RPC_FC_PSTRUCT:
3166 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3168 case RPC_FC_CSTRUCT:
3169 case RPC_FC_CPSTRUCT:
3170 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3172 case RPC_FC_CVSTRUCT:
3173 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3175 case RPC_FC_BOGUS_STRUCT:
3176 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3179 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
3184 const type_t *ref = type->ref;
3185 if (type->type == RPC_FC_RP && is_base_type(ref->type))
3187 if (phase != PHASE_FREE)
3188 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3190 else if (type->type == RPC_FC_RP && get_struct_type(ref) == RPC_FC_STRUCT &&
3193 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3194 print_phase_function(file, indent, "SimpleStruct",
3195 local_var_prefix, phase, var,
3196 ref->typestring_offset);
3200 if (ref->type == RPC_FC_IP)
3201 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3203 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3206 fprintf(file, "\n");
3209 void write_remoting_arguments(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3210 enum pass pass, enum remoting_phase phase)
3212 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3214 unsigned int size = get_function_buffer_size( func, pass );
3215 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3218 if (pass == PASS_RETURN)
3222 var.type = get_func_return_type(func);
3223 var.name = xstrdup( "_RetVal" );
3224 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3232 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3233 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3238 size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3240 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3244 size_t get_size_procformatstring_func(const func_t *func)
3249 /* argument list size */
3251 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3252 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3254 /* return value size */
3255 if (is_void(get_func_return_type(func)))
3256 size += 2; /* FC_END and FC_PAD */
3258 size += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
3263 size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3265 const statement_t *stmt;
3269 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3271 const type_t *iface;
3272 if (stmt->type == STMT_LIBRARY)
3274 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3277 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3280 iface = stmt->u.type;
3285 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
3286 if (!is_local(func->def->attrs))
3287 size += get_size_procformatstring_func( func );
3292 size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3294 set_all_tfswrite(FALSE);
3295 return process_tfs(NULL, stmts, pred);
3298 void declare_stub_args( FILE *file, int indent, const func_t *func )
3300 int in_attr, out_attr;
3304 /* declare return value '_RetVal' */
3305 if (!is_void(get_func_return_type(func)))
3307 print_file(file, indent, "");
3308 write_type_decl_left(file, get_func_return_type(func));
3309 fprintf(file, " _RetVal;\n");
3315 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3317 int is_string = is_attr(var->attrs, ATTR_STRING);
3319 in_attr = is_attr(var->attrs, ATTR_IN);
3320 out_attr = is_attr(var->attrs, ATTR_OUT);
3321 if (!out_attr && !in_attr)
3324 if (is_context_handle(var->type))
3325 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3328 if (!in_attr && !var->type->size_is && !is_string)
3330 print_file(file, indent, "");
3331 write_type_decl(file, var->type->declarray ? var->type : var->type->ref,
3333 fprintf(file, ";\n");
3336 print_file(file, indent, "");
3337 write_type_decl_left(file, var->type);
3339 if (var->type->declarray) {
3340 fprintf(file, "(*%s)", var->name);
3342 fprintf(file, "%s", var->name);
3343 write_type_right(file, var->type, FALSE);
3344 fprintf(file, ";\n");
3346 if (decl_indirect(var->type))
3347 print_file(file, indent, "void *_p_%s;\n", var->name);
3353 void assign_stub_out_args( FILE *file, int indent, const func_t *func, const char *local_var_prefix )
3355 int in_attr, out_attr;
3362 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3364 int is_string = is_attr(var->attrs, ATTR_STRING);
3365 in_attr = is_attr(var->attrs, ATTR_IN);
3366 out_attr = is_attr(var->attrs, ATTR_OUT);
3367 if (!out_attr && !in_attr)
3372 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3374 if (is_context_handle(var->type))
3376 fprintf(file, " = NdrContextHandleInitialize(\n");
3377 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3378 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3379 var->type->typestring_offset);
3381 else if (var->type->size_is)
3383 unsigned int size, align = 0;
3384 type_t *type = var->type;
3386 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3387 for ( ; type->size_is ; type = type->ref)
3389 write_expr(file, type->size_is, TRUE, TRUE, NULL, NULL, local_var_prefix);
3390 fprintf(file, " * ");
3392 size = type_memsize(type, &align);
3393 fprintf(file, "%u);\n", size);
3395 else if (!is_string)
3397 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3398 if (is_ptr(var->type) && !last_ptr(var->type))
3399 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3407 fprintf(file, "\n");
3411 int write_expr_eval_routines(FILE *file, const char *iface)
3413 static const char *var_name = "pS";
3414 static const char *var_name_expr = "pS->";
3416 struct expr_eval_routine *eval;
3417 unsigned short callback_offset = 0;
3419 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3421 const char *name = eval->structure->name;
3424 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3425 iface, name, callback_offset);
3426 print_file(file, 0, "{\n");
3427 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3428 name, var_name, name, eval->baseoff);
3429 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3430 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3431 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3432 fprintf(file, ";\n");
3433 print_file(file, 0, "}\n\n");
3439 void write_expr_eval_routine_list(FILE *file, const char *iface)
3441 struct expr_eval_routine *eval;
3442 struct expr_eval_routine *cursor;
3443 unsigned short callback_offset = 0;
3445 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3446 fprintf(file, "{\n");
3448 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3450 const char *name = eval->structure->name;
3451 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3453 list_remove(&eval->entry);
3457 fprintf(file, "};\n\n");
3460 void write_user_quad_list(FILE *file)
3464 if (list_empty(&user_type_list))
3467 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3468 fprintf(file, "{\n");
3469 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3471 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3472 print_file(file, 1, "{\n");
3473 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3474 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3475 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3476 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3477 print_file(file, 1, "}%s\n", sep);
3479 fprintf(file, "};\n\n");
3482 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3484 const struct str_list_entry_t *endpoint;
3487 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3488 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3489 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3491 print_file( f, 1, "{ (const unsigned char *)\"" );
3492 for (p = endpoint->str; *p && *p != ':'; p++)
3494 if (*p == '"' || *p == '\\') fputc( '\\', f );
3497 if (!*p) goto error;
3498 if (p[1] != '[') goto error;
3500 fprintf( f, "\", (const unsigned char *)\"" );
3501 for (p += 2; *p && *p != ']'; p++)
3503 if (*p == '"' || *p == '\\') fputc( '\\', f );
3506 if (*p != ']') goto error;
3507 fprintf( f, "\" },\n" );
3509 print_file( f, 0, "};\n\n" );
3513 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3516 void write_exceptions( FILE *file )
3518 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3519 fprintf( file, "\n");
3520 fprintf( file, "#include \"wine/exception.h\"\n");
3521 fprintf( file, "#undef RpcTryExcept\n");
3522 fprintf( file, "#undef RpcExcept\n");
3523 fprintf( file, "#undef RpcEndExcept\n");
3524 fprintf( file, "#undef RpcTryFinally\n");
3525 fprintf( file, "#undef RpcFinally\n");
3526 fprintf( file, "#undef RpcEndFinally\n");
3527 fprintf( file, "#undef RpcExceptionCode\n");
3528 fprintf( file, "#undef RpcAbnormalTermination\n");
3529 fprintf( file, "\n");
3530 fprintf( file, "struct __exception_frame;\n");
3531 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3532 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3533 fprintf( file, "\n");
3534 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3535 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3536 fprintf( file, " __filter_func filter; \\\n");
3537 fprintf( file, " __finally_func finally; \\\n");
3538 fprintf( file, " sigjmp_buf jmp; \\\n");
3539 fprintf( file, " DWORD code; \\\n");
3540 fprintf( file, " unsigned char abnormal_termination; \\\n");
3541 fprintf( file, " unsigned char filter_level; \\\n");
3542 fprintf( file, " unsigned char finally_level;\n");
3543 fprintf( file, "\n");
3544 fprintf( file, "struct __exception_frame\n{\n");
3545 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3546 fprintf( file, "};\n");
3547 fprintf( file, "\n");
3548 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3549 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3550 fprintf( file, " CONTEXT *context,\n");
3551 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3552 fprintf( file, "{\n");
3553 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3554 fprintf( file, "\n");
3555 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3556 fprintf( file, " {\n" );
3557 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3558 fprintf( file, " {\n" );
3559 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3560 fprintf( file, " exc_frame->finally( exc_frame );\n");
3561 fprintf( file, " }\n" );
3562 fprintf( file, " return ExceptionContinueSearch;\n");
3563 fprintf( file, " }\n" );
3564 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3565 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3566 fprintf( file, " {\n");
3567 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3568 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3569 fprintf( file, " {\n");
3570 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3571 fprintf( file, " exc_frame->finally( exc_frame );\n");
3572 fprintf( file, " __wine_pop_frame( frame );\n");
3573 fprintf( file, " }\n");
3574 fprintf( file, " exc_frame->filter_level = 0;\n");
3575 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3576 fprintf( file, " }\n");
3577 fprintf( file, " return ExceptionContinueSearch;\n");
3578 fprintf( file, "}\n");
3579 fprintf( file, "\n");
3580 fprintf( file, "#define RpcTryExcept \\\n");
3581 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3582 fprintf( file, " { \\\n");
3583 fprintf( file, " if (!__frame->finally_level) \\\n" );
3584 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3585 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3586 fprintf( file, "\n");
3587 fprintf( file, "#define RpcExcept(expr) \\\n");
3588 fprintf( file, " if (!__frame->finally_level) \\\n" );
3589 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3590 fprintf( file, " __frame->filter_level = 0; \\\n" );
3591 fprintf( file, " } \\\n");
3592 fprintf( file, " else \\\n");
3593 fprintf( file, "\n");
3594 fprintf( file, "#define RpcEndExcept\n");
3595 fprintf( file, "\n");
3596 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3597 fprintf( file, "\n");
3598 fprintf( file, "#define RpcTryFinally \\\n");
3599 fprintf( file, " if (!__frame->filter_level) \\\n");
3600 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3601 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3602 fprintf( file, "\n");
3603 fprintf( file, "#define RpcFinally \\\n");
3604 fprintf( file, " if (!__frame->filter_level) \\\n");
3605 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3606 fprintf( file, " __frame->finally_level = 0;\n");
3607 fprintf( file, "\n");
3608 fprintf( file, "#define RpcEndFinally\n");
3609 fprintf( file, "\n");
3610 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3611 fprintf( file, "\n");
3612 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3613 fprintf( file, " do { \\\n");
3614 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3615 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3616 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3617 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3618 fprintf( file, " __frame->filter_level = 0; \\\n");
3619 fprintf( file, " __frame->finally_level = 0; \\\n");
3620 fprintf( file, " } while (0)\n");
3621 fprintf( file, "\n");
3622 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3623 fprintf( file, "\n");
3624 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3625 fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
3626 fprintf( file, "\n");
3627 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");