Release 1.1.14.
[wine/gsoc-2012-control.git] / tools / widl / typegen.c
blob599e2fe70d19921bc739358a7df8e5da77b7115b
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
62 static 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 unsigned char get_pointer_fc(const type_t *type)
125 assert(is_ptr(type));
126 /* FIXME: see corresponding hack in set_type - we shouldn't be getting
127 * the pointer type from an alias, rather determining it from the
128 * position */
129 return type->type;
132 static int get_struct_type(const type_t *type)
134 int has_pointer = 0;
135 int has_conformance = 0;
136 int has_variance = 0;
137 var_t *field;
138 var_list_t *fields;
140 if (type->type != RPC_FC_STRUCT) return type->type;
142 fields = type_struct_get_fields(type);
144 if (get_padding(fields))
145 return RPC_FC_BOGUS_STRUCT;
147 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
149 type_t *t = field->type;
151 if (is_user_type(t))
152 return RPC_FC_BOGUS_STRUCT;
154 if (field->type->declarray)
156 if (is_string_type(field->attrs, field->type))
158 if (is_conformant_array(field->type))
159 has_conformance = 1;
160 has_variance = 1;
161 continue;
164 if (is_array(type_array_get_element(field->type)))
165 return RPC_FC_BOGUS_STRUCT;
167 if (type_array_has_conformance(field->type))
169 has_conformance = 1;
170 if (field->type->declarray && list_next(fields, &field->entry))
171 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
172 field->name);
174 if (type_array_has_variance(field->type))
175 has_variance = 1;
177 t = type_array_get_element(field->type);
180 switch (get_struct_type(t))
183 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
184 * Simple types don't effect the type of struct.
185 * A struct containing a simple struct is still a simple struct.
186 * So long as we can block copy the data, we return RPC_FC_STRUCT.
188 case 0: /* void pointer */
189 case RPC_FC_BYTE:
190 case RPC_FC_CHAR:
191 case RPC_FC_SMALL:
192 case RPC_FC_USMALL:
193 case RPC_FC_WCHAR:
194 case RPC_FC_SHORT:
195 case RPC_FC_USHORT:
196 case RPC_FC_LONG:
197 case RPC_FC_ULONG:
198 case RPC_FC_INT3264:
199 case RPC_FC_UINT3264:
200 case RPC_FC_HYPER:
201 case RPC_FC_FLOAT:
202 case RPC_FC_DOUBLE:
203 case RPC_FC_STRUCT:
204 case RPC_FC_ENUM32:
205 break;
207 case RPC_FC_RP:
208 return RPC_FC_BOGUS_STRUCT;
210 case RPC_FC_UP:
211 case RPC_FC_FP:
212 case RPC_FC_OP:
213 if (pointer_size != 4)
214 return RPC_FC_BOGUS_STRUCT;
215 /* pointers to interfaces aren't really pointers and have to be
216 * marshalled specially so they make the structure complex */
217 if (type_pointer_get_ref(t)->type == RPC_FC_IP)
218 return RPC_FC_BOGUS_STRUCT;
219 has_pointer = 1;
220 break;
222 case RPC_FC_SMFARRAY:
223 case RPC_FC_LGFARRAY:
224 case RPC_FC_SMVARRAY:
225 case RPC_FC_LGVARRAY:
226 case RPC_FC_CARRAY:
227 case RPC_FC_CVARRAY:
228 case RPC_FC_BOGUS_ARRAY:
230 unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
231 if (!ptr_type || ptr_type == RPC_FC_RP)
232 return RPC_FC_BOGUS_STRUCT;
233 else if (pointer_size != 4)
234 return RPC_FC_BOGUS_STRUCT;
235 has_pointer = 1;
236 break;
240 * Propagate member attributes
241 * a struct should be at least as complex as its member
243 case RPC_FC_CVSTRUCT:
244 has_conformance = 1;
245 has_variance = 1;
246 has_pointer = 1;
247 break;
249 case RPC_FC_CPSTRUCT:
250 has_conformance = 1;
251 if (list_next( fields, &field->entry ))
252 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
253 field->name);
254 has_pointer = 1;
255 break;
257 case RPC_FC_CSTRUCT:
258 has_conformance = 1;
259 if (list_next( fields, &field->entry ))
260 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
261 field->name);
262 break;
264 case RPC_FC_PSTRUCT:
265 has_pointer = 1;
266 break;
268 default:
269 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
270 /* fallthru - treat it as complex */
272 /* as soon as we see one of these these members, it's bogus... */
273 case RPC_FC_ENCAPSULATED_UNION:
274 case RPC_FC_NON_ENCAPSULATED_UNION:
275 case RPC_FC_BOGUS_STRUCT:
276 case RPC_FC_ENUM16:
277 return RPC_FC_BOGUS_STRUCT;
281 if( has_variance )
283 if ( has_conformance )
284 return RPC_FC_CVSTRUCT;
285 else
286 return RPC_FC_BOGUS_STRUCT;
288 if( has_conformance && has_pointer )
289 return RPC_FC_CPSTRUCT;
290 if( has_conformance )
291 return RPC_FC_CSTRUCT;
292 if( has_pointer )
293 return RPC_FC_PSTRUCT;
294 return RPC_FC_STRUCT;
297 static unsigned char get_array_type(const type_t *type)
299 unsigned char fc;
300 const expr_t *size_is;
301 const type_t *elem_type;
303 if (!is_array(type))
304 return type->type;
306 elem_type = type_array_get_element(type);
307 size_is = type_array_get_conformance(type);
309 if (!size_is)
311 unsigned int align = 0;
312 size_t size = type_memsize(elem_type, &align);
313 if (size * type_array_get_dim(type) > 0xffffuL)
314 fc = RPC_FC_LGFARRAY;
315 else
316 fc = RPC_FC_SMFARRAY;
318 else
319 fc = RPC_FC_CARRAY;
321 if (type_array_has_variance(type))
323 if (fc == RPC_FC_SMFARRAY)
324 fc = RPC_FC_SMVARRAY;
325 else if (fc == RPC_FC_LGFARRAY)
326 fc = RPC_FC_LGVARRAY;
327 else if (fc == RPC_FC_CARRAY)
328 fc = RPC_FC_CVARRAY;
331 if (is_user_type(elem_type))
332 fc = RPC_FC_BOGUS_ARRAY;
333 else if (is_struct(elem_type->type))
335 switch (get_struct_type(elem_type))
337 case RPC_FC_BOGUS_STRUCT:
338 fc = RPC_FC_BOGUS_ARRAY;
339 break;
342 else if (elem_type->type == RPC_FC_ENUM16)
344 /* is 16-bit enum - if so, wire size differs from mem size and so
345 * the array cannot be block copied, which means the array is complex */
346 fc = RPC_FC_BOGUS_ARRAY;
348 else if (is_union(elem_type->type))
349 fc = RPC_FC_BOGUS_ARRAY;
350 else if (is_ptr(elem_type))
352 /* ref pointers cannot just be block copied. unique pointers to
353 * interfaces need special treatment. either case means the array is
354 * complex */
355 if (get_pointer_fc(elem_type) == RPC_FC_RP ||
356 type_pointer_get_ref(elem_type)->type == RPC_FC_IP)
357 fc = RPC_FC_BOGUS_ARRAY;
360 return fc;
363 int is_struct(unsigned char type)
365 switch (type)
367 case RPC_FC_STRUCT:
368 case RPC_FC_PSTRUCT:
369 case RPC_FC_CSTRUCT:
370 case RPC_FC_CPSTRUCT:
371 case RPC_FC_CVSTRUCT:
372 case RPC_FC_BOGUS_STRUCT:
373 return 1;
374 default:
375 return 0;
379 static int is_non_complex_struct(const type_t *type)
381 switch (get_struct_type(type))
383 case RPC_FC_STRUCT:
384 case RPC_FC_PSTRUCT:
385 case RPC_FC_CSTRUCT:
386 case RPC_FC_CPSTRUCT:
387 case RPC_FC_CVSTRUCT:
388 return 1;
389 default:
390 return 0;
394 int is_union(unsigned char type)
396 switch (type)
398 case RPC_FC_ENCAPSULATED_UNION:
399 case RPC_FC_NON_ENCAPSULATED_UNION:
400 return 1;
401 default:
402 return 0;
406 static int type_has_pointers(const type_t *type)
408 if (is_user_type(type))
409 return FALSE;
410 else if (is_ptr(type))
411 return TRUE;
412 else if (is_array(type))
413 return type_has_pointers(type_array_get_element(type));
414 else if (is_struct(type->type))
416 var_list_t *fields = type_struct_get_fields(type);
417 const var_t *field;
418 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
420 if (type_has_pointers(field->type))
421 return TRUE;
424 else if (is_union(type->type))
426 var_list_t *fields;
427 const var_t *field;
428 fields = type_union_get_cases(type);
429 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
431 if (field->type && type_has_pointers(field->type))
432 return TRUE;
436 return FALSE;
439 static int type_has_full_pointer(const type_t *type)
441 if (is_user_type(type))
442 return FALSE;
443 else if (type->type == RPC_FC_FP)
444 return TRUE;
445 else if (is_ptr(type))
446 return FALSE;
447 else if (is_array(type))
448 return type_has_full_pointer(type_array_get_element(type));
449 else if (is_struct(type->type))
451 var_list_t *fields = type_struct_get_fields(type);
452 const var_t *field;
453 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
455 if (type_has_full_pointer(field->type))
456 return TRUE;
459 else if (is_union(type->type))
461 var_list_t *fields;
462 const var_t *field;
463 fields = type_union_get_cases(type);
464 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
466 if (field->type && type_has_full_pointer(field->type))
467 return TRUE;
471 return FALSE;
474 static unsigned short user_type_offset(const char *name)
476 user_type_t *ut;
477 unsigned short off = 0;
478 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
480 if (strcmp(name, ut->name) == 0)
481 return off;
482 ++off;
484 error("user_type_offset: couldn't find type (%s)\n", name);
485 return 0;
488 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
490 type->typestring_offset = offset;
491 if (file) type->tfswrite = FALSE;
494 static void guard_rec(type_t *type)
496 /* types that contain references to themselves (like a linked list),
497 need to be shielded from infinite recursion when writing embedded
498 types */
499 if (type->typestring_offset)
500 type->tfswrite = FALSE;
501 else
502 type->typestring_offset = 1;
505 static type_t *get_user_type(const type_t *t, const char **pname)
507 for (;;)
509 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
510 if (ut)
512 if (pname)
513 *pname = t->name;
514 return ut;
517 if (type_is_alias(t))
518 t = type_alias_get_aliasee(t);
519 else
520 return 0;
524 int is_user_type(const type_t *t)
526 return get_user_type(t, NULL) != NULL;
529 static int is_embedded_complex(const type_t *type)
531 unsigned char tc = type->type;
532 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
533 || (is_ptr(type) && type_pointer_get_ref(type)->type == RPC_FC_IP);
536 static const char *get_context_handle_type_name(const type_t *type)
538 const type_t *t;
539 for (t = type; is_ptr(t); t = type_pointer_get_ref(t))
540 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
541 return t->name;
542 assert(0);
543 return NULL;
546 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
547 do { \
548 if (file) \
549 fprintf(file, "/* %2u */\n", typestring_offset); \
550 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
552 while (0)
554 static void print_file(FILE *file, int indent, const char *format, ...)
556 va_list va;
557 va_start(va, format);
558 print(file, indent, format, va);
559 va_end(va);
562 void print(FILE *file, int indent, const char *format, va_list va)
564 if (file)
566 if (format[0] != '\n')
567 while (0 < indent--)
568 fprintf(file, " ");
569 vfprintf(file, format, va);
574 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
576 if (decl_indirect(t))
578 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
579 local_var_prefix, n, local_var_prefix, n);
580 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
582 else if (is_ptr(t) || is_array(t))
583 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
586 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
588 const var_t *var;
590 if (!is_void(type_function_get_rettype(func->type)))
591 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
593 if (!type_get_function_args(func->type))
594 return;
596 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
597 write_var_init(file, indent, var->type, var->name, local_var_prefix);
599 fprintf(file, "\n");
602 static void write_formatdesc(FILE *f, int indent, const char *str)
604 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
605 print_file(f, indent, "{\n");
606 print_file(f, indent + 1, "short Pad;\n");
607 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
608 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
609 print_file(f, indent, "\n");
612 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
614 clear_all_offsets();
616 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
617 get_size_typeformatstring(stmts, pred));
619 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
620 get_size_procformatstring(stmts, pred));
622 fprintf(f, "\n");
623 write_formatdesc(f, indent, "TYPE");
624 write_formatdesc(f, indent, "PROC");
625 fprintf(f, "\n");
626 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
627 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
628 print_file(f, indent, "\n");
631 static inline int is_base_type(unsigned char type)
633 switch (type)
635 case RPC_FC_BYTE:
636 case RPC_FC_CHAR:
637 case RPC_FC_USMALL:
638 case RPC_FC_SMALL:
639 case RPC_FC_WCHAR:
640 case RPC_FC_USHORT:
641 case RPC_FC_SHORT:
642 case RPC_FC_ULONG:
643 case RPC_FC_LONG:
644 case RPC_FC_HYPER:
645 case RPC_FC_IGNORE:
646 case RPC_FC_FLOAT:
647 case RPC_FC_DOUBLE:
648 case RPC_FC_ENUM16:
649 case RPC_FC_ENUM32:
650 case RPC_FC_ERROR_STATUS_T:
651 case RPC_FC_BIND_PRIMITIVE:
652 return TRUE;
654 default:
655 return FALSE;
659 int decl_indirect(const type_t *t)
661 return is_user_type(t)
662 || (!is_base_type(t->type)
663 && !is_ptr(t)
664 && !is_array(t));
667 static size_t write_procformatstring_type(FILE *file, int indent,
668 const char *name,
669 const type_t *type,
670 const attr_list_t *attrs,
671 int is_return)
673 size_t size;
675 int is_in = is_attr(attrs, ATTR_IN);
676 int is_out = is_attr(attrs, ATTR_OUT);
678 if (!is_in && !is_out) is_in = TRUE;
680 if (!type->declarray && is_base_type(type->type))
682 if (is_return)
683 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
684 else
685 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
687 if (type->type == RPC_FC_BIND_PRIMITIVE)
689 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
690 size = 2; /* includes param type prefix */
692 else if (is_base_type(type->type))
694 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
695 size = 2; /* includes param type prefix */
697 else
699 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
700 size = 0;
703 else
705 if (is_return)
706 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
707 else if (is_in && is_out)
708 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
709 else if (is_out)
710 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
711 else
712 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
714 print_file(file, indent, "0x01,\n");
715 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
716 size = 4; /* includes param type prefix */
718 return size;
721 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
723 const statement_t *stmt;
724 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
726 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
728 const statement_t *stmt_func;
729 if (!pred(stmt->u.type))
730 continue;
731 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
733 const var_t *func = stmt_func->u.var;
734 if (is_local(func->attrs)) continue;
735 /* emit argument data */
736 if (type_get_function_args(func->type))
738 const var_t *var;
739 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
740 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
743 /* emit return value data */
744 if (is_void(type_function_get_rettype(func->type)))
746 print_file(file, indent, "0x5b, /* FC_END */\n");
747 print_file(file, indent, "0x5c, /* FC_PAD */\n");
749 else
750 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
753 else if (stmt->type == STMT_LIBRARY)
754 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
758 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
760 int indent = 0;
762 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
763 print_file(file, indent, "{\n");
764 indent++;
765 print_file(file, indent, "0,\n");
766 print_file(file, indent, "{\n");
767 indent++;
769 write_procformatstring_stmts(file, indent, stmts, pred);
771 print_file(file, indent, "0x0\n");
772 indent--;
773 print_file(file, indent, "}\n");
774 indent--;
775 print_file(file, indent, "};\n");
776 print_file(file, indent, "\n");
779 static int write_base_type(FILE *file, unsigned char fc, unsigned int *typestring_offset)
781 if (is_base_type(fc))
783 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
784 *typestring_offset += 1;
785 return 1;
788 return 0;
791 /* write conformance / variance descriptor */
792 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
793 unsigned int baseoff, const type_t *type,
794 const expr_t *expr)
796 unsigned char operator_type = 0;
797 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
798 const char *conftype_string = "";
799 const char *operator_string = "no operators";
800 const expr_t *subexpr;
802 if (!expr)
804 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
805 return 4;
808 if (!structure)
810 /* Top-level conformance calculations are done inline. */
811 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
812 RPC_FC_TOP_LEVEL_CONFORMANCE);
813 print_file (file, 2, "0x0,\n");
814 print_file (file, 2, "NdrFcShort(0x0),\n");
815 return 4;
818 if (expr->is_const)
820 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
821 error("write_conf_or_var_desc: constant value %ld is greater than "
822 "the maximum constant size of %d\n", expr->cval,
823 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
825 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
826 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
827 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
828 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
830 return 4;
833 if (is_ptr(type) || (is_array(type) && !type->declarray))
835 conftype = RPC_FC_POINTER_CONFORMANCE;
836 conftype_string = "field pointer, ";
839 subexpr = expr;
840 switch (subexpr->type)
842 case EXPR_PPTR:
843 subexpr = subexpr->ref;
844 operator_type = RPC_FC_DEREFERENCE;
845 operator_string = "FC_DEREFERENCE";
846 break;
847 case EXPR_DIV:
848 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
850 subexpr = subexpr->ref;
851 operator_type = RPC_FC_DIV_2;
852 operator_string = "FC_DIV_2";
854 break;
855 case EXPR_MUL:
856 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
858 subexpr = subexpr->ref;
859 operator_type = RPC_FC_MULT_2;
860 operator_string = "FC_MULT_2";
862 break;
863 case EXPR_SUB:
864 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
866 subexpr = subexpr->ref;
867 operator_type = RPC_FC_SUB_1;
868 operator_string = "FC_SUB_1";
870 break;
871 case EXPR_ADD:
872 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
874 subexpr = subexpr->ref;
875 operator_type = RPC_FC_ADD_1;
876 operator_string = "FC_ADD_1";
878 break;
879 default:
880 break;
883 if (subexpr->type == EXPR_IDENTIFIER)
885 const type_t *correlation_variable = NULL;
886 unsigned char correlation_variable_type;
887 unsigned char param_type = 0;
888 size_t offset = 0;
889 const var_t *var;
890 var_list_t *fields = type_struct_get_fields(structure);
892 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
894 unsigned int align = 0;
895 /* FIXME: take alignment into account */
896 if (var->name && !strcmp(var->name, subexpr->u.sval))
898 correlation_variable = var->type;
899 break;
901 offset += type_memsize(var->type, &align);
903 if (!correlation_variable)
904 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
905 subexpr->u.sval);
907 correlation_variable = expr_resolve_type(NULL, structure, expr);
909 offset -= baseoff;
910 correlation_variable_type = correlation_variable->type;
912 switch (correlation_variable_type)
914 case RPC_FC_CHAR:
915 case RPC_FC_SMALL:
916 param_type = RPC_FC_SMALL;
917 break;
918 case RPC_FC_BYTE:
919 case RPC_FC_USMALL:
920 param_type = RPC_FC_USMALL;
921 break;
922 case RPC_FC_WCHAR:
923 case RPC_FC_SHORT:
924 case RPC_FC_ENUM16:
925 param_type = RPC_FC_SHORT;
926 break;
927 case RPC_FC_USHORT:
928 param_type = RPC_FC_USHORT;
929 break;
930 case RPC_FC_LONG:
931 case RPC_FC_ENUM32:
932 param_type = RPC_FC_LONG;
933 break;
934 case RPC_FC_ULONG:
935 param_type = RPC_FC_ULONG;
936 break;
937 default:
938 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
939 correlation_variable_type);
942 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
943 conftype | param_type, conftype_string, string_of_type(param_type));
944 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
945 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
946 offset, offset);
948 else
950 unsigned int callback_offset = 0;
951 struct expr_eval_routine *eval;
952 int found = 0;
954 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
956 if (!strcmp (eval->structure->name, structure->name)
957 && !compare_expr (eval->expr, expr))
959 found = 1;
960 break;
962 callback_offset++;
965 if (!found)
967 eval = xmalloc (sizeof(*eval));
968 eval->structure = structure;
969 eval->baseoff = baseoff;
970 eval->expr = expr;
971 list_add_tail (&expr_eval_routines, &eval->entry);
974 if (callback_offset > USHRT_MAX)
975 error("Maximum number of callback routines reached\n");
977 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
978 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
979 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
981 return 4;
984 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
986 int have_align = FALSE;
987 size_t size = 0;
988 const var_t *v;
990 if (!fields) return 0;
991 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
993 unsigned int falign = 0;
994 size_t fsize = type_memsize(v->type, &falign);
995 if (!have_align)
997 *align = falign;
998 have_align = TRUE;
1000 size = ROUND_SIZE(size, falign);
1001 size += fsize;
1004 size = ROUND_SIZE(size, *align);
1005 return size;
1008 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1010 size_t size, maxs = 0;
1011 unsigned int align = *pmaxa;
1012 const var_t *v;
1014 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1016 /* we could have an empty default field with NULL type */
1017 if (v->type)
1019 size = type_memsize(v->type, &align);
1020 if (maxs < size) maxs = size;
1021 if (*pmaxa < align) *pmaxa = align;
1025 return maxs;
1028 int get_padding(const var_list_t *fields)
1030 unsigned short offset = 0;
1031 int salign = -1;
1032 const var_t *f;
1034 if (!fields)
1035 return 0;
1037 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1039 type_t *ft = f->type;
1040 unsigned int align = 0;
1041 size_t size = type_memsize(ft, &align);
1042 if (salign == -1)
1043 salign = align;
1044 offset = ROUND_SIZE(offset, align);
1045 offset += size;
1048 return ROUNDING(offset, salign);
1051 size_t type_memsize(const type_t *t, unsigned int *align)
1053 size_t size = 0;
1055 if (type_is_alias(t))
1056 size = type_memsize(type_alias_get_aliasee(t), align);
1057 else if (t->declarray && is_conformant_array(t))
1059 type_memsize(type_array_get_element(t), align);
1060 size = 0;
1062 else if (is_ptr(t) || is_conformant_array(t))
1064 assert( pointer_size );
1065 size = pointer_size;
1066 if (size > *align) *align = size;
1068 else switch (t->type)
1070 case RPC_FC_BYTE:
1071 case RPC_FC_CHAR:
1072 case RPC_FC_USMALL:
1073 case RPC_FC_SMALL:
1074 size = 1;
1075 if (size > *align) *align = size;
1076 break;
1077 case RPC_FC_WCHAR:
1078 case RPC_FC_USHORT:
1079 case RPC_FC_SHORT:
1080 case RPC_FC_ENUM16:
1081 size = 2;
1082 if (size > *align) *align = size;
1083 break;
1084 case RPC_FC_ULONG:
1085 case RPC_FC_LONG:
1086 case RPC_FC_ERROR_STATUS_T:
1087 case RPC_FC_ENUM32:
1088 case RPC_FC_FLOAT:
1089 size = 4;
1090 if (size > *align) *align = size;
1091 break;
1092 case RPC_FC_HYPER:
1093 case RPC_FC_DOUBLE:
1094 size = 8;
1095 if (size > *align) *align = size;
1096 break;
1097 case RPC_FC_STRUCT:
1098 case RPC_FC_CVSTRUCT:
1099 case RPC_FC_CPSTRUCT:
1100 case RPC_FC_CSTRUCT:
1101 case RPC_FC_PSTRUCT:
1102 case RPC_FC_BOGUS_STRUCT:
1103 size = fields_memsize(type_struct_get_fields(t), align);
1104 break;
1105 case RPC_FC_ENCAPSULATED_UNION:
1106 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1107 break;
1108 case RPC_FC_NON_ENCAPSULATED_UNION:
1109 size = union_memsize(type_union_get_cases(t), align);
1110 break;
1111 case RPC_FC_SMFARRAY:
1112 case RPC_FC_LGFARRAY:
1113 case RPC_FC_SMVARRAY:
1114 case RPC_FC_LGVARRAY:
1115 case RPC_FC_BOGUS_ARRAY:
1116 size = type_array_get_dim(t) * type_memsize(type_array_get_element(t), align);
1117 break;
1118 default:
1119 error("type_memsize: Unknown type 0x%x\n", t->type);
1120 size = 0;
1123 return size;
1126 int is_full_pointer_function(const var_t *func)
1128 const var_t *var;
1129 if (type_has_full_pointer(type_function_get_rettype(func->type)))
1130 return TRUE;
1131 if (!type_get_function_args(func->type))
1132 return FALSE;
1133 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1134 if (type_has_full_pointer( var->type ))
1135 return TRUE;
1136 return FALSE;
1139 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1141 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1142 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1143 fprintf(file, "\n");
1146 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1148 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1149 fprintf(file, "\n");
1152 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
1154 short absoff = type_pointer_get_ref(type)->typestring_offset;
1155 short reloff = absoff - (offset + 2);
1156 int ptr_attr = is_ptr(type_pointer_get_ref(type)) ? 0x10 : 0x0;
1158 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1159 type->type, ptr_attr, string_of_type(type->type));
1160 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1161 reloff, reloff, absoff);
1162 return 4;
1165 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1167 unsigned char fc = type_pointer_get_ref(type)->type;
1168 /* for historical reasons, write_simple_pointer also handled string types,
1169 * but no longer does. catch bad uses of the function with this check */
1170 if (is_string_type(type->attrs, type))
1171 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1172 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1173 type->type, string_of_type(type->type));
1174 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1175 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1176 return 4;
1179 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1181 print_file(file, 0, "/* %u (", tfsoff);
1182 write_type_decl(file, t, NULL);
1183 print_file(file, 0, ") */\n");
1186 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1188 unsigned int offset = *typestring_offset;
1190 print_start_tfs_comment(file, type, offset);
1191 update_tfsoff(type, offset, file);
1193 if (type_pointer_get_ref(type)->typestring_offset)
1194 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1195 else if (is_base_type(type_pointer_get_ref(type)->type))
1196 *typestring_offset += write_simple_pointer(file, type);
1198 return offset;
1201 static int processed(const type_t *type)
1203 return type->typestring_offset && !type->tfswrite;
1206 static int user_type_has_variable_size(const type_t *t)
1208 if (is_ptr(t))
1209 return TRUE;
1210 else
1211 switch (get_struct_type(t))
1213 case RPC_FC_PSTRUCT:
1214 case RPC_FC_CSTRUCT:
1215 case RPC_FC_CPSTRUCT:
1216 case RPC_FC_CVSTRUCT:
1217 return TRUE;
1219 /* Note: Since this only applies to user types, we can't have a conformant
1220 array here, and strings should get filed under pointer in this case. */
1221 return FALSE;
1224 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1226 unsigned int start, absoff, flags;
1227 unsigned int align = 0, ualign = 0;
1228 const char *name = NULL;
1229 type_t *utype = get_user_type(type, &name);
1230 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1231 size_t size = type_memsize(type, &align);
1232 unsigned short funoff = user_type_offset(name);
1233 short reloff;
1235 guard_rec(type);
1237 if (is_base_type(utype->type))
1239 absoff = *tfsoff;
1240 print_start_tfs_comment(file, utype, absoff);
1241 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1242 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1243 *tfsoff += 2;
1245 else
1247 if (!processed(utype))
1248 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1249 absoff = utype->typestring_offset;
1252 if (utype->type == RPC_FC_RP)
1253 flags = 0x40;
1254 else if (utype->type == RPC_FC_UP)
1255 flags = 0x80;
1256 else
1257 flags = 0;
1259 start = *tfsoff;
1260 update_tfsoff(type, start, file);
1261 print_start_tfs_comment(file, type, start);
1262 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1263 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1264 flags | (align - 1), align - 1, flags);
1265 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1266 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1267 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1268 *tfsoff += 8;
1269 reloff = absoff - *tfsoff;
1270 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1271 *tfsoff += 2;
1274 static inline unsigned char make_signed(unsigned char fc)
1276 switch(fc)
1278 case RPC_FC_USMALL:
1279 return RPC_FC_SMALL;
1280 case RPC_FC_USHORT:
1281 return RPC_FC_SHORT;
1282 case RPC_FC_ULONG:
1283 return RPC_FC_LONG;
1284 default:
1285 return fc;
1289 static void write_member_type(FILE *file, const type_t *cont,
1290 const attr_list_t *attrs, const type_t *type,
1291 unsigned int *corroff, unsigned int *tfsoff)
1293 if (is_embedded_complex(type) && !is_conformant_array(type))
1295 size_t absoff;
1296 short reloff;
1298 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1300 absoff = *corroff;
1301 *corroff += 8;
1303 else
1305 absoff = type->typestring_offset;
1307 reloff = absoff - (*tfsoff + 2);
1309 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1310 /* FIXME: actually compute necessary padding */
1311 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1312 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1313 reloff, reloff, absoff);
1314 *tfsoff += 4;
1316 else if (is_ptr(type) || is_conformant_array(type))
1318 unsigned char fc = (get_struct_type(cont) == RPC_FC_BOGUS_STRUCT
1319 ? RPC_FC_POINTER
1320 : RPC_FC_LONG);
1321 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1322 *tfsoff += 1;
1324 else if (!write_base_type(file, make_signed(type->type), tfsoff))
1325 error("Unsupported member type 0x%x\n", type->type);
1328 static void write_end(FILE *file, unsigned int *tfsoff)
1330 if (*tfsoff % 2 == 0)
1332 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1333 *tfsoff += 1;
1335 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1336 *tfsoff += 1;
1339 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1341 unsigned int offset = 0;
1342 var_list_t *fs = type_struct_get_fields(type);
1343 var_t *f;
1345 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1347 unsigned int align = 0;
1348 type_t *ft = f->type;
1349 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1351 unsigned int absoff = ft->typestring_offset;
1352 short reloff = absoff - (*tfsoff + 6);
1353 print_file(file, 0, "/* %d */\n", *tfsoff);
1354 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1355 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1356 write_conf_or_var_desc(file, current_structure, offset, ft,
1357 get_attrp(f->attrs, ATTR_SWITCHIS));
1358 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1359 reloff, reloff, absoff);
1360 *tfsoff += 8;
1363 /* FIXME: take alignment into account */
1364 offset += type_memsize(ft, &align);
1368 static int write_no_repeat_pointer_descriptions(
1369 FILE *file, type_t *type,
1370 size_t *offset_in_memory, size_t *offset_in_buffer,
1371 unsigned int *typestring_offset)
1373 int written = 0;
1374 unsigned int align;
1376 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1378 size_t memsize;
1380 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1381 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1383 /* pointer instance */
1384 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1385 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1386 *typestring_offset += 6;
1388 if (is_ptr(type))
1390 if (is_string_type(type->attrs, type))
1391 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1392 else
1393 write_pointer_tfs(file, type, typestring_offset);
1395 else
1397 unsigned absoff = type->typestring_offset;
1398 short reloff = absoff - (*typestring_offset + 2);
1399 /* FIXME: get pointer attributes from field */
1400 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1401 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1402 reloff, reloff, absoff);
1403 *typestring_offset += 4;
1406 align = 0;
1407 memsize = type_memsize(type, &align);
1408 *offset_in_memory += memsize;
1409 /* increment these separately as in the case of conformant (varying)
1410 * structures these start at different values */
1411 *offset_in_buffer += memsize;
1413 return 1;
1416 if (is_non_complex_struct(type))
1418 const var_t *v;
1419 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1421 if (offset_in_memory && offset_in_buffer)
1423 size_t padding;
1424 align = 0;
1425 type_memsize(v->type, &align);
1426 padding = ROUNDING(*offset_in_memory, align);
1427 *offset_in_memory += padding;
1428 *offset_in_buffer += padding;
1430 written += write_no_repeat_pointer_descriptions(
1431 file, v->type,
1432 offset_in_memory, offset_in_buffer, typestring_offset);
1435 else
1437 size_t memsize;
1438 align = 0;
1439 memsize = type_memsize(type, &align);
1440 *offset_in_memory += memsize;
1441 /* increment these separately as in the case of conformant (varying)
1442 * structures these start at different values */
1443 *offset_in_buffer += memsize;
1446 return written;
1449 static int write_pointer_description_offsets(
1450 FILE *file, const attr_list_t *attrs, type_t *type,
1451 size_t *offset_in_memory, size_t *offset_in_buffer,
1452 unsigned int *typestring_offset)
1454 int written = 0;
1455 unsigned int align;
1457 if (is_ptr(type) && type_pointer_get_ref(type)->type != RPC_FC_IP)
1459 if (offset_in_memory && offset_in_buffer)
1461 size_t memsize;
1463 /* pointer instance */
1464 /* FIXME: sometimes from end of structure, sometimes from beginning */
1465 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1466 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1468 align = 0;
1469 memsize = type_memsize(type, &align);
1470 *offset_in_memory += memsize;
1471 /* increment these separately as in the case of conformant (varying)
1472 * structures these start at different values */
1473 *offset_in_buffer += memsize;
1475 *typestring_offset += 4;
1477 if (is_string_type(attrs, type))
1478 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1479 else if (processed(type_pointer_get_ref(type)) ||
1480 is_base_type(type_pointer_get_ref(type)->type))
1481 write_pointer_tfs(file, type, typestring_offset);
1482 else
1483 error("write_pointer_description_offsets: type format string unknown\n");
1485 return 1;
1488 if (is_array(type))
1490 return write_pointer_description_offsets(
1491 file, attrs, type_array_get_element(type), offset_in_memory,
1492 offset_in_buffer, typestring_offset);
1494 else if (is_non_complex_struct(type))
1496 /* otherwise search for interesting fields to parse */
1497 const var_t *v;
1498 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1500 if (offset_in_memory && offset_in_buffer)
1502 size_t padding;
1503 align = 0;
1504 type_memsize(v->type, &align);
1505 padding = ROUNDING(*offset_in_memory, align);
1506 *offset_in_memory += padding;
1507 *offset_in_buffer += padding;
1509 written += write_pointer_description_offsets(
1510 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1511 typestring_offset);
1514 else
1516 if (offset_in_memory && offset_in_buffer)
1518 size_t memsize;
1519 align = 0;
1520 memsize = type_memsize(type, &align);
1521 *offset_in_memory += memsize;
1522 /* increment these separately as in the case of conformant (varying)
1523 * structures these start at different values */
1524 *offset_in_buffer += memsize;
1528 return written;
1531 /* Note: if file is NULL return value is number of pointers to write, else
1532 * it is the number of type format characters written */
1533 static int write_fixed_array_pointer_descriptions(
1534 FILE *file, const attr_list_t *attrs, type_t *type,
1535 size_t *offset_in_memory, size_t *offset_in_buffer,
1536 unsigned int *typestring_offset)
1538 unsigned int align;
1539 int pointer_count = 0;
1540 int real_type = get_array_type( type );
1542 if (real_type == RPC_FC_SMFARRAY || real_type == RPC_FC_LGFARRAY)
1544 unsigned int temp = 0;
1545 /* unfortunately, this needs to be done in two passes to avoid
1546 * writing out redundant FC_FIXED_REPEAT descriptions */
1547 pointer_count = write_pointer_description_offsets(
1548 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1549 if (pointer_count > 0)
1551 unsigned int increment_size;
1552 size_t offset_of_array_pointer_mem = 0;
1553 size_t offset_of_array_pointer_buf = 0;
1555 align = 0;
1556 increment_size = type_memsize(type_array_get_element(type), &align);
1558 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1559 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1560 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1561 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1562 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1563 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1564 *typestring_offset += 10;
1566 pointer_count = write_pointer_description_offsets(
1567 file, attrs, type, &offset_of_array_pointer_mem,
1568 &offset_of_array_pointer_buf, typestring_offset);
1571 else if (is_struct(type->type))
1573 const var_t *v;
1574 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1576 if (offset_in_memory && offset_in_buffer)
1578 size_t padding;
1579 align = 0;
1580 type_memsize(v->type, &align);
1581 padding = ROUNDING(*offset_in_memory, align);
1582 *offset_in_memory += padding;
1583 *offset_in_buffer += padding;
1585 pointer_count += write_fixed_array_pointer_descriptions(
1586 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1587 typestring_offset);
1590 else
1592 if (offset_in_memory && offset_in_buffer)
1594 size_t memsize;
1595 align = 0;
1596 memsize = type_memsize(type, &align);
1597 *offset_in_memory += memsize;
1598 /* increment these separately as in the case of conformant (varying)
1599 * structures these start at different values */
1600 *offset_in_buffer += memsize;
1604 return pointer_count;
1607 /* Note: if file is NULL return value is number of pointers to write, else
1608 * it is the number of type format characters written */
1609 static int write_conformant_array_pointer_descriptions(
1610 FILE *file, const attr_list_t *attrs, type_t *type,
1611 size_t offset_in_memory, unsigned int *typestring_offset)
1613 unsigned int align;
1614 int pointer_count = 0;
1616 if (is_conformant_array(type) && !type_array_has_variance(type))
1618 unsigned int temp = 0;
1619 /* unfortunately, this needs to be done in two passes to avoid
1620 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1621 pointer_count = write_pointer_description_offsets(
1622 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1623 if (pointer_count > 0)
1625 unsigned int increment_size;
1626 size_t offset_of_array_pointer_mem = offset_in_memory;
1627 size_t offset_of_array_pointer_buf = offset_in_memory;
1629 align = 0;
1630 increment_size = type_memsize(type_array_get_element(type), &align);
1632 if (increment_size > USHRT_MAX)
1633 error("array size of %u bytes is too large\n", increment_size);
1635 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1636 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1637 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1638 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1639 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1640 *typestring_offset += 8;
1642 pointer_count = write_pointer_description_offsets(
1643 file, attrs, type_array_get_element(type),
1644 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1645 typestring_offset);
1649 return pointer_count;
1652 /* Note: if file is NULL return value is number of pointers to write, else
1653 * it is the number of type format characters written */
1654 static int write_varying_array_pointer_descriptions(
1655 FILE *file, const attr_list_t *attrs, type_t *type,
1656 size_t *offset_in_memory, size_t *offset_in_buffer,
1657 unsigned int *typestring_offset)
1659 unsigned int align;
1660 int pointer_count = 0;
1662 if (is_array(type) && type_array_has_variance(type))
1664 unsigned int temp = 0;
1665 /* unfortunately, this needs to be done in two passes to avoid
1666 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1667 pointer_count = write_pointer_description_offsets(
1668 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1669 if (pointer_count > 0)
1671 unsigned int increment_size;
1673 align = 0;
1674 increment_size = type_memsize(type_array_get_element(type), &align);
1676 if (increment_size > USHRT_MAX)
1677 error("array size of %u bytes is too large\n", increment_size);
1679 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1680 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1681 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1682 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1683 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1684 *typestring_offset += 8;
1686 pointer_count = write_pointer_description_offsets(
1687 file, attrs, type, offset_in_memory,
1688 offset_in_buffer, typestring_offset);
1691 else if (is_struct(type->type))
1693 const var_t *v;
1694 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1696 if (offset_in_memory && offset_in_buffer)
1698 size_t padding;
1700 if (is_array(v->type) && type_array_has_variance(v->type))
1702 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1703 /* skip over variance and offset in buffer */
1704 *offset_in_buffer += 8;
1707 align = 0;
1708 type_memsize(v->type, &align);
1709 padding = ROUNDING(*offset_in_memory, align);
1710 *offset_in_memory += padding;
1711 *offset_in_buffer += padding;
1713 pointer_count += write_varying_array_pointer_descriptions(
1714 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1715 typestring_offset);
1718 else
1720 if (offset_in_memory && offset_in_buffer)
1722 size_t memsize;
1723 align = 0;
1724 memsize = type_memsize(type, &align);
1725 *offset_in_memory += memsize;
1726 /* increment these separately as in the case of conformant (varying)
1727 * structures these start at different values */
1728 *offset_in_buffer += memsize;
1732 return pointer_count;
1735 static void write_pointer_description(FILE *file, type_t *type,
1736 unsigned int *typestring_offset)
1738 size_t offset_in_buffer;
1739 size_t offset_in_memory;
1741 /* pass 1: search for single instance of a pointer (i.e. don't descend
1742 * into arrays) */
1743 if (!is_array(type))
1745 offset_in_memory = 0;
1746 offset_in_buffer = 0;
1747 write_no_repeat_pointer_descriptions(
1748 file, type,
1749 &offset_in_memory, &offset_in_buffer, typestring_offset);
1752 /* pass 2: search for pointers in fixed arrays */
1753 offset_in_memory = 0;
1754 offset_in_buffer = 0;
1755 write_fixed_array_pointer_descriptions(
1756 file, NULL, type,
1757 &offset_in_memory, &offset_in_buffer, typestring_offset);
1759 /* pass 3: search for pointers in conformant only arrays (but don't descend
1760 * into conformant varying or varying arrays) */
1761 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1762 write_conformant_array_pointer_descriptions(
1763 file, NULL, type, 0, typestring_offset);
1764 else if (get_struct_type(type) == RPC_FC_CPSTRUCT)
1766 unsigned int align = 0;
1767 type_t *carray = find_array_or_string_in_struct(type)->type;
1768 write_conformant_array_pointer_descriptions(
1769 file, NULL, carray,
1770 type_memsize(type, &align),
1771 typestring_offset);
1774 /* pass 4: search for pointers in varying arrays */
1775 offset_in_memory = 0;
1776 offset_in_buffer = 0;
1777 write_varying_array_pointer_descriptions(
1778 file, NULL, type,
1779 &offset_in_memory, &offset_in_buffer, typestring_offset);
1782 int is_declptr(const type_t *t)
1784 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1787 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1788 type_t *type,
1789 const char *name, unsigned int *typestring_offset)
1791 size_t start_offset;
1792 unsigned char rtype;
1794 if (is_declptr(type))
1796 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1797 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1798 if (!pointer_type)
1799 pointer_type = RPC_FC_RP;
1800 print_start_tfs_comment(file, type, *typestring_offset);
1801 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1802 pointer_type, flag, string_of_type(pointer_type),
1803 flag ? " [simple_pointer]" : "");
1804 *typestring_offset += 2;
1805 if (!flag)
1807 print_file(file, 2, "NdrFcShort(0x2),\n");
1808 *typestring_offset += 2;
1812 start_offset = *typestring_offset;
1813 update_tfsoff(type, start_offset, file);
1815 if (is_array(type))
1816 rtype = type_array_get_element(type)->type;
1817 else
1818 rtype = type_pointer_get_ref(type)->type;
1820 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1822 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1823 return start_offset;
1826 if (type->declarray && !is_conformant_array(type))
1828 unsigned long dim = type_array_get_dim(type);
1830 /* FIXME: multi-dimensional array */
1831 if (0xffffuL < dim)
1832 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1833 name, 0xffffu, dim - 0xffffu);
1835 if (rtype == RPC_FC_CHAR)
1836 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1837 else
1838 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1839 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1840 *typestring_offset += 2;
1842 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", dim, dim);
1843 *typestring_offset += 2;
1845 return start_offset;
1847 else if (is_conformant_array(type))
1849 unsigned int align = 0;
1851 if (rtype == RPC_FC_CHAR)
1852 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1853 else
1854 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1855 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1856 *typestring_offset += 2;
1858 *typestring_offset += write_conf_or_var_desc(
1859 file, current_structure,
1860 (type->declarray && current_structure
1861 ? type_memsize(current_structure, &align)
1862 : 0),
1863 type, type_array_get_conformance(type));
1865 return start_offset;
1867 else
1869 if (rtype == RPC_FC_WCHAR)
1870 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1871 else
1872 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1873 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1874 *typestring_offset += 2;
1876 return start_offset;
1880 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1881 const char *name, unsigned int *typestring_offset)
1883 const expr_t *length_is = type_array_get_variance(type);
1884 const expr_t *size_is = type_array_get_conformance(type);
1885 unsigned int align = 0;
1886 size_t size;
1887 size_t start_offset;
1888 int real_type;
1889 int has_pointer;
1890 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1891 unsigned int baseoff
1892 = type->declarray && current_structure
1893 ? type_memsize(current_structure, &align)
1894 : 0;
1896 if (!pointer_type)
1897 pointer_type = RPC_FC_RP;
1899 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
1900 has_pointer = TRUE;
1901 else
1902 has_pointer = type_has_pointers(type_array_get_element(type));
1904 align = 0;
1905 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
1906 real_type = get_array_type( type );
1908 start_offset = *typestring_offset;
1909 update_tfsoff(type, start_offset, file);
1910 print_start_tfs_comment(file, type, start_offset);
1911 print_file(file, 2, "0x%02x,\t/* %s */\n", real_type, string_of_type(real_type));
1912 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1913 *typestring_offset += 2;
1915 align = 0;
1916 if (real_type != RPC_FC_BOGUS_ARRAY)
1918 if (real_type == RPC_FC_LGFARRAY || real_type == RPC_FC_LGVARRAY)
1920 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1921 *typestring_offset += 4;
1923 else
1925 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1926 *typestring_offset += 2;
1929 if (is_conformant_array(type))
1930 *typestring_offset
1931 += write_conf_or_var_desc(file, current_structure, baseoff,
1932 type, size_is);
1934 if (real_type == RPC_FC_SMVARRAY || real_type == RPC_FC_LGVARRAY)
1936 unsigned int elalign = 0;
1937 size_t elsize = type_memsize(type_array_get_element(type), &elalign);
1938 unsigned long dim = type_array_get_dim(type);
1940 if (real_type == RPC_FC_LGVARRAY)
1942 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", dim, dim);
1943 *typestring_offset += 4;
1945 else
1947 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", dim, dim);
1948 *typestring_offset += 2;
1951 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1952 *typestring_offset += 2;
1955 if (length_is)
1956 *typestring_offset
1957 += write_conf_or_var_desc(file, current_structure, baseoff,
1958 type, length_is);
1960 if (has_pointer && (!type->declarray || !current_structure))
1962 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1963 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1964 *typestring_offset += 2;
1965 write_pointer_description(file, type, typestring_offset);
1966 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1967 *typestring_offset += 1;
1970 write_member_type(file, type, NULL, type_array_get_element(type), NULL, typestring_offset);
1971 write_end(file, typestring_offset);
1973 else
1975 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
1976 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1977 *typestring_offset += 2;
1978 *typestring_offset
1979 += write_conf_or_var_desc(file, current_structure, baseoff,
1980 type, size_is);
1981 *typestring_offset
1982 += write_conf_or_var_desc(file, current_structure, baseoff,
1983 type, length_is);
1984 write_member_type(file, type, NULL, type_array_get_element(type), NULL, typestring_offset);
1985 write_end(file, typestring_offset);
1988 return start_offset;
1991 static const var_t *find_array_or_string_in_struct(const type_t *type)
1993 const var_list_t *fields = type_struct_get_fields(type);
1994 const var_t *last_field;
1995 const type_t *ft;
1996 int real_type;
1998 if (!fields || list_empty(fields))
1999 return NULL;
2001 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2002 ft = last_field->type;
2004 if (ft->declarray && is_conformant_array(ft))
2005 return last_field;
2007 real_type = get_struct_type( type );
2008 if (real_type == RPC_FC_CSTRUCT || real_type == RPC_FC_CPSTRUCT || real_type == RPC_FC_CVSTRUCT)
2009 return find_array_or_string_in_struct(ft);
2010 else
2011 return NULL;
2014 static void write_struct_members(FILE *file, const type_t *type,
2015 unsigned int *corroff, unsigned int *typestring_offset)
2017 const var_t *field;
2018 unsigned short offset = 0;
2019 int salign = -1;
2020 int padding;
2021 var_list_t *fields = type_struct_get_fields(type);
2023 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2025 type_t *ft = field->type;
2026 if (!ft->declarray || !is_conformant_array(ft))
2028 unsigned int align = 0;
2029 size_t size = type_memsize(ft, &align);
2030 if (salign == -1)
2031 salign = align;
2032 if ((align - 1) & offset)
2034 unsigned char fc = 0;
2035 switch (align)
2037 case 4:
2038 fc = RPC_FC_ALIGNM4;
2039 break;
2040 case 8:
2041 fc = RPC_FC_ALIGNM8;
2042 break;
2043 default:
2044 error("write_struct_members: cannot align type %d\n", ft->type);
2046 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2047 offset = ROUND_SIZE(offset, align);
2048 *typestring_offset += 1;
2050 write_member_type(file, type, field->attrs, field->type, corroff,
2051 typestring_offset);
2052 offset += size;
2056 padding = ROUNDING(offset, salign);
2057 if (padding)
2059 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2060 RPC_FC_STRUCTPAD1 + padding - 1,
2061 padding);
2062 *typestring_offset += 1;
2065 write_end(file, typestring_offset);
2068 static size_t write_struct_tfs(FILE *file, type_t *type,
2069 const char *name, unsigned int *tfsoff)
2071 const type_t *save_current_structure = current_structure;
2072 unsigned int total_size;
2073 const var_t *array;
2074 size_t start_offset;
2075 size_t array_offset;
2076 int has_pointers = 0;
2077 unsigned int align = 0;
2078 unsigned int corroff;
2079 var_t *f;
2080 int real_type = get_struct_type( type );
2081 var_list_t *fields = type_struct_get_fields(type);
2083 guard_rec(type);
2084 current_structure = type;
2086 total_size = type_memsize(type, &align);
2087 if (total_size > USHRT_MAX)
2088 error("structure size for %s exceeds %d bytes by %d bytes\n",
2089 name, USHRT_MAX, total_size - USHRT_MAX);
2091 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2092 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2093 FALSE, tfsoff);
2094 if (!has_pointers) has_pointers = type_has_pointers(type);
2096 array = find_array_or_string_in_struct(type);
2097 if (array && !processed(array->type))
2098 array_offset
2099 = is_string_type(array->attrs, array->type)
2100 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2101 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2103 corroff = *tfsoff;
2104 write_descriptors(file, type, tfsoff);
2106 start_offset = *tfsoff;
2107 update_tfsoff(type, start_offset, file);
2108 print_start_tfs_comment(file, type, start_offset);
2109 print_file(file, 2, "0x%x,\t/* %s */\n", real_type, string_of_type(real_type));
2110 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2111 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
2112 *tfsoff += 4;
2114 if (array)
2116 unsigned int absoff = array->type->typestring_offset;
2117 short reloff = absoff - *tfsoff;
2118 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
2119 reloff, reloff, absoff);
2120 *tfsoff += 2;
2122 else if (real_type == RPC_FC_BOGUS_STRUCT)
2124 print_file(file, 2, "NdrFcShort(0x0),\n");
2125 *tfsoff += 2;
2128 if (real_type == RPC_FC_BOGUS_STRUCT)
2130 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2131 nothing is written to file yet. On the actual writing pass,
2132 this will have been updated. */
2133 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2134 int reloff = absoff - *tfsoff;
2135 assert( reloff >= 0 );
2136 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%u) */\n",
2137 reloff, reloff, absoff);
2138 *tfsoff += 2;
2140 else if ((real_type == RPC_FC_PSTRUCT) ||
2141 (real_type == RPC_FC_CPSTRUCT) ||
2142 (real_type == RPC_FC_CVSTRUCT && has_pointers))
2144 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2145 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2146 *tfsoff += 2;
2147 write_pointer_description(file, type, tfsoff);
2148 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2149 *tfsoff += 1;
2152 write_struct_members(file, type, &corroff, tfsoff);
2154 if (real_type == RPC_FC_BOGUS_STRUCT)
2156 const var_t *f;
2158 type->ptrdesc = *tfsoff;
2159 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2161 type_t *ft = f->type;
2162 if (is_ptr(ft))
2164 if (is_string_type(f->attrs, ft))
2165 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2166 else
2167 write_pointer_tfs(file, ft, tfsoff);
2169 else if (!ft->declarray && is_conformant_array(ft))
2171 unsigned int absoff = ft->typestring_offset;
2172 short reloff = absoff - (*tfsoff + 2);
2173 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2174 /* FIXME: We need to store pointer attributes for arrays
2175 so we don't lose pointer_default info. */
2176 if (ptr_type == 0)
2177 ptr_type = RPC_FC_UP;
2178 print_file(file, 0, "/* %d */\n", *tfsoff);
2179 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2180 string_of_type(ptr_type));
2181 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2182 reloff, reloff, absoff);
2183 *tfsoff += 4;
2186 if (type->ptrdesc == *tfsoff)
2187 type->ptrdesc = 0;
2190 current_structure = save_current_structure;
2191 return start_offset;
2194 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2195 unsigned char flags, size_t offset,
2196 unsigned int *typeformat_offset)
2198 size_t start_offset = *typeformat_offset;
2199 short reloff = offset - (*typeformat_offset + 2);
2200 int in_attr, out_attr;
2201 in_attr = is_attr(attrs, ATTR_IN);
2202 out_attr = is_attr(attrs, ATTR_OUT);
2203 if (!in_attr && !out_attr) in_attr = 1;
2205 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2206 flags |= 0x04;
2208 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2209 pointer_type,
2210 flags,
2211 string_of_type(pointer_type));
2212 if (file)
2214 if (flags & 0x04)
2215 fprintf(file, " [allocated_on_stack]");
2216 if (flags & 0x10)
2217 fprintf(file, " [pointer_deref]");
2218 fprintf(file, " */\n");
2221 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
2222 *typeformat_offset += 4;
2224 return start_offset;
2227 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2229 if (t == NULL)
2231 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2233 else if (is_base_type(t->type))
2235 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2236 t->type, string_of_type(t->type));
2238 else if (t->typestring_offset)
2240 short reloff = t->typestring_offset - *tfsoff;
2241 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
2242 reloff, reloff, t->typestring_offset);
2244 else
2245 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
2247 *tfsoff += 2;
2250 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2252 unsigned int align = 0;
2253 unsigned int start_offset;
2254 size_t size = type_memsize(type, &align);
2255 var_list_t *fields;
2256 size_t nbranch = 0;
2257 type_t *deftype = NULL;
2258 short nodeftype = 0xffff;
2259 var_t *f;
2261 guard_rec(type);
2263 fields = type_union_get_cases(type);
2265 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2267 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2268 if (cases)
2269 nbranch += list_count(cases);
2270 if (f->type)
2271 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2274 start_offset = *tfsoff;
2275 update_tfsoff(type, start_offset, file);
2276 print_start_tfs_comment(file, type, start_offset);
2277 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2279 const var_t *sv = type_union_get_switch_value(type);
2280 const type_t *st = sv->type;
2282 switch (st->type)
2284 case RPC_FC_CHAR:
2285 case RPC_FC_SMALL:
2286 case RPC_FC_USMALL:
2287 case RPC_FC_SHORT:
2288 case RPC_FC_USHORT:
2289 case RPC_FC_LONG:
2290 case RPC_FC_ULONG:
2291 case RPC_FC_ENUM16:
2292 case RPC_FC_ENUM32:
2293 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2294 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2295 0x40 | st->type, string_of_type(st->type));
2296 *tfsoff += 2;
2297 break;
2298 default:
2299 error("union switch type must be an integer, char, or enum\n");
2302 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2304 static const expr_t dummy_expr; /* FIXME */
2305 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2307 switch (st->type)
2309 case RPC_FC_CHAR:
2310 case RPC_FC_SMALL:
2311 case RPC_FC_USMALL:
2312 case RPC_FC_SHORT:
2313 case RPC_FC_USHORT:
2314 case RPC_FC_LONG:
2315 case RPC_FC_ULONG:
2316 case RPC_FC_ENUM16:
2317 case RPC_FC_ENUM32:
2318 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2319 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2320 st->type, string_of_type(st->type));
2321 *tfsoff += 2;
2322 break;
2323 default:
2324 error("union switch type must be an integer, char, or enum\n");
2327 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2328 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2329 *tfsoff += 2;
2332 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
2333 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
2334 *tfsoff += 4;
2336 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2338 type_t *ft = f->type;
2339 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2340 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2341 expr_t *c;
2343 if (cases == NULL && !deflt)
2344 error("union field %s with neither case nor default attribute\n", f->name);
2346 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2348 /* MIDL doesn't check for duplicate cases, even though that seems
2349 like a reasonable thing to do, it just dumps them to the TFS
2350 like we're going to do here. */
2351 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2352 *tfsoff += 4;
2353 write_branch_type(file, ft, tfsoff);
2356 /* MIDL allows multiple default branches, even though that seems
2357 illogical, it just chooses the last one, which is what we will
2358 do. */
2359 if (deflt)
2361 deftype = ft;
2362 nodeftype = 0;
2366 if (deftype)
2368 write_branch_type(file, deftype, tfsoff);
2370 else
2372 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2373 *tfsoff += 2;
2376 return start_offset;
2379 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2380 unsigned int *typeformat_offset)
2382 size_t i;
2383 size_t start_offset = *typeformat_offset;
2384 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2386 if (iid)
2388 print_file(file, 2, "0x2f, /* FC_IP */\n");
2389 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2390 *typeformat_offset
2391 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2393 else
2395 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2396 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2398 if (! uuid)
2399 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2401 update_tfsoff(type, start_offset, file);
2402 print_start_tfs_comment(file, type, start_offset);
2403 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2404 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2405 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2406 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2407 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2408 for (i = 0; i < 8; ++i)
2409 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2411 if (file)
2412 fprintf(file, "\n");
2414 *typeformat_offset += 18;
2416 return start_offset;
2419 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2420 const var_t *var,
2421 unsigned int *typeformat_offset)
2423 size_t start_offset = *typeformat_offset;
2424 unsigned char flags = 0;
2426 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2427 flags |= NDR_STRICT_CONTEXT_HANDLE;
2429 if (is_ptr(type))
2430 flags |= 0x80;
2431 if (is_attr(var->attrs, ATTR_IN))
2433 flags |= 0x40;
2434 if (!is_attr(var->attrs, ATTR_OUT))
2435 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2437 if (is_attr(var->attrs, ATTR_OUT))
2438 flags |= 0x20;
2440 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2441 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2442 /* return and can't be null values overlap */
2443 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2444 print_file(file, 0, "can't be null, ");
2445 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2446 print_file(file, 0, "serialize, ");
2447 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2448 print_file(file, 0, "no serialize, ");
2449 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2450 print_file(file, 0, "strict, ");
2451 if ((flags & 0x21) == 0x20)
2452 print_file(file, 0, "out, ");
2453 if ((flags & 0x21) == 0x21)
2454 print_file(file, 0, "return, ");
2455 if (flags & 0x40)
2456 print_file(file, 0, "in, ");
2457 if (flags & 0x80)
2458 print_file(file, 0, "via ptr, ");
2459 print_file(file, 0, "*/\n");
2460 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2461 print_file(file, 2, "0, /* FIXME: param num */\n");
2462 *typeformat_offset += 4;
2464 return start_offset;
2467 static size_t write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2468 type_t *type, const var_t *var,
2469 unsigned int *typeformat_offset)
2471 size_t offset;
2473 if (is_context_handle(type))
2474 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2476 if (is_user_type(type))
2478 write_user_tfs(file, type, typeformat_offset);
2479 return type->typestring_offset;
2482 if (is_string_type(var->attrs, type))
2483 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2485 if (is_array(type))
2487 int ptr_type;
2488 size_t off;
2489 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2490 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2491 /* Top level pointers to conformant arrays may be handled specially
2492 since we can bypass the pointer, but if the array is buried
2493 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2494 always need to write the pointer. */
2495 if (!ptr_type && var->type != type)
2496 /* FIXME: This should use pointer_default, but the information
2497 isn't kept around for arrays. */
2498 ptr_type = RPC_FC_UP;
2499 if (ptr_type && ptr_type != RPC_FC_RP)
2501 unsigned int absoff = type->typestring_offset;
2502 short reloff = absoff - (*typeformat_offset + 2);
2503 off = *typeformat_offset;
2504 print_file(file, 0, "/* %d */\n", off);
2505 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2506 string_of_type(ptr_type));
2507 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2508 reloff, reloff, absoff);
2509 *typeformat_offset += 4;
2511 return off;
2514 if (!is_ptr(type))
2516 /* basic types don't need a type format string */
2517 if (is_base_type(type->type))
2518 return 0;
2520 if (processed(type)) return type->typestring_offset;
2522 switch (type->type)
2524 case RPC_FC_STRUCT:
2525 case RPC_FC_PSTRUCT:
2526 case RPC_FC_CSTRUCT:
2527 case RPC_FC_CPSTRUCT:
2528 case RPC_FC_CVSTRUCT:
2529 case RPC_FC_BOGUS_STRUCT:
2530 return write_struct_tfs(file, type, var->name, typeformat_offset);
2531 case RPC_FC_ENCAPSULATED_UNION:
2532 case RPC_FC_NON_ENCAPSULATED_UNION:
2533 return write_union_tfs(file, type, typeformat_offset);
2534 case RPC_FC_IGNORE:
2535 case RPC_FC_BIND_PRIMITIVE:
2536 /* nothing to do */
2537 return 0;
2538 default:
2539 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2542 else if (last_ptr(type))
2544 size_t start_offset = *typeformat_offset;
2545 int in_attr = is_attr(var->attrs, ATTR_IN);
2546 int out_attr = is_attr(var->attrs, ATTR_OUT);
2547 const type_t *base = type_pointer_get_ref(type);
2549 if (base->type == RPC_FC_IP
2550 || (base->type == 0
2551 && is_attr(var->attrs, ATTR_IIDIS)))
2553 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2556 /* special case for pointers to base types */
2557 if (is_base_type(base->type))
2559 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2560 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2561 string_of_type(type->type),
2562 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2563 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2564 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2565 *typeformat_offset += 4;
2566 return start_offset;
2570 assert(is_ptr(type));
2572 offset = write_typeformatstring_var(file, indent, func,
2573 type_pointer_get_ref(type), var,
2574 typeformat_offset);
2575 if (file)
2576 fprintf(file, "/* %2u */\n", *typeformat_offset);
2577 return write_pointer_only_tfs(file, var->attrs, type->type,
2578 !last_ptr(type) ? 0x10 : 0,
2579 offset, typeformat_offset);
2582 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2583 const char *name, int write_ptr, unsigned int *tfsoff)
2585 int retmask = 0;
2587 if (is_user_type(type))
2589 write_user_tfs(file, type, tfsoff);
2591 else if (is_string_type(attrs, type))
2593 write_string_tfs(file, attrs, type, name, tfsoff);
2595 else if (is_ptr(type))
2597 type_t *ref = type_pointer_get_ref(type);
2599 if (ref->type == RPC_FC_IP
2600 || (ref->type == 0
2601 && is_attr(attrs, ATTR_IIDIS)))
2603 write_ip_tfs(file, attrs, type, tfsoff);
2605 else
2607 if (!processed(ref) && !is_base_type(ref->type))
2608 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2610 if (write_ptr)
2611 write_pointer_tfs(file, type, tfsoff);
2613 retmask |= 1;
2616 else if (type->declarray && is_conformant_array(type))
2617 ; /* conformant arrays and strings are handled specially */
2618 else if (is_array(type))
2620 write_array_tfs(file, attrs, type, name, tfsoff);
2621 if (is_conformant_array(type))
2622 retmask |= 1;
2624 else if (is_struct(type->type))
2626 if (!processed(type))
2627 write_struct_tfs(file, type, name, tfsoff);
2629 else if (is_union(type->type))
2631 if (!processed(type))
2632 write_union_tfs(file, type, tfsoff);
2634 else if (!is_base_type(type->type))
2635 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2636 name, type->type);
2638 return retmask;
2641 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2642 type_pred_t pred, unsigned int *typeformat_offset)
2644 const var_t *var;
2645 const statement_t *stmt;
2647 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2649 const type_t *iface;
2650 const statement_t *stmt_func;
2652 if (stmt->type == STMT_LIBRARY)
2654 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2655 continue;
2657 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2658 continue;
2660 iface = stmt->u.type;
2661 if (!pred(iface))
2662 continue;
2664 current_iface = iface;
2665 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2667 const var_t *func = stmt_func->u.var;
2668 if (is_local(func->attrs)) continue;
2670 if (!is_void(type_function_get_rettype(func->type)))
2672 var_t v = *func;
2673 v.type = type_function_get_rettype(func->type);
2674 update_tfsoff(type_function_get_rettype(func->type),
2675 write_typeformatstring_var(
2676 file, 2, NULL,
2677 type_function_get_rettype(func->type),
2678 &v, typeformat_offset),
2679 file);
2682 current_func = func;
2683 if (type_get_function_args(func->type))
2684 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2685 update_tfsoff(
2686 var->type,
2687 write_typeformatstring_var(
2688 file, 2, func, var->type, var,
2689 typeformat_offset),
2690 file);
2694 return *typeformat_offset + 1;
2697 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2699 unsigned int typeformat_offset = 2;
2701 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2705 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2707 int indent = 0;
2709 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2710 print_file(file, indent, "{\n");
2711 indent++;
2712 print_file(file, indent, "0,\n");
2713 print_file(file, indent, "{\n");
2714 indent++;
2715 print_file(file, indent, "NdrFcShort(0x0),\n");
2717 set_all_tfswrite(TRUE);
2718 process_tfs(file, stmts, pred);
2720 print_file(file, indent, "0x0\n");
2721 indent--;
2722 print_file(file, indent, "}\n");
2723 indent--;
2724 print_file(file, indent, "};\n");
2725 print_file(file, indent, "\n");
2728 static unsigned int get_required_buffer_size_type(
2729 const type_t *type, const char *name, unsigned int *alignment)
2731 const char *uname;
2732 const type_t *utype;
2734 *alignment = 0;
2735 if ((utype = get_user_type(type, &uname)))
2737 return get_required_buffer_size_type(utype, uname, alignment);
2739 else
2741 switch (get_struct_type(type))
2743 case RPC_FC_BYTE:
2744 case RPC_FC_CHAR:
2745 case RPC_FC_USMALL:
2746 case RPC_FC_SMALL:
2747 *alignment = 4;
2748 return 1;
2750 case RPC_FC_WCHAR:
2751 case RPC_FC_USHORT:
2752 case RPC_FC_SHORT:
2753 case RPC_FC_ENUM16:
2754 *alignment = 4;
2755 return 2;
2757 case RPC_FC_ULONG:
2758 case RPC_FC_LONG:
2759 case RPC_FC_ENUM32:
2760 case RPC_FC_FLOAT:
2761 case RPC_FC_ERROR_STATUS_T:
2762 *alignment = 4;
2763 return 4;
2765 case RPC_FC_HYPER:
2766 case RPC_FC_DOUBLE:
2767 *alignment = 8;
2768 return 8;
2770 case RPC_FC_IGNORE:
2771 case RPC_FC_BIND_PRIMITIVE:
2772 return 0;
2774 case RPC_FC_STRUCT:
2775 if (!type_struct_get_fields(type)) return 0;
2776 return fields_memsize(type_struct_get_fields(type), alignment);
2778 case RPC_FC_RP:
2780 const type_t *ref = type_pointer_get_ref(type);
2781 return is_base_type( ref->type ) || get_struct_type(ref) == RPC_FC_STRUCT ?
2782 get_required_buffer_size_type( ref, name, alignment ) : 0;
2785 case RPC_FC_SMFARRAY:
2786 case RPC_FC_LGFARRAY:
2787 return type_array_get_dim(type) * get_required_buffer_size_type(type_array_get_element(type), name, alignment);
2789 default:
2790 return 0;
2795 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2797 int in_attr = is_attr(var->attrs, ATTR_IN);
2798 int out_attr = is_attr(var->attrs, ATTR_OUT);
2800 if (!in_attr && !out_attr)
2801 in_attr = 1;
2803 *alignment = 0;
2805 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2806 pass == PASS_RETURN)
2808 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2810 *alignment = 4;
2811 return 20;
2814 if (!is_string_type(var->attrs, var->type))
2815 return get_required_buffer_size_type(var->type, var->name,
2816 alignment);
2818 return 0;
2821 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
2823 const var_t *var;
2824 unsigned int total_size = 0, alignment;
2826 if (type_get_function_args(func->type))
2828 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2830 total_size += get_required_buffer_size(var, &alignment, pass);
2831 total_size += alignment;
2835 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
2837 var_t v = *func;
2838 v.type = type_function_get_rettype(func->type);
2839 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2840 total_size += alignment;
2842 return total_size;
2845 static void print_phase_function(FILE *file, int indent, const char *type,
2846 const char *local_var_prefix, enum remoting_phase phase,
2847 const var_t *var, unsigned int type_offset)
2849 const char *function;
2850 switch (phase)
2852 case PHASE_BUFFERSIZE:
2853 function = "BufferSize";
2854 break;
2855 case PHASE_MARSHAL:
2856 function = "Marshall";
2857 break;
2858 case PHASE_UNMARSHAL:
2859 function = "Unmarshall";
2860 break;
2861 case PHASE_FREE:
2862 function = "Free";
2863 break;
2864 default:
2865 assert(0);
2866 return;
2869 print_file(file, indent, "Ndr%s%s(\n", type, function);
2870 indent++;
2871 print_file(file, indent, "&__frame->_StubMsg,\n");
2872 print_file(file, indent, "%s%s%s%s%s,\n",
2873 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2874 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2875 local_var_prefix,
2876 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2877 var->name);
2878 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2879 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2880 if (phase == PHASE_UNMARSHAL)
2881 print_file(file, indent, "0);\n");
2882 indent--;
2885 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2886 enum remoting_phase phase, enum pass pass, const var_t *var,
2887 const char *varname)
2889 type_t *type = var->type;
2890 unsigned int size;
2891 unsigned int alignment = 0;
2892 unsigned char rtype;
2894 /* no work to do for other phases, buffer sizing is done elsewhere */
2895 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2896 return;
2898 rtype = is_ptr(type) ? type_pointer_get_ref(type)->type : type->type;
2900 switch (rtype)
2902 case RPC_FC_BYTE:
2903 case RPC_FC_CHAR:
2904 case RPC_FC_SMALL:
2905 case RPC_FC_USMALL:
2906 size = 1;
2907 alignment = 1;
2908 break;
2910 case RPC_FC_WCHAR:
2911 case RPC_FC_USHORT:
2912 case RPC_FC_SHORT:
2913 case RPC_FC_ENUM16:
2914 size = 2;
2915 alignment = 2;
2916 break;
2918 case RPC_FC_ULONG:
2919 case RPC_FC_LONG:
2920 case RPC_FC_ENUM32:
2921 case RPC_FC_FLOAT:
2922 case RPC_FC_ERROR_STATUS_T:
2923 size = 4;
2924 alignment = 4;
2925 break;
2927 case RPC_FC_HYPER:
2928 case RPC_FC_DOUBLE:
2929 size = 8;
2930 alignment = 8;
2931 break;
2933 case RPC_FC_IGNORE:
2934 case RPC_FC_BIND_PRIMITIVE:
2935 /* no marshalling needed */
2936 return;
2938 default:
2939 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2940 size = 0;
2943 if (phase == PHASE_MARSHAL)
2944 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2945 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2946 alignment - 1, alignment - 1);
2948 if (phase == PHASE_MARSHAL)
2950 print_file(file, indent, "*(");
2951 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2952 if (is_ptr(type))
2953 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2954 else
2955 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2956 fprintf(file, "%s%s", local_var_prefix, varname);
2957 fprintf(file, ";\n");
2959 else if (phase == PHASE_UNMARSHAL)
2961 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2962 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2963 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2964 print_file(file, indent, "{\n");
2965 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2966 print_file(file, indent, "}\n");
2967 if (pass == PASS_IN || pass == PASS_RETURN)
2968 print_file(file, indent, "");
2969 else
2970 print_file(file, indent, "*");
2971 fprintf(file, "%s%s", local_var_prefix, varname);
2972 if (pass == PASS_IN && is_ptr(type))
2973 fprintf(file, " = (");
2974 else
2975 fprintf(file, " = *(");
2976 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2977 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2980 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2981 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2982 fprintf(file, ");\n");
2985 /* returns whether the MaxCount, Offset or ActualCount members need to be
2986 * filled in for the specified phase */
2987 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2989 return (phase != PHASE_UNMARSHAL);
2992 expr_t *get_size_is_expr(const type_t *t, const char *name)
2994 expr_t *x = NULL;
2996 for ( ; is_array(t); t = type_array_get_element(t))
2997 if (type_array_has_conformance(t))
2999 if (!x)
3000 x = type_array_get_conformance(t);
3001 else
3002 error("%s: multidimensional conformant"
3003 " arrays not supported at the top level\n",
3004 name);
3007 return x;
3010 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3011 enum remoting_phase phase, const var_t *var)
3013 const type_t *type = var->type;
3014 /* get fundamental type for the argument */
3015 for (;;)
3017 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3018 break;
3019 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3020 break;
3021 else if (is_array(type) || is_string_type(var->attrs, type))
3023 if (is_conformance_needed_for_phase(phase) && is_array(type))
3025 if (type_array_has_conformance(type))
3027 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3028 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3029 fprintf(file, ";\n\n");
3031 if (type_array_has_variance(type))
3033 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3034 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3035 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3036 fprintf(file, ";\n\n");
3039 break;
3041 else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
3043 if (is_conformance_needed_for_phase(phase))
3045 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3046 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3047 fprintf(file, ";\n\n");
3049 break;
3051 else if (type->type == RPC_FC_IP || is_void(type))
3053 expr_t *iid;
3055 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3057 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3058 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3059 fprintf( file, ";\n\n" );
3061 break;
3063 else if (is_ptr(type))
3064 type = type_pointer_get_ref(type);
3065 else
3066 break;
3070 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3071 enum pass pass, enum remoting_phase phase, const var_t *var)
3073 int in_attr, out_attr, pointer_type;
3074 const type_t *type = var->type;
3075 unsigned char rtype;
3076 size_t start_offset = type->typestring_offset;
3078 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3079 if (!pointer_type)
3080 pointer_type = RPC_FC_RP;
3082 in_attr = is_attr(var->attrs, ATTR_IN);
3083 out_attr = is_attr(var->attrs, ATTR_OUT);
3084 if (!in_attr && !out_attr)
3085 in_attr = 1;
3087 if (phase != PHASE_FREE)
3088 switch (pass)
3090 case PASS_IN:
3091 if (!in_attr) return;
3092 break;
3093 case PASS_OUT:
3094 if (!out_attr) return;
3095 break;
3096 case PASS_RETURN:
3097 break;
3100 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3101 rtype = get_struct_type(type);
3103 if (is_context_handle(type))
3105 if (phase == PHASE_MARSHAL)
3107 if (pass == PASS_IN)
3109 /* if the context_handle attribute appears in the chain of types
3110 * without pointers being followed, then the context handle must
3111 * be direct, otherwise it is a pointer */
3112 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3113 print_file(file, indent, "NdrClientContextMarshall(\n");
3114 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3115 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3116 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3118 else
3120 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3121 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3122 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3123 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3124 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3127 else if (phase == PHASE_UNMARSHAL)
3129 if (pass == PASS_OUT)
3131 if (!in_attr)
3132 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3133 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3134 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3135 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3136 print_file(file, indent + 1, "__frame->_Handle);\n");
3138 else
3140 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3141 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3142 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3146 else if (is_user_type(var->type))
3148 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3150 else if (is_string_type(var->attrs, var->type))
3152 if (is_array(type) && !is_conformant_array(type))
3153 print_phase_function(file, indent, "NonConformantString", local_var_prefix,
3154 phase, var, start_offset);
3155 else
3157 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
3158 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
3159 start_offset - (is_conformant_array(type) ? 4 : 2));
3160 else
3161 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3162 phase, var, start_offset);
3165 else if (is_array(type))
3167 unsigned char tc = get_array_type( type );
3168 const char *array_type = "FixedArray";
3170 /* We already have the size_is expression since it's at the
3171 top level, but do checks for multidimensional conformant
3172 arrays. When we handle them, we'll need to extend this
3173 function to return a list, and then we'll actually use
3174 the return value. */
3175 get_size_is_expr(type, var->name);
3177 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3179 array_type = "VaryingArray";
3181 else if (tc == RPC_FC_CARRAY)
3183 array_type = "ConformantArray";
3185 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3187 array_type = (tc == RPC_FC_BOGUS_ARRAY
3188 ? "ComplexArray"
3189 : "ConformantVaryingArray");
3192 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3193 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3194 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3196 /* these are all unmarshalled by allocating memory */
3197 if (tc == RPC_FC_BOGUS_ARRAY ||
3198 tc == RPC_FC_CVARRAY ||
3199 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3200 (tc == RPC_FC_CARRAY && !in_attr))
3202 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3203 indent++;
3204 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3208 else if (!is_ptr(var->type) && is_base_type(rtype))
3210 if (phase != PHASE_FREE)
3211 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3213 else if (!is_ptr(var->type))
3215 switch (rtype)
3217 case RPC_FC_STRUCT:
3218 case RPC_FC_PSTRUCT:
3219 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3220 break;
3221 case RPC_FC_CSTRUCT:
3222 case RPC_FC_CPSTRUCT:
3223 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3224 break;
3225 case RPC_FC_CVSTRUCT:
3226 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3227 break;
3228 case RPC_FC_BOGUS_STRUCT:
3229 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3230 break;
3231 default:
3232 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
3235 else
3237 const type_t *ref = type_pointer_get_ref(type);
3238 if (type->type == RPC_FC_RP && is_base_type(ref->type))
3240 if (phase != PHASE_FREE)
3241 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3243 else if (type->type == RPC_FC_RP && get_struct_type(ref) == RPC_FC_STRUCT &&
3244 !is_user_type(ref))
3246 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3247 print_phase_function(file, indent, "SimpleStruct",
3248 local_var_prefix, phase, var,
3249 ref->typestring_offset);
3251 else
3253 if (ref->type == RPC_FC_IP)
3254 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3255 else
3256 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3259 fprintf(file, "\n");
3262 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3263 enum pass pass, enum remoting_phase phase)
3265 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3267 unsigned int size = get_function_buffer_size( func, pass );
3268 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3271 if (pass == PASS_RETURN)
3273 var_t var;
3274 var = *func;
3275 var.type = type_function_get_rettype(func->type);
3276 var.name = xstrdup( "_RetVal" );
3277 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3278 free( var.name );
3280 else
3282 const var_t *var;
3283 if (!type_get_function_args(func->type))
3284 return;
3285 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3286 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3291 size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3293 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3297 size_t get_size_procformatstring_func(const var_t *func)
3299 const var_t *var;
3300 size_t size = 0;
3302 /* argument list size */
3303 if (type_get_function_args(func->type))
3304 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3305 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3307 /* return value size */
3308 if (is_void(type_function_get_rettype(func->type)))
3309 size += 2; /* FC_END and FC_PAD */
3310 else
3311 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3313 return size;
3316 size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3318 const statement_t *stmt;
3319 size_t size = 1;
3321 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3323 const type_t *iface;
3324 const statement_t *stmt_func;
3326 if (stmt->type == STMT_LIBRARY)
3328 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3329 continue;
3331 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3332 continue;
3334 iface = stmt->u.type;
3335 if (!pred(iface))
3336 continue;
3338 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3340 const var_t *func = stmt_func->u.var;
3341 if (!is_local(func->attrs))
3342 size += get_size_procformatstring_func( func );
3345 return size;
3348 size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3350 set_all_tfswrite(FALSE);
3351 return process_tfs(NULL, stmts, pred);
3354 void declare_stub_args( FILE *file, int indent, const var_t *func )
3356 int in_attr, out_attr;
3357 int i = 0;
3358 const var_t *var;
3360 /* declare return value '_RetVal' */
3361 if (!is_void(type_function_get_rettype(func->type)))
3363 print_file(file, indent, "");
3364 write_type_decl_left(file, type_function_get_rettype(func->type));
3365 fprintf(file, " _RetVal;\n");
3368 if (!type_get_function_args(func->type))
3369 return;
3371 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3373 int is_string = is_string_type(var->attrs, var->type);
3375 in_attr = is_attr(var->attrs, ATTR_IN);
3376 out_attr = is_attr(var->attrs, ATTR_OUT);
3377 if (!out_attr && !in_attr)
3378 in_attr = 1;
3380 if (is_context_handle(var->type))
3381 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3382 else
3384 if (!in_attr && !is_conformant_array(var->type) && !is_string)
3386 type_t *type_to_print;
3387 print_file(file, indent, "");
3388 if (var->type->declarray)
3389 type_to_print = var->type;
3390 else
3391 type_to_print = type_pointer_get_ref(var->type);
3392 write_type_decl(file, type_to_print, "_W%u", i++);
3393 fprintf(file, ";\n");
3396 print_file(file, indent, "");
3397 write_type_decl_left(file, var->type);
3398 fprintf(file, " ");
3399 if (var->type->declarray) {
3400 fprintf(file, "(*%s)", var->name);
3401 } else
3402 fprintf(file, "%s", var->name);
3403 write_type_right(file, var->type, FALSE);
3404 fprintf(file, ";\n");
3406 if (decl_indirect(var->type))
3407 print_file(file, indent, "void *_p_%s;\n", var->name);
3413 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3415 int in_attr, out_attr;
3416 int i = 0, sep = 0;
3417 const var_t *var;
3419 if (!type_get_function_args(func->type))
3420 return;
3422 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3424 int is_string = is_string_type(var->attrs, var->type);
3425 in_attr = is_attr(var->attrs, ATTR_IN);
3426 out_attr = is_attr(var->attrs, ATTR_OUT);
3427 if (!out_attr && !in_attr)
3428 in_attr = 1;
3430 if (!in_attr)
3432 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3434 if (is_context_handle(var->type))
3436 fprintf(file, " = NdrContextHandleInitialize(\n");
3437 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3438 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3439 var->type->typestring_offset);
3441 else if (is_array(var->type) &&
3442 type_array_has_conformance(var->type))
3444 unsigned int size, align = 0;
3445 type_t *type = var->type;
3447 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3448 for ( ;
3449 is_array(type) && type_array_has_conformance(type);
3450 type = type_array_get_element(type))
3452 write_expr(file, type_array_get_conformance(type), TRUE,
3453 TRUE, NULL, NULL, local_var_prefix);
3454 fprintf(file, " * ");
3456 size = type_memsize(type, &align);
3457 fprintf(file, "%u);\n", size);
3459 else if (!is_string)
3461 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3462 if (is_ptr(var->type) && !last_ptr(var->type))
3463 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3464 i++;
3467 sep = 1;
3470 if (sep)
3471 fprintf(file, "\n");
3475 int write_expr_eval_routines(FILE *file, const char *iface)
3477 static const char *var_name = "pS";
3478 static const char *var_name_expr = "pS->";
3479 int result = 0;
3480 struct expr_eval_routine *eval;
3481 unsigned short callback_offset = 0;
3483 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3485 const char *name = eval->structure->name;
3486 result = 1;
3488 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3489 iface, name, callback_offset);
3490 print_file(file, 0, "{\n");
3491 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3492 name, var_name, name, eval->baseoff);
3493 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3494 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3495 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3496 fprintf(file, ";\n");
3497 print_file(file, 0, "}\n\n");
3498 callback_offset++;
3500 return result;
3503 void write_expr_eval_routine_list(FILE *file, const char *iface)
3505 struct expr_eval_routine *eval;
3506 struct expr_eval_routine *cursor;
3507 unsigned short callback_offset = 0;
3509 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3510 fprintf(file, "{\n");
3512 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3514 const char *name = eval->structure->name;
3515 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3516 callback_offset++;
3517 list_remove(&eval->entry);
3518 free(eval);
3521 fprintf(file, "};\n\n");
3524 void write_user_quad_list(FILE *file)
3526 user_type_t *ut;
3528 if (list_empty(&user_type_list))
3529 return;
3531 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3532 fprintf(file, "{\n");
3533 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3535 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3536 print_file(file, 1, "{\n");
3537 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3538 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3539 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3540 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3541 print_file(file, 1, "}%s\n", sep);
3543 fprintf(file, "};\n\n");
3546 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3548 const struct str_list_entry_t *endpoint;
3549 const char *p;
3551 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3552 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3553 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3555 print_file( f, 1, "{ (const unsigned char *)\"" );
3556 for (p = endpoint->str; *p && *p != ':'; p++)
3558 if (*p == '"' || *p == '\\') fputc( '\\', f );
3559 fputc( *p, f );
3561 if (!*p) goto error;
3562 if (p[1] != '[') goto error;
3564 fprintf( f, "\", (const unsigned char *)\"" );
3565 for (p += 2; *p && *p != ']'; p++)
3567 if (*p == '"' || *p == '\\') fputc( '\\', f );
3568 fputc( *p, f );
3570 if (*p != ']') goto error;
3571 fprintf( f, "\" },\n" );
3573 print_file( f, 0, "};\n\n" );
3574 return;
3576 error:
3577 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3580 void write_exceptions( FILE *file )
3582 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3583 fprintf( file, "\n");
3584 fprintf( file, "#include \"wine/exception.h\"\n");
3585 fprintf( file, "#undef RpcTryExcept\n");
3586 fprintf( file, "#undef RpcExcept\n");
3587 fprintf( file, "#undef RpcEndExcept\n");
3588 fprintf( file, "#undef RpcTryFinally\n");
3589 fprintf( file, "#undef RpcFinally\n");
3590 fprintf( file, "#undef RpcEndFinally\n");
3591 fprintf( file, "#undef RpcExceptionCode\n");
3592 fprintf( file, "#undef RpcAbnormalTermination\n");
3593 fprintf( file, "\n");
3594 fprintf( file, "struct __exception_frame;\n");
3595 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3596 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3597 fprintf( file, "\n");
3598 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3599 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3600 fprintf( file, " __filter_func filter; \\\n");
3601 fprintf( file, " __finally_func finally; \\\n");
3602 fprintf( file, " sigjmp_buf jmp; \\\n");
3603 fprintf( file, " DWORD code; \\\n");
3604 fprintf( file, " unsigned char abnormal_termination; \\\n");
3605 fprintf( file, " unsigned char filter_level; \\\n");
3606 fprintf( file, " unsigned char finally_level;\n");
3607 fprintf( file, "\n");
3608 fprintf( file, "struct __exception_frame\n{\n");
3609 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3610 fprintf( file, "};\n");
3611 fprintf( file, "\n");
3612 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3613 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3614 fprintf( file, " CONTEXT *context,\n");
3615 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3616 fprintf( file, "{\n");
3617 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3618 fprintf( file, "\n");
3619 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3620 fprintf( file, " {\n" );
3621 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3622 fprintf( file, " {\n" );
3623 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3624 fprintf( file, " exc_frame->finally( exc_frame );\n");
3625 fprintf( file, " }\n" );
3626 fprintf( file, " return ExceptionContinueSearch;\n");
3627 fprintf( file, " }\n" );
3628 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3629 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3630 fprintf( file, " {\n");
3631 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3632 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3633 fprintf( file, " {\n");
3634 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3635 fprintf( file, " exc_frame->finally( exc_frame );\n");
3636 fprintf( file, " __wine_pop_frame( frame );\n");
3637 fprintf( file, " }\n");
3638 fprintf( file, " exc_frame->filter_level = 0;\n");
3639 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3640 fprintf( file, " }\n");
3641 fprintf( file, " return ExceptionContinueSearch;\n");
3642 fprintf( file, "}\n");
3643 fprintf( file, "\n");
3644 fprintf( file, "#define RpcTryExcept \\\n");
3645 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3646 fprintf( file, " { \\\n");
3647 fprintf( file, " if (!__frame->finally_level) \\\n" );
3648 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3649 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3650 fprintf( file, "\n");
3651 fprintf( file, "#define RpcExcept(expr) \\\n");
3652 fprintf( file, " if (!__frame->finally_level) \\\n" );
3653 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3654 fprintf( file, " __frame->filter_level = 0; \\\n" );
3655 fprintf( file, " } \\\n");
3656 fprintf( file, " else \\\n");
3657 fprintf( file, "\n");
3658 fprintf( file, "#define RpcEndExcept\n");
3659 fprintf( file, "\n");
3660 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3661 fprintf( file, "\n");
3662 fprintf( file, "#define RpcTryFinally \\\n");
3663 fprintf( file, " if (!__frame->filter_level) \\\n");
3664 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3665 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3666 fprintf( file, "\n");
3667 fprintf( file, "#define RpcFinally \\\n");
3668 fprintf( file, " if (!__frame->filter_level) \\\n");
3669 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3670 fprintf( file, " __frame->finally_level = 0;\n");
3671 fprintf( file, "\n");
3672 fprintf( file, "#define RpcEndFinally\n");
3673 fprintf( file, "\n");
3674 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3675 fprintf( file, "\n");
3676 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3677 fprintf( file, " do { \\\n");
3678 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3679 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3680 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3681 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3682 fprintf( file, " __frame->filter_level = 0; \\\n");
3683 fprintf( file, " __frame->finally_level = 0; \\\n");
3684 fprintf( file, " } while (0)\n");
3685 fprintf( file, "\n");
3686 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3687 fprintf( file, "\n");
3688 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3689 fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
3690 fprintf( file, "\n");
3691 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");