widl: Move determination of the exact type of structures and array to the code genera...
[wine/wine64.git] / tools / widl / typegen.c
blob123715c999744e82c1f817140a75f4778eed8be4
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 "wine/list.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 func_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 size_t fields_memsize(const var_list_t *fields, unsigned int *align);
63 static size_t 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 size_t 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 static int get_struct_type(const type_t *type)
125 int has_pointer = 0;
126 int has_conformance = 0;
127 int has_variance = 0;
128 var_t *field;
130 if (type->type != RPC_FC_STRUCT) return type->type;
132 if (get_padding(type->fields_or_args))
133 return RPC_FC_BOGUS_STRUCT;
135 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, var_t, entry )
137 type_t *t = field->type;
139 if (is_user_type(t))
140 return RPC_FC_BOGUS_STRUCT;
142 if (is_ptr(t))
145 t = t->ref;
146 while (is_ptr(t));
148 switch (get_struct_type(t))
150 case RPC_FC_IP:
151 case RPC_FC_ENCAPSULATED_UNION:
152 case RPC_FC_NON_ENCAPSULATED_UNION:
153 case RPC_FC_BOGUS_STRUCT:
154 return RPC_FC_BOGUS_STRUCT;
157 has_pointer = 1;
158 continue;
161 if (field->type->declarray)
163 if (is_string_type(field->attrs, field->type))
165 if (is_conformant_array(field->type))
166 has_conformance = 1;
167 has_variance = 1;
168 continue;
171 if (is_array(field->type->ref))
172 return RPC_FC_BOGUS_STRUCT;
174 if (is_conformant_array(field->type))
176 has_conformance = 1;
177 if (field->type->declarray && list_next(type->fields_or_args, &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 (field->type->length_is)
182 has_variance = 1;
184 t = field->type->ref;
187 switch (get_struct_type(t))
190 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
191 * Simple types don't effect the type of struct.
192 * A struct containing a simple struct is still a simple struct.
193 * So long as we can block copy the data, we return RPC_FC_STRUCT.
195 case 0: /* void pointer */
196 case RPC_FC_BYTE:
197 case RPC_FC_CHAR:
198 case RPC_FC_SMALL:
199 case RPC_FC_USMALL:
200 case RPC_FC_WCHAR:
201 case RPC_FC_SHORT:
202 case RPC_FC_USHORT:
203 case RPC_FC_LONG:
204 case RPC_FC_ULONG:
205 case RPC_FC_INT3264:
206 case RPC_FC_UINT3264:
207 case RPC_FC_HYPER:
208 case RPC_FC_FLOAT:
209 case RPC_FC_DOUBLE:
210 case RPC_FC_STRUCT:
211 case RPC_FC_ENUM32:
212 break;
214 case RPC_FC_RP:
215 case RPC_FC_UP:
216 case RPC_FC_FP:
217 case RPC_FC_OP:
218 case RPC_FC_CARRAY:
219 case RPC_FC_CVARRAY:
220 case RPC_FC_BOGUS_ARRAY:
221 has_pointer = 1;
222 break;
225 * Propagate member attributes
226 * a struct should be at least as complex as its member
228 case RPC_FC_CVSTRUCT:
229 has_conformance = 1;
230 has_variance = 1;
231 has_pointer = 1;
232 break;
234 case RPC_FC_CPSTRUCT:
235 has_conformance = 1;
236 if (list_next( type->fields_or_args, &field->entry ))
237 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
238 field->name);
239 has_pointer = 1;
240 break;
242 case RPC_FC_CSTRUCT:
243 has_conformance = 1;
244 if (list_next( type->fields_or_args, &field->entry ))
245 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
246 field->name);
247 break;
249 case RPC_FC_PSTRUCT:
250 has_pointer = 1;
251 break;
253 default:
254 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
255 /* fallthru - treat it as complex */
257 /* as soon as we see one of these these members, it's bogus... */
258 case RPC_FC_ENCAPSULATED_UNION:
259 case RPC_FC_NON_ENCAPSULATED_UNION:
260 case RPC_FC_BOGUS_STRUCT:
261 case RPC_FC_ENUM16:
262 return RPC_FC_BOGUS_STRUCT;
266 if( has_variance )
268 if ( has_conformance )
269 return RPC_FC_CVSTRUCT;
270 else
271 return RPC_FC_BOGUS_STRUCT;
273 if( has_conformance && has_pointer )
274 return RPC_FC_CPSTRUCT;
275 if( has_conformance )
276 return RPC_FC_CSTRUCT;
277 if( has_pointer )
278 return RPC_FC_PSTRUCT;
279 return RPC_FC_STRUCT;
282 static int get_array_type(const type_t *type)
284 if (is_array(type))
286 const type_t *rt = type->ref;
287 if (is_user_type(rt))
288 return RPC_FC_BOGUS_ARRAY;
289 switch (get_struct_type(rt))
291 case RPC_FC_BOGUS_STRUCT:
292 case RPC_FC_NON_ENCAPSULATED_UNION:
293 case RPC_FC_ENCAPSULATED_UNION:
294 case RPC_FC_ENUM16:
295 return RPC_FC_BOGUS_ARRAY;
296 /* FC_RP should be above, but widl overuses these, and will break things. */
297 case RPC_FC_UP:
298 case RPC_FC_RP:
299 if (rt->ref->type == RPC_FC_IP) return RPC_FC_BOGUS_ARRAY;
300 break;
303 if (type->type == RPC_FC_LGFARRAY || type->type == RPC_FC_LGVARRAY)
305 unsigned int align = 0;
306 size_t size = type_memsize(type, &align);
307 if (size * type->dim <= 0xffff)
308 return (type->type == RPC_FC_LGFARRAY) ? RPC_FC_SMFARRAY : RPC_FC_SMVARRAY;
311 return type->type;
314 int is_struct(unsigned char type)
316 switch (type)
318 case RPC_FC_STRUCT:
319 case RPC_FC_PSTRUCT:
320 case RPC_FC_CSTRUCT:
321 case RPC_FC_CPSTRUCT:
322 case RPC_FC_CVSTRUCT:
323 case RPC_FC_BOGUS_STRUCT:
324 return 1;
325 default:
326 return 0;
330 static int is_non_complex_struct(const type_t *type)
332 switch (get_struct_type(type))
334 case RPC_FC_STRUCT:
335 case RPC_FC_PSTRUCT:
336 case RPC_FC_CSTRUCT:
337 case RPC_FC_CPSTRUCT:
338 case RPC_FC_CVSTRUCT:
339 return 1;
340 default:
341 return 0;
345 int is_union(unsigned char type)
347 switch (type)
349 case RPC_FC_ENCAPSULATED_UNION:
350 case RPC_FC_NON_ENCAPSULATED_UNION:
351 return 1;
352 default:
353 return 0;
357 static int type_has_pointers(const type_t *type)
359 if (is_user_type(type))
360 return FALSE;
361 else if (is_ptr(type))
362 return TRUE;
363 else if (is_array(type))
364 return type_has_pointers(type->ref);
365 else if (is_struct(type->type))
367 const var_t *field;
368 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
370 if (type_has_pointers(field->type))
371 return TRUE;
374 else if (is_union(type->type))
376 var_list_t *fields;
377 const var_t *field;
378 if (type->type == RPC_FC_ENCAPSULATED_UNION)
380 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
381 fields = uv->type->fields_or_args;
383 else
384 fields = type->fields_or_args;
385 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
387 if (field->type && type_has_pointers(field->type))
388 return TRUE;
392 return FALSE;
395 static int type_has_full_pointer(const type_t *type)
397 if (is_user_type(type))
398 return FALSE;
399 else if (type->type == RPC_FC_FP)
400 return TRUE;
401 else if (is_ptr(type))
402 return FALSE;
403 else if (is_array(type))
404 return type_has_full_pointer(type->ref);
405 else if (is_struct(type->type))
407 const var_t *field;
408 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
410 if (type_has_full_pointer(field->type))
411 return TRUE;
414 else if (is_union(type->type))
416 var_list_t *fields;
417 const var_t *field;
418 if (type->type == RPC_FC_ENCAPSULATED_UNION)
420 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
421 fields = uv->type->fields_or_args;
423 else
424 fields = type->fields_or_args;
425 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
427 if (field->type && type_has_full_pointer(field->type))
428 return TRUE;
432 return FALSE;
435 static unsigned short user_type_offset(const char *name)
437 user_type_t *ut;
438 unsigned short off = 0;
439 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
441 if (strcmp(name, ut->name) == 0)
442 return off;
443 ++off;
445 error("user_type_offset: couldn't find type (%s)\n", name);
446 return 0;
449 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
451 type->typestring_offset = offset;
452 if (file) type->tfswrite = FALSE;
455 static void guard_rec(type_t *type)
457 /* types that contain references to themselves (like a linked list),
458 need to be shielded from infinite recursion when writing embedded
459 types */
460 if (type->typestring_offset)
461 type->tfswrite = FALSE;
462 else
463 type->typestring_offset = 1;
466 static type_t *get_user_type(const type_t *t, const char **pname)
468 for (;;)
470 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
471 if (ut)
473 if (pname)
474 *pname = t->name;
475 return ut;
478 if (t->kind == TKIND_ALIAS)
479 t = t->orig;
480 else
481 return 0;
485 int is_user_type(const type_t *t)
487 return get_user_type(t, NULL) != NULL;
490 static int is_embedded_complex(const type_t *type)
492 unsigned char tc = type->type;
493 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
494 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
497 static const char *get_context_handle_type_name(const type_t *type)
499 const type_t *t;
500 for (t = type; is_ptr(t); t = t->ref)
501 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
502 return t->name;
503 assert(0);
504 return NULL;
507 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
508 do { \
509 if (file) \
510 fprintf(file, "/* %2u */\n", typestring_offset); \
511 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
513 while (0)
515 static void print_file(FILE *file, int indent, const char *format, ...)
517 va_list va;
518 va_start(va, format);
519 print(file, indent, format, va);
520 va_end(va);
523 void print(FILE *file, int indent, const char *format, va_list va)
525 if (file)
527 if (format[0] != '\n')
528 while (0 < indent--)
529 fprintf(file, " ");
530 vfprintf(file, format, va);
535 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
537 if (decl_indirect(t))
539 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
540 local_var_prefix, n, local_var_prefix, n);
541 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
543 else if (is_ptr(t) || is_array(t))
544 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
547 void write_parameters_init(FILE *file, int indent, const func_t *func, const char *local_var_prefix)
549 const var_t *var;
551 if (!is_void(get_func_return_type(func)))
552 write_var_init(file, indent, get_func_return_type(func), "_RetVal", local_var_prefix);
554 if (!func->args)
555 return;
557 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
558 write_var_init(file, indent, var->type, var->name, local_var_prefix);
560 fprintf(file, "\n");
563 static void write_formatdesc(FILE *f, int indent, const char *str)
565 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
566 print_file(f, indent, "{\n");
567 print_file(f, indent + 1, "short Pad;\n");
568 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
569 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
570 print_file(f, indent, "\n");
573 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
575 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
576 get_size_typeformatstring(stmts, pred));
578 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
579 get_size_procformatstring(stmts, pred));
581 fprintf(f, "\n");
582 write_formatdesc(f, indent, "TYPE");
583 write_formatdesc(f, indent, "PROC");
584 fprintf(f, "\n");
585 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
586 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
587 print_file(f, indent, "\n");
590 static inline int is_base_type(unsigned char type)
592 switch (type)
594 case RPC_FC_BYTE:
595 case RPC_FC_CHAR:
596 case RPC_FC_USMALL:
597 case RPC_FC_SMALL:
598 case RPC_FC_WCHAR:
599 case RPC_FC_USHORT:
600 case RPC_FC_SHORT:
601 case RPC_FC_ULONG:
602 case RPC_FC_LONG:
603 case RPC_FC_HYPER:
604 case RPC_FC_IGNORE:
605 case RPC_FC_FLOAT:
606 case RPC_FC_DOUBLE:
607 case RPC_FC_ENUM16:
608 case RPC_FC_ENUM32:
609 case RPC_FC_ERROR_STATUS_T:
610 case RPC_FC_BIND_PRIMITIVE:
611 return TRUE;
613 default:
614 return FALSE;
618 int decl_indirect(const type_t *t)
620 return is_user_type(t)
621 || (!is_base_type(t->type)
622 && !is_ptr(t)
623 && !is_array(t));
626 static size_t write_procformatstring_type(FILE *file, int indent,
627 const char *name,
628 const type_t *type,
629 const attr_list_t *attrs,
630 int is_return)
632 size_t size;
634 int is_in = is_attr(attrs, ATTR_IN);
635 int is_out = is_attr(attrs, ATTR_OUT);
637 if (!is_in && !is_out) is_in = TRUE;
639 if (!type->declarray && is_base_type(type->type))
641 if (is_return)
642 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
643 else
644 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
646 if (type->type == RPC_FC_BIND_PRIMITIVE)
648 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
649 size = 2; /* includes param type prefix */
651 else if (is_base_type(type->type))
653 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
654 size = 2; /* includes param type prefix */
656 else
658 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
659 size = 0;
662 else
664 if (is_return)
665 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
666 else if (is_in && is_out)
667 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
668 else if (is_out)
669 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
670 else
671 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
673 print_file(file, indent, "0x01,\n");
674 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
675 size = 4; /* includes param type prefix */
677 return size;
680 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
682 const statement_t *stmt;
683 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
685 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
687 const func_t *func;
688 if (!pred(stmt->u.type))
689 continue;
690 if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
692 if (is_local(func->def->attrs)) continue;
693 /* emit argument data */
694 if (func->args)
696 const var_t *var;
697 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
698 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
701 /* emit return value data */
702 if (is_void(get_func_return_type(func)))
704 print_file(file, indent, "0x5b, /* FC_END */\n");
705 print_file(file, indent, "0x5c, /* FC_PAD */\n");
707 else
708 write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
711 else if (stmt->type == STMT_LIBRARY)
712 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
716 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
718 int indent = 0;
720 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
721 print_file(file, indent, "{\n");
722 indent++;
723 print_file(file, indent, "0,\n");
724 print_file(file, indent, "{\n");
725 indent++;
727 write_procformatstring_stmts(file, indent, stmts, pred);
729 print_file(file, indent, "0x0\n");
730 indent--;
731 print_file(file, indent, "}\n");
732 indent--;
733 print_file(file, indent, "};\n");
734 print_file(file, indent, "\n");
737 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
739 if (is_base_type(type->type))
741 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
742 *typestring_offset += 1;
743 return 1;
746 return 0;
749 /* write conformance / variance descriptor */
750 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
751 unsigned int baseoff, const type_t *type,
752 const expr_t *expr)
754 unsigned char operator_type = 0;
755 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
756 const char *conftype_string = "";
757 const char *operator_string = "no operators";
758 const expr_t *subexpr;
760 if (!expr)
762 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
763 return 4;
766 if (!structure)
768 /* Top-level conformance calculations are done inline. */
769 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
770 RPC_FC_TOP_LEVEL_CONFORMANCE);
771 print_file (file, 2, "0x0,\n");
772 print_file (file, 2, "NdrFcShort(0x0),\n");
773 return 4;
776 if (expr->is_const)
778 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
779 error("write_conf_or_var_desc: constant value %ld is greater than "
780 "the maximum constant size of %d\n", expr->cval,
781 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
783 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
784 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
785 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
786 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
788 return 4;
791 if (is_ptr(type) || (is_array(type) && !type->declarray))
793 conftype = RPC_FC_POINTER_CONFORMANCE;
794 conftype_string = "field pointer, ";
797 subexpr = expr;
798 switch (subexpr->type)
800 case EXPR_PPTR:
801 subexpr = subexpr->ref;
802 operator_type = RPC_FC_DEREFERENCE;
803 operator_string = "FC_DEREFERENCE";
804 break;
805 case EXPR_DIV:
806 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
808 subexpr = subexpr->ref;
809 operator_type = RPC_FC_DIV_2;
810 operator_string = "FC_DIV_2";
812 break;
813 case EXPR_MUL:
814 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
816 subexpr = subexpr->ref;
817 operator_type = RPC_FC_MULT_2;
818 operator_string = "FC_MULT_2";
820 break;
821 case EXPR_SUB:
822 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
824 subexpr = subexpr->ref;
825 operator_type = RPC_FC_SUB_1;
826 operator_string = "FC_SUB_1";
828 break;
829 case EXPR_ADD:
830 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
832 subexpr = subexpr->ref;
833 operator_type = RPC_FC_ADD_1;
834 operator_string = "FC_ADD_1";
836 break;
837 default:
838 break;
841 if (subexpr->type == EXPR_IDENTIFIER)
843 const type_t *correlation_variable = NULL;
844 unsigned char correlation_variable_type;
845 unsigned char param_type = 0;
846 size_t offset = 0;
847 const var_t *var;
849 if (structure->fields_or_args) LIST_FOR_EACH_ENTRY( var, structure->fields_or_args, const var_t, entry )
851 unsigned int align = 0;
852 /* FIXME: take alignment into account */
853 if (var->name && !strcmp(var->name, subexpr->u.sval))
855 correlation_variable = var->type;
856 break;
858 offset += type_memsize(var->type, &align);
860 if (!correlation_variable)
861 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
862 subexpr->u.sval);
864 correlation_variable = expr_resolve_type(NULL, structure, expr);
866 offset -= baseoff;
867 correlation_variable_type = correlation_variable->type;
869 switch (correlation_variable_type)
871 case RPC_FC_CHAR:
872 case RPC_FC_SMALL:
873 param_type = RPC_FC_SMALL;
874 break;
875 case RPC_FC_BYTE:
876 case RPC_FC_USMALL:
877 param_type = RPC_FC_USMALL;
878 break;
879 case RPC_FC_WCHAR:
880 case RPC_FC_SHORT:
881 case RPC_FC_ENUM16:
882 param_type = RPC_FC_SHORT;
883 break;
884 case RPC_FC_USHORT:
885 param_type = RPC_FC_USHORT;
886 break;
887 case RPC_FC_LONG:
888 case RPC_FC_ENUM32:
889 param_type = RPC_FC_LONG;
890 break;
891 case RPC_FC_ULONG:
892 param_type = RPC_FC_ULONG;
893 break;
894 default:
895 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
896 correlation_variable_type);
899 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
900 conftype | param_type, conftype_string, string_of_type(param_type));
901 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
902 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
903 offset, offset);
905 else
907 unsigned int callback_offset = 0;
908 struct expr_eval_routine *eval;
909 int found = 0;
911 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
913 if (!strcmp (eval->structure->name, structure->name)
914 && !compare_expr (eval->expr, expr))
916 found = 1;
917 break;
919 callback_offset++;
922 if (!found)
924 eval = xmalloc (sizeof(*eval));
925 eval->structure = structure;
926 eval->baseoff = baseoff;
927 eval->expr = expr;
928 list_add_tail (&expr_eval_routines, &eval->entry);
931 if (callback_offset > USHRT_MAX)
932 error("Maximum number of callback routines reached\n");
934 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
935 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
936 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
938 return 4;
941 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
943 int have_align = FALSE;
944 size_t size = 0;
945 const var_t *v;
947 if (!fields) return 0;
948 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
950 unsigned int falign = 0;
951 size_t fsize = type_memsize(v->type, &falign);
952 if (!have_align)
954 *align = falign;
955 have_align = TRUE;
957 size = ROUND_SIZE(size, falign);
958 size += fsize;
961 size = ROUND_SIZE(size, *align);
962 return size;
965 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
967 size_t size, maxs = 0;
968 unsigned int align = *pmaxa;
969 const var_t *v;
971 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
973 /* we could have an empty default field with NULL type */
974 if (v->type)
976 size = type_memsize(v->type, &align);
977 if (maxs < size) maxs = size;
978 if (*pmaxa < align) *pmaxa = align;
982 return maxs;
985 int get_padding(const var_list_t *fields)
987 unsigned short offset = 0;
988 int salign = -1;
989 const var_t *f;
991 if (!fields)
992 return 0;
994 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
996 type_t *ft = f->type;
997 unsigned int align = 0;
998 size_t size = type_memsize(ft, &align);
999 if (salign == -1)
1000 salign = align;
1001 offset = ROUND_SIZE(offset, align);
1002 offset += size;
1005 return ROUNDING(offset, salign);
1008 size_t type_memsize(const type_t *t, unsigned int *align)
1010 size_t size = 0;
1012 if (t->kind == TKIND_ALIAS)
1013 size = type_memsize(t->orig, align);
1014 else if (t->declarray && is_conformant_array(t))
1016 type_memsize(t->ref, align);
1017 size = 0;
1019 else if (is_ptr(t) || is_conformant_array(t))
1021 size = sizeof(void *);
1022 if (size > *align) *align = size;
1024 else switch (t->type)
1026 case RPC_FC_BYTE:
1027 case RPC_FC_CHAR:
1028 case RPC_FC_USMALL:
1029 case RPC_FC_SMALL:
1030 size = 1;
1031 if (size > *align) *align = size;
1032 break;
1033 case RPC_FC_WCHAR:
1034 case RPC_FC_USHORT:
1035 case RPC_FC_SHORT:
1036 case RPC_FC_ENUM16:
1037 size = 2;
1038 if (size > *align) *align = size;
1039 break;
1040 case RPC_FC_ULONG:
1041 case RPC_FC_LONG:
1042 case RPC_FC_ERROR_STATUS_T:
1043 case RPC_FC_ENUM32:
1044 case RPC_FC_FLOAT:
1045 size = 4;
1046 if (size > *align) *align = size;
1047 break;
1048 case RPC_FC_HYPER:
1049 case RPC_FC_DOUBLE:
1050 size = 8;
1051 if (size > *align) *align = size;
1052 break;
1053 case RPC_FC_STRUCT:
1054 case RPC_FC_CVSTRUCT:
1055 case RPC_FC_CPSTRUCT:
1056 case RPC_FC_CSTRUCT:
1057 case RPC_FC_PSTRUCT:
1058 case RPC_FC_BOGUS_STRUCT:
1059 size = fields_memsize(t->fields_or_args, align);
1060 break;
1061 case RPC_FC_ENCAPSULATED_UNION:
1062 case RPC_FC_NON_ENCAPSULATED_UNION:
1063 size = union_memsize(t->fields_or_args, align);
1064 break;
1065 case RPC_FC_SMFARRAY:
1066 case RPC_FC_LGFARRAY:
1067 case RPC_FC_SMVARRAY:
1068 case RPC_FC_LGVARRAY:
1069 case RPC_FC_BOGUS_ARRAY:
1070 size = t->dim * type_memsize(t->ref, align);
1071 break;
1072 default:
1073 error("type_memsize: Unknown type %d\n", t->type);
1074 size = 0;
1077 return size;
1080 int is_full_pointer_function(const func_t *func)
1082 const var_t *var;
1083 if (type_has_full_pointer(get_func_return_type(func)))
1084 return TRUE;
1085 if (!func->args)
1086 return FALSE;
1087 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1088 if (type_has_full_pointer( var->type ))
1089 return TRUE;
1090 return FALSE;
1093 void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
1095 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1096 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1097 fprintf(file, "\n");
1100 void write_full_pointer_free(FILE *file, int indent, const func_t *func)
1102 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1103 fprintf(file, "\n");
1106 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
1108 short absoff = type->ref->typestring_offset;
1109 short reloff = absoff - (offset + 2);
1110 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
1112 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1113 type->type, ptr_attr, string_of_type(type->type));
1114 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1115 reloff, reloff, absoff);
1116 return 4;
1119 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1121 unsigned char fc = type->ref->type;
1122 /* for historical reasons, write_simple_pointer also handled string types,
1123 * but no longer does. catch bad uses of the function with this check */
1124 if (is_string_type(type->attrs, type))
1125 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1126 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1127 type->type, string_of_type(type->type));
1128 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1129 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1130 return 4;
1133 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1135 print_file(file, 0, "/* %u (", tfsoff);
1136 write_type_decl(file, t, NULL);
1137 print_file(file, 0, ") */\n");
1140 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1142 unsigned int offset = *typestring_offset;
1144 print_start_tfs_comment(file, type, offset);
1145 update_tfsoff(type, offset, file);
1147 if (type->ref->typestring_offset)
1148 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1149 else if (is_base_type(type->ref->type))
1150 *typestring_offset += write_simple_pointer(file, type);
1152 return offset;
1155 static int processed(const type_t *type)
1157 return type->typestring_offset && !type->tfswrite;
1160 static int user_type_has_variable_size(const type_t *t)
1162 if (is_ptr(t))
1163 return TRUE;
1164 else
1165 switch (get_struct_type(t))
1167 case RPC_FC_PSTRUCT:
1168 case RPC_FC_CSTRUCT:
1169 case RPC_FC_CPSTRUCT:
1170 case RPC_FC_CVSTRUCT:
1171 return TRUE;
1173 /* Note: Since this only applies to user types, we can't have a conformant
1174 array here, and strings should get filed under pointer in this case. */
1175 return FALSE;
1178 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1180 unsigned int start, absoff, flags;
1181 unsigned int align = 0, ualign = 0;
1182 const char *name = NULL;
1183 type_t *utype = get_user_type(type, &name);
1184 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1185 size_t size = type_memsize(type, &align);
1186 unsigned short funoff = user_type_offset(name);
1187 short reloff;
1189 guard_rec(type);
1191 if (is_base_type(utype->type))
1193 absoff = *tfsoff;
1194 print_start_tfs_comment(file, utype, absoff);
1195 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1196 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1197 *tfsoff += 2;
1199 else
1201 if (!processed(utype))
1202 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1203 absoff = utype->typestring_offset;
1206 if (utype->type == RPC_FC_RP)
1207 flags = 0x40;
1208 else if (utype->type == RPC_FC_UP)
1209 flags = 0x80;
1210 else
1211 flags = 0;
1213 start = *tfsoff;
1214 update_tfsoff(type, start, file);
1215 print_start_tfs_comment(file, type, start);
1216 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1217 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1218 flags | (align - 1), align - 1, flags);
1219 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1220 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1221 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1222 *tfsoff += 8;
1223 reloff = absoff - *tfsoff;
1224 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1225 *tfsoff += 2;
1228 static void write_member_type(FILE *file, const type_t *cont,
1229 const attr_list_t *attrs, const type_t *type,
1230 unsigned int *corroff, unsigned int *tfsoff)
1232 if (is_embedded_complex(type) && !is_conformant_array(type))
1234 size_t absoff;
1235 short reloff;
1237 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1239 absoff = *corroff;
1240 *corroff += 8;
1242 else
1244 absoff = type->typestring_offset;
1246 reloff = absoff - (*tfsoff + 2);
1248 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1249 /* FIXME: actually compute necessary padding */
1250 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1251 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1252 reloff, reloff, absoff);
1253 *tfsoff += 4;
1255 else if (is_ptr(type) || is_conformant_array(type))
1257 unsigned char fc = (get_struct_type(cont) == RPC_FC_BOGUS_STRUCT
1258 ? RPC_FC_POINTER
1259 : RPC_FC_LONG);
1260 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1261 *tfsoff += 1;
1263 else if (!write_base_type(file, type, tfsoff))
1264 error("Unsupported member type 0x%x\n", type->type);
1267 static void write_end(FILE *file, unsigned int *tfsoff)
1269 if (*tfsoff % 2 == 0)
1271 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1272 *tfsoff += 1;
1274 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1275 *tfsoff += 1;
1278 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1280 unsigned int offset = 0;
1281 var_list_t *fs = type->fields_or_args;
1282 var_t *f;
1284 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1286 unsigned int align = 0;
1287 type_t *ft = f->type;
1288 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1290 unsigned int absoff = ft->typestring_offset;
1291 short reloff = absoff - (*tfsoff + 6);
1292 print_file(file, 0, "/* %d */\n", *tfsoff);
1293 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1294 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1295 write_conf_or_var_desc(file, current_structure, offset, ft,
1296 get_attrp(f->attrs, ATTR_SWITCHIS));
1297 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1298 reloff, reloff, absoff);
1299 *tfsoff += 8;
1302 /* FIXME: take alignment into account */
1303 offset += type_memsize(ft, &align);
1307 static int write_no_repeat_pointer_descriptions(
1308 FILE *file, type_t *type,
1309 size_t *offset_in_memory, size_t *offset_in_buffer,
1310 unsigned int *typestring_offset)
1312 int written = 0;
1313 unsigned int align;
1315 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1317 size_t memsize;
1319 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1320 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1322 /* pointer instance */
1323 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1324 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1325 *typestring_offset += 6;
1327 if (is_ptr(type))
1329 if (is_string_type(type->attrs, type))
1330 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1331 else
1332 write_pointer_tfs(file, type, typestring_offset);
1334 else
1336 unsigned absoff = type->typestring_offset;
1337 short reloff = absoff - (*typestring_offset + 2);
1338 /* FIXME: get pointer attributes from field */
1339 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1340 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1341 reloff, reloff, absoff);
1342 *typestring_offset += 4;
1345 align = 0;
1346 memsize = type_memsize(type, &align);
1347 *offset_in_memory += memsize;
1348 /* increment these separately as in the case of conformant (varying)
1349 * structures these start at different values */
1350 *offset_in_buffer += memsize;
1352 return 1;
1355 if (is_non_complex_struct(type))
1357 const var_t *v;
1358 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1360 if (offset_in_memory && offset_in_buffer)
1362 size_t padding;
1363 align = 0;
1364 type_memsize(v->type, &align);
1365 padding = ROUNDING(*offset_in_memory, align);
1366 *offset_in_memory += padding;
1367 *offset_in_buffer += padding;
1369 written += write_no_repeat_pointer_descriptions(
1370 file, v->type,
1371 offset_in_memory, offset_in_buffer, typestring_offset);
1374 else
1376 size_t memsize;
1377 align = 0;
1378 memsize = type_memsize(type, &align);
1379 *offset_in_memory += memsize;
1380 /* increment these separately as in the case of conformant (varying)
1381 * structures these start at different values */
1382 *offset_in_buffer += memsize;
1385 return written;
1388 static int write_pointer_description_offsets(
1389 FILE *file, const attr_list_t *attrs, type_t *type,
1390 size_t *offset_in_memory, size_t *offset_in_buffer,
1391 unsigned int *typestring_offset)
1393 int written = 0;
1394 unsigned int align;
1396 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1398 if (offset_in_memory && offset_in_buffer)
1400 size_t memsize;
1402 /* pointer instance */
1403 /* FIXME: sometimes from end of structure, sometimes from beginning */
1404 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1405 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1407 align = 0;
1408 memsize = type_memsize(type, &align);
1409 *offset_in_memory += memsize;
1410 /* increment these separately as in the case of conformant (varying)
1411 * structures these start at different values */
1412 *offset_in_buffer += memsize;
1414 *typestring_offset += 4;
1416 if (is_string_type(attrs, type))
1417 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1418 else if (processed(type->ref) || is_base_type(type->ref->type))
1419 write_pointer_tfs(file, type, typestring_offset);
1420 else
1421 error("write_pointer_description_offsets: type format string unknown\n");
1423 return 1;
1426 if (is_array(type))
1428 return write_pointer_description_offsets(
1429 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1430 typestring_offset);
1432 else if (is_non_complex_struct(type))
1434 /* otherwise search for interesting fields to parse */
1435 const var_t *v;
1436 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1438 if (offset_in_memory && offset_in_buffer)
1440 size_t padding;
1441 align = 0;
1442 type_memsize(v->type, &align);
1443 padding = ROUNDING(*offset_in_memory, align);
1444 *offset_in_memory += padding;
1445 *offset_in_buffer += padding;
1447 written += write_pointer_description_offsets(
1448 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1449 typestring_offset);
1452 else
1454 if (offset_in_memory && offset_in_buffer)
1456 size_t memsize;
1457 align = 0;
1458 memsize = type_memsize(type, &align);
1459 *offset_in_memory += memsize;
1460 /* increment these separately as in the case of conformant (varying)
1461 * structures these start at different values */
1462 *offset_in_buffer += memsize;
1466 return written;
1469 /* Note: if file is NULL return value is number of pointers to write, else
1470 * it is the number of type format characters written */
1471 static int write_fixed_array_pointer_descriptions(
1472 FILE *file, const attr_list_t *attrs, type_t *type,
1473 size_t *offset_in_memory, size_t *offset_in_buffer,
1474 unsigned int *typestring_offset)
1476 unsigned int align;
1477 int pointer_count = 0;
1478 int real_type = get_array_type( type );
1480 if (real_type == RPC_FC_SMFARRAY || real_type == RPC_FC_LGFARRAY)
1482 unsigned int temp = 0;
1483 /* unfortunately, this needs to be done in two passes to avoid
1484 * writing out redundant FC_FIXED_REPEAT descriptions */
1485 pointer_count = write_pointer_description_offsets(
1486 NULL, attrs, type->ref, NULL, NULL, &temp);
1487 if (pointer_count > 0)
1489 unsigned int increment_size;
1490 size_t offset_of_array_pointer_mem = 0;
1491 size_t offset_of_array_pointer_buf = 0;
1493 align = 0;
1494 increment_size = type_memsize(type->ref, &align);
1496 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1497 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1498 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1499 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1500 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1501 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1502 *typestring_offset += 10;
1504 pointer_count = write_pointer_description_offsets(
1505 file, attrs, type, &offset_of_array_pointer_mem,
1506 &offset_of_array_pointer_buf, typestring_offset);
1509 else if (is_struct(type->type))
1511 const var_t *v;
1512 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1514 if (offset_in_memory && offset_in_buffer)
1516 size_t padding;
1517 align = 0;
1518 type_memsize(v->type, &align);
1519 padding = ROUNDING(*offset_in_memory, align);
1520 *offset_in_memory += padding;
1521 *offset_in_buffer += padding;
1523 pointer_count += write_fixed_array_pointer_descriptions(
1524 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1525 typestring_offset);
1528 else
1530 if (offset_in_memory && offset_in_buffer)
1532 size_t memsize;
1533 align = 0;
1534 memsize = type_memsize(type, &align);
1535 *offset_in_memory += memsize;
1536 /* increment these separately as in the case of conformant (varying)
1537 * structures these start at different values */
1538 *offset_in_buffer += memsize;
1542 return pointer_count;
1545 /* Note: if file is NULL return value is number of pointers to write, else
1546 * it is the number of type format characters written */
1547 static int write_conformant_array_pointer_descriptions(
1548 FILE *file, const attr_list_t *attrs, type_t *type,
1549 size_t offset_in_memory, unsigned int *typestring_offset)
1551 unsigned int align;
1552 int pointer_count = 0;
1554 if (is_conformant_array(type) && !type->length_is)
1556 unsigned int temp = 0;
1557 /* unfortunately, this needs to be done in two passes to avoid
1558 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1559 pointer_count = write_pointer_description_offsets(
1560 NULL, attrs, type->ref, NULL, NULL, &temp);
1561 if (pointer_count > 0)
1563 unsigned int increment_size;
1564 size_t offset_of_array_pointer_mem = offset_in_memory;
1565 size_t offset_of_array_pointer_buf = offset_in_memory;
1567 align = 0;
1568 increment_size = type_memsize(type->ref, &align);
1570 if (increment_size > USHRT_MAX)
1571 error("array size of %u bytes is too large\n", increment_size);
1573 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1574 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1575 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1576 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1577 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1578 *typestring_offset += 8;
1580 pointer_count = write_pointer_description_offsets(
1581 file, attrs, type->ref, &offset_of_array_pointer_mem,
1582 &offset_of_array_pointer_buf, typestring_offset);
1586 return pointer_count;
1589 /* Note: if file is NULL return value is number of pointers to write, else
1590 * it is the number of type format characters written */
1591 static int write_varying_array_pointer_descriptions(
1592 FILE *file, const attr_list_t *attrs, type_t *type,
1593 size_t *offset_in_memory, size_t *offset_in_buffer,
1594 unsigned int *typestring_offset)
1596 unsigned int align;
1597 int pointer_count = 0;
1599 if (is_array(type) && type->length_is)
1601 unsigned int temp = 0;
1602 /* unfortunately, this needs to be done in two passes to avoid
1603 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1604 pointer_count = write_pointer_description_offsets(
1605 NULL, attrs, type->ref, NULL, NULL, &temp);
1606 if (pointer_count > 0)
1608 unsigned int increment_size;
1610 align = 0;
1611 increment_size = type_memsize(type->ref, &align);
1613 if (increment_size > USHRT_MAX)
1614 error("array size of %u bytes is too large\n", increment_size);
1616 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1617 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1618 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1619 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1620 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1621 *typestring_offset += 8;
1623 pointer_count = write_pointer_description_offsets(
1624 file, attrs, type, offset_in_memory,
1625 offset_in_buffer, typestring_offset);
1628 else if (is_struct(type->type))
1630 const var_t *v;
1631 LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1633 if (offset_in_memory && offset_in_buffer)
1635 size_t padding;
1637 if (is_array(v->type) && v->type->length_is)
1639 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1640 /* skip over variance and offset in buffer */
1641 *offset_in_buffer += 8;
1644 align = 0;
1645 type_memsize(v->type, &align);
1646 padding = ROUNDING(*offset_in_memory, align);
1647 *offset_in_memory += padding;
1648 *offset_in_buffer += padding;
1650 pointer_count += write_varying_array_pointer_descriptions(
1651 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1652 typestring_offset);
1655 else
1657 if (offset_in_memory && offset_in_buffer)
1659 size_t memsize;
1660 align = 0;
1661 memsize = type_memsize(type, &align);
1662 *offset_in_memory += memsize;
1663 /* increment these separately as in the case of conformant (varying)
1664 * structures these start at different values */
1665 *offset_in_buffer += memsize;
1669 return pointer_count;
1672 static void write_pointer_description(FILE *file, type_t *type,
1673 unsigned int *typestring_offset)
1675 size_t offset_in_buffer;
1676 size_t offset_in_memory;
1678 /* pass 1: search for single instance of a pointer (i.e. don't descend
1679 * into arrays) */
1680 if (!is_array(type))
1682 offset_in_memory = 0;
1683 offset_in_buffer = 0;
1684 write_no_repeat_pointer_descriptions(
1685 file, type,
1686 &offset_in_memory, &offset_in_buffer, typestring_offset);
1689 /* pass 2: search for pointers in fixed arrays */
1690 offset_in_memory = 0;
1691 offset_in_buffer = 0;
1692 write_fixed_array_pointer_descriptions(
1693 file, NULL, type,
1694 &offset_in_memory, &offset_in_buffer, typestring_offset);
1696 /* pass 3: search for pointers in conformant only arrays (but don't descend
1697 * into conformant varying or varying arrays) */
1698 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1699 write_conformant_array_pointer_descriptions(
1700 file, NULL, type, 0, typestring_offset);
1701 else if (get_struct_type(type) == RPC_FC_CPSTRUCT)
1703 unsigned int align = 0;
1704 type_t *carray = find_array_or_string_in_struct(type)->type;
1705 write_conformant_array_pointer_descriptions(
1706 file, NULL, carray,
1707 type_memsize(type, &align),
1708 typestring_offset);
1711 /* pass 4: search for pointers in varying arrays */
1712 offset_in_memory = 0;
1713 offset_in_buffer = 0;
1714 write_varying_array_pointer_descriptions(
1715 file, NULL, type,
1716 &offset_in_memory, &offset_in_buffer, typestring_offset);
1719 int is_declptr(const type_t *t)
1721 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1724 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1725 type_t *type,
1726 const char *name, unsigned int *typestring_offset)
1728 size_t start_offset;
1729 unsigned char rtype;
1731 if (is_declptr(type))
1733 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1734 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1735 if (!pointer_type)
1736 pointer_type = RPC_FC_RP;
1737 print_start_tfs_comment(file, type, *typestring_offset);
1738 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1739 pointer_type, flag, string_of_type(pointer_type),
1740 flag ? " [simple_pointer]" : "");
1741 *typestring_offset += 2;
1742 if (!flag)
1744 print_file(file, 2, "NdrFcShort(0x2),\n");
1745 *typestring_offset += 2;
1749 start_offset = *typestring_offset;
1750 update_tfsoff(type, start_offset, file);
1752 rtype = type->ref->type;
1754 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1756 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1757 return start_offset;
1760 if (type->declarray && !is_conformant_array(type))
1762 /* FIXME: multi-dimensional array */
1763 if (0xffffuL < type->dim)
1764 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1765 name, 0xffffu, type->dim - 0xffffu);
1767 if (rtype == RPC_FC_CHAR)
1768 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1769 else
1770 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1771 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1772 *typestring_offset += 2;
1774 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1775 *typestring_offset += 2;
1777 return start_offset;
1779 else if (type->size_is)
1781 unsigned int align = 0;
1783 if (rtype == RPC_FC_CHAR)
1784 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1785 else
1786 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1787 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1788 *typestring_offset += 2;
1790 *typestring_offset += write_conf_or_var_desc(
1791 file, current_structure,
1792 (type->declarray && current_structure
1793 ? type_memsize(current_structure, &align)
1794 : 0),
1795 type, type->size_is);
1797 return start_offset;
1799 else
1801 if (rtype == RPC_FC_WCHAR)
1802 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1803 else
1804 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1805 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1806 *typestring_offset += 2;
1808 return start_offset;
1812 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1813 const char *name, unsigned int *typestring_offset)
1815 const expr_t *length_is = type->length_is;
1816 const expr_t *size_is = type->size_is;
1817 unsigned int align = 0;
1818 size_t size;
1819 size_t start_offset;
1820 int real_type;
1821 int has_pointer;
1822 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1823 unsigned int baseoff
1824 = type->declarray && current_structure
1825 ? type_memsize(current_structure, &align)
1826 : 0;
1828 if (!pointer_type)
1829 pointer_type = RPC_FC_RP;
1831 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1832 has_pointer = TRUE;
1833 else
1834 has_pointer = type_has_pointers(type->ref);
1836 align = 0;
1837 size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1838 real_type = get_array_type( type );
1840 start_offset = *typestring_offset;
1841 update_tfsoff(type, start_offset, file);
1842 print_start_tfs_comment(file, type, start_offset);
1843 print_file(file, 2, "0x%02x,\t/* %s */\n", real_type, string_of_type(real_type));
1844 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1845 *typestring_offset += 2;
1847 align = 0;
1848 if (real_type != RPC_FC_BOGUS_ARRAY)
1850 if (real_type == RPC_FC_LGFARRAY || real_type == RPC_FC_LGVARRAY)
1852 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1853 *typestring_offset += 4;
1855 else
1857 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1858 *typestring_offset += 2;
1861 if (is_conformant_array(type))
1862 *typestring_offset
1863 += write_conf_or_var_desc(file, current_structure, baseoff,
1864 type, size_is);
1866 if (real_type == RPC_FC_SMVARRAY || real_type == RPC_FC_LGVARRAY)
1868 unsigned int elalign = 0;
1869 size_t elsize = type_memsize(type->ref, &elalign);
1871 if (real_type == RPC_FC_LGVARRAY)
1873 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1874 *typestring_offset += 4;
1876 else
1878 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1879 *typestring_offset += 2;
1882 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1883 *typestring_offset += 2;
1886 if (length_is)
1887 *typestring_offset
1888 += write_conf_or_var_desc(file, current_structure, baseoff,
1889 type, length_is);
1891 if (has_pointer && (!type->declarray || !current_structure))
1893 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1894 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1895 *typestring_offset += 2;
1896 write_pointer_description(file, type, typestring_offset);
1897 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1898 *typestring_offset += 1;
1901 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1902 write_end(file, typestring_offset);
1904 else
1906 unsigned int dim = size_is ? 0 : type->dim;
1907 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1908 *typestring_offset += 2;
1909 *typestring_offset
1910 += write_conf_or_var_desc(file, current_structure, baseoff,
1911 type, size_is);
1912 *typestring_offset
1913 += write_conf_or_var_desc(file, current_structure, baseoff,
1914 type, length_is);
1915 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1916 write_end(file, typestring_offset);
1919 return start_offset;
1922 static const var_t *find_array_or_string_in_struct(const type_t *type)
1924 const var_t *last_field;
1925 const type_t *ft;
1926 int real_type;
1928 if (!type->fields_or_args || list_empty(type->fields_or_args))
1929 return NULL;
1931 last_field = LIST_ENTRY( list_tail(type->fields_or_args), const var_t, entry );
1932 ft = last_field->type;
1934 if (ft->declarray && is_conformant_array(ft))
1935 return last_field;
1937 real_type = get_struct_type( type );
1938 if (real_type == RPC_FC_CSTRUCT || real_type == RPC_FC_CPSTRUCT || real_type == RPC_FC_CVSTRUCT)
1939 return find_array_or_string_in_struct(ft);
1940 else
1941 return NULL;
1944 static void write_struct_members(FILE *file, const type_t *type,
1945 unsigned int *corroff, unsigned int *typestring_offset)
1947 const var_t *field;
1948 unsigned short offset = 0;
1949 int salign = -1;
1950 int padding;
1952 if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
1954 type_t *ft = field->type;
1955 if (!ft->declarray || !is_conformant_array(ft))
1957 unsigned int align = 0;
1958 size_t size = type_memsize(ft, &align);
1959 if (salign == -1)
1960 salign = align;
1961 if ((align - 1) & offset)
1963 unsigned char fc = 0;
1964 switch (align)
1966 case 4:
1967 fc = RPC_FC_ALIGNM4;
1968 break;
1969 case 8:
1970 fc = RPC_FC_ALIGNM8;
1971 break;
1972 default:
1973 error("write_struct_members: cannot align type %d\n", ft->type);
1975 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1976 offset = ROUND_SIZE(offset, align);
1977 *typestring_offset += 1;
1979 write_member_type(file, type, field->attrs, field->type, corroff,
1980 typestring_offset);
1981 offset += size;
1985 padding = ROUNDING(offset, salign);
1986 if (padding)
1988 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1989 RPC_FC_STRUCTPAD1 + padding - 1,
1990 padding);
1991 *typestring_offset += 1;
1994 write_end(file, typestring_offset);
1997 static size_t write_struct_tfs(FILE *file, type_t *type,
1998 const char *name, unsigned int *tfsoff)
2000 const type_t *save_current_structure = current_structure;
2001 unsigned int total_size;
2002 const var_t *array;
2003 size_t start_offset;
2004 size_t array_offset;
2005 int has_pointers = 0;
2006 unsigned int align = 0;
2007 unsigned int corroff;
2008 var_t *f;
2009 int real_type = get_struct_type( type );
2011 guard_rec(type);
2012 current_structure = type;
2014 total_size = type_memsize(type, &align);
2015 if (total_size > USHRT_MAX)
2016 error("structure size for %s exceeds %d bytes by %d bytes\n",
2017 name, USHRT_MAX, total_size - USHRT_MAX);
2019 if (type->fields_or_args) LIST_FOR_EACH_ENTRY(f, type->fields_or_args, var_t, entry)
2020 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2021 FALSE, tfsoff);
2022 if (!has_pointers) has_pointers = type_has_pointers(type);
2024 array = find_array_or_string_in_struct(type);
2025 if (array && !processed(array->type))
2026 array_offset
2027 = is_attr(array->attrs, ATTR_STRING)
2028 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2029 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2031 corroff = *tfsoff;
2032 write_descriptors(file, type, tfsoff);
2034 start_offset = *tfsoff;
2035 update_tfsoff(type, start_offset, file);
2036 print_start_tfs_comment(file, type, start_offset);
2037 print_file(file, 2, "0x%x,\t/* %s */\n", real_type, string_of_type(real_type));
2038 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2039 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
2040 *tfsoff += 4;
2042 if (array)
2044 unsigned int absoff = array->type->typestring_offset;
2045 short reloff = absoff - *tfsoff;
2046 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
2047 reloff, reloff, absoff);
2048 *tfsoff += 2;
2050 else if (real_type == RPC_FC_BOGUS_STRUCT)
2052 print_file(file, 2, "NdrFcShort(0x0),\n");
2053 *tfsoff += 2;
2056 if (real_type == RPC_FC_BOGUS_STRUCT)
2058 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2059 nothing is written to file yet. On the actual writing pass,
2060 this will have been updated. */
2061 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2062 int reloff = absoff - *tfsoff;
2063 assert( reloff >= 0 );
2064 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%u) */\n",
2065 reloff, reloff, absoff);
2066 *tfsoff += 2;
2068 else if ((real_type == RPC_FC_PSTRUCT) ||
2069 (real_type == RPC_FC_CPSTRUCT) ||
2070 (real_type == RPC_FC_CVSTRUCT && has_pointers))
2072 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2073 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2074 *tfsoff += 2;
2075 write_pointer_description(file, type, tfsoff);
2076 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2077 *tfsoff += 1;
2080 write_struct_members(file, type, &corroff, tfsoff);
2082 if (real_type == RPC_FC_BOGUS_STRUCT)
2084 const var_list_t *fs = type->fields_or_args;
2085 const var_t *f;
2087 type->ptrdesc = *tfsoff;
2088 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
2090 type_t *ft = f->type;
2091 if (is_ptr(ft))
2093 if (is_string_type(f->attrs, ft))
2094 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2095 else
2096 write_pointer_tfs(file, ft, tfsoff);
2098 else if (!ft->declarray && is_conformant_array(ft))
2100 unsigned int absoff = ft->typestring_offset;
2101 short reloff = absoff - (*tfsoff + 2);
2102 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2103 /* FIXME: We need to store pointer attributes for arrays
2104 so we don't lose pointer_default info. */
2105 if (ptr_type == 0)
2106 ptr_type = RPC_FC_UP;
2107 print_file(file, 0, "/* %d */\n", *tfsoff);
2108 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2109 string_of_type(ptr_type));
2110 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2111 reloff, reloff, absoff);
2112 *tfsoff += 4;
2115 if (type->ptrdesc == *tfsoff)
2116 type->ptrdesc = 0;
2119 current_structure = save_current_structure;
2120 return start_offset;
2123 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2124 unsigned char flags, size_t offset,
2125 unsigned int *typeformat_offset)
2127 size_t start_offset = *typeformat_offset;
2128 short reloff = offset - (*typeformat_offset + 2);
2129 int in_attr, out_attr;
2130 in_attr = is_attr(attrs, ATTR_IN);
2131 out_attr = is_attr(attrs, ATTR_OUT);
2132 if (!in_attr && !out_attr) in_attr = 1;
2134 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2135 flags |= 0x04;
2137 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2138 pointer_type,
2139 flags,
2140 string_of_type(pointer_type));
2141 if (file)
2143 if (flags & 0x04)
2144 fprintf(file, " [allocated_on_stack]");
2145 if (flags & 0x10)
2146 fprintf(file, " [pointer_deref]");
2147 fprintf(file, " */\n");
2150 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
2151 *typeformat_offset += 4;
2153 return start_offset;
2156 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2158 if (t == NULL)
2160 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2162 else if (is_base_type(t->type))
2164 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2165 t->type, string_of_type(t->type));
2167 else if (t->typestring_offset)
2169 short reloff = t->typestring_offset - *tfsoff;
2170 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
2171 reloff, reloff, t->typestring_offset);
2173 else
2174 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
2176 *tfsoff += 2;
2179 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2181 unsigned int align = 0;
2182 unsigned int start_offset;
2183 size_t size = type_memsize(type, &align);
2184 var_list_t *fields;
2185 size_t nbranch = 0;
2186 type_t *deftype = NULL;
2187 short nodeftype = 0xffff;
2188 var_t *f;
2190 guard_rec(type);
2192 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2194 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
2195 fields = uv->type->fields_or_args;
2197 else
2198 fields = type->fields_or_args;
2200 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2202 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2203 if (cases)
2204 nbranch += list_count(cases);
2205 if (f->type)
2206 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2209 start_offset = *tfsoff;
2210 update_tfsoff(type, start_offset, file);
2211 print_start_tfs_comment(file, type, start_offset);
2212 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2214 const var_t *sv = LIST_ENTRY(list_head(type->fields_or_args), const var_t, entry);
2215 const type_t *st = sv->type;
2217 switch (st->type)
2219 case RPC_FC_CHAR:
2220 case RPC_FC_SMALL:
2221 case RPC_FC_USMALL:
2222 case RPC_FC_SHORT:
2223 case RPC_FC_USHORT:
2224 case RPC_FC_LONG:
2225 case RPC_FC_ULONG:
2226 case RPC_FC_ENUM16:
2227 case RPC_FC_ENUM32:
2228 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2229 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2230 0x40 | st->type, string_of_type(st->type));
2231 *tfsoff += 2;
2232 break;
2233 default:
2234 error("union switch type must be an integer, char, or enum\n");
2237 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2239 static const expr_t dummy_expr; /* FIXME */
2240 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2242 switch (st->type)
2244 case RPC_FC_CHAR:
2245 case RPC_FC_SMALL:
2246 case RPC_FC_USMALL:
2247 case RPC_FC_SHORT:
2248 case RPC_FC_USHORT:
2249 case RPC_FC_LONG:
2250 case RPC_FC_ULONG:
2251 case RPC_FC_ENUM16:
2252 case RPC_FC_ENUM32:
2253 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2254 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2255 st->type, string_of_type(st->type));
2256 *tfsoff += 2;
2257 break;
2258 default:
2259 error("union switch type must be an integer, char, or enum\n");
2262 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2265 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
2266 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
2267 *tfsoff += 4;
2269 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2271 type_t *ft = f->type;
2272 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2273 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2274 expr_t *c;
2276 if (cases == NULL && !deflt)
2277 error("union field %s with neither case nor default attribute\n", f->name);
2279 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2281 /* MIDL doesn't check for duplicate cases, even though that seems
2282 like a reasonable thing to do, it just dumps them to the TFS
2283 like we're going to do here. */
2284 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2285 *tfsoff += 4;
2286 write_branch_type(file, ft, tfsoff);
2289 /* MIDL allows multiple default branches, even though that seems
2290 illogical, it just chooses the last one, which is what we will
2291 do. */
2292 if (deflt)
2294 deftype = ft;
2295 nodeftype = 0;
2299 if (deftype)
2301 write_branch_type(file, deftype, tfsoff);
2303 else
2305 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2306 *tfsoff += 2;
2309 return start_offset;
2312 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2313 unsigned int *typeformat_offset)
2315 size_t i;
2316 size_t start_offset = *typeformat_offset;
2317 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2319 if (iid)
2321 print_file(file, 2, "0x2f, /* FC_IP */\n");
2322 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2323 *typeformat_offset
2324 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2326 else
2328 const type_t *base = is_ptr(type) ? type->ref : type;
2329 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2331 if (! uuid)
2332 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2334 update_tfsoff(type, start_offset, file);
2335 print_start_tfs_comment(file, type, start_offset);
2336 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2337 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2338 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2339 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2340 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2341 for (i = 0; i < 8; ++i)
2342 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2344 if (file)
2345 fprintf(file, "\n");
2347 *typeformat_offset += 18;
2349 return start_offset;
2352 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2353 const var_t *var,
2354 unsigned int *typeformat_offset)
2356 size_t start_offset = *typeformat_offset;
2357 unsigned char flags = 0;
2359 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2360 flags |= NDR_STRICT_CONTEXT_HANDLE;
2362 if (is_ptr(type))
2363 flags |= 0x80;
2364 if (is_attr(var->attrs, ATTR_IN))
2366 flags |= 0x40;
2367 if (!is_attr(var->attrs, ATTR_OUT))
2368 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2370 if (is_attr(var->attrs, ATTR_OUT))
2371 flags |= 0x20;
2373 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2374 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2375 /* return and can't be null values overlap */
2376 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2377 print_file(file, 0, "can't be null, ");
2378 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2379 print_file(file, 0, "serialize, ");
2380 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2381 print_file(file, 0, "no serialize, ");
2382 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2383 print_file(file, 0, "strict, ");
2384 if ((flags & 0x21) == 0x20)
2385 print_file(file, 0, "out, ");
2386 if ((flags & 0x21) == 0x21)
2387 print_file(file, 0, "return, ");
2388 if (flags & 0x40)
2389 print_file(file, 0, "in, ");
2390 if (flags & 0x80)
2391 print_file(file, 0, "via ptr, ");
2392 print_file(file, 0, "*/\n");
2393 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2394 print_file(file, 2, "0, /* FIXME: param num */\n");
2395 *typeformat_offset += 4;
2397 return start_offset;
2400 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2401 type_t *type, const var_t *var,
2402 unsigned int *typeformat_offset)
2404 size_t offset;
2406 if (is_context_handle(type))
2407 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2409 if (is_user_type(type))
2411 write_user_tfs(file, type, typeformat_offset);
2412 return type->typestring_offset;
2415 if (is_string_type(var->attrs, type))
2416 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2418 if (is_array(type))
2420 int ptr_type;
2421 size_t off;
2422 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2423 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2424 /* Top level pointers to conformant arrays may be handled specially
2425 since we can bypass the pointer, but if the array is buried
2426 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2427 always need to write the pointer. */
2428 if (!ptr_type && var->type != type)
2429 /* FIXME: This should use pointer_default, but the information
2430 isn't kept around for arrays. */
2431 ptr_type = RPC_FC_UP;
2432 if (ptr_type && ptr_type != RPC_FC_RP)
2434 unsigned int absoff = type->typestring_offset;
2435 short reloff = absoff - (*typeformat_offset + 2);
2436 off = *typeformat_offset;
2437 print_file(file, 0, "/* %d */\n", off);
2438 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2439 string_of_type(ptr_type));
2440 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2441 reloff, reloff, absoff);
2442 *typeformat_offset += 4;
2444 return off;
2447 if (!is_ptr(type))
2449 /* basic types don't need a type format string */
2450 if (is_base_type(type->type))
2451 return 0;
2453 if (processed(type)) return type->typestring_offset;
2455 switch (type->type)
2457 case RPC_FC_STRUCT:
2458 case RPC_FC_PSTRUCT:
2459 case RPC_FC_CSTRUCT:
2460 case RPC_FC_CPSTRUCT:
2461 case RPC_FC_CVSTRUCT:
2462 case RPC_FC_BOGUS_STRUCT:
2463 return write_struct_tfs(file, type, var->name, typeformat_offset);
2464 case RPC_FC_ENCAPSULATED_UNION:
2465 case RPC_FC_NON_ENCAPSULATED_UNION:
2466 return write_union_tfs(file, type, typeformat_offset);
2467 case RPC_FC_IGNORE:
2468 case RPC_FC_BIND_PRIMITIVE:
2469 /* nothing to do */
2470 return 0;
2471 default:
2472 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2475 else if (last_ptr(type))
2477 size_t start_offset = *typeformat_offset;
2478 int in_attr = is_attr(var->attrs, ATTR_IN);
2479 int out_attr = is_attr(var->attrs, ATTR_OUT);
2480 const type_t *base = type->ref;
2482 if (base->type == RPC_FC_IP
2483 || (base->type == 0
2484 && is_attr(var->attrs, ATTR_IIDIS)))
2486 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2489 /* special case for pointers to base types */
2490 if (is_base_type(base->type))
2492 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2493 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2494 string_of_type(type->type),
2495 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2496 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2497 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2498 *typeformat_offset += 4;
2499 return start_offset;
2503 assert(is_ptr(type));
2505 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2506 if (file)
2507 fprintf(file, "/* %2u */\n", *typeformat_offset);
2508 return write_pointer_only_tfs(file, var->attrs, type->type,
2509 !last_ptr(type) ? 0x10 : 0,
2510 offset, typeformat_offset);
2513 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2514 const char *name, int write_ptr, unsigned int *tfsoff)
2516 int retmask = 0;
2518 if (is_user_type(type))
2520 write_user_tfs(file, type, tfsoff);
2522 else if (is_string_type(attrs, type))
2524 write_string_tfs(file, attrs, type, name, tfsoff);
2526 else if (is_ptr(type))
2528 type_t *ref = type->ref;
2530 if (ref->type == RPC_FC_IP
2531 || (ref->type == 0
2532 && is_attr(attrs, ATTR_IIDIS)))
2534 write_ip_tfs(file, attrs, type, tfsoff);
2536 else
2538 if (!processed(ref) && !is_base_type(ref->type))
2539 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2541 if (write_ptr)
2542 write_pointer_tfs(file, type, tfsoff);
2544 retmask |= 1;
2547 else if (type->declarray && is_conformant_array(type))
2548 ; /* conformant arrays and strings are handled specially */
2549 else if (is_array(type))
2551 write_array_tfs(file, attrs, type, name, tfsoff);
2552 if (is_conformant_array(type))
2553 retmask |= 1;
2555 else if (is_struct(type->type))
2557 if (!processed(type))
2558 write_struct_tfs(file, type, name, tfsoff);
2560 else if (is_union(type->type))
2562 if (!processed(type))
2563 write_union_tfs(file, type, tfsoff);
2565 else if (!is_base_type(type->type))
2566 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2567 name, type->type);
2569 return retmask;
2572 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2573 type_pred_t pred, unsigned int *typeformat_offset)
2575 const var_t *var;
2576 const statement_t *stmt;
2578 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2580 const type_t *iface;
2581 if (stmt->type == STMT_LIBRARY)
2583 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2584 continue;
2586 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2587 continue;
2589 iface = stmt->u.type;
2590 if (!pred(iface))
2591 continue;
2593 if (iface->funcs)
2595 const func_t *func;
2596 current_iface = iface;
2597 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2599 if (is_local(func->def->attrs)) continue;
2601 if (!is_void(get_func_return_type(func)))
2603 var_t v = *func->def;
2604 v.type = get_func_return_type(func);
2605 update_tfsoff(get_func_return_type(func),
2606 write_typeformatstring_var(
2607 file, 2, NULL, get_func_return_type(func),
2608 &v, typeformat_offset),
2609 file);
2612 current_func = func;
2613 if (func->args)
2614 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2615 update_tfsoff(
2616 var->type,
2617 write_typeformatstring_var(
2618 file, 2, func, var->type, var,
2619 typeformat_offset),
2620 file);
2625 return *typeformat_offset + 1;
2628 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2630 unsigned int typeformat_offset = 2;
2632 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2636 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2638 int indent = 0;
2640 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2641 print_file(file, indent, "{\n");
2642 indent++;
2643 print_file(file, indent, "0,\n");
2644 print_file(file, indent, "{\n");
2645 indent++;
2646 print_file(file, indent, "NdrFcShort(0x0),\n");
2648 set_all_tfswrite(TRUE);
2649 process_tfs(file, stmts, pred);
2651 print_file(file, indent, "0x0\n");
2652 indent--;
2653 print_file(file, indent, "}\n");
2654 indent--;
2655 print_file(file, indent, "};\n");
2656 print_file(file, indent, "\n");
2659 static unsigned int get_required_buffer_size_type(
2660 const type_t *type, const char *name, unsigned int *alignment)
2662 const char *uname;
2663 const type_t *utype;
2665 *alignment = 0;
2666 if ((utype = get_user_type(type, &uname)))
2668 return get_required_buffer_size_type(utype, uname, alignment);
2670 else
2672 switch (get_struct_type(type))
2674 case RPC_FC_BYTE:
2675 case RPC_FC_CHAR:
2676 case RPC_FC_USMALL:
2677 case RPC_FC_SMALL:
2678 *alignment = 4;
2679 return 1;
2681 case RPC_FC_WCHAR:
2682 case RPC_FC_USHORT:
2683 case RPC_FC_SHORT:
2684 case RPC_FC_ENUM16:
2685 *alignment = 4;
2686 return 2;
2688 case RPC_FC_ULONG:
2689 case RPC_FC_LONG:
2690 case RPC_FC_ENUM32:
2691 case RPC_FC_FLOAT:
2692 case RPC_FC_ERROR_STATUS_T:
2693 *alignment = 4;
2694 return 4;
2696 case RPC_FC_HYPER:
2697 case RPC_FC_DOUBLE:
2698 *alignment = 8;
2699 return 8;
2701 case RPC_FC_IGNORE:
2702 case RPC_FC_BIND_PRIMITIVE:
2703 return 0;
2705 case RPC_FC_STRUCT:
2706 if (!type->fields_or_args) return 0;
2707 return fields_memsize(type->fields_or_args, alignment);
2709 case RPC_FC_RP:
2710 return
2711 is_base_type( type->ref->type ) || get_struct_type(type->ref) == RPC_FC_STRUCT
2712 ? get_required_buffer_size_type( type->ref, name, alignment )
2713 : 0;
2715 case RPC_FC_SMFARRAY:
2716 case RPC_FC_LGFARRAY:
2717 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2719 default:
2720 return 0;
2725 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2727 int in_attr = is_attr(var->attrs, ATTR_IN);
2728 int out_attr = is_attr(var->attrs, ATTR_OUT);
2730 if (!in_attr && !out_attr)
2731 in_attr = 1;
2733 *alignment = 0;
2735 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2736 pass == PASS_RETURN)
2738 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2740 *alignment = 4;
2741 return 20;
2744 if (!is_string_type(var->attrs, var->type))
2745 return get_required_buffer_size_type(var->type, var->name,
2746 alignment);
2748 return 0;
2751 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2753 const var_t *var;
2754 unsigned int total_size = 0, alignment;
2756 if (func->args)
2758 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2760 total_size += get_required_buffer_size(var, &alignment, pass);
2761 total_size += alignment;
2765 if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2767 var_t v = *func->def;
2768 v.type = get_func_return_type(func);
2769 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2770 total_size += alignment;
2772 return total_size;
2775 static void print_phase_function(FILE *file, int indent, const char *type,
2776 const char *local_var_prefix, enum remoting_phase phase,
2777 const var_t *var, unsigned int type_offset)
2779 const char *function;
2780 switch (phase)
2782 case PHASE_BUFFERSIZE:
2783 function = "BufferSize";
2784 break;
2785 case PHASE_MARSHAL:
2786 function = "Marshall";
2787 break;
2788 case PHASE_UNMARSHAL:
2789 function = "Unmarshall";
2790 break;
2791 case PHASE_FREE:
2792 function = "Free";
2793 break;
2794 default:
2795 assert(0);
2796 return;
2799 print_file(file, indent, "Ndr%s%s(\n", type, function);
2800 indent++;
2801 print_file(file, indent, "&__frame->_StubMsg,\n");
2802 print_file(file, indent, "%s%s%s%s%s,\n",
2803 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2804 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2805 local_var_prefix,
2806 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2807 var->name);
2808 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2809 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2810 if (phase == PHASE_UNMARSHAL)
2811 print_file(file, indent, "0);\n");
2812 indent--;
2815 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2816 enum remoting_phase phase, enum pass pass, const var_t *var,
2817 const char *varname)
2819 type_t *type = var->type;
2820 unsigned int size;
2821 unsigned int alignment = 0;
2822 unsigned char rtype;
2824 /* no work to do for other phases, buffer sizing is done elsewhere */
2825 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2826 return;
2828 rtype = is_ptr(type) ? type->ref->type : type->type;
2830 switch (rtype)
2832 case RPC_FC_BYTE:
2833 case RPC_FC_CHAR:
2834 case RPC_FC_SMALL:
2835 case RPC_FC_USMALL:
2836 size = 1;
2837 alignment = 1;
2838 break;
2840 case RPC_FC_WCHAR:
2841 case RPC_FC_USHORT:
2842 case RPC_FC_SHORT:
2843 case RPC_FC_ENUM16:
2844 size = 2;
2845 alignment = 2;
2846 break;
2848 case RPC_FC_ULONG:
2849 case RPC_FC_LONG:
2850 case RPC_FC_ENUM32:
2851 case RPC_FC_FLOAT:
2852 case RPC_FC_ERROR_STATUS_T:
2853 size = 4;
2854 alignment = 4;
2855 break;
2857 case RPC_FC_HYPER:
2858 case RPC_FC_DOUBLE:
2859 size = 8;
2860 alignment = 8;
2861 break;
2863 case RPC_FC_IGNORE:
2864 case RPC_FC_BIND_PRIMITIVE:
2865 /* no marshalling needed */
2866 return;
2868 default:
2869 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2870 size = 0;
2873 if (phase == PHASE_MARSHAL)
2874 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2875 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2876 alignment - 1, alignment - 1);
2878 if (phase == PHASE_MARSHAL)
2880 print_file(file, indent, "*(");
2881 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2882 if (is_ptr(type))
2883 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2884 else
2885 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2886 fprintf(file, "%s%s", local_var_prefix, varname);
2887 fprintf(file, ";\n");
2889 else if (phase == PHASE_UNMARSHAL)
2891 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2892 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2893 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2894 print_file(file, indent, "{\n");
2895 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2896 print_file(file, indent, "}\n");
2897 if (pass == PASS_IN || pass == PASS_RETURN)
2898 print_file(file, indent, "");
2899 else
2900 print_file(file, indent, "*");
2901 fprintf(file, "%s%s", local_var_prefix, varname);
2902 if (pass == PASS_IN && is_ptr(type))
2903 fprintf(file, " = (");
2904 else
2905 fprintf(file, " = *(");
2906 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2907 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2910 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2911 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2912 fprintf(file, ");\n");
2915 /* returns whether the MaxCount, Offset or ActualCount members need to be
2916 * filled in for the specified phase */
2917 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2919 return (phase != PHASE_UNMARSHAL);
2922 expr_t *get_size_is_expr(const type_t *t, const char *name)
2924 expr_t *x = NULL;
2926 for ( ; is_ptr(t) || is_array(t); t = t->ref)
2927 if (t->size_is)
2929 if (!x)
2930 x = t->size_is;
2931 else
2932 error("%s: multidimensional conformant"
2933 " arrays not supported at the top level\n",
2934 name);
2937 return x;
2940 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
2941 enum remoting_phase phase, const var_t *var)
2943 const type_t *type = var->type;
2944 /* get fundamental type for the argument */
2945 for (;;)
2947 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2948 break;
2949 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2950 break;
2951 else if (is_array(type) || is_string_type(var->attrs, type))
2953 if (is_conformance_needed_for_phase(phase))
2955 if (type->size_is)
2957 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2958 write_expr(file, type->size_is, 1, 1, NULL, NULL, local_var_prefix);
2959 fprintf(file, ";\n\n");
2961 if (type->length_is)
2963 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
2964 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
2965 write_expr(file, type->length_is, 1, 1, NULL, NULL, local_var_prefix);
2966 fprintf(file, ";\n\n");
2969 break;
2971 else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
2973 if (is_conformance_needed_for_phase(phase))
2975 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2976 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
2977 fprintf(file, ";\n\n");
2979 break;
2981 else if (type->type == RPC_FC_IP)
2983 expr_t *iid;
2985 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
2987 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
2988 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
2989 fprintf( file, ";\n\n" );
2991 break;
2993 else if (is_ptr(type))
2994 type = type->ref;
2995 else
2996 break;
3000 static void write_remoting_arg(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3001 enum pass pass, enum remoting_phase phase, const var_t *var)
3003 int in_attr, out_attr, pointer_type;
3004 const type_t *type = var->type;
3005 unsigned char rtype;
3006 size_t start_offset = type->typestring_offset;
3008 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3009 if (!pointer_type)
3010 pointer_type = RPC_FC_RP;
3012 in_attr = is_attr(var->attrs, ATTR_IN);
3013 out_attr = is_attr(var->attrs, ATTR_OUT);
3014 if (!in_attr && !out_attr)
3015 in_attr = 1;
3017 if (phase != PHASE_FREE)
3018 switch (pass)
3020 case PASS_IN:
3021 if (!in_attr) return;
3022 break;
3023 case PASS_OUT:
3024 if (!out_attr) return;
3025 break;
3026 case PASS_RETURN:
3027 break;
3030 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3031 rtype = get_struct_type(type);
3033 if (is_context_handle(type))
3035 if (phase == PHASE_MARSHAL)
3037 if (pass == PASS_IN)
3039 /* if the context_handle attribute appears in the chain of types
3040 * without pointers being followed, then the context handle must
3041 * be direct, otherwise it is a pointer */
3042 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3043 print_file(file, indent, "NdrClientContextMarshall(\n");
3044 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3045 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3046 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3048 else
3050 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3051 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3052 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3053 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3054 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3057 else if (phase == PHASE_UNMARSHAL)
3059 if (pass == PASS_OUT)
3061 if (!in_attr)
3062 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3063 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3064 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3065 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3066 print_file(file, indent + 1, "__frame->_Handle);\n");
3068 else
3070 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3071 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3072 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3076 else if (is_user_type(var->type))
3078 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3080 else if (is_string_type(var->attrs, var->type))
3082 if (is_array(type) && !is_conformant_array(type))
3083 print_phase_function(file, indent, "NonConformantString", local_var_prefix,
3084 phase, var, start_offset);
3085 else
3087 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
3088 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
3089 start_offset - (type->size_is ? 4 : 2));
3090 else
3091 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3092 phase, var, start_offset);
3095 else if (is_array(type))
3097 unsigned char tc = get_array_type( type );
3098 const char *array_type = "FixedArray";
3100 /* We already have the size_is expression since it's at the
3101 top level, but do checks for multidimensional conformant
3102 arrays. When we handle them, we'll need to extend this
3103 function to return a list, and then we'll actually use
3104 the return value. */
3105 get_size_is_expr(type, var->name);
3107 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3109 array_type = "VaryingArray";
3111 else if (tc == RPC_FC_CARRAY)
3113 array_type = "ConformantArray";
3115 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3117 array_type = (tc == RPC_FC_BOGUS_ARRAY
3118 ? "ComplexArray"
3119 : "ConformantVaryingArray");
3122 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3123 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3124 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3126 /* these are all unmarshalled by allocating memory */
3127 if (tc == RPC_FC_BOGUS_ARRAY ||
3128 tc == RPC_FC_CVARRAY ||
3129 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3130 (tc == RPC_FC_CARRAY && !in_attr))
3132 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3133 indent++;
3134 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3138 else if (!is_ptr(var->type) && is_base_type(rtype))
3140 if (phase != PHASE_FREE)
3141 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3143 else if (!is_ptr(var->type))
3145 switch (rtype)
3147 case RPC_FC_STRUCT:
3148 case RPC_FC_PSTRUCT:
3149 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3150 break;
3151 case RPC_FC_CSTRUCT:
3152 case RPC_FC_CPSTRUCT:
3153 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3154 break;
3155 case RPC_FC_CVSTRUCT:
3156 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3157 break;
3158 case RPC_FC_BOGUS_STRUCT:
3159 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3160 break;
3161 default:
3162 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
3165 else
3167 const type_t *ref = type->ref;
3168 if (type->type == RPC_FC_RP && is_base_type(ref->type))
3170 if (phase != PHASE_FREE)
3171 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3173 else if (type->type == RPC_FC_RP && get_struct_type(ref) == RPC_FC_STRUCT &&
3174 !is_user_type(ref))
3176 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3177 print_phase_function(file, indent, "SimpleStruct",
3178 local_var_prefix, phase, var,
3179 ref->typestring_offset);
3181 else
3183 if (ref->type == RPC_FC_IP)
3184 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3185 else
3186 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3189 fprintf(file, "\n");
3192 void write_remoting_arguments(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3193 enum pass pass, enum remoting_phase phase)
3195 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3197 unsigned int size = get_function_buffer_size( func, pass );
3198 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3201 if (pass == PASS_RETURN)
3203 var_t var;
3204 var = *func->def;
3205 var.type = get_func_return_type(func);
3206 var.name = xstrdup( "_RetVal" );
3207 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3208 free( var.name );
3210 else
3212 const var_t *var;
3213 if (!func->args)
3214 return;
3215 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3216 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3221 size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3223 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3227 size_t get_size_procformatstring_func(const func_t *func)
3229 const var_t *var;
3230 size_t size = 0;
3232 /* argument list size */
3233 if (func->args)
3234 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3235 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3237 /* return value size */
3238 if (is_void(get_func_return_type(func)))
3239 size += 2; /* FC_END and FC_PAD */
3240 else
3241 size += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
3243 return size;
3246 size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3248 const statement_t *stmt;
3249 size_t size = 1;
3250 const func_t *func;
3252 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3254 const type_t *iface;
3255 if (stmt->type == STMT_LIBRARY)
3257 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3258 continue;
3260 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3261 continue;
3263 iface = stmt->u.type;
3264 if (!pred(iface))
3265 continue;
3267 if (iface->funcs)
3268 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
3269 if (!is_local(func->def->attrs))
3270 size += get_size_procformatstring_func( func );
3272 return size;
3275 size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3277 set_all_tfswrite(FALSE);
3278 return process_tfs(NULL, stmts, pred);
3281 void declare_stub_args( FILE *file, int indent, const func_t *func )
3283 int in_attr, out_attr;
3284 int i = 0;
3285 const var_t *var;
3287 /* declare return value '_RetVal' */
3288 if (!is_void(get_func_return_type(func)))
3290 print_file(file, indent, "");
3291 write_type_decl_left(file, get_func_return_type(func));
3292 fprintf(file, " _RetVal;\n");
3295 if (!func->args)
3296 return;
3298 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3300 int is_string = is_attr(var->attrs, ATTR_STRING);
3302 in_attr = is_attr(var->attrs, ATTR_IN);
3303 out_attr = is_attr(var->attrs, ATTR_OUT);
3304 if (!out_attr && !in_attr)
3305 in_attr = 1;
3307 if (is_context_handle(var->type))
3308 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3309 else
3311 if (!in_attr && !var->type->size_is && !is_string)
3313 print_file(file, indent, "");
3314 write_type_decl(file, var->type->declarray ? var->type : var->type->ref,
3315 "_W%u", i++);
3316 fprintf(file, ";\n");
3319 print_file(file, indent, "");
3320 write_type_decl_left(file, var->type);
3321 fprintf(file, " ");
3322 if (var->type->declarray) {
3323 fprintf(file, "(*%s)", var->name);
3324 } else
3325 fprintf(file, "%s", var->name);
3326 write_type_right(file, var->type, FALSE);
3327 fprintf(file, ";\n");
3329 if (decl_indirect(var->type))
3330 print_file(file, indent, "void *_p_%s;\n", var->name);
3336 void assign_stub_out_args( FILE *file, int indent, const func_t *func, const char *local_var_prefix )
3338 int in_attr, out_attr;
3339 int i = 0, sep = 0;
3340 const var_t *var;
3342 if (!func->args)
3343 return;
3345 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3347 int is_string = is_attr(var->attrs, ATTR_STRING);
3348 in_attr = is_attr(var->attrs, ATTR_IN);
3349 out_attr = is_attr(var->attrs, ATTR_OUT);
3350 if (!out_attr && !in_attr)
3351 in_attr = 1;
3353 if (!in_attr)
3355 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3357 if (is_context_handle(var->type))
3359 fprintf(file, " = NdrContextHandleInitialize(\n");
3360 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3361 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3362 var->type->typestring_offset);
3364 else if (var->type->size_is)
3366 unsigned int size, align = 0;
3367 type_t *type = var->type;
3369 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3370 for ( ; type->size_is ; type = type->ref)
3372 write_expr(file, type->size_is, TRUE, TRUE, NULL, NULL, local_var_prefix);
3373 fprintf(file, " * ");
3375 size = type_memsize(type, &align);
3376 fprintf(file, "%u);\n", size);
3378 else if (!is_string)
3380 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3381 if (is_ptr(var->type) && !last_ptr(var->type))
3382 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3383 i++;
3386 sep = 1;
3389 if (sep)
3390 fprintf(file, "\n");
3394 int write_expr_eval_routines(FILE *file, const char *iface)
3396 static const char *var_name = "pS";
3397 static const char *var_name_expr = "pS->";
3398 int result = 0;
3399 struct expr_eval_routine *eval;
3400 unsigned short callback_offset = 0;
3402 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3404 const char *name = eval->structure->name;
3405 result = 1;
3407 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3408 iface, name, callback_offset);
3409 print_file(file, 0, "{\n");
3410 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3411 name, var_name, name, eval->baseoff);
3412 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3413 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3414 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3415 fprintf(file, ";\n");
3416 print_file(file, 0, "}\n\n");
3417 callback_offset++;
3419 return result;
3422 void write_expr_eval_routine_list(FILE *file, const char *iface)
3424 struct expr_eval_routine *eval;
3425 struct expr_eval_routine *cursor;
3426 unsigned short callback_offset = 0;
3428 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3429 fprintf(file, "{\n");
3431 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3433 const char *name = eval->structure->name;
3434 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3435 callback_offset++;
3436 list_remove(&eval->entry);
3437 free(eval);
3440 fprintf(file, "};\n\n");
3443 void write_user_quad_list(FILE *file)
3445 user_type_t *ut;
3447 if (list_empty(&user_type_list))
3448 return;
3450 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3451 fprintf(file, "{\n");
3452 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3454 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3455 print_file(file, 1, "{\n");
3456 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3457 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3458 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3459 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3460 print_file(file, 1, "}%s\n", sep);
3462 fprintf(file, "};\n\n");
3465 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3467 const struct str_list_entry_t *endpoint;
3468 const char *p;
3470 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3471 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3472 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3474 print_file( f, 1, "{ (const unsigned char *)\"" );
3475 for (p = endpoint->str; *p && *p != ':'; p++)
3477 if (*p == '"' || *p == '\\') fputc( '\\', f );
3478 fputc( *p, f );
3480 if (!*p) goto error;
3481 if (p[1] != '[') goto error;
3483 fprintf( f, "\", (const unsigned char *)\"" );
3484 for (p += 2; *p && *p != ']'; p++)
3486 if (*p == '"' || *p == '\\') fputc( '\\', f );
3487 fputc( *p, f );
3489 if (*p != ']') goto error;
3490 fprintf( f, "\" },\n" );
3492 print_file( f, 0, "};\n\n" );
3493 return;
3495 error:
3496 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3499 void write_exceptions( FILE *file )
3501 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3502 fprintf( file, "\n");
3503 fprintf( file, "#include \"wine/exception.h\"\n");
3504 fprintf( file, "#undef RpcTryExcept\n");
3505 fprintf( file, "#undef RpcExcept\n");
3506 fprintf( file, "#undef RpcEndExcept\n");
3507 fprintf( file, "#undef RpcTryFinally\n");
3508 fprintf( file, "#undef RpcFinally\n");
3509 fprintf( file, "#undef RpcEndFinally\n");
3510 fprintf( file, "#undef RpcExceptionCode\n");
3511 fprintf( file, "#undef RpcAbnormalTermination\n");
3512 fprintf( file, "\n");
3513 fprintf( file, "struct __exception_frame;\n");
3514 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3515 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3516 fprintf( file, "\n");
3517 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3518 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3519 fprintf( file, " __filter_func filter; \\\n");
3520 fprintf( file, " __finally_func finally; \\\n");
3521 fprintf( file, " sigjmp_buf jmp; \\\n");
3522 fprintf( file, " DWORD code; \\\n");
3523 fprintf( file, " unsigned char abnormal_termination; \\\n");
3524 fprintf( file, " unsigned char filter_level; \\\n");
3525 fprintf( file, " unsigned char finally_level;\n");
3526 fprintf( file, "\n");
3527 fprintf( file, "struct __exception_frame\n{\n");
3528 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3529 fprintf( file, "};\n");
3530 fprintf( file, "\n");
3531 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3532 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3533 fprintf( file, " CONTEXT *context,\n");
3534 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3535 fprintf( file, "{\n");
3536 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3537 fprintf( file, "\n");
3538 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3539 fprintf( file, " {\n" );
3540 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3541 fprintf( file, " {\n" );
3542 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3543 fprintf( file, " exc_frame->finally( exc_frame );\n");
3544 fprintf( file, " }\n" );
3545 fprintf( file, " return ExceptionContinueSearch;\n");
3546 fprintf( file, " }\n" );
3547 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3548 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3549 fprintf( file, " {\n");
3550 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3551 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3552 fprintf( file, " {\n");
3553 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3554 fprintf( file, " exc_frame->finally( exc_frame );\n");
3555 fprintf( file, " __wine_pop_frame( frame );\n");
3556 fprintf( file, " }\n");
3557 fprintf( file, " exc_frame->filter_level = 0;\n");
3558 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3559 fprintf( file, " }\n");
3560 fprintf( file, " return ExceptionContinueSearch;\n");
3561 fprintf( file, "}\n");
3562 fprintf( file, "\n");
3563 fprintf( file, "#define RpcTryExcept \\\n");
3564 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3565 fprintf( file, " { \\\n");
3566 fprintf( file, " if (!__frame->finally_level) \\\n" );
3567 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3568 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3569 fprintf( file, "\n");
3570 fprintf( file, "#define RpcExcept(expr) \\\n");
3571 fprintf( file, " if (!__frame->finally_level) \\\n" );
3572 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3573 fprintf( file, " __frame->filter_level = 0; \\\n" );
3574 fprintf( file, " } \\\n");
3575 fprintf( file, " else \\\n");
3576 fprintf( file, "\n");
3577 fprintf( file, "#define RpcEndExcept\n");
3578 fprintf( file, "\n");
3579 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3580 fprintf( file, "\n");
3581 fprintf( file, "#define RpcTryFinally \\\n");
3582 fprintf( file, " if (!__frame->filter_level) \\\n");
3583 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3584 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3585 fprintf( file, "\n");
3586 fprintf( file, "#define RpcFinally \\\n");
3587 fprintf( file, " if (!__frame->filter_level) \\\n");
3588 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3589 fprintf( file, " __frame->finally_level = 0;\n");
3590 fprintf( file, "\n");
3591 fprintf( file, "#define RpcEndFinally\n");
3592 fprintf( file, "\n");
3593 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3594 fprintf( file, "\n");
3595 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3596 fprintf( file, " do { \\\n");
3597 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3598 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3599 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3600 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3601 fprintf( file, " __frame->filter_level = 0; \\\n");
3602 fprintf( file, " __frame->finally_level = 0; \\\n");
3603 fprintf( file, " } while (0)\n");
3604 fprintf( file, "\n");
3605 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3606 fprintf( file, "\n");
3607 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3608 fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
3609 fprintf( file, "\n");
3610 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");