widl: size_is and length_is are expression lists now.
[wine/multimedia.git] / tools / widl / typegen.c
blobe4f9de2313f2b985740ae6a433d7c7b84a75cce0
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 type_memsize(const type_t *t, int ptr_level, const array_dims_t *array, unsigned int *align);
63 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
65 static int compare_expr(const expr_t *a, const expr_t *b)
67 int ret;
69 if (a->type != b->type)
70 return a->type - b->type;
72 switch (a->type)
74 case EXPR_NUM:
75 case EXPR_HEXNUM:
76 case EXPR_TRUEFALSE:
77 return a->u.lval - b->u.lval;
78 case EXPR_IDENTIFIER:
79 return strcmp(a->u.sval, b->u.sval);
80 case EXPR_COND:
81 ret = compare_expr(a->ref, b->ref);
82 if (ret != 0)
83 return ret;
84 ret = compare_expr(a->u.ext, b->u.ext);
85 if (ret != 0)
86 return ret;
87 return compare_expr(a->ext2, b->ext2);
88 case EXPR_OR:
89 case EXPR_AND:
90 case EXPR_ADD:
91 case EXPR_SUB:
92 case EXPR_MUL:
93 case EXPR_DIV:
94 case EXPR_SHL:
95 case EXPR_SHR:
96 ret = compare_expr(a->ref, b->ref);
97 if (ret != 0)
98 return ret;
99 return compare_expr(a->u.ext, b->u.ext);
100 case EXPR_NOT:
101 case EXPR_NEG:
102 case EXPR_PPTR:
103 case EXPR_CAST:
104 case EXPR_SIZEOF:
105 return compare_expr(a->ref, b->ref);
106 case EXPR_VOID:
107 return 0;
109 return -1;
112 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
113 do { \
114 if (file) \
115 fprintf(file, "/* %2u */\n", typestring_offset); \
116 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
118 while (0)
120 static int print_file(FILE *file, int indent, const char *format, ...)
122 va_list va;
123 int i, r;
125 if (!file) return 0;
127 va_start(va, format);
128 for (i = 0; i < indent; i++)
129 fprintf(file, " ");
130 r = vfprintf(file, format, va);
131 va_end(va);
132 return r;
135 static void write_formatdesc(FILE *f, int indent, const char *str)
137 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
138 print_file(f, indent, "{\n");
139 print_file(f, indent + 1, "short Pad;\n");
140 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
141 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
142 print_file(f, indent, "\n");
145 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
147 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
148 get_size_typeformatstring(ifaces, for_objects));
150 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
151 get_size_procformatstring(ifaces, for_objects));
153 fprintf(f, "\n");
154 write_formatdesc(f, indent, "TYPE");
155 write_formatdesc(f, indent, "PROC");
156 fprintf(f, "\n");
157 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
158 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
159 print_file(f, indent, "\n");
162 static int is_user_derived(const var_t *v)
164 const type_t *type = v->type;
166 if (v->attrs && is_attr( v->attrs, ATTR_WIREMARSHAL )) return 1;
168 while (type)
170 if (type->attrs && is_attr( type->attrs, ATTR_WIREMARSHAL )) return 1;
171 type = type->ref;
173 return 0;
176 static inline int is_base_type(unsigned char type)
178 switch (type)
180 case RPC_FC_BYTE:
181 case RPC_FC_CHAR:
182 case RPC_FC_USMALL:
183 case RPC_FC_SMALL:
184 case RPC_FC_WCHAR:
185 case RPC_FC_USHORT:
186 case RPC_FC_SHORT:
187 case RPC_FC_ULONG:
188 case RPC_FC_LONG:
189 case RPC_FC_HYPER:
190 case RPC_FC_IGNORE:
191 case RPC_FC_FLOAT:
192 case RPC_FC_DOUBLE:
193 case RPC_FC_ENUM16:
194 case RPC_FC_ENUM32:
195 case RPC_FC_ERROR_STATUS_T:
196 case RPC_FC_BIND_PRIMITIVE:
197 return TRUE;
199 default:
200 return FALSE;
204 static size_t write_procformatstring_var(FILE *file, int indent,
205 const var_t *var, int is_return, unsigned int *type_offset)
207 size_t size;
208 int ptr_level = var->ptr_level;
209 const type_t *type = var->type;
211 int is_in = is_attr(var->attrs, ATTR_IN);
212 int is_out = is_attr(var->attrs, ATTR_OUT);
214 if (!is_in && !is_out) is_in = TRUE;
216 if (ptr_level == 0 && !var->array && is_base_type(type->type))
218 if (is_return)
219 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
220 else
221 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
223 switch(type->type)
225 #define CASE_BASETYPE(fctype) \
226 case RPC_##fctype: \
227 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
228 size = 2; /* includes param type prefix */ \
229 break
231 CASE_BASETYPE(FC_BYTE);
232 CASE_BASETYPE(FC_CHAR);
233 CASE_BASETYPE(FC_WCHAR);
234 CASE_BASETYPE(FC_USHORT);
235 CASE_BASETYPE(FC_SHORT);
236 CASE_BASETYPE(FC_ULONG);
237 CASE_BASETYPE(FC_LONG);
238 CASE_BASETYPE(FC_HYPER);
239 CASE_BASETYPE(FC_IGNORE);
240 CASE_BASETYPE(FC_USMALL);
241 CASE_BASETYPE(FC_SMALL);
242 CASE_BASETYPE(FC_FLOAT);
243 CASE_BASETYPE(FC_DOUBLE);
244 CASE_BASETYPE(FC_ERROR_STATUS_T);
245 #undef CASE_BASETYPE
247 case RPC_FC_BIND_PRIMITIVE:
248 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
249 size = 2; /* includes param type prefix */
250 break;
252 default:
253 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
254 size = 0;
257 else
259 if (is_return)
260 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
261 else if (is_in && is_out)
262 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
263 else if (is_out)
264 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
265 else
266 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
268 print_file(file, indent, "0x01,\n");
269 print_file(file, indent, "NdrFcShort(0x%x),\n", *type_offset);
270 size = 4; /* includes param type prefix */
272 *type_offset += get_size_typeformatstring_var(var);
273 return size;
276 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
278 const ifref_t *iface;
279 int indent = 0;
280 const var_t *var;
281 unsigned int type_offset = 2;
283 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
284 print_file(file, indent, "{\n");
285 indent++;
286 print_file(file, indent, "0,\n");
287 print_file(file, indent, "{\n");
288 indent++;
290 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
292 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
293 continue;
295 if (iface->iface->funcs)
297 const func_t *func;
298 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
300 if (is_local(func->def->attrs)) continue;
301 /* emit argument data */
302 if (func->args)
304 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
305 write_procformatstring_var(file, indent, var, FALSE, &type_offset);
308 /* emit return value data */
309 var = func->def;
310 if (is_void(var->type, NULL))
312 print_file(file, indent, "0x5b, /* FC_END */\n");
313 print_file(file, indent, "0x5c, /* FC_PAD */\n");
315 else
316 write_procformatstring_var(file, indent, var, TRUE,
317 &type_offset);
322 print_file(file, indent, "0x0\n");
323 indent--;
324 print_file(file, indent, "}\n");
325 indent--;
326 print_file(file, indent, "};\n");
327 print_file(file, indent, "\n");
330 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
332 switch (type->type)
334 #define CASE_BASETYPE(fctype) \
335 case RPC_##fctype: \
336 print_file(file, 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
337 *typestring_offset += 1; \
338 return 1;
340 CASE_BASETYPE(FC_BYTE);
341 CASE_BASETYPE(FC_CHAR);
342 CASE_BASETYPE(FC_SMALL);
343 CASE_BASETYPE(FC_USMALL);
344 CASE_BASETYPE(FC_WCHAR);
345 CASE_BASETYPE(FC_SHORT);
346 CASE_BASETYPE(FC_USHORT);
347 CASE_BASETYPE(FC_LONG);
348 CASE_BASETYPE(FC_ULONG);
349 CASE_BASETYPE(FC_FLOAT);
350 CASE_BASETYPE(FC_HYPER);
351 CASE_BASETYPE(FC_DOUBLE);
352 CASE_BASETYPE(FC_ENUM16);
353 CASE_BASETYPE(FC_ENUM32);
354 CASE_BASETYPE(FC_IGNORE);
355 CASE_BASETYPE(FC_ERROR_STATUS_T);
356 #undef CASE_BASETYPE
358 return 0;
361 /* write conformance / variance descriptor */
362 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_list_t *expr_list)
364 unsigned char operator_type = 0;
365 const char *operator_string = "no operators";
366 const expr_t *expr, *subexpr;
367 unsigned char correlation_type;
369 if (!file) return 4; /* optimisation for sizing pass */
371 if (list_count(expr_list) > 1)
372 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
374 expr = subexpr = LIST_ENTRY( list_head(expr_list), const expr_t, entry );
376 if (expr->is_const)
378 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
379 error("write_conf_or_var_desc: constant value %ld is greater than "
380 "the maximum constant size of %d\n", expr->cval,
381 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
383 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
384 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
385 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
386 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
388 return 4;
391 switch (subexpr->type)
393 case EXPR_PPTR:
394 subexpr = subexpr->ref;
395 operator_type = RPC_FC_DEREFERENCE;
396 operator_string = "FC_DEREFERENCE";
397 break;
398 case EXPR_DIV:
399 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
401 subexpr = subexpr->ref;
402 operator_type = RPC_FC_DIV_2;
403 operator_string = "FC_DIV_2";
405 break;
406 case EXPR_MUL:
407 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
409 subexpr = subexpr->ref;
410 operator_type = RPC_FC_MULT_2;
411 operator_string = "FC_MULT_2";
413 break;
414 case EXPR_SUB:
415 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
417 subexpr = subexpr->ref;
418 operator_type = RPC_FC_SUB_1;
419 operator_string = "FC_SUB_1";
421 break;
422 case EXPR_ADD:
423 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
425 subexpr = subexpr->ref;
426 operator_type = RPC_FC_ADD_1;
427 operator_string = "FC_ADD_1";
429 break;
430 default:
431 break;
434 if (subexpr->type == EXPR_IDENTIFIER)
436 const type_t *correlation_variable = NULL;
437 unsigned char correlation_variable_type;
438 unsigned char param_type = 0;
439 const char *param_type_string = NULL;
440 size_t offset;
442 if (structure)
444 const var_t *var;
446 offset = 0;
447 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
449 unsigned int align = 0;
450 offset -= type_memsize(var->type, var->ptr_level, var->array, &align);
451 /* FIXME: take alignment into account */
452 if (!strcmp(var->name, subexpr->u.sval))
454 correlation_variable = var->type;
455 break;
458 if (!correlation_variable)
459 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
460 subexpr->u.sval);
462 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
464 else
466 const var_t *var;
468 offset = 0;
469 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
471 if (!strcmp(var->name, subexpr->u.sval))
473 correlation_variable = var->type;
474 break;
476 /* FIXME: not all stack variables are sizeof(void *) */
477 offset += sizeof(void *);
479 if (!correlation_variable)
480 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
481 subexpr->u.sval);
483 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
486 correlation_variable_type = correlation_variable->type;
488 switch (correlation_variable_type)
490 case RPC_FC_CHAR:
491 case RPC_FC_SMALL:
492 param_type = RPC_FC_SMALL;
493 param_type_string = "FC_SMALL";
494 break;
495 case RPC_FC_BYTE:
496 case RPC_FC_USMALL:
497 param_type = RPC_FC_USMALL;
498 param_type_string = "FC_USMALL";
499 break;
500 case RPC_FC_WCHAR:
501 case RPC_FC_SHORT:
502 param_type = RPC_FC_SHORT;
503 param_type_string = "FC_SHORT";
504 break;
505 case RPC_FC_USHORT:
506 param_type = RPC_FC_USHORT;
507 param_type_string = "FC_USHORT";
508 break;
509 case RPC_FC_LONG:
510 param_type = RPC_FC_LONG;
511 param_type_string = "FC_LONG";
512 break;
513 case RPC_FC_ULONG:
514 param_type = RPC_FC_ULONG;
515 param_type_string = "FC_ULONG";
516 break;
517 default:
518 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
519 correlation_variable_type);
522 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
523 correlation_type | param_type,
524 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
525 param_type_string);
526 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
527 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
528 offset,
529 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
530 offset);
532 else
534 unsigned int callback_offset = 0;
536 if (structure)
538 struct expr_eval_routine *eval;
539 int found = 0;
541 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
543 if (!strcmp(eval->structure->name, structure->name) &&
544 !compare_expr(eval->expr, expr))
546 found = 1;
547 break;
549 callback_offset++;
552 if (!found)
554 unsigned int align = 0;
555 eval = xmalloc(sizeof(*eval));
556 eval->structure = structure;
557 eval->structure_size = fields_memsize(structure->fields, &align);
558 eval->expr = expr;
559 list_add_tail(&expr_eval_routines, &eval->entry);
562 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
564 else
566 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
567 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
570 if (callback_offset > USHRT_MAX)
571 error("Maximum number of callback routines reached\n");
573 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
574 correlation_type,
575 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
576 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
577 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
579 return 4;
582 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
584 size_t size = 0;
585 const var_t *v;
587 if (!fields) return 0;
588 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
589 size += type_memsize(v->type, v->ptr_level, v->array, align);
591 return size;
594 static size_t get_array_size( const array_dims_t *array )
596 size_t size = 1;
597 const expr_t *dim;
599 if (!array) return 0;
601 LIST_FOR_EACH_ENTRY( dim, array, expr_t, entry )
603 if (!dim->is_const) return 0;
604 size *= dim->cval;
607 return size;
610 static size_t type_memsize(const type_t *t, int ptr_level, const array_dims_t *array, unsigned int *align)
612 size_t size = 0;
614 if (ptr_level)
616 size = sizeof(void *);
617 if (size > *align) *align = size;
619 else switch (t->type)
621 case RPC_FC_BYTE:
622 case RPC_FC_CHAR:
623 case RPC_FC_USMALL:
624 case RPC_FC_SMALL:
625 size = 1;
626 if (size > *align) *align = size;
627 break;
628 case RPC_FC_WCHAR:
629 case RPC_FC_USHORT:
630 case RPC_FC_SHORT:
631 case RPC_FC_ENUM16:
632 size = 2;
633 if (size > *align) *align = size;
634 break;
635 case RPC_FC_ULONG:
636 case RPC_FC_LONG:
637 case RPC_FC_ERROR_STATUS_T:
638 case RPC_FC_ENUM32:
639 case RPC_FC_FLOAT:
640 size = 4;
641 if (size > *align) *align = size;
642 break;
643 case RPC_FC_HYPER:
644 case RPC_FC_DOUBLE:
645 size = 8;
646 if (size > *align) *align = size;
647 break;
648 case RPC_FC_STRUCT:
649 case RPC_FC_CVSTRUCT:
650 case RPC_FC_CPSTRUCT:
651 case RPC_FC_CSTRUCT:
652 case RPC_FC_PSTRUCT:
653 case RPC_FC_BOGUS_STRUCT:
654 case RPC_FC_ENCAPSULATED_UNION:
655 case RPC_FC_NON_ENCAPSULATED_UNION:
656 size = fields_memsize(t->fields, align);
657 break;
658 default:
659 error("type_memsize: Unknown type %d\n", t->type);
660 size = 0;
663 if (array) size *= get_array_size( array );
664 return size;
667 static int write_pointers(FILE *file, const attr_list_t *attrs,
668 const type_t *type, int ptr_level,
669 const array_dims_t *array, int level,
670 unsigned int *typestring_offset)
672 int pointers_written = 0;
673 const var_t *v;
675 /* don't generate a pointer for first-level arrays since we want to
676 * descend into them to write their pointers, not stop here */
677 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
679 return write_pointers(file, NULL, type, 0, NULL, level + 1, typestring_offset);
682 if (ptr_level != 0)
684 /* FIXME: only general algorithm implemented, not the actual writing */
685 error("write_pointers: Writing type format string for pointer is unimplemented\n");
686 return 1;
689 switch (type->type)
691 /* note: don't descend into complex structures or unions since these
692 * will always be generated as a separate type */
693 case RPC_FC_STRUCT:
694 case RPC_FC_CVSTRUCT:
695 case RPC_FC_CPSTRUCT:
696 case RPC_FC_CSTRUCT:
697 case RPC_FC_PSTRUCT:
698 if (!type->fields) break;
699 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
700 pointers_written += write_pointers(file, v->attrs, v->type,
701 v->ptr_level, v->array,
702 level + 1,
703 typestring_offset);
705 break;
707 default:
708 /* nothing to do */
709 break;
712 return pointers_written;
715 static size_t write_pointer_description(FILE *file, const attr_list_t *attrs,
716 const type_t *type, int ptr_level,
717 const array_dims_t *array, int level,
718 size_t typestring_offset)
720 size_t size = 0;
721 const var_t *v;
723 /* don't generate a pointer for first-level arrays since we want to
724 * descend into them to write their pointers, not stop here */
725 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
727 return write_pointer_description(file, NULL, type, 0, NULL,
728 level + 1, typestring_offset);
731 if (ptr_level != 0)
733 /* FIXME: only general algorithm implemented, not the actual writing */
734 error("write_pointer_description: Writing pointer description is unimplemented\n");
735 return 0;
738 /* FIXME: search through all refs for pointers too */
740 switch (type->type)
742 /* note: don't descend into complex structures or unions since these
743 * will always be generated as a separate type */
744 case RPC_FC_STRUCT:
745 case RPC_FC_CVSTRUCT:
746 case RPC_FC_CPSTRUCT:
747 case RPC_FC_CSTRUCT:
748 case RPC_FC_PSTRUCT:
749 if (!type->fields) break;
750 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
751 size += write_pointer_description(file, v->attrs, v->type,
752 v->ptr_level, v->array,
753 level + 1,
754 typestring_offset);
756 break;
758 default:
759 /* nothing to do */
760 break;
763 return size;
766 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
767 const type_t *type, const array_dims_t *array,
768 const char *name, unsigned int *typestring_offset)
770 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
771 int has_size = is_non_void(size_is);
772 size_t start_offset = *typestring_offset;
773 unsigned char flags = 0;
774 int pointer_type;
775 unsigned char rtype;
777 if (is_ptr(type))
779 pointer_type = type->type;
780 type = type->ref;
782 else
783 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
785 if (!pointer_type)
786 pointer_type = RPC_FC_RP;
788 if (!get_attrp(attrs, ATTR_SIZEIS))
789 flags |= RPC_FC_P_SIMPLEPOINTER;
791 rtype = type->type;
793 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
795 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
796 return start_offset;
799 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
800 pointer_type, flags,
801 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
802 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
803 *typestring_offset += 2;
805 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
807 print_file(file, 2, "NdrFcShort(0x2),\n");
808 *typestring_offset += 2;
811 if (array && !is_conformant_array(array))
813 /* FIXME: multi-dimensional array */
814 const expr_t *dim = LIST_ENTRY( list_head( array ), expr_t, entry );
815 if (dim->cval > USHRT_MAX)
816 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
817 name, USHRT_MAX, dim->cval - USHRT_MAX);
819 if (rtype == RPC_FC_CHAR)
820 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
821 else
822 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
823 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
824 *typestring_offset += 2;
826 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim->cval, dim->cval);
827 *typestring_offset += 2;
829 return start_offset;
831 else if (has_size)
833 if (rtype == RPC_FC_CHAR)
834 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
835 else
836 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
837 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
838 *typestring_offset += 2;
840 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
842 return start_offset;
844 else
846 if (rtype == RPC_FC_CHAR)
847 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
848 else
849 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
850 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
851 *typestring_offset += 2;
853 return start_offset;
857 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs,
858 const type_t *type, const array_dims_t *array,
859 const char *name, unsigned int *typestring_offset)
861 const expr_list_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
862 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
863 int has_length = is_non_void(length_is);
864 int has_size = is_non_void(size_is) || is_conformant_array(array);
865 size_t start_offset;
866 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
867 if (!pointer_type)
868 pointer_type = RPC_FC_RP;
870 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
871 pointer_type,
872 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
873 print_file(file, 2, "NdrFcShort(0x2),\n");
874 *typestring_offset += 4;
876 if (array && list_count(array) > 1) /* multi-dimensional array */
878 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
879 return 0;
881 else
883 const expr_t *dim = array ? LIST_ENTRY( list_head( array ), expr_t, entry ) : NULL;
884 size_t pointer_start_offset = *typestring_offset;
885 int has_pointer = 0;
887 if (write_pointers(file, attrs, type, 0, array, 0, typestring_offset) > 0)
888 has_pointer = 1;
890 start_offset = *typestring_offset;
892 if (!has_length && !has_size)
894 /* fixed array */
895 unsigned int align = 0;
896 size_t size = type_memsize(type, 0, array, &align);
897 if (size < USHRT_MAX)
899 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
900 /* alignment */
901 print_file(file, 2, "0x%02x,\n", align - 1);
902 /* size */
903 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
904 *typestring_offset += 4;
906 else
908 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
909 /* alignment */
910 print_file(file, 2, "0x%02x,\n", align - 1);
911 /* size */
912 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
913 *typestring_offset += 6;
916 if (has_pointer)
918 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
919 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
920 *typestring_offset += 2;
921 *typestring_offset = write_pointer_description(file, attrs,
922 type, 0, array, 0, pointer_start_offset);
923 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
924 *typestring_offset += 1;
927 if (!write_base_type( file, type, typestring_offset ))
929 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
930 *typestring_offset += 1;
932 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
933 *typestring_offset += 1;
935 return start_offset;
937 else if (has_length && !has_size)
939 /* varying array */
940 unsigned int align = 0;
941 size_t element_size = type_memsize(type, 0, NULL, &align);
942 size_t elements = dim->cval;
943 size_t total_size = element_size * elements;
945 if (total_size < USHRT_MAX)
947 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
948 /* alignment */
949 print_file(file, 2, "0x%02x,\n", align - 1);
950 /* total size */
951 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
952 /* number of elements */
953 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
954 *typestring_offset += 6;
956 else
958 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
959 /* alignment */
960 print_file(file, 2, "0x%02x,\n", align - 1);
961 /* total size */
962 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
963 /* number of elements */
964 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
965 *typestring_offset += 10;
967 /* element size */
968 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
969 *typestring_offset += 2;
971 *typestring_offset += write_conf_or_var_desc(file, current_func,
972 current_structure,
973 length_is);
975 if (has_pointer)
977 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
978 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
979 *typestring_offset += 2;
980 *typestring_offset += write_pointer_description(file, attrs,
981 type, 0, array, 0, pointer_start_offset);
982 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
983 *typestring_offset += 1;
986 if (!write_base_type( file, type, typestring_offset ))
988 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
989 *typestring_offset += 1;
991 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
992 *typestring_offset += 1;
994 return start_offset;
996 else if (!has_length && has_size)
998 /* conformant array */
999 unsigned int align = 0;
1000 size_t element_size = type_memsize(type, 0, NULL, &align);
1002 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
1003 /* alignment */
1004 print_file(file, 2, "0x%02x,\n", align - 1);
1005 /* element size */
1006 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1007 *typestring_offset += 4;
1009 *typestring_offset += write_conf_or_var_desc(file, current_func,
1010 current_structure,
1011 size_is ? size_is : array);
1013 if (has_pointer)
1015 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1016 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1017 *typestring_offset += 2;
1018 *typestring_offset += write_pointer_description(file, attrs,
1019 type, 0, array, 0, pointer_start_offset);
1020 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1021 *typestring_offset += 1;
1024 if (!write_base_type( file, type, typestring_offset ))
1026 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1027 *typestring_offset += 1;
1029 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1030 *typestring_offset += 1;
1032 return start_offset;
1034 else
1036 /* conformant varying array */
1037 unsigned int align = 0;
1038 size_t element_size = type_memsize(type, 0, NULL, &align);
1040 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
1041 /* alignment */
1042 print_file(file, 2, "0x%02x,\n", align - 1);
1043 /* element size */
1044 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1045 *typestring_offset += 4;
1047 *typestring_offset += write_conf_or_var_desc(file, current_func,
1048 current_structure,
1049 size_is ? size_is : array);
1050 *typestring_offset += write_conf_or_var_desc(file, current_func,
1051 current_structure,
1052 length_is);
1054 if (has_pointer)
1056 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1057 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1058 *typestring_offset += 2;
1059 *typestring_offset += write_pointer_description(file, attrs,
1060 type, 0, array, 0, pointer_start_offset);
1061 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1062 *typestring_offset += 1;
1065 if (!write_base_type( file, type, typestring_offset ))
1067 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1068 *typestring_offset += 1;
1070 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1071 *typestring_offset += 1;
1073 return start_offset;
1078 static const var_t *find_array_or_string_in_struct(const type_t *type)
1080 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1082 if (is_array_type(last_field->attrs, last_field->ptr_level, last_field->array))
1083 return last_field;
1085 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1086 (last_field->type->type == RPC_FC_CPSTRUCT) ||
1087 (last_field->type->type == RPC_FC_CVSTRUCT));
1089 return find_array_or_string_in_struct(last_field->type);
1092 static void write_struct_members(FILE *file, const type_t *type, unsigned int *typestring_offset)
1094 const var_t *field;
1096 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1098 unsigned char rtype = field->type->type;
1100 if (field->array)
1101 write_array_tfs( file, field->attrs, field->type, field->array,
1102 field->name, typestring_offset );
1103 else if (!write_base_type( file, field->type, typestring_offset ))
1104 error("Unsupported member type 0x%x\n", rtype);
1107 if (!(*typestring_offset % 2))
1109 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1110 *typestring_offset += 1;
1113 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1114 *typestring_offset += 1;
1117 static size_t write_struct_tfs(FILE *file, const type_t *type,
1118 const char *name, unsigned int *typestring_offset)
1120 unsigned int total_size;
1121 const var_t *array;
1122 size_t start_offset;
1123 size_t array_offset;
1124 size_t pointer_offset;
1125 unsigned int align = 0;
1127 switch (type->type)
1129 case RPC_FC_STRUCT:
1130 case RPC_FC_PSTRUCT:
1131 total_size = type_memsize(type, 0, NULL, &align);
1133 if (total_size > USHRT_MAX)
1134 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1135 name, USHRT_MAX, total_size - USHRT_MAX);
1137 if (type->type == RPC_FC_PSTRUCT)
1139 pointer_offset = *typestring_offset;
1140 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1142 else pointer_offset = 0; /* silence warning */
1144 start_offset = *typestring_offset;
1145 if (type->type == RPC_FC_STRUCT)
1146 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1147 else
1148 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1149 /* alignment */
1150 print_file(file, 2, "0x%02x,\n", align - 1);
1151 /* total size */
1152 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1153 *typestring_offset += 4;
1155 if (type->type == RPC_FC_PSTRUCT)
1157 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1158 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1159 *typestring_offset += 2;
1160 *typestring_offset += write_pointer_description(file, NULL,
1161 type, 0, NULL, 0, pointer_offset);
1162 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1163 *typestring_offset += 1;
1166 /* member layout */
1167 write_struct_members(file, type, typestring_offset);
1168 return start_offset;
1169 case RPC_FC_CSTRUCT:
1170 case RPC_FC_CPSTRUCT:
1171 total_size = type_memsize(type, 0, NULL, &align);
1173 if (total_size > USHRT_MAX)
1174 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1175 name, USHRT_MAX, total_size - USHRT_MAX);
1177 array = find_array_or_string_in_struct(type);
1178 current_structure = type;
1179 array_offset = write_array_tfs(file, array->attrs, array->type,
1180 array->array, array->name,
1181 typestring_offset);
1182 current_structure = NULL;
1184 if (type->type == RPC_FC_CPSTRUCT)
1186 pointer_offset = *typestring_offset;
1187 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1189 else pointer_offset = 0; /* silence warning */
1191 start_offset = *typestring_offset;
1192 if (type->type == RPC_FC_CSTRUCT)
1193 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1194 else
1195 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1196 /* alignment */
1197 print_file(file, 2, "0x%02x,\n", align - 1);
1198 /* total size */
1199 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1200 *typestring_offset += 4;
1201 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1202 array_offset - *typestring_offset,
1203 array_offset - *typestring_offset,
1204 array_offset);
1205 *typestring_offset += 2;
1207 if (type->type == RPC_FC_CPSTRUCT)
1209 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1210 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1211 *typestring_offset += 2;
1212 *typestring_offset += write_pointer_description(file, NULL,
1213 type, 0, NULL, 0, pointer_offset);
1214 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1215 *typestring_offset += 1;
1218 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1219 *typestring_offset += 1;
1221 return start_offset;
1222 case RPC_FC_CVSTRUCT:
1223 total_size = type_memsize(type, 0, NULL, &align);
1225 if (total_size > USHRT_MAX)
1226 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1227 name, USHRT_MAX, total_size - USHRT_MAX);
1229 array = find_array_or_string_in_struct(type);
1230 current_structure = type;
1231 if (is_attr(array->attrs, ATTR_STRING))
1232 array_offset = write_string_tfs(file, array->attrs, array->type,
1233 array->array, array->name,
1234 typestring_offset);
1235 else
1236 array_offset = write_array_tfs(file, array->attrs, array->type,
1237 array->array, array->name,
1238 typestring_offset);
1239 current_structure = NULL;
1241 pointer_offset = *typestring_offset;
1242 if (!write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset))
1243 pointer_offset = 0;
1245 start_offset = *typestring_offset;
1246 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1247 /* alignment */
1248 print_file(file, 2, "0x%02x,\n", align - 1);
1249 /* total size */
1250 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1251 *typestring_offset += 4;
1252 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1253 array_offset - *typestring_offset,
1254 array_offset - *typestring_offset,
1255 array_offset);
1256 *typestring_offset += 2;
1258 if (pointer_offset != 0)
1260 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1261 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1262 *typestring_offset += 2;
1263 *typestring_offset += write_pointer_description(file, NULL,
1264 type, 0, NULL, 0, pointer_offset);
1265 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1266 *typestring_offset += 1;
1269 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1270 *typestring_offset += 1;
1272 return start_offset;
1273 default:
1274 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1275 return *typestring_offset;
1279 static void write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1280 unsigned char flags, size_t offset,
1281 unsigned int *typeformat_offset)
1283 int in_attr, out_attr;
1284 in_attr = is_attr(attrs, ATTR_IN);
1285 out_attr = is_attr(attrs, ATTR_OUT);
1286 if (!in_attr && !out_attr) in_attr = 1;
1288 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1289 flags |= 0x04;
1291 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1292 pointer_type,
1293 flags,
1294 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
1295 if (file)
1297 if (flags & 0x04)
1298 fprintf(file, " [allocated_on_stack]");
1299 if (flags & 0x10)
1300 fprintf(file, " [pointer_deref]");
1301 fprintf(file, " */\n");
1304 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", offset, offset);
1305 *typeformat_offset += 4;
1308 static size_t write_union_tfs(FILE *file, const attr_list_t *attrs,
1309 const type_t *type, const char *name,
1310 unsigned int *typeformat_offset)
1312 error("write_union_tfs: Unimplemented\n");
1313 return *typeformat_offset;
1316 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs,
1317 const char *name, unsigned int *typeformat_offset)
1319 size_t i;
1320 size_t start_offset = *typeformat_offset;
1321 const UUID *uuid = get_attrp(attrs, ATTR_UUID);
1323 if (! uuid)
1324 error("%s: interface %s missing UUID\n", __FUNCTION__, name);
1326 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1327 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1328 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1329 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1330 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1331 for (i = 0; i < 8; ++i)
1332 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1334 if (file)
1335 fprintf(file, "\n");
1337 *typeformat_offset += 18;
1338 return start_offset;
1341 static int get_ptr_attr(const type_t *t, int def_type)
1343 while (TRUE)
1345 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1346 if (ptr_attr)
1347 return ptr_attr;
1348 if (t->kind != TKIND_ALIAS)
1349 return def_type;
1350 t = t->orig;
1354 static size_t write_typeformatstring_var(FILE *file, int indent,
1355 const var_t *var, unsigned int *typeformat_offset)
1357 const type_t *type = var->type;
1358 int var_ptrs = var->ptr_level, type_ptrs = 0;
1359 int is_str = is_attr(var->attrs, ATTR_STRING);
1361 chat("write_typeformatstring_var: %s\n", var->name);
1363 while (TRUE)
1365 is_str = is_str || is_attr(type->attrs, ATTR_STRING);
1366 if (type->kind == TKIND_ALIAS)
1367 type = type->orig;
1368 else if (is_ptr(type))
1370 ++type_ptrs;
1371 type = type->ref;
1373 else
1375 type = var->type;
1376 break;
1380 while (TRUE)
1382 int ptr_level = var_ptrs + type_ptrs;
1383 int pointer_type = 0;
1385 chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type->type, type->name, ptr_level);
1387 /* var attrs only effect the rightmost pointer */
1388 if ((0 < var->ptr_level && var_ptrs == var->ptr_level)
1389 || (var->ptr_level == 0 && type == var->type))
1391 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1392 if (pointer_attr)
1394 if (! ptr_level)
1395 error("'%s': pointer attribute applied to non-pointer type",
1396 var->name);
1397 pointer_type = pointer_attr;
1399 else
1400 pointer_type = RPC_FC_RP;
1402 else /* pointers below other pointers default to unique */
1403 pointer_type = var_ptrs ? RPC_FC_UP : get_ptr_attr(type, RPC_FC_UP);
1405 if (is_str && ptr_level + (var->array != NULL) == 1)
1406 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1408 if (is_array_type(var->attrs, ptr_level, var->array))
1409 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1411 if (ptr_level == 0)
1413 /* basic types don't need a type format string */
1414 if (is_base_type(type->type))
1415 return 0;
1417 switch (type->type)
1419 case RPC_FC_STRUCT:
1420 case RPC_FC_PSTRUCT:
1421 case RPC_FC_CSTRUCT:
1422 case RPC_FC_CPSTRUCT:
1423 case RPC_FC_CVSTRUCT:
1424 case RPC_FC_BOGUS_STRUCT:
1425 return write_struct_tfs(file, type, var->name, typeformat_offset);
1426 case RPC_FC_ENCAPSULATED_UNION:
1427 case RPC_FC_NON_ENCAPSULATED_UNION:
1428 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1429 case RPC_FC_IGNORE:
1430 case RPC_FC_BIND_PRIMITIVE:
1431 /* nothing to do */
1432 return 0;
1433 default:
1434 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1437 else if (ptr_level == 1)
1439 size_t start_offset = *typeformat_offset;
1440 int in_attr = is_attr(var->attrs, ATTR_IN);
1441 int out_attr = is_attr(var->attrs, ATTR_OUT);
1442 const type_t *base = is_ptr(type) ? type->ref : type;
1444 if (base->type == RPC_FC_IP)
1446 return write_ip_tfs(file, base->attrs, base->name, typeformat_offset);
1449 /* special case for pointers to base types */
1450 switch (base->type)
1452 #define CASE_BASETYPE(fctype) \
1453 case RPC_##fctype: \
1454 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n", \
1455 pointer_type, \
1456 (!in_attr && out_attr) ? 0x0C : 0x08, \
1457 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1458 (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1459 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
1460 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
1461 *typeformat_offset += 4; \
1462 return start_offset
1463 CASE_BASETYPE(FC_BYTE);
1464 CASE_BASETYPE(FC_CHAR);
1465 CASE_BASETYPE(FC_SMALL);
1466 CASE_BASETYPE(FC_USMALL);
1467 CASE_BASETYPE(FC_WCHAR);
1468 CASE_BASETYPE(FC_SHORT);
1469 CASE_BASETYPE(FC_USHORT);
1470 CASE_BASETYPE(FC_LONG);
1471 CASE_BASETYPE(FC_ULONG);
1472 CASE_BASETYPE(FC_FLOAT);
1473 CASE_BASETYPE(FC_HYPER);
1474 CASE_BASETYPE(FC_DOUBLE);
1475 CASE_BASETYPE(FC_ENUM16);
1476 CASE_BASETYPE(FC_ENUM32);
1477 CASE_BASETYPE(FC_IGNORE);
1478 CASE_BASETYPE(FC_ERROR_STATUS_T);
1479 default:
1480 break;
1484 assert(ptr_level > 0);
1486 if (file)
1487 fprintf(file, "/* %2u */\n", *typeformat_offset);
1488 write_pointer_only_tfs(file, var->attrs, pointer_type,
1489 1 < ptr_level ? 0x10 : 0,
1490 2, typeformat_offset);
1492 if (var_ptrs)
1493 --var_ptrs;
1494 else
1496 --type_ptrs;
1497 type = type->ref;
1503 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1505 int indent = 0;
1506 const var_t *var;
1507 unsigned int typeformat_offset;
1508 const ifref_t *iface;
1510 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1511 print_file(file, indent, "{\n");
1512 indent++;
1513 print_file(file, indent, "0,\n");
1514 print_file(file, indent, "{\n");
1515 indent++;
1516 print_file(file, indent, "NdrFcShort(0x0),\n");
1517 typeformat_offset = 2;
1519 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1521 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1522 continue;
1524 if (iface->iface->funcs)
1526 const func_t *func;
1527 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1529 if (is_local(func->def->attrs)) continue;
1530 current_func = func;
1531 if (func->args)
1532 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1533 write_typeformatstring_var(file, indent, var,
1534 &typeformat_offset);
1539 print_file(file, indent, "0x0\n");
1540 indent--;
1541 print_file(file, indent, "}\n");
1542 indent--;
1543 print_file(file, indent, "};\n");
1544 print_file(file, indent, "\n");
1547 static unsigned int get_required_buffer_size_type(
1548 const type_t *type, int ptr_level, const array_dims_t *array,
1549 const char *name, unsigned int *alignment)
1551 size_t size = 0;
1553 *alignment = 0;
1554 if (ptr_level == 0)
1556 switch (type->type)
1558 case RPC_FC_BYTE:
1559 case RPC_FC_CHAR:
1560 case RPC_FC_USMALL:
1561 case RPC_FC_SMALL:
1562 *alignment = 4;
1563 size = 1;
1564 break;
1566 case RPC_FC_WCHAR:
1567 case RPC_FC_USHORT:
1568 case RPC_FC_SHORT:
1569 *alignment = 4;
1570 size = 2;
1571 break;
1573 case RPC_FC_ULONG:
1574 case RPC_FC_LONG:
1575 case RPC_FC_FLOAT:
1576 case RPC_FC_ERROR_STATUS_T:
1577 *alignment = 4;
1578 size = 4;
1579 break;
1581 case RPC_FC_HYPER:
1582 case RPC_FC_DOUBLE:
1583 *alignment = 8;
1584 size = 8;
1585 break;
1587 case RPC_FC_IGNORE:
1588 case RPC_FC_BIND_PRIMITIVE:
1589 return 0;
1591 case RPC_FC_STRUCT:
1593 const var_t *field;
1594 if (!type->fields) return 0;
1595 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1597 unsigned int alignment;
1598 size += get_required_buffer_size_type(
1599 field->type, field->ptr_level, field->array, field->name,
1600 &alignment);
1602 break;
1605 case RPC_FC_RP:
1606 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1607 size = get_required_buffer_size_type( type->ref, 0, NULL, name, alignment );
1608 break;
1610 default:
1611 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1612 return 0;
1614 if (array) size *= get_array_size( array );
1616 return size;
1619 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1621 expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1622 int has_size = is_non_void(size_is);
1623 int in_attr = is_attr(var->attrs, ATTR_IN);
1624 int out_attr = is_attr(var->attrs, ATTR_OUT);
1626 if (!in_attr && !out_attr)
1627 in_attr = 1;
1629 *alignment = 0;
1631 if (pass == PASS_OUT)
1633 if (out_attr && var->ptr_level > 0)
1635 type_t *type = var->type;
1637 if (type->type == RPC_FC_STRUCT)
1639 const var_t *field;
1640 unsigned int size = 36;
1642 if (!type->fields) return size;
1643 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1645 unsigned int align;
1646 size += get_required_buffer_size_type(
1647 field->type, field->ptr_level, field->array, field->name,
1648 &align);
1650 return size;
1653 return 0;
1655 else
1657 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1659 if (var->ptr_level > 0)
1661 type_t *type = var->type;
1663 if (is_base_type(type->type))
1665 return 25;
1667 else if (type->type == RPC_FC_STRUCT)
1669 unsigned int size = 36;
1670 const var_t *field;
1672 if (!type->fields) return size;
1673 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1675 unsigned int align;
1676 size += get_required_buffer_size_type(
1677 field->type, field->ptr_level, field->array, field->name,
1678 &align);
1680 return size;
1685 return get_required_buffer_size_type(var->type, var->ptr_level, var->array, var->name, alignment);
1689 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
1691 const var_t *var;
1692 unsigned int total_size = 0, alignment;
1694 if (func->args)
1696 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1698 total_size += get_required_buffer_size(var, &alignment, pass);
1699 total_size += alignment;
1703 if (pass == PASS_OUT && !is_void(func->def->type, NULL))
1705 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
1706 total_size += alignment;
1708 return total_size;
1711 static void print_phase_function(FILE *file, int indent, const char *type,
1712 enum remoting_phase phase,
1713 const char *varname, unsigned int type_offset)
1715 const char *function;
1716 switch (phase)
1718 case PHASE_BUFFERSIZE:
1719 function = "BufferSize";
1720 break;
1721 case PHASE_MARSHAL:
1722 function = "Marshall";
1723 break;
1724 case PHASE_UNMARSHAL:
1725 function = "Unmarshall";
1726 break;
1727 case PHASE_FREE:
1728 function = "Free";
1729 break;
1730 default:
1731 assert(0);
1732 return;
1735 print_file(file, indent, "Ndr%s%s(\n", type, function);
1736 indent++;
1737 print_file(file, indent, "&_StubMsg,\n");
1738 print_file(file, indent, "%s%s,\n",
1739 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1740 varname);
1741 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1742 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1743 if (phase == PHASE_UNMARSHAL)
1744 print_file(file, indent, "0);\n");
1745 indent--;
1748 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1749 enum pass pass, const var_t *var,
1750 const char *varname)
1752 const type_t *type = var->type;
1753 unsigned int size;
1754 unsigned int alignment = 0;
1755 unsigned char rtype;
1757 /* no work to do for other phases, buffer sizing is done elsewhere */
1758 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1759 return;
1761 rtype = type->type;
1763 switch (rtype)
1765 case RPC_FC_BYTE:
1766 case RPC_FC_CHAR:
1767 case RPC_FC_SMALL:
1768 case RPC_FC_USMALL:
1769 size = 1;
1770 alignment = 1;
1771 break;
1773 case RPC_FC_WCHAR:
1774 case RPC_FC_USHORT:
1775 case RPC_FC_SHORT:
1776 size = 2;
1777 alignment = 2;
1778 break;
1780 case RPC_FC_ULONG:
1781 case RPC_FC_LONG:
1782 case RPC_FC_FLOAT:
1783 case RPC_FC_ERROR_STATUS_T:
1784 size = 4;
1785 alignment = 4;
1786 break;
1788 case RPC_FC_HYPER:
1789 case RPC_FC_DOUBLE:
1790 size = 8;
1791 alignment = 8;
1792 break;
1794 case RPC_FC_IGNORE:
1795 case RPC_FC_BIND_PRIMITIVE:
1796 /* no marshalling needed */
1797 return;
1799 default:
1800 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1801 size = 0;
1804 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1805 alignment - 1, alignment - 1);
1807 if (phase == PHASE_MARSHAL)
1809 print_file(file, indent, "*(");
1810 write_type(file, var->type, NULL, var->tname);
1811 if (var->ptr_level)
1812 fprintf(file, " *)_StubMsg.Buffer = *");
1813 else
1814 fprintf(file, " *)_StubMsg.Buffer = ");
1815 fprintf(file, varname);
1816 fprintf(file, ";\n");
1818 else if (phase == PHASE_UNMARSHAL)
1820 if (pass == PASS_IN || pass == PASS_RETURN)
1821 print_file(file, indent, "");
1822 else
1823 print_file(file, indent, "*");
1824 fprintf(file, varname);
1825 if (pass == PASS_IN && var->ptr_level)
1826 fprintf(file, " = (");
1827 else
1828 fprintf(file, " = *(");
1829 write_type(file, var->type, NULL, var->tname);
1830 fprintf(file, " *)_StubMsg.Buffer;\n");
1833 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1834 write_type(file, var->type, NULL, var->tname);
1835 fprintf(file, ");\n");
1838 /* returns whether the MaxCount, Offset or ActualCount members need to be
1839 * filled in for the specified phase */
1840 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1842 return (phase != PHASE_UNMARSHAL);
1845 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1846 unsigned int *type_offset, enum pass pass,
1847 enum remoting_phase phase)
1849 const expr_list_t *length_is;
1850 const expr_list_t *size_is;
1851 int in_attr, out_attr, has_length, has_size, pointer_type;
1852 const var_t *var;
1854 if (!func->args)
1855 return;
1857 if (phase == PHASE_BUFFERSIZE)
1859 unsigned int size = get_function_buffer_size( func, pass );
1860 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
1863 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1865 const type_t *type = var->type;
1866 unsigned char rtype;
1868 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1869 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1870 has_length = is_non_void(length_is);
1871 has_size = is_non_void(size_is) || (var->array && is_conformant_array(var->array));
1873 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1874 if (!pointer_type)
1875 pointer_type = RPC_FC_RP;
1877 in_attr = is_attr(var->attrs, ATTR_IN);
1878 out_attr = is_attr(var->attrs, ATTR_OUT);
1879 if (!in_attr && !out_attr)
1880 in_attr = 1;
1882 switch (pass)
1884 case PASS_IN:
1885 if (!in_attr) goto next;
1886 break;
1887 case PASS_OUT:
1888 if (!out_attr) goto next;
1889 break;
1890 case PASS_RETURN:
1891 break;
1894 rtype = type->type;
1896 if (is_user_derived( var ))
1898 print_phase_function(file, indent, "UserMarshal", phase, var->name, *type_offset);
1900 else if (is_string_type(var->attrs, var->ptr_level, var->array))
1902 if (var->array && !is_conformant_array(var->array))
1903 print_phase_function(file, indent, "NonConformantString", phase, var->name, *type_offset);
1904 else
1906 if (size_is && is_size_needed_for_phase(phase))
1908 const expr_t *size = LIST_ENTRY( list_head(size_is), const expr_t, entry );
1909 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1910 write_expr(file, size, 1);
1911 fprintf(file, ";\n");
1914 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1915 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1916 else
1917 print_phase_function(file, indent, "ConformantString", phase, var->name,
1918 *type_offset + (has_size ? 4 : 2));
1921 else if (is_array_type(var->attrs, var->ptr_level, var->array))
1923 const char *array_type;
1925 if (var->array && list_count(var->array) > 1) /* multi-dimensional array */
1926 array_type = "ComplexArray";
1927 else
1929 if (!has_length && !has_size)
1930 array_type = "FixedArray";
1931 else if (has_length && !has_size)
1933 if (is_size_needed_for_phase(phase))
1935 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
1936 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1937 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1938 write_expr(file, length, 1);
1939 fprintf(file, ";\n\n");
1941 array_type = "VaryingArray";
1943 else if (!has_length && has_size)
1945 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
1947 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
1948 const expr_t, entry );
1949 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1950 write_expr(file, size, 1);
1951 fprintf(file, ";\n\n");
1953 array_type = "ConformantArray";
1955 else
1957 if (is_size_needed_for_phase(phase))
1959 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
1960 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
1961 const expr_t, entry );
1962 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1963 write_expr(file, size, 1);
1964 fprintf(file, ";\n");
1965 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1966 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1967 write_expr(file, length, 1);
1968 fprintf(file, ";\n\n");
1970 array_type = "ConformantVaryingArray";
1974 if (!in_attr && phase == PHASE_FREE)
1976 print_file(file, indent, "if (%s)\n", var->name);
1977 indent++;
1978 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
1980 else if (phase != PHASE_FREE)
1982 if (pointer_type == RPC_FC_UP)
1983 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1984 else
1985 print_phase_function(file, indent, array_type, phase, var->name, *type_offset + 4);
1988 else if (var->ptr_level == 0 && is_base_type(rtype))
1990 print_phase_basetype(file, indent, phase, pass, var, var->name);
1992 else if (var->ptr_level == 0)
1994 switch (rtype)
1996 case RPC_FC_STRUCT:
1997 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset);
1998 break;
1999 case RPC_FC_CSTRUCT:
2000 case RPC_FC_CPSTRUCT:
2001 print_phase_function(file, indent, "ConformantStruct", phase, var->name, *type_offset);
2002 break;
2003 case RPC_FC_CVSTRUCT:
2004 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var->name, *type_offset);
2005 break;
2006 case RPC_FC_BOGUS_STRUCT:
2007 print_phase_function(file, indent, "ComplexStruct", phase, var->name, *type_offset);
2008 break;
2009 case RPC_FC_RP:
2010 if (is_base_type( var->type->ref->type ))
2012 print_phase_basetype(file, indent, phase, pass, var, var->name);
2014 else if (var->type->ref->type == RPC_FC_STRUCT)
2016 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2017 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
2019 else
2021 const var_t *iid;
2022 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2023 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2024 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
2026 break;
2027 default:
2028 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
2029 var->name, rtype, var->ptr_level);
2032 else
2034 if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2036 print_phase_basetype(file, indent, phase, pass, var, var->name);
2038 else if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2040 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2041 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
2043 else
2045 const var_t *iid;
2046 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2047 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2048 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
2051 fprintf(file, "\n");
2052 next:
2053 *type_offset += get_size_typeformatstring_var(var);
2058 size_t get_size_procformatstring_var(const var_t *var)
2060 unsigned int type_offset = 2;
2061 return write_procformatstring_var(NULL, 0, var, FALSE, &type_offset);
2065 size_t get_size_procformatstring_func(const func_t *func)
2067 const var_t *var;
2068 size_t size = 0;
2070 /* argument list size */
2071 if (func->args)
2072 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2073 size += get_size_procformatstring_var(var);
2075 /* return value size */
2076 if (is_void(func->def->type, NULL))
2077 size += 2; /* FC_END and FC_PAD */
2078 else
2079 size += get_size_procformatstring_var(func->def);
2081 return size;
2084 size_t get_size_typeformatstring_var(const var_t *var)
2086 unsigned int type_offset = 0;
2087 write_typeformatstring_var(NULL, 0, var, &type_offset);
2088 return type_offset;
2091 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2093 const ifref_t *iface;
2094 size_t size = 1;
2095 const func_t *func;
2097 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2099 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2100 continue;
2102 if (iface->iface->funcs)
2103 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2104 if (!is_local(func->def->attrs))
2105 size += get_size_procformatstring_func( func );
2107 return size;
2110 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2112 const ifref_t *iface;
2113 size_t size = 3;
2114 const func_t *func;
2115 const var_t *var;
2117 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2119 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2120 continue;
2122 if (iface->iface->funcs)
2124 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2126 if (is_local(func->def->attrs)) continue;
2127 /* argument list size */
2128 if (func->args)
2129 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2130 size += get_size_typeformatstring_var(var);
2134 return size;
2137 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2138 const var_list_t *fields, const char *structvar)
2140 switch (e->type) {
2141 case EXPR_VOID:
2142 break;
2143 case EXPR_NUM:
2144 fprintf(h, "%ld", e->u.lval);
2145 break;
2146 case EXPR_HEXNUM:
2147 fprintf(h, "0x%lx", e->u.lval);
2148 break;
2149 case EXPR_TRUEFALSE:
2150 if (e->u.lval == 0)
2151 fprintf(h, "FALSE");
2152 else
2153 fprintf(h, "TRUE");
2154 break;
2155 case EXPR_IDENTIFIER:
2157 const var_t *field;
2158 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2159 if (!strcmp(e->u.sval, field->name))
2161 fprintf(h, "%s->%s", structvar, e->u.sval);
2162 break;
2165 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2166 break;
2168 case EXPR_NEG:
2169 fprintf(h, "-");
2170 write_struct_expr(h, e->ref, 1, fields, structvar);
2171 break;
2172 case EXPR_NOT:
2173 fprintf(h, "~");
2174 write_struct_expr(h, e->ref, 1, fields, structvar);
2175 break;
2176 case EXPR_PPTR:
2177 fprintf(h, "*");
2178 write_struct_expr(h, e->ref, 1, fields, structvar);
2179 break;
2180 case EXPR_CAST:
2181 fprintf(h, "(");
2182 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2183 fprintf(h, ")");
2184 write_struct_expr(h, e->ref, 1, fields, structvar);
2185 break;
2186 case EXPR_SIZEOF:
2187 fprintf(h, "sizeof(");
2188 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2189 fprintf(h, ")");
2190 break;
2191 case EXPR_SHL:
2192 case EXPR_SHR:
2193 case EXPR_MUL:
2194 case EXPR_DIV:
2195 case EXPR_ADD:
2196 case EXPR_SUB:
2197 case EXPR_AND:
2198 case EXPR_OR:
2199 if (brackets) fprintf(h, "(");
2200 write_struct_expr(h, e->ref, 1, fields, structvar);
2201 switch (e->type) {
2202 case EXPR_SHL: fprintf(h, " << "); break;
2203 case EXPR_SHR: fprintf(h, " >> "); break;
2204 case EXPR_MUL: fprintf(h, " * "); break;
2205 case EXPR_DIV: fprintf(h, " / "); break;
2206 case EXPR_ADD: fprintf(h, " + "); break;
2207 case EXPR_SUB: fprintf(h, " - "); break;
2208 case EXPR_AND: fprintf(h, " & "); break;
2209 case EXPR_OR: fprintf(h, " | "); break;
2210 default: break;
2212 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2213 if (brackets) fprintf(h, ")");
2214 break;
2215 case EXPR_COND:
2216 if (brackets) fprintf(h, "(");
2217 write_struct_expr(h, e->ref, 1, fields, structvar);
2218 fprintf(h, " ? ");
2219 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2220 fprintf(h, " : ");
2221 write_struct_expr(h, e->ext2, 1, fields, structvar);
2222 if (brackets) fprintf(h, ")");
2223 break;
2228 void declare_stub_args( FILE *file, int indent, const func_t *func )
2230 int in_attr, out_attr;
2231 int i = 0;
2232 const var_t *def = func->def;
2233 const var_t *var;
2235 /* declare return value '_RetVal' */
2236 if (!is_void(def->type, NULL))
2238 print_file(file, indent, "");
2239 write_type(file, def->type, def, def->tname);
2240 fprintf(file, " _RetVal;\n");
2243 if (!func->args)
2244 return;
2246 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2248 const expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2249 int has_size = is_non_void(size_is);
2250 int is_string = is_attr(var->attrs, ATTR_STRING);
2252 in_attr = is_attr(var->attrs, ATTR_IN);
2253 out_attr = is_attr(var->attrs, ATTR_OUT);
2254 if (!out_attr && !in_attr)
2255 in_attr = 1;
2257 if (!in_attr && !has_size && !is_string)
2259 int indirection;
2260 print_file(file, indent, "");
2261 write_type(file, var->type, NULL, var->tname);
2262 for (indirection = 0; indirection < var->ptr_level - 1; indirection++)
2263 fprintf(file, "*");
2264 fprintf(file, " _W%u;\n", i++);
2267 print_file(file, indent, "");
2268 write_type(file, var->type, var, var->tname);
2269 fprintf(file, " ");
2270 write_name(file, var);
2271 write_array(file, var->array, 0);
2272 fprintf(file, ";\n");
2277 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2279 int in_attr, out_attr;
2280 int i = 0, sep = 0;
2281 const var_t *var;
2282 const expr_list_t *size_is;
2283 int has_size;
2285 if (!func->args)
2286 return;
2288 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2290 int is_string = is_attr(var->attrs, ATTR_STRING);
2291 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2292 has_size = is_non_void(size_is);
2293 in_attr = is_attr(var->attrs, ATTR_IN);
2294 out_attr = is_attr(var->attrs, ATTR_OUT);
2295 if (!out_attr && !in_attr)
2296 in_attr = 1;
2298 if (!in_attr)
2300 print_file(file, indent, "");
2301 write_name(file, var);
2303 if (has_size)
2305 const expr_t *expr;
2306 unsigned int size, align = 0;
2307 type_t *type = var->type;
2309 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2310 LIST_FOR_EACH_ENTRY( expr, size_is, const expr_t, entry )
2312 if (expr->type == EXPR_VOID) continue;
2313 write_expr( file, expr, 1 );
2314 fprintf(file, " * ");
2316 size = type_memsize(type, 0, NULL, &align);
2317 fprintf(file, "%u);\n", size);
2319 else if (!is_string)
2321 fprintf(file, " = &_W%u;\n", i);
2322 if (var->ptr_level > 1)
2323 print_file(file, indent, "_W%u = 0;\n", i);
2324 i++;
2327 sep = 1;
2330 if (sep)
2331 fprintf(file, "\n");
2335 int write_expr_eval_routines(FILE *file, const char *iface)
2337 int result = 0;
2338 struct expr_eval_routine *eval;
2339 unsigned short callback_offset = 0;
2341 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2343 int indent = 0;
2344 result = 1;
2345 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2346 iface, eval->structure->name, callback_offset);
2347 print_file(file, indent, "{\n");
2348 indent++;
2349 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2350 eval->structure->name, eval->structure->name, eval->structure_size);
2351 fprintf(file, "\n");
2352 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2353 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2354 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2355 fprintf(file, ";\n");
2356 indent--;
2357 print_file(file, indent, "}\n\n");
2358 callback_offset++;
2360 return result;
2363 void write_expr_eval_routine_list(FILE *file, const char *iface)
2365 struct expr_eval_routine *eval;
2366 struct expr_eval_routine *cursor;
2367 unsigned short callback_offset = 0;
2369 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2370 fprintf(file, "{\n");
2372 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2374 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2375 iface, eval->structure->name, callback_offset);
2377 callback_offset++;
2378 list_remove(&eval->entry);
2379 free(eval);
2382 fprintf(file, "};\n\n");