push ab3cc77eb114d2bd400734a9dccad24563f936a6
[wine/hacks.git] / tools / widl / typegen.c
blobc2e16e5c57c0dac1c5a3188453ebd6c56ae8d6d8
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 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
105 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
106 case RPC_FC_CSTRING: return "FC_CSTRING";
107 case RPC_FC_WSTRING: return "FC_WSTRING";
108 default:
109 error("string_of_type: unknown type 0x%02x\n", type);
110 return NULL;
114 int is_struct(unsigned char type)
116 switch (type)
118 case RPC_FC_STRUCT:
119 case RPC_FC_PSTRUCT:
120 case RPC_FC_CSTRUCT:
121 case RPC_FC_CPSTRUCT:
122 case RPC_FC_CVSTRUCT:
123 case RPC_FC_BOGUS_STRUCT:
124 return 1;
125 default:
126 return 0;
130 static int is_non_complex_struct(const type_t *type)
132 switch (type->type)
134 case RPC_FC_STRUCT:
135 case RPC_FC_PSTRUCT:
136 case RPC_FC_CSTRUCT:
137 case RPC_FC_CPSTRUCT:
138 case RPC_FC_CVSTRUCT:
139 return 1;
140 default:
141 return 0;
145 int is_union(unsigned char type)
147 switch (type)
149 case RPC_FC_ENCAPSULATED_UNION:
150 case RPC_FC_NON_ENCAPSULATED_UNION:
151 return 1;
152 default:
153 return 0;
157 static unsigned short user_type_offset(const char *name)
159 user_type_t *ut;
160 unsigned short off = 0;
161 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
163 if (strcmp(name, ut->name) == 0)
164 return off;
165 ++off;
167 error("user_type_offset: couldn't find type (%s)\n", name);
168 return 0;
171 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
173 type->typestring_offset = offset;
174 if (file) type->tfswrite = FALSE;
177 static void guard_rec(type_t *type)
179 /* types that contain references to themselves (like a linked list),
180 need to be shielded from infinite recursion when writing embedded
181 types */
182 if (type->typestring_offset)
183 type->tfswrite = FALSE;
184 else
185 type->typestring_offset = 1;
188 static type_t *get_user_type(const type_t *t, const char **pname)
190 for (;;)
192 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
193 if (ut)
195 if (pname)
196 *pname = t->name;
197 return ut;
200 if (t->kind == TKIND_ALIAS)
201 t = t->orig;
202 else
203 return 0;
207 int is_user_type(const type_t *t)
209 return get_user_type(t, NULL) != NULL;
212 static int is_embedded_complex(const type_t *type)
214 unsigned char tc = type->type;
215 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
216 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
219 static int compare_expr(const expr_t *a, const expr_t *b)
221 int ret;
223 if (a->type != b->type)
224 return a->type - b->type;
226 switch (a->type)
228 case EXPR_NUM:
229 case EXPR_HEXNUM:
230 case EXPR_TRUEFALSE:
231 return a->u.lval - b->u.lval;
232 case EXPR_DOUBLE:
233 return a->u.dval - b->u.dval;
234 case EXPR_IDENTIFIER:
235 return strcmp(a->u.sval, b->u.sval);
236 case EXPR_COND:
237 ret = compare_expr(a->ref, b->ref);
238 if (ret != 0)
239 return ret;
240 ret = compare_expr(a->u.ext, b->u.ext);
241 if (ret != 0)
242 return ret;
243 return compare_expr(a->ext2, b->ext2);
244 case EXPR_OR:
245 case EXPR_AND:
246 case EXPR_ADD:
247 case EXPR_SUB:
248 case EXPR_MUL:
249 case EXPR_DIV:
250 case EXPR_SHL:
251 case EXPR_SHR:
252 ret = compare_expr(a->ref, b->ref);
253 if (ret != 0)
254 return ret;
255 return compare_expr(a->u.ext, b->u.ext);
256 case EXPR_NOT:
257 case EXPR_NEG:
258 case EXPR_PPTR:
259 case EXPR_CAST:
260 case EXPR_SIZEOF:
261 return compare_expr(a->ref, b->ref);
262 case EXPR_VOID:
263 return 0;
265 return -1;
268 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
269 do { \
270 if (file) \
271 fprintf(file, "/* %2u */\n", typestring_offset); \
272 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
274 while (0)
276 static void print_file(FILE *file, int indent, const char *format, ...)
278 va_list va;
279 va_start(va, format);
280 print(file, indent, format, va);
281 va_end(va);
284 void print(FILE *file, int indent, const char *format, va_list va)
286 if (file)
288 if (format[0] != '\n')
289 while (0 < indent--)
290 fprintf(file, " ");
291 vfprintf(file, format, va);
295 void write_parameters_init(FILE *file, int indent, const func_t *func)
297 const var_t *var;
299 if (!func->args)
300 return;
302 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
304 const type_t *t = var->type;
305 const char *n = var->name;
306 if (decl_indirect(t))
307 print_file(file, indent, "MIDL_memset(&%s, 0, sizeof %s);\n", n, n);
308 else if (is_ptr(t) || is_array(t))
309 print_file(file, indent, "%s = 0;\n", n);
312 fprintf(file, "\n");
315 static void write_formatdesc(FILE *f, int indent, const char *str)
317 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
318 print_file(f, indent, "{\n");
319 print_file(f, indent + 1, "short Pad;\n");
320 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
321 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
322 print_file(f, indent, "\n");
325 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
327 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
328 get_size_typeformatstring(ifaces, for_objects));
330 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
331 get_size_procformatstring(ifaces, for_objects));
333 fprintf(f, "\n");
334 write_formatdesc(f, indent, "TYPE");
335 write_formatdesc(f, indent, "PROC");
336 fprintf(f, "\n");
337 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
338 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
339 print_file(f, indent, "\n");
342 static inline int is_base_type(unsigned char type)
344 switch (type)
346 case RPC_FC_BYTE:
347 case RPC_FC_CHAR:
348 case RPC_FC_USMALL:
349 case RPC_FC_SMALL:
350 case RPC_FC_WCHAR:
351 case RPC_FC_USHORT:
352 case RPC_FC_SHORT:
353 case RPC_FC_ULONG:
354 case RPC_FC_LONG:
355 case RPC_FC_HYPER:
356 case RPC_FC_IGNORE:
357 case RPC_FC_FLOAT:
358 case RPC_FC_DOUBLE:
359 case RPC_FC_ENUM16:
360 case RPC_FC_ENUM32:
361 case RPC_FC_ERROR_STATUS_T:
362 case RPC_FC_BIND_PRIMITIVE:
363 return TRUE;
365 default:
366 return FALSE;
370 int decl_indirect(const type_t *t)
372 return is_user_type(t)
373 || (!is_base_type(t->type)
374 && !is_ptr(t)
375 && !is_array(t));
378 static size_t write_procformatstring_var(FILE *file, int indent,
379 const var_t *var, int is_return)
381 size_t size;
382 const type_t *type = var->type;
384 int is_in = is_attr(var->attrs, ATTR_IN);
385 int is_out = is_attr(var->attrs, ATTR_OUT);
387 if (!is_in && !is_out) is_in = TRUE;
389 if (!type->declarray && is_base_type(type->type))
391 if (is_return)
392 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
393 else
394 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
396 if (type->type == RPC_FC_BIND_PRIMITIVE)
398 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
399 size = 2; /* includes param type prefix */
401 else if (is_base_type(type->type))
403 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
404 size = 2; /* includes param type prefix */
406 else
408 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
409 size = 0;
412 else
414 if (is_return)
415 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
416 else if (is_in && is_out)
417 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
418 else if (is_out)
419 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
420 else
421 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
423 print_file(file, indent, "0x01,\n");
424 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
425 size = 4; /* includes param type prefix */
427 return size;
430 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
432 const ifref_t *iface;
433 int indent = 0;
434 const var_t *var;
436 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
437 print_file(file, indent, "{\n");
438 indent++;
439 print_file(file, indent, "0,\n");
440 print_file(file, indent, "{\n");
441 indent++;
443 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
445 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
446 continue;
448 if (iface->iface->funcs)
450 const func_t *func;
451 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
453 if (is_local(func->def->attrs)) continue;
454 /* emit argument data */
455 if (func->args)
457 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
458 write_procformatstring_var(file, indent, var, FALSE);
461 /* emit return value data */
462 var = func->def;
463 if (is_void(var->type))
465 print_file(file, indent, "0x5b, /* FC_END */\n");
466 print_file(file, indent, "0x5c, /* FC_PAD */\n");
468 else
469 write_procformatstring_var(file, indent, var, TRUE);
474 print_file(file, indent, "0x0\n");
475 indent--;
476 print_file(file, indent, "}\n");
477 indent--;
478 print_file(file, indent, "};\n");
479 print_file(file, indent, "\n");
482 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
484 if (is_base_type(type->type))
486 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
487 *typestring_offset += 1;
488 return 1;
491 return 0;
494 /* write conformance / variance descriptor */
495 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
496 unsigned int baseoff, const expr_t *expr)
498 unsigned char operator_type = 0;
499 const char *operator_string = "no operators";
500 const expr_t *subexpr;
502 if (!expr)
504 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
505 return 4;
508 if (!structure)
510 /* Top-level conformance calculations are done inline. */
511 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
512 RPC_FC_TOP_LEVEL_CONFORMANCE);
513 print_file (file, 2, "0x0,\n");
514 print_file (file, 2, "NdrFcShort(0x0),\n");
515 return 4;
518 if (expr->is_const)
520 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
521 error("write_conf_or_var_desc: constant value %ld is greater than "
522 "the maximum constant size of %d\n", expr->cval,
523 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
525 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
526 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
527 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
528 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
530 return 4;
533 subexpr = expr;
534 switch (subexpr->type)
536 case EXPR_PPTR:
537 subexpr = subexpr->ref;
538 operator_type = RPC_FC_DEREFERENCE;
539 operator_string = "FC_DEREFERENCE";
540 break;
541 case EXPR_DIV:
542 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
544 subexpr = subexpr->ref;
545 operator_type = RPC_FC_DIV_2;
546 operator_string = "FC_DIV_2";
548 break;
549 case EXPR_MUL:
550 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
552 subexpr = subexpr->ref;
553 operator_type = RPC_FC_MULT_2;
554 operator_string = "FC_MULT_2";
556 break;
557 case EXPR_SUB:
558 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
560 subexpr = subexpr->ref;
561 operator_type = RPC_FC_SUB_1;
562 operator_string = "FC_SUB_1";
564 break;
565 case EXPR_ADD:
566 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
568 subexpr = subexpr->ref;
569 operator_type = RPC_FC_ADD_1;
570 operator_string = "FC_ADD_1";
572 break;
573 default:
574 break;
577 if (subexpr->type == EXPR_IDENTIFIER)
579 const type_t *correlation_variable = NULL;
580 unsigned char correlation_variable_type;
581 unsigned char param_type = 0;
582 size_t offset = 0;
583 const var_t *var;
585 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
587 unsigned int align = 0;
588 /* FIXME: take alignment into account */
589 if (var->name && !strcmp(var->name, subexpr->u.sval))
591 correlation_variable = var->type;
592 break;
594 offset += type_memsize(var->type, &align);
596 if (!correlation_variable)
597 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
598 subexpr->u.sval);
600 offset -= baseoff;
601 correlation_variable_type = correlation_variable->type;
603 switch (correlation_variable_type)
605 case RPC_FC_CHAR:
606 case RPC_FC_SMALL:
607 param_type = RPC_FC_SMALL;
608 break;
609 case RPC_FC_BYTE:
610 case RPC_FC_USMALL:
611 param_type = RPC_FC_USMALL;
612 break;
613 case RPC_FC_WCHAR:
614 case RPC_FC_SHORT:
615 case RPC_FC_ENUM16:
616 param_type = RPC_FC_SHORT;
617 break;
618 case RPC_FC_USHORT:
619 param_type = RPC_FC_USHORT;
620 break;
621 case RPC_FC_LONG:
622 case RPC_FC_ENUM32:
623 param_type = RPC_FC_LONG;
624 break;
625 case RPC_FC_ULONG:
626 param_type = RPC_FC_ULONG;
627 break;
628 case RPC_FC_RP:
629 case RPC_FC_UP:
630 case RPC_FC_OP:
631 case RPC_FC_FP:
632 if (sizeof(void *) == 4) /* FIXME */
633 param_type = RPC_FC_LONG;
634 else
635 param_type = RPC_FC_HYPER;
636 break;
637 default:
638 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
639 correlation_variable_type);
642 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
643 RPC_FC_NORMAL_CONFORMANCE | param_type, string_of_type(param_type));
644 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
645 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
646 offset, offset);
648 else
650 unsigned int callback_offset = 0;
651 struct expr_eval_routine *eval;
652 int found = 0;
654 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
656 if (!strcmp (eval->structure->name, structure->name)
657 && !compare_expr (eval->expr, expr))
659 found = 1;
660 break;
662 callback_offset++;
665 if (!found)
667 eval = xmalloc (sizeof(*eval));
668 eval->structure = structure;
669 eval->expr = expr;
670 list_add_tail (&expr_eval_routines, &eval->entry);
673 if (callback_offset > USHRT_MAX)
674 error("Maximum number of callback routines reached\n");
676 print_file(file, 2, "0x%x, /* Corr desc: */\n", RPC_FC_NORMAL_CONFORMANCE);
677 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
678 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
680 return 4;
683 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
685 int have_align = FALSE;
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 )
692 unsigned int falign = 0;
693 size_t fsize = type_memsize(v->type, &falign);
694 if (!have_align)
696 *align = falign;
697 have_align = TRUE;
699 size = (size + (falign - 1)) & ~(falign - 1);
700 size += fsize;
703 size = (size + (*align - 1)) & ~(*align - 1);
704 return size;
707 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
709 size_t size, maxs = 0;
710 unsigned int align = *pmaxa;
711 const var_t *v;
713 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
715 /* we could have an empty default field with NULL type */
716 if (v->type)
718 size = type_memsize(v->type, &align);
719 if (maxs < size) maxs = size;
720 if (*pmaxa < align) *pmaxa = align;
724 return maxs;
727 int get_padding(const var_list_t *fields)
729 unsigned short offset = 0;
730 int salign = -1;
731 const var_t *f;
733 if (!fields)
734 return 0;
736 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
738 type_t *ft = f->type;
739 unsigned int align = 0;
740 size_t size = type_memsize(ft, &align);
741 if (salign == -1)
742 salign = align;
743 offset = (offset + (align - 1)) & ~(align - 1);
744 offset += size;
747 return ((offset + (salign - 1)) & ~(salign - 1)) - offset;
750 size_t type_memsize(const type_t *t, unsigned int *align)
752 size_t size = 0;
754 if (t->declarray && is_conformant_array(t))
756 type_memsize(t->ref, align);
757 size = 0;
759 else if (is_ptr(t) || is_conformant_array(t))
761 size = sizeof(void *);
762 if (size > *align) *align = size;
764 else switch (t->type)
766 case RPC_FC_BYTE:
767 case RPC_FC_CHAR:
768 case RPC_FC_USMALL:
769 case RPC_FC_SMALL:
770 size = 1;
771 if (size > *align) *align = size;
772 break;
773 case RPC_FC_WCHAR:
774 case RPC_FC_USHORT:
775 case RPC_FC_SHORT:
776 case RPC_FC_ENUM16:
777 size = 2;
778 if (size > *align) *align = size;
779 break;
780 case RPC_FC_ULONG:
781 case RPC_FC_LONG:
782 case RPC_FC_ERROR_STATUS_T:
783 case RPC_FC_ENUM32:
784 case RPC_FC_FLOAT:
785 size = 4;
786 if (size > *align) *align = size;
787 break;
788 case RPC_FC_HYPER:
789 case RPC_FC_DOUBLE:
790 size = 8;
791 if (size > *align) *align = size;
792 break;
793 case RPC_FC_STRUCT:
794 case RPC_FC_CVSTRUCT:
795 case RPC_FC_CPSTRUCT:
796 case RPC_FC_CSTRUCT:
797 case RPC_FC_PSTRUCT:
798 case RPC_FC_BOGUS_STRUCT:
799 size = fields_memsize(t->fields, align);
800 break;
801 case RPC_FC_ENCAPSULATED_UNION:
802 case RPC_FC_NON_ENCAPSULATED_UNION:
803 size = union_memsize(t->fields, align);
804 break;
805 case RPC_FC_SMFARRAY:
806 case RPC_FC_LGFARRAY:
807 case RPC_FC_SMVARRAY:
808 case RPC_FC_LGVARRAY:
809 case RPC_FC_BOGUS_ARRAY:
810 size = t->dim * type_memsize(t->ref, align);
811 break;
812 default:
813 error("type_memsize: Unknown type %d\n", t->type);
814 size = 0;
817 return size;
820 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
822 short absoff = type->ref->typestring_offset;
823 short reloff = absoff - (offset + 2);
824 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
826 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
827 type->type, ptr_attr, string_of_type(type->type));
828 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
829 reloff, reloff, absoff);
830 return 4;
833 static unsigned char conf_string_type_of_char_type(unsigned char t)
835 switch (t)
837 case RPC_FC_BYTE:
838 case RPC_FC_CHAR:
839 return RPC_FC_C_CSTRING;
840 case RPC_FC_WCHAR:
841 return RPC_FC_C_WSTRING;
844 error("string_type_of_char_type: unrecognized type %d", t);
845 return 0;
848 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
850 unsigned char fc
851 = is_string_type(type->attrs, type)
852 ? conf_string_type_of_char_type(type->ref->type)
853 : type->ref->type;
854 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
855 type->type, string_of_type(type->type));
856 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
857 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
858 return 4;
861 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
863 unsigned int offset = *typestring_offset;
865 print_file(file, 0, "/* %d */\n", offset);
866 update_tfsoff(type, offset, file);
868 if (type->ref->typestring_offset)
869 *typestring_offset += write_nonsimple_pointer(file, type, offset);
870 else if (is_base_type(type->ref->type))
871 *typestring_offset += write_simple_pointer(file, type);
873 return offset;
876 static int processed(const type_t *type)
878 return type->typestring_offset && !type->tfswrite;
881 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
883 unsigned int start, absoff, flags;
884 unsigned int align = 0, ualign = 0;
885 const char *name;
886 type_t *utype = get_user_type(type, &name);
887 size_t usize = type_memsize(utype, &ualign);
888 size_t size = type_memsize(type, &align);
889 unsigned short funoff = user_type_offset(name);
890 short reloff;
892 guard_rec(type);
894 if (is_base_type(utype->type))
896 absoff = *tfsoff;
897 print_file(file, 0, "/* %d */\n", absoff);
898 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
899 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
900 *tfsoff += 2;
902 else
904 if (!processed(utype))
905 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
906 absoff = utype->typestring_offset;
909 if (utype->type == RPC_FC_RP)
910 flags = 0x40;
911 else if (utype->type == RPC_FC_UP)
912 flags = 0x80;
913 else
914 flags = 0;
916 start = *tfsoff;
917 update_tfsoff(type, start, file);
918 print_file(file, 0, "/* %d */\n", start);
919 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
920 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
921 flags | (align - 1), align - 1, flags);
922 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
923 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
924 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
925 *tfsoff += 8;
926 reloff = absoff - *tfsoff;
927 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
928 *tfsoff += 2;
931 static void write_member_type(FILE *file, const type_t *cont,
932 const attr_list_t *attrs, const type_t *type,
933 unsigned int *corroff, unsigned int *tfsoff)
935 if (is_embedded_complex(type))
937 size_t absoff;
938 short reloff;
940 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
942 absoff = *corroff;
943 *corroff += 8;
945 else
947 absoff = type->typestring_offset;
949 reloff = absoff - (*tfsoff + 2);
951 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
952 /* FIXME: actually compute necessary padding */
953 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
954 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
955 reloff, reloff, absoff);
956 *tfsoff += 4;
958 else if (is_ptr(type))
960 unsigned char fc = (cont->type == RPC_FC_BOGUS_STRUCT
961 ? RPC_FC_POINTER
962 : RPC_FC_LONG);
963 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
964 *tfsoff += 1;
966 else if (!write_base_type(file, type, tfsoff))
967 error("Unsupported member type 0x%x\n", type->type);
970 static void write_end(FILE *file, unsigned int *tfsoff)
972 if (*tfsoff % 2 == 0)
974 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
975 *tfsoff += 1;
977 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
978 *tfsoff += 1;
981 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
983 unsigned int offset = 0;
984 var_list_t *fs = type->fields;
985 var_t *f;
987 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
989 unsigned int align = 0;
990 type_t *ft = f->type;
991 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
993 unsigned int absoff = ft->typestring_offset;
994 short reloff = absoff - (*tfsoff + 6);
995 print_file(file, 0, "/* %d */\n", *tfsoff);
996 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
997 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
998 write_conf_or_var_desc(file, current_structure, offset,
999 get_attrp(f->attrs, ATTR_SWITCHIS));
1000 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1001 reloff, reloff, absoff);
1002 *tfsoff += 8;
1005 /* FIXME: take alignment into account */
1006 offset += type_memsize(ft, &align);
1010 static int write_no_repeat_pointer_descriptions(
1011 FILE *file, type_t *type,
1012 size_t *offset_in_memory, size_t *offset_in_buffer,
1013 unsigned int *typestring_offset)
1015 int written = 0;
1016 unsigned int align;
1018 if (is_ptr(type))
1020 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1021 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1023 /* pointer instance */
1024 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1025 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1026 *typestring_offset += 6;
1028 if (processed(type->ref) || is_base_type(type->ref->type))
1029 write_pointer_tfs(file, type, typestring_offset);
1030 else
1031 error("write_pointer_description: type format string unknown\n");
1033 align = 0;
1034 *offset_in_memory += type_memsize(type, &align);
1035 /* FIXME: is there a case where these two are different? */
1036 align = 0;
1037 *offset_in_buffer += type_memsize(type, &align);
1039 return 1;
1042 if (is_non_complex_struct(type))
1044 const var_t *v;
1045 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1046 written += write_no_repeat_pointer_descriptions(
1047 file, v->type,
1048 offset_in_memory, offset_in_buffer, typestring_offset);
1050 else
1052 align = 0;
1053 *offset_in_memory += type_memsize(type, &align);
1054 /* FIXME: is there a case where these two are different? */
1055 align = 0;
1056 *offset_in_buffer += type_memsize(type, &align);
1059 return written;
1062 static int write_pointer_description_offsets(
1063 FILE *file, const attr_list_t *attrs, type_t *type,
1064 size_t *offset_in_memory, size_t *offset_in_buffer,
1065 unsigned int *typestring_offset)
1067 int written = 0;
1068 unsigned int align;
1070 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1072 if (offset_in_memory && offset_in_buffer)
1074 /* pointer instance */
1075 /* FIXME: sometimes from end of structure, sometimes from beginning */
1076 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1077 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1079 align = 0;
1080 *offset_in_memory += type_memsize(type, &align);
1081 /* FIXME: is there a case where these two are different? */
1082 align = 0;
1083 *offset_in_buffer += type_memsize(type, &align);
1085 *typestring_offset += 4;
1087 if (processed(type->ref) || is_base_type(type->ref->type))
1088 write_pointer_tfs(file, type, typestring_offset);
1089 else
1090 error("write_pointer_description_offsets: type format string unknown\n");
1092 return 1;
1095 if (is_array(type))
1097 return write_pointer_description_offsets(
1098 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1099 typestring_offset);
1101 else if (is_non_complex_struct(type))
1103 /* otherwise search for interesting fields to parse */
1104 const var_t *v;
1105 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1107 written += write_pointer_description_offsets(
1108 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1109 typestring_offset);
1112 else
1114 align = 0;
1115 if (offset_in_memory)
1116 *offset_in_memory += type_memsize(type, &align);
1117 /* FIXME: is there a case where these two are different? */
1118 align = 0;
1119 if (offset_in_buffer)
1120 *offset_in_buffer += type_memsize(type, &align);
1123 return written;
1126 /* Note: if file is NULL return value is number of pointers to write, else
1127 * it is the number of type format characters written */
1128 static int write_fixed_array_pointer_descriptions(
1129 FILE *file, const attr_list_t *attrs, type_t *type,
1130 size_t *offset_in_memory, size_t *offset_in_buffer,
1131 unsigned int *typestring_offset)
1133 unsigned int align;
1134 int pointer_count = 0;
1136 if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1138 unsigned int temp = 0;
1139 /* unfortunately, this needs to be done in two passes to avoid
1140 * writing out redundant FC_FIXED_REPEAT descriptions */
1141 pointer_count = write_pointer_description_offsets(
1142 NULL, attrs, type->ref, NULL, NULL, &temp);
1143 if (pointer_count > 0)
1145 unsigned int increment_size;
1146 size_t offset_of_array_pointer_mem = 0;
1147 size_t offset_of_array_pointer_buf = 0;
1149 align = 0;
1150 increment_size = type_memsize(type->ref, &align);
1152 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1153 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1154 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1155 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1156 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1157 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1158 *typestring_offset += 10;
1160 pointer_count = write_pointer_description_offsets(
1161 file, attrs, type, &offset_of_array_pointer_mem,
1162 &offset_of_array_pointer_buf, typestring_offset);
1165 else if (is_struct(type->type))
1167 const var_t *v;
1168 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1170 pointer_count += write_fixed_array_pointer_descriptions(
1171 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1172 typestring_offset);
1175 else
1177 align = 0;
1178 if (offset_in_memory)
1179 *offset_in_memory += type_memsize(type, &align);
1180 /* FIXME: is there a case where these two are different? */
1181 align = 0;
1182 if (offset_in_buffer)
1183 *offset_in_buffer += type_memsize(type, &align);
1186 return pointer_count;
1189 /* Note: if file is NULL return value is number of pointers to write, else
1190 * it is the number of type format characters written */
1191 static int write_conformant_array_pointer_descriptions(
1192 FILE *file, const attr_list_t *attrs, type_t *type,
1193 size_t *offset_in_memory, size_t *offset_in_buffer,
1194 unsigned int *typestring_offset)
1196 unsigned int align;
1197 int pointer_count = 0;
1199 if (is_conformant_array(type) && !type->length_is)
1201 unsigned int temp = 0;
1202 /* unfortunately, this needs to be done in two passes to avoid
1203 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1204 pointer_count = write_pointer_description_offsets(
1205 NULL, attrs, type->ref, NULL, NULL, &temp);
1206 if (pointer_count > 0)
1208 unsigned int increment_size;
1209 size_t offset_of_array_pointer_mem = 0;
1210 size_t offset_of_array_pointer_buf = 0;
1212 align = 0;
1213 increment_size = type_memsize(type->ref, &align);
1215 if (increment_size > USHRT_MAX)
1216 error("array size of %u bytes is too large\n", increment_size);
1218 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1219 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1220 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1221 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1222 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1223 *typestring_offset += 8;
1225 pointer_count = write_pointer_description_offsets(
1226 file, attrs, type->ref, &offset_of_array_pointer_mem,
1227 &offset_of_array_pointer_buf, typestring_offset);
1230 else if (is_struct(type->type))
1232 const var_t *v;
1233 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1235 pointer_count += write_conformant_array_pointer_descriptions(
1236 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1237 typestring_offset);
1240 else
1242 align = 0;
1243 if (offset_in_memory)
1244 *offset_in_memory += type_memsize(type, &align);
1245 /* FIXME: is there a case where these two are different? */
1246 align = 0;
1247 if (offset_in_buffer)
1248 *offset_in_buffer += type_memsize(type, &align);
1251 return pointer_count;
1254 /* Note: if file is NULL return value is number of pointers to write, else
1255 * it is the number of type format characters written */
1256 static int write_varying_array_pointer_descriptions(
1257 FILE *file, const attr_list_t *attrs, type_t *type,
1258 size_t *offset_in_memory, size_t *offset_in_buffer,
1259 unsigned int *typestring_offset)
1261 unsigned int align;
1262 int pointer_count = 0;
1264 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1266 if (is_array(type) && type->length_is)
1268 unsigned int temp = 0;
1269 /* unfortunately, this needs to be done in two passes to avoid
1270 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1271 pointer_count = write_pointer_description_offsets(
1272 NULL, attrs, type->ref, NULL, NULL, &temp);
1273 if (pointer_count > 0)
1275 unsigned int increment_size;
1276 size_t offset_of_array_pointer_mem = 0;
1277 size_t offset_of_array_pointer_buf = 0;
1279 align = 0;
1280 increment_size = type_memsize(type->ref, &align);
1282 if (increment_size > USHRT_MAX)
1283 error("array size of %u bytes is too large\n", increment_size);
1285 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1286 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1287 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1288 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1289 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1290 *typestring_offset += 8;
1292 pointer_count = write_pointer_description_offsets(
1293 file, attrs, type, &offset_of_array_pointer_mem,
1294 &offset_of_array_pointer_buf, typestring_offset);
1297 else if (is_struct(type->type))
1299 const var_t *v;
1300 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1302 pointer_count += write_varying_array_pointer_descriptions(
1303 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1304 typestring_offset);
1307 else
1309 align = 0;
1310 if (offset_in_memory)
1311 *offset_in_memory += type_memsize(type, &align);
1312 /* FIXME: is there a case where these two are different? */
1313 align = 0;
1314 if (offset_in_buffer)
1315 *offset_in_buffer += type_memsize(type, &align);
1318 return pointer_count;
1321 static void write_pointer_description(FILE *file, type_t *type,
1322 unsigned int *typestring_offset)
1324 size_t offset_in_buffer;
1325 size_t offset_in_memory;
1327 /* pass 1: search for single instance of a pointer (i.e. don't descend
1328 * into arrays) */
1329 offset_in_memory = 0;
1330 offset_in_buffer = 0;
1331 write_no_repeat_pointer_descriptions(
1332 file, type,
1333 &offset_in_memory, &offset_in_buffer, typestring_offset);
1335 /* pass 2: search for pointers in fixed arrays */
1336 offset_in_memory = 0;
1337 offset_in_buffer = 0;
1338 write_fixed_array_pointer_descriptions(
1339 file, NULL, type,
1340 &offset_in_memory, &offset_in_buffer, typestring_offset);
1342 /* pass 3: search for pointers in conformant only arrays (but don't descend
1343 * into conformant varying or varying arrays) */
1344 offset_in_memory = 0;
1345 offset_in_buffer = 0;
1346 write_conformant_array_pointer_descriptions(
1347 file, NULL, type,
1348 &offset_in_memory, &offset_in_buffer, typestring_offset);
1350 /* pass 4: search for pointers in varying arrays */
1351 offset_in_memory = 0;
1352 offset_in_buffer = 0;
1353 write_varying_array_pointer_descriptions(
1354 file, NULL, type,
1355 &offset_in_memory, &offset_in_buffer, typestring_offset);
1358 static int is_declptr(const type_t *t)
1360 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1363 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1364 type_t *type,
1365 const char *name, unsigned int *typestring_offset)
1367 size_t start_offset = *typestring_offset;
1368 unsigned char rtype;
1370 update_tfsoff(type, start_offset, file);
1372 if (is_declptr(type))
1374 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1375 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1376 if (!pointer_type)
1377 pointer_type = RPC_FC_RP;
1378 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1379 pointer_type, flag, string_of_type(pointer_type),
1380 flag ? " [simple_pointer]" : "");
1381 *typestring_offset += 2;
1382 if (!flag)
1384 print_file(file, 2, "NdrFcShort(0x2),\n");
1385 *typestring_offset += 2;
1387 rtype = type->ref->type;
1389 else
1390 rtype = type->ref->type;
1392 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1394 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1395 return start_offset;
1398 if (type->declarray && !is_conformant_array(type))
1400 /* FIXME: multi-dimensional array */
1401 if (0xffffuL < type->dim)
1402 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1403 name, 0xffffu, type->dim - 0xffffu);
1405 if (rtype == RPC_FC_CHAR)
1406 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1407 else
1408 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1409 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1410 *typestring_offset += 2;
1412 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1413 *typestring_offset += 2;
1415 return start_offset;
1417 else if (type->size_is)
1419 unsigned int align = 0;
1421 if (rtype == RPC_FC_CHAR)
1422 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1423 else
1424 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1425 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1426 *typestring_offset += 2;
1428 *typestring_offset += write_conf_or_var_desc(
1429 file, current_structure,
1430 (type->declarray && current_structure
1431 ? type_memsize(current_structure, &align)
1432 : 0),
1433 type->size_is);
1435 return start_offset;
1437 else
1439 if (rtype == RPC_FC_WCHAR)
1440 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1441 else
1442 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1443 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1444 *typestring_offset += 2;
1446 return start_offset;
1450 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1451 const char *name, unsigned int *typestring_offset)
1453 const expr_t *length_is = type->length_is;
1454 const expr_t *size_is = type->size_is;
1455 unsigned int align = 0;
1456 size_t size;
1457 size_t start_offset;
1458 int has_pointer;
1459 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1460 unsigned int baseoff
1461 = type->declarray && current_structure
1462 ? type_memsize(current_structure, &align)
1463 : 0;
1465 if (!pointer_type)
1466 pointer_type = RPC_FC_RP;
1468 has_pointer = FALSE;
1469 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1470 has_pointer = TRUE;
1472 align = 0;
1473 size = type_memsize(type, &align);
1474 if (size == 0) /* conformant array */
1475 size = type_memsize(type->ref, &align);
1477 start_offset = *typestring_offset;
1478 update_tfsoff(type, start_offset, file);
1479 print_file(file, 0, "/* %lu */\n", start_offset);
1480 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1481 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1482 *typestring_offset += 2;
1484 align = 0;
1485 if (type->type != RPC_FC_BOGUS_ARRAY)
1487 unsigned char tc = type->type;
1489 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1491 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1492 *typestring_offset += 4;
1494 else
1496 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1497 *typestring_offset += 2;
1500 if (is_conformant_array(type))
1501 *typestring_offset
1502 += write_conf_or_var_desc(file, current_structure, baseoff,
1503 size_is);
1505 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1507 unsigned int elalign = 0;
1508 size_t elsize = type_memsize(type->ref, &elalign);
1510 if (type->type == RPC_FC_LGVARRAY)
1512 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1513 *typestring_offset += 4;
1515 else
1517 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1518 *typestring_offset += 2;
1521 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1522 *typestring_offset += 2;
1525 if (length_is)
1526 *typestring_offset
1527 += write_conf_or_var_desc(file, current_structure, baseoff,
1528 length_is);
1530 if (has_pointer)
1532 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1533 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1534 *typestring_offset += 2;
1535 write_pointer_description(file, type, typestring_offset);
1536 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1537 *typestring_offset += 1;
1540 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1541 write_end(file, typestring_offset);
1543 else
1545 unsigned int dim = size_is ? 0 : type->dim;
1546 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1547 *typestring_offset += 2;
1548 *typestring_offset
1549 += write_conf_or_var_desc(file, current_structure, baseoff,
1550 size_is);
1551 *typestring_offset
1552 += write_conf_or_var_desc(file, current_structure, baseoff,
1553 length_is);
1554 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1555 write_end(file, typestring_offset);
1558 return start_offset;
1561 static const var_t *find_array_or_string_in_struct(const type_t *type)
1563 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1564 const type_t *ft = last_field->type;
1566 if (ft->declarray && is_conformant_array(ft))
1567 return last_field;
1569 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1570 return find_array_or_string_in_struct(last_field->type);
1571 else
1572 return NULL;
1575 static void write_struct_members(FILE *file, const type_t *type,
1576 unsigned int *corroff, unsigned int *typestring_offset)
1578 const var_t *field;
1579 unsigned short offset = 0;
1580 int salign = -1;
1581 int padding;
1583 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1585 type_t *ft = field->type;
1586 if (!ft->declarray || !is_conformant_array(ft))
1588 unsigned int align = 0;
1589 size_t size = type_memsize(ft, &align);
1590 if (salign == -1)
1591 salign = align;
1592 if ((align - 1) & offset)
1594 unsigned char fc = 0;
1595 switch (align)
1597 case 4:
1598 fc = RPC_FC_ALIGNM4;
1599 break;
1600 case 8:
1601 fc = RPC_FC_ALIGNM8;
1602 break;
1603 default:
1604 error("write_struct_members: cannot align type %d", ft->type);
1606 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1607 offset = (offset + (align - 1)) & ~(align - 1);
1608 *typestring_offset += 1;
1610 write_member_type(file, type, field->attrs, field->type, corroff,
1611 typestring_offset);
1612 offset += size;
1616 padding = ((offset + (salign - 1)) & ~(salign - 1)) - offset;
1617 if (padding)
1619 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1620 RPC_FC_STRUCTPAD1 + padding - 1,
1621 padding);
1622 *typestring_offset += 1;
1625 write_end(file, typestring_offset);
1628 static size_t write_struct_tfs(FILE *file, type_t *type,
1629 const char *name, unsigned int *tfsoff)
1631 const type_t *save_current_structure = current_structure;
1632 unsigned int total_size;
1633 const var_t *array;
1634 size_t start_offset;
1635 size_t array_offset;
1636 int has_pointers = 0;
1637 unsigned int align = 0;
1638 unsigned int corroff;
1639 var_t *f;
1641 guard_rec(type);
1642 current_structure = type;
1644 total_size = type_memsize(type, &align);
1645 if (total_size > USHRT_MAX)
1646 error("structure size for %s exceeds %d bytes by %d bytes\n",
1647 name, USHRT_MAX, total_size - USHRT_MAX);
1649 if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1650 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1651 FALSE, tfsoff);
1653 array = find_array_or_string_in_struct(type);
1654 if (array && !processed(array->type))
1655 array_offset
1656 = is_attr(array->attrs, ATTR_STRING)
1657 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1658 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1660 corroff = *tfsoff;
1661 write_descriptors(file, type, tfsoff);
1663 start_offset = *tfsoff;
1664 update_tfsoff(type, start_offset, file);
1665 print_file(file, 0, "/* %d */\n", start_offset);
1666 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1667 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1668 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1669 *tfsoff += 4;
1671 if (array)
1673 unsigned int absoff = array->type->typestring_offset;
1674 short reloff = absoff - *tfsoff;
1675 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1676 reloff, reloff, absoff);
1677 *tfsoff += 2;
1679 else if (type->type == RPC_FC_BOGUS_STRUCT)
1681 print_file(file, 2, "NdrFcShort(0x0),\n");
1682 *tfsoff += 2;
1685 if (type->type == RPC_FC_BOGUS_STRUCT)
1687 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1688 nothing is written to file yet. On the actual writing pass,
1689 this will have been updated. */
1690 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
1691 short reloff = absoff - *tfsoff;
1692 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1693 reloff, reloff, absoff);
1694 *tfsoff += 2;
1696 else if ((type->type == RPC_FC_PSTRUCT) ||
1697 (type->type == RPC_FC_CPSTRUCT) ||
1698 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1700 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1701 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1702 *tfsoff += 2;
1703 write_pointer_description(file, type, tfsoff);
1704 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1705 *tfsoff += 1;
1708 write_struct_members(file, type, &corroff, tfsoff);
1710 if (type->type == RPC_FC_BOGUS_STRUCT)
1712 const var_list_t *fs = type->fields;
1713 const var_t *f;
1715 type->ptrdesc = *tfsoff;
1716 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
1718 type_t *ft = f->type;
1719 if (is_ptr(ft))
1720 write_pointer_tfs(file, ft, tfsoff);
1722 if (type->ptrdesc == *tfsoff)
1723 type->ptrdesc = 0;
1726 current_structure = save_current_structure;
1727 return start_offset;
1730 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1731 unsigned char flags, size_t offset,
1732 unsigned int *typeformat_offset)
1734 size_t start_offset = *typeformat_offset;
1735 short reloff = offset - (*typeformat_offset + 2);
1736 int in_attr, out_attr;
1737 in_attr = is_attr(attrs, ATTR_IN);
1738 out_attr = is_attr(attrs, ATTR_OUT);
1739 if (!in_attr && !out_attr) in_attr = 1;
1741 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1742 flags |= 0x04;
1744 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1745 pointer_type,
1746 flags,
1747 string_of_type(pointer_type));
1748 if (file)
1750 if (flags & 0x04)
1751 fprintf(file, " [allocated_on_stack]");
1752 if (flags & 0x10)
1753 fprintf(file, " [pointer_deref]");
1754 fprintf(file, " */\n");
1757 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1758 *typeformat_offset += 4;
1760 return start_offset;
1763 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1765 if (t == NULL)
1767 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1769 else if (is_base_type(t->type))
1771 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1772 t->type, string_of_type(t->type));
1774 else if (t->typestring_offset)
1776 short reloff = t->typestring_offset - *tfsoff;
1777 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1778 reloff, reloff, t->typestring_offset);
1780 else
1781 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1783 *tfsoff += 2;
1786 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1788 unsigned int align = 0;
1789 unsigned int start_offset;
1790 size_t size = type_memsize(type, &align);
1791 var_list_t *fields;
1792 size_t nbranch = 0;
1793 type_t *deftype = NULL;
1794 short nodeftype = 0xffff;
1795 var_t *f;
1797 guard_rec(type);
1799 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1801 const var_t *uv = LIST_ENTRY(list_tail(type->fields), const var_t, entry);
1802 fields = uv->type->fields;
1804 else
1805 fields = type->fields;
1807 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1809 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1810 if (cases)
1811 nbranch += list_count(cases);
1812 if (f->type)
1813 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1816 start_offset = *tfsoff;
1817 update_tfsoff(type, start_offset, file);
1818 print_file(file, 0, "/* %d */\n", start_offset);
1819 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1821 const var_t *sv = LIST_ENTRY(list_head(type->fields), const var_t, entry);
1822 const type_t *st = sv->type;
1824 switch (st->type)
1826 case RPC_FC_CHAR:
1827 case RPC_FC_SMALL:
1828 case RPC_FC_USMALL:
1829 case RPC_FC_SHORT:
1830 case RPC_FC_USHORT:
1831 case RPC_FC_LONG:
1832 case RPC_FC_ULONG:
1833 case RPC_FC_ENUM16:
1834 case RPC_FC_ENUM32:
1835 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1836 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1837 0x40 | st->type, string_of_type(st->type));
1838 *tfsoff += 2;
1839 break;
1840 default:
1841 error("union switch type must be an integer, char, or enum\n");
1844 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1845 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1846 *tfsoff += 4;
1848 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1850 type_t *ft = f->type;
1851 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1852 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1853 expr_t *c;
1855 if (cases == NULL && !deflt)
1856 error("union field %s with neither case nor default attribute\n", f->name);
1858 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1860 /* MIDL doesn't check for duplicate cases, even though that seems
1861 like a reasonable thing to do, it just dumps them to the TFS
1862 like we're going to do here. */
1863 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1864 *tfsoff += 4;
1865 write_branch_type(file, ft, tfsoff);
1868 /* MIDL allows multiple default branches, even though that seems
1869 illogical, it just chooses the last one, which is what we will
1870 do. */
1871 if (deflt)
1873 deftype = ft;
1874 nodeftype = 0;
1878 if (deftype)
1880 write_branch_type(file, deftype, tfsoff);
1882 else
1884 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1885 *tfsoff += 2;
1888 return start_offset;
1891 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1892 unsigned int *typeformat_offset)
1894 size_t i;
1895 size_t start_offset = *typeformat_offset;
1896 const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1898 if (iid)
1900 expr_t expr;
1902 expr.type = EXPR_IDENTIFIER;
1903 expr.ref = NULL;
1904 expr.u.sval = iid->name;
1905 expr.is_const = FALSE;
1906 print_file(file, 2, "0x2f, /* FC_IP */\n");
1907 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1908 *typeformat_offset += write_conf_or_var_desc(file, NULL, 0, &expr) + 2;
1910 else
1912 const type_t *base = is_ptr(type) ? type->ref : type;
1913 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1915 if (! uuid)
1916 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1918 update_tfsoff(type, start_offset, file);
1919 print_file(file, 0, "/* %d */\n", start_offset);
1920 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1921 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1922 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1923 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1924 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1925 for (i = 0; i < 8; ++i)
1926 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1928 if (file)
1929 fprintf(file, "\n");
1931 *typeformat_offset += 18;
1933 return start_offset;
1936 static int get_ptr_attr(const type_t *t, int def_type)
1938 while (TRUE)
1940 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1941 if (ptr_attr)
1942 return ptr_attr;
1943 if (t->kind != TKIND_ALIAS)
1944 return def_type;
1945 t = t->orig;
1949 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1950 type_t *type, const var_t *var,
1951 unsigned int *typeformat_offset)
1953 int pointer_type;
1954 size_t offset;
1956 if (is_user_type(type))
1958 write_user_tfs(file, type, typeformat_offset);
1959 return type->typestring_offset;
1962 if (type == var->type) /* top-level pointers */
1964 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1965 if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1966 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1968 if (pointer_attr == 0)
1969 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1971 pointer_type = pointer_attr;
1973 else
1974 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1976 if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1977 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1979 if (is_array(type))
1981 size_t off;
1982 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1983 if (pointer_type != RPC_FC_RP)
1985 unsigned int absoff = type->typestring_offset;
1986 short reloff = absoff - (*typeformat_offset + 2);
1987 off = *typeformat_offset;
1988 print_file(file, 0, "/* %d */\n", off);
1989 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", pointer_type,
1990 string_of_type(pointer_type));
1991 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1992 reloff, reloff, absoff);
1993 *typeformat_offset += 4;
1995 return off;
1998 if (!is_ptr(type))
2000 /* basic types don't need a type format string */
2001 if (is_base_type(type->type))
2002 return 0;
2004 switch (type->type)
2006 case RPC_FC_STRUCT:
2007 case RPC_FC_PSTRUCT:
2008 case RPC_FC_CSTRUCT:
2009 case RPC_FC_CPSTRUCT:
2010 case RPC_FC_CVSTRUCT:
2011 case RPC_FC_BOGUS_STRUCT:
2012 return write_struct_tfs(file, type, var->name, typeformat_offset);
2013 case RPC_FC_ENCAPSULATED_UNION:
2014 case RPC_FC_NON_ENCAPSULATED_UNION:
2015 return write_union_tfs(file, type, typeformat_offset);
2016 case RPC_FC_IGNORE:
2017 case RPC_FC_BIND_PRIMITIVE:
2018 /* nothing to do */
2019 return 0;
2020 default:
2021 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2024 else if (last_ptr(type))
2026 size_t start_offset = *typeformat_offset;
2027 int in_attr = is_attr(var->attrs, ATTR_IN);
2028 int out_attr = is_attr(var->attrs, ATTR_OUT);
2029 const type_t *base = type->ref;
2031 if (base->type == RPC_FC_IP)
2033 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2036 /* special case for pointers to base types */
2037 if (is_base_type(base->type))
2039 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2040 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
2041 string_of_type(pointer_type),
2042 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2043 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2044 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2045 *typeformat_offset += 4;
2046 return start_offset;
2050 assert(is_ptr(type));
2052 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2053 if (file)
2054 fprintf(file, "/* %2u */\n", *typeformat_offset);
2055 return write_pointer_only_tfs(file, var->attrs, pointer_type,
2056 !last_ptr(type) ? 0x10 : 0,
2057 offset, typeformat_offset);
2060 static void set_tfswrite(type_t *type, int val)
2062 while (type->tfswrite != val)
2064 type_t *utype = get_user_type(type, NULL);
2066 type->tfswrite = val;
2068 if (utype)
2069 set_tfswrite(utype, val);
2071 if (type->kind == TKIND_ALIAS)
2072 type = type->orig;
2073 else if (is_ptr(type) || is_array(type))
2074 type = type->ref;
2075 else
2077 if (type->fields)
2079 var_t *v;
2080 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
2081 if (v->type)
2082 set_tfswrite(v->type, val);
2085 return;
2090 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2091 const char *name, int write_ptr, unsigned int *tfsoff)
2093 int retmask = 0;
2095 if (is_user_type(type))
2097 write_user_tfs(file, type, tfsoff);
2099 else if (is_ptr(type))
2101 type_t *ref = type->ref;
2103 if (ref->type == RPC_FC_IP)
2105 write_ip_tfs(file, attrs, type, tfsoff);
2107 else
2109 if (!processed(ref) && !is_base_type(ref->type))
2110 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2112 if (write_ptr)
2113 write_pointer_tfs(file, type, tfsoff);
2115 retmask |= 1;
2118 else if (last_array(type) && is_attr(attrs, ATTR_STRING))
2120 write_string_tfs(file, attrs, type, name, tfsoff);
2122 else if (type->declarray && is_conformant_array(type))
2123 ; /* conformant arrays and strings are handled specially */
2124 else if (is_array(type))
2126 write_array_tfs(file, attrs, type, name, tfsoff);
2128 else if (is_struct(type->type))
2130 if (!processed(type))
2131 write_struct_tfs(file, type, name, tfsoff);
2133 else if (is_union(type->type))
2135 if (!processed(type))
2136 write_union_tfs(file, type, tfsoff);
2138 else if (!is_base_type(type->type))
2139 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2140 name, type->type);
2142 return retmask;
2145 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2147 const ifref_t * iface;
2148 const func_t *func;
2149 const var_t *var;
2151 if (ifaces)
2152 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2153 if (iface->iface->funcs)
2154 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2155 if (func->args)
2156 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2157 set_tfswrite(var->type, val);
2160 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
2162 const var_t *var;
2163 const ifref_t *iface;
2164 unsigned int typeformat_offset = 2;
2166 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2168 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2169 continue;
2171 if (iface->iface->funcs)
2173 const func_t *func;
2174 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2176 if (is_local(func->def->attrs)) continue;
2178 current_func = func;
2179 if (func->args)
2180 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2181 update_tfsoff(
2182 var->type,
2183 write_typeformatstring_var(
2184 file, 2, func, var->type, var,
2185 &typeformat_offset),
2186 file);
2191 return typeformat_offset + 1;
2195 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2197 int indent = 0;
2199 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2200 print_file(file, indent, "{\n");
2201 indent++;
2202 print_file(file, indent, "0,\n");
2203 print_file(file, indent, "{\n");
2204 indent++;
2205 print_file(file, indent, "NdrFcShort(0x0),\n");
2207 set_all_tfswrite(ifaces, TRUE);
2208 process_tfs(file, ifaces, for_objects);
2210 print_file(file, indent, "0x0\n");
2211 indent--;
2212 print_file(file, indent, "}\n");
2213 indent--;
2214 print_file(file, indent, "};\n");
2215 print_file(file, indent, "\n");
2218 static unsigned int get_required_buffer_size_type(
2219 const type_t *type, const char *name, unsigned int *alignment)
2221 *alignment = 0;
2222 if (is_user_type(type))
2224 const char *uname;
2225 const type_t *utype = get_user_type(type, &uname);
2226 return get_required_buffer_size_type(utype, uname, alignment);
2228 else
2230 switch (type->type)
2232 case RPC_FC_BYTE:
2233 case RPC_FC_CHAR:
2234 case RPC_FC_USMALL:
2235 case RPC_FC_SMALL:
2236 *alignment = 4;
2237 return 1;
2239 case RPC_FC_WCHAR:
2240 case RPC_FC_USHORT:
2241 case RPC_FC_SHORT:
2242 case RPC_FC_ENUM16:
2243 *alignment = 4;
2244 return 2;
2246 case RPC_FC_ULONG:
2247 case RPC_FC_LONG:
2248 case RPC_FC_ENUM32:
2249 case RPC_FC_FLOAT:
2250 case RPC_FC_ERROR_STATUS_T:
2251 *alignment = 4;
2252 return 4;
2254 case RPC_FC_HYPER:
2255 case RPC_FC_DOUBLE:
2256 *alignment = 8;
2257 return 8;
2259 case RPC_FC_IGNORE:
2260 case RPC_FC_BIND_PRIMITIVE:
2261 return 0;
2263 case RPC_FC_STRUCT:
2264 case RPC_FC_PSTRUCT:
2266 size_t size = 0;
2267 const var_t *field;
2268 if (!type->fields) return 0;
2269 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2271 unsigned int alignment;
2272 size += get_required_buffer_size_type(field->type, field->name,
2273 &alignment);
2275 return size;
2278 case RPC_FC_RP:
2279 return
2280 is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
2281 ? get_required_buffer_size_type( type->ref, name, alignment )
2282 : 0;
2284 case RPC_FC_SMFARRAY:
2285 case RPC_FC_LGFARRAY:
2286 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2288 default:
2289 return 0;
2294 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2296 int in_attr = is_attr(var->attrs, ATTR_IN);
2297 int out_attr = is_attr(var->attrs, ATTR_OUT);
2299 if (!in_attr && !out_attr)
2300 in_attr = 1;
2302 *alignment = 0;
2304 if (pass == PASS_OUT)
2306 if (out_attr && is_ptr(var->type))
2308 type_t *type = var->type;
2310 if (type->type == RPC_FC_STRUCT)
2312 const var_t *field;
2313 unsigned int size = 36;
2315 if (!type->fields) return size;
2316 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2318 unsigned int align;
2319 size += get_required_buffer_size_type(
2320 field->type, field->name, &align);
2322 return size;
2325 return 0;
2327 else
2329 if ((!out_attr || in_attr) && !var->type->size_is
2330 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2332 if (is_ptr(var->type))
2334 type_t *type = var->type;
2336 if (is_base_type(type->type))
2338 return 25;
2340 else if (type->type == RPC_FC_STRUCT)
2342 unsigned int size = 36;
2343 const var_t *field;
2345 if (!type->fields) return size;
2346 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2348 unsigned int align;
2349 size += get_required_buffer_size_type(
2350 field->type, field->name, &align);
2352 return size;
2357 return get_required_buffer_size_type(var->type, var->name, alignment);
2361 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2363 const var_t *var;
2364 unsigned int total_size = 0, alignment;
2366 if (func->args)
2368 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2370 total_size += get_required_buffer_size(var, &alignment, pass);
2371 total_size += alignment;
2375 if (pass == PASS_OUT && !is_void(func->def->type))
2377 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2378 total_size += alignment;
2380 return total_size;
2383 static void print_phase_function(FILE *file, int indent, const char *type,
2384 enum remoting_phase phase,
2385 const var_t *var, unsigned int type_offset)
2387 const char *function;
2388 switch (phase)
2390 case PHASE_BUFFERSIZE:
2391 function = "BufferSize";
2392 break;
2393 case PHASE_MARSHAL:
2394 function = "Marshall";
2395 break;
2396 case PHASE_UNMARSHAL:
2397 function = "Unmarshall";
2398 break;
2399 case PHASE_FREE:
2400 function = "Free";
2401 break;
2402 default:
2403 assert(0);
2404 return;
2407 print_file(file, indent, "Ndr%s%s(\n", type, function);
2408 indent++;
2409 print_file(file, indent, "&_StubMsg,\n");
2410 print_file(file, indent, "%s%s%s%s,\n",
2411 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2412 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2413 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2414 var->name);
2415 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2416 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2417 if (phase == PHASE_UNMARSHAL)
2418 print_file(file, indent, "0);\n");
2419 indent--;
2422 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2423 enum pass pass, const var_t *var,
2424 const char *varname)
2426 type_t *type = var->type;
2427 unsigned int size;
2428 unsigned int alignment = 0;
2429 unsigned char rtype;
2431 /* no work to do for other phases, buffer sizing is done elsewhere */
2432 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2433 return;
2435 rtype = is_ptr(type) ? type->ref->type : type->type;
2437 switch (rtype)
2439 case RPC_FC_BYTE:
2440 case RPC_FC_CHAR:
2441 case RPC_FC_SMALL:
2442 case RPC_FC_USMALL:
2443 size = 1;
2444 alignment = 1;
2445 break;
2447 case RPC_FC_WCHAR:
2448 case RPC_FC_USHORT:
2449 case RPC_FC_SHORT:
2450 case RPC_FC_ENUM16:
2451 size = 2;
2452 alignment = 2;
2453 break;
2455 case RPC_FC_ULONG:
2456 case RPC_FC_LONG:
2457 case RPC_FC_ENUM32:
2458 case RPC_FC_FLOAT:
2459 case RPC_FC_ERROR_STATUS_T:
2460 size = 4;
2461 alignment = 4;
2462 break;
2464 case RPC_FC_HYPER:
2465 case RPC_FC_DOUBLE:
2466 size = 8;
2467 alignment = 8;
2468 break;
2470 case RPC_FC_IGNORE:
2471 case RPC_FC_BIND_PRIMITIVE:
2472 /* no marshalling needed */
2473 return;
2475 default:
2476 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2477 size = 0;
2480 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2481 alignment - 1, alignment - 1);
2483 if (phase == PHASE_MARSHAL)
2485 print_file(file, indent, "*(");
2486 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2487 if (is_ptr(type))
2488 fprintf(file, " *)_StubMsg.Buffer = *");
2489 else
2490 fprintf(file, " *)_StubMsg.Buffer = ");
2491 fprintf(file, varname);
2492 fprintf(file, ";\n");
2494 else if (phase == PHASE_UNMARSHAL)
2496 if (pass == PASS_IN || pass == PASS_RETURN)
2497 print_file(file, indent, "");
2498 else
2499 print_file(file, indent, "*");
2500 fprintf(file, varname);
2501 if (pass == PASS_IN && is_ptr(type))
2502 fprintf(file, " = (");
2503 else
2504 fprintf(file, " = *(");
2505 write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2506 fprintf(file, " *)_StubMsg.Buffer;\n");
2509 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2510 write_type(file, var->type, FALSE, NULL);
2511 fprintf(file, ");\n");
2514 /* returns whether the MaxCount, Offset or ActualCount members need to be
2515 * filled in for the specified phase */
2516 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2518 return (phase != PHASE_UNMARSHAL);
2521 static int needs_freeing(const attr_list_t *attrs, const type_t *t, int out)
2523 return
2524 (is_user_type(t)
2525 || (is_ptr(t)
2526 && (t->ref->type == RPC_FC_IP
2527 || is_ptr(t->ref))))
2528 || (out && (is_string_type(attrs, t)
2529 || is_array(t)));
2532 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2533 enum pass pass, enum remoting_phase phase)
2535 int in_attr, out_attr, pointer_type;
2536 const var_t *var;
2538 if (!func->args)
2539 return;
2541 if (phase == PHASE_BUFFERSIZE)
2543 unsigned int size = get_function_buffer_size( func, pass );
2544 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2547 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2549 const type_t *type = var->type;
2550 unsigned char rtype;
2551 size_t start_offset = type->typestring_offset;
2553 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2554 if (!pointer_type)
2555 pointer_type = RPC_FC_RP;
2557 in_attr = is_attr(var->attrs, ATTR_IN);
2558 out_attr = is_attr(var->attrs, ATTR_OUT);
2559 if (!in_attr && !out_attr)
2560 in_attr = 1;
2562 if (phase == PHASE_FREE)
2564 if (!needs_freeing(var->attrs, type, out_attr))
2565 continue;
2567 else
2568 switch (pass)
2570 case PASS_IN:
2571 if (!in_attr) continue;
2572 break;
2573 case PASS_OUT:
2574 if (!out_attr) continue;
2575 break;
2576 case PASS_RETURN:
2577 break;
2580 rtype = type->type;
2582 if (is_user_type(var->type))
2584 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2586 else if (is_string_type(var->attrs, var->type))
2588 if (is_array(type) && !is_conformant_array(type))
2589 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2590 else
2592 if (type->size_is && is_size_needed_for_phase(phase))
2594 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2595 write_expr(file, type->size_is, 1);
2596 fprintf(file, ";\n");
2599 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2600 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2601 else
2602 print_phase_function(file, indent, "ConformantString", phase, var,
2603 start_offset + (type->size_is ? 4 : 2));
2606 else if (is_array(type))
2608 unsigned char tc = type->type;
2609 const char *array_type = "FixedArray";
2611 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2613 if (is_size_needed_for_phase(phase))
2615 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2616 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2617 write_expr(file, type->length_is, 1);
2618 fprintf(file, ";\n\n");
2620 array_type = "VaryingArray";
2622 else if (tc == RPC_FC_CARRAY)
2624 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2626 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2627 write_expr(file, type->size_is, 1);
2628 fprintf(file, ";\n\n");
2630 array_type = "ConformantArray";
2632 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
2634 if (is_size_needed_for_phase(phase))
2636 if (type->size_is)
2638 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2639 write_expr(file, type->size_is, 1);
2640 fprintf(file, ";\n");
2642 if (type->length_is)
2644 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2645 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2646 write_expr(file, type->length_is, 1);
2647 fprintf(file, ";\n\n");
2650 array_type = (tc == RPC_FC_BOGUS_ARRAY
2651 ? "ComplexArray"
2652 : "ConformantVaryingArray");
2655 if (!in_attr && phase == PHASE_FREE)
2657 print_file(file, indent, "if (%s)\n", var->name);
2658 indent++;
2659 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2661 else if (phase != PHASE_FREE)
2663 const char *t = pointer_type == RPC_FC_RP ? array_type : "Pointer";
2664 print_phase_function(file, indent, t, phase, var, start_offset);
2667 else if (!is_ptr(var->type) && is_base_type(rtype))
2669 print_phase_basetype(file, indent, phase, pass, var, var->name);
2671 else if (!is_ptr(var->type))
2673 switch (rtype)
2675 case RPC_FC_STRUCT:
2676 case RPC_FC_PSTRUCT:
2677 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2678 break;
2679 case RPC_FC_CSTRUCT:
2680 case RPC_FC_CPSTRUCT:
2681 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2682 break;
2683 case RPC_FC_CVSTRUCT:
2684 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2685 break;
2686 case RPC_FC_BOGUS_STRUCT:
2687 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2688 break;
2689 case RPC_FC_RP:
2690 if (is_base_type( var->type->ref->type ))
2692 print_phase_basetype(file, indent, phase, pass, var, var->name);
2694 else if (var->type->ref->type == RPC_FC_STRUCT)
2696 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2697 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2699 else
2701 const var_t *iid;
2702 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2703 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2704 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2706 break;
2707 default:
2708 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2711 else
2713 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2715 print_phase_basetype(file, indent, phase, pass, var, var->name);
2717 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2719 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2720 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2722 else
2724 const var_t *iid;
2725 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2726 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2727 if (var->type->ref->type == RPC_FC_IP)
2728 print_phase_function(file, indent, "InterfacePointer", phase, var, start_offset);
2729 else
2730 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2733 fprintf(file, "\n");
2738 size_t get_size_procformatstring_var(const var_t *var)
2740 return write_procformatstring_var(NULL, 0, var, FALSE);
2744 size_t get_size_procformatstring_func(const func_t *func)
2746 const var_t *var;
2747 size_t size = 0;
2749 /* argument list size */
2750 if (func->args)
2751 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2752 size += get_size_procformatstring_var(var);
2754 /* return value size */
2755 if (is_void(func->def->type))
2756 size += 2; /* FC_END and FC_PAD */
2757 else
2758 size += get_size_procformatstring_var(func->def);
2760 return size;
2763 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2765 const ifref_t *iface;
2766 size_t size = 1;
2767 const func_t *func;
2769 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2771 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2772 continue;
2774 if (iface->iface->funcs)
2775 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2776 if (!is_local(func->def->attrs))
2777 size += get_size_procformatstring_func( func );
2779 return size;
2782 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2784 set_all_tfswrite(ifaces, FALSE);
2785 return process_tfs(NULL, ifaces, for_objects);
2788 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2789 const var_list_t *fields, const char *structvar)
2791 switch (e->type) {
2792 case EXPR_VOID:
2793 break;
2794 case EXPR_NUM:
2795 fprintf(h, "%lu", e->u.lval);
2796 break;
2797 case EXPR_HEXNUM:
2798 fprintf(h, "0x%lx", e->u.lval);
2799 break;
2800 case EXPR_DOUBLE:
2801 fprintf(h, "%#.15g", e->u.dval);
2802 break;
2803 case EXPR_TRUEFALSE:
2804 if (e->u.lval == 0)
2805 fprintf(h, "FALSE");
2806 else
2807 fprintf(h, "TRUE");
2808 break;
2809 case EXPR_IDENTIFIER:
2811 const var_t *field;
2812 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2813 if (!strcmp(e->u.sval, field->name))
2815 fprintf(h, "%s->%s", structvar, e->u.sval);
2816 break;
2819 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2820 break;
2822 case EXPR_NEG:
2823 fprintf(h, "-");
2824 write_struct_expr(h, e->ref, 1, fields, structvar);
2825 break;
2826 case EXPR_NOT:
2827 fprintf(h, "~");
2828 write_struct_expr(h, e->ref, 1, fields, structvar);
2829 break;
2830 case EXPR_PPTR:
2831 fprintf(h, "*");
2832 write_struct_expr(h, e->ref, 1, fields, structvar);
2833 break;
2834 case EXPR_CAST:
2835 fprintf(h, "(");
2836 write_type(h, e->u.tref, FALSE, NULL);
2837 fprintf(h, ")");
2838 write_struct_expr(h, e->ref, 1, fields, structvar);
2839 break;
2840 case EXPR_SIZEOF:
2841 fprintf(h, "sizeof(");
2842 write_type(h, e->u.tref, FALSE, NULL);
2843 fprintf(h, ")");
2844 break;
2845 case EXPR_SHL:
2846 case EXPR_SHR:
2847 case EXPR_MUL:
2848 case EXPR_DIV:
2849 case EXPR_ADD:
2850 case EXPR_SUB:
2851 case EXPR_AND:
2852 case EXPR_OR:
2853 if (brackets) fprintf(h, "(");
2854 write_struct_expr(h, e->ref, 1, fields, structvar);
2855 switch (e->type) {
2856 case EXPR_SHL: fprintf(h, " << "); break;
2857 case EXPR_SHR: fprintf(h, " >> "); break;
2858 case EXPR_MUL: fprintf(h, " * "); break;
2859 case EXPR_DIV: fprintf(h, " / "); break;
2860 case EXPR_ADD: fprintf(h, " + "); break;
2861 case EXPR_SUB: fprintf(h, " - "); break;
2862 case EXPR_AND: fprintf(h, " & "); break;
2863 case EXPR_OR: fprintf(h, " | "); break;
2864 default: break;
2866 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2867 if (brackets) fprintf(h, ")");
2868 break;
2869 case EXPR_COND:
2870 if (brackets) fprintf(h, "(");
2871 write_struct_expr(h, e->ref, 1, fields, structvar);
2872 fprintf(h, " ? ");
2873 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2874 fprintf(h, " : ");
2875 write_struct_expr(h, e->ext2, 1, fields, structvar);
2876 if (brackets) fprintf(h, ")");
2877 break;
2882 void declare_stub_args( FILE *file, int indent, const func_t *func )
2884 int in_attr, out_attr;
2885 int i = 0;
2886 const var_t *def = func->def;
2887 const var_t *var;
2889 /* declare return value '_RetVal' */
2890 if (!is_void(def->type))
2892 print_file(file, indent, "");
2893 write_type_left(file, def->type);
2894 fprintf(file, " _RetVal;\n");
2897 if (!func->args)
2898 return;
2900 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2902 int is_string = is_attr(var->attrs, ATTR_STRING);
2904 in_attr = is_attr(var->attrs, ATTR_IN);
2905 out_attr = is_attr(var->attrs, ATTR_OUT);
2906 if (!out_attr && !in_attr)
2907 in_attr = 1;
2909 if (!in_attr && !var->type->size_is && !is_string)
2911 print_file(file, indent, "");
2912 write_type(file, var->type->ref, FALSE, "_W%u", i++);
2913 fprintf(file, ";\n");
2916 print_file(file, indent, "");
2917 write_type_left(file, var->type);
2918 fprintf(file, " ");
2919 if (var->type->declarray) {
2920 fprintf(file, "( *");
2921 write_name(file, var);
2922 fprintf(file, " )");
2923 } else
2924 write_name(file, var);
2925 write_type_right(file, var->type, FALSE);
2926 fprintf(file, ";\n");
2928 if (decl_indirect(var->type))
2929 print_file(file, indent, "void *_p_%s = &%s;\n",
2930 var->name, var->name);
2935 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2937 int in_attr, out_attr;
2938 int i = 0, sep = 0;
2939 const var_t *var;
2941 if (!func->args)
2942 return;
2944 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2946 int is_string = is_attr(var->attrs, ATTR_STRING);
2947 in_attr = is_attr(var->attrs, ATTR_IN);
2948 out_attr = is_attr(var->attrs, ATTR_OUT);
2949 if (!out_attr && !in_attr)
2950 in_attr = 1;
2952 if (!in_attr)
2954 print_file(file, indent, "");
2955 write_name(file, var);
2957 if (var->type->size_is)
2959 unsigned int size, align = 0;
2960 type_t *type = var->type;
2962 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2963 for ( ; type->size_is ; type = type->ref)
2965 write_expr(file, type->size_is, TRUE);
2966 fprintf(file, " * ");
2968 size = type_memsize(type, &align);
2969 fprintf(file, "%u);\n", size);
2971 else if (!is_string)
2973 fprintf(file, " = &_W%u;\n", i);
2974 if (is_ptr(var->type) && !last_ptr(var->type))
2975 print_file(file, indent, "_W%u = 0;\n", i);
2976 i++;
2979 sep = 1;
2982 if (sep)
2983 fprintf(file, "\n");
2987 int write_expr_eval_routines(FILE *file, const char *iface)
2989 static const char *var_name = "pS";
2990 int result = 0;
2991 struct expr_eval_routine *eval;
2992 unsigned short callback_offset = 0;
2994 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2996 const char *name = eval->structure->name;
2997 const var_list_t *fields = eval->structure->fields;
2998 unsigned align = 0;
2999 result = 1;
3001 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3002 iface, name, callback_offset);
3003 print_file(file, 0, "{\n");
3004 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3005 name, var_name, name, type_memsize (eval->structure, &align));
3006 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3007 print_file(file, 1, "pStubMsg->MaxCount = (unsigned long)");
3008 write_struct_expr(file, eval->expr, 1, fields, var_name);
3009 fprintf(file, ";\n");
3010 print_file(file, 0, "}\n\n");
3011 callback_offset++;
3013 return result;
3016 void write_expr_eval_routine_list(FILE *file, const char *iface)
3018 struct expr_eval_routine *eval;
3019 struct expr_eval_routine *cursor;
3020 unsigned short callback_offset = 0;
3022 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3023 fprintf(file, "{\n");
3025 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3027 const char *name = eval->structure->name;
3028 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3029 callback_offset++;
3030 list_remove(&eval->entry);
3031 free(eval);
3034 fprintf(file, "};\n\n");
3037 void write_user_quad_list(FILE *file)
3039 user_type_t *ut;
3041 if (list_empty(&user_type_list))
3042 return;
3044 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3045 fprintf(file, "{\n");
3046 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3048 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3049 print_file(file, 1, "{\n");
3050 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3051 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3052 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3053 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3054 print_file(file, 1, "}%s\n", sep);
3056 fprintf(file, "};\n\n");
3059 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3061 const struct str_list_entry_t *endpoint;
3062 const char *p;
3064 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3065 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3066 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3068 print_file( f, 1, "{ (const unsigned char *)\"" );
3069 for (p = endpoint->str; *p && *p != ':'; p++)
3071 if (*p == '"' || *p == '\\') fputc( '\\', f );
3072 fputc( *p, f );
3074 if (!*p) goto error;
3075 if (p[1] != '[') goto error;
3077 fprintf( f, "\", (const unsigned char *)\"" );
3078 for (p += 2; *p && *p != ']'; p++)
3080 if (*p == '"' || *p == '\\') fputc( '\\', f );
3081 fputc( *p, f );
3083 if (*p != ']') goto error;
3084 fprintf( f, "\" },\n" );
3086 print_file( f, 0, "};\n\n" );
3087 return;
3089 error:
3090 error("Invalid endpoint syntax '%s'\n", endpoint->str);