35a94c954d69c6337ce638afbcb9ecb2a5976d53
[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 <limits.h>
34
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "wine/list.h"
40
41 #include "typegen.h"
42 #include "expr.h"
43
44 static const func_t *current_func;
45 static const type_t *current_structure;
46 static const type_t *current_iface;
47
48 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
49 struct expr_eval_routine
50 {
51 struct list entry;
52 const type_t *structure;
53 unsigned int baseoff;
54 const expr_t *expr;
55 };
56
57 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
58 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
59 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
60 const char *name, int write_ptr, unsigned int *tfsoff);
61 static const var_t *find_array_or_string_in_struct(const type_t *type);
62 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
63 type_t *type,
64 const char *name, unsigned int *typestring_offset);
65
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_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
91 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
92 case RPC_FC_STRUCT: return "FC_STRUCT";
93 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
94 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
95 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
96 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
97 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
98 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
99 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
100 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
101 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
102 case RPC_FC_CARRAY: return "FC_CARRAY";
103 case RPC_FC_CVARRAY: return "FC_CVARRAY";
104 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
105 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
106 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
107 case RPC_FC_POINTER: return "FC_POINTER";
108 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
109 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
110 case RPC_FC_CSTRING: return "FC_CSTRING";
111 case RPC_FC_WSTRING: return "FC_WSTRING";
112 default:
113 error("string_of_type: unknown type 0x%02x\n", type);
114 return NULL;
115 }
116 }
117
118 int is_struct(unsigned char type)
119 {
120 switch (type)
121 {
122 case RPC_FC_STRUCT:
123 case RPC_FC_PSTRUCT:
124 case RPC_FC_CSTRUCT:
125 case RPC_FC_CPSTRUCT:
126 case RPC_FC_CVSTRUCT:
127 case RPC_FC_BOGUS_STRUCT:
128 return 1;
129 default:
130 return 0;
131 }
132 }
133
134 static int is_non_complex_struct(const type_t *type)
135 {
136 switch (type->type)
137 {
138 case RPC_FC_STRUCT:
139 case RPC_FC_PSTRUCT:
140 case RPC_FC_CSTRUCT:
141 case RPC_FC_CPSTRUCT:
142 case RPC_FC_CVSTRUCT:
143 return 1;
144 default:
145 return 0;
146 }
147 }
148
149 int is_union(unsigned char type)
150 {
151 switch (type)
152 {
153 case RPC_FC_ENCAPSULATED_UNION:
154 case RPC_FC_NON_ENCAPSULATED_UNION:
155 return 1;
156 default:
157 return 0;
158 }
159 }
160
161 static int type_has_pointers(const type_t *type)
162 {
163 if (is_user_type(type))
164 return FALSE;
165 else if (is_ptr(type))
166 return TRUE;
167 else if (is_array(type))
168 return type_has_pointers(type->ref);
169 else if (is_struct(type->type))
170 {
171 const var_t *field;
172 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
173 {
174 if (type_has_pointers(field->type))
175 return TRUE;
176 }
177 }
178 else if (is_union(type->type))
179 {
180 var_list_t *fields;
181 const var_t *field;
182 if (type->type == RPC_FC_ENCAPSULATED_UNION)
183 {
184 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
185 fields = uv->type->fields_or_args;
186 }
187 else
188 fields = type->fields_or_args;
189 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
190 {
191 if (field->type && type_has_pointers(field->type))
192 return TRUE;
193 }
194 }
195
196 return FALSE;
197 }
198
199 static int type_has_full_pointer(const type_t *type)
200 {
201 if (is_user_type(type))
202 return FALSE;
203 else if (type->type == RPC_FC_FP)
204 return TRUE;
205 else if (is_ptr(type))
206 return FALSE;
207 else if (is_array(type))
208 return type_has_full_pointer(type->ref);
209 else if (is_struct(type->type))
210 {
211 const var_t *field;
212 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
213 {
214 if (type_has_full_pointer(field->type))
215 return TRUE;
216 }
217 }
218 else if (is_union(type->type))
219 {
220 var_list_t *fields;
221 const var_t *field;
222 if (type->type == RPC_FC_ENCAPSULATED_UNION)
223 {
224 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
225 fields = uv->type->fields_or_args;
226 }
227 else
228 fields = type->fields_or_args;
229 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
230 {
231 if (field->type && type_has_full_pointer(field->type))
232 return TRUE;
233 }
234 }
235
236 return FALSE;
237 }
238
239 static unsigned short user_type_offset(const char *name)
240 {
241 user_type_t *ut;
242 unsigned short off = 0;
243 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
244 {
245 if (strcmp(name, ut->name) == 0)
246 return off;
247 ++off;
248 }
249 error("user_type_offset: couldn't find type (%s)\n", name);
250 return 0;
251 }
252
253 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
254 {
255 type->typestring_offset = offset;
256 if (file) type->tfswrite = FALSE;
257 }
258
259 static void guard_rec(type_t *type)
260 {
261 /* types that contain references to themselves (like a linked list),
262 need to be shielded from infinite recursion when writing embedded
263 types */
264 if (type->typestring_offset)
265 type->tfswrite = FALSE;
266 else
267 type->typestring_offset = 1;
268 }
269
270 static type_t *get_user_type(const type_t *t, const char **pname)
271 {
272 for (;;)
273 {
274 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
275 if (ut)
276 {
277 if (pname)
278 *pname = t->name;
279 return ut;
280 }
281
282 if (t->kind == TKIND_ALIAS)
283 t = t->orig;
284 else
285 return 0;
286 }
287 }
288
289 int is_user_type(const type_t *t)
290 {
291 return get_user_type(t, NULL) != NULL;
292 }
293
294 static int is_embedded_complex(const type_t *type)
295 {
296 unsigned char tc = type->type;
297 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
298 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
299 }
300
301 static const char *get_context_handle_type_name(const type_t *type)
302 {
303 const type_t *t;
304 for (t = type; is_ptr(t); t = t->ref)
305 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
306 return t->name;
307 assert(0);
308 return NULL;
309 }
310
311 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
312 do { \
313 if (file) \
314 fprintf(file, "/* %2u */\n", typestring_offset); \
315 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
316 } \
317 while (0)
318
319 static void print_file(FILE *file, int indent, const char *format, ...)
320 {
321 va_list va;
322 va_start(va, format);
323 print(file, indent, format, va);
324 va_end(va);
325 }
326
327 void print(FILE *file, int indent, const char *format, va_list va)
328 {
329 if (file)
330 {
331 if (format[0] != '\n')
332 while (0 < indent--)
333 fprintf(file, " ");
334 vfprintf(file, format, va);
335 }
336 }
337
338
339 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n)
340 {
341 if (decl_indirect(t))
342 print_file(file, indent, "MIDL_memset(&%s, 0, sizeof(%s));\n", n, n);
343 else if (is_ptr(t) || is_array(t))
344 print_file(file, indent, "%s = 0;\n", n);
345 }
346
347 void write_parameters_init(FILE *file, int indent, const func_t *func)
348 {
349 const var_t *var;
350
351 if (!is_void(get_func_return_type(func)))
352 write_var_init(file, indent, get_func_return_type(func), "_RetVal");
353
354 if (!func->args)
355 return;
356
357 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
358 write_var_init(file, indent, var->type, var->name);
359
360 fprintf(file, "\n");
361 }
362
363 static void write_formatdesc(FILE *f, int indent, const char *str)
364 {
365 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
366 print_file(f, indent, "{\n");
367 print_file(f, indent + 1, "short Pad;\n");
368 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
369 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
370 print_file(f, indent, "\n");
371 }
372
373 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
374 {
375 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
376 get_size_typeformatstring(stmts, pred));
377
378 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
379 get_size_procformatstring(stmts, pred));
380
381 fprintf(f, "\n");
382 write_formatdesc(f, indent, "TYPE");
383 write_formatdesc(f, indent, "PROC");
384 fprintf(f, "\n");
385 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
386 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
387 print_file(f, indent, "\n");
388 }
389
390 static inline int is_base_type(unsigned char type)
391 {
392 switch (type)
393 {
394 case RPC_FC_BYTE:
395 case RPC_FC_CHAR:
396 case RPC_FC_USMALL:
397 case RPC_FC_SMALL:
398 case RPC_FC_WCHAR:
399 case RPC_FC_USHORT:
400 case RPC_FC_SHORT:
401 case RPC_FC_ULONG:
402 case RPC_FC_LONG:
403 case RPC_FC_HYPER:
404 case RPC_FC_IGNORE:
405 case RPC_FC_FLOAT:
406 case RPC_FC_DOUBLE:
407 case RPC_FC_ENUM16:
408 case RPC_FC_ENUM32:
409 case RPC_FC_ERROR_STATUS_T:
410 case RPC_FC_BIND_PRIMITIVE:
411 return TRUE;
412
413 default:
414 return FALSE;
415 }
416 }
417
418 int decl_indirect(const type_t *t)
419 {
420 return is_user_type(t)
421 || (!is_base_type(t->type)
422 && !is_ptr(t)
423 && !is_array(t));
424 }
425
426 static size_t write_procformatstring_type(FILE *file, int indent,
427 const char *name,
428 const type_t *type,
429 const attr_list_t *attrs,
430 int is_return)
431 {
432 size_t size;
433
434 int is_in = is_attr(attrs, ATTR_IN);
435 int is_out = is_attr(attrs, ATTR_OUT);
436
437 if (!is_in && !is_out) is_in = TRUE;
438
439 if (!type->declarray && is_base_type(type->type))
440 {
441 if (is_return)
442 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
443 else
444 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
445
446 if (type->type == RPC_FC_BIND_PRIMITIVE)
447 {
448 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
449 size = 2; /* includes param type prefix */
450 }
451 else if (is_base_type(type->type))
452 {
453 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
454 size = 2; /* includes param type prefix */
455 }
456 else
457 {
458 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
459 size = 0;
460 }
461 }
462 else
463 {
464 if (is_return)
465 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
466 else if (is_in && is_out)
467 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
468 else if (is_out)
469 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
470 else
471 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
472
473 print_file(file, indent, "0x01,\n");
474 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
475 size = 4; /* includes param type prefix */
476 }
477 return size;
478 }
479
480 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
481 {
482 const statement_t *stmt;
483 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
484 {
485 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
486 {
487 const func_t *func;
488 if (!pred(stmt->u.type))
489 continue;
490 if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
491 {
492 if (is_local(func->def->attrs)) continue;
493 /* emit argument data */
494 if (func->args)
495 {
496 const var_t *var;
497 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
498 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
499 }
500
501 /* emit return value data */
502 if (is_void(get_func_return_type(func)))
503 {
504 print_file(file, indent, "0x5b, /* FC_END */\n");
505 print_file(file, indent, "0x5c, /* FC_PAD */\n");
506 }
507 else
508 write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
509 }
510 }
511 else if (stmt->type == STMT_LIBRARY)
512 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
513 }
514 }
515
516 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
517 {
518 int indent = 0;
519
520 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
521 print_file(file, indent, "{\n");
522 indent++;
523 print_file(file, indent, "0,\n");
524 print_file(file, indent, "{\n");
525 indent++;
526
527 write_procformatstring_stmts(file, indent, stmts, pred);
528
529 print_file(file, indent, "0x0\n");
530 indent--;
531 print_file(file, indent, "}\n");
532 indent--;
533 print_file(file, indent, "};\n");
534 print_file(file, indent, "\n");
535 }
536
537 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
538 {
539 if (is_base_type(type->type))
540 {
541 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
542 *typestring_offset += 1;
543 return 1;
544 }
545
546 return 0;
547 }
548
549 /* write conformance / variance descriptor */
550 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
551 unsigned int baseoff, const type_t *type,
552 const expr_t *expr)
553 {
554 unsigned char operator_type = 0;
555 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
556 const char *conftype_string = "";
557 const char *operator_string = "no operators";
558 const expr_t *subexpr;
559
560 if (!expr)
561 {
562 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
563 return 4;
564 }
565
566 if (!structure)
567 {
568 /* Top-level conformance calculations are done inline. */
569 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
570 RPC_FC_TOP_LEVEL_CONFORMANCE);
571 print_file (file, 2, "0x0,\n");
572 print_file (file, 2, "NdrFcShort(0x0),\n");
573 return 4;
574 }
575
576 if (expr->is_const)
577 {
578 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
579 error("write_conf_or_var_desc: constant value %ld is greater than "
580 "the maximum constant size of %d\n", expr->cval,
581 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
582
583 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
584 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
585 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
586 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
587
588 return 4;
589 }
590
591 if (is_ptr(type) || (is_array(type) && !type->declarray))
592 {
593 conftype = RPC_FC_POINTER_CONFORMANCE;
594 conftype_string = "field pointer, ";
595 }
596
597 subexpr = expr;
598 switch (subexpr->type)
599 {
600 case EXPR_PPTR:
601 subexpr = subexpr->ref;
602 operator_type = RPC_FC_DEREFERENCE;
603 operator_string = "FC_DEREFERENCE";
604 break;
605 case EXPR_DIV:
606 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
607 {
608 subexpr = subexpr->ref;
609 operator_type = RPC_FC_DIV_2;
610 operator_string = "FC_DIV_2";
611 }
612 break;
613 case EXPR_MUL:
614 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
615 {
616 subexpr = subexpr->ref;
617 operator_type = RPC_FC_MULT_2;
618 operator_string = "FC_MULT_2";
619 }
620 break;
621 case EXPR_SUB:
622 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
623 {
624 subexpr = subexpr->ref;
625 operator_type = RPC_FC_SUB_1;
626 operator_string = "FC_SUB_1";
627 }
628 break;
629 case EXPR_ADD:
630 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
631 {
632 subexpr = subexpr->ref;
633 operator_type = RPC_FC_ADD_1;
634 operator_string = "FC_ADD_1";
635 }
636 break;
637 default:
638 break;
639 }
640
641 if (subexpr->type == EXPR_IDENTIFIER)
642 {
643 const type_t *correlation_variable = NULL;
644 unsigned char correlation_variable_type;
645 unsigned char param_type = 0;
646 size_t offset = 0;
647 const var_t *var;
648
649 if (structure->fields_or_args) LIST_FOR_EACH_ENTRY( var, structure->fields_or_args, const var_t, entry )
650 {
651 unsigned int align = 0;
652 /* FIXME: take alignment into account */
653 if (var->name && !strcmp(var->name, subexpr->u.sval))
654 {
655 correlation_variable = var->type;
656 break;
657 }
658 offset += type_memsize(var->type, &align);
659 }
660 if (!correlation_variable)
661 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
662 subexpr->u.sval);
663
664 correlation_variable = expr_resolve_type(NULL, structure, expr);
665
666 offset -= baseoff;
667 correlation_variable_type = correlation_variable->type;
668
669 switch (correlation_variable_type)
670 {
671 case RPC_FC_CHAR:
672 case RPC_FC_SMALL:
673 param_type = RPC_FC_SMALL;
674 break;
675 case RPC_FC_BYTE:
676 case RPC_FC_USMALL:
677 param_type = RPC_FC_USMALL;
678 break;
679 case RPC_FC_WCHAR:
680 case RPC_FC_SHORT:
681 case RPC_FC_ENUM16:
682 param_type = RPC_FC_SHORT;
683 break;
684 case RPC_FC_USHORT:
685 param_type = RPC_FC_USHORT;
686 break;
687 case RPC_FC_LONG:
688 case RPC_FC_ENUM32:
689 param_type = RPC_FC_LONG;
690 break;
691 case RPC_FC_ULONG:
692 param_type = RPC_FC_ULONG;
693 break;
694 default:
695 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
696 correlation_variable_type);
697 }
698
699 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
700 conftype | param_type, conftype_string, string_of_type(param_type));
701 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
702 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
703 offset, offset);
704 }
705 else
706 {
707 unsigned int callback_offset = 0;
708 struct expr_eval_routine *eval;
709 int found = 0;
710
711 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
712 {
713 if (!strcmp (eval->structure->name, structure->name)
714 && !compare_expr (eval->expr, expr))
715 {
716 found = 1;
717 break;
718 }
719 callback_offset++;
720 }
721
722 if (!found)
723 {
724 eval = xmalloc (sizeof(*eval));
725 eval->structure = structure;
726 eval->baseoff = baseoff;
727 eval->expr = expr;
728 list_add_tail (&expr_eval_routines, &eval->entry);
729 }
730
731 if (callback_offset > USHRT_MAX)
732 error("Maximum number of callback routines reached\n");
733
734 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
735 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
736 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
737 }
738 return 4;
739 }
740
741 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
742 {
743 int have_align = FALSE;
744 size_t size = 0;
745 const var_t *v;
746
747 if (!fields) return 0;
748 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
749 {
750 unsigned int falign = 0;
751 size_t fsize = type_memsize(v->type, &falign);
752 if (!have_align)
753 {
754 *align = falign;
755 have_align = TRUE;
756 }
757 size = (size + (falign - 1)) & ~(falign - 1);
758 size += fsize;
759 }
760
761 size = (size + (*align - 1)) & ~(*align - 1);
762 return size;
763 }
764
765 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
766 {
767 size_t size, maxs = 0;
768 unsigned int align = *pmaxa;
769 const var_t *v;
770
771 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
772 {
773 /* we could have an empty default field with NULL type */
774 if (v->type)
775 {
776 size = type_memsize(v->type, &align);
777 if (maxs < size) maxs = size;
778 if (*pmaxa < align) *pmaxa = align;
779 }
780 }
781
782 return maxs;
783 }
784
785 int get_padding(const var_list_t *fields)
786 {
787 unsigned short offset = 0;
788 int salign = -1;
789 const var_t *f;
790
791 if (!fields)
792 return 0;
793
794 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
795 {
796 type_t *ft = f->type;
797 unsigned int align = 0;
798 size_t size = type_memsize(ft, &align);
799 if (salign == -1)
800 salign = align;
801 offset = (offset + (align - 1)) & ~(align - 1);
802 offset += size;
803 }
804
805 return ((offset + (salign - 1)) & ~(salign - 1)) - offset;
806 }
807
808 size_t type_memsize(const type_t *t, unsigned int *align)
809 {
810 size_t size = 0;
811
812 if (t->declarray && is_conformant_array(t))
813 {
814 type_memsize(t->ref, align);
815 size = 0;
816 }
817 else if (is_ptr(t) || is_conformant_array(t))
818 {
819 size = sizeof(void *);
820 if (size > *align) *align = size;
821 }
822 else switch (t->type)
823 {
824 case RPC_FC_BYTE:
825 case RPC_FC_CHAR:
826 case RPC_FC_USMALL:
827 case RPC_FC_SMALL:
828 size = 1;
829 if (size > *align) *align = size;
830 break;
831 case RPC_FC_WCHAR:
832 case RPC_FC_USHORT:
833 case RPC_FC_SHORT:
834 case RPC_FC_ENUM16:
835 size = 2;
836 if (size > *align) *align = size;
837 break;
838 case RPC_FC_ULONG:
839 case RPC_FC_LONG:
840 case RPC_FC_ERROR_STATUS_T:
841 case RPC_FC_ENUM32:
842 case RPC_FC_FLOAT:
843 size = 4;
844 if (size > *align) *align = size;
845 break;
846 case RPC_FC_HYPER:
847 case RPC_FC_DOUBLE:
848 size = 8;
849 if (size > *align) *align = size;
850 break;
851 case RPC_FC_STRUCT:
852 case RPC_FC_CVSTRUCT:
853 case RPC_FC_CPSTRUCT:
854 case RPC_FC_CSTRUCT:
855 case RPC_FC_PSTRUCT:
856 case RPC_FC_BOGUS_STRUCT:
857 size = fields_memsize(t->fields_or_args, align);
858 break;
859 case RPC_FC_ENCAPSULATED_UNION:
860 case RPC_FC_NON_ENCAPSULATED_UNION:
861 size = union_memsize(t->fields_or_args, align);
862 break;
863 case RPC_FC_SMFARRAY:
864 case RPC_FC_LGFARRAY:
865 case RPC_FC_SMVARRAY:
866 case RPC_FC_LGVARRAY:
867 case RPC_FC_BOGUS_ARRAY:
868 size = t->dim * type_memsize(t->ref, align);
869 break;
870 default:
871 error("type_memsize: Unknown type %d\n", t->type);
872 size = 0;
873 }
874
875 return size;
876 }
877
878 int is_full_pointer_function(const func_t *func)
879 {
880 const var_t *var;
881 if (type_has_full_pointer(get_func_return_type(func)))
882 return TRUE;
883 if (!func->args)
884 return FALSE;
885 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
886 if (type_has_full_pointer( var->type ))
887 return TRUE;
888 return FALSE;
889 }
890
891 void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
892 {
893 print_file(file, indent, "_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
894 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
895 fprintf(file, "\n");
896 }
897
898 void write_full_pointer_free(FILE *file, int indent, const func_t *func)
899 {
900 print_file(file, indent, "NdrFullPointerXlatFree(_StubMsg.FullPtrXlatTables);\n");
901 fprintf(file, "\n");
902 }
903
904 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
905 {
906 short absoff = type->ref->typestring_offset;
907 short reloff = absoff - (offset + 2);
908 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
909
910 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
911 type->type, ptr_attr, string_of_type(type->type));
912 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
913 reloff, reloff, absoff);
914 return 4;
915 }
916
917 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
918 {
919 unsigned char fc = type->ref->type;
920 /* for historical reasons, write_simple_pointer also handled string types,
921 * but no longer does. catch bad uses of the function with this check */
922 if (is_string_type(type->attrs, type))
923 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
924 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
925 type->type, string_of_type(type->type));
926 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
927 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
928 return 4;
929 }
930
931 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
932 {
933 print_file(file, 0, "/* %u (", tfsoff);
934 write_type_decl(file, t, NULL);
935 print_file(file, 0, ") */\n");
936 }
937
938 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
939 {
940 unsigned int offset = *typestring_offset;
941
942 print_start_tfs_comment(file, type, offset);
943 update_tfsoff(type, offset, file);
944
945 if (type->ref->typestring_offset)
946 *typestring_offset += write_nonsimple_pointer(file, type, offset);
947 else if (is_base_type(type->ref->type))
948 *typestring_offset += write_simple_pointer(file, type);
949
950 return offset;
951 }
952
953 static int processed(const type_t *type)
954 {
955 return type->typestring_offset && !type->tfswrite;
956 }
957
958 static int user_type_has_variable_size(const type_t *t)
959 {
960 if (is_ptr(t))
961 return TRUE;
962 else
963 switch (t->type)
964 {
965 case RPC_FC_PSTRUCT:
966 case RPC_FC_CSTRUCT:
967 case RPC_FC_CPSTRUCT:
968 case RPC_FC_CVSTRUCT:
969 return TRUE;
970 }
971 /* Note: Since this only applies to user types, we can't have a conformant
972 array here, and strings should get filed under pointer in this case. */
973 return FALSE;
974 }
975
976 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
977 {
978 unsigned int start, absoff, flags;
979 unsigned int align = 0, ualign = 0;
980 const char *name;
981 type_t *utype = get_user_type(type, &name);
982 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
983 size_t size = type_memsize(type, &align);
984 unsigned short funoff = user_type_offset(name);
985 short reloff;
986
987 guard_rec(type);
988
989 if (is_base_type(utype->type))
990 {
991 absoff = *tfsoff;
992 print_start_tfs_comment(file, utype, absoff);
993 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
994 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
995 *tfsoff += 2;
996 }
997 else
998 {
999 if (!processed(utype))
1000 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1001 absoff = utype->typestring_offset;
1002 }
1003
1004 if (utype->type == RPC_FC_RP)
1005 flags = 0x40;
1006 else if (utype->type == RPC_FC_UP)
1007 flags = 0x80;
1008 else
1009 flags = 0;
1010
1011 start = *tfsoff;
1012 update_tfsoff(type, start, file);
1013 print_start_tfs_comment(file, type, start);
1014 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1015 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1016 flags | (align - 1), align - 1, flags);
1017 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1018 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1019 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1020 *tfsoff += 8;
1021 reloff = absoff - *tfsoff;
1022 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1023 *tfsoff += 2;
1024 }
1025
1026 static void write_member_type(FILE *file, const type_t *cont,
1027 const attr_list_t *attrs, const type_t *type,
1028 unsigned int *corroff, unsigned int *tfsoff)
1029 {
1030 if (is_embedded_complex(type) && !is_conformant_array(type))
1031 {
1032 size_t absoff;
1033 short reloff;
1034
1035 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1036 {
1037 absoff = *corroff;
1038 *corroff += 8;
1039 }
1040 else
1041 {
1042 absoff = type->typestring_offset;
1043 }
1044 reloff = absoff - (*tfsoff + 2);
1045
1046 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1047 /* FIXME: actually compute necessary padding */
1048 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1049 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1050 reloff, reloff, absoff);
1051 *tfsoff += 4;
1052 }
1053 else if (is_ptr(type) || is_conformant_array(type))
1054 {
1055 unsigned char fc = (cont->type == RPC_FC_BOGUS_STRUCT
1056 ? RPC_FC_POINTER
1057 : RPC_FC_LONG);
1058 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1059 *tfsoff += 1;
1060 }
1061 else if (!write_base_type(file, type, tfsoff))
1062 error("Unsupported member type 0x%x\n", type->type);
1063 }
1064
1065 static void write_end(FILE *file, unsigned int *tfsoff)
1066 {
1067 if (*tfsoff % 2 == 0)
1068 {
1069 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1070 *tfsoff += 1;
1071 }
1072 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1073 *tfsoff += 1;
1074 }
1075
1076 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1077 {
1078 unsigned int offset = 0;
1079 var_list_t *fs = type->fields_or_args;
1080 var_t *f;
1081
1082 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1083 {
1084 unsigned int align = 0;
1085 type_t *ft = f->type;
1086 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1087 {
1088 unsigned int absoff = ft->typestring_offset;
1089 short reloff = absoff - (*tfsoff + 6);
1090 print_file(file, 0, "/* %d */\n", *tfsoff);
1091 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1092 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1093 write_conf_or_var_desc(file, current_structure, offset, ft,
1094 get_attrp(f->attrs, ATTR_SWITCHIS));
1095 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1096 reloff, reloff, absoff);
1097 *tfsoff += 8;
1098 }
1099
1100 /* FIXME: take alignment into account */
1101 offset += type_memsize(ft, &align);
1102 }
1103 }
1104
1105 static int write_no_repeat_pointer_descriptions(
1106 FILE *file, type_t *type,
1107 size_t *offset_in_memory, size_t *offset_in_buffer,
1108 unsigned int *typestring_offset)
1109 {
1110 int written = 0;
1111 unsigned int align;
1112
1113 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1114 {
1115 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1116 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1117
1118 /* pointer instance */
1119 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1120 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1121 *typestring_offset += 6;
1122
1123 if (is_ptr(type))
1124 {
1125 if (is_string_type(type->attrs, type))
1126 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1127 else
1128 write_pointer_tfs(file, type, typestring_offset);
1129 }
1130 else
1131 {
1132 unsigned absoff = type->typestring_offset;
1133 short reloff = absoff - (*typestring_offset + 2);
1134 /* FIXME: get pointer attributes from field */
1135 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1136 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1137 reloff, reloff, absoff);
1138 *typestring_offset += 4;
1139 }
1140
1141 align = 0;
1142 *offset_in_memory += type_memsize(type, &align);
1143 /* FIXME: is there a case where these two are different? */
1144 align = 0;
1145 *offset_in_buffer += type_memsize(type, &align);
1146
1147 return 1;
1148 }
1149
1150 if (is_non_complex_struct(type))
1151 {
1152 const var_t *v;
1153 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1154 written += write_no_repeat_pointer_descriptions(
1155 file, v->type,
1156 offset_in_memory, offset_in_buffer, typestring_offset);
1157 }
1158 else
1159 {
1160 align = 0;
1161 *offset_in_memory += type_memsize(type, &align);
1162 /* FIXME: is there a case where these two are different? */
1163 align = 0;
1164 *offset_in_buffer += type_memsize(type, &align);
1165 }
1166
1167 return written;
1168 }
1169
1170 static int write_pointer_description_offsets(
1171 FILE *file, const attr_list_t *attrs, type_t *type,
1172 size_t *offset_in_memory, size_t *offset_in_buffer,
1173 unsigned int *typestring_offset)
1174 {
1175 int written = 0;
1176 unsigned int align;
1177
1178 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1179 {
1180 if (offset_in_memory && offset_in_buffer)
1181 {
1182 /* pointer instance */
1183 /* FIXME: sometimes from end of structure, sometimes from beginning */
1184 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1185 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1186
1187 align = 0;
1188 *offset_in_memory += type_memsize(type, &align);
1189 /* FIXME: is there a case where these two are different? */
1190 align = 0;
1191 *offset_in_buffer += type_memsize(type, &align);
1192 }
1193 *typestring_offset += 4;
1194
1195 if (is_string_type(attrs, type))
1196 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1197 else if (processed(type->ref) || is_base_type(type->ref->type))
1198 write_pointer_tfs(file, type, typestring_offset);
1199 else
1200 error("write_pointer_description_offsets: type format string unknown\n");
1201
1202 return 1;
1203 }
1204
1205 if (is_array(type))
1206 {
1207 return write_pointer_description_offsets(
1208 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1209 typestring_offset);
1210 }
1211 else if (is_non_complex_struct(type))
1212 {
1213 /* otherwise search for interesting fields to parse */
1214 const var_t *v;
1215 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1216 {
1217 written += write_pointer_description_offsets(
1218 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1219 typestring_offset);
1220 }
1221 }
1222 else
1223 {
1224 align = 0;
1225 if (offset_in_memory)
1226 *offset_in_memory += type_memsize(type, &align);
1227 /* FIXME: is there a case where these two are different? */
1228 align = 0;
1229 if (offset_in_buffer)
1230 *offset_in_buffer += type_memsize(type, &align);
1231 }
1232
1233 return written;
1234 }
1235
1236 /* Note: if file is NULL return value is number of pointers to write, else
1237 * it is the number of type format characters written */
1238 static int write_fixed_array_pointer_descriptions(
1239 FILE *file, const attr_list_t *attrs, type_t *type,
1240 size_t *offset_in_memory, size_t *offset_in_buffer,
1241 unsigned int *typestring_offset)
1242 {
1243 unsigned int align;
1244 int pointer_count = 0;
1245
1246 if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1247 {
1248 unsigned int temp = 0;
1249 /* unfortunately, this needs to be done in two passes to avoid
1250 * writing out redundant FC_FIXED_REPEAT descriptions */
1251 pointer_count = write_pointer_description_offsets(
1252 NULL, attrs, type->ref, NULL, NULL, &temp);
1253 if (pointer_count > 0)
1254 {
1255 unsigned int increment_size;
1256 size_t offset_of_array_pointer_mem = 0;
1257 size_t offset_of_array_pointer_buf = 0;
1258
1259 align = 0;
1260 increment_size = type_memsize(type->ref, &align);
1261
1262 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1263 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1264 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1265 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1266 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1267 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1268 *typestring_offset += 10;
1269
1270 pointer_count = write_pointer_description_offsets(
1271 file, attrs, type, &offset_of_array_pointer_mem,
1272 &offset_of_array_pointer_buf, typestring_offset);
1273 }
1274 }
1275 else if (is_struct(type->type))
1276 {
1277 const var_t *v;
1278 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1279 {
1280 pointer_count += write_fixed_array_pointer_descriptions(
1281 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1282 typestring_offset);
1283 }
1284 }
1285 else
1286 {
1287 align = 0;
1288 if (offset_in_memory)
1289 *offset_in_memory += type_memsize(type, &align);
1290 /* FIXME: is there a case where these two are different? */
1291 align = 0;
1292 if (offset_in_buffer)
1293 *offset_in_buffer += type_memsize(type, &align);
1294 }
1295
1296 return pointer_count;
1297 }
1298
1299 /* Note: if file is NULL return value is number of pointers to write, else
1300 * it is the number of type format characters written */
1301 static int write_conformant_array_pointer_descriptions(
1302 FILE *file, const attr_list_t *attrs, type_t *type,
1303 size_t offset_in_memory, unsigned int *typestring_offset)
1304 {
1305 unsigned int align;
1306 int pointer_count = 0;
1307
1308 if (is_conformant_array(type) && !type->length_is)
1309 {
1310 unsigned int temp = 0;
1311 /* unfortunately, this needs to be done in two passes to avoid
1312 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1313 pointer_count = write_pointer_description_offsets(
1314 NULL, attrs, type->ref, NULL, NULL, &temp);
1315 if (pointer_count > 0)
1316 {
1317 unsigned int increment_size;
1318 size_t offset_of_array_pointer_mem = offset_in_memory;
1319 size_t offset_of_array_pointer_buf = offset_in_memory;
1320
1321 align = 0;
1322 increment_size = type_memsize(type->ref, &align);
1323
1324 if (increment_size > USHRT_MAX)
1325 error("array size of %u bytes is too large\n", increment_size);
1326
1327 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1328 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1329 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1330 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1331 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1332 *typestring_offset += 8;
1333
1334 pointer_count = write_pointer_description_offsets(
1335 file, attrs, type->ref, &offset_of_array_pointer_mem,
1336 &offset_of_array_pointer_buf, typestring_offset);
1337 }
1338 }
1339
1340 return pointer_count;
1341 }
1342
1343 /* Note: if file is NULL return value is number of pointers to write, else
1344 * it is the number of type format characters written */
1345 static int write_varying_array_pointer_descriptions(
1346 FILE *file, const attr_list_t *attrs, type_t *type,
1347 size_t *offset_in_memory, size_t *offset_in_buffer,
1348 unsigned int *typestring_offset)
1349 {
1350 unsigned int align;
1351 int pointer_count = 0;
1352
1353 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1354
1355 if (is_array(type) && type->length_is)
1356 {
1357 unsigned int temp = 0;
1358 /* unfortunately, this needs to be done in two passes to avoid
1359 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1360 pointer_count = write_pointer_description_offsets(
1361 NULL, attrs, type->ref, NULL, NULL, &temp);
1362 if (pointer_count > 0)
1363 {
1364 unsigned int increment_size;
1365 size_t offset_of_array_pointer_mem = 0;
1366 size_t offset_of_array_pointer_buf = 0;
1367
1368 align = 0;
1369 increment_size = type_memsize(type->ref, &align);
1370
1371 if (increment_size > USHRT_MAX)
1372 error("array size of %u bytes is too large\n", increment_size);
1373
1374 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1375 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1376 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1377 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1378 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1379 *typestring_offset += 8;
1380
1381 pointer_count = write_pointer_description_offsets(
1382 file, attrs, type, &offset_of_array_pointer_mem,
1383 &offset_of_array_pointer_buf, typestring_offset);
1384 }
1385 }
1386 else if (is_struct(type->type))
1387 {
1388 const var_t *v;
1389 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1390 {
1391 pointer_count += write_varying_array_pointer_descriptions(
1392 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1393 typestring_offset);
1394 }
1395 }
1396 else
1397 {
1398 align = 0;
1399 if (offset_in_memory)
1400 *offset_in_memory += type_memsize(type, &align);
1401 /* FIXME: is there a case where these two are different? */
1402 align = 0;
1403 if (offset_in_buffer)
1404 *offset_in_buffer += type_memsize(type, &align);
1405 }
1406
1407 return pointer_count;
1408 }
1409
1410 static void write_pointer_description(FILE *file, type_t *type,
1411 unsigned int *typestring_offset)
1412 {
1413 size_t offset_in_buffer;
1414 size_t offset_in_memory;
1415
1416 /* pass 1: search for single instance of a pointer (i.e. don't descend
1417 * into arrays) */
1418 if (!is_array(type))
1419 {
1420 offset_in_memory = 0;
1421 offset_in_buffer = 0;
1422 write_no_repeat_pointer_descriptions(
1423 file, type,
1424 &offset_in_memory, &offset_in_buffer, typestring_offset);
1425 }
1426
1427 /* pass 2: search for pointers in fixed arrays */
1428 offset_in_memory = 0;
1429 offset_in_buffer = 0;
1430 write_fixed_array_pointer_descriptions(
1431 file, NULL, type,
1432 &offset_in_memory, &offset_in_buffer, typestring_offset);
1433
1434 /* pass 3: search for pointers in conformant only arrays (but don't descend
1435 * into conformant varying or varying arrays) */
1436 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1437 write_conformant_array_pointer_descriptions(
1438 file, NULL, type, 0, typestring_offset);
1439 else if (type->type == RPC_FC_CPSTRUCT)
1440 {
1441 unsigned int align = 0;
1442 type_t *carray = find_array_or_string_in_struct(type)->type;
1443 write_conformant_array_pointer_descriptions(
1444 file, NULL, carray,
1445 type_memsize(type, &align),
1446 typestring_offset);
1447 }
1448
1449 /* pass 4: search for pointers in varying arrays */
1450 offset_in_memory = 0;
1451 offset_in_buffer = 0;
1452 write_varying_array_pointer_descriptions(
1453 file, NULL, type,
1454 &offset_in_memory, &offset_in_buffer, typestring_offset);
1455 }
1456
1457 int is_declptr(const type_t *t)
1458 {
1459 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1460 }
1461
1462 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1463 type_t *type,
1464 const char *name, unsigned int *typestring_offset)
1465 {
1466 size_t start_offset;
1467 unsigned char rtype;
1468
1469 if (is_declptr(type))
1470 {
1471 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1472 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1473 if (!pointer_type)
1474 pointer_type = RPC_FC_RP;
1475 print_start_tfs_comment(file, type, *typestring_offset);
1476 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1477 pointer_type, flag, string_of_type(pointer_type),
1478 flag ? " [simple_pointer]" : "");
1479 *typestring_offset += 2;
1480 if (!flag)
1481 {
1482 print_file(file, 2, "NdrFcShort(0x2),\n");
1483 *typestring_offset += 2;
1484 }
1485 }
1486
1487 start_offset = *typestring_offset;
1488 update_tfsoff(type, start_offset, file);
1489
1490 rtype = type->ref->type;
1491
1492 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1493 {
1494 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1495 return start_offset;
1496 }
1497
1498 if (type->declarray && !is_conformant_array(type))
1499 {
1500 /* FIXME: multi-dimensional array */
1501 if (0xffffuL < type->dim)
1502 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1503 name, 0xffffu, type->dim - 0xffffu);
1504
1505 if (rtype == RPC_FC_CHAR)
1506 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1507 else
1508 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1509 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1510 *typestring_offset += 2;
1511
1512 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1513 *typestring_offset += 2;
1514
1515 return start_offset;
1516 }
1517 else if (type->size_is)
1518 {
1519 unsigned int align = 0;
1520
1521 if (rtype == RPC_FC_CHAR)
1522 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1523 else
1524 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1525 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1526 *typestring_offset += 2;
1527
1528 *typestring_offset += write_conf_or_var_desc(
1529 file, current_structure,
1530 (type->declarray && current_structure
1531 ? type_memsize(current_structure, &align)
1532 : 0),
1533 type, type->size_is);
1534
1535 return start_offset;
1536 }
1537 else
1538 {
1539 if (rtype == RPC_FC_WCHAR)
1540 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1541 else
1542 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1543 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1544 *typestring_offset += 2;
1545
1546 return start_offset;
1547 }
1548 }
1549
1550 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1551 const char *name, unsigned int *typestring_offset)
1552 {
1553 const expr_t *length_is = type->length_is;
1554 const expr_t *size_is = type->size_is;
1555 unsigned int align = 0;
1556 size_t size;
1557 size_t start_offset;
1558 int has_pointer;
1559 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1560 unsigned int baseoff
1561 = type->declarray && current_structure
1562 ? type_memsize(current_structure, &align)
1563 : 0;
1564
1565 if (!pointer_type)
1566 pointer_type = RPC_FC_RP;
1567
1568 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1569 has_pointer = TRUE;
1570 else
1571 has_pointer = type_has_pointers(type->ref);
1572
1573 align = 0;
1574 size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1575
1576 start_offset = *typestring_offset;
1577 update_tfsoff(type, start_offset, file);
1578 print_start_tfs_comment(file, type, start_offset);
1579 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1580 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1581 *typestring_offset += 2;
1582
1583 align = 0;
1584 if (type->type != RPC_FC_BOGUS_ARRAY)
1585 {
1586 unsigned char tc = type->type;
1587
1588 if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1589 {
1590 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1591 *typestring_offset += 4;
1592 }
1593 else
1594 {
1595 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1596 *typestring_offset += 2;
1597 }
1598
1599 if (is_conformant_array(type))
1600 *typestring_offset
1601 += write_conf_or_var_desc(file, current_structure, baseoff,
1602 type, size_is);
1603
1604 if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1605 {
1606 unsigned int elalign = 0;
1607 size_t elsize = type_memsize(type->ref, &elalign);
1608
1609 if (type->type == RPC_FC_LGVARRAY)
1610 {
1611 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1612 *typestring_offset += 4;
1613 }
1614 else
1615 {
1616 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1617 *typestring_offset += 2;
1618 }
1619
1620 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1621 *typestring_offset += 2;
1622 }
1623
1624 if (length_is)
1625 *typestring_offset
1626 += write_conf_or_var_desc(file, current_structure, baseoff,
1627 type, length_is);
1628
1629 if (has_pointer && (!type->declarray || !current_structure))
1630 {
1631 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1632 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1633 *typestring_offset += 2;
1634 write_pointer_description(file, type, typestring_offset);
1635 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1636 *typestring_offset += 1;
1637 }
1638
1639 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1640 write_end(file, typestring_offset);
1641 }
1642 else
1643 {
1644 unsigned int dim = size_is ? 0 : type->dim;
1645 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1646 *typestring_offset += 2;
1647 *typestring_offset
1648 += write_conf_or_var_desc(file, current_structure, baseoff,
1649 type, size_is);
1650 *typestring_offset
1651 += write_conf_or_var_desc(file, current_structure, baseoff,
1652 type, length_is);
1653 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1654 write_end(file, typestring_offset);
1655 }
1656
1657 return start_offset;
1658 }
1659
1660 static const var_t *find_array_or_string_in_struct(const type_t *type)
1661 {
1662 const var_t *last_field;
1663 const type_t *ft;
1664
1665 if (!type->fields_or_args || list_empty(type->fields_or_args))
1666 return NULL;
1667
1668 last_field = LIST_ENTRY( list_tail(type->fields_or_args), const var_t, entry );
1669 ft = last_field->type;
1670
1671 if (ft->declarray && is_conformant_array(ft))
1672 return last_field;
1673
1674 if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1675 return find_array_or_string_in_struct(ft);
1676 else
1677 return NULL;
1678 }
1679
1680 static void write_struct_members(FILE *file, const type_t *type,
1681 unsigned int *corroff, unsigned int *typestring_offset)
1682 {
1683 const var_t *field;
1684 unsigned short offset = 0;
1685 int salign = -1;
1686 int padding;
1687
1688 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
1689 {
1690 type_t *ft = field->type;
1691 if (!ft->declarray || !is_conformant_array(ft))
1692 {
1693 unsigned int align = 0;
1694 size_t size = type_memsize(ft, &align);
1695 if (salign == -1)
1696 salign = align;
1697 if ((align - 1) & offset)
1698 {
1699 unsigned char fc = 0;
1700 switch (align)
1701 {
1702 case 4:
1703 fc = RPC_FC_ALIGNM4;
1704 break;
1705 case 8:
1706 fc = RPC_FC_ALIGNM8;
1707 break;
1708 default:
1709 error("write_struct_members: cannot align type %d\n", ft->type);
1710 }
1711 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1712 offset = (offset + (align - 1)) & ~(align - 1);
1713 *typestring_offset += 1;
1714 }
1715 write_member_type(file, type, field->attrs, field->type, corroff,
1716 typestring_offset);
1717 offset += size;
1718 }
1719 }
1720
1721 padding = ((offset + (salign - 1)) & ~(salign - 1)) - offset;
1722 if (padding)
1723 {
1724 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1725 RPC_FC_STRUCTPAD1 + padding - 1,
1726 padding);
1727 *typestring_offset += 1;
1728 }
1729
1730 write_end(file, typestring_offset);
1731 }
1732
1733 static size_t write_struct_tfs(FILE *file, type_t *type,
1734 const char *name, unsigned int *tfsoff)
1735 {
1736 const type_t *save_current_structure = current_structure;
1737 unsigned int total_size;
1738 const var_t *array;
1739 size_t start_offset;
1740 size_t array_offset;
1741 int has_pointers = 0;
1742 unsigned int align = 0;
1743 unsigned int corroff;
1744 var_t *f;
1745
1746 guard_rec(type);
1747 current_structure = type;
1748
1749 total_size = type_memsize(type, &align);
1750 if (total_size > USHRT_MAX)
1751 error("structure size for %s exceeds %d bytes by %d bytes\n",
1752 name, USHRT_MAX, total_size - USHRT_MAX);
1753
1754 if (type->fields_or_args) LIST_FOR_EACH_ENTRY(f, type->fields_or_args, var_t, entry)
1755 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1756 FALSE, tfsoff);
1757 if (!has_pointers) has_pointers = type_has_pointers(type);
1758
1759 array = find_array_or_string_in_struct(type);
1760 if (array && !processed(array->type))
1761 array_offset
1762 = is_attr(array->attrs, ATTR_STRING)
1763 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1764 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1765
1766 corroff = *tfsoff;
1767 write_descriptors(file, type, tfsoff);
1768
1769 start_offset = *tfsoff;
1770 update_tfsoff(type, start_offset, file);
1771 print_start_tfs_comment(file, type, start_offset);
1772 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1773 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1774 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1775 *tfsoff += 4;
1776
1777 if (array)
1778 {
1779 unsigned int absoff = array->type->typestring_offset;
1780 short reloff = absoff - *tfsoff;
1781 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1782 reloff, reloff, absoff);
1783 *tfsoff += 2;
1784 }
1785 else if (type->type == RPC_FC_BOGUS_STRUCT)
1786 {
1787 print_file(file, 2, "NdrFcShort(0x0),\n");
1788 *tfsoff += 2;
1789 }
1790
1791 if (type->type == RPC_FC_BOGUS_STRUCT)
1792 {
1793 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1794 nothing is written to file yet. On the actual writing pass,
1795 this will have been updated. */
1796 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
1797 short reloff = absoff - *tfsoff;
1798 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1799 reloff, reloff, absoff);
1800 *tfsoff += 2;
1801 }
1802 else if ((type->type == RPC_FC_PSTRUCT) ||
1803 (type->type == RPC_FC_CPSTRUCT) ||
1804 (type->type == RPC_FC_CVSTRUCT && has_pointers))
1805 {
1806 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1807 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1808 *tfsoff += 2;
1809 write_pointer_description(file, type, tfsoff);
1810 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1811 *tfsoff += 1;
1812 }
1813
1814 write_struct_members(file, type, &corroff, tfsoff);
1815
1816 if (type->type == RPC_FC_BOGUS_STRUCT)
1817 {
1818 const var_list_t *fs = type->fields_or_args;
1819 const var_t *f;
1820
1821 type->ptrdesc = *tfsoff;
1822 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
1823 {
1824 type_t *ft = f->type;
1825 if (is_ptr(ft))
1826 {
1827 if (is_string_type(f->attrs, ft))
1828 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
1829 else
1830 write_pointer_tfs(file, ft, tfsoff);
1831 }
1832 else if (!ft->declarray && is_conformant_array(ft))
1833 {
1834 unsigned int absoff = ft->typestring_offset;
1835 short reloff = absoff - (*tfsoff + 2);
1836 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
1837 /* FIXME: We need to store pointer attributes for arrays
1838 so we don't lose pointer_default info. */
1839 if (ptr_type == 0)
1840 ptr_type = RPC_FC_UP;
1841 print_file(file, 0, "/* %d */\n", *tfsoff);
1842 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
1843 string_of_type(ptr_type));
1844 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1845 reloff, reloff, absoff);
1846 *tfsoff += 4;
1847 }
1848 }
1849 if (type->ptrdesc == *tfsoff)
1850 type->ptrdesc = 0;
1851 }
1852
1853 current_structure = save_current_structure;
1854 return start_offset;
1855 }
1856
1857 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1858 unsigned char flags, size_t offset,
1859 unsigned int *typeformat_offset)
1860 {
1861 size_t start_offset = *typeformat_offset;
1862 short reloff = offset - (*typeformat_offset + 2);
1863 int in_attr, out_attr;
1864 in_attr = is_attr(attrs, ATTR_IN);
1865 out_attr = is_attr(attrs, ATTR_OUT);
1866 if (!in_attr && !out_attr) in_attr = 1;
1867
1868 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1869 flags |= 0x04;
1870
1871 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1872 pointer_type,
1873 flags,
1874 string_of_type(pointer_type));
1875 if (file)
1876 {
1877 if (flags & 0x04)
1878 fprintf(file, " [allocated_on_stack]");
1879 if (flags & 0x10)
1880 fprintf(file, " [pointer_deref]");
1881 fprintf(file, " */\n");
1882 }
1883
1884 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1885 *typeformat_offset += 4;
1886
1887 return start_offset;
1888 }
1889
1890 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1891 {
1892 if (t == NULL)
1893 {
1894 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1895 }
1896 else if (is_base_type(t->type))
1897 {
1898 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1899 t->type, string_of_type(t->type));
1900 }
1901 else if (t->typestring_offset)
1902 {
1903 short reloff = t->typestring_offset - *tfsoff;
1904 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1905 reloff, reloff, t->typestring_offset);
1906 }
1907 else
1908 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1909
1910 *tfsoff += 2;
1911 }
1912
1913 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1914 {
1915 unsigned int align = 0;
1916 unsigned int start_offset;
1917 size_t size = type_memsize(type, &align);
1918 var_list_t *fields;
1919 size_t nbranch = 0;
1920 type_t *deftype = NULL;
1921 short nodeftype = 0xffff;
1922 var_t *f;
1923
1924 guard_rec(type);
1925
1926 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1927 {
1928 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
1929 fields = uv->type->fields_or_args;
1930 }
1931 else
1932 fields = type->fields_or_args;
1933
1934 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1935 {
1936 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1937 if (cases)
1938 nbranch += list_count(cases);
1939 if (f->type)
1940 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1941 }
1942
1943 start_offset = *tfsoff;
1944 update_tfsoff(type, start_offset, file);
1945 print_start_tfs_comment(file, type, start_offset);
1946 if (type->type == RPC_FC_ENCAPSULATED_UNION)
1947 {
1948 const var_t *sv = LIST_ENTRY(list_head(type->fields_or_args), const var_t, entry);
1949 const type_t *st = sv->type;
1950
1951 switch (st->type)
1952 {
1953 case RPC_FC_CHAR:
1954 case RPC_FC_SMALL:
1955 case RPC_FC_USMALL:
1956 case RPC_FC_SHORT:
1957 case RPC_FC_USHORT:
1958 case RPC_FC_LONG:
1959 case RPC_FC_ULONG:
1960 case RPC_FC_ENUM16:
1961 case RPC_FC_ENUM32:
1962 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1963 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1964 0x40 | st->type, string_of_type(st->type));
1965 *tfsoff += 2;
1966 break;
1967 default:
1968 error("union switch type must be an integer, char, or enum\n");
1969 }
1970 }
1971 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1972 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1973 *tfsoff += 4;
1974
1975 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1976 {
1977 type_t *ft = f->type;
1978 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1979 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1980 expr_t *c;
1981
1982 if (cases == NULL && !deflt)
1983 error("union field %s with neither case nor default attribute\n", f->name);
1984
1985 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1986 {
1987 /* MIDL doesn't check for duplicate cases, even though that seems
1988 like a reasonable thing to do, it just dumps them to the TFS
1989 like we're going to do here. */
1990 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1991 *tfsoff += 4;
1992 write_branch_type(file, ft, tfsoff);
1993 }
1994
1995 /* MIDL allows multiple default branches, even though that seems
1996 illogical, it just chooses the last one, which is what we will
1997 do. */
1998 if (deflt)
1999 {
2000 deftype = ft;
2001 nodeftype = 0;
2002 }
2003 }
2004
2005 if (deftype)
2006 {
2007 write_branch_type(file, deftype, tfsoff);
2008 }
2009 else
2010 {
2011 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2012 *tfsoff += 2;
2013 }
2014
2015 return start_offset;
2016 }
2017
2018 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2019 unsigned int *typeformat_offset)
2020 {
2021 size_t i;
2022 size_t start_offset = *typeformat_offset;
2023 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2024
2025 if (iid)
2026 {
2027 print_file(file, 2, "0x2f, /* FC_IP */\n");
2028 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2029 *typeformat_offset
2030 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2031 }
2032 else
2033 {
2034 const type_t *base = is_ptr(type) ? type->ref : type;
2035 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2036
2037 if (! uuid)
2038 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2039
2040 update_tfsoff(type, start_offset, file);
2041 print_start_tfs_comment(file, type, start_offset);
2042 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2043 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2044 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2045 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2046 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2047 for (i = 0; i < 8; ++i)
2048 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2049
2050 if (file)
2051 fprintf(file, "\n");
2052
2053 *typeformat_offset += 18;
2054 }
2055 return start_offset;
2056 }
2057
2058 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2059 const var_t *var,
2060 unsigned int *typeformat_offset)
2061 {
2062 size_t start_offset = *typeformat_offset;
2063 unsigned char flags = 0;
2064
2065 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2066 flags |= NDR_STRICT_CONTEXT_HANDLE;
2067
2068 if (is_ptr(type))
2069 flags |= 0x80;
2070 if (is_attr(var->attrs, ATTR_IN))
2071 {
2072 flags |= 0x40;
2073 if (!is_attr(var->attrs, ATTR_OUT))
2074 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2075 }
2076 if (is_attr(var->attrs, ATTR_OUT))
2077 flags |= 0x20;
2078
2079 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2080 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2081 /* return and can't be null values overlap */
2082 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2083 print_file(file, 0, "can't be null, ");
2084 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2085 print_file(file, 0, "serialize, ");
2086 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2087 print_file(file, 0, "no serialize, ");
2088 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2089 print_file(file, 0, "strict, ");
2090 if ((flags & 0x21) == 0x20)
2091 print_file(file, 0, "out, ");
2092 if ((flags & 0x21) == 0x21)
2093 print_file(file, 0, "return, ");
2094 if (flags & 0x40)
2095 print_file(file, 0, "in, ");
2096 if (flags & 0x80)
2097 print_file(file, 0, "via ptr, ");
2098 print_file(file, 0, "*/\n");
2099 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2100 print_file(file, 2, "0, /* FIXME: param num */\n");
2101 *typeformat_offset += 4;
2102
2103 return start_offset;
2104 }
2105
2106 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2107 type_t *type, const var_t *var,
2108 unsigned int *typeformat_offset)
2109 {
2110 size_t offset;
2111
2112 if (is_context_handle(type))
2113 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2114
2115 if (is_user_type(type))
2116 {
2117 write_user_tfs(file, type, typeformat_offset);
2118 return type->typestring_offset;
2119 }
2120
2121 if (is_string_type(var->attrs, type))
2122 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2123
2124 if (is_array(type))
2125 {
2126 int ptr_type;
2127 size_t off;
2128 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2129 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2130 /* Top level pointers to conformant arrays may be handled specially
2131 since we can bypass the pointer, but if the array is buried
2132 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2133 always need to write the pointer. */
2134 if (!ptr_type && var->type != type)
2135 /* FIXME: This should use pointer_default, but the information
2136 isn't kept around for arrays. */
2137 ptr_type = RPC_FC_UP;
2138 if (ptr_type && ptr_type != RPC_FC_RP)
2139 {
2140 unsigned int absoff = type->typestring_offset;
2141 short reloff = absoff - (*typeformat_offset + 2);
2142 off = *typeformat_offset;
2143 print_file(file, 0, "/* %d */\n", off);
2144 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2145 string_of_type(ptr_type));
2146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2147 reloff, reloff, absoff);
2148 *typeformat_offset += 4;
2149 }
2150 return off;
2151 }
2152
2153 if (!is_ptr(type))
2154 {
2155 /* basic types don't need a type format string */
2156 if (is_base_type(type->type))
2157 return 0;
2158
2159 switch (type->type)
2160 {
2161 case RPC_FC_STRUCT:
2162 case RPC_FC_PSTRUCT:
2163 case RPC_FC_CSTRUCT:
2164 case RPC_FC_CPSTRUCT:
2165 case RPC_FC_CVSTRUCT:
2166 case RPC_FC_BOGUS_STRUCT:
2167 return write_struct_tfs(file, type, var->name, typeformat_offset);
2168 case RPC_FC_ENCAPSULATED_UNION:
2169 case RPC_FC_NON_ENCAPSULATED_UNION:
2170 return write_union_tfs(file, type, typeformat_offset);
2171 case RPC_FC_IGNORE:
2172 case RPC_FC_BIND_PRIMITIVE:
2173 /* nothing to do */
2174 return 0;
2175 default:
2176 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2177 }
2178 }
2179 else if (last_ptr(type))
2180 {
2181 size_t start_offset = *typeformat_offset;
2182 int in_attr = is_attr(var->attrs, ATTR_IN);
2183 int out_attr = is_attr(var->attrs, ATTR_OUT);
2184 const type_t *base = type->ref;
2185
2186 if (base->type == RPC_FC_IP
2187 || (base->type == 0
2188 && is_attr(var->attrs, ATTR_IIDIS)))
2189 {
2190 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2191 }
2192
2193 /* special case for pointers to base types */
2194 if (is_base_type(base->type))
2195 {
2196 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2197 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2198 string_of_type(type->type),
2199 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2200 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2201 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2202 *typeformat_offset += 4;
2203 return start_offset;
2204 }
2205 }
2206
2207 assert(is_ptr(type));
2208
2209 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2210 if (file)
2211 fprintf(file, "/* %2u */\n", *typeformat_offset);
2212 return write_pointer_only_tfs(file, var->attrs, type->type,
2213 !last_ptr(type) ? 0x10 : 0,
2214 offset, typeformat_offset);
2215 }
2216
2217 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2218 const char *name, int write_ptr, unsigned int *tfsoff)
2219 {
2220 int retmask = 0;
2221
2222 if (is_user_type(type))
2223 {
2224 write_user_tfs(file, type, tfsoff);
2225 }
2226 else if (is_string_type(attrs, type))
2227 {
2228 write_string_tfs(file, attrs, type, name, tfsoff);
2229 }
2230 else if (is_ptr(type))
2231 {
2232 type_t *ref = type->ref;
2233
2234 if (ref->type == RPC_FC_IP
2235 || (ref->type == 0
2236 && is_attr(attrs, ATTR_IIDIS)))
2237 {
2238 write_ip_tfs(file, attrs, type, tfsoff);
2239 }
2240 else
2241 {
2242 if (!processed(ref) && !is_base_type(ref->type))
2243 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2244
2245 if (write_ptr)
2246 write_pointer_tfs(file, type, tfsoff);
2247
2248 retmask |= 1;
2249 }
2250 }
2251 else if (type->declarray && is_conformant_array(type))
2252 ; /* conformant arrays and strings are handled specially */
2253 else if (is_array(type))
2254 {
2255 write_array_tfs(file, attrs, type, name, tfsoff);
2256 if (is_conformant_array(type))
2257 retmask |= 1;
2258 }
2259 else if (is_struct(type->type))
2260 {
2261 if (!processed(type))
2262 write_struct_tfs(file, type, name, tfsoff);
2263 }
2264 else if (is_union(type->type))
2265 {
2266 if (!processed(type))
2267 write_union_tfs(file, type, tfsoff);
2268 }
2269 else if (!is_base_type(type->type))
2270 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2271 name, type->type);
2272
2273 return retmask;
2274 }
2275
2276 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2277 type_pred_t pred, unsigned int *typeformat_offset)
2278 {
2279 const var_t *var;
2280 const statement_t *stmt;
2281
2282 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2283 {
2284 const type_t *iface;
2285 if (stmt->type == STMT_LIBRARY)
2286 {
2287 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2288 continue;
2289 }
2290 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2291 continue;
2292
2293 iface = stmt->u.type;
2294 if (!pred(iface))
2295 continue;
2296
2297 if (iface->funcs)
2298 {
2299 const func_t *func;
2300 current_iface = iface;
2301 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2302 {
2303 if (is_local(func->def->attrs)) continue;
2304
2305 if (!is_void(get_func_return_type(func)))
2306 {
2307 var_t v = *func->def;
2308 v.type = get_func_return_type(func);
2309 update_tfsoff(get_func_return_type(func),
2310 write_typeformatstring_var(
2311 file, 2, NULL, get_func_return_type(func),
2312 &v, typeformat_offset),
2313 file);
2314 }
2315
2316 current_func = func;
2317 if (func->args)
2318 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2319 update_tfsoff(
2320 var->type,
2321 write_typeformatstring_var(
2322 file, 2, func, var->type, var,
2323 typeformat_offset),
2324 file);
2325 }
2326 }
2327 }
2328
2329 return *typeformat_offset + 1;
2330 }
2331
2332 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2333 {
2334 unsigned int typeformat_offset = 2;
2335
2336 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2337 }
2338
2339
2340 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2341 {
2342 int indent = 0;
2343
2344 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2345 print_file(file, indent, "{\n");
2346 indent++;
2347 print_file(file, indent, "0,\n");
2348 print_file(file, indent, "{\n");
2349 indent++;
2350 print_file(file, indent, "NdrFcShort(0x0),\n");
2351
2352 set_all_tfswrite(TRUE);
2353 process_tfs(file, stmts, pred);
2354
2355 print_file(file, indent, "0x0\n");
2356 indent--;
2357 print_file(file, indent, "}\n");
2358 indent--;
2359 print_file(file, indent, "};\n");
2360 print_file(file, indent, "\n");
2361 }
2362
2363 static unsigned int get_required_buffer_size_type(
2364 const type_t *type, const char *name, unsigned int *alignment)
2365 {
2366 *alignment = 0;
2367 if (is_user_type(type))
2368 {
2369 const char *uname;
2370 const type_t *utype = get_user_type(type, &uname);
2371 return get_required_buffer_size_type(utype, uname, alignment);
2372 }
2373 else
2374 {
2375 switch (type->type)
2376 {
2377 case RPC_FC_BYTE:
2378 case RPC_FC_CHAR:
2379 case RPC_FC_USMALL:
2380 case RPC_FC_SMALL:
2381 *alignment = 4;
2382 return 1;
2383
2384 case RPC_FC_WCHAR:
2385 case RPC_FC_USHORT:
2386 case RPC_FC_SHORT:
2387 case RPC_FC_ENUM16:
2388 *alignment = 4;
2389 return 2;
2390
2391 case RPC_FC_ULONG:
2392 case RPC_FC_LONG:
2393 case RPC_FC_ENUM32:
2394 case RPC_FC_FLOAT:
2395 case RPC_FC_ERROR_STATUS_T:
2396 *alignment = 4;
2397 return 4;
2398
2399 case RPC_FC_HYPER:
2400 case RPC_FC_DOUBLE:
2401 *alignment = 8;
2402 return 8;
2403
2404 case RPC_FC_IGNORE:
2405 case RPC_FC_BIND_PRIMITIVE:
2406 return 0;
2407
2408 case RPC_FC_STRUCT:
2409 case RPC_FC_PSTRUCT:
2410 {
2411 size_t size = 0;
2412 const var_t *field;
2413 if (!type->fields_or_args) return 0;
2414 LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2415 {
2416 unsigned int alignment;
2417 size += get_required_buffer_size_type(field->type, field->name,
2418 &alignment);
2419 }
2420 return size;
2421 }
2422
2423 case RPC_FC_RP:
2424 return
2425 is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
2426 ? get_required_buffer_size_type( type->ref, name, alignment )
2427 : 0;
2428
2429 case RPC_FC_SMFARRAY:
2430 case RPC_FC_LGFARRAY:
2431 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2432
2433 default:
2434 return 0;
2435 }
2436 }
2437 }
2438
2439 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2440 {
2441 int in_attr = is_attr(var->attrs, ATTR_IN);
2442 int out_attr = is_attr(var->attrs, ATTR_OUT);
2443 const type_t *t;
2444
2445 if (!in_attr && !out_attr)
2446 in_attr = 1;
2447
2448 *alignment = 0;
2449
2450 for (t = var->type; is_ptr(t); t = t->ref)
2451 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
2452 {
2453 *alignment = 4;
2454 return 20;
2455 }
2456
2457 if (pass == PASS_OUT)
2458 {
2459 if (out_attr && is_ptr(var->type))
2460 {
2461 type_t *type = var->type;
2462
2463 if (type->type == RPC_FC_STRUCT)
2464 {
2465 const var_t *field;
2466 unsigned int size = 36;
2467
2468 if (!type->fields_or_args) return size;
2469 LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2470 {
2471 unsigned int align;
2472 size += get_required_buffer_size_type(
2473 field->type, field->name, &align);
2474 }
2475 return size;
2476 }
2477 }
2478 return 0;
2479 }
2480 else
2481 {
2482 if ((!out_attr || in_attr) && !var->type->size_is
2483 && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2484 {
2485 if (is_ptr(var->type))
2486 {
2487 type_t *type = var->type;
2488
2489 if (is_base_type(type->type))
2490 {
2491 return 25;
2492 }
2493 else if (type->type == RPC_FC_STRUCT)
2494 {
2495 unsigned int size = 36;
2496 const var_t *field;
2497
2498 if (!type->fields_or_args) return size;
2499 LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2500 {
2501 unsigned int align;
2502 size += get_required_buffer_size_type(
2503 field->type, field->name, &align);
2504 }
2505 return size;
2506 }
2507 }
2508 }
2509
2510 return get_required_buffer_size_type(var->type, var->name, alignment);
2511 }
2512 }
2513
2514 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2515 {
2516 const var_t *var;
2517 unsigned int total_size = 0, alignment;
2518
2519 if (func->args)
2520 {
2521 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2522 {
2523 total_size += get_required_buffer_size(var, &alignment, pass);
2524 total_size += alignment;
2525 }
2526 }
2527
2528 if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2529 {
2530 var_t v = *func->def;
2531 v.type = get_func_return_type(func);
2532 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2533 total_size += alignment;
2534 }
2535 return total_size;
2536 }
2537
2538 static void print_phase_function(FILE *file, int indent, const char *type,
2539 enum remoting_phase phase,
2540 const var_t *var, unsigned int type_offset)
2541 {
2542 const char *function;
2543 switch (phase)
2544 {
2545 case PHASE_BUFFERSIZE:
2546 function = "BufferSize";
2547 break;
2548 case PHASE_MARSHAL:
2549 function = "Marshall";
2550 break;
2551 case PHASE_UNMARSHAL:
2552 function = "Unmarshall";
2553 break;
2554 case PHASE_FREE:
2555 function = "Free";
2556 break;
2557 default:
2558 assert(0);
2559 return;
2560 }
2561
2562 print_file(file, indent, "Ndr%s%s(\n", type, function);
2563 indent++;
2564 print_file(file, indent, "&_StubMsg,\n");
2565 print_file(file, indent, "%s%s%s%s,\n",
2566 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2567 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2568 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2569 var->name);
2570 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2571 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2572 if (phase == PHASE_UNMARSHAL)
2573 print_file(file, indent, "0);\n");
2574 indent--;
2575 }
2576
2577 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2578 enum pass pass, const var_t *var,
2579 const char *varname)
2580 {
2581 type_t *type = var->type;
2582 unsigned int size;
2583 unsigned int alignment = 0;
2584 unsigned char rtype;
2585
2586 /* no work to do for other phases, buffer sizing is done elsewhere */
2587 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2588 return;
2589
2590 rtype = is_ptr(type) ? type->ref->type : type->type;
2591
2592 switch (rtype)
2593 {
2594 case RPC_FC_BYTE:
2595 case RPC_FC_CHAR:
2596 case RPC_FC_SMALL:
2597 case RPC_FC_USMALL:
2598 size = 1;
2599 alignment = 1;
2600 break;
2601
2602 case RPC_FC_WCHAR:
2603 case RPC_FC_USHORT:
2604 case RPC_FC_SHORT:
2605 case RPC_FC_ENUM16:
2606 size = 2;
2607 alignment = 2;
2608 break;
2609
2610 case RPC_FC_ULONG:
2611 case RPC_FC_LONG:
2612 case RPC_FC_ENUM32:
2613 case RPC_FC_FLOAT:
2614 case RPC_FC_ERROR_STATUS_T:
2615 size = 4;
2616 alignment = 4;
2617 break;
2618
2619 case RPC_FC_HYPER:
2620 case RPC_FC_DOUBLE:
2621 size = 8;
2622 alignment = 8;
2623 break;
2624
2625 case RPC_FC_IGNORE:
2626 case RPC_FC_BIND_PRIMITIVE:
2627 /* no marshalling needed */
2628 return;
2629
2630 default:
2631 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2632 size = 0;
2633 }
2634
2635 if (phase == PHASE_MARSHAL)
2636 print_file(file, indent, "MIDL_memset(_StubMsg.Buffer, 0, (0x%x - (long)_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2637 print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2638 alignment - 1, alignment - 1);
2639
2640 if (phase == PHASE_MARSHAL)
2641 {
2642 print_file(file, indent, "*(");
2643 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2644 if (is_ptr(type))
2645 fprintf(file, " *)_StubMsg.Buffer = *");
2646 else
2647 fprintf(file, " *)_StubMsg.Buffer = ");
2648 fprintf(file, "%s", varname);
2649 fprintf(file, ";\n");
2650 }
2651 else if (phase == PHASE_UNMARSHAL)
2652 {
2653 print_file(file, indent, "if (_StubMsg.Buffer + sizeof(");
2654 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2655 fprintf(file, ") > _StubMsg.BufferEnd)\n");
2656 print_file(file, indent, "{\n");
2657 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2658 print_file(file, indent, "}\n");
2659 if (pass == PASS_IN || pass == PASS_RETURN)
2660 print_file(file, indent, "");
2661 else
2662 print_file(file, indent, "*");
2663 fprintf(file, "%s", varname);
2664 if (pass == PASS_IN && is_ptr(type))
2665 fprintf(file, " = (");
2666 else
2667 fprintf(file, " = *(");
2668 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2669 fprintf(file, " *)_StubMsg.Buffer;\n");
2670 }
2671
2672 print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2673 write_type_decl(file, var->type, NULL);
2674 fprintf(file, ");\n");
2675 }
2676
2677 /* returns whether the MaxCount, Offset or ActualCount members need to be
2678 * filled in for the specified phase */
2679 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2680 {
2681 return (phase != PHASE_UNMARSHAL);
2682 }
2683
2684 expr_t *get_size_is_expr(const type_t *t, const char *name)
2685 {
2686 expr_t *x = NULL;
2687
2688 for ( ; is_ptr(t) || is_array(t); t = t->ref)
2689 if (t->size_is)
2690 {
2691 if (!x)
2692 x = t->size_is;
2693 else
2694 error("%s: multidimensional conformant"
2695 " arrays not supported at the top level\n",
2696 name);
2697 }
2698
2699 return x;
2700 }
2701
2702 static void write_remoting_arg(FILE *file, int indent, const func_t *func,
2703 enum pass pass, enum remoting_phase phase,
2704 const var_t *var)
2705 {
2706 int in_attr, out_attr, pointer_type;
2707 const type_t *type = var->type;
2708 unsigned char rtype;
2709 size_t start_offset = type->typestring_offset;
2710
2711 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2712 if (!pointer_type)
2713 pointer_type = RPC_FC_RP;
2714
2715 in_attr = is_attr(var->attrs, ATTR_IN);
2716 out_attr = is_attr(var->attrs, ATTR_OUT);
2717 if (!in_attr && !out_attr)
2718 in_attr = 1;
2719
2720 if (phase != PHASE_FREE)
2721 switch (pass)
2722 {
2723 case PASS_IN:
2724 if (!in_attr) return;
2725 break;
2726 case PASS_OUT:
2727 if (!out_attr) return;
2728 break;
2729 case PASS_RETURN:
2730 break;
2731 }
2732
2733 rtype = type->type;
2734
2735 if (is_context_handle(type))
2736 {
2737 if (phase == PHASE_MARSHAL)
2738 {
2739 if (pass == PASS_IN)
2740 {
2741 /* if the context_handle attribute appears in the chain of types
2742 * without pointers being followed, then the context handle must
2743 * be direct, otherwise it is a pointer */
2744 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
2745 print_file(file, indent, "NdrClientContextMarshall(\n");
2746 print_file(file, indent + 1, "&_StubMsg,\n");
2747 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s,\n", is_ch_ptr ? "*" : "", var->name);
2748 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
2749 }
2750 else
2751 {
2752 print_file(file, indent, "NdrServerContextNewMarshall(\n");
2753 print_file(file, indent + 1, "&_StubMsg,\n");
2754 print_file(file, indent + 1, "(NDR_SCONTEXT)%s,\n", var->name);
2755 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
2756 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
2757 }
2758 }
2759 else if (phase == PHASE_UNMARSHAL)
2760 {
2761 if (pass == PASS_OUT)
2762 {
2763 if (!in_attr)
2764 print_file(file, indent, "*%s = 0;\n", var->name);
2765 print_file(file, indent, "NdrClientContextUnmarshall(\n");
2766 print_file(file, indent + 1, "&_StubMsg,\n");
2767 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s,\n", var->name);
2768 print_file(file, indent + 1, "_Handle);\n");
2769 }
2770 else
2771 {
2772 print_file(file, indent, "%s = NdrServerContextNewUnmarshall(\n", var->name);
2773 print_file(file, indent + 1, "&_StubMsg,\n");
2774 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
2775 }
2776 }
2777 }
2778 else if (is_user_type(var->type))
2779 {
2780 print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2781 }
2782 else if (is_string_type(var->attrs, var->type))
2783 {
2784 if (is_array(type) && !is_conformant_array(type))
2785 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2786 else
2787 {
2788 if (type->size_is && is_size_needed_for_phase(phase))
2789 {
2790 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2791 write_expr(file, type->size_is, 1, 1, NULL, NULL);
2792 fprintf(file, ";\n");
2793 }
2794
2795 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
2796 print_phase_function(file, indent, "Pointer", phase, var,
2797 start_offset - (type->size_is ? 4 : 2));
2798 else
2799 print_phase_function(file, indent, "ConformantString", phase, var,
2800 start_offset);
2801 }
2802 }
2803 else if (is_array(type))
2804 {
2805 unsigned char tc = type->type;
2806 const char *array_type = "FixedArray";
2807
2808 /* We already have the size_is expression since it's at the
2809 top level, but do checks for multidimensional conformant
2810 arrays. When we handle them, we'll need to extend this
2811 function to return a list, and then we'll actually use
2812 the return value. */
2813 get_size_is_expr(type, var->name);
2814
2815 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2816 {
2817 if (is_size_needed_for_phase(phase))
2818 {
2819 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2820 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2821 write_expr(file, type->length_is, 1, 1, NULL, NULL);
2822 fprintf(file, ";\n\n");
2823 }
2824 array_type = "VaryingArray";
2825 }
2826 else if (tc == RPC_FC_CARRAY)
2827 {
2828 if (is_size_needed_for_phase(phase))
2829 {
2830 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2831 write_expr(file, type->size_is, 1, 1, NULL, NULL);
2832 fprintf(file, ";\n\n");
2833 }
2834 array_type = "ConformantArray";
2835 }
2836 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
2837 {
2838 if (is_size_needed_for_phase(phase))
2839 {
2840 if (type->size_is)
2841 {
2842 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2843 write_expr(file, type->size_is, 1, 1, NULL, NULL);
2844 fprintf(file, ";\n");
2845 }
2846 if (type->length_is)
2847 {
2848 print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2849 print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2850 write_expr(file, type->length_is, 1, 1, NULL, NULL);
2851 fprintf(file, ";\n\n");
2852 }
2853 }
2854 array_type = (tc == RPC_FC_BOGUS_ARRAY
2855 ? "ComplexArray"
2856 : "ConformantVaryingArray");
2857 }
2858
2859 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
2860 print_phase_function(file, indent, array_type, phase, var, start_offset);
2861 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
2862 {
2863 /* these are all unmarshalled by allocating memory */
2864 if (type->type == RPC_FC_BOGUS_ARRAY ||
2865 type->type == RPC_FC_CVARRAY ||
2866 ((type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY) && in_attr) ||
2867 (type->type == RPC_FC_CARRAY && !in_attr))
2868 {
2869 print_file(file, indent, "if (%s)\n", var->name);
2870 indent++;
2871 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2872 }
2873 }
2874 }
2875 else if (!is_ptr(var->type) && is_base_type(rtype))
2876 {
2877 if (phase != PHASE_FREE)
2878 print_phase_basetype(file, indent, phase, pass, var, var->name);
2879 }
2880 else if (!is_ptr(var->type))
2881 {
2882 switch (rtype)
2883 {
2884 case RPC_FC_STRUCT:
2885 case RPC_FC_PSTRUCT:
2886 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2887 break;
2888 case RPC_FC_CSTRUCT:
2889 case RPC_FC_CPSTRUCT:
2890 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2891 break;
2892 case RPC_FC_CVSTRUCT:
2893 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2894 break;
2895 case RPC_FC_BOGUS_STRUCT:
2896 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2897 break;
2898 case RPC_FC_RP:
2899 if (is_base_type( var->type->ref->type ))
2900 {
2901 print_phase_basetype(file, indent, phase, pass, var, var->name);
2902 }
2903 else if (var->type->ref->type == RPC_FC_STRUCT)
2904 {
2905 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2906 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2907 }
2908 else
2909 {
2910 expr_t *iid;
2911 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2912 {
2913 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long) " );
2914 write_expr( file, iid, 1, 1, NULL, NULL );
2915 fprintf( file, ";\n\n" );
2916 }
2917 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2918 }
2919 break;
2920 default:
2921 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2922 }
2923 }
2924 else
2925 {
2926 if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2927 {
2928 if (phase != PHASE_FREE)
2929 print_phase_basetype(file, indent, phase, pass, var, var->name);
2930 }
2931 else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2932 {
2933 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2934 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2935 }
2936 else
2937 {
2938 expr_t *iid;
2939 expr_t *sx = get_size_is_expr(type, var->name);
2940
2941 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2942 {
2943 print_file( file, indent, "_StubMsg.MaxCount = (unsigned long) " );
2944 write_expr( file, iid, 1, 1, NULL, NULL );
2945 fprintf( file, ";\n\n" );
2946 }
2947 else if (sx)
2948 {
2949 print_file(file, indent, "_StubMsg.MaxCount = (unsigned long) ");
2950 write_expr(file, sx, 1, 1, NULL, NULL);
2951 fprintf(file, ";\n\n");
2952 }
2953 if (var->type->ref->type == RPC_FC_IP)
2954 print_phase_function(file, indent, "InterfacePointer", phase, var, start_offset);
2955 else
2956 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2957 }
2958 }
2959 fprintf(file, "\n");
2960 }
2961
2962 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2963 enum pass pass, enum remoting_phase phase)
2964 {
2965 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
2966 {
2967 unsigned int size = get_function_buffer_size( func, pass );
2968 print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2969 }
2970
2971 if (pass == PASS_RETURN)
2972 {
2973 var_t var;
2974 var = *func->def;
2975 var.type = get_func_return_type(func);
2976 var.name = xstrdup( "_RetVal" );
2977 write_remoting_arg( file, indent, func, pass, phase, &var );
2978 free( var.name );
2979 }