widl: Added support for floating-point constants.
[wine/multimedia.git] / tools / widl / typegen.c
blob73a36224bd620d5604359b26710b76c7385ac02b
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include <limits.h>
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "windef.h"
41 #include "wine/list.h"
43 #include "widl.h"
44 #include "typegen.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
56 struct list entry;
57 const type_t *structure;
58 size_t structure_size;
59 const expr_t *expr;
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)
69 switch (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";
106 default:
107 error("string_of_type: unknown type 0x%02x\n", type);
108 return NULL;
112 int is_struct(unsigned char type)
114 switch (type)
116 case RPC_FC_STRUCT:
117 case RPC_FC_PSTRUCT:
118 case RPC_FC_CSTRUCT:
119 case RPC_FC_CPSTRUCT:
120 case RPC_FC_CVSTRUCT:
121 case RPC_FC_BOGUS_STRUCT:
122 return 1;
123 default:
124 return 0;
128 static int is_non_complex_struct(const type_t *type)
130 switch (type->type)
132 case RPC_FC_STRUCT:
133 case RPC_FC_PSTRUCT:
134 case RPC_FC_CSTRUCT:
135 case RPC_FC_CPSTRUCT:
136 case RPC_FC_CVSTRUCT:
137 return 1;
138 default:
139 return 0;
143 int is_union(unsigned char type)
145 switch (type)
147 case RPC_FC_ENCAPSULATED_UNION:
148 case RPC_FC_NON_ENCAPSULATED_UNION:
149 return 1;
150 default:
151 return 0;
155 static unsigned short user_type_offset(const char *name)
157 user_type_t *ut;
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)
162 return off;
163 ++off;
165 error("user_type_offset: couldn't find type (%s)\n", name);
166 return 0;
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
179 types */
180 if (type->typestring_offset)
181 type->tfswrite = FALSE;
182 else
183 type->typestring_offset = 1;
186 static type_t *get_user_type(const type_t *t, const char **pname)
188 for (;;)
190 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
191 if (ut)
193 if (pname)
194 *pname = t->name;
195 return ut;
198 if (t->kind == TKIND_ALIAS)
199 t = t->orig;
200 else
201 return 0;
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)
219 int ret;
221 if (a->type != b->type)
222 return a->type - b->type;
224 switch (a->type)
226 case EXPR_NUM:
227 case EXPR_HEXNUM:
228 case EXPR_TRUEFALSE:
229 return a->u.lval - b->u.lval;
230 case EXPR_DOUBLE:
231 return a->u.dval - b->u.dval;
232 case EXPR_IDENTIFIER:
233 return strcmp(a->u.sval, b->u.sval);
234 case EXPR_COND:
235 ret = compare_expr(a->ref, b->ref);
236 if (ret != 0)
237 return ret;
238 ret = compare_expr(a->u.ext, b->u.ext);
239 if (ret != 0)
240 return ret;
241 return compare_expr(a->ext2, b->ext2);
242 case EXPR_OR:
243 case EXPR_AND:
244 case EXPR_ADD:
245 case EXPR_SUB:
246 case EXPR_MUL:
247 case EXPR_DIV:
248 case EXPR_SHL:
249 case EXPR_SHR:
250 ret = compare_expr(a->ref, b->ref);
251 if (ret != 0)
252 return ret;
253 return compare_expr(a->u.ext, b->u.ext);
254 case EXPR_NOT:
255 case EXPR_NEG:
256 case EXPR_PPTR:
257 case EXPR_CAST:
258 case EXPR_SIZEOF:
259 return compare_expr(a->ref, b->ref);
260 case EXPR_VOID:
261 return 0;
263 return -1;
266 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
267 do { \
268 if (file) \
269 fprintf(file, "/* %2u */\n", typestring_offset); \
270 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
272 while (0)
274 static void print_file(FILE *file, int indent, const char *format, ...)
276 va_list va;
277 va_start(va, format);
278 print(file, indent, format, va);
279 va_end(va);
282 void print(FILE *file, int indent, const char *format, va_list va)
284 if (file)
286 if (format[0] != '\n')
287 while (0 < indent--)
288 fprintf(file, " ");
289 vfprintf(file, format, va);
293 void write_parameters_init(FILE *file, int indent, const func_t *func)
295 const var_t *var;
297 if (!func->args)
298 return;
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);
310 fprintf(file, "\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));
331 fprintf(f, "\n");
332 write_formatdesc(f, indent, "TYPE");
333 write_formatdesc(f, indent, "PROC");
334 fprintf(f, "\n");
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)
342 switch (type)
344 case RPC_FC_BYTE:
345 case RPC_FC_CHAR:
346 case RPC_FC_USMALL:
347 case RPC_FC_SMALL:
348 case RPC_FC_WCHAR:
349 case RPC_FC_USHORT:
350 case RPC_FC_SHORT:
351 case RPC_FC_ULONG:
352 case RPC_FC_LONG:
353 case RPC_FC_HYPER:
354 case RPC_FC_IGNORE:
355 case RPC_FC_FLOAT:
356 case RPC_FC_DOUBLE:
357 case RPC_FC_ENUM16:
358 case RPC_FC_ENUM32:
359 case RPC_FC_ERROR_STATUS_T:
360 case RPC_FC_BIND_PRIMITIVE:
361 return TRUE;
363 default:
364 return FALSE;
368 int decl_indirect(const type_t *t)
370 return is_user_type(t)
371 || (!is_base_type(t->type)
372 && !is_ptr(t)
373 && !is_array(t));
376 static size_t write_procformatstring_var(FILE *file, int indent,
377 const var_t *var, int is_return)
379 size_t size;
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))
389 if (is_return)
390 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
391 else
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 */
404 else
406 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
407 size = 0;
410 else
412 if (is_return)
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");
416 else if (is_out)
417 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
418 else
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 */
425 return size;
428 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
430 const ifref_t *iface;
431 int indent = 0;
432 const var_t *var;
434 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
435 print_file(file, indent, "{\n");
436 indent++;
437 print_file(file, indent, "0,\n");
438 print_file(file, indent, "{\n");
439 indent++;
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))
444 continue;
446 if (iface->iface->funcs)
448 const func_t *func;
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 */
453 if (func->args)
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 */
460 var = func->def;
461 if (is_void(var->type))
463 print_file(file, indent, "0x5b, /* FC_END */\n");
464 print_file(file, indent, "0x5c, /* FC_PAD */\n");
466 else
467 write_procformatstring_var(file, indent, var, TRUE);
472 print_file(file, indent, "0x0\n");
473 indent--;
474 print_file(file, indent, "}\n");
475 indent--;
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;
486 return 1;
489 return 0;
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 */
503 if (expr->is_const)
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);
515 return 4;
518 subexpr = expr;
519 switch (subexpr->type)
521 case EXPR_PPTR:
522 subexpr = subexpr->ref;
523 operator_type = RPC_FC_DEREFERENCE;
524 operator_string = "FC_DEREFERENCE";
525 break;
526 case EXPR_DIV:
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";
533 break;
534 case EXPR_MUL:
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";
541 break;
542 case EXPR_SUB:
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";
549 break;
550 case EXPR_ADD:
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";
557 break;
558 default:
559 break;
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;
568 size_t offset;
570 if (structure)
572 const var_t *var;
574 offset = 0;
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;
582 break;
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",
588 subexpr->u.sval);
590 offset -= baseoff;
591 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
593 else
595 const var_t *var;
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;
603 break;
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",
610 subexpr->u.sval);
612 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
615 correlation_variable_type = correlation_variable->type;
617 switch (correlation_variable_type)
619 case RPC_FC_CHAR:
620 case RPC_FC_SMALL:
621 param_type = RPC_FC_SMALL;
622 param_type_string = "FC_SMALL";
623 break;
624 case RPC_FC_BYTE:
625 case RPC_FC_USMALL:
626 param_type = RPC_FC_USMALL;
627 param_type_string = "FC_USMALL";
628 break;
629 case RPC_FC_WCHAR:
630 case RPC_FC_SHORT:
631 case RPC_FC_ENUM16:
632 param_type = RPC_FC_SHORT;
633 param_type_string = "FC_SHORT";
634 break;
635 case RPC_FC_USHORT:
636 param_type = RPC_FC_USHORT;
637 param_type_string = "FC_USHORT";
638 break;
639 case RPC_FC_LONG:
640 case RPC_FC_ENUM32:
641 param_type = RPC_FC_LONG;
642 param_type_string = "FC_LONG";
643 break;
644 case RPC_FC_ULONG:
645 param_type = RPC_FC_ULONG;
646 param_type_string = "FC_ULONG";
647 break;
648 case RPC_FC_RP:
649 case RPC_FC_UP:
650 case RPC_FC_OP:
651 case RPC_FC_FP:
652 if (sizeof(void *) == 4) /* FIXME */
654 param_type = RPC_FC_LONG;
655 param_type_string = "FC_LONG";
657 else
659 param_type = RPC_FC_HYPER;
660 param_type_string = "FC_HYPER";
662 break;
663 default:
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, " : "",
671 param_type_string);
672 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
673 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
674 offset,
675 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
676 offset);
678 else
680 unsigned int callback_offset = 0;
682 if (structure)
684 struct expr_eval_routine *eval;
685 int found = 0;
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))
692 found = 1;
693 break;
695 callback_offset++;
698 if (!found)
700 unsigned int align = 0;
701 eval = xmalloc(sizeof(*eval));
702 eval->structure = structure;
703 eval->structure_size = fields_memsize(structure->fields, &align);
704 eval->expr = expr;
705 list_add_tail(&expr_eval_routines, &eval->entry);
708 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
710 else
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",
720 correlation_type,
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);
725 return 4;
728 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
730 size_t size = 0;
731 const var_t *v;
733 if (!fields) return 0;
734 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
735 size += type_memsize(v->type, align);
737 return size;
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;
744 const var_t *v;
746 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
748 /* we could have an empty default field with NULL type */
749 if (v->type)
751 size = type_memsize(v->type, &align);
752 if (maxs < size) maxs = size;
753 if (*pmaxa < align) *pmaxa = align;
757 return maxs;
760 size_t type_memsize(const type_t *t, unsigned int *align)
762 size_t size = 0;
764 if (t->declarray && is_conformant_array(t))
766 type_memsize(t->ref, align);
767 size = 0;
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)
776 case RPC_FC_BYTE:
777 case RPC_FC_CHAR:
778 case RPC_FC_USMALL:
779 case RPC_FC_SMALL:
780 size = 1;
781 if (size > *align) *align = size;
782 break;
783 case RPC_FC_WCHAR:
784 case RPC_FC_USHORT:
785 case RPC_FC_SHORT:
786 case RPC_FC_ENUM16:
787 size = 2;
788 if (size > *align) *align = size;
789 break;
790 case RPC_FC_ULONG:
791 case RPC_FC_LONG:
792 case RPC_FC_ERROR_STATUS_T:
793 case RPC_FC_ENUM32:
794 case RPC_FC_FLOAT:
795 size = 4;
796 if (size > *align) *align = size;
797 break;
798 case RPC_FC_HYPER:
799 case RPC_FC_DOUBLE:
800 size = 8;
801 if (size > *align) *align = size;
802 break;
803 case RPC_FC_STRUCT:
804 case RPC_FC_CVSTRUCT:
805 case RPC_FC_CPSTRUCT:
806 case RPC_FC_CSTRUCT:
807 case RPC_FC_PSTRUCT:
808 case RPC_FC_BOGUS_STRUCT:
809 size = fields_memsize(t->fields, align);
810 break;
811 case RPC_FC_ENCAPSULATED_UNION:
812 case RPC_FC_NON_ENCAPSULATED_UNION:
813 size = union_memsize(t->fields, align);
814 break;
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);
821 break;
822 default:
823 error("type_memsize: Unknown type %d\n", t->type);
824 size = 0;
827 return size;
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);
840 return 4;
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");
850 return 4;
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);
865 return offset;
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;
877 const char *name;
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);
882 short reloff;
884 guard_rec(type);
886 if (is_base_type(utype->type))
888 absoff = *tfsoff;
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");
892 *tfsoff += 2;
894 else
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)
902 flags = 0x40;
903 else if (utype->type == RPC_FC_UP)
904 flags = 0x80;
905 else
906 flags = 0;
908 start = *tfsoff;
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);
917 *tfsoff += 8;
918 reloff = absoff - *tfsoff;
919 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
920 *tfsoff += 2;
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))
928 size_t absoff;
929 short reloff;
931 if (is_union(type->type) && is_attr(field->attrs, ATTR_SWITCHIS))
933 absoff = *corroff;
934 *corroff += 8;
936 else
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);
947 *tfsoff += 4;
949 else if (is_ptr(type))
951 print_file(file, 2, "0x8,\t/* FC_LONG */\n");
952 *tfsoff += 1;
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);
963 *tfsoff += 1;
965 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
966 *tfsoff += 1;
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;
973 var_t *f;
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);
990 *tfsoff += 8;
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)
1003 int written = 0;
1004 unsigned int align;
1006 if (is_ptr(type))
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);
1018 else
1019 error("write_pointer_description: type format string unknown\n");
1021 align = 0;
1022 *offset_in_memory += type_memsize(type, &align);
1023 /* FIXME: is there a case where these two are different? */
1024 align = 0;
1025 *offset_in_buffer += type_memsize(type, &align);
1027 return 1;
1030 if (is_non_complex_struct(type))
1032 const var_t *v;
1033 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1034 written += write_no_repeat_pointer_descriptions(
1035 file, v->type,
1036 offset_in_memory, offset_in_buffer, typestring_offset);
1038 else
1040 align = 0;
1041 *offset_in_memory += type_memsize(type, &align);
1042 /* FIXME: is there a case where these two are different? */
1043 align = 0;
1044 *offset_in_buffer += type_memsize(type, &align);
1047 return written;
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)
1055 int written = 0;
1056 unsigned int align;
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);
1067 align = 0;
1068 *offset_in_memory += type_memsize(type, &align);
1069 /* FIXME: is there a case where these two are different? */
1070 align = 0;
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);
1077 else
1078 error("write_pointer_description_offsets: type format string unknown\n");
1080 return 1;
1083 if (is_array(type))
1085 return write_pointer_description_offsets(
1086 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1087 typestring_offset);
1089 else if (is_non_complex_struct(type))
1091 /* otherwise search for interesting fields to parse */
1092 const var_t *v;
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,
1097 typestring_offset);
1100 else
1102 align = 0;
1103 if (offset_in_memory)
1104 *offset_in_memory += type_memsize(type, &align);
1105 /* FIXME: is there a case where these two are different? */
1106 align = 0;
1107 if (offset_in_buffer)
1108 *offset_in_buffer += type_memsize(type, &align);
1111 return written;
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)
1121 unsigned int align;
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;
1137 align = 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))
1155 const var_t *v;
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,
1160 typestring_offset);
1163 else
1165 align = 0;
1166 if (offset_in_memory)
1167 *offset_in_memory += type_memsize(type, &align);
1168 /* FIXME: is there a case where these two are different? */
1169 align = 0;
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)
1184 unsigned int align;
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;
1200 align = 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))
1220 const var_t *v;
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,
1225 typestring_offset);
1228 else
1230 align = 0;
1231 if (offset_in_memory)
1232 *offset_in_memory += type_memsize(type, &align);
1233 /* FIXME: is there a case where these two are different? */
1234 align = 0;
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)
1249 unsigned int align;
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;
1267 align = 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))
1287 const var_t *v;
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,
1292 typestring_offset);
1295 else
1297 align = 0;
1298 if (offset_in_memory)
1299 *offset_in_memory += type_memsize(type, &align);
1300 /* FIXME: is there a case where these two are different? */
1301 align = 0;
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
1316 * into arrays) */
1317 offset_in_memory = 0;
1318 offset_in_buffer = 0;
1319 write_no_repeat_pointer_descriptions(
1320 file, type,
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(
1327 file, NULL, type,
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(
1335 file, NULL, type,
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(
1342 file, NULL, type,
1343 &offset_in_memory, &offset_in_buffer, typestring_offset);
1346 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1347 const type_t *type,
1348 const char *name, unsigned int *typestring_offset)
1350 size_t start_offset = *typestring_offset;
1351 unsigned char flags = 0;
1352 int pointer_type;
1353 unsigned char rtype;
1355 if (is_ptr(type))
1357 pointer_type = type->type;
1358 type = type->ref;
1360 else
1361 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1363 if (!pointer_type)
1364 pointer_type = RPC_FC_RP;
1366 if (!get_attrp(attrs, ATTR_SIZEIS))
1367 flags |= RPC_FC_P_SIMPLEPOINTER;
1369 rtype = type->type;
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);
1398 else
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);
1414 else
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)
1423 : 0),
1424 type->size_is);
1426 return start_offset;
1428 else
1430 if (rtype == RPC_FC_CHAR)
1431 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1432 else
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;
1447 size_t size;
1448 size_t start_offset;
1449 int has_pointer;
1450 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1451 if (!pointer_type)
1452 pointer_type = RPC_FC_RP;
1454 has_pointer = FALSE;
1455 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1456 has_pointer = TRUE;
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;
1469 align = 0;
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)
1476 : 0;
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;
1483 else
1485 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1486 *typestring_offset += 2;
1489 if (is_conformant_array(type))
1490 *typestring_offset
1491 += write_conf_or_var_desc(file, current_func, current_structure,
1492 baseoff, size_is);
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;
1504 else
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;
1514 if (length_is)
1515 *typestring_offset
1516 += write_conf_or_var_desc(file, current_func, current_structure,
1517 baseoff, length_is);
1519 if (has_pointer)
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);
1532 else
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))
1544 return last_field;
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);
1548 else
1549 return NULL;
1552 static void write_struct_members(FILE *file, const type_t *type,
1553 unsigned int *corroff, unsigned int *typestring_offset)
1555 const var_t *field;
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;
1572 const var_t *array;
1573 size_t start_offset;
1574 size_t array_offset;
1575 int has_pointers = 0;
1576 unsigned int align = 0;
1577 unsigned int corroff;
1578 var_t *f;
1580 guard_rec(type);
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,
1590 FALSE, tfsoff);
1592 array = find_array_or_string_in_struct(type);
1593 if (array && !processed(array->type))
1594 array_offset
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);
1599 corroff = *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);
1608 *tfsoff += 4;
1610 if (array)
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);
1616 *tfsoff += 2;
1618 else if (type->type == RPC_FC_BOGUS_STRUCT)
1620 print_file(file, 2, "NdrFcShort(0x0),\n");
1621 *tfsoff += 2;
1624 if (type->type == RPC_FC_BOGUS_STRUCT)
1627 print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1628 *tfsoff += 2;
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);
1636 *tfsoff += 2;
1637 write_pointer_description(file, type, tfsoff);
1638 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1639 *tfsoff += 1;
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)
1660 flags |= 0x04;
1662 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1663 pointer_type,
1664 flags,
1665 string_of_type(pointer_type));
1666 if (file)
1668 if (flags & 0x04)
1669 fprintf(file, " [allocated_on_stack]");
1670 if (flags & 0x10)
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)
1683 if (t == NULL)
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);
1698 else
1699 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1701 *tfsoff += 2;
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);
1709 var_list_t *fields;
1710 size_t nbranch = 0;
1711 type_t *deftype = NULL;
1712 short nodeftype = 0xffff;
1713 var_t *f;
1715 guard_rec(type);
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;
1722 else
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);
1728 if (cases)
1729 nbranch += list_count(cases);
1730 if (f->type)
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;
1742 switch (st->type)
1744 case RPC_FC_CHAR:
1745 case RPC_FC_SMALL:
1746 case RPC_FC_USMALL:
1747 case RPC_FC_SHORT:
1748 case RPC_FC_USHORT:
1749 case RPC_FC_LONG:
1750 case RPC_FC_ULONG:
1751 case RPC_FC_ENUM16:
1752 case RPC_FC_ENUM32:
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));
1756 *tfsoff += 2;
1757 break;
1758 default:
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);
1764 *tfsoff += 4;
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);
1771 expr_t *c;
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);
1782 *tfsoff += 4;
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
1788 do. */
1789 if (deflt)
1791 deftype = ft;
1792 nodeftype = 0;
1796 if (deftype)
1798 write_branch_type(file, deftype, tfsoff);
1800 else
1802 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1803 *tfsoff += 2;
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)
1812 size_t i;
1813 size_t start_offset = *typeformat_offset;
1814 const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1816 if (iid)
1818 expr_t expr;
1820 expr.type = EXPR_IDENTIFIER;
1821 expr.ref = NULL;
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;
1828 else
1830 const type_t *base = is_ptr(type) ? type->ref : type;
1831 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1833 if (! 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]);
1846 if (file)
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)
1856 while (TRUE)
1858 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1859 if (ptr_attr)
1860 return ptr_attr;
1861 if (t->kind != TKIND_ALIAS)
1862 return def_type;
1863 t = t->orig;
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)
1871 int pointer_type;
1872 size_t 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;
1891 else
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);
1897 if (is_array(type))
1898 return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1900 if (!is_ptr(type))
1902 /* basic types don't need a type format string */
1903 if (is_base_type(type->type))
1904 return 0;
1906 switch (type->type)
1908 case RPC_FC_STRUCT:
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);
1918 case RPC_FC_IGNORE:
1919 case RPC_FC_BIND_PRIMITIVE:
1920 /* nothing to do */
1921 return 0;
1922 default:
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);
1955 if (file)
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;
1970 if (utype)
1971 set_tfswrite(utype, val);
1973 if (type->kind == TKIND_ALIAS)
1974 type = type->orig;
1975 else if (is_ptr(type) || is_array(type))
1976 type = type->ref;
1977 else
1979 if (type->fields)
1981 var_t *v;
1982 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1983 if (v->type)
1984 set_tfswrite(v->type, val);
1987 return;
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)
1995 int retmask = 0;
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);
2009 else
2011 if (!processed(ref) && !is_base_type(ref->type))
2012 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2014 if (write_ptr)
2015 write_pointer_tfs(file, type, tfsoff);
2017 retmask |= 1;
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",
2038 name, type->type);
2040 return retmask;
2043 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2045 const ifref_t * iface;
2046 const func_t *func;
2047 const var_t *var;
2049 if (ifaces)
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 )
2053 if (func->args)
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)
2060 const var_t *var;
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))
2067 continue;
2069 if (iface->iface->funcs)
2071 const func_t *func;
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;
2077 if (func->args)
2078 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2079 update_tfsoff(
2080 var->type,
2081 write_typeformatstring_var(
2082 file, 2, func, var->type, var,
2083 &typeformat_offset),
2084 file);
2089 return typeformat_offset + 1;
2093 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2095 int indent = 0;
2097 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2098 print_file(file, indent, "{\n");
2099 indent++;
2100 print_file(file, indent, "0,\n");
2101 print_file(file, indent, "{\n");
2102 indent++;
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");
2109 indent--;
2110 print_file(file, indent, "}\n");
2111 indent--;
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)
2119 size_t size = 0;
2121 *alignment = 0;
2122 if (is_user_type(type))
2124 const char *uname;
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))
2130 switch (type->type)
2132 case RPC_FC_BYTE:
2133 case RPC_FC_CHAR:
2134 case RPC_FC_USMALL:
2135 case RPC_FC_SMALL:
2136 *alignment = 4;
2137 size = 1;
2138 break;
2140 case RPC_FC_WCHAR:
2141 case RPC_FC_USHORT:
2142 case RPC_FC_SHORT:
2143 case RPC_FC_ENUM16:
2144 *alignment = 4;
2145 size = 2;
2146 break;
2148 case RPC_FC_ULONG:
2149 case RPC_FC_LONG:
2150 case RPC_FC_ENUM32:
2151 case RPC_FC_FLOAT:
2152 case RPC_FC_ERROR_STATUS_T:
2153 *alignment = 4;
2154 size = 4;
2155 break;
2157 case RPC_FC_HYPER:
2158 case RPC_FC_DOUBLE:
2159 *alignment = 8;
2160 size = 8;
2161 break;
2163 case RPC_FC_IGNORE:
2164 case RPC_FC_BIND_PRIMITIVE:
2165 return 0;
2167 case RPC_FC_STRUCT:
2168 case RPC_FC_PSTRUCT:
2170 const var_t *field;
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,
2176 &alignment);
2178 break;
2181 case RPC_FC_RP:
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 );
2184 break;
2186 case RPC_FC_SMFARRAY:
2187 case RPC_FC_LGFARRAY:
2188 size = type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2189 break;
2191 case RPC_FC_SMVARRAY:
2192 case RPC_FC_LGVARRAY:
2193 get_required_buffer_size_type(type->ref, name, alignment);
2194 size = 0;
2195 break;
2197 case RPC_FC_CARRAY:
2198 case RPC_FC_CVARRAY:
2199 get_required_buffer_size_type(type->ref, name, alignment);
2200 size = sizeof(void *);
2201 break;
2203 default:
2204 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
2205 return 0;
2208 return size;
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)
2217 in_attr = 1;
2219 *alignment = 0;
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)
2229 const var_t *field;
2230 unsigned int size = 36;
2232 if (!type->fields) return size;
2233 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2235 unsigned int align;
2236 size += get_required_buffer_size_type(
2237 field->type, field->name, &align);
2239 return size;
2242 return 0;
2244 else
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))
2255 return 25;
2257 else if (type->type == RPC_FC_STRUCT)
2259 unsigned int size = 36;
2260 const var_t *field;
2262 if (!type->fields) return size;
2263 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2265 unsigned int align;
2266 size += get_required_buffer_size_type(
2267 field->type, field->name, &align);
2269 return size;
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 )
2280 const var_t *var;
2281 unsigned int total_size = 0, alignment;
2283 if (func->args)
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;
2297 return total_size;
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;
2305 switch (phase)
2307 case PHASE_BUFFERSIZE:
2308 function = "BufferSize";
2309 break;
2310 case PHASE_MARSHAL:
2311 function = "Marshall";
2312 break;
2313 case PHASE_UNMARSHAL:
2314 function = "Unmarshall";
2315 break;
2316 case PHASE_FREE:
2317 function = "Free";
2318 break;
2319 default:
2320 assert(0);
2321 return;
2324 print_file(file, indent, "Ndr%s%s(\n", type, function);
2325 indent++;
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_" : "",
2331 var->name);
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");
2336 indent--;
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;
2344 unsigned int size;
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)
2350 return;
2352 rtype = is_ptr(type) ? type->ref->type : type->type;
2354 switch (rtype)
2356 case RPC_FC_BYTE:
2357 case RPC_FC_CHAR:
2358 case RPC_FC_SMALL:
2359 case RPC_FC_USMALL:
2360 size = 1;
2361 alignment = 1;
2362 break;
2364 case RPC_FC_WCHAR:
2365 case RPC_FC_USHORT:
2366 case RPC_FC_SHORT:
2367 case RPC_FC_ENUM16:
2368 size = 2;
2369 alignment = 2;
2370 break;
2372 case RPC_FC_ULONG:
2373 case RPC_FC_LONG:
2374 case RPC_FC_ENUM32:
2375 case RPC_FC_FLOAT:
2376 case RPC_FC_ERROR_STATUS_T:
2377 size = 4;
2378 alignment = 4;
2379 break;
2381 case RPC_FC_HYPER:
2382 case RPC_FC_DOUBLE:
2383 size = 8;
2384 alignment = 8;
2385 break;
2387 case RPC_FC_IGNORE:
2388 case RPC_FC_BIND_PRIMITIVE:
2389 /* no marshalling needed */
2390 return;
2392 default:
2393 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2394 size = 0;
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);
2404 if (is_ptr(type))
2405 fprintf(file, " *)_StubMsg.Buffer = *");
2406 else
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, "");
2415 else
2416 print_file(file, indent, "*");
2417 fprintf(file, varname);
2418 if (pass == PASS_IN && is_ptr(type))
2419 fprintf(file, " = (");
2420 else
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;
2442 const var_t *var;
2444 if (!func->args)
2445 return;
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);
2460 if (!pointer_type)
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)
2466 in_attr = 1;
2468 switch (pass)
2470 case PASS_IN:
2471 if (!in_attr) continue;
2472 break;
2473 case PASS_OUT:
2474 if (!out_attr) continue;
2475 break;
2476 case PASS_RETURN:
2477 break;
2480 rtype = type->type;
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);
2490 else
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);
2501 else
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";
2548 else
2549 array_type = "ComplexArray";
2551 if (!in_attr && phase == PHASE_FREE)
2553 print_file(file, indent, "if (%s)\n", var->name);
2554 indent++;
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);
2561 else
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))
2571 switch (rtype)
2573 case RPC_FC_STRUCT:
2574 case RPC_FC_PSTRUCT:
2575 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2576 break;
2577 case RPC_FC_CSTRUCT:
2578 case RPC_FC_CPSTRUCT:
2579 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2580 break;
2581 case RPC_FC_CVSTRUCT:
2582 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2583 break;
2584 case RPC_FC_BOGUS_STRUCT:
2585 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2586 break;
2587 case RPC_FC_RP:
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);
2597 else
2599 const var_t *iid;
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);
2604 break;
2605 default:
2606 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2609 else
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);
2620 else
2622 const var_t *iid;
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)
2641 const var_t *var;
2642 size_t size = 0;
2644 /* argument list size */
2645 if (func->args)
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 */
2652 else
2653 size += get_size_procformatstring_var(func->def);
2655 return size;
2658 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2660 const ifref_t *iface;
2661 size_t size = 1;
2662 const func_t *func;
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))
2667 continue;
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 );
2674 return size;
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)
2686 switch (e->type) {
2687 case EXPR_VOID:
2688 break;
2689 case EXPR_NUM:
2690 fprintf(h, "%lu", e->u.lval);
2691 break;
2692 case EXPR_HEXNUM:
2693 fprintf(h, "0x%lx", e->u.lval);
2694 break;
2695 case EXPR_DOUBLE:
2696 fprintf(h, "%#.15g", e->u.dval);
2697 break;
2698 case EXPR_TRUEFALSE:
2699 if (e->u.lval == 0)
2700 fprintf(h, "FALSE");
2701 else
2702 fprintf(h, "TRUE");
2703 break;
2704 case EXPR_IDENTIFIER:
2706 const var_t *field;
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);
2711 break;
2714 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2715 break;
2717 case EXPR_NEG:
2718 fprintf(h, "-");
2719 write_struct_expr(h, e->ref, 1, fields, structvar);
2720 break;
2721 case EXPR_NOT:
2722 fprintf(h, "~");
2723 write_struct_expr(h, e->ref, 1, fields, structvar);
2724 break;
2725 case EXPR_PPTR:
2726 fprintf(h, "*");
2727 write_struct_expr(h, e->ref, 1, fields, structvar);
2728 break;
2729 case EXPR_CAST:
2730 fprintf(h, "(");
2731 write_type(h, e->u.tref, FALSE, NULL);
2732 fprintf(h, ")");
2733 write_struct_expr(h, e->ref, 1, fields, structvar);
2734 break;
2735 case EXPR_SIZEOF:
2736 fprintf(h, "sizeof(");
2737 write_type(h, e->u.tref, FALSE, NULL);
2738 fprintf(h, ")");
2739 break;
2740 case EXPR_SHL:
2741 case EXPR_SHR:
2742 case EXPR_MUL:
2743 case EXPR_DIV:
2744 case EXPR_ADD:
2745 case EXPR_SUB:
2746 case EXPR_AND:
2747 case EXPR_OR:
2748 if (brackets) fprintf(h, "(");
2749 write_struct_expr(h, e->ref, 1, fields, structvar);
2750 switch (e->type) {
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;
2759 default: break;
2761 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2762 if (brackets) fprintf(h, ")");
2763 break;
2764 case EXPR_COND:
2765 if (brackets) fprintf(h, "(");
2766 write_struct_expr(h, e->ref, 1, fields, structvar);
2767 fprintf(h, " ? ");
2768 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2769 fprintf(h, " : ");
2770 write_struct_expr(h, e->ext2, 1, fields, structvar);
2771 if (brackets) fprintf(h, ")");
2772 break;
2777 void declare_stub_args( FILE *file, int indent, const func_t *func )
2779 int in_attr, out_attr;
2780 int i = 0;
2781 const var_t *def = func->def;
2782 const var_t *var;
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");
2792 if (!func->args)
2793 return;
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)
2802 in_attr = 1;
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);
2813 fprintf(file, " ");
2814 if (var->type->declarray) {
2815 fprintf(file, "( *");
2816 write_name(file, var);
2817 fprintf(file, " )");
2818 } else
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;
2833 int i = 0, sep = 0;
2834 const var_t *var;
2836 if (!func->args)
2837 return;
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)
2845 in_attr = 1;
2847 if (!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);
2871 i++;
2874 sep = 1;
2877 if (sep)
2878 fprintf(file, "\n");
2882 int write_expr_eval_routines(FILE *file, const char *iface)
2884 int result = 0;
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)
2890 int indent = 0;
2891 result = 1;
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");
2895 indent++;
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");
2903 indent--;
2904 print_file(file, indent, "}\n\n");
2905 callback_offset++;
2907 return result;
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);
2924 callback_offset++;
2925 list_remove(&eval->entry);
2926 free(eval);
2929 fprintf(file, "};\n\n");
2932 void write_user_quad_list(FILE *file)
2934 user_type_t *ut;
2936 if (list_empty(&user_type_list))
2937 return;
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;
2957 const char *p;
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 );
2967 fputc( *p, 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 );
2976 fputc( *p, f );
2978 if (*p != ']') goto error;
2979 fprintf( f, "\" },\n" );
2981 print_file( f, 0, "};\n\n" );
2982 return;
2984 error:
2985 error("Invalid endpoint syntax '%s'\n", endpoint->str);