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"
41 #include "wine/list.h"
46 static const func_t *current_func;
47 static const type_t *current_structure;
49 /* name of the structure variable for structure callbacks */
50 #define STRUCT_EXPR_EVAL_VAR "pS"
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
57 const type_t *structure;
58 size_t structure_size;
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);
67 const char *string_of_type(unsigned char type)
71 case RPC_FC_BYTE: return "FC_BYTE";
72 case RPC_FC_CHAR: return "FC_CHAR";
73 case RPC_FC_SMALL: return "FC_SMALL";
74 case RPC_FC_USMALL: return "FC_USMALL";
75 case RPC_FC_WCHAR: return "FC_WCHAR";
76 case RPC_FC_SHORT: return "FC_SHORT";
77 case RPC_FC_USHORT: return "FC_USHORT";
78 case RPC_FC_LONG: return "FC_LONG";
79 case RPC_FC_ULONG: return "FC_ULONG";
80 case RPC_FC_FLOAT: return "FC_FLOAT";
81 case RPC_FC_HYPER: return "FC_HYPER";
82 case RPC_FC_DOUBLE: return "FC_DOUBLE";
83 case RPC_FC_ENUM16: return "FC_ENUM16";
84 case RPC_FC_ENUM32: return "FC_ENUM32";
85 case RPC_FC_IGNORE: return "FC_IGNORE";
86 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
87 case RPC_FC_RP: return "FC_RP";
88 case RPC_FC_UP: return "FC_UP";
89 case RPC_FC_OP: return "FC_OP";
90 case RPC_FC_FP: return "FC_FP";
91 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
92 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
93 case RPC_FC_STRUCT: return "FC_STRUCT";
94 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
95 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
96 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
97 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
98 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
99 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
100 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
101 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
102 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
103 case RPC_FC_CARRAY: return "FC_CARRAY";
104 case RPC_FC_CVARRAY: return "FC_CVARRAY";
105 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
107 error("string_of_type: unknown type 0x%02x\n", type);
112 int is_struct(unsigned char type)
119 case RPC_FC_CPSTRUCT:
120 case RPC_FC_CVSTRUCT:
121 case RPC_FC_BOGUS_STRUCT:
128 static int is_non_complex_struct(const type_t *type)
135 case RPC_FC_CPSTRUCT:
136 case RPC_FC_CVSTRUCT:
143 int is_union(unsigned char type)
147 case RPC_FC_ENCAPSULATED_UNION:
148 case RPC_FC_NON_ENCAPSULATED_UNION:
155 static unsigned short user_type_offset(const char *name)
158 unsigned short off = 0;
159 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
161 if (strcmp(name, ut->name) == 0)
165 error("user_type_offset: couldn't find type (%s)\n", name);
169 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
171 type->typestring_offset = offset;
172 if (file) type->tfswrite = FALSE;
175 static void guard_rec(type_t *type)
177 /* types that contain references to themselves (like a linked list),
178 need to be shielded from infinite recursion when writing embedded
180 if (type->typestring_offset)
181 type->tfswrite = FALSE;
183 type->typestring_offset = 1;
186 static type_t *get_user_type(const type_t *t, const char **pname)
190 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
198 if (t->kind == TKIND_ALIAS)
205 static int is_user_type(const type_t *t)
207 return get_user_type(t, NULL) != NULL;
210 static int is_embedded_complex(const type_t *type)
212 unsigned char tc = type->type;
213 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
214 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
217 static int compare_expr(const expr_t *a, const expr_t *b)
221 if (a->type != b->type)
222 return a->type - b->type;
229 return a->u.lval - b->u.lval;
231 return a->u.dval - b->u.dval;
232 case EXPR_IDENTIFIER:
233 return strcmp(a->u.sval, b->u.sval);
235 ret = compare_expr(a->ref, b->ref);
238 ret = compare_expr(a->u.ext, b->u.ext);
241 return compare_expr(a->ext2, b->ext2);
250 ret = compare_expr(a->ref, b->ref);
253 return compare_expr(a->u.ext, b->u.ext);
259 return compare_expr(a->ref, b->ref);
266 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
269 fprintf(file, "/* %2u */\n", typestring_offset); \
270 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
274 static void print_file(FILE *file, int indent, const char *format, ...)
277 va_start(va, format);
278 print(file, indent, format, va);
282 void print(FILE *file, int indent, const char *format, va_list va)
286 if (format[0] != '\n')
289 vfprintf(file, format, va);
293 void write_parameters_init(FILE *file, int indent, const func_t *func)
300 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
302 const type_t *t = var->type;
303 const char *n = var->name;
304 if (decl_indirect(t))
305 print_file(file, indent, "MIDL_memset(&%s, 0, sizeof %s);\n", n, n);
306 else if (is_ptr(t) || is_array(t))
307 print_file(file, indent, "%s = 0;\n", n);
313 static void write_formatdesc(FILE *f, int indent, const char *str)
315 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
316 print_file(f, indent, "{\n");
317 print_file(f, indent + 1, "short Pad;\n");
318 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
319 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
320 print_file(f, indent, "\n");
323 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
325 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
326 get_size_typeformatstring(ifaces, for_objects));
328 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
329 get_size_procformatstring(ifaces, for_objects));
332 write_formatdesc(f, indent, "TYPE");
333 write_formatdesc(f, indent, "PROC");
335 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
336 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
337 print_file(f, indent, "\n");
340 static inline int is_base_type(unsigned char type)
359 case RPC_FC_ERROR_STATUS_T:
360 case RPC_FC_BIND_PRIMITIVE:
368 int decl_indirect(const type_t *t)
370 return is_user_type(t)
371 || (!is_base_type(t->type)
376 static size_t write_procformatstring_var(FILE *file, int indent,
377 const var_t *var, int is_return)
380 const type_t *type = var->type;
382 int is_in = is_attr(var->attrs, ATTR_IN);
383 int is_out = is_attr(var->attrs, ATTR_OUT);
385 if (!is_in && !is_out) is_in = TRUE;
387 if (!type->declarray && is_base_type(type->type))
390 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
392 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
394 if (type->type == RPC_FC_BIND_PRIMITIVE)
396 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
397 size = 2; /* includes param type prefix */
399 else if (is_base_type(type->type))
401 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
402 size = 2; /* includes param type prefix */
406 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
413 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
414 else if (is_in && is_out)
415 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
417 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
419 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
421 print_file(file, indent, "0x01,\n");
422 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
423 size = 4; /* includes param type prefix */
428 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
430 const ifref_t *iface;
434 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
435 print_file(file, indent, "{\n");
437 print_file(file, indent, "0,\n");
438 print_file(file, indent, "{\n");
441 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
443 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
446 if (iface->iface->funcs)
449 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
451 if (is_local(func->def->attrs)) continue;
452 /* emit argument data */
455 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
456 write_procformatstring_var(file, indent, var, FALSE);
459 /* emit return value data */
461 if (is_void(var->type))
463 print_file(file, indent, "0x5b, /* FC_END */\n");
464 print_file(file, indent, "0x5c, /* FC_PAD */\n");
467 write_procformatstring_var(file, indent, var, TRUE);
472 print_file(file, indent, "0x0\n");
474 print_file(file, indent, "}\n");
476 print_file(file, indent, "};\n");
477 print_file(file, indent, "\n");
480 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
482 if (is_base_type(type->type))
484 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
485 *typestring_offset += 1;
492 /* write conformance / variance descriptor */
493 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure,
494 unsigned int baseoff, const expr_t *expr)
496 unsigned char operator_type = 0;
497 const char *operator_string = "no operators";
498 const expr_t *subexpr;
499 unsigned char correlation_type;
501 if (!file) return 4; /* optimisation for sizing pass */
505 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
506 error("write_conf_or_var_desc: constant value %ld is greater than "
507 "the maximum constant size of %d\n", expr->cval,
508 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
510 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
511 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
512 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
513 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
519 switch (subexpr->type)
522 subexpr = subexpr->ref;
523 operator_type = RPC_FC_DEREFERENCE;
524 operator_string = "FC_DEREFERENCE";
527 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
529 subexpr = subexpr->ref;
530 operator_type = RPC_FC_DIV_2;
531 operator_string = "FC_DIV_2";
535 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
537 subexpr = subexpr->ref;
538 operator_type = RPC_FC_MULT_2;
539 operator_string = "FC_MULT_2";
543 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
545 subexpr = subexpr->ref;
546 operator_type = RPC_FC_SUB_1;
547 operator_string = "FC_SUB_1";
551 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
553 subexpr = subexpr->ref;
554 operator_type = RPC_FC_ADD_1;
555 operator_string = "FC_ADD_1";
562 if (subexpr->type == EXPR_IDENTIFIER)
564 const type_t *correlation_variable = NULL;
565 unsigned char correlation_variable_type;
566 unsigned char param_type = 0;
567 const char *param_type_string = NULL;
575 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
577 unsigned int align = 0;
578 /* FIXME: take alignment into account */
579 if (var->name && !strcmp(var->name, subexpr->u.sval))
581 correlation_variable = var->type;
584 offset += type_memsize(var->type, &align);
586 if (!correlation_variable)
587 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
591 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
597 offset = sizeof(void *);
598 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
600 if (!strcmp(var->name, subexpr->u.sval))
602 correlation_variable = var->type;
605 /* FIXME: not all stack variables are sizeof(void *) */
606 offset += sizeof(void *);
608 if (!correlation_variable)
609 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
612 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
615 correlation_variable_type = correlation_variable->type;
617 switch (correlation_variable_type)
621 param_type = RPC_FC_SMALL;
622 param_type_string = "FC_SMALL";
626 param_type = RPC_FC_USMALL;
627 param_type_string = "FC_USMALL";
632 param_type = RPC_FC_SHORT;
633 param_type_string = "FC_SHORT";
636 param_type = RPC_FC_USHORT;
637 param_type_string = "FC_USHORT";
641 param_type = RPC_FC_LONG;
642 param_type_string = "FC_LONG";
645 param_type = RPC_FC_ULONG;
646 param_type_string = "FC_ULONG";
652 if (sizeof(void *) == 4) /* FIXME */
654 param_type = RPC_FC_LONG;
655 param_type_string = "FC_LONG";
659 param_type = RPC_FC_HYPER;
660 param_type_string = "FC_HYPER";
664 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
665 correlation_variable_type);
668 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
669 correlation_type | param_type,
670 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
672 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
673 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
675 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
680 unsigned int callback_offset = 0;
684 struct expr_eval_routine *eval;
687 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
689 if (!strcmp(eval->structure->name, structure->name) &&
690 !compare_expr(eval->expr, expr))
700 unsigned int align = 0;
701 eval = xmalloc(sizeof(*eval));
702 eval->structure = structure;
703 eval->structure_size = fields_memsize(structure->fields, &align);
705 list_add_tail(&expr_eval_routines, &eval->entry);
708 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
712 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
713 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
716 if (callback_offset > USHRT_MAX)
717 error("Maximum number of callback routines reached\n");
719 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
721 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
722 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
723 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
728 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
733 if (!fields) return 0;
734 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
735 size += type_memsize(v->type, align);
740 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
742 size_t size, maxs = 0;
743 unsigned int align = *pmaxa;
746 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
748 /* we could have an empty default field with NULL type */
751 size = type_memsize(v->type, &align);
752 if (maxs < size) maxs = size;
753 if (*pmaxa < align) *pmaxa = align;
760 size_t type_memsize(const type_t *t, unsigned int *align)
764 if (t->declarray && is_conformant_array(t))
766 type_memsize(t->ref, align);
769 else if (is_ptr(t) || is_conformant_array(t))
771 size = sizeof(void *);
772 if (size > *align) *align = size;
774 else switch (t->type)
781 if (size > *align) *align = size;
788 if (size > *align) *align = size;
792 case RPC_FC_ERROR_STATUS_T:
796 if (size > *align) *align = size;
801 if (size > *align) *align = size;
804 case RPC_FC_CVSTRUCT:
805 case RPC_FC_CPSTRUCT:
808 case RPC_FC_BOGUS_STRUCT:
809 size = fields_memsize(t->fields, align);
811 case RPC_FC_ENCAPSULATED_UNION:
812 case RPC_FC_NON_ENCAPSULATED_UNION:
813 size = union_memsize(t->fields, align);
815 case RPC_FC_SMFARRAY:
816 case RPC_FC_LGFARRAY:
817 case RPC_FC_SMVARRAY:
818 case RPC_FC_LGVARRAY:
819 case RPC_FC_BOGUS_ARRAY:
820 size = t->dim * type_memsize(t->ref, align);
823 error("type_memsize: Unknown type %d\n", t->type);
830 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
832 short absoff = type->ref->typestring_offset;
833 short reloff = absoff - (offset + 2);
834 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
836 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
837 type->type, ptr_attr, string_of_type(type->type));
838 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
839 reloff, reloff, absoff);
843 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
845 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
846 type->type, string_of_type(type->type));
847 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
848 string_of_type(type->ref->type));
849 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
853 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
855 unsigned int offset = *typestring_offset;
857 print_file(file, 0, "/* %d */\n", offset);
858 update_tfsoff(type, offset, file);
860 if (type->ref->typestring_offset)
861 *typestring_offset += write_nonsimple_pointer(file, type, offset);
862 else if (is_base_type(type->ref->type))
863 *typestring_offset += write_simple_pointer(file, type);
868 static int processed(const type_t *type)
870 return type->typestring_offset && !type->tfswrite;
873 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
875 unsigned int start, absoff, flags;
876 unsigned int align = 0, ualign = 0;
878 type_t *utype = get_user_type(type, &name);
879 size_t usize = type_memsize(utype, &ualign);
880 size_t size = type_memsize(type, &align);
881 unsigned short funoff = user_type_offset(name);
886 if (is_base_type(utype->type))
889 print_file(file, 0, "/* %d */\n", absoff);
890 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
891 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
896 if (!processed(utype))
897 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
898 absoff = utype->typestring_offset;
901 if (utype->type == RPC_FC_RP)
903 else if (utype->type == RPC_FC_UP)
909 update_tfsoff(type, start, file);
910 print_file(file, 0, "/* %d */\n", start);
911 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
912 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
913 flags | (align - 1), align - 1, flags);
914 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
915 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
916 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
918 reloff = absoff - *tfsoff;
919 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
923 static void write_member_type(FILE *file, type_t *type, const var_t *field,
924 unsigned int *corroff, unsigned int *tfsoff)
926 if (is_embedded_complex(type))
931 if (is_union(type->type) && is_attr(field->attrs, ATTR_SWITCHIS))
938 absoff = type->typestring_offset;
940 reloff = absoff - (*tfsoff + 2);
942 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
943 /* FIXME: actually compute necessary padding */
944 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
945 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
946 reloff, reloff, absoff);
949 else if (is_ptr(type))
951 print_file(file, 2, "0x8,\t/* FC_LONG */\n");
954 else if (!write_base_type(file, type, tfsoff))
955 error("Unsupported member type 0x%x\n", type->type);
958 static void write_end(FILE *file, unsigned int *tfsoff)
960 if (*tfsoff % 2 == 0)
962 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
965 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
969 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
971 unsigned int offset = 0;
972 var_list_t *fs = type->fields;
975 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
977 unsigned int align = 0;
978 type_t *ft = f->type;
979 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
981 unsigned int absoff = ft->typestring_offset;
982 short reloff = absoff - (*tfsoff + 6);
983 print_file(file, 0, "/* %d */\n", *tfsoff);
984 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
985 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
986 write_conf_or_var_desc(file, current_func, current_structure, offset,
987 get_attrp(f->attrs, ATTR_SWITCHIS));
988 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
989 reloff, reloff, absoff);
993 /* FIXME: take alignment into account */
994 offset += type_memsize(ft, &align);
998 static int write_no_repeat_pointer_descriptions(
999 FILE *file, type_t *type,
1000 size_t *offset_in_memory, size_t *offset_in_buffer,
1001 unsigned int *typestring_offset)
1008 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1009 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1011 /* pointer instance */
1012 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1013 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1014 *typestring_offset += 6;
1016 if (processed(type->ref) || is_base_type(type->ref->type))
1017 write_pointer_tfs(file, type, typestring_offset);
1019 error("write_pointer_description: type format string unknown\n");
1022 *offset_in_memory += type_memsize(type, &align);
1023 /* FIXME: is there a case where these two are different? */
1025 *offset_in_buffer += type_memsize(type, &align);
1030 if (is_non_complex_struct(type))
1033 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1034 written += write_no_repeat_pointer_descriptions(
1036 offset_in_memory, offset_in_buffer, typestring_offset);
1041 *offset_in_memory += type_memsize(type, &align);
1042 /* FIXME: is there a case where these two are different? */
1044 *offset_in_buffer += type_memsize(type, &align);
1050 static int write_pointer_description_offsets(
1051 FILE *file, const attr_list_t *attrs, type_t *type,
1052 size_t *offset_in_memory, size_t *offset_in_buffer,
1053 unsigned int *typestring_offset)
1058 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1060 if (offset_in_memory && offset_in_buffer)
1062 /* pointer instance */
1063 /* FIXME: sometimes from end of structure, sometimes from beginning */
1064 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1065 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1068 *offset_in_memory += type_memsize(type, &align);
1069 /* FIXME: is there a case where these two are different? */
1071 *offset_in_buffer += type_memsize(type, &align);
1073 *typestring_offset += 4;
1075 if (processed(type->ref) || is_base_type(type->ref->type))
1076 write_pointer_tfs(file, type, typestring_offset);
1078 error("write_pointer_description_offsets: type format string unknown\n");
1085 return write_pointer_description_offsets(
1086 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1089 else if (is_non_complex_struct(type))
1091 /* otherwise search for interesting fields to parse */
1093 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1095 written += write_pointer_description_offsets(
1096 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1103 if (offset_in_memory)
1104 *offset_in_memory += type_memsize(type, &align);
1105 /* FIXME: is there a case where these two are different? */
1107 if (offset_in_buffer)
1108 *offset_in_buffer += type_memsize(type, &align);
1114 /* Note: if file is NULL return value is number of pointers to write, else
1115 * it is the number of type format characters written */
1116 static int write_fixed_array_pointer_descriptions(
1117 FILE *file, const attr_list_t *attrs, type_t *type,
1118 size_t *offset_in_memory, size_t *offset_in_buffer,
1119 unsigned int *typestring_offset)
1122 int pointer_count = 0;
1124 if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1126 unsigned int temp = 0;
1127 /* unfortunately, this needs to be done in two passes to avoid
1128 * writing out redundant FC_FIXED_REPEAT descriptions */
1129 pointer_count = write_pointer_description_offsets(
1130 NULL, attrs, type->ref, NULL, NULL, &temp);
1131 if (pointer_count > 0)
1133 unsigned int increment_size;
1134 size_t offset_of_array_pointer_mem = 0;
1135 size_t offset_of_array_pointer_buf = 0;
1138 increment_size = type_memsize(type->ref, &align);
1140 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1141 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1142 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1143 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1144 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1145 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1146 *typestring_offset += 10;
1148 pointer_count = write_pointer_description_offsets(
1149 file, attrs, type, &offset_of_array_pointer_mem,
1150 &offset_of_array_pointer_buf, typestring_offset);
1153 else if (is_struct(type->type))
1156 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1158 pointer_count += write_fixed_array_pointer_descriptions(
1159 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1166 if (offset_in_memory)
1167 *offset_in_memory += type_memsize(type, &align);
1168 /* FIXME: is there a case where these two are different? */
1170 if (offset_in_buffer)
1171 *offset_in_buffer += type_memsize(type, &align);
1174 return pointer_count;
1177 /* Note: if file is NULL return value is number of pointers to write, else
1178 * it is the number of type format characters written */
1179 static int write_conformant_array_pointer_descriptions(
1180 FILE *file, const attr_list_t *attrs, type_t *type,
1181 size_t *offset_in_memory, size_t *offset_in_buffer,
1182 unsigned int *typestring_offset)
1185 int pointer_count = 0;
1187 if (is_conformant_array(type) && !type->length_is)
1189 unsigned int temp = 0;
1190 /* unfortunately, this needs to be done in two passes to avoid
1191 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1192 pointer_count = write_pointer_description_offsets(
1193 NULL, attrs, type->ref, NULL, NULL, &temp);
1194 if (pointer_count > 0)
1196 unsigned int increment_size;
1197 size_t offset_of_array_pointer_mem = 0;
1198 size_t offset_of_array_pointer_buf = 0;
1201 increment_size = type_memsize(type->ref, &align);
1203 if (increment_size > USHRT_MAX)
1204 error("array size of %u bytes is too large\n", increment_size);
1206 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1207 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1208 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1209 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1210 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1211 *typestring_offset += 8;
1213 pointer_count = write_pointer_description_offsets(
1214 file, attrs, type->ref, &offset_of_array_pointer_mem,
1215 &offset_of_array_pointer_buf, typestring_offset);
1218 else if (is_struct(type->type))
1221 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1223 pointer_count += write_conformant_array_pointer_descriptions(
1224 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1231 if (offset_in_memory)
1232 *offset_in_memory += type_memsize(type, &align);
1233 /* FIXME: is there a case where these two are different? */
1235 if (offset_in_buffer)
1236 *offset_in_buffer += type_memsize(type, &align);
1239 return pointer_count;
1242 /* Note: if file is NULL return value is number of pointers to write, else
1243 * it is the number of type format characters written */
1244 static int write_varying_array_pointer_descriptions(
1245 FILE *file, const attr_list_t *attrs, type_t *type,
1246 size_t *offset_in_memory, size_t *offset_in_buffer,
1247 unsigned int *typestring_offset)
1250 int pointer_count = 0;
1252 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1254 if (is_array(type) && type->length_is)
1256 unsigned int temp = 0;
1257 /* unfortunately, this needs to be done in two passes to avoid
1258 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1259 pointer_count = write_pointer_description_offsets(
1260 NULL, attrs, type->ref, NULL, NULL, &temp);
1261 if (pointer_count > 0)
1263 unsigned int increment_size;
1264 size_t offset_of_array_pointer_mem = 0;
1265 size_t offset_of_array_pointer_buf = 0;
1268 increment_size = type_memsize(type->ref, &align);
1270 if (increment_size > USHRT_MAX)
1271 error("array size of %u bytes is too large\n", increment_size);
1273 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1274 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1275 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1276 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1277 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1278 *typestring_offset += 8;
1280 pointer_count = write_pointer_description_offsets(
1281 file, attrs, type, &offset_of_array_pointer_mem,
1282 &offset_of_array_pointer_buf, typestring_offset);
1285 else if (is_struct(type->type))
1288 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1290 pointer_count += write_varying_array_pointer_descriptions(
1291 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1298 if (offset_in_memory)
1299 *offset_in_memory += type_memsize(type, &align);
1300 /* FIXME: is there a case where these two are different? */
1302 if (offset_in_buffer)
1303 *offset_in_buffer += type_memsize(type, &align);
1306 return pointer_count;
1309 static void write_pointer_description(FILE *file, type_t *type,
1310 unsigned int *typestring_offset)
1312 size_t offset_in_buffer;
1313 size_t offset_in_memory;
1315 /* pass 1: search for single instance of a pointer (i.e. don't descend
1317 offset_in_memory = 0;
1318 offset_in_buffer = 0;
1319 write_no_repeat_pointer_descriptions(
1321 &offset_in_memory, &offset_in_buffer, typestring_offset);
1323 /* pass 2: search for pointers in fixed arrays */
1324 offset_in_memory = 0;
1325 offset_in_buffer = 0;
1326 write_fixed_array_pointer_descriptions(
1328 &offset_in_memory, &offset_in_buffer, typestring_offset);
1330 /* pass 3: search for pointers in conformant only arrays (but don't descend
1331 * into conformant varying or varying arrays) */
1332 offset_in_memory = 0;
1333 offset_in_buffer = 0;
1334 write_conformant_array_pointer_descriptions(
1336 &offset_in_memory, &offset_in_buffer, typestring_offset);
1338 /* pass 4: search for pointers in varying arrays */
1339 offset_in_memory = 0;
1340 offset_in_buffer = 0;
1341 write_varying_array_pointer_descriptions(
1343 &offset_in_memory, &offset_in_buffer, typestring_offset);
1346 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1348 const char *name, unsigned int *typestring_offset)
1350 size_t start_offset = *typestring_offset;
1351 unsigned char flags = 0;
1353 unsigned char rtype;
1357 pointer_type = type->type;
1361 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1364 pointer_type = RPC_FC_RP;
1366 if (!get_attrp(attrs, ATTR_SIZEIS))
1367 flags |= RPC_FC_P_SIMPLEPOINTER;
1371 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1373 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1374 return start_offset;
1377 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
1378 pointer_type, flags,
1379 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
1380 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
1381 *typestring_offset += 2;
1383 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
1385 print_file(file, 2, "NdrFcShort(0x2),\n");
1386 *typestring_offset += 2;
1389 if (type->declarray && !is_conformant_array(type))
1391 /* FIXME: multi-dimensional array */
1392 if (0xffffuL < type->dim)
1393 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1394 name, 0xffffu, type->dim - 0xffffu);
1396 if (rtype == RPC_FC_CHAR)
1397 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1399 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1400 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1401 *typestring_offset += 2;
1403 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1404 *typestring_offset += 2;
1406 return start_offset;
1408 else if (type->size_is)
1410 unsigned int align = 0;
1412 if (rtype == RPC_FC_CHAR)
1413 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1415 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1416 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1417 *typestring_offset += 2;
1419 *typestring_offset += write_conf_or_var_desc(
1420 file, current_func, current_structure,
1421 (type->declarray && current_structure
1422 ? type_memsize(current_structure, &align)
1426 return start_offset;
1430 if (rtype == RPC_FC_CHAR)
1431 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1433 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1434 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1435 *typestring_offset += 2;
1437 return start_offset;
1441 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1442 const char *name, unsigned int *typestring_offset)
1444 const expr_t *length_is = type->length_is;
1445 const expr_t *size_is = type->size_is;
1446 unsigned int align = 0;
1448 size_t start_offset;
1450 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1452 pointer_type = RPC_FC_RP;
1454 has_pointer = FALSE;
1455 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1458 size = type_memsize(type, &align);
1459 if (size == 0) /* conformant array */
1460 size = type_memsize(type->ref, &align);
1462 start_offset = *typestring_offset;
1463 update_tfsoff(type, start_offset, file);
1464 print_file(file, 0, "/* %lu */\n", start_offset);
1465 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1466 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1467 *typestring_offset += 2;
1470 if (type->type != RPC_FC_BOGUS_ARRAY)
1472 unsigned char tc = type->type;
1473 unsigned int baseoff
1474 = type->declarray && current_structure
1475 ? type_memsize(current_structure, &align)
1478 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1480 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1481 *typestring_offset += 4;
1485 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1486 *typestring_offset += 2;
1489 if (is_conformant_array(type))
1491 += write_conf_or_var_desc(file, current_func, current_structure,
1494 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1496 unsigned int elalign = 0;
1497 size_t elsize = type_memsize(type->ref, &elalign);
1499 if (type->type == RPC_FC_LGVARRAY)
1501 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1502 *typestring_offset += 4;
1506 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1507 *typestring_offset += 2;
1510 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1511 *typestring_offset += 2;
1516 += write_conf_or_var_desc(file, current_func, current_structure,
1517 baseoff, length_is);
1521 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1522 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1523 *typestring_offset += 2;
1524 write_pointer_description(file, type, typestring_offset);
1525 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1526 *typestring_offset += 1;
1529 write_member_type(file, type->ref, NULL, NULL, typestring_offset);
1530 write_end(file, typestring_offset);
1533 error("%s: complex arrays unimplemented\n", name);
1535 return start_offset;
1538 static const var_t *find_array_or_string_in_struct(const type_t *type)
1540 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1541 const type_t *ft = last_field->type;
1543 if (ft->declarray && is_conformant_array(ft))
1546 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1547 return find_array_or_string_in_struct(last_field->type);
1552 static void write_struct_members(FILE *file, const type_t *type,
1553 unsigned int *corroff, unsigned int *typestring_offset)
1557 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1559 type_t *ft = field->type;
1560 if (!ft->declarray || !is_conformant_array(ft))
1561 write_member_type(file, ft, field, corroff, typestring_offset);
1564 write_end(file, typestring_offset);
1567 static size_t write_struct_tfs(FILE *file, type_t *type,
1568 const char *name, unsigned int *tfsoff)
1570 const type_t *save_current_structure = current_structure;
1571 unsigned int total_size;
1573 size_t start_offset;
1574 size_t array_offset;
1575 int has_pointers = 0;
1576 unsigned int align = 0;
1577 unsigned int corroff;
1581 current_structure = type;
1583 total_size = type_memsize(type, &align);
1584 if (total_size > USHRT_MAX)
1585 error("structure size for %s exceeds %d bytes by %d bytes\n",
1586 name, USHRT_MAX, total_size - USHRT_MAX);
1588 if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1589 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1592 array = find_array_or_string_in_struct(type);
1593 if (array && !processed(array->type))
1595 = is_attr(array->attrs, ATTR_STRING)
1596 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1597 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1600 write_descriptors(file, type, tfsoff);
1602 start_offset = *tfsoff;
1603 update_tfsoff(type, start_offset, file);
1604 print_file(file, 0, "/* %d */\n", start_offset);
1605 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1606 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1607 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1612 unsigned int absoff = array->type->typestring_offset;
1613 short reloff = absoff - *tfsoff;
1614 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1615 reloff, reloff, absoff);
1618 else if (type->type == RPC_FC_BOGUS_STRUCT)
1620 print_file(file, 2, "NdrFcShort(0x0),\n");
1624 if (type->type == RPC_FC_BOGUS_STRUCT)
1627 print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1630 else if ((type->type == RPC_FC_PSTRUCT) ||
1631 (type->type == RPC_FC_CPSTRUCT) ||
1632 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1634 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1635 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1637 write_pointer_description(file, type, tfsoff);
1638 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1642 write_struct_members(file, type, &corroff, tfsoff);
1644 current_structure = save_current_structure;
1645 return start_offset;
1648 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1649 unsigned char flags, size_t offset,
1650 unsigned int *typeformat_offset)
1652 size_t start_offset = *typeformat_offset;
1653 short reloff = offset - (*typeformat_offset + 2);
1654 int in_attr, out_attr;
1655 in_attr = is_attr(attrs, ATTR_IN);
1656 out_attr = is_attr(attrs, ATTR_OUT);
1657 if (!in_attr && !out_attr) in_attr = 1;
1659 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1662 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1665 string_of_type(pointer_type));
1669 fprintf(file, " [allocated_on_stack]");
1671 fprintf(file, " [pointer_deref]");
1672 fprintf(file, " */\n");
1675 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1676 *typeformat_offset += 4;
1678 return start_offset;
1681 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1685 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1687 else if (is_base_type(t->type))
1689 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1690 t->type, string_of_type(t->type));
1692 else if (t->typestring_offset)
1694 short reloff = t->typestring_offset - *tfsoff;
1695 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1696 reloff, reloff, t->typestring_offset);
1699 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1704 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1706 unsigned int align = 0;
1707 unsigned int start_offset;
1708 size_t size = type_memsize(type, &align);
1711 type_t *deftype = NULL;
1712 short nodeftype = 0xffff;
1717 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1719 const var_t *uv = LIST_ENTRY(list_tail(type->fields), const var_t, entry);
1720 fields = uv->type->fields;
1723 fields = type->fields;
1725 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1727 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1729 nbranch += list_count(cases);
1731 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1734 start_offset = *tfsoff;
1735 update_tfsoff(type, start_offset, file);
1736 print_file(file, 0, "/* %d */\n", start_offset);
1737 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1739 const var_t *sv = LIST_ENTRY(list_head(type->fields), const var_t, entry);
1740 const type_t *st = sv->type;
1753 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1754 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1755 0x40 | st->type, string_of_type(st->type));
1759 error("union switch type must be an integer, char, or enum\n");
1762 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1763 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1766 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1768 type_t *ft = f->type;
1769 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1770 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1773 if (cases == NULL && !deflt)
1774 error("union field %s with neither case nor default attribute\n", f->name);
1776 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1778 /* MIDL doesn't check for duplicate cases, even though that seems
1779 like a reasonable thing to do, it just dumps them to the TFS
1780 like we're going to do here. */
1781 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1783 write_branch_type(file, ft, tfsoff);
1786 /* MIDL allows multiple default branches, even though that seems
1787 illogical, it just chooses the last one, which is what we will
1798 write_branch_type(file, deftype, tfsoff);
1802 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1806 return start_offset;
1809 static size_t write_ip_tfs(FILE *file, const func_t *func, const attr_list_t *attrs,
1810 type_t *type, unsigned int *typeformat_offset)
1813 size_t start_offset = *typeformat_offset;
1814 const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1820 expr.type = EXPR_IDENTIFIER;
1822 expr.u.sval = iid->name;
1823 expr.is_const = FALSE;
1824 print_file(file, 2, "0x2f, /* FC_IP */\n");
1825 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1826 *typeformat_offset += write_conf_or_var_desc(file, func, NULL, 0, &expr) + 2;
1830 const type_t *base = is_ptr(type) ? type->ref : type;
1831 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1834 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1836 update_tfsoff(type, start_offset, file);
1837 print_file(file, 0, "/* %d */\n", start_offset);
1838 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1839 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1840 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1841 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1842 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1843 for (i = 0; i < 8; ++i)
1844 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1847 fprintf(file, "\n");
1849 *typeformat_offset += 18;
1851 return start_offset;
1854 static int get_ptr_attr(const type_t *t, int def_type)
1858 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1861 if (t->kind != TKIND_ALIAS)
1867 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1868 type_t *type, const var_t *var,
1869 unsigned int *typeformat_offset)
1874 if (is_user_type(type))
1876 write_user_tfs(file, type, typeformat_offset);
1877 return type->typestring_offset;
1880 if (type == var->type) /* top-level pointers */
1882 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1883 if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1884 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1886 if (pointer_attr == 0)
1887 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1889 pointer_type = pointer_attr;
1892 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1894 if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1895 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1898 return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1902 /* basic types don't need a type format string */
1903 if (is_base_type(type->type))
1909 case RPC_FC_PSTRUCT:
1910 case RPC_FC_CSTRUCT:
1911 case RPC_FC_CPSTRUCT:
1912 case RPC_FC_CVSTRUCT:
1913 case RPC_FC_BOGUS_STRUCT:
1914 return write_struct_tfs(file, type, var->name, typeformat_offset);
1915 case RPC_FC_ENCAPSULATED_UNION:
1916 case RPC_FC_NON_ENCAPSULATED_UNION:
1917 return write_union_tfs(file, type, typeformat_offset);
1919 case RPC_FC_BIND_PRIMITIVE:
1923 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1926 else if (last_ptr(type))
1928 size_t start_offset = *typeformat_offset;
1929 int in_attr = is_attr(var->attrs, ATTR_IN);
1930 int out_attr = is_attr(var->attrs, ATTR_OUT);
1931 const type_t *base = type->ref;
1933 if (base->type == RPC_FC_IP)
1935 return write_ip_tfs(file, func, var->attrs, type, typeformat_offset);
1938 /* special case for pointers to base types */
1939 if (is_base_type(base->type))
1941 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1942 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1943 string_of_type(pointer_type),
1944 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1945 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
1946 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1947 *typeformat_offset += 4;
1948 return start_offset;
1952 assert(is_ptr(type));
1954 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1956 fprintf(file, "/* %2u */\n", *typeformat_offset);
1957 return write_pointer_only_tfs(file, var->attrs, pointer_type,
1958 !last_ptr(type) ? 0x10 : 0,
1959 offset, typeformat_offset);
1962 static void set_tfswrite(type_t *type, int val)
1964 while (type->tfswrite != val)
1966 type_t *utype = get_user_type(type, NULL);
1968 type->tfswrite = val;
1971 set_tfswrite(utype, val);
1973 if (type->kind == TKIND_ALIAS)
1975 else if (is_ptr(type) || is_array(type))
1982 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1984 set_tfswrite(v->type, val);
1992 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
1993 const char *name, int write_ptr, unsigned int *tfsoff)
1997 if (is_user_type(type))
1999 write_user_tfs(file, type, tfsoff);
2001 else if (is_ptr(type))
2003 type_t *ref = type->ref;
2005 if (ref->type == RPC_FC_IP)
2007 write_ip_tfs(file, NULL, attrs, type, tfsoff);
2011 if (!processed(ref) && !is_base_type(ref->type))
2012 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2015 write_pointer_tfs(file, type, tfsoff);
2020 else if (type->declarray && is_conformant_array(type))
2021 ; /* conformant arrays and strings are handled specially */
2022 else if (is_array(type))
2024 write_array_tfs(file, attrs, type, name, tfsoff);
2026 else if (is_struct(type->type))
2028 if (!processed(type))
2029 write_struct_tfs(file, type, name, tfsoff);
2031 else if (is_union(type->type))
2033 if (!processed(type))
2034 write_union_tfs(file, type, tfsoff);
2036 else if (!is_base_type(type->type))
2037 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2043 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2045 const ifref_t * iface;
2050 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2051 if (iface->iface->funcs)
2052 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2054 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2055 set_tfswrite(var->type, val);
2058 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
2061 const ifref_t *iface;
2062 unsigned int typeformat_offset = 2;
2064 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2066 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2069 if (iface->iface->funcs)
2072 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2074 if (is_local(func->def->attrs)) continue;
2076 current_func = func;
2078 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2081 write_typeformatstring_var(
2082 file, 2, func, var->type, var,
2083 &typeformat_offset),
2089 return typeformat_offset + 1;
2093 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2097 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2098 print_file(file, indent, "{\n");
2100 print_file(file, indent, "0,\n");
2101 print_file(file, indent, "{\n");
2103 print_file(file, indent, "NdrFcShort(0x0),\n");
2105 set_all_tfswrite(ifaces, TRUE);
2106 process_tfs(file, ifaces, for_objects);
2108 print_file(file, indent, "0x0\n");
2110 print_file(file, indent, "}\n");
2112 print_file(file, indent, "};\n");
2113 print_file(file, indent, "\n");
2116 static unsigned int get_required_buffer_size_type(
2117 const type_t *type, const char *name, unsigned int *alignment)
2122 if (is_user_type(type))
2125 const type_t *utype = get_user_type(type, &uname);
2126 size = get_required_buffer_size_type(utype, uname, alignment);
2128 else if (!is_ptr(type))
2152 case RPC_FC_ERROR_STATUS_T:
2164 case RPC_FC_BIND_PRIMITIVE:
2168 case RPC_FC_PSTRUCT:
2171 if (!type->fields) return 0;
2172 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2174 unsigned int alignment;
2175 size += get_required_buffer_size_type(field->type, field->name,
2182 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
2183 size = get_required_buffer_size_type( type->ref, name, alignment );
2186 case RPC_FC_SMFARRAY:
2187 case RPC_FC_LGFARRAY:
2188 size = type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2191 case RPC_FC_SMVARRAY:
2192 case RPC_FC_LGVARRAY:
2193 get_required_buffer_size_type(type->ref, name, alignment);
2198 case RPC_FC_CVARRAY:
2199 get_required_buffer_size_type(type->ref, name, alignment);
2200 size = sizeof(void *);
2204 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
2211 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2213 int in_attr = is_attr(var->attrs, ATTR_IN);
2214 int out_attr = is_attr(var->attrs, ATTR_OUT);
2216 if (!in_attr && !out_attr)
2221 if (pass == PASS_OUT)
2223 if (out_attr && is_ptr(var->type))
2225 type_t *type = var->type;
2227 if (type->type == RPC_FC_STRUCT)
2230 unsigned int size = 36;
2232 if (!type->fields) return size;
2233 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2236 size += get_required_buffer_size_type(
2237 field->type, field->name, &align);
2246 if ((!out_attr || in_attr) && !var->type->size_is
2247 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2249 if (is_ptr(var->type))
2251 type_t *type = var->type;
2253 if (is_base_type(type->type))
2257 else if (type->type == RPC_FC_STRUCT)
2259 unsigned int size = 36;
2262 if (!type->fields) return size;
2263 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2266 size += get_required_buffer_size_type(
2267 field->type, field->name, &align);
2274 return get_required_buffer_size_type(var->type, var->name, alignment);
2278 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2281 unsigned int total_size = 0, alignment;
2285 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2287 total_size += get_required_buffer_size(var, &alignment, pass);
2288 total_size += alignment;
2292 if (pass == PASS_OUT && !is_void(func->def->type))
2294 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2295 total_size += alignment;
2300 static void print_phase_function(FILE *file, int indent, const char *type,
2301 enum remoting_phase phase,
2302 const var_t *var, unsigned int type_offset)
2304 const char *function;
2307 case PHASE_BUFFERSIZE:
2308 function = "BufferSize";
2311 function = "Marshall";
2313 case PHASE_UNMARSHAL:
2314 function = "Unmarshall";
2324 print_file(file, indent, "Ndr%s%s(\n", type, function);
2326 print_file(file, indent, "&_StubMsg,\n");
2327 print_file(file, indent, "%s%s%s%s,\n",
2328 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2329 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2330 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2332 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2333 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2334 if (phase == PHASE_UNMARSHAL)
2335 print_file(file, indent, "0);\n");
2339 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2340 enum pass pass, const var_t *var,
2341 const char *varname)
2343 type_t *type = var->type;
2345 unsigned int alignment = 0;
2346 unsigned char rtype;
2348 /* no work to do for other phases, buffer sizing is done elsewhere */
2349 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2352 rtype = is_ptr(type) ? type->ref->type : type->type;
2376 case RPC_FC_ERROR_STATUS_T:
2388 case RPC_FC_BIND_PRIMITIVE:
2389 /* no marshalling needed */
2393 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2397 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2398 alignment - 1, alignment - 1);
2400 if (phase == PHASE_MARSHAL)
2402 print_file(file, indent, "*(");
2403 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2405 fprintf(file, " *)_StubMsg.Buffer = *");
2407 fprintf(file, " *)_StubMsg.Buffer = ");
2408 fprintf(file, varname);
2409 fprintf(file, ";\n");
2411 else if (phase == PHASE_UNMARSHAL)
2413 if (pass == PASS_IN || pass == PASS_RETURN)
2414 print_file(file, indent, "");
2416 print_file(file, indent, "*");
2417 fprintf(file, varname);
2418 if (pass == PASS_IN && is_ptr(type))
2419 fprintf(file, " = (");
2421 fprintf(file, " = *(");
2422 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2423 fprintf(file, " *)_StubMsg.Buffer;\n");
2426 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2427 write_type(file, var->type, FALSE, NULL);
2428 fprintf(file, ");\n");
2431 /* returns whether the MaxCount, Offset or ActualCount members need to be
2432 * filled in for the specified phase */
2433 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2435 return (phase != PHASE_UNMARSHAL);
2438 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2439 enum pass pass, enum remoting_phase phase)
2441 int in_attr, out_attr, pointer_type;
2447 if (phase == PHASE_BUFFERSIZE)
2449 unsigned int size = get_function_buffer_size( func, pass );
2450 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2453 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2455 const type_t *type = var->type;
2456 unsigned char rtype;
2457 size_t start_offset = type->typestring_offset;
2459 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2461 pointer_type = RPC_FC_RP;
2463 in_attr = is_attr(var->attrs, ATTR_IN);
2464 out_attr = is_attr(var->attrs, ATTR_OUT);
2465 if (!in_attr && !out_attr)
2471 if (!in_attr) continue;
2474 if (!out_attr) continue;
2482 if (is_user_type(var->type))
2484 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2486 else if (is_string_type(var->attrs, var->type))
2488 if (is_array(type) && !is_conformant_array(type))
2489 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2492 if (type->size_is && is_size_needed_for_phase(phase))
2494 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2495 write_expr(file, type->size_is, 1);
2496 fprintf(file, ";\n");
2499 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2500 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2502 print_phase_function(file, indent, "ConformantString", phase, var,
2503 start_offset + (type->size_is ? 4 : 2));
2506 else if (is_array(type))
2508 unsigned char tc = type->type;
2509 const char *array_type;
2511 if (tc == RPC_FC_SMFARRAY || tc == RPC_FC_LGFARRAY)
2512 array_type = "FixedArray";
2513 else if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2515 if (is_size_needed_for_phase(phase))
2517 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2518 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2519 write_expr(file, type->length_is, 1);
2520 fprintf(file, ";\n\n");
2522 array_type = "VaryingArray";
2524 else if (tc == RPC_FC_CARRAY)
2526 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2528 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2529 write_expr(file, type->size_is, 1);
2530 fprintf(file, ";\n\n");
2532 array_type = "ConformantArray";
2534 else if (tc == RPC_FC_CVARRAY)
2536 if (is_size_needed_for_phase(phase))
2538 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2539 write_expr(file, type->size_is, 1);
2540 fprintf(file, ";\n");
2541 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2542 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2543 write_expr(file, type->length_is, 1);
2544 fprintf(file, ";\n\n");
2546 array_type = "ConformantVaryingArray";
2549 array_type = "ComplexArray";
2551 if (!in_attr && phase == PHASE_FREE)
2553 print_file(file, indent, "if (%s)\n", var->name);
2555 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2557 else if (phase != PHASE_FREE)
2559 if (pointer_type == RPC_FC_UP)
2560 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2562 print_phase_function(file, indent, array_type, phase, var, start_offset);
2565 else if (!is_ptr(var->type) && is_base_type(rtype))
2567 print_phase_basetype(file, indent, phase, pass, var, var->name);
2569 else if (!is_ptr(var->type))
2574 case RPC_FC_PSTRUCT:
2575 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2577 case RPC_FC_CSTRUCT:
2578 case RPC_FC_CPSTRUCT:
2579 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2581 case RPC_FC_CVSTRUCT:
2582 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2584 case RPC_FC_BOGUS_STRUCT:
2585 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2588 if (is_base_type( var->type->ref->type ))
2590 print_phase_basetype(file, indent, phase, pass, var, var->name);
2592 else if (var->type->ref->type == RPC_FC_STRUCT)
2594 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2595 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2600 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2601 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2602 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2606 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2611 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2613 print_phase_basetype(file, indent, phase, pass, var, var->name);
2615 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2617 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2618 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2623 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2624 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2625 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2628 fprintf(file, "\n");
2633 size_t get_size_procformatstring_var(const var_t *var)
2635 return write_procformatstring_var(NULL, 0, var, FALSE);
2639 size_t get_size_procformatstring_func(const func_t *func)
2644 /* argument list size */
2646 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2647 size += get_size_procformatstring_var(var);
2649 /* return value size */
2650 if (is_void(func->def->type))
2651 size += 2; /* FC_END and FC_PAD */
2653 size += get_size_procformatstring_var(func->def);
2658 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2660 const ifref_t *iface;
2664 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2666 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2669 if (iface->iface->funcs)
2670 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2671 if (!is_local(func->def->attrs))
2672 size += get_size_procformatstring_func( func );
2677 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2679 set_all_tfswrite(ifaces, FALSE);
2680 return process_tfs(NULL, ifaces, for_objects);
2683 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2684 const var_list_t *fields, const char *structvar)
2690 fprintf(h, "%lu", e->u.lval);
2693 fprintf(h, "0x%lx", e->u.lval);
2696 fprintf(h, "%#.15g", e->u.dval);
2698 case EXPR_TRUEFALSE:
2700 fprintf(h, "FALSE");
2704 case EXPR_IDENTIFIER:
2707 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2708 if (!strcmp(e->u.sval, field->name))
2710 fprintf(h, "%s->%s", structvar, e->u.sval);
2714 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2719 write_struct_expr(h, e->ref, 1, fields, structvar);
2723 write_struct_expr(h, e->ref, 1, fields, structvar);
2727 write_struct_expr(h, e->ref, 1, fields, structvar);
2731 write_type(h, e->u.tref, FALSE, NULL);
2733 write_struct_expr(h, e->ref, 1, fields, structvar);
2736 fprintf(h, "sizeof(");
2737 write_type(h, e->u.tref, FALSE, NULL);
2748 if (brackets) fprintf(h, "(");
2749 write_struct_expr(h, e->ref, 1, fields, structvar);
2751 case EXPR_SHL: fprintf(h, " << "); break;
2752 case EXPR_SHR: fprintf(h, " >> "); break;
2753 case EXPR_MUL: fprintf(h, " * "); break;
2754 case EXPR_DIV: fprintf(h, " / "); break;
2755 case EXPR_ADD: fprintf(h, " + "); break;
2756 case EXPR_SUB: fprintf(h, " - "); break;
2757 case EXPR_AND: fprintf(h, " & "); break;
2758 case EXPR_OR: fprintf(h, " | "); break;
2761 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2762 if (brackets) fprintf(h, ")");
2765 if (brackets) fprintf(h, "(");
2766 write_struct_expr(h, e->ref, 1, fields, structvar);
2768 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2770 write_struct_expr(h, e->ext2, 1, fields, structvar);
2771 if (brackets) fprintf(h, ")");
2777 void declare_stub_args( FILE *file, int indent, const func_t *func )
2779 int in_attr, out_attr;
2781 const var_t *def = func->def;
2784 /* declare return value '_RetVal' */
2785 if (!is_void(def->type))
2787 print_file(file, indent, "");
2788 write_type_left(file, def->type);
2789 fprintf(file, " _RetVal;\n");
2795 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2797 int is_string = is_attr(var->attrs, ATTR_STRING);
2799 in_attr = is_attr(var->attrs, ATTR_IN);
2800 out_attr = is_attr(var->attrs, ATTR_OUT);
2801 if (!out_attr && !in_attr)
2804 if (!in_attr && !var->type->size_is && !is_string)
2806 print_file(file, indent, "");
2807 write_type(file, var->type->ref, FALSE, "_W%u", i++);
2808 fprintf(file, ";\n");
2811 print_file(file, indent, "");
2812 write_type_left(file, var->type);
2814 if (var->type->declarray) {
2815 fprintf(file, "( *");
2816 write_name(file, var);
2817 fprintf(file, " )");
2819 write_name(file, var);
2820 write_type_right(file, var->type, FALSE);
2821 fprintf(file, ";\n");
2823 if (decl_indirect(var->type))
2824 print_file(file, indent, "void *_p_%s = &%s;\n",
2825 var->name, var->name);
2830 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2832 int in_attr, out_attr;
2839 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2841 int is_string = is_attr(var->attrs, ATTR_STRING);
2842 in_attr = is_attr(var->attrs, ATTR_IN);
2843 out_attr = is_attr(var->attrs, ATTR_OUT);
2844 if (!out_attr && !in_attr)
2849 print_file(file, indent, "");
2850 write_name(file, var);
2852 if (var->type->size_is)
2854 unsigned int size, align = 0;
2855 type_t *type = var->type;
2857 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2858 for ( ; type->size_is ; type = type->ref)
2860 write_expr(file, type->size_is, TRUE);
2861 fprintf(file, " * ");
2863 size = type_memsize(type, &align);
2864 fprintf(file, "%u);\n", size);
2866 else if (!is_string)
2868 fprintf(file, " = &_W%u;\n", i);
2869 if (is_ptr(var->type) && !last_ptr(var->type))
2870 print_file(file, indent, "_W%u = 0;\n", i);
2878 fprintf(file, "\n");
2882 int write_expr_eval_routines(FILE *file, const char *iface)
2885 struct expr_eval_routine *eval;
2886 unsigned short callback_offset = 0;
2888 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2892 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2893 iface, eval->structure->name, callback_offset);
2894 print_file(file, indent, "{\n");
2896 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2897 eval->structure->name, eval->structure->name, eval->structure_size);
2898 fprintf(file, "\n");
2899 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2900 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2901 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2902 fprintf(file, ";\n");
2904 print_file(file, indent, "}\n\n");
2910 void write_expr_eval_routine_list(FILE *file, const char *iface)
2912 struct expr_eval_routine *eval;
2913 struct expr_eval_routine *cursor;
2914 unsigned short callback_offset = 0;
2916 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2917 fprintf(file, "{\n");
2919 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2921 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2922 iface, eval->structure->name, callback_offset);
2925 list_remove(&eval->entry);
2929 fprintf(file, "};\n\n");
2932 void write_user_quad_list(FILE *file)
2936 if (list_empty(&user_type_list))
2939 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
2940 fprintf(file, "{\n");
2941 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
2943 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
2944 print_file(file, 1, "{\n");
2945 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
2946 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
2947 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
2948 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
2949 print_file(file, 1, "}%s\n", sep);
2951 fprintf(file, "};\n\n");
2954 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2956 const struct str_list_entry_t *endpoint;
2959 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2960 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2961 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2963 print_file( f, 1, "{ (const unsigned char *)\"" );
2964 for (p = endpoint->str; *p && *p != ':'; p++)
2966 if (*p == '"' || *p == '\\') fputc( '\\', f );
2969 if (!*p) goto error;
2970 if (p[1] != '[') goto error;
2972 fprintf( f, "\", (const unsigned char *)\"" );
2973 for (p += 2; *p && *p != ']'; p++)
2975 if (*p == '"' || *p == '\\') fputc( '\\', f );
2978 if (*p != ']') goto error;
2979 fprintf( f, "\" },\n" );
2981 print_file( f, 0, "};\n\n" );
2985 error("Invalid endpoint syntax '%s'\n", endpoint->str);