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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
76 return a->u.lval - b->u.lval;
78 return strcmp(a->u.sval, b->u.sval);
80 ret = compare_expr(a->ref, b->ref);
83 ret = compare_expr(a->u.ext, b->u.ext);
86 return compare_expr(a->ext2, b->ext2);
95 ret = compare_expr(a->ref, b->ref);
98 return compare_expr(a->u.ext, b->u.ext);
104 return compare_expr(a->ref, b->ref);
111 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
114 fprintf(file, "/* %2u */\n", typestring_offset); \
115 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
119 static int print_file(FILE *file, int indent, const char *format, ...)
126 va_start(va, format);
127 for (i = 0; i < indent; i++)
129 r = vfprintf(file, format, va);
134 static inline int type_has_ref(const type_t *type)
136 return (type->type == 0 && type->ref);
139 static inline int is_base_type(unsigned char type)
158 case RPC_FC_ERROR_STATUS_T:
159 case RPC_FC_BIND_PRIMITIVE:
167 static size_t write_procformatstring_var(FILE *file, int indent,
168 const var_t *var, int is_return, unsigned int *type_offset)
171 int ptr_level = var->ptr_level;
172 const type_t *type = var->type;
174 int is_in = is_attr(var->attrs, ATTR_IN);
175 int is_out = is_attr(var->attrs, ATTR_OUT);
177 if (!is_in && !is_out) is_in = TRUE;
179 if (ptr_level == 0 && type_has_ref(type))
182 if (ptr_level == 0 && !var->array && is_base_type(type->type))
185 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
187 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
191 #define CASE_BASETYPE(fctype) \
193 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
194 size = 2; /* includes param type prefix */ \
197 CASE_BASETYPE(FC_BYTE);
198 CASE_BASETYPE(FC_CHAR);
199 CASE_BASETYPE(FC_WCHAR);
200 CASE_BASETYPE(FC_USHORT);
201 CASE_BASETYPE(FC_SHORT);
202 CASE_BASETYPE(FC_ULONG);
203 CASE_BASETYPE(FC_LONG);
204 CASE_BASETYPE(FC_HYPER);
205 CASE_BASETYPE(FC_IGNORE);
206 CASE_BASETYPE(FC_USMALL);
207 CASE_BASETYPE(FC_SMALL);
208 CASE_BASETYPE(FC_FLOAT);
209 CASE_BASETYPE(FC_DOUBLE);
210 CASE_BASETYPE(FC_ERROR_STATUS_T);
213 case RPC_FC_BIND_PRIMITIVE:
214 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
215 size = 2; /* includes param type prefix */
219 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
226 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
227 else if (is_in && is_out)
228 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
230 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
232 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
234 print_file(file, indent, "0x01,\n");
235 print_file(file, indent, "NdrFcShort(0x%x),\n", *type_offset);
236 size = 4; /* includes param type prefix */
238 *type_offset += get_size_typeformatstring_var(var);
242 void write_procformatstring(FILE *file, type_t *iface)
246 unsigned int type_offset = 2;
248 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
249 print_file(file, indent, "{\n");
251 print_file(file, indent, "0,\n");
252 print_file(file, indent, "{\n");
257 func_t *func = iface->funcs;
258 while (NEXT_LINK(func)) func = NEXT_LINK(func);
259 for (; func; func = PREV_LINK(func))
261 /* emit argument data */
265 while (NEXT_LINK(var)) var = NEXT_LINK(var);
268 write_procformatstring_var(file, indent, var, FALSE,
271 var = PREV_LINK(var);
275 /* emit return value data */
277 if (is_void(var->type, NULL))
279 print_file(file, indent, "0x5b, /* FC_END */\n");
280 print_file(file, indent, "0x5c, /* FC_PAD */\n");
283 write_procformatstring_var(file, indent, var, TRUE,
288 print_file(file, indent, "0x0\n");
290 print_file(file, indent, "}\n");
292 print_file(file, indent, "};\n");
293 print_file(file, indent, "\n");
296 /* write conformance / variance descriptor */
297 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_t *expr)
299 unsigned char operator_type = 0;
300 const char *operator_string = "no operators";
301 const expr_t *subexpr = expr;
302 unsigned char correlation_type;
304 if (!file) return 4; /* optimisation for sizing pass */
308 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
309 error("write_conf_or_var_desc: constant value %ld is greater than "
310 "the maximum constant size of %d\n", expr->cval,
311 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
313 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
314 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
315 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
316 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
321 switch (subexpr->type)
324 subexpr = subexpr->ref;
325 operator_type = RPC_FC_DEREFERENCE;
326 operator_string = "FC_DEREFERENCE";
329 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
331 subexpr = subexpr->ref;
332 operator_type = RPC_FC_DIV_2;
333 operator_string = "FC_DIV_2";
337 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
339 subexpr = subexpr->ref;
340 operator_type = RPC_FC_MULT_2;
341 operator_string = "FC_MULT_2";
345 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
347 subexpr = subexpr->ref;
348 operator_type = RPC_FC_SUB_1;
349 operator_string = "FC_SUB_1";
353 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
355 subexpr = subexpr->ref;
356 operator_type = RPC_FC_ADD_1;
357 operator_string = "FC_ADD_1";
364 if (subexpr->type == EXPR_IDENTIFIER)
366 const type_t *correlation_variable = NULL;
367 unsigned char param_type = 0;
368 const char *param_type_string = NULL;
375 for (offset = 0, var = structure->fields; var; var = NEXT_LINK(var))
377 offset -= type_memsize(var->type, var->ptr_level, var->array);
378 if (!strcmp(var->name, subexpr->u.sval))
380 correlation_variable = var->type;
384 if (!correlation_variable)
385 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
388 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
392 const var_t *var = func->args;
394 while (NEXT_LINK(var)) var = NEXT_LINK(var);
395 /* FIXME: not all stack variables are sizeof(void *) */
396 for (offset = 0; var; offset += sizeof(void *), var = PREV_LINK(var))
398 if (!strcmp(var->name, subexpr->u.sval))
400 correlation_variable = var->type;
404 if (!correlation_variable)
405 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
408 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
411 while (type_has_ref(correlation_variable))
412 correlation_variable = correlation_variable->ref;
414 switch (correlation_variable->type)
418 param_type = RPC_FC_SMALL;
419 param_type_string = "FC_SMALL";
423 param_type = RPC_FC_USMALL;
424 param_type_string = "FC_USMALL";
428 param_type = RPC_FC_SHORT;
429 param_type_string = "FC_SHORT";
432 param_type = RPC_FC_USHORT;
433 param_type_string = "FC_USHORT";
436 param_type = RPC_FC_LONG;
437 param_type_string = "FC_LONG";
440 param_type = RPC_FC_ULONG;
441 param_type_string = "FC_ULONG";
444 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
445 correlation_variable->type);
448 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
449 correlation_type | param_type,
450 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
452 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
453 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
455 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
460 unsigned int callback_offset = 0;
464 struct expr_eval_routine *eval;
467 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
469 if (!strcmp(eval->structure->name, structure->name) &&
470 !compare_expr(eval->expr, expr))
480 eval = xmalloc(sizeof(*eval));
481 eval->structure = structure;
482 eval->structure_size = fields_memsize(structure->fields);
484 list_add_tail(&expr_eval_routines, &eval->entry);
487 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
491 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
492 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
495 if (callback_offset > USHRT_MAX)
496 error("Maximum number of callback routines reached\n");
498 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
500 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
501 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
502 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
507 static size_t fields_memsize(const var_t *v)
510 const var_t *first = v;
512 while (NEXT_LINK(v)) v = NEXT_LINK(v);
514 size += type_memsize(v->type, v->ptr_level, v->array);
515 if (v == first) break;
521 static size_t type_memsize(const type_t *t, int ptr_level, const expr_t *array)
526 return sizeof(void *);
529 return type_memsize(t->ref, 0 /* FIXME */, NULL);
547 case RPC_FC_ERROR_STATUS_T:
557 case RPC_FC_CVSTRUCT:
558 case RPC_FC_CPSTRUCT:
561 case RPC_FC_BOGUS_STRUCT:
562 case RPC_FC_ENCAPSULATED_UNION:
563 case RPC_FC_NON_ENCAPSULATED_UNION:
564 size = fields_memsize(t->fields);
567 error("type_memsize: Unknown type %d\n", t->type);
582 size_t get_type_memsize(const type_t *type)
584 return type_memsize(type, 0, NULL);
587 static int write_pointers(FILE *file, const attr_t *attrs,
588 const type_t *type, int ptr_level,
589 const expr_t *array, int level,
590 size_t *typestring_offset)
592 int pointers_written = 0;
595 /* don't generate a pointer for first-level arrays since we want to
596 * descend into them to write their pointers, not stop here */
597 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
599 return write_pointers(file, NULL, type, 0, NULL, level + 1, typestring_offset);
604 /* FIXME: only general algorithm implemented, not the actual writing */
605 error("write_pointers: Writing type format string for pointer is unimplemented\n");
609 /* FIXME: search through all refs for pointers too */
610 while(type_has_ref(type))
615 /* note: don't descend into complex structures or unions since these
616 * will always be generated as a separate type */
618 case RPC_FC_CVSTRUCT:
619 case RPC_FC_CPSTRUCT:
624 while (NEXT_LINK(v)) v = NEXT_LINK(v);
625 for (; v; v = PREV_LINK(v))
626 pointers_written += write_pointers(file, v->attrs, v->type,
627 v->ptr_level, v->array,
638 return pointers_written;
641 static size_t write_pointer_description(FILE *file, const attr_t *attrs,
642 const type_t *type, int ptr_level,
643 const expr_t *array, int level,
644 size_t typestring_offset)
649 /* don't generate a pointer for first-level arrays since we want to
650 * descend into them to write their pointers, not stop here */
651 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
653 return write_pointer_description(file, NULL, type, 0, NULL,
654 level + 1, typestring_offset);
659 /* FIXME: only general algorithm implemented, not the actual writing */
660 error("write_pointer_description: Writing pointer description is unimplemented\n");
664 /* FIXME: search through all refs for pointers too */
668 /* note: don't descend into complex structures or unions since these
669 * will always be generated as a separate type */
671 case RPC_FC_CVSTRUCT:
672 case RPC_FC_CPSTRUCT:
677 while (NEXT_LINK(v)) v = NEXT_LINK(v);
678 for (; v; v = PREV_LINK(v))
679 size += write_pointer_description(file, v->attrs, v->type,
680 v->ptr_level, v->array,
694 static size_t write_string_tfs(FILE *file, const attr_t *attrs,
695 const type_t *type, const expr_t *array,
696 const char *name, size_t *typestring_offset)
698 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
699 int has_size = size_is && (size_is->type != EXPR_VOID);
700 size_t start_offset = *typestring_offset;
701 unsigned char flags = 0;
702 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
704 pointer_type = RPC_FC_RP;
706 if (!get_attrp(attrs, ATTR_SIZEIS))
707 flags |= RPC_FC_P_SIMPLEPOINTER;
709 while (type_has_ref(type))
712 if ((type->type != RPC_FC_BYTE) && (type->type != RPC_FC_CHAR) && (type->type != RPC_FC_WCHAR))
714 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", type->type, name);
718 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
720 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
721 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
722 *typestring_offset += 2;
724 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
726 print_file(file, 2, "NdrFcShort(0x2);\n");
727 *typestring_offset += 2;
730 if (array && array->is_const)
732 if (array->cval > USHRT_MAX)
733 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
734 name, USHRT_MAX, array->cval - USHRT_MAX);
736 if (type->type == RPC_FC_CHAR)
737 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
739 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
740 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
741 *typestring_offset += 2;
743 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", array->cval, array->cval);
744 *typestring_offset += 2;
750 if (type->type == RPC_FC_CHAR)
751 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
753 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
754 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
755 *typestring_offset += 2;
757 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
763 if (type->type == RPC_FC_CHAR)
764 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
766 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
767 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
768 *typestring_offset += 2;
774 static size_t write_array_tfs(FILE *file, const attr_t *attrs,
775 const type_t *type, const expr_t *array,
776 const char *name, size_t *typestring_offset)
778 const expr_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
779 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
780 int has_length = length_is && (length_is->type != EXPR_VOID);
781 int has_size = (size_is && (size_is->type != EXPR_VOID)) || !array->is_const;
784 if (array && NEXT_LINK(array)) /* multi-dimensional array */
786 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
791 size_t pointer_start_offset = *typestring_offset;
794 if (write_pointers(file, attrs, type, 0, array, 0, typestring_offset) > 0)
797 start_offset = *typestring_offset;
799 if (!has_length && !has_size)
802 size_t size = type_memsize(type, 0, array);
803 if (size < USHRT_MAX)
805 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
807 print_file(file, 2, "0x%x, /* 0 */\n", 0);
809 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
810 *typestring_offset += 4;
814 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
816 print_file(file, 2, "0x%x, /* 0 */\n", 0);
818 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
819 *typestring_offset += 6;
824 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
825 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
826 *typestring_offset += 2;
827 *typestring_offset = write_pointer_description(file, attrs,
828 type, 0, array, 0, pointer_start_offset);
829 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
830 *typestring_offset += 1;
833 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
834 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
835 *typestring_offset += 2;
839 else if (has_length && !has_size)
842 size_t element_size = type_memsize(type, 0, NULL);
843 size_t elements = array->cval;
844 size_t total_size = element_size * elements;
846 if (total_size < USHRT_MAX)
848 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
850 print_file(file, 2, "0x%x, /* 0 */\n", 0);
852 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
853 /* number of elements */
854 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
855 *typestring_offset += 6;
859 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
861 print_file(file, 2, "0x%x, /* 0 */\n", 0);
863 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
864 /* number of elements */
865 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
866 *typestring_offset += 10;
869 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
870 *typestring_offset += 2;
872 *typestring_offset += write_conf_or_var_desc(file, current_func,
878 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
879 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
880 *typestring_offset += 2;
881 *typestring_offset += write_pointer_description(file, attrs,
882 type, 0, array, 0, pointer_start_offset);
883 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
884 *typestring_offset += 1;
887 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
888 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
889 *typestring_offset += 2;
893 else if (!has_length && has_size)
895 /* conformant array */
896 size_t element_size = type_memsize(type, 0, NULL);
898 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
900 print_file(file, 2, "0x%x, /* 0 */\n", 0);
902 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
903 *typestring_offset += 4;
905 *typestring_offset += write_conf_or_var_desc(file, current_func,
907 size_is ? size_is : array);
911 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
912 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
913 *typestring_offset += 2;
914 *typestring_offset += write_pointer_description(file, attrs,
915 type, 0, array, 0, pointer_start_offset);
916 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
917 *typestring_offset += 1;
920 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
921 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
922 *typestring_offset += 2;
928 /* conformant varying array */
929 size_t element_size = type_memsize(type, 0, NULL);
931 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
933 print_file(file, 2, "0x%x, /* 0 */\n", 0);
935 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
936 *typestring_offset += 4;
938 *typestring_offset += write_conf_or_var_desc(file, current_func,
940 size_is ? size_is : array);
941 *typestring_offset += write_conf_or_var_desc(file, current_func,
947 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
948 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
949 *typestring_offset += 2;
950 *typestring_offset += write_pointer_description(file, attrs,
951 type, 0, array, 0, pointer_start_offset);
952 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
953 *typestring_offset += 1;
956 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
957 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
958 *typestring_offset += 2;
965 static const var_t *find_array_or_string_in_struct(const type_t *type)
967 /* last field is the first in the fields linked list */
968 const var_t *last_field = type->fields;
969 if (is_array_type(last_field->attrs, last_field->ptr_level, last_field->array))
972 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
973 (last_field->type->type == RPC_FC_CPSTRUCT) ||
974 (last_field->type->type == RPC_FC_CVSTRUCT));
976 return find_array_or_string_in_struct(last_field->type);
979 static size_t write_struct_tfs(FILE *file, const type_t *type,
980 const char *name, size_t *typestring_offset)
986 size_t pointer_offset;
992 total_size = type_memsize(type, 0, NULL);
994 if (total_size > USHRT_MAX)
995 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
996 name, USHRT_MAX, total_size - USHRT_MAX);
998 if (type->type == RPC_FC_PSTRUCT)
1000 pointer_offset = *typestring_offset;
1001 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1003 else pointer_offset = 0; /* silence warning */
1005 start_offset = *typestring_offset;
1006 if (type->type == RPC_FC_STRUCT)
1007 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1009 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1011 print_file(file, 2, "0x0,\n");
1013 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1014 *typestring_offset += 4;
1016 if (type->type == RPC_FC_PSTRUCT)
1018 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1019 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1020 *typestring_offset += 2;
1021 *typestring_offset += write_pointer_description(file, NULL,
1022 type, 0, NULL, 0, pointer_offset);
1023 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1024 *typestring_offset += 1;
1028 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1029 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1031 *typestring_offset += 2;
1032 return start_offset;
1033 case RPC_FC_CSTRUCT:
1034 case RPC_FC_CPSTRUCT:
1035 total_size = type_memsize(type, 0, NULL);
1037 if (total_size > USHRT_MAX)
1038 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1039 name, USHRT_MAX, total_size - USHRT_MAX);
1041 array = find_array_or_string_in_struct(type);
1042 current_structure = type;
1043 array_offset = write_array_tfs(file, array->attrs, array->type,
1044 array->array, array->name,
1046 current_structure = NULL;
1048 if (type->type == RPC_FC_CPSTRUCT)
1050 pointer_offset = *typestring_offset;
1051 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1053 else pointer_offset = 0; /* silence warning */
1055 start_offset = *typestring_offset;
1056 if (type->type == RPC_FC_CSTRUCT)
1057 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1059 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1061 print_file(file, 2, "0x0,\n");
1063 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1064 *typestring_offset += 4;
1065 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1066 array_offset - *typestring_offset,
1067 array_offset - *typestring_offset,
1069 *typestring_offset += 2;
1071 if (type->type == RPC_FC_CPSTRUCT)
1073 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1074 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1075 *typestring_offset += 2;
1076 *typestring_offset += write_pointer_description(file, NULL,
1077 type, 0, NULL, 0, pointer_offset);
1078 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1079 *typestring_offset += 1;
1082 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1083 *typestring_offset += 1;
1085 return start_offset;
1086 case RPC_FC_CVSTRUCT:
1087 total_size = type_memsize(type, 0, NULL);
1089 if (total_size > USHRT_MAX)
1090 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1091 name, USHRT_MAX, total_size - USHRT_MAX);
1093 array = find_array_or_string_in_struct(type);
1094 current_structure = type;
1095 if (is_attr(array->attrs, ATTR_STRING))
1096 array_offset = write_string_tfs(file, array->attrs, array->type,
1097 array->array, array->name,
1100 array_offset = write_array_tfs(file, array->attrs, array->type,
1101 array->array, array->name,
1103 current_structure = NULL;
1105 pointer_offset = *typestring_offset;
1106 if (!write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset))
1109 start_offset = *typestring_offset;
1110 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1112 print_file(file, 2, "0x0,\n");
1114 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1115 *typestring_offset += 4;
1116 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1117 array_offset - *typestring_offset,
1118 array_offset - *typestring_offset,
1120 *typestring_offset += 2;
1122 if (pointer_offset != 0)
1124 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1125 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1126 *typestring_offset += 2;
1127 *typestring_offset += write_pointer_description(file, NULL,
1128 type, 0, NULL, 0, pointer_offset);
1129 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1130 *typestring_offset += 1;
1133 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1134 *typestring_offset += 1;
1136 return start_offset;
1138 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1139 return *typestring_offset;
1143 static void write_pointer_only_tfs(FILE *file, const attr_t *attrs, size_t offset, size_t *typeformat_offset)
1145 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1146 if (!pointer_type) pointer_type = RPC_FC_RP;
1148 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
1150 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
1151 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", offset, offset);
1152 *typeformat_offset += 4;
1155 static size_t write_union_tfs(FILE *file, const attr_t *attrs,
1156 const type_t *type, const char *name,
1157 size_t *typeformat_offset)
1159 error("write_union_tfs: Unimplemented\n");
1160 return *typeformat_offset;
1163 static size_t write_typeformatstring_var(FILE *file, int indent,
1164 const var_t *var, size_t *typeformat_offset)
1166 const type_t *type = var->type;
1167 int ptr_level = var->ptr_level;
1169 chat("write_typeformatstring_var: %s\n", var->name);
1173 chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type->type, type->name, ptr_level);
1175 if (is_string_type(var->attrs, ptr_level, var->array))
1176 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1178 if (is_array_type(var->attrs, ptr_level, var->array))
1179 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1183 /* follow reference if the type has one */
1184 if (type_has_ref(type))
1187 /* FIXME: get new ptr_level from type */
1191 /* basic types don't need a type format string */
1192 if (is_base_type(type->type))
1198 case RPC_FC_PSTRUCT:
1199 case RPC_FC_CSTRUCT:
1200 case RPC_FC_CPSTRUCT:
1201 case RPC_FC_CVSTRUCT:
1202 case RPC_FC_BOGUS_STRUCT:
1203 return write_struct_tfs(file, type, var->name, typeformat_offset);
1204 case RPC_FC_ENCAPSULATED_UNION:
1205 case RPC_FC_NON_ENCAPSULATED_UNION:
1206 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1208 case RPC_FC_BIND_PRIMITIVE:
1212 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1215 else if (ptr_level == 1 && !type_has_ref(type))
1217 size_t start_offset = *typeformat_offset;
1218 int in_attr = is_attr(var->attrs, ATTR_IN);
1219 int out_attr = is_attr(var->attrs, ATTR_OUT);
1220 int pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1221 if (!pointer_type) pointer_type = RPC_FC_RP;
1223 /* special case for pointers to base types */
1226 #define CASE_BASETYPE(fctype) \
1227 case RPC_##fctype: \
1228 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n", \
1230 (!in_attr && out_attr) ? 0x0C : 0x08, \
1231 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1232 (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1233 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
1234 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
1235 *typeformat_offset += 4; \
1237 CASE_BASETYPE(FC_BYTE);
1238 CASE_BASETYPE(FC_CHAR);
1239 CASE_BASETYPE(FC_SMALL);
1240 CASE_BASETYPE(FC_USMALL);
1241 CASE_BASETYPE(FC_WCHAR);
1242 CASE_BASETYPE(FC_SHORT);
1243 CASE_BASETYPE(FC_USHORT);
1244 CASE_BASETYPE(FC_LONG);
1245 CASE_BASETYPE(FC_ULONG);
1246 CASE_BASETYPE(FC_FLOAT);
1247 CASE_BASETYPE(FC_HYPER);
1248 CASE_BASETYPE(FC_DOUBLE);
1249 CASE_BASETYPE(FC_ENUM16);
1250 CASE_BASETYPE(FC_ENUM32);
1251 CASE_BASETYPE(FC_IGNORE);
1252 CASE_BASETYPE(FC_ERROR_STATUS_T);
1258 assert(ptr_level > 0);
1261 fprintf(file, "/* %2u */\n", *typeformat_offset);
1262 write_pointer_only_tfs(file, var->attrs, 2, typeformat_offset);
1269 void write_typeformatstring(FILE *file, type_t *iface)
1273 size_t typeformat_offset;
1275 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1276 print_file(file, indent, "{\n");
1278 print_file(file, indent, "0,\n");
1279 print_file(file, indent, "{\n");
1281 print_file(file, indent, "NdrFcShort(0x0),\n");
1282 typeformat_offset = 2;
1286 func_t *func = iface->funcs;
1287 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1288 for (; func; func = PREV_LINK(func))
1290 current_func = func;
1294 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1297 write_typeformatstring_var(file, indent, var,
1298 &typeformat_offset);
1299 var = PREV_LINK(var);
1305 print_file(file, indent, "0x0\n");
1307 print_file(file, indent, "}\n");
1309 print_file(file, indent, "};\n");
1310 print_file(file, indent, "\n");
1313 static unsigned int get_required_buffer_size_type(
1314 const type_t *type, int ptr_level, const expr_t *array,
1315 const char *name, unsigned int *alignment)
1318 if (ptr_level == 0 && !array && !type_has_ref(type))
1338 case RPC_FC_ERROR_STATUS_T:
1348 case RPC_FC_BIND_PRIMITIVE:
1355 for (field = type->fields; field; field = NEXT_LINK(field))
1357 unsigned int alignment;
1358 size += get_required_buffer_size_type(
1359 field->type, field->ptr_level, field->array, field->name,
1366 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1370 if (ptr_level == 0 && type_has_ref(type))
1371 return get_required_buffer_size_type(type->ref, 0 /* FIXME */, array, name, alignment);
1375 unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment)
1377 int in_attr = is_attr(var->attrs, ATTR_IN);
1378 int out_attr = is_attr(var->attrs, ATTR_OUT);
1380 if (!in_attr && !out_attr)
1383 if ((!out_attr || in_attr) && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1385 if (var->ptr_level > 0 || (var->ptr_level == 0 && type_has_ref(var->type)))
1387 type_t *type = var->type;
1388 while (type->type == 0 && type->ref)
1391 if (is_base_type(type->type))
1396 return get_required_buffer_size_type(var->type, var->ptr_level, var->array, var->name, alignment);
1399 static inline const char *function_from_phase(enum remoting_phase phase)
1403 case PHASE_BUFFERSIZE:
1404 return "BufferSize";
1407 case PHASE_UNMARSHAL:
1408 return "Unmarshall";
1415 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1416 unsigned int *type_offset, enum pass pass,
1417 enum remoting_phase phase)
1419 const expr_t *length_is;
1420 const expr_t *size_is;
1421 int in_attr, out_attr, has_length, has_size, pointer_type;
1428 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1429 for (; var; *type_offset += get_size_typeformatstring_var(var), var = PREV_LINK(var))
1431 const type_t *type = var->type;
1432 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1433 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1434 has_length = length_is && (length_is->type != EXPR_VOID);
1435 has_size = (size_is && (size_is->type != EXPR_VOID)) || (var->array && !var->array->is_const);
1437 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1439 pointer_type = RPC_FC_RP;
1441 in_attr = is_attr(var->attrs, ATTR_IN);
1442 out_attr = is_attr(var->attrs, ATTR_OUT);
1443 if (!in_attr && !out_attr)
1460 while (type_has_ref(type))
1463 if (is_string_type(var->attrs, var->ptr_level, var->array))
1465 if (var->array && var->array->is_const)
1466 print_file(file, indent,
1467 "NdrNonConformantString%s(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1468 function_from_phase(phase), var->name, *type_offset);
1471 if (size_is && phase != PHASE_UNMARSHAL)
1473 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1474 write_expr(file, size_is, 1);
1475 fprintf(file, ";\n");
1478 if (phase == PHASE_FREE)
1480 print_file(file, indent, "NdrPointerFree(\n");
1482 print_file(file, indent, "&_StubMsg,\n");
1483 print_file(file, indent, "(unsigned char *)%s,\n", var->name);
1484 print_file(file, indent, "&__MIDL_TypeFormatString.Format[%d]);\n",
1490 print_file(file, indent, "NdrConformantString%s(\n", function_from_phase(phase));
1492 print_file(file, indent, "(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
1493 print_file(file, indent, "(unsigned char *)%s,\n", var->name);
1494 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1495 *type_offset + (has_size ? 4 : 2),
1496 (phase == PHASE_MARSHAL) ? ");" : ",");
1497 if (phase == PHASE_UNMARSHAL)
1498 print_file(file, indent, "(unsigned char)0);\n");
1503 else if (is_array_type(var->attrs, var->ptr_level, var->array))
1505 const char *array_type;
1507 if (var->array && NEXT_LINK(var->array)) /* multi-dimensional array */
1508 array_type = "ComplexArray";
1511 if (!has_length && !has_size)
1512 array_type = "FixedArray";
1513 else if (has_length && !has_size)
1515 if (phase == PHASE_MARSHAL)
1517 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1518 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1519 write_expr(file, length_is, 1);
1520 fprintf(file, ";\n\n");
1522 array_type = "VaryingArray";
1524 else if (!has_length && has_size)
1526 if (phase == PHASE_MARSHAL)
1528 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1529 write_expr(file, size_is ? size_is : var->array, 1);
1530 fprintf(file, ";\n\n");
1532 array_type = "ConformantArray";
1536 if (phase == PHASE_MARSHAL)
1538 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1539 write_expr(file, size_is ? size_is : var->array, 1);
1540 fprintf(file, ";\n");
1541 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1542 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1543 write_expr(file, length_is, 1);
1544 fprintf(file, ";\n\n");
1546 array_type = "ConformantVaryingArray";
1550 print_file(file, indent,
1551 "Ndr%s%s(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1552 array_type, function_from_phase(phase), var->name,
1555 else if (var->ptr_level == 0 && is_base_type(type->type))
1558 unsigned int alignment = 0;
1579 case RPC_FC_ERROR_STATUS_T:
1591 case RPC_FC_BIND_PRIMITIVE:
1592 /* no marshalling needed */
1596 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, type->type);
1600 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
1602 print_file(file, indent, "_StubMsg.Buffer += (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1603 alignment - 1, alignment - 1);
1605 if (phase == PHASE_MARSHAL)
1607 print_file(file, indent, "*(");
1608 write_type(file, var->type, var, var->tname);
1609 fprintf(file, " *)_StubMsg.Buffer = ");
1610 write_name(file, var);
1611 fprintf(file, ";\n");
1613 else if (phase == PHASE_UNMARSHAL)
1615 print_file(file, indent, "");
1616 write_name(file, var);
1617 fprintf(file, " = *(");
1618 write_type(file, var->type, var, var->tname);
1619 fprintf(file, " *)_StubMsg.Buffer;\n");
1622 error("write_remoting_arguments: Unimplemented for base types for phase %d\n", phase);
1624 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1625 write_type(file, var->type, var, var->tname);
1626 fprintf(file, ");\n");
1629 else if (var->ptr_level == 0)
1631 const char *ndrtype;
1636 ndrtype = "SimpleStruct";
1638 case RPC_FC_CSTRUCT:
1639 case RPC_FC_CPSTRUCT:
1640 ndrtype = "ConformantStruct";
1642 case RPC_FC_CVSTRUCT:
1643 ndrtype = "ConformantVaryingStruct";
1645 case RPC_FC_BOGUS_STRUCT:
1646 ndrtype = "ComplexStruct";
1649 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1650 var->name, type->type, var->ptr_level);
1654 print_file(file, indent,
1655 "Ndr%s%s(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1656 ndrtype, function_from_phase(phase), var->name, *type_offset);
1660 if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && is_base_type(type->type))
1681 case RPC_FC_ERROR_STATUS_T:
1691 case RPC_FC_BIND_PRIMITIVE:
1692 /* no marshalling needed */
1696 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: 1)\n", var->name, type->type);
1700 print_file(file, indent,
1701 "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1702 size - 1, size - 1);
1704 if (phase == PHASE_MARSHAL)
1706 print_file(file, indent, "*(");
1707 write_type(file, var->type, NULL, var->tname);
1708 fprintf(file, " *)_StubMsg.Buffer = *");
1709 write_name(file, var);
1710 fprintf(file, ";\n");
1712 else if (phase == PHASE_UNMARSHAL)
1714 print_file(file, indent, (pass == PASS_IN) ? "" : "*");
1715 write_name(file, var);
1716 fprintf(file, (pass == PASS_IN) ? " = (" : " = *(");
1717 write_type(file, var->type, NULL, var->tname);
1718 fprintf(file, " *)_StubMsg.Buffer;\n");
1721 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1722 write_type(file, var->type, NULL, var->tname);
1723 fprintf(file, ");\n");
1727 print_file(file, indent, "NdrPointer%s(\n", function_from_phase(phase));
1729 print_file(file, indent, "(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
1730 print_file(file, indent, "(unsigned char *)%s,\n", var->name);
1731 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1732 *type_offset, (phase == PHASE_MARSHAL) ? ");" : ",");
1733 if (phase == PHASE_UNMARSHAL)
1734 print_file(file, indent, "(unsigned char *)0);\n");
1738 fprintf(file, "\n");
1743 size_t get_size_procformatstring_var(const var_t *var)
1745 unsigned int type_offset = 2;
1746 return write_procformatstring_var(NULL, 0, var, FALSE, &type_offset);
1750 size_t get_size_typeformatstring_var(const var_t *var)
1752 size_t type_offset = 0;
1753 write_typeformatstring_var(NULL, 0, var, &type_offset);
1757 size_t get_size_procformatstring(const type_t *iface)
1765 func = iface->funcs;
1766 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1769 /* argument list size */
1773 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1776 size += get_size_procformatstring_var(var);
1777 var = PREV_LINK(var);
1781 /* return value size */
1782 size += 2; /* FIXME: determine real size */
1783 func = PREV_LINK(func);
1789 size_t get_size_typeformatstring(const type_t *iface)
1797 func = iface->funcs;
1798 while (NEXT_LINK(func)) func = NEXT_LINK(func);
1801 /* argument list size */
1805 while (NEXT_LINK(var)) var = NEXT_LINK(var);
1808 size += get_size_typeformatstring_var(var);
1809 var = PREV_LINK(var);
1813 func = PREV_LINK(func);
1819 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
1820 const var_t *fields, const char *structvar)
1826 fprintf(h, "%ld", e->u.lval);
1829 fprintf(h, "0x%lx", e->u.lval);
1831 case EXPR_IDENTIFIER:
1834 for (field = fields; field; field = NEXT_LINK(field))
1836 if (!strcmp(e->u.sval, field->name))
1838 fprintf(h, "%s->%s", structvar, e->u.sval);
1842 if (!field) error("no field found for identifier %s\n", e->u.sval);
1847 write_struct_expr(h, e->ref, 1, fields, structvar);
1851 write_struct_expr(h, e->ref, 1, fields, structvar);
1855 write_struct_expr(h, e->ref, 1, fields, structvar);
1859 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
1861 write_struct_expr(h, e->ref, 1, fields, structvar);
1864 fprintf(h, "sizeof(");
1865 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
1876 if (brackets) fprintf(h, "(");
1877 write_struct_expr(h, e->ref, 1, fields, structvar);
1879 case EXPR_SHL: fprintf(h, " << "); break;
1880 case EXPR_SHR: fprintf(h, " >> "); break;
1881 case EXPR_MUL: fprintf(h, " * "); break;
1882 case EXPR_DIV: fprintf(h, " / "); break;
1883 case EXPR_ADD: fprintf(h, " + "); break;
1884 case EXPR_SUB: fprintf(h, " - "); break;
1885 case EXPR_AND: fprintf(h, " & "); break;
1886 case EXPR_OR: fprintf(h, " | "); break;
1889 write_struct_expr(h, e->u.ext, 1, fields, structvar);
1890 if (brackets) fprintf(h, ")");
1893 if (brackets) fprintf(h, "(");
1894 write_struct_expr(h, e->ref, 1, fields, structvar);
1896 write_struct_expr(h, e->u.ext, 1, fields, structvar);
1898 write_struct_expr(h, e->ext2, 1, fields, structvar);
1899 if (brackets) fprintf(h, ")");
1904 int write_expr_eval_routines(FILE *file, const char *iface)
1907 struct expr_eval_routine *eval;
1908 unsigned short callback_offset = 0;
1910 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1914 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
1915 iface, eval->structure->name, callback_offset);
1916 print_file(file, indent, "{\n");
1918 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
1919 eval->structure->name, eval->structure->name, eval->structure_size);
1920 fprintf(file, "\n");
1921 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
1922 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
1923 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
1924 fprintf(file, ";\n");
1926 print_file(file, indent, "}\n\n");
1932 void write_expr_eval_routine_list(FILE *file, const char *iface)
1934 struct expr_eval_routine *eval;
1935 struct expr_eval_routine *cursor;
1936 unsigned short callback_offset = 0;
1938 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
1939 fprintf(file, "{\n");
1941 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
1943 print_file(file, 1, "%s_%sExprEval_%04u,\n",
1944 iface, eval->structure->name, callback_offset);
1947 list_remove(&eval->entry);
1951 fprintf(file, "};\n\n");