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 type_memsize(const type_t *t, int ptr_level, const expr_t *array);
63 static size_t fields_memsize(const var_t *v);
65 static int compare_expr(const expr_t *a, const expr_t *b)
69 if (a->type != b->type)
70 return a->type - b->type;
77 return a->u.lval - b->u.lval;
79 return strcmp(a->u.sval, b->u.sval);
81 ret = compare_expr(a->ref, b->ref);
84 ret = compare_expr(a->u.ext, b->u.ext);
87 return compare_expr(a->ext2, b->ext2);
96 ret = compare_expr(a->ref, b->ref);
99 return compare_expr(a->u.ext, b->u.ext);
105 return compare_expr(a->ref, b->ref);
112 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
115 fprintf(file, "/* %2u */\n", typestring_offset); \
116 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
120 static int print_file(FILE *file, int indent, const char *format, ...)
127 va_start(va, format);
128 for (i = 0; i < indent; i++)
130 r = vfprintf(file, format, va);
135 static inline int type_has_ref(const type_t *type)
137 return (type->type == 0 && type->ref);
140 static inline int is_base_type(unsigned char type)
159 case RPC_FC_ERROR_STATUS_T:
160 case RPC_FC_BIND_PRIMITIVE:
168 static size_t write_procformatstring_var(FILE *file, int indent,
169 const var_t *var, int is_return, unsigned int *type_offset)
172 int ptr_level = var->ptr_level;
173 const type_t *type = var->type;
175 int is_in = is_attr(var->attrs, ATTR_IN);
176 int is_out = is_attr(var->attrs, ATTR_OUT);
178 if (!is_in && !is_out) is_in = TRUE;
180 if (ptr_level == 0 && type_has_ref(type))
183 if (ptr_level == 0 && !var->array && is_base_type(type->type))
186 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
188 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
192 #define CASE_BASETYPE(fctype) \
194 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
195 size = 2; /* includes param type prefix */ \
198 CASE_BASETYPE(FC_BYTE);
199 CASE_BASETYPE(FC_CHAR);
200 CASE_BASETYPE(FC_WCHAR);
201 CASE_BASETYPE(FC_USHORT);
202 CASE_BASETYPE(FC_SHORT);
203 CASE_BASETYPE(FC_ULONG);
204 CASE_BASETYPE(FC_LONG);
205 CASE_BASETYPE(FC_HYPER);
206 CASE_BASETYPE(FC_IGNORE);
207 CASE_BASETYPE(FC_USMALL);
208 CASE_BASETYPE(FC_SMALL);
209 CASE_BASETYPE(FC_FLOAT);
210 CASE_BASETYPE(FC_DOUBLE);
211 CASE_BASETYPE(FC_ERROR_STATUS_T);
214 case RPC_FC_BIND_PRIMITIVE:
215 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
216 size = 2; /* includes param type prefix */
220 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
227 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
228 else if (is_in && is_out)
229 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
231 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
233 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
235 print_file(file, indent, "0x01,\n");
236 print_file(file, indent, "NdrFcShort(0x%x),\n", *type_offset);
237 size = 4; /* includes param type prefix */
239 *type_offset += get_size_typeformatstring_var(var);
243 void write_procformatstring(FILE *file, const ifref_t *ifaces)
245 const ifref_t *iface = ifaces;
248 unsigned int type_offset = 2;
250 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
251 print_file(file, indent, "{\n");
253 print_file(file, indent, "0,\n");
254 print_file(file, indent, "{\n");
259 for (; iface; iface = PREV_LINK(iface))
261 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
264 if (iface->iface->funcs)
266 func_t *func = iface->iface->funcs;
267 while (NEXT_LINK(func)) func = NEXT_LINK(func);
268 for (; func; func = PREV_LINK(func))
270 /* emit argument data */
274 while (NEXT_LINK(var)) var = NEXT_LINK(var);
277 write_procformatstring_var(file, indent, var, FALSE,
280 var = PREV_LINK(var);
284 /* emit return value data */
286 if (is_void(var->type, NULL))
288 print_file(file, indent, "0x5b, /* FC_END */\n");
289 print_file(file, indent, "0x5c, /* FC_PAD */\n");
292 write_procformatstring_var(file, indent, var, TRUE,
298 print_file(file, indent, "0x0\n");
300 print_file(file, indent, "}\n");
302 print_file(file, indent, "};\n");
303 print_file(file, indent, "\n");
306 /* write conformance / variance descriptor */
307 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_t *expr)
309 unsigned char operator_type = 0;
310 const char *operator_string = "no operators";
311 const expr_t *subexpr = expr;
312 unsigned char correlation_type;
314 if (!file) return 4; /* optimisation for sizing pass */
318 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
319 error("write_conf_or_var_desc: constant value %ld is greater than "
320 "the maximum constant size of %d\n", expr->cval,
321 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
323 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
324 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
325 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
326 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
331 switch (subexpr->type)
334 subexpr = subexpr->ref;
335 operator_type = RPC_FC_DEREFERENCE;
336 operator_string = "FC_DEREFERENCE";
339 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
341 subexpr = subexpr->ref;
342 operator_type = RPC_FC_DIV_2;
343 operator_string = "FC_DIV_2";
347 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
349 subexpr = subexpr->ref;
350 operator_type = RPC_FC_MULT_2;
351 operator_string = "FC_MULT_2";
355 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
357 subexpr = subexpr->ref;
358 operator_type = RPC_FC_SUB_1;
359 operator_string = "FC_SUB_1";
363 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
365 subexpr = subexpr->ref;
366 operator_type = RPC_FC_ADD_1;
367 operator_string = "FC_ADD_1";
374 if (subexpr->type == EXPR_IDENTIFIER)
376 const type_t *correlation_variable = NULL;
377 unsigned char param_type = 0;
378 const char *param_type_string = NULL;
385 for (offset = 0, var = structure->fields; var; var = NEXT_LINK(var))
387 offset -= type_memsize(var->type, var->ptr_level, var->array);
388 if (!strcmp(var->name, subexpr->u.sval))
390 correlation_variable = var->type;
394 if (!correlation_variable)
395 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
398 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
402 const var_t *var = func->args;
404 while (NEXT_LINK(var)) var = NEXT_LINK(var);
405 /* FIXME: not all stack variables are sizeof(void *) */
406 for (offset = 0; var; offset += sizeof(void *), var = PREV_LINK(var))
408 if (!strcmp(var->name, subexpr->u.sval))
410 correlation_variable = var->type;
414 if (!correlation_variable)
415 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
418 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
421 while (type_has_ref(correlation_variable))
422 correlation_variable = correlation_variable->ref;
424 switch (correlation_variable->type)
428 param_type = RPC_FC_SMALL;
429 param_type_string = "FC_SMALL";
433 param_type = RPC_FC_USMALL;
434 param_type_string = "FC_USMALL";
438 param_type = RPC_FC_SHORT;
439 param_type_string = "FC_SHORT";
442 param_type = RPC_FC_USHORT;
443 param_type_string = "FC_USHORT";
446 param_type = RPC_FC_LONG;
447 param_type_string = "FC_LONG";
450 param_type = RPC_FC_ULONG;
451 param_type_string = "FC_ULONG";
454 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
455 correlation_variable->type);
458 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
459 correlation_type | param_type,
460 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
462 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
463 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
465 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
470 unsigned int callback_offset = 0;
474 struct expr_eval_routine *eval;
477 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
479 if (!strcmp(eval->structure->name, structure->name) &&
480 !compare_expr(eval->expr, expr))
490 eval = xmalloc(sizeof(*eval));
491 eval->structure = structure;
492 eval->structure_size = fields_memsize(structure->fields);
494 list_add_tail(&expr_eval_routines, &eval->entry);
497 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
501 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
502 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
505 if (callback_offset > USHRT_MAX)
506 error("Maximum number of callback routines reached\n");
508 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
510 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
511 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
512 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
517 static size_t fields_memsize(const var_t *v)
520 const var_t *first = v;
522 while (NEXT_LINK(v)) v = NEXT_LINK(v);
524 size += type_memsize(v->type, v->ptr_level, v->array);
525 if (v == first) break;
531 static size_t type_memsize(const type_t *t, int ptr_level, const expr_t *array)
536 return sizeof(void *);
539 return type_memsize(t->ref, 0 /* FIXME */, NULL);
557 case RPC_FC_ERROR_STATUS_T:
567 case RPC_FC_CVSTRUCT:
568 case RPC_FC_CPSTRUCT:
571 case RPC_FC_BOGUS_STRUCT:
572 case RPC_FC_ENCAPSULATED_UNION:
573 case RPC_FC_NON_ENCAPSULATED_UNION:
574 size = fields_memsize(t->fields);
577 error("type_memsize: Unknown type %d\n", t->type);
592 size_t get_type_memsize(const type_t *type)
594 return type_memsize(type, 0, NULL);
597 static int write_pointers(FILE *file, const attr_t *attrs,
598 const type_t *type, int ptr_level,
599 const expr_t *array, int level,
600 unsigned int *typestring_offset)
602 int pointers_written = 0;
605 /* don't generate a pointer for first-level arrays since we want to
606 * descend into them to write their pointers, not stop here */
607 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
609 return write_pointers(file, NULL, type, 0, NULL, level + 1, typestring_offset);
614 /* FIXME: only general algorithm implemented, not the actual writing */
615 error("write_pointers: Writing type format string for pointer is unimplemented\n");
619 /* FIXME: search through all refs for pointers too */
620 while(type_has_ref(type))
625 /* note: don't descend into complex structures or unions since these
626 * will always be generated as a separate type */
628 case RPC_FC_CVSTRUCT:
629 case RPC_FC_CPSTRUCT:
634 while (NEXT_LINK(v)) v = NEXT_LINK(v);
635 for (; v; v = PREV_LINK(v))
636 pointers_written += write_pointers(file, v->attrs, v->type,
637 v->ptr_level, v->array,
648 return pointers_written;
651 static size_t write_pointer_description(FILE *file, const attr_t *attrs,
652 const type_t *type, int ptr_level,
653 const expr_t *array, int level,
654 size_t typestring_offset)
659 /* don't generate a pointer for first-level arrays since we want to
660 * descend into them to write their pointers, not stop here */
661 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
663 return write_pointer_description(file, NULL, type, 0, NULL,
664 level + 1, typestring_offset);
669 /* FIXME: only general algorithm implemented, not the actual writing */
670 error("write_pointer_description: Writing pointer description is unimplemented\n");
674 /* FIXME: search through all refs for pointers too */
678 /* note: don't descend into complex structures or unions since these
679 * will always be generated as a separate type */
681 case RPC_FC_CVSTRUCT:
682 case RPC_FC_CPSTRUCT:
687 while (NEXT_LINK(v)) v = NEXT_LINK(v);
688 for (; v; v = PREV_LINK(v))
689 size += write_pointer_description(file, v->attrs, v->type,
690 v->ptr_level, v->array,
704 static size_t write_string_tfs(FILE *file, const attr_t *attrs,
705 const type_t *type, const expr_t *array,
706 const char *name, unsigned int *typestring_offset)
708 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
709 int has_size = size_is && (size_is->type != EXPR_VOID);
710 size_t start_offset = *typestring_offset;
711 unsigned char flags = 0;
712 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
714 pointer_type = RPC_FC_RP;
716 if (!get_attrp(attrs, ATTR_SIZEIS))
717 flags |= RPC_FC_P_SIMPLEPOINTER;
719 while (type_has_ref(type))
722 if ((type->type != RPC_FC_BYTE) && (type->type != RPC_FC_CHAR) && (type->type != RPC_FC_WCHAR))
724 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", type->type, name);
728 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
730 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
731 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
732 *typestring_offset += 2;
734 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
736 print_file(file, 2, "NdrFcShort(0x2),\n");
737 *typestring_offset += 2;
740 if (array && array->is_const)
742 if (array->cval > USHRT_MAX)
743 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
744 name, USHRT_MAX, array->cval - USHRT_MAX);
746 if (type->type == RPC_FC_CHAR)
747 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
749 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
750 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
751 *typestring_offset += 2;
753 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", array->cval, array->cval);
754 *typestring_offset += 2;
760 if (type->type == RPC_FC_CHAR)
761 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
763 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
764 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
765 *typestring_offset += 2;
767 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
773 if (type->type == RPC_FC_CHAR)
774 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
776 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
777 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
778 *typestring_offset += 2;
784 static size_t write_array_tfs(FILE *file, const attr_t *attrs,
785 const type_t *type, const expr_t *array,
786 const char *name, unsigned int *typestring_offset)
788 const expr_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
789 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
790 int has_length = length_is && (length_is->type != EXPR_VOID);
791 int has_size = (size_is && (size_is->type != EXPR_VOID)) || !array->is_const;
793 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
795 pointer_type = RPC_FC_RP;
797 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
799 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
800 print_file(file, 2, "NdrFcShort(0x2),\n");
801 *typestring_offset += 4;
803 if (array && NEXT_LINK(array)) /* multi-dimensional array */
805 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
810 size_t pointer_start_offset = *typestring_offset;
813 if (write_pointers(file, attrs, type, 0, array, 0, typestring_offset) > 0)
816 start_offset = *typestring_offset;
818 if (!has_length && !has_size)
821 size_t size = type_memsize(type, 0, array);
822 if (size < USHRT_MAX)
824 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
826 print_file(file, 2, "0x%x, /* 0 */\n", 0);
828 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
829 *typestring_offset += 4;
833 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
835 print_file(file, 2, "0x%x, /* 0 */\n", 0);
837 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
838 *typestring_offset += 6;
843 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
844 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
845 *typestring_offset += 2;
846 *typestring_offset = write_pointer_description(file, attrs,
847 type, 0, array, 0, pointer_start_offset);
848 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
849 *typestring_offset += 1;
852 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
853 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
854 *typestring_offset += 2;
858 else if (has_length && !has_size)
861 size_t element_size = type_memsize(type, 0, NULL);
862 size_t elements = array->cval;
863 size_t total_size = element_size * elements;
865 if (total_size < USHRT_MAX)
867 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
869 print_file(file, 2, "0x%x, /* 0 */\n", 0);
871 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
872 /* number of elements */
873 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
874 *typestring_offset += 6;
878 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
880 print_file(file, 2, "0x%x, /* 0 */\n", 0);
882 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
883 /* number of elements */
884 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
885 *typestring_offset += 10;
888 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
889 *typestring_offset += 2;
891 *typestring_offset += write_conf_or_var_desc(file, current_func,
897 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
898 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
899 *typestring_offset += 2;
900 *typestring_offset += write_pointer_description(file, attrs,
901 type, 0, array, 0, pointer_start_offset);
902 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
903 *typestring_offset += 1;
906 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
907 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
908 *typestring_offset += 2;
912 else if (!has_length && has_size)
914 /* conformant array */
915 size_t element_size = type_memsize(type, 0, NULL);
917 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
919 print_file(file, 2, "0x%x, /* 0 */\n", 0);
921 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
922 *typestring_offset += 4;
924 *typestring_offset += write_conf_or_var_desc(file, current_func,
926 size_is ? size_is : array);
930 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
931 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
932 *typestring_offset += 2;
933 *typestring_offset += write_pointer_description(file, attrs,
934 type, 0, array, 0, pointer_start_offset);
935 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
936 *typestring_offset += 1;
939 print_file(file, 2, "0x%x, /* FIXME: write out conversion data */\n", type->type);
940 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
941 *typestring_offset += 2;
947 /* conformant varying array */
948 size_t element_size = type_memsize(type, 0, NULL);
950 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
952 print_file(file, 2, "0x%x, /* 0 */\n", 0);
954 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
955 *typestring_offset += 4;
957 *typestring_offset += write_conf_or_var_desc(file, current_func,
959 size_is ? size_is : array);
960 *typestring_offset += write_conf_or_var_desc(file, current_func,
966 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
967 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
968 *typestring_offset += 2;
969 *typestring_offset += write_pointer_description(file, attrs,
970 type, 0, array, 0, pointer_start_offset);
971 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
972 *typestring_offset += 1;
975 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
976 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
977 *typestring_offset += 2;
984 static const var_t *find_array_or_string_in_struct(const type_t *type)
986 /* last field is the first in the fields linked list */
987 const var_t *last_field = type->fields;
988 if (is_array_type(last_field->attrs, last_field->ptr_level, last_field->array))
991 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
992 (last_field->type->type == RPC_FC_CPSTRUCT) ||
993 (last_field->type->type == RPC_FC_CVSTRUCT));
995 return find_array_or_string_in_struct(last_field->type);
998 static size_t write_struct_members(FILE *file, const type_t *type)
1000 size_t typestring_size = 0;
1003 field = type->fields;
1004 while (NEXT_LINK(field)) field = NEXT_LINK(field);
1005 for (; field; field = PREV_LINK(field))
1007 if (is_base_type(field->type->type))
1009 switch (field->type->type)
1011 #define CASE_BASETYPE(fctype) \
1012 case RPC_##fctype: \
1013 print_file(file, 2, "0x%02x,\t\t/* " #fctype " */\n", RPC_##fctype); \
1014 typestring_size++; \
1016 CASE_BASETYPE(FC_BYTE);
1017 CASE_BASETYPE(FC_CHAR);
1018 CASE_BASETYPE(FC_SMALL);
1019 CASE_BASETYPE(FC_USMALL);
1020 CASE_BASETYPE(FC_WCHAR);
1021 CASE_BASETYPE(FC_SHORT);
1022 CASE_BASETYPE(FC_USHORT);
1023 CASE_BASETYPE(FC_LONG);
1024 CASE_BASETYPE(FC_ULONG);
1025 CASE_BASETYPE(FC_FLOAT);
1026 CASE_BASETYPE(FC_HYPER);
1027 CASE_BASETYPE(FC_DOUBLE);
1028 CASE_BASETYPE(FC_ENUM16);
1029 CASE_BASETYPE(FC_ENUM32);
1030 CASE_BASETYPE(FC_IGNORE);
1031 CASE_BASETYPE(FC_ERROR_STATUS_T);
1034 #undef CASE_BASETYPE
1038 error("Unsupported member type 0x%x\n", field->type->type);
1041 if (typestring_size % 1)
1043 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1047 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1050 return typestring_size;
1053 static size_t write_struct_tfs(FILE *file, const type_t *type,
1054 const char *name, unsigned int *typestring_offset)
1056 unsigned int total_size;
1058 size_t start_offset;
1059 size_t array_offset;
1060 size_t pointer_offset;
1065 case RPC_FC_PSTRUCT:
1066 total_size = type_memsize(type, 0, NULL);
1068 if (total_size > USHRT_MAX)
1069 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1070 name, USHRT_MAX, total_size - USHRT_MAX);
1072 if (type->type == RPC_FC_PSTRUCT)
1074 pointer_offset = *typestring_offset;
1075 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1077 else pointer_offset = 0; /* silence warning */
1079 start_offset = *typestring_offset;
1080 if (type->type == RPC_FC_STRUCT)
1081 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1083 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1085 print_file(file, 2, "0x3,\n"); /* FIXME */
1087 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1088 *typestring_offset += 4;
1090 if (type->type == RPC_FC_PSTRUCT)
1092 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1093 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1094 *typestring_offset += 2;
1095 *typestring_offset += write_pointer_description(file, NULL,
1096 type, 0, NULL, 0, pointer_offset);
1097 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1098 *typestring_offset += 1;
1102 *typestring_offset += write_struct_members(file, type);
1103 return start_offset;
1104 case RPC_FC_CSTRUCT:
1105 case RPC_FC_CPSTRUCT:
1106 total_size = type_memsize(type, 0, NULL);
1108 if (total_size > USHRT_MAX)
1109 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1110 name, USHRT_MAX, total_size - USHRT_MAX);
1112 array = find_array_or_string_in_struct(type);
1113 current_structure = type;
1114 array_offset = write_array_tfs(file, array->attrs, array->type,
1115 array->array, array->name,
1117 current_structure = NULL;
1119 if (type->type == RPC_FC_CPSTRUCT)
1121 pointer_offset = *typestring_offset;
1122 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1124 else pointer_offset = 0; /* silence warning */
1126 start_offset = *typestring_offset;
1127 if (type->type == RPC_FC_CSTRUCT)
1128 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1130 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1132 print_file(file, 2, "0x0,\n");
1134 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1135 *typestring_offset += 4;
1136 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1137 array_offset - *typestring_offset,
1138 array_offset - *typestring_offset,
1140 *typestring_offset += 2;
1142 if (type->type == RPC_FC_CPSTRUCT)
1144 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1145 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1146 *typestring_offset += 2;
1147 *typestring_offset += write_pointer_description(file, NULL,
1148 type, 0, NULL, 0, pointer_offset);
1149 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1150 *typestring_offset += 1;
1153 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1154 *typestring_offset += 1;
1156 return start_offset;
1157 case RPC_FC_CVSTRUCT:
1158 total_size = type_memsize(type, 0, NULL);
1160 if (total_size > USHRT_MAX)
1161 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1162 name, USHRT_MAX, total_size - USHRT_MAX);
1164 array = find_array_or_string_in_struct(type);
1165 current_structure = type;
1166 if (is_attr(array->attrs, ATTR_STRING))
1167 array_offset = write_string_tfs(file, array->attrs, array->type,
1168 array->array, array->name,
1171 array_offset = write_array_tfs(file, array->attrs, array->type,
1172 array->array, array->name,
1174 current_structure = NULL;
1176 pointer_offset = *typestring_offset;
1177 if (!write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset))
1180 start_offset = *typestring_offset;
1181 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1183 print_file(file, 2, "0x0,\n");
1185 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1186 *typestring_offset += 4;
1187 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1188 array_offset - *typestring_offset,
1189 array_offset - *typestring_offset,
1191 *typestring_offset += 2;
1193 if (pointer_offset != 0)
1195 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1196 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1197 *typestring_offset += 2;
1198 *typestring_offset += write_pointer_description(file, NULL,
1199 type, 0, NULL, 0, pointer_offset);
1200 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1201 *typestring_offset += 1;
1204 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1205 *typestring_offset += 1;
1207 return start_offset;
1209 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1210 return *typestring_offset;
1214 static void write_pointer_only_tfs(FILE *file, const attr_t *attrs, size_t offset, unsigned int *typeformat_offset)
1216 int in_attr, out_attr;
1217 unsigned char flags = 0;
1218 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1219 if (!pointer_type) pointer_type = RPC_FC_RP;
1220 in_attr = is_attr(attrs, ATTR_IN);
1221 out_attr = is_attr(attrs, ATTR_OUT);
1222 if (!in_attr && !out_attr) in_attr = 1;
1224 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1227 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s%s */\n",
1230 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
1231 (flags & 0x04) ? " [allocated_on_stack]" : "");
1232 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", offset, offset);
1233 *typeformat_offset += 4;
1236 static size_t write_union_tfs(FILE *file, const attr_t *attrs,
1237 const type_t *type, const char *name,
1238 unsigned int *typeformat_offset)
1240 error("write_union_tfs: Unimplemented\n");
1241 return *typeformat_offset;
1244 static size_t write_typeformatstring_var(FILE *file, int indent,
1245 const var_t *var, unsigned int *typeformat_offset)
1247 const type_t *type = var->type;
1248 int ptr_level = var->ptr_level;
1250 chat("write_typeformatstring_var: %s\n", var->name);
1254 chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type->type, type->name, ptr_level);
1256 if (is_string_type(var->attrs, ptr_level, var->array))
1257 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1259 if (is_array_type(var->attrs, ptr_level, var->array))
1260 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1264 /* follow reference if the type has one */
1265 if (type_has_ref(type))
1268 /* FIXME: get new ptr_level from type */
1272 /* basic types don't need a type format string */
1273 if (is_base_type(type->type))
1279 case RPC_FC_PSTRUCT:
1280 case RPC_FC_CSTRUCT:
1281 case RPC_FC_CPSTRUCT:
1282 case RPC_FC_CVSTRUCT:
1283 case RPC_FC_BOGUS_STRUCT:
1284 return write_struct_tfs(file, type, var->name, typeformat_offset);
1285 case RPC_FC_ENCAPSULATED_UNION:
1286 case RPC_FC_NON_ENCAPSULATED_UNION:
1287 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1289 case RPC_FC_BIND_PRIMITIVE:
1293 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1296 else if (ptr_level == 1 && !type_has_ref(type))
1298 size_t start_offset = *typeformat_offset;
1299 int in_attr = is_attr(var->attrs, ATTR_IN);
1300 int out_attr = is_attr(var->attrs, ATTR_OUT);
1301 int pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1302 if (!pointer_type) pointer_type = RPC_FC_RP;
1304 /* special case for pointers to base types */
1307 #define CASE_BASETYPE(fctype) \
1308 case RPC_##fctype: \
1309 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n", \
1311 (!in_attr && out_attr) ? 0x0C : 0x08, \
1312 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1313 (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1314 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
1315 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
1316 *typeformat_offset += 4; \
1318 CASE_BASETYPE(FC_BYTE);
1319 CASE_BASETYPE(FC_CHAR);
1320 CASE_BASETYPE(FC_SMALL);
1321 CASE_BASETYPE(FC_USMALL);
1322 CASE_BASETYPE(FC_WCHAR);
1323 CASE_BASETYPE(FC_SHORT);
1324 CASE_BASETYPE(FC_USHORT);
1325 CASE_BASETYPE(FC_LONG);
1326 CASE_BASETYPE(FC_ULONG);
1327 CASE_BASETYPE(FC_FLOAT);
1328 CASE_BASETYPE(FC_HYPER);
1329 CASE_BASETYPE(FC_DOUBLE);
1330 CASE_BASETYPE(FC_ENUM16);
1331 CASE_BASETYPE(FC_ENUM32);
1332 CASE_BASETYPE(FC_IGNORE);
1333 CASE_BASETYPE(FC_ERROR_STATUS_T);
1339 assert(ptr_level > 0);
1342 fprintf(file, "/* %2u */\n", *typeformat_offset);
1343 write_pointer_only_tfs(file, var->attrs, 2, typeformat_offset);
1350 void write_typeformatstring(FILE *file, const ifref_t *ifaces)
1354 unsigned int typeformat_offset;
1355 const ifref_t *iface = ifaces;
1357 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1358 print_file(file, indent, "{\n");
1360 print_file(file, indent, "0,\n");
1361 print_file(file, indent, "{\n");
1363 print_file(file, indent, "NdrFcShort(0x0),\n");
1364 typeformat_offset = 2;
1368 for (; iface; iface = PREV_LINK(iface))
1370 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1373 if (iface->iface->funcs)
1375 func_t *func = iface->iface->funcs;
1376 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1377 for (; func; func = PREV_LINK(func))
1379 current_func = func;
1383 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1386 write_typeformatstring_var(file, indent, var,
1387 &typeformat_offset);
1388 var = PREV_LINK(var);
1395 print_file(file, indent, "0x0\n");
1397 print_file(file, indent, "}\n");
1399 print_file(file, indent, "};\n");
1400 print_file(file, indent, "\n");
1403 static unsigned int get_required_buffer_size_type(
1404 const type_t *type, int ptr_level, const expr_t *array,
1405 const char *name, unsigned int *alignment)
1408 if (ptr_level == 0 && !array && !type_has_ref(type))
1428 case RPC_FC_ERROR_STATUS_T:
1438 case RPC_FC_BIND_PRIMITIVE:
1445 for (field = type->fields; field; field = NEXT_LINK(field))
1447 unsigned int alignment;
1448 size += get_required_buffer_size_type(
1449 field->type, field->ptr_level, field->array, field->name,
1456 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1460 if (ptr_level == 0 && type_has_ref(type))
1461 return get_required_buffer_size_type(type->ref, 0 /* FIXME */, array, name, alignment);
1465 unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1467 expr_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1468 int has_size = (size_is && (size_is->type != EXPR_VOID));
1469 int in_attr = is_attr(var->attrs, ATTR_IN);
1470 int out_attr = is_attr(var->attrs, ATTR_OUT);
1472 if (!in_attr && !out_attr)
1477 if (pass == PASS_OUT)
1479 if (out_attr && var->ptr_level > 0)
1481 type_t *type = var->type;
1482 while (type->type == 0 && type->ref)
1485 if (type->type == RPC_FC_STRUCT)
1488 unsigned int size = 36;
1489 for (field = type->fields; field; field = NEXT_LINK(field))
1492 size += get_required_buffer_size_type(
1493 field->type, field->ptr_level, field->array, field->name,
1503 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1505 if (var->ptr_level > 0 || (var->ptr_level == 0 && type_has_ref(var->type)))
1507 type_t *type = var->type;
1508 while (type->type == 0 && type->ref)
1511 if (is_base_type(type->type))
1515 else if (type->type == RPC_FC_STRUCT)
1517 unsigned int size = 36;
1519 for (field = type->fields; field; field = NEXT_LINK(field))
1522 size += get_required_buffer_size_type(
1523 field->type, field->ptr_level, field->array, field->name,
1531 return get_required_buffer_size_type(var->type, var->ptr_level, var->array, var->name, alignment);
1535 static void print_phase_function(FILE *file, int indent, const char *type,
1536 enum remoting_phase phase,
1537 const char *varname, unsigned int type_offset)
1539 const char *function;
1542 case PHASE_BUFFERSIZE:
1543 function = "BufferSize";
1546 function = "Marshall";
1548 case PHASE_UNMARSHAL:
1549 function = "Unmarshall";
1559 print_file(file, indent, "Ndr%s%s(\n", type, function);
1561 print_file(file, indent, "&_StubMsg,\n");
1562 print_file(file, indent, "%s%s,\n",
1563 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1565 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1566 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1567 if (phase == PHASE_UNMARSHAL)
1568 print_file(file, indent, "0);\n");
1572 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1573 enum pass pass, const var_t *var,
1574 const char *varname)
1576 const type_t *type = var->type;
1578 unsigned int alignment = 0;
1580 /* no work to do for other phases, buffer sizing is done elsewhere */
1581 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1584 while (type_has_ref(type))
1607 case RPC_FC_ERROR_STATUS_T:
1619 case RPC_FC_BIND_PRIMITIVE:
1620 /* no marshalling needed */
1624 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, type->type);
1628 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1629 alignment - 1, alignment - 1);
1631 if (phase == PHASE_MARSHAL)
1633 print_file(file, indent, "*(");
1634 write_type(file, var->type, NULL, var->tname);
1636 fprintf(file, " *)_StubMsg.Buffer = *");
1638 fprintf(file, " *)_StubMsg.Buffer = ");
1639 fprintf(file, varname);
1640 fprintf(file, ";\n");
1642 else if (phase == PHASE_UNMARSHAL)
1644 if (pass == PASS_IN || pass == PASS_RETURN)
1645 print_file(file, indent, "");
1647 print_file(file, indent, "*");
1648 fprintf(file, varname);
1649 if (pass == PASS_IN && var->ptr_level)
1650 fprintf(file, " = (");
1652 fprintf(file, " = *(");
1653 write_type(file, var->type, NULL, var->tname);
1654 fprintf(file, " *)_StubMsg.Buffer;\n");
1657 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1658 write_type(file, var->type, NULL, var->tname);
1659 fprintf(file, ");\n");
1662 /* returns whether the MaxCount, Offset or ActualCount members need to be
1663 * filled in for the specified phase */
1664 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1666 return (phase != PHASE_UNMARSHAL);
1669 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1670 unsigned int *type_offset, enum pass pass,
1671 enum remoting_phase phase)
1673 const expr_t *length_is;
1674 const expr_t *size_is;
1675 int in_attr, out_attr, has_length, has_size, pointer_type;
1682 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1683 for (; var; *type_offset += get_size_typeformatstring_var(var), var = PREV_LINK(var))
1685 const type_t *type = var->type;
1686 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1687 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1688 has_length = length_is && (length_is->type != EXPR_VOID);
1689 has_size = (size_is && (size_is->type != EXPR_VOID)) || (var->array && !var->array->is_const);
1691 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1693 pointer_type = RPC_FC_RP;
1695 in_attr = is_attr(var->attrs, ATTR_IN);
1696 out_attr = is_attr(var->attrs, ATTR_OUT);
1697 if (!in_attr && !out_attr)
1714 while (type_has_ref(type))
1717 if (is_string_type(var->attrs, var->ptr_level, var->array))
1719 if (var->array && var->array->is_const)
1720 print_phase_function(file, indent, "NonConformantString", phase, var->name, *type_offset);
1723 if (size_is && is_size_needed_for_phase(phase))
1725 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1726 write_expr(file, size_is, 1);
1727 fprintf(file, ";\n");
1730 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1731 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1733 print_phase_function(file, indent, "ConformantString", phase, var->name,
1734 *type_offset + (has_size ? 4 : 2));
1737 else if (is_array_type(var->attrs, var->ptr_level, var->array))
1739 const char *array_type;
1741 if (var->array && NEXT_LINK(var->array)) /* multi-dimensional array */
1742 array_type = "ComplexArray";
1745 if (!has_length && !has_size)
1746 array_type = "FixedArray";
1747 else if (has_length && !has_size)
1749 if (is_size_needed_for_phase(phase))
1751 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1752 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1753 write_expr(file, length_is, 1);
1754 fprintf(file, ";\n\n");
1756 array_type = "VaryingArray";
1758 else if (!has_length && has_size)
1760 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
1762 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1763 write_expr(file, size_is ? size_is : var->array, 1);
1764 fprintf(file, ";\n\n");
1766 array_type = "ConformantArray";
1770 if (is_size_needed_for_phase(phase))
1772 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1773 write_expr(file, size_is ? size_is : var->array, 1);
1774 fprintf(file, ";\n");
1775 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1776 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1777 write_expr(file, length_is, 1);
1778 fprintf(file, ";\n\n");
1780 array_type = "ConformantVaryingArray";
1784 if (!in_attr && phase == PHASE_FREE)
1786 print_file(file, indent, "if (%s)\n", var->name);
1788 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
1790 else if (phase != PHASE_FREE)
1792 if (pointer_type == RPC_FC_UP)
1793 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1795 print_phase_function(file, indent, array_type, phase, var->name, *type_offset + 4);
1798 else if (var->ptr_level == 0 && is_base_type(type->type))
1800 print_phase_basetype(file, indent, phase, pass, var, var->name);
1802 else if (var->ptr_level == 0)
1804 const char *ndrtype;
1809 ndrtype = "SimpleStruct";
1811 case RPC_FC_CSTRUCT:
1812 case RPC_FC_CPSTRUCT:
1813 ndrtype = "ConformantStruct";
1815 case RPC_FC_CVSTRUCT:
1816 ndrtype = "ConformantVaryingStruct";
1818 case RPC_FC_BOGUS_STRUCT:
1819 ndrtype = "ComplexStruct";
1822 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1823 var->name, type->type, var->ptr_level);
1827 print_phase_function(file, indent, ndrtype, phase, var->name, *type_offset);
1831 if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && is_base_type(type->type))
1833 print_phase_basetype(file, indent, phase, pass, var, var->name);
1835 else if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && (type->type == RPC_FC_STRUCT))
1837 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
1838 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
1842 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1845 fprintf(file, "\n");
1850 size_t get_size_procformatstring_var(const var_t *var)
1852 unsigned int type_offset = 2;
1853 return write_procformatstring_var(NULL, 0, var, FALSE, &type_offset);
1857 size_t get_size_typeformatstring_var(const var_t *var)
1859 unsigned int type_offset = 0;
1860 write_typeformatstring_var(NULL, 0, var, &type_offset);
1864 size_t get_size_procformatstring(const ifref_t *ifaces)
1866 const ifref_t *iface = ifaces;
1873 for (; iface; iface = PREV_LINK(iface))
1875 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1878 if (iface->iface->funcs)
1880 func = iface->iface->funcs;
1881 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1884 /* argument list size */
1888 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1891 size += get_size_procformatstring_var(var);
1892 var = PREV_LINK(var);
1897 /* return value size */
1898 if (is_void(var->type, NULL))
1901 size += get_size_procformatstring_var(var);
1903 func = PREV_LINK(func);
1910 size_t get_size_typeformatstring(const ifref_t *ifaces)
1912 const ifref_t *iface = ifaces;
1919 for (; iface; iface = PREV_LINK(iface))
1921 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1924 if (iface->iface->funcs)
1926 func = iface->iface->funcs;
1927 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1930 /* argument list size */
1934 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1937 size += get_size_typeformatstring_var(var);
1938 var = PREV_LINK(var);
1942 func = PREV_LINK(func);
1949 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
1950 const var_t *fields, const char *structvar)
1956 fprintf(h, "%ld", e->u.lval);
1959 fprintf(h, "0x%lx", e->u.lval);
1961 case EXPR_TRUEFALSE:
1963 fprintf(h, "FALSE");
1967 case EXPR_IDENTIFIER:
1970 for (field = fields; field; field = NEXT_LINK(field))
1972 if (!strcmp(e->u.sval, field->name))
1974 fprintf(h, "%s->%s", structvar, e->u.sval);
1978 if (!field) error("no field found for identifier %s\n", e->u.sval);
1983 write_struct_expr(h, e->ref, 1, fields, structvar);
1987 write_struct_expr(h, e->ref, 1, fields, structvar);
1991 write_struct_expr(h, e->ref, 1, fields, structvar);
1995 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
1997 write_struct_expr(h, e->ref, 1, fields, structvar);
2000 fprintf(h, "sizeof(");
2001 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2012 if (brackets) fprintf(h, "(");
2013 write_struct_expr(h, e->ref, 1, fields, structvar);
2015 case EXPR_SHL: fprintf(h, " << "); break;
2016 case EXPR_SHR: fprintf(h, " >> "); break;
2017 case EXPR_MUL: fprintf(h, " * "); break;
2018 case EXPR_DIV: fprintf(h, " / "); break;
2019 case EXPR_ADD: fprintf(h, " + "); break;
2020 case EXPR_SUB: fprintf(h, " - "); break;
2021 case EXPR_AND: fprintf(h, " & "); break;
2022 case EXPR_OR: fprintf(h, " | "); break;
2025 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2026 if (brackets) fprintf(h, ")");
2029 if (brackets) fprintf(h, "(");
2030 write_struct_expr(h, e->ref, 1, fields, structvar);
2032 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2034 write_struct_expr(h, e->ext2, 1, fields, structvar);
2035 if (brackets) fprintf(h, ")");
2040 int write_expr_eval_routines(FILE *file, const char *iface)
2043 struct expr_eval_routine *eval;
2044 unsigned short callback_offset = 0;
2046 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2050 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2051 iface, eval->structure->name, callback_offset);
2052 print_file(file, indent, "{\n");
2054 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2055 eval->structure->name, eval->structure->name, eval->structure_size);
2056 fprintf(file, "\n");
2057 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2058 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2059 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2060 fprintf(file, ";\n");
2062 print_file(file, indent, "}\n\n");
2068 void write_expr_eval_routine_list(FILE *file, const char *iface)
2070 struct expr_eval_routine *eval;
2071 struct expr_eval_routine *cursor;
2072 unsigned short callback_offset = 0;
2074 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2075 fprintf(file, "{\n");
2077 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2079 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2080 iface, eval->structure->name, callback_offset);
2083 list_remove(&eval->entry);
2087 fprintf(file, "};\n\n");