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
23 #include "wine/port.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
57 const type_t *structure;
62 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
63 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
64 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
65 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
66 const char *name, int write_ptr, unsigned int *tfsoff);
67 static const var_t *find_array_or_string_in_struct(const type_t *type);
68 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
69 type_t *type, int toplevel_param,
70 const char *name, unsigned int *typestring_offset);
72 const char *string_of_type(unsigned char type)
76 case RPC_FC_BYTE: return "FC_BYTE";
77 case RPC_FC_CHAR: return "FC_CHAR";
78 case RPC_FC_SMALL: return "FC_SMALL";
79 case RPC_FC_USMALL: return "FC_USMALL";
80 case RPC_FC_WCHAR: return "FC_WCHAR";
81 case RPC_FC_SHORT: return "FC_SHORT";
82 case RPC_FC_USHORT: return "FC_USHORT";
83 case RPC_FC_LONG: return "FC_LONG";
84 case RPC_FC_ULONG: return "FC_ULONG";
85 case RPC_FC_FLOAT: return "FC_FLOAT";
86 case RPC_FC_HYPER: return "FC_HYPER";
87 case RPC_FC_DOUBLE: return "FC_DOUBLE";
88 case RPC_FC_ENUM16: return "FC_ENUM16";
89 case RPC_FC_ENUM32: return "FC_ENUM32";
90 case RPC_FC_IGNORE: return "FC_IGNORE";
91 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
92 case RPC_FC_RP: return "FC_RP";
93 case RPC_FC_UP: return "FC_UP";
94 case RPC_FC_OP: return "FC_OP";
95 case RPC_FC_FP: return "FC_FP";
96 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
97 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
98 case RPC_FC_STRUCT: return "FC_STRUCT";
99 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
100 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
101 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
102 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
103 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
104 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
105 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
106 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
107 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
108 case RPC_FC_CARRAY: return "FC_CARRAY";
109 case RPC_FC_CVARRAY: return "FC_CVARRAY";
110 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
111 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
112 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
113 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
114 case RPC_FC_POINTER: return "FC_POINTER";
115 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
116 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
117 case RPC_FC_CSTRING: return "FC_CSTRING";
118 case RPC_FC_WSTRING: return "FC_WSTRING";
120 error("string_of_type: unknown type 0x%02x\n", type);
125 unsigned char get_basic_fc(const type_t *type)
127 int sign = type_basic_get_sign(type);
128 switch (type_basic_get_type(type))
130 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
131 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
132 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
133 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
134 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
135 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
136 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
137 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
138 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
139 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
140 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
141 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
142 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
147 static inline unsigned int clamp_align(unsigned int align)
149 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
150 if(align > packing) align = packing;
154 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
159 assert(is_ptr(type) || is_array(type));
161 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
165 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
167 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
174 else if (is_ptr(type))
175 return type_pointer_get_default_fc(type);
177 return type_array_get_ptr_default_fc(type);
180 static unsigned char get_enum_fc(const type_t *type)
182 assert(type_get_type(type) == TYPE_ENUM);
183 if (is_aliaschain_attr(type, ATTR_V1ENUM))
184 return RPC_FC_ENUM32;
186 return RPC_FC_ENUM16;
189 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
191 if (is_user_type(type))
192 return TGT_USER_TYPE;
194 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
195 return TGT_CTXT_HANDLE;
197 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
200 switch (type_get_type(type))
207 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
208 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
209 return TGT_IFACE_POINTER;
210 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
211 return TGT_CTXT_HANDLE_POINTER;
216 case TYPE_ENCAPSULATED_UNION:
232 unsigned char get_struct_fc(const type_t *type)
235 int has_conformance = 0;
236 int has_variance = 0;
240 fields = type_struct_get_fields(type);
242 if (get_padding(fields))
243 return RPC_FC_BOGUS_STRUCT;
245 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
247 type_t *t = field->type;
248 enum typegen_type typegen_type;
250 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
252 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
254 if (is_string_type(field->attrs, field->type))
256 if (is_conformant_array(t))
262 if (is_array(type_array_get_element(field->type)))
263 return RPC_FC_BOGUS_STRUCT;
265 if (type_array_has_conformance(field->type))
268 if (list_next(fields, &field->entry))
269 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
272 if (type_array_has_variance(t))
275 t = type_array_get_element(t);
276 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
279 switch (typegen_type)
282 case TGT_IFACE_POINTER:
283 return RPC_FC_BOGUS_STRUCT;
287 if (get_enum_fc(t) == RPC_FC_ENUM16)
288 return RPC_FC_BOGUS_STRUCT;
292 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
293 return RPC_FC_BOGUS_STRUCT;
297 return RPC_FC_BOGUS_STRUCT;
300 unsigned char fc = get_struct_fc(t);
305 case RPC_FC_CVSTRUCT:
311 case RPC_FC_CPSTRUCT:
313 if (list_next( fields, &field->entry ))
314 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
321 if (list_next( fields, &field->entry ))
322 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
331 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
332 /* fallthru - treat it as complex */
334 /* as soon as we see one of these these members, it's bogus... */
335 case RPC_FC_BOGUS_STRUCT:
336 return RPC_FC_BOGUS_STRUCT;
341 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
343 case TGT_CTXT_HANDLE:
344 case TGT_CTXT_HANDLE_POINTER:
345 /* checking after parsing should mean that we don't get here. if we do,
346 * it's a checker bug */
353 if ( has_conformance )
354 return RPC_FC_CVSTRUCT;
356 return RPC_FC_BOGUS_STRUCT;
358 if( has_conformance && has_pointer )
359 return RPC_FC_CPSTRUCT;
360 if( has_conformance )
361 return RPC_FC_CSTRUCT;
363 return RPC_FC_PSTRUCT;
364 return RPC_FC_STRUCT;
367 unsigned char get_array_fc(const type_t *type)
370 const expr_t *size_is;
371 const type_t *elem_type;
373 elem_type = type_array_get_element(type);
374 size_is = type_array_get_conformance(type);
378 unsigned int align = 0;
379 unsigned int size = type_memsize(elem_type, &align);
380 if (size * type_array_get_dim(type) > 0xffffuL)
381 fc = RPC_FC_LGFARRAY;
383 fc = RPC_FC_SMFARRAY;
388 if (type_array_has_variance(type))
390 if (fc == RPC_FC_SMFARRAY)
391 fc = RPC_FC_SMVARRAY;
392 else if (fc == RPC_FC_LGFARRAY)
393 fc = RPC_FC_LGVARRAY;
394 else if (fc == RPC_FC_CARRAY)
398 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
401 fc = RPC_FC_BOGUS_ARRAY;
404 switch (get_struct_fc(elem_type))
406 case RPC_FC_BOGUS_STRUCT:
407 fc = RPC_FC_BOGUS_ARRAY;
412 /* is 16-bit enum - if so, wire size differs from mem size and so
413 * the array cannot be block copied, which means the array is complex */
414 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
415 fc = RPC_FC_BOGUS_ARRAY;
418 case TGT_IFACE_POINTER:
419 fc = RPC_FC_BOGUS_ARRAY;
422 /* ref pointers cannot just be block copied. unique pointers to
423 * interfaces need special treatment. either case means the array is
425 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
426 fc = RPC_FC_BOGUS_ARRAY;
429 case TGT_CTXT_HANDLE:
430 case TGT_CTXT_HANDLE_POINTER:
434 /* nothing to do for everything else */
441 int is_struct(unsigned char type)
448 case RPC_FC_CPSTRUCT:
449 case RPC_FC_CVSTRUCT:
450 case RPC_FC_BOGUS_STRUCT:
457 static int is_non_complex_struct(const type_t *type)
459 return (type_get_type(type) == TYPE_STRUCT &&
460 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
463 static int type_has_pointers(const type_t *type)
465 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
472 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
475 var_list_t *fields = type_struct_get_fields(type);
477 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
479 if (type_has_pointers(field->type))
488 fields = type_union_get_cases(type);
489 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
491 if (field->type && type_has_pointers(field->type))
496 case TGT_CTXT_HANDLE:
497 case TGT_CTXT_HANDLE_POINTER:
499 case TGT_IFACE_POINTER:
509 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
512 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
517 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
522 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
525 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
528 var_list_t *fields = type_struct_get_fields(type);
530 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
532 if (type_has_full_pointer(field->type, field->attrs, FALSE))
541 fields = type_union_get_cases(type);
542 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
544 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
549 case TGT_CTXT_HANDLE:
550 case TGT_CTXT_HANDLE_POINTER:
552 case TGT_IFACE_POINTER:
562 static unsigned short user_type_offset(const char *name)
565 unsigned short off = 0;
566 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
568 if (strcmp(name, ut->name) == 0)
572 error("user_type_offset: couldn't find type (%s)\n", name);
576 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
578 type->typestring_offset = offset;
579 if (file) type->tfswrite = FALSE;
582 static void guard_rec(type_t *type)
584 /* types that contain references to themselves (like a linked list),
585 need to be shielded from infinite recursion when writing embedded
587 if (type->typestring_offset)
588 type->tfswrite = FALSE;
590 type->typestring_offset = 1;
593 static type_t *get_user_type(const type_t *t, const char **pname)
597 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
605 if (type_is_alias(t))
606 t = type_alias_get_aliasee(t);
612 int is_user_type(const type_t *t)
614 return get_user_type(t, NULL) != NULL;
617 static int is_embedded_complex(const type_t *type)
619 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
625 case TGT_IFACE_POINTER:
632 static const char *get_context_handle_type_name(const type_t *type)
636 is_ptr(t) || type_is_alias(t);
637 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
638 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
644 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
647 fprintf(file, "/* %2u */\n", typestring_offset); \
648 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
652 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
653 static void print_file(FILE *file, int indent, const char *format, ...)
656 va_start(va, format);
657 print(file, indent, format, va);
661 void print(FILE *file, int indent, const char *format, va_list va)
665 if (format[0] != '\n')
668 vfprintf(file, format, va);
673 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
675 if (decl_indirect(t))
677 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
678 local_var_prefix, n, local_var_prefix, n);
679 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
681 else if (is_ptr(t) || is_array(t))
682 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
685 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
689 if (!is_void(type_function_get_rettype(func->type)))
690 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
692 if (!type_get_function_args(func->type))
695 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
696 write_var_init(file, indent, var->type, var->name, local_var_prefix);
701 static void write_formatdesc(FILE *f, int indent, const char *str)
703 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
704 print_file(f, indent, "{\n");
705 print_file(f, indent + 1, "short Pad;\n");
706 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
707 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
708 print_file(f, indent, "\n");
711 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
715 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
716 get_size_typeformatstring(stmts, pred));
718 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
719 get_size_procformatstring(stmts, pred));
722 write_formatdesc(f, indent, "TYPE");
723 write_formatdesc(f, indent, "PROC");
725 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
726 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
727 print_file(f, indent, "\n");
730 int decl_indirect(const type_t *t)
734 return (type_get_type(t) != TYPE_BASIC &&
735 type_get_type(t) != TYPE_ENUM &&
736 type_get_type(t) != TYPE_POINTER &&
737 type_get_type(t) != TYPE_ARRAY);
740 static unsigned int write_procformatstring_type(FILE *file, int indent,
743 const attr_list_t *attrs,
748 int is_in = is_attr(attrs, ATTR_IN);
749 int is_out = is_attr(attrs, ATTR_OUT);
751 if (!is_in && !is_out) is_in = TRUE;
753 if (type_get_type(type) == TYPE_BASIC ||
754 type_get_type(type) == TYPE_ENUM)
759 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
761 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
763 if (type_get_type(type) == TYPE_ENUM)
765 fc = get_enum_fc(type);
769 fc = get_basic_fc(type);
771 if (fc == RPC_FC_BIND_PRIMITIVE)
775 print_file(file, indent, "0x%02x, /* %s */\n",
776 fc, string_of_type(fc));
777 size = 2; /* includes param type prefix */
782 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
783 else if (is_in && is_out)
784 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
786 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
788 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
790 print_file(file, indent, "0x01,\n");
791 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
792 size = 4; /* includes param type prefix */
797 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
799 const statement_t *stmt;
800 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
802 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
804 const statement_t *stmt_func;
805 if (!pred(stmt->u.type))
807 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
809 const var_t *func = stmt_func->u.var;
810 if (is_local(func->attrs)) continue;
811 /* emit argument data */
812 if (type_get_function_args(func->type))
815 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
816 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
819 /* emit return value data */
820 if (is_void(type_function_get_rettype(func->type)))
822 print_file(file, indent, "0x5b, /* FC_END */\n");
823 print_file(file, indent, "0x5c, /* FC_PAD */\n");
826 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
829 else if (stmt->type == STMT_LIBRARY)
830 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
834 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
838 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
839 print_file(file, indent, "{\n");
841 print_file(file, indent, "0,\n");
842 print_file(file, indent, "{\n");
845 write_procformatstring_stmts(file, indent, stmts, pred);
847 print_file(file, indent, "0x0\n");
849 print_file(file, indent, "}\n");
851 print_file(file, indent, "};\n");
852 print_file(file, indent, "\n");
855 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
859 if (type_get_type(type) == TYPE_BASIC)
860 fc = get_basic_fc(type);
861 else if (type_get_type(type) == TYPE_ENUM)
862 fc = get_enum_fc(type);
866 if (convert_to_signed_type)
882 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
883 *typestring_offset += 1;
887 /* write conformance / variance descriptor */
888 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
889 unsigned int baseoff, const type_t *type,
892 unsigned char operator_type = 0;
893 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
894 const char *conftype_string = "";
895 const char *operator_string = "no operators";
896 const expr_t *subexpr;
900 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
906 /* Top-level conformance calculations are done inline. */
907 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
908 RPC_FC_TOP_LEVEL_CONFORMANCE);
909 print_file (file, 2, "0x0,\n");
910 print_file (file, 2, "NdrFcShort(0x0),\n");
916 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
917 error("write_conf_or_var_desc: constant value %ld is greater than "
918 "the maximum constant size of %d\n", expr->cval,
919 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
921 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
922 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
923 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
924 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
929 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
931 conftype = RPC_FC_POINTER_CONFORMANCE;
932 conftype_string = "field pointer, ";
936 switch (subexpr->type)
939 subexpr = subexpr->ref;
940 operator_type = RPC_FC_DEREFERENCE;
941 operator_string = "FC_DEREFERENCE";
944 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
946 subexpr = subexpr->ref;
947 operator_type = RPC_FC_DIV_2;
948 operator_string = "FC_DIV_2";
952 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
954 subexpr = subexpr->ref;
955 operator_type = RPC_FC_MULT_2;
956 operator_string = "FC_MULT_2";
960 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
962 subexpr = subexpr->ref;
963 operator_type = RPC_FC_SUB_1;
964 operator_string = "FC_SUB_1";
968 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
970 subexpr = subexpr->ref;
971 operator_type = RPC_FC_ADD_1;
972 operator_string = "FC_ADD_1";
979 if (subexpr->type == EXPR_IDENTIFIER)
981 const type_t *correlation_variable = NULL;
982 unsigned char param_type = 0;
983 unsigned int offset = 0;
985 var_list_t *fields = type_struct_get_fields(structure);
987 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
989 unsigned int size = field_memsize( var->type, &offset );
990 if (var->name && !strcmp(var->name, subexpr->u.sval))
992 correlation_variable = var->type;
997 if (!correlation_variable)
998 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1001 correlation_variable = expr_resolve_type(NULL, structure, expr);
1005 if (type_get_type(correlation_variable) == TYPE_BASIC)
1007 switch (get_basic_fc(correlation_variable))
1011 param_type = RPC_FC_SMALL;
1015 param_type = RPC_FC_USMALL;
1019 param_type = RPC_FC_SHORT;
1022 param_type = RPC_FC_USHORT;
1025 param_type = RPC_FC_LONG;
1028 param_type = RPC_FC_ULONG;
1031 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1032 get_basic_fc(correlation_variable));
1035 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1037 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1038 param_type = RPC_FC_LONG;
1040 param_type = RPC_FC_SHORT;
1044 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1049 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1050 conftype | param_type, conftype_string, string_of_type(param_type));
1051 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1052 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1057 unsigned int callback_offset = 0;
1058 struct expr_eval_routine *eval;
1061 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1063 if (!strcmp (eval->structure->name, structure->name)
1064 && !compare_expr (eval->expr, expr))
1074 eval = xmalloc (sizeof(*eval));
1075 eval->structure = structure;
1076 eval->baseoff = baseoff;
1078 list_add_tail (&expr_eval_routines, &eval->entry);
1081 if (callback_offset > USHRT_MAX)
1082 error("Maximum number of callback routines reached\n");
1084 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1085 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1086 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1091 /* return size and start offset of a data field based on current offset */
1092 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1094 unsigned int align = 0;
1095 unsigned int size = type_memsize( type, &align );
1097 *offset = ROUND_SIZE( *offset, align );
1101 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1103 unsigned int size = 0;
1104 unsigned int max_align;
1107 if (!fields) return 0;
1108 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1110 unsigned int falign = 0;
1111 unsigned int fsize = type_memsize(v->type, &falign);
1112 if (*align < falign) *align = falign;
1113 falign = clamp_align(falign);
1114 size = ROUND_SIZE(size, falign);
1118 max_align = clamp_align(*align);
1119 size = ROUND_SIZE(size, max_align);
1124 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1126 unsigned int size, maxs = 0;
1127 unsigned int align = *pmaxa;
1130 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1132 /* we could have an empty default field with NULL type */
1135 size = type_memsize(v->type, &align);
1136 if (maxs < size) maxs = size;
1137 if (*pmaxa < align) *pmaxa = align;
1144 int get_padding(const var_list_t *fields)
1146 unsigned short offset = 0;
1147 unsigned int salign = 1;
1153 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1155 type_t *ft = f->type;
1156 unsigned int align = 0;
1157 unsigned int size = type_memsize(ft, &align);
1158 align = clamp_align(align);
1159 if (align > salign) salign = align;
1160 offset = ROUND_SIZE(offset, align);
1164 return ROUNDING(offset, salign);
1167 unsigned int type_memsize(const type_t *t, unsigned int *align)
1169 unsigned int size = 0;
1171 switch (type_get_type(t))
1174 switch (get_basic_fc(t))
1181 if (size > *align) *align = size;
1187 if (size > *align) *align = size;
1191 case RPC_FC_ERROR_STATUS_T:
1194 if (size > *align) *align = size;
1199 if (size > *align) *align = size;
1202 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1207 switch (get_enum_fc(t))
1212 if (size > *align) *align = size;
1215 error("type_memsize: Unknown enum type\n");
1220 size = fields_memsize(type_struct_get_fields(t), align);
1222 case TYPE_ENCAPSULATED_UNION:
1223 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1226 size = union_memsize(type_union_get_cases(t), align);
1229 assert( pointer_size );
1230 size = pointer_size;
1231 if (size > *align) *align = size;
1234 if (!type_array_is_decl_as_ptr(t))
1236 if (is_conformant_array(t))
1238 type_memsize(type_array_get_element(t), align);
1242 size = type_array_get_dim(t) *
1243 type_memsize(type_array_get_element(t), align);
1245 else /* declared as a pointer */
1247 assert( pointer_size );
1248 size = pointer_size;
1249 if (size > *align) *align = size;
1252 case TYPE_INTERFACE:
1258 /* these types should not be encountered here due to language
1259 * restrictions (interface, void, coclass, module), logical
1260 * restrictions (alias - due to type_get_type call above) or
1261 * checking restrictions (function). */
1268 int is_full_pointer_function(const var_t *func)
1271 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1273 if (!type_get_function_args(func->type))
1275 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1276 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1281 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1283 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1284 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1285 fprintf(file, "\n");
1288 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1290 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1291 fprintf(file, "\n");
1294 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1297 unsigned int offset,
1298 unsigned int *typeformat_offset)
1300 unsigned int start_offset = *typeformat_offset;
1301 short reloff = offset - (*typeformat_offset + 2);
1302 int in_attr, out_attr;
1304 unsigned char flags = 0;
1306 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1308 in_attr = is_attr(attrs, ATTR_IN);
1309 out_attr = is_attr(attrs, ATTR_OUT);
1310 if (!in_attr && !out_attr) in_attr = 1;
1312 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1313 flags |= RPC_FC_P_ONSTACK;
1315 if (is_ptr(type) && !last_ptr(type))
1316 flags |= RPC_FC_P_DEREF;
1318 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1321 string_of_type(pointer_type));
1324 if (flags & RPC_FC_P_ONSTACK)
1325 fprintf(file, " [allocated_on_stack]");
1326 if (flags & RPC_FC_P_DEREF)
1327 fprintf(file, " [pointer_deref]");
1328 fprintf(file, " */\n");
1331 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1332 *typeformat_offset += 4;
1334 return start_offset;
1337 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1340 unsigned char pointer_fc;
1343 /* for historical reasons, write_simple_pointer also handled string types,
1344 * but no longer does. catch bad uses of the function with this check */
1345 if (is_string_type(attrs, type))
1346 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1348 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1350 ref = type_pointer_get_ref(type);
1351 if (type_get_type(ref) == TYPE_ENUM)
1352 fc = get_enum_fc(ref);
1354 fc = get_basic_fc(ref);
1356 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1357 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1358 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1359 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1363 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1365 print_file(file, 0, "/* %u (", tfsoff);
1366 write_type_decl(file, t, NULL);
1367 print_file(file, 0, ") */\n");
1370 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1371 type_t *type, int toplevel_param,
1372 unsigned int *typestring_offset)
1374 unsigned int offset = *typestring_offset;
1375 type_t *ref = type_pointer_get_ref(type);
1377 print_start_tfs_comment(file, type, offset);
1378 update_tfsoff(type, offset, file);
1380 if (ref->typestring_offset)
1381 write_nonsimple_pointer(file, attrs, type,
1383 type_pointer_get_ref(type)->typestring_offset,
1385 else if (type_get_type(ref) == TYPE_BASIC ||
1386 type_get_type(ref) == TYPE_ENUM)
1387 *typestring_offset += write_simple_pointer(file, attrs, type,
1393 static int processed(const type_t *type)
1395 return type->typestring_offset && !type->tfswrite;
1398 static int user_type_has_variable_size(const type_t *t)
1402 else if (type_get_type(t) == TYPE_STRUCT)
1404 switch (get_struct_fc(t))
1406 case RPC_FC_PSTRUCT:
1407 case RPC_FC_CSTRUCT:
1408 case RPC_FC_CPSTRUCT:
1409 case RPC_FC_CVSTRUCT:
1413 /* Note: Since this only applies to user types, we can't have a conformant
1414 array here, and strings should get filed under pointer in this case. */
1418 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1420 unsigned int start, absoff, flags;
1421 unsigned int align = 0, ualign = 0;
1422 const char *name = NULL;
1423 type_t *utype = get_user_type(type, &name);
1424 unsigned int usize = type_memsize(utype, &ualign);
1425 unsigned int size = type_memsize(type, &align);
1426 unsigned short funoff = user_type_offset(name);
1431 if(user_type_has_variable_size(utype)) usize = 0;
1433 if (type_get_type(utype) == TYPE_BASIC ||
1434 type_get_type(utype) == TYPE_ENUM)
1438 if (type_get_type(utype) == TYPE_ENUM)
1439 fc = get_enum_fc(utype);
1441 fc = get_basic_fc(utype);
1444 print_start_tfs_comment(file, utype, absoff);
1445 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1446 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1451 if (!processed(utype))
1452 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1453 absoff = utype->typestring_offset;
1456 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1458 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1464 update_tfsoff(type, start, file);
1465 print_start_tfs_comment(file, type, start);
1466 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1467 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1468 flags | (ualign - 1), ualign - 1, flags);
1469 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1470 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1471 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1473 reloff = absoff - *tfsoff;
1474 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1478 static void write_member_type(FILE *file, const type_t *cont,
1479 int cont_is_complex, const attr_list_t *attrs,
1480 const type_t *type, unsigned int *corroff,
1481 unsigned int *tfsoff)
1483 if (is_embedded_complex(type) && !is_conformant_array(type))
1485 unsigned int absoff;
1488 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1495 absoff = type->typestring_offset;
1497 reloff = absoff - (*tfsoff + 2);
1499 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1500 /* FIXME: actually compute necessary padding */
1501 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1502 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1503 reloff, reloff, absoff);
1506 else if (is_ptr(type) || is_conformant_array(type))
1508 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1509 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1512 else if (!write_base_type(file, type, TRUE, tfsoff))
1513 error("Unsupported member type %d\n", type_get_type(type));
1516 static void write_array_element_type(FILE *file, const type_t *type,
1517 int cont_is_complex, unsigned int *tfsoff)
1519 type_t *elem = type_array_get_element(type);
1521 if (!is_embedded_complex(elem) && is_ptr(elem))
1523 type_t *ref = type_pointer_get_ref(elem);
1527 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1530 if (!is_string_type(NULL, elem) &&
1531 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1533 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1537 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1540 static void write_end(FILE *file, unsigned int *tfsoff)
1542 if (*tfsoff % 2 == 0)
1544 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1547 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1551 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1553 unsigned int offset = 0;
1554 var_list_t *fs = type_struct_get_fields(type);
1557 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1559 type_t *ft = f->type;
1560 unsigned int size = field_memsize( ft, &offset );
1561 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1564 unsigned int absoff = ft->typestring_offset;
1565 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1566 absoff += 8; /* we already have a corr descr, skip it */
1567 reloff = absoff - (*tfsoff + 6);
1568 print_file(file, 0, "/* %d */\n", *tfsoff);
1569 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1570 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1571 write_conf_or_var_desc(file, current_structure, offset, ft,
1572 get_attrp(f->attrs, ATTR_SWITCHIS));
1573 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1574 reloff, reloff, absoff);
1581 static int write_pointer_description_offsets(
1582 FILE *file, const attr_list_t *attrs, type_t *type,
1583 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1584 unsigned int *typestring_offset)
1589 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1590 (is_array(type) && type_array_is_decl_as_ptr(type)))
1592 if (offset_in_memory && offset_in_buffer)
1594 unsigned int memsize;
1596 /* pointer instance */
1597 /* FIXME: sometimes from end of structure, sometimes from beginning */
1598 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1599 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1602 memsize = type_memsize(type, &align);
1603 *offset_in_memory += memsize;
1604 /* increment these separately as in the case of conformant (varying)
1605 * structures these start at different values */
1606 *offset_in_buffer += memsize;
1608 *typestring_offset += 4;
1612 type_t *ref = type_pointer_get_ref(type);
1614 if (is_string_type(attrs, type))
1615 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1616 else if (processed(ref))
1617 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1618 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1619 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1621 error("write_pointer_description_offsets: type format string unknown\n");
1625 unsigned int offset = type->typestring_offset;
1626 /* skip over the pointer that is written for strings, since a
1627 * pointer has to be written in-place here */
1628 if (is_string_type(attrs, type))
1630 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1638 return write_pointer_description_offsets(
1639 file, attrs, type_array_get_element(type), offset_in_memory,
1640 offset_in_buffer, typestring_offset);
1642 else if (is_non_complex_struct(type))
1644 /* otherwise search for interesting fields to parse */
1646 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1648 if (offset_in_memory && offset_in_buffer)
1650 unsigned int padding;
1652 type_memsize(v->type, &align);
1653 padding = ROUNDING(*offset_in_memory, align);
1654 *offset_in_memory += padding;
1655 *offset_in_buffer += padding;
1657 written += write_pointer_description_offsets(
1658 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1664 if (offset_in_memory && offset_in_buffer)
1666 unsigned int memsize;
1668 memsize = type_memsize(type, &align);
1669 *offset_in_memory += memsize;
1670 /* increment these separately as in the case of conformant (varying)
1671 * structures these start at different values */
1672 *offset_in_buffer += memsize;
1679 static int write_no_repeat_pointer_descriptions(
1680 FILE *file, const attr_list_t *attrs, type_t *type,
1681 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1682 unsigned int *typestring_offset)
1688 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1690 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1691 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1692 *typestring_offset += 2;
1694 return write_pointer_description_offsets(file, attrs, type,
1695 offset_in_memory, offset_in_buffer, typestring_offset);
1698 if (is_non_complex_struct(type))
1701 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1703 if (offset_in_memory && offset_in_buffer)
1705 unsigned int padding;
1707 type_memsize(v->type, &align);
1708 padding = ROUNDING(*offset_in_memory, align);
1709 *offset_in_memory += padding;
1710 *offset_in_buffer += padding;
1712 written += write_no_repeat_pointer_descriptions(
1713 file, v->attrs, v->type,
1714 offset_in_memory, offset_in_buffer, typestring_offset);
1719 unsigned int memsize;
1721 memsize = type_memsize(type, &align);
1722 *offset_in_memory += memsize;
1723 /* increment these separately as in the case of conformant (varying)
1724 * structures these start at different values */
1725 *offset_in_buffer += memsize;
1731 /* Note: if file is NULL return value is number of pointers to write, else
1732 * it is the number of type format characters written */
1733 static int write_fixed_array_pointer_descriptions(
1734 FILE *file, const attr_list_t *attrs, type_t *type,
1735 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1736 unsigned int *typestring_offset)
1739 int pointer_count = 0;
1741 if (type_get_type(type) == TYPE_ARRAY &&
1742 !type_array_has_conformance(type) && !type_array_has_variance(type))
1744 unsigned int temp = 0;
1745 /* unfortunately, this needs to be done in two passes to avoid
1746 * writing out redundant FC_FIXED_REPEAT descriptions */
1747 pointer_count = write_pointer_description_offsets(
1748 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1749 if (pointer_count > 0)
1751 unsigned int increment_size;
1752 unsigned int offset_of_array_pointer_mem = 0;
1753 unsigned int offset_of_array_pointer_buf = 0;
1756 increment_size = type_memsize(type_array_get_element(type), &align);
1758 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1759 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1760 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1761 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1762 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1763 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1764 *typestring_offset += 10;
1766 pointer_count = write_pointer_description_offsets(
1767 file, attrs, type, &offset_of_array_pointer_mem,
1768 &offset_of_array_pointer_buf, typestring_offset);
1771 else if (type_get_type(type) == TYPE_STRUCT)
1774 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1776 if (offset_in_memory && offset_in_buffer)
1778 unsigned int padding;
1780 type_memsize(v->type, &align);
1781 padding = ROUNDING(*offset_in_memory, align);
1782 *offset_in_memory += padding;
1783 *offset_in_buffer += padding;
1785 pointer_count += write_fixed_array_pointer_descriptions(
1786 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1792 if (offset_in_memory && offset_in_buffer)
1794 unsigned int memsize;
1796 memsize = type_memsize(type, &align);
1797 *offset_in_memory += memsize;
1798 /* increment these separately as in the case of conformant (varying)
1799 * structures these start at different values */
1800 *offset_in_buffer += memsize;
1804 return pointer_count;
1807 /* Note: if file is NULL return value is number of pointers to write, else
1808 * it is the number of type format characters written */
1809 static int write_conformant_array_pointer_descriptions(
1810 FILE *file, const attr_list_t *attrs, type_t *type,
1811 unsigned int offset_in_memory, unsigned int *typestring_offset)
1814 int pointer_count = 0;
1816 if (is_conformant_array(type) && !type_array_has_variance(type))
1818 unsigned int temp = 0;
1819 /* unfortunately, this needs to be done in two passes to avoid
1820 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1821 pointer_count = write_pointer_description_offsets(
1822 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1823 if (pointer_count > 0)
1825 unsigned int increment_size;
1826 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1827 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1830 increment_size = type_memsize(type_array_get_element(type), &align);
1832 if (increment_size > USHRT_MAX)
1833 error("array size of %u bytes is too large\n", increment_size);
1835 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1836 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1837 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1838 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1839 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1840 *typestring_offset += 8;
1842 pointer_count = write_pointer_description_offsets(
1843 file, attrs, type_array_get_element(type),
1844 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1849 return pointer_count;
1852 /* Note: if file is NULL return value is number of pointers to write, else
1853 * it is the number of type format characters written */
1854 static int write_varying_array_pointer_descriptions(
1855 FILE *file, const attr_list_t *attrs, type_t *type,
1856 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1857 unsigned int *typestring_offset)
1860 int pointer_count = 0;
1862 if (is_array(type) && type_array_has_variance(type))
1864 unsigned int temp = 0;
1865 /* unfortunately, this needs to be done in two passes to avoid
1866 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1867 pointer_count = write_pointer_description_offsets(
1868 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1869 if (pointer_count > 0)
1871 unsigned int increment_size;
1874 increment_size = type_memsize(type_array_get_element(type), &align);
1876 if (increment_size > USHRT_MAX)
1877 error("array size of %u bytes is too large\n", increment_size);
1879 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1880 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1881 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1882 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1883 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1884 *typestring_offset += 8;
1886 pointer_count = write_pointer_description_offsets(
1887 file, attrs, type_array_get_element(type), offset_in_memory,
1888 offset_in_buffer, typestring_offset);
1891 else if (type_get_type(type) == TYPE_STRUCT)
1894 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1896 if (offset_in_memory && offset_in_buffer)
1898 unsigned int padding;
1900 if (is_array(v->type) && type_array_has_variance(v->type))
1902 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1903 /* skip over variance and offset in buffer */
1904 *offset_in_buffer += 8;
1908 type_memsize(v->type, &align);
1909 padding = ROUNDING(*offset_in_memory, align);
1910 *offset_in_memory += padding;
1911 *offset_in_buffer += padding;
1913 pointer_count += write_varying_array_pointer_descriptions(
1914 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1920 if (offset_in_memory && offset_in_buffer)
1922 unsigned int memsize;
1924 memsize = type_memsize(type, &align);
1925 *offset_in_memory += memsize;
1926 /* increment these separately as in the case of conformant (varying)
1927 * structures these start at different values */
1928 *offset_in_buffer += memsize;
1932 return pointer_count;
1935 static void write_pointer_description(FILE *file, type_t *type,
1936 unsigned int *typestring_offset)
1938 unsigned int offset_in_buffer;
1939 unsigned int offset_in_memory;
1941 /* pass 1: search for single instance of a pointer (i.e. don't descend
1943 if (!is_array(type))
1945 offset_in_memory = 0;
1946 offset_in_buffer = 0;
1947 write_no_repeat_pointer_descriptions(
1949 &offset_in_memory, &offset_in_buffer, typestring_offset);
1952 /* pass 2: search for pointers in fixed arrays */
1953 offset_in_memory = 0;
1954 offset_in_buffer = 0;
1955 write_fixed_array_pointer_descriptions(
1957 &offset_in_memory, &offset_in_buffer, typestring_offset);
1959 /* pass 3: search for pointers in conformant only arrays (but don't descend
1960 * into conformant varying or varying arrays) */
1961 if (is_conformant_array(type) &&
1962 (type_array_is_decl_as_ptr(type) || !current_structure))
1963 write_conformant_array_pointer_descriptions(
1964 file, NULL, type, 0, typestring_offset);
1965 else if (type_get_type(type) == TYPE_STRUCT &&
1966 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1968 unsigned int align = 0;
1969 type_t *carray = find_array_or_string_in_struct(type)->type;
1970 write_conformant_array_pointer_descriptions(
1972 type_memsize(type, &align),
1976 /* pass 4: search for pointers in varying arrays */
1977 offset_in_memory = 0;
1978 offset_in_buffer = 0;
1979 write_varying_array_pointer_descriptions(
1981 &offset_in_memory, &offset_in_buffer, typestring_offset);
1984 int is_declptr(const type_t *t)
1986 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
1989 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1990 type_t *type, int toplevel_param,
1991 const char *name, unsigned int *typestring_offset)
1993 unsigned int start_offset;
1994 unsigned char rtype;
1997 start_offset = *typestring_offset;
1998 update_tfsoff(type, start_offset, file);
2000 if (is_declptr(type))
2002 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2003 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2005 pointer_type = RPC_FC_RP;
2006 print_start_tfs_comment(file, type, *typestring_offset);
2007 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2008 pointer_type, flag, string_of_type(pointer_type),
2009 flag ? " [simple_pointer]" : "");
2010 *typestring_offset += 2;
2013 print_file(file, 2, "NdrFcShort(0x2),\n");
2014 *typestring_offset += 2;
2019 elem_type = type_array_get_element(type);
2021 elem_type = type_pointer_get_ref(type);
2023 if (type_get_type(elem_type) != TYPE_BASIC)
2025 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2026 return start_offset;
2029 rtype = get_basic_fc(elem_type);
2030 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2032 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2033 return start_offset;
2036 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2038 unsigned int dim = type_array_get_dim(type);
2040 /* FIXME: multi-dimensional array */
2042 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2043 name, 0xffffu, dim - 0xffffu);
2045 if (rtype == RPC_FC_WCHAR)
2046 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2048 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2049 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2050 *typestring_offset += 2;
2052 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2053 *typestring_offset += 2;
2055 return start_offset;
2057 else if (is_conformant_array(type))
2059 unsigned int align = 0;
2061 if (rtype == RPC_FC_WCHAR)
2062 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2064 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2065 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2066 *typestring_offset += 2;
2068 *typestring_offset += write_conf_or_var_desc(
2069 file, current_structure,
2070 (!type_array_is_decl_as_ptr(type) && current_structure
2071 ? type_memsize(current_structure, &align)
2073 type, type_array_get_conformance(type));
2075 return start_offset;
2079 if (rtype == RPC_FC_WCHAR)
2080 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2082 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2083 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2084 *typestring_offset += 2;
2086 return start_offset;
2090 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2091 const char *name, unsigned int *typestring_offset)
2093 const expr_t *length_is = type_array_get_variance(type);
2094 const expr_t *size_is = type_array_get_conformance(type);
2095 unsigned int align = 0;
2097 unsigned int start_offset;
2100 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2101 unsigned int baseoff
2102 = !type_array_is_decl_as_ptr(type) && current_structure
2103 ? type_memsize(current_structure, &align)
2107 pointer_type = RPC_FC_RP;
2109 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2112 has_pointer = type_has_pointers(type_array_get_element(type));
2115 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2116 fc = get_array_fc(type);
2118 start_offset = *typestring_offset;
2119 update_tfsoff(type, start_offset, file);
2120 print_start_tfs_comment(file, type, start_offset);
2121 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2122 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2123 *typestring_offset += 2;
2126 if (fc != RPC_FC_BOGUS_ARRAY)
2128 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2130 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2131 *typestring_offset += 4;
2135 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2136 *typestring_offset += 2;
2139 if (is_conformant_array(type))
2141 += write_conf_or_var_desc(file, current_structure, baseoff,
2144 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2146 unsigned int elalign = 0;
2147 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2148 unsigned int dim = type_array_get_dim(type);
2150 if (fc == RPC_FC_LGVARRAY)
2152 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2153 *typestring_offset += 4;
2157 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2158 *typestring_offset += 2;
2161 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2162 *typestring_offset += 2;
2167 += write_conf_or_var_desc(file, current_structure, baseoff,
2170 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2172 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2173 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2174 *typestring_offset += 2;
2175 write_pointer_description(file, type, typestring_offset);
2176 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2177 *typestring_offset += 1;
2180 write_array_element_type(file, type, FALSE, typestring_offset);
2181 write_end(file, typestring_offset);
2185 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2186 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2187 *typestring_offset += 2;
2189 += write_conf_or_var_desc(file, current_structure, baseoff,
2192 += write_conf_or_var_desc(file, current_structure, baseoff,
2195 write_array_element_type(file, type, TRUE, typestring_offset);
2196 write_end(file, typestring_offset);
2199 return start_offset;
2202 static const var_t *find_array_or_string_in_struct(const type_t *type)
2204 const var_list_t *fields = type_struct_get_fields(type);
2205 const var_t *last_field;
2208 if (!fields || list_empty(fields))
2211 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2212 ft = last_field->type;
2214 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2217 if (type_get_type(ft) == TYPE_STRUCT)
2218 return find_array_or_string_in_struct(ft);
2223 static void write_struct_members(FILE *file, const type_t *type,
2224 int is_complex, unsigned int *corroff,
2225 unsigned int *typestring_offset)
2228 unsigned short offset = 0;
2229 unsigned int salign = 1;
2231 var_list_t *fields = type_struct_get_fields(type);
2233 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2235 type_t *ft = field->type;
2236 unsigned int align = 0;
2237 unsigned int size = type_memsize(ft, &align);
2238 align = clamp_align(align);
2239 if (salign < align) salign = align;
2241 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2243 if ((align - 1) & offset)
2245 unsigned char fc = 0;
2249 fc = RPC_FC_ALIGNM2;
2252 fc = RPC_FC_ALIGNM4;
2255 fc = RPC_FC_ALIGNM8;
2258 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2260 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2261 offset = ROUND_SIZE(offset, align);
2262 *typestring_offset += 1;
2264 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2270 padding = ROUNDING(offset, salign);
2273 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2274 RPC_FC_STRUCTPAD1 + padding - 1,
2276 *typestring_offset += 1;
2279 write_end(file, typestring_offset);
2282 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2283 const char *name, unsigned int *tfsoff)
2285 const type_t *save_current_structure = current_structure;
2286 unsigned int total_size;
2288 unsigned int start_offset;
2289 unsigned int array_offset;
2290 int has_pointers = 0;
2291 unsigned int align = 0;
2292 unsigned int corroff;
2294 unsigned char fc = get_struct_fc(type);
2295 var_list_t *fields = type_struct_get_fields(type);
2298 current_structure = type;
2300 total_size = type_memsize(type, &align);
2301 if (total_size > USHRT_MAX)
2302 error("structure size for %s exceeds %d bytes by %d bytes\n",
2303 name, USHRT_MAX, total_size - USHRT_MAX);
2305 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2306 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2308 if (!has_pointers) has_pointers = type_has_pointers(type);
2310 array = find_array_or_string_in_struct(type);
2311 if (array && !processed(array->type))
2313 = is_string_type(array->attrs, array->type)
2314 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2315 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2318 write_descriptors(file, type, tfsoff);
2320 start_offset = *tfsoff;
2321 update_tfsoff(type, start_offset, file);
2322 print_start_tfs_comment(file, type, start_offset);
2323 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2324 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2325 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2330 unsigned int absoff = array->type->typestring_offset;
2331 short reloff = absoff - *tfsoff;
2332 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2333 reloff, reloff, absoff);
2336 else if (fc == RPC_FC_BOGUS_STRUCT)
2338 print_file(file, 2, "NdrFcShort(0x0),\n");
2342 if (fc == RPC_FC_BOGUS_STRUCT)
2344 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2345 nothing is written to file yet. On the actual writing pass,
2346 this will have been updated. */
2347 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2348 int reloff = absoff - *tfsoff;
2349 assert( reloff >= 0 );
2350 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2351 reloff, reloff, absoff);
2354 else if ((fc == RPC_FC_PSTRUCT) ||
2355 (fc == RPC_FC_CPSTRUCT) ||
2356 (fc == RPC_FC_CVSTRUCT && has_pointers))
2358 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2359 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2361 write_pointer_description(file, type, tfsoff);
2362 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2366 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2369 if (fc == RPC_FC_BOGUS_STRUCT)
2373 type->ptrdesc = *tfsoff;
2374 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2376 type_t *ft = f->type;
2379 if (is_string_type(f->attrs, ft))
2380 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2382 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2384 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2386 unsigned int offset;
2388 print_file(file, 0, "/* %d */\n", *tfsoff);
2390 offset = ft->typestring_offset;
2391 /* skip over the pointer that is written for strings, since a
2392 * pointer has to be written in-place here */
2393 if (is_string_type(f->attrs, ft))
2395 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2398 if (type->ptrdesc == *tfsoff)
2402 current_structure = save_current_structure;
2403 return start_offset;
2406 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2410 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2414 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2417 if (type_get_type(t) == TYPE_BASIC)
2418 fc = get_basic_fc(t);
2420 fc = get_enum_fc(t);
2421 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2422 fc, string_of_type(fc));
2424 else if (t->typestring_offset)
2426 short reloff = t->typestring_offset - *tfsoff;
2427 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2428 reloff, reloff, t->typestring_offset);
2431 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2437 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2440 unsigned int start_offset;
2443 unsigned int nbranch = 0;
2444 type_t *deftype = NULL;
2445 short nodeftype = 0xffff;
2451 size = type_memsize(type, &align);
2453 fields = type_union_get_cases(type);
2455 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2457 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2459 nbranch += list_count(cases);
2461 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2464 start_offset = *tfsoff;
2465 update_tfsoff(type, start_offset, file);
2466 print_start_tfs_comment(file, type, start_offset);
2467 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2469 const var_t *sv = type_union_get_switch_value(type);
2470 const type_t *st = sv->type;
2473 if (type_get_type(st) == TYPE_BASIC)
2475 switch (get_basic_fc(st))
2486 fc = get_basic_fc(st);
2490 error("union switch type must be an integer, char, or enum\n");
2493 else if (type_get_type(st) == TYPE_ENUM)
2494 fc = get_enum_fc(st);
2496 error("union switch type must be an integer, char, or enum\n");
2498 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2499 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2500 0x40 | fc, string_of_type(fc));
2503 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2505 static const expr_t dummy_expr; /* FIXME */
2506 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2509 if (type_get_type(st) == TYPE_BASIC)
2511 switch (get_basic_fc(st))
2522 fc = get_basic_fc(st);
2526 error("union switch type must be an integer, char, or enum\n");
2529 else if (type_get_type(st) == TYPE_ENUM)
2530 fc = get_enum_fc(st);
2532 error("union switch type must be an integer, char, or enum\n");
2534 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2535 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2536 fc, string_of_type(fc));
2539 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2540 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2542 print_file(file, 0, "/* %u */\n", *tfsoff);
2545 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2546 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2549 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2551 type_t *ft = f->type;
2552 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2553 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2556 if (cases == NULL && !deflt)
2557 error("union field %s with neither case nor default attribute\n", f->name);
2559 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2561 /* MIDL doesn't check for duplicate cases, even though that seems
2562 like a reasonable thing to do, it just dumps them to the TFS
2563 like we're going to do here. */
2564 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2566 write_branch_type(file, ft, tfsoff);
2569 /* MIDL allows multiple default branches, even though that seems
2570 illogical, it just chooses the last one, which is what we will
2581 write_branch_type(file, deftype, tfsoff);
2585 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2589 return start_offset;
2592 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2593 unsigned int *typeformat_offset)
2596 unsigned int start_offset = *typeformat_offset;
2597 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2601 print_file(file, 2, "0x2f, /* FC_IP */\n");
2602 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2604 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2608 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2609 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2612 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2614 update_tfsoff(type, start_offset, file);
2615 print_start_tfs_comment(file, type, start_offset);
2616 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2617 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2618 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2619 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2620 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2621 for (i = 0; i < 8; ++i)
2622 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2625 fprintf(file, "\n");
2627 *typeformat_offset += 18;
2629 return start_offset;
2632 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2634 unsigned int *typeformat_offset)
2636 unsigned int start_offset = *typeformat_offset;
2637 unsigned char flags = 0;
2639 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2640 flags |= NDR_STRICT_CONTEXT_HANDLE;
2644 if (is_attr(var->attrs, ATTR_IN))
2647 if (!is_attr(var->attrs, ATTR_OUT))
2648 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2650 if (is_attr(var->attrs, ATTR_OUT))
2653 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2654 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2655 /* return and can't be null values overlap */
2656 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2657 print_file(file, 0, "can't be null, ");
2658 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2659 print_file(file, 0, "serialize, ");
2660 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2661 print_file(file, 0, "no serialize, ");
2662 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2663 print_file(file, 0, "strict, ");
2664 if ((flags & 0x21) == 0x20)
2665 print_file(file, 0, "out, ");
2666 if ((flags & 0x21) == 0x21)
2667 print_file(file, 0, "return, ");
2669 print_file(file, 0, "in, ");
2671 print_file(file, 0, "via ptr, ");
2672 print_file(file, 0, "*/\n");
2673 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2674 print_file(file, 2, "0, /* FIXME: param num */\n");
2675 *typeformat_offset += 4;
2677 return start_offset;
2680 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2681 type_t *type, const var_t *var,
2683 unsigned int *typeformat_offset)
2685 unsigned int offset;
2687 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2689 case TGT_CTXT_HANDLE:
2690 case TGT_CTXT_HANDLE_POINTER:
2691 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2693 write_user_tfs(file, type, typeformat_offset);
2694 return type->typestring_offset;
2696 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2701 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2702 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2703 if (ptr_type != RPC_FC_RP)
2705 unsigned int absoff = type->typestring_offset;
2706 short reloff = absoff - (*typeformat_offset + 2);
2707 off = *typeformat_offset;
2708 print_file(file, 0, "/* %d */\n", off);
2709 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2710 string_of_type(ptr_type));
2711 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2712 reloff, reloff, absoff);
2713 *typeformat_offset += 4;
2718 if (processed(type)) return type->typestring_offset;
2719 return write_struct_tfs(file, type, var->name, typeformat_offset);
2721 if (processed(type)) return type->typestring_offset;
2722 return write_union_tfs(file, type, typeformat_offset);
2727 case TGT_IFACE_POINTER:
2728 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2732 size_t start_offset = *typeformat_offset;
2733 int in_attr = is_attr(var->attrs, ATTR_IN);
2734 int out_attr = is_attr(var->attrs, ATTR_OUT);
2735 const type_t *ref = type_pointer_get_ref(type);
2737 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2739 /* special case for pointers to base types */
2745 if (type_get_type(ref) == TYPE_ENUM)
2746 fc = get_enum_fc(ref);
2748 fc = get_basic_fc(ref);
2750 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2751 get_pointer_fc(type, var->attrs, toplevel_param),
2752 (!in_attr && out_attr) ? 0x0C : 0x08,
2753 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2754 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2755 print_file(file, indent, "0x%02x, /* %s */\n",
2756 fc, string_of_type(fc));
2757 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2758 *typeformat_offset += 4;
2759 return start_offset;
2766 offset = write_typeformatstring_var(file, indent, func,
2767 type_pointer_get_ref(type), var,
2768 FALSE, typeformat_offset);
2770 fprintf(file, "/* %2u */\n", *typeformat_offset);
2771 return write_nonsimple_pointer(file, var->attrs, type,
2773 offset, typeformat_offset);
2777 error("invalid type %s for var %s\n", type->name, var->name);
2781 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2782 const char *name, int write_ptr, unsigned int *tfsoff)
2786 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2789 write_user_tfs(file, type, tfsoff);
2792 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2794 case TGT_IFACE_POINTER:
2795 write_ip_tfs(file, attrs, type, tfsoff);
2799 type_t *ref = type_pointer_get_ref(type);
2801 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2802 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2805 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2811 /* conformant arrays and strings are handled specially */
2812 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2814 write_array_tfs(file, attrs, type, name, tfsoff);
2815 if (is_conformant_array(type))
2820 if (!processed(type))
2821 write_struct_tfs(file, type, name, tfsoff);
2824 if (!processed(type))
2825 write_union_tfs(file, type, tfsoff);
2831 case TGT_CTXT_HANDLE:
2832 case TGT_CTXT_HANDLE_POINTER:
2834 error("invalid type %s for var %s\n", type->name, name);
2841 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2842 type_pred_t pred, unsigned int *typeformat_offset)
2845 const statement_t *stmt;
2847 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2849 const type_t *iface;
2850 const statement_t *stmt_func;
2852 if (stmt->type == STMT_LIBRARY)
2854 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2857 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2860 iface = stmt->u.type;
2864 current_iface = iface;
2865 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2867 const var_t *func = stmt_func->u.var;
2868 if (is_local(func->attrs)) continue;
2870 if (!is_void(type_function_get_rettype(func->type)))
2873 v.type = type_function_get_rettype(func->type);
2874 update_tfsoff(type_function_get_rettype(func->type),
2875 write_typeformatstring_var(
2877 type_function_get_rettype(func->type),
2878 &v, FALSE, typeformat_offset),
2882 current_func = func;
2883 if (type_get_function_args(func->type))
2884 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2887 write_typeformatstring_var(
2888 file, 2, func, var->type, var,
2889 TRUE, typeformat_offset),
2894 return *typeformat_offset + 1;
2897 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2899 unsigned int typeformat_offset = 2;
2901 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2905 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2909 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2910 print_file(file, indent, "{\n");
2912 print_file(file, indent, "0,\n");
2913 print_file(file, indent, "{\n");
2915 print_file(file, indent, "NdrFcShort(0x0),\n");
2917 set_all_tfswrite(TRUE);
2918 process_tfs(file, stmts, pred);
2920 print_file(file, indent, "0x0\n");
2922 print_file(file, indent, "}\n");
2924 print_file(file, indent, "};\n");
2925 print_file(file, indent, "\n");
2928 static unsigned int get_required_buffer_size_type(
2929 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2932 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2937 const type_t *utype = get_user_type(type, &uname);
2938 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
2941 switch (get_basic_fc(type))
2959 case RPC_FC_ERROR_STATUS_T:
2969 case RPC_FC_BIND_PRIMITIVE:
2973 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2974 get_basic_fc(type));
2980 switch (get_enum_fc(type))
2992 if (get_struct_fc(type) == RPC_FC_STRUCT)
2994 if (!type_struct_get_fields(type)) return 0;
2995 return fields_memsize(type_struct_get_fields(type), alignment);
3000 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3002 const type_t *ref = type_pointer_get_ref(type);
3003 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3007 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3009 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3010 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3013 case TGT_CTXT_HANDLE:
3014 case TGT_CTXT_HANDLE_POINTER:
3018 case TGT_IFACE_POINTER:
3027 /* FIXME: depends on pointer type */
3028 return type_array_get_dim(type) *
3029 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3037 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3039 int in_attr = is_attr(var->attrs, ATTR_IN);
3040 int out_attr = is_attr(var->attrs, ATTR_OUT);
3042 if (!in_attr && !out_attr)
3047 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3048 pass == PASS_RETURN)
3050 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3056 if (!is_string_type(var->attrs, var->type))
3057 return get_required_buffer_size_type(var->type, var->name,
3058 var->attrs, TRUE, alignment);
3063 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3066 unsigned int total_size = 0, alignment;
3068 if (type_get_function_args(func->type))
3070 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3072 total_size += get_required_buffer_size(var, &alignment, pass);
3073 total_size += alignment;
3077 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3080 v.type = type_function_get_rettype(func->type);
3081 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3082 total_size += alignment;
3087 static void print_phase_function(FILE *file, int indent, const char *type,
3088 const char *local_var_prefix, enum remoting_phase phase,
3089 const var_t *var, unsigned int type_offset)
3091 const char *function;
3094 case PHASE_BUFFERSIZE:
3095 function = "BufferSize";
3098 function = "Marshall";
3100 case PHASE_UNMARSHAL:
3101 function = "Unmarshall";
3111 print_file(file, indent, "Ndr%s%s(\n", type, function);
3113 print_file(file, indent, "&__frame->_StubMsg,\n");
3114 print_file(file, indent, "%s%s%s%s%s,\n",
3115 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3116 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3118 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3120 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3121 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3122 if (phase == PHASE_UNMARSHAL)
3123 print_file(file, indent, "0);\n");
3127 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3128 enum remoting_phase phase, enum pass pass, const var_t *var,
3129 const char *varname)
3131 type_t *type = var->type;
3133 unsigned int alignment = 0;
3136 /* no work to do for other phases, buffer sizing is done elsewhere */
3137 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3140 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3141 if (type_get_type(ref) == TYPE_ENUM)
3143 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3148 else /* RPC_FC_ENUM16 */
3156 switch (get_basic_fc(ref))
3176 case RPC_FC_ERROR_STATUS_T:
3188 case RPC_FC_BIND_PRIMITIVE:
3189 /* no marshalling needed */
3193 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3194 var->name, get_basic_fc(ref));
3199 if (phase == PHASE_MARSHAL)
3200 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3201 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3202 alignment - 1, alignment - 1);
3204 if (phase == PHASE_MARSHAL)
3206 print_file(file, indent, "*(");
3207 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3209 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3211 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3212 fprintf(file, "%s%s", local_var_prefix, varname);
3213 fprintf(file, ";\n");
3215 else if (phase == PHASE_UNMARSHAL)
3217 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3218 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3219 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3220 print_file(file, indent, "{\n");
3221 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3222 print_file(file, indent, "}\n");
3223 print_file(file, indent, "%s%s%s",
3224 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3225 local_var_prefix, varname);
3226 if (pass == PASS_IN && is_ptr(type))
3227 fprintf(file, " = (");
3229 fprintf(file, " = *(");
3230 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3231 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3234 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3235 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3236 fprintf(file, ");\n");
3239 /* returns whether the MaxCount, Offset or ActualCount members need to be
3240 * filled in for the specified phase */
3241 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3243 return (phase != PHASE_UNMARSHAL);
3246 expr_t *get_size_is_expr(const type_t *t, const char *name)
3250 for ( ; is_array(t); t = type_array_get_element(t))
3251 if (type_array_has_conformance(t))
3254 x = type_array_get_conformance(t);
3256 error("%s: multidimensional conformant"
3257 " arrays not supported at the top level\n",
3264 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3265 enum remoting_phase phase, const var_t *var)
3267 const type_t *type = var->type;
3268 /* get fundamental type for the argument */
3271 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3273 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3275 else if (type_is_alias(type))
3276 type = type_alias_get_aliasee(type);
3277 else if (is_array(type))
3279 if (is_conformance_needed_for_phase(phase) && is_array(type))
3281 if (type_array_has_conformance(type))
3283 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3284 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3285 fprintf(file, ";\n\n");
3287 if (type_array_has_variance(type))
3289 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3290 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3291 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3292 fprintf(file, ";\n\n");
3297 else if (type_get_type(type) == TYPE_UNION)
3299 if (is_conformance_needed_for_phase(phase))
3301 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3302 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3303 fprintf(file, ";\n\n");
3307 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3311 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3313 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3314 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3315 fprintf( file, ";\n\n" );
3319 else if (is_ptr(type))
3320 type = type_pointer_get_ref(type);
3326 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3327 enum pass pass, enum remoting_phase phase, const var_t *var)
3329 int in_attr, out_attr, pointer_type;
3330 const type_t *type = var->type;
3331 unsigned int start_offset = type->typestring_offset;
3333 if (is_ptr(type) || is_array(type))
3334 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3338 in_attr = is_attr(var->attrs, ATTR_IN);
3339 out_attr = is_attr(var->attrs, ATTR_OUT);
3340 if (!in_attr && !out_attr)
3343 if (phase != PHASE_FREE)
3347 if (!in_attr) return;
3350 if (!out_attr) return;
3356 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3358 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3360 case TGT_CTXT_HANDLE:
3361 case TGT_CTXT_HANDLE_POINTER:
3362 if (phase == PHASE_MARSHAL)
3364 if (pass == PASS_IN)
3366 /* if the context_handle attribute appears in the chain of types
3367 * without pointers being followed, then the context handle must
3368 * be direct, otherwise it is a pointer */
3369 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3370 print_file(file, indent, "NdrClientContextMarshall(\n");
3371 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3372 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3373 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3377 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3378 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3379 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3380 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3381 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3384 else if (phase == PHASE_UNMARSHAL)
3386 if (pass == PASS_OUT)
3389 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3390 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3391 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3392 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3393 print_file(file, indent + 1, "__frame->_Handle);\n");
3397 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3398 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3399 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3404 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3407 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3408 pointer_type != RPC_FC_RP)
3410 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3411 !in_attr && is_conformant_array(type))
3413 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3415 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3417 /* strings returned are assumed to be global and hence don't
3419 else if (is_declptr(type) &&
3420 !(phase == PHASE_FREE && pass == PASS_RETURN))
3421 print_phase_function(file, indent, "Pointer", local_var_prefix,
3422 phase, var, start_offset);
3426 unsigned int real_start_offset = start_offset;
3427 /* skip over pointer description straight to string description */
3428 if (is_declptr(type))
3430 if (is_conformant_array(type))
3431 real_start_offset += 4;
3433 real_start_offset += 2;
3435 if (is_array(type) && !is_conformant_array(type))
3436 print_phase_function(file, indent, "NonConformantString",
3437 local_var_prefix, phase, var,
3440 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3441 phase, var, real_start_offset);
3446 unsigned char tc = get_array_fc(type);
3447 const char *array_type = "FixedArray";
3449 /* We already have the size_is expression since it's at the
3450 top level, but do checks for multidimensional conformant
3451 arrays. When we handle them, we'll need to extend this
3452 function to return a list, and then we'll actually use
3453 the return value. */
3454 get_size_is_expr(type, var->name);
3456 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3458 array_type = "VaryingArray";
3460 else if (tc == RPC_FC_CARRAY)
3462 array_type = "ConformantArray";
3464 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3466 array_type = (tc == RPC_FC_BOGUS_ARRAY
3468 : "ConformantVaryingArray");
3471 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3472 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3473 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3475 /* these are all unmarshalled by allocating memory */
3476 if (tc == RPC_FC_BOGUS_ARRAY ||
3477 tc == RPC_FC_CVARRAY ||
3478 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3479 (tc == RPC_FC_CARRAY && !in_attr))
3481 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3483 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3489 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3490 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3493 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3495 if (phase == PHASE_MARSHAL)
3496 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3498 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3499 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3500 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3503 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3507 switch (get_struct_fc(type))
3510 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3511 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3513 case RPC_FC_PSTRUCT:
3514 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3516 case RPC_FC_CSTRUCT:
3517 case RPC_FC_CPSTRUCT:
3518 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3520 case RPC_FC_CVSTRUCT:
3521 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3523 case RPC_FC_BOGUS_STRUCT:
3524 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3527 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3532 const char *union_type = NULL;
3534 if (type_get_type(type) == TYPE_UNION)
3535 union_type = "NonEncapsulatedUnion";
3536 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3537 union_type = "EncapsulatedUnion";
3539 print_phase_function(file, indent, union_type, local_var_prefix,
3540 phase, var, start_offset);
3545 const type_t *ref = type_pointer_get_ref(type);
3546 if (pointer_type == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3549 /* base types have known sizes, so don't need a sizing pass
3550 * and don't have any memory to free and so don't need a
3552 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3553 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3556 /* base types have known sizes, so don't need a sizing pass
3557 * and don't have any memory to free and so don't need a
3559 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3560 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3564 const char *struct_type = NULL;
3565 switch (get_struct_fc(ref))
3568 /* simple structs have known sizes, so don't need a sizing
3569 * pass and don't have any memory to free and so don't
3570 * need a freeing pass */
3571 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3572 struct_type = "SimpleStruct";
3573 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3575 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3577 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3581 case RPC_FC_PSTRUCT:
3582 struct_type = "SimpleStruct";
3584 case RPC_FC_CSTRUCT:
3585 case RPC_FC_CPSTRUCT:
3586 struct_type = "ConformantStruct";
3588 case RPC_FC_CVSTRUCT:
3589 struct_type = "ConformantVaryingStruct";
3591 case RPC_FC_BOGUS_STRUCT:
3592 struct_type = "ComplexStruct";
3595 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3600 if (phase == PHASE_FREE)
3601 struct_type = "Pointer";
3603 start_offset = ref->typestring_offset;
3604 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3609 case TYPE_ENCAPSULATED_UNION:
3611 const char *union_type = NULL;
3612 if (phase == PHASE_FREE)
3613 union_type = "Pointer";
3616 if (type_get_type(ref) == TYPE_UNION)
3617 union_type = "NonEncapsulatedUnion";
3618 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3619 union_type = "EncapsulatedUnion";
3621 start_offset = ref->typestring_offset;
3624 print_phase_function(file, indent, union_type, local_var_prefix,
3625 phase, var, start_offset);
3630 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3637 case TYPE_INTERFACE:
3642 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3645 case TGT_IFACE_POINTER:
3646 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3652 fprintf(file, "\n");
3655 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3656 enum pass pass, enum remoting_phase phase)
3658 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3660 unsigned int size = get_function_buffer_size( func, pass );
3661 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3664 if (pass == PASS_RETURN)
3668 var.type = type_function_get_rettype(func->type);
3669 var.name = xstrdup( "_RetVal" );
3670 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3676 if (!type_get_function_args(func->type))
3678 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3679 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3684 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3686 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3690 unsigned int get_size_procformatstring_func(const var_t *func)
3693 unsigned int size = 0;
3695 /* argument list size */
3696 if (type_get_function_args(func->type))
3697 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3698 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3700 /* return value size */
3701 if (is_void(type_function_get_rettype(func->type)))
3702 size += 2; /* FC_END and FC_PAD */
3704 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3709 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3711 const statement_t *stmt;
3712 unsigned int size = 1;
3714 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3716 const type_t *iface;
3717 const statement_t *stmt_func;
3719 if (stmt->type == STMT_LIBRARY)
3721 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3724 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3727 iface = stmt->u.type;
3731 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3733 const var_t *func = stmt_func->u.var;
3734 if (!is_local(func->attrs))
3735 size += get_size_procformatstring_func( func );
3741 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3743 set_all_tfswrite(FALSE);
3744 return process_tfs(NULL, stmts, pred);
3747 void declare_stub_args( FILE *file, int indent, const var_t *func )
3749 int in_attr, out_attr;
3753 /* declare return value '_RetVal' */
3754 if (!is_void(type_function_get_rettype(func->type)))
3756 print_file(file, indent, "%s", "");
3757 write_type_decl_left(file, type_function_get_rettype(func->type));
3758 fprintf(file, " _RetVal;\n");
3761 if (!type_get_function_args(func->type))
3764 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3766 in_attr = is_attr(var->attrs, ATTR_IN);
3767 out_attr = is_attr(var->attrs, ATTR_OUT);
3768 if (!out_attr && !in_attr)
3771 if (is_context_handle(var->type))
3772 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3775 if (!in_attr && !is_conformant_array(var->type))
3777 type_t *type_to_print;
3779 print_file(file, indent, "%s", "");
3780 if (type_get_type(var->type) == TYPE_ARRAY &&
3781 !type_array_is_decl_as_ptr(var->type))
3782 type_to_print = var->type;
3784 type_to_print = type_pointer_get_ref(var->type);
3785 sprintf(name, "_W%u", i++);
3786 write_type_decl(file, type_to_print, name);
3787 fprintf(file, ";\n");
3790 print_file(file, indent, "%s", "");
3791 write_type_decl_left(file, var->type);
3793 if (type_get_type(var->type) == TYPE_ARRAY &&
3794 !type_array_is_decl_as_ptr(var->type)) {
3795 fprintf(file, "(*%s)", var->name);
3797 fprintf(file, "%s", var->name);
3798 write_type_right(file, var->type, FALSE);
3799 fprintf(file, ";\n");
3801 if (decl_indirect(var->type))
3802 print_file(file, indent, "void *_p_%s;\n", var->name);
3808 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3810 int in_attr, out_attr;
3814 if (!type_get_function_args(func->type))
3817 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3819 in_attr = is_attr(var->attrs, ATTR_IN);
3820 out_attr = is_attr(var->attrs, ATTR_OUT);
3821 if (!out_attr && !in_attr)
3826 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3828 if (is_context_handle(var->type))
3830 fprintf(file, " = NdrContextHandleInitialize(\n");
3831 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3832 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3833 var->type->typestring_offset);
3835 else if (is_array(var->type) &&
3836 type_array_has_conformance(var->type))
3838 unsigned int size, align = 0;
3839 type_t *type = var->type;
3841 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3843 is_array(type) && type_array_has_conformance(type);
3844 type = type_array_get_element(type))
3846 write_expr(file, type_array_get_conformance(type), TRUE,
3847 TRUE, NULL, NULL, local_var_prefix);
3848 fprintf(file, " * ");
3850 size = type_memsize(type, &align);
3851 fprintf(file, "%u);\n", size);
3855 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3856 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3861 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3866 case TGT_IFACE_POINTER:
3868 case TGT_CTXT_HANDLE:
3869 case TGT_CTXT_HANDLE_POINTER:
3872 /* not initialised */
3882 fprintf(file, "\n");
3886 int write_expr_eval_routines(FILE *file, const char *iface)
3888 static const char *var_name = "pS";
3889 static const char *var_name_expr = "pS->";
3891 struct expr_eval_routine *eval;
3892 unsigned short callback_offset = 0;
3894 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3896 const char *name = eval->structure->name;
3899 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3900 iface, name, callback_offset);
3901 print_file(file, 0, "{\n");
3902 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3903 name, var_name, name, eval->baseoff);
3904 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3905 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3906 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3907 fprintf(file, ";\n");
3908 print_file(file, 0, "}\n\n");
3914 void write_expr_eval_routine_list(FILE *file, const char *iface)
3916 struct expr_eval_routine *eval;
3917 struct expr_eval_routine *cursor;
3918 unsigned short callback_offset = 0;
3920 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3921 fprintf(file, "{\n");
3923 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3925 const char *name = eval->structure->name;
3926 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3928 list_remove(&eval->entry);
3932 fprintf(file, "};\n\n");
3935 void write_user_quad_list(FILE *file)
3939 if (list_empty(&user_type_list))
3942 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3943 fprintf(file, "{\n");
3944 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3946 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3947 print_file(file, 1, "{\n");
3948 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3949 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3950 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3951 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3952 print_file(file, 1, "}%s\n", sep);
3954 fprintf(file, "};\n\n");
3957 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3959 const struct str_list_entry_t *endpoint;
3962 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3963 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3964 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3966 print_file( f, 1, "{ (const unsigned char *)\"" );
3967 for (p = endpoint->str; *p && *p != ':'; p++)
3969 if (*p == '"' || *p == '\\') fputc( '\\', f );
3972 if (!*p) goto error;
3973 if (p[1] != '[') goto error;
3975 fprintf( f, "\", (const unsigned char *)\"" );
3976 for (p += 2; *p && *p != ']'; p++)
3978 if (*p == '"' || *p == '\\') fputc( '\\', f );
3981 if (*p != ']') goto error;
3982 fprintf( f, "\" },\n" );
3984 print_file( f, 0, "};\n\n" );
3988 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3991 void write_exceptions( FILE *file )
3993 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3994 fprintf( file, "\n");
3995 fprintf( file, "#include \"wine/exception.h\"\n");
3996 fprintf( file, "#undef RpcTryExcept\n");
3997 fprintf( file, "#undef RpcExcept\n");
3998 fprintf( file, "#undef RpcEndExcept\n");
3999 fprintf( file, "#undef RpcTryFinally\n");
4000 fprintf( file, "#undef RpcFinally\n");
4001 fprintf( file, "#undef RpcEndFinally\n");
4002 fprintf( file, "#undef RpcExceptionCode\n");
4003 fprintf( file, "#undef RpcAbnormalTermination\n");
4004 fprintf( file, "\n");
4005 fprintf( file, "struct __exception_frame;\n");
4006 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4007 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4008 fprintf( file, "\n");
4009 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4010 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4011 fprintf( file, " __filter_func filter; \\\n");
4012 fprintf( file, " __finally_func finally; \\\n");
4013 fprintf( file, " sigjmp_buf jmp; \\\n");
4014 fprintf( file, " DWORD code; \\\n");
4015 fprintf( file, " unsigned char abnormal_termination; \\\n");
4016 fprintf( file, " unsigned char filter_level; \\\n");
4017 fprintf( file, " unsigned char finally_level;\n");
4018 fprintf( file, "\n");
4019 fprintf( file, "struct __exception_frame\n{\n");
4020 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4021 fprintf( file, "};\n");
4022 fprintf( file, "\n");
4023 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4024 fprintf( file, "{\n");
4025 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4026 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4027 fprintf( file, " {\n");
4028 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4029 fprintf( file, " exc_frame->finally( exc_frame );\n");
4030 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4031 fprintf( file, " }\n");
4032 fprintf( file, " exc_frame->filter_level = 0;\n");
4033 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4034 fprintf( file, "}\n");
4035 fprintf( file, "\n");
4036 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4037 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4038 fprintf( file, " CONTEXT *context,\n");
4039 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4040 fprintf( file, "{\n");
4041 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4042 fprintf( file, "\n");
4043 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4044 fprintf( file, " {\n" );
4045 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4046 fprintf( file, " {\n" );
4047 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4048 fprintf( file, " exc_frame->finally( exc_frame );\n");
4049 fprintf( file, " }\n" );
4050 fprintf( file, " return ExceptionContinueSearch;\n");
4051 fprintf( file, " }\n" );
4052 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4053 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4054 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4055 fprintf( file, " return ExceptionContinueSearch;\n");
4056 fprintf( file, "}\n");
4057 fprintf( file, "\n");
4058 fprintf( file, "#define RpcTryExcept \\\n");
4059 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4060 fprintf( file, " { \\\n");
4061 fprintf( file, " if (!__frame->finally_level) \\\n" );
4062 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4063 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4064 fprintf( file, "\n");
4065 fprintf( file, "#define RpcExcept(expr) \\\n");
4066 fprintf( file, " if (!__frame->finally_level) \\\n" );
4067 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4068 fprintf( file, " __frame->filter_level = 0; \\\n" );
4069 fprintf( file, " } \\\n");
4070 fprintf( file, " else \\\n");
4071 fprintf( file, "\n");
4072 fprintf( file, "#define RpcEndExcept\n");
4073 fprintf( file, "\n");
4074 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4075 fprintf( file, "\n");
4076 fprintf( file, "#define RpcTryFinally \\\n");
4077 fprintf( file, " if (!__frame->filter_level) \\\n");
4078 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4079 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4080 fprintf( file, "\n");
4081 fprintf( file, "#define RpcFinally \\\n");
4082 fprintf( file, " if (!__frame->filter_level) \\\n");
4083 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4084 fprintf( file, " __frame->finally_level = 0;\n");
4085 fprintf( file, "\n");
4086 fprintf( file, "#define RpcEndFinally\n");
4087 fprintf( file, "\n");
4088 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4089 fprintf( file, "\n");
4090 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4091 fprintf( file, " do { \\\n");
4092 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4093 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4094 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4095 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4096 fprintf( file, " __frame->filter_level = 0; \\\n");
4097 fprintf( file, " __frame->finally_level = 0; \\\n");
4098 fprintf( file, " } while (0)\n");
4099 fprintf( file, "\n");
4100 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4101 fprintf( file, "\n");
4102 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4103 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4104 fprintf( file, "\n");
4105 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4106 fprintf( file, " DWORD code;\n");
4107 fprintf( file, "\n");
4108 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");