push 79ae37814c052c7718c54e0a3539015c4c961c8f
[wine/hacks.git] / tools / widl / typegen.c
blob6625bda55bbfaead9d93517a60b543c242a161ca
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 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 unsigned int baseoff
1452 = type->declarray && current_structure
1453 ? type_memsize(current_structure, &align)
1454 : 0;
1456 if (!pointer_type)
1457 pointer_type = RPC_FC_RP;
1459 has_pointer = FALSE;
1460 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1461 has_pointer = TRUE;
1463 size = type_memsize(type, &align);
1464 if (size == 0) /* conformant array */
1465 size = type_memsize(type->ref, &align);
1467 start_offset = *typestring_offset;
1468 update_tfsoff(type, start_offset, file);
1469 print_file(file, 0, "/* %lu */\n", start_offset);
1470 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1471 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1472 *typestring_offset += 2;
1474 align = 0;
1475 if (type->type != RPC_FC_BOGUS_ARRAY)
1477 unsigned char tc = type->type;
1479 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1481 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1482 *typestring_offset += 4;
1484 else
1486 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1487 *typestring_offset += 2;
1490 if (is_conformant_array(type))
1491 *typestring_offset
1492 += write_conf_or_var_desc(file, current_structure, baseoff,
1493 size_is);
1495 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1497 unsigned int elalign = 0;
1498 size_t elsize = type_memsize(type->ref, &elalign);
1500 if (type->type == RPC_FC_LGVARRAY)
1502 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1503 *typestring_offset += 4;
1505 else
1507 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1508 *typestring_offset += 2;
1511 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1512 *typestring_offset += 2;
1515 if (length_is)
1516 *typestring_offset
1517 += write_conf_or_var_desc(file, current_structure, baseoff,
1518 length_is);
1520 if (has_pointer)
1522 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1523 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1524 *typestring_offset += 2;
1525 write_pointer_description(file, type, typestring_offset);
1526 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1527 *typestring_offset += 1;
1530 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1531 write_end(file, typestring_offset);
1533 else
1535 unsigned int dim = size_is ? 0 : type->dim;
1536 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1537 *typestring_offset += 2;
1538 *typestring_offset
1539 += write_conf_or_var_desc(file, current_structure, baseoff,
1540 size_is);
1541 *typestring_offset
1542 += write_conf_or_var_desc(file, current_structure, baseoff,
1543 length_is);
1544 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1545 write_end(file, typestring_offset);
1548 return start_offset;
1551 static const var_t *find_array_or_string_in_struct(const type_t *type)
1553 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1554 const type_t *ft = last_field->type;
1556 if (ft->declarray && is_conformant_array(ft))
1557 return last_field;
1559 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1560 return find_array_or_string_in_struct(last_field->type);
1561 else
1562 return NULL;
1565 static void write_struct_members(FILE *file, const type_t *type,
1566 unsigned int *corroff, unsigned int *typestring_offset)
1568 const var_t *field;
1569 unsigned short offset = 0;
1570 int salign = -1;
1571 int padding;
1573 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1575 type_t *ft = field->type;
1576 if (!ft->declarray || !is_conformant_array(ft))
1578 unsigned int align = 0;
1579 size_t size = type_memsize(ft, &align);
1580 if (salign == -1)
1581 salign = align;
1582 if ((align - 1) & offset)
1584 unsigned char fc = 0;
1585 switch (align)
1587 case 4:
1588 fc = RPC_FC_ALIGNM4;
1589 break;
1590 case 8:
1591 fc = RPC_FC_ALIGNM8;
1592 break;
1593 default:
1594 error("write_struct_members: cannot align type %d", ft->type);
1596 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1597 offset = (offset + (align - 1)) & ~(align - 1);
1598 *typestring_offset += 1;
1600 write_member_type(file, type, field->attrs, field->type, corroff,
1601 typestring_offset);
1602 offset += size;
1606 padding = ((offset + (salign - 1)) & ~(salign - 1)) - offset;
1607 if (padding)
1609 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1610 RPC_FC_STRUCTPAD1 + padding - 1,
1611 padding);
1612 *typestring_offset += 1;
1615 write_end(file, typestring_offset);
1618 static size_t write_struct_tfs(FILE *file, type_t *type,
1619 const char *name, unsigned int *tfsoff)
1621 const type_t *save_current_structure = current_structure;
1622 unsigned int total_size;
1623 const var_t *array;
1624 size_t start_offset;
1625 size_t array_offset;
1626 int has_pointers = 0;
1627 unsigned int align = 0;
1628 unsigned int corroff;
1629 var_t *f;
1631 guard_rec(type);
1632 current_structure = type;
1634 total_size = type_memsize(type, &align);
1635 if (total_size > USHRT_MAX)
1636 error("structure size for %s exceeds %d bytes by %d bytes\n",
1637 name, USHRT_MAX, total_size - USHRT_MAX);
1639 if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1640 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1641 FALSE, tfsoff);
1643 array = find_array_or_string_in_struct(type);
1644 if (array && !processed(array->type))
1645 array_offset
1646 = is_attr(array->attrs, ATTR_STRING)
1647 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1648 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1650 corroff = *tfsoff;
1651 write_descriptors(file, type, tfsoff);
1653 start_offset = *tfsoff;
1654 update_tfsoff(type, start_offset, file);
1655 print_file(file, 0, "/* %d */\n", start_offset);
1656 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1657 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1658 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1659 *tfsoff += 4;
1661 if (array)
1663 unsigned int absoff = array->type->typestring_offset;
1664 short reloff = absoff - *tfsoff;
1665 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1666 reloff, reloff, absoff);
1667 *tfsoff += 2;
1669 else if (type->type == RPC_FC_BOGUS_STRUCT)
1671 print_file(file, 2, "NdrFcShort(0x0),\n");
1672 *tfsoff += 2;
1675 if (type->type == RPC_FC_BOGUS_STRUCT)
1677 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1678 nothing is written to file yet. On the actual writing pass,
1679 this will have been updated. */
1680 short reloff = type->ptrdesc - *tfsoff;
1681 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1682 reloff, reloff, type->ptrdesc);
1683 *tfsoff += 2;
1685 else if ((type->type == RPC_FC_PSTRUCT) ||
1686 (type->type == RPC_FC_CPSTRUCT) ||
1687 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1689 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1690 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1691 *tfsoff += 2;
1692 write_pointer_description(file, type, tfsoff);
1693 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1694 *tfsoff += 1;
1697 write_struct_members(file, type, &corroff, tfsoff);
1699 if (type->type == RPC_FC_BOGUS_STRUCT)
1701 const var_list_t *fs = type->fields;
1702 const var_t *f;
1704 type->ptrdesc = *tfsoff;
1705 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
1707 type_t *ft = f->type;
1708 if (is_ptr(ft))
1709 write_pointer_tfs(file, ft, tfsoff);
1713 current_structure = save_current_structure;
1714 return start_offset;
1717 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1718 unsigned char flags, size_t offset,
1719 unsigned int *typeformat_offset)
1721 size_t start_offset = *typeformat_offset;
1722 short reloff = offset - (*typeformat_offset + 2);
1723 int in_attr, out_attr;
1724 in_attr = is_attr(attrs, ATTR_IN);
1725 out_attr = is_attr(attrs, ATTR_OUT);
1726 if (!in_attr && !out_attr) in_attr = 1;
1728 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1729 flags |= 0x04;
1731 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1732 pointer_type,
1733 flags,
1734 string_of_type(pointer_type));
1735 if (file)
1737 if (flags & 0x04)
1738 fprintf(file, " [allocated_on_stack]");
1739 if (flags & 0x10)
1740 fprintf(file, " [pointer_deref]");
1741 fprintf(file, " */\n");
1744 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1745 *typeformat_offset += 4;
1747 return start_offset;
1750 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1752 if (t == NULL)
1754 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1756 else if (is_base_type(t->type))
1758 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1759 t->type, string_of_type(t->type));
1761 else if (t->typestring_offset)
1763 short reloff = t->typestring_offset - *tfsoff;
1764 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1765 reloff, reloff, t->typestring_offset);
1767 else
1768 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1770 *tfsoff += 2;
1773 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1775 unsigned int align = 0;
1776 unsigned int start_offset;
1777 size_t size = type_memsize(type, &align);
1778 var_list_t *fields;
1779 size_t nbranch = 0;
1780 type_t *deftype = NULL;
1781 short nodeftype = 0xffff;
1782 var_t *f;
1784 guard_rec(type);
1786 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1788 const var_t *uv = LIST_ENTRY(list_tail(type->fields), const var_t, entry);
1789 fields = uv->type->fields;
1791 else
1792 fields = type->fields;
1794 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1796 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1797 if (cases)
1798 nbranch += list_count(cases);
1799 if (f->type)
1800 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1803 start_offset = *tfsoff;
1804 update_tfsoff(type, start_offset, file);
1805 print_file(file, 0, "/* %d */\n", start_offset);
1806 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1808 const var_t *sv = LIST_ENTRY(list_head(type->fields), const var_t, entry);
1809 const type_t *st = sv->type;
1811 switch (st->type)
1813 case RPC_FC_CHAR:
1814 case RPC_FC_SMALL:
1815 case RPC_FC_USMALL:
1816 case RPC_FC_SHORT:
1817 case RPC_FC_USHORT:
1818 case RPC_FC_LONG:
1819 case RPC_FC_ULONG:
1820 case RPC_FC_ENUM16:
1821 case RPC_FC_ENUM32:
1822 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1823 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1824 0x40 | st->type, string_of_type(st->type));
1825 *tfsoff += 2;
1826 break;
1827 default:
1828 error("union switch type must be an integer, char, or enum\n");
1831 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1832 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1833 *tfsoff += 4;
1835 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1837 type_t *ft = f->type;
1838 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1839 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1840 expr_t *c;
1842 if (cases == NULL && !deflt)
1843 error("union field %s with neither case nor default attribute\n", f->name);
1845 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1847 /* MIDL doesn't check for duplicate cases, even though that seems
1848 like a reasonable thing to do, it just dumps them to the TFS
1849 like we're going to do here. */
1850 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1851 *tfsoff += 4;
1852 write_branch_type(file, ft, tfsoff);
1855 /* MIDL allows multiple default branches, even though that seems
1856 illogical, it just chooses the last one, which is what we will
1857 do. */
1858 if (deflt)
1860 deftype = ft;
1861 nodeftype = 0;
1865 if (deftype)
1867 write_branch_type(file, deftype, tfsoff);
1869 else
1871 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1872 *tfsoff += 2;
1875 return start_offset;
1878 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1879 unsigned int *typeformat_offset)
1881 size_t i;
1882 size_t start_offset = *typeformat_offset;
1883 const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1885 if (iid)
1887 expr_t expr;
1889 expr.type = EXPR_IDENTIFIER;
1890 expr.ref = NULL;
1891 expr.u.sval = iid->name;
1892 expr.is_const = FALSE;
1893 print_file(file, 2, "0x2f, /* FC_IP */\n");
1894 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1895 *typeformat_offset += write_conf_or_var_desc(file, NULL, 0, &expr) + 2;
1897 else
1899 const type_t *base = is_ptr(type) ? type->ref : type;
1900 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1902 if (! uuid)
1903 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1905 update_tfsoff(type, start_offset, file);
1906 print_file(file, 0, "/* %d */\n", start_offset);
1907 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1908 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1909 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1910 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1911 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1912 for (i = 0; i < 8; ++i)
1913 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1915 if (file)
1916 fprintf(file, "\n");
1918 *typeformat_offset += 18;
1920 return start_offset;
1923 static int get_ptr_attr(const type_t *t, int def_type)
1925 while (TRUE)
1927 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1928 if (ptr_attr)
1929 return ptr_attr;
1930 if (t->kind != TKIND_ALIAS)
1931 return def_type;
1932 t = t->orig;
1936 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1937 type_t *type, const var_t *var,
1938 unsigned int *typeformat_offset)
1940 int pointer_type;
1941 size_t offset;
1943 if (is_user_type(type))
1945 write_user_tfs(file, type, typeformat_offset);
1946 return type->typestring_offset;
1949 if (type == var->type) /* top-level pointers */
1951 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1952 if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1953 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1955 if (pointer_attr == 0)
1956 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1958 pointer_type = pointer_attr;
1960 else
1961 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1963 if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1964 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1966 if (is_array(type))
1967 return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1969 if (!is_ptr(type))
1971 /* basic types don't need a type format string */
1972 if (is_base_type(type->type))
1973 return 0;
1975 switch (type->type)
1977 case RPC_FC_STRUCT:
1978 case RPC_FC_PSTRUCT:
1979 case RPC_FC_CSTRUCT:
1980 case RPC_FC_CPSTRUCT:
1981 case RPC_FC_CVSTRUCT:
1982 case RPC_FC_BOGUS_STRUCT:
1983 return write_struct_tfs(file, type, var->name, typeformat_offset);
1984 case RPC_FC_ENCAPSULATED_UNION:
1985 case RPC_FC_NON_ENCAPSULATED_UNION:
1986 return write_union_tfs(file, type, typeformat_offset);
1987 case RPC_FC_IGNORE:
1988 case RPC_FC_BIND_PRIMITIVE:
1989 /* nothing to do */
1990 return 0;
1991 default:
1992 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1995 else if (last_ptr(type))
1997 size_t start_offset = *typeformat_offset;
1998 int in_attr = is_attr(var->attrs, ATTR_IN);
1999 int out_attr = is_attr(var->attrs, ATTR_OUT);
2000 const type_t *base = type->ref;
2002 if (base->type == RPC_FC_IP)
2004 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2007 /* special case for pointers to base types */
2008 if (is_base_type(base->type))
2010 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2011 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
2012 string_of_type(pointer_type),
2013 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2014 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2015 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2016 *typeformat_offset += 4;
2017 return start_offset;
2021 assert(is_ptr(type));
2023 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2024 if (file)
2025 fprintf(file, "/* %2u */\n", *typeformat_offset);
2026 return write_pointer_only_tfs(file, var->attrs, pointer_type,
2027 !last_ptr(type) ? 0x10 : 0,
2028 offset, typeformat_offset);
2031 static void set_tfswrite(type_t *type, int val)
2033 while (type->tfswrite != val)
2035 type_t *utype = get_user_type(type, NULL);
2037 type->tfswrite = val;
2039 if (utype)
2040 set_tfswrite(utype, val);
2042 if (type->kind == TKIND_ALIAS)
2043 type = type->orig;
2044 else if (is_ptr(type) || is_array(type))
2045 type = type->ref;
2046 else
2048 if (type->fields)
2050 var_t *v;
2051 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
2052 if (v->type)
2053 set_tfswrite(v->type, val);
2056 return;
2061 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2062 const char *name, int write_ptr, unsigned int *tfsoff)
2064 int retmask = 0;
2066 if (is_user_type(type))
2068 write_user_tfs(file, type, tfsoff);
2070 else if (is_ptr(type))
2072 type_t *ref = type->ref;
2074 if (ref->type == RPC_FC_IP)
2076 write_ip_tfs(file, attrs, type, tfsoff);
2078 else
2080 if (!processed(ref) && !is_base_type(ref->type))
2081 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2083 if (write_ptr)
2084 write_pointer_tfs(file, type, tfsoff);
2086 retmask |= 1;
2089 else if (last_array(type) && is_attr(attrs, ATTR_STRING))
2091 write_string_tfs(file, attrs, type, name, tfsoff);
2093 else if (type->declarray && is_conformant_array(type))
2094 ; /* conformant arrays and strings are handled specially */
2095 else if (is_array(type))
2097 write_array_tfs(file, attrs, type, name, tfsoff);
2099 else if (is_struct(type->type))
2101 if (!processed(type))
2102 write_struct_tfs(file, type, name, tfsoff);
2104 else if (is_union(type->type))
2106 if (!processed(type))
2107 write_union_tfs(file, type, tfsoff);
2109 else if (!is_base_type(type->type))
2110 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2111 name, type->type);
2113 return retmask;
2116 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2118 const ifref_t * iface;
2119 const func_t *func;
2120 const var_t *var;
2122 if (ifaces)
2123 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2124 if (iface->iface->funcs)
2125 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2126 if (func->args)
2127 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2128 set_tfswrite(var->type, val);
2131 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
2133 const var_t *var;
2134 const ifref_t *iface;
2135 unsigned int typeformat_offset = 2;
2137 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2139 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2140 continue;
2142 if (iface->iface->funcs)
2144 const func_t *func;
2145 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2147 if (is_local(func->def->attrs)) continue;
2149 current_func = func;
2150 if (func->args)
2151 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2152 update_tfsoff(
2153 var->type,
2154 write_typeformatstring_var(
2155 file, 2, func, var->type, var,
2156 &typeformat_offset),
2157 file);
2162 return typeformat_offset + 1;
2166 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2168 int indent = 0;
2170 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2171 print_file(file, indent, "{\n");
2172 indent++;
2173 print_file(file, indent, "0,\n");
2174 print_file(file, indent, "{\n");
2175 indent++;
2176 print_file(file, indent, "NdrFcShort(0x0),\n");
2178 set_all_tfswrite(ifaces, TRUE);
2179 process_tfs(file, ifaces, for_objects);
2181 print_file(file, indent, "0x0\n");
2182 indent--;
2183 print_file(file, indent, "}\n");
2184 indent--;
2185 print_file(file, indent, "};\n");
2186 print_file(file, indent, "\n");
2189 static unsigned int get_required_buffer_size_type(
2190 const type_t *type, const char *name, unsigned int *alignment)
2192 *alignment = 0;
2193 if (is_user_type(type))
2195 const char *uname;
2196 const type_t *utype = get_user_type(type, &uname);
2197 return get_required_buffer_size_type(utype, uname, alignment);
2199 else
2201 switch (type->type)
2203 case RPC_FC_BYTE:
2204 case RPC_FC_CHAR:
2205 case RPC_FC_USMALL:
2206 case RPC_FC_SMALL:
2207 *alignment = 4;
2208 return 1;
2210 case RPC_FC_WCHAR:
2211 case RPC_FC_USHORT:
2212 case RPC_FC_SHORT:
2213 case RPC_FC_ENUM16:
2214 *alignment = 4;
2215 return 2;
2217 case RPC_FC_ULONG:
2218 case RPC_FC_LONG:
2219 case RPC_FC_ENUM32:
2220 case RPC_FC_FLOAT:
2221 case RPC_FC_ERROR_STATUS_T:
2222 *alignment = 4;
2223 return 4;
2225 case RPC_FC_HYPER:
2226 case RPC_FC_DOUBLE:
2227 *alignment = 8;
2228 return 8;
2230 case RPC_FC_IGNORE:
2231 case RPC_FC_BIND_PRIMITIVE:
2232 return 0;
2234 case RPC_FC_STRUCT:
2235 case RPC_FC_PSTRUCT:
2237 size_t size = 0;
2238 const var_t *field;
2239 if (!type->fields) return 0;
2240 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2242 unsigned int alignment;
2243 size += get_required_buffer_size_type(field->type, field->name,
2244 &alignment);
2246 return size;
2249 case RPC_FC_RP:
2250 return
2251 is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
2252 ? get_required_buffer_size_type( type->ref, name, alignment )
2253 : 0;
2255 case RPC_FC_SMFARRAY:
2256 case RPC_FC_LGFARRAY:
2257 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2259 default:
2260 return 0;
2265 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2267 int in_attr = is_attr(var->attrs, ATTR_IN);
2268 int out_attr = is_attr(var->attrs, ATTR_OUT);
2270 if (!in_attr && !out_attr)
2271 in_attr = 1;
2273 *alignment = 0;
2275 if (pass == PASS_OUT)
2277 if (out_attr && is_ptr(var->type))
2279 type_t *type = var->type;
2281 if (type->type == RPC_FC_STRUCT)
2283 const var_t *field;
2284 unsigned int size = 36;
2286 if (!type->fields) return size;
2287 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2289 unsigned int align;
2290 size += get_required_buffer_size_type(
2291 field->type, field->name, &align);
2293 return size;
2296 return 0;
2298 else
2300 if ((!out_attr || in_attr) && !var->type->size_is
2301 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2303 if (is_ptr(var->type))
2305 type_t *type = var->type;
2307 if (is_base_type(type->type))
2309 return 25;
2311 else if (type->type == RPC_FC_STRUCT)
2313 unsigned int size = 36;
2314 const var_t *field;
2316 if (!type->fields) return size;
2317 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2319 unsigned int align;
2320 size += get_required_buffer_size_type(
2321 field->type, field->name, &align);
2323 return size;
2328 return get_required_buffer_size_type(var->type, var->name, alignment);
2332 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2334 const var_t *var;
2335 unsigned int total_size = 0, alignment;
2337 if (func->args)
2339 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2341 total_size += get_required_buffer_size(var, &alignment, pass);
2342 total_size += alignment;
2346 if (pass == PASS_OUT && !is_void(func->def->type))
2348 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2349 total_size += alignment;
2351 return total_size;
2354 static void print_phase_function(FILE *file, int indent, const char *type,
2355 enum remoting_phase phase,
2356 const var_t *var, unsigned int type_offset)
2358 const char *function;
2359 switch (phase)
2361 case PHASE_BUFFERSIZE:
2362 function = "BufferSize";
2363 break;
2364 case PHASE_MARSHAL:
2365 function = "Marshall";
2366 break;
2367 case PHASE_UNMARSHAL:
2368 function = "Unmarshall";
2369 break;
2370 case PHASE_FREE:
2371 function = "Free";
2372 break;
2373 default:
2374 assert(0);
2375 return;
2378 print_file(file, indent, "Ndr%s%s(\n", type, function);
2379 indent++;
2380 print_file(file, indent, "&_StubMsg,\n");
2381 print_file(file, indent, "%s%s%s%s,\n",
2382 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2383 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2384 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2385 var->name);
2386 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2387 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2388 if (phase == PHASE_UNMARSHAL)
2389 print_file(file, indent, "0);\n");
2390 indent--;
2393 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2394 enum pass pass, const var_t *var,
2395 const char *varname)
2397 type_t *type = var->type;
2398 unsigned int size;
2399 unsigned int alignment = 0;
2400 unsigned char rtype;
2402 /* no work to do for other phases, buffer sizing is done elsewhere */
2403 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2404 return;
2406 rtype = is_ptr(type) ? type->ref->type : type->type;
2408 switch (rtype)
2410 case RPC_FC_BYTE:
2411 case RPC_FC_CHAR:
2412 case RPC_FC_SMALL:
2413 case RPC_FC_USMALL:
2414 size = 1;
2415 alignment = 1;
2416 break;
2418 case RPC_FC_WCHAR:
2419 case RPC_FC_USHORT:
2420 case RPC_FC_SHORT:
2421 case RPC_FC_ENUM16:
2422 size = 2;
2423 alignment = 2;
2424 break;
2426 case RPC_FC_ULONG:
2427 case RPC_FC_LONG:
2428 case RPC_FC_ENUM32:
2429 case RPC_FC_FLOAT:
2430 case RPC_FC_ERROR_STATUS_T:
2431 size = 4;
2432 alignment = 4;
2433 break;
2435 case RPC_FC_HYPER:
2436 case RPC_FC_DOUBLE:
2437 size = 8;
2438 alignment = 8;
2439 break;
2441 case RPC_FC_IGNORE:
2442 case RPC_FC_BIND_PRIMITIVE:
2443 /* no marshalling needed */
2444 return;
2446 default:
2447 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2448 size = 0;
2451 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2452 alignment - 1, alignment - 1);
2454 if (phase == PHASE_MARSHAL)
2456 print_file(file, indent, "*(");
2457 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2458 if (is_ptr(type))
2459 fprintf(file, " *)_StubMsg.Buffer = *");
2460 else
2461 fprintf(file, " *)_StubMsg.Buffer = ");
2462 fprintf(file, varname);
2463 fprintf(file, ";\n");
2465 else if (phase == PHASE_UNMARSHAL)
2467 if (pass == PASS_IN || pass == PASS_RETURN)
2468 print_file(file, indent, "");
2469 else
2470 print_file(file, indent, "*");
2471 fprintf(file, varname);
2472 if (pass == PASS_IN && is_ptr(type))
2473 fprintf(file, " = (");
2474 else
2475 fprintf(file, " = *(");
2476 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2477 fprintf(file, " *)_StubMsg.Buffer;\n");
2480 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2481 write_type(file, var->type, FALSE, NULL);
2482 fprintf(file, ");\n");
2485 /* returns whether the MaxCount, Offset or ActualCount members need to be
2486 * filled in for the specified phase */
2487 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2489 return (phase != PHASE_UNMARSHAL);
2492 static int needs_freeing(const attr_list_t *attrs, const type_t *t, int out)
2494 return
2495 (is_user_type(t)
2496 || (is_ptr(t)
2497 && (t->ref->type == RPC_FC_IP
2498 || is_ptr(t->ref))))
2499 || (out && (is_string_type(attrs, t)
2500 || is_array(t)));
2503 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2504 enum pass pass, enum remoting_phase phase)
2506 int in_attr, out_attr, pointer_type;
2507 const var_t *var;
2509 if (!func->args)
2510 return;
2512 if (phase == PHASE_BUFFERSIZE)
2514 unsigned int size = get_function_buffer_size( func, pass );
2515 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2518 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2520 const type_t *type = var->type;
2521 unsigned char rtype;
2522 size_t start_offset = type->typestring_offset;
2524 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2525 if (!pointer_type)
2526 pointer_type = RPC_FC_RP;
2528 in_attr = is_attr(var->attrs, ATTR_IN);
2529 out_attr = is_attr(var->attrs, ATTR_OUT);
2530 if (!in_attr && !out_attr)
2531 in_attr = 1;
2533 if (phase == PHASE_FREE)
2535 if (!needs_freeing(var->attrs, type, out_attr))
2536 continue;
2538 else
2539 switch (pass)
2541 case PASS_IN:
2542 if (!in_attr) continue;
2543 break;
2544 case PASS_OUT:
2545 if (!out_attr) continue;
2546 break;
2547 case PASS_RETURN:
2548 break;
2551 rtype = type->type;
2553 if (is_user_type(var->type))
2555 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2557 else if (is_string_type(var->attrs, var->type))
2559 if (is_array(type) && !is_conformant_array(type))
2560 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2561 else
2563 if (type->size_is && is_size_needed_for_phase(phase))
2565 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2566 write_expr(file, type->size_is, 1);
2567 fprintf(file, ";\n");
2570 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2571 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2572 else
2573 print_phase_function(file, indent, "ConformantString", phase, var,
2574 start_offset + (type->size_is ? 4 : 2));
2577 else if (is_array(type))
2579 unsigned char tc = type->type;
2580 const char *array_type;
2582 if (tc == RPC_FC_SMFARRAY || tc == RPC_FC_LGFARRAY)
2583 array_type = "FixedArray";
2584 else if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2586 if (is_size_needed_for_phase(phase))
2588 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2589 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2590 write_expr(file, type->length_is, 1);
2591 fprintf(file, ";\n\n");
2593 array_type = "VaryingArray";
2595 else if (tc == RPC_FC_CARRAY)
2597 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2599 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2600 write_expr(file, type->size_is, 1);
2601 fprintf(file, ";\n\n");
2603 array_type = "ConformantArray";
2605 else if (tc == RPC_FC_CVARRAY)
2607 if (is_size_needed_for_phase(phase))
2609 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2610 write_expr(file, type->size_is, 1);
2611 fprintf(file, ";\n");
2612 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2613 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2614 write_expr(file, type->length_is, 1);
2615 fprintf(file, ";\n\n");
2617 array_type = "ConformantVaryingArray";
2619 else
2620 array_type = "ComplexArray";
2622 if (!in_attr && phase == PHASE_FREE)
2624 print_file(file, indent, "if (%s)\n", var->name);
2625 indent++;
2626 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2628 else if (phase != PHASE_FREE)
2630 if (pointer_type == RPC_FC_UP)
2631 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2632 else
2633 print_phase_function(file, indent, array_type, phase, var, start_offset);
2636 else if (!is_ptr(var->type) && is_base_type(rtype))
2638 print_phase_basetype(file, indent, phase, pass, var, var->name);
2640 else if (!is_ptr(var->type))
2642 switch (rtype)
2644 case RPC_FC_STRUCT:
2645 case RPC_FC_PSTRUCT:
2646 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2647 break;
2648 case RPC_FC_CSTRUCT:
2649 case RPC_FC_CPSTRUCT:
2650 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2651 break;
2652 case RPC_FC_CVSTRUCT:
2653 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2654 break;
2655 case RPC_FC_BOGUS_STRUCT:
2656 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2657 break;
2658 case RPC_FC_RP:
2659 if (is_base_type( var->type->ref->type ))
2661 print_phase_basetype(file, indent, phase, pass, var, var->name);
2663 else if (var->type->ref->type == RPC_FC_STRUCT)
2665 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2666 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2668 else
2670 const var_t *iid;
2671 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2672 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2673 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2675 break;
2676 default:
2677 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2680 else
2682 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2684 print_phase_basetype(file, indent, phase, pass, var, var->name);
2686 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2688 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2689 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2691 else
2693 const var_t *iid;
2694 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2695 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2696 if (var->type->ref->type == RPC_FC_IP)
2697 print_phase_function(file, indent, "InterfacePointer", phase, var, start_offset);
2698 else
2699 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2702 fprintf(file, "\n");
2707 size_t get_size_procformatstring_var(const var_t *var)
2709 return write_procformatstring_var(NULL, 0, var, FALSE);
2713 size_t get_size_procformatstring_func(const func_t *func)
2715 const var_t *var;
2716 size_t size = 0;
2718 /* argument list size */
2719 if (func->args)
2720 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2721 size += get_size_procformatstring_var(var);
2723 /* return value size */
2724 if (is_void(func->def->type))
2725 size += 2; /* FC_END and FC_PAD */
2726 else
2727 size += get_size_procformatstring_var(func->def);
2729 return size;
2732 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2734 const ifref_t *iface;
2735 size_t size = 1;
2736 const func_t *func;
2738 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2740 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2741 continue;
2743 if (iface->iface->funcs)
2744 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2745 if (!is_local(func->def->attrs))
2746 size += get_size_procformatstring_func( func );
2748 return size;
2751 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2753 set_all_tfswrite(ifaces, FALSE);
2754 return process_tfs(NULL, ifaces, for_objects);
2757 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2758 const var_list_t *fields, const char *structvar)
2760 switch (e->type) {
2761 case EXPR_VOID:
2762 break;
2763 case EXPR_NUM:
2764 fprintf(h, "%lu", e->u.lval);
2765 break;
2766 case EXPR_HEXNUM:
2767 fprintf(h, "0x%lx", e->u.lval);
2768 break;
2769 case EXPR_DOUBLE:
2770 fprintf(h, "%#.15g", e->u.dval);
2771 break;
2772 case EXPR_TRUEFALSE:
2773 if (e->u.lval == 0)
2774 fprintf(h, "FALSE");
2775 else
2776 fprintf(h, "TRUE");
2777 break;
2778 case EXPR_IDENTIFIER:
2780 const var_t *field;
2781 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2782 if (!strcmp(e->u.sval, field->name))
2784 fprintf(h, "%s->%s", structvar, e->u.sval);
2785 break;
2788 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2789 break;
2791 case EXPR_NEG:
2792 fprintf(h, "-");
2793 write_struct_expr(h, e->ref, 1, fields, structvar);
2794 break;
2795 case EXPR_NOT:
2796 fprintf(h, "~");
2797 write_struct_expr(h, e->ref, 1, fields, structvar);
2798 break;
2799 case EXPR_PPTR:
2800 fprintf(h, "*");
2801 write_struct_expr(h, e->ref, 1, fields, structvar);
2802 break;
2803 case EXPR_CAST:
2804 fprintf(h, "(");
2805 write_type(h, e->u.tref, FALSE, NULL);
2806 fprintf(h, ")");
2807 write_struct_expr(h, e->ref, 1, fields, structvar);
2808 break;
2809 case EXPR_SIZEOF:
2810 fprintf(h, "sizeof(");
2811 write_type(h, e->u.tref, FALSE, NULL);
2812 fprintf(h, ")");
2813 break;
2814 case EXPR_SHL:
2815 case EXPR_SHR:
2816 case EXPR_MUL:
2817 case EXPR_DIV:
2818 case EXPR_ADD:
2819 case EXPR_SUB:
2820 case EXPR_AND:
2821 case EXPR_OR:
2822 if (brackets) fprintf(h, "(");
2823 write_struct_expr(h, e->ref, 1, fields, structvar);
2824 switch (e->type) {
2825 case EXPR_SHL: fprintf(h, " << "); break;
2826 case EXPR_SHR: fprintf(h, " >> "); break;
2827 case EXPR_MUL: fprintf(h, " * "); break;
2828 case EXPR_DIV: fprintf(h, " / "); break;
2829 case EXPR_ADD: fprintf(h, " + "); break;
2830 case EXPR_SUB: fprintf(h, " - "); break;
2831 case EXPR_AND: fprintf(h, " & "); break;
2832 case EXPR_OR: fprintf(h, " | "); break;
2833 default: break;
2835 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2836 if (brackets) fprintf(h, ")");
2837 break;
2838 case EXPR_COND:
2839 if (brackets) fprintf(h, "(");
2840 write_struct_expr(h, e->ref, 1, fields, structvar);
2841 fprintf(h, " ? ");
2842 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2843 fprintf(h, " : ");
2844 write_struct_expr(h, e->ext2, 1, fields, structvar);
2845 if (brackets) fprintf(h, ")");
2846 break;
2851 void declare_stub_args( FILE *file, int indent, const func_t *func )
2853 int in_attr, out_attr;
2854 int i = 0;
2855 const var_t *def = func->def;
2856 const var_t *var;
2858 /* declare return value '_RetVal' */
2859 if (!is_void(def->type))
2861 print_file(file, indent, "");
2862 write_type_left(file, def->type);
2863 fprintf(file, " _RetVal;\n");
2866 if (!func->args)
2867 return;
2869 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2871 int is_string = is_attr(var->attrs, ATTR_STRING);
2873 in_attr = is_attr(var->attrs, ATTR_IN);
2874 out_attr = is_attr(var->attrs, ATTR_OUT);
2875 if (!out_attr && !in_attr)
2876 in_attr = 1;
2878 if (!in_attr && !var->type->size_is && !is_string)
2880 print_file(file, indent, "");
2881 write_type(file, var->type->ref, FALSE, "_W%u", i++);
2882 fprintf(file, ";\n");
2885 print_file(file, indent, "");
2886 write_type_left(file, var->type);
2887 fprintf(file, " ");
2888 if (var->type->declarray) {
2889 fprintf(file, "( *");
2890 write_name(file, var);
2891 fprintf(file, " )");
2892 } else
2893 write_name(file, var);
2894 write_type_right(file, var->type, FALSE);
2895 fprintf(file, ";\n");
2897 if (decl_indirect(var->type))
2898 print_file(file, indent, "void *_p_%s = &%s;\n",
2899 var->name, var->name);
2904 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2906 int in_attr, out_attr;
2907 int i = 0, sep = 0;
2908 const var_t *var;
2910 if (!func->args)
2911 return;
2913 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2915 int is_string = is_attr(var->attrs, ATTR_STRING);
2916 in_attr = is_attr(var->attrs, ATTR_IN);
2917 out_attr = is_attr(var->attrs, ATTR_OUT);
2918 if (!out_attr && !in_attr)
2919 in_attr = 1;
2921 if (!in_attr)
2923 print_file(file, indent, "");
2924 write_name(file, var);
2926 if (var->type->size_is)
2928 unsigned int size, align = 0;
2929 type_t *type = var->type;
2931 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2932 for ( ; type->size_is ; type = type->ref)
2934 write_expr(file, type->size_is, TRUE);
2935 fprintf(file, " * ");
2937 size = type_memsize(type, &align);
2938 fprintf(file, "%u);\n", size);
2940 else if (!is_string)
2942 fprintf(file, " = &_W%u;\n", i);
2943 if (is_ptr(var->type) && !last_ptr(var->type))
2944 print_file(file, indent, "_W%u = 0;\n", i);
2945 i++;
2948 sep = 1;
2951 if (sep)
2952 fprintf(file, "\n");
2956 int write_expr_eval_routines(FILE *file, const char *iface)
2958 static const char *var_name = "pS";
2959 int result = 0;
2960 struct expr_eval_routine *eval;
2961 unsigned short callback_offset = 0;
2963 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2965 const char *name = eval->structure->name;
2966 const var_list_t *fields = eval->structure->fields;
2967 unsigned align = 0;
2968 result = 1;
2970 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2971 iface, name, callback_offset);
2972 print_file(file, 0, "{\n");
2973 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
2974 name, var_name, name, type_memsize (eval->structure, &align));
2975 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
2976 print_file(file, 1, "pStubMsg->MaxCount = (unsigned long)");
2977 write_struct_expr(file, eval->expr, 1, fields, var_name);
2978 fprintf(file, ";\n");
2979 print_file(file, 0, "}\n\n");
2980 callback_offset++;
2982 return result;
2985 void write_expr_eval_routine_list(FILE *file, const char *iface)
2987 struct expr_eval_routine *eval;
2988 struct expr_eval_routine *cursor;
2989 unsigned short callback_offset = 0;
2991 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2992 fprintf(file, "{\n");
2994 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2996 const char *name = eval->structure->name;
2997 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
2998 callback_offset++;
2999 list_remove(&eval->entry);
3000 free(eval);
3003 fprintf(file, "};\n\n");
3006 void write_user_quad_list(FILE *file)
3008 user_type_t *ut;
3010 if (list_empty(&user_type_list))
3011 return;
3013 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3014 fprintf(file, "{\n");
3015 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3017 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3018 print_file(file, 1, "{\n");
3019 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3020 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3021 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3022 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3023 print_file(file, 1, "}%s\n", sep);
3025 fprintf(file, "};\n\n");
3028 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3030 const struct str_list_entry_t *endpoint;
3031 const char *p;
3033 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3034 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3035 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3037 print_file( f, 1, "{ (const unsigned char *)\"" );
3038 for (p = endpoint->str; *p && *p != ':'; p++)
3040 if (*p == '"' || *p == '\\') fputc( '\\', f );
3041 fputc( *p, f );
3043 if (!*p) goto error;
3044 if (p[1] != '[') goto error;
3046 fprintf( f, "\", (const unsigned char *)\"" );
3047 for (p += 2; *p && *p != ']'; p++)
3049 if (*p == '"' || *p == '\\') fputc( '\\', f );
3050 fputc( *p, f );
3052 if (*p != ']') goto error;
3053 fprintf( f, "\" },\n" );
3055 print_file( f, 0, "};\n\n" );
3056 return;
3058 error:
3059 error("Invalid endpoint syntax '%s'\n", endpoint->str);