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 static type_t *get_user_type(const type_t *t, const char **pname);
234 static int type_contains_iface(const type_t *type)
236 enum typegen_type typegen_type;
240 typegen_type = typegen_detect_type(type, type->attrs, TDT_IGNORE_STRINGS);
245 return type_contains_iface(get_user_type(type, NULL));
252 return type_contains_iface(type_pointer_get_ref(type));
255 return type_contains_iface(type_array_get_element(type));
257 case TGT_IFACE_POINTER:
261 fields = type_struct_get_fields(type);
262 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
264 if(type_contains_iface(field->type))
270 fields = type_union_get_cases(type);
271 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
273 if(field->type && type_contains_iface(field->type))
279 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
281 case TGT_CTXT_HANDLE:
282 case TGT_CTXT_HANDLE_POINTER:
283 /* checking after parsing should mean that we don't get here. if we do,
284 * it's a checker bug */
290 unsigned char get_struct_fc(const type_t *type)
293 int has_conformance = 0;
294 int has_variance = 0;
298 fields = type_struct_get_fields(type);
300 if (get_padding(fields))
301 return RPC_FC_BOGUS_STRUCT;
303 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
305 type_t *t = field->type;
306 enum typegen_type typegen_type;
308 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
310 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
312 if (is_string_type(field->attrs, field->type))
314 if (is_conformant_array(t))
320 if (is_array(type_array_get_element(field->type)))
321 return RPC_FC_BOGUS_STRUCT;
323 if (type_array_has_conformance(field->type))
326 if (list_next(fields, &field->entry))
327 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
330 if (type_array_has_variance(t))
333 t = type_array_get_element(t);
334 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
337 switch (typegen_type)
340 case TGT_IFACE_POINTER:
341 return RPC_FC_BOGUS_STRUCT;
345 if (get_enum_fc(t) == RPC_FC_ENUM16)
346 return RPC_FC_BOGUS_STRUCT;
349 if(type_contains_iface(type_array_get_element(t)))
350 return RPC_FC_BOGUS_STRUCT;
352 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
353 return RPC_FC_BOGUS_STRUCT;
357 return RPC_FC_BOGUS_STRUCT;
360 unsigned char fc = get_struct_fc(t);
365 case RPC_FC_CVSTRUCT:
371 case RPC_FC_CPSTRUCT:
373 if (list_next( fields, &field->entry ))
374 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
381 if (list_next( fields, &field->entry ))
382 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
391 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
392 /* fallthru - treat it as complex */
394 /* as soon as we see one of these these members, it's bogus... */
395 case RPC_FC_BOGUS_STRUCT:
396 return RPC_FC_BOGUS_STRUCT;
401 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
403 case TGT_CTXT_HANDLE:
404 case TGT_CTXT_HANDLE_POINTER:
405 /* checking after parsing should mean that we don't get here. if we do,
406 * it's a checker bug */
413 if ( has_conformance )
414 return RPC_FC_CVSTRUCT;
416 return RPC_FC_BOGUS_STRUCT;
418 if( has_conformance && has_pointer )
419 return RPC_FC_CPSTRUCT;
420 if( has_conformance )
421 return RPC_FC_CSTRUCT;
423 return RPC_FC_PSTRUCT;
424 return RPC_FC_STRUCT;
427 unsigned char get_array_fc(const type_t *type)
430 const expr_t *size_is;
431 const type_t *elem_type;
433 elem_type = type_array_get_element(type);
434 size_is = type_array_get_conformance(type);
438 unsigned int align = 0;
439 unsigned int size = type_memsize(elem_type, &align);
440 if (size * type_array_get_dim(type) > 0xffffuL)
441 fc = RPC_FC_LGFARRAY;
443 fc = RPC_FC_SMFARRAY;
448 if (type_array_has_variance(type))
450 if (fc == RPC_FC_SMFARRAY)
451 fc = RPC_FC_SMVARRAY;
452 else if (fc == RPC_FC_LGFARRAY)
453 fc = RPC_FC_LGVARRAY;
454 else if (fc == RPC_FC_CARRAY)
458 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
461 fc = RPC_FC_BOGUS_ARRAY;
464 switch (get_struct_fc(elem_type))
466 case RPC_FC_BOGUS_STRUCT:
467 fc = RPC_FC_BOGUS_ARRAY;
472 /* is 16-bit enum - if so, wire size differs from mem size and so
473 * the array cannot be block copied, which means the array is complex */
474 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
475 fc = RPC_FC_BOGUS_ARRAY;
478 case TGT_IFACE_POINTER:
479 fc = RPC_FC_BOGUS_ARRAY;
482 /* ref pointers cannot just be block copied. unique pointers to
483 * interfaces need special treatment. either case means the array is
485 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
486 fc = RPC_FC_BOGUS_ARRAY;
489 case TGT_CTXT_HANDLE:
490 case TGT_CTXT_HANDLE_POINTER:
494 /* nothing to do for everything else */
501 int is_struct(unsigned char type)
508 case RPC_FC_CPSTRUCT:
509 case RPC_FC_CVSTRUCT:
510 case RPC_FC_BOGUS_STRUCT:
517 static int is_non_complex_struct(const type_t *type)
519 return (type_get_type(type) == TYPE_STRUCT &&
520 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
523 static int type_has_pointers(const type_t *type)
525 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
532 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
535 var_list_t *fields = type_struct_get_fields(type);
537 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
539 if (type_has_pointers(field->type))
548 fields = type_union_get_cases(type);
549 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
551 if (field->type && type_has_pointers(field->type))
556 case TGT_CTXT_HANDLE:
557 case TGT_CTXT_HANDLE_POINTER:
559 case TGT_IFACE_POINTER:
569 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
572 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
577 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
582 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
585 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
588 var_list_t *fields = type_struct_get_fields(type);
590 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
592 if (type_has_full_pointer(field->type, field->attrs, FALSE))
601 fields = type_union_get_cases(type);
602 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
604 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
609 case TGT_CTXT_HANDLE:
610 case TGT_CTXT_HANDLE_POINTER:
612 case TGT_IFACE_POINTER:
622 static unsigned short user_type_offset(const char *name)
625 unsigned short off = 0;
626 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
628 if (strcmp(name, ut->name) == 0)
632 error("user_type_offset: couldn't find type (%s)\n", name);
636 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
638 type->typestring_offset = offset;
639 if (file) type->tfswrite = FALSE;
642 static void guard_rec(type_t *type)
644 /* types that contain references to themselves (like a linked list),
645 need to be shielded from infinite recursion when writing embedded
647 if (type->typestring_offset)
648 type->tfswrite = FALSE;
650 type->typestring_offset = 1;
653 static type_t *get_user_type(const type_t *t, const char **pname)
657 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
665 if (type_is_alias(t))
666 t = type_alias_get_aliasee(t);
672 int is_user_type(const type_t *t)
674 return get_user_type(t, NULL) != NULL;
677 static int is_embedded_complex(const type_t *type)
679 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
685 case TGT_IFACE_POINTER:
692 static const char *get_context_handle_type_name(const type_t *type)
696 is_ptr(t) || type_is_alias(t);
697 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
698 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
704 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
707 fprintf(file, "/* %2u */\n", typestring_offset); \
708 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
712 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
713 static void print_file(FILE *file, int indent, const char *format, ...)
716 va_start(va, format);
717 print(file, indent, format, va);
721 void print(FILE *file, int indent, const char *format, va_list va)
725 if (format[0] != '\n')
728 vfprintf(file, format, va);
733 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
735 if (decl_indirect(t))
737 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
738 local_var_prefix, n, local_var_prefix, n);
739 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
741 else if (is_ptr(t) || is_array(t))
742 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
745 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
749 if (!is_void(type_function_get_rettype(func->type)))
750 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
752 if (!type_get_function_args(func->type))
755 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
756 write_var_init(file, indent, var->type, var->name, local_var_prefix);
761 static void write_formatdesc(FILE *f, int indent, const char *str)
763 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
764 print_file(f, indent, "{\n");
765 print_file(f, indent + 1, "short Pad;\n");
766 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
767 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
768 print_file(f, indent, "\n");
771 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
775 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
776 get_size_typeformatstring(stmts, pred));
778 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
779 get_size_procformatstring(stmts, pred));
782 write_formatdesc(f, indent, "TYPE");
783 write_formatdesc(f, indent, "PROC");
785 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
786 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
787 print_file(f, indent, "\n");
790 int decl_indirect(const type_t *t)
794 return (type_get_type(t) != TYPE_BASIC &&
795 type_get_type(t) != TYPE_ENUM &&
796 type_get_type(t) != TYPE_POINTER &&
797 type_get_type(t) != TYPE_ARRAY);
800 static unsigned int write_procformatstring_type(FILE *file, int indent,
803 const attr_list_t *attrs,
808 int is_in = is_attr(attrs, ATTR_IN);
809 int is_out = is_attr(attrs, ATTR_OUT);
811 if (!is_in && !is_out) is_in = TRUE;
813 if (type_get_type(type) == TYPE_BASIC ||
814 type_get_type(type) == TYPE_ENUM)
819 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
821 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
823 if (type_get_type(type) == TYPE_ENUM)
825 fc = get_enum_fc(type);
829 fc = get_basic_fc(type);
831 if (fc == RPC_FC_BIND_PRIMITIVE)
835 print_file(file, indent, "0x%02x, /* %s */\n",
836 fc, string_of_type(fc));
837 size = 2; /* includes param type prefix */
842 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
843 else if (is_in && is_out)
844 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
846 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
848 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
850 print_file(file, indent, "0x01,\n");
851 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
852 size = 4; /* includes param type prefix */
857 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
859 const statement_t *stmt;
860 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
862 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
864 const statement_t *stmt_func;
865 if (!pred(stmt->u.type))
867 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
869 const var_t *func = stmt_func->u.var;
870 if (is_local(func->attrs)) continue;
871 /* emit argument data */
872 if (type_get_function_args(func->type))
875 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
876 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
879 /* emit return value data */
880 if (is_void(type_function_get_rettype(func->type)))
882 print_file(file, indent, "0x5b, /* FC_END */\n");
883 print_file(file, indent, "0x5c, /* FC_PAD */\n");
886 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
889 else if (stmt->type == STMT_LIBRARY)
890 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
894 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
898 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
899 print_file(file, indent, "{\n");
901 print_file(file, indent, "0,\n");
902 print_file(file, indent, "{\n");
905 write_procformatstring_stmts(file, indent, stmts, pred);
907 print_file(file, indent, "0x0\n");
909 print_file(file, indent, "}\n");
911 print_file(file, indent, "};\n");
912 print_file(file, indent, "\n");
915 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
919 if (type_get_type(type) == TYPE_BASIC)
920 fc = get_basic_fc(type);
921 else if (type_get_type(type) == TYPE_ENUM)
922 fc = get_enum_fc(type);
926 if (convert_to_signed_type)
942 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
943 *typestring_offset += 1;
947 /* write conformance / variance descriptor */
948 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
949 unsigned int baseoff, const type_t *type,
952 unsigned char operator_type = 0;
953 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
954 const char *conftype_string = "";
955 const char *operator_string = "no operators";
956 const expr_t *subexpr;
960 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
966 /* Top-level conformance calculations are done inline. */
967 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
968 RPC_FC_TOP_LEVEL_CONFORMANCE);
969 print_file (file, 2, "0x0,\n");
970 print_file (file, 2, "NdrFcShort(0x0),\n");
976 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
977 error("write_conf_or_var_desc: constant value %ld is greater than "
978 "the maximum constant size of %d\n", expr->cval,
979 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
981 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
982 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
983 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
984 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
989 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
991 conftype = RPC_FC_POINTER_CONFORMANCE;
992 conftype_string = "field pointer, ";
996 switch (subexpr->type)
999 subexpr = subexpr->ref;
1000 operator_type = RPC_FC_DEREFERENCE;
1001 operator_string = "FC_DEREFERENCE";
1004 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1006 subexpr = subexpr->ref;
1007 operator_type = RPC_FC_DIV_2;
1008 operator_string = "FC_DIV_2";
1012 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1014 subexpr = subexpr->ref;
1015 operator_type = RPC_FC_MULT_2;
1016 operator_string = "FC_MULT_2";
1020 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1022 subexpr = subexpr->ref;
1023 operator_type = RPC_FC_SUB_1;
1024 operator_string = "FC_SUB_1";
1028 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1030 subexpr = subexpr->ref;
1031 operator_type = RPC_FC_ADD_1;
1032 operator_string = "FC_ADD_1";
1039 if (subexpr->type == EXPR_IDENTIFIER)
1041 const type_t *correlation_variable = NULL;
1042 unsigned char param_type = 0;
1043 unsigned int offset = 0;
1045 var_list_t *fields = type_struct_get_fields(structure);
1047 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1049 unsigned int size = field_memsize( var->type, &offset );
1050 if (var->name && !strcmp(var->name, subexpr->u.sval))
1052 correlation_variable = var->type;
1057 if (!correlation_variable)
1058 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1061 correlation_variable = expr_resolve_type(NULL, structure, expr);
1065 if (type_get_type(correlation_variable) == TYPE_BASIC)
1067 switch (get_basic_fc(correlation_variable))
1071 param_type = RPC_FC_SMALL;
1075 param_type = RPC_FC_USMALL;
1079 param_type = RPC_FC_SHORT;
1082 param_type = RPC_FC_USHORT;
1085 param_type = RPC_FC_LONG;
1088 param_type = RPC_FC_ULONG;
1091 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1092 get_basic_fc(correlation_variable));
1095 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1097 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1098 param_type = RPC_FC_LONG;
1100 param_type = RPC_FC_SHORT;
1104 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1109 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1110 conftype | param_type, conftype_string, string_of_type(param_type));
1111 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1112 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1117 unsigned int callback_offset = 0;
1118 struct expr_eval_routine *eval;
1121 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1123 if (!strcmp (eval->structure->name, structure->name)
1124 && !compare_expr (eval->expr, expr))
1134 eval = xmalloc (sizeof(*eval));
1135 eval->structure = structure;
1136 eval->baseoff = baseoff;
1138 list_add_tail (&expr_eval_routines, &eval->entry);
1141 if (callback_offset > USHRT_MAX)
1142 error("Maximum number of callback routines reached\n");
1144 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1145 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1151 /* return size and start offset of a data field based on current offset */
1152 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1154 unsigned int align = 0;
1155 unsigned int size = type_memsize( type, &align );
1157 *offset = ROUND_SIZE( *offset, align );
1161 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1163 unsigned int size = 0;
1164 unsigned int max_align;
1167 if (!fields) return 0;
1168 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1170 unsigned int falign = 0;
1171 unsigned int fsize = type_memsize(v->type, &falign);
1172 if (*align < falign) *align = falign;
1173 falign = clamp_align(falign);
1174 size = ROUND_SIZE(size, falign);
1178 max_align = clamp_align(*align);
1179 size = ROUND_SIZE(size, max_align);
1184 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1186 unsigned int size, maxs = 0;
1187 unsigned int align = *pmaxa;
1190 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1192 /* we could have an empty default field with NULL type */
1195 size = type_memsize(v->type, &align);
1196 if (maxs < size) maxs = size;
1197 if (*pmaxa < align) *pmaxa = align;
1204 int get_padding(const var_list_t *fields)
1206 unsigned short offset = 0;
1207 unsigned int salign = 1;
1213 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1215 type_t *ft = f->type;
1216 unsigned int align = 0;
1217 unsigned int size = type_memsize(ft, &align);
1218 align = clamp_align(align);
1219 if (align > salign) salign = align;
1220 offset = ROUND_SIZE(offset, align);
1224 return ROUNDING(offset, salign);
1227 unsigned int type_memsize(const type_t *t, unsigned int *align)
1229 unsigned int size = 0;
1231 switch (type_get_type(t))
1234 switch (get_basic_fc(t))
1241 if (size > *align) *align = size;
1247 if (size > *align) *align = size;
1251 case RPC_FC_ERROR_STATUS_T:
1254 if (size > *align) *align = size;
1259 if (size > *align) *align = size;
1262 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1267 switch (get_enum_fc(t))
1272 if (size > *align) *align = size;
1275 error("type_memsize: Unknown enum type\n");
1280 size = fields_memsize(type_struct_get_fields(t), align);
1282 case TYPE_ENCAPSULATED_UNION:
1283 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1286 size = union_memsize(type_union_get_cases(t), align);
1289 assert( pointer_size );
1290 size = pointer_size;
1291 if (size > *align) *align = size;
1294 if (!type_array_is_decl_as_ptr(t))
1296 if (is_conformant_array(t))
1298 type_memsize(type_array_get_element(t), align);
1302 size = type_array_get_dim(t) *
1303 type_memsize(type_array_get_element(t), align);
1305 else /* declared as a pointer */
1307 assert( pointer_size );
1308 size = pointer_size;
1309 if (size > *align) *align = size;
1312 case TYPE_INTERFACE:
1318 /* these types should not be encountered here due to language
1319 * restrictions (interface, void, coclass, module), logical
1320 * restrictions (alias - due to type_get_type call above) or
1321 * checking restrictions (function). */
1328 int is_full_pointer_function(const var_t *func)
1331 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1333 if (!type_get_function_args(func->type))
1335 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1336 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1341 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1343 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1344 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1345 fprintf(file, "\n");
1348 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1350 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1351 fprintf(file, "\n");
1354 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1357 unsigned int offset,
1358 unsigned int *typeformat_offset)
1360 unsigned int start_offset = *typeformat_offset;
1361 short reloff = offset - (*typeformat_offset + 2);
1362 int in_attr, out_attr;
1364 unsigned char flags = 0;
1366 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1368 in_attr = is_attr(attrs, ATTR_IN);
1369 out_attr = is_attr(attrs, ATTR_OUT);
1370 if (!in_attr && !out_attr) in_attr = 1;
1372 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1373 flags |= RPC_FC_P_ONSTACK;
1375 if (is_ptr(type) && !last_ptr(type))
1376 flags |= RPC_FC_P_DEREF;
1378 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1381 string_of_type(pointer_type));
1384 if (flags & RPC_FC_P_ONSTACK)
1385 fprintf(file, " [allocated_on_stack]");
1386 if (flags & RPC_FC_P_DEREF)
1387 fprintf(file, " [pointer_deref]");
1388 fprintf(file, " */\n");
1391 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1392 *typeformat_offset += 4;
1394 return start_offset;
1397 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1400 unsigned char pointer_fc;
1403 /* for historical reasons, write_simple_pointer also handled string types,
1404 * but no longer does. catch bad uses of the function with this check */
1405 if (is_string_type(attrs, type))
1406 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1408 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1410 ref = type_pointer_get_ref(type);
1411 if (type_get_type(ref) == TYPE_ENUM)
1412 fc = get_enum_fc(ref);
1414 fc = get_basic_fc(ref);
1416 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1417 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1418 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1419 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1423 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1425 print_file(file, 0, "/* %u (", tfsoff);
1426 write_type_decl(file, t, NULL);
1427 print_file(file, 0, ") */\n");
1430 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1431 type_t *type, int toplevel_param,
1432 unsigned int *typestring_offset)
1434 unsigned int offset = *typestring_offset;
1435 type_t *ref = type_pointer_get_ref(type);
1437 print_start_tfs_comment(file, type, offset);
1438 update_tfsoff(type, offset, file);
1440 if (ref->typestring_offset)
1441 write_nonsimple_pointer(file, attrs, type,
1443 type_pointer_get_ref(type)->typestring_offset,
1445 else if (type_get_type(ref) == TYPE_BASIC ||
1446 type_get_type(ref) == TYPE_ENUM)
1447 *typestring_offset += write_simple_pointer(file, attrs, type,
1453 static int processed(const type_t *type)
1455 return type->typestring_offset && !type->tfswrite;
1458 static int user_type_has_variable_size(const type_t *t)
1462 else if (type_get_type(t) == TYPE_STRUCT)
1464 switch (get_struct_fc(t))
1466 case RPC_FC_PSTRUCT:
1467 case RPC_FC_CSTRUCT:
1468 case RPC_FC_CPSTRUCT:
1469 case RPC_FC_CVSTRUCT:
1473 /* Note: Since this only applies to user types, we can't have a conformant
1474 array here, and strings should get filed under pointer in this case. */
1478 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1480 unsigned int start, absoff, flags;
1481 unsigned int align = 0, ualign = 0;
1482 const char *name = NULL;
1483 type_t *utype = get_user_type(type, &name);
1484 unsigned int usize = type_memsize(utype, &ualign);
1485 unsigned int size = type_memsize(type, &align);
1486 unsigned short funoff = user_type_offset(name);
1491 if(user_type_has_variable_size(utype)) usize = 0;
1493 if (type_get_type(utype) == TYPE_BASIC ||
1494 type_get_type(utype) == TYPE_ENUM)
1498 if (type_get_type(utype) == TYPE_ENUM)
1499 fc = get_enum_fc(utype);
1501 fc = get_basic_fc(utype);
1504 print_start_tfs_comment(file, utype, absoff);
1505 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1506 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1511 if (!processed(utype))
1512 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1513 absoff = utype->typestring_offset;
1516 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1518 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1524 update_tfsoff(type, start, file);
1525 print_start_tfs_comment(file, type, start);
1526 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1527 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1528 flags | (ualign - 1), ualign - 1, flags);
1529 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1530 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1531 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1533 reloff = absoff - *tfsoff;
1534 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1538 static void write_member_type(FILE *file, const type_t *cont,
1539 int cont_is_complex, const attr_list_t *attrs,
1540 const type_t *type, unsigned int *corroff,
1541 unsigned int *tfsoff)
1543 if (is_embedded_complex(type) && !is_conformant_array(type))
1545 unsigned int absoff;
1548 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1555 absoff = type->typestring_offset;
1557 reloff = absoff - (*tfsoff + 2);
1559 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1560 /* FIXME: actually compute necessary padding */
1561 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1562 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1563 reloff, reloff, absoff);
1566 else if (is_ptr(type) || is_conformant_array(type))
1568 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1569 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1572 else if (!write_base_type(file, type, TRUE, tfsoff))
1573 error("Unsupported member type %d\n", type_get_type(type));
1576 static void write_array_element_type(FILE *file, const type_t *type,
1577 int cont_is_complex, unsigned int *tfsoff)
1579 type_t *elem = type_array_get_element(type);
1581 if (!is_embedded_complex(elem) && is_ptr(elem))
1583 type_t *ref = type_pointer_get_ref(elem);
1587 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1590 if (!is_string_type(NULL, elem) &&
1591 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1593 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1597 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1600 static void write_end(FILE *file, unsigned int *tfsoff)
1602 if (*tfsoff % 2 == 0)
1604 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1607 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1611 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1613 unsigned int offset = 0;
1614 var_list_t *fs = type_struct_get_fields(type);
1617 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1619 type_t *ft = f->type;
1620 unsigned int size = field_memsize( ft, &offset );
1621 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1624 unsigned int absoff = ft->typestring_offset;
1625 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1626 absoff += 8; /* we already have a corr descr, skip it */
1627 reloff = absoff - (*tfsoff + 6);
1628 print_file(file, 0, "/* %d */\n", *tfsoff);
1629 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1630 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1631 write_conf_or_var_desc(file, current_structure, offset, ft,
1632 get_attrp(f->attrs, ATTR_SWITCHIS));
1633 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1634 reloff, reloff, absoff);
1641 static int write_pointer_description_offsets(
1642 FILE *file, const attr_list_t *attrs, type_t *type,
1643 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1644 unsigned int *typestring_offset)
1649 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1650 (is_array(type) && type_array_is_decl_as_ptr(type)))
1652 if (offset_in_memory && offset_in_buffer)
1654 unsigned int memsize;
1656 /* pointer instance */
1657 /* FIXME: sometimes from end of structure, sometimes from beginning */
1658 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1659 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1662 memsize = type_memsize(type, &align);
1663 *offset_in_memory += memsize;
1664 /* increment these separately as in the case of conformant (varying)
1665 * structures these start at different values */
1666 *offset_in_buffer += memsize;
1668 *typestring_offset += 4;
1672 type_t *ref = type_pointer_get_ref(type);
1674 if (is_string_type(attrs, type))
1675 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1676 else if (processed(ref))
1677 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1678 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1679 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1681 error("write_pointer_description_offsets: type format string unknown\n");
1685 unsigned int offset = type->typestring_offset;
1686 /* skip over the pointer that is written for strings, since a
1687 * pointer has to be written in-place here */
1688 if (is_string_type(attrs, type))
1690 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1698 return write_pointer_description_offsets(
1699 file, attrs, type_array_get_element(type), offset_in_memory,
1700 offset_in_buffer, typestring_offset);
1702 else if (is_non_complex_struct(type))
1704 /* otherwise search for interesting fields to parse */
1706 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1708 if (offset_in_memory && offset_in_buffer)
1710 unsigned int padding;
1712 type_memsize(v->type, &align);
1713 padding = ROUNDING(*offset_in_memory, align);
1714 *offset_in_memory += padding;
1715 *offset_in_buffer += padding;
1717 written += write_pointer_description_offsets(
1718 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1724 if (offset_in_memory && offset_in_buffer)
1726 unsigned int memsize;
1728 memsize = type_memsize(type, &align);
1729 *offset_in_memory += memsize;
1730 /* increment these separately as in the case of conformant (varying)
1731 * structures these start at different values */
1732 *offset_in_buffer += memsize;
1739 static int write_no_repeat_pointer_descriptions(
1740 FILE *file, const attr_list_t *attrs, type_t *type,
1741 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1742 unsigned int *typestring_offset)
1748 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1750 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1751 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1752 *typestring_offset += 2;
1754 return write_pointer_description_offsets(file, attrs, type,
1755 offset_in_memory, offset_in_buffer, typestring_offset);
1758 if (is_non_complex_struct(type))
1761 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1763 if (offset_in_memory && offset_in_buffer)
1765 unsigned int padding;
1767 type_memsize(v->type, &align);
1768 padding = ROUNDING(*offset_in_memory, align);
1769 *offset_in_memory += padding;
1770 *offset_in_buffer += padding;
1772 written += write_no_repeat_pointer_descriptions(
1773 file, v->attrs, v->type,
1774 offset_in_memory, offset_in_buffer, typestring_offset);
1779 unsigned int memsize;
1781 memsize = type_memsize(type, &align);
1782 *offset_in_memory += memsize;
1783 /* increment these separately as in the case of conformant (varying)
1784 * structures these start at different values */
1785 *offset_in_buffer += memsize;
1791 /* Note: if file is NULL return value is number of pointers to write, else
1792 * it is the number of type format characters written */
1793 static int write_fixed_array_pointer_descriptions(
1794 FILE *file, const attr_list_t *attrs, type_t *type,
1795 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1796 unsigned int *typestring_offset)
1799 int pointer_count = 0;
1801 if (type_get_type(type) == TYPE_ARRAY &&
1802 !type_array_has_conformance(type) && !type_array_has_variance(type))
1804 unsigned int temp = 0;
1805 /* unfortunately, this needs to be done in two passes to avoid
1806 * writing out redundant FC_FIXED_REPEAT descriptions */
1807 pointer_count = write_pointer_description_offsets(
1808 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1809 if (pointer_count > 0)
1811 unsigned int increment_size;
1812 unsigned int offset_of_array_pointer_mem = 0;
1813 unsigned int offset_of_array_pointer_buf = 0;
1816 increment_size = type_memsize(type_array_get_element(type), &align);
1818 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1819 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1820 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1821 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1822 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1823 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1824 *typestring_offset += 10;
1826 pointer_count = write_pointer_description_offsets(
1827 file, attrs, type, &offset_of_array_pointer_mem,
1828 &offset_of_array_pointer_buf, typestring_offset);
1831 else if (type_get_type(type) == TYPE_STRUCT)
1834 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1836 if (offset_in_memory && offset_in_buffer)
1838 unsigned int padding;
1840 type_memsize(v->type, &align);
1841 padding = ROUNDING(*offset_in_memory, align);
1842 *offset_in_memory += padding;
1843 *offset_in_buffer += padding;
1845 pointer_count += write_fixed_array_pointer_descriptions(
1846 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1852 if (offset_in_memory && offset_in_buffer)
1854 unsigned int memsize;
1856 memsize = type_memsize(type, &align);
1857 *offset_in_memory += memsize;
1858 /* increment these separately as in the case of conformant (varying)
1859 * structures these start at different values */
1860 *offset_in_buffer += memsize;
1864 return pointer_count;
1867 /* Note: if file is NULL return value is number of pointers to write, else
1868 * it is the number of type format characters written */
1869 static int write_conformant_array_pointer_descriptions(
1870 FILE *file, const attr_list_t *attrs, type_t *type,
1871 unsigned int offset_in_memory, unsigned int *typestring_offset)
1874 int pointer_count = 0;
1876 if (is_conformant_array(type) && !type_array_has_variance(type))
1878 unsigned int temp = 0;
1879 /* unfortunately, this needs to be done in two passes to avoid
1880 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1881 pointer_count = write_pointer_description_offsets(
1882 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1883 if (pointer_count > 0)
1885 unsigned int increment_size;
1886 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1887 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1890 increment_size = type_memsize(type_array_get_element(type), &align);
1892 if (increment_size > USHRT_MAX)
1893 error("array size of %u bytes is too large\n", increment_size);
1895 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1896 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1897 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1898 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1899 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1900 *typestring_offset += 8;
1902 pointer_count = write_pointer_description_offsets(
1903 file, attrs, type_array_get_element(type),
1904 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1909 return pointer_count;
1912 /* Note: if file is NULL return value is number of pointers to write, else
1913 * it is the number of type format characters written */
1914 static int write_varying_array_pointer_descriptions(
1915 FILE *file, const attr_list_t *attrs, type_t *type,
1916 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1917 unsigned int *typestring_offset)
1920 int pointer_count = 0;
1922 if (is_array(type) && type_array_has_variance(type))
1924 unsigned int temp = 0;
1925 /* unfortunately, this needs to be done in two passes to avoid
1926 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1927 pointer_count = write_pointer_description_offsets(
1928 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1929 if (pointer_count > 0)
1931 unsigned int increment_size;
1934 increment_size = type_memsize(type_array_get_element(type), &align);
1936 if (increment_size > USHRT_MAX)
1937 error("array size of %u bytes is too large\n", increment_size);
1939 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1940 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1941 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1942 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1943 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1944 *typestring_offset += 8;
1946 pointer_count = write_pointer_description_offsets(
1947 file, attrs, type_array_get_element(type), offset_in_memory,
1948 offset_in_buffer, typestring_offset);
1951 else if (type_get_type(type) == TYPE_STRUCT)
1954 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1956 if (offset_in_memory && offset_in_buffer)
1958 unsigned int padding;
1960 if (is_array(v->type) && type_array_has_variance(v->type))
1962 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1963 /* skip over variance and offset in buffer */
1964 *offset_in_buffer += 8;
1968 type_memsize(v->type, &align);
1969 padding = ROUNDING(*offset_in_memory, align);
1970 *offset_in_memory += padding;
1971 *offset_in_buffer += padding;
1973 pointer_count += write_varying_array_pointer_descriptions(
1974 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1980 if (offset_in_memory && offset_in_buffer)
1982 unsigned int memsize;
1984 memsize = type_memsize(type, &align);
1985 *offset_in_memory += memsize;
1986 /* increment these separately as in the case of conformant (varying)
1987 * structures these start at different values */
1988 *offset_in_buffer += memsize;
1992 return pointer_count;
1995 static void write_pointer_description(FILE *file, type_t *type,
1996 unsigned int *typestring_offset)
1998 unsigned int offset_in_buffer;
1999 unsigned int offset_in_memory;
2001 /* pass 1: search for single instance of a pointer (i.e. don't descend
2003 if (!is_array(type))
2005 offset_in_memory = 0;
2006 offset_in_buffer = 0;
2007 write_no_repeat_pointer_descriptions(
2009 &offset_in_memory, &offset_in_buffer, typestring_offset);
2012 /* pass 2: search for pointers in fixed arrays */
2013 offset_in_memory = 0;
2014 offset_in_buffer = 0;
2015 write_fixed_array_pointer_descriptions(
2017 &offset_in_memory, &offset_in_buffer, typestring_offset);
2019 /* pass 3: search for pointers in conformant only arrays (but don't descend
2020 * into conformant varying or varying arrays) */
2021 if (is_conformant_array(type) &&
2022 (type_array_is_decl_as_ptr(type) || !current_structure))
2023 write_conformant_array_pointer_descriptions(
2024 file, NULL, type, 0, typestring_offset);
2025 else if (type_get_type(type) == TYPE_STRUCT &&
2026 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2028 unsigned int align = 0;
2029 type_t *carray = find_array_or_string_in_struct(type)->type;
2030 write_conformant_array_pointer_descriptions(
2032 type_memsize(type, &align),
2036 /* pass 4: search for pointers in varying arrays */
2037 offset_in_memory = 0;
2038 offset_in_buffer = 0;
2039 write_varying_array_pointer_descriptions(
2041 &offset_in_memory, &offset_in_buffer, typestring_offset);
2044 int is_declptr(const type_t *t)
2046 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2049 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2050 type_t *type, int toplevel_param,
2051 const char *name, unsigned int *typestring_offset)
2053 unsigned int start_offset;
2054 unsigned char rtype;
2057 start_offset = *typestring_offset;
2058 update_tfsoff(type, start_offset, file);
2060 if (is_declptr(type))
2062 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2063 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2065 pointer_type = RPC_FC_RP;
2066 print_start_tfs_comment(file, type, *typestring_offset);
2067 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2068 pointer_type, flag, string_of_type(pointer_type),
2069 flag ? " [simple_pointer]" : "");
2070 *typestring_offset += 2;
2073 print_file(file, 2, "NdrFcShort(0x2),\n");
2074 *typestring_offset += 2;
2079 elem_type = type_array_get_element(type);
2081 elem_type = type_pointer_get_ref(type);
2083 if (type_get_type(elem_type) != TYPE_BASIC)
2085 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2086 return start_offset;
2089 rtype = get_basic_fc(elem_type);
2090 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2092 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2093 return start_offset;
2096 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2098 unsigned int dim = type_array_get_dim(type);
2100 /* FIXME: multi-dimensional array */
2102 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2103 name, 0xffffu, dim - 0xffffu);
2105 if (rtype == RPC_FC_WCHAR)
2106 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2108 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2109 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2110 *typestring_offset += 2;
2112 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2113 *typestring_offset += 2;
2115 return start_offset;
2117 else if (is_conformant_array(type))
2119 unsigned int align = 0;
2121 if (rtype == RPC_FC_WCHAR)
2122 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2124 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2125 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2126 *typestring_offset += 2;
2128 *typestring_offset += write_conf_or_var_desc(
2129 file, current_structure,
2130 (!type_array_is_decl_as_ptr(type) && current_structure
2131 ? type_memsize(current_structure, &align)
2133 type, type_array_get_conformance(type));
2135 return start_offset;
2139 if (rtype == RPC_FC_WCHAR)
2140 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2142 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2143 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2144 *typestring_offset += 2;
2146 return start_offset;
2150 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2151 const char *name, unsigned int *typestring_offset)
2153 const expr_t *length_is = type_array_get_variance(type);
2154 const expr_t *size_is = type_array_get_conformance(type);
2155 unsigned int align = 0;
2157 unsigned int start_offset;
2160 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2161 unsigned int baseoff
2162 = !type_array_is_decl_as_ptr(type) && current_structure
2163 ? type_memsize(current_structure, &align)
2167 pointer_type = RPC_FC_RP;
2169 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2172 has_pointer = type_has_pointers(type_array_get_element(type));
2175 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2176 fc = get_array_fc(type);
2178 start_offset = *typestring_offset;
2179 update_tfsoff(type, start_offset, file);
2180 print_start_tfs_comment(file, type, start_offset);
2181 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2182 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2183 *typestring_offset += 2;
2186 if (fc != RPC_FC_BOGUS_ARRAY)
2188 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2190 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2191 *typestring_offset += 4;
2195 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2196 *typestring_offset += 2;
2199 if (is_conformant_array(type))
2201 += write_conf_or_var_desc(file, current_structure, baseoff,
2204 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2206 unsigned int elalign = 0;
2207 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2208 unsigned int dim = type_array_get_dim(type);
2210 if (fc == RPC_FC_LGVARRAY)
2212 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2213 *typestring_offset += 4;
2217 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2218 *typestring_offset += 2;
2221 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2222 *typestring_offset += 2;
2227 += write_conf_or_var_desc(file, current_structure, baseoff,
2230 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2232 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2233 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2234 *typestring_offset += 2;
2235 write_pointer_description(file, type, typestring_offset);
2236 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2237 *typestring_offset += 1;
2240 write_array_element_type(file, type, FALSE, typestring_offset);
2241 write_end(file, typestring_offset);
2245 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2246 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2247 *typestring_offset += 2;
2249 += write_conf_or_var_desc(file, current_structure, baseoff,
2252 += write_conf_or_var_desc(file, current_structure, baseoff,
2255 write_array_element_type(file, type, TRUE, typestring_offset);
2256 write_end(file, typestring_offset);
2259 return start_offset;
2262 static const var_t *find_array_or_string_in_struct(const type_t *type)
2264 const var_list_t *fields = type_struct_get_fields(type);
2265 const var_t *last_field;
2268 if (!fields || list_empty(fields))
2271 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2272 ft = last_field->type;
2274 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2277 if (type_get_type(ft) == TYPE_STRUCT)
2278 return find_array_or_string_in_struct(ft);
2283 static void write_struct_members(FILE *file, const type_t *type,
2284 int is_complex, unsigned int *corroff,
2285 unsigned int *typestring_offset)
2288 unsigned short offset = 0;
2289 unsigned int salign = 1;
2291 var_list_t *fields = type_struct_get_fields(type);
2293 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2295 type_t *ft = field->type;
2296 unsigned int align = 0;
2297 unsigned int size = type_memsize(ft, &align);
2298 align = clamp_align(align);
2299 if (salign < align) salign = align;
2301 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2303 if ((align - 1) & offset)
2305 unsigned char fc = 0;
2309 fc = RPC_FC_ALIGNM2;
2312 fc = RPC_FC_ALIGNM4;
2315 fc = RPC_FC_ALIGNM8;
2318 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2320 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2321 offset = ROUND_SIZE(offset, align);
2322 *typestring_offset += 1;
2324 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2330 padding = ROUNDING(offset, salign);
2333 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2334 RPC_FC_STRUCTPAD1 + padding - 1,
2336 *typestring_offset += 1;
2339 write_end(file, typestring_offset);
2342 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2343 const char *name, unsigned int *tfsoff)
2345 const type_t *save_current_structure = current_structure;
2346 unsigned int total_size;
2348 unsigned int start_offset;
2349 unsigned int array_offset;
2350 int has_pointers = 0;
2351 unsigned int align = 0;
2352 unsigned int corroff;
2354 unsigned char fc = get_struct_fc(type);
2355 var_list_t *fields = type_struct_get_fields(type);
2358 current_structure = type;
2360 total_size = type_memsize(type, &align);
2361 if (total_size > USHRT_MAX)
2362 error("structure size for %s exceeds %d bytes by %d bytes\n",
2363 name, USHRT_MAX, total_size - USHRT_MAX);
2365 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2366 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2368 if (!has_pointers) has_pointers = type_has_pointers(type);
2370 array = find_array_or_string_in_struct(type);
2371 if (array && !processed(array->type))
2373 = is_string_type(array->attrs, array->type)
2374 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2375 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2378 write_descriptors(file, type, tfsoff);
2380 start_offset = *tfsoff;
2381 update_tfsoff(type, start_offset, file);
2382 print_start_tfs_comment(file, type, start_offset);
2383 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2384 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2385 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2390 unsigned int absoff = array->type->typestring_offset;
2391 short reloff = absoff - *tfsoff;
2392 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2393 reloff, reloff, absoff);
2396 else if (fc == RPC_FC_BOGUS_STRUCT)
2398 print_file(file, 2, "NdrFcShort(0x0),\n");
2402 if (fc == RPC_FC_BOGUS_STRUCT)
2404 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2405 nothing is written to file yet. On the actual writing pass,
2406 this will have been updated. */
2407 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2408 int reloff = absoff - *tfsoff;
2409 assert( reloff >= 0 );
2410 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2411 reloff, reloff, absoff);
2414 else if ((fc == RPC_FC_PSTRUCT) ||
2415 (fc == RPC_FC_CPSTRUCT) ||
2416 (fc == RPC_FC_CVSTRUCT && has_pointers))
2418 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2419 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2421 write_pointer_description(file, type, tfsoff);
2422 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2426 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2429 if (fc == RPC_FC_BOGUS_STRUCT)
2433 type->ptrdesc = *tfsoff;
2434 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2436 type_t *ft = f->type;
2439 if (is_string_type(f->attrs, ft))
2440 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2442 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2444 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2446 unsigned int offset;
2448 print_file(file, 0, "/* %d */\n", *tfsoff);
2450 offset = ft->typestring_offset;
2451 /* skip over the pointer that is written for strings, since a
2452 * pointer has to be written in-place here */
2453 if (is_string_type(f->attrs, ft))
2455 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2458 if (type->ptrdesc == *tfsoff)
2462 current_structure = save_current_structure;
2463 return start_offset;
2466 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2470 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2474 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2477 if (type_get_type(t) == TYPE_BASIC)
2478 fc = get_basic_fc(t);
2480 fc = get_enum_fc(t);
2481 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2482 fc, string_of_type(fc));
2484 else if (t->typestring_offset)
2486 short reloff = t->typestring_offset - *tfsoff;
2487 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2488 reloff, reloff, t->typestring_offset);
2491 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2497 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2500 unsigned int start_offset;
2503 unsigned int nbranch = 0;
2504 type_t *deftype = NULL;
2505 short nodeftype = 0xffff;
2511 size = type_memsize(type, &align);
2513 fields = type_union_get_cases(type);
2515 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2517 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2519 nbranch += list_count(cases);
2521 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2524 start_offset = *tfsoff;
2525 update_tfsoff(type, start_offset, file);
2526 print_start_tfs_comment(file, type, start_offset);
2527 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2529 const var_t *sv = type_union_get_switch_value(type);
2530 const type_t *st = sv->type;
2533 if (type_get_type(st) == TYPE_BASIC)
2535 switch (get_basic_fc(st))
2546 fc = get_basic_fc(st);
2550 error("union switch type must be an integer, char, or enum\n");
2553 else if (type_get_type(st) == TYPE_ENUM)
2554 fc = get_enum_fc(st);
2556 error("union switch type must be an integer, char, or enum\n");
2558 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2559 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2560 0x40 | fc, string_of_type(fc));
2563 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2565 static const expr_t dummy_expr; /* FIXME */
2566 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2569 if (type_get_type(st) == TYPE_BASIC)
2571 switch (get_basic_fc(st))
2582 fc = get_basic_fc(st);
2586 error("union switch type must be an integer, char, or enum\n");
2589 else if (type_get_type(st) == TYPE_ENUM)
2590 fc = get_enum_fc(st);
2592 error("union switch type must be an integer, char, or enum\n");
2594 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2595 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2596 fc, string_of_type(fc));
2599 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2600 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2602 print_file(file, 0, "/* %u */\n", *tfsoff);
2605 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2606 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2609 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2611 type_t *ft = f->type;
2612 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2613 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2616 if (cases == NULL && !deflt)
2617 error("union field %s with neither case nor default attribute\n", f->name);
2619 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2621 /* MIDL doesn't check for duplicate cases, even though that seems
2622 like a reasonable thing to do, it just dumps them to the TFS
2623 like we're going to do here. */
2624 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2626 write_branch_type(file, ft, tfsoff);
2629 /* MIDL allows multiple default branches, even though that seems
2630 illogical, it just chooses the last one, which is what we will
2641 write_branch_type(file, deftype, tfsoff);
2645 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2649 return start_offset;
2652 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2653 unsigned int *typeformat_offset)
2656 unsigned int start_offset = *typeformat_offset;
2657 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2661 print_file(file, 2, "0x2f, /* FC_IP */\n");
2662 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2664 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2668 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2669 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2672 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2674 update_tfsoff(type, start_offset, file);
2675 print_start_tfs_comment(file, type, start_offset);
2676 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2677 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2678 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2679 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2680 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2681 for (i = 0; i < 8; ++i)
2682 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2685 fprintf(file, "\n");
2687 *typeformat_offset += 18;
2689 return start_offset;
2692 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2694 unsigned int *typeformat_offset)
2696 unsigned int start_offset = *typeformat_offset;
2697 unsigned char flags = 0;
2699 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2700 flags |= NDR_STRICT_CONTEXT_HANDLE;
2704 if (is_attr(var->attrs, ATTR_IN))
2707 if (!is_attr(var->attrs, ATTR_OUT))
2708 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2710 if (is_attr(var->attrs, ATTR_OUT))
2713 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2714 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2715 /* return and can't be null values overlap */
2716 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2717 print_file(file, 0, "can't be null, ");
2718 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2719 print_file(file, 0, "serialize, ");
2720 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2721 print_file(file, 0, "no serialize, ");
2722 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2723 print_file(file, 0, "strict, ");
2724 if ((flags & 0x21) == 0x20)
2725 print_file(file, 0, "out, ");
2726 if ((flags & 0x21) == 0x21)
2727 print_file(file, 0, "return, ");
2729 print_file(file, 0, "in, ");
2731 print_file(file, 0, "via ptr, ");
2732 print_file(file, 0, "*/\n");
2733 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2734 print_file(file, 2, "0, /* FIXME: param num */\n");
2735 *typeformat_offset += 4;
2737 return start_offset;
2740 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2741 type_t *type, const var_t *var,
2743 unsigned int *typeformat_offset)
2745 unsigned int offset;
2747 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2749 case TGT_CTXT_HANDLE:
2750 case TGT_CTXT_HANDLE_POINTER:
2751 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2753 write_user_tfs(file, type, typeformat_offset);
2754 return type->typestring_offset;
2756 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2761 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2762 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2763 if (ptr_type != RPC_FC_RP)
2765 unsigned int absoff = type->typestring_offset;
2766 short reloff = absoff - (*typeformat_offset + 2);
2767 off = *typeformat_offset;
2768 print_file(file, 0, "/* %d */\n", off);
2769 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2770 string_of_type(ptr_type));
2771 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2772 reloff, reloff, absoff);
2773 *typeformat_offset += 4;
2778 if (processed(type)) return type->typestring_offset;
2779 return write_struct_tfs(file, type, var->name, typeformat_offset);
2781 if (processed(type)) return type->typestring_offset;
2782 return write_union_tfs(file, type, typeformat_offset);
2787 case TGT_IFACE_POINTER:
2788 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2792 size_t start_offset = *typeformat_offset;
2793 int in_attr = is_attr(var->attrs, ATTR_IN);
2794 int out_attr = is_attr(var->attrs, ATTR_OUT);
2795 const type_t *ref = type_pointer_get_ref(type);
2797 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2799 /* special case for pointers to base types */
2805 if (type_get_type(ref) == TYPE_ENUM)
2806 fc = get_enum_fc(ref);
2808 fc = get_basic_fc(ref);
2810 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2811 get_pointer_fc(type, var->attrs, toplevel_param),
2812 (!in_attr && out_attr) ? 0x0C : 0x08,
2813 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2814 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2815 print_file(file, indent, "0x%02x, /* %s */\n",
2816 fc, string_of_type(fc));
2817 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2818 *typeformat_offset += 4;
2819 return start_offset;
2826 offset = write_typeformatstring_var(file, indent, func,
2827 type_pointer_get_ref(type), var,
2828 FALSE, typeformat_offset);
2830 fprintf(file, "/* %2u */\n", *typeformat_offset);
2831 return write_nonsimple_pointer(file, var->attrs, type,
2833 offset, typeformat_offset);
2837 error("invalid type %s for var %s\n", type->name, var->name);
2841 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2842 const char *name, int write_ptr, unsigned int *tfsoff)
2846 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2849 write_user_tfs(file, type, tfsoff);
2852 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2854 case TGT_IFACE_POINTER:
2855 write_ip_tfs(file, attrs, type, tfsoff);
2859 type_t *ref = type_pointer_get_ref(type);
2861 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2862 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2865 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2871 /* conformant arrays and strings are handled specially */
2872 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2874 write_array_tfs(file, attrs, type, name, tfsoff);
2875 if (is_conformant_array(type))
2880 if (!processed(type))
2881 write_struct_tfs(file, type, name, tfsoff);
2884 if (!processed(type))
2885 write_union_tfs(file, type, tfsoff);
2891 case TGT_CTXT_HANDLE:
2892 case TGT_CTXT_HANDLE_POINTER:
2894 error("invalid type %s for var %s\n", type->name, name);
2901 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2902 type_pred_t pred, unsigned int *typeformat_offset)
2905 const statement_t *stmt;
2907 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2909 const type_t *iface;
2910 const statement_t *stmt_func;
2912 if (stmt->type == STMT_LIBRARY)
2914 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2917 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2920 iface = stmt->u.type;
2924 current_iface = iface;
2925 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2927 const var_t *func = stmt_func->u.var;
2928 if (is_local(func->attrs)) continue;
2930 if (!is_void(type_function_get_rettype(func->type)))
2933 v.type = type_function_get_rettype(func->type);
2934 update_tfsoff(type_function_get_rettype(func->type),
2935 write_typeformatstring_var(
2937 type_function_get_rettype(func->type),
2938 &v, FALSE, typeformat_offset),
2942 current_func = func;
2943 if (type_get_function_args(func->type))
2944 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2947 write_typeformatstring_var(
2948 file, 2, func, var->type, var,
2949 TRUE, typeformat_offset),
2954 return *typeformat_offset + 1;
2957 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2959 unsigned int typeformat_offset = 2;
2961 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2965 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2969 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2970 print_file(file, indent, "{\n");
2972 print_file(file, indent, "0,\n");
2973 print_file(file, indent, "{\n");
2975 print_file(file, indent, "NdrFcShort(0x0),\n");
2977 set_all_tfswrite(TRUE);
2978 process_tfs(file, stmts, pred);
2980 print_file(file, indent, "0x0\n");
2982 print_file(file, indent, "}\n");
2984 print_file(file, indent, "};\n");
2985 print_file(file, indent, "\n");
2988 static unsigned int get_required_buffer_size_type(
2989 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2992 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2997 const type_t *utype = get_user_type(type, &uname);
2998 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3001 switch (get_basic_fc(type))
3019 case RPC_FC_ERROR_STATUS_T:
3029 case RPC_FC_BIND_PRIMITIVE:
3033 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3034 get_basic_fc(type));
3040 switch (get_enum_fc(type))
3052 if (get_struct_fc(type) == RPC_FC_STRUCT)
3054 if (!type_struct_get_fields(type)) return 0;
3055 return fields_memsize(type_struct_get_fields(type), alignment);
3060 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3062 const type_t *ref = type_pointer_get_ref(type);
3063 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3067 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3069 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3070 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3073 case TGT_CTXT_HANDLE:
3074 case TGT_CTXT_HANDLE_POINTER:
3078 case TGT_IFACE_POINTER:
3087 /* FIXME: depends on pointer type */
3088 return type_array_get_dim(type) *
3089 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3097 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3099 int in_attr = is_attr(var->attrs, ATTR_IN);
3100 int out_attr = is_attr(var->attrs, ATTR_OUT);
3102 if (!in_attr && !out_attr)
3107 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3108 pass == PASS_RETURN)
3110 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3116 if (!is_string_type(var->attrs, var->type))
3117 return get_required_buffer_size_type(var->type, var->name,
3118 var->attrs, TRUE, alignment);
3123 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3126 unsigned int total_size = 0, alignment;
3128 if (type_get_function_args(func->type))
3130 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3132 total_size += get_required_buffer_size(var, &alignment, pass);
3133 total_size += alignment;
3137 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3140 v.type = type_function_get_rettype(func->type);
3141 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3142 total_size += alignment;
3147 static void print_phase_function(FILE *file, int indent, const char *type,
3148 const char *local_var_prefix, enum remoting_phase phase,
3149 const var_t *var, unsigned int type_offset)
3151 const char *function;
3154 case PHASE_BUFFERSIZE:
3155 function = "BufferSize";
3158 function = "Marshall";
3160 case PHASE_UNMARSHAL:
3161 function = "Unmarshall";
3171 print_file(file, indent, "Ndr%s%s(\n", type, function);
3173 print_file(file, indent, "&__frame->_StubMsg,\n");
3174 print_file(file, indent, "%s%s%s%s%s,\n",
3175 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3176 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3178 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3180 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3181 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3182 if (phase == PHASE_UNMARSHAL)
3183 print_file(file, indent, "0);\n");
3187 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3188 enum remoting_phase phase, enum pass pass, const var_t *var,
3189 const char *varname)
3191 type_t *type = var->type;
3193 unsigned int alignment = 0;
3196 /* no work to do for other phases, buffer sizing is done elsewhere */
3197 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3200 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3201 if (type_get_type(ref) == TYPE_ENUM)
3203 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3208 else /* RPC_FC_ENUM16 */
3216 switch (get_basic_fc(ref))
3236 case RPC_FC_ERROR_STATUS_T:
3248 case RPC_FC_BIND_PRIMITIVE:
3249 /* no marshalling needed */
3253 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3254 var->name, get_basic_fc(ref));
3259 if (phase == PHASE_MARSHAL)
3260 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3261 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3262 alignment - 1, alignment - 1);
3264 if (phase == PHASE_MARSHAL)
3266 print_file(file, indent, "*(");
3267 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3269 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3271 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3272 fprintf(file, "%s%s", local_var_prefix, varname);
3273 fprintf(file, ";\n");
3275 else if (phase == PHASE_UNMARSHAL)
3277 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3278 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3279 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3280 print_file(file, indent, "{\n");
3281 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3282 print_file(file, indent, "}\n");
3283 print_file(file, indent, "%s%s%s",
3284 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3285 local_var_prefix, varname);
3286 if (pass == PASS_IN && is_ptr(type))
3287 fprintf(file, " = (");
3289 fprintf(file, " = *(");
3290 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3291 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3294 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3295 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3296 fprintf(file, ");\n");
3299 /* returns whether the MaxCount, Offset or ActualCount members need to be
3300 * filled in for the specified phase */
3301 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3303 return (phase != PHASE_UNMARSHAL);
3306 expr_t *get_size_is_expr(const type_t *t, const char *name)
3310 for ( ; is_array(t); t = type_array_get_element(t))
3311 if (type_array_has_conformance(t))
3314 x = type_array_get_conformance(t);
3316 error("%s: multidimensional conformant"
3317 " arrays not supported at the top level\n",
3324 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3325 enum remoting_phase phase, const var_t *var)
3327 const type_t *type = var->type;
3328 /* get fundamental type for the argument */
3331 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3333 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3335 else if (type_is_alias(type))
3336 type = type_alias_get_aliasee(type);
3337 else if (is_array(type))
3339 if (is_conformance_needed_for_phase(phase) && is_array(type))
3341 if (type_array_has_conformance(type))
3343 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3344 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3345 fprintf(file, ";\n\n");
3347 if (type_array_has_variance(type))
3349 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3350 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3351 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3352 fprintf(file, ";\n\n");
3357 else if (type_get_type(type) == TYPE_UNION)
3359 if (is_conformance_needed_for_phase(phase))
3361 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3362 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3363 fprintf(file, ";\n\n");
3367 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3371 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3373 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3374 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3375 fprintf( file, ";\n\n" );
3379 else if (is_ptr(type))
3380 type = type_pointer_get_ref(type);
3386 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3387 enum pass pass, enum remoting_phase phase, const var_t *var)
3389 int in_attr, out_attr, pointer_type;
3390 const type_t *type = var->type;
3391 unsigned int start_offset = type->typestring_offset;
3393 if (is_ptr(type) || is_array(type))
3394 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3398 in_attr = is_attr(var->attrs, ATTR_IN);
3399 out_attr = is_attr(var->attrs, ATTR_OUT);
3400 if (!in_attr && !out_attr)
3403 if (phase != PHASE_FREE)
3407 if (!in_attr) return;
3410 if (!out_attr) return;
3416 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3418 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3420 case TGT_CTXT_HANDLE:
3421 case TGT_CTXT_HANDLE_POINTER:
3422 if (phase == PHASE_MARSHAL)
3424 if (pass == PASS_IN)
3426 /* if the context_handle attribute appears in the chain of types
3427 * without pointers being followed, then the context handle must
3428 * be direct, otherwise it is a pointer */
3429 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3430 print_file(file, indent, "NdrClientContextMarshall(\n");
3431 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3432 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3433 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3437 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3438 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3439 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3440 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3441 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3444 else if (phase == PHASE_UNMARSHAL)
3446 if (pass == PASS_OUT)
3449 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3450 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3451 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3452 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3453 print_file(file, indent + 1, "__frame->_Handle);\n");
3457 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3458 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3459 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3464 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3467 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3468 pointer_type != RPC_FC_RP)
3470 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3471 !in_attr && is_conformant_array(type))
3473 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3475 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3477 /* strings returned are assumed to be global and hence don't
3479 else if (is_declptr(type) &&
3480 !(phase == PHASE_FREE && pass == PASS_RETURN))
3481 print_phase_function(file, indent, "Pointer", local_var_prefix,
3482 phase, var, start_offset);
3486 unsigned int real_start_offset = start_offset;
3487 /* skip over pointer description straight to string description */
3488 if (is_declptr(type))
3490 if (is_conformant_array(type))
3491 real_start_offset += 4;
3493 real_start_offset += 2;
3495 if (is_array(type) && !is_conformant_array(type))
3496 print_phase_function(file, indent, "NonConformantString",
3497 local_var_prefix, phase, var,
3500 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3501 phase, var, real_start_offset);
3506 unsigned char tc = get_array_fc(type);
3507 const char *array_type = "FixedArray";
3509 /* We already have the size_is expression since it's at the
3510 top level, but do checks for multidimensional conformant
3511 arrays. When we handle them, we'll need to extend this
3512 function to return a list, and then we'll actually use
3513 the return value. */
3514 get_size_is_expr(type, var->name);
3516 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3518 array_type = "VaryingArray";
3520 else if (tc == RPC_FC_CARRAY)
3522 array_type = "ConformantArray";
3524 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3526 array_type = (tc == RPC_FC_BOGUS_ARRAY
3528 : "ConformantVaryingArray");
3531 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3532 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3533 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3535 /* these are all unmarshalled by allocating memory */
3536 if (tc == RPC_FC_BOGUS_ARRAY ||
3537 tc == RPC_FC_CVARRAY ||
3538 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3539 (tc == RPC_FC_CARRAY && !in_attr))
3541 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3543 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3549 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3550 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3553 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3555 if (phase == PHASE_MARSHAL)
3556 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3558 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3559 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3560 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3563 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3567 switch (get_struct_fc(type))
3570 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3571 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3573 case RPC_FC_PSTRUCT:
3574 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3576 case RPC_FC_CSTRUCT:
3577 case RPC_FC_CPSTRUCT:
3578 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3580 case RPC_FC_CVSTRUCT:
3581 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3583 case RPC_FC_BOGUS_STRUCT:
3584 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3587 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3592 const char *union_type = NULL;
3594 if (type_get_type(type) == TYPE_UNION)
3595 union_type = "NonEncapsulatedUnion";
3596 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3597 union_type = "EncapsulatedUnion";
3599 print_phase_function(file, indent, union_type, local_var_prefix,
3600 phase, var, start_offset);
3605 const type_t *ref = type_pointer_get_ref(type);
3606 if (pointer_type == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3609 /* base types have known sizes, so don't need a sizing pass
3610 * and don't have any memory to free and so don't need a
3612 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3613 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3616 /* base types have known sizes, so don't need a sizing pass
3617 * and don't have any memory to free and so don't need a
3619 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3620 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3624 const char *struct_type = NULL;
3625 switch (get_struct_fc(ref))
3628 /* simple structs have known sizes, so don't need a sizing
3629 * pass and don't have any memory to free and so don't
3630 * need a freeing pass */
3631 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3632 struct_type = "SimpleStruct";
3633 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3635 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3637 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3641 case RPC_FC_PSTRUCT:
3642 struct_type = "SimpleStruct";
3644 case RPC_FC_CSTRUCT:
3645 case RPC_FC_CPSTRUCT:
3646 struct_type = "ConformantStruct";
3648 case RPC_FC_CVSTRUCT:
3649 struct_type = "ConformantVaryingStruct";
3651 case RPC_FC_BOGUS_STRUCT:
3652 struct_type = "ComplexStruct";
3655 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3660 if (phase == PHASE_FREE)
3661 struct_type = "Pointer";
3663 start_offset = ref->typestring_offset;
3664 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3669 case TYPE_ENCAPSULATED_UNION:
3671 const char *union_type = NULL;
3672 if (phase == PHASE_FREE)
3673 union_type = "Pointer";
3676 if (type_get_type(ref) == TYPE_UNION)
3677 union_type = "NonEncapsulatedUnion";
3678 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3679 union_type = "EncapsulatedUnion";
3681 start_offset = ref->typestring_offset;
3684 print_phase_function(file, indent, union_type, local_var_prefix,
3685 phase, var, start_offset);
3690 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3697 case TYPE_INTERFACE:
3702 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3705 case TGT_IFACE_POINTER:
3706 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3712 fprintf(file, "\n");
3715 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3716 enum pass pass, enum remoting_phase phase)
3718 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3720 unsigned int size = get_function_buffer_size( func, pass );
3721 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3724 if (pass == PASS_RETURN)
3728 var.type = type_function_get_rettype(func->type);
3729 var.name = xstrdup( "_RetVal" );
3730 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3736 if (!type_get_function_args(func->type))
3738 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3739 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3744 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3746 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3750 unsigned int get_size_procformatstring_func(const var_t *func)
3753 unsigned int size = 0;
3755 /* argument list size */
3756 if (type_get_function_args(func->type))
3757 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3758 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3760 /* return value size */
3761 if (is_void(type_function_get_rettype(func->type)))
3762 size += 2; /* FC_END and FC_PAD */
3764 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3769 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3771 const statement_t *stmt;
3772 unsigned int size = 1;
3774 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3776 const type_t *iface;
3777 const statement_t *stmt_func;
3779 if (stmt->type == STMT_LIBRARY)
3781 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3784 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3787 iface = stmt->u.type;
3791 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3793 const var_t *func = stmt_func->u.var;
3794 if (!is_local(func->attrs))
3795 size += get_size_procformatstring_func( func );
3801 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3803 set_all_tfswrite(FALSE);
3804 return process_tfs(NULL, stmts, pred);
3807 void declare_stub_args( FILE *file, int indent, const var_t *func )
3809 int in_attr, out_attr;
3813 /* declare return value '_RetVal' */
3814 if (!is_void(type_function_get_rettype(func->type)))
3816 print_file(file, indent, "%s", "");
3817 write_type_decl_left(file, type_function_get_rettype(func->type));
3818 fprintf(file, " _RetVal;\n");
3821 if (!type_get_function_args(func->type))
3824 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3826 in_attr = is_attr(var->attrs, ATTR_IN);
3827 out_attr = is_attr(var->attrs, ATTR_OUT);
3828 if (!out_attr && !in_attr)
3831 if (is_context_handle(var->type))
3832 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3835 if (!in_attr && !is_conformant_array(var->type))
3837 type_t *type_to_print;
3839 print_file(file, indent, "%s", "");
3840 if (type_get_type(var->type) == TYPE_ARRAY &&
3841 !type_array_is_decl_as_ptr(var->type))
3842 type_to_print = var->type;
3844 type_to_print = type_pointer_get_ref(var->type);
3845 sprintf(name, "_W%u", i++);
3846 write_type_decl(file, type_to_print, name);
3847 fprintf(file, ";\n");
3850 print_file(file, indent, "%s", "");
3851 write_type_decl_left(file, var->type);
3853 if (type_get_type(var->type) == TYPE_ARRAY &&
3854 !type_array_is_decl_as_ptr(var->type)) {
3855 fprintf(file, "(*%s)", var->name);
3857 fprintf(file, "%s", var->name);
3858 write_type_right(file, var->type, FALSE);
3859 fprintf(file, ";\n");
3861 if (decl_indirect(var->type))
3862 print_file(file, indent, "void *_p_%s;\n", var->name);
3868 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3870 int in_attr, out_attr;
3874 if (!type_get_function_args(func->type))
3877 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3879 in_attr = is_attr(var->attrs, ATTR_IN);
3880 out_attr = is_attr(var->attrs, ATTR_OUT);
3881 if (!out_attr && !in_attr)
3886 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3888 if (is_context_handle(var->type))
3890 fprintf(file, " = NdrContextHandleInitialize(\n");
3891 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3892 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3893 var->type->typestring_offset);
3895 else if (is_array(var->type) &&
3896 type_array_has_conformance(var->type))
3898 unsigned int size, align = 0;
3899 type_t *type = var->type;
3901 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3903 is_array(type) && type_array_has_conformance(type);
3904 type = type_array_get_element(type))
3906 write_expr(file, type_array_get_conformance(type), TRUE,
3907 TRUE, NULL, NULL, local_var_prefix);
3908 fprintf(file, " * ");
3910 size = type_memsize(type, &align);
3911 fprintf(file, "%u);\n", size);
3915 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3916 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3921 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3926 case TGT_IFACE_POINTER:
3928 case TGT_CTXT_HANDLE:
3929 case TGT_CTXT_HANDLE_POINTER:
3932 /* not initialised */
3942 fprintf(file, "\n");
3946 int write_expr_eval_routines(FILE *file, const char *iface)
3948 static const char *var_name = "pS";
3949 static const char *var_name_expr = "pS->";
3951 struct expr_eval_routine *eval;
3952 unsigned short callback_offset = 0;
3954 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3956 const char *name = eval->structure->name;
3959 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3960 iface, name, callback_offset);
3961 print_file(file, 0, "{\n");
3962 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3963 name, var_name, name, eval->baseoff);
3964 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3965 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3966 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3967 fprintf(file, ";\n");
3968 print_file(file, 0, "}\n\n");
3974 void write_expr_eval_routine_list(FILE *file, const char *iface)
3976 struct expr_eval_routine *eval;
3977 struct expr_eval_routine *cursor;
3978 unsigned short callback_offset = 0;
3980 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3981 fprintf(file, "{\n");
3983 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3985 const char *name = eval->structure->name;
3986 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3988 list_remove(&eval->entry);
3992 fprintf(file, "};\n\n");
3995 void write_user_quad_list(FILE *file)
3999 if (list_empty(&user_type_list))
4002 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4003 fprintf(file, "{\n");
4004 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4006 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4007 print_file(file, 1, "{\n");
4008 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4009 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4010 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4011 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4012 print_file(file, 1, "}%s\n", sep);
4014 fprintf(file, "};\n\n");
4017 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4019 const struct str_list_entry_t *endpoint;
4022 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4023 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4024 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4026 print_file( f, 1, "{ (const unsigned char *)\"" );
4027 for (p = endpoint->str; *p && *p != ':'; p++)
4029 if (*p == '"' || *p == '\\') fputc( '\\', f );
4032 if (!*p) goto error;
4033 if (p[1] != '[') goto error;
4035 fprintf( f, "\", (const unsigned char *)\"" );
4036 for (p += 2; *p && *p != ']'; p++)
4038 if (*p == '"' || *p == '\\') fputc( '\\', f );
4041 if (*p != ']') goto error;
4042 fprintf( f, "\" },\n" );
4044 print_file( f, 0, "};\n\n" );
4048 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4051 void write_exceptions( FILE *file )
4053 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4054 fprintf( file, "\n");
4055 fprintf( file, "#include \"wine/exception.h\"\n");
4056 fprintf( file, "#undef RpcTryExcept\n");
4057 fprintf( file, "#undef RpcExcept\n");
4058 fprintf( file, "#undef RpcEndExcept\n");
4059 fprintf( file, "#undef RpcTryFinally\n");
4060 fprintf( file, "#undef RpcFinally\n");
4061 fprintf( file, "#undef RpcEndFinally\n");
4062 fprintf( file, "#undef RpcExceptionCode\n");
4063 fprintf( file, "#undef RpcAbnormalTermination\n");
4064 fprintf( file, "\n");
4065 fprintf( file, "struct __exception_frame;\n");
4066 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4067 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4068 fprintf( file, "\n");
4069 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4070 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4071 fprintf( file, " __filter_func filter; \\\n");
4072 fprintf( file, " __finally_func finally; \\\n");
4073 fprintf( file, " sigjmp_buf jmp; \\\n");
4074 fprintf( file, " DWORD code; \\\n");
4075 fprintf( file, " unsigned char abnormal_termination; \\\n");
4076 fprintf( file, " unsigned char filter_level; \\\n");
4077 fprintf( file, " unsigned char finally_level;\n");
4078 fprintf( file, "\n");
4079 fprintf( file, "struct __exception_frame\n{\n");
4080 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4081 fprintf( file, "};\n");
4082 fprintf( file, "\n");
4083 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4084 fprintf( file, "{\n");
4085 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4086 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4087 fprintf( file, " {\n");
4088 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4089 fprintf( file, " exc_frame->finally( exc_frame );\n");
4090 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4091 fprintf( file, " }\n");
4092 fprintf( file, " exc_frame->filter_level = 0;\n");
4093 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4094 fprintf( file, "}\n");
4095 fprintf( file, "\n");
4096 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4097 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4098 fprintf( file, " CONTEXT *context,\n");
4099 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4100 fprintf( file, "{\n");
4101 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4102 fprintf( file, "\n");
4103 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4104 fprintf( file, " {\n" );
4105 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4106 fprintf( file, " {\n" );
4107 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4108 fprintf( file, " exc_frame->finally( exc_frame );\n");
4109 fprintf( file, " }\n" );
4110 fprintf( file, " return ExceptionContinueSearch;\n");
4111 fprintf( file, " }\n" );
4112 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4113 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4114 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4115 fprintf( file, " return ExceptionContinueSearch;\n");
4116 fprintf( file, "}\n");
4117 fprintf( file, "\n");
4118 fprintf( file, "#define RpcTryExcept \\\n");
4119 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4120 fprintf( file, " { \\\n");
4121 fprintf( file, " if (!__frame->finally_level) \\\n" );
4122 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4123 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4124 fprintf( file, "\n");
4125 fprintf( file, "#define RpcExcept(expr) \\\n");
4126 fprintf( file, " if (!__frame->finally_level) \\\n" );
4127 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4128 fprintf( file, " __frame->filter_level = 0; \\\n" );
4129 fprintf( file, " } \\\n");
4130 fprintf( file, " else \\\n");
4131 fprintf( file, "\n");
4132 fprintf( file, "#define RpcEndExcept\n");
4133 fprintf( file, "\n");
4134 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4135 fprintf( file, "\n");
4136 fprintf( file, "#define RpcTryFinally \\\n");
4137 fprintf( file, " if (!__frame->filter_level) \\\n");
4138 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4139 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4140 fprintf( file, "\n");
4141 fprintf( file, "#define RpcFinally \\\n");
4142 fprintf( file, " if (!__frame->filter_level) \\\n");
4143 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4144 fprintf( file, " __frame->finally_level = 0;\n");
4145 fprintf( file, "\n");
4146 fprintf( file, "#define RpcEndFinally\n");
4147 fprintf( file, "\n");
4148 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4149 fprintf( file, "\n");
4150 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4151 fprintf( file, " do { \\\n");
4152 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4153 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4154 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4155 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4156 fprintf( file, " __frame->filter_level = 0; \\\n");
4157 fprintf( file, " __frame->finally_level = 0; \\\n");
4158 fprintf( file, " } while (0)\n");
4159 fprintf( file, "\n");
4160 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4161 fprintf( file, "\n");
4162 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4163 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4164 fprintf( file, "\n");
4165 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4166 fprintf( file, " DWORD code;\n");
4167 fprintf( file, "\n");
4168 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");