widl: Fix compilation warnings in 64-bit mode.
[wine/multimedia.git] / tools / widl / typegen.c
blob49e95c83ab81e2697a5b49f49f354c40bcbbb9c3
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, unsigned int *tfsoff);
65 static void write_embedded_types(FILE *file, const type_t *type, size_t *tfsoff);
67 const char *string_of_type(unsigned char type)
69 switch (type)
71 case RPC_FC_BYTE: return "FC_BYTE";
72 case RPC_FC_CHAR: return "FC_CHAR";
73 case RPC_FC_SMALL: return "FC_SMALL";
74 case RPC_FC_USMALL: return "FC_USMALL";
75 case RPC_FC_WCHAR: return "FC_WCHAR";
76 case RPC_FC_SHORT: return "FC_SHORT";
77 case RPC_FC_USHORT: return "FC_USHORT";
78 case RPC_FC_LONG: return "FC_LONG";
79 case RPC_FC_ULONG: return "FC_ULONG";
80 case RPC_FC_FLOAT: return "FC_FLOAT";
81 case RPC_FC_HYPER: return "FC_HYPER";
82 case RPC_FC_DOUBLE: return "FC_DOUBLE";
83 case RPC_FC_ENUM16: return "FC_ENUM16";
84 case RPC_FC_ENUM32: return "FC_ENUM32";
85 case RPC_FC_IGNORE: return "FC_IGNORE";
86 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
87 case RPC_FC_RP: return "FC_RP";
88 case RPC_FC_UP: return "FC_UP";
89 case RPC_FC_OP: return "FC_OP";
90 case RPC_FC_FP: return "FC_FP";
91 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
92 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
93 case RPC_FC_STRUCT: return "FC_STRUCT";
94 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
95 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
96 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
97 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
98 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
99 default:
100 error("string_of_type: unknown type 0x%02x\n", type);
101 return NULL;
105 static int is_struct(unsigned char type)
107 switch (type)
109 case RPC_FC_STRUCT:
110 case RPC_FC_PSTRUCT:
111 case RPC_FC_CSTRUCT:
112 case RPC_FC_CPSTRUCT:
113 case RPC_FC_CVSTRUCT:
114 case RPC_FC_BOGUS_STRUCT:
115 return 1;
116 default:
117 return 0;
121 static int is_union(unsigned char type)
123 switch (type)
125 case RPC_FC_ENCAPSULATED_UNION:
126 case RPC_FC_NON_ENCAPSULATED_UNION:
127 return 1;
128 default:
129 return 0;
133 static int is_embedded_complex(const type_t *type)
135 return is_struct(type->type) || is_union(type->type);
138 static long field_offset(const type_t *strct, const char *name, var_t **pfield)
140 long offset = 0;
141 var_list_t *fields = strct->fields;
142 var_t *f;
144 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
146 unsigned int align = 0;
148 if (f->name != NULL && strcmp(name, f->name) == 0)
150 if (pfield) *pfield = f;
151 return offset;
153 else
155 /* FIXME: handle possible padding */
156 offset += type_memsize(f->type, f->array, &align);
160 if (pfield) *pfield = NULL;
161 return -1;
164 static int compare_expr(const expr_t *a, const expr_t *b)
166 int ret;
168 if (a->type != b->type)
169 return a->type - b->type;
171 switch (a->type)
173 case EXPR_NUM:
174 case EXPR_HEXNUM:
175 case EXPR_TRUEFALSE:
176 return a->u.lval - b->u.lval;
177 case EXPR_IDENTIFIER:
178 return strcmp(a->u.sval, b->u.sval);
179 case EXPR_COND:
180 ret = compare_expr(a->ref, b->ref);
181 if (ret != 0)
182 return ret;
183 ret = compare_expr(a->u.ext, b->u.ext);
184 if (ret != 0)
185 return ret;
186 return compare_expr(a->ext2, b->ext2);
187 case EXPR_OR:
188 case EXPR_AND:
189 case EXPR_ADD:
190 case EXPR_SUB:
191 case EXPR_MUL:
192 case EXPR_DIV:
193 case EXPR_SHL:
194 case EXPR_SHR:
195 ret = compare_expr(a->ref, b->ref);
196 if (ret != 0)
197 return ret;
198 return compare_expr(a->u.ext, b->u.ext);
199 case EXPR_NOT:
200 case EXPR_NEG:
201 case EXPR_PPTR:
202 case EXPR_CAST:
203 case EXPR_SIZEOF:
204 return compare_expr(a->ref, b->ref);
205 case EXPR_VOID:
206 return 0;
208 return -1;
211 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
212 do { \
213 if (file) \
214 fprintf(file, "/* %2u */\n", typestring_offset); \
215 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
217 while (0)
219 static int print_file(FILE *file, int indent, const char *format, ...)
221 va_list va;
222 int i, r;
224 if (!file) return 0;
226 va_start(va, format);
227 for (i = 0; i < indent; i++)
228 fprintf(file, " ");
229 r = vfprintf(file, format, va);
230 va_end(va);
231 return r;
234 static void write_formatdesc(FILE *f, int indent, const char *str)
236 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
237 print_file(f, indent, "{\n");
238 print_file(f, indent + 1, "short Pad;\n");
239 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
240 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
241 print_file(f, indent, "\n");
244 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
246 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
247 get_size_typeformatstring(ifaces, for_objects));
249 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
250 get_size_procformatstring(ifaces, for_objects));
252 fprintf(f, "\n");
253 write_formatdesc(f, indent, "TYPE");
254 write_formatdesc(f, indent, "PROC");
255 fprintf(f, "\n");
256 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
257 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
258 print_file(f, indent, "\n");
261 static int is_user_derived(const var_t *v)
263 const type_t *type = v->type;
265 if (v->attrs && is_attr( v->attrs, ATTR_WIREMARSHAL )) return 1;
267 while (type)
269 if (type->attrs && is_attr( type->attrs, ATTR_WIREMARSHAL )) return 1;
270 type = type->ref;
272 return 0;
275 static inline int is_base_type(unsigned char type)
277 switch (type)
279 case RPC_FC_BYTE:
280 case RPC_FC_CHAR:
281 case RPC_FC_USMALL:
282 case RPC_FC_SMALL:
283 case RPC_FC_WCHAR:
284 case RPC_FC_USHORT:
285 case RPC_FC_SHORT:
286 case RPC_FC_ULONG:
287 case RPC_FC_LONG:
288 case RPC_FC_HYPER:
289 case RPC_FC_IGNORE:
290 case RPC_FC_FLOAT:
291 case RPC_FC_DOUBLE:
292 case RPC_FC_ENUM16:
293 case RPC_FC_ENUM32:
294 case RPC_FC_ERROR_STATUS_T:
295 case RPC_FC_BIND_PRIMITIVE:
296 return TRUE;
298 default:
299 return FALSE;
303 static size_t write_procformatstring_var(FILE *file, int indent,
304 const var_t *var, int is_return)
306 size_t size;
307 const type_t *type = var->type;
309 int is_in = is_attr(var->attrs, ATTR_IN);
310 int is_out = is_attr(var->attrs, ATTR_OUT);
312 if (!is_in && !is_out) is_in = TRUE;
314 if (!var->array && is_base_type(type->type))
316 if (is_return)
317 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
318 else
319 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
321 if (is_base_type(type->type))
323 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
324 size = 2; /* includes param type prefix */
326 else if (type->type == RPC_FC_BIND_PRIMITIVE)
328 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
329 size = 2; /* includes param type prefix */
331 else
333 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
334 size = 0;
337 else
339 if (is_return)
340 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
341 else if (is_in && is_out)
342 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
343 else if (is_out)
344 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
345 else
346 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
348 print_file(file, indent, "0x01,\n");
349 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
350 size = 4; /* includes param type prefix */
352 return size;
355 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
357 const ifref_t *iface;
358 int indent = 0;
359 const var_t *var;
361 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
362 print_file(file, indent, "{\n");
363 indent++;
364 print_file(file, indent, "0,\n");
365 print_file(file, indent, "{\n");
366 indent++;
368 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
370 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
371 continue;
373 if (iface->iface->funcs)
375 const func_t *func;
376 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
378 if (is_local(func->def->attrs)) continue;
379 /* emit argument data */
380 if (func->args)
382 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
383 write_procformatstring_var(file, indent, var, FALSE);
386 /* emit return value data */
387 var = func->def;
388 if (is_void(var->type))
390 print_file(file, indent, "0x5b, /* FC_END */\n");
391 print_file(file, indent, "0x5c, /* FC_PAD */\n");
393 else
394 write_procformatstring_var(file, indent, var, TRUE);
399 print_file(file, indent, "0x0\n");
400 indent--;
401 print_file(file, indent, "}\n");
402 indent--;
403 print_file(file, indent, "};\n");
404 print_file(file, indent, "\n");
407 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
409 if (is_base_type(type->type))
411 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
412 *typestring_offset += 1;
413 return 1;
416 return 0;
419 /* write conformance / variance descriptor */
420 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_list_t *expr_list)
422 unsigned char operator_type = 0;
423 const char *operator_string = "no operators";
424 const expr_t *expr, *subexpr;
425 unsigned char correlation_type;
427 if (!file) return 4; /* optimisation for sizing pass */
429 if (list_count(expr_list) > 1)
430 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
432 expr = subexpr = LIST_ENTRY( list_head(expr_list), const expr_t, entry );
434 if (expr->is_const)
436 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
437 error("write_conf_or_var_desc: constant value %ld is greater than "
438 "the maximum constant size of %d\n", expr->cval,
439 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
441 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
442 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
443 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
444 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
446 return 4;
449 switch (subexpr->type)
451 case EXPR_PPTR:
452 subexpr = subexpr->ref;
453 operator_type = RPC_FC_DEREFERENCE;
454 operator_string = "FC_DEREFERENCE";
455 break;
456 case EXPR_DIV:
457 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
459 subexpr = subexpr->ref;
460 operator_type = RPC_FC_DIV_2;
461 operator_string = "FC_DIV_2";
463 break;
464 case EXPR_MUL:
465 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
467 subexpr = subexpr->ref;
468 operator_type = RPC_FC_MULT_2;
469 operator_string = "FC_MULT_2";
471 break;
472 case EXPR_SUB:
473 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
475 subexpr = subexpr->ref;
476 operator_type = RPC_FC_SUB_1;
477 operator_string = "FC_SUB_1";
479 break;
480 case EXPR_ADD:
481 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
483 subexpr = subexpr->ref;
484 operator_type = RPC_FC_ADD_1;
485 operator_string = "FC_ADD_1";
487 break;
488 default:
489 break;
492 if (subexpr->type == EXPR_IDENTIFIER)
494 const type_t *correlation_variable = NULL;
495 unsigned char correlation_variable_type;
496 unsigned char param_type = 0;
497 const char *param_type_string = NULL;
498 size_t offset;
500 if (structure)
502 const var_t *var;
504 offset = 0;
505 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
507 unsigned int align = 0;
508 offset -= type_memsize(var->type, var->array, &align);
509 /* FIXME: take alignment into account */
510 if (!strcmp(var->name, subexpr->u.sval))
512 correlation_variable = var->type;
513 break;
516 if (!correlation_variable)
517 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
518 subexpr->u.sval);
520 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
522 else
524 const var_t *var;
526 offset = sizeof(void *);
527 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
529 if (!strcmp(var->name, subexpr->u.sval))
531 correlation_variable = var->type;
532 break;
534 /* FIXME: not all stack variables are sizeof(void *) */
535 offset += sizeof(void *);
537 if (!correlation_variable)
538 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
539 subexpr->u.sval);
541 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
544 correlation_variable_type = correlation_variable->type;
546 switch (correlation_variable_type)
548 case RPC_FC_CHAR:
549 case RPC_FC_SMALL:
550 param_type = RPC_FC_SMALL;
551 param_type_string = "FC_SMALL";
552 break;
553 case RPC_FC_BYTE:
554 case RPC_FC_USMALL:
555 param_type = RPC_FC_USMALL;
556 param_type_string = "FC_USMALL";
557 break;
558 case RPC_FC_WCHAR:
559 case RPC_FC_SHORT:
560 param_type = RPC_FC_SHORT;
561 param_type_string = "FC_SHORT";
562 break;
563 case RPC_FC_USHORT:
564 param_type = RPC_FC_USHORT;
565 param_type_string = "FC_USHORT";
566 break;
567 case RPC_FC_LONG:
568 param_type = RPC_FC_LONG;
569 param_type_string = "FC_LONG";
570 break;
571 case RPC_FC_ULONG:
572 param_type = RPC_FC_ULONG;
573 param_type_string = "FC_ULONG";
574 break;
575 case RPC_FC_RP:
576 case RPC_FC_UP:
577 case RPC_FC_OP:
578 case RPC_FC_FP:
579 if (sizeof(void *) == 4) /* FIXME */
581 param_type = RPC_FC_LONG;
582 param_type_string = "FC_LONG";
584 else
586 param_type = RPC_FC_HYPER;
587 param_type_string = "FC_HYPER";
589 break;
590 default:
591 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
592 correlation_variable_type);
595 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
596 correlation_type | param_type,
597 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
598 param_type_string);
599 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
600 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
601 offset,
602 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
603 offset);
605 else
607 unsigned int callback_offset = 0;
609 if (structure)
611 struct expr_eval_routine *eval;
612 int found = 0;
614 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
616 if (!strcmp(eval->structure->name, structure->name) &&
617 !compare_expr(eval->expr, expr))
619 found = 1;
620 break;
622 callback_offset++;
625 if (!found)
627 unsigned int align = 0;
628 eval = xmalloc(sizeof(*eval));
629 eval->structure = structure;
630 eval->structure_size = fields_memsize(structure->fields, &align);
631 eval->expr = expr;
632 list_add_tail(&expr_eval_routines, &eval->entry);
635 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
637 else
639 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
640 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
643 if (callback_offset > USHRT_MAX)
644 error("Maximum number of callback routines reached\n");
646 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
647 correlation_type,
648 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
649 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
650 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
652 return 4;
655 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
657 size_t size = 0;
658 const var_t *v;
660 if (!fields) return 0;
661 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
662 size += type_memsize(v->type, v->array, align);
664 return size;
667 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
669 size_t size, maxs = 0;
670 unsigned int align = *pmaxa;
671 const var_t *v;
673 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
675 /* we could have an empty default field with NULL type */
676 if (v->type)
678 size = type_memsize(v->type, v->array, &align);
679 if (maxs < size) maxs = size;
680 if (*pmaxa < align) *pmaxa = align;
684 return maxs;
687 static size_t get_array_size( const array_dims_t *array )
689 size_t size = 1;
690 const expr_t *dim;
692 if (!array) return 0;
694 LIST_FOR_EACH_ENTRY( dim, array, expr_t, entry )
696 if (!dim->is_const) return 0;
697 size *= dim->cval;
700 return size;
703 static size_t type_memsize(const type_t *t, const array_dims_t *array, unsigned int *align)
705 size_t size = 0;
707 if (is_ptr(t))
709 size = sizeof(void *);
710 if (size > *align) *align = size;
712 else switch (t->type)
714 case RPC_FC_BYTE:
715 case RPC_FC_CHAR:
716 case RPC_FC_USMALL:
717 case RPC_FC_SMALL:
718 size = 1;
719 if (size > *align) *align = size;
720 break;
721 case RPC_FC_WCHAR:
722 case RPC_FC_USHORT:
723 case RPC_FC_SHORT:
724 case RPC_FC_ENUM16:
725 size = 2;
726 if (size > *align) *align = size;
727 break;
728 case RPC_FC_ULONG:
729 case RPC_FC_LONG:
730 case RPC_FC_ERROR_STATUS_T:
731 case RPC_FC_ENUM32:
732 case RPC_FC_FLOAT:
733 size = 4;
734 if (size > *align) *align = size;
735 break;
736 case RPC_FC_HYPER:
737 case RPC_FC_DOUBLE:
738 size = 8;
739 if (size > *align) *align = size;
740 break;
741 case RPC_FC_STRUCT:
742 case RPC_FC_CVSTRUCT:
743 case RPC_FC_CPSTRUCT:
744 case RPC_FC_CSTRUCT:
745 case RPC_FC_PSTRUCT:
746 case RPC_FC_BOGUS_STRUCT:
747 size = fields_memsize(t->fields, align);
748 break;
749 case RPC_FC_ENCAPSULATED_UNION:
750 case RPC_FC_NON_ENCAPSULATED_UNION:
751 size = union_memsize(t->fields, align);
752 break;
753 default:
754 error("type_memsize: Unknown type %d\n", t->type);
755 size = 0;
758 if (array) size *= get_array_size( array );
759 return size;
762 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
764 short absoff = type->ref->typestring_offset;
765 short reloff = absoff - (offset + 2);
766 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
768 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
769 type->type, ptr_attr, string_of_type(type->type));
770 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
771 reloff, reloff, absoff);
772 return 4;
775 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
777 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
778 type->type, string_of_type(type->type));
779 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
780 string_of_type(type->ref->type));
781 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
782 return 4;
785 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
787 unsigned int offset = *typestring_offset;
789 print_file(file, 0, "/* %d */\n", offset);
790 type->typestring_offset = offset;
792 if (type->ref->typestring_offset)
793 *typestring_offset += write_nonsimple_pointer(file, type, offset);
794 else if (is_base_type(type->ref->type))
795 *typestring_offset += write_simple_pointer(file, type);
797 return offset;
800 static int has_known_tfs(const type_t *type)
802 return type->typestring_offset || is_base_type(type->type);
805 static int write_pointers(FILE *file, const attr_list_t *attrs,
806 type_t *type, const char *name,
807 const array_dims_t *array, int level,
808 unsigned int *typestring_offset)
810 const var_t *v;
812 /* don't generate a pointer for first-level arrays since we want to
813 * descend into them to write their pointers, not stop here */
814 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
816 return write_pointers(file, NULL, type, name, NULL, level + 1, typestring_offset);
818 else if (is_ptr(type))
820 type_t *ref = type->ref;
822 if (!has_known_tfs(ref))
824 if (is_ptr(ref))
826 write_pointers(file, attrs, ref, name, array, level + 1,
827 typestring_offset);
829 else if (is_struct(ref->type))
831 write_struct_tfs(file, ref, name, typestring_offset);
833 else
835 error("write_pointers: type format string unknown for %s (0x%02x)\n",
836 name, ref->type);
840 /* top-level pointers are handled by write_pointer_description */
841 if (1 < level)
842 write_pointer_tfs(file, type, typestring_offset);
844 return 1;
846 else if (is_struct(type->type))
848 int pointers_written = 0;
849 if (type->fields)
851 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
852 pointers_written += write_pointers(file, v->attrs, v->type,
853 v->name, v->array,
854 level + 1,
855 typestring_offset);
857 return pointers_written;
859 else return 0;
862 static size_t write_pointer_description(FILE *file, const attr_list_t *attrs,
863 type_t *type, size_t mem_offset,
864 const array_dims_t *array, int level,
865 unsigned int *typestring_offset)
867 const var_t *v;
868 unsigned int align = 0;
870 /* don't generate a pointer for first-level arrays since we want to
871 * descend into them to write their pointers, not stop here */
872 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
874 write_pointer_description(file, NULL, type, mem_offset, NULL,
875 level + 1, typestring_offset);
877 else if (is_ptr(type))
879 print_file(file, 2, "0x46,\t/* FC_NO_REPEAT */\n");
880 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
881 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
882 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
883 *typestring_offset += 6;
885 if (has_known_tfs(type->ref))
886 write_pointer_tfs(file, type, typestring_offset);
887 else
888 error("write_pointer_description: type format string unknown\n");
890 else if (level == 0 && is_struct(type->type))
892 if (type->fields)
894 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
895 mem_offset
896 += write_pointer_description(file, v->attrs, v->type,
897 mem_offset, v->array,
898 level + 1,
899 typestring_offset);
903 return type_memsize(type, array, &align);
906 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
907 const type_t *type, const array_dims_t *array,
908 const char *name, unsigned int *typestring_offset)
910 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
911 int has_size = is_non_void(size_is);
912 size_t start_offset = *typestring_offset;
913 unsigned char flags = 0;
914 int pointer_type;
915 unsigned char rtype;
917 if (is_ptr(type))
919 pointer_type = type->type;
920 type = type->ref;
922 else
923 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
925 if (!pointer_type)
926 pointer_type = RPC_FC_RP;
928 if (!get_attrp(attrs, ATTR_SIZEIS))
929 flags |= RPC_FC_P_SIMPLEPOINTER;
931 rtype = type->type;
933 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
935 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
936 return start_offset;
939 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
940 pointer_type, flags,
941 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
942 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
943 *typestring_offset += 2;
945 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
947 print_file(file, 2, "NdrFcShort(0x2),\n");
948 *typestring_offset += 2;
951 if (array && !is_conformant_array(array))
953 /* FIXME: multi-dimensional array */
954 const expr_t *dim = LIST_ENTRY( list_head( array ), expr_t, entry );
955 if (dim->cval > USHRT_MAX)
956 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
957 name, USHRT_MAX, dim->cval - USHRT_MAX);
959 if (rtype == RPC_FC_CHAR)
960 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
961 else
962 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
963 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
964 *typestring_offset += 2;
966 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim->cval, dim->cval);
967 *typestring_offset += 2;
969 return start_offset;
971 else if (has_size)
973 if (rtype == RPC_FC_CHAR)
974 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
975 else
976 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
977 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
978 *typestring_offset += 2;
980 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
982 return start_offset;
984 else
986 if (rtype == RPC_FC_CHAR)
987 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
988 else
989 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
990 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
991 *typestring_offset += 2;
993 return start_offset;
997 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs,
998 type_t *type, const array_dims_t *array,
999 const char *name, unsigned int *typestring_offset)
1001 const expr_list_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
1002 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
1003 int has_length = is_non_void(length_is);
1004 int has_size = is_non_void(size_is) || is_conformant_array(array);
1005 size_t start_offset;
1006 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1007 if (!pointer_type)
1008 pointer_type = RPC_FC_RP;
1010 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
1011 pointer_type,
1012 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
1013 print_file(file, 2, "NdrFcShort(0x2),\n");
1014 *typestring_offset += 4;
1016 if (array && list_count(array) > 1) /* multi-dimensional array */
1018 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
1019 return 0;
1021 else
1023 const expr_t *dim = array ? LIST_ENTRY( list_head( array ), expr_t, entry ) : NULL;
1024 int has_pointer = 0;
1026 if (write_pointers(file, attrs, type, name, array, 0, typestring_offset) > 0)
1027 has_pointer = 1;
1029 start_offset = *typestring_offset;
1031 if (!has_length && !has_size)
1033 /* fixed array */
1034 unsigned int align = 0;
1035 size_t size = type_memsize(type, array, &align);
1036 if (size < USHRT_MAX)
1038 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
1039 /* alignment */
1040 print_file(file, 2, "0x%02x,\n", align - 1);
1041 /* size */
1042 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
1043 *typestring_offset += 4;
1045 else
1047 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
1048 /* alignment */
1049 print_file(file, 2, "0x%02x,\n", align - 1);
1050 /* size */
1051 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
1052 *typestring_offset += 6;
1055 if (has_pointer)
1057 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1058 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1059 *typestring_offset += 2;
1060 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1061 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1062 *typestring_offset += 1;
1065 if (!write_base_type( file, type, typestring_offset ))
1067 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1068 *typestring_offset += 1;
1070 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1071 *typestring_offset += 1;
1073 return start_offset;
1075 else if (has_length && !has_size)
1077 /* varying array */
1078 unsigned int align = 0;
1079 size_t element_size = type_memsize(type, NULL, &align);
1080 size_t elements = dim->cval;
1081 size_t total_size = element_size * elements;
1083 if (total_size < USHRT_MAX)
1085 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
1086 /* alignment */
1087 print_file(file, 2, "0x%02x,\n", align - 1);
1088 /* total size */
1089 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
1090 /* number of elements */
1091 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
1092 *typestring_offset += 6;
1094 else
1096 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
1097 /* alignment */
1098 print_file(file, 2, "0x%02x,\n", align - 1);
1099 /* total size */
1100 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
1101 /* number of elements */
1102 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
1103 *typestring_offset += 10;
1105 /* element size */
1106 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1107 *typestring_offset += 2;
1109 *typestring_offset += write_conf_or_var_desc(file, current_func,
1110 current_structure,
1111 length_is);
1113 if (has_pointer)
1115 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1116 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1117 *typestring_offset += 2;
1118 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1119 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1120 *typestring_offset += 1;
1123 if (!write_base_type( file, type, typestring_offset ))
1125 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1126 *typestring_offset += 1;
1128 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1129 *typestring_offset += 1;
1131 return start_offset;
1133 else if (!has_length && has_size)
1135 /* conformant array */
1136 unsigned int align = 0;
1137 size_t element_size = type_memsize(type, NULL, &align);
1139 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
1140 /* alignment */
1141 print_file(file, 2, "0x%02x,\n", align - 1);
1142 /* element size */
1143 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1144 *typestring_offset += 4;
1146 *typestring_offset += write_conf_or_var_desc(file, current_func,
1147 current_structure,
1148 size_is ? size_is : array);
1150 if (has_pointer)
1152 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1153 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1154 *typestring_offset += 2;
1155 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1156 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1157 *typestring_offset += 1;
1160 if (!write_base_type( file, type, typestring_offset ))
1162 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1163 *typestring_offset += 1;
1165 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1166 *typestring_offset += 1;
1168 return start_offset;
1170 else
1172 /* conformant varying array */
1173 unsigned int align = 0;
1174 size_t element_size = type_memsize(type, NULL, &align);
1176 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
1177 /* alignment */
1178 print_file(file, 2, "0x%02x,\n", align - 1);
1179 /* element size */
1180 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1181 *typestring_offset += 4;
1183 *typestring_offset += write_conf_or_var_desc(file, current_func,
1184 current_structure,
1185 size_is ? size_is : array);
1186 *typestring_offset += write_conf_or_var_desc(file, current_func,
1187 current_structure,
1188 length_is);
1190 if (has_pointer)
1192 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1193 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1194 *typestring_offset += 2;
1195 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1196 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1197 *typestring_offset += 1;
1200 if (!write_base_type( file, type, typestring_offset ))
1202 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1203 *typestring_offset += 1;
1205 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1206 *typestring_offset += 1;
1208 return start_offset;
1213 static const var_t *find_array_or_string_in_struct(const type_t *type)
1215 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1217 if (is_array_type(last_field->attrs, last_field->type, last_field->array))
1218 return last_field;
1220 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1221 (last_field->type->type == RPC_FC_CPSTRUCT) ||
1222 (last_field->type->type == RPC_FC_CVSTRUCT));
1224 return find_array_or_string_in_struct(last_field->type);
1227 static void write_struct_members(FILE *file, const type_t *type, unsigned int *typestring_offset)
1229 const var_t *field;
1231 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1233 unsigned char rtype = field->type->type;
1235 if (field->array)
1236 write_array_tfs( file, field->attrs, field->type, field->array,
1237 field->name, typestring_offset );
1238 else if (is_ptr( field->type ))
1240 /* pointers are handled in detail earlier, here just treat them like longs */
1241 print_file( file, 2, "0x8,\t/* FC_LONG */\n" );
1242 *typestring_offset += 1;
1244 else if (is_embedded_complex(field->type))
1246 size_t absoff = (field->corrdesc
1247 ? field->corrdesc
1248 : field->type->typestring_offset);
1249 short reloff = absoff - (*typestring_offset + 2);
1251 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1252 /* FIXME: actually compute necessary padding */
1253 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1254 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1255 reloff, reloff, absoff);
1256 *typestring_offset += 4;
1258 else if (!write_base_type( file, field->type, typestring_offset ))
1259 error("Unsupported member type 0x%x\n", rtype);
1262 if (!(*typestring_offset % 2))
1264 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1265 *typestring_offset += 1;
1268 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1269 *typestring_offset += 1;
1272 static size_t write_struct_tfs(FILE *file, type_t *type,
1273 const char *name, unsigned int *typestring_offset)
1275 unsigned int total_size;
1276 const var_t *array;
1277 size_t start_offset;
1278 size_t array_offset;
1279 int has_pointers;
1280 unsigned int align = 0;
1282 switch (type->type)
1284 case RPC_FC_STRUCT:
1285 case RPC_FC_PSTRUCT:
1286 total_size = type_memsize(type, NULL, &align);
1288 if (total_size > USHRT_MAX)
1289 error("structure size for %s exceeds %d bytes by %d bytes\n",
1290 name, USHRT_MAX, total_size - USHRT_MAX);
1292 if (type->type == RPC_FC_PSTRUCT)
1293 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1295 start_offset = *typestring_offset;
1296 type->typestring_offset = start_offset;
1297 if (type->type == RPC_FC_STRUCT)
1298 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1299 else
1300 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1301 /* alignment */
1302 print_file(file, 2, "0x%02x,\n", align - 1);
1303 /* total size */
1304 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1305 *typestring_offset += 4;
1307 if (type->type == RPC_FC_PSTRUCT)
1309 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1310 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1311 *typestring_offset += 2;
1312 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1313 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1314 *typestring_offset += 1;
1317 /* member layout */
1318 write_struct_members(file, type, typestring_offset);
1319 return start_offset;
1320 case RPC_FC_CSTRUCT:
1321 case RPC_FC_CPSTRUCT:
1322 total_size = type_memsize(type, NULL, &align);
1324 if (total_size > USHRT_MAX)
1325 error("structure size for %s exceeds %d bytes by %d bytes\n",
1326 name, USHRT_MAX, total_size - USHRT_MAX);
1328 array = find_array_or_string_in_struct(type);
1329 current_structure = type;
1330 array_offset = write_array_tfs(file, array->attrs, array->type,
1331 array->array, array->name,
1332 typestring_offset);
1333 current_structure = NULL;
1335 if (type->type == RPC_FC_CPSTRUCT)
1336 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1338 start_offset = *typestring_offset;
1339 type->typestring_offset = start_offset;
1340 if (type->type == RPC_FC_CSTRUCT)
1341 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1342 else
1343 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1344 /* alignment */
1345 print_file(file, 2, "0x%02x,\n", align - 1);
1346 /* total size */
1347 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1348 *typestring_offset += 4;
1349 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1350 array_offset - *typestring_offset,
1351 array_offset - *typestring_offset,
1352 array_offset);
1353 *typestring_offset += 2;
1355 if (type->type == RPC_FC_CPSTRUCT)
1357 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1358 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1359 *typestring_offset += 2;
1360 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1361 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1362 *typestring_offset += 1;
1365 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1366 *typestring_offset += 1;
1368 return start_offset;
1369 case RPC_FC_CVSTRUCT:
1370 total_size = type_memsize(type, NULL, &align);
1372 if (total_size > USHRT_MAX)
1373 error("structure size for %s exceeds %d bytes by %d bytes\n",
1374 name, USHRT_MAX, total_size - USHRT_MAX);
1376 array = find_array_or_string_in_struct(type);
1377 current_structure = type;
1378 if (is_attr(array->attrs, ATTR_STRING))
1379 array_offset = write_string_tfs(file, array->attrs, array->type,
1380 array->array, array->name,
1381 typestring_offset);
1382 else
1383 array_offset = write_array_tfs(file, array->attrs, array->type,
1384 array->array, array->name,
1385 typestring_offset);
1386 current_structure = NULL;
1388 has_pointers = write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1390 start_offset = *typestring_offset;
1391 type->typestring_offset = start_offset;
1392 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1393 /* alignment */
1394 print_file(file, 2, "0x%02x,\n", align - 1);
1395 /* total size */
1396 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1397 *typestring_offset += 4;
1398 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1399 array_offset - *typestring_offset,
1400 array_offset - *typestring_offset,
1401 array_offset);
1402 *typestring_offset += 2;
1404 if (has_pointers)
1406 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1407 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1408 *typestring_offset += 2;
1409 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1410 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1411 *typestring_offset += 1;
1414 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1415 *typestring_offset += 1;
1417 return start_offset;
1419 case RPC_FC_BOGUS_STRUCT:
1420 total_size = type_memsize(type, NULL, &align);
1421 if (total_size > USHRT_MAX)
1422 error("structure size for %s exceeds %d bytes by %d bytes\n",
1423 name, USHRT_MAX, total_size - USHRT_MAX);
1425 write_embedded_types(file, type, typestring_offset);
1427 start_offset = *typestring_offset;
1428 print_file(file, 0, "/* %d */\n", start_offset);
1429 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1430 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1431 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1433 /* do conformant array stuff */
1434 print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: conformant array stuff */\n");
1436 /* do pointer stuff here */
1437 print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1439 *typestring_offset += 8;
1440 write_struct_members(file, type, typestring_offset);
1442 return start_offset;
1444 default:
1445 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1446 return *typestring_offset;
1450 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1451 unsigned char flags, size_t offset,
1452 unsigned int *typeformat_offset)
1454 size_t start_offset = *typeformat_offset;
1455 short reloff = offset - (*typeformat_offset + 2);
1456 int in_attr, out_attr;
1457 in_attr = is_attr(attrs, ATTR_IN);
1458 out_attr = is_attr(attrs, ATTR_OUT);
1459 if (!in_attr && !out_attr) in_attr = 1;
1461 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1462 flags |= 0x04;
1464 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1465 pointer_type,
1466 flags,
1467 string_of_type(pointer_type));
1468 if (file)
1470 if (flags & 0x04)
1471 fprintf(file, " [allocated_on_stack]");
1472 if (flags & 0x10)
1473 fprintf(file, " [pointer_deref]");
1474 fprintf(file, " */\n");
1477 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1478 *typeformat_offset += 4;
1480 return start_offset;
1483 static void write_branch_type(FILE *file, const type_t *t, size_t *tfsoff)
1485 if (is_base_type(t->type))
1487 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1488 t->type, string_of_type(t->type));
1490 else if (t->typestring_offset)
1492 short reloff = t->typestring_offset - (*tfsoff + 2);
1493 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1494 reloff, reloff, t->typestring_offset);
1496 else
1497 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1499 *tfsoff += 2;
1502 static size_t write_union_tfs(FILE *file, type_t *type, size_t *tfsoff)
1504 size_t align = 0;
1505 size_t start_offset;
1506 size_t size = type_memsize(type, NULL, &align);
1507 var_list_t *fields = type->fields;
1508 size_t nbranch = 0;
1509 type_t *deftype = NULL;
1510 short nodeftype = 0xffff;
1511 var_t *f;
1513 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1515 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1516 if (cases)
1517 nbranch += list_count(cases);
1520 start_offset = *tfsoff;
1521 type->typestring_offset = start_offset;
1522 print_file(file, 0, "/* %d */\n", start_offset);
1523 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1524 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1525 *tfsoff += 4;
1527 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1529 type_t *ft = f->type;
1530 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1531 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1532 expr_t *c;
1534 if (cases == NULL && !deflt)
1535 error("union field %s with neither case nor default attribute\n", f->name);
1537 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1539 /* MIDL doesn't check for duplicate cases, even though that seems
1540 like a reasonable thing to do, it just dumps them to the TFS
1541 like we're going to do here. */
1542 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1543 *tfsoff += 4;
1544 write_branch_type(file, ft, tfsoff);
1547 /* MIDL allows multiple default branches, even though that seems
1548 illogical, it just chooses the last one, which is what we will
1549 do. */
1550 if (deflt)
1552 deftype = ft;
1553 nodeftype = 0;
1557 if (deftype)
1559 write_branch_type(file, deftype, tfsoff);
1561 else
1563 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1564 *tfsoff += 2;
1567 return start_offset;
1570 static size_t write_ip_tfs(FILE *file, const func_t *func, const type_t *type, const var_t *var,
1571 unsigned int *typeformat_offset)
1573 size_t i;
1574 size_t start_offset = *typeformat_offset;
1575 const var_t *iid = get_attrp(var->attrs, ATTR_IIDIS);
1577 if (iid)
1579 expr_t expr;
1580 expr_list_t expr_list;
1582 expr.type = EXPR_IDENTIFIER;
1583 expr.ref = NULL;
1584 expr.u.sval = iid->name;
1585 expr.is_const = FALSE;
1586 list_init( &expr_list );
1587 list_add_head( &expr_list, &expr.entry );
1588 print_file(file, 2, "0x2f, /* FC_IP */\n");
1589 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1590 *typeformat_offset += write_conf_or_var_desc(file, func, NULL, &expr_list) + 2;
1592 else
1594 const type_t *base = is_ptr(type) ? type->ref : type;
1595 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1597 if (! uuid)
1598 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1600 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1601 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1602 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1603 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1604 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1605 for (i = 0; i < 8; ++i)
1606 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1608 if (file)
1609 fprintf(file, "\n");
1611 *typeformat_offset += 18;
1613 return start_offset;
1616 static int get_ptr_attr(const type_t *t, int def_type)
1618 while (TRUE)
1620 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1621 if (ptr_attr)
1622 return ptr_attr;
1623 if (t->kind != TKIND_ALIAS)
1624 return def_type;
1625 t = t->orig;
1629 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1630 type_t *type, const var_t *var,
1631 unsigned int *typeformat_offset)
1633 int pointer_type;
1634 size_t offset;
1636 if (type == var->type) /* top-level pointers */
1638 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1639 if (pointer_attr != 0 && !is_ptr(type))
1640 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1642 if (pointer_attr == 0)
1643 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1645 pointer_type = pointer_attr;
1647 else
1648 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1650 if (((last_ptr(type) && var->array == NULL)
1651 || (!is_ptr(type) && var->array != NULL))
1652 && is_ptrchain_attr(var, ATTR_STRING))
1654 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1657 if (is_array_type(var->attrs, type, var->array))
1658 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1660 if (!is_ptr(type))
1662 /* basic types don't need a type format string */
1663 if (is_base_type(type->type))
1664 return 0;
1666 switch (type->type)
1668 case RPC_FC_STRUCT:
1669 case RPC_FC_PSTRUCT:
1670 case RPC_FC_CSTRUCT:
1671 case RPC_FC_CPSTRUCT:
1672 case RPC_FC_CVSTRUCT:
1673 case RPC_FC_BOGUS_STRUCT:
1674 return write_struct_tfs(file, type, var->name, typeformat_offset);
1675 case RPC_FC_ENCAPSULATED_UNION:
1676 case RPC_FC_NON_ENCAPSULATED_UNION:
1677 return write_union_tfs(file, type, typeformat_offset);
1678 case RPC_FC_IGNORE:
1679 case RPC_FC_BIND_PRIMITIVE:
1680 /* nothing to do */
1681 return 0;
1682 default:
1683 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1686 else if (last_ptr(type))
1688 size_t start_offset = *typeformat_offset;
1689 int in_attr = is_attr(var->attrs, ATTR_IN);
1690 int out_attr = is_attr(var->attrs, ATTR_OUT);
1691 const type_t *base = type->ref;
1693 if (base->type == RPC_FC_IP)
1695 return write_ip_tfs(file, func, type, var, typeformat_offset);
1698 /* special case for pointers to base types */
1699 if (is_base_type(base->type))
1701 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1702 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1703 string_of_type(pointer_type),
1704 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1705 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
1706 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1707 *typeformat_offset += 4;
1708 return start_offset;
1712 assert(is_ptr(type));
1714 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1715 if (file)
1716 fprintf(file, "/* %2u */\n", *typeformat_offset);
1717 return write_pointer_only_tfs(file, var->attrs, pointer_type,
1718 !last_ptr(type) ? 0x10 : 0,
1719 offset, typeformat_offset);
1722 static void clear_tfsoff(type_t *type)
1724 for (;;)
1726 type->typestring_offset = 0;
1728 if (type->kind == TKIND_ALIAS)
1729 type = type->orig;
1730 else if (is_ptr(type))
1731 type = type->ref;
1732 else
1734 if (type->fields)
1736 var_t *v;
1737 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1738 clear_tfsoff(v->type);
1741 return;
1746 static void write_embedded_types(FILE *file, const type_t *type, size_t *tfsoff)
1748 var_list_t *fields = type->fields;
1749 size_t offset = 0;
1750 var_t *f;
1752 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1754 unsigned int align = 0;
1755 type_t *ft = f->type;
1756 size_t corroff;
1758 if (ft->type == RPC_FC_NON_ENCAPSULATED_UNION)
1760 expr_t *swexp = get_attrp(f->attrs, ATTR_SWITCHIS);
1761 const char *swname;
1762 var_t *swvar;
1763 unsigned char corrdesc, op = 0;
1764 short creloff, ureloff;
1766 if (swexp == NULL)
1767 error("union %s needs a switch_is attribute\n", f->name);
1768 if (swexp->type != EXPR_IDENTIFIER)
1769 error("%s: only identifiers are supported for switch_is at this time\n",
1770 f->name);
1772 if (ft->typestring_offset == 0)
1773 write_union_tfs(file, ft, tfsoff);
1775 swname = swexp->u.sval;
1776 corroff = field_offset(type, swname, &swvar);
1777 corrdesc = swvar->type->type;
1778 creloff = corroff - offset;
1780 f->corrdesc = *tfsoff;
1781 ureloff = ft->typestring_offset - (f->corrdesc + 6);
1782 print_file(file, 0, "/* %d */\n", f->corrdesc);
1783 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1784 print_file(file, 2, "0x8,\t/* FIXME: support other switch types */\n");
1785 print_file(file, 2, "0x%x,\t/* Corr desc: %s */\n",
1786 corrdesc, string_of_type(corrdesc & 0xf));
1787 print_file(file, 2, "0x%x,\n", op);
1788 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %hd */\n", creloff, creloff);
1789 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1790 ureloff, ureloff, ft->typestring_offset);
1791 *tfsoff += 8;
1793 else if (!is_base_type(ft->type))
1794 error("write_embedded_types: unknown type (0x%x)\n", ft->type);
1796 /* FIXME: this doesn't take alignment/padding into account */
1797 offset += type_memsize(ft, NULL, &align);
1801 static void clear_all_tfsoffs(const ifref_list_t *ifaces)
1803 const ifref_t * iface;
1804 const func_t *func;
1805 const var_t *var;
1807 if (ifaces)
1808 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1809 if (iface->iface->funcs)
1810 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1811 if (func->args)
1812 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1813 clear_tfsoff(var->type);
1816 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
1818 const var_t *var;
1819 const ifref_t *iface;
1820 unsigned int typeformat_offset = 2;
1822 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1824 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1825 continue;
1827 if (iface->iface->funcs)
1829 const func_t *func;
1830 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1832 if (is_local(func->def->attrs)) continue;
1834 current_func = func;
1835 if (func->args)
1836 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1837 var->type->typestring_offset
1838 = write_typeformatstring_var(file, 2, func, var->type,
1839 var, &typeformat_offset);
1844 return typeformat_offset + 1;
1848 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1850 int indent = 0;
1852 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1853 print_file(file, indent, "{\n");
1854 indent++;
1855 print_file(file, indent, "0,\n");
1856 print_file(file, indent, "{\n");
1857 indent++;
1858 print_file(file, indent, "NdrFcShort(0x0),\n");
1860 clear_all_tfsoffs(ifaces);
1861 process_tfs(file, ifaces, for_objects);
1863 print_file(file, indent, "0x0\n");
1864 indent--;
1865 print_file(file, indent, "}\n");
1866 indent--;
1867 print_file(file, indent, "};\n");
1868 print_file(file, indent, "\n");
1871 static unsigned int get_required_buffer_size_type(
1872 const type_t *type, const array_dims_t *array,
1873 const char *name, unsigned int *alignment)
1875 size_t size = 0;
1877 *alignment = 0;
1878 if (!is_ptr(type))
1880 switch (type->type)
1882 case RPC_FC_BYTE:
1883 case RPC_FC_CHAR:
1884 case RPC_FC_USMALL:
1885 case RPC_FC_SMALL:
1886 *alignment = 4;
1887 size = 1;
1888 break;
1890 case RPC_FC_WCHAR:
1891 case RPC_FC_USHORT:
1892 case RPC_FC_SHORT:
1893 *alignment = 4;
1894 size = 2;
1895 break;
1897 case RPC_FC_ULONG:
1898 case RPC_FC_LONG:
1899 case RPC_FC_FLOAT:
1900 case RPC_FC_ERROR_STATUS_T:
1901 *alignment = 4;
1902 size = 4;
1903 break;
1905 case RPC_FC_HYPER:
1906 case RPC_FC_DOUBLE:
1907 *alignment = 8;
1908 size = 8;
1909 break;
1911 case RPC_FC_IGNORE:
1912 case RPC_FC_BIND_PRIMITIVE:
1913 return 0;
1915 case RPC_FC_STRUCT:
1916 case RPC_FC_PSTRUCT:
1918 const var_t *field;
1919 if (!type->fields) return 0;
1920 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1922 unsigned int alignment;
1923 size += get_required_buffer_size_type(
1924 field->type, field->array, field->name,
1925 &alignment);
1927 break;
1930 case RPC_FC_RP:
1931 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1932 size = get_required_buffer_size_type( type->ref, NULL, name, alignment );
1933 break;
1935 default:
1936 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1937 return 0;
1939 if (array) size *= get_array_size( array );
1941 return size;
1944 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1946 expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1947 int has_size = is_non_void(size_is);
1948 int in_attr = is_attr(var->attrs, ATTR_IN);
1949 int out_attr = is_attr(var->attrs, ATTR_OUT);
1951 if (!in_attr && !out_attr)
1952 in_attr = 1;
1954 *alignment = 0;
1956 if (pass == PASS_OUT)
1958 if (out_attr && is_ptr(var->type))
1960 type_t *type = var->type;
1962 if (type->type == RPC_FC_STRUCT)
1964 const var_t *field;
1965 unsigned int size = 36;
1967 if (!type->fields) return size;
1968 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1970 unsigned int align;
1971 size += get_required_buffer_size_type(
1972 field->type, field->array, field->name,
1973 &align);
1975 return size;
1978 return 0;
1980 else
1982 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1984 if (is_ptr(var->type))
1986 type_t *type = var->type;
1988 if (is_base_type(type->type))
1990 return 25;
1992 else if (type->type == RPC_FC_STRUCT)
1994 unsigned int size = 36;
1995 const var_t *field;
1997 if (!type->fields) return size;
1998 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2000 unsigned int align;
2001 size += get_required_buffer_size_type(
2002 field->type, field->array, field->name,
2003 &align);
2005 return size;
2010 return get_required_buffer_size_type(var->type, var->array, var->name, alignment);
2014 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2016 const var_t *var;
2017 unsigned int total_size = 0, alignment;
2019 if (func->args)
2021 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2023 total_size += get_required_buffer_size(var, &alignment, pass);
2024 total_size += alignment;
2028 if (pass == PASS_OUT && !is_void(func->def->type))
2030 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2031 total_size += alignment;
2033 return total_size;
2036 static void print_phase_function(FILE *file, int indent, const char *type,
2037 enum remoting_phase phase,
2038 const char *varname, unsigned int type_offset)
2040 const char *function;
2041 switch (phase)
2043 case PHASE_BUFFERSIZE:
2044 function = "BufferSize";
2045 break;
2046 case PHASE_MARSHAL:
2047 function = "Marshall";
2048 break;
2049 case PHASE_UNMARSHAL:
2050 function = "Unmarshall";
2051 break;
2052 case PHASE_FREE:
2053 function = "Free";
2054 break;
2055 default:
2056 assert(0);
2057 return;
2060 print_file(file, indent, "Ndr%s%s(\n", type, function);
2061 indent++;
2062 print_file(file, indent, "&_StubMsg,\n");
2063 print_file(file, indent, "%s%s,\n",
2064 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
2065 varname);
2066 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2067 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2068 if (phase == PHASE_UNMARSHAL)
2069 print_file(file, indent, "0);\n");
2070 indent--;
2073 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2074 enum pass pass, const var_t *var,
2075 const char *varname)
2077 type_t *type = var->type;
2078 unsigned int size;
2079 unsigned int alignment = 0;
2080 unsigned char rtype;
2082 /* no work to do for other phases, buffer sizing is done elsewhere */
2083 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2084 return;
2086 rtype = is_ptr(type) ? type->ref->type : type->type;
2088 switch (rtype)
2090 case RPC_FC_BYTE:
2091 case RPC_FC_CHAR:
2092 case RPC_FC_SMALL:
2093 case RPC_FC_USMALL:
2094 size = 1;
2095 alignment = 1;
2096 break;
2098 case RPC_FC_WCHAR:
2099 case RPC_FC_USHORT:
2100 case RPC_FC_SHORT:
2101 size = 2;
2102 alignment = 2;
2103 break;
2105 case RPC_FC_ULONG:
2106 case RPC_FC_LONG:
2107 case RPC_FC_FLOAT:
2108 case RPC_FC_ERROR_STATUS_T:
2109 size = 4;
2110 alignment = 4;
2111 break;
2113 case RPC_FC_HYPER:
2114 case RPC_FC_DOUBLE:
2115 size = 8;
2116 alignment = 8;
2117 break;
2119 case RPC_FC_IGNORE:
2120 case RPC_FC_BIND_PRIMITIVE:
2121 /* no marshalling needed */
2122 return;
2124 default:
2125 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2126 size = 0;
2129 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2130 alignment - 1, alignment - 1);
2132 if (phase == PHASE_MARSHAL)
2134 print_file(file, indent, "*(");
2135 write_type(file, is_ptr(type) ? type->ref : type);
2136 if (is_ptr(type))
2137 fprintf(file, " *)_StubMsg.Buffer = *");
2138 else
2139 fprintf(file, " *)_StubMsg.Buffer = ");
2140 fprintf(file, varname);
2141 fprintf(file, ";\n");
2143 else if (phase == PHASE_UNMARSHAL)
2145 if (pass == PASS_IN || pass == PASS_RETURN)
2146 print_file(file, indent, "");
2147 else
2148 print_file(file, indent, "*");
2149 fprintf(file, varname);
2150 if (pass == PASS_IN && is_ptr(type))
2151 fprintf(file, " = (");
2152 else
2153 fprintf(file, " = *(");
2154 write_type(file, is_ptr(type) ? type->ref : type);
2155 fprintf(file, " *)_StubMsg.Buffer;\n");
2158 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2159 write_type(file, var->type);
2160 fprintf(file, ");\n");
2163 /* returns whether the MaxCount, Offset or ActualCount members need to be
2164 * filled in for the specified phase */
2165 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2167 return (phase != PHASE_UNMARSHAL);
2170 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2171 enum pass pass, enum remoting_phase phase)
2173 const expr_list_t *length_is;
2174 const expr_list_t *size_is;
2175 int in_attr, out_attr, has_length, has_size, pointer_type;
2176 const var_t *var;
2178 if (!func->args)
2179 return;
2181 if (phase == PHASE_BUFFERSIZE)
2183 unsigned int size = get_function_buffer_size( func, pass );
2184 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2187 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2189 const type_t *type = var->type;
2190 unsigned char rtype;
2191 size_t start_offset = type->typestring_offset;
2193 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
2194 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2195 has_length = is_non_void(length_is);
2196 has_size = is_non_void(size_is) || (var->array && is_conformant_array(var->array));
2198 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2199 if (!pointer_type)
2200 pointer_type = RPC_FC_RP;
2202 in_attr = is_attr(var->attrs, ATTR_IN);
2203 out_attr = is_attr(var->attrs, ATTR_OUT);
2204 if (!in_attr && !out_attr)
2205 in_attr = 1;
2207 switch (pass)
2209 case PASS_IN:
2210 if (!in_attr) continue;
2211 break;
2212 case PASS_OUT:
2213 if (!out_attr) continue;
2214 break;
2215 case PASS_RETURN:
2216 break;
2219 rtype = type->type;
2221 if (is_user_derived( var ))
2223 print_phase_function(file, indent, "UserMarshal", phase, var->name, start_offset);
2225 else if (is_string_type(var->attrs, var->type, var->array))
2227 if (var->array && !is_conformant_array(var->array))
2228 print_phase_function(file, indent, "NonConformantString", phase, var->name, start_offset);
2229 else
2231 if (size_is && is_size_needed_for_phase(phase))
2233 const expr_t *size = LIST_ENTRY( list_head(size_is), const expr_t, entry );
2234 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2235 write_expr(file, size, 1);
2236 fprintf(file, ";\n");
2239 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2240 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2241 else
2242 print_phase_function(file, indent, "ConformantString", phase, var->name,
2243 start_offset + (has_size ? 4 : 2));
2246 else if (is_array_type(var->attrs, var->type, var->array))
2248 const char *array_type;
2250 if (var->array && list_count(var->array) > 1) /* multi-dimensional array */
2251 array_type = "ComplexArray";
2252 else
2254 if (!has_length && !has_size)
2255 array_type = "FixedArray";
2256 else if (has_length && !has_size)
2258 if (is_size_needed_for_phase(phase))
2260 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2261 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2262 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2263 write_expr(file, length, 1);
2264 fprintf(file, ";\n\n");
2266 array_type = "VaryingArray";
2268 else if (!has_length && has_size)
2270 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2272 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2273 const expr_t, entry );
2274 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2275 write_expr(file, size, 1);
2276 fprintf(file, ";\n\n");
2278 array_type = "ConformantArray";
2280 else
2282 if (is_size_needed_for_phase(phase))
2284 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2285 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2286 const expr_t, entry );
2287 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2288 write_expr(file, size, 1);
2289 fprintf(file, ";\n");
2290 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2291 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2292 write_expr(file, length, 1);
2293 fprintf(file, ";\n\n");
2295 array_type = "ConformantVaryingArray";
2299 if (!in_attr && phase == PHASE_FREE)
2301 print_file(file, indent, "if (%s)\n", var->name);
2302 indent++;
2303 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2305 else if (phase != PHASE_FREE)
2307 if (pointer_type == RPC_FC_UP)
2308 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2309 else
2310 print_phase_function(file, indent, array_type, phase, var->name, start_offset);
2313 else if (!is_ptr(var->type) && is_base_type(rtype))
2315 print_phase_basetype(file, indent, phase, pass, var, var->name);
2317 else if (!is_ptr(var->type))
2319 switch (rtype)
2321 case RPC_FC_STRUCT:
2322 case RPC_FC_PSTRUCT:
2323 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset);
2324 break;
2325 case RPC_FC_CSTRUCT:
2326 case RPC_FC_CPSTRUCT:
2327 print_phase_function(file, indent, "ConformantStruct", phase, var->name, start_offset);
2328 break;
2329 case RPC_FC_CVSTRUCT:
2330 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var->name, start_offset);
2331 break;
2332 case RPC_FC_BOGUS_STRUCT:
2333 print_phase_function(file, indent, "ComplexStruct", phase, var->name, start_offset);
2334 break;
2335 case RPC_FC_RP:
2336 if (is_base_type( var->type->ref->type ))
2338 print_phase_basetype(file, indent, phase, pass, var, var->name);
2340 else if (var->type->ref->type == RPC_FC_STRUCT)
2342 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2343 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2345 else
2347 const var_t *iid;
2348 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2349 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2350 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2352 break;
2353 default:
2354 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2357 else
2359 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2361 print_phase_basetype(file, indent, phase, pass, var, var->name);
2363 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2365 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2366 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2368 else
2370 const var_t *iid;
2371 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2372 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2373 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2376 fprintf(file, "\n");
2381 size_t get_size_procformatstring_var(const var_t *var)
2383 return write_procformatstring_var(NULL, 0, var, FALSE);
2387 size_t get_size_procformatstring_func(const func_t *func)
2389 const var_t *var;
2390 size_t size = 0;
2392 /* argument list size */
2393 if (func->args)
2394 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2395 size += get_size_procformatstring_var(var);
2397 /* return value size */
2398 if (is_void(func->def->type))
2399 size += 2; /* FC_END and FC_PAD */
2400 else
2401 size += get_size_procformatstring_var(func->def);
2403 return size;
2406 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2408 const ifref_t *iface;
2409 size_t size = 1;
2410 const func_t *func;
2412 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2414 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2415 continue;
2417 if (iface->iface->funcs)
2418 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2419 if (!is_local(func->def->attrs))
2420 size += get_size_procformatstring_func( func );
2422 return size;
2425 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2427 return process_tfs(NULL, ifaces, for_objects);
2430 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2431 const var_list_t *fields, const char *structvar)
2433 switch (e->type) {
2434 case EXPR_VOID:
2435 break;
2436 case EXPR_NUM:
2437 fprintf(h, "%lu", e->u.lval);
2438 break;
2439 case EXPR_HEXNUM:
2440 fprintf(h, "0x%lx", e->u.lval);
2441 break;
2442 case EXPR_TRUEFALSE:
2443 if (e->u.lval == 0)
2444 fprintf(h, "FALSE");
2445 else
2446 fprintf(h, "TRUE");
2447 break;
2448 case EXPR_IDENTIFIER:
2450 const var_t *field;
2451 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2452 if (!strcmp(e->u.sval, field->name))
2454 fprintf(h, "%s->%s", structvar, e->u.sval);
2455 break;
2458 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2459 break;
2461 case EXPR_NEG:
2462 fprintf(h, "-");
2463 write_struct_expr(h, e->ref, 1, fields, structvar);
2464 break;
2465 case EXPR_NOT:
2466 fprintf(h, "~");
2467 write_struct_expr(h, e->ref, 1, fields, structvar);
2468 break;
2469 case EXPR_PPTR:
2470 fprintf(h, "*");
2471 write_struct_expr(h, e->ref, 1, fields, structvar);
2472 break;
2473 case EXPR_CAST:
2474 fprintf(h, "(");
2475 write_type(h, e->u.tref);
2476 fprintf(h, ")");
2477 write_struct_expr(h, e->ref, 1, fields, structvar);
2478 break;
2479 case EXPR_SIZEOF:
2480 fprintf(h, "sizeof(");
2481 write_type(h, e->u.tref);
2482 fprintf(h, ")");
2483 break;
2484 case EXPR_SHL:
2485 case EXPR_SHR:
2486 case EXPR_MUL:
2487 case EXPR_DIV:
2488 case EXPR_ADD:
2489 case EXPR_SUB:
2490 case EXPR_AND:
2491 case EXPR_OR:
2492 if (brackets) fprintf(h, "(");
2493 write_struct_expr(h, e->ref, 1, fields, structvar);
2494 switch (e->type) {
2495 case EXPR_SHL: fprintf(h, " << "); break;
2496 case EXPR_SHR: fprintf(h, " >> "); break;
2497 case EXPR_MUL: fprintf(h, " * "); break;
2498 case EXPR_DIV: fprintf(h, " / "); break;
2499 case EXPR_ADD: fprintf(h, " + "); break;
2500 case EXPR_SUB: fprintf(h, " - "); break;
2501 case EXPR_AND: fprintf(h, " & "); break;
2502 case EXPR_OR: fprintf(h, " | "); break;
2503 default: break;
2505 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2506 if (brackets) fprintf(h, ")");
2507 break;
2508 case EXPR_COND:
2509 if (brackets) fprintf(h, "(");
2510 write_struct_expr(h, e->ref, 1, fields, structvar);
2511 fprintf(h, " ? ");
2512 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2513 fprintf(h, " : ");
2514 write_struct_expr(h, e->ext2, 1, fields, structvar);
2515 if (brackets) fprintf(h, ")");
2516 break;
2521 void declare_stub_args( FILE *file, int indent, const func_t *func )
2523 int in_attr, out_attr;
2524 int i = 0;
2525 const var_t *def = func->def;
2526 const var_t *var;
2528 /* declare return value '_RetVal' */
2529 if (!is_void(def->type))
2531 print_file(file, indent, "");
2532 write_type(file, def->type);
2533 fprintf(file, " _RetVal;\n");
2536 if (!func->args)
2537 return;
2539 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2541 const expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2542 int has_size = is_non_void(size_is);
2543 int is_string = is_attr(var->attrs, ATTR_STRING);
2545 in_attr = is_attr(var->attrs, ATTR_IN);
2546 out_attr = is_attr(var->attrs, ATTR_OUT);
2547 if (!out_attr && !in_attr)
2548 in_attr = 1;
2550 if (!in_attr && !has_size && !is_string)
2552 print_file(file, indent, "");
2553 write_type(file, var->type->ref);
2554 fprintf(file, " _W%u;\n", i++);
2557 print_file(file, indent, "");
2558 write_type(file, var->type);
2559 fprintf(file, " ");
2560 if (var->array) {
2561 fprintf(file, "( *");
2562 write_name(file, var);
2563 fprintf(file, " )");
2564 } else
2565 write_name(file, var);
2566 write_array(file, var->array, 0);
2567 fprintf(file, ";\n");
2572 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2574 int in_attr, out_attr;
2575 int i = 0, sep = 0;
2576 const var_t *var;
2577 const expr_list_t *size_is;
2578 int has_size;
2580 if (!func->args)
2581 return;
2583 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2585 int is_string = is_attr(var->attrs, ATTR_STRING);
2586 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2587 has_size = is_non_void(size_is);
2588 in_attr = is_attr(var->attrs, ATTR_IN);
2589 out_attr = is_attr(var->attrs, ATTR_OUT);
2590 if (!out_attr && !in_attr)
2591 in_attr = 1;
2593 if (!in_attr)
2595 print_file(file, indent, "");
2596 write_name(file, var);
2598 if (has_size)
2600 const expr_t *expr;
2601 unsigned int size, align = 0;
2602 type_t *type = var->type;
2604 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2605 LIST_FOR_EACH_ENTRY( expr, size_is, const expr_t, entry )
2607 if (expr->type == EXPR_VOID) continue;
2608 write_expr( file, expr, 1 );
2609 fprintf(file, " * ");
2611 size = type_memsize(type, NULL, &align);
2612 fprintf(file, "%u);\n", size);
2614 else if (!is_string)
2616 fprintf(file, " = &_W%u;\n", i);
2617 if (is_ptr(var->type) && !last_ptr(var->type))
2618 print_file(file, indent, "_W%u = 0;\n", i);
2619 i++;
2622 sep = 1;
2625 if (sep)
2626 fprintf(file, "\n");
2630 int write_expr_eval_routines(FILE *file, const char *iface)
2632 int result = 0;
2633 struct expr_eval_routine *eval;
2634 unsigned short callback_offset = 0;
2636 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2638 int indent = 0;
2639 result = 1;
2640 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2641 iface, eval->structure->name, callback_offset);
2642 print_file(file, indent, "{\n");
2643 indent++;
2644 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2645 eval->structure->name, eval->structure->name, eval->structure_size);
2646 fprintf(file, "\n");
2647 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2648 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2649 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2650 fprintf(file, ";\n");
2651 indent--;
2652 print_file(file, indent, "}\n\n");
2653 callback_offset++;
2655 return result;
2658 void write_expr_eval_routine_list(FILE *file, const char *iface)
2660 struct expr_eval_routine *eval;
2661 struct expr_eval_routine *cursor;
2662 unsigned short callback_offset = 0;
2664 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2665 fprintf(file, "{\n");
2667 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2669 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2670 iface, eval->structure->name, callback_offset);
2672 callback_offset++;
2673 list_remove(&eval->entry);
2674 free(eval);
2677 fprintf(file, "};\n\n");
2681 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2683 const struct str_list_entry_t *endpoint;
2684 const char *p;
2686 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2687 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2688 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2690 print_file( f, 1, "{ (const unsigned char *)\"" );
2691 for (p = endpoint->str; *p && *p != ':'; p++)
2693 if (*p == '"' || *p == '\\') fputc( '\\', f );
2694 fputc( *p, f );
2696 if (!*p) goto error;
2697 if (p[1] != '[') goto error;
2699 fprintf( f, "\", (const unsigned char *)\"" );
2700 for (p += 2; *p && *p != ']'; p++)
2702 if (*p == '"' || *p == '\\') fputc( '\\', f );
2703 fputc( *p, f );
2705 if (*p != ']') goto error;
2706 fprintf( f, "\" },\n" );
2708 print_file( f, 0, "};\n\n" );
2709 return;
2711 error:
2712 error("Invalid endpoint syntax '%s'\n", endpoint->str);