widl: Handle all buffer sizes.
[wine/hacks.git] / tools / widl / typegen.c
blobd8210658ac2202035835010518d05c2ce72f8acf
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 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
50 struct expr_eval_routine
52 struct list entry;
53 const type_t *structure;
54 const expr_t *expr;
57 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
58 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
59 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
60 const char *name, int write_ptr, unsigned int *tfsoff);
62 const char *string_of_type(unsigned char type)
64 switch (type)
66 case RPC_FC_BYTE: return "FC_BYTE";
67 case RPC_FC_CHAR: return "FC_CHAR";
68 case RPC_FC_SMALL: return "FC_SMALL";
69 case RPC_FC_USMALL: return "FC_USMALL";
70 case RPC_FC_WCHAR: return "FC_WCHAR";
71 case RPC_FC_SHORT: return "FC_SHORT";
72 case RPC_FC_USHORT: return "FC_USHORT";
73 case RPC_FC_LONG: return "FC_LONG";
74 case RPC_FC_ULONG: return "FC_ULONG";
75 case RPC_FC_FLOAT: return "FC_FLOAT";
76 case RPC_FC_HYPER: return "FC_HYPER";
77 case RPC_FC_DOUBLE: return "FC_DOUBLE";
78 case RPC_FC_ENUM16: return "FC_ENUM16";
79 case RPC_FC_ENUM32: return "FC_ENUM32";
80 case RPC_FC_IGNORE: return "FC_IGNORE";
81 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
82 case RPC_FC_RP: return "FC_RP";
83 case RPC_FC_UP: return "FC_UP";
84 case RPC_FC_OP: return "FC_OP";
85 case RPC_FC_FP: return "FC_FP";
86 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
87 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
88 case RPC_FC_STRUCT: return "FC_STRUCT";
89 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
90 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
91 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
92 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
93 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
94 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
95 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
96 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
97 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
98 case RPC_FC_CARRAY: return "FC_CARRAY";
99 case RPC_FC_CVARRAY: return "FC_CVARRAY";
100 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
101 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
102 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
103 case RPC_FC_POINTER: return "FC_POINTER";
104 default:
105 error("string_of_type: unknown type 0x%02x\n", type);
106 return NULL;
110 int is_struct(unsigned char type)
112 switch (type)
114 case RPC_FC_STRUCT:
115 case RPC_FC_PSTRUCT:
116 case RPC_FC_CSTRUCT:
117 case RPC_FC_CPSTRUCT:
118 case RPC_FC_CVSTRUCT:
119 case RPC_FC_BOGUS_STRUCT:
120 return 1;
121 default:
122 return 0;
126 static int is_non_complex_struct(const type_t *type)
128 switch (type->type)
130 case RPC_FC_STRUCT:
131 case RPC_FC_PSTRUCT:
132 case RPC_FC_CSTRUCT:
133 case RPC_FC_CPSTRUCT:
134 case RPC_FC_CVSTRUCT:
135 return 1;
136 default:
137 return 0;
141 int is_union(unsigned char type)
143 switch (type)
145 case RPC_FC_ENCAPSULATED_UNION:
146 case RPC_FC_NON_ENCAPSULATED_UNION:
147 return 1;
148 default:
149 return 0;
153 static unsigned short user_type_offset(const char *name)
155 user_type_t *ut;
156 unsigned short off = 0;
157 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
159 if (strcmp(name, ut->name) == 0)
160 return off;
161 ++off;
163 error("user_type_offset: couldn't find type (%s)\n", name);
164 return 0;
167 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
169 type->typestring_offset = offset;
170 if (file) type->tfswrite = FALSE;
173 static void guard_rec(type_t *type)
175 /* types that contain references to themselves (like a linked list),
176 need to be shielded from infinite recursion when writing embedded
177 types */
178 if (type->typestring_offset)
179 type->tfswrite = FALSE;
180 else
181 type->typestring_offset = 1;
184 static type_t *get_user_type(const type_t *t, const char **pname)
186 for (;;)
188 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
189 if (ut)
191 if (pname)
192 *pname = t->name;
193 return ut;
196 if (t->kind == TKIND_ALIAS)
197 t = t->orig;
198 else
199 return 0;
203 static int is_user_type(const type_t *t)
205 return get_user_type(t, NULL) != NULL;
208 static int is_embedded_complex(const type_t *type)
210 unsigned char tc = type->type;
211 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
212 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
215 static int compare_expr(const expr_t *a, const expr_t *b)
217 int ret;
219 if (a->type != b->type)
220 return a->type - b->type;
222 switch (a->type)
224 case EXPR_NUM:
225 case EXPR_HEXNUM:
226 case EXPR_TRUEFALSE:
227 return a->u.lval - b->u.lval;
228 case EXPR_DOUBLE:
229 return a->u.dval - b->u.dval;
230 case EXPR_IDENTIFIER:
231 return strcmp(a->u.sval, b->u.sval);
232 case EXPR_COND:
233 ret = compare_expr(a->ref, b->ref);
234 if (ret != 0)
235 return ret;
236 ret = compare_expr(a->u.ext, b->u.ext);
237 if (ret != 0)
238 return ret;
239 return compare_expr(a->ext2, b->ext2);
240 case EXPR_OR:
241 case EXPR_AND:
242 case EXPR_ADD:
243 case EXPR_SUB:
244 case EXPR_MUL:
245 case EXPR_DIV:
246 case EXPR_SHL:
247 case EXPR_SHR:
248 ret = compare_expr(a->ref, b->ref);
249 if (ret != 0)
250 return ret;
251 return compare_expr(a->u.ext, b->u.ext);
252 case EXPR_NOT:
253 case EXPR_NEG:
254 case EXPR_PPTR:
255 case EXPR_CAST:
256 case EXPR_SIZEOF:
257 return compare_expr(a->ref, b->ref);
258 case EXPR_VOID:
259 return 0;
261 return -1;
264 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
265 do { \
266 if (file) \
267 fprintf(file, "/* %2u */\n", typestring_offset); \
268 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
270 while (0)
272 static void print_file(FILE *file, int indent, const char *format, ...)
274 va_list va;
275 va_start(va, format);
276 print(file, indent, format, va);
277 va_end(va);
280 void print(FILE *file, int indent, const char *format, va_list va)
282 if (file)
284 if (format[0] != '\n')
285 while (0 < indent--)
286 fprintf(file, " ");
287 vfprintf(file, format, va);
291 void write_parameters_init(FILE *file, int indent, const func_t *func)
293 const var_t *var;
295 if (!func->args)
296 return;
298 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
300 const type_t *t = var->type;
301 const char *n = var->name;
302 if (decl_indirect(t))
303 print_file(file, indent, "MIDL_memset(&%s, 0, sizeof %s);\n", n, n);
304 else if (is_ptr(t) || is_array(t))
305 print_file(file, indent, "%s = 0;\n", n);
308 fprintf(file, "\n");
311 static void write_formatdesc(FILE *f, int indent, const char *str)
313 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
314 print_file(f, indent, "{\n");
315 print_file(f, indent + 1, "short Pad;\n");
316 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
317 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
318 print_file(f, indent, "\n");
321 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
323 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
324 get_size_typeformatstring(ifaces, for_objects));
326 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
327 get_size_procformatstring(ifaces, for_objects));
329 fprintf(f, "\n");
330 write_formatdesc(f, indent, "TYPE");
331 write_formatdesc(f, indent, "PROC");
332 fprintf(f, "\n");
333 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
334 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
335 print_file(f, indent, "\n");
338 static inline int is_base_type(unsigned char type)
340 switch (type)
342 case RPC_FC_BYTE:
343 case RPC_FC_CHAR:
344 case RPC_FC_USMALL:
345 case RPC_FC_SMALL:
346 case RPC_FC_WCHAR:
347 case RPC_FC_USHORT:
348 case RPC_FC_SHORT:
349 case RPC_FC_ULONG:
350 case RPC_FC_LONG:
351 case RPC_FC_HYPER:
352 case RPC_FC_IGNORE:
353 case RPC_FC_FLOAT:
354 case RPC_FC_DOUBLE:
355 case RPC_FC_ENUM16:
356 case RPC_FC_ENUM32:
357 case RPC_FC_ERROR_STATUS_T:
358 case RPC_FC_BIND_PRIMITIVE:
359 return TRUE;
361 default:
362 return FALSE;
366 int decl_indirect(const type_t *t)
368 return is_user_type(t)
369 || (!is_base_type(t->type)
370 && !is_ptr(t)
371 && !is_array(t));
374 static size_t write_procformatstring_var(FILE *file, int indent,
375 const var_t *var, int is_return)
377 size_t size;
378 const type_t *type = var->type;
380 int is_in = is_attr(var->attrs, ATTR_IN);
381 int is_out = is_attr(var->attrs, ATTR_OUT);
383 if (!is_in && !is_out) is_in = TRUE;
385 if (!type->declarray && is_base_type(type->type))
387 if (is_return)
388 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
389 else
390 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
392 if (type->type == RPC_FC_BIND_PRIMITIVE)
394 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
395 size = 2; /* includes param type prefix */
397 else if (is_base_type(type->type))
399 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
400 size = 2; /* includes param type prefix */
402 else
404 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
405 size = 0;
408 else
410 if (is_return)
411 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
412 else if (is_in && is_out)
413 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
414 else if (is_out)
415 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
416 else
417 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
419 print_file(file, indent, "0x01,\n");
420 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
421 size = 4; /* includes param type prefix */
423 return size;
426 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
428 const ifref_t *iface;
429 int indent = 0;
430 const var_t *var;
432 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
433 print_file(file, indent, "{\n");
434 indent++;
435 print_file(file, indent, "0,\n");
436 print_file(file, indent, "{\n");
437 indent++;
439 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
441 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
442 continue;
444 if (iface->iface->funcs)
446 const func_t *func;
447 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
449 if (is_local(func->def->attrs)) continue;
450 /* emit argument data */
451 if (func->args)
453 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
454 write_procformatstring_var(file, indent, var, FALSE);
457 /* emit return value data */
458 var = func->def;
459 if (is_void(var->type))
461 print_file(file, indent, "0x5b, /* FC_END */\n");
462 print_file(file, indent, "0x5c, /* FC_PAD */\n");
464 else
465 write_procformatstring_var(file, indent, var, TRUE);
470 print_file(file, indent, "0x0\n");
471 indent--;
472 print_file(file, indent, "}\n");
473 indent--;
474 print_file(file, indent, "};\n");
475 print_file(file, indent, "\n");
478 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
480 if (is_base_type(type->type))
482 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
483 *typestring_offset += 1;
484 return 1;
487 return 0;
490 /* write conformance / variance descriptor */
491 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
492 unsigned int baseoff, const expr_t *expr)
494 unsigned char operator_type = 0;
495 const char *operator_string = "no operators";
496 const expr_t *subexpr;
498 if (!expr)
500 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
501 return 4;
504 if (!structure)
506 /* Top-level conformance calculations are done inline. */
507 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
508 RPC_FC_TOP_LEVEL_CONFORMANCE);
509 print_file (file, 2, "0x0,\n");
510 print_file (file, 2, "NdrFcShort(0x0),\n");
511 return 4;
514 if (expr->is_const)
516 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
517 error("write_conf_or_var_desc: constant value %ld is greater than "
518 "the maximum constant size of %d\n", expr->cval,
519 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
521 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
522 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
523 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
524 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
526 return 4;
529 subexpr = expr;
530 switch (subexpr->type)
532 case EXPR_PPTR:
533 subexpr = subexpr->ref;
534 operator_type = RPC_FC_DEREFERENCE;
535 operator_string = "FC_DEREFERENCE";
536 break;
537 case EXPR_DIV:
538 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
540 subexpr = subexpr->ref;
541 operator_type = RPC_FC_DIV_2;
542 operator_string = "FC_DIV_2";
544 break;
545 case EXPR_MUL:
546 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
548 subexpr = subexpr->ref;
549 operator_type = RPC_FC_MULT_2;
550 operator_string = "FC_MULT_2";
552 break;
553 case EXPR_SUB:
554 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
556 subexpr = subexpr->ref;
557 operator_type = RPC_FC_SUB_1;
558 operator_string = "FC_SUB_1";
560 break;
561 case EXPR_ADD:
562 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
564 subexpr = subexpr->ref;
565 operator_type = RPC_FC_ADD_1;
566 operator_string = "FC_ADD_1";
568 break;
569 default:
570 break;
573 if (subexpr->type == EXPR_IDENTIFIER)
575 const type_t *correlation_variable = NULL;
576 unsigned char correlation_variable_type;
577 unsigned char param_type = 0;
578 const char *param_type_string = NULL;
579 size_t offset = 0;
580 const var_t *var;
582 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
584 unsigned int align = 0;
585 /* FIXME: take alignment into account */
586 if (var->name && !strcmp(var->name, subexpr->u.sval))
588 correlation_variable = var->type;
589 break;
591 offset += type_memsize(var->type, &align);
593 if (!correlation_variable)
594 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
595 subexpr->u.sval);
597 offset -= baseoff;
598 correlation_variable_type = correlation_variable->type;
600 switch (correlation_variable_type)
602 case RPC_FC_CHAR:
603 case RPC_FC_SMALL:
604 param_type = RPC_FC_SMALL;
605 param_type_string = "FC_SMALL";
606 break;
607 case RPC_FC_BYTE:
608 case RPC_FC_USMALL:
609 param_type = RPC_FC_USMALL;
610 param_type_string = "FC_USMALL";
611 break;
612 case RPC_FC_WCHAR:
613 case RPC_FC_SHORT:
614 case RPC_FC_ENUM16:
615 param_type = RPC_FC_SHORT;
616 param_type_string = "FC_SHORT";
617 break;
618 case RPC_FC_USHORT:
619 param_type = RPC_FC_USHORT;
620 param_type_string = "FC_USHORT";
621 break;
622 case RPC_FC_LONG:
623 case RPC_FC_ENUM32:
624 param_type = RPC_FC_LONG;
625 param_type_string = "FC_LONG";
626 break;
627 case RPC_FC_ULONG:
628 param_type = RPC_FC_ULONG;
629 param_type_string = "FC_ULONG";
630 break;
631 case RPC_FC_RP:
632 case RPC_FC_UP:
633 case RPC_FC_OP:
634 case RPC_FC_FP:
635 if (sizeof(void *) == 4) /* FIXME */
637 param_type = RPC_FC_LONG;
638 param_type_string = "FC_LONG";
640 else
642 param_type = RPC_FC_HYPER;
643 param_type_string = "FC_HYPER";
645 break;
646 default:
647 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
648 correlation_variable_type);
651 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
652 RPC_FC_NORMAL_CONFORMANCE | param_type, param_type_string);
653 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
654 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
655 offset, offset);
657 else
659 unsigned int callback_offset = 0;
660 struct expr_eval_routine *eval;
661 int found = 0;
663 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
665 if (!strcmp (eval->structure->name, structure->name)
666 && !compare_expr (eval->expr, expr))
668 found = 1;
669 break;
671 callback_offset++;
674 if (!found)
676 eval = xmalloc (sizeof(*eval));
677 eval->structure = structure;
678 eval->expr = expr;
679 list_add_tail (&expr_eval_routines, &eval->entry);
682 if (callback_offset > USHRT_MAX)
683 error("Maximum number of callback routines reached\n");
685 print_file(file, 2, "0x%x, /* Corr desc: */\n", RPC_FC_NORMAL_CONFORMANCE);
686 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
687 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
689 return 4;
692 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
694 int have_align = FALSE;
695 size_t size = 0;
696 const var_t *v;
698 if (!fields) return 0;
699 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
701 unsigned int falign = 0;
702 size_t fsize = type_memsize(v->type, &falign);
703 if (!have_align)
705 *align = falign;
706 have_align = TRUE;
708 size = (size + (falign - 1)) & ~(falign - 1);
709 size += fsize;
712 size = (size + (*align - 1)) & ~(*align - 1);
713 return size;
716 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
718 size_t size, maxs = 0;
719 unsigned int align = *pmaxa;
720 const var_t *v;
722 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
724 /* we could have an empty default field with NULL type */
725 if (v->type)
727 size = type_memsize(v->type, &align);
728 if (maxs < size) maxs = size;
729 if (*pmaxa < align) *pmaxa = align;
733 return maxs;
736 int get_padding(const var_list_t *fields)
738 unsigned short offset = 0;
739 int salign = -1;
740 const var_t *f;
742 if (!fields)
743 return 0;
745 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
747 type_t *ft = f->type;
748 unsigned int align = 0;
749 size_t size = type_memsize(ft, &align);
750 if (salign == -1)
751 salign = align;
752 offset = (offset + (align - 1)) & ~(align - 1);
753 offset += size;
756 return ((offset + (salign - 1)) & ~(salign - 1)) - offset;
759 size_t type_memsize(const type_t *t, unsigned int *align)
761 size_t size = 0;
763 if (t->declarray && is_conformant_array(t))
765 type_memsize(t->ref, align);
766 size = 0;
768 else if (is_ptr(t) || is_conformant_array(t))
770 size = sizeof(void *);
771 if (size > *align) *align = size;
773 else switch (t->type)
775 case RPC_FC_BYTE:
776 case RPC_FC_CHAR:
777 case RPC_FC_USMALL:
778 case RPC_FC_SMALL:
779 size = 1;
780 if (size > *align) *align = size;
781 break;
782 case RPC_FC_WCHAR:
783 case RPC_FC_USHORT:
784 case RPC_FC_SHORT:
785 case RPC_FC_ENUM16:
786 size = 2;
787 if (size > *align) *align = size;
788 break;
789 case RPC_FC_ULONG:
790 case RPC_FC_LONG:
791 case RPC_FC_ERROR_STATUS_T:
792 case RPC_FC_ENUM32:
793 case RPC_FC_FLOAT:
794 size = 4;
795 if (size > *align) *align = size;
796 break;
797 case RPC_FC_HYPER:
798 case RPC_FC_DOUBLE:
799 size = 8;
800 if (size > *align) *align = size;
801 break;
802 case RPC_FC_STRUCT:
803 case RPC_FC_CVSTRUCT:
804 case RPC_FC_CPSTRUCT:
805 case RPC_FC_CSTRUCT:
806 case RPC_FC_PSTRUCT:
807 case RPC_FC_BOGUS_STRUCT:
808 size = fields_memsize(t->fields, align);
809 break;
810 case RPC_FC_ENCAPSULATED_UNION:
811 case RPC_FC_NON_ENCAPSULATED_UNION:
812 size = union_memsize(t->fields, align);
813 break;
814 case RPC_FC_SMFARRAY:
815 case RPC_FC_LGFARRAY:
816 case RPC_FC_SMVARRAY:
817 case RPC_FC_LGVARRAY:
818 case RPC_FC_BOGUS_ARRAY:
819 size = t->dim * type_memsize(t->ref, align);
820 break;
821 default:
822 error("type_memsize: Unknown type %d\n", t->type);
823 size = 0;
826 return size;
829 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
831 short absoff = type->ref->typestring_offset;
832 short reloff = absoff - (offset + 2);
833 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
835 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
836 type->type, ptr_attr, string_of_type(type->type));
837 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
838 reloff, reloff, absoff);
839 return 4;
842 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
844 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
845 type->type, string_of_type(type->type));
846 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
847 string_of_type(type->ref->type));
848 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
849 return 4;
852 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
854 unsigned int offset = *typestring_offset;
856 print_file(file, 0, "/* %d */\n", offset);
857 update_tfsoff(type, offset, file);
859 if (type->ref->typestring_offset)
860 *typestring_offset += write_nonsimple_pointer(file, type, offset);
861 else if (is_base_type(type->ref->type))
862 *typestring_offset += write_simple_pointer(file, type);
864 return offset;
867 static int processed(const type_t *type)
869 return type->typestring_offset && !type->tfswrite;
872 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
874 unsigned int start, absoff, flags;
875 unsigned int align = 0, ualign = 0;
876 const char *name;
877 type_t *utype = get_user_type(type, &name);
878 size_t usize = type_memsize(utype, &ualign);
879 size_t size = type_memsize(type, &align);
880 unsigned short funoff = user_type_offset(name);
881 short reloff;
883 guard_rec(type);
885 if (is_base_type(utype->type))
887 absoff = *tfsoff;
888 print_file(file, 0, "/* %d */\n", absoff);
889 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
890 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
891 *tfsoff += 2;
893 else
895 if (!processed(utype))
896 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
897 absoff = utype->typestring_offset;
900 if (utype->type == RPC_FC_RP)
901 flags = 0x40;
902 else if (utype->type == RPC_FC_UP)
903 flags = 0x80;
904 else
905 flags = 0;
907 start = *tfsoff;
908 update_tfsoff(type, start, file);
909 print_file(file, 0, "/* %d */\n", start);
910 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
911 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
912 flags | (align - 1), align - 1, flags);
913 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
914 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
915 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
916 *tfsoff += 8;
917 reloff = absoff - *tfsoff;
918 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
919 *tfsoff += 2;
922 static void write_member_type(FILE *file, const type_t *cont,
923 const attr_list_t *attrs, const type_t *type,
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(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 unsigned char fc = (cont->type == RPC_FC_BOGUS_STRUCT
952 ? RPC_FC_POINTER
953 : RPC_FC_LONG);
954 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
955 *tfsoff += 1;
957 else if (!write_base_type(file, type, tfsoff))
958 error("Unsupported member type 0x%x\n", type->type);
961 static void write_end(FILE *file, unsigned int *tfsoff)
963 if (*tfsoff % 2 == 0)
965 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
966 *tfsoff += 1;
968 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
969 *tfsoff += 1;
972 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
974 unsigned int offset = 0;
975 var_list_t *fs = type->fields;
976 var_t *f;
978 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
980 unsigned int align = 0;
981 type_t *ft = f->type;
982 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
984 unsigned int absoff = ft->typestring_offset;
985 short reloff = absoff - (*tfsoff + 6);
986 print_file(file, 0, "/* %d */\n", *tfsoff);
987 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
988 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
989 write_conf_or_var_desc(file, current_structure, offset,
990 get_attrp(f->attrs, ATTR_SWITCHIS));
991 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
992 reloff, reloff, absoff);
993 *tfsoff += 8;
996 /* FIXME: take alignment into account */
997 offset += type_memsize(ft, &align);
1001 static int write_no_repeat_pointer_descriptions(
1002 FILE *file, type_t *type,
1003 size_t *offset_in_memory, size_t *offset_in_buffer,
1004 unsigned int *typestring_offset)
1006 int written = 0;
1007 unsigned int align;
1009 if (is_ptr(type))
1011 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1012 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1014 /* pointer instance */
1015 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1016 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1017 *typestring_offset += 6;
1019 if (processed(type->ref) || is_base_type(type->ref->type))
1020 write_pointer_tfs(file, type, typestring_offset);
1021 else
1022 error("write_pointer_description: type format string unknown\n");
1024 align = 0;
1025 *offset_in_memory += type_memsize(type, &align);
1026 /* FIXME: is there a case where these two are different? */
1027 align = 0;
1028 *offset_in_buffer += type_memsize(type, &align);
1030 return 1;
1033 if (is_non_complex_struct(type))
1035 const var_t *v;
1036 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1037 written += write_no_repeat_pointer_descriptions(
1038 file, v->type,
1039 offset_in_memory, offset_in_buffer, typestring_offset);
1041 else
1043 align = 0;
1044 *offset_in_memory += type_memsize(type, &align);
1045 /* FIXME: is there a case where these two are different? */
1046 align = 0;
1047 *offset_in_buffer += type_memsize(type, &align);
1050 return written;
1053 static int write_pointer_description_offsets(
1054 FILE *file, const attr_list_t *attrs, type_t *type,
1055 size_t *offset_in_memory, size_t *offset_in_buffer,
1056 unsigned int *typestring_offset)
1058 int written = 0;
1059 unsigned int align;
1061 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1063 if (offset_in_memory && offset_in_buffer)
1065 /* pointer instance */
1066 /* FIXME: sometimes from end of structure, sometimes from beginning */
1067 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1068 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1070 align = 0;
1071 *offset_in_memory += type_memsize(type, &align);
1072 /* FIXME: is there a case where these two are different? */
1073 align = 0;
1074 *offset_in_buffer += type_memsize(type, &align);
1076 *typestring_offset += 4;
1078 if (processed(type->ref) || is_base_type(type->ref->type))
1079 write_pointer_tfs(file, type, typestring_offset);
1080 else
1081 error("write_pointer_description_offsets: type format string unknown\n");
1083 return 1;
1086 if (is_array(type))
1088 return write_pointer_description_offsets(
1089 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1090 typestring_offset);
1092 else if (is_non_complex_struct(type))
1094 /* otherwise search for interesting fields to parse */
1095 const var_t *v;
1096 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1098 written += write_pointer_description_offsets(
1099 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1100 typestring_offset);
1103 else
1105 align = 0;
1106 if (offset_in_memory)
1107 *offset_in_memory += type_memsize(type, &align);
1108 /* FIXME: is there a case where these two are different? */
1109 align = 0;
1110 if (offset_in_buffer)
1111 *offset_in_buffer += type_memsize(type, &align);
1114 return written;
1117 /* Note: if file is NULL return value is number of pointers to write, else
1118 * it is the number of type format characters written */
1119 static int write_fixed_array_pointer_descriptions(
1120 FILE *file, const attr_list_t *attrs, type_t *type,
1121 size_t *offset_in_memory, size_t *offset_in_buffer,
1122 unsigned int *typestring_offset)
1124 unsigned int align;
1125 int pointer_count = 0;
1127 if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1129 unsigned int temp = 0;
1130 /* unfortunately, this needs to be done in two passes to avoid
1131 * writing out redundant FC_FIXED_REPEAT descriptions */
1132 pointer_count = write_pointer_description_offsets(
1133 NULL, attrs, type->ref, NULL, NULL, &temp);
1134 if (pointer_count > 0)
1136 unsigned int increment_size;
1137 size_t offset_of_array_pointer_mem = 0;
1138 size_t offset_of_array_pointer_buf = 0;
1140 align = 0;
1141 increment_size = type_memsize(type->ref, &align);
1143 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1144 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1145 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1146 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1147 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1148 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1149 *typestring_offset += 10;
1151 pointer_count = write_pointer_description_offsets(
1152 file, attrs, type, &offset_of_array_pointer_mem,
1153 &offset_of_array_pointer_buf, typestring_offset);
1156 else if (is_struct(type->type))
1158 const var_t *v;
1159 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1161 pointer_count += write_fixed_array_pointer_descriptions(
1162 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1163 typestring_offset);
1166 else
1168 align = 0;
1169 if (offset_in_memory)
1170 *offset_in_memory += type_memsize(type, &align);
1171 /* FIXME: is there a case where these two are different? */
1172 align = 0;
1173 if (offset_in_buffer)
1174 *offset_in_buffer += type_memsize(type, &align);
1177 return pointer_count;
1180 /* Note: if file is NULL return value is number of pointers to write, else
1181 * it is the number of type format characters written */
1182 static int write_conformant_array_pointer_descriptions(
1183 FILE *file, const attr_list_t *attrs, type_t *type,
1184 size_t *offset_in_memory, size_t *offset_in_buffer,
1185 unsigned int *typestring_offset)
1187 unsigned int align;
1188 int pointer_count = 0;
1190 if (is_conformant_array(type) && !type->length_is)
1192 unsigned int temp = 0;
1193 /* unfortunately, this needs to be done in two passes to avoid
1194 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1195 pointer_count = write_pointer_description_offsets(
1196 NULL, attrs, type->ref, NULL, NULL, &temp);
1197 if (pointer_count > 0)
1199 unsigned int increment_size;
1200 size_t offset_of_array_pointer_mem = 0;
1201 size_t offset_of_array_pointer_buf = 0;
1203 align = 0;
1204 increment_size = type_memsize(type->ref, &align);
1206 if (increment_size > USHRT_MAX)
1207 error("array size of %u bytes is too large\n", increment_size);
1209 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1210 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1211 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1212 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1213 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1214 *typestring_offset += 8;
1216 pointer_count = write_pointer_description_offsets(
1217 file, attrs, type->ref, &offset_of_array_pointer_mem,
1218 &offset_of_array_pointer_buf, typestring_offset);
1221 else if (is_struct(type->type))
1223 const var_t *v;
1224 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1226 pointer_count += write_conformant_array_pointer_descriptions(
1227 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1228 typestring_offset);
1231 else
1233 align = 0;
1234 if (offset_in_memory)
1235 *offset_in_memory += type_memsize(type, &align);
1236 /* FIXME: is there a case where these two are different? */
1237 align = 0;
1238 if (offset_in_buffer)
1239 *offset_in_buffer += type_memsize(type, &align);
1242 return pointer_count;
1245 /* Note: if file is NULL return value is number of pointers to write, else
1246 * it is the number of type format characters written */
1247 static int write_varying_array_pointer_descriptions(
1248 FILE *file, const attr_list_t *attrs, type_t *type,
1249 size_t *offset_in_memory, size_t *offset_in_buffer,
1250 unsigned int *typestring_offset)
1252 unsigned int align;
1253 int pointer_count = 0;
1255 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1257 if (is_array(type) && type->length_is)
1259 unsigned int temp = 0;
1260 /* unfortunately, this needs to be done in two passes to avoid
1261 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1262 pointer_count = write_pointer_description_offsets(
1263 NULL, attrs, type->ref, NULL, NULL, &temp);
1264 if (pointer_count > 0)
1266 unsigned int increment_size;
1267 size_t offset_of_array_pointer_mem = 0;
1268 size_t offset_of_array_pointer_buf = 0;
1270 align = 0;
1271 increment_size = type_memsize(type->ref, &align);
1273 if (increment_size > USHRT_MAX)
1274 error("array size of %u bytes is too large\n", increment_size);
1276 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1277 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1278 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1279 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1280 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1281 *typestring_offset += 8;
1283 pointer_count = write_pointer_description_offsets(
1284 file, attrs, type, &offset_of_array_pointer_mem,
1285 &offset_of_array_pointer_buf, typestring_offset);
1288 else if (is_struct(type->type))
1290 const var_t *v;
1291 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1293 pointer_count += write_varying_array_pointer_descriptions(
1294 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1295 typestring_offset);
1298 else
1300 align = 0;
1301 if (offset_in_memory)
1302 *offset_in_memory += type_memsize(type, &align);
1303 /* FIXME: is there a case where these two are different? */
1304 align = 0;
1305 if (offset_in_buffer)
1306 *offset_in_buffer += type_memsize(type, &align);
1309 return pointer_count;
1312 static void write_pointer_description(FILE *file, type_t *type,
1313 unsigned int *typestring_offset)
1315 size_t offset_in_buffer;
1316 size_t offset_in_memory;
1318 /* pass 1: search for single instance of a pointer (i.e. don't descend
1319 * into arrays) */
1320 offset_in_memory = 0;
1321 offset_in_buffer = 0;
1322 write_no_repeat_pointer_descriptions(
1323 file, type,
1324 &offset_in_memory, &offset_in_buffer, typestring_offset);
1326 /* pass 2: search for pointers in fixed arrays */
1327 offset_in_memory = 0;
1328 offset_in_buffer = 0;
1329 write_fixed_array_pointer_descriptions(
1330 file, NULL, type,
1331 &offset_in_memory, &offset_in_buffer, typestring_offset);
1333 /* pass 3: search for pointers in conformant only arrays (but don't descend
1334 * into conformant varying or varying arrays) */
1335 offset_in_memory = 0;
1336 offset_in_buffer = 0;
1337 write_conformant_array_pointer_descriptions(
1338 file, NULL, type,
1339 &offset_in_memory, &offset_in_buffer, typestring_offset);
1341 /* pass 4: search for pointers in varying arrays */
1342 offset_in_memory = 0;
1343 offset_in_buffer = 0;
1344 write_varying_array_pointer_descriptions(
1345 file, NULL, type,
1346 &offset_in_memory, &offset_in_buffer, typestring_offset);
1349 static int is_declptr(const type_t *t)
1351 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1354 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1355 type_t *type,
1356 const char *name, unsigned int *typestring_offset)
1358 size_t start_offset = *typestring_offset;
1359 unsigned char rtype;
1361 update_tfsoff(type, start_offset, file);
1363 if (is_declptr(type))
1365 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1366 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1367 if (!pointer_type)
1368 pointer_type = RPC_FC_RP;
1369 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1370 pointer_type, flag, string_of_type(pointer_type),
1371 flag ? " [simple_pointer]" : "");
1372 *typestring_offset += 2;
1373 if (!flag)
1375 print_file(file, 2, "NdrFcShort(0x2),\n");
1376 *typestring_offset += 2;
1378 rtype = type->ref->type;
1380 else
1381 rtype = type->ref->type;
1383 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1385 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1386 return start_offset;
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_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_WCHAR)
1431 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1432 else
1433 WRITE_FCTYPE(file, FC_C_CSTRING, *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_structure, baseoff,
1492 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_structure, baseoff,
1517 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, NULL, type->ref, 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;
1556 unsigned short offset = 0;
1557 int salign = -1;
1558 int padding;
1560 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1562 type_t *ft = field->type;
1563 if (!ft->declarray || !is_conformant_array(ft))
1565 unsigned int align = 0;
1566 size_t size = type_memsize(ft, &align);
1567 if (salign == -1)
1568 salign = align;
1569 if ((align - 1) & offset)
1571 unsigned char fc = 0;
1572 switch (align)
1574 case 4:
1575 fc = RPC_FC_ALIGNM4;
1576 break;
1577 case 8:
1578 fc = RPC_FC_ALIGNM8;
1579 break;
1580 default:
1581 error("write_struct_members: cannot align type %d", ft->type);
1583 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1584 offset = (offset + (align - 1)) & ~(align - 1);
1585 *typestring_offset += 1;
1587 write_member_type(file, type, field->attrs, field->type, corroff,
1588 typestring_offset);
1589 offset += size;
1593 padding = ((offset + (salign - 1)) & ~(salign - 1)) - offset;
1594 if (padding)
1596 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1597 RPC_FC_STRUCTPAD1 + padding - 1,
1598 padding);
1599 *typestring_offset += 1;
1602 write_end(file, typestring_offset);
1605 static size_t write_struct_tfs(FILE *file, type_t *type,
1606 const char *name, unsigned int *tfsoff)
1608 const type_t *save_current_structure = current_structure;
1609 unsigned int total_size;
1610 const var_t *array;
1611 size_t start_offset;
1612 size_t array_offset;
1613 int has_pointers = 0;
1614 unsigned int align = 0;
1615 unsigned int corroff;
1616 var_t *f;
1618 guard_rec(type);
1619 current_structure = type;
1621 total_size = type_memsize(type, &align);
1622 if (total_size > USHRT_MAX)
1623 error("structure size for %s exceeds %d bytes by %d bytes\n",
1624 name, USHRT_MAX, total_size - USHRT_MAX);
1626 if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1627 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1628 FALSE, tfsoff);
1630 array = find_array_or_string_in_struct(type);
1631 if (array && !processed(array->type))
1632 array_offset
1633 = is_attr(array->attrs, ATTR_STRING)
1634 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1635 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1637 corroff = *tfsoff;
1638 write_descriptors(file, type, tfsoff);
1640 start_offset = *tfsoff;
1641 update_tfsoff(type, start_offset, file);
1642 print_file(file, 0, "/* %d */\n", start_offset);
1643 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1644 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1645 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1646 *tfsoff += 4;
1648 if (array)
1650 unsigned int absoff = array->type->typestring_offset;
1651 short reloff = absoff - *tfsoff;
1652 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1653 reloff, reloff, absoff);
1654 *tfsoff += 2;
1656 else if (type->type == RPC_FC_BOGUS_STRUCT)
1658 print_file(file, 2, "NdrFcShort(0x0),\n");
1659 *tfsoff += 2;
1662 if (type->type == RPC_FC_BOGUS_STRUCT)
1664 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1665 nothing is written to file yet. On the actual writing pass,
1666 this will have been updated. */
1667 short reloff = type->ptrdesc - *tfsoff;
1668 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1669 reloff, reloff, type->ptrdesc);
1670 *tfsoff += 2;
1672 else if ((type->type == RPC_FC_PSTRUCT) ||
1673 (type->type == RPC_FC_CPSTRUCT) ||
1674 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1676 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1677 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1678 *tfsoff += 2;
1679 write_pointer_description(file, type, tfsoff);
1680 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1681 *tfsoff += 1;
1684 write_struct_members(file, type, &corroff, tfsoff);
1686 if (type->type == RPC_FC_BOGUS_STRUCT)
1688 const var_list_t *fs = type->fields;
1689 const var_t *f;
1691 type->ptrdesc = *tfsoff;
1692 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
1694 type_t *ft = f->type;
1695 if (is_ptr(ft))
1696 write_pointer_tfs(file, ft, tfsoff);
1700 current_structure = save_current_structure;
1701 return start_offset;
1704 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1705 unsigned char flags, size_t offset,
1706 unsigned int *typeformat_offset)
1708 size_t start_offset = *typeformat_offset;
1709 short reloff = offset - (*typeformat_offset + 2);
1710 int in_attr, out_attr;
1711 in_attr = is_attr(attrs, ATTR_IN);
1712 out_attr = is_attr(attrs, ATTR_OUT);
1713 if (!in_attr && !out_attr) in_attr = 1;
1715 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1716 flags |= 0x04;
1718 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1719 pointer_type,
1720 flags,
1721 string_of_type(pointer_type));
1722 if (file)
1724 if (flags & 0x04)
1725 fprintf(file, " [allocated_on_stack]");
1726 if (flags & 0x10)
1727 fprintf(file, " [pointer_deref]");
1728 fprintf(file, " */\n");
1731 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1732 *typeformat_offset += 4;
1734 return start_offset;
1737 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1739 if (t == NULL)
1741 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1743 else if (is_base_type(t->type))
1745 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1746 t->type, string_of_type(t->type));
1748 else if (t->typestring_offset)
1750 short reloff = t->typestring_offset - *tfsoff;
1751 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1752 reloff, reloff, t->typestring_offset);
1754 else
1755 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1757 *tfsoff += 2;
1760 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1762 unsigned int align = 0;
1763 unsigned int start_offset;
1764 size_t size = type_memsize(type, &align);
1765 var_list_t *fields;
1766 size_t nbranch = 0;
1767 type_t *deftype = NULL;
1768 short nodeftype = 0xffff;
1769 var_t *f;
1771 guard_rec(type);
1773 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1775 const var_t *uv = LIST_ENTRY(list_tail(type->fields), const var_t, entry);
1776 fields = uv->type->fields;
1778 else
1779 fields = type->fields;
1781 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1783 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1784 if (cases)
1785 nbranch += list_count(cases);
1786 if (f->type)
1787 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1790 start_offset = *tfsoff;
1791 update_tfsoff(type, start_offset, file);
1792 print_file(file, 0, "/* %d */\n", start_offset);
1793 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1795 const var_t *sv = LIST_ENTRY(list_head(type->fields), const var_t, entry);
1796 const type_t *st = sv->type;
1798 switch (st->type)
1800 case RPC_FC_CHAR:
1801 case RPC_FC_SMALL:
1802 case RPC_FC_USMALL:
1803 case RPC_FC_SHORT:
1804 case RPC_FC_USHORT:
1805 case RPC_FC_LONG:
1806 case RPC_FC_ULONG:
1807 case RPC_FC_ENUM16:
1808 case RPC_FC_ENUM32:
1809 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1810 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1811 0x40 | st->type, string_of_type(st->type));
1812 *tfsoff += 2;
1813 break;
1814 default:
1815 error("union switch type must be an integer, char, or enum\n");
1818 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1819 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1820 *tfsoff += 4;
1822 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1824 type_t *ft = f->type;
1825 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1826 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1827 expr_t *c;
1829 if (cases == NULL && !deflt)
1830 error("union field %s with neither case nor default attribute\n", f->name);
1832 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1834 /* MIDL doesn't check for duplicate cases, even though that seems
1835 like a reasonable thing to do, it just dumps them to the TFS
1836 like we're going to do here. */
1837 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1838 *tfsoff += 4;
1839 write_branch_type(file, ft, tfsoff);
1842 /* MIDL allows multiple default branches, even though that seems
1843 illogical, it just chooses the last one, which is what we will
1844 do. */
1845 if (deflt)
1847 deftype = ft;
1848 nodeftype = 0;
1852 if (deftype)
1854 write_branch_type(file, deftype, tfsoff);
1856 else
1858 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1859 *tfsoff += 2;
1862 return start_offset;
1865 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1866 unsigned int *typeformat_offset)
1868 size_t i;
1869 size_t start_offset = *typeformat_offset;
1870 const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1872 if (iid)
1874 expr_t expr;
1876 expr.type = EXPR_IDENTIFIER;
1877 expr.ref = NULL;
1878 expr.u.sval = iid->name;
1879 expr.is_const = FALSE;
1880 print_file(file, 2, "0x2f, /* FC_IP */\n");
1881 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1882 *typeformat_offset += write_conf_or_var_desc(file, NULL, 0, &expr) + 2;
1884 else
1886 const type_t *base = is_ptr(type) ? type->ref : type;
1887 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1889 if (! uuid)
1890 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1892 update_tfsoff(type, start_offset, file);
1893 print_file(file, 0, "/* %d */\n", start_offset);
1894 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1895 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1896 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1897 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1898 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1899 for (i = 0; i < 8; ++i)
1900 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1902 if (file)
1903 fprintf(file, "\n");
1905 *typeformat_offset += 18;
1907 return start_offset;
1910 static int get_ptr_attr(const type_t *t, int def_type)
1912 while (TRUE)
1914 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1915 if (ptr_attr)
1916 return ptr_attr;
1917 if (t->kind != TKIND_ALIAS)
1918 return def_type;
1919 t = t->orig;
1923 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1924 type_t *type, const var_t *var,
1925 unsigned int *typeformat_offset)
1927 int pointer_type;
1928 size_t offset;
1930 if (is_user_type(type))
1932 write_user_tfs(file, type, typeformat_offset);
1933 return type->typestring_offset;
1936 if (type == var->type) /* top-level pointers */
1938 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1939 if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1940 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1942 if (pointer_attr == 0)
1943 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1945 pointer_type = pointer_attr;
1947 else
1948 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1950 if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1951 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1953 if (is_array(type))
1954 return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1956 if (!is_ptr(type))
1958 /* basic types don't need a type format string */
1959 if (is_base_type(type->type))
1960 return 0;
1962 switch (type->type)
1964 case RPC_FC_STRUCT:
1965 case RPC_FC_PSTRUCT:
1966 case RPC_FC_CSTRUCT:
1967 case RPC_FC_CPSTRUCT:
1968 case RPC_FC_CVSTRUCT:
1969 case RPC_FC_BOGUS_STRUCT:
1970 return write_struct_tfs(file, type, var->name, typeformat_offset);
1971 case RPC_FC_ENCAPSULATED_UNION:
1972 case RPC_FC_NON_ENCAPSULATED_UNION:
1973 return write_union_tfs(file, type, typeformat_offset);
1974 case RPC_FC_IGNORE:
1975 case RPC_FC_BIND_PRIMITIVE:
1976 /* nothing to do */
1977 return 0;
1978 default:
1979 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1982 else if (last_ptr(type))
1984 size_t start_offset = *typeformat_offset;
1985 int in_attr = is_attr(var->attrs, ATTR_IN);
1986 int out_attr = is_attr(var->attrs, ATTR_OUT);
1987 const type_t *base = type->ref;
1989 if (base->type == RPC_FC_IP)
1991 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
1994 /* special case for pointers to base types */
1995 if (is_base_type(base->type))
1997 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1998 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1999 string_of_type(pointer_type),
2000 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2001 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2002 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2003 *typeformat_offset += 4;
2004 return start_offset;
2008 assert(is_ptr(type));
2010 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2011 if (file)
2012 fprintf(file, "/* %2u */\n", *typeformat_offset);
2013 return write_pointer_only_tfs(file, var->attrs, pointer_type,
2014 !last_ptr(type) ? 0x10 : 0,
2015 offset, typeformat_offset);
2018 static void set_tfswrite(type_t *type, int val)
2020 while (type->tfswrite != val)
2022 type_t *utype = get_user_type(type, NULL);
2024 type->tfswrite = val;
2026 if (utype)
2027 set_tfswrite(utype, val);
2029 if (type->kind == TKIND_ALIAS)
2030 type = type->orig;
2031 else if (is_ptr(type) || is_array(type))
2032 type = type->ref;
2033 else
2035 if (type->fields)
2037 var_t *v;
2038 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
2039 if (v->type)
2040 set_tfswrite(v->type, val);
2043 return;
2048 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2049 const char *name, int write_ptr, unsigned int *tfsoff)
2051 int retmask = 0;
2053 if (is_user_type(type))
2055 write_user_tfs(file, type, tfsoff);
2057 else if (is_ptr(type))
2059 type_t *ref = type->ref;
2061 if (ref->type == RPC_FC_IP)
2063 write_ip_tfs(file, attrs, type, tfsoff);
2065 else
2067 if (!processed(ref) && !is_base_type(ref->type))
2068 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2070 if (write_ptr)
2071 write_pointer_tfs(file, type, tfsoff);
2073 retmask |= 1;
2076 else if (last_array(type) && is_attr(attrs, ATTR_STRING))
2078 write_string_tfs(file, attrs, type, name, tfsoff);
2080 else if (type->declarray && is_conformant_array(type))
2081 ; /* conformant arrays and strings are handled specially */
2082 else if (is_array(type))
2084 write_array_tfs(file, attrs, type, name, tfsoff);
2086 else if (is_struct(type->type))
2088 if (!processed(type))
2089 write_struct_tfs(file, type, name, tfsoff);
2091 else if (is_union(type->type))
2093 if (!processed(type))
2094 write_union_tfs(file, type, tfsoff);
2096 else if (!is_base_type(type->type))
2097 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2098 name, type->type);
2100 return retmask;
2103 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2105 const ifref_t * iface;
2106 const func_t *func;
2107 const var_t *var;
2109 if (ifaces)
2110 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2111 if (iface->iface->funcs)
2112 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2113 if (func->args)
2114 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2115 set_tfswrite(var->type, val);
2118 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
2120 const var_t *var;
2121 const ifref_t *iface;
2122 unsigned int typeformat_offset = 2;
2124 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2126 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2127 continue;
2129 if (iface->iface->funcs)
2131 const func_t *func;
2132 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2134 if (is_local(func->def->attrs)) continue;
2136 current_func = func;
2137 if (func->args)
2138 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2139 update_tfsoff(
2140 var->type,
2141 write_typeformatstring_var(
2142 file, 2, func, var->type, var,
2143 &typeformat_offset),
2144 file);
2149 return typeformat_offset + 1;
2153 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2155 int indent = 0;
2157 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2158 print_file(file, indent, "{\n");
2159 indent++;
2160 print_file(file, indent, "0,\n");
2161 print_file(file, indent, "{\n");
2162 indent++;
2163 print_file(file, indent, "NdrFcShort(0x0),\n");
2165 set_all_tfswrite(ifaces, TRUE);
2166 process_tfs(file, ifaces, for_objects);
2168 print_file(file, indent, "0x0\n");
2169 indent--;
2170 print_file(file, indent, "}\n");
2171 indent--;
2172 print_file(file, indent, "};\n");
2173 print_file(file, indent, "\n");
2176 static unsigned int get_required_buffer_size_type(
2177 const type_t *type, const char *name, unsigned int *alignment)
2179 *alignment = 0;
2180 if (is_user_type(type))
2182 const char *uname;
2183 const type_t *utype = get_user_type(type, &uname);
2184 return get_required_buffer_size_type(utype, uname, alignment);
2186 else
2188 switch (type->type)
2190 case RPC_FC_BYTE:
2191 case RPC_FC_CHAR:
2192 case RPC_FC_USMALL:
2193 case RPC_FC_SMALL:
2194 *alignment = 4;
2195 return 1;
2197 case RPC_FC_WCHAR:
2198 case RPC_FC_USHORT:
2199 case RPC_FC_SHORT:
2200 case RPC_FC_ENUM16:
2201 *alignment = 4;
2202 return 2;
2204 case RPC_FC_ULONG:
2205 case RPC_FC_LONG:
2206 case RPC_FC_ENUM32:
2207 case RPC_FC_FLOAT:
2208 case RPC_FC_ERROR_STATUS_T:
2209 *alignment = 4;
2210 return 4;
2212 case RPC_FC_HYPER:
2213 case RPC_FC_DOUBLE:
2214 *alignment = 8;
2215 return 8;
2217 case RPC_FC_IGNORE:
2218 case RPC_FC_BIND_PRIMITIVE:
2219 return 0;
2221 case RPC_FC_STRUCT:
2222 case RPC_FC_PSTRUCT:
2224 size_t size = 0;
2225 const var_t *field;
2226 if (!type->fields) return 0;
2227 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2229 unsigned int alignment;
2230 size += get_required_buffer_size_type(field->type, field->name,
2231 &alignment);
2233 return size;
2236 case RPC_FC_RP:
2237 return
2238 is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
2239 ? get_required_buffer_size_type( type->ref, name, alignment )
2240 : 0;
2242 case RPC_FC_SMFARRAY:
2243 case RPC_FC_LGFARRAY:
2244 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2246 default:
2247 return 0;
2252 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2254 int in_attr = is_attr(var->attrs, ATTR_IN);
2255 int out_attr = is_attr(var->attrs, ATTR_OUT);
2257 if (!in_attr && !out_attr)
2258 in_attr = 1;
2260 *alignment = 0;
2262 if (pass == PASS_OUT)
2264 if (out_attr && is_ptr(var->type))
2266 type_t *type = var->type;
2268 if (type->type == RPC_FC_STRUCT)
2270 const var_t *field;
2271 unsigned int size = 36;
2273 if (!type->fields) return size;
2274 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2276 unsigned int align;
2277 size += get_required_buffer_size_type(
2278 field->type, field->name, &align);
2280 return size;
2283 return 0;
2285 else
2287 if ((!out_attr || in_attr) && !var->type->size_is
2288 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2290 if (is_ptr(var->type))
2292 type_t *type = var->type;
2294 if (is_base_type(type->type))
2296 return 25;
2298 else if (type->type == RPC_FC_STRUCT)
2300 unsigned int size = 36;
2301 const var_t *field;
2303 if (!type->fields) return size;
2304 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2306 unsigned int align;
2307 size += get_required_buffer_size_type(
2308 field->type, field->name, &align);
2310 return size;
2315 return get_required_buffer_size_type(var->type, var->name, alignment);
2319 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2321 const var_t *var;
2322 unsigned int total_size = 0, alignment;
2324 if (func->args)
2326 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2328 total_size += get_required_buffer_size(var, &alignment, pass);
2329 total_size += alignment;
2333 if (pass == PASS_OUT && !is_void(func->def->type))
2335 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2336 total_size += alignment;
2338 return total_size;
2341 static void print_phase_function(FILE *file, int indent, const char *type,
2342 enum remoting_phase phase,
2343 const var_t *var, unsigned int type_offset)
2345 const char *function;
2346 switch (phase)
2348 case PHASE_BUFFERSIZE:
2349 function = "BufferSize";
2350 break;
2351 case PHASE_MARSHAL:
2352 function = "Marshall";
2353 break;
2354 case PHASE_UNMARSHAL:
2355 function = "Unmarshall";
2356 break;
2357 case PHASE_FREE:
2358 function = "Free";
2359 break;
2360 default:
2361 assert(0);
2362 return;
2365 print_file(file, indent, "Ndr%s%s(\n", type, function);
2366 indent++;
2367 print_file(file, indent, "&_StubMsg,\n");
2368 print_file(file, indent, "%s%s%s%s,\n",
2369 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2370 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2371 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2372 var->name);
2373 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2374 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2375 if (phase == PHASE_UNMARSHAL)
2376 print_file(file, indent, "0);\n");
2377 indent--;
2380 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2381 enum pass pass, const var_t *var,
2382 const char *varname)
2384 type_t *type = var->type;
2385 unsigned int size;
2386 unsigned int alignment = 0;
2387 unsigned char rtype;
2389 /* no work to do for other phases, buffer sizing is done elsewhere */
2390 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2391 return;
2393 rtype = is_ptr(type) ? type->ref->type : type->type;
2395 switch (rtype)
2397 case RPC_FC_BYTE:
2398 case RPC_FC_CHAR:
2399 case RPC_FC_SMALL:
2400 case RPC_FC_USMALL:
2401 size = 1;
2402 alignment = 1;
2403 break;
2405 case RPC_FC_WCHAR:
2406 case RPC_FC_USHORT:
2407 case RPC_FC_SHORT:
2408 case RPC_FC_ENUM16:
2409 size = 2;
2410 alignment = 2;
2411 break;
2413 case RPC_FC_ULONG:
2414 case RPC_FC_LONG:
2415 case RPC_FC_ENUM32:
2416 case RPC_FC_FLOAT:
2417 case RPC_FC_ERROR_STATUS_T:
2418 size = 4;
2419 alignment = 4;
2420 break;
2422 case RPC_FC_HYPER:
2423 case RPC_FC_DOUBLE:
2424 size = 8;
2425 alignment = 8;
2426 break;
2428 case RPC_FC_IGNORE:
2429 case RPC_FC_BIND_PRIMITIVE:
2430 /* no marshalling needed */
2431 return;
2433 default:
2434 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2435 size = 0;
2438 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2439 alignment - 1, alignment - 1);
2441 if (phase == PHASE_MARSHAL)
2443 print_file(file, indent, "*(");
2444 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2445 if (is_ptr(type))
2446 fprintf(file, " *)_StubMsg.Buffer = *");
2447 else
2448 fprintf(file, " *)_StubMsg.Buffer = ");
2449 fprintf(file, varname);
2450 fprintf(file, ";\n");
2452 else if (phase == PHASE_UNMARSHAL)
2454 if (pass == PASS_IN || pass == PASS_RETURN)
2455 print_file(file, indent, "");
2456 else
2457 print_file(file, indent, "*");
2458 fprintf(file, varname);
2459 if (pass == PASS_IN && is_ptr(type))
2460 fprintf(file, " = (");
2461 else
2462 fprintf(file, " = *(");
2463 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2464 fprintf(file, " *)_StubMsg.Buffer;\n");
2467 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2468 write_type(file, var->type, FALSE, NULL);
2469 fprintf(file, ");\n");
2472 /* returns whether the MaxCount, Offset or ActualCount members need to be
2473 * filled in for the specified phase */
2474 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2476 return (phase != PHASE_UNMARSHAL);
2479 static int needs_freeing(const attr_list_t *attrs, const type_t *t, int out)
2481 return
2482 (is_user_type(t)
2483 || (is_ptr(t)
2484 && (t->ref->type == RPC_FC_IP
2485 || is_ptr(t->ref))))
2486 || (out && (is_string_type(attrs, t)
2487 || is_array(t)));
2490 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2491 enum pass pass, enum remoting_phase phase)
2493 int in_attr, out_attr, pointer_type;
2494 const var_t *var;
2496 if (!func->args)
2497 return;
2499 if (phase == PHASE_BUFFERSIZE)
2501 unsigned int size = get_function_buffer_size( func, pass );
2502 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2505 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2507 const type_t *type = var->type;
2508 unsigned char rtype;
2509 size_t start_offset = type->typestring_offset;
2511 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2512 if (!pointer_type)
2513 pointer_type = RPC_FC_RP;
2515 in_attr = is_attr(var->attrs, ATTR_IN);
2516 out_attr = is_attr(var->attrs, ATTR_OUT);
2517 if (!in_attr && !out_attr)
2518 in_attr = 1;
2520 if (phase == PHASE_FREE)
2522 if (!needs_freeing(var->attrs, type, out_attr))
2523 continue;
2525 else
2526 switch (pass)
2528 case PASS_IN:
2529 if (!in_attr) continue;
2530 break;
2531 case PASS_OUT:
2532 if (!out_attr) continue;
2533 break;
2534 case PASS_RETURN:
2535 break;
2538 rtype = type->type;
2540 if (is_user_type(var->type))
2542 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2544 else if (is_string_type(var->attrs, var->type))
2546 if (is_array(type) && !is_conformant_array(type))
2547 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2548 else
2550 if (type->size_is && is_size_needed_for_phase(phase))
2552 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2553 write_expr(file, type->size_is, 1);
2554 fprintf(file, ";\n");
2557 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2558 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2559 else
2560 print_phase_function(file, indent, "ConformantString", phase, var,
2561 start_offset + (type->size_is ? 4 : 2));
2564 else if (is_array(type))
2566 unsigned char tc = type->type;
2567 const char *array_type;
2569 if (tc == RPC_FC_SMFARRAY || tc == RPC_FC_LGFARRAY)
2570 array_type = "FixedArray";
2571 else if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2573 if (is_size_needed_for_phase(phase))
2575 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2576 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2577 write_expr(file, type->length_is, 1);
2578 fprintf(file, ";\n\n");
2580 array_type = "VaryingArray";
2582 else if (tc == RPC_FC_CARRAY)
2584 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2586 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2587 write_expr(file, type->size_is, 1);
2588 fprintf(file, ";\n\n");
2590 array_type = "ConformantArray";
2592 else if (tc == RPC_FC_CVARRAY)
2594 if (is_size_needed_for_phase(phase))
2596 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2597 write_expr(file, type->size_is, 1);
2598 fprintf(file, ";\n");
2599 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2600 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2601 write_expr(file, type->length_is, 1);
2602 fprintf(file, ";\n\n");
2604 array_type = "ConformantVaryingArray";
2606 else
2607 array_type = "ComplexArray";
2609 if (!in_attr && phase == PHASE_FREE)
2611 print_file(file, indent, "if (%s)\n", var->name);
2612 indent++;
2613 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2615 else if (phase != PHASE_FREE)
2617 if (pointer_type == RPC_FC_UP)
2618 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2619 else
2620 print_phase_function(file, indent, array_type, phase, var, start_offset);
2623 else if (!is_ptr(var->type) && is_base_type(rtype))
2625 print_phase_basetype(file, indent, phase, pass, var, var->name);
2627 else if (!is_ptr(var->type))
2629 switch (rtype)
2631 case RPC_FC_STRUCT:
2632 case RPC_FC_PSTRUCT:
2633 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2634 break;
2635 case RPC_FC_CSTRUCT:
2636 case RPC_FC_CPSTRUCT:
2637 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2638 break;
2639 case RPC_FC_CVSTRUCT:
2640 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2641 break;
2642 case RPC_FC_BOGUS_STRUCT:
2643 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2644 break;
2645 case RPC_FC_RP:
2646 if (is_base_type( var->type->ref->type ))
2648 print_phase_basetype(file, indent, phase, pass, var, var->name);
2650 else if (var->type->ref->type == RPC_FC_STRUCT)
2652 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2653 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2655 else
2657 const var_t *iid;
2658 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2659 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2660 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2662 break;
2663 default:
2664 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2667 else
2669 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2671 print_phase_basetype(file, indent, phase, pass, var, var->name);
2673 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2675 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2676 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2678 else
2680 const var_t *iid;
2681 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2682 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2683 if (var->type->ref->type == RPC_FC_IP)
2684 print_phase_function(file, indent, "InterfacePointer", phase, var, start_offset);
2685 else
2686 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2689 fprintf(file, "\n");
2694 size_t get_size_procformatstring_var(const var_t *var)
2696 return write_procformatstring_var(NULL, 0, var, FALSE);
2700 size_t get_size_procformatstring_func(const func_t *func)
2702 const var_t *var;
2703 size_t size = 0;
2705 /* argument list size */
2706 if (func->args)
2707 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2708 size += get_size_procformatstring_var(var);
2710 /* return value size */
2711 if (is_void(func->def->type))
2712 size += 2; /* FC_END and FC_PAD */
2713 else
2714 size += get_size_procformatstring_var(func->def);
2716 return size;
2719 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2721 const ifref_t *iface;
2722 size_t size = 1;
2723 const func_t *func;
2725 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2727 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2728 continue;
2730 if (iface->iface->funcs)
2731 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2732 if (!is_local(func->def->attrs))
2733 size += get_size_procformatstring_func( func );
2735 return size;
2738 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2740 set_all_tfswrite(ifaces, FALSE);
2741 return process_tfs(NULL, ifaces, for_objects);
2744 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2745 const var_list_t *fields, const char *structvar)
2747 switch (e->type) {
2748 case EXPR_VOID:
2749 break;
2750 case EXPR_NUM:
2751 fprintf(h, "%lu", e->u.lval);
2752 break;
2753 case EXPR_HEXNUM:
2754 fprintf(h, "0x%lx", e->u.lval);
2755 break;
2756 case EXPR_DOUBLE:
2757 fprintf(h, "%#.15g", e->u.dval);
2758 break;
2759 case EXPR_TRUEFALSE:
2760 if (e->u.lval == 0)
2761 fprintf(h, "FALSE");
2762 else
2763 fprintf(h, "TRUE");
2764 break;
2765 case EXPR_IDENTIFIER:
2767 const var_t *field;
2768 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2769 if (!strcmp(e->u.sval, field->name))
2771 fprintf(h, "%s->%s", structvar, e->u.sval);
2772 break;
2775 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2776 break;
2778 case EXPR_NEG:
2779 fprintf(h, "-");
2780 write_struct_expr(h, e->ref, 1, fields, structvar);
2781 break;
2782 case EXPR_NOT:
2783 fprintf(h, "~");
2784 write_struct_expr(h, e->ref, 1, fields, structvar);
2785 break;
2786 case EXPR_PPTR:
2787 fprintf(h, "*");
2788 write_struct_expr(h, e->ref, 1, fields, structvar);
2789 break;
2790 case EXPR_CAST:
2791 fprintf(h, "(");
2792 write_type(h, e->u.tref, FALSE, NULL);
2793 fprintf(h, ")");
2794 write_struct_expr(h, e->ref, 1, fields, structvar);
2795 break;
2796 case EXPR_SIZEOF:
2797 fprintf(h, "sizeof(");
2798 write_type(h, e->u.tref, FALSE, NULL);
2799 fprintf(h, ")");
2800 break;
2801 case EXPR_SHL:
2802 case EXPR_SHR:
2803 case EXPR_MUL:
2804 case EXPR_DIV:
2805 case EXPR_ADD:
2806 case EXPR_SUB:
2807 case EXPR_AND:
2808 case EXPR_OR:
2809 if (brackets) fprintf(h, "(");
2810 write_struct_expr(h, e->ref, 1, fields, structvar);
2811 switch (e->type) {
2812 case EXPR_SHL: fprintf(h, " << "); break;
2813 case EXPR_SHR: fprintf(h, " >> "); break;
2814 case EXPR_MUL: fprintf(h, " * "); break;
2815 case EXPR_DIV: fprintf(h, " / "); break;
2816 case EXPR_ADD: fprintf(h, " + "); break;
2817 case EXPR_SUB: fprintf(h, " - "); break;
2818 case EXPR_AND: fprintf(h, " & "); break;
2819 case EXPR_OR: fprintf(h, " | "); break;
2820 default: break;
2822 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2823 if (brackets) fprintf(h, ")");
2824 break;
2825 case EXPR_COND:
2826 if (brackets) fprintf(h, "(");
2827 write_struct_expr(h, e->ref, 1, fields, structvar);
2828 fprintf(h, " ? ");
2829 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2830 fprintf(h, " : ");
2831 write_struct_expr(h, e->ext2, 1, fields, structvar);
2832 if (brackets) fprintf(h, ")");
2833 break;
2838 void declare_stub_args( FILE *file, int indent, const func_t *func )
2840 int in_attr, out_attr;
2841 int i = 0;
2842 const var_t *def = func->def;
2843 const var_t *var;
2845 /* declare return value '_RetVal' */
2846 if (!is_void(def->type))
2848 print_file(file, indent, "");
2849 write_type_left(file, def->type);
2850 fprintf(file, " _RetVal;\n");
2853 if (!func->args)
2854 return;
2856 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2858 int is_string = is_attr(var->attrs, ATTR_STRING);
2860 in_attr = is_attr(var->attrs, ATTR_IN);
2861 out_attr = is_attr(var->attrs, ATTR_OUT);
2862 if (!out_attr && !in_attr)
2863 in_attr = 1;
2865 if (!in_attr && !var->type->size_is && !is_string)
2867 print_file(file, indent, "");
2868 write_type(file, var->type->ref, FALSE, "_W%u", i++);
2869 fprintf(file, ";\n");
2872 print_file(file, indent, "");
2873 write_type_left(file, var->type);
2874 fprintf(file, " ");
2875 if (var->type->declarray) {
2876 fprintf(file, "( *");
2877 write_name(file, var);
2878 fprintf(file, " )");
2879 } else
2880 write_name(file, var);
2881 write_type_right(file, var->type, FALSE);
2882 fprintf(file, ";\n");
2884 if (decl_indirect(var->type))
2885 print_file(file, indent, "void *_p_%s = &%s;\n",
2886 var->name, var->name);
2891 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2893 int in_attr, out_attr;
2894 int i = 0, sep = 0;
2895 const var_t *var;
2897 if (!func->args)
2898 return;
2900 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2902 int is_string = is_attr(var->attrs, ATTR_STRING);
2903 in_attr = is_attr(var->attrs, ATTR_IN);
2904 out_attr = is_attr(var->attrs, ATTR_OUT);
2905 if (!out_attr && !in_attr)
2906 in_attr = 1;
2908 if (!in_attr)
2910 print_file(file, indent, "");
2911 write_name(file, var);
2913 if (var->type->size_is)
2915 unsigned int size, align = 0;
2916 type_t *type = var->type;
2918 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2919 for ( ; type->size_is ; type = type->ref)
2921 write_expr(file, type->size_is, TRUE);
2922 fprintf(file, " * ");
2924 size = type_memsize(type, &align);
2925 fprintf(file, "%u);\n", size);
2927 else if (!is_string)
2929 fprintf(file, " = &_W%u;\n", i);
2930 if (is_ptr(var->type) && !last_ptr(var->type))
2931 print_file(file, indent, "_W%u = 0;\n", i);
2932 i++;
2935 sep = 1;
2938 if (sep)
2939 fprintf(file, "\n");
2943 int write_expr_eval_routines(FILE *file, const char *iface)
2945 static const char *var_name = "pS";
2946 int result = 0;
2947 struct expr_eval_routine *eval;
2948 unsigned short callback_offset = 0;
2950 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2952 const char *name = eval->structure->name;
2953 const var_list_t *fields = eval->structure->fields;
2954 unsigned align = 0;
2955 result = 1;
2957 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2958 iface, name, callback_offset);
2959 print_file(file, 0, "{\n");
2960 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
2961 name, var_name, name, type_memsize (eval->structure, &align));
2962 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
2963 print_file(file, 1, "pStubMsg->MaxCount = (unsigned long)");
2964 write_struct_expr(file, eval->expr, 1, fields, var_name);
2965 fprintf(file, ";\n");
2966 print_file(file, 0, "}\n\n");
2967 callback_offset++;
2969 return result;
2972 void write_expr_eval_routine_list(FILE *file, const char *iface)
2974 struct expr_eval_routine *eval;
2975 struct expr_eval_routine *cursor;
2976 unsigned short callback_offset = 0;
2978 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2979 fprintf(file, "{\n");
2981 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2983 const char *name = eval->structure->name;
2984 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
2985 callback_offset++;
2986 list_remove(&eval->entry);
2987 free(eval);
2990 fprintf(file, "};\n\n");
2993 void write_user_quad_list(FILE *file)
2995 user_type_t *ut;
2997 if (list_empty(&user_type_list))
2998 return;
3000 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3001 fprintf(file, "{\n");
3002 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3004 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3005 print_file(file, 1, "{\n");
3006 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3007 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3008 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3009 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3010 print_file(file, 1, "}%s\n", sep);
3012 fprintf(file, "};\n\n");
3015 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3017 const struct str_list_entry_t *endpoint;
3018 const char *p;
3020 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3021 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3022 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3024 print_file( f, 1, "{ (const unsigned char *)\"" );
3025 for (p = endpoint->str; *p && *p != ':'; p++)
3027 if (*p == '"' || *p == '\\') fputc( '\\', f );
3028 fputc( *p, f );
3030 if (!*p) goto error;
3031 if (p[1] != '[') goto error;
3033 fprintf( f, "\", (const unsigned char *)\"" );
3034 for (p += 2; *p && *p != ']'; p++)
3036 if (*p == '"' || *p == '\\') fputc( '\\', f );
3037 fputc( *p, f );
3039 if (*p != ']') goto error;
3040 fprintf( f, "\" },\n" );
3042 print_file( f, 0, "};\n\n" );
3043 return;
3045 error:
3046 error("Invalid endpoint syntax '%s'\n", endpoint->str);