widl: Use type_get_type to determine the types of types during C client, server and...
[wine/multimedia.git] / tools / widl / typegen.c
blob9e58227450d64c63a11ab2f3e58c274c5b8f1f5f
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
62 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
63 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
66 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
68 type_t *type,
69 const char *name, unsigned int *typestring_offset);
71 const char *string_of_type(unsigned char type)
73 switch (type)
75 case RPC_FC_BYTE: return "FC_BYTE";
76 case RPC_FC_CHAR: return "FC_CHAR";
77 case RPC_FC_SMALL: return "FC_SMALL";
78 case RPC_FC_USMALL: return "FC_USMALL";
79 case RPC_FC_WCHAR: return "FC_WCHAR";
80 case RPC_FC_SHORT: return "FC_SHORT";
81 case RPC_FC_USHORT: return "FC_USHORT";
82 case RPC_FC_LONG: return "FC_LONG";
83 case RPC_FC_ULONG: return "FC_ULONG";
84 case RPC_FC_FLOAT: return "FC_FLOAT";
85 case RPC_FC_HYPER: return "FC_HYPER";
86 case RPC_FC_DOUBLE: return "FC_DOUBLE";
87 case RPC_FC_ENUM16: return "FC_ENUM16";
88 case RPC_FC_ENUM32: return "FC_ENUM32";
89 case RPC_FC_IGNORE: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP: return "FC_RP";
92 case RPC_FC_UP: return "FC_UP";
93 case RPC_FC_OP: return "FC_OP";
94 case RPC_FC_FP: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY: return "FC_CARRAY";
108 case RPC_FC_CVARRAY: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112 case RPC_FC_POINTER: return "FC_POINTER";
113 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING: return "FC_CSTRING";
116 case RPC_FC_WSTRING: return "FC_WSTRING";
117 default:
118 error("string_of_type: unknown type 0x%02x\n", type);
119 return NULL;
123 unsigned char get_pointer_fc(const type_t *type)
125 assert(is_ptr(type));
126 /* FIXME: see corresponding hack in set_type - we shouldn't be getting
127 * the pointer type from an alias, rather determining it from the
128 * position */
129 return type->type;
132 static unsigned char get_enum_fc(const type_t *type)
134 assert(type_get_type(type) == TYPE_ENUM);
135 if (is_aliaschain_attr(type, ATTR_V1ENUM))
136 return RPC_FC_ENUM32;
137 else
138 return RPC_FC_ENUM16;
141 unsigned char get_struct_fc(const type_t *type)
143 int has_pointer = 0;
144 int has_conformance = 0;
145 int has_variance = 0;
146 var_t *field;
147 var_list_t *fields;
149 fields = type_struct_get_fields(type);
151 if (get_padding(fields))
152 return RPC_FC_BOGUS_STRUCT;
154 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
156 type_t *t = field->type;
158 if (is_user_type(t))
159 return RPC_FC_BOGUS_STRUCT;
161 if (t->declarray)
163 if (is_string_type(field->attrs, field->type))
165 if (is_conformant_array(t))
166 has_conformance = 1;
167 has_variance = 1;
168 continue;
171 if (is_array(type_array_get_element(field->type)))
172 return RPC_FC_BOGUS_STRUCT;
174 if (type_array_has_conformance(field->type))
176 has_conformance = 1;
177 if (list_next(fields, &field->entry))
178 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
179 field->name);
181 if (type_array_has_variance(t))
182 has_variance = 1;
184 t = type_array_get_element(t);
187 if (is_user_type(t))
188 return RPC_FC_BOGUS_STRUCT;
190 switch (type_get_type(t))
192 case TYPE_VOID:
193 case TYPE_ALIAS:
194 case TYPE_MODULE:
195 assert(0);
196 break;
197 case TYPE_BASIC:
198 break;
199 case TYPE_ENUM:
200 if (get_enum_fc(t) == RPC_FC_ENUM16)
201 return RPC_FC_BOGUS_STRUCT;
202 break;
203 case TYPE_POINTER:
204 if (get_pointer_fc(t) == RPC_FC_RP || pointer_size != 4)
205 return RPC_FC_BOGUS_STRUCT;
206 if (type_get_type(type_pointer_get_ref(t)) == TYPE_INTERFACE)
207 return RPC_FC_BOGUS_STRUCT;
208 has_pointer = 1;
209 break;
210 case TYPE_ARRAY:
212 unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
213 if (!ptr_type || ptr_type == RPC_FC_RP)
214 return RPC_FC_BOGUS_STRUCT;
215 else if (pointer_size != 4)
216 return RPC_FC_BOGUS_STRUCT;
217 has_pointer = 1;
218 break;
220 case TYPE_INTERFACE:
221 case TYPE_UNION:
222 case TYPE_ENCAPSULATED_UNION:
223 case TYPE_COCLASS:
224 case TYPE_FUNCTION:
225 return RPC_FC_BOGUS_STRUCT;
226 case TYPE_STRUCT:
228 unsigned char fc = get_struct_fc(t);
229 switch (fc)
231 case RPC_FC_STRUCT:
232 break;
233 case RPC_FC_CVSTRUCT:
234 has_conformance = 1;
235 has_variance = 1;
236 has_pointer = 1;
237 break;
239 case RPC_FC_CPSTRUCT:
240 has_conformance = 1;
241 if (list_next( fields, &field->entry ))
242 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
243 field->name);
244 has_pointer = 1;
245 break;
247 case RPC_FC_CSTRUCT:
248 has_conformance = 1;
249 if (list_next( fields, &field->entry ))
250 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
251 field->name);
252 break;
254 case RPC_FC_PSTRUCT:
255 has_pointer = 1;
256 break;
258 default:
259 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
260 /* fallthru - treat it as complex */
262 /* as soon as we see one of these these members, it's bogus... */
263 case RPC_FC_BOGUS_STRUCT:
264 return RPC_FC_BOGUS_STRUCT;
266 break;
271 if( has_variance )
273 if ( has_conformance )
274 return RPC_FC_CVSTRUCT;
275 else
276 return RPC_FC_BOGUS_STRUCT;
278 if( has_conformance && has_pointer )
279 return RPC_FC_CPSTRUCT;
280 if( has_conformance )
281 return RPC_FC_CSTRUCT;
282 if( has_pointer )
283 return RPC_FC_PSTRUCT;
284 return RPC_FC_STRUCT;
287 unsigned char get_array_fc(const type_t *type)
289 unsigned char fc;
290 const expr_t *size_is;
291 const type_t *elem_type;
293 elem_type = type_array_get_element(type);
294 size_is = type_array_get_conformance(type);
296 if (!size_is)
298 unsigned int align = 0;
299 unsigned int size = type_memsize(elem_type, &align);
300 if (size * type_array_get_dim(type) > 0xffffuL)
301 fc = RPC_FC_LGFARRAY;
302 else
303 fc = RPC_FC_SMFARRAY;
305 else
306 fc = RPC_FC_CARRAY;
308 if (type_array_has_variance(type))
310 if (fc == RPC_FC_SMFARRAY)
311 fc = RPC_FC_SMVARRAY;
312 else if (fc == RPC_FC_LGFARRAY)
313 fc = RPC_FC_LGVARRAY;
314 else if (fc == RPC_FC_CARRAY)
315 fc = RPC_FC_CVARRAY;
318 if (is_user_type(elem_type))
319 fc = RPC_FC_BOGUS_ARRAY;
320 else switch (type_get_type(elem_type))
322 case TYPE_STRUCT:
323 switch (get_struct_fc(elem_type))
325 case RPC_FC_BOGUS_STRUCT:
326 fc = RPC_FC_BOGUS_ARRAY;
327 break;
329 break;
330 case TYPE_ENUM:
331 /* is 16-bit enum - if so, wire size differs from mem size and so
332 * the array cannot be block copied, which means the array is complex */
333 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
334 fc = RPC_FC_BOGUS_ARRAY;
335 break;
336 case TYPE_UNION:
337 case TYPE_ENCAPSULATED_UNION:
338 fc = RPC_FC_BOGUS_ARRAY;
339 break;
340 case TYPE_POINTER:
341 /* ref pointers cannot just be block copied. unique pointers to
342 * interfaces need special treatment. either case means the array is
343 * complex */
344 if (get_pointer_fc(elem_type) == RPC_FC_RP ||
345 type_get_type(type_pointer_get_ref(elem_type)) == TYPE_INTERFACE)
346 fc = RPC_FC_BOGUS_ARRAY;
347 break;
348 default:
349 /* nothing to do for everything else */
350 break;
353 return fc;
356 int is_struct(unsigned char type)
358 switch (type)
360 case RPC_FC_STRUCT:
361 case RPC_FC_PSTRUCT:
362 case RPC_FC_CSTRUCT:
363 case RPC_FC_CPSTRUCT:
364 case RPC_FC_CVSTRUCT:
365 case RPC_FC_BOGUS_STRUCT:
366 return 1;
367 default:
368 return 0;
372 static int is_non_complex_struct(const type_t *type)
374 return (type_get_type(type) == TYPE_STRUCT &&
375 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
378 static int type_has_pointers(const type_t *type)
380 if (is_user_type(type))
381 return FALSE;
383 switch (type_get_type(type))
385 case TYPE_POINTER:
386 return TRUE;
387 case TYPE_ARRAY:
388 return type_has_pointers(type_array_get_element(type));
389 case TYPE_STRUCT:
391 var_list_t *fields = type_struct_get_fields(type);
392 const var_t *field;
393 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
395 if (type_has_pointers(field->type))
396 return TRUE;
398 break;
400 case TYPE_UNION:
401 case TYPE_ENCAPSULATED_UNION:
403 var_list_t *fields;
404 const var_t *field;
405 fields = type_union_get_cases(type);
406 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
408 if (field->type && type_has_pointers(field->type))
409 return TRUE;
411 break;
413 default:
414 break;
417 return FALSE;
420 static int type_has_full_pointer(const type_t *type)
422 if (is_user_type(type))
423 return FALSE;
425 switch (type_get_type(type))
427 case TYPE_POINTER:
428 if (get_pointer_fc(type) == RPC_FC_FP)
429 return TRUE;
430 else
431 return FALSE;
432 case TYPE_ARRAY:
433 return type_has_full_pointer(type_array_get_element(type));
434 case TYPE_STRUCT:
436 var_list_t *fields = type_struct_get_fields(type);
437 const var_t *field;
438 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
440 if (type_has_full_pointer(field->type))
441 return TRUE;
443 break;
445 case TYPE_UNION:
446 case TYPE_ENCAPSULATED_UNION:
448 var_list_t *fields;
449 const var_t *field;
450 fields = type_union_get_cases(type);
451 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
453 if (field->type && type_has_full_pointer(field->type))
454 return TRUE;
456 break;
458 default:
459 break;
462 return FALSE;
465 static unsigned short user_type_offset(const char *name)
467 user_type_t *ut;
468 unsigned short off = 0;
469 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
471 if (strcmp(name, ut->name) == 0)
472 return off;
473 ++off;
475 error("user_type_offset: couldn't find type (%s)\n", name);
476 return 0;
479 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
481 type->typestring_offset = offset;
482 if (file) type->tfswrite = FALSE;
485 static void guard_rec(type_t *type)
487 /* types that contain references to themselves (like a linked list),
488 need to be shielded from infinite recursion when writing embedded
489 types */
490 if (type->typestring_offset)
491 type->tfswrite = FALSE;
492 else
493 type->typestring_offset = 1;
496 static type_t *get_user_type(const type_t *t, const char **pname)
498 for (;;)
500 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
501 if (ut)
503 if (pname)
504 *pname = t->name;
505 return ut;
508 if (type_is_alias(t))
509 t = type_alias_get_aliasee(t);
510 else
511 return 0;
515 int is_user_type(const type_t *t)
517 return get_user_type(t, NULL) != NULL;
520 static int is_embedded_complex(const type_t *type)
522 if (is_user_type(type))
523 return TRUE;
525 switch (type_get_type(type))
527 case TYPE_STRUCT:
528 case TYPE_UNION:
529 case TYPE_ENCAPSULATED_UNION:
530 case TYPE_ARRAY:
531 return TRUE;
532 case TYPE_POINTER:
533 return type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE;
534 default:
535 return FALSE;
539 static const char *get_context_handle_type_name(const type_t *type)
541 const type_t *t;
542 for (t = type;
543 is_ptr(t) || type_is_alias(t);
544 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
545 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
546 return t->name;
547 assert(0);
548 return NULL;
551 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
552 do { \
553 if (file) \
554 fprintf(file, "/* %2u */\n", typestring_offset); \
555 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
557 while (0)
559 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
560 static void print_file(FILE *file, int indent, const char *format, ...)
562 va_list va;
563 va_start(va, format);
564 print(file, indent, format, va);
565 va_end(va);
568 void print(FILE *file, int indent, const char *format, va_list va)
570 if (file)
572 if (format[0] != '\n')
573 while (0 < indent--)
574 fprintf(file, " ");
575 vfprintf(file, format, va);
580 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
582 if (decl_indirect(t))
584 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
585 local_var_prefix, n, local_var_prefix, n);
586 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
588 else if (is_ptr(t) || is_array(t))
589 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
592 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
594 const var_t *var;
596 if (!is_void(type_function_get_rettype(func->type)))
597 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
599 if (!type_get_function_args(func->type))
600 return;
602 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
603 write_var_init(file, indent, var->type, var->name, local_var_prefix);
605 fprintf(file, "\n");
608 static void write_formatdesc(FILE *f, int indent, const char *str)
610 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
611 print_file(f, indent, "{\n");
612 print_file(f, indent + 1, "short Pad;\n");
613 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
614 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
615 print_file(f, indent, "\n");
618 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
620 clear_all_offsets();
622 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
623 get_size_typeformatstring(stmts, pred));
625 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
626 get_size_procformatstring(stmts, pred));
628 fprintf(f, "\n");
629 write_formatdesc(f, indent, "TYPE");
630 write_formatdesc(f, indent, "PROC");
631 fprintf(f, "\n");
632 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
633 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
634 print_file(f, indent, "\n");
637 int decl_indirect(const type_t *t)
639 if (is_user_type(t))
640 return TRUE;
641 return (type_get_type(t) != TYPE_BASIC &&
642 type_get_type(t) != TYPE_ENUM &&
643 type_get_type(t) != TYPE_POINTER &&
644 type_get_type(t) != TYPE_ARRAY);
647 static unsigned int write_procformatstring_type(FILE *file, int indent,
648 const char *name,
649 const type_t *type,
650 const attr_list_t *attrs,
651 int is_return)
653 unsigned int size;
655 int is_in = is_attr(attrs, ATTR_IN);
656 int is_out = is_attr(attrs, ATTR_OUT);
658 if (!is_in && !is_out) is_in = TRUE;
660 if (type_get_type(type) == TYPE_BASIC ||
661 type_get_type(type) == TYPE_ENUM)
663 unsigned char fc;
665 if (is_return)
666 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
667 else
668 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
670 if (type_get_type(type) == TYPE_ENUM)
672 fc = get_enum_fc(type);
674 else
676 fc = type_basic_get_fc(type);
678 if (fc == RPC_FC_BIND_PRIMITIVE)
679 fc = RPC_FC_IGNORE;
682 print_file(file, indent, "0x%02x, /* %s */\n",
683 fc, string_of_type(fc));
684 size = 2; /* includes param type prefix */
686 else
688 if (is_return)
689 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
690 else if (is_in && is_out)
691 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
692 else if (is_out)
693 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
694 else
695 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
697 print_file(file, indent, "0x01,\n");
698 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
699 size = 4; /* includes param type prefix */
701 return size;
704 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
706 const statement_t *stmt;
707 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
709 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
711 const statement_t *stmt_func;
712 if (!pred(stmt->u.type))
713 continue;
714 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
716 const var_t *func = stmt_func->u.var;
717 if (is_local(func->attrs)) continue;
718 /* emit argument data */
719 if (type_get_function_args(func->type))
721 const var_t *var;
722 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
723 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
726 /* emit return value data */
727 if (is_void(type_function_get_rettype(func->type)))
729 print_file(file, indent, "0x5b, /* FC_END */\n");
730 print_file(file, indent, "0x5c, /* FC_PAD */\n");
732 else
733 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
736 else if (stmt->type == STMT_LIBRARY)
737 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
741 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
743 int indent = 0;
745 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
746 print_file(file, indent, "{\n");
747 indent++;
748 print_file(file, indent, "0,\n");
749 print_file(file, indent, "{\n");
750 indent++;
752 write_procformatstring_stmts(file, indent, stmts, pred);
754 print_file(file, indent, "0x0\n");
755 indent--;
756 print_file(file, indent, "}\n");
757 indent--;
758 print_file(file, indent, "};\n");
759 print_file(file, indent, "\n");
762 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
764 unsigned char fc;
766 if (type_get_type(type) == TYPE_BASIC)
767 fc = type_basic_get_fc(type);
768 else if (type_get_type(type) == TYPE_ENUM)
769 fc = get_enum_fc(type);
770 else
771 return 0;
773 if (convert_to_signed_type)
775 switch(fc)
777 case RPC_FC_USMALL:
778 fc = RPC_FC_SMALL;
779 break;
780 case RPC_FC_USHORT:
781 fc = RPC_FC_SHORT;
782 break;
783 case RPC_FC_ULONG:
784 fc = RPC_FC_LONG;
785 break;
789 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
790 *typestring_offset += 1;
791 return 1;
794 /* write conformance / variance descriptor */
795 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
796 unsigned int baseoff, const type_t *type,
797 const expr_t *expr)
799 unsigned char operator_type = 0;
800 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
801 const char *conftype_string = "";
802 const char *operator_string = "no operators";
803 const expr_t *subexpr;
805 if (!expr)
807 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
808 return 4;
811 if (!structure)
813 /* Top-level conformance calculations are done inline. */
814 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
815 RPC_FC_TOP_LEVEL_CONFORMANCE);
816 print_file (file, 2, "0x0,\n");
817 print_file (file, 2, "NdrFcShort(0x0),\n");
818 return 4;
821 if (expr->is_const)
823 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
824 error("write_conf_or_var_desc: constant value %ld is greater than "
825 "the maximum constant size of %d\n", expr->cval,
826 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
828 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
829 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
830 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
831 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
833 return 4;
836 if (is_ptr(type) || (is_array(type) && !type->declarray))
838 conftype = RPC_FC_POINTER_CONFORMANCE;
839 conftype_string = "field pointer, ";
842 subexpr = expr;
843 switch (subexpr->type)
845 case EXPR_PPTR:
846 subexpr = subexpr->ref;
847 operator_type = RPC_FC_DEREFERENCE;
848 operator_string = "FC_DEREFERENCE";
849 break;
850 case EXPR_DIV:
851 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
853 subexpr = subexpr->ref;
854 operator_type = RPC_FC_DIV_2;
855 operator_string = "FC_DIV_2";
857 break;
858 case EXPR_MUL:
859 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
861 subexpr = subexpr->ref;
862 operator_type = RPC_FC_MULT_2;
863 operator_string = "FC_MULT_2";
865 break;
866 case EXPR_SUB:
867 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
869 subexpr = subexpr->ref;
870 operator_type = RPC_FC_SUB_1;
871 operator_string = "FC_SUB_1";
873 break;
874 case EXPR_ADD:
875 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
877 subexpr = subexpr->ref;
878 operator_type = RPC_FC_ADD_1;
879 operator_string = "FC_ADD_1";
881 break;
882 default:
883 break;
886 if (subexpr->type == EXPR_IDENTIFIER)
888 const type_t *correlation_variable = NULL;
889 unsigned char param_type = 0;
890 unsigned int offset = 0;
891 const var_t *var;
892 var_list_t *fields = type_struct_get_fields(structure);
894 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
896 unsigned int align = 0;
897 /* FIXME: take alignment into account */
898 if (var->name && !strcmp(var->name, subexpr->u.sval))
900 correlation_variable = var->type;
901 break;
903 offset += type_memsize(var->type, &align);
905 if (!correlation_variable)
906 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
907 subexpr->u.sval);
909 correlation_variable = expr_resolve_type(NULL, structure, expr);
911 offset -= baseoff;
913 if (type_get_type(correlation_variable) == TYPE_BASIC)
915 switch (type_basic_get_fc(correlation_variable))
917 case RPC_FC_CHAR:
918 case RPC_FC_SMALL:
919 param_type = RPC_FC_SMALL;
920 break;
921 case RPC_FC_BYTE:
922 case RPC_FC_USMALL:
923 param_type = RPC_FC_USMALL;
924 break;
925 case RPC_FC_WCHAR:
926 case RPC_FC_SHORT:
927 param_type = RPC_FC_SHORT;
928 break;
929 case RPC_FC_USHORT:
930 param_type = RPC_FC_USHORT;
931 break;
932 case RPC_FC_LONG:
933 param_type = RPC_FC_LONG;
934 break;
935 case RPC_FC_ULONG:
936 param_type = RPC_FC_ULONG;
937 break;
938 default:
939 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
940 type_basic_get_fc(correlation_variable));
943 else if (type_get_type(correlation_variable) == TYPE_ENUM)
945 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
946 param_type = RPC_FC_LONG;
947 else
948 param_type = RPC_FC_SHORT;
950 else
952 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
953 subexpr->u.sval);
954 return 0;
957 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
958 conftype | param_type, conftype_string, string_of_type(param_type));
959 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
960 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
961 offset, offset);
963 else
965 unsigned int callback_offset = 0;
966 struct expr_eval_routine *eval;
967 int found = 0;
969 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
971 if (!strcmp (eval->structure->name, structure->name)
972 && !compare_expr (eval->expr, expr))
974 found = 1;
975 break;
977 callback_offset++;
980 if (!found)
982 eval = xmalloc (sizeof(*eval));
983 eval->structure = structure;
984 eval->baseoff = baseoff;
985 eval->expr = expr;
986 list_add_tail (&expr_eval_routines, &eval->entry);
989 if (callback_offset > USHRT_MAX)
990 error("Maximum number of callback routines reached\n");
992 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
993 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
994 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
996 return 4;
999 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1001 int have_align = FALSE;
1002 unsigned int size = 0;
1003 const var_t *v;
1005 if (!fields) return 0;
1006 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1008 unsigned int falign = 0;
1009 unsigned int fsize = type_memsize(v->type, &falign);
1010 if (!have_align)
1012 *align = falign;
1013 have_align = TRUE;
1015 size = ROUND_SIZE(size, falign);
1016 size += fsize;
1019 size = ROUND_SIZE(size, *align);
1020 return size;
1023 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1025 unsigned int size, maxs = 0;
1026 unsigned int align = *pmaxa;
1027 const var_t *v;
1029 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1031 /* we could have an empty default field with NULL type */
1032 if (v->type)
1034 size = type_memsize(v->type, &align);
1035 if (maxs < size) maxs = size;
1036 if (*pmaxa < align) *pmaxa = align;
1040 return maxs;
1043 int get_padding(const var_list_t *fields)
1045 unsigned short offset = 0;
1046 int salign = -1;
1047 const var_t *f;
1049 if (!fields)
1050 return 0;
1052 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1054 type_t *ft = f->type;
1055 unsigned int align = 0;
1056 unsigned int size = type_memsize(ft, &align);
1057 if (salign == -1)
1058 salign = align;
1059 offset = ROUND_SIZE(offset, align);
1060 offset += size;
1063 return ROUNDING(offset, salign);
1066 unsigned int type_memsize(const type_t *t, unsigned int *align)
1068 unsigned int size = 0;
1070 switch (type_get_type(t))
1072 case TYPE_BASIC:
1073 switch (type_basic_get_fc(t))
1075 case RPC_FC_BYTE:
1076 case RPC_FC_CHAR:
1077 case RPC_FC_USMALL:
1078 case RPC_FC_SMALL:
1079 size = 1;
1080 if (size > *align) *align = size;
1081 break;
1082 case RPC_FC_WCHAR:
1083 case RPC_FC_USHORT:
1084 case RPC_FC_SHORT:
1085 size = 2;
1086 if (size > *align) *align = size;
1087 break;
1088 case RPC_FC_ULONG:
1089 case RPC_FC_LONG:
1090 case RPC_FC_ERROR_STATUS_T:
1091 case RPC_FC_FLOAT:
1092 size = 4;
1093 if (size > *align) *align = size;
1094 break;
1095 case RPC_FC_HYPER:
1096 case RPC_FC_DOUBLE:
1097 size = 8;
1098 if (size > *align) *align = size;
1099 break;
1100 default:
1101 error("type_memsize: Unknown type 0x%x\n", type_basic_get_fc(t));
1102 size = 0;
1104 break;
1105 case TYPE_ENUM:
1106 switch (get_enum_fc(t))
1108 case RPC_FC_ENUM32:
1109 size = 4;
1110 if (size > *align) *align = size;
1111 break;
1112 case RPC_FC_ENUM16:
1113 size = 2;
1114 if (size > *align) *align = size;
1115 break;
1116 default:
1117 error("type_memsize: Unknown enum type\n");
1118 size = 0;
1120 break;
1121 case TYPE_STRUCT:
1122 size = fields_memsize(type_struct_get_fields(t), align);
1123 break;
1124 case TYPE_ENCAPSULATED_UNION:
1125 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1126 break;
1127 case TYPE_UNION:
1128 size = union_memsize(type_union_get_cases(t), align);
1129 break;
1130 case TYPE_POINTER:
1131 assert( pointer_size );
1132 size = pointer_size;
1133 if (size > *align) *align = size;
1134 break;
1135 case TYPE_ARRAY:
1136 if (t->declarray)
1138 if (is_conformant_array(t))
1140 type_memsize(type_array_get_element(t), align);
1141 size = 0;
1143 else
1144 size = type_array_get_dim(t) *
1145 type_memsize(type_array_get_element(t), align);
1147 else /* declared as a pointer */
1149 assert( pointer_size );
1150 size = pointer_size;
1151 if (size > *align) *align = size;
1153 break;
1154 case TYPE_INTERFACE:
1155 case TYPE_ALIAS:
1156 case TYPE_VOID:
1157 case TYPE_COCLASS:
1158 case TYPE_MODULE:
1159 case TYPE_FUNCTION:
1160 /* these types should not be encountered here due to language
1161 * restrictions (interface, void, coclass, module), logical
1162 * restrictions (alias - due to type_get_type call above) or
1163 * checking restrictions (function). */
1164 assert(0);
1167 return size;
1170 int is_full_pointer_function(const var_t *func)
1172 const var_t *var;
1173 if (type_has_full_pointer(type_function_get_rettype(func->type)))
1174 return TRUE;
1175 if (!type_get_function_args(func->type))
1176 return FALSE;
1177 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1178 if (type_has_full_pointer( var->type ))
1179 return TRUE;
1180 return FALSE;
1183 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1185 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1186 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1187 fprintf(file, "\n");
1190 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1192 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1193 fprintf(file, "\n");
1196 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, unsigned int offset)
1198 short absoff = type_pointer_get_ref(type)->typestring_offset;
1199 short reloff = absoff - (offset + 2);
1200 int ptr_attr = is_ptr(type_pointer_get_ref(type)) ? 0x10 : 0x0;
1201 unsigned char pointer_fc = get_pointer_fc(type);
1203 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1204 pointer_fc, ptr_attr, string_of_type(pointer_fc));
1205 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1206 reloff, reloff, absoff);
1207 return 4;
1210 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1212 unsigned char fc;
1213 unsigned char pointer_fc;
1214 const type_t *ref;
1216 /* for historical reasons, write_simple_pointer also handled string types,
1217 * but no longer does. catch bad uses of the function with this check */
1218 if (is_string_type(type->attrs, type))
1219 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1221 pointer_fc = get_pointer_fc(type);
1223 ref = type_pointer_get_ref(type);
1224 if (type_get_type(ref) == TYPE_ENUM)
1225 fc = get_enum_fc(ref);
1226 else
1227 fc = type_basic_get_fc(ref);
1229 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1230 pointer_fc, string_of_type(pointer_fc));
1231 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1232 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1233 return 4;
1236 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1238 print_file(file, 0, "/* %u (", tfsoff);
1239 write_type_decl(file, t, NULL);
1240 print_file(file, 0, ") */\n");
1243 static unsigned int write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1245 unsigned int offset = *typestring_offset;
1246 type_t *ref = type_pointer_get_ref(type);
1248 print_start_tfs_comment(file, type, offset);
1249 update_tfsoff(type, offset, file);
1251 if (ref->typestring_offset)
1252 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1253 else if (type_get_type(ref) == TYPE_BASIC ||
1254 type_get_type(ref) == TYPE_ENUM)
1255 *typestring_offset += write_simple_pointer(file, type);
1257 return offset;
1260 static int processed(const type_t *type)
1262 return type->typestring_offset && !type->tfswrite;
1265 static int user_type_has_variable_size(const type_t *t)
1267 if (is_ptr(t))
1268 return TRUE;
1269 else if (type_get_type(t) == TYPE_STRUCT)
1271 switch (get_struct_fc(t))
1273 case RPC_FC_PSTRUCT:
1274 case RPC_FC_CSTRUCT:
1275 case RPC_FC_CPSTRUCT:
1276 case RPC_FC_CVSTRUCT:
1277 return TRUE;
1280 /* Note: Since this only applies to user types, we can't have a conformant
1281 array here, and strings should get filed under pointer in this case. */
1282 return FALSE;
1285 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1287 unsigned int start, absoff, flags;
1288 unsigned int align = 0, ualign = 0;
1289 const char *name = NULL;
1290 type_t *utype = get_user_type(type, &name);
1291 unsigned int usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1292 unsigned int size = type_memsize(type, &align);
1293 unsigned short funoff = user_type_offset(name);
1294 short reloff;
1296 guard_rec(type);
1298 if (type_get_type(utype) == TYPE_BASIC ||
1299 type_get_type(utype) == TYPE_ENUM)
1301 unsigned char fc;
1303 if (type_get_type(utype) == TYPE_ENUM)
1304 fc = get_enum_fc(utype);
1305 else
1306 fc = type_basic_get_fc(utype);
1308 absoff = *tfsoff;
1309 print_start_tfs_comment(file, utype, absoff);
1310 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1311 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1312 *tfsoff += 2;
1314 else
1316 if (!processed(utype))
1317 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1318 absoff = utype->typestring_offset;
1321 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype) == RPC_FC_RP)
1322 flags = 0x40;
1323 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype) == RPC_FC_UP)
1324 flags = 0x80;
1325 else
1326 flags = 0;
1328 start = *tfsoff;
1329 update_tfsoff(type, start, file);
1330 print_start_tfs_comment(file, type, start);
1331 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1332 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1333 flags | (align - 1), align - 1, flags);
1334 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1335 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1336 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1337 *tfsoff += 8;
1338 reloff = absoff - *tfsoff;
1339 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1340 *tfsoff += 2;
1343 static void write_member_type(FILE *file, const type_t *cont,
1344 int cont_is_complex, const attr_list_t *attrs,
1345 const type_t *type, unsigned int *corroff,
1346 unsigned int *tfsoff)
1348 if (is_embedded_complex(type) && !is_conformant_array(type))
1350 unsigned int absoff;
1351 short reloff;
1353 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1355 absoff = *corroff;
1356 *corroff += 8;
1358 else
1360 absoff = type->typestring_offset;
1362 reloff = absoff - (*tfsoff + 2);
1364 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1365 /* FIXME: actually compute necessary padding */
1366 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1367 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1368 reloff, reloff, absoff);
1369 *tfsoff += 4;
1371 else if (is_ptr(type) || is_conformant_array(type))
1373 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1374 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1375 *tfsoff += 1;
1377 else if (!write_base_type(file, type, TRUE, tfsoff))
1378 error("Unsupported member type %d\n", type_get_type(type));
1381 static void write_end(FILE *file, unsigned int *tfsoff)
1383 if (*tfsoff % 2 == 0)
1385 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1386 *tfsoff += 1;
1388 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1389 *tfsoff += 1;
1392 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1394 unsigned int offset = 0;
1395 var_list_t *fs = type_struct_get_fields(type);
1396 var_t *f;
1398 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1400 unsigned int align = 0;
1401 type_t *ft = f->type;
1402 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1404 unsigned int absoff = ft->typestring_offset;
1405 short reloff = absoff - (*tfsoff + 6);
1406 print_file(file, 0, "/* %d */\n", *tfsoff);
1407 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1408 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1409 write_conf_or_var_desc(file, current_structure, offset, ft,
1410 get_attrp(f->attrs, ATTR_SWITCHIS));
1411 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1412 reloff, reloff, absoff);
1413 *tfsoff += 8;
1416 /* FIXME: take alignment into account */
1417 offset += type_memsize(ft, &align);
1421 static int write_no_repeat_pointer_descriptions(
1422 FILE *file, type_t *type,
1423 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1424 unsigned int *typestring_offset)
1426 int written = 0;
1427 unsigned int align;
1429 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1431 unsigned int memsize;
1433 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1434 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1436 /* pointer instance */
1437 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1438 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1439 *typestring_offset += 6;
1441 if (is_ptr(type))
1443 if (is_string_type(type->attrs, type))
1444 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1445 else
1446 write_pointer_tfs(file, type, typestring_offset);
1448 else
1450 unsigned absoff = type->typestring_offset;
1451 short reloff = absoff - (*typestring_offset + 2);
1452 /* FIXME: get pointer attributes from field */
1453 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1454 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1455 reloff, reloff, absoff);
1456 *typestring_offset += 4;
1459 align = 0;
1460 memsize = type_memsize(type, &align);
1461 *offset_in_memory += memsize;
1462 /* increment these separately as in the case of conformant (varying)
1463 * structures these start at different values */
1464 *offset_in_buffer += memsize;
1466 return 1;
1469 if (is_non_complex_struct(type))
1471 const var_t *v;
1472 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1474 if (offset_in_memory && offset_in_buffer)
1476 unsigned int padding;
1477 align = 0;
1478 type_memsize(v->type, &align);
1479 padding = ROUNDING(*offset_in_memory, align);
1480 *offset_in_memory += padding;
1481 *offset_in_buffer += padding;
1483 written += write_no_repeat_pointer_descriptions(
1484 file, v->type,
1485 offset_in_memory, offset_in_buffer, typestring_offset);
1488 else
1490 unsigned int memsize;
1491 align = 0;
1492 memsize = type_memsize(type, &align);
1493 *offset_in_memory += memsize;
1494 /* increment these separately as in the case of conformant (varying)
1495 * structures these start at different values */
1496 *offset_in_buffer += memsize;
1499 return written;
1502 static int write_pointer_description_offsets(
1503 FILE *file, const attr_list_t *attrs, type_t *type,
1504 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1505 unsigned int *typestring_offset)
1507 int written = 0;
1508 unsigned int align;
1510 if (is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE)
1512 type_t *ref = type_pointer_get_ref(type);
1514 if (offset_in_memory && offset_in_buffer)
1516 unsigned int memsize;
1518 /* pointer instance */
1519 /* FIXME: sometimes from end of structure, sometimes from beginning */
1520 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1521 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1523 align = 0;
1524 memsize = type_memsize(type, &align);
1525 *offset_in_memory += memsize;
1526 /* increment these separately as in the case of conformant (varying)
1527 * structures these start at different values */
1528 *offset_in_buffer += memsize;
1530 *typestring_offset += 4;
1532 if (is_string_type(attrs, type))
1533 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1534 else if (processed(ref) || type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1535 write_pointer_tfs(file, type, typestring_offset);
1536 else
1537 error("write_pointer_description_offsets: type format string unknown\n");
1539 return 1;
1542 if (is_array(type))
1544 return write_pointer_description_offsets(
1545 file, attrs, type_array_get_element(type), offset_in_memory,
1546 offset_in_buffer, typestring_offset);
1548 else if (is_non_complex_struct(type))
1550 /* otherwise search for interesting fields to parse */
1551 const var_t *v;
1552 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1554 if (offset_in_memory && offset_in_buffer)
1556 unsigned int padding;
1557 align = 0;
1558 type_memsize(v->type, &align);
1559 padding = ROUNDING(*offset_in_memory, align);
1560 *offset_in_memory += padding;
1561 *offset_in_buffer += padding;
1563 written += write_pointer_description_offsets(
1564 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1565 typestring_offset);
1568 else
1570 if (offset_in_memory && offset_in_buffer)
1572 unsigned int memsize;
1573 align = 0;
1574 memsize = type_memsize(type, &align);
1575 *offset_in_memory += memsize;
1576 /* increment these separately as in the case of conformant (varying)
1577 * structures these start at different values */
1578 *offset_in_buffer += memsize;
1582 return written;
1585 /* Note: if file is NULL return value is number of pointers to write, else
1586 * it is the number of type format characters written */
1587 static int write_fixed_array_pointer_descriptions(
1588 FILE *file, const attr_list_t *attrs, type_t *type,
1589 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1590 unsigned int *typestring_offset)
1592 unsigned int align;
1593 int pointer_count = 0;
1595 if (type_get_type(type) == TYPE_ARRAY &&
1596 !type_array_has_conformance(type) && !type_array_has_variance(type))
1598 unsigned int temp = 0;
1599 /* unfortunately, this needs to be done in two passes to avoid
1600 * writing out redundant FC_FIXED_REPEAT descriptions */
1601 pointer_count = write_pointer_description_offsets(
1602 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1603 if (pointer_count > 0)
1605 unsigned int increment_size;
1606 unsigned int offset_of_array_pointer_mem = 0;
1607 unsigned int offset_of_array_pointer_buf = 0;
1609 align = 0;
1610 increment_size = type_memsize(type_array_get_element(type), &align);
1612 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1613 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1614 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1615 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1616 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1617 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1618 *typestring_offset += 10;
1620 pointer_count = write_pointer_description_offsets(
1621 file, attrs, type, &offset_of_array_pointer_mem,
1622 &offset_of_array_pointer_buf, typestring_offset);
1625 else if (type_get_type(type) == TYPE_STRUCT)
1627 const var_t *v;
1628 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1630 if (offset_in_memory && offset_in_buffer)
1632 unsigned int padding;
1633 align = 0;
1634 type_memsize(v->type, &align);
1635 padding = ROUNDING(*offset_in_memory, align);
1636 *offset_in_memory += padding;
1637 *offset_in_buffer += padding;
1639 pointer_count += write_fixed_array_pointer_descriptions(
1640 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1641 typestring_offset);
1644 else
1646 if (offset_in_memory && offset_in_buffer)
1648 unsigned int memsize;
1649 align = 0;
1650 memsize = type_memsize(type, &align);
1651 *offset_in_memory += memsize;
1652 /* increment these separately as in the case of conformant (varying)
1653 * structures these start at different values */
1654 *offset_in_buffer += memsize;
1658 return pointer_count;
1661 /* Note: if file is NULL return value is number of pointers to write, else
1662 * it is the number of type format characters written */
1663 static int write_conformant_array_pointer_descriptions(
1664 FILE *file, const attr_list_t *attrs, type_t *type,
1665 unsigned int offset_in_memory, unsigned int *typestring_offset)
1667 unsigned int align;
1668 int pointer_count = 0;
1670 if (is_conformant_array(type) && !type_array_has_variance(type))
1672 unsigned int temp = 0;
1673 /* unfortunately, this needs to be done in two passes to avoid
1674 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1675 pointer_count = write_pointer_description_offsets(
1676 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1677 if (pointer_count > 0)
1679 unsigned int increment_size;
1680 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1681 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1683 align = 0;
1684 increment_size = type_memsize(type_array_get_element(type), &align);
1686 if (increment_size > USHRT_MAX)
1687 error("array size of %u bytes is too large\n", increment_size);
1689 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1690 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1691 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1692 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1693 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1694 *typestring_offset += 8;
1696 pointer_count = write_pointer_description_offsets(
1697 file, attrs, type_array_get_element(type),
1698 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1699 typestring_offset);
1703 return pointer_count;
1706 /* Note: if file is NULL return value is number of pointers to write, else
1707 * it is the number of type format characters written */
1708 static int write_varying_array_pointer_descriptions(
1709 FILE *file, const attr_list_t *attrs, type_t *type,
1710 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1711 unsigned int *typestring_offset)
1713 unsigned int align;
1714 int pointer_count = 0;
1716 if (is_array(type) && type_array_has_variance(type))
1718 unsigned int temp = 0;
1719 /* unfortunately, this needs to be done in two passes to avoid
1720 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1721 pointer_count = write_pointer_description_offsets(
1722 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1723 if (pointer_count > 0)
1725 unsigned int increment_size;
1727 align = 0;
1728 increment_size = type_memsize(type_array_get_element(type), &align);
1730 if (increment_size > USHRT_MAX)
1731 error("array size of %u bytes is too large\n", increment_size);
1733 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1734 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1735 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1736 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1737 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1738 *typestring_offset += 8;
1740 pointer_count = write_pointer_description_offsets(
1741 file, attrs, type, offset_in_memory,
1742 offset_in_buffer, typestring_offset);
1745 else if (type_get_type(type) == TYPE_STRUCT)
1747 const var_t *v;
1748 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1750 if (offset_in_memory && offset_in_buffer)
1752 unsigned int padding;
1754 if (is_array(v->type) && type_array_has_variance(v->type))
1756 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1757 /* skip over variance and offset in buffer */
1758 *offset_in_buffer += 8;
1761 align = 0;
1762 type_memsize(v->type, &align);
1763 padding = ROUNDING(*offset_in_memory, align);
1764 *offset_in_memory += padding;
1765 *offset_in_buffer += padding;
1767 pointer_count += write_varying_array_pointer_descriptions(
1768 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1769 typestring_offset);
1772 else
1774 if (offset_in_memory && offset_in_buffer)
1776 unsigned int memsize;
1777 align = 0;
1778 memsize = type_memsize(type, &align);
1779 *offset_in_memory += memsize;
1780 /* increment these separately as in the case of conformant (varying)
1781 * structures these start at different values */
1782 *offset_in_buffer += memsize;
1786 return pointer_count;
1789 static void write_pointer_description(FILE *file, type_t *type,
1790 unsigned int *typestring_offset)
1792 unsigned int offset_in_buffer;
1793 unsigned int offset_in_memory;
1795 /* pass 1: search for single instance of a pointer (i.e. don't descend
1796 * into arrays) */
1797 if (!is_array(type))
1799 offset_in_memory = 0;
1800 offset_in_buffer = 0;
1801 write_no_repeat_pointer_descriptions(
1802 file, type,
1803 &offset_in_memory, &offset_in_buffer, typestring_offset);
1806 /* pass 2: search for pointers in fixed arrays */
1807 offset_in_memory = 0;
1808 offset_in_buffer = 0;
1809 write_fixed_array_pointer_descriptions(
1810 file, NULL, type,
1811 &offset_in_memory, &offset_in_buffer, typestring_offset);
1813 /* pass 3: search for pointers in conformant only arrays (but don't descend
1814 * into conformant varying or varying arrays) */
1815 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1816 write_conformant_array_pointer_descriptions(
1817 file, NULL, type, 0, typestring_offset);
1818 else if (type_get_type(type) == TYPE_STRUCT &&
1819 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1821 unsigned int align = 0;
1822 type_t *carray = find_array_or_string_in_struct(type)->type;
1823 write_conformant_array_pointer_descriptions(
1824 file, NULL, carray,
1825 type_memsize(type, &align),
1826 typestring_offset);
1829 /* pass 4: search for pointers in varying arrays */
1830 offset_in_memory = 0;
1831 offset_in_buffer = 0;
1832 write_varying_array_pointer_descriptions(
1833 file, NULL, type,
1834 &offset_in_memory, &offset_in_buffer, typestring_offset);
1837 int is_declptr(const type_t *t)
1839 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1842 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1843 type_t *type,
1844 const char *name, unsigned int *typestring_offset)
1846 unsigned int start_offset;
1847 unsigned char rtype;
1848 type_t *elem_type;
1850 if (is_declptr(type))
1852 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1853 int pointer_type = is_ptr(type) ? get_pointer_fc(type) : get_attrv(attrs, ATTR_POINTERTYPE);
1854 if (!pointer_type)
1855 pointer_type = RPC_FC_RP;
1856 print_start_tfs_comment(file, type, *typestring_offset);
1857 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1858 pointer_type, flag, string_of_type(pointer_type),
1859 flag ? " [simple_pointer]" : "");
1860 *typestring_offset += 2;
1861 if (!flag)
1863 print_file(file, 2, "NdrFcShort(0x2),\n");
1864 *typestring_offset += 2;
1868 start_offset = *typestring_offset;
1869 update_tfsoff(type, start_offset, file);
1871 if (is_array(type))
1872 elem_type = type_array_get_element(type);
1873 else
1874 elem_type = type_pointer_get_ref(type);
1876 if (type_get_type(elem_type) != TYPE_BASIC)
1878 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
1879 return start_offset;
1882 rtype = type_basic_get_fc(elem_type);
1883 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1885 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1886 return start_offset;
1889 if (type->declarray && !is_conformant_array(type))
1891 unsigned int dim = type_array_get_dim(type);
1893 /* FIXME: multi-dimensional array */
1894 if (0xffffu < dim)
1895 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
1896 name, 0xffffu, dim - 0xffffu);
1898 if (rtype == RPC_FC_WCHAR)
1899 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1900 else
1901 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1902 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1903 *typestring_offset += 2;
1905 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
1906 *typestring_offset += 2;
1908 return start_offset;
1910 else if (is_conformant_array(type))
1912 unsigned int align = 0;
1914 if (rtype == RPC_FC_WCHAR)
1915 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1916 else
1917 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1918 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1919 *typestring_offset += 2;
1921 *typestring_offset += write_conf_or_var_desc(
1922 file, current_structure,
1923 (type->declarray && current_structure
1924 ? type_memsize(current_structure, &align)
1925 : 0),
1926 type, type_array_get_conformance(type));
1928 return start_offset;
1930 else
1932 if (rtype == RPC_FC_WCHAR)
1933 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1934 else
1935 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1936 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1937 *typestring_offset += 2;
1939 return start_offset;
1943 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1944 const char *name, unsigned int *typestring_offset)
1946 const expr_t *length_is = type_array_get_variance(type);
1947 const expr_t *size_is = type_array_get_conformance(type);
1948 unsigned int align = 0;
1949 unsigned int size;
1950 unsigned int start_offset;
1951 unsigned char fc;
1952 int has_pointer;
1953 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1954 unsigned int baseoff
1955 = type->declarray && current_structure
1956 ? type_memsize(current_structure, &align)
1957 : 0;
1959 if (!pointer_type)
1960 pointer_type = RPC_FC_RP;
1962 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
1963 has_pointer = TRUE;
1964 else
1965 has_pointer = type_has_pointers(type_array_get_element(type));
1967 align = 0;
1968 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
1969 fc = get_array_fc(type);
1971 start_offset = *typestring_offset;
1972 update_tfsoff(type, start_offset, file);
1973 print_start_tfs_comment(file, type, start_offset);
1974 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1975 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1976 *typestring_offset += 2;
1978 align = 0;
1979 if (fc != RPC_FC_BOGUS_ARRAY)
1981 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
1983 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
1984 *typestring_offset += 4;
1986 else
1988 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1989 *typestring_offset += 2;
1992 if (is_conformant_array(type))
1993 *typestring_offset
1994 += write_conf_or_var_desc(file, current_structure, baseoff,
1995 type, size_is);
1997 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
1999 unsigned int elalign = 0;
2000 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2001 unsigned int dim = type_array_get_dim(type);
2003 if (fc == RPC_FC_LGVARRAY)
2005 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2006 *typestring_offset += 4;
2008 else
2010 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2011 *typestring_offset += 2;
2014 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2015 *typestring_offset += 2;
2018 if (length_is)
2019 *typestring_offset
2020 += write_conf_or_var_desc(file, current_structure, baseoff,
2021 type, length_is);
2023 if (has_pointer && (!type->declarray || !current_structure))
2025 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2026 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2027 *typestring_offset += 2;
2028 write_pointer_description(file, type, typestring_offset);
2029 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2030 *typestring_offset += 1;
2033 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2034 write_end(file, typestring_offset);
2036 else
2038 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2039 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2040 *typestring_offset += 2;
2041 *typestring_offset
2042 += write_conf_or_var_desc(file, current_structure, baseoff,
2043 type, size_is);
2044 *typestring_offset
2045 += write_conf_or_var_desc(file, current_structure, baseoff,
2046 type, length_is);
2047 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2048 write_end(file, typestring_offset);
2051 return start_offset;
2054 static const var_t *find_array_or_string_in_struct(const type_t *type)
2056 const var_list_t *fields = type_struct_get_fields(type);
2057 const var_t *last_field;
2058 const type_t *ft;
2060 if (!fields || list_empty(fields))
2061 return NULL;
2063 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2064 ft = last_field->type;
2066 if (ft->declarray && is_conformant_array(ft))
2067 return last_field;
2069 if (type_get_type(ft) == TYPE_STRUCT)
2070 return find_array_or_string_in_struct(ft);
2071 else
2072 return NULL;
2075 static void write_struct_members(FILE *file, const type_t *type,
2076 int is_complex, unsigned int *corroff,
2077 unsigned int *typestring_offset)
2079 const var_t *field;
2080 unsigned short offset = 0;
2081 int salign = -1;
2082 int padding;
2083 var_list_t *fields = type_struct_get_fields(type);
2085 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2087 type_t *ft = field->type;
2088 if (!ft->declarray || !is_conformant_array(ft))
2090 unsigned int align = 0;
2091 unsigned int size = type_memsize(ft, &align);
2092 if (salign == -1)
2093 salign = align;
2094 if ((align - 1) & offset)
2096 unsigned char fc = 0;
2097 switch (align)
2099 case 4:
2100 fc = RPC_FC_ALIGNM4;
2101 break;
2102 case 8:
2103 fc = RPC_FC_ALIGNM8;
2104 break;
2105 default:
2106 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2108 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2109 offset = ROUND_SIZE(offset, align);
2110 *typestring_offset += 1;
2112 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2113 typestring_offset);
2114 offset += size;
2118 padding = ROUNDING(offset, salign);
2119 if (padding)
2121 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2122 RPC_FC_STRUCTPAD1 + padding - 1,
2123 padding);
2124 *typestring_offset += 1;
2127 write_end(file, typestring_offset);
2130 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2131 const char *name, unsigned int *tfsoff)
2133 const type_t *save_current_structure = current_structure;
2134 unsigned int total_size;
2135 const var_t *array;
2136 unsigned int start_offset;
2137 unsigned int array_offset;
2138 int has_pointers = 0;
2139 unsigned int align = 0;
2140 unsigned int corroff;
2141 var_t *f;
2142 unsigned char fc = get_struct_fc(type);
2143 var_list_t *fields = type_struct_get_fields(type);
2145 guard_rec(type);
2146 current_structure = type;
2148 total_size = type_memsize(type, &align);
2149 if (total_size > USHRT_MAX)
2150 error("structure size for %s exceeds %d bytes by %d bytes\n",
2151 name, USHRT_MAX, total_size - USHRT_MAX);
2153 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2154 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2155 FALSE, tfsoff);
2156 if (!has_pointers) has_pointers = type_has_pointers(type);
2158 array = find_array_or_string_in_struct(type);
2159 if (array && !processed(array->type))
2160 array_offset
2161 = is_string_type(array->attrs, array->type)
2162 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2163 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2165 corroff = *tfsoff;
2166 write_descriptors(file, type, tfsoff);
2168 start_offset = *tfsoff;
2169 update_tfsoff(type, start_offset, file);
2170 print_start_tfs_comment(file, type, start_offset);
2171 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2172 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2173 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2174 *tfsoff += 4;
2176 if (array)
2178 unsigned int absoff = array->type->typestring_offset;
2179 short reloff = absoff - *tfsoff;
2180 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2181 reloff, reloff, absoff);
2182 *tfsoff += 2;
2184 else if (fc == RPC_FC_BOGUS_STRUCT)
2186 print_file(file, 2, "NdrFcShort(0x0),\n");
2187 *tfsoff += 2;
2190 if (fc == RPC_FC_BOGUS_STRUCT)
2192 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2193 nothing is written to file yet. On the actual writing pass,
2194 this will have been updated. */
2195 unsigned int absoff = type_get_real_type(type)->ptrdesc ?
2196 type_get_real_type(type)->ptrdesc : *tfsoff;
2197 int reloff = absoff - *tfsoff;
2198 assert( reloff >= 0 );
2199 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2200 reloff, reloff, absoff);
2201 *tfsoff += 2;
2203 else if ((fc == RPC_FC_PSTRUCT) ||
2204 (fc == RPC_FC_CPSTRUCT) ||
2205 (fc == RPC_FC_CVSTRUCT && has_pointers))
2207 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2208 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2209 *tfsoff += 2;
2210 write_pointer_description(file, type, tfsoff);
2211 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2212 *tfsoff += 1;
2215 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2216 tfsoff);
2218 if (fc == RPC_FC_BOGUS_STRUCT)
2220 const var_t *f;
2222 type_get_real_type(type)->ptrdesc = *tfsoff;
2223 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2225 type_t *ft = f->type;
2226 if (is_ptr(ft))
2228 if (is_string_type(f->attrs, ft))
2229 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2230 else
2231 write_pointer_tfs(file, ft, tfsoff);
2233 else if (!ft->declarray && is_conformant_array(ft))
2235 unsigned int absoff = ft->typestring_offset;
2236 short reloff = absoff - (*tfsoff + 2);
2237 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2238 /* FIXME: We need to store pointer attributes for arrays
2239 so we don't lose pointer_default info. */
2240 if (ptr_type == 0)
2241 ptr_type = RPC_FC_UP;
2242 print_file(file, 0, "/* %d */\n", *tfsoff);
2243 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2244 string_of_type(ptr_type));
2245 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2246 reloff, reloff, absoff);
2247 *tfsoff += 4;
2250 if (type_get_real_type(type)->ptrdesc == *tfsoff)
2251 type_get_real_type(type)->ptrdesc = 0;
2254 current_structure = save_current_structure;
2255 return start_offset;
2258 static unsigned int write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2259 unsigned char flags, unsigned int offset,
2260 unsigned int *typeformat_offset)
2262 unsigned int start_offset = *typeformat_offset;
2263 short reloff = offset - (*typeformat_offset + 2);
2264 int in_attr, out_attr;
2265 in_attr = is_attr(attrs, ATTR_IN);
2266 out_attr = is_attr(attrs, ATTR_OUT);
2267 if (!in_attr && !out_attr) in_attr = 1;
2269 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2270 flags |= 0x04;
2272 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2273 pointer_type,
2274 flags,
2275 string_of_type(pointer_type));
2276 if (file)
2278 if (flags & 0x04)
2279 fprintf(file, " [allocated_on_stack]");
2280 if (flags & 0x10)
2281 fprintf(file, " [pointer_deref]");
2282 fprintf(file, " */\n");
2285 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", reloff, offset);
2286 *typeformat_offset += 4;
2288 return start_offset;
2291 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2293 if (t == NULL)
2295 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2297 else
2299 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2301 unsigned char fc;
2302 if (type_get_type(t) == TYPE_BASIC)
2303 fc = type_basic_get_fc(t);
2304 else
2305 fc = get_enum_fc(t);
2306 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2307 fc, string_of_type(fc));
2309 else if (t->typestring_offset)
2311 short reloff = t->typestring_offset - *tfsoff;
2312 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2313 reloff, reloff, t->typestring_offset);
2315 else
2316 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2319 *tfsoff += 2;
2322 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2324 unsigned int align;
2325 unsigned int start_offset;
2326 unsigned int size;
2327 var_list_t *fields;
2328 unsigned int nbranch = 0;
2329 type_t *deftype = NULL;
2330 short nodeftype = 0xffff;
2331 var_t *f;
2333 guard_rec(type);
2335 align = 0;
2336 size = type_memsize(type, &align);
2338 fields = type_union_get_cases(type);
2340 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2342 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2343 if (cases)
2344 nbranch += list_count(cases);
2345 if (f->type)
2346 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2349 start_offset = *tfsoff;
2350 update_tfsoff(type, start_offset, file);
2351 print_start_tfs_comment(file, type, start_offset);
2352 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2354 const var_t *sv = type_union_get_switch_value(type);
2355 const type_t *st = sv->type;
2356 unsigned char fc;
2358 if (type_get_type(st) == TYPE_BASIC)
2360 switch (type_basic_get_fc(st))
2362 case RPC_FC_CHAR:
2363 case RPC_FC_SMALL:
2364 case RPC_FC_BYTE:
2365 case RPC_FC_USMALL:
2366 case RPC_FC_WCHAR:
2367 case RPC_FC_SHORT:
2368 case RPC_FC_USHORT:
2369 case RPC_FC_LONG:
2370 case RPC_FC_ULONG:
2371 fc = type_basic_get_fc(st);
2372 break;
2373 default:
2374 fc = 0;
2375 error("union switch type must be an integer, char, or enum\n");
2378 else if (type_get_type(st) == TYPE_ENUM)
2379 fc = get_enum_fc(st);
2380 else
2381 error("union switch type must be an integer, char, or enum\n");
2383 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2384 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2385 0x40 | fc, string_of_type(fc));
2386 *tfsoff += 2;
2388 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2390 static const expr_t dummy_expr; /* FIXME */
2391 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2392 unsigned char fc;
2394 if (type_get_type(st) == TYPE_BASIC)
2396 switch (type_basic_get_fc(st))
2398 case RPC_FC_CHAR:
2399 case RPC_FC_SMALL:
2400 case RPC_FC_USMALL:
2401 case RPC_FC_SHORT:
2402 case RPC_FC_USHORT:
2403 case RPC_FC_LONG:
2404 case RPC_FC_ULONG:
2405 case RPC_FC_ENUM16:
2406 case RPC_FC_ENUM32:
2407 fc = type_basic_get_fc(st);
2408 break;
2409 default:
2410 fc = 0;
2411 error("union switch type must be an integer, char, or enum\n");
2414 else if (type_get_type(st) == TYPE_ENUM)
2415 fc = get_enum_fc(st);
2416 else
2417 error("union switch type must be an integer, char, or enum\n");
2419 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2420 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2421 fc, string_of_type(fc));
2422 *tfsoff += 2;
2424 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2425 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2426 *tfsoff += 2;
2429 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2430 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2431 *tfsoff += 4;
2433 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2435 type_t *ft = f->type;
2436 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2437 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2438 expr_t *c;
2440 if (cases == NULL && !deflt)
2441 error("union field %s with neither case nor default attribute\n", f->name);
2443 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2445 /* MIDL doesn't check for duplicate cases, even though that seems
2446 like a reasonable thing to do, it just dumps them to the TFS
2447 like we're going to do here. */
2448 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2449 *tfsoff += 4;
2450 write_branch_type(file, ft, tfsoff);
2453 /* MIDL allows multiple default branches, even though that seems
2454 illogical, it just chooses the last one, which is what we will
2455 do. */
2456 if (deflt)
2458 deftype = ft;
2459 nodeftype = 0;
2463 if (deftype)
2465 write_branch_type(file, deftype, tfsoff);
2467 else
2469 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2470 *tfsoff += 2;
2473 return start_offset;
2476 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2477 unsigned int *typeformat_offset)
2479 unsigned int i;
2480 unsigned int start_offset = *typeformat_offset;
2481 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2483 if (iid)
2485 print_file(file, 2, "0x2f, /* FC_IP */\n");
2486 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2487 *typeformat_offset
2488 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2490 else
2492 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2493 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2495 if (! uuid)
2496 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2498 update_tfsoff(type, start_offset, file);
2499 print_start_tfs_comment(file, type, start_offset);
2500 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2501 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2502 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2503 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2504 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2505 for (i = 0; i < 8; ++i)
2506 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2508 if (file)
2509 fprintf(file, "\n");
2511 *typeformat_offset += 18;
2513 return start_offset;
2516 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2517 const var_t *var,
2518 unsigned int *typeformat_offset)
2520 unsigned int start_offset = *typeformat_offset;
2521 unsigned char flags = 0;
2523 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2524 flags |= NDR_STRICT_CONTEXT_HANDLE;
2526 if (is_ptr(type))
2527 flags |= 0x80;
2528 if (is_attr(var->attrs, ATTR_IN))
2530 flags |= 0x40;
2531 if (!is_attr(var->attrs, ATTR_OUT))
2532 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2534 if (is_attr(var->attrs, ATTR_OUT))
2535 flags |= 0x20;
2537 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2538 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2539 /* return and can't be null values overlap */
2540 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2541 print_file(file, 0, "can't be null, ");
2542 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2543 print_file(file, 0, "serialize, ");
2544 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2545 print_file(file, 0, "no serialize, ");
2546 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2547 print_file(file, 0, "strict, ");
2548 if ((flags & 0x21) == 0x20)
2549 print_file(file, 0, "out, ");
2550 if ((flags & 0x21) == 0x21)
2551 print_file(file, 0, "return, ");
2552 if (flags & 0x40)
2553 print_file(file, 0, "in, ");
2554 if (flags & 0x80)
2555 print_file(file, 0, "via ptr, ");
2556 print_file(file, 0, "*/\n");
2557 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2558 print_file(file, 2, "0, /* FIXME: param num */\n");
2559 *typeformat_offset += 4;
2561 return start_offset;
2564 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2565 type_t *type, const var_t *var,
2566 unsigned int *typeformat_offset)
2568 unsigned int offset;
2570 if (is_context_handle(type))
2571 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2573 if (is_user_type(type))
2575 write_user_tfs(file, type, typeformat_offset);
2576 return type->typestring_offset;
2579 if (is_string_type(var->attrs, type))
2580 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2582 if (is_array(type))
2584 int ptr_type;
2585 unsigned int off;
2586 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2587 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2588 /* Top level pointers to conformant arrays may be handled specially
2589 since we can bypass the pointer, but if the array is buried
2590 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2591 always need to write the pointer. */
2592 if (!ptr_type && var->type != type)
2593 /* FIXME: This should use pointer_default, but the information
2594 isn't kept around for arrays. */
2595 ptr_type = RPC_FC_UP;
2596 if (ptr_type && ptr_type != RPC_FC_RP)
2598 unsigned int absoff = type->typestring_offset;
2599 short reloff = absoff - (*typeformat_offset + 2);
2600 off = *typeformat_offset;
2601 print_file(file, 0, "/* %d */\n", off);
2602 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2603 string_of_type(ptr_type));
2604 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2605 reloff, reloff, absoff);
2606 *typeformat_offset += 4;
2608 return off;
2611 if (!is_ptr(type))
2613 switch (type_get_type(type))
2615 case TYPE_STRUCT:
2616 if (processed(type)) return type->typestring_offset;
2617 return write_struct_tfs(file, type, var->name, typeformat_offset);
2618 case TYPE_UNION:
2619 case TYPE_ENCAPSULATED_UNION:
2620 if (processed(type)) return type->typestring_offset;
2621 return write_union_tfs(file, type, typeformat_offset);
2622 case TYPE_ENUM:
2623 case TYPE_BASIC:
2624 /* nothing to do */
2625 return 0;
2626 default:
2627 error("write_typeformatstring_var: Unsupported type %d for variable %s\n",
2628 type_get_type(type), var->name);
2631 else if (last_ptr(type))
2633 unsigned int start_offset = *typeformat_offset;
2634 int in_attr = is_attr(var->attrs, ATTR_IN);
2635 int out_attr = is_attr(var->attrs, ATTR_OUT);
2636 const type_t *ref = type_pointer_get_ref(type);
2638 if (type_get_type(ref) == TYPE_INTERFACE
2639 || (type_get_type(ref) == TYPE_VOID
2640 && is_attr(var->attrs, ATTR_IIDIS)))
2642 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2645 /* special case for pointers to base types */
2646 if (type_get_type(ref) == TYPE_BASIC ||
2647 type_get_type(ref) == TYPE_ENUM)
2649 unsigned char fc;
2651 if (type_get_type(ref) == TYPE_ENUM)
2652 fc = get_enum_fc(ref);
2653 else
2654 fc = type_basic_get_fc(ref);
2656 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2657 get_pointer_fc(type),
2658 (!in_attr && out_attr) ? 0x0C : 0x08,
2659 string_of_type(get_pointer_fc(type)),
2660 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2661 print_file(file, indent, "0x%02x, /* %s */\n",
2662 fc, string_of_type(fc));
2663 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2664 *typeformat_offset += 4;
2665 return start_offset;
2669 assert(is_ptr(type));
2671 offset = write_typeformatstring_var(file, indent, func,
2672 type_pointer_get_ref(type), var,
2673 typeformat_offset);
2674 if (file)
2675 fprintf(file, "/* %2u */\n", *typeformat_offset);
2676 return write_pointer_only_tfs(file, var->attrs, get_pointer_fc(type),
2677 !last_ptr(type) ? 0x10 : 0,
2678 offset, typeformat_offset);
2681 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2682 const char *name, int write_ptr, unsigned int *tfsoff)
2684 int retmask = 0;
2686 if (is_user_type(type))
2688 write_user_tfs(file, type, tfsoff);
2690 else if (is_string_type(attrs, type))
2692 write_string_tfs(file, attrs, type, name, tfsoff);
2694 else switch (type_get_type(type))
2696 case TYPE_POINTER:
2698 type_t *ref = type_pointer_get_ref(type);
2700 if (type_get_type(ref) == TYPE_INTERFACE
2701 || (type_get_type(ref) == TYPE_VOID
2702 && is_attr(attrs, ATTR_IIDIS)))
2704 write_ip_tfs(file, attrs, type, tfsoff);
2706 else
2708 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2709 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2711 if (write_ptr)
2712 write_pointer_tfs(file, type, tfsoff);
2714 retmask |= 1;
2716 break;
2718 case TYPE_ARRAY:
2719 /* conformant arrays and strings are handled specially */
2720 if (!type->declarray || !is_conformant_array(type))
2722 write_array_tfs(file, attrs, type, name, tfsoff);
2723 if (is_conformant_array(type))
2724 retmask |= 1;
2726 break;
2727 case TYPE_STRUCT:
2728 if (!processed(type))
2729 write_struct_tfs(file, type, name, tfsoff);
2730 break;
2731 case TYPE_UNION:
2732 case TYPE_ENCAPSULATED_UNION:
2733 if (!processed(type))
2734 write_union_tfs(file, type, tfsoff);
2735 break;
2736 case TYPE_ENUM:
2737 case TYPE_BASIC:
2738 /* nothing to do */
2739 break;
2740 case TYPE_VOID:
2741 case TYPE_ALIAS:
2742 case TYPE_MODULE:
2743 case TYPE_COCLASS:
2744 case TYPE_FUNCTION:
2745 case TYPE_INTERFACE:
2746 assert(0);
2749 return retmask;
2752 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2753 type_pred_t pred, unsigned int *typeformat_offset)
2755 const var_t *var;
2756 const statement_t *stmt;
2758 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2760 const type_t *iface;
2761 const statement_t *stmt_func;
2763 if (stmt->type == STMT_LIBRARY)
2765 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2766 continue;
2768 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2769 continue;
2771 iface = stmt->u.type;
2772 if (!pred(iface))
2773 continue;
2775 current_iface = iface;
2776 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2778 const var_t *func = stmt_func->u.var;
2779 if (is_local(func->attrs)) continue;
2781 if (!is_void(type_function_get_rettype(func->type)))
2783 var_t v = *func;
2784 v.type = type_function_get_rettype(func->type);
2785 update_tfsoff(type_function_get_rettype(func->type),
2786 write_typeformatstring_var(
2787 file, 2, NULL,
2788 type_function_get_rettype(func->type),
2789 &v, typeformat_offset),
2790 file);
2793 current_func = func;
2794 if (type_get_function_args(func->type))
2795 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2796 update_tfsoff(
2797 var->type,
2798 write_typeformatstring_var(
2799 file, 2, func, var->type, var,
2800 typeformat_offset),
2801 file);
2805 return *typeformat_offset + 1;
2808 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2810 unsigned int typeformat_offset = 2;
2812 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2816 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2818 int indent = 0;
2820 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2821 print_file(file, indent, "{\n");
2822 indent++;
2823 print_file(file, indent, "0,\n");
2824 print_file(file, indent, "{\n");
2825 indent++;
2826 print_file(file, indent, "NdrFcShort(0x0),\n");
2828 set_all_tfswrite(TRUE);
2829 process_tfs(file, stmts, pred);
2831 print_file(file, indent, "0x0\n");
2832 indent--;
2833 print_file(file, indent, "}\n");
2834 indent--;
2835 print_file(file, indent, "};\n");
2836 print_file(file, indent, "\n");
2839 static unsigned int get_required_buffer_size_type(
2840 const type_t *type, const char *name, unsigned int *alignment)
2842 const char *uname;
2843 const type_t *utype;
2845 *alignment = 0;
2846 if ((utype = get_user_type(type, &uname)))
2848 return get_required_buffer_size_type(utype, uname, alignment);
2850 else
2852 switch (type_get_type(type))
2854 case TYPE_BASIC:
2855 switch (type_basic_get_fc(type))
2857 case RPC_FC_BYTE:
2858 case RPC_FC_CHAR:
2859 case RPC_FC_USMALL:
2860 case RPC_FC_SMALL:
2861 *alignment = 4;
2862 return 1;
2864 case RPC_FC_WCHAR:
2865 case RPC_FC_USHORT:
2866 case RPC_FC_SHORT:
2867 *alignment = 4;
2868 return 2;
2870 case RPC_FC_ULONG:
2871 case RPC_FC_LONG:
2872 case RPC_FC_FLOAT:
2873 case RPC_FC_ERROR_STATUS_T:
2874 *alignment = 4;
2875 return 4;
2877 case RPC_FC_HYPER:
2878 case RPC_FC_DOUBLE:
2879 *alignment = 8;
2880 return 8;
2882 case RPC_FC_IGNORE:
2883 case RPC_FC_BIND_PRIMITIVE:
2884 return 0;
2886 default:
2887 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2888 type_basic_get_fc(type));
2889 return 0;
2891 break;
2893 case TYPE_ENUM:
2894 switch (get_enum_fc(type))
2896 case RPC_FC_ENUM32:
2897 *alignment = 4;
2898 return 4;
2899 case RPC_FC_ENUM16:
2900 *alignment = 4;
2901 return 2;
2903 break;
2905 case TYPE_STRUCT:
2906 if (get_struct_fc(type) == RPC_FC_STRUCT)
2908 if (!type_struct_get_fields(type)) return 0;
2909 return fields_memsize(type_struct_get_fields(type), alignment);
2911 break;
2913 case TYPE_POINTER:
2914 if (get_pointer_fc(type) == RPC_FC_RP)
2916 const type_t *ref = type_pointer_get_ref(type);
2917 return (type_get_type(ref) == TYPE_BASIC ||
2918 type_get_type(ref) == TYPE_ENUM ||
2919 (type_get_type(ref) == TYPE_STRUCT && get_struct_fc(ref) == RPC_FC_STRUCT)) ?
2920 get_required_buffer_size_type( ref, name, alignment ) : 0;
2922 break;
2924 case TYPE_ARRAY:
2925 return type_array_get_dim(type) *
2926 get_required_buffer_size_type(type_array_get_element(type), name, alignment);
2928 default:
2929 break;
2932 return 0;
2935 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2937 int in_attr = is_attr(var->attrs, ATTR_IN);
2938 int out_attr = is_attr(var->attrs, ATTR_OUT);
2940 if (!in_attr && !out_attr)
2941 in_attr = 1;
2943 *alignment = 0;
2945 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2946 pass == PASS_RETURN)
2948 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2950 *alignment = 4;
2951 return 20;
2954 if (!is_string_type(var->attrs, var->type))
2955 return get_required_buffer_size_type(var->type, var->name,
2956 alignment);
2958 return 0;
2961 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
2963 const var_t *var;
2964 unsigned int total_size = 0, alignment;
2966 if (type_get_function_args(func->type))
2968 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2970 total_size += get_required_buffer_size(var, &alignment, pass);
2971 total_size += alignment;
2975 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
2977 var_t v = *func;
2978 v.type = type_function_get_rettype(func->type);
2979 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2980 total_size += alignment;
2982 return total_size;
2985 static void print_phase_function(FILE *file, int indent, const char *type,
2986 const char *local_var_prefix, enum remoting_phase phase,
2987 const var_t *var, unsigned int type_offset)
2989 const char *function;
2990 switch (phase)
2992 case PHASE_BUFFERSIZE:
2993 function = "BufferSize";
2994 break;
2995 case PHASE_MARSHAL:
2996 function = "Marshall";
2997 break;
2998 case PHASE_UNMARSHAL:
2999 function = "Unmarshall";
3000 break;
3001 case PHASE_FREE:
3002 function = "Free";
3003 break;
3004 default:
3005 assert(0);
3006 return;
3009 print_file(file, indent, "Ndr%s%s(\n", type, function);
3010 indent++;
3011 print_file(file, indent, "&__frame->_StubMsg,\n");
3012 print_file(file, indent, "%s%s%s%s%s,\n",
3013 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3014 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3015 local_var_prefix,
3016 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3017 var->name);
3018 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3019 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3020 if (phase == PHASE_UNMARSHAL)
3021 print_file(file, indent, "0);\n");
3022 indent--;
3025 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3026 enum remoting_phase phase, enum pass pass, const var_t *var,
3027 const char *varname)
3029 type_t *type = var->type;
3030 unsigned int size;
3031 unsigned int alignment = 0;
3032 const type_t *ref;
3034 /* no work to do for other phases, buffer sizing is done elsewhere */
3035 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3036 return;
3038 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3039 if (type_get_type(ref) == TYPE_ENUM)
3041 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3043 size = 4;
3044 alignment = 4;
3046 else /* RPC_FC_ENUM16 */
3048 size = 2;
3049 alignment = 2;
3052 else
3054 switch (type_basic_get_fc(ref))
3056 case RPC_FC_BYTE:
3057 case RPC_FC_CHAR:
3058 case RPC_FC_SMALL:
3059 case RPC_FC_USMALL:
3060 size = 1;
3061 alignment = 1;
3062 break;
3064 case RPC_FC_WCHAR:
3065 case RPC_FC_USHORT:
3066 case RPC_FC_SHORT:
3067 size = 2;
3068 alignment = 2;
3069 break;
3071 case RPC_FC_ULONG:
3072 case RPC_FC_LONG:
3073 case RPC_FC_FLOAT:
3074 case RPC_FC_ERROR_STATUS_T:
3075 size = 4;
3076 alignment = 4;
3077 break;
3079 case RPC_FC_HYPER:
3080 case RPC_FC_DOUBLE:
3081 size = 8;
3082 alignment = 8;
3083 break;
3085 case RPC_FC_IGNORE:
3086 case RPC_FC_BIND_PRIMITIVE:
3087 /* no marshalling needed */
3088 return;
3090 default:
3091 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3092 var->name, type_basic_get_fc(ref));
3093 size = 0;
3097 if (phase == PHASE_MARSHAL)
3098 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3099 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3100 alignment - 1, alignment - 1);
3102 if (phase == PHASE_MARSHAL)
3104 print_file(file, indent, "*(");
3105 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3106 if (is_ptr(type))
3107 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3108 else
3109 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3110 fprintf(file, "%s%s", local_var_prefix, varname);
3111 fprintf(file, ";\n");
3113 else if (phase == PHASE_UNMARSHAL)
3115 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3116 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3117 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3118 print_file(file, indent, "{\n");
3119 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3120 print_file(file, indent, "}\n");
3121 print_file(file, indent, "%s%s%s",
3122 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3123 local_var_prefix, varname);
3124 if (pass == PASS_IN && is_ptr(type))
3125 fprintf(file, " = (");
3126 else
3127 fprintf(file, " = *(");
3128 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3129 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3132 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3133 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3134 fprintf(file, ");\n");
3137 /* returns whether the MaxCount, Offset or ActualCount members need to be
3138 * filled in for the specified phase */
3139 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3141 return (phase != PHASE_UNMARSHAL);
3144 expr_t *get_size_is_expr(const type_t *t, const char *name)
3146 expr_t *x = NULL;
3148 for ( ; is_array(t); t = type_array_get_element(t))
3149 if (type_array_has_conformance(t))
3151 if (!x)
3152 x = type_array_get_conformance(t);
3153 else
3154 error("%s: multidimensional conformant"
3155 " arrays not supported at the top level\n",
3156 name);
3159 return x;
3162 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3163 enum remoting_phase phase, const var_t *var)
3165 const type_t *type = var->type;
3166 /* get fundamental type for the argument */
3167 for (;;)
3169 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3170 break;
3171 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3172 break;
3173 else if (type_is_alias(type))
3174 type = type_alias_get_aliasee(type);
3175 else if (is_array(type) || is_string_type(var->attrs, type))
3177 if (is_conformance_needed_for_phase(phase) && is_array(type))
3179 if (type_array_has_conformance(type))
3181 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3182 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3183 fprintf(file, ";\n\n");
3185 if (type_array_has_variance(type))
3187 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3188 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3189 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3190 fprintf(file, ";\n\n");
3193 break;
3195 else if (type_get_type(type) == TYPE_UNION)
3197 if (is_conformance_needed_for_phase(phase))
3199 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3200 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3201 fprintf(file, ";\n\n");
3203 break;
3205 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3207 expr_t *iid;
3209 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3211 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3212 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3213 fprintf( file, ";\n\n" );
3215 break;
3217 else if (is_ptr(type))
3218 type = type_pointer_get_ref(type);
3219 else
3220 break;
3224 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3225 enum pass pass, enum remoting_phase phase, const var_t *var)
3227 int in_attr, out_attr, pointer_type;
3228 const type_t *type = var->type;
3229 unsigned int start_offset = type->typestring_offset;
3231 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3232 if (!pointer_type)
3233 pointer_type = RPC_FC_RP;
3235 in_attr = is_attr(var->attrs, ATTR_IN);
3236 out_attr = is_attr(var->attrs, ATTR_OUT);
3237 if (!in_attr && !out_attr)
3238 in_attr = 1;
3240 if (phase != PHASE_FREE)
3241 switch (pass)
3243 case PASS_IN:
3244 if (!in_attr) return;
3245 break;
3246 case PASS_OUT:
3247 if (!out_attr) return;
3248 break;
3249 case PASS_RETURN:
3250 break;
3253 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3255 if (is_context_handle(type))
3257 if (phase == PHASE_MARSHAL)
3259 if (pass == PASS_IN)
3261 /* if the context_handle attribute appears in the chain of types
3262 * without pointers being followed, then the context handle must
3263 * be direct, otherwise it is a pointer */
3264 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3265 print_file(file, indent, "NdrClientContextMarshall(\n");
3266 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3267 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3268 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3270 else
3272 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3273 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3274 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3275 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3276 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3279 else if (phase == PHASE_UNMARSHAL)
3281 if (pass == PASS_OUT)
3283 if (!in_attr)
3284 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3285 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3286 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3287 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3288 print_file(file, indent + 1, "__frame->_Handle);\n");
3290 else
3292 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3293 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3294 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3298 else if (is_user_type(var->type))
3300 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3302 else if (is_string_type(var->attrs, var->type))
3304 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3305 pointer_type != RPC_FC_RP)
3307 unsigned int ptr_start_offset = (start_offset - (is_conformant_array(type) ? 4 : 2));
3308 print_phase_function(file, indent, "Pointer", local_var_prefix,
3309 phase, var, ptr_start_offset);
3311 else
3313 if (is_array(type) && !is_conformant_array(type))
3314 print_phase_function(file, indent, "NonConformantString",
3315 local_var_prefix, phase, var,
3316 start_offset);
3317 else
3318 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3319 phase, var, start_offset);
3322 else switch (type_get_type(type))
3324 case TYPE_ARRAY:
3326 unsigned char tc = get_array_fc(type);
3327 const char *array_type = "FixedArray";
3329 /* We already have the size_is expression since it's at the
3330 top level, but do checks for multidimensional conformant
3331 arrays. When we handle them, we'll need to extend this
3332 function to return a list, and then we'll actually use
3333 the return value. */
3334 get_size_is_expr(type, var->name);
3336 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3338 array_type = "VaryingArray";
3340 else if (tc == RPC_FC_CARRAY)
3342 array_type = "ConformantArray";
3344 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3346 array_type = (tc == RPC_FC_BOGUS_ARRAY
3347 ? "ComplexArray"
3348 : "ConformantVaryingArray");
3351 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3352 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3353 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3355 /* these are all unmarshalled by allocating memory */
3356 if (tc == RPC_FC_BOGUS_ARRAY ||
3357 tc == RPC_FC_CVARRAY ||
3358 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3359 (tc == RPC_FC_CARRAY && !in_attr))
3361 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3362 indent++;
3363 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3366 break;
3368 case TYPE_BASIC:
3369 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3370 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3371 break;
3372 case TYPE_ENUM:
3373 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3375 if (phase == PHASE_MARSHAL)
3376 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3377 else
3378 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3379 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3380 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3381 local_var_prefix,
3382 var->name);
3383 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3385 break;
3386 case TYPE_STRUCT:
3387 switch (get_struct_fc(type))
3389 case RPC_FC_STRUCT:
3390 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3391 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3392 break;
3393 case RPC_FC_PSTRUCT:
3394 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3395 break;
3396 case RPC_FC_CSTRUCT:
3397 case RPC_FC_CPSTRUCT:
3398 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3399 break;
3400 case RPC_FC_CVSTRUCT:
3401 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3402 break;
3403 case RPC_FC_BOGUS_STRUCT:
3404 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3405 break;
3406 default:
3407 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3409 break;
3410 case TYPE_UNION:
3411 case TYPE_ENCAPSULATED_UNION:
3413 const char *union_type = NULL;
3415 if (type_get_type(type) == TYPE_UNION)
3416 union_type = "NonEncapsulatedUnion";
3417 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3418 union_type = "EncapsulatedUnion";
3420 print_phase_function(file, indent, union_type, local_var_prefix,
3421 phase, var, start_offset);
3422 break;
3424 case TYPE_POINTER:
3426 const type_t *ref = type_pointer_get_ref(type);
3427 if (get_pointer_fc(type) == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3429 case TYPE_BASIC:
3430 /* base types have known sizes, so don't need a sizing pass
3431 * and don't have any memory to free and so don't need a
3432 * freeing pass */
3433 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3434 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3435 break;
3436 case TYPE_ENUM:
3437 /* base types have known sizes, so don't need a sizing pass
3438 * and don't have any memory to free and so don't need a
3439 * freeing pass */
3440 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3441 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3442 break;
3443 case TYPE_STRUCT:
3445 const char *struct_type = NULL;
3446 switch (get_struct_fc(ref))
3448 case RPC_FC_STRUCT:
3449 /* simple structs have known sizes, so don't need a sizing
3450 * pass and don't have any memory to free and so don't
3451 * need a freeing pass */
3452 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3453 struct_type = "SimpleStruct";
3454 break;
3455 case RPC_FC_PSTRUCT:
3456 struct_type = "SimpleStruct";
3457 break;
3458 case RPC_FC_CSTRUCT:
3459 case RPC_FC_CPSTRUCT:
3460 struct_type = "ConformantStruct";
3461 break;
3462 case RPC_FC_CVSTRUCT:
3463 struct_type = "ConformantVaryingStruct";
3464 break;
3465 case RPC_FC_BOGUS_STRUCT:
3466 struct_type = "ComplexStruct";
3467 break;
3468 default:
3469 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3472 if (struct_type)
3474 if (phase == PHASE_FREE)
3475 struct_type = "Pointer";
3476 else
3477 start_offset = ref->typestring_offset;
3478 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3480 break;
3482 case TYPE_UNION:
3483 case TYPE_ENCAPSULATED_UNION:
3485 const char *union_type = NULL;
3486 if (phase == PHASE_FREE)
3487 union_type = "Pointer";
3488 else
3490 if (type_get_type(ref) == TYPE_UNION)
3491 union_type = "NonEncapsulatedUnion";
3492 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3493 union_type = "EncapsulatedUnion";
3495 start_offset = ref->typestring_offset;
3498 print_phase_function(file, indent, union_type, local_var_prefix,
3499 phase, var, start_offset);
3500 break;
3502 case TYPE_POINTER:
3503 case TYPE_ARRAY:
3504 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3505 break;
3506 case TYPE_INTERFACE:
3507 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3508 break;
3509 case TYPE_VOID:
3510 case TYPE_ALIAS:
3511 case TYPE_MODULE:
3512 case TYPE_COCLASS:
3513 case TYPE_FUNCTION:
3514 assert(0);
3515 break;
3517 else if (type_get_type(ref) == TYPE_INTERFACE)
3518 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3519 else
3520 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3521 break;
3523 case TYPE_VOID:
3524 case TYPE_ALIAS:
3525 case TYPE_MODULE:
3526 case TYPE_COCLASS:
3527 case TYPE_FUNCTION:
3528 case TYPE_INTERFACE:
3529 assert(0);
3530 break;
3532 fprintf(file, "\n");
3535 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3536 enum pass pass, enum remoting_phase phase)
3538 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3540 unsigned int size = get_function_buffer_size( func, pass );
3541 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3544 if (pass == PASS_RETURN)
3546 var_t var;
3547 var = *func;
3548 var.type = type_function_get_rettype(func->type);
3549 var.name = xstrdup( "_RetVal" );
3550 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3551 free( var.name );
3553 else
3555 const var_t *var;
3556 if (!type_get_function_args(func->type))
3557 return;
3558 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3559 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3564 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3566 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3570 unsigned int get_size_procformatstring_func(const var_t *func)
3572 const var_t *var;
3573 unsigned int size = 0;
3575 /* argument list size */
3576 if (type_get_function_args(func->type))
3577 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3578 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3580 /* return value size */
3581 if (is_void(type_function_get_rettype(func->type)))
3582 size += 2; /* FC_END and FC_PAD */
3583 else
3584 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3586 return size;
3589 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3591 const statement_t *stmt;
3592 unsigned int size = 1;
3594 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3596 const type_t *iface;
3597 const statement_t *stmt_func;
3599 if (stmt->type == STMT_LIBRARY)
3601 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3602 continue;
3604 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3605 continue;
3607 iface = stmt->u.type;
3608 if (!pred(iface))
3609 continue;
3611 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3613 const var_t *func = stmt_func->u.var;
3614 if (!is_local(func->attrs))
3615 size += get_size_procformatstring_func( func );
3618 return size;
3621 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3623 set_all_tfswrite(FALSE);
3624 return process_tfs(NULL, stmts, pred);
3627 void declare_stub_args( FILE *file, int indent, const var_t *func )
3629 int in_attr, out_attr;
3630 int i = 0;
3631 const var_t *var;
3633 /* declare return value '_RetVal' */
3634 if (!is_void(type_function_get_rettype(func->type)))
3636 print_file(file, indent, "%s", "");
3637 write_type_decl_left(file, type_function_get_rettype(func->type));
3638 fprintf(file, " _RetVal;\n");
3641 if (!type_get_function_args(func->type))
3642 return;
3644 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3646 int is_string = is_string_type(var->attrs, var->type);
3648 in_attr = is_attr(var->attrs, ATTR_IN);
3649 out_attr = is_attr(var->attrs, ATTR_OUT);
3650 if (!out_attr && !in_attr)
3651 in_attr = 1;
3653 if (is_context_handle(var->type))
3654 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3655 else
3657 if (!in_attr && !is_conformant_array(var->type) && !is_string)
3659 type_t *type_to_print;
3660 char name[16];
3661 print_file(file, indent, "%s", "");
3662 if (var->type->declarray)
3663 type_to_print = var->type;
3664 else
3665 type_to_print = type_pointer_get_ref(var->type);
3666 sprintf(name, "_W%u", i++);
3667 write_type_decl(file, type_to_print, name);
3668 fprintf(file, ";\n");
3671 print_file(file, indent, "%s", "");
3672 write_type_decl_left(file, var->type);
3673 fprintf(file, " ");
3674 if (var->type->declarray) {
3675 fprintf(file, "(*%s)", var->name);
3676 } else
3677 fprintf(file, "%s", var->name);
3678 write_type_right(file, var->type, FALSE);
3679 fprintf(file, ";\n");
3681 if (decl_indirect(var->type))
3682 print_file(file, indent, "void *_p_%s;\n", var->name);
3688 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3690 int in_attr, out_attr;
3691 int i = 0, sep = 0;
3692 const var_t *var;
3694 if (!type_get_function_args(func->type))
3695 return;
3697 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3699 int is_string = is_string_type(var->attrs, var->type);
3700 in_attr = is_attr(var->attrs, ATTR_IN);
3701 out_attr = is_attr(var->attrs, ATTR_OUT);
3702 if (!out_attr && !in_attr)
3703 in_attr = 1;
3705 if (!in_attr)
3707 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3709 if (is_context_handle(var->type))
3711 fprintf(file, " = NdrContextHandleInitialize(\n");
3712 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3713 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3714 var->type->typestring_offset);
3716 else if (is_array(var->type) &&
3717 type_array_has_conformance(var->type))
3719 unsigned int size, align = 0;
3720 type_t *type = var->type;
3722 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3723 for ( ;
3724 is_array(type) && type_array_has_conformance(type);
3725 type = type_array_get_element(type))
3727 write_expr(file, type_array_get_conformance(type), TRUE,
3728 TRUE, NULL, NULL, local_var_prefix);
3729 fprintf(file, " * ");
3731 size = type_memsize(type, &align);
3732 fprintf(file, "%u);\n", size);
3734 else if (!is_string)
3736 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3737 if (is_ptr(var->type) && !last_ptr(var->type))
3738 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3739 i++;
3742 sep = 1;
3745 if (sep)
3746 fprintf(file, "\n");
3750 int write_expr_eval_routines(FILE *file, const char *iface)
3752 static const char *var_name = "pS";
3753 static const char *var_name_expr = "pS->";
3754 int result = 0;
3755 struct expr_eval_routine *eval;
3756 unsigned short callback_offset = 0;
3758 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3760 const char *name = eval->structure->name;
3761 result = 1;
3763 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3764 iface, name, callback_offset);
3765 print_file(file, 0, "{\n");
3766 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3767 name, var_name, name, eval->baseoff);
3768 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3769 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3770 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3771 fprintf(file, ";\n");
3772 print_file(file, 0, "}\n\n");
3773 callback_offset++;
3775 return result;
3778 void write_expr_eval_routine_list(FILE *file, const char *iface)
3780 struct expr_eval_routine *eval;
3781 struct expr_eval_routine *cursor;
3782 unsigned short callback_offset = 0;
3784 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3785 fprintf(file, "{\n");
3787 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3789 const char *name = eval->structure->name;
3790 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3791 callback_offset++;
3792 list_remove(&eval->entry);
3793 free(eval);
3796 fprintf(file, "};\n\n");
3799 void write_user_quad_list(FILE *file)
3801 user_type_t *ut;
3803 if (list_empty(&user_type_list))
3804 return;
3806 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3807 fprintf(file, "{\n");
3808 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3810 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3811 print_file(file, 1, "{\n");
3812 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3813 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3814 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3815 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3816 print_file(file, 1, "}%s\n", sep);
3818 fprintf(file, "};\n\n");
3821 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3823 const struct str_list_entry_t *endpoint;
3824 const char *p;
3826 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3827 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3828 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3830 print_file( f, 1, "{ (const unsigned char *)\"" );
3831 for (p = endpoint->str; *p && *p != ':'; p++)
3833 if (*p == '"' || *p == '\\') fputc( '\\', f );
3834 fputc( *p, f );
3836 if (!*p) goto error;
3837 if (p[1] != '[') goto error;
3839 fprintf( f, "\", (const unsigned char *)\"" );
3840 for (p += 2; *p && *p != ']'; p++)
3842 if (*p == '"' || *p == '\\') fputc( '\\', f );
3843 fputc( *p, f );
3845 if (*p != ']') goto error;
3846 fprintf( f, "\" },\n" );
3848 print_file( f, 0, "};\n\n" );
3849 return;
3851 error:
3852 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3855 void write_exceptions( FILE *file )
3857 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3858 fprintf( file, "\n");
3859 fprintf( file, "#include \"wine/exception.h\"\n");
3860 fprintf( file, "#undef RpcTryExcept\n");
3861 fprintf( file, "#undef RpcExcept\n");
3862 fprintf( file, "#undef RpcEndExcept\n");
3863 fprintf( file, "#undef RpcTryFinally\n");
3864 fprintf( file, "#undef RpcFinally\n");
3865 fprintf( file, "#undef RpcEndFinally\n");
3866 fprintf( file, "#undef RpcExceptionCode\n");
3867 fprintf( file, "#undef RpcAbnormalTermination\n");
3868 fprintf( file, "\n");
3869 fprintf( file, "struct __exception_frame;\n");
3870 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3871 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3872 fprintf( file, "\n");
3873 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3874 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3875 fprintf( file, " __filter_func filter; \\\n");
3876 fprintf( file, " __finally_func finally; \\\n");
3877 fprintf( file, " sigjmp_buf jmp; \\\n");
3878 fprintf( file, " DWORD code; \\\n");
3879 fprintf( file, " unsigned char abnormal_termination; \\\n");
3880 fprintf( file, " unsigned char filter_level; \\\n");
3881 fprintf( file, " unsigned char finally_level;\n");
3882 fprintf( file, "\n");
3883 fprintf( file, "struct __exception_frame\n{\n");
3884 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3885 fprintf( file, "};\n");
3886 fprintf( file, "\n");
3887 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3888 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3889 fprintf( file, " CONTEXT *context,\n");
3890 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3891 fprintf( file, "{\n");
3892 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3893 fprintf( file, "\n");
3894 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3895 fprintf( file, " {\n" );
3896 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3897 fprintf( file, " {\n" );
3898 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3899 fprintf( file, " exc_frame->finally( exc_frame );\n");
3900 fprintf( file, " }\n" );
3901 fprintf( file, " return ExceptionContinueSearch;\n");
3902 fprintf( file, " }\n" );
3903 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3904 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3905 fprintf( file, " {\n");
3906 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3907 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3908 fprintf( file, " {\n");
3909 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3910 fprintf( file, " exc_frame->finally( exc_frame );\n");
3911 fprintf( file, " __wine_pop_frame( frame );\n");
3912 fprintf( file, " }\n");
3913 fprintf( file, " exc_frame->filter_level = 0;\n");
3914 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3915 fprintf( file, " }\n");
3916 fprintf( file, " return ExceptionContinueSearch;\n");
3917 fprintf( file, "}\n");
3918 fprintf( file, "\n");
3919 fprintf( file, "#define RpcTryExcept \\\n");
3920 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3921 fprintf( file, " { \\\n");
3922 fprintf( file, " if (!__frame->finally_level) \\\n" );
3923 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3924 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3925 fprintf( file, "\n");
3926 fprintf( file, "#define RpcExcept(expr) \\\n");
3927 fprintf( file, " if (!__frame->finally_level) \\\n" );
3928 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3929 fprintf( file, " __frame->filter_level = 0; \\\n" );
3930 fprintf( file, " } \\\n");
3931 fprintf( file, " else \\\n");
3932 fprintf( file, "\n");
3933 fprintf( file, "#define RpcEndExcept\n");
3934 fprintf( file, "\n");
3935 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3936 fprintf( file, "\n");
3937 fprintf( file, "#define RpcTryFinally \\\n");
3938 fprintf( file, " if (!__frame->filter_level) \\\n");
3939 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3940 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3941 fprintf( file, "\n");
3942 fprintf( file, "#define RpcFinally \\\n");
3943 fprintf( file, " if (!__frame->filter_level) \\\n");
3944 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3945 fprintf( file, " __frame->finally_level = 0;\n");
3946 fprintf( file, "\n");
3947 fprintf( file, "#define RpcEndFinally\n");
3948 fprintf( file, "\n");
3949 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3950 fprintf( file, "\n");
3951 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3952 fprintf( file, " do { \\\n");
3953 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3954 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3955 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3956 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3957 fprintf( file, " __frame->filter_level = 0; \\\n");
3958 fprintf( file, " __frame->finally_level = 0; \\\n");
3959 fprintf( file, " } while (0)\n");
3960 fprintf( file, "\n");
3961 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3962 fprintf( file, "\n");
3963 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3964 fprintf( file, " do { (void)(filter_func); } while(0)\n");
3965 fprintf( file, "\n");
3966 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3967 fprintf( file, " DWORD code;\n");
3968 fprintf( file, "\n");
3969 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");