widl: Add support for some simple pointer types.
[wine/multimedia.git] / tools / widl / typegen.c
blob4b4f4cf34f5e95ee6524c9e5b27718b183f08b85
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);
63 static size_t fields_memsize(const var_list_t *fields);
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 inline int is_base_type(unsigned char type)
164 switch (type)
166 case RPC_FC_BYTE:
167 case RPC_FC_CHAR:
168 case RPC_FC_USMALL:
169 case RPC_FC_SMALL:
170 case RPC_FC_WCHAR:
171 case RPC_FC_USHORT:
172 case RPC_FC_SHORT:
173 case RPC_FC_ULONG:
174 case RPC_FC_LONG:
175 case RPC_FC_HYPER:
176 case RPC_FC_IGNORE:
177 case RPC_FC_FLOAT:
178 case RPC_FC_DOUBLE:
179 case RPC_FC_ENUM16:
180 case RPC_FC_ENUM32:
181 case RPC_FC_ERROR_STATUS_T:
182 case RPC_FC_BIND_PRIMITIVE:
183 return TRUE;
185 default:
186 return FALSE;
190 static size_t write_procformatstring_var(FILE *file, int indent,
191 const var_t *var, int is_return, unsigned int *type_offset)
193 size_t size;
194 int ptr_level = var->ptr_level;
195 const type_t *type = var->type;
197 int is_in = is_attr(var->attrs, ATTR_IN);
198 int is_out = is_attr(var->attrs, ATTR_OUT);
200 if (!is_in && !is_out) is_in = TRUE;
202 if (ptr_level == 0 && !var->array && is_base_type(type->type))
204 if (is_return)
205 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
206 else
207 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
209 switch(type->type)
211 #define CASE_BASETYPE(fctype) \
212 case RPC_##fctype: \
213 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
214 size = 2; /* includes param type prefix */ \
215 break
217 CASE_BASETYPE(FC_BYTE);
218 CASE_BASETYPE(FC_CHAR);
219 CASE_BASETYPE(FC_WCHAR);
220 CASE_BASETYPE(FC_USHORT);
221 CASE_BASETYPE(FC_SHORT);
222 CASE_BASETYPE(FC_ULONG);
223 CASE_BASETYPE(FC_LONG);
224 CASE_BASETYPE(FC_HYPER);
225 CASE_BASETYPE(FC_IGNORE);
226 CASE_BASETYPE(FC_USMALL);
227 CASE_BASETYPE(FC_SMALL);
228 CASE_BASETYPE(FC_FLOAT);
229 CASE_BASETYPE(FC_DOUBLE);
230 CASE_BASETYPE(FC_ERROR_STATUS_T);
231 #undef CASE_BASETYPE
233 case RPC_FC_BIND_PRIMITIVE:
234 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
235 size = 2; /* includes param type prefix */
236 break;
238 default:
239 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
240 size = 0;
243 else
245 if (is_return)
246 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
247 else if (is_in && is_out)
248 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
249 else if (is_out)
250 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
251 else
252 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
254 print_file(file, indent, "0x01,\n");
255 print_file(file, indent, "NdrFcShort(0x%x),\n", *type_offset);
256 size = 4; /* includes param type prefix */
258 *type_offset += get_size_typeformatstring_var(var);
259 return size;
262 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
264 const ifref_t *iface;
265 int indent = 0;
266 const var_t *var;
267 unsigned int type_offset = 2;
269 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
270 print_file(file, indent, "{\n");
271 indent++;
272 print_file(file, indent, "0,\n");
273 print_file(file, indent, "{\n");
274 indent++;
276 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
278 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
279 continue;
281 if (iface->iface->funcs)
283 const func_t *func;
284 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
286 if (is_local(func->def->attrs)) continue;
287 /* emit argument data */
288 if (func->args)
290 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
291 write_procformatstring_var(file, indent, var, FALSE, &type_offset);
294 /* emit return value data */
295 var = func->def;
296 if (is_void(var->type, NULL))
298 print_file(file, indent, "0x5b, /* FC_END */\n");
299 print_file(file, indent, "0x5c, /* FC_PAD */\n");
301 else
302 write_procformatstring_var(file, indent, var, TRUE,
303 &type_offset);
308 print_file(file, indent, "0x0\n");
309 indent--;
310 print_file(file, indent, "}\n");
311 indent--;
312 print_file(file, indent, "};\n");
313 print_file(file, indent, "\n");
316 /* write conformance / variance descriptor */
317 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_t *expr)
319 unsigned char operator_type = 0;
320 const char *operator_string = "no operators";
321 const expr_t *subexpr = expr;
322 unsigned char correlation_type;
324 if (!file) return 4; /* optimisation for sizing pass */
326 if (expr->is_const)
328 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
329 error("write_conf_or_var_desc: constant value %ld is greater than "
330 "the maximum constant size of %d\n", expr->cval,
331 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
333 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
334 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
335 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
336 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
338 return 4;
341 switch (subexpr->type)
343 case EXPR_PPTR:
344 subexpr = subexpr->ref;
345 operator_type = RPC_FC_DEREFERENCE;
346 operator_string = "FC_DEREFERENCE";
347 break;
348 case EXPR_DIV:
349 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
351 subexpr = subexpr->ref;
352 operator_type = RPC_FC_DIV_2;
353 operator_string = "FC_DIV_2";
355 break;
356 case EXPR_MUL:
357 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
359 subexpr = subexpr->ref;
360 operator_type = RPC_FC_MULT_2;
361 operator_string = "FC_MULT_2";
363 break;
364 case EXPR_SUB:
365 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
367 subexpr = subexpr->ref;
368 operator_type = RPC_FC_SUB_1;
369 operator_string = "FC_SUB_1";
371 break;
372 case EXPR_ADD:
373 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
375 subexpr = subexpr->ref;
376 operator_type = RPC_FC_ADD_1;
377 operator_string = "FC_ADD_1";
379 break;
380 default:
381 break;
384 if (subexpr->type == EXPR_IDENTIFIER)
386 const type_t *correlation_variable = NULL;
387 unsigned char correlation_variable_type;
388 unsigned char param_type = 0;
389 const char *param_type_string = NULL;
390 size_t offset;
392 if (structure)
394 const var_t *var;
396 offset = 0;
397 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
399 offset -= type_memsize(var->type, var->ptr_level, var->array);
400 if (!strcmp(var->name, subexpr->u.sval))
402 correlation_variable = var->type;
403 break;
406 if (!correlation_variable)
407 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
408 subexpr->u.sval);
410 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
412 else
414 const var_t *var;
416 offset = 0;
417 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
419 if (!strcmp(var->name, subexpr->u.sval))
421 correlation_variable = var->type;
422 break;
424 /* FIXME: not all stack variables are sizeof(void *) */
425 offset += sizeof(void *);
427 if (!correlation_variable)
428 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
429 subexpr->u.sval);
431 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
434 correlation_variable_type = correlation_variable->type;
436 switch (correlation_variable_type)
438 case RPC_FC_CHAR:
439 case RPC_FC_SMALL:
440 param_type = RPC_FC_SMALL;
441 param_type_string = "FC_SMALL";
442 break;
443 case RPC_FC_BYTE:
444 case RPC_FC_USMALL:
445 param_type = RPC_FC_USMALL;
446 param_type_string = "FC_USMALL";
447 break;
448 case RPC_FC_WCHAR:
449 case RPC_FC_SHORT:
450 param_type = RPC_FC_SHORT;
451 param_type_string = "FC_SHORT";
452 break;
453 case RPC_FC_USHORT:
454 param_type = RPC_FC_USHORT;
455 param_type_string = "FC_USHORT";
456 break;
457 case RPC_FC_LONG:
458 param_type = RPC_FC_LONG;
459 param_type_string = "FC_LONG";
460 break;
461 case RPC_FC_ULONG:
462 param_type = RPC_FC_ULONG;
463 param_type_string = "FC_ULONG";
464 break;
465 default:
466 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
467 correlation_variable_type);
470 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
471 correlation_type | param_type,
472 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
473 param_type_string);
474 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
475 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
476 offset,
477 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
478 offset);
480 else
482 unsigned int callback_offset = 0;
484 if (structure)
486 struct expr_eval_routine *eval;
487 int found = 0;
489 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
491 if (!strcmp(eval->structure->name, structure->name) &&
492 !compare_expr(eval->expr, expr))
494 found = 1;
495 break;
497 callback_offset++;
500 if (!found)
502 eval = xmalloc(sizeof(*eval));
503 eval->structure = structure;
504 eval->structure_size = fields_memsize(structure->fields);
505 eval->expr = expr;
506 list_add_tail(&expr_eval_routines, &eval->entry);
509 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
511 else
513 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
514 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
517 if (callback_offset > USHRT_MAX)
518 error("Maximum number of callback routines reached\n");
520 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
521 correlation_type,
522 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
523 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
524 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
526 return 4;
529 static size_t fields_memsize(const var_list_t *fields)
531 size_t size = 0;
532 const var_t *v;
534 if (!fields) return 0;
535 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
536 size += type_memsize(v->type, v->ptr_level, v->array);
538 return size;
541 static size_t type_memsize(const type_t *t, int ptr_level, const array_dims_t *array)
543 size_t size = 0;
545 if (ptr_level)
546 return sizeof(void *);
548 switch (t->type)
550 case RPC_FC_BYTE:
551 case RPC_FC_CHAR:
552 case RPC_FC_USMALL:
553 case RPC_FC_SMALL:
554 size = 1;
555 break;
556 case RPC_FC_WCHAR:
557 case RPC_FC_USHORT:
558 case RPC_FC_SHORT:
559 case RPC_FC_ENUM16:
560 size = 2;
561 break;
562 case RPC_FC_ULONG:
563 case RPC_FC_LONG:
564 case RPC_FC_ERROR_STATUS_T:
565 case RPC_FC_ENUM32:
566 case RPC_FC_FLOAT:
567 size = 4;
568 break;
569 case RPC_FC_HYPER:
570 case RPC_FC_DOUBLE:
571 size = 8;
572 break;
573 case RPC_FC_STRUCT:
574 case RPC_FC_CVSTRUCT:
575 case RPC_FC_CPSTRUCT:
576 case RPC_FC_CSTRUCT:
577 case RPC_FC_PSTRUCT:
578 case RPC_FC_BOGUS_STRUCT:
579 case RPC_FC_ENCAPSULATED_UNION:
580 case RPC_FC_NON_ENCAPSULATED_UNION:
581 size = fields_memsize(t->fields);
582 break;
583 default:
584 error("type_memsize: Unknown type %d\n", t->type);
585 size = 0;
588 if (array)
590 expr_t *dim;
591 LIST_FOR_EACH_ENTRY( dim, array, expr_t, entry )
592 if (dim->is_const)
593 size *= dim->cval;
594 else
595 size = 0;
598 return size;
601 size_t get_type_memsize(const type_t *type)
603 return type_memsize(type, 0, NULL);
606 static int write_pointers(FILE *file, const attr_list_t *attrs,
607 const type_t *type, int ptr_level,
608 const array_dims_t *array, int level,
609 unsigned int *typestring_offset)
611 int pointers_written = 0;
612 const var_t *v;
614 /* don't generate a pointer for first-level arrays since we want to
615 * descend into them to write their pointers, not stop here */
616 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
618 return write_pointers(file, NULL, type, 0, NULL, level + 1, typestring_offset);
621 if (ptr_level != 0)
623 /* FIXME: only general algorithm implemented, not the actual writing */
624 error("write_pointers: Writing type format string for pointer is unimplemented\n");
625 return 1;
628 switch (type->type)
630 /* note: don't descend into complex structures or unions since these
631 * will always be generated as a separate type */
632 case RPC_FC_STRUCT:
633 case RPC_FC_CVSTRUCT:
634 case RPC_FC_CPSTRUCT:
635 case RPC_FC_CSTRUCT:
636 case RPC_FC_PSTRUCT:
637 if (!type->fields) break;
638 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
639 pointers_written += write_pointers(file, v->attrs, v->type,
640 v->ptr_level, v->array,
641 level + 1,
642 typestring_offset);
644 break;
646 default:
647 /* nothing to do */
648 break;
651 return pointers_written;
654 static size_t write_pointer_description(FILE *file, const attr_list_t *attrs,
655 const type_t *type, int ptr_level,
656 const array_dims_t *array, int level,
657 size_t typestring_offset)
659 size_t size = 0;
660 const var_t *v;
662 /* don't generate a pointer for first-level arrays since we want to
663 * descend into them to write their pointers, not stop here */
664 if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
666 return write_pointer_description(file, NULL, type, 0, NULL,
667 level + 1, typestring_offset);
670 if (ptr_level != 0)
672 /* FIXME: only general algorithm implemented, not the actual writing */
673 error("write_pointer_description: Writing pointer description is unimplemented\n");
674 return 0;
677 /* FIXME: search through all refs for pointers too */
679 switch (type->type)
681 /* note: don't descend into complex structures or unions since these
682 * will always be generated as a separate type */
683 case RPC_FC_STRUCT:
684 case RPC_FC_CVSTRUCT:
685 case RPC_FC_CPSTRUCT:
686 case RPC_FC_CSTRUCT:
687 case RPC_FC_PSTRUCT:
688 if (!type->fields) break;
689 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
690 size += write_pointer_description(file, v->attrs, v->type,
691 v->ptr_level, v->array,
692 level + 1,
693 typestring_offset);
695 break;
697 default:
698 /* nothing to do */
699 break;
702 return size;
705 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
706 const type_t *type, const array_dims_t *array,
707 const char *name, unsigned int *typestring_offset)
709 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
710 int has_size = size_is && (size_is->type != EXPR_VOID);
711 size_t start_offset = *typestring_offset;
712 unsigned char flags = 0;
713 int pointer_type;
714 unsigned char rtype;
716 if (is_ptr(type))
718 pointer_type = type->type;
719 type = type->ref;
721 else
722 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
724 if (!pointer_type)
725 pointer_type = RPC_FC_RP;
727 if (!get_attrp(attrs, ATTR_SIZEIS))
728 flags |= RPC_FC_P_SIMPLEPOINTER;
730 rtype = type->type;
732 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
734 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
735 return start_offset;
738 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
739 pointer_type, flags,
740 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
741 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
742 *typestring_offset += 2;
744 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
746 print_file(file, 2, "NdrFcShort(0x2),\n");
747 *typestring_offset += 2;
750 if (array && !is_conformant_array(array))
752 /* FIXME: multi-dimensional array */
753 const expr_t *dim = LIST_ENTRY( list_head( array ), expr_t, entry );
754 if (dim->cval > USHRT_MAX)
755 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
756 name, USHRT_MAX, dim->cval - USHRT_MAX);
758 if (rtype == RPC_FC_CHAR)
759 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
760 else
761 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
762 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
763 *typestring_offset += 2;
765 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim->cval, dim->cval);
766 *typestring_offset += 2;
768 return start_offset;
770 else if (has_size)
772 if (rtype == RPC_FC_CHAR)
773 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
774 else
775 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
776 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
777 *typestring_offset += 2;
779 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
781 return start_offset;
783 else
785 if (rtype == RPC_FC_CHAR)
786 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
787 else
788 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
789 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
790 *typestring_offset += 2;
792 return start_offset;
796 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs,
797 const type_t *type, const array_dims_t *array,
798 const char *name, unsigned int *typestring_offset)
800 const expr_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
801 const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
802 int has_length = length_is && (length_is->type != EXPR_VOID);
803 int has_size = (size_is && (size_is->type != EXPR_VOID)) || is_conformant_array(array);
804 size_t start_offset;
805 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
806 if (!pointer_type)
807 pointer_type = RPC_FC_RP;
809 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
810 pointer_type,
811 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
812 print_file(file, 2, "NdrFcShort(0x2),\n");
813 *typestring_offset += 4;
815 if (array && list_count(array) > 1) /* multi-dimensional array */
817 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
818 return 0;
820 else
822 const expr_t *dim = array ? LIST_ENTRY( list_head( array ), expr_t, entry ) : NULL;
823 size_t pointer_start_offset = *typestring_offset;
824 int has_pointer = 0;
826 if (write_pointers(file, attrs, type, 0, array, 0, typestring_offset) > 0)
827 has_pointer = 1;
829 start_offset = *typestring_offset;
831 if (!has_length && !has_size)
833 /* fixed array */
834 size_t size = type_memsize(type, 0, array);
835 if (size < USHRT_MAX)
837 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
838 /* alignment */
839 print_file(file, 2, "0x%x, /* 0 */\n", 0);
840 /* size */
841 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
842 *typestring_offset += 4;
844 else
846 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
847 /* alignment */
848 print_file(file, 2, "0x%x, /* 0 */\n", 0);
849 /* size */
850 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
851 *typestring_offset += 6;
854 if (has_pointer)
856 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
857 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
858 *typestring_offset += 2;
859 *typestring_offset = write_pointer_description(file, attrs,
860 type, 0, array, 0, pointer_start_offset);
861 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
862 *typestring_offset += 1;
865 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
866 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
867 *typestring_offset += 2;
869 return start_offset;
871 else if (has_length && !has_size)
873 /* varying array */
874 size_t element_size = type_memsize(type, 0, NULL);
875 size_t elements = dim->cval;
876 size_t total_size = element_size * elements;
878 if (total_size < USHRT_MAX)
880 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
881 /* alignment */
882 print_file(file, 2, "0x%x, /* 0 */\n", 0);
883 /* total size */
884 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
885 /* number of elements */
886 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
887 *typestring_offset += 6;
889 else
891 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
892 /* alignment */
893 print_file(file, 2, "0x%x, /* 0 */\n", 0);
894 /* total size */
895 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
896 /* number of elements */
897 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
898 *typestring_offset += 10;
900 /* element size */
901 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
902 *typestring_offset += 2;
904 *typestring_offset += write_conf_or_var_desc(file, current_func,
905 current_structure,
906 length_is);
908 if (has_pointer)
910 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
911 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
912 *typestring_offset += 2;
913 *typestring_offset += write_pointer_description(file, attrs,
914 type, 0, array, 0, pointer_start_offset);
915 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
916 *typestring_offset += 1;
919 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
920 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
921 *typestring_offset += 2;
923 return start_offset;
925 else if (!has_length && has_size)
927 /* conformant array */
928 size_t element_size = type_memsize(type, 0, NULL);
930 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
931 /* alignment */
932 print_file(file, 2, "0x%x, /* 0 */\n", 0);
933 /* element size */
934 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
935 *typestring_offset += 4;
937 *typestring_offset += write_conf_or_var_desc(file, current_func,
938 current_structure,
939 size_is ? size_is : dim);
941 if (has_pointer)
943 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
944 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
945 *typestring_offset += 2;
946 *typestring_offset += write_pointer_description(file, attrs,
947 type, 0, array, 0, pointer_start_offset);
948 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
949 *typestring_offset += 1;
952 print_file(file, 2, "0x%x, /* FIXME: write out conversion data */\n", type->type);
953 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
954 *typestring_offset += 2;
956 return start_offset;
958 else
960 /* conformant varying array */
961 size_t element_size = type_memsize(type, 0, NULL);
963 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
964 /* alignment */
965 print_file(file, 2, "0x%x, /* 0 */\n", 0);
966 /* element size */
967 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
968 *typestring_offset += 4;
970 *typestring_offset += write_conf_or_var_desc(file, current_func,
971 current_structure,
972 size_is ? size_is : dim);
973 *typestring_offset += write_conf_or_var_desc(file, current_func,
974 current_structure,
975 length_is);
977 if (has_pointer)
979 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
980 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
981 *typestring_offset += 2;
982 *typestring_offset += write_pointer_description(file, attrs,
983 type, 0, array, 0, pointer_start_offset);
984 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
985 *typestring_offset += 1;
988 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
989 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
990 *typestring_offset += 2;
992 return start_offset;
997 static const var_t *find_array_or_string_in_struct(const type_t *type)
999 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1001 if (is_array_type(last_field->attrs, last_field->ptr_level, last_field->array))
1002 return last_field;
1004 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1005 (last_field->type->type == RPC_FC_CPSTRUCT) ||
1006 (last_field->type->type == RPC_FC_CVSTRUCT));
1008 return find_array_or_string_in_struct(last_field->type);
1011 static size_t write_struct_members(FILE *file, const type_t *type)
1013 size_t typestring_size = 0;
1014 const var_t *field;
1016 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1018 unsigned char rtype = field->type->type;
1020 if (is_base_type(rtype))
1022 switch (rtype)
1024 #define CASE_BASETYPE(fctype) \
1025 case RPC_##fctype: \
1026 print_file(file, 2, "0x%02x,\t\t/* " #fctype " */\n", RPC_##fctype); \
1027 typestring_size++; \
1028 break;
1029 CASE_BASETYPE(FC_BYTE);
1030 CASE_BASETYPE(FC_CHAR);
1031 CASE_BASETYPE(FC_SMALL);
1032 CASE_BASETYPE(FC_USMALL);
1033 CASE_BASETYPE(FC_WCHAR);
1034 CASE_BASETYPE(FC_SHORT);
1035 CASE_BASETYPE(FC_USHORT);
1036 CASE_BASETYPE(FC_LONG);
1037 CASE_BASETYPE(FC_ULONG);
1038 CASE_BASETYPE(FC_FLOAT);
1039 CASE_BASETYPE(FC_HYPER);
1040 CASE_BASETYPE(FC_DOUBLE);
1041 CASE_BASETYPE(FC_ENUM16);
1042 CASE_BASETYPE(FC_ENUM32);
1043 CASE_BASETYPE(FC_IGNORE);
1044 CASE_BASETYPE(FC_ERROR_STATUS_T);
1045 default:
1046 break;
1047 #undef CASE_BASETYPE
1050 else
1051 error("Unsupported member type 0x%x\n", rtype);
1054 if (!(typestring_size % 2))
1056 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1057 typestring_size++;
1060 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1061 typestring_size++;
1063 return typestring_size;
1066 static size_t write_struct_tfs(FILE *file, const type_t *type,
1067 const char *name, unsigned int *typestring_offset)
1069 unsigned int total_size;
1070 const var_t *array;
1071 size_t start_offset;
1072 size_t array_offset;
1073 size_t pointer_offset;
1075 switch (type->type)
1077 case RPC_FC_STRUCT:
1078 case RPC_FC_PSTRUCT:
1079 total_size = type_memsize(type, 0, NULL);
1081 if (total_size > USHRT_MAX)
1082 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1083 name, USHRT_MAX, total_size - USHRT_MAX);
1085 if (type->type == RPC_FC_PSTRUCT)
1087 pointer_offset = *typestring_offset;
1088 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1090 else pointer_offset = 0; /* silence warning */
1092 start_offset = *typestring_offset;
1093 if (type->type == RPC_FC_STRUCT)
1094 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1095 else
1096 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1097 /* alignment */
1098 print_file(file, 2, "0x3,\n"); /* FIXME */
1099 /* total size */
1100 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1101 *typestring_offset += 4;
1103 if (type->type == RPC_FC_PSTRUCT)
1105 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1106 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1107 *typestring_offset += 2;
1108 *typestring_offset += write_pointer_description(file, NULL,
1109 type, 0, NULL, 0, pointer_offset);
1110 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1111 *typestring_offset += 1;
1114 /* member layout */
1115 *typestring_offset += write_struct_members(file, type);
1116 return start_offset;
1117 case RPC_FC_CSTRUCT:
1118 case RPC_FC_CPSTRUCT:
1119 total_size = type_memsize(type, 0, NULL);
1121 if (total_size > USHRT_MAX)
1122 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1123 name, USHRT_MAX, total_size - USHRT_MAX);
1125 array = find_array_or_string_in_struct(type);
1126 current_structure = type;
1127 array_offset = write_array_tfs(file, array->attrs, array->type,
1128 array->array, array->name,
1129 typestring_offset);
1130 current_structure = NULL;
1132 if (type->type == RPC_FC_CPSTRUCT)
1134 pointer_offset = *typestring_offset;
1135 write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1137 else pointer_offset = 0; /* silence warning */
1139 start_offset = *typestring_offset;
1140 if (type->type == RPC_FC_CSTRUCT)
1141 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1142 else
1143 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1144 /* alignment */
1145 print_file(file, 2, "0x0,\n");
1146 /* total size */
1147 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1148 *typestring_offset += 4;
1149 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1150 array_offset - *typestring_offset,
1151 array_offset - *typestring_offset,
1152 array_offset);
1153 *typestring_offset += 2;
1155 if (type->type == RPC_FC_CPSTRUCT)
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 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1167 *typestring_offset += 1;
1169 return start_offset;
1170 case RPC_FC_CVSTRUCT:
1171 total_size = type_memsize(type, 0, NULL);
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 if (is_attr(array->attrs, ATTR_STRING))
1180 array_offset = write_string_tfs(file, array->attrs, array->type,
1181 array->array, array->name,
1182 typestring_offset);
1183 else
1184 array_offset = write_array_tfs(file, array->attrs, array->type,
1185 array->array, array->name,
1186 typestring_offset);
1187 current_structure = NULL;
1189 pointer_offset = *typestring_offset;
1190 if (!write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset))
1191 pointer_offset = 0;
1193 start_offset = *typestring_offset;
1194 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1195 /* alignment */
1196 print_file(file, 2, "0x0,\n");
1197 /* total size */
1198 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1199 *typestring_offset += 4;
1200 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1201 array_offset - *typestring_offset,
1202 array_offset - *typestring_offset,
1203 array_offset);
1204 *typestring_offset += 2;
1206 if (pointer_offset != 0)
1208 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1209 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1210 *typestring_offset += 2;
1211 *typestring_offset += write_pointer_description(file, NULL,
1212 type, 0, NULL, 0, pointer_offset);
1213 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1214 *typestring_offset += 1;
1217 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1218 *typestring_offset += 1;
1220 return start_offset;
1221 default:
1222 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1223 return *typestring_offset;
1227 static void write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1228 unsigned char flags, size_t offset,
1229 unsigned int *typeformat_offset)
1231 int in_attr, out_attr;
1232 in_attr = is_attr(attrs, ATTR_IN);
1233 out_attr = is_attr(attrs, ATTR_OUT);
1234 if (!in_attr && !out_attr) in_attr = 1;
1236 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1237 flags |= 0x04;
1239 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1240 pointer_type,
1241 flags,
1242 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
1243 if (file)
1245 if (flags & 0x04)
1246 fprintf(file, " [allocated_on_stack]");
1247 if (flags & 0x10)
1248 fprintf(file, " [pointer_deref]");
1249 fprintf(file, " */\n");
1252 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", offset, offset);
1253 *typeformat_offset += 4;
1256 static size_t write_union_tfs(FILE *file, const attr_list_t *attrs,
1257 const type_t *type, const char *name,
1258 unsigned int *typeformat_offset)
1260 error("write_union_tfs: Unimplemented\n");
1261 return *typeformat_offset;
1264 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs,
1265 const char *name, unsigned int *typeformat_offset)
1267 size_t i;
1268 size_t start_offset = *typeformat_offset;
1269 const UUID *uuid = get_attrp(attrs, ATTR_UUID);
1271 if (! uuid)
1272 error("%s: interface %s missing UUID\n", __FUNCTION__, name);
1274 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1275 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1276 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1277 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1278 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1279 for (i = 0; i < 8; ++i)
1280 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1282 if (file)
1283 fprintf(file, "\n");
1285 *typeformat_offset += 18;
1286 return start_offset;
1289 static int get_ptr_attr(const type_t *t, int def_type)
1291 while (TRUE)
1293 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1294 if (ptr_attr)
1295 return ptr_attr;
1296 if (t->kind != TKIND_ALIAS)
1297 return def_type;
1298 t = t->orig;
1302 static size_t write_typeformatstring_var(FILE *file, int indent,
1303 const var_t *var, unsigned int *typeformat_offset)
1305 const type_t *type = var->type;
1306 int var_ptrs = var->ptr_level, type_ptrs = 0;
1307 int is_str = is_attr(var->attrs, ATTR_STRING);
1309 chat("write_typeformatstring_var: %s\n", var->name);
1311 while (TRUE)
1313 is_str = is_str || is_attr(type->attrs, ATTR_STRING);
1314 if (type->kind == TKIND_ALIAS)
1315 type = type->orig;
1316 else if (is_ptr(type))
1318 ++type_ptrs;
1319 type = type->ref;
1321 else
1323 type = var->type;
1324 break;
1328 while (TRUE)
1330 int ptr_level = var_ptrs + type_ptrs;
1331 int pointer_type = 0;
1333 chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type->type, type->name, ptr_level);
1335 /* var attrs only effect the rightmost pointer */
1336 if ((0 < var->ptr_level && var_ptrs == var->ptr_level)
1337 || (var->ptr_level == 0 && type == var->type))
1339 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1340 if (pointer_attr)
1342 if (! ptr_level)
1343 error("'%s': pointer attribute applied to non-pointer type",
1344 var->name);
1345 pointer_type = pointer_attr;
1347 else
1348 pointer_type = RPC_FC_RP;
1350 else /* pointers below other pointers default to unique */
1351 pointer_type = var_ptrs ? RPC_FC_UP : get_ptr_attr(type, RPC_FC_UP);
1353 if (is_str && ptr_level + (var->array != NULL) == 1)
1354 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1356 if (is_array_type(var->attrs, ptr_level, var->array))
1357 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1359 if (ptr_level == 0)
1361 /* basic types don't need a type format string */
1362 if (is_base_type(type->type))
1363 return 0;
1365 switch (type->type)
1367 case RPC_FC_STRUCT:
1368 case RPC_FC_PSTRUCT:
1369 case RPC_FC_CSTRUCT:
1370 case RPC_FC_CPSTRUCT:
1371 case RPC_FC_CVSTRUCT:
1372 case RPC_FC_BOGUS_STRUCT:
1373 return write_struct_tfs(file, type, var->name, typeformat_offset);
1374 case RPC_FC_ENCAPSULATED_UNION:
1375 case RPC_FC_NON_ENCAPSULATED_UNION:
1376 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1377 case RPC_FC_IGNORE:
1378 case RPC_FC_BIND_PRIMITIVE:
1379 /* nothing to do */
1380 return 0;
1381 default:
1382 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1385 else if (ptr_level == 1)
1387 size_t start_offset = *typeformat_offset;
1388 int in_attr = is_attr(var->attrs, ATTR_IN);
1389 int out_attr = is_attr(var->attrs, ATTR_OUT);
1390 const type_t *base = is_ptr(type) ? type->ref : type;
1392 if (base->type == RPC_FC_IP)
1394 return write_ip_tfs(file, base->attrs, base->name, typeformat_offset);
1397 /* special case for pointers to base types */
1398 switch (base->type)
1400 #define CASE_BASETYPE(fctype) \
1401 case RPC_##fctype: \
1402 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n", \
1403 pointer_type, \
1404 (!in_attr && out_attr) ? 0x0C : 0x08, \
1405 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1406 (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1407 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
1408 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
1409 *typeformat_offset += 4; \
1410 return start_offset
1411 CASE_BASETYPE(FC_BYTE);
1412 CASE_BASETYPE(FC_CHAR);
1413 CASE_BASETYPE(FC_SMALL);
1414 CASE_BASETYPE(FC_USMALL);
1415 CASE_BASETYPE(FC_WCHAR);
1416 CASE_BASETYPE(FC_SHORT);
1417 CASE_BASETYPE(FC_USHORT);
1418 CASE_BASETYPE(FC_LONG);
1419 CASE_BASETYPE(FC_ULONG);
1420 CASE_BASETYPE(FC_FLOAT);
1421 CASE_BASETYPE(FC_HYPER);
1422 CASE_BASETYPE(FC_DOUBLE);
1423 CASE_BASETYPE(FC_ENUM16);
1424 CASE_BASETYPE(FC_ENUM32);
1425 CASE_BASETYPE(FC_IGNORE);
1426 CASE_BASETYPE(FC_ERROR_STATUS_T);
1427 default:
1428 break;
1432 assert(ptr_level > 0);
1434 if (file)
1435 fprintf(file, "/* %2u */\n", *typeformat_offset);
1436 write_pointer_only_tfs(file, var->attrs, pointer_type,
1437 1 < ptr_level ? 0x10 : 0,
1438 2, typeformat_offset);
1440 if (var_ptrs)
1441 --var_ptrs;
1442 else
1444 --type_ptrs;
1445 type = type->ref;
1451 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1453 int indent = 0;
1454 const var_t *var;
1455 unsigned int typeformat_offset;
1456 const ifref_t *iface;
1458 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1459 print_file(file, indent, "{\n");
1460 indent++;
1461 print_file(file, indent, "0,\n");
1462 print_file(file, indent, "{\n");
1463 indent++;
1464 print_file(file, indent, "NdrFcShort(0x0),\n");
1465 typeformat_offset = 2;
1467 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1469 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1470 continue;
1472 if (iface->iface->funcs)
1474 const func_t *func;
1475 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1477 if (is_local(func->def->attrs)) continue;
1478 current_func = func;
1479 if (func->args)
1480 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1481 write_typeformatstring_var(file, indent, var,
1482 &typeformat_offset);
1487 print_file(file, indent, "0x0\n");
1488 indent--;
1489 print_file(file, indent, "}\n");
1490 indent--;
1491 print_file(file, indent, "};\n");
1492 print_file(file, indent, "\n");
1495 static unsigned int get_required_buffer_size_type(
1496 const type_t *type, int ptr_level, const array_dims_t *array,
1497 const char *name, unsigned int *alignment)
1499 *alignment = 0;
1500 if (ptr_level == 0 && !array)
1502 switch (type->type)
1504 case RPC_FC_BYTE:
1505 case RPC_FC_CHAR:
1506 case RPC_FC_USMALL:
1507 case RPC_FC_SMALL:
1508 *alignment = 4;
1509 return 1;
1511 case RPC_FC_WCHAR:
1512 case RPC_FC_USHORT:
1513 case RPC_FC_SHORT:
1514 *alignment = 4;
1515 return 2;
1517 case RPC_FC_ULONG:
1518 case RPC_FC_LONG:
1519 case RPC_FC_FLOAT:
1520 case RPC_FC_ERROR_STATUS_T:
1521 *alignment = 4;
1522 return 4;
1524 case RPC_FC_HYPER:
1525 case RPC_FC_DOUBLE:
1526 *alignment = 8;
1527 return 8;
1529 case RPC_FC_IGNORE:
1530 case RPC_FC_BIND_PRIMITIVE:
1531 return 0;
1533 case RPC_FC_STRUCT:
1535 size_t size = 0;
1536 const var_t *field;
1537 if (!type->fields) return 0;
1538 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1540 unsigned int alignment;
1541 size += get_required_buffer_size_type(
1542 field->type, field->ptr_level, field->array, field->name,
1543 &alignment);
1545 return size;
1548 case RPC_FC_RP:
1549 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1550 return get_required_buffer_size_type( type->ref, 0, NULL, name, alignment );
1551 return 0;
1553 default:
1554 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1555 return 0;
1558 return 0;
1561 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1563 expr_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1564 int has_size = (size_is && (size_is->type != EXPR_VOID));
1565 int in_attr = is_attr(var->attrs, ATTR_IN);
1566 int out_attr = is_attr(var->attrs, ATTR_OUT);
1568 if (!in_attr && !out_attr)
1569 in_attr = 1;
1571 *alignment = 0;
1573 if (pass == PASS_OUT)
1575 if (out_attr && var->ptr_level > 0)
1577 type_t *type = var->type;
1579 if (type->type == RPC_FC_STRUCT)
1581 const var_t *field;
1582 unsigned int size = 36;
1584 if (!type->fields) return size;
1585 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1587 unsigned int align;
1588 size += get_required_buffer_size_type(
1589 field->type, field->ptr_level, field->array, field->name,
1590 &align);
1592 return size;
1595 return 0;
1597 else
1599 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1601 if (var->ptr_level > 0)
1603 type_t *type = var->type;
1605 if (is_base_type(type->type))
1607 return 25;
1609 else if (type->type == RPC_FC_STRUCT)
1611 unsigned int size = 36;
1612 const var_t *field;
1614 if (!type->fields) return size;
1615 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1617 unsigned int align;
1618 size += get_required_buffer_size_type(
1619 field->type, field->ptr_level, field->array, field->name,
1620 &align);
1622 return size;
1627 return get_required_buffer_size_type(var->type, var->ptr_level, var->array, var->name, alignment);
1631 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
1633 const var_t *var;
1634 unsigned int total_size = 0, alignment;
1636 if (func->args)
1638 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1640 total_size += get_required_buffer_size(var, &alignment, pass);
1641 total_size += alignment;
1645 if (pass == PASS_OUT && !is_void(func->def->type, NULL))
1647 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
1648 total_size += alignment;
1650 return total_size;
1653 static void print_phase_function(FILE *file, int indent, const char *type,
1654 enum remoting_phase phase,
1655 const char *varname, unsigned int type_offset)
1657 const char *function;
1658 switch (phase)
1660 case PHASE_BUFFERSIZE:
1661 function = "BufferSize";
1662 break;
1663 case PHASE_MARSHAL:
1664 function = "Marshall";
1665 break;
1666 case PHASE_UNMARSHAL:
1667 function = "Unmarshall";
1668 break;
1669 case PHASE_FREE:
1670 function = "Free";
1671 break;
1672 default:
1673 assert(0);
1674 return;
1677 print_file(file, indent, "Ndr%s%s(\n", type, function);
1678 indent++;
1679 print_file(file, indent, "&_StubMsg,\n");
1680 print_file(file, indent, "%s%s,\n",
1681 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1682 varname);
1683 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1684 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1685 if (phase == PHASE_UNMARSHAL)
1686 print_file(file, indent, "0);\n");
1687 indent--;
1690 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1691 enum pass pass, const var_t *var,
1692 const char *varname)
1694 const type_t *type = var->type;
1695 unsigned int size;
1696 unsigned int alignment = 0;
1697 unsigned char rtype;
1699 /* no work to do for other phases, buffer sizing is done elsewhere */
1700 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1701 return;
1703 rtype = type->type;
1705 switch (rtype)
1707 case RPC_FC_BYTE:
1708 case RPC_FC_CHAR:
1709 case RPC_FC_SMALL:
1710 case RPC_FC_USMALL:
1711 size = 1;
1712 alignment = 1;
1713 break;
1715 case RPC_FC_WCHAR:
1716 case RPC_FC_USHORT:
1717 case RPC_FC_SHORT:
1718 size = 2;
1719 alignment = 2;
1720 break;
1722 case RPC_FC_ULONG:
1723 case RPC_FC_LONG:
1724 case RPC_FC_FLOAT:
1725 case RPC_FC_ERROR_STATUS_T:
1726 size = 4;
1727 alignment = 4;
1728 break;
1730 case RPC_FC_HYPER:
1731 case RPC_FC_DOUBLE:
1732 size = 8;
1733 alignment = 8;
1734 break;
1736 case RPC_FC_IGNORE:
1737 case RPC_FC_BIND_PRIMITIVE:
1738 /* no marshalling needed */
1739 return;
1741 default:
1742 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1743 size = 0;
1746 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1747 alignment - 1, alignment - 1);
1749 if (phase == PHASE_MARSHAL)
1751 print_file(file, indent, "*(");
1752 write_type(file, var->type, NULL, var->tname);
1753 if (var->ptr_level)
1754 fprintf(file, " *)_StubMsg.Buffer = *");
1755 else
1756 fprintf(file, " *)_StubMsg.Buffer = ");
1757 fprintf(file, varname);
1758 fprintf(file, ";\n");
1760 else if (phase == PHASE_UNMARSHAL)
1762 if (pass == PASS_IN || pass == PASS_RETURN)
1763 print_file(file, indent, "");
1764 else
1765 print_file(file, indent, "*");
1766 fprintf(file, varname);
1767 if (pass == PASS_IN && var->ptr_level)
1768 fprintf(file, " = (");
1769 else
1770 fprintf(file, " = *(");
1771 write_type(file, var->type, NULL, var->tname);
1772 fprintf(file, " *)_StubMsg.Buffer;\n");
1775 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1776 write_type(file, var->type, NULL, var->tname);
1777 fprintf(file, ");\n");
1780 /* returns whether the MaxCount, Offset or ActualCount members need to be
1781 * filled in for the specified phase */
1782 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1784 return (phase != PHASE_UNMARSHAL);
1787 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1788 unsigned int *type_offset, enum pass pass,
1789 enum remoting_phase phase)
1791 const expr_t *length_is;
1792 const expr_t *size_is;
1793 int in_attr, out_attr, has_length, has_size, pointer_type;
1794 const var_t *var;
1796 if (!func->args)
1797 return;
1799 if (phase == PHASE_BUFFERSIZE)
1801 unsigned int size = get_function_buffer_size( func, pass );
1802 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
1805 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1807 const type_t *type = var->type;
1808 unsigned char rtype;
1810 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1811 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1812 has_length = length_is && (length_is->type != EXPR_VOID);
1813 has_size = (size_is && (size_is->type != EXPR_VOID)) || (var->array && is_conformant_array(var->array));
1815 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1816 if (!pointer_type)
1817 pointer_type = RPC_FC_RP;
1819 in_attr = is_attr(var->attrs, ATTR_IN);
1820 out_attr = is_attr(var->attrs, ATTR_OUT);
1821 if (!in_attr && !out_attr)
1822 in_attr = 1;
1824 switch (pass)
1826 case PASS_IN:
1827 if (!in_attr) goto next;
1828 break;
1829 case PASS_OUT:
1830 if (!out_attr) goto next;
1831 break;
1832 case PASS_RETURN:
1833 break;
1836 rtype = type->type;
1838 if (is_string_type(var->attrs, var->ptr_level, var->array))
1840 if (var->array && !is_conformant_array(var->array))
1841 print_phase_function(file, indent, "NonConformantString", phase, var->name, *type_offset);
1842 else
1844 if (size_is && is_size_needed_for_phase(phase))
1846 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1847 write_expr(file, size_is, 1);
1848 fprintf(file, ";\n");
1851 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1852 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1853 else
1854 print_phase_function(file, indent, "ConformantString", phase, var->name,
1855 *type_offset + (has_size ? 4 : 2));
1858 else if (is_array_type(var->attrs, var->ptr_level, var->array))
1860 const char *array_type;
1862 if (var->array && list_count(var->array) > 1) /* multi-dimensional array */
1863 array_type = "ComplexArray";
1864 else
1866 const expr_t *dim = LIST_ENTRY( list_head( var->array ), expr_t, entry );
1867 if (!has_length && !has_size)
1868 array_type = "FixedArray";
1869 else if (has_length && !has_size)
1871 if (is_size_needed_for_phase(phase))
1873 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1874 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1875 write_expr(file, length_is, 1);
1876 fprintf(file, ";\n\n");
1878 array_type = "VaryingArray";
1880 else if (!has_length && has_size)
1882 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
1884 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1885 write_expr(file, size_is ? size_is : dim, 1);
1886 fprintf(file, ";\n\n");
1888 array_type = "ConformantArray";
1890 else
1892 if (is_size_needed_for_phase(phase))
1894 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1895 write_expr(file, size_is ? size_is : dim, 1);
1896 fprintf(file, ";\n");
1897 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1898 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1899 write_expr(file, length_is, 1);
1900 fprintf(file, ";\n\n");
1902 array_type = "ConformantVaryingArray";
1906 if (!in_attr && phase == PHASE_FREE)
1908 print_file(file, indent, "if (%s)\n", var->name);
1909 indent++;
1910 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
1912 else if (phase != PHASE_FREE)
1914 if (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, array_type, phase, var->name, *type_offset + 4);
1920 else if (var->ptr_level == 0 && is_base_type(rtype))
1922 print_phase_basetype(file, indent, phase, pass, var, var->name);
1924 else if (var->ptr_level == 0)
1926 switch (rtype)
1928 case RPC_FC_STRUCT:
1929 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset);
1930 break;
1931 case RPC_FC_CSTRUCT:
1932 case RPC_FC_CPSTRUCT:
1933 print_phase_function(file, indent, "ConformantStruct", phase, var->name, *type_offset);
1934 break;
1935 case RPC_FC_CVSTRUCT:
1936 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var->name, *type_offset);
1937 break;
1938 case RPC_FC_BOGUS_STRUCT:
1939 print_phase_function(file, indent, "ComplexStruct", phase, var->name, *type_offset);
1940 break;
1941 case RPC_FC_RP:
1942 if (is_base_type( var->type->ref->type ))
1944 print_phase_basetype(file, indent, phase, pass, var, var->name);
1946 else if (var->type->ref->type == RPC_FC_STRUCT)
1948 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
1949 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
1951 else
1953 const var_t *iid;
1954 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
1955 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
1956 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1958 break;
1959 default:
1960 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1961 var->name, rtype, var->ptr_level);
1964 else
1966 if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
1968 print_phase_basetype(file, indent, phase, pass, var, var->name);
1970 else if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
1972 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
1973 print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
1975 else
1977 const var_t *iid;
1978 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
1979 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
1980 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1983 fprintf(file, "\n");
1984 next:
1985 *type_offset += get_size_typeformatstring_var(var);
1990 size_t get_size_procformatstring_var(const var_t *var)
1992 unsigned int type_offset = 2;
1993 return write_procformatstring_var(NULL, 0, var, FALSE, &type_offset);
1997 size_t get_size_procformatstring_func(const func_t *func)
1999 const var_t *var;
2000 size_t size = 0;
2002 /* argument list size */
2003 if (func->args)
2004 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2005 size += get_size_procformatstring_var(var);
2007 /* return value size */
2008 if (is_void(func->def->type, NULL))
2009 size += 2; /* FC_END and FC_PAD */
2010 else
2011 size += get_size_procformatstring_var(func->def);
2013 return size;
2016 size_t get_size_typeformatstring_var(const var_t *var)
2018 unsigned int type_offset = 0;
2019 write_typeformatstring_var(NULL, 0, var, &type_offset);
2020 return type_offset;
2023 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2025 const ifref_t *iface;
2026 size_t size = 1;
2027 const func_t *func;
2029 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2031 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2032 continue;
2034 if (iface->iface->funcs)
2035 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2036 if (!is_local(func->def->attrs))
2037 size += get_size_procformatstring_func( func );
2039 return size;
2042 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2044 const ifref_t *iface;
2045 size_t size = 3;
2046 const func_t *func;
2047 const var_t *var;
2049 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2051 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2052 continue;
2054 if (iface->iface->funcs)
2056 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2058 if (is_local(func->def->attrs)) continue;
2059 /* argument list size */
2060 if (func->args)
2061 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2062 size += get_size_typeformatstring_var(var);
2066 return size;
2069 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2070 const var_list_t *fields, const char *structvar)
2072 switch (e->type) {
2073 case EXPR_VOID:
2074 break;
2075 case EXPR_NUM:
2076 fprintf(h, "%ld", e->u.lval);
2077 break;
2078 case EXPR_HEXNUM:
2079 fprintf(h, "0x%lx", e->u.lval);
2080 break;
2081 case EXPR_TRUEFALSE:
2082 if (e->u.lval == 0)
2083 fprintf(h, "FALSE");
2084 else
2085 fprintf(h, "TRUE");
2086 break;
2087 case EXPR_IDENTIFIER:
2089 const var_t *field;
2090 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2091 if (!strcmp(e->u.sval, field->name))
2093 fprintf(h, "%s->%s", structvar, e->u.sval);
2094 break;
2097 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2098 break;
2100 case EXPR_NEG:
2101 fprintf(h, "-");
2102 write_struct_expr(h, e->ref, 1, fields, structvar);
2103 break;
2104 case EXPR_NOT:
2105 fprintf(h, "~");
2106 write_struct_expr(h, e->ref, 1, fields, structvar);
2107 break;
2108 case EXPR_PPTR:
2109 fprintf(h, "*");
2110 write_struct_expr(h, e->ref, 1, fields, structvar);
2111 break;
2112 case EXPR_CAST:
2113 fprintf(h, "(");
2114 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2115 fprintf(h, ")");
2116 write_struct_expr(h, e->ref, 1, fields, structvar);
2117 break;
2118 case EXPR_SIZEOF:
2119 fprintf(h, "sizeof(");
2120 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2121 fprintf(h, ")");
2122 break;
2123 case EXPR_SHL:
2124 case EXPR_SHR:
2125 case EXPR_MUL:
2126 case EXPR_DIV:
2127 case EXPR_ADD:
2128 case EXPR_SUB:
2129 case EXPR_AND:
2130 case EXPR_OR:
2131 if (brackets) fprintf(h, "(");
2132 write_struct_expr(h, e->ref, 1, fields, structvar);
2133 switch (e->type) {
2134 case EXPR_SHL: fprintf(h, " << "); break;
2135 case EXPR_SHR: fprintf(h, " >> "); break;
2136 case EXPR_MUL: fprintf(h, " * "); break;
2137 case EXPR_DIV: fprintf(h, " / "); break;
2138 case EXPR_ADD: fprintf(h, " + "); break;
2139 case EXPR_SUB: fprintf(h, " - "); break;
2140 case EXPR_AND: fprintf(h, " & "); break;
2141 case EXPR_OR: fprintf(h, " | "); break;
2142 default: break;
2144 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2145 if (brackets) fprintf(h, ")");
2146 break;
2147 case EXPR_COND:
2148 if (brackets) fprintf(h, "(");
2149 write_struct_expr(h, e->ref, 1, fields, structvar);
2150 fprintf(h, " ? ");
2151 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2152 fprintf(h, " : ");
2153 write_struct_expr(h, e->ext2, 1, fields, structvar);
2154 if (brackets) fprintf(h, ")");
2155 break;
2160 void declare_stub_args( FILE *file, int indent, const func_t *func )
2162 int in_attr, out_attr;
2163 int i = 0;
2164 const var_t *def = func->def;
2165 const var_t *var;
2167 /* declare return value '_RetVal' */
2168 if (!is_void(def->type, NULL))
2170 print_file(file, indent, "");
2171 write_type(file, def->type, def, def->tname);
2172 fprintf(file, " _RetVal;\n");
2175 if (!func->args)
2176 return;
2178 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2180 const expr_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2181 int has_size = size_is && (size_is->type != EXPR_VOID);
2182 int is_string = is_attr(var->attrs, ATTR_STRING);
2184 in_attr = is_attr(var->attrs, ATTR_IN);
2185 out_attr = is_attr(var->attrs, ATTR_OUT);
2186 if (!out_attr && !in_attr)
2187 in_attr = 1;
2189 if (!in_attr && !has_size && !is_string)
2191 int indirection;
2192 print_file(file, indent, "");
2193 write_type(file, var->type, NULL, var->tname);
2194 for (indirection = 0; indirection < var->ptr_level - 1; indirection++)
2195 fprintf(file, "*");
2196 fprintf(file, " _W%u;\n", i++);
2199 print_file(file, indent, "");
2200 write_type(file, var->type, var, var->tname);
2201 fprintf(file, " ");
2202 write_name(file, var);
2203 write_array(file, var->array, 0);
2204 fprintf(file, ";\n");
2209 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2211 int in_attr, out_attr;
2212 int i = 0, sep = 0;
2213 const var_t *var;
2214 const expr_t *size_is;
2215 int has_size;
2217 if (!func->args)
2218 return;
2220 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2222 int is_string = is_attr(var->attrs, ATTR_STRING);
2223 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2224 has_size = size_is && (size_is->type != EXPR_VOID);
2225 in_attr = is_attr(var->attrs, ATTR_IN);
2226 out_attr = is_attr(var->attrs, ATTR_OUT);
2227 if (!out_attr && !in_attr)
2228 in_attr = 1;
2230 if (!in_attr)
2232 print_file(file, indent, "");
2233 write_name(file, var);
2235 if (has_size)
2237 unsigned int size;
2238 type_t *type = var->type;
2240 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2241 write_expr(file, size_is, 1);
2242 size = get_type_memsize(type);
2243 fprintf(file, " * %u);\n", size);
2245 else if (!is_string)
2247 fprintf(file, " = &_W%u;\n", i);
2248 if (var->ptr_level > 1)
2249 print_file(file, indent, "_W%u = 0;\n", i);
2250 i++;
2253 sep = 1;
2256 if (sep)
2257 fprintf(file, "\n");
2261 int write_expr_eval_routines(FILE *file, const char *iface)
2263 int result = 0;
2264 struct expr_eval_routine *eval;
2265 unsigned short callback_offset = 0;
2267 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2269 int indent = 0;
2270 result = 1;
2271 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2272 iface, eval->structure->name, callback_offset);
2273 print_file(file, indent, "{\n");
2274 indent++;
2275 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2276 eval->structure->name, eval->structure->name, eval->structure_size);
2277 fprintf(file, "\n");
2278 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2279 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2280 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2281 fprintf(file, ";\n");
2282 indent--;
2283 print_file(file, indent, "}\n\n");
2284 callback_offset++;
2286 return result;
2289 void write_expr_eval_routine_list(FILE *file, const char *iface)
2291 struct expr_eval_routine *eval;
2292 struct expr_eval_routine *cursor;
2293 unsigned short callback_offset = 0;
2295 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2296 fprintf(file, "{\n");
2298 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2300 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2301 iface, eval->structure->name, callback_offset);
2303 callback_offset++;
2304 list_remove(&eval->entry);
2305 free(eval);
2308 fprintf(file, "};\n\n");