Update WIDL to rev 20070519.
[reactos.git] / reactos / tools / widl / typegen.c
1 /*
2 * Format String Generator for IDL Compiler
3 *
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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>
35
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"
42
43 #include "widl.h"
44 #include "typegen.h"
45
46 static const func_t *current_func;
47 static const type_t *current_structure;
48
49 /* name of the structure variable for structure callbacks */
50 #define STRUCT_EXPR_EVAL_VAR "pS"
51
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
53
54 struct expr_eval_routine
55 {
56 struct list entry;
57 const type_t *structure;
58 size_t structure_size;
59 const expr_t *expr;
60 };
61
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)
67 {
68 switch (type)
69 {
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 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
91 default:
92 error("string_of_type: unknown type 0x%02x\n", type);
93 return NULL;
94 }
95 }
96
97 static int is_struct(unsigned char type)
98 {
99 switch (type)
100 {
101 case RPC_FC_STRUCT:
102 case RPC_FC_PSTRUCT:
103 case RPC_FC_CSTRUCT:
104 case RPC_FC_CPSTRUCT:
105 case RPC_FC_CVSTRUCT:
106 case RPC_FC_BOGUS_STRUCT:
107 return 1;
108 default:
109 return 0;
110 }
111 }
112
113 static int compare_expr(const expr_t *a, const expr_t *b)
114 {
115 int ret;
116
117 if (a->type != b->type)
118 return a->type - b->type;
119
120 switch (a->type)
121 {
122 case EXPR_NUM:
123 case EXPR_HEXNUM:
124 case EXPR_TRUEFALSE:
125 return a->u.lval - b->u.lval;
126 case EXPR_IDENTIFIER:
127 return strcmp(a->u.sval, b->u.sval);
128 case EXPR_COND:
129 ret = compare_expr(a->ref, b->ref);
130 if (ret != 0)
131 return ret;
132 ret = compare_expr(a->u.ext, b->u.ext);
133 if (ret != 0)
134 return ret;
135 return compare_expr(a->ext2, b->ext2);
136 case EXPR_OR:
137 case EXPR_AND:
138 case EXPR_ADD:
139 case EXPR_SUB:
140 case EXPR_MUL:
141 case EXPR_DIV:
142 case EXPR_SHL:
143 case EXPR_SHR:
144 ret = compare_expr(a->ref, b->ref);
145 if (ret != 0)
146 return ret;
147 return compare_expr(a->u.ext, b->u.ext);
148 case EXPR_NOT:
149 case EXPR_NEG:
150 case EXPR_PPTR:
151 case EXPR_CAST:
152 case EXPR_SIZEOF:
153 return compare_expr(a->ref, b->ref);
154 case EXPR_VOID:
155 return 0;
156 }
157 return -1;
158 }
159
160 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
161 do { \
162 if (file) \
163 fprintf(file, "/* %2u */\n", typestring_offset); \
164 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
165 } \
166 while (0)
167
168 static int print_file(FILE *file, int indent, const char *format, ...)
169 {
170 va_list va;
171 int i, r;
172
173 if (!file) return 0;
174
175 va_start(va, format);
176 for (i = 0; i < indent; i++)
177 fprintf(file, " ");
178 r = vfprintf(file, format, va);
179 va_end(va);
180 return r;
181 }
182
183 static void write_formatdesc(FILE *f, int indent, const char *str)
184 {
185 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
186 print_file(f, indent, "{\n");
187 print_file(f, indent + 1, "short Pad;\n");
188 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
189 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
190 print_file(f, indent, "\n");
191 }
192
193 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
194 {
195 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
196 get_size_typeformatstring(ifaces, for_objects));
197
198 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
199 get_size_procformatstring(ifaces, for_objects));
200
201 fprintf(f, "\n");
202 write_formatdesc(f, indent, "TYPE");
203 write_formatdesc(f, indent, "PROC");
204 fprintf(f, "\n");
205 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
206 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
207 print_file(f, indent, "\n");
208 }
209
210 static int is_user_derived(const var_t *v)
211 {
212 const type_t *type = v->type;
213
214 if (v->attrs && is_attr( v->attrs, ATTR_WIREMARSHAL )) return 1;
215
216 while (type)
217 {
218 if (type->attrs && is_attr( type->attrs, ATTR_WIREMARSHAL )) return 1;
219 type = type->ref;
220 }
221 return 0;
222 }
223
224 static inline int is_base_type(unsigned char type)
225 {
226 switch (type)
227 {
228 case RPC_FC_BYTE:
229 case RPC_FC_CHAR:
230 case RPC_FC_USMALL:
231 case RPC_FC_SMALL:
232 case RPC_FC_WCHAR:
233 case RPC_FC_USHORT:
234 case RPC_FC_SHORT:
235 case RPC_FC_ULONG:
236 case RPC_FC_LONG:
237 case RPC_FC_HYPER:
238 case RPC_FC_IGNORE:
239 case RPC_FC_FLOAT:
240 case RPC_FC_DOUBLE:
241 case RPC_FC_ENUM16:
242 case RPC_FC_ENUM32:
243 case RPC_FC_ERROR_STATUS_T:
244 case RPC_FC_BIND_PRIMITIVE:
245 return TRUE;
246
247 default:
248 return FALSE;
249 }
250 }
251
252 static size_t write_procformatstring_var(FILE *file, int indent,
253 const var_t *var, int is_return)
254 {
255 size_t size;
256 const type_t *type = var->type;
257
258 int is_in = is_attr(var->attrs, ATTR_IN);
259 int is_out = is_attr(var->attrs, ATTR_OUT);
260
261 if (!is_in && !is_out) is_in = TRUE;
262
263 if (!var->array && is_base_type(type->type))
264 {
265 if (is_return)
266 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
267 else
268 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
269
270 if (is_base_type(type->type))
271 {
272 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
273 size = 2; /* includes param type prefix */
274 }
275 else if (type->type == RPC_FC_BIND_PRIMITIVE)
276 {
277 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
278 size = 2; /* includes param type prefix */
279 }
280 else
281 {
282 error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
283 size = 0;
284 }
285 }
286 else
287 {
288 if (is_return)
289 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
290 else if (is_in && is_out)
291 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
292 else if (is_out)
293 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
294 else
295 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
296
297 print_file(file, indent, "0x01,\n");
298 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
299 size = 4; /* includes param type prefix */
300 }
301 return size;
302 }
303
304 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
305 {
306 const ifref_t *iface;
307 int indent = 0;
308 const var_t *var;
309
310 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
311 print_file(file, indent, "{\n");
312 indent++;
313 print_file(file, indent, "0,\n");
314 print_file(file, indent, "{\n");
315 indent++;
316
317 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
318 {
319 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
320 continue;
321
322 if (iface->iface->funcs)
323 {
324 const func_t *func;
325 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
326 {
327 if (is_local(func->def->attrs)) continue;
328 /* emit argument data */
329 if (func->args)
330 {
331 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
332 write_procformatstring_var(file, indent, var, FALSE);
333 }
334
335 /* emit return value data */
336 var = func->def;
337 if (is_void(var->type))
338 {
339 print_file(file, indent, "0x5b, /* FC_END */\n");
340 print_file(file, indent, "0x5c, /* FC_PAD */\n");
341 }
342 else
343 write_procformatstring_var(file, indent, var, TRUE);
344 }
345 }
346 }
347
348 print_file(file, indent, "0x0\n");
349 indent--;
350 print_file(file, indent, "}\n");
351 indent--;
352 print_file(file, indent, "};\n");
353 print_file(file, indent, "\n");
354 }
355
356 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
357 {
358 if (is_base_type(type->type))
359 {
360 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
361 *typestring_offset += 1;
362 return 1;
363 }
364
365 return 0;
366 }
367
368 /* write conformance / variance descriptor */
369 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 {
371 unsigned char operator_type = 0;
372 const char *operator_string = "no operators";
373 const expr_t *expr, *subexpr;
374 unsigned char correlation_type;
375
376 if (!file) return 4; /* optimisation for sizing pass */
377
378 if (list_count(expr_list) > 1)
379 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
380
381 expr = subexpr = LIST_ENTRY( list_head(expr_list), const expr_t, entry );
382
383 if (expr->is_const)
384 {
385 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
386 error("write_conf_or_var_desc: constant value %ld is greater than "
387 "the maximum constant size of %d\n", expr->cval,
388 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
389
390 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
391 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
392 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
393 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
394
395 return 4;
396 }
397
398 switch (subexpr->type)
399 {
400 case EXPR_PPTR:
401 subexpr = subexpr->ref;
402 operator_type = RPC_FC_DEREFERENCE;
403 operator_string = "FC_DEREFERENCE";
404 break;
405 case EXPR_DIV:
406 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
407 {
408 subexpr = subexpr->ref;
409 operator_type = RPC_FC_DIV_2;
410 operator_string = "FC_DIV_2";
411 }
412 break;
413 case EXPR_MUL:
414 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
415 {
416 subexpr = subexpr->ref;
417 operator_type = RPC_FC_MULT_2;
418 operator_string = "FC_MULT_2";
419 }
420 break;
421 case EXPR_SUB:
422 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
423 {
424 subexpr = subexpr->ref;
425 operator_type = RPC_FC_SUB_1;
426 operator_string = "FC_SUB_1";
427 }
428 break;
429 case EXPR_ADD:
430 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
431 {
432 subexpr = subexpr->ref;
433 operator_type = RPC_FC_ADD_1;
434 operator_string = "FC_ADD_1";
435 }
436 break;
437 default:
438 break;
439 }
440
441 if (subexpr->type == EXPR_IDENTIFIER)
442 {
443 const type_t *correlation_variable = NULL;
444 unsigned char correlation_variable_type;
445 unsigned char param_type = 0;
446 const char *param_type_string = NULL;
447 size_t offset;
448
449 if (structure)
450 {
451 const var_t *var;
452
453 offset = 0;
454 if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
455 {
456 unsigned int align = 0;
457 offset -= type_memsize(var->type, var->array, &align);
458 /* FIXME: take alignment into account */
459 if (!strcmp(var->name, subexpr->u.sval))
460 {
461 correlation_variable = var->type;
462 break;
463 }
464 }
465 if (!correlation_variable)
466 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
467 subexpr->u.sval);
468
469 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
470 }
471 else
472 {
473 const var_t *var;
474
475 offset = sizeof(void *);
476 if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
477 {
478 if (!strcmp(var->name, subexpr->u.sval))
479 {
480 correlation_variable = var->type;
481 break;
482 }
483 /* FIXME: not all stack variables are sizeof(void *) */
484 offset += sizeof(void *);
485 }
486 if (!correlation_variable)
487 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
488 subexpr->u.sval);
489
490 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
491 }
492
493 correlation_variable_type = correlation_variable->type;
494
495 switch (correlation_variable_type)
496 {
497 case RPC_FC_CHAR:
498 case RPC_FC_SMALL:
499 param_type = RPC_FC_SMALL;
500 param_type_string = "FC_SMALL";
501 break;
502 case RPC_FC_BYTE:
503 case RPC_FC_USMALL:
504 param_type = RPC_FC_USMALL;
505 param_type_string = "FC_USMALL";
506 break;
507 case RPC_FC_WCHAR:
508 case RPC_FC_SHORT:
509 param_type = RPC_FC_SHORT;
510 param_type_string = "FC_SHORT";
511 break;
512 case RPC_FC_USHORT:
513 param_type = RPC_FC_USHORT;
514 param_type_string = "FC_USHORT";
515 break;
516 case RPC_FC_LONG:
517 param_type = RPC_FC_LONG;
518 param_type_string = "FC_LONG";
519 break;
520 case RPC_FC_ULONG:
521 param_type = RPC_FC_ULONG;
522 param_type_string = "FC_ULONG";
523 break;
524 case RPC_FC_RP:
525 case RPC_FC_UP:
526 case RPC_FC_OP:
527 case RPC_FC_FP:
528 if (sizeof(void *) == 4) /* FIXME */
529 {
530 param_type = RPC_FC_LONG;
531 param_type_string = "FC_LONG";
532 }
533 else
534 {
535 param_type = RPC_FC_HYPER;
536 param_type_string = "FC_HYPER";
537 }
538 break;
539 default:
540 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
541 correlation_variable_type);
542 }
543
544 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
545 correlation_type | param_type,
546 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
547 param_type_string);
548 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
549 print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
550 offset,
551 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
552 offset);
553 }
554 else
555 {
556 unsigned int callback_offset = 0;
557
558 if (structure)
559 {
560 struct expr_eval_routine *eval;
561 int found = 0;
562
563 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
564 {
565 if (!strcmp(eval->structure->name, structure->name) &&
566 !compare_expr(eval->expr, expr))
567 {
568 found = 1;
569 break;
570 }
571 callback_offset++;
572 }
573
574 if (!found)
575 {
576 unsigned int align = 0;
577 eval = xmalloc(sizeof(*eval));
578 eval->structure = structure;
579 eval->structure_size = fields_memsize(structure->fields, &align);
580 eval->expr = expr;
581 list_add_tail(&expr_eval_routines, &eval->entry);
582 }
583
584 correlation_type = RPC_FC_NORMAL_CONFORMANCE;
585 }
586 else
587 {
588 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
589 correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
590 }
591
592 if (callback_offset > USHRT_MAX)
593 error("Maximum number of callback routines reached\n");
594
595 print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
596 correlation_type,
597 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
598 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
599 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
600 }
601 return 4;
602 }
603
604 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
605 {
606 size_t size = 0;
607 const var_t *v;
608
609 if (!fields) return 0;
610 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
611 size += type_memsize(v->type, v->array, align);
612
613 return size;
614 }
615
616 static size_t get_array_size( const array_dims_t *array )
617 {
618 size_t size = 1;
619 const expr_t *dim;
620
621 if (!array) return 0;
622
623 LIST_FOR_EACH_ENTRY( dim, array, expr_t, entry )
624 {
625 if (!dim->is_const) return 0;
626 size *= dim->cval;
627 }
628
629 return size;
630 }
631
632 static size_t type_memsize(const type_t *t, const array_dims_t *array, unsigned int *align)
633 {
634 size_t size = 0;
635
636 if (is_ptr(t))
637 {
638 size = sizeof(void *);
639 if (size > *align) *align = size;
640 }
641 else switch (t->type)
642 {
643 case RPC_FC_BYTE:
644 case RPC_FC_CHAR:
645 case RPC_FC_USMALL:
646 case RPC_FC_SMALL:
647 size = 1;
648 if (size > *align) *align = size;
649 break;
650 case RPC_FC_WCHAR:
651 case RPC_FC_USHORT:
652 case RPC_FC_SHORT:
653 case RPC_FC_ENUM16:
654 size = 2;
655 if (size > *align) *align = size;
656 break;
657 case RPC_FC_ULONG:
658 case RPC_FC_LONG:
659 case RPC_FC_ERROR_STATUS_T:
660 case RPC_FC_ENUM32:
661 case RPC_FC_FLOAT:
662 size = 4;
663 if (size > *align) *align = size;
664 break;
665 case RPC_FC_HYPER:
666 case RPC_FC_DOUBLE:
667 size = 8;
668 if (size > *align) *align = size;
669 break;
670 case RPC_FC_STRUCT:
671 case RPC_FC_CVSTRUCT:
672 case RPC_FC_CPSTRUCT:
673 case RPC_FC_CSTRUCT:
674 case RPC_FC_PSTRUCT:
675 case RPC_FC_BOGUS_STRUCT:
676 case RPC_FC_ENCAPSULATED_UNION:
677 case RPC_FC_NON_ENCAPSULATED_UNION:
678 size = fields_memsize(t->fields, align);
679 break;
680 default:
681 error("type_memsize: Unknown type %d\n", t->type);
682 size = 0;
683 }
684
685 if (array) size *= get_array_size( array );
686 return size;
687 }
688
689 static size_t write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
690 {
691 short absoff = type->ref->typestring_offset;
692 short reloff = absoff - (offset + 2);
693 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
694
695 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
696 type->type, ptr_attr, string_of_type(type->type));
697 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
698 reloff, reloff, absoff);
699 return 4;
700 }
701
702 static size_t write_simple_pointer(FILE *file, const type_t *type)
703 {
704 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
705 type->type, string_of_type(type->type));
706 print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
707 string_of_type(type->ref->type));
708 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
709 return 4;
710 }
711
712 static size_t write_pointer_tfs(FILE *file, type_t *type, size_t *typestring_offset)
713 {
714 size_t offset = *typestring_offset;
715
716 print_file(file, 0, "/* %d */\n", offset);
717 type->typestring_offset = offset;
718
719 if (type->ref->typestring_offset)
720 *typestring_offset += write_nonsimple_pointer(file, type, offset);
721 else if (is_base_type(type->ref->type))
722 *typestring_offset += write_simple_pointer(file, type);
723
724 return offset;
725 }
726
727 static int has_known_tfs(const type_t *type)
728 {
729 return type->typestring_offset || is_base_type(type->type);
730 }
731
732 static int write_pointers(FILE *file, const attr_list_t *attrs,
733 type_t *type, const char *name,
734 const array_dims_t *array, int level,
735 unsigned int *typestring_offset)
736 {
737 const var_t *v;
738
739 /* don't generate a pointer for first-level arrays since we want to
740 * descend into them to write their pointers, not stop here */
741 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
742 {
743 return write_pointers(file, NULL, type, name, NULL, level + 1, typestring_offset);
744 }
745 else if (is_ptr(type))
746 {
747 type_t *ref = type->ref;
748
749 if (!has_known_tfs(ref))
750 {
751 if (is_ptr(ref))
752 {
753 write_pointers(file, attrs, ref, name, array, level + 1,
754 typestring_offset);
755 }
756 else if (is_struct(ref->type))
757 {
758 write_struct_tfs(file, ref, name, typestring_offset);
759 }
760 else
761 {
762 error("write_pointers: type format string unknown for %s (0x%02x)\n",
763 name, ref->type);
764 }
765 }
766
767 /* top-level pointers are handled by write_pointer_description */
768 if (1 < level)
769 write_pointer_tfs(file, type, typestring_offset);
770
771 return 1;
772 }
773 else if (is_struct(type->type))
774 {
775 int pointers_written = 0;
776 if (type->fields)
777 {
778 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
779 pointers_written += write_pointers(file, v->attrs, v->type,
780 v->name, v->array,
781 level + 1,
782 typestring_offset);
783 }
784 return pointers_written;
785 }
786 else return 0;
787 }
788
789 static size_t write_pointer_description(FILE *file, const attr_list_t *attrs,
790 type_t *type, size_t mem_offset,
791 const array_dims_t *array, int level,
792 size_t *typestring_offset)
793 {
794 const var_t *v;
795 unsigned int align = 0;
796
797 /* don't generate a pointer for first-level arrays since we want to
798 * descend into them to write their pointers, not stop here */
799 if ((level == 0 || !is_ptr(type)) && is_array_type(attrs, type, array))
800 {
801 write_pointer_description(file, NULL, type, mem_offset, NULL,
802 level + 1, typestring_offset);
803 }
804 else if (is_ptr(type))
805 {
806 print_file(file, 2, "0x46,\t/* FC_NO_REPEAT */\n");
807 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
808 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
809 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset, mem_offset);
810 *typestring_offset += 6;
811
812 if (has_known_tfs(type->ref))
813 write_pointer_tfs(file, type, typestring_offset);
814 else
815 error("write_pointer_description: type format string unknown\n");
816 }
817 else if (level == 0 && is_struct(type->type))
818 {
819 if (type->fields)
820 {
821 LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
822 mem_offset
823 += write_pointer_description(file, v->attrs, v->type,
824 mem_offset, v->array,
825 level + 1,
826 typestring_offset);
827 }
828 }
829
830 return type_memsize(type, array, &align);
831 }
832
833 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
834 const type_t *type, const array_dims_t *array,
835 const char *name, unsigned int *typestring_offset)
836 {
837 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
838 int has_size = is_non_void(size_is);
839 size_t start_offset = *typestring_offset;
840 unsigned char flags = 0;
841 int pointer_type;
842 unsigned char rtype;
843
844 if (is_ptr(type))
845 {
846 pointer_type = type->type;
847 type = type->ref;
848 }
849 else
850 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
851
852 if (!pointer_type)
853 pointer_type = RPC_FC_RP;
854
855 if (!get_attrp(attrs, ATTR_SIZEIS))
856 flags |= RPC_FC_P_SIMPLEPOINTER;
857
858 rtype = type->type;
859
860 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
861 {
862 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
863 return start_offset;
864 }
865
866 print_file(file, 2,"0x%x, 0x%x, /* %s%s */\n",
867 pointer_type, flags,
868 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
869 (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
870 *typestring_offset += 2;
871
872 if (!(flags & RPC_FC_P_SIMPLEPOINTER))
873 {
874 print_file(file, 2, "NdrFcShort(0x2),\n");
875 *typestring_offset += 2;
876 }
877
878 if (array && !is_conformant_array(array))
879 {
880 /* FIXME: multi-dimensional array */
881 const expr_t *dim = LIST_ENTRY( list_head( array ), expr_t, entry );
882 if (dim->cval > USHRT_MAX)
883 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
884 name, USHRT_MAX, dim->cval - USHRT_MAX);
885
886 if (rtype == RPC_FC_CHAR)
887 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
888 else
889 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
890 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
891 *typestring_offset += 2;
892
893 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim->cval, dim->cval);
894 *typestring_offset += 2;
895
896 return start_offset;
897 }
898 else if (has_size)
899 {
900 if (rtype == RPC_FC_CHAR)
901 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
902 else
903 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
904 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
905 *typestring_offset += 2;
906
907 *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
908
909 return start_offset;
910 }
911 else
912 {
913 if (rtype == RPC_FC_CHAR)
914 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
915 else
916 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
917 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
918 *typestring_offset += 2;
919
920 return start_offset;
921 }
922 }
923
924 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs,
925 type_t *type, const array_dims_t *array,
926 const char *name, unsigned int *typestring_offset)
927 {
928 const expr_list_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
929 const expr_list_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
930 int has_length = is_non_void(length_is);
931 int has_size = is_non_void(size_is) || is_conformant_array(array);
932 size_t start_offset;
933 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
934 if (!pointer_type)
935 pointer_type = RPC_FC_RP;
936
937 print_file(file, 2, "0x%x, 0x00, /* %s */\n",
938 pointer_type,
939 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
940 print_file(file, 2, "NdrFcShort(0x2),\n");
941 *typestring_offset += 4;
942
943 if (array && list_count(array) > 1) /* multi-dimensional array */
944 {
945 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
946 return 0;
947 }
948 else
949 {
950 const expr_t *dim = array ? LIST_ENTRY( list_head( array ), expr_t, entry ) : NULL;
951 int has_pointer = 0;
952
953 if (write_pointers(file, attrs, type, name, array, 0, typestring_offset) > 0)
954 has_pointer = 1;
955
956 start_offset = *typestring_offset;
957
958 if (!has_length && !has_size)
959 {
960 /* fixed array */
961 unsigned int align = 0;
962 size_t size = type_memsize(type, array, &align);
963 if (size < USHRT_MAX)
964 {
965 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
966 /* alignment */
967 print_file(file, 2, "0x%02x,\n", align - 1);
968 /* size */
969 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
970 *typestring_offset += 4;
971 }
972 else
973 {
974 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
975 /* alignment */
976 print_file(file, 2, "0x%02x,\n", align - 1);
977 /* size */
978 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
979 *typestring_offset += 6;
980 }
981
982 if (has_pointer)
983 {
984 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
985 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
986 *typestring_offset += 2;
987 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
988 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
989 *typestring_offset += 1;
990 }
991
992 if (!write_base_type( file, type, typestring_offset ))
993 {
994 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
995 *typestring_offset += 1;
996 }
997 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
998 *typestring_offset += 1;
999
1000 return start_offset;
1001 }
1002 else if (has_length && !has_size)
1003 {
1004 /* varying array */
1005 unsigned int align = 0;
1006 size_t element_size = type_memsize(type, NULL, &align);
1007 size_t elements = dim->cval;
1008 size_t total_size = element_size * elements;
1009
1010 if (total_size < USHRT_MAX)
1011 {
1012 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
1013 /* alignment */
1014 print_file(file, 2, "0x%02x,\n", align - 1);
1015 /* total size */
1016 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
1017 /* number of elements */
1018 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
1019 *typestring_offset += 6;
1020 }
1021 else
1022 {
1023 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
1024 /* alignment */
1025 print_file(file, 2, "0x%02x,\n", align - 1);
1026 /* total size */
1027 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
1028 /* number of elements */
1029 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
1030 *typestring_offset += 10;
1031 }
1032 /* element size */
1033 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1034 *typestring_offset += 2;
1035
1036 *typestring_offset += write_conf_or_var_desc(file, current_func,
1037 current_structure,
1038 length_is);
1039
1040 if (has_pointer)
1041 {
1042 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1043 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1044 *typestring_offset += 2;
1045 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1046 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1047 *typestring_offset += 1;
1048 }
1049
1050 if (!write_base_type( file, type, typestring_offset ))
1051 {
1052 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1053 *typestring_offset += 1;
1054 }
1055 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1056 *typestring_offset += 1;
1057
1058 return start_offset;
1059 }
1060 else if (!has_length && has_size)
1061 {
1062 /* conformant array */
1063 unsigned int align = 0;
1064 size_t element_size = type_memsize(type, NULL, &align);
1065
1066 WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
1067 /* alignment */
1068 print_file(file, 2, "0x%02x,\n", align - 1);
1069 /* element size */
1070 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1071 *typestring_offset += 4;
1072
1073 *typestring_offset += write_conf_or_var_desc(file, current_func,
1074 current_structure,
1075 size_is ? size_is : array);
1076
1077 if (has_pointer)
1078 {
1079 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1080 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1081 *typestring_offset += 2;
1082 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1083 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1084 *typestring_offset += 1;
1085 }
1086
1087 if (!write_base_type( file, type, typestring_offset ))
1088 {
1089 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1090 *typestring_offset += 1;
1091 }
1092 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1093 *typestring_offset += 1;
1094
1095 return start_offset;
1096 }
1097 else
1098 {
1099 /* conformant varying array */
1100 unsigned int align = 0;
1101 size_t element_size = type_memsize(type, NULL, &align);
1102
1103 WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
1104 /* alignment */
1105 print_file(file, 2, "0x%02x,\n", align - 1);
1106 /* element size */
1107 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
1108 *typestring_offset += 4;
1109
1110 *typestring_offset += write_conf_or_var_desc(file, current_func,
1111 current_structure,
1112 size_is ? size_is : array);
1113 *typestring_offset += write_conf_or_var_desc(file, current_func,
1114 current_structure,
1115 length_is);
1116
1117 if (has_pointer)
1118 {
1119 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1120 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1121 *typestring_offset += 2;
1122 write_pointer_description(file, attrs, type, 0, array, 0, typestring_offset);
1123 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1124 *typestring_offset += 1;
1125 }
1126
1127 if (!write_base_type( file, type, typestring_offset ))
1128 {
1129 print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
1130 *typestring_offset += 1;
1131 }
1132 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1133 *typestring_offset += 1;
1134
1135 return start_offset;
1136 }
1137 }
1138 }
1139
1140 static const var_t *find_array_or_string_in_struct(const type_t *type)
1141 {
1142 const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1143
1144 if (is_array_type(last_field->attrs, last_field->type, last_field->array))
1145 return last_field;
1146
1147 assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1148 (last_field->type->type == RPC_FC_CPSTRUCT) ||
1149 (last_field->type->type == RPC_FC_CVSTRUCT));
1150
1151 return find_array_or_string_in_struct(last_field->type);
1152 }
1153
1154 static void write_struct_members(FILE *file, const type_t *type, unsigned int *typestring_offset)
1155 {
1156 const var_t *field;
1157
1158 if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1159 {
1160 unsigned char rtype = field->type->type;
1161
1162 if (field->array)
1163 write_array_tfs( file, field->attrs, field->type, field->array,
1164 field->name, typestring_offset );
1165 else if (is_ptr( field->type ))
1166 {
1167 /* pointers are handled in detail earlier, here just treat them like longs */
1168 print_file( file, 2, "0x8,\t/* FC_LONG */\n" );
1169 *typestring_offset += 1;
1170 }
1171 else if (!write_base_type( file, field->type, typestring_offset ))
1172 error("Unsupported member type 0x%x\n", rtype);
1173 }
1174
1175 if (!(*typestring_offset % 2))
1176 {
1177 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1178 *typestring_offset += 1;
1179 }
1180
1181 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1182 *typestring_offset += 1;
1183 }
1184
1185 static size_t write_struct_tfs(FILE *file, type_t *type,
1186 const char *name, unsigned int *typestring_offset)
1187 {
1188 unsigned int total_size;
1189 const var_t *array;
1190 size_t start_offset;
1191 size_t array_offset;
1192 int has_pointers;
1193 unsigned int align = 0;
1194
1195 switch (type->type)
1196 {
1197 case RPC_FC_STRUCT:
1198 case RPC_FC_PSTRUCT:
1199 total_size = type_memsize(type, NULL, &align);
1200
1201 if (total_size > USHRT_MAX)
1202 error("structure size for %s exceeds %d bytes by %d bytes\n",
1203 name, USHRT_MAX, total_size - USHRT_MAX);
1204
1205 if (type->type == RPC_FC_PSTRUCT)
1206 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1207
1208 start_offset = *typestring_offset;
1209 type->typestring_offset = start_offset;
1210 if (type->type == RPC_FC_STRUCT)
1211 WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1212 else
1213 WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1214 /* alignment */
1215 print_file(file, 2, "0x%02x,\n", align - 1);
1216 /* total size */
1217 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1218 *typestring_offset += 4;
1219
1220 if (type->type == RPC_FC_PSTRUCT)
1221 {
1222 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1223 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1224 *typestring_offset += 2;
1225 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1226 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1227 *typestring_offset += 1;
1228 }
1229
1230 /* member layout */
1231 write_struct_members(file, type, typestring_offset);
1232 return start_offset;
1233 case RPC_FC_CSTRUCT:
1234 case RPC_FC_CPSTRUCT:
1235 total_size = type_memsize(type, NULL, &align);
1236
1237 if (total_size > USHRT_MAX)
1238 error("structure size for %s exceeds %d bytes by %d bytes\n",
1239 name, USHRT_MAX, total_size - USHRT_MAX);
1240
1241 array = find_array_or_string_in_struct(type);
1242 current_structure = type;
1243 array_offset = write_array_tfs(file, array->attrs, array->type,
1244 array->array, array->name,
1245 typestring_offset);
1246 current_structure = NULL;
1247
1248 if (type->type == RPC_FC_CPSTRUCT)
1249 write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1250
1251 start_offset = *typestring_offset;
1252 type->typestring_offset = start_offset;
1253 if (type->type == RPC_FC_CSTRUCT)
1254 WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1255 else
1256 WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1257 /* alignment */
1258 print_file(file, 2, "0x%02x,\n", align - 1);
1259 /* total size */
1260 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1261 *typestring_offset += 4;
1262 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1263 array_offset - *typestring_offset,
1264 array_offset - *typestring_offset,
1265 array_offset);
1266 *typestring_offset += 2;
1267
1268 if (type->type == RPC_FC_CPSTRUCT)
1269 {
1270 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1271 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1272 *typestring_offset += 2;
1273 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1274 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1275 *typestring_offset += 1;
1276 }
1277
1278 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1279 *typestring_offset += 1;
1280
1281 return start_offset;
1282 case RPC_FC_CVSTRUCT:
1283 total_size = type_memsize(type, NULL, &align);
1284
1285 if (total_size > USHRT_MAX)
1286 error("structure size for %s exceeds %d bytes by %d bytes\n",
1287 name, USHRT_MAX, total_size - USHRT_MAX);
1288
1289 array = find_array_or_string_in_struct(type);
1290 current_structure = type;
1291 if (is_attr(array->attrs, ATTR_STRING))
1292 array_offset = write_string_tfs(file, array->attrs, array->type,
1293 array->array, array->name,
1294 typestring_offset);
1295 else
1296 array_offset = write_array_tfs(file, array->attrs, array->type,
1297 array->array, array->name,
1298 typestring_offset);
1299 current_structure = NULL;
1300
1301 has_pointers = write_pointers(file, NULL, type, name, NULL, 0, typestring_offset);
1302
1303 start_offset = *typestring_offset;
1304 type->typestring_offset = start_offset;
1305 WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1306 /* alignment */
1307 print_file(file, 2, "0x%02x,\n", align - 1);
1308 /* total size */
1309 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1310 *typestring_offset += 4;
1311 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1312 array_offset - *typestring_offset,
1313 array_offset - *typestring_offset,
1314 array_offset);
1315 *typestring_offset += 2;
1316
1317 if (has_pointers)
1318 {
1319 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1320 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1321 *typestring_offset += 2;
1322 write_pointer_description(file, NULL, type, 0, NULL, 0, typestring_offset);
1323 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1324 *typestring_offset += 1;
1325 }
1326
1327 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1328 *typestring_offset += 1;
1329
1330 return start_offset;
1331 default:
1332 error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1333 return *typestring_offset;
1334 }
1335 }
1336
1337 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1338 unsigned char flags, size_t offset,
1339 unsigned int *typeformat_offset)
1340 {
1341 size_t start_offset = *typeformat_offset;
1342 short reloff = offset - (*typeformat_offset + 2);
1343 int in_attr, out_attr;
1344 in_attr = is_attr(attrs, ATTR_IN);
1345 out_attr = is_attr(attrs, ATTR_OUT);
1346 if (!in_attr && !out_attr) in_attr = 1;
1347
1348 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1349 flags |= 0x04;
1350
1351 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1352 pointer_type,
1353 flags,
1354 string_of_type(pointer_type));
1355 if (file)
1356 {
1357 if (flags & 0x04)
1358 fprintf(file, " [allocated_on_stack]");
1359 if (flags & 0x10)
1360 fprintf(file, " [pointer_deref]");
1361 fprintf(file, " */\n");
1362 }
1363
1364 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1365 *typeformat_offset += 4;
1366
1367 return start_offset;
1368 }
1369
1370 static size_t write_union_tfs(FILE *file, const attr_list_t *attrs,
1371 const type_t *type, const char *name,
1372 unsigned int *typeformat_offset)
1373 {
1374 error("write_union_tfs: Unimplemented\n");
1375 return *typeformat_offset;
1376 }
1377
1378 static size_t write_ip_tfs(FILE *file, const func_t *func, const type_t *type, const var_t *var,
1379 unsigned int *typeformat_offset)
1380 {
1381 size_t i;
1382 size_t start_offset = *typeformat_offset;
1383 const var_t *iid = get_attrp(var->attrs, ATTR_IIDIS);
1384
1385 if (iid)
1386 {
1387 expr_t expr;
1388 expr_list_t expr_list;
1389
1390 expr.type = EXPR_IDENTIFIER;
1391 expr.ref = NULL;
1392 expr.u.sval = iid->name;
1393 expr.is_const = FALSE;
1394 list_init( &expr_list );
1395 list_add_head( &expr_list, &expr.entry );
1396 print_file(file, 2, "0x2f, /* FC_IP */\n");
1397 print_file(file, 2, "0x5c, /* FC_PAD */\n");
1398 *typeformat_offset += write_conf_or_var_desc(file, func, NULL, &expr_list) + 2;
1399 }
1400 else
1401 {
1402 const type_t *base = is_ptr(type) ? type->ref : type;
1403 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1404
1405 if (! uuid)
1406 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1407
1408 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1409 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1410 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1411 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1412 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1413 for (i = 0; i < 8; ++i)
1414 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1415
1416 if (file)
1417 fprintf(file, "\n");
1418
1419 *typeformat_offset += 18;
1420 }
1421 return start_offset;
1422 }
1423
1424 static int get_ptr_attr(const type_t *t, int def_type)
1425 {
1426 while (TRUE)
1427 {
1428 int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1429 if (ptr_attr)
1430 return ptr_attr;
1431 if (t->kind != TKIND_ALIAS)
1432 return def_type;
1433 t = t->orig;
1434 }
1435 }
1436
1437 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1438 type_t *type, const var_t *var,
1439 unsigned int *typeformat_offset)
1440 {
1441 int pointer_type;
1442 size_t offset;
1443
1444 if (type == var->type) /* top-level pointers */
1445 {
1446 int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1447 if (pointer_attr != 0 && !is_ptr(type))
1448 error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1449
1450 if (pointer_attr == 0)
1451 pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1452
1453 pointer_type = pointer_attr;
1454 }
1455 else
1456 pointer_type = get_ptr_attr(type, RPC_FC_UP);
1457
1458 if (((last_ptr(type) && var->array == NULL)
1459 || (!is_ptr(type) && var->array != NULL))
1460 && is_ptrchain_attr(var, ATTR_STRING))
1461 {
1462 return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1463 }
1464
1465 if (is_array_type(var->attrs, type, var->array))
1466 return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1467
1468 if (!is_ptr(type))
1469 {
1470 /* basic types don't need a type format string */
1471 if (is_base_type(type->type))
1472 return 0;
1473
1474 switch (type->type)
1475 {
1476 case RPC_FC_STRUCT:
1477 case RPC_FC_PSTRUCT:
1478 case RPC_FC_CSTRUCT:
1479 case RPC_FC_CPSTRUCT:
1480 case RPC_FC_CVSTRUCT:
1481 case RPC_FC_BOGUS_STRUCT:
1482 return write_struct_tfs(file, type, var->name, typeformat_offset);
1483 case RPC_FC_ENCAPSULATED_UNION:
1484 case RPC_FC_NON_ENCAPSULATED_UNION:
1485 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1486 case RPC_FC_IGNORE:
1487 case RPC_FC_BIND_PRIMITIVE:
1488 /* nothing to do */
1489 return 0;
1490 default:
1491 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1492 }
1493 }
1494 else if (last_ptr(type))
1495 {
1496 size_t start_offset = *typeformat_offset;
1497 int in_attr = is_attr(var->attrs, ATTR_IN);
1498 int out_attr = is_attr(var->attrs, ATTR_OUT);
1499 const type_t *base = type->ref;
1500
1501 if (base->type == RPC_FC_IP)
1502 {
1503 return write_ip_tfs(file, func, type, var, typeformat_offset);
1504 }
1505
1506 /* special case for pointers to base types */
1507 if (is_base_type(base->type))
1508 {
1509 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1510 pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1511 string_of_type(pointer_type),
1512 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1513 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
1514 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1515 *typeformat_offset += 4;
1516 return start_offset;
1517 }
1518 }
1519
1520 assert(is_ptr(type));
1521
1522 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1523 if (file)
1524 fprintf(file, "/* %2u */\n", *typeformat_offset);
1525 return write_pointer_only_tfs(file, var->attrs, pointer_type,
1526 !last_ptr(type) ? 0x10 : 0,
1527 offset, typeformat_offset);
1528 }
1529
1530 static void clear_tfsoff(type_t *type)
1531 {
1532 for (;;)
1533 {
1534 type->typestring_offset = 0;
1535
1536 if (type->kind == TKIND_ALIAS)
1537 type = type->orig;
1538 else if (is_ptr(type))
1539 type = type->ref;
1540 else
1541 {
1542 if (type->fields)
1543 {
1544 var_t *v;
1545 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1546 clear_tfsoff(v->type);
1547 }
1548
1549 return;
1550 }
1551 }
1552 }
1553
1554 static void clear_all_tfsoffs(const ifref_list_t *ifaces)
1555 {
1556 const ifref_t * iface;
1557 const func_t *func;
1558 const var_t *var;
1559
1560 if (ifaces)
1561 LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1562 if (iface->iface->funcs)
1563 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1564 if (func->args)
1565 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1566 clear_tfsoff(var->type);
1567 }
1568
1569 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
1570 {
1571 const var_t *var;
1572 const ifref_t *iface;
1573 size_t typeformat_offset = 2;
1574
1575 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
1576 {
1577 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1578 continue;
1579
1580 if (iface->iface->funcs)
1581 {
1582 const func_t *func;
1583 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
1584 {
1585 if (is_local(func->def->attrs)) continue;
1586
1587 current_func = func;
1588 if (func->args)
1589 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1590 var->type->typestring_offset
1591 = write_typeformatstring_var(file, 2, func, var->type,
1592 var, &typeformat_offset);
1593 }
1594 }
1595 }
1596
1597 return typeformat_offset + 1;
1598 }
1599
1600
1601 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
1602 {
1603 int indent = 0;
1604
1605 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1606 print_file(file, indent, "{\n");
1607 indent++;
1608 print_file(file, indent, "0,\n");
1609 print_file(file, indent, "{\n");
1610 indent++;
1611 print_file(file, indent, "NdrFcShort(0x0),\n");
1612
1613 clear_all_tfsoffs(ifaces);
1614 process_tfs(file, ifaces, for_objects);
1615
1616 print_file(file, indent, "0x0\n");
1617 indent--;
1618 print_file(file, indent, "}\n");
1619 indent--;
1620 print_file(file, indent, "};\n");
1621 print_file(file, indent, "\n");
1622 }
1623
1624 static unsigned int get_required_buffer_size_type(
1625 const type_t *type, const array_dims_t *array,
1626 const char *name, unsigned int *alignment)
1627 {
1628 size_t size = 0;
1629
1630 *alignment = 0;
1631 if (!is_ptr(type))
1632 {
1633 switch (type->type)
1634 {
1635 case RPC_FC_BYTE:
1636 case RPC_FC_CHAR:
1637 case RPC_FC_USMALL:
1638 case RPC_FC_SMALL:
1639 *alignment = 4;
1640 size = 1;
1641 break;
1642
1643 case RPC_FC_WCHAR:
1644 case RPC_FC_USHORT:
1645 case RPC_FC_SHORT:
1646 *alignment = 4;
1647 size = 2;
1648 break;
1649
1650 case RPC_FC_ULONG:
1651 case RPC_FC_LONG:
1652 case RPC_FC_FLOAT:
1653 case RPC_FC_ERROR_STATUS_T:
1654 *alignment = 4;
1655 size = 4;
1656 break;
1657
1658 case RPC_FC_HYPER:
1659 case RPC_FC_DOUBLE:
1660 *alignment = 8;
1661 size = 8;
1662 break;
1663
1664 case RPC_FC_IGNORE:
1665 case RPC_FC_BIND_PRIMITIVE:
1666 return 0;
1667
1668 case RPC_FC_STRUCT:
1669 case RPC_FC_PSTRUCT:
1670 {
1671 const var_t *field;
1672 if (!type->fields) return 0;
1673 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1674 {
1675 unsigned int alignment;
1676 size += get_required_buffer_size_type(
1677 field->type, field->array, field->name,
1678 &alignment);
1679 }
1680 break;
1681 }
1682
1683 case RPC_FC_RP:
1684 if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
1685 size = get_required_buffer_size_type( type->ref, NULL, name, alignment );
1686 break;
1687
1688 default:
1689 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1690 return 0;
1691 }
1692 if (array) size *= get_array_size( array );
1693 }
1694 return size;
1695 }
1696
1697 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1698 {
1699 expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1700 int has_size = is_non_void(size_is);
1701 int in_attr = is_attr(var->attrs, ATTR_IN);
1702 int out_attr = is_attr(var->attrs, ATTR_OUT);
1703
1704 if (!in_attr && !out_attr)
1705 in_attr = 1;
1706
1707 *alignment = 0;
1708
1709 if (pass == PASS_OUT)
1710 {
1711 if (out_attr && is_ptr(var->type))
1712 {
1713 type_t *type = var->type;
1714
1715 if (type->type == RPC_FC_STRUCT)
1716 {
1717 const var_t *field;
1718 unsigned int size = 36;
1719
1720 if (!type->fields) return size;
1721 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1722 {
1723 unsigned int align;
1724 size += get_required_buffer_size_type(
1725 field->type, field->array, field->name,
1726 &align);
1727 }
1728 return size;
1729 }
1730 }
1731 return 0;
1732 }
1733 else
1734 {
1735 if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1736 {
1737 if (is_ptr(var->type))
1738 {
1739 type_t *type = var->type;
1740
1741 if (is_base_type(type->type))
1742 {
1743 return 25;
1744 }
1745 else if (type->type == RPC_FC_STRUCT)
1746 {
1747 unsigned int size = 36;
1748 const var_t *field;
1749
1750 if (!type->fields) return size;
1751 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1752 {
1753 unsigned int align;
1754 size += get_required_buffer_size_type(
1755 field->type, field->array, field->name,
1756 &align);
1757 }
1758 return size;
1759 }
1760 }
1761 }
1762
1763 return get_required_buffer_size_type(var->type, var->array, var->name, alignment);
1764 }
1765 }
1766
1767 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
1768 {
1769 const var_t *var;
1770 unsigned int total_size = 0, alignment;
1771
1772 if (func->args)
1773 {
1774 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1775 {
1776 total_size += get_required_buffer_size(var, &alignment, pass);
1777 total_size += alignment;
1778 }
1779 }
1780
1781 if (pass == PASS_OUT && !is_void(func->def->type))
1782 {
1783 total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
1784 total_size += alignment;
1785 }
1786 return total_size;
1787 }
1788
1789 static void print_phase_function(FILE *file, int indent, const char *type,
1790 enum remoting_phase phase,
1791 const char *varname, unsigned int type_offset)
1792 {
1793 const char *function;
1794 switch (phase)
1795 {
1796 case PHASE_BUFFERSIZE:
1797 function = "BufferSize";
1798 break;
1799 case PHASE_MARSHAL:
1800 function = "Marshall";
1801 break;
1802 case PHASE_UNMARSHAL:
1803 function = "Unmarshall";
1804 break;
1805 case PHASE_FREE:
1806 function = "Free";
1807 break;
1808 default:
1809 assert(0);
1810 return;
1811 }
1812
1813 print_file(file, indent, "Ndr%s%s(\n", type, function);
1814 indent++;
1815 print_file(file, indent, "&_StubMsg,\n");
1816 print_file(file, indent, "%s%s,\n",
1817 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1818 varname);
1819 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1820 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1821 if (phase == PHASE_UNMARSHAL)
1822 print_file(file, indent, "0);\n");
1823 indent--;
1824 }
1825
1826 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1827 enum pass pass, const var_t *var,
1828 const char *varname)
1829 {
1830 type_t *type = var->type;
1831 unsigned int size;
1832 unsigned int alignment = 0;
1833 unsigned char rtype;
1834
1835 /* no work to do for other phases, buffer sizing is done elsewhere */
1836 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1837 return;
1838
1839 rtype = is_ptr(type) ? type->ref->type : type->type;
1840
1841 switch (rtype)
1842 {
1843 case RPC_FC_BYTE:
1844 case RPC_FC_CHAR:
1845 case RPC_FC_SMALL:
1846 case RPC_FC_USMALL:
1847 size = 1;
1848 alignment = 1;
1849 break;
1850
1851 case RPC_FC_WCHAR:
1852 case RPC_FC_USHORT:
1853 case RPC_FC_SHORT:
1854 size = 2;
1855 alignment = 2;
1856 break;
1857
1858 case RPC_FC_ULONG:
1859 case RPC_FC_LONG:
1860 case RPC_FC_FLOAT:
1861 case RPC_FC_ERROR_STATUS_T:
1862 size = 4;
1863 alignment = 4;
1864 break;
1865
1866 case RPC_FC_HYPER:
1867 case RPC_FC_DOUBLE:
1868 size = 8;
1869 alignment = 8;
1870 break;
1871
1872 case RPC_FC_IGNORE:
1873 case RPC_FC_BIND_PRIMITIVE:
1874 /* no marshalling needed */
1875 return;
1876
1877 default:
1878 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1879 size = 0;
1880 }
1881
1882 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1883 alignment - 1, alignment - 1);
1884
1885 if (phase == PHASE_MARSHAL)
1886 {
1887 print_file(file, indent, "*(");
1888 write_type(file, is_ptr(type) ? type->ref : type);
1889 if (is_ptr(type))
1890 fprintf(file, " *)_StubMsg.Buffer = *");
1891 else
1892 fprintf(file, " *)_StubMsg.Buffer = ");
1893 fprintf(file, varname);
1894 fprintf(file, ";\n");
1895 }
1896 else if (phase == PHASE_UNMARSHAL)
1897 {
1898 if (pass == PASS_IN || pass == PASS_RETURN)
1899 print_file(file, indent, "");
1900 else
1901 print_file(file, indent, "*");
1902 fprintf(file, varname);
1903 if (pass == PASS_IN && is_ptr(type))
1904 fprintf(file, " = (");
1905 else
1906 fprintf(file, " = *(");
1907 write_type(file, is_ptr(type) ? type->ref : type);
1908 fprintf(file, " *)_StubMsg.Buffer;\n");
1909 }
1910
1911 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1912 write_type(file, var->type);
1913 fprintf(file, ");\n");
1914 }
1915
1916 /* returns whether the MaxCount, Offset or ActualCount members need to be
1917 * filled in for the specified phase */
1918 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1919 {
1920 return (phase != PHASE_UNMARSHAL);
1921 }
1922
1923 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1924 enum pass pass, enum remoting_phase phase)
1925 {
1926 const expr_list_t *length_is;
1927 const expr_list_t *size_is;
1928 int in_attr, out_attr, has_length, has_size, pointer_type;
1929 const var_t *var;
1930
1931 if (!func->args)
1932 return;
1933
1934 if (phase == PHASE_BUFFERSIZE)
1935 {
1936 unsigned int size = get_function_buffer_size( func, pass );
1937 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
1938 }
1939
1940 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1941 {
1942 const type_t *type = var->type;
1943 unsigned char rtype;
1944 size_t start_offset = type->typestring_offset;
1945
1946 length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1947 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1948 has_length = is_non_void(length_is);
1949 has_size = is_non_void(size_is) || (var->array && is_conformant_array(var->array));
1950
1951 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1952 if (!pointer_type)
1953 pointer_type = RPC_FC_RP;
1954
1955 in_attr = is_attr(var->attrs, ATTR_IN);
1956 out_attr = is_attr(var->attrs, ATTR_OUT);
1957 if (!in_attr && !out_attr)
1958 in_attr = 1;
1959
1960 switch (pass)
1961 {
1962 case PASS_IN:
1963 if (!in_attr) continue;
1964 break;
1965 case PASS_OUT:
1966 if (!out_attr) continue;
1967 break;
1968 case PASS_RETURN:
1969 break;
1970 }
1971
1972 rtype = type->type;
1973
1974 if (is_user_derived( var ))
1975 {
1976 print_phase_function(file, indent, "UserMarshal", phase, var->name, start_offset);
1977 }
1978 else if (is_string_type(var->attrs, var->type, var->array))
1979 {
1980 if (var->array && !is_conformant_array(var->array))
1981 print_phase_function(file, indent, "NonConformantString", phase, var->name, start_offset);
1982 else
1983 {
1984 if (size_is && is_size_needed_for_phase(phase))
1985 {
1986 const expr_t *size = LIST_ENTRY( list_head(size_is), const expr_t, entry );
1987 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1988 write_expr(file, size, 1);
1989 fprintf(file, ";\n");
1990 }
1991
1992 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1993 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
1994 else
1995 print_phase_function(file, indent, "ConformantString", phase, var->name,
1996 start_offset + (has_size ? 4 : 2));
1997 }
1998 }
1999 else if (is_array_type(var->attrs, var->type, var->array))
2000 {
2001 const char *array_type;
2002
2003 if (var->array && list_count(var->array) > 1) /* multi-dimensional array */
2004 array_type = "ComplexArray";
2005 else
2006 {
2007 if (!has_length && !has_size)
2008 array_type = "FixedArray";
2009 else if (has_length && !has_size)
2010 {
2011 if (is_size_needed_for_phase(phase))
2012 {
2013 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2014 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2015 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2016 write_expr(file, length, 1);
2017 fprintf(file, ";\n\n");
2018 }
2019 array_type = "VaryingArray";
2020 }
2021 else if (!has_length && has_size)
2022 {
2023 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2024 {
2025 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2026 const expr_t, entry );
2027 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2028 write_expr(file, size, 1);
2029 fprintf(file, ";\n\n");
2030 }
2031 array_type = "ConformantArray";
2032 }
2033 else
2034 {
2035 if (is_size_needed_for_phase(phase))
2036 {
2037 const expr_t *length = LIST_ENTRY( list_head(length_is), const expr_t, entry );
2038 const expr_t *size = LIST_ENTRY( list_head(size_is ? size_is : var->array),
2039 const expr_t, entry );
2040 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2041 write_expr(file, size, 1);
2042 fprintf(file, ";\n");
2043 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2044 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2045 write_expr(file, length, 1);
2046 fprintf(file, ";\n\n");
2047 }
2048 array_type = "ConformantVaryingArray";
2049 }
2050 }
2051
2052 if (!in_attr && phase == PHASE_FREE)
2053 {
2054 print_file(file, indent, "if (%s)\n", var->name);
2055 indent++;
2056 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2057 }
2058 else if (phase != PHASE_FREE)
2059 {
2060 if (pointer_type == RPC_FC_UP)
2061 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2062 else
2063 print_phase_function(file, indent, array_type, phase, var->name, start_offset);
2064 }
2065 }
2066 else if (!is_ptr(var->type) && is_base_type(rtype))
2067 {
2068 print_phase_basetype(file, indent, phase, pass, var, var->name);
2069 }
2070 else if (!is_ptr(var->type))
2071 {
2072 switch (rtype)
2073 {
2074 case RPC_FC_STRUCT:
2075 case RPC_FC_PSTRUCT:
2076 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset);
2077 break;
2078 case RPC_FC_CSTRUCT:
2079 case RPC_FC_CPSTRUCT:
2080 print_phase_function(file, indent, "ConformantStruct", phase, var->name, start_offset);
2081 break;
2082 case RPC_FC_CVSTRUCT:
2083 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var->name, start_offset);
2084 break;
2085 case RPC_FC_BOGUS_STRUCT:
2086 print_phase_function(file, indent, "ComplexStruct", phase, var->name, start_offset);
2087 break;
2088 case RPC_FC_RP:
2089 if (is_base_type( var->type->ref->type ))
2090 {
2091 print_phase_basetype(file, indent, phase, pass, var, var->name);
2092 }
2093 else if (var->type->ref->type == RPC_FC_STRUCT)
2094 {
2095 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2096 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2097 }
2098 else
2099 {
2100 const var_t *iid;
2101 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2102 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2103 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2104 }
2105 break;
2106 default:
2107 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2108 }
2109 }
2110 else
2111 {
2112 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2113 {
2114 print_phase_basetype(file, indent, phase, pass, var, var->name);
2115 }
2116 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2117 {
2118 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2119 print_phase_function(file, indent, "SimpleStruct", phase, var->name, start_offset + 4);
2120 }
2121 else
2122 {
2123 const var_t *iid;
2124 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2125 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2126 print_phase_function(file, indent, "Pointer", phase, var->name, start_offset);
2127 }
2128 }
2129 fprintf(file, "\n");
2130 }
2131 }
2132
2133
2134 size_t get_size_procformatstring_var(const var_t *var)
2135 {
2136 return write_procformatstring_var(NULL, 0, var, FALSE);
2137 }
2138
2139
2140 size_t get_size_procformatstring_func(const func_t *func)
2141 {
2142 const var_t *var;
2143 size_t size = 0;
2144
2145 /* argument list size */
2146 if (func->args)
2147 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2148 size += get_size_procformatstring_var(var);
2149
2150 /* return value size */
2151 if (is_void(func->def->type))
2152 size += 2; /* FC_END and FC_PAD */
2153 else
2154 size += get_size_procformatstring_var(func->def);
2155
2156 return size;
2157 }
2158
2159 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2160 {
2161 const ifref_t *iface;
2162 size_t size = 1;
2163 const func_t *func;
2164
2165 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2166 {
2167 if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2168 continue;
2169
2170 if (iface->iface->funcs)
2171 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2172 if (!is_local(func->def->attrs))
2173 size += get_size_procformatstring_func( func );
2174 }
2175 return size;
2176 }
2177
2178 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2179 {
2180 return process_tfs(NULL, ifaces, for_objects);
2181 }
2182
2183 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2184 const var_list_t *fields, const char *structvar)
2185 {
2186 switch (e->type) {
2187 case EXPR_VOID:
2188 break;
2189 case EXPR_NUM:
2190 fprintf(h, "%lu", e->u.lval);
2191 break;
2192 case EXPR_HEXNUM:
2193 fprintf(h, "0x%lx", e->u.lval);
2194 break;
2195 case EXPR_TRUEFALSE:
2196 if (e->u.lval == 0)
2197 fprintf(h, "FALSE");
2198 else
2199 fprintf(h, "TRUE");
2200 break;
2201 case EXPR_IDENTIFIER:
2202 {
2203 const var_t *field;
2204 LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2205 if (!strcmp(e->u.sval, field->name))
2206 {
2207 fprintf(h, "%s->%s", structvar, e->u.sval);
2208 break;
2209 }
2210
2211 if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2212 break;
2213 }
2214 case EXPR_NEG:
2215 fprintf(h, "-");
2216 write_struct_expr(h, e->ref, 1, fields, structvar);
2217 break;
2218 case EXPR_NOT:
2219 fprintf(h, "~");
2220 write_struct_expr(h, e->ref, 1, fields, structvar);
2221 break;
2222 case EXPR_PPTR:
2223 fprintf(h, "*");
2224 write_struct_expr(h, e->ref, 1, fields, structvar);
2225 break;
2226 case EXPR_CAST:
2227 fprintf(h, "(");
2228 write_type(h, e->u.tref);
2229 fprintf(h, ")");
2230 write_struct_expr(h, e->ref, 1, fields, structvar);
2231 break;
2232 case EXPR_SIZEOF:
2233 fprintf(h, "sizeof(");
2234 write_type(h, e->u.tref);
2235 fprintf(h, ")");
2236 break;
2237 case EXPR_SHL:
2238 case EXPR_SHR:
2239 case EXPR_MUL:
2240 case EXPR_DIV:
2241 case EXPR_ADD:
2242 case EXPR_SUB:
2243 case EXPR_AND:
2244 case EXPR_OR:
2245 if (brackets) fprintf(h, "(");
2246 write_struct_expr(h, e->ref, 1, fields, structvar);
2247 switch (e->type) {
2248 case EXPR_SHL: fprintf(h, " << "); break;
2249 case EXPR_SHR: fprintf(h, " >> "); break;
2250 case EXPR_MUL: fprintf(h, " * "); break;
2251 case EXPR_DIV: fprintf(h, " / "); break;
2252 case EXPR_ADD: fprintf(h, " + "); break;
2253 case EXPR_SUB: fprintf(h, " - "); break;
2254 case EXPR_AND: fprintf(h, " & "); break;
2255 case EXPR_OR: fprintf(h, " | "); break;
2256 default: break;
2257 }
2258 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2259 if (brackets) fprintf(h, ")");
2260 break;
2261 case EXPR_COND:
2262 if (brackets) fprintf(h, "(");
2263 write_struct_expr(h, e->ref, 1, fields, structvar);
2264 fprintf(h, " ? ");
2265 write_struct_expr(h, e->u.ext, 1, fields, structvar);
2266 fprintf(h, " : ");
2267 write_struct_expr(h, e->ext2, 1, fields, structvar);
2268 if (brackets) fprintf(h, ")");
2269 break;
2270 }
2271 }
2272
2273
2274 void declare_stub_args( FILE *file, int indent, const func_t *func )
2275 {
2276 int in_attr, out_attr;
2277 int i = 0;
2278 const var_t *def = func->def;
2279 const var_t *var;
2280
2281 /* declare return value '_RetVal' */
2282 if (!is_void(def->type))
2283 {
2284 print_file(file, indent, "");
2285 write_type(file, def->type);
2286 fprintf(file, " _RetVal;\n");
2287 }
2288
2289 if (!func->args)
2290 return;
2291
2292 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2293 {
2294 const expr_list_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2295 int has_size = is_non_void(size_is);
2296 int is_string = is_attr(var->attrs, ATTR_STRING);
2297
2298 in_attr = is_attr(var->attrs, ATTR_IN);
2299 out_attr = is_attr(var->attrs, ATTR_OUT);
2300 if (!out_attr && !in_attr)
2301 in_attr = 1;
2302
2303 if (!in_attr && !has_size && !is_string)
2304 {
2305 print_file(file, indent, "");
2306 write_type(file, var->type->ref);
2307 fprintf(file, " _W%u;\n", i++);
2308 }
2309
2310 print_file(file, indent, "");
2311 write_type(file, var->type);
2312 fprintf(file, " ");
2313 if (var->array) {
2314 fprintf(file, "( *");
2315 write_name(file, var);
2316 fprintf(file, " )");
2317 } else
2318 write_name(file, var);
2319 write_array(file, var->array, 0);
2320 fprintf(file, ";\n");
2321 }
2322 }
2323
2324
2325 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2326 {
2327 int in_attr, out_attr;
2328 int i = 0, sep = 0;
2329 const var_t *var;
2330 const expr_list_t *size_is;
2331 int has_size;
2332
2333 if (!func->args)
2334 return;
2335
2336 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2337 {
2338 int is_string = is_attr(var->attrs, ATTR_STRING);
2339 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
2340 has_size = is_non_void(size_is);
2341 in_attr = is_attr(var->attrs, ATTR_IN);
2342 out_attr = is_attr(var->attrs, ATTR_OUT);
2343 if (!out_attr && !in_attr)
2344 in_attr = 1;
2345
2346 if (!in_attr)
2347 {
2348 print_file(file, indent, "");
2349 write_name(file, var);
2350
2351 if (has_size)
2352 {
2353 const expr_t *expr;
2354 unsigned int size, align = 0;
2355 type_t *type = var->type;
2356
2357 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2358 LIST_FOR_EACH_ENTRY( expr, size_is, const expr_t, entry )
2359 {
2360 if (expr->type == EXPR_VOID) continue;
2361 write_expr( file, expr, 1 );
2362 fprintf(file, " * ");
2363 }
2364 size = type_memsize(type, NULL, &align);
2365 fprintf(file, "%u);\n", size);
2366 }
2367 else if (!is_string)
2368 {
2369 fprintf(file, " = &_W%u;\n", i);
2370 if (is_ptr(var->type) && !last_ptr(var->type))
2371 print_file(file, indent, "_W%u = 0;\n", i);
2372 i++;
2373 }
2374
2375 sep = 1;
2376 }
2377 }
2378 if (sep)
2379 fprintf(file, "\n");
2380 }
2381
2382
2383 int write_expr_eval_routines(FILE *file, const char *iface)
2384 {
2385 int result = 0;
2386 struct expr_eval_routine *eval;
2387 unsigned short callback_offset = 0;
2388
2389 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2390 {
2391 int indent = 0;
2392 result = 1;
2393 print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2394 iface, eval->structure->name, callback_offset);
2395 print_file(file, indent, "{\n");
2396 indent++;
2397 print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2398 eval->structure->name, eval->structure->name, eval->structure_size);
2399 fprintf(file, "\n");
2400 print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2401 print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2402 write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2403 fprintf(file, ";\n");
2404 indent--;
2405 print_file(file, indent, "}\n\n");
2406 callback_offset++;
2407 }
2408 return result;
2409 }
2410
2411 void write_expr_eval_routine_list(FILE *file, const char *iface)
2412 {
2413 struct expr_eval_routine *eval;
2414 struct expr_eval_routine *cursor;
2415 unsigned short callback_offset = 0;
2416
2417 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2418 fprintf(file, "{\n");
2419
2420 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2421 {
2422 print_file(file, 1, "%s_%sExprEval_%04u,\n",
2423 iface, eval->structure->name, callback_offset);
2424
2425 callback_offset++;
2426 list_remove(&eval->entry);
2427 free(eval);
2428 }
2429
2430 fprintf(file, "};\n\n");
2431 }
2432
2433
2434 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2435 {
2436 const struct str_list_entry_t *endpoint;
2437 const char *p;
2438
2439 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2440 print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2441 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2442 {
2443 print_file( f, 1, "{ (const unsigned char *)\"" );
2444 for (p = endpoint->str; *p && *p != ':'; p++)
2445 {
2446 if (*p == '"' || *p == '\\') fputc( '\\', f );
2447 fputc( *p, f );
2448 }
2449 if (!*p) goto error;
2450 if (p[1] != '[') goto error;
2451
2452 fprintf( f, "\", (const unsigned char *)\"" );
2453 for (p += 2; *p && *p != ']'; p++)
2454 {
2455 if (*p == '"' || *p == '\\') fputc( '\\', f );
2456 fputc( *p, f );
2457 }
2458 if (*p != ']') goto error;
2459 fprintf( f, "\" },\n" );
2460 }
2461 print_file( f, 0, "};\n\n" );
2462 return;
2463
2464 error:
2465 error("Invalid endpoint syntax '%s'\n", endpoint->str);
2466 }