widl: Handle wire_marshal pointer attributes.
[wine/wine-gecko.git] / tools / widl / typegen.c
blob0b115ab7949ac3e75a3e454c5a0330979ea2592b
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include <limits.h>
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "windef.h"
41 #include "wine/list.h"
43 #include "widl.h"
44 #include "typegen.h"
46 static const func_t *current_func;
47 static const type_t *current_structure;
49 /* name of the structure variable for structure callbacks */
50 #define STRUCT_EXPR_EVAL_VAR "pS"
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 size_t structure_size;
59 const expr_t *expr;
62 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
63 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
67 const char *string_of_type(unsigned char type)
69 switch (type)
71 case RPC_FC_BYTE: return "FC_BYTE";
72 case RPC_FC_CHAR: return "FC_CHAR";
73 case RPC_FC_SMALL: return "FC_SMALL";
74 case RPC_FC_USMALL: return "FC_USMALL";
75 case RPC_FC_WCHAR: return "FC_WCHAR";
76 case RPC_FC_SHORT: return "FC_SHORT";
77 case RPC_FC_USHORT: return "FC_USHORT";
78 case RPC_FC_LONG: return "FC_LONG";
79 case RPC_FC_ULONG: return "FC_ULONG";
80 case RPC_FC_FLOAT: return "FC_FLOAT";
81 case RPC_FC_HYPER: return "FC_HYPER";
82 case RPC_FC_DOUBLE: return "FC_DOUBLE";
83 case RPC_FC_ENUM16: return "FC_ENUM16";
84 case RPC_FC_ENUM32: return "FC_ENUM32";
85 case RPC_FC_IGNORE: return "FC_IGNORE";
86 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
87 case RPC_FC_RP: return "FC_RP";
88 case RPC_FC_UP: return "FC_UP";
89 case RPC_FC_OP: return "FC_OP";
90 case RPC_FC_FP: return "FC_FP";
91 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
92 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
93 case RPC_FC_STRUCT: return "FC_STRUCT";
94 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
95 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
96 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
97 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
98 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
99 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
100 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
101 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
102 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
103 case RPC_FC_CARRAY: return "FC_CARRAY";
104 case RPC_FC_CVARRAY: return "FC_CVARRAY";
105 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
106 default:
107 error("string_of_type: unknown type 0x%02x\n", type);
108 return NULL;
112 int is_struct(unsigned char type)
114 switch (type)
116 case RPC_FC_STRUCT:
117 case RPC_FC_PSTRUCT:
118 case RPC_FC_CSTRUCT:
119 case RPC_FC_CPSTRUCT:
120 case RPC_FC_CVSTRUCT:
121 case RPC_FC_BOGUS_STRUCT:
122 return 1;
123 default:
124 return 0;
128 int is_union(unsigned char type)
130 switch (type)
132 case RPC_FC_ENCAPSULATED_UNION:
133 case RPC_FC_NON_ENCAPSULATED_UNION:
134 return 1;
135 default:
136 return 0;
140 static unsigned short user_type_offset(const char *name)
142 user_type_t *ut;
143 unsigned short off = 0;
144 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
146 if (strcmp(name, ut->name) == 0)
147 return off;
148 ++off;
150 error("user_type_offset: couldn't find type (%s)\n", name);
151 return 0;
154 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
156 type->typestring_offset = offset;
157 if (file) type->tfswrite = FALSE;
160 static void guard_rec(type_t *type)
162 /* types that contain references to themselves (like a linked list),
163 need to be shielded from infinite recursion when writing embedded
164 types */
165 if (type->typestring_offset)
166 type->tfswrite = FALSE;
167 else
168 type->typestring_offset = 1;
171 static type_t *get_user_type(const type_t *t, const char **pname)
173 for (;;)
175 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
176 if (ut)
178 if (pname)
179 *pname = t->name;
180 return ut;
183 if (t->kind == TKIND_ALIAS)
184 t = t->orig;
185 else
186 return 0;
190 static int is_user_type(const type_t *t)
192 return get_user_type(t, NULL) != NULL;
195 static int is_embedded_complex(const type_t *type)
197 unsigned char tc = type->type;
198 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type);
201 static int compare_expr(const expr_t *a, const expr_t *b)
203 int ret;
205 if (a->type != b->type)
206 return a->type - b->type;
208 switch (a->type)
210 case EXPR_NUM:
211 case EXPR_HEXNUM:
212 case EXPR_TRUEFALSE:
213 return a->u.lval - b->u.lval;
214 case EXPR_IDENTIFIER:
215 return strcmp(a->u.sval, b->u.sval);
216 case EXPR_COND:
217 ret = compare_expr(a->ref, b->ref);
218 if (ret != 0)
219 return ret;
220 ret = compare_expr(a->u.ext, b->u.ext);
221 if (ret != 0)
222 return ret;
223 return compare_expr(a->ext2, b->ext2);
224 case EXPR_OR:
225 case EXPR_AND:
226 case EXPR_ADD:
227 case EXPR_SUB:
228 case EXPR_MUL:
229 case EXPR_DIV:
230 case EXPR_SHL:
231 case EXPR_SHR:
232 ret = compare_expr(a->ref, b->ref);
233 if (ret != 0)
234 return ret;
235 return compare_expr(a->u.ext, b->u.ext);
236 case EXPR_NOT:
237 case EXPR_NEG:
238 case EXPR_PPTR:
239 case EXPR_CAST:
240 case EXPR_SIZEOF:
241 return compare_expr(a->ref, b->ref);
242 case EXPR_VOID:
243 return 0;
245 return -1;
248 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
249 do { \
250 if (file) \
251 fprintf(file, "/* %2u */\n", typestring_offset); \
252 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
254 while (0)
256 static int print_file(FILE *file, int indent, const char *format, ...)
258 va_list va;
259 int i, r;
261 if (!file) return 0;
263 va_start(va, format);
264 for (i = 0; i < indent; i++)
265 fprintf(file, " ");
266 r = vfprintf(file, format, va);
267 va_end(va);
268 return r;
271 static void write_formatdesc(FILE *f, int indent, const char *str)
273 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
274 print_file(f, indent, "{\n");
275 print_file(f, indent + 1, "short Pad;\n");
276 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
277 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
278 print_file(f, indent, "\n");
281 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
283 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
284 get_size_typeformatstring(ifaces, for_objects));
286 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
287 get_size_procformatstring(ifaces, for_objects));
289 fprintf(f, "\n");
290 write_formatdesc(f, indent, "TYPE");
291 write_formatdesc(f, indent, "PROC");
292 fprintf(f, "\n");
293 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
294 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
295 print_file(f, indent, "\n");
298 static inline int is_base_type(unsigned char type)
300 switch (type)
302 case RPC_FC_BYTE:
303 case RPC_FC_CHAR:
304 case RPC_FC_USMALL:
305 case RPC_FC_SMALL:
306 case RPC_FC_WCHAR:
307 case RPC_FC_USHORT:
308 case RPC_FC_SHORT:
309 case RPC_FC_ULONG:
310 case RPC_FC_LONG:
311 case RPC_FC_HYPER:
312 case RPC_FC_IGNORE:
313 case RPC_FC_FLOAT:
314 case RPC_FC_DOUBLE:
315 case RPC_FC_ENUM16:
316 case RPC_FC_ENUM32:
317 case RPC_FC_ERROR_STATUS_T:
318 case RPC_FC_BIND_PRIMITIVE:
319 return TRUE;
321 default:
322 return FALSE;
326 int decl_indirect(const type_t *t)
328 return is_user_type(t)
329 || (!is_base_type(t->type)
330 && !is_ptr(t)
331 && !is_array(t));
334 static size_t write_procformatstring_var(FILE *file, int indent,
335 const var_t *var, int is_return)
337 size_t size;
338 const type_t *type = var->type;
340 int is_in = is_attr(var->attrs, ATTR_IN);
341 int is_out = is_attr(var->attrs, ATTR_OUT);
343 if (!is_in && !is_out) is_in = TRUE;
345 if (!type->declarray && is_base_type(type->type))
347 if (is_return)
348 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
349 else
350 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
352 if (type->type == RPC_FC_BIND_PRIMITIVE)
354 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
355 size = 2; /* includes param type prefix */
357 else if (is_base_type(type->type))
359 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
360 size = 2; /* includes param type prefix */
362 else
364 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
365 size = 0;
368 else
370 if (is_return)
371 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
372 else if (is_in && is_out)
373 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
374 else if (is_out)
375 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
376 else
377 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
379 print_file(file, indent, "0x01,\n");
380 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
381 size = 4; /* includes param type prefix */
383 return size;
386 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
388 const ifref_t *iface;
389 int indent = 0;
390 const var_t *var;
392 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
393 print_file(file, indent, "{\n");
394 indent++;
395 print_file(file, indent, "0,\n");
396 print_file(file, indent, "{\n");
397 indent++;
399 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
401 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
402 continue;
404 if (iface->iface->funcs)
406 const func_t *func;
407 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
409 if (is_local(func->def->attrs)) continue;
410 /* emit argument data */
411 if (func->args)
413 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
414 write_procformatstring_var(file, indent, var, FALSE);
417 /* emit return value data */
418 var = func->def;
419 if (is_void(var->type))
421 print_file(file, indent, "0x5b, /* FC_END */\n");
422 print_file(file, indent, "0x5c, /* FC_PAD */\n");
424 else
425 write_procformatstring_var(file, indent, var, TRUE);
430 print_file(file, indent, "0x0\n");
431 indent--;
432 print_file(file, indent, "}\n");
433 indent--;
434 print_file(file, indent, "};\n");
435 print_file(file, indent, "\n");
438 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
440 if (is_base_type(type->type))
442 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
443 *typestring_offset += 1;
444 return 1;
447 return 0;
450 /* write conformance / variance descriptor */
451 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure,
452 unsigned int baseoff, const expr_t *expr)
454 unsigned char operator_type = 0;
455 const char *operator_string = "no operators";
456 const expr_t *subexpr;
457 unsigned char correlation_type;
459 if (!file) return 4; /* optimisation for sizing pass */
461 if (expr->is_const)
463 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
464 error("write_conf_or_var_desc: constant value %ld is greater than "
465 "the maximum constant size of %d\n", expr->cval,
466 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
468 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
469 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
470 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
471 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
473 return 4;
476 subexpr = expr;
477 switch (subexpr->type)
479 case EXPR_PPTR:
480 subexpr = subexpr->ref;
481 operator_type = RPC_FC_DEREFERENCE;
482 operator_string = "FC_DEREFERENCE";
483 break;
484 case EXPR_DIV:
485 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
487 subexpr = subexpr->ref;
488 operator_type = RPC_FC_DIV_2;
489 operator_string = "FC_DIV_2";
491 break;
492 case EXPR_MUL:
493 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
495 subexpr = subexpr->ref;
496 operator_type = RPC_FC_MULT_2;
497 operator_string = "FC_MULT_2";
499 break;
500 case EXPR_SUB:
501 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
503 subexpr = subexpr->ref;
504 operator_type = RPC_FC_SUB_1;
505 operator_string = "FC_SUB_1";
507 break;
508 case EXPR_ADD:
509 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
511 subexpr = subexpr->ref;
512 operator_type = RPC_FC_ADD_1;
513 operator_string = "FC_ADD_1";
515 break;
516 default:
517 break;
520 if (subexpr->type == EXPR_IDENTIFIER)
522 const type_t *correlation_variable = NULL;
523 unsigned char correlation_variable_type;
524 unsigned char param_type = 0;
525 const char *param_type_string = NULL;
526 size_t offset;
528 if (structure)
530 const var_t *var;
532 offset = 0;
533 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
535 unsigned int align = 0;
536 /* FIXME: take alignment into account */
537 if (!strcmp(var->name, subexpr->u.sval))
539 correlation_variable = var->type;
540 break;
542 offset += type_memsize(var->type, &align);
544 if (!correlation_variable)
545 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
546 subexpr->u.sval);
548 offset -= baseoff;
549 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
551 else
553 const var_t *var;
555 offset = sizeof(void *);
556 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
558 if (!strcmp(var->name, subexpr->u.sval))
560 correlation_variable = var->type;
561 break;
563 /* FIXME: not all stack variables are sizeof(void *) */
564 offset += sizeof(void *);
566 if (!correlation_variable)
567 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
568 subexpr->u.sval);
570 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
573 correlation_variable_type = correlation_variable->type;
575 switch (correlation_variable_type)
577 case RPC_FC_CHAR:
578 case RPC_FC_SMALL:
579 param_type = RPC_FC_SMALL;
580 param_type_string = "FC_SMALL";
581 break;
582 case RPC_FC_BYTE:
583 case RPC_FC_USMALL:
584 param_type = RPC_FC_USMALL;
585 param_type_string = "FC_USMALL";
586 break;
587 case RPC_FC_WCHAR:
588 case RPC_FC_SHORT:
589 param_type = RPC_FC_SHORT;
590 param_type_string = "FC_SHORT";
591 break;
592 case RPC_FC_USHORT:
593 param_type = RPC_FC_USHORT;
594 param_type_string = "FC_USHORT";
595 break;
596 case RPC_FC_LONG:
597 param_type = RPC_FC_LONG;
598 param_type_string = "FC_LONG";
599 break;
600 case RPC_FC_ULONG:
601 param_type = RPC_FC_ULONG;
602 param_type_string = "FC_ULONG";
603 break;
604 case RPC_FC_RP:
605 case RPC_FC_UP:
606 case RPC_FC_OP:
607 case RPC_FC_FP:
608 if (sizeof(void *) == 4) /* FIXME */
610 param_type = RPC_FC_LONG;
611 param_type_string = "FC_LONG";
613 else
615 param_type = RPC_FC_HYPER;
616 param_type_string = "FC_HYPER";
618 break;
619 default:
620 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
621 correlation_variable_type);
624 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
625 correlation_type | param_type,
626 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
627 param_type_string);
628 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
629 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
630 offset,
631 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
632 offset);
634 else
636 unsigned int callback_offset = 0;
638 if (structure)
640 struct expr_eval_routine *eval;
641 int found = 0;
643 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
645 if (!strcmp(eval->structure->name, structure->name) &&
646 !compare_expr(eval->expr, expr))
648 found = 1;
649 break;
651 callback_offset++;
654 if (!found)
656 unsigned int align = 0;
657 eval = xmalloc(sizeof(*eval));
658 eval->structure = structure;
659 eval->structure_size = fields_memsize(structure->fields, &align);
660 eval->expr = expr;
661 list_add_tail(&expr_eval_routines, &eval->entry);
664 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
666 else
668 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
669 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
672 if (callback_offset > USHRT_MAX)
673 error("Maximum number of callback routines reached\n");
675 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
676 correlation_type,
677 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
678 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
679 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
681 return 4;
684 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
686 size_t size = 0;
687 const var_t *v;
689 if (!fields) return 0;
690 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
691 size += type_memsize(v->type, align);
693 return size;
696 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
698 size_t size, maxs = 0;
699 unsigned int align = *pmaxa;
700 const var_t *v;
702 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
704 /* we could have an empty default field with NULL type */
705 if (v->type)
707 size = type_memsize(v->type, &align);
708 if (maxs < size) maxs = size;
709 if (*pmaxa < align) *pmaxa = align;
713 return maxs;
716 size_t type_memsize(const type_t *t, unsigned int *align)
718 size_t size = 0;
720 if (t->declarray && is_conformant_array(t))
722 type_memsize(t->ref, align);
723 size = 0;
725 else if (is_ptr(t) || is_conformant_array(t))
727 size = sizeof(void *);
728 if (size > *align) *align = size;
730 else switch (t->type)
732 case RPC_FC_BYTE:
733 case RPC_FC_CHAR:
734 case RPC_FC_USMALL:
735 case RPC_FC_SMALL:
736 size = 1;
737 if (size > *align) *align = size;
738 break;
739 case RPC_FC_WCHAR:
740 case RPC_FC_USHORT:
741 case RPC_FC_SHORT:
742 case RPC_FC_ENUM16:
743 size = 2;
744 if (size > *align) *align = size;
745 break;
746 case RPC_FC_ULONG:
747 case RPC_FC_LONG:
748 case RPC_FC_ERROR_STATUS_T:
749 case RPC_FC_ENUM32:
750 case RPC_FC_FLOAT:
751 size = 4;
752 if (size > *align) *align = size;
753 break;
754 case RPC_FC_HYPER:
755 case RPC_FC_DOUBLE:
756 size = 8;
757 if (size > *align) *align = size;
758 break;
759 case RPC_FC_STRUCT:
760 case RPC_FC_CVSTRUCT:
761 case RPC_FC_CPSTRUCT:
762 case RPC_FC_CSTRUCT:
763 case RPC_FC_PSTRUCT:
764 case RPC_FC_BOGUS_STRUCT:
765 size = fields_memsize(t->fields, align);
766 break;
767 case RPC_FC_ENCAPSULATED_UNION:
768 case RPC_FC_NON_ENCAPSULATED_UNION:
769 size = union_memsize(t->fields, align);
770 break;
771 case RPC_FC_SMFARRAY:
772 case RPC_FC_LGFARRAY:
773 case RPC_FC_SMVARRAY:
774 case RPC_FC_LGVARRAY:
775 case RPC_FC_BOGUS_ARRAY:
776 size = t->dim * type_memsize(t->ref, align);
777 break;
778 default:
779 error("type_memsize: Unknown type %d\n", t->type);
780 size = 0;
783 return size;
786 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
788 short absoff = type->ref->typestring_offset;
789 short reloff = absoff - (offset + 2);
790 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
792 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
793 type->type, ptr_attr, string_of_type(type->type));
794 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
795 reloff, reloff, absoff);
796 return 4;
799 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
801 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
802 type->type, string_of_type(type->type));
803 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
804 string_of_type(type->ref->type));
805 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
806 return 4;
809 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
811 unsigned int offset = *typestring_offset;
813 print_file(file, 0, "/* %d */\n", offset);
814 update_tfsoff(type, offset, file);
816 if (type->ref->typestring_offset)
817 *typestring_offset += write_nonsimple_pointer(file, type, offset);
818 else if (is_base_type(type->ref->type))
819 *typestring_offset += write_simple_pointer(file, type);
821 return offset;
824 static int processed(const type_t *type)
826 return type->typestring_offset && !type->tfswrite;
829 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
831 unsigned int start, absoff, flags;
832 unsigned int align = 0, ualign = 0;
833 const char *name;
834 type_t *utype = get_user_type(type, &name);
835 size_t usize = type_memsize(utype, &ualign);
836 size_t size = type_memsize(type, &align);
837 unsigned short funoff = user_type_offset(name);
838 short reloff;
840 guard_rec(type);
842 if (is_base_type(utype->type))
844 absoff = *tfsoff;
845 print_file(file, 0, "/* %d */\n", absoff);
846 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
847 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
848 *tfsoff += 2;
850 else
852 if (!processed(utype))
853 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
854 absoff = utype->typestring_offset;
857 if (utype->type == RPC_FC_RP)
858 flags = 0x40;
859 else if (utype->type == RPC_FC_UP)
860 flags = 0x80;
861 else
862 flags = 0;
864 start = *tfsoff;
865 update_tfsoff(type, start, file);
866 print_file(file, 0, "/* %d */\n", start);
867 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
868 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
869 flags | (align - 1), align - 1, flags);
870 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
871 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
872 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
873 *tfsoff += 8;
874 reloff = absoff - *tfsoff;
875 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
876 *tfsoff += 2;
879 static void write_member_type(FILE *file, type_t *type, const var_t *field,
880 unsigned int *corroff, unsigned int *tfsoff)
882 if (is_ptr(type))
884 print_file(file, 2, "0x8,\t/* FC_LONG */\n");
885 *tfsoff += 1;
887 else if (is_embedded_complex(type))
889 size_t absoff;
890 short reloff;
892 if (is_union(type->type) && is_attr(field->attrs, ATTR_SWITCHIS))
894 absoff = *corroff;
895 *corroff += 8;
897 else
899 absoff = type->typestring_offset;
901 reloff = absoff - (*tfsoff + 2);
903 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
904 /* FIXME: actually compute necessary padding */
905 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
906 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
907 reloff, reloff, absoff);
908 *tfsoff += 4;
910 else if (!write_base_type(file, type, tfsoff))
911 error("Unsupported member type 0x%x\n", type->type);
914 static void write_end(FILE *file, unsigned int *tfsoff)
916 if (*tfsoff % 2 == 0)
918 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
919 *tfsoff += 1;
921 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
922 *tfsoff += 1;
925 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
927 unsigned int offset = 0;
928 var_list_t *fs = type->fields;
929 var_t *f;
931 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
933 unsigned int align = 0;
934 type_t *ft = f->type;
935 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
937 unsigned int absoff = ft->typestring_offset;
938 short reloff = absoff - (*tfsoff + 6);
939 print_file(file, 0, "/* %d */\n", *tfsoff);
940 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
941 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
942 write_conf_or_var_desc(file, current_func, current_structure, offset,
943 get_attrp(f->attrs, ATTR_SWITCHIS));
944 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
945 reloff, reloff, absoff);
946 *tfsoff += 8;
949 /* FIXME: take alignment into account */
950 offset += type_memsize(ft, &align);
954 static size_t write_pointer_description(FILE *file, type_t *type, size_t mem_offset,
955 int level, unsigned int *typestring_offset)
957 const var_t *v;
958 unsigned int align = 0;
960 /* don't generate a pointer for first-level arrays since we want to
961 * descend into them to write their pointers, not stop here */
962 if (level == 0 && is_array(type))
964 write_pointer_description(file, type->ref, mem_offset, level + 1,
965 typestring_offset);
967 else if (is_ptr(type))
969 print_file(file, 2, "0x46,\t/* FC_NO_REPEAT */\n");
970 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
971 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
972 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
973 *typestring_offset += 6;
975 if (processed(type->ref) || is_base_type(type->ref->type))
976 write_pointer_tfs(file, type, typestring_offset);
977 else
978 error("write_pointer_description: type format string unknown\n");
980 else if (level == 0 && is_struct(type->type))
982 if (type->fields)
984 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
985 mem_offset
986 += write_pointer_description(file, v->type, mem_offset,
987 level + 1, typestring_offset);
991 return type_memsize(type, &align);
994 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
995 const type_t *type,
996 const char *name, unsigned int *typestring_offset)
998 size_t start_offset = *typestring_offset;
999 unsigned char flags = 0;
1000 int pointer_type;
1001 unsigned char rtype;
1003 if (is_ptr(type))
1005 pointer_type = type->type;
1006 type = type->ref;
1008 else
1009 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1011 if (!pointer_type)
1012 pointer_type = RPC_FC_RP;
1014 if (!get_attrp(attrs, ATTR_SIZEIS))
1015 flags |= RPC_FC_P_SIMPLEPOINTER;
1017 rtype = type->type;
1019 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1021 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1022 return start_offset;
1025 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
1026 pointer_type, flags,
1027 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
1028 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
1029 *typestring_offset += 2;
1031 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
1033 print_file(file, 2, "NdrFcShort(0x2),\n");
1034 *typestring_offset += 2;
1037 if (type->declarray && !is_conformant_array(type))
1039 /* FIXME: multi-dimensional array */
1040 if (0xffffuL < type->dim)
1041 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1042 name, 0xffffu, type->dim - 0xffffu);
1044 if (rtype == RPC_FC_CHAR)
1045 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1046 else
1047 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1048 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1049 *typestring_offset += 2;
1051 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1052 *typestring_offset += 2;
1054 return start_offset;
1056 else if (type->size_is)
1058 unsigned int align = 0;
1060 if (rtype == RPC_FC_CHAR)
1061 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1062 else
1063 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1064 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1065 *typestring_offset += 2;
1067 *typestring_offset += write_conf_or_var_desc(
1068 file, current_func, current_structure,
1069 (type->declarray && current_structure
1070 ? type_memsize(current_structure, &align)
1071 : 0),
1072 type->size_is);
1074 return start_offset;
1076 else
1078 if (rtype == RPC_FC_CHAR)
1079 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1080 else
1081 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1082 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1083 *typestring_offset += 2;
1085 return start_offset;
1089 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1090 const char *name, unsigned int *typestring_offset)
1092 const expr_t *length_is = type->length_is;
1093 const expr_t *size_is = type->size_is;
1094 unsigned int align = 0;
1095 size_t size;
1096 size_t start_offset;
1097 int has_pointer;
1098 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1099 if (!pointer_type)
1100 pointer_type = RPC_FC_RP;
1102 has_pointer = FALSE;
1103 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1104 has_pointer = TRUE;
1106 size = type_memsize(type, &align);
1107 if (size == 0) /* conformant array */
1108 size = type_memsize(type->ref, &align);
1110 start_offset = *typestring_offset;
1111 update_tfsoff(type, start_offset, file);
1112 print_file(file, 0, "/* %lu */\n", start_offset);
1113 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1114 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1115 *typestring_offset += 2;
1117 align = 0;
1118 if (type->type != RPC_FC_BOGUS_ARRAY)
1120 unsigned char tc = type->type;
1121 unsigned int baseoff
1122 = type->declarray && current_structure
1123 ? type_memsize(current_structure, &align)
1124 : 0;
1126 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1128 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1129 *typestring_offset += 4;
1131 else
1133 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1134 *typestring_offset += 2;
1137 if (is_conformant_array(type))
1138 *typestring_offset
1139 += write_conf_or_var_desc(file, current_func, current_structure,
1140 baseoff, size_is);
1142 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1144 unsigned int elalign = 0;
1145 size_t elsize = type_memsize(type->ref, &elalign);
1147 if (type->type == RPC_FC_LGVARRAY)
1149 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1150 *typestring_offset += 4;
1152 else
1154 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1155 *typestring_offset += 2;
1158 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1159 *typestring_offset += 2;
1162 if (length_is)
1163 *typestring_offset
1164 += write_conf_or_var_desc(file, current_func, current_structure,
1165 baseoff, length_is);
1167 if (has_pointer)
1169 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1170 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1171 *typestring_offset += 2;
1172 write_pointer_description(file, type, 0, 0, typestring_offset);
1173 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1174 *typestring_offset += 1;
1177 write_member_type(file, type->ref, NULL, NULL, typestring_offset);
1178 write_end(file, typestring_offset);
1180 else
1181 error("%s: complex arrays unimplemented\n", name);
1183 return start_offset;
1186 static const var_t *find_array_or_string_in_struct(const type_t *type)
1188 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1189 const type_t *ft = last_field->type;
1191 if (ft->declarray && is_conformant_array(ft))
1192 return last_field;
1194 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1195 return find_array_or_string_in_struct(last_field->type);
1196 else
1197 return NULL;
1200 static void write_struct_members(FILE *file, const type_t *type,
1201 unsigned int *corroff, unsigned int *typestring_offset)
1203 const var_t *field;
1205 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1207 type_t *ft = field->type;
1208 if (!ft->declarray || !is_conformant_array(ft))
1209 write_member_type(file, ft, field, corroff, typestring_offset);
1212 write_end(file, typestring_offset);
1215 static size_t write_struct_tfs(FILE *file, type_t *type,
1216 const char *name, unsigned int *tfsoff)
1218 const type_t *save_current_structure = current_structure;
1219 unsigned int total_size;
1220 const var_t *array;
1221 size_t start_offset;
1222 size_t array_offset;
1223 int has_pointers = 0;
1224 unsigned int align = 0;
1225 unsigned int corroff;
1226 var_t *f;
1228 guard_rec(type);
1229 current_structure = type;
1231 total_size = type_memsize(type, &align);
1232 if (total_size > USHRT_MAX)
1233 error("structure size for %s exceeds %d bytes by %d bytes\n",
1234 name, USHRT_MAX, total_size - USHRT_MAX);
1236 if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1237 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1238 FALSE, tfsoff);
1240 array = find_array_or_string_in_struct(type);
1241 if (array && !processed(array->type))
1242 array_offset
1243 = is_attr(array->attrs, ATTR_STRING)
1244 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1245 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1247 corroff = *tfsoff;
1248 write_descriptors(file, type, tfsoff);
1250 start_offset = *tfsoff;
1251 update_tfsoff(type, start_offset, file);
1252 print_file(file, 0, "/* %d */\n", start_offset);
1253 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1254 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1255 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1256 *tfsoff += 4;
1258 if (array)
1260 unsigned int absoff = array->type->typestring_offset;
1261 short reloff = absoff - *tfsoff;
1262 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1263 reloff, reloff, absoff);
1264 *tfsoff += 2;
1266 else if (type->type == RPC_FC_BOGUS_STRUCT)
1268 print_file(file, 2, "NdrFcShort(0x0),\n");
1269 *tfsoff += 2;
1272 if (type->type == RPC_FC_BOGUS_STRUCT)
1275 print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1276 *tfsoff += 2;
1278 else if (has_pointers)
1280 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1281 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1282 *tfsoff += 2;
1283 write_pointer_description(file, type, 0, 0, tfsoff);
1284 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1285 *tfsoff += 1;
1288 write_struct_members(file, type, &corroff, tfsoff);
1290 current_structure = save_current_structure;
1291 return start_offset;
1294 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1295 unsigned char flags, size_t offset,
1296 unsigned int *typeformat_offset)
1298 size_t start_offset = *typeformat_offset;
1299 short reloff = offset - (*typeformat_offset + 2);
1300 int in_attr, out_attr;
1301 in_attr = is_attr(attrs, ATTR_IN);
1302 out_attr = is_attr(attrs, ATTR_OUT);
1303 if (!in_attr && !out_attr) in_attr = 1;
1305 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1306 flags |= 0x04;
1308 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1309 pointer_type,
1310 flags,
1311 string_of_type(pointer_type));
1312 if (file)
1314 if (flags & 0x04)
1315 fprintf(file, " [allocated_on_stack]");
1316 if (flags & 0x10)
1317 fprintf(file, " [pointer_deref]");
1318 fprintf(file, " */\n");
1321 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1322 *typeformat_offset += 4;
1324 return start_offset;
1327 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1329 if (is_base_type(t->type))
1331 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1332 t->type, string_of_type(t->type));
1334 else if (t->typestring_offset)
1336 short reloff = t->typestring_offset - *tfsoff;
1337 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1338 reloff, reloff, t->typestring_offset);
1340 else
1341 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1343 *tfsoff += 2;
1346 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1348 unsigned int align = 0;
1349 unsigned int start_offset;
1350 size_t size = type_memsize(type, &align);
1351 var_list_t *fields = type->fields;
1352 size_t nbranch = 0;
1353 type_t *deftype = NULL;
1354 short nodeftype = 0xffff;
1355 var_t *f;
1357 guard_rec(type);
1359 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1361 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1362 if (cases)
1363 nbranch += list_count(cases);
1364 if (f->type)
1365 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1368 start_offset = *tfsoff;
1369 update_tfsoff(type, start_offset, file);
1370 print_file(file, 0, "/* %d */\n", start_offset);
1371 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1372 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1373 *tfsoff += 4;
1375 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1377 type_t *ft = f->type;
1378 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1379 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1380 expr_t *c;
1382 if (cases == NULL && !deflt)
1383 error("union field %s with neither case nor default attribute\n", f->name);
1385 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1387 /* MIDL doesn't check for duplicate cases, even though that seems
1388 like a reasonable thing to do, it just dumps them to the TFS
1389 like we're going to do here. */
1390 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1391 *tfsoff += 4;
1392 write_branch_type(file, ft, tfsoff);
1395 /* MIDL allows multiple default branches, even though that seems
1396 illogical, it just chooses the last one, which is what we will
1397 do. */
1398 if (deflt)
1400 deftype = ft;
1401 nodeftype = 0;
1405 if (deftype)
1407 write_branch_type(file, deftype, tfsoff);
1409 else
1411 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1412 *tfsoff += 2;
1415 return start_offset;
1418 static size_t write_ip_tfs(FILE *file, const func_t *func, const type_t *type, const var_t *var,
1419 unsigned int *typeformat_offset)
1421 size_t i;
1422 size_t start_offset = *typeformat_offset;
1423 const var_t *iid = get_attrp(var->attrs, ATTR_IIDIS);
1425 if (iid)
1427 expr_t expr;
1429 expr.type = EXPR_IDENTIFIER;
1430 expr.ref = NULL;
1431 expr.u.sval = iid->name;
1432 expr.is_const = FALSE;
1433 print_file(file, 2, "0x2f, /* FC_IP */\n");
1434 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1435 *typeformat_offset += write_conf_or_var_desc(file, func, NULL, 0, &expr) + 2;
1437 else
1439 const type_t *base = is_ptr(type) ? type->ref : type;
1440 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1442 if (! uuid)
1443 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1445 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1446 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1447 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1448 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1449 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1450 for (i = 0; i < 8; ++i)
1451 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1453 if (file)
1454 fprintf(file, "\n");
1456 *typeformat_offset += 18;
1458 return start_offset;
1461 static int get_ptr_attr(const type_t *t, int def_type)
1463 while (TRUE)
1465 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1466 if (ptr_attr)
1467 return ptr_attr;
1468 if (t->kind != TKIND_ALIAS)
1469 return def_type;
1470 t = t->orig;
1474 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1475 type_t *type, const var_t *var,
1476 unsigned int *typeformat_offset)
1478 int pointer_type;
1479 size_t offset;
1481 if (is_user_type(type))
1483 write_user_tfs(file, type, typeformat_offset);
1484 return type->typestring_offset;
1487 if (type == var->type) /* top-level pointers */
1489 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1490 if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1491 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1493 if (pointer_attr == 0)
1494 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1496 pointer_type = pointer_attr;
1498 else
1499 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1501 if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1502 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1504 if (is_array(type))
1505 return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1507 if (!is_ptr(type))
1509 /* basic types don't need a type format string */
1510 if (is_base_type(type->type))
1511 return 0;
1513 switch (type->type)
1515 case RPC_FC_STRUCT:
1516 case RPC_FC_PSTRUCT:
1517 case RPC_FC_CSTRUCT:
1518 case RPC_FC_CPSTRUCT:
1519 case RPC_FC_CVSTRUCT:
1520 case RPC_FC_BOGUS_STRUCT:
1521 return write_struct_tfs(file, type, var->name, typeformat_offset);
1522 case RPC_FC_ENCAPSULATED_UNION:
1523 case RPC_FC_NON_ENCAPSULATED_UNION:
1524 return write_union_tfs(file, type, typeformat_offset);
1525 case RPC_FC_IGNORE:
1526 case RPC_FC_BIND_PRIMITIVE:
1527 /* nothing to do */
1528 return 0;
1529 default:
1530 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1533 else if (last_ptr(type))
1535 size_t start_offset = *typeformat_offset;
1536 int in_attr = is_attr(var->attrs, ATTR_IN);
1537 int out_attr = is_attr(var->attrs, ATTR_OUT);
1538 const type_t *base = type->ref;
1540 if (base->type == RPC_FC_IP)
1542 return write_ip_tfs(file, func, type, var, typeformat_offset);
1545 /* special case for pointers to base types */
1546 if (is_base_type(base->type))
1548 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1549 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1550 string_of_type(pointer_type),
1551 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1552 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
1553 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1554 *typeformat_offset += 4;
1555 return start_offset;
1559 assert(is_ptr(type));
1561 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1562 if (file)
1563 fprintf(file, "/* %2u */\n", *typeformat_offset);
1564 return write_pointer_only_tfs(file, var->attrs, pointer_type,
1565 !last_ptr(type) ? 0x10 : 0,
1566 offset, typeformat_offset);
1569 static void set_tfswrite(type_t *type, int val)
1571 while (type->tfswrite != val)
1573 type_t *utype = get_user_type(type, NULL);
1575 type->tfswrite = val;
1577 if (utype)
1578 set_tfswrite(utype, val);
1580 if (type->kind == TKIND_ALIAS)
1581 type = type->orig;
1582 else if (is_ptr(type) || is_array(type))
1583 type = type->ref;
1584 else
1586 if (type->fields)
1588 var_t *v;
1589 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1590 if (v->type)
1591 set_tfswrite(v->type, val);
1594 return;
1599 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
1600 const char *name, int write_ptr, unsigned int *tfsoff)
1602 int retmask = 0;
1604 if (is_user_type(type))
1606 write_user_tfs(file, type, tfsoff);
1608 else if (is_ptr(type))
1610 type_t *ref = type->ref;
1612 if (!processed(ref) && !is_base_type(ref->type))
1613 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
1615 /* top-level pointers are handled inline for structures */
1616 if (write_ptr)
1617 write_pointer_tfs(file, type, tfsoff);
1619 retmask |= 1;
1621 else if (type->declarray && is_conformant_array(type))
1622 ; /* conformant arrays and strings are handled specially */
1623 else if (is_array(type))
1625 write_array_tfs(file, attrs, type, name, tfsoff);
1627 else if (is_struct(type->type))
1629 if (!processed(type))
1630 write_struct_tfs(file, type, name, tfsoff);
1632 else if (is_union(type->type))
1634 if (!processed(type))
1635 write_union_tfs(file, type, tfsoff);
1637 else if (!is_base_type(type->type))
1638 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
1639 name, type->type);
1641 return retmask;
1644 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
1646 const ifref_t * iface;
1647 const func_t *func;
1648 const var_t *var;
1650 if (ifaces)
1651 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1652 if (iface->iface->funcs)
1653 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1654 if (func->args)
1655 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1656 set_tfswrite(var->type, val);
1659 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
1661 const var_t *var;
1662 const ifref_t *iface;
1663 unsigned int typeformat_offset = 2;
1665 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1667 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1668 continue;
1670 if (iface->iface->funcs)
1672 const func_t *func;
1673 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1675 if (is_local(func->def->attrs)) continue;
1677 current_func = func;
1678 if (func->args)
1679 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1680 update_tfsoff(
1681 var->type,
1682 write_typeformatstring_var(
1683 file, 2, func, var->type, var,
1684 &typeformat_offset),
1685 file);
1690 return typeformat_offset + 1;
1694 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1696 int indent = 0;
1698 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1699 print_file(file, indent, "{\n");
1700 indent++;
1701 print_file(file, indent, "0,\n");
1702 print_file(file, indent, "{\n");
1703 indent++;
1704 print_file(file, indent, "NdrFcShort(0x0),\n");
1706 set_all_tfswrite(ifaces, TRUE);
1707 process_tfs(file, ifaces, for_objects);
1709 print_file(file, indent, "0x0\n");
1710 indent--;
1711 print_file(file, indent, "}\n");
1712 indent--;
1713 print_file(file, indent, "};\n");
1714 print_file(file, indent, "\n");
1717 static unsigned int get_required_buffer_size_type(
1718 const type_t *type, const char *name, unsigned int *alignment)
1720 size_t size = 0;
1722 *alignment = 0;
1723 if (!is_ptr(type))
1725 switch (type->type)
1727 case RPC_FC_BYTE:
1728 case RPC_FC_CHAR:
1729 case RPC_FC_USMALL:
1730 case RPC_FC_SMALL:
1731 *alignment = 4;
1732 size = 1;
1733 break;
1735 case RPC_FC_WCHAR:
1736 case RPC_FC_USHORT:
1737 case RPC_FC_SHORT:
1738 *alignment = 4;
1739 size = 2;
1740 break;
1742 case RPC_FC_ULONG:
1743 case RPC_FC_LONG:
1744 case RPC_FC_FLOAT:
1745 case RPC_FC_ERROR_STATUS_T:
1746 *alignment = 4;
1747 size = 4;
1748 break;
1750 case RPC_FC_HYPER:
1751 case RPC_FC_DOUBLE:
1752 *alignment = 8;
1753 size = 8;
1754 break;
1756 case RPC_FC_IGNORE:
1757 case RPC_FC_BIND_PRIMITIVE:
1758 return 0;
1760 case RPC_FC_STRUCT:
1761 case RPC_FC_PSTRUCT:
1763 const var_t *field;
1764 if (!type->fields) return 0;
1765 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1767 unsigned int alignment;
1768 size += get_required_buffer_size_type(field->type, field->name,
1769 &alignment);
1771 break;
1774 case RPC_FC_RP:
1775 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1776 size = get_required_buffer_size_type( type->ref, name, alignment );
1777 break;
1779 case RPC_FC_SMFARRAY:
1780 case RPC_FC_LGFARRAY:
1781 size = type->dim * get_required_buffer_size_type(type->ref, name, alignment);
1782 break;
1784 case RPC_FC_SMVARRAY:
1785 case RPC_FC_LGVARRAY:
1786 get_required_buffer_size_type(type->ref, name, alignment);
1787 size = 0;
1788 break;
1790 case RPC_FC_CARRAY:
1791 case RPC_FC_CVARRAY:
1792 get_required_buffer_size_type(type->ref, name, alignment);
1793 size = sizeof(void *);
1794 break;
1796 default:
1797 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1798 return 0;
1801 return size;
1804 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1806 int in_attr = is_attr(var->attrs, ATTR_IN);
1807 int out_attr = is_attr(var->attrs, ATTR_OUT);
1809 if (!in_attr && !out_attr)
1810 in_attr = 1;
1812 *alignment = 0;
1814 if (pass == PASS_OUT)
1816 if (out_attr && is_ptr(var->type))
1818 type_t *type = var->type;
1820 if (type->type == RPC_FC_STRUCT)
1822 const var_t *field;
1823 unsigned int size = 36;
1825 if (!type->fields) return size;
1826 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1828 unsigned int align;
1829 size += get_required_buffer_size_type(
1830 field->type, field->name, &align);
1832 return size;
1835 return 0;
1837 else
1839 if ((!out_attr || in_attr) && !var->type->size_is
1840 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
1842 if (is_ptr(var->type))
1844 type_t *type = var->type;
1846 if (is_base_type(type->type))
1848 return 25;
1850 else if (type->type == RPC_FC_STRUCT)
1852 unsigned int size = 36;
1853 const var_t *field;
1855 if (!type->fields) return size;
1856 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1858 unsigned int align;
1859 size += get_required_buffer_size_type(
1860 field->type, field->name, &align);
1862 return size;
1867 return get_required_buffer_size_type(var->type, var->name, alignment);
1871 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
1873 const var_t *var;
1874 unsigned int total_size = 0, alignment;
1876 if (func->args)
1878 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1880 total_size += get_required_buffer_size(var, &alignment, pass);
1881 total_size += alignment;
1885 if (pass == PASS_OUT && !is_void(func->def->type))
1887 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
1888 total_size += alignment;
1890 return total_size;
1893 static void print_phase_function(FILE *file, int indent, const char *type,
1894 enum remoting_phase phase,
1895 const var_t *var, unsigned int type_offset)
1897 const char *function;
1898 switch (phase)
1900 case PHASE_BUFFERSIZE:
1901 function = "BufferSize";
1902 break;
1903 case PHASE_MARSHAL:
1904 function = "Marshall";
1905 break;
1906 case PHASE_UNMARSHAL:
1907 function = "Unmarshall";
1908 break;
1909 case PHASE_FREE:
1910 function = "Free";
1911 break;
1912 default:
1913 assert(0);
1914 return;
1917 print_file(file, indent, "Ndr%s%s(\n", type, function);
1918 indent++;
1919 print_file(file, indent, "&_StubMsg,\n");
1920 print_file(file, indent, "%s%s%s%s,\n",
1921 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
1922 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
1923 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
1924 var->name);
1925 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1926 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1927 if (phase == PHASE_UNMARSHAL)
1928 print_file(file, indent, "0);\n");
1929 indent--;
1932 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1933 enum pass pass, const var_t *var,
1934 const char *varname)
1936 type_t *type = var->type;
1937 unsigned int size;
1938 unsigned int alignment = 0;
1939 unsigned char rtype;
1941 /* no work to do for other phases, buffer sizing is done elsewhere */
1942 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1943 return;
1945 rtype = is_ptr(type) ? type->ref->type : type->type;
1947 switch (rtype)
1949 case RPC_FC_BYTE:
1950 case RPC_FC_CHAR:
1951 case RPC_FC_SMALL:
1952 case RPC_FC_USMALL:
1953 size = 1;
1954 alignment = 1;
1955 break;
1957 case RPC_FC_WCHAR:
1958 case RPC_FC_USHORT:
1959 case RPC_FC_SHORT:
1960 size = 2;
1961 alignment = 2;
1962 break;
1964 case RPC_FC_ULONG:
1965 case RPC_FC_LONG:
1966 case RPC_FC_FLOAT:
1967 case RPC_FC_ERROR_STATUS_T:
1968 size = 4;
1969 alignment = 4;
1970 break;
1972 case RPC_FC_HYPER:
1973 case RPC_FC_DOUBLE:
1974 size = 8;
1975 alignment = 8;
1976 break;
1978 case RPC_FC_IGNORE:
1979 case RPC_FC_BIND_PRIMITIVE:
1980 /* no marshalling needed */
1981 return;
1983 default:
1984 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1985 size = 0;
1988 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1989 alignment - 1, alignment - 1);
1991 if (phase == PHASE_MARSHAL)
1993 print_file(file, indent, "*(");
1994 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
1995 if (is_ptr(type))
1996 fprintf(file, " *)_StubMsg.Buffer = *");
1997 else
1998 fprintf(file, " *)_StubMsg.Buffer = ");
1999 fprintf(file, varname);
2000 fprintf(file, ";\n");
2002 else if (phase == PHASE_UNMARSHAL)
2004 if (pass == PASS_IN || pass == PASS_RETURN)
2005 print_file(file, indent, "");
2006 else
2007 print_file(file, indent, "*");
2008 fprintf(file, varname);
2009 if (pass == PASS_IN && is_ptr(type))
2010 fprintf(file, " = (");
2011 else
2012 fprintf(file, " = *(");
2013 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2014 fprintf(file, " *)_StubMsg.Buffer;\n");
2017 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2018 write_type(file, var->type, FALSE, NULL);
2019 fprintf(file, ");\n");
2022 /* returns whether the MaxCount, Offset or ActualCount members need to be
2023 * filled in for the specified phase */
2024 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2026 return (phase != PHASE_UNMARSHAL);
2029 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2030 enum pass pass, enum remoting_phase phase)
2032 int in_attr, out_attr, pointer_type;
2033 const var_t *var;
2035 if (!func->args)
2036 return;
2038 if (phase == PHASE_BUFFERSIZE)
2040 unsigned int size = get_function_buffer_size( func, pass );
2041 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2044 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2046 const type_t *type = var->type;
2047 unsigned char rtype;
2048 size_t start_offset = type->typestring_offset;
2050 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2051 if (!pointer_type)
2052 pointer_type = RPC_FC_RP;
2054 in_attr = is_attr(var->attrs, ATTR_IN);
2055 out_attr = is_attr(var->attrs, ATTR_OUT);
2056 if (!in_attr && !out_attr)
2057 in_attr = 1;
2059 switch (pass)
2061 case PASS_IN:
2062 if (!in_attr) continue;
2063 break;
2064 case PASS_OUT:
2065 if (!out_attr) continue;
2066 break;
2067 case PASS_RETURN:
2068 break;
2071 rtype = type->type;
2073 if (is_user_type(var->type))
2075 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2077 else if (is_string_type(var->attrs, var->type))
2079 if (is_array(type) && !is_conformant_array(type))
2080 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2081 else
2083 if (type->size_is && is_size_needed_for_phase(phase))
2085 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2086 write_expr(file, type->size_is, 1);
2087 fprintf(file, ";\n");
2090 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2091 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2092 else
2093 print_phase_function(file, indent, "ConformantString", phase, var,
2094 start_offset + (type->size_is ? 4 : 2));
2097 else if (is_array(type))
2099 unsigned char tc = type->type;
2100 const char *array_type;
2102 if (tc == RPC_FC_SMFARRAY || tc == RPC_FC_LGFARRAY)
2103 array_type = "FixedArray";
2104 else if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2106 if (is_size_needed_for_phase(phase))
2108 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2109 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2110 write_expr(file, type->length_is, 1);
2111 fprintf(file, ";\n\n");
2113 array_type = "VaryingArray";
2115 else if (tc == RPC_FC_CARRAY)
2117 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2119 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2120 write_expr(file, type->size_is, 1);
2121 fprintf(file, ";\n\n");
2123 array_type = "ConformantArray";
2125 else if (tc == RPC_FC_CVARRAY)
2127 if (is_size_needed_for_phase(phase))
2129 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2130 write_expr(file, type->size_is, 1);
2131 fprintf(file, ";\n");
2132 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2133 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2134 write_expr(file, type->length_is, 1);
2135 fprintf(file, ";\n\n");
2137 array_type = "ConformantVaryingArray";
2139 else
2140 array_type = "ComplexArray";
2142 if (!in_attr && phase == PHASE_FREE)
2144 print_file(file, indent, "if (%s)\n", var->name);
2145 indent++;
2146 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2148 else if (phase != PHASE_FREE)
2150 if (pointer_type == RPC_FC_UP)
2151 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2152 else
2153 print_phase_function(file, indent, array_type, phase, var, start_offset);
2156 else if (!is_ptr(var->type) && is_base_type(rtype))
2158 print_phase_basetype(file, indent, phase, pass, var, var->name);
2160 else if (!is_ptr(var->type))
2162 switch (rtype)
2164 case RPC_FC_STRUCT:
2165 case RPC_FC_PSTRUCT:
2166 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2167 break;
2168 case RPC_FC_CSTRUCT:
2169 case RPC_FC_CPSTRUCT:
2170 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2171 break;
2172 case RPC_FC_CVSTRUCT:
2173 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2174 break;
2175 case RPC_FC_BOGUS_STRUCT:
2176 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2177 break;
2178 case RPC_FC_RP:
2179 if (is_base_type( var->type->ref->type ))
2181 print_phase_basetype(file, indent, phase, pass, var, var->name);
2183 else if (var->type->ref->type == RPC_FC_STRUCT)
2185 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2186 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2188 else
2190 const var_t *iid;
2191 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2192 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2193 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2195 break;
2196 default:
2197 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2200 else
2202 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2204 print_phase_basetype(file, indent, phase, pass, var, var->name);
2206 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2208 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2209 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2211 else
2213 const var_t *iid;
2214 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2215 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2216 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2219 fprintf(file, "\n");
2224 size_t get_size_procformatstring_var(const var_t *var)
2226 return write_procformatstring_var(NULL, 0, var, FALSE);
2230 size_t get_size_procformatstring_func(const func_t *func)
2232 const var_t *var;
2233 size_t size = 0;
2235 /* argument list size */
2236 if (func->args)
2237 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2238 size += get_size_procformatstring_var(var);
2240 /* return value size */
2241 if (is_void(func->def->type))
2242 size += 2; /* FC_END and FC_PAD */
2243 else
2244 size += get_size_procformatstring_var(func->def);
2246 return size;
2249 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2251 const ifref_t *iface;
2252 size_t size = 1;
2253 const func_t *func;
2255 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2257 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2258 continue;
2260 if (iface->iface->funcs)
2261 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2262 if (!is_local(func->def->attrs))
2263 size += get_size_procformatstring_func( func );
2265 return size;
2268 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2270 set_all_tfswrite(ifaces, FALSE);
2271 return process_tfs(NULL, ifaces, for_objects);
2274 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2275 const var_list_t *fields, const char *structvar)
2277 switch (e->type) {
2278 case EXPR_VOID:
2279 break;
2280 case EXPR_NUM:
2281 fprintf(h, "%lu", e->u.lval);
2282 break;
2283 case EXPR_HEXNUM:
2284 fprintf(h, "0x%lx", e->u.lval);
2285 break;
2286 case EXPR_TRUEFALSE:
2287 if (e->u.lval == 0)
2288 fprintf(h, "FALSE");
2289 else
2290 fprintf(h, "TRUE");
2291 break;
2292 case EXPR_IDENTIFIER:
2294 const var_t *field;
2295 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2296 if (!strcmp(e->u.sval, field->name))
2298 fprintf(h, "%s->%s", structvar, e->u.sval);
2299 break;
2302 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2303 break;
2305 case EXPR_NEG:
2306 fprintf(h, "-");
2307 write_struct_expr(h, e->ref, 1, fields, structvar);
2308 break;
2309 case EXPR_NOT:
2310 fprintf(h, "~");
2311 write_struct_expr(h, e->ref, 1, fields, structvar);
2312 break;
2313 case EXPR_PPTR:
2314 fprintf(h, "*");
2315 write_struct_expr(h, e->ref, 1, fields, structvar);
2316 break;
2317 case EXPR_CAST:
2318 fprintf(h, "(");
2319 write_type(h, e->u.tref, FALSE, NULL);
2320 fprintf(h, ")");
2321 write_struct_expr(h, e->ref, 1, fields, structvar);
2322 break;
2323 case EXPR_SIZEOF:
2324 fprintf(h, "sizeof(");
2325 write_type(h, e->u.tref, FALSE, NULL);
2326 fprintf(h, ")");
2327 break;
2328 case EXPR_SHL:
2329 case EXPR_SHR:
2330 case EXPR_MUL:
2331 case EXPR_DIV:
2332 case EXPR_ADD:
2333 case EXPR_SUB:
2334 case EXPR_AND:
2335 case EXPR_OR:
2336 if (brackets) fprintf(h, "(");
2337 write_struct_expr(h, e->ref, 1, fields, structvar);
2338 switch (e->type) {
2339 case EXPR_SHL: fprintf(h, " << "); break;
2340 case EXPR_SHR: fprintf(h, " >> "); break;
2341 case EXPR_MUL: fprintf(h, " * "); break;
2342 case EXPR_DIV: fprintf(h, " / "); break;
2343 case EXPR_ADD: fprintf(h, " + "); break;
2344 case EXPR_SUB: fprintf(h, " - "); break;
2345 case EXPR_AND: fprintf(h, " & "); break;
2346 case EXPR_OR: fprintf(h, " | "); break;
2347 default: break;
2349 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2350 if (brackets) fprintf(h, ")");
2351 break;
2352 case EXPR_COND:
2353 if (brackets) fprintf(h, "(");
2354 write_struct_expr(h, e->ref, 1, fields, structvar);
2355 fprintf(h, " ? ");
2356 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2357 fprintf(h, " : ");
2358 write_struct_expr(h, e->ext2, 1, fields, structvar);
2359 if (brackets) fprintf(h, ")");
2360 break;
2365 void declare_stub_args( FILE *file, int indent, const func_t *func )
2367 int in_attr, out_attr;
2368 int i = 0;
2369 const var_t *def = func->def;
2370 const var_t *var;
2372 /* declare return value '_RetVal' */
2373 if (!is_void(def->type))
2375 print_file(file, indent, "");
2376 write_type_left(file, def->type);
2377 fprintf(file, " _RetVal;\n");
2380 if (!func->args)
2381 return;
2383 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2385 int is_string = is_attr(var->attrs, ATTR_STRING);
2387 in_attr = is_attr(var->attrs, ATTR_IN);
2388 out_attr = is_attr(var->attrs, ATTR_OUT);
2389 if (!out_attr && !in_attr)
2390 in_attr = 1;
2392 if (!in_attr && !var->type->size_is && !is_string)
2394 print_file(file, indent, "");
2395 write_type(file, var->type->ref, FALSE, "_W%u", i++);
2396 fprintf(file, ";\n");
2399 print_file(file, indent, "");
2400 write_type_left(file, var->type);
2401 fprintf(file, " ");
2402 if (var->type->declarray) {
2403 fprintf(file, "( *");
2404 write_name(file, var);
2405 fprintf(file, " )");
2406 } else
2407 write_name(file, var);
2408 write_type_right(file, var->type, FALSE);
2409 fprintf(file, ";\n");
2411 if (decl_indirect(var->type))
2412 print_file(file, indent, "void *_p_%s = &%s;\n",
2413 var->name, var->name);
2418 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2420 int in_attr, out_attr;
2421 int i = 0, sep = 0;
2422 const var_t *var;
2424 if (!func->args)
2425 return;
2427 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2429 int is_string = is_attr(var->attrs, ATTR_STRING);
2430 in_attr = is_attr(var->attrs, ATTR_IN);
2431 out_attr = is_attr(var->attrs, ATTR_OUT);
2432 if (!out_attr && !in_attr)
2433 in_attr = 1;
2435 if (!in_attr)
2437 print_file(file, indent, "");
2438 write_name(file, var);
2440 if (var->type->size_is)
2442 unsigned int size, align = 0;
2443 type_t *type = var->type;
2445 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2446 for ( ; type->size_is ; type = type->ref)
2448 write_expr(file, type->size_is, TRUE);
2449 fprintf(file, " * ");
2451 size = type_memsize(type, &align);
2452 fprintf(file, "%u);\n", size);
2454 else if (!is_string)
2456 fprintf(file, " = &_W%u;\n", i);
2457 if (is_ptr(var->type) && !last_ptr(var->type))
2458 print_file(file, indent, "_W%u = 0;\n", i);
2459 i++;
2462 sep = 1;
2465 if (sep)
2466 fprintf(file, "\n");
2470 int write_expr_eval_routines(FILE *file, const char *iface)
2472 int result = 0;
2473 struct expr_eval_routine *eval;
2474 unsigned short callback_offset = 0;
2476 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2478 int indent = 0;
2479 result = 1;
2480 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2481 iface, eval->structure->name, callback_offset);
2482 print_file(file, indent, "{\n");
2483 indent++;
2484 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2485 eval->structure->name, eval->structure->name, eval->structure_size);
2486 fprintf(file, "\n");
2487 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2488 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2489 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2490 fprintf(file, ";\n");
2491 indent--;
2492 print_file(file, indent, "}\n\n");
2493 callback_offset++;
2495 return result;
2498 void write_expr_eval_routine_list(FILE *file, const char *iface)
2500 struct expr_eval_routine *eval;
2501 struct expr_eval_routine *cursor;
2502 unsigned short callback_offset = 0;
2504 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2505 fprintf(file, "{\n");
2507 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2509 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2510 iface, eval->structure->name, callback_offset);
2512 callback_offset++;
2513 list_remove(&eval->entry);
2514 free(eval);
2517 fprintf(file, "};\n\n");
2520 void write_user_quad_list(FILE *file)
2522 user_type_t *ut;
2524 if (list_empty(&user_type_list))
2525 return;
2527 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
2528 fprintf(file, "{\n");
2529 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
2531 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
2532 print_file(file, 1, "{\n");
2533 print_file(file, 2, "%s_UserSize,\n", ut->name);
2534 print_file(file, 2, "%s_UserMarshal,\n", ut->name);
2535 print_file(file, 2, "%s_UserUnmarshal,\n", ut->name);
2536 print_file(file, 2, "%s_UserFree\n", ut->name);
2537 print_file(file, 1, "}%s\n", sep);
2539 fprintf(file, "};\n\n");
2542 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2544 const struct str_list_entry_t *endpoint;
2545 const char *p;
2547 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2548 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2549 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2551 print_file( f, 1, "{ (const unsigned char *)\"" );
2552 for (p = endpoint->str; *p && *p != ':'; p++)
2554 if (*p == '"' || *p == '\\') fputc( '\\', f );
2555 fputc( *p, f );
2557 if (!*p) goto error;
2558 if (p[1] != '[') goto error;
2560 fprintf( f, "\", (const unsigned char *)\"" );
2561 for (p += 2; *p && *p != ']'; p++)
2563 if (*p == '"' || *p == '\\') fputc( '\\', f );
2564 fputc( *p, f );
2566 if (*p != ']') goto error;
2567 fprintf( f, "\" },\n" );
2569 print_file( f, 0, "};\n\n" );
2570 return;
2572 error:
2573 error("Invalid endpoint syntax '%s'\n", endpoint->str);