widl: Replace erroneously removed current_func assignment.
[wine/winequartzdrv.git] / tools / widl / typegen.c
blob3aca92f0634beb38154ad397d79ab5d7894e64cc
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, const array_dims_t *array, unsigned int *align);
63 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
64 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name,
65 unsigned int *typestring_offset);
66 const char *string_of_type(unsigned char type)
68 switch (type)
70 case RPC_FC_BYTE: return "FC_BYTE";
71 case RPC_FC_CHAR: return "FC_CHAR";
72 case RPC_FC_SMALL: return "FC_SMALL";
73 case RPC_FC_USMALL: return "FC_USMALL";
74 case RPC_FC_WCHAR: return "FC_WCHAR";
75 case RPC_FC_SHORT: return "FC_SHORT";
76 case RPC_FC_USHORT: return "FC_USHORT";
77 case RPC_FC_LONG: return "FC_LONG";
78 case RPC_FC_ULONG: return "FC_ULONG";
79 case RPC_FC_FLOAT: return "FC_FLOAT";
80 case RPC_FC_HYPER: return "FC_HYPER";
81 case RPC_FC_DOUBLE: return "FC_DOUBLE";
82 case RPC_FC_ENUM16: return "FC_ENUM16";
83 case RPC_FC_ENUM32: return "FC_ENUM32";
84 case RPC_FC_IGNORE: return "FC_IGNORE";
85 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
86 case RPC_FC_RP: return "FC_RP";
87 case RPC_FC_UP: return "FC_UP";
88 case RPC_FC_OP: return "FC_OP";
89 case RPC_FC_FP: return "FC_FP";
90 default:
91 error("string_of_type: unknown type 0x%02x\n", type);
92 return NULL;
96 static int is_struct(unsigned char type)
98 switch (type)
100 case RPC_FC_STRUCT:
101 case RPC_FC_PSTRUCT:
102 case RPC_FC_CSTRUCT:
103 case RPC_FC_CPSTRUCT:
104 case RPC_FC_CVSTRUCT:
105 case RPC_FC_BOGUS_STRUCT:
106 return 1;
107 default:
108 return 0;
112 static int compare_expr(const expr_t *a, const expr_t *b)
114 int ret;
116 if (a->type != b->type)
117 return a->type - b->type;
119 switch (a->type)
121 case EXPR_NUM:
122 case EXPR_HEXNUM:
123 case EXPR_TRUEFALSE:
124 return a->u.lval - b->u.lval;
125 case EXPR_IDENTIFIER:
126 return strcmp(a->u.sval, b->u.sval);
127 case EXPR_COND:
128 ret = compare_expr(a->ref, b->ref);
129 if (ret != 0)
130 return ret;
131 ret = compare_expr(a->u.ext, b->u.ext);
132 if (ret != 0)
133 return ret;
134 return compare_expr(a->ext2, b->ext2);
135 case EXPR_OR:
136 case EXPR_AND:
137 case EXPR_ADD:
138 case EXPR_SUB:
139 case EXPR_MUL:
140 case EXPR_DIV:
141 case EXPR_SHL:
142 case EXPR_SHR:
143 ret = compare_expr(a->ref, b->ref);
144 if (ret != 0)
145 return ret;
146 return compare_expr(a->u.ext, b->u.ext);
147 case EXPR_NOT:
148 case EXPR_NEG:
149 case EXPR_PPTR:
150 case EXPR_CAST:
151 case EXPR_SIZEOF:
152 return compare_expr(a->ref, b->ref);
153 case EXPR_VOID:
154 return 0;
156 return -1;
159 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
160 do { \
161 if (file) \
162 fprintf(file, "/* %2u */\n", typestring_offset); \
163 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
165 while (0)
167 static int print_file(FILE *file, int indent, const char *format, ...)
169 va_list va;
170 int i, r;
172 if (!file) return 0;
174 va_start(va, format);
175 for (i = 0; i < indent; i++)
176 fprintf(file, " ");
177 r = vfprintf(file, format, va);
178 va_end(va);
179 return r;
182 static void write_formatdesc(FILE *f, int indent, const char *str)
184 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
185 print_file(f, indent, "{\n");
186 print_file(f, indent + 1, "short Pad;\n");
187 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
188 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
189 print_file(f, indent, "\n");
192 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
194 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
195 get_size_typeformatstring(ifaces, for_objects));
197 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
198 get_size_procformatstring(ifaces, for_objects));
200 fprintf(f, "\n");
201 write_formatdesc(f, indent, "TYPE");
202 write_formatdesc(f, indent, "PROC");
203 fprintf(f, "\n");
204 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
205 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
206 print_file(f, indent, "\n");
209 static int is_user_derived(const var_t *v)
211 const type_t *type = v->type;
213 if (v->attrs && is_attr( v->attrs, ATTR_WIREMARSHAL )) return 1;
215 while (type)
217 if (type->attrs && is_attr( type->attrs, ATTR_WIREMARSHAL )) return 1;
218 type = type->ref;
220 return 0;
223 static inline int is_base_type(unsigned char type)
225 switch (type)
227 case RPC_FC_BYTE:
228 case RPC_FC_CHAR:
229 case RPC_FC_USMALL:
230 case RPC_FC_SMALL:
231 case RPC_FC_WCHAR:
232 case RPC_FC_USHORT:
233 case RPC_FC_SHORT:
234 case RPC_FC_ULONG:
235 case RPC_FC_LONG:
236 case RPC_FC_HYPER:
237 case RPC_FC_IGNORE:
238 case RPC_FC_FLOAT:
239 case RPC_FC_DOUBLE:
240 case RPC_FC_ENUM16:
241 case RPC_FC_ENUM32:
242 case RPC_FC_ERROR_STATUS_T:
243 case RPC_FC_BIND_PRIMITIVE:
244 return TRUE;
246 default:
247 return FALSE;
251 static size_t write_procformatstring_var(FILE *file, int indent,
252 const var_t *var, int is_return)
254 size_t size;
255 const type_t *type = var->type;
257 int is_in = is_attr(var->attrs, ATTR_IN);
258 int is_out = is_attr(var->attrs, ATTR_OUT);
260 if (!is_in && !is_out) is_in = TRUE;
262 if (!var->array && is_base_type(type->type))
264 if (is_return)
265 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
266 else
267 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
269 if (is_base_type(type->type))
271 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
272 size = 2; /* includes param type prefix */
274 else if (type->type == RPC_FC_BIND_PRIMITIVE)
276 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
277 size = 2; /* includes param type prefix */
279 else
281 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
282 size = 0;
285 else
287 if (is_return)
288 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
289 else if (is_in && is_out)
290 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
291 else if (is_out)
292 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
293 else
294 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
296 print_file(file, indent, "0x01,\n");
297 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
298 size = 4; /* includes param type prefix */
300 return size;
303 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
305 const ifref_t *iface;
306 int indent = 0;
307 const var_t *var;
309 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
310 print_file(file, indent, "{\n");
311 indent++;
312 print_file(file, indent, "0,\n");
313 print_file(file, indent, "{\n");
314 indent++;
316 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
318 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
319 continue;
321 if (iface->iface->funcs)
323 const func_t *func;
324 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
326 if (is_local(func->def->attrs)) continue;
327 /* emit argument data */
328 if (func->args)
330 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
331 write_procformatstring_var(file, indent, var, FALSE);
334 /* emit return value data */
335 var = func->def;
336 if (is_void(var->type))
338 print_file(file, indent, "0x5b, /* FC_END */\n");
339 print_file(file, indent, "0x5c, /* FC_PAD */\n");
341 else
342 write_procformatstring_var(file, indent, var, TRUE);
347 print_file(file, indent, "0x0\n");
348 indent--;
349 print_file(file, indent, "}\n");
350 indent--;
351 print_file(file, indent, "};\n");
352 print_file(file, indent, "\n");
355 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
357 if (is_base_type(type->type))
359 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
360 *typestring_offset += 1;
361 return 1;
364 return 0;
367 /* write conformance / variance descriptor */
368 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_list_t *expr_list)
370 unsigned char operator_type = 0;
371 const char *operator_string = "no operators";
372 const expr_t *expr, *subexpr;
373 unsigned char correlation_type;
375 if (!file) return 4; /* optimisation for sizing pass */
377 if (list_count(expr_list) > 1)
378 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
380 expr = subexpr = LIST_ENTRY( list_head(expr_list), const expr_t, entry );
382 if (expr->is_const)
384 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
385 error("write_conf_or_var_desc: constant value %ld is greater than "
386 "the maximum constant size of %d\n", expr->cval,
387 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
389 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
390 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
391 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
392 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
394 return 4;
397 switch (subexpr->type)
399 case EXPR_PPTR:
400 subexpr = subexpr->ref;
401 operator_type = RPC_FC_DEREFERENCE;
402 operator_string = "FC_DEREFERENCE";
403 break;
404 case EXPR_DIV:
405 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
407 subexpr = subexpr->ref;
408 operator_type = RPC_FC_DIV_2;
409 operator_string = "FC_DIV_2";
411 break;
412 case EXPR_MUL:
413 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
415 subexpr = subexpr->ref;
416 operator_type = RPC_FC_MULT_2;
417 operator_string = "FC_MULT_2";
419 break;
420 case EXPR_SUB:
421 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
423 subexpr = subexpr->ref;
424 operator_type = RPC_FC_SUB_1;
425 operator_string = "FC_SUB_1";
427 break;
428 case EXPR_ADD:
429 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
431 subexpr = subexpr->ref;
432 operator_type = RPC_FC_ADD_1;
433 operator_string = "FC_ADD_1";
435 break;
436 default:
437 break;
440 if (subexpr->type == EXPR_IDENTIFIER)
442 const type_t *correlation_variable = NULL;
443 unsigned char correlation_variable_type;
444 unsigned char param_type = 0;
445 const char *param_type_string = NULL;
446 size_t offset;
448 if (structure)
450 const var_t *var;
452 offset = 0;
453 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
455 unsigned int align = 0;
456 offset -= type_memsize(var->type, var->array, &align);
457 /* FIXME: take alignment into account */
458 if (!strcmp(var->name, subexpr->u.sval))
460 correlation_variable = var->type;
461 break;
464 if (!correlation_variable)
465 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
466 subexpr->u.sval);
468 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
470 else
472 const var_t *var;
474 offset = sizeof(void *);
475 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
477 if (!strcmp(var->name, subexpr->u.sval))
479 correlation_variable = var->type;
480 break;
482 /* FIXME: not all stack variables are sizeof(void *) */
483 offset += sizeof(void *);
485 if (!correlation_variable)
486 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
487 subexpr->u.sval);
489 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
492 correlation_variable_type = correlation_variable->type;
494 switch (correlation_variable_type)
496 case RPC_FC_CHAR:
497 case RPC_FC_SMALL:
498 param_type = RPC_FC_SMALL;
499 param_type_string = "FC_SMALL";
500 break;
501 case RPC_FC_BYTE:
502 case RPC_FC_USMALL:
503 param_type = RPC_FC_USMALL;
504 param_type_string = "FC_USMALL";
505 break;
506 case RPC_FC_WCHAR:
507 case RPC_FC_SHORT:
508 param_type = RPC_FC_SHORT;
509 param_type_string = "FC_SHORT";
510 break;
511 case RPC_FC_USHORT:
512 param_type = RPC_FC_USHORT;
513 param_type_string = "FC_USHORT";
514 break;
515 case RPC_FC_LONG:
516 param_type = RPC_FC_LONG;
517 param_type_string = "FC_LONG";
518 break;
519 case RPC_FC_ULONG:
520 param_type = RPC_FC_ULONG;
521 param_type_string = "FC_ULONG";
522 break;
523 case RPC_FC_RP:
524 case RPC_FC_UP:
525 case RPC_FC_OP:
526 case RPC_FC_FP:
527 if (sizeof(void *) == 4) /* FIXME */
529 param_type = RPC_FC_LONG;
530 param_type_string = "FC_LONG";
532 else
534 param_type = RPC_FC_HYPER;
535 param_type_string = "FC_HYPER";
537 break;
538 default:
539 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
540 correlation_variable_type);
543 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
544 correlation_type | param_type,
545 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
546 param_type_string);
547 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
548 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
549 offset,
550 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
551 offset);
553 else
555 unsigned int callback_offset = 0;
557 if (structure)
559 struct expr_eval_routine *eval;
560 int found = 0;
562 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
564 if (!strcmp(eval->structure->name, structure->name) &&
565 !compare_expr(eval->expr, expr))
567 found = 1;
568 break;
570 callback_offset++;
573 if (!found)
575 unsigned int align = 0;
576 eval = xmalloc(sizeof(*eval));
577 eval->structure = structure;
578 eval->structure_size = fields_memsize(structure->fields, &align);
579 eval->expr = expr;
580 list_add_tail(&expr_eval_routines, &eval->entry);
583 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
585 else
587 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
588 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
591 if (callback_offset > USHRT_MAX)
592 error("Maximum number of callback routines reached\n");
594 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
595 correlation_type,
596 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
597 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
598 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
600 return 4;
603 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
605 size_t size = 0;
606 const var_t *v;
608 if (!fields) return 0;
609 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
610 size += type_memsize(v->type, v->array, align);
612 return size;
615 static size_t get_array_size( const array_dims_t *array )
617 size_t size = 1;
618 const expr_t *dim;
620 if (!array) return 0;
622 LIST_FOR_EACH_ENTRY( dim, array, expr_t, entry )
624 if (!dim->is_const) return 0;
625 size *= dim->cval;
628 return size;
631 static size_t type_memsize(const type_t *t, const array_dims_t *array, unsigned int *align)
633 size_t size = 0;
635 if (is_ptr(t))
637 size = sizeof(void *);
638 if (size > *align) *align = size;
640 else switch (t->type)
642 case RPC_FC_BYTE:
643 case RPC_FC_CHAR:
644 case RPC_FC_USMALL:
645 case RPC_FC_SMALL:
646 size = 1;
647 if (size > *align) *align = size;
648 break;
649 case RPC_FC_WCHAR:
650 case RPC_FC_USHORT:
651 case RPC_FC_SHORT:
652 case RPC_FC_ENUM16:
653 size = 2;
654 if (size > *align) *align = size;
655 break;
656 case RPC_FC_ULONG:
657 case RPC_FC_LONG:
658 case RPC_FC_ERROR_STATUS_T:
659 case RPC_FC_ENUM32:
660 case RPC_FC_FLOAT:
661 size = 4;
662 if (size > *align) *align = size;
663 break;
664 case RPC_FC_HYPER:
665 case RPC_FC_DOUBLE:
666 size = 8;
667 if (size > *align) *align = size;
668 break;
669 case RPC_FC_STRUCT:
670 case RPC_FC_CVSTRUCT:
671 case RPC_FC_CPSTRUCT:
672 case RPC_FC_CSTRUCT:
673 case RPC_FC_PSTRUCT:
674 case RPC_FC_BOGUS_STRUCT:
675 case RPC_FC_ENCAPSULATED_UNION:
676 case RPC_FC_NON_ENCAPSULATED_UNION:
677 size = fields_memsize(t->fields, align);
678 break;
679 default:
680 error("type_memsize: Unknown type %d\n", t->type);
681 size = 0;
684 if (array) size *= get_array_size( array );
685 return size;
688 static size_t write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
690 short absoff = type->ref->typestring_offset;
691 short reloff = absoff - (offset + 2);
692 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
694 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
695 type->type, ptr_attr, string_of_type(type->type));
696 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
697 reloff, reloff, absoff);
698 return 4;
701 static size_t write_simple_pointer(FILE *file, const type_t *type)
703 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
704 type->type, string_of_type(type->type));
705 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
706 string_of_type(type->ref->type));
707 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
708 return 4;
711 static size_t write_pointer_tfs(FILE *file, type_t *type, size_t *typestring_offset)
713 size_t offset = *typestring_offset;
715 print_file(file, 0, "/* %d */\n", offset);
716 type->typestring_offset = offset;
718 if (type->ref->typestring_offset)
719 *typestring_offset += write_nonsimple_pointer(file, type, offset);
720 else if (is_base_type(type->ref->type))
721 *typestring_offset += write_simple_pointer(file, type);
723 return offset;
726 static int has_known_tfs(const type_t *type)
728 return type->typestring_offset || is_base_type(type->type);
731 static int write_pointers(FILE *file, const attr_list_t *attrs,
732 type_t *type, const char *name,
733 const array_dims_t *array, int level,
734 unsigned int *typestring_offset)
736 const var_t *v;
738 /* don't generate a pointer for first-level arrays since we want to
739 * descend into them to write their pointers, not stop here */
740 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
742 return write_pointers(file, NULL, type, name, NULL, level + 1, typestring_offset);
744 else if (is_ptr(type))
746 type_t *ref = type->ref;
748 if (!has_known_tfs(ref))
750 if (is_ptr(ref))
752 write_pointers(file, attrs, ref, name, array, level + 1,
753 typestring_offset);
755 else if (is_struct(ref->type))
757 write_struct_tfs(file, ref, name, typestring_offset);
759 else
761 error("write_pointers: type format string unknown for %s (0x%02x)\n",
762 name, ref->type);
766 /* top-level pointers are handled by write_pointer_description */
767 if (1 < level)
768 write_pointer_tfs(file, type, typestring_offset);
770 return 1;
772 else if (is_struct(type->type))
774 int pointers_written = 0;
775 if (type->fields)
777 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
778 pointers_written += write_pointers(file, v->attrs, v->type,
779 v->name, v->array,
780 level + 1,
781 typestring_offset);
783 return pointers_written;
785 else return 0;
788 static size_t write_pointer_description(FILE *file, const attr_list_t *attrs,
789 type_t *type, size_t mem_offset,
790 const array_dims_t *array, int level,
791 size_t *typestring_offset)
793 const var_t *v;
794 unsigned int align = 0;
796 /* don't generate a pointer for first-level arrays since we want to
797 * descend into them to write their pointers, not stop here */
798 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
800 write_pointer_description(file, NULL, type, mem_offset, NULL,
801 level + 1, typestring_offset);
803 else if (is_ptr(type))
805 print_file(file, 2, "0x46,\t/* FC_NO_REPEAT */\n");
806 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
807 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
808 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
809 *typestring_offset += 6;
811 if (has_known_tfs(type->ref))
812 write_pointer_tfs(file, type, typestring_offset);
813 else
814 error("write_pointer_description: type format string unknown\n");
816 else if (level == 0 && is_struct(type->type))
818 if (type->fields)
820 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
821 mem_offset
822 += write_pointer_description(file, v->attrs, v->type,
823 mem_offset, v->array,
824 level + 1,
825 typestring_offset);
829 return type_memsize(type, array, &align);
832 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
833 const type_t *type, const array_dims_t *array,
834 const char *name, unsigned int *typestring_offset)
836 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
837 int has_size = is_non_void(size_is);
838 size_t start_offset = *typestring_offset;
839 unsigned char flags = 0;
840 int pointer_type;
841 unsigned char rtype;
843 if (is_ptr(type))
845 pointer_type = type->type;
846 type = type->ref;
848 else
849 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
851 if (!pointer_type)
852 pointer_type = RPC_FC_RP;
854 if (!get_attrp(attrs, ATTR_SIZEIS))
855 flags |= RPC_FC_P_SIMPLEPOINTER;
857 rtype = type->type;
859 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
861 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
862 return start_offset;
865 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
866 pointer_type, flags,
867 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
868 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
869 *typestring_offset += 2;
871 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
873 print_file(file, 2, "NdrFcShort(0x2),\n");
874 *typestring_offset += 2;
877 if (array && !is_conformant_array(array))
879 /* FIXME: multi-dimensional array */
880 const expr_t *dim = LIST_ENTRY( list_head( array ), expr_t, entry );
881 if (dim->cval > USHRT_MAX)
882 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
883 name, USHRT_MAX, dim->cval - USHRT_MAX);
885 if (rtype == RPC_FC_CHAR)
886 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
887 else
888 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
889 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
890 *typestring_offset += 2;
892 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim->cval, dim->cval);
893 *typestring_offset += 2;
895 return start_offset;
897 else if (has_size)
899 if (rtype == RPC_FC_CHAR)
900 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
901 else
902 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
903 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
904 *typestring_offset += 2;
906 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
908 return start_offset;
910 else
912 if (rtype == RPC_FC_CHAR)
913 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
914 else
915 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
916 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
917 *typestring_offset += 2;
919 return start_offset;
923 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs,
924 type_t *type, const array_dims_t *array,
925 const char *name, unsigned int *typestring_offset)
927 const expr_list_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
928 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
929 int has_length = is_non_void(length_is);
930 int has_size = is_non_void(size_is) || is_conformant_array(array);
931 size_t start_offset;
932 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
933 if (!pointer_type)
934 pointer_type = RPC_FC_RP;
936 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
937 pointer_type,
938 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
939 print_file(file, 2, "NdrFcShort(0x2),\n");
940 *typestring_offset += 4;
942 if (array && list_count(array) > 1) /* multi-dimensional array */
944 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
945 return 0;
947 else
949 const expr_t *dim = array ? LIST_ENTRY( list_head( array ), expr_t, entry ) : NULL;
950 int has_pointer = 0;
952 if (write_pointers(file, attrs, type, name, array, 0, typestring_offset) > 0)
953 has_pointer = 1;
955 start_offset = *typestring_offset;
957 if (!has_length && !has_size)
959 /* fixed array */
960 unsigned int align = 0;
961 size_t size = type_memsize(type, array, &align);
962 if (size < USHRT_MAX)
964 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
965 /* alignment */
966 print_file(file, 2, "0x%02x,\n", align - 1);
967 /* size */
968 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
969 *typestring_offset += 4;
971 else
973 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
974 /* alignment */
975 print_file(file, 2, "0x%02x,\n", align - 1);
976 /* size */
977 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
978 *typestring_offset += 6;
981 if (has_pointer)
983 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
984 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
985 *typestring_offset += 2;
986 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
987 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
988 *typestring_offset += 1;
991 if (!write_base_type( file, type, typestring_offset ))
993 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
994 *typestring_offset += 1;
996 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
997 *typestring_offset += 1;
999 return start_offset;
1001 else if (has_length && !has_size)
1003 /* varying array */
1004 unsigned int align = 0;
1005 size_t element_size = type_memsize(type, NULL, &align);
1006 size_t elements = dim->cval;
1007 size_t total_size = element_size * elements;
1009 if (total_size < USHRT_MAX)
1011 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
1012 /* alignment */
1013 print_file(file, 2, "0x%02x,\n", align - 1);
1014 /* total size */
1015 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
1016 /* number of elements */
1017 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
1018 *typestring_offset += 6;
1020 else
1022 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
1023 /* alignment */
1024 print_file(file, 2, "0x%02x,\n", align - 1);
1025 /* total size */
1026 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
1027 /* number of elements */
1028 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
1029 *typestring_offset += 10;
1031 /* element size */
1032 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1033 *typestring_offset += 2;
1035 *typestring_offset += write_conf_or_var_desc(file, current_func,
1036 current_structure,
1037 length_is);
1039 if (has_pointer)
1041 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1042 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1043 *typestring_offset += 2;
1044 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1045 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1046 *typestring_offset += 1;
1049 if (!write_base_type( file, type, typestring_offset ))
1051 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1052 *typestring_offset += 1;
1054 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1055 *typestring_offset += 1;
1057 return start_offset;
1059 else if (!has_length && has_size)
1061 /* conformant array */
1062 unsigned int align = 0;
1063 size_t element_size = type_memsize(type, NULL, &align);
1065 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
1066 /* alignment */
1067 print_file(file, 2, "0x%02x,\n", align - 1);
1068 /* element size */
1069 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1070 *typestring_offset += 4;
1072 *typestring_offset += write_conf_or_var_desc(file, current_func,
1073 current_structure,
1074 size_is ? size_is : array);
1076 if (has_pointer)
1078 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1079 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1080 *typestring_offset += 2;
1081 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1082 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1083 *typestring_offset += 1;
1086 if (!write_base_type( file, type, typestring_offset ))
1088 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1089 *typestring_offset += 1;
1091 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1092 *typestring_offset += 1;
1094 return start_offset;
1096 else
1098 /* conformant varying array */
1099 unsigned int align = 0;
1100 size_t element_size = type_memsize(type, NULL, &align);
1102 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
1103 /* alignment */
1104 print_file(file, 2, "0x%02x,\n", align - 1);
1105 /* element size */
1106 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1107 *typestring_offset += 4;
1109 *typestring_offset += write_conf_or_var_desc(file, current_func,
1110 current_structure,
1111 size_is ? size_is : array);
1112 *typestring_offset += write_conf_or_var_desc(file, current_func,
1113 current_structure,
1114 length_is);
1116 if (has_pointer)
1118 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1119 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1120 *typestring_offset += 2;
1121 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1122 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1123 *typestring_offset += 1;
1126 if (!write_base_type( file, type, typestring_offset ))
1128 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1129 *typestring_offset += 1;
1131 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1132 *typestring_offset += 1;
1134 return start_offset;
1139 static const var_t *find_array_or_string_in_struct(const type_t *type)
1141 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1143 if (is_array_type(last_field->attrs, last_field->type, last_field->array))
1144 return last_field;
1146 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1147 (last_field->type->type == RPC_FC_CPSTRUCT) ||
1148 (last_field->type->type == RPC_FC_CVSTRUCT));
1150 return find_array_or_string_in_struct(last_field->type);
1153 static void write_struct_members(FILE *file, const type_t *type, unsigned int *typestring_offset)
1155 const var_t *field;
1157 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1159 unsigned char rtype = field->type->type;
1161 if (field->array)
1162 write_array_tfs( file, field->attrs, field->type, field->array,
1163 field->name, typestring_offset );
1164 else if (is_ptr( field->type ))
1166 /* pointers are handled in detail earlier, here just treat them like longs */
1167 print_file( file, 2, "0x8,\t/* FC_LONG */\n" );
1168 *typestring_offset += 1;
1170 else if (!write_base_type( file, field->type, typestring_offset ))
1171 error("Unsupported member type 0x%x\n", rtype);
1174 if (!(*typestring_offset % 2))
1176 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1177 *typestring_offset += 1;
1180 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1181 *typestring_offset += 1;
1184 static size_t write_struct_tfs(FILE *file, type_t *type,
1185 const char *name, unsigned int *typestring_offset)
1187 unsigned int total_size;
1188 const var_t *array;
1189 size_t start_offset;
1190 size_t array_offset;
1191 int has_pointers;
1192 unsigned int align = 0;
1194 switch (type->type)
1196 case RPC_FC_STRUCT:
1197 case RPC_FC_PSTRUCT:
1198 total_size = type_memsize(type, NULL, &align);
1200 if (total_size > USHRT_MAX)
1201 error("structure size for %s exceeds %d bytes by %d bytes\n",
1202 name, USHRT_MAX, total_size - USHRT_MAX);
1204 if (type->type == RPC_FC_PSTRUCT)
1205 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1207 start_offset = *typestring_offset;
1208 type->typestring_offset = start_offset;
1209 if (type->type == RPC_FC_STRUCT)
1210 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1211 else
1212 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1213 /* alignment */
1214 print_file(file, 2, "0x%02x,\n", align - 1);
1215 /* total size */
1216 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1217 *typestring_offset += 4;
1219 if (type->type == RPC_FC_PSTRUCT)
1221 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1222 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1223 *typestring_offset += 2;
1224 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1225 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1226 *typestring_offset += 1;
1229 /* member layout */
1230 write_struct_members(file, type, typestring_offset);
1231 return start_offset;
1232 case RPC_FC_CSTRUCT:
1233 case RPC_FC_CPSTRUCT:
1234 total_size = type_memsize(type, NULL, &align);
1236 if (total_size > USHRT_MAX)
1237 error("structure size for %s exceeds %d bytes by %d bytes\n",
1238 name, USHRT_MAX, total_size - USHRT_MAX);
1240 array = find_array_or_string_in_struct(type);
1241 current_structure = type;
1242 array_offset = write_array_tfs(file, array->attrs, array->type,
1243 array->array, array->name,
1244 typestring_offset);
1245 current_structure = NULL;
1247 if (type->type == RPC_FC_CPSTRUCT)
1248 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1250 start_offset = *typestring_offset;
1251 type->typestring_offset = start_offset;
1252 if (type->type == RPC_FC_CSTRUCT)
1253 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1254 else
1255 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1256 /* alignment */
1257 print_file(file, 2, "0x%02x,\n", align - 1);
1258 /* total size */
1259 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1260 *typestring_offset += 4;
1261 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1262 array_offset - *typestring_offset,
1263 array_offset - *typestring_offset,
1264 array_offset);
1265 *typestring_offset += 2;
1267 if (type->type == RPC_FC_CPSTRUCT)
1269 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1270 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1271 *typestring_offset += 2;
1272 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1273 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1274 *typestring_offset += 1;
1277 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1278 *typestring_offset += 1;
1280 return start_offset;
1281 case RPC_FC_CVSTRUCT:
1282 total_size = type_memsize(type, NULL, &align);
1284 if (total_size > USHRT_MAX)
1285 error("structure size for %s exceeds %d bytes by %d bytes\n",
1286 name, USHRT_MAX, total_size - USHRT_MAX);
1288 array = find_array_or_string_in_struct(type);
1289 current_structure = type;
1290 if (is_attr(array->attrs, ATTR_STRING))
1291 array_offset = write_string_tfs(file, array->attrs, array->type,
1292 array->array, array->name,
1293 typestring_offset);
1294 else
1295 array_offset = write_array_tfs(file, array->attrs, array->type,
1296 array->array, array->name,
1297 typestring_offset);
1298 current_structure = NULL;
1300 has_pointers = write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1302 start_offset = *typestring_offset;
1303 type->typestring_offset = start_offset;
1304 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1305 /* alignment */
1306 print_file(file, 2, "0x%02x,\n", align - 1);
1307 /* total size */
1308 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1309 *typestring_offset += 4;
1310 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1311 array_offset - *typestring_offset,
1312 array_offset - *typestring_offset,
1313 array_offset);
1314 *typestring_offset += 2;
1316 if (has_pointers)
1318 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1319 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1320 *typestring_offset += 2;
1321 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1322 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1323 *typestring_offset += 1;
1326 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1327 *typestring_offset += 1;
1329 return start_offset;
1330 default:
1331 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1332 return *typestring_offset;
1336 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1337 unsigned char flags, size_t offset,
1338 unsigned int *typeformat_offset)
1340 size_t start_offset = *typeformat_offset;
1341 short reloff = offset - (*typeformat_offset + 2);
1342 int in_attr, out_attr;
1343 in_attr = is_attr(attrs, ATTR_IN);
1344 out_attr = is_attr(attrs, ATTR_OUT);
1345 if (!in_attr && !out_attr) in_attr = 1;
1347 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1348 flags |= 0x04;
1350 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1351 pointer_type,
1352 flags,
1353 string_of_type(pointer_type));
1354 if (file)
1356 if (flags & 0x04)
1357 fprintf(file, " [allocated_on_stack]");
1358 if (flags & 0x10)
1359 fprintf(file, " [pointer_deref]");
1360 fprintf(file, " */\n");
1363 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1364 *typeformat_offset += 4;
1366 return start_offset;
1369 static size_t write_union_tfs(FILE *file, const attr_list_t *attrs,
1370 const type_t *type, const char *name,
1371 unsigned int *typeformat_offset)
1373 error("write_union_tfs: Unimplemented\n");
1374 return *typeformat_offset;
1377 static size_t write_ip_tfs(FILE *file, const func_t *func, const type_t *type, const var_t *var,
1378 unsigned int *typeformat_offset)
1380 size_t i;
1381 size_t start_offset = *typeformat_offset;
1382 const var_t *iid = get_attrp(var->attrs, ATTR_IIDIS);
1384 if (iid)
1386 expr_t expr;
1387 expr_list_t expr_list;
1389 expr.type = EXPR_IDENTIFIER;
1390 expr.ref = NULL;
1391 expr.u.sval = iid->name;
1392 expr.is_const = FALSE;
1393 list_init( &expr_list );
1394 list_add_head( &expr_list, &expr.entry );
1395 print_file(file, 2, "0x2f, /* FC_IP */\n");
1396 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1397 *typeformat_offset += write_conf_or_var_desc(file, func, NULL, &expr_list) + 2;
1399 else
1401 const type_t *base = is_ptr(type) ? type->ref : type;
1402 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1404 if (! uuid)
1405 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1407 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1408 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1409 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1410 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1411 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1412 for (i = 0; i < 8; ++i)
1413 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1415 if (file)
1416 fprintf(file, "\n");
1418 *typeformat_offset += 18;
1420 return start_offset;
1423 static int get_ptr_attr(const type_t *t, int def_type)
1425 while (TRUE)
1427 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1428 if (ptr_attr)
1429 return ptr_attr;
1430 if (t->kind != TKIND_ALIAS)
1431 return def_type;
1432 t = t->orig;
1436 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1437 type_t *type, const var_t *var,
1438 unsigned int *typeformat_offset)
1440 int pointer_type;
1441 size_t offset;
1443 if (type == var->type) /* top-level pointers */
1445 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1446 if (pointer_attr != 0 && !is_ptr(type))
1447 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1449 if (pointer_attr == 0)
1450 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1452 pointer_type = pointer_attr;
1454 else
1455 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1457 if (((last_ptr(type) && var->array == NULL)
1458 || (!is_ptr(type) && var->array != NULL))
1459 && is_ptrchain_attr(var, ATTR_STRING))
1461 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1464 if (is_array_type(var->attrs, type, var->array))
1465 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1467 if (!is_ptr(type))
1469 /* basic types don't need a type format string */
1470 if (is_base_type(type->type))
1471 return 0;
1473 switch (type->type)
1475 case RPC_FC_STRUCT:
1476 case RPC_FC_PSTRUCT:
1477 case RPC_FC_CSTRUCT:
1478 case RPC_FC_CPSTRUCT:
1479 case RPC_FC_CVSTRUCT:
1480 case RPC_FC_BOGUS_STRUCT:
1481 return write_struct_tfs(file, type, var->name, typeformat_offset);
1482 case RPC_FC_ENCAPSULATED_UNION:
1483 case RPC_FC_NON_ENCAPSULATED_UNION:
1484 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1485 case RPC_FC_IGNORE:
1486 case RPC_FC_BIND_PRIMITIVE:
1487 /* nothing to do */
1488 return 0;
1489 default:
1490 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1493 else if (last_ptr(type))
1495 size_t start_offset = *typeformat_offset;
1496 int in_attr = is_attr(var->attrs, ATTR_IN);
1497 int out_attr = is_attr(var->attrs, ATTR_OUT);
1498 const type_t *base = type->ref;
1500 if (base->type == RPC_FC_IP)
1502 return write_ip_tfs(file, func, type, var, typeformat_offset);
1505 /* special case for pointers to base types */
1506 if (is_base_type(base->type))
1508 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1509 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1510 string_of_type(pointer_type),
1511 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1512 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
1513 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1514 *typeformat_offset += 4;
1515 return start_offset;
1519 assert(is_ptr(type));
1521 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1522 if (file)
1523 fprintf(file, "/* %2u */\n", *typeformat_offset);
1524 return write_pointer_only_tfs(file, var->attrs, pointer_type,
1525 !last_ptr(type) ? 0x10 : 0,
1526 offset, typeformat_offset);
1529 static void clear_tfsoff(type_t *type)
1531 for (;;)
1533 type->typestring_offset = 0;
1535 if (type->kind == TKIND_ALIAS)
1536 type = type->orig;
1537 else if (is_ptr(type))
1538 type = type->ref;
1539 else
1541 if (type->fields)
1543 var_t *v;
1544 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1545 clear_tfsoff(v->type);
1548 return;
1553 static void clear_all_tfsoffs(const ifref_list_t *ifaces)
1555 const ifref_t * iface;
1556 const func_t *func;
1557 const var_t *var;
1559 if (ifaces)
1560 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1561 if (iface->iface->funcs)
1562 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1563 if (func->args)
1564 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1565 clear_tfsoff(var->type);
1568 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
1570 const var_t *var;
1571 const ifref_t *iface;
1572 size_t typeformat_offset = 2;
1574 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1576 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1577 continue;
1579 if (iface->iface->funcs)
1581 const func_t *func;
1582 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1584 if (is_local(func->def->attrs)) continue;
1586 current_func = func;
1587 if (func->args)
1588 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1589 var->type->typestring_offset
1590 = write_typeformatstring_var(file, 2, func, var->type,
1591 var, &typeformat_offset);
1596 return typeformat_offset + 1;
1600 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1602 int indent = 0;
1604 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1605 print_file(file, indent, "{\n");
1606 indent++;
1607 print_file(file, indent, "0,\n");
1608 print_file(file, indent, "{\n");
1609 indent++;
1610 print_file(file, indent, "NdrFcShort(0x0),\n");
1612 clear_all_tfsoffs(ifaces);
1613 process_tfs(file, ifaces, for_objects);
1615 print_file(file, indent, "0x0\n");
1616 indent--;
1617 print_file(file, indent, "}\n");
1618 indent--;
1619 print_file(file, indent, "};\n");
1620 print_file(file, indent, "\n");
1623 static unsigned int get_required_buffer_size_type(
1624 const type_t *type, const array_dims_t *array,
1625 const char *name, unsigned int *alignment)
1627 size_t size = 0;
1629 *alignment = 0;
1630 if (!is_ptr(type))
1632 switch (type->type)
1634 case RPC_FC_BYTE:
1635 case RPC_FC_CHAR:
1636 case RPC_FC_USMALL:
1637 case RPC_FC_SMALL:
1638 *alignment = 4;
1639 size = 1;
1640 break;
1642 case RPC_FC_WCHAR:
1643 case RPC_FC_USHORT:
1644 case RPC_FC_SHORT:
1645 *alignment = 4;
1646 size = 2;
1647 break;
1649 case RPC_FC_ULONG:
1650 case RPC_FC_LONG:
1651 case RPC_FC_FLOAT:
1652 case RPC_FC_ERROR_STATUS_T:
1653 *alignment = 4;
1654 size = 4;
1655 break;
1657 case RPC_FC_HYPER:
1658 case RPC_FC_DOUBLE:
1659 *alignment = 8;
1660 size = 8;
1661 break;
1663 case RPC_FC_IGNORE:
1664 case RPC_FC_BIND_PRIMITIVE:
1665 return 0;
1667 case RPC_FC_STRUCT:
1668 case RPC_FC_PSTRUCT:
1670 const var_t *field;
1671 if (!type->fields) return 0;
1672 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1674 unsigned int alignment;
1675 size += get_required_buffer_size_type(
1676 field->type, field->array, field->name,
1677 &alignment);
1679 break;
1682 case RPC_FC_RP:
1683 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1684 size = get_required_buffer_size_type( type->ref, NULL, name, alignment );
1685 break;
1687 default:
1688 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1689 return 0;
1691 if (array) size *= get_array_size( array );
1693 return size;
1696 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1698 expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1699 int has_size = is_non_void(size_is);
1700 int in_attr = is_attr(var->attrs, ATTR_IN);
1701 int out_attr = is_attr(var->attrs, ATTR_OUT);
1703 if (!in_attr && !out_attr)
1704 in_attr = 1;
1706 *alignment = 0;
1708 if (pass == PASS_OUT)
1710 if (out_attr && is_ptr(var->type))
1712 type_t *type = var->type;
1714 if (type->type == RPC_FC_STRUCT)
1716 const var_t *field;
1717 unsigned int size = 36;
1719 if (!type->fields) return size;
1720 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1722 unsigned int align;
1723 size += get_required_buffer_size_type(
1724 field->type, field->array, field->name,
1725 &align);
1727 return size;
1730 return 0;
1732 else
1734 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1736 if (is_ptr(var->type))
1738 type_t *type = var->type;
1740 if (is_base_type(type->type))
1742 return 25;
1744 else if (type->type == RPC_FC_STRUCT)
1746 unsigned int size = 36;
1747 const var_t *field;
1749 if (!type->fields) return size;
1750 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1752 unsigned int align;
1753 size += get_required_buffer_size_type(
1754 field->type, field->array, field->name,
1755 &align);
1757 return size;
1762 return get_required_buffer_size_type(var->type, var->array, var->name, alignment);
1766 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
1768 const var_t *var;
1769 unsigned int total_size = 0, alignment;
1771 if (func->args)
1773 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1775 total_size += get_required_buffer_size(var, &alignment, pass);
1776 total_size += alignment;
1780 if (pass == PASS_OUT && !is_void(func->def->type))
1782 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
1783 total_size += alignment;
1785 return total_size;
1788 static void print_phase_function(FILE *file, int indent, const char *type,
1789 enum remoting_phase phase,
1790 const char *varname, unsigned int type_offset)
1792 const char *function;
1793 switch (phase)
1795 case PHASE_BUFFERSIZE:
1796 function = "BufferSize";
1797 break;
1798 case PHASE_MARSHAL:
1799 function = "Marshall";
1800 break;
1801 case PHASE_UNMARSHAL:
1802 function = "Unmarshall";
1803 break;
1804 case PHASE_FREE:
1805 function = "Free";
1806 break;
1807 default:
1808 assert(0);
1809 return;
1812 print_file(file, indent, "Ndr%s%s(\n", type, function);
1813 indent++;
1814 print_file(file, indent, "&_StubMsg,\n");
1815 print_file(file, indent, "%s%s,\n",
1816 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1817 varname);
1818 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1819 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1820 if (phase == PHASE_UNMARSHAL)
1821 print_file(file, indent, "0);\n");
1822 indent--;
1825 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1826 enum pass pass, const var_t *var,
1827 const char *varname)
1829 type_t *type = var->type;
1830 unsigned int size;
1831 unsigned int alignment = 0;
1832 unsigned char rtype;
1834 /* no work to do for other phases, buffer sizing is done elsewhere */
1835 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1836 return;
1838 rtype = is_ptr(type) ? type->ref->type : type->type;
1840 switch (rtype)
1842 case RPC_FC_BYTE:
1843 case RPC_FC_CHAR:
1844 case RPC_FC_SMALL:
1845 case RPC_FC_USMALL:
1846 size = 1;
1847 alignment = 1;
1848 break;
1850 case RPC_FC_WCHAR:
1851 case RPC_FC_USHORT:
1852 case RPC_FC_SHORT:
1853 size = 2;
1854 alignment = 2;
1855 break;
1857 case RPC_FC_ULONG:
1858 case RPC_FC_LONG:
1859 case RPC_FC_FLOAT:
1860 case RPC_FC_ERROR_STATUS_T:
1861 size = 4;
1862 alignment = 4;
1863 break;
1865 case RPC_FC_HYPER:
1866 case RPC_FC_DOUBLE:
1867 size = 8;
1868 alignment = 8;
1869 break;
1871 case RPC_FC_IGNORE:
1872 case RPC_FC_BIND_PRIMITIVE:
1873 /* no marshalling needed */
1874 return;
1876 default:
1877 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1878 size = 0;
1881 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1882 alignment - 1, alignment - 1);
1884 if (phase == PHASE_MARSHAL)
1886 print_file(file, indent, "*(");
1887 write_type(file, is_ptr(type) ? type->ref : type);
1888 if (is_ptr(type))
1889 fprintf(file, " *)_StubMsg.Buffer = *");
1890 else
1891 fprintf(file, " *)_StubMsg.Buffer = ");
1892 fprintf(file, varname);
1893 fprintf(file, ";\n");
1895 else if (phase == PHASE_UNMARSHAL)
1897 if (pass == PASS_IN || pass == PASS_RETURN)
1898 print_file(file, indent, "");
1899 else
1900 print_file(file, indent, "*");
1901 fprintf(file, varname);
1902 if (pass == PASS_IN && is_ptr(type))
1903 fprintf(file, " = (");
1904 else
1905 fprintf(file, " = *(");
1906 write_type(file, is_ptr(type) ? type->ref : type);
1907 fprintf(file, " *)_StubMsg.Buffer;\n");
1910 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1911 write_type(file, var->type);
1912 fprintf(file, ");\n");
1915 /* returns whether the MaxCount, Offset or ActualCount members need to be
1916 * filled in for the specified phase */
1917 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1919 return (phase != PHASE_UNMARSHAL);
1922 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1923 enum pass pass, enum remoting_phase phase)
1925 const expr_list_t *length_is;
1926 const expr_list_t *size_is;
1927 int in_attr, out_attr, has_length, has_size, pointer_type;
1928 const var_t *var;
1930 if (!func->args)
1931 return;
1933 if (phase == PHASE_BUFFERSIZE)
1935 unsigned int size = get_function_buffer_size( func, pass );
1936 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
1939 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1941 const type_t *type = var->type;
1942 unsigned char rtype;
1943 size_t start_offset = type->typestring_offset;
1945 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1946 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1947 has_length = is_non_void(length_is);
1948 has_size = is_non_void(size_is) || (var->array && is_conformant_array(var->array));
1950 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1951 if (!pointer_type)
1952 pointer_type = RPC_FC_RP;
1954 in_attr = is_attr(var->attrs, ATTR_IN);
1955 out_attr = is_attr(var->attrs, ATTR_OUT);
1956 if (!in_attr && !out_attr)
1957 in_attr = 1;
1959 switch (pass)
1961 case PASS_IN:
1962 if (!in_attr) continue;
1963 break;
1964 case PASS_OUT:
1965 if (!out_attr) continue;
1966 break;
1967 case PASS_RETURN:
1968 break;
1971 rtype = type->type;
1973 if (is_user_derived( var ))
1975 print_phase_function(file, indent, "UserMarshal", phase, var->name, start_offset);
1977 else if (is_string_type(var->attrs, var->type, var->array))
1979 if (var->array && !is_conformant_array(var->array))
1980 print_phase_function(file, indent, "NonConformantString", phase, var->name, start_offset);
1981 else
1983 if (size_is && is_size_needed_for_phase(phase))
1985 const expr_t *size = LIST_ENTRY( list_head(size_is), const expr_t, entry );
1986 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1987 write_expr(file, size, 1);
1988 fprintf(file, ";\n");
1991 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1992 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
1993 else
1994 print_phase_function(file, indent, "ConformantString", phase, var->name,
1995 start_offset + (has_size ? 4 : 2));
1998 else if (is_array_type(var->attrs, var->type, var->array))
2000 const char *array_type;
2002 if (var->array && list_count(var->array) > 1) /* multi-dimensional array */
2003 array_type = "ComplexArray";
2004 else
2006 if (!has_length && !has_size)
2007 array_type = "FixedArray";
2008 else if (has_length && !has_size)
2010 if (is_size_needed_for_phase(phase))
2012 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2013 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2014 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2015 write_expr(file, length, 1);
2016 fprintf(file, ";\n\n");
2018 array_type = "VaryingArray";
2020 else if (!has_length && has_size)
2022 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2024 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2025 const expr_t, entry );
2026 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2027 write_expr(file, size, 1);
2028 fprintf(file, ";\n\n");
2030 array_type = "ConformantArray";
2032 else
2034 if (is_size_needed_for_phase(phase))
2036 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2037 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2038 const expr_t, entry );
2039 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2040 write_expr(file, size, 1);
2041 fprintf(file, ";\n");
2042 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2043 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2044 write_expr(file, length, 1);
2045 fprintf(file, ";\n\n");
2047 array_type = "ConformantVaryingArray";
2051 if (!in_attr && phase == PHASE_FREE)
2053 print_file(file, indent, "if (%s)\n", var->name);
2054 indent++;
2055 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2057 else if (phase != PHASE_FREE)
2059 if (pointer_type == RPC_FC_UP)
2060 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2061 else
2062 print_phase_function(file, indent, array_type, phase, var->name, start_offset);
2065 else if (!is_ptr(var->type) && is_base_type(rtype))
2067 print_phase_basetype(file, indent, phase, pass, var, var->name);
2069 else if (!is_ptr(var->type))
2071 switch (rtype)
2073 case RPC_FC_STRUCT:
2074 case RPC_FC_PSTRUCT:
2075 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset);
2076 break;
2077 case RPC_FC_CSTRUCT:
2078 case RPC_FC_CPSTRUCT:
2079 print_phase_function(file, indent, "ConformantStruct", phase, var->name, start_offset);
2080 break;
2081 case RPC_FC_CVSTRUCT:
2082 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var->name, start_offset);
2083 break;
2084 case RPC_FC_BOGUS_STRUCT:
2085 print_phase_function(file, indent, "ComplexStruct", phase, var->name, start_offset);
2086 break;
2087 case RPC_FC_RP:
2088 if (is_base_type( var->type->ref->type ))
2090 print_phase_basetype(file, indent, phase, pass, var, var->name);
2092 else if (var->type->ref->type == RPC_FC_STRUCT)
2094 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2095 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2097 else
2099 const var_t *iid;
2100 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2101 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2102 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2104 break;
2105 default:
2106 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2109 else
2111 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2113 print_phase_basetype(file, indent, phase, pass, var, var->name);
2115 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2117 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2118 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2120 else
2122 const var_t *iid;
2123 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2124 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2125 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2128 fprintf(file, "\n");
2133 size_t get_size_procformatstring_var(const var_t *var)
2135 return write_procformatstring_var(NULL, 0, var, FALSE);
2139 size_t get_size_procformatstring_func(const func_t *func)
2141 const var_t *var;
2142 size_t size = 0;
2144 /* argument list size */
2145 if (func->args)
2146 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2147 size += get_size_procformatstring_var(var);
2149 /* return value size */
2150 if (is_void(func->def->type))
2151 size += 2; /* FC_END and FC_PAD */
2152 else
2153 size += get_size_procformatstring_var(func->def);
2155 return size;
2158 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2160 const ifref_t *iface;
2161 size_t size = 1;
2162 const func_t *func;
2164 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2166 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2167 continue;
2169 if (iface->iface->funcs)
2170 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2171 if (!is_local(func->def->attrs))
2172 size += get_size_procformatstring_func( func );
2174 return size;
2177 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2179 return process_tfs(NULL, ifaces, for_objects);
2182 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2183 const var_list_t *fields, const char *structvar)
2185 switch (e->type) {
2186 case EXPR_VOID:
2187 break;
2188 case EXPR_NUM:
2189 fprintf(h, "%lu", e->u.lval);
2190 break;
2191 case EXPR_HEXNUM:
2192 fprintf(h, "0x%lx", e->u.lval);
2193 break;
2194 case EXPR_TRUEFALSE:
2195 if (e->u.lval == 0)
2196 fprintf(h, "FALSE");
2197 else
2198 fprintf(h, "TRUE");
2199 break;
2200 case EXPR_IDENTIFIER:
2202 const var_t *field;
2203 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2204 if (!strcmp(e->u.sval, field->name))
2206 fprintf(h, "%s->%s", structvar, e->u.sval);
2207 break;
2210 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2211 break;
2213 case EXPR_NEG:
2214 fprintf(h, "-");
2215 write_struct_expr(h, e->ref, 1, fields, structvar);
2216 break;
2217 case EXPR_NOT:
2218 fprintf(h, "~");
2219 write_struct_expr(h, e->ref, 1, fields, structvar);
2220 break;
2221 case EXPR_PPTR:
2222 fprintf(h, "*");
2223 write_struct_expr(h, e->ref, 1, fields, structvar);
2224 break;
2225 case EXPR_CAST:
2226 fprintf(h, "(");
2227 write_type(h, e->u.tref);
2228 fprintf(h, ")");
2229 write_struct_expr(h, e->ref, 1, fields, structvar);
2230 break;
2231 case EXPR_SIZEOF:
2232 fprintf(h, "sizeof(");
2233 write_type(h, e->u.tref);
2234 fprintf(h, ")");
2235 break;
2236 case EXPR_SHL:
2237 case EXPR_SHR:
2238 case EXPR_MUL:
2239 case EXPR_DIV:
2240 case EXPR_ADD:
2241 case EXPR_SUB:
2242 case EXPR_AND:
2243 case EXPR_OR:
2244 if (brackets) fprintf(h, "(");
2245 write_struct_expr(h, e->ref, 1, fields, structvar);
2246 switch (e->type) {
2247 case EXPR_SHL: fprintf(h, " << "); break;
2248 case EXPR_SHR: fprintf(h, " >> "); break;
2249 case EXPR_MUL: fprintf(h, " * "); break;
2250 case EXPR_DIV: fprintf(h, " / "); break;
2251 case EXPR_ADD: fprintf(h, " + "); break;
2252 case EXPR_SUB: fprintf(h, " - "); break;
2253 case EXPR_AND: fprintf(h, " & "); break;
2254 case EXPR_OR: fprintf(h, " | "); break;
2255 default: break;
2257 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2258 if (brackets) fprintf(h, ")");
2259 break;
2260 case EXPR_COND:
2261 if (brackets) fprintf(h, "(");
2262 write_struct_expr(h, e->ref, 1, fields, structvar);
2263 fprintf(h, " ? ");
2264 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2265 fprintf(h, " : ");
2266 write_struct_expr(h, e->ext2, 1, fields, structvar);
2267 if (brackets) fprintf(h, ")");
2268 break;
2273 void declare_stub_args( FILE *file, int indent, const func_t *func )
2275 int in_attr, out_attr;
2276 int i = 0;
2277 const var_t *def = func->def;
2278 const var_t *var;
2280 /* declare return value '_RetVal' */
2281 if (!is_void(def->type))
2283 print_file(file, indent, "");
2284 write_type(file, def->type);
2285 fprintf(file, " _RetVal;\n");
2288 if (!func->args)
2289 return;
2291 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2293 const expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2294 int has_size = is_non_void(size_is);
2295 int is_string = is_attr(var->attrs, ATTR_STRING);
2297 in_attr = is_attr(var->attrs, ATTR_IN);
2298 out_attr = is_attr(var->attrs, ATTR_OUT);
2299 if (!out_attr && !in_attr)
2300 in_attr = 1;
2302 if (!in_attr && !has_size && !is_string)
2304 print_file(file, indent, "");
2305 write_type(file, var->type->ref);
2306 fprintf(file, " _W%u;\n", i++);
2309 print_file(file, indent, "");
2310 write_type(file, var->type);
2311 fprintf(file, " ");
2312 if (var->array) {
2313 fprintf(file, "( *");
2314 write_name(file, var);
2315 fprintf(file, " )");
2316 } else
2317 write_name(file, var);
2318 write_array(file, var->array, 0);
2319 fprintf(file, ";\n");
2324 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2326 int in_attr, out_attr;
2327 int i = 0, sep = 0;
2328 const var_t *var;
2329 const expr_list_t *size_is;
2330 int has_size;
2332 if (!func->args)
2333 return;
2335 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2337 int is_string = is_attr(var->attrs, ATTR_STRING);
2338 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2339 has_size = is_non_void(size_is);
2340 in_attr = is_attr(var->attrs, ATTR_IN);
2341 out_attr = is_attr(var->attrs, ATTR_OUT);
2342 if (!out_attr && !in_attr)
2343 in_attr = 1;
2345 if (!in_attr)
2347 print_file(file, indent, "");
2348 write_name(file, var);
2350 if (has_size)
2352 const expr_t *expr;
2353 unsigned int size, align = 0;
2354 type_t *type = var->type;
2356 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2357 LIST_FOR_EACH_ENTRY( expr, size_is, const expr_t, entry )
2359 if (expr->type == EXPR_VOID) continue;
2360 write_expr( file, expr, 1 );
2361 fprintf(file, " * ");
2363 size = type_memsize(type, NULL, &align);
2364 fprintf(file, "%u);\n", size);
2366 else if (!is_string)
2368 fprintf(file, " = &_W%u;\n", i);
2369 if (is_ptr(var->type) && !last_ptr(var->type))
2370 print_file(file, indent, "_W%u = 0;\n", i);
2371 i++;
2374 sep = 1;
2377 if (sep)
2378 fprintf(file, "\n");
2382 int write_expr_eval_routines(FILE *file, const char *iface)
2384 int result = 0;
2385 struct expr_eval_routine *eval;
2386 unsigned short callback_offset = 0;
2388 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2390 int indent = 0;
2391 result = 1;
2392 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2393 iface, eval->structure->name, callback_offset);
2394 print_file(file, indent, "{\n");
2395 indent++;
2396 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2397 eval->structure->name, eval->structure->name, eval->structure_size);
2398 fprintf(file, "\n");
2399 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2400 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2401 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2402 fprintf(file, ";\n");
2403 indent--;
2404 print_file(file, indent, "}\n\n");
2405 callback_offset++;
2407 return result;
2410 void write_expr_eval_routine_list(FILE *file, const char *iface)
2412 struct expr_eval_routine *eval;
2413 struct expr_eval_routine *cursor;
2414 unsigned short callback_offset = 0;
2416 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2417 fprintf(file, "{\n");
2419 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2421 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2422 iface, eval->structure->name, callback_offset);
2424 callback_offset++;
2425 list_remove(&eval->entry);
2426 free(eval);
2429 fprintf(file, "};\n\n");
2433 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2435 const struct str_list_entry_t *endpoint;
2436 const char *p;
2438 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2439 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2440 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2442 print_file( f, 1, "{ (const unsigned char *)\"" );
2443 for (p = endpoint->str; *p && *p != ':'; p++)
2445 if (*p == '"' || *p == '\\') fputc( '\\', f );
2446 fputc( *p, f );
2448 if (!*p) goto error;
2449 if (p[1] != '[') goto error;
2451 fprintf( f, "\", (const unsigned char *)\"" );
2452 for (p += 2; *p && *p != ']'; p++)
2454 if (*p == '"' || *p == '\\') fputc( '\\', f );
2455 fputc( *p, f );
2457 if (*p != ']') goto error;
2458 fprintf( f, "\" },\n" );
2460 print_file( f, 0, "};\n\n" );
2461 return;
2463 error:
2464 error("Invalid endpoint syntax '%s'\n", endpoint->str);