2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
39 #include "wine/list.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const func_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
57 const type_t *structure;
62 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
63 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
66 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
69 const char *name, unsigned int *typestring_offset);
71 const char *string_of_type(unsigned char type)
75 case RPC_FC_BYTE: return "FC_BYTE";
76 case RPC_FC_CHAR: return "FC_CHAR";
77 case RPC_FC_SMALL: return "FC_SMALL";
78 case RPC_FC_USMALL: return "FC_USMALL";
79 case RPC_FC_WCHAR: return "FC_WCHAR";
80 case RPC_FC_SHORT: return "FC_SHORT";
81 case RPC_FC_USHORT: return "FC_USHORT";
82 case RPC_FC_LONG: return "FC_LONG";
83 case RPC_FC_ULONG: return "FC_ULONG";
84 case RPC_FC_FLOAT: return "FC_FLOAT";
85 case RPC_FC_HYPER: return "FC_HYPER";
86 case RPC_FC_DOUBLE: return "FC_DOUBLE";
87 case RPC_FC_ENUM16: return "FC_ENUM16";
88 case RPC_FC_ENUM32: return "FC_ENUM32";
89 case RPC_FC_IGNORE: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP: return "FC_RP";
92 case RPC_FC_UP: return "FC_UP";
93 case RPC_FC_OP: return "FC_OP";
94 case RPC_FC_FP: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY: return "FC_CARRAY";
108 case RPC_FC_CVARRAY: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112 case RPC_FC_POINTER: return "FC_POINTER";
113 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING: return "FC_CSTRING";
116 case RPC_FC_WSTRING: return "FC_WSTRING";
118 error("string_of_type: unknown type 0x%02x\n", type);
123 int is_struct(unsigned char type)
130 case RPC_FC_CPSTRUCT:
131 case RPC_FC_CVSTRUCT:
132 case RPC_FC_BOGUS_STRUCT:
139 static int is_non_complex_struct(const type_t *type)
146 case RPC_FC_CPSTRUCT:
147 case RPC_FC_CVSTRUCT:
154 int is_union(unsigned char type)
158 case RPC_FC_ENCAPSULATED_UNION:
159 case RPC_FC_NON_ENCAPSULATED_UNION:
166 static int type_has_pointers(const type_t *type)
168 if (is_user_type(type))
170 else if (is_ptr(type))
172 else if (is_array(type))
173 return type_has_pointers(type->ref);
174 else if (is_struct(type->type))
177 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
179 if (type_has_pointers(field->type))
183 else if (is_union(type->type))
187 if (type->type == RPC_FC_ENCAPSULATED_UNION)
189 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
190 fields = uv->type->fields_or_args;
193 fields = type->fields_or_args;
194 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
196 if (field->type && type_has_pointers(field->type))
204 static int type_has_full_pointer(const type_t *type)
206 if (is_user_type(type))
208 else if (type->type == RPC_FC_FP)
210 else if (is_ptr(type))
212 else if (is_array(type))
213 return type_has_full_pointer(type->ref);
214 else if (is_struct(type->type))
217 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
219 if (type_has_full_pointer(field->type))
223 else if (is_union(type->type))
227 if (type->type == RPC_FC_ENCAPSULATED_UNION)
229 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
230 fields = uv->type->fields_or_args;
233 fields = type->fields_or_args;
234 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
236 if (field->type && type_has_full_pointer(field->type))
244 static unsigned short user_type_offset(const char *name)
247 unsigned short off = 0;
248 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
250 if (strcmp(name, ut->name) == 0)
254 error("user_type_offset: couldn't find type (%s)\n", name);
258 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
260 type->typestring_offset = offset;
261 if (file) type->tfswrite = FALSE;
264 static void guard_rec(type_t *type)
266 /* types that contain references to themselves (like a linked list),
267 need to be shielded from infinite recursion when writing embedded
269 if (type->typestring_offset)
270 type->tfswrite = FALSE;
272 type->typestring_offset = 1;
275 static type_t *get_user_type(const type_t *t, const char **pname)
279 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
287 if (t->kind == TKIND_ALIAS)
294 int is_user_type(const type_t *t)
296 return get_user_type(t, NULL) != NULL;
299 static int is_embedded_complex(const type_t *type)
301 unsigned char tc = type->type;
302 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
303 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
306 static const char *get_context_handle_type_name(const type_t *type)
309 for (t = type; is_ptr(t); t = t->ref)
310 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
316 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
319 fprintf(file, "/* %2u */\n", typestring_offset); \
320 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
324 static void print_file(FILE *file, int indent, const char *format, ...)
327 va_start(va, format);
328 print(file, indent, format, va);
332 void print(FILE *file, int indent, const char *format, va_list va)
336 if (format[0] != '\n')
339 vfprintf(file, format, va);
344 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
346 if (decl_indirect(t))
348 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
349 local_var_prefix, n, local_var_prefix, n);
350 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
352 else if (is_ptr(t) || is_array(t))
353 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
356 void write_parameters_init(FILE *file, int indent, const func_t *func, const char *local_var_prefix)
360 if (!is_void(get_func_return_type(func)))
361 write_var_init(file, indent, get_func_return_type(func), "_RetVal", local_var_prefix);
366 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
367 write_var_init(file, indent, var->type, var->name, local_var_prefix);
372 static void write_formatdesc(FILE *f, int indent, const char *str)
374 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
375 print_file(f, indent, "{\n");
376 print_file(f, indent + 1, "short Pad;\n");
377 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
378 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
379 print_file(f, indent, "\n");
382 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
384 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
385 get_size_typeformatstring(stmts, pred));
387 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
388 get_size_procformatstring(stmts, pred));
391 write_formatdesc(f, indent, "TYPE");
392 write_formatdesc(f, indent, "PROC");
394 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
395 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
396 print_file(f, indent, "\n");
399 static inline int is_base_type(unsigned char type)
418 case RPC_FC_ERROR_STATUS_T:
419 case RPC_FC_BIND_PRIMITIVE:
427 int decl_indirect(const type_t *t)
429 return is_user_type(t)
430 || (!is_base_type(t->type)
435 static size_t write_procformatstring_type(FILE *file, int indent,
438 const attr_list_t *attrs,
443 int is_in = is_attr(attrs, ATTR_IN);
444 int is_out = is_attr(attrs, ATTR_OUT);
446 if (!is_in && !is_out) is_in = TRUE;
448 if (!type->declarray && is_base_type(type->type))
451 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
453 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
455 if (type->type == RPC_FC_BIND_PRIMITIVE)
457 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
458 size = 2; /* includes param type prefix */
460 else if (is_base_type(type->type))
462 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
463 size = 2; /* includes param type prefix */
467 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
474 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
475 else if (is_in && is_out)
476 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
478 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
480 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
482 print_file(file, indent, "0x01,\n");
483 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
484 size = 4; /* includes param type prefix */
489 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
491 const statement_t *stmt;
492 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
494 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
497 if (!pred(stmt->u.type))
499 if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
501 if (is_local(func->def->attrs)) continue;
502 /* emit argument data */
506 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
507 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
510 /* emit return value data */
511 if (is_void(get_func_return_type(func)))
513 print_file(file, indent, "0x5b, /* FC_END */\n");
514 print_file(file, indent, "0x5c, /* FC_PAD */\n");
517 write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
520 else if (stmt->type == STMT_LIBRARY)
521 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
525 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
529 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
530 print_file(file, indent, "{\n");
532 print_file(file, indent, "0,\n");
533 print_file(file, indent, "{\n");
536 write_procformatstring_stmts(file, indent, stmts, pred);
538 print_file(file, indent, "0x0\n");
540 print_file(file, indent, "}\n");
542 print_file(file, indent, "};\n");
543 print_file(file, indent, "\n");
546 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
548 if (is_base_type(type->type))
550 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
551 *typestring_offset += 1;
558 /* write conformance / variance descriptor */
559 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
560 unsigned int baseoff, const type_t *type,
563 unsigned char operator_type = 0;
564 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
565 const char *conftype_string = "";
566 const char *operator_string = "no operators";
567 const expr_t *subexpr;
571 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
577 /* Top-level conformance calculations are done inline. */
578 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
579 RPC_FC_TOP_LEVEL_CONFORMANCE);
580 print_file (file, 2, "0x0,\n");
581 print_file (file, 2, "NdrFcShort(0x0),\n");
587 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
588 error("write_conf_or_var_desc: constant value %ld is greater than "
589 "the maximum constant size of %d\n", expr->cval,
590 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
592 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
593 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
594 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
595 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
600 if (is_ptr(type) || (is_array(type) && !type->declarray))
602 conftype = RPC_FC_POINTER_CONFORMANCE;
603 conftype_string = "field pointer, ";
607 switch (subexpr->type)
610 subexpr = subexpr->ref;
611 operator_type = RPC_FC_DEREFERENCE;
612 operator_string = "FC_DEREFERENCE";
615 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
617 subexpr = subexpr->ref;
618 operator_type = RPC_FC_DIV_2;
619 operator_string = "FC_DIV_2";
623 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
625 subexpr = subexpr->ref;
626 operator_type = RPC_FC_MULT_2;
627 operator_string = "FC_MULT_2";
631 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
633 subexpr = subexpr->ref;
634 operator_type = RPC_FC_SUB_1;
635 operator_string = "FC_SUB_1";
639 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
641 subexpr = subexpr->ref;
642 operator_type = RPC_FC_ADD_1;
643 operator_string = "FC_ADD_1";
650 if (subexpr->type == EXPR_IDENTIFIER)
652 const type_t *correlation_variable = NULL;
653 unsigned char correlation_variable_type;
654 unsigned char param_type = 0;
658 if (structure->fields_or_args) LIST_FOR_EACH_ENTRY( var, structure->fields_or_args, const var_t, entry )
660 unsigned int align = 0;
661 /* FIXME: take alignment into account */
662 if (var->name && !strcmp(var->name, subexpr->u.sval))
664 correlation_variable = var->type;
667 offset += type_memsize(var->type, &align);
669 if (!correlation_variable)
670 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
673 correlation_variable = expr_resolve_type(NULL, structure, expr);
676 correlation_variable_type = correlation_variable->type;
678 switch (correlation_variable_type)
682 param_type = RPC_FC_SMALL;
686 param_type = RPC_FC_USMALL;
691 param_type = RPC_FC_SHORT;
694 param_type = RPC_FC_USHORT;
698 param_type = RPC_FC_LONG;
701 param_type = RPC_FC_ULONG;
704 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
705 correlation_variable_type);
708 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
709 conftype | param_type, conftype_string, string_of_type(param_type));
710 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
711 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
716 unsigned int callback_offset = 0;
717 struct expr_eval_routine *eval;
720 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
722 if (!strcmp (eval->structure->name, structure->name)
723 && !compare_expr (eval->expr, expr))
733 eval = xmalloc (sizeof(*eval));
734 eval->structure = structure;
735 eval->baseoff = baseoff;
737 list_add_tail (&expr_eval_routines, &eval->entry);
740 if (callback_offset > USHRT_MAX)
741 error("Maximum number of callback routines reached\n");
743 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
744 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
745 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
750 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
752 int have_align = FALSE;
756 if (!fields) return 0;
757 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
759 unsigned int falign = 0;
760 size_t fsize = type_memsize(v->type, &falign);
766 size = ROUND_SIZE(size, falign);
770 size = ROUND_SIZE(size, *align);
774 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
776 size_t size, maxs = 0;
777 unsigned int align = *pmaxa;
780 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
782 /* we could have an empty default field with NULL type */
785 size = type_memsize(v->type, &align);
786 if (maxs < size) maxs = size;
787 if (*pmaxa < align) *pmaxa = align;
794 int get_padding(const var_list_t *fields)
796 unsigned short offset = 0;
803 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
805 type_t *ft = f->type;
806 unsigned int align = 0;
807 size_t size = type_memsize(ft, &align);
810 offset = ROUND_SIZE(offset, align);
814 return ROUNDING(offset, salign);
817 size_t type_memsize(const type_t *t, unsigned int *align)
821 if (t->kind == TKIND_ALIAS)
822 size = type_memsize(t->orig, align);
823 else if (t->declarray && is_conformant_array(t))
825 type_memsize(t->ref, align);
828 else if (is_ptr(t) || is_conformant_array(t))
830 size = sizeof(void *);
831 if (size > *align) *align = size;
833 else switch (t->type)
840 if (size > *align) *align = size;
847 if (size > *align) *align = size;
851 case RPC_FC_ERROR_STATUS_T:
855 if (size > *align) *align = size;
860 if (size > *align) *align = size;
863 case RPC_FC_CVSTRUCT:
864 case RPC_FC_CPSTRUCT:
867 case RPC_FC_BOGUS_STRUCT:
868 size = fields_memsize(t->fields_or_args, align);
870 case RPC_FC_ENCAPSULATED_UNION:
871 case RPC_FC_NON_ENCAPSULATED_UNION:
872 size = union_memsize(t->fields_or_args, align);
874 case RPC_FC_SMFARRAY:
875 case RPC_FC_LGFARRAY:
876 case RPC_FC_SMVARRAY:
877 case RPC_FC_LGVARRAY:
878 case RPC_FC_BOGUS_ARRAY:
879 size = t->dim * type_memsize(t->ref, align);
882 error("type_memsize: Unknown type %d\n", t->type);
889 int is_full_pointer_function(const func_t *func)
892 if (type_has_full_pointer(get_func_return_type(func)))
896 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
897 if (type_has_full_pointer( var->type ))
902 void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
904 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
905 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
909 void write_full_pointer_free(FILE *file, int indent, const func_t *func)
911 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
915 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
917 short absoff = type->ref->typestring_offset;
918 short reloff = absoff - (offset + 2);
919 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
921 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
922 type->type, ptr_attr, string_of_type(type->type));
923 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
924 reloff, reloff, absoff);
928 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
930 unsigned char fc = type->ref->type;
931 /* for historical reasons, write_simple_pointer also handled string types,
932 * but no longer does. catch bad uses of the function with this check */
933 if (is_string_type(type->attrs, type))
934 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
935 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
936 type->type, string_of_type(type->type));
937 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
938 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
942 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
944 print_file(file, 0, "/* %u (", tfsoff);
945 write_type_decl(file, t, NULL);
946 print_file(file, 0, ") */\n");
949 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
951 unsigned int offset = *typestring_offset;
953 print_start_tfs_comment(file, type, offset);
954 update_tfsoff(type, offset, file);
956 if (type->ref->typestring_offset)
957 *typestring_offset += write_nonsimple_pointer(file, type, offset);
958 else if (is_base_type(type->ref->type))
959 *typestring_offset += write_simple_pointer(file, type);
964 static int processed(const type_t *type)
966 return type->typestring_offset && !type->tfswrite;
969 static int user_type_has_variable_size(const type_t *t)
978 case RPC_FC_CPSTRUCT:
979 case RPC_FC_CVSTRUCT:
982 /* Note: Since this only applies to user types, we can't have a conformant
983 array here, and strings should get filed under pointer in this case. */
987 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
989 unsigned int start, absoff, flags;
990 unsigned int align = 0, ualign = 0;
991 const char *name = NULL;
992 type_t *utype = get_user_type(type, &name);
993 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
994 size_t size = type_memsize(type, &align);
995 unsigned short funoff = user_type_offset(name);
1000 if (is_base_type(utype->type))
1003 print_start_tfs_comment(file, utype, absoff);
1004 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1005 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1010 if (!processed(utype))
1011 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1012 absoff = utype->typestring_offset;
1015 if (utype->type == RPC_FC_RP)
1017 else if (utype->type == RPC_FC_UP)
1023 update_tfsoff(type, start, file);
1024 print_start_tfs_comment(file, type, start);
1025 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1026 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1027 flags | (align - 1), align - 1, flags);
1028 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1029 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1030 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1032 reloff = absoff - *tfsoff;
1033 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1037 static void write_member_type(FILE *file, const type_t *cont,
1038 const attr_list_t *attrs, const type_t *type,
1039 unsigned int *corroff, unsigned int *tfsoff)
1041 if (is_embedded_complex(type) && !is_conformant_array(type))
1046 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1053 absoff = type->typestring_offset;
1055 reloff = absoff - (*tfsoff + 2);
1057 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1058 /* FIXME: actually compute necessary padding */
1059 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1060 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1061 reloff, reloff, absoff);
1064 else if (is_ptr(type) || is_conformant_array(type))
1066 unsigned char fc = (cont->type == RPC_FC_BOGUS_STRUCT
1069 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1072 else if (!write_base_type(file, type, tfsoff))
1073 error("Unsupported member type 0x%x\n", type->type);
1076 static void write_end(FILE *file, unsigned int *tfsoff)
1078 if (*tfsoff % 2 == 0)
1080 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1083 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1087 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1089 unsigned int offset = 0;
1090 var_list_t *fs = type->fields_or_args;
1093 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1095 unsigned int align = 0;
1096 type_t *ft = f->type;
1097 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1099 unsigned int absoff = ft->typestring_offset;
1100 short reloff = absoff - (*tfsoff + 6);
1101 print_file(file, 0, "/* %d */\n", *tfsoff);
1102 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1103 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1104 write_conf_or_var_desc(file, current_structure, offset, ft,
1105 get_attrp(f->attrs, ATTR_SWITCHIS));
1106 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1107 reloff, reloff, absoff);
1111 /* FIXME: take alignment into account */
1112 offset += type_memsize(ft, &align);
1116 static int write_no_repeat_pointer_descriptions(
1117 FILE *file, type_t *type,
1118 size_t *offset_in_memory, size_t *offset_in_buffer,
1119 unsigned int *typestring_offset)
1124 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1128 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1129 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1131 /* pointer instance */
1132 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1133 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1134 *typestring_offset += 6;
1138 if (is_string_type(type->attrs, type))
1139 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1141 write_pointer_tfs(file, type, typestring_offset);
1145 unsigned absoff = type->typestring_offset;
1146 short reloff = absoff - (*typestring_offset + 2);
1147 /* FIXME: get pointer attributes from field */
1148 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1149 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1150 reloff, reloff, absoff);
1151 *typestring_offset += 4;
1155 memsize = type_memsize(type, &align);
1156 *offset_in_memory += memsize;
1157 /* increment these separately as in the case of conformant (varying)
1158 * structures these start at different values */
1159 *offset_in_buffer += memsize;
1164 if (is_non_complex_struct(type))
1167 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1169 if (offset_in_memory && offset_in_buffer)
1173 type_memsize(v->type, &align);
1174 padding = ROUNDING(*offset_in_memory, align);
1175 *offset_in_memory += padding;
1176 *offset_in_buffer += padding;
1178 written += write_no_repeat_pointer_descriptions(
1180 offset_in_memory, offset_in_buffer, typestring_offset);
1187 memsize = type_memsize(type, &align);
1188 *offset_in_memory += memsize;
1189 /* increment these separately as in the case of conformant (varying)
1190 * structures these start at different values */
1191 *offset_in_buffer += memsize;
1197 static int write_pointer_description_offsets(
1198 FILE *file, const attr_list_t *attrs, type_t *type,
1199 size_t *offset_in_memory, size_t *offset_in_buffer,
1200 unsigned int *typestring_offset)
1205 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1207 if (offset_in_memory && offset_in_buffer)
1211 /* pointer instance */
1212 /* FIXME: sometimes from end of structure, sometimes from beginning */
1213 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1214 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1217 memsize = type_memsize(type, &align);
1218 *offset_in_memory += memsize;
1219 /* increment these separately as in the case of conformant (varying)
1220 * structures these start at different values */
1221 *offset_in_buffer += memsize;
1223 *typestring_offset += 4;
1225 if (is_string_type(attrs, type))
1226 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1227 else if (processed(type->ref) || is_base_type(type->ref->type))
1228 write_pointer_tfs(file, type, typestring_offset);
1230 error("write_pointer_description_offsets: type format string unknown\n");
1237 return write_pointer_description_offsets(
1238 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1241 else if (is_non_complex_struct(type))
1243 /* otherwise search for interesting fields to parse */
1245 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1247 if (offset_in_memory && offset_in_buffer)
1251 type_memsize(v->type, &align);
1252 padding = ROUNDING(*offset_in_memory, align);
1253 *offset_in_memory += padding;
1254 *offset_in_buffer += padding;
1256 written += write_pointer_description_offsets(
1257 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1263 if (offset_in_memory && offset_in_buffer)
1267 memsize = type_memsize(type, &align);
1268 *offset_in_memory += memsize;
1269 /* increment these separately as in the case of conformant (varying)
1270 * structures these start at different values */
1271 *offset_in_buffer += memsize;
1278 /* Note: if file is NULL return value is number of pointers to write, else
1279 * it is the number of type format characters written */
1280 static int write_fixed_array_pointer_descriptions(
1281 FILE *file, const attr_list_t *attrs, type_t *type,
1282 size_t *offset_in_memory, size_t *offset_in_buffer,
1283 unsigned int *typestring_offset)
1286 int pointer_count = 0;
1288 if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1290 unsigned int temp = 0;
1291 /* unfortunately, this needs to be done in two passes to avoid
1292 * writing out redundant FC_FIXED_REPEAT descriptions */
1293 pointer_count = write_pointer_description_offsets(
1294 NULL, attrs, type->ref, NULL, NULL, &temp);
1295 if (pointer_count > 0)
1297 unsigned int increment_size;
1298 size_t offset_of_array_pointer_mem = 0;
1299 size_t offset_of_array_pointer_buf = 0;
1302 increment_size = type_memsize(type->ref, &align);
1304 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1305 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1306 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1307 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1308 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1309 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1310 *typestring_offset += 10;
1312 pointer_count = write_pointer_description_offsets(
1313 file, attrs, type, &offset_of_array_pointer_mem,
1314 &offset_of_array_pointer_buf, typestring_offset);
1317 else if (is_struct(type->type))
1320 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1322 if (offset_in_memory && offset_in_buffer)
1326 type_memsize(v->type, &align);
1327 padding = ROUNDING(*offset_in_memory, align);
1328 *offset_in_memory += padding;
1329 *offset_in_buffer += padding;
1331 pointer_count += write_fixed_array_pointer_descriptions(
1332 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1338 if (offset_in_memory && offset_in_buffer)
1342 memsize = type_memsize(type, &align);
1343 *offset_in_memory += memsize;
1344 /* increment these separately as in the case of conformant (varying)
1345 * structures these start at different values */
1346 *offset_in_buffer += memsize;
1350 return pointer_count;
1353 /* Note: if file is NULL return value is number of pointers to write, else
1354 * it is the number of type format characters written */
1355 static int write_conformant_array_pointer_descriptions(
1356 FILE *file, const attr_list_t *attrs, type_t *type,
1357 size_t offset_in_memory, unsigned int *typestring_offset)
1360 int pointer_count = 0;
1362 if (is_conformant_array(type) && !type->length_is)
1364 unsigned int temp = 0;
1365 /* unfortunately, this needs to be done in two passes to avoid
1366 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1367 pointer_count = write_pointer_description_offsets(
1368 NULL, attrs, type->ref, NULL, NULL, &temp);
1369 if (pointer_count > 0)
1371 unsigned int increment_size;
1372 size_t offset_of_array_pointer_mem = offset_in_memory;
1373 size_t offset_of_array_pointer_buf = offset_in_memory;
1376 increment_size = type_memsize(type->ref, &align);
1378 if (increment_size > USHRT_MAX)
1379 error("array size of %u bytes is too large\n", increment_size);
1381 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1382 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1383 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1384 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1385 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1386 *typestring_offset += 8;
1388 pointer_count = write_pointer_description_offsets(
1389 file, attrs, type->ref, &offset_of_array_pointer_mem,
1390 &offset_of_array_pointer_buf, typestring_offset);
1394 return pointer_count;
1397 /* Note: if file is NULL return value is number of pointers to write, else
1398 * it is the number of type format characters written */
1399 static int write_varying_array_pointer_descriptions(
1400 FILE *file, const attr_list_t *attrs, type_t *type,
1401 size_t *offset_in_memory, size_t *offset_in_buffer,
1402 unsigned int *typestring_offset)
1405 int pointer_count = 0;
1407 if (is_array(type) && type->length_is)
1409 unsigned int temp = 0;
1410 /* unfortunately, this needs to be done in two passes to avoid
1411 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1412 pointer_count = write_pointer_description_offsets(
1413 NULL, attrs, type->ref, NULL, NULL, &temp);
1414 if (pointer_count > 0)
1416 unsigned int increment_size;
1419 increment_size = type_memsize(type->ref, &align);
1421 if (increment_size > USHRT_MAX)
1422 error("array size of %u bytes is too large\n", increment_size);
1424 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1425 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1426 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1427 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1428 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1429 *typestring_offset += 8;
1431 pointer_count = write_pointer_description_offsets(
1432 file, attrs, type, offset_in_memory,
1433 offset_in_buffer, typestring_offset);
1436 else if (is_struct(type->type))
1439 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1441 if (offset_in_memory && offset_in_buffer)
1445 if (is_array(v->type) && v->type->length_is)
1447 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1448 /* skip over variance and offset in buffer */
1449 *offset_in_buffer += 8;
1453 type_memsize(v->type, &align);
1454 padding = ROUNDING(*offset_in_memory, align);
1455 *offset_in_memory += padding;
1456 *offset_in_buffer += padding;
1458 pointer_count += write_varying_array_pointer_descriptions(
1459 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1465 if (offset_in_memory && offset_in_buffer)
1469 memsize = type_memsize(type, &align);
1470 *offset_in_memory += memsize;
1471 /* increment these separately as in the case of conformant (varying)
1472 * structures these start at different values */
1473 *offset_in_buffer += memsize;
1477 return pointer_count;
1480 static void write_pointer_description(FILE *file, type_t *type,
1481 unsigned int *typestring_offset)
1483 size_t offset_in_buffer;
1484 size_t offset_in_memory;
1486 /* pass 1: search for single instance of a pointer (i.e. don't descend
1488 if (!is_array(type))
1490 offset_in_memory = 0;
1491 offset_in_buffer = 0;
1492 write_no_repeat_pointer_descriptions(
1494 &offset_in_memory, &offset_in_buffer, typestring_offset);
1497 /* pass 2: search for pointers in fixed arrays */
1498 offset_in_memory = 0;
1499 offset_in_buffer = 0;
1500 write_fixed_array_pointer_descriptions(
1502 &offset_in_memory, &offset_in_buffer, typestring_offset);
1504 /* pass 3: search for pointers in conformant only arrays (but don't descend
1505 * into conformant varying or varying arrays) */
1506 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1507 write_conformant_array_pointer_descriptions(
1508 file, NULL, type, 0, typestring_offset);
1509 else if (type->type == RPC_FC_CPSTRUCT)
1511 unsigned int align = 0;
1512 type_t *carray = find_array_or_string_in_struct(type)->type;
1513 write_conformant_array_pointer_descriptions(
1515 type_memsize(type, &align),
1519 /* pass 4: search for pointers in varying arrays */
1520 offset_in_memory = 0;
1521 offset_in_buffer = 0;
1522 write_varying_array_pointer_descriptions(
1524 &offset_in_memory, &offset_in_buffer, typestring_offset);
1527 int is_declptr(const type_t *t)
1529 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1532 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1534 const char *name, unsigned int *typestring_offset)
1536 size_t start_offset;
1537 unsigned char rtype;
1539 if (is_declptr(type))
1541 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1542 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1544 pointer_type = RPC_FC_RP;
1545 print_start_tfs_comment(file, type, *typestring_offset);
1546 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1547 pointer_type, flag, string_of_type(pointer_type),
1548 flag ? " [simple_pointer]" : "");
1549 *typestring_offset += 2;
1552 print_file(file, 2, "NdrFcShort(0x2),\n");
1553 *typestring_offset += 2;
1557 start_offset = *typestring_offset;
1558 update_tfsoff(type, start_offset, file);
1560 rtype = type->ref->type;
1562 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1564 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1565 return start_offset;
1568 if (type->declarray && !is_conformant_array(type))
1570 /* FIXME: multi-dimensional array */
1571 if (0xffffuL < type->dim)
1572 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1573 name, 0xffffu, type->dim - 0xffffu);
1575 if (rtype == RPC_FC_CHAR)
1576 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1578 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1579 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1580 *typestring_offset += 2;
1582 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1583 *typestring_offset += 2;
1585 return start_offset;
1587 else if (type->size_is)
1589 unsigned int align = 0;
1591 if (rtype == RPC_FC_CHAR)
1592 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1594 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1595 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1596 *typestring_offset += 2;
1598 *typestring_offset += write_conf_or_var_desc(
1599 file, current_structure,
1600 (type->declarray && current_structure
1601 ? type_memsize(current_structure, &align)
1603 type, type->size_is);
1605 return start_offset;
1609 if (rtype == RPC_FC_WCHAR)
1610 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1612 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1613 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1614 *typestring_offset += 2;
1616 return start_offset;
1620 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1621 const char *name, unsigned int *typestring_offset)
1623 const expr_t *length_is = type->length_is;
1624 const expr_t *size_is = type->size_is;
1625 unsigned int align = 0;
1627 size_t start_offset;
1629 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1630 unsigned int baseoff
1631 = type->declarray && current_structure
1632 ? type_memsize(current_structure, &align)
1636 pointer_type = RPC_FC_RP;
1638 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1641 has_pointer = type_has_pointers(type->ref);
1644 size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1646 start_offset = *typestring_offset;
1647 update_tfsoff(type, start_offset, file);
1648 print_start_tfs_comment(file, type, start_offset);
1649 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1650 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1651 *typestring_offset += 2;
1654 if (type->type != RPC_FC_BOGUS_ARRAY)
1656 unsigned char tc = type->type;
1658 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1660 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1661 *typestring_offset += 4;
1665 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1666 *typestring_offset += 2;
1669 if (is_conformant_array(type))
1671 += write_conf_or_var_desc(file, current_structure, baseoff,
1674 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1676 unsigned int elalign = 0;
1677 size_t elsize = type_memsize(type->ref, &elalign);
1679 if (type->type == RPC_FC_LGVARRAY)
1681 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1682 *typestring_offset += 4;
1686 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1687 *typestring_offset += 2;
1690 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1691 *typestring_offset += 2;
1696 += write_conf_or_var_desc(file, current_structure, baseoff,
1699 if (has_pointer && (!type->declarray || !current_structure))
1701 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1702 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1703 *typestring_offset += 2;
1704 write_pointer_description(file, type, typestring_offset);
1705 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1706 *typestring_offset += 1;
1709 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1710 write_end(file, typestring_offset);
1714 unsigned int dim = size_is ? 0 : type->dim;
1715 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1716 *typestring_offset += 2;
1718 += write_conf_or_var_desc(file, current_structure, baseoff,
1721 += write_conf_or_var_desc(file, current_structure, baseoff,
1723 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1724 write_end(file, typestring_offset);
1727 return start_offset;
1730 static const var_t *find_array_or_string_in_struct(const type_t *type)
1732 const var_t *last_field;
1735 if (!type->fields_or_args || list_empty(type->fields_or_args))
1738 last_field = LIST_ENTRY( list_tail(type->fields_or_args), const var_t, entry );
1739 ft = last_field->type;
1741 if (ft->declarray && is_conformant_array(ft))
1744 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1745 return find_array_or_string_in_struct(ft);
1750 static void write_struct_members(FILE *file, const type_t *type,
1751 unsigned int *corroff, unsigned int *typestring_offset)
1754 unsigned short offset = 0;
1758 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
1760 type_t *ft = field->type;
1761 if (!ft->declarray || !is_conformant_array(ft))
1763 unsigned int align = 0;
1764 size_t size = type_memsize(ft, &align);
1767 if ((align - 1) & offset)
1769 unsigned char fc = 0;
1773 fc = RPC_FC_ALIGNM4;
1776 fc = RPC_FC_ALIGNM8;
1779 error("write_struct_members: cannot align type %d\n", ft->type);
1781 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1782 offset = ROUND_SIZE(offset, align);
1783 *typestring_offset += 1;
1785 write_member_type(file, type, field->attrs, field->type, corroff,
1791 padding = ROUNDING(offset, salign);
1794 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1795 RPC_FC_STRUCTPAD1 + padding - 1,
1797 *typestring_offset += 1;
1800 write_end(file, typestring_offset);
1803 static size_t write_struct_tfs(FILE *file, type_t *type,
1804 const char *name, unsigned int *tfsoff)
1806 const type_t *save_current_structure = current_structure;
1807 unsigned int total_size;
1809 size_t start_offset;
1810 size_t array_offset;
1811 int has_pointers = 0;
1812 unsigned int align = 0;
1813 unsigned int corroff;
1817 current_structure = type;
1819 total_size = type_memsize(type, &align);
1820 if (total_size > USHRT_MAX)
1821 error("structure size for %s exceeds %d bytes by %d bytes\n",
1822 name, USHRT_MAX, total_size - USHRT_MAX);
1824 if (type->fields_or_args) LIST_FOR_EACH_ENTRY(f, type->fields_or_args, var_t, entry)
1825 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1827 if (!has_pointers) has_pointers = type_has_pointers(type);
1829 array = find_array_or_string_in_struct(type);
1830 if (array && !processed(array->type))
1832 = is_attr(array->attrs, ATTR_STRING)
1833 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1834 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1837 write_descriptors(file, type, tfsoff);
1839 start_offset = *tfsoff;
1840 update_tfsoff(type, start_offset, file);
1841 print_start_tfs_comment(file, type, start_offset);
1842 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1843 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1844 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1849 unsigned int absoff = array->type->typestring_offset;
1850 short reloff = absoff - *tfsoff;
1851 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1852 reloff, reloff, absoff);
1855 else if (type->type == RPC_FC_BOGUS_STRUCT)
1857 print_file(file, 2, "NdrFcShort(0x0),\n");
1861 if (type->type == RPC_FC_BOGUS_STRUCT)
1863 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1864 nothing is written to file yet. On the actual writing pass,
1865 this will have been updated. */
1866 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
1867 int reloff = absoff - *tfsoff;
1868 assert( reloff >= 0 );
1869 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%u) */\n",
1870 reloff, reloff, absoff);
1873 else if ((type->type == RPC_FC_PSTRUCT) ||
1874 (type->type == RPC_FC_CPSTRUCT) ||
1875 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1877 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1878 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1880 write_pointer_description(file, type, tfsoff);
1881 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1885 write_struct_members(file, type, &corroff, tfsoff);
1887 if (type->type == RPC_FC_BOGUS_STRUCT)
1889 const var_list_t *fs = type->fields_or_args;
1892 type->ptrdesc = *tfsoff;
1893 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
1895 type_t *ft = f->type;
1898 if (is_string_type(f->attrs, ft))
1899 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
1901 write_pointer_tfs(file, ft, tfsoff);
1903 else if (!ft->declarray && is_conformant_array(ft))
1905 unsigned int absoff = ft->typestring_offset;
1906 short reloff = absoff - (*tfsoff + 2);
1907 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
1908 /* FIXME: We need to store pointer attributes for arrays
1909 so we don't lose pointer_default info. */
1911 ptr_type = RPC_FC_UP;
1912 print_file(file, 0, "/* %d */\n", *tfsoff);
1913 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
1914 string_of_type(ptr_type));
1915 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1916 reloff, reloff, absoff);
1920 if (type->ptrdesc == *tfsoff)
1924 current_structure = save_current_structure;
1925 return start_offset;
1928 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1929 unsigned char flags, size_t offset,
1930 unsigned int *typeformat_offset)
1932 size_t start_offset = *typeformat_offset;
1933 short reloff = offset - (*typeformat_offset + 2);
1934 int in_attr, out_attr;
1935 in_attr = is_attr(attrs, ATTR_IN);
1936 out_attr = is_attr(attrs, ATTR_OUT);
1937 if (!in_attr && !out_attr) in_attr = 1;
1939 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1942 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1945 string_of_type(pointer_type));
1949 fprintf(file, " [allocated_on_stack]");
1951 fprintf(file, " [pointer_deref]");
1952 fprintf(file, " */\n");
1955 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1956 *typeformat_offset += 4;
1958 return start_offset;
1961 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1965 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1967 else if (is_base_type(t->type))
1969 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1970 t->type, string_of_type(t->type));
1972 else if (t->typestring_offset)
1974 short reloff = t->typestring_offset - *tfsoff;
1975 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1976 reloff, reloff, t->typestring_offset);
1979 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1984 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1986 unsigned int align = 0;
1987 unsigned int start_offset;
1988 size_t size = type_memsize(type, &align);
1991 type_t *deftype = NULL;
1992 short nodeftype = 0xffff;
1997 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1999 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
2000 fields = uv->type->fields_or_args;
2003 fields = type->fields_or_args;
2005 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2007 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2009 nbranch += list_count(cases);
2011 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2014 start_offset = *tfsoff;
2015 update_tfsoff(type, start_offset, file);
2016 print_start_tfs_comment(file, type, start_offset);
2017 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2019 const var_t *sv = LIST_ENTRY(list_head(type->fields_or_args), const var_t, entry);
2020 const type_t *st = sv->type;
2033 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2034 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2035 0x40 | st->type, string_of_type(st->type));
2039 error("union switch type must be an integer, char, or enum\n");
2042 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2044 static const expr_t dummy_expr; /* FIXME */
2045 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2058 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2059 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2060 st->type, string_of_type(st->type));
2064 error("union switch type must be an integer, char, or enum\n");
2067 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2070 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
2071 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
2074 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2076 type_t *ft = f->type;
2077 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2078 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2081 if (cases == NULL && !deflt)
2082 error("union field %s with neither case nor default attribute\n", f->name);
2084 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2086 /* MIDL doesn't check for duplicate cases, even though that seems
2087 like a reasonable thing to do, it just dumps them to the TFS
2088 like we're going to do here. */
2089 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2091 write_branch_type(file, ft, tfsoff);
2094 /* MIDL allows multiple default branches, even though that seems
2095 illogical, it just chooses the last one, which is what we will
2106 write_branch_type(file, deftype, tfsoff);
2110 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2114 return start_offset;
2117 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2118 unsigned int *typeformat_offset)
2121 size_t start_offset = *typeformat_offset;
2122 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2126 print_file(file, 2, "0x2f, /* FC_IP */\n");
2127 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2129 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2133 const type_t *base = is_ptr(type) ? type->ref : type;
2134 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2137 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2139 update_tfsoff(type, start_offset, file);
2140 print_start_tfs_comment(file, type, start_offset);
2141 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2142 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2143 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2144 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2145 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2146 for (i = 0; i < 8; ++i)
2147 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2150 fprintf(file, "\n");
2152 *typeformat_offset += 18;
2154 return start_offset;
2157 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2159 unsigned int *typeformat_offset)
2161 size_t start_offset = *typeformat_offset;
2162 unsigned char flags = 0;
2164 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2165 flags |= NDR_STRICT_CONTEXT_HANDLE;
2169 if (is_attr(var->attrs, ATTR_IN))
2172 if (!is_attr(var->attrs, ATTR_OUT))
2173 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2175 if (is_attr(var->attrs, ATTR_OUT))
2178 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2179 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2180 /* return and can't be null values overlap */
2181 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2182 print_file(file, 0, "can't be null, ");
2183 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2184 print_file(file, 0, "serialize, ");
2185 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2186 print_file(file, 0, "no serialize, ");
2187 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2188 print_file(file, 0, "strict, ");
2189 if ((flags & 0x21) == 0x20)
2190 print_file(file, 0, "out, ");
2191 if ((flags & 0x21) == 0x21)
2192 print_file(file, 0, "return, ");
2194 print_file(file, 0, "in, ");
2196 print_file(file, 0, "via ptr, ");
2197 print_file(file, 0, "*/\n");
2198 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2199 print_file(file, 2, "0, /* FIXME: param num */\n");
2200 *typeformat_offset += 4;
2202 return start_offset;
2205 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2206 type_t *type, const var_t *var,
2207 unsigned int *typeformat_offset)
2211 if (is_context_handle(type))
2212 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2214 if (is_user_type(type))
2216 write_user_tfs(file, type, typeformat_offset);
2217 return type->typestring_offset;
2220 if (is_string_type(var->attrs, type))
2221 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2227 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2228 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2229 /* Top level pointers to conformant arrays may be handled specially
2230 since we can bypass the pointer, but if the array is buried
2231 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2232 always need to write the pointer. */
2233 if (!ptr_type && var->type != type)
2234 /* FIXME: This should use pointer_default, but the information
2235 isn't kept around for arrays. */
2236 ptr_type = RPC_FC_UP;
2237 if (ptr_type && ptr_type != RPC_FC_RP)
2239 unsigned int absoff = type->typestring_offset;
2240 short reloff = absoff - (*typeformat_offset + 2);
2241 off = *typeformat_offset;
2242 print_file(file, 0, "/* %d */\n", off);
2243 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2244 string_of_type(ptr_type));
2245 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2246 reloff, reloff, absoff);
2247 *typeformat_offset += 4;
2254 /* basic types don't need a type format string */
2255 if (is_base_type(type->type))
2258 if (processed(type)) return type->typestring_offset;
2263 case RPC_FC_PSTRUCT:
2264 case RPC_FC_CSTRUCT:
2265 case RPC_FC_CPSTRUCT:
2266 case RPC_FC_CVSTRUCT:
2267 case RPC_FC_BOGUS_STRUCT:
2268 return write_struct_tfs(file, type, var->name, typeformat_offset);
2269 case RPC_FC_ENCAPSULATED_UNION:
2270 case RPC_FC_NON_ENCAPSULATED_UNION:
2271 return write_union_tfs(file, type, typeformat_offset);
2273 case RPC_FC_BIND_PRIMITIVE:
2277 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2280 else if (last_ptr(type))
2282 size_t start_offset = *typeformat_offset;
2283 int in_attr = is_attr(var->attrs, ATTR_IN);
2284 int out_attr = is_attr(var->attrs, ATTR_OUT);
2285 const type_t *base = type->ref;
2287 if (base->type == RPC_FC_IP
2289 && is_attr(var->attrs, ATTR_IIDIS)))
2291 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2294 /* special case for pointers to base types */
2295 if (is_base_type(base->type))
2297 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2298 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2299 string_of_type(type->type),
2300 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2301 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2302 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2303 *typeformat_offset += 4;
2304 return start_offset;
2308 assert(is_ptr(type));
2310 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2312 fprintf(file, "/* %2u */\n", *typeformat_offset);
2313 return write_pointer_only_tfs(file, var->attrs, type->type,
2314 !last_ptr(type) ? 0x10 : 0,
2315 offset, typeformat_offset);
2318 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2319 const char *name, int write_ptr, unsigned int *tfsoff)
2323 if (is_user_type(type))
2325 write_user_tfs(file, type, tfsoff);
2327 else if (is_string_type(attrs, type))
2329 write_string_tfs(file, attrs, type, name, tfsoff);
2331 else if (is_ptr(type))
2333 type_t *ref = type->ref;
2335 if (ref->type == RPC_FC_IP
2337 && is_attr(attrs, ATTR_IIDIS)))
2339 write_ip_tfs(file, attrs, type, tfsoff);
2343 if (!processed(ref) && !is_base_type(ref->type))
2344 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2347 write_pointer_tfs(file, type, tfsoff);
2352 else if (type->declarray && is_conformant_array(type))
2353 ; /* conformant arrays and strings are handled specially */
2354 else if (is_array(type))
2356 write_array_tfs(file, attrs, type, name, tfsoff);
2357 if (is_conformant_array(type))
2360 else if (is_struct(type->type))
2362 if (!processed(type))
2363 write_struct_tfs(file, type, name, tfsoff);
2365 else if (is_union(type->type))
2367 if (!processed(type))
2368 write_union_tfs(file, type, tfsoff);
2370 else if (!is_base_type(type->type))
2371 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2377 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2378 type_pred_t pred, unsigned int *typeformat_offset)
2381 const statement_t *stmt;
2383 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2385 const type_t *iface;
2386 if (stmt->type == STMT_LIBRARY)
2388 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2391 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2394 iface = stmt->u.type;
2401 current_iface = iface;
2402 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2404 if (is_local(func->def->attrs)) continue;
2406 if (!is_void(get_func_return_type(func)))
2408 var_t v = *func->def;
2409 v.type = get_func_return_type(func);
2410 update_tfsoff(get_func_return_type(func),
2411 write_typeformatstring_var(
2412 file, 2, NULL, get_func_return_type(func),
2413 &v, typeformat_offset),
2417 current_func = func;
2419 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2422 write_typeformatstring_var(
2423 file, 2, func, var->type, var,
2430 return *typeformat_offset + 1;
2433 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2435 unsigned int typeformat_offset = 2;
2437 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2441 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2445 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2446 print_file(file, indent, "{\n");
2448 print_file(file, indent, "0,\n");
2449 print_file(file, indent, "{\n");
2451 print_file(file, indent, "NdrFcShort(0x0),\n");
2453 set_all_tfswrite(TRUE);
2454 process_tfs(file, stmts, pred);
2456 print_file(file, indent, "0x0\n");
2458 print_file(file, indent, "}\n");
2460 print_file(file, indent, "};\n");
2461 print_file(file, indent, "\n");
2464 static unsigned int get_required_buffer_size_type(
2465 const type_t *type, const char *name, unsigned int *alignment)
2468 const type_t *utype;
2471 if ((utype = get_user_type(type, &uname)))
2473 return get_required_buffer_size_type(utype, uname, alignment);
2497 case RPC_FC_ERROR_STATUS_T:
2507 case RPC_FC_BIND_PRIMITIVE:
2511 if (!type->fields_or_args) return 0;
2512 return fields_memsize(type->fields_or_args, alignment);
2516 is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
2517 ? get_required_buffer_size_type( type->ref, name, alignment )
2520 case RPC_FC_SMFARRAY:
2521 case RPC_FC_LGFARRAY:
2522 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2530 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2532 int in_attr = is_attr(var->attrs, ATTR_IN);
2533 int out_attr = is_attr(var->attrs, ATTR_OUT);
2535 if (!in_attr && !out_attr)
2540 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2541 pass == PASS_RETURN)
2543 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2549 if (!is_string_type(var->attrs, var->type))
2550 return get_required_buffer_size_type(var->type, var->name,
2556 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2559 unsigned int total_size = 0, alignment;
2563 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2565 total_size += get_required_buffer_size(var, &alignment, pass);
2566 total_size += alignment;
2570 if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2572 var_t v = *func->def;
2573 v.type = get_func_return_type(func);
2574 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2575 total_size += alignment;
2580 static void print_phase_function(FILE *file, int indent, const char *type,
2581 const char *local_var_prefix, enum remoting_phase phase,
2582 const var_t *var, unsigned int type_offset)
2584 const char *function;
2587 case PHASE_BUFFERSIZE:
2588 function = "BufferSize";
2591 function = "Marshall";
2593 case PHASE_UNMARSHAL:
2594 function = "Unmarshall";
2604 print_file(file, indent, "Ndr%s%s(\n", type, function);
2606 print_file(file, indent, "&__frame->_StubMsg,\n");
2607 print_file(file, indent, "%s%s%s%s%s,\n",
2608 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2609 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2611 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2613 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2614 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2615 if (phase == PHASE_UNMARSHAL)
2616 print_file(file, indent, "0);\n");
2620 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2621 enum remoting_phase phase, enum pass pass, const var_t *var,
2622 const char *varname)
2624 type_t *type = var->type;
2626 unsigned int alignment = 0;
2627 unsigned char rtype;
2629 /* no work to do for other phases, buffer sizing is done elsewhere */
2630 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2633 rtype = is_ptr(type) ? type->ref->type : type->type;
2657 case RPC_FC_ERROR_STATUS_T:
2669 case RPC_FC_BIND_PRIMITIVE:
2670 /* no marshalling needed */
2674 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2678 if (phase == PHASE_MARSHAL)
2679 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (long)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2680 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((long)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2681 alignment - 1, alignment - 1);
2683 if (phase == PHASE_MARSHAL)
2685 print_file(file, indent, "*(");
2686 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2688 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2690 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2691 fprintf(file, "%s%s", local_var_prefix, varname);
2692 fprintf(file, ";\n");
2694 else if (phase == PHASE_UNMARSHAL)
2696 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2697 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2698 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2699 print_file(file, indent, "{\n");
2700 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2701 print_file(file, indent, "}\n");
2702 if (pass == PASS_IN || pass == PASS_RETURN)
2703 print_file(file, indent, "");
2705 print_file(file, indent, "*");
2706 fprintf(file, "%s%s", local_var_prefix, varname);
2707 if (pass == PASS_IN && is_ptr(type))
2708 fprintf(file, " = (");
2710 fprintf(file, " = *(");
2711 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2712 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2715 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2716 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2717 fprintf(file, ");\n");
2720 /* returns whether the MaxCount, Offset or ActualCount members need to be
2721 * filled in for the specified phase */
2722 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2724 return (phase != PHASE_UNMARSHAL);
2727 expr_t *get_size_is_expr(const type_t *t, const char *name)
2731 for ( ; is_ptr(t) || is_array(t); t = t->ref)
2737 error("%s: multidimensional conformant"
2738 " arrays not supported at the top level\n",
2745 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
2746 enum remoting_phase phase, const var_t *var)
2748 const type_t *type = var->type;
2749 /* get fundamental type for the argument */
2752 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2754 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2756 else if (is_array(type) || is_string_type(var->attrs, type))
2758 if (is_conformance_needed_for_phase(phase))
2762 print_file(file, indent, "__frame->_StubMsg.MaxCount = (unsigned long)");
2763 write_expr(file, type->size_is, 1, 1, NULL, NULL, local_var_prefix);
2764 fprintf(file, ";\n\n");
2766 if (type->length_is)
2768 print_file(file, indent, "__frame->_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2769 print_file(file, indent, "__frame->_StubMsg.ActualCount = (unsigned long)");
2770 write_expr(file, type->length_is, 1, 1, NULL, NULL, local_var_prefix);
2771 fprintf(file, ";\n\n");
2776 else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
2778 if (is_conformance_needed_for_phase(phase))
2780 print_file(file, indent, "__frame->_StubMsg.MaxCount = (unsigned long)");
2781 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
2782 fprintf(file, ";\n\n");
2786 else if (type->type == RPC_FC_IP)
2790 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
2792 print_file( file, indent, "__frame->_StubMsg.MaxCount = (unsigned long) " );
2793 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
2794 fprintf( file, ";\n\n" );
2798 else if (is_ptr(type))
2805 static void write_remoting_arg(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
2806 enum pass pass, enum remoting_phase phase, const var_t *var)
2808 int in_attr, out_attr, pointer_type;
2809 const type_t *type = var->type;
2810 unsigned char rtype;
2811 size_t start_offset = type->typestring_offset;
2813 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2815 pointer_type = RPC_FC_RP;
2817 in_attr = is_attr(var->attrs, ATTR_IN);
2818 out_attr = is_attr(var->attrs, ATTR_OUT);
2819 if (!in_attr && !out_attr)
2822 if (phase != PHASE_FREE)
2826 if (!in_attr) return;
2829 if (!out_attr) return;
2835 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
2838 if (is_context_handle(type))
2840 if (phase == PHASE_MARSHAL)
2842 if (pass == PASS_IN)
2844 /* if the context_handle attribute appears in the chain of types
2845 * without pointers being followed, then the context handle must
2846 * be direct, otherwise it is a pointer */
2847 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
2848 print_file(file, indent, "NdrClientContextMarshall(\n");
2849 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2850 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
2851 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
2855 print_file(file, indent, "NdrServerContextNewMarshall(\n");
2856 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2857 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
2858 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
2859 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
2862 else if (phase == PHASE_UNMARSHAL)
2864 if (pass == PASS_OUT)
2867 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
2868 print_file(file, indent, "NdrClientContextUnmarshall(\n");
2869 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2870 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
2871 print_file(file, indent + 1, "__frame->_Handle);\n");
2875 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
2876 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2877 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
2881 else if (is_user_type(var->type))
2883 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
2885 else if (is_string_type(var->attrs, var->type))
2887 if (is_array(type) && !is_conformant_array(type))
2888 print_phase_function(file, indent, "NonConformantString", local_var_prefix,
2889 phase, var, start_offset);
2892 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
2893 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
2894 start_offset - (type->size_is ? 4 : 2));
2896 print_phase_function(file, indent, "ConformantString", local_var_prefix,
2897 phase, var, start_offset);
2900 else if (is_array(type))
2902 unsigned char tc = type->type;
2903 const char *array_type = "FixedArray";
2905 /* We already have the size_is expression since it's at the
2906 top level, but do checks for multidimensional conformant
2907 arrays. When we handle them, we'll need to extend this
2908 function to return a list, and then we'll actually use
2909 the return value. */
2910 get_size_is_expr(type, var->name);
2912 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2914 array_type = "VaryingArray";
2916 else if (tc == RPC_FC_CARRAY)
2918 array_type = "ConformantArray";
2920 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
2922 array_type = (tc == RPC_FC_BOGUS_ARRAY
2924 : "ConformantVaryingArray");
2927 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
2928 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
2929 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
2931 /* these are all unmarshalled by allocating memory */
2932 if (type->type == RPC_FC_BOGUS_ARRAY ||
2933 type->type == RPC_FC_CVARRAY ||
2934 ((type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY) && in_attr) ||
2935 (type->type == RPC_FC_CARRAY && !in_attr))
2937 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
2939 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
2943 else if (!is_ptr(var->type) && is_base_type(rtype))
2945 if (phase != PHASE_FREE)
2946 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
2948 else if (!is_ptr(var->type))
2953 case RPC_FC_PSTRUCT:
2954 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
2956 case RPC_FC_CSTRUCT:
2957 case RPC_FC_CPSTRUCT:
2958 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
2960 case RPC_FC_CVSTRUCT:
2961 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
2963 case RPC_FC_BOGUS_STRUCT:
2964 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
2967 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2972 const type_t *ref = type->ref;
2973 if (type->type == RPC_FC_RP && is_base_type(ref->type))
2975 if (phase != PHASE_FREE)
2976 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
2978 else if (type->type == RPC_FC_RP && ref->type == RPC_FC_STRUCT &&
2981 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2982 print_phase_function(file, indent, "SimpleStruct",
2983 local_var_prefix, phase, var,
2984 ref->typestring_offset);
2988 if (ref->type == RPC_FC_IP)
2989 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
2991 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
2994 fprintf(file, "\n");
2997 void write_remoting_arguments(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
2998 enum pass pass, enum remoting_phase phase)
3000 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3002 unsigned int size = get_function_buffer_size( func, pass );
3003 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3006 if (pass == PASS_RETURN)
3010 var.type = get_func_return_type(func);
3011 var.name = xstrdup( "_RetVal" );
3012 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3020 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3021 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3026 size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3028 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3032 size_t get_size_procformatstring_func(const func_t *func)
3037 /* argument list size */
3039 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3040 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3042 /* return value size */
3043 if (is_void(get_func_return_type(func)))
3044 size += 2; /* FC_END and FC_PAD */
3046 size += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
3051 size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3053 const statement_t *stmt;
3057 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3059 const type_t *iface;
3060 if (stmt->type == STMT_LIBRARY)
3062 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3065 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3068 iface = stmt->u.type;
3073 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
3074 if (!is_local(func->def->attrs))
3075 size += get_size_procformatstring_func( func );
3080 size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3082 set_all_tfswrite(FALSE);
3083 return process_tfs(NULL, stmts, pred);
3086 void declare_stub_args( FILE *file, int indent, const func_t *func )
3088 int in_attr, out_attr;
3092 /* declare return value '_RetVal' */
3093 if (!is_void(get_func_return_type(func)))
3095 print_file(file, indent, "");
3096 write_type_decl_left(file, get_func_return_type(func));
3097 fprintf(file, " _RetVal;\n");
3103 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3105 int is_string = is_attr(var->attrs, ATTR_STRING);
3107 in_attr = is_attr(var->attrs, ATTR_IN);
3108 out_attr = is_attr(var->attrs, ATTR_OUT);
3109 if (!out_attr && !in_attr)
3112 if (is_context_handle(var->type))
3113 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3116 if (!in_attr && !var->type->size_is && !is_string)
3118 print_file(file, indent, "");
3119 write_type_decl(file, var->type->declarray ? var->type : var->type->ref,
3121 fprintf(file, ";\n");
3124 print_file(file, indent, "");
3125 write_type_decl_left(file, var->type);
3127 if (var->type->declarray) {
3128 fprintf(file, "(*%s)", var->name);
3130 fprintf(file, "%s", var->name);
3131 write_type_right(file, var->type, FALSE);
3132 fprintf(file, ";\n");
3134 if (decl_indirect(var->type))
3135 print_file(file, indent, "void *_p_%s;\n", var->name);
3141 void assign_stub_out_args( FILE *file, int indent, const func_t *func, const char *local_var_prefix )
3143 int in_attr, out_attr;
3150 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3152 int is_string = is_attr(var->attrs, ATTR_STRING);
3153 in_attr = is_attr(var->attrs, ATTR_IN);
3154 out_attr = is_attr(var->attrs, ATTR_OUT);
3155 if (!out_attr && !in_attr)
3160 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3162 if (is_context_handle(var->type))
3164 fprintf(file, " = NdrContextHandleInitialize(\n");
3165 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3166 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3167 var->type->typestring_offset);
3169 else if (var->type->size_is)
3171 unsigned int size, align = 0;
3172 type_t *type = var->type;
3174 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3175 for ( ; type->size_is ; type = type->ref)
3177 write_expr(file, type->size_is, TRUE, TRUE, NULL, NULL, local_var_prefix);
3178 fprintf(file, " * ");
3180 size = type_memsize(type, &align);
3181 fprintf(file, "%u);\n", size);
3183 else if (!is_string)
3185 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3186 if (is_ptr(var->type) && !last_ptr(var->type))
3187 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3195 fprintf(file, "\n");
3199 int write_expr_eval_routines(FILE *file, const char *iface)
3201 static const char *var_name = "pS";
3202 static const char *var_name_expr = "pS->";
3204 struct expr_eval_routine *eval;
3205 unsigned short callback_offset = 0;
3207 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3209 const char *name = eval->structure->name;
3212 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3213 iface, name, callback_offset);
3214 print_file(file, 0, "{\n");
3215 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3216 name, var_name, name, eval->baseoff);
3217 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3218 print_file(file, 1, "pStubMsg->MaxCount = (unsigned long)");
3219 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3220 fprintf(file, ";\n");
3221 print_file(file, 0, "}\n\n");
3227 void write_expr_eval_routine_list(FILE *file, const char *iface)
3229 struct expr_eval_routine *eval;
3230 struct expr_eval_routine *cursor;
3231 unsigned short callback_offset = 0;
3233 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3234 fprintf(file, "{\n");
3236 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3238 const char *name = eval->structure->name;
3239 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3241 list_remove(&eval->entry);
3245 fprintf(file, "};\n\n");
3248 void write_user_quad_list(FILE *file)
3252 if (list_empty(&user_type_list))
3255 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3256 fprintf(file, "{\n");
3257 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3259 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3260 print_file(file, 1, "{\n");
3261 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3262 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3263 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3264 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3265 print_file(file, 1, "}%s\n", sep);
3267 fprintf(file, "};\n\n");
3270 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3272 const struct str_list_entry_t *endpoint;
3275 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3276 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3277 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3279 print_file( f, 1, "{ (const unsigned char *)\"" );
3280 for (p = endpoint->str; *p && *p != ':'; p++)
3282 if (*p == '"' || *p == '\\') fputc( '\\', f );
3285 if (!*p) goto error;
3286 if (p[1] != '[') goto error;
3288 fprintf( f, "\", (const unsigned char *)\"" );
3289 for (p += 2; *p && *p != ']'; p++)
3291 if (*p == '"' || *p == '\\') fputc( '\\', f );
3294 if (*p != ']') goto error;
3295 fprintf( f, "\" },\n" );
3297 print_file( f, 0, "};\n\n" );
3301 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3304 void write_exceptions( FILE *file )
3306 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3307 fprintf( file, "\n");
3308 fprintf( file, "#include \"wine/exception.h\"\n");
3309 fprintf( file, "#undef RpcTryExcept\n");
3310 fprintf( file, "#undef RpcExcept\n");
3311 fprintf( file, "#undef RpcEndExcept\n");
3312 fprintf( file, "#undef RpcTryFinally\n");
3313 fprintf( file, "#undef RpcFinally\n");
3314 fprintf( file, "#undef RpcEndFinally\n");
3315 fprintf( file, "#undef RpcExceptionCode\n");
3316 fprintf( file, "#undef RpcAbnormalTermination\n");
3317 fprintf( file, "\n");
3318 fprintf( file, "struct __exception_frame;\n");
3319 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3320 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3321 fprintf( file, "\n");
3322 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3323 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3324 fprintf( file, " __filter_func filter; \\\n");
3325 fprintf( file, " __finally_func finally; \\\n");
3326 fprintf( file, " sigjmp_buf jmp; \\\n");
3327 fprintf( file, " DWORD code; \\\n");
3328 fprintf( file, " unsigned char abnormal_termination; \\\n");
3329 fprintf( file, " unsigned char filter_level; \\\n");
3330 fprintf( file, " unsigned char finally_level;\n");
3331 fprintf( file, "\n");
3332 fprintf( file, "struct __exception_frame\n{\n");
3333 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3334 fprintf( file, "};\n");
3335 fprintf( file, "\n");
3336 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3337 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3338 fprintf( file, " CONTEXT *context,\n");
3339 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3340 fprintf( file, "{\n");
3341 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3342 fprintf( file, "\n");
3343 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3344 fprintf( file, " {\n" );
3345 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3346 fprintf( file, " {\n" );
3347 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3348 fprintf( file, " exc_frame->finally( exc_frame );\n");
3349 fprintf( file, " }\n" );
3350 fprintf( file, " return ExceptionContinueSearch;\n");
3351 fprintf( file, " }\n" );
3352 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3353 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3354 fprintf( file, " {\n");
3355 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3356 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3357 fprintf( file, " {\n");
3358 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3359 fprintf( file, " exc_frame->finally( exc_frame );\n");
3360 fprintf( file, " __wine_pop_frame( frame );\n");
3361 fprintf( file, " }\n");
3362 fprintf( file, " exc_frame->filter_level = 0;\n");
3363 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3364 fprintf( file, " }\n");
3365 fprintf( file, " return ExceptionContinueSearch;\n");
3366 fprintf( file, "}\n");
3367 fprintf( file, "\n");
3368 fprintf( file, "#define RpcTryExcept \\\n");
3369 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3370 fprintf( file, " { \\\n");
3371 fprintf( file, " if (!__frame->finally_level) \\\n" );
3372 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3373 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3374 fprintf( file, "\n");
3375 fprintf( file, "#define RpcExcept(expr) \\\n");
3376 fprintf( file, " if (!__frame->finally_level) \\\n" );
3377 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3378 fprintf( file, " __frame->filter_level = 0; \\\n" );
3379 fprintf( file, " } \\\n");
3380 fprintf( file, " else \\\n");
3381 fprintf( file, "\n");
3382 fprintf( file, "#define RpcEndExcept\n");
3383 fprintf( file, "\n");
3384 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3385 fprintf( file, "\n");
3386 fprintf( file, "#define RpcTryFinally \\\n");
3387 fprintf( file, " if (!__frame->filter_level) \\\n");
3388 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3389 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3390 fprintf( file, "\n");
3391 fprintf( file, "#define RpcFinally \\\n");
3392 fprintf( file, " if (!__frame->filter_level) \\\n");
3393 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3394 fprintf( file, " __frame->finally_level = 0;\n");
3395 fprintf( file, "\n");
3396 fprintf( file, "#define RpcEndFinally\n");
3397 fprintf( file, "\n");
3398 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3399 fprintf( file, "\n");
3400 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3401 fprintf( file, " do { \\\n");
3402 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3403 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3404 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3405 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3406 fprintf( file, " __frame->filter_level = 0; \\\n");
3407 fprintf( file, " __frame->finally_level = 0; \\\n");
3408 fprintf( file, " } while (0)\n");
3409 fprintf( file, "\n");
3410 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3411 fprintf( file, "\n");
3412 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3413 fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
3414 fprintf( file, "\n");
3415 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");