push c22b4aa826b27f0c6bc5101b378e6e68716a85d9
[wine/hacks.git] / tools / widl / typegen.c
blob7810f80d50758554b3e2b1c68e63842083bac557
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const func_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
62 static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
63 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
66 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
68 type_t *type,
69 const char *name, unsigned int *typestring_offset);
71 const char *string_of_type(unsigned char type)
73 switch (type)
75 case RPC_FC_BYTE: return "FC_BYTE";
76 case RPC_FC_CHAR: return "FC_CHAR";
77 case RPC_FC_SMALL: return "FC_SMALL";
78 case RPC_FC_USMALL: return "FC_USMALL";
79 case RPC_FC_WCHAR: return "FC_WCHAR";
80 case RPC_FC_SHORT: return "FC_SHORT";
81 case RPC_FC_USHORT: return "FC_USHORT";
82 case RPC_FC_LONG: return "FC_LONG";
83 case RPC_FC_ULONG: return "FC_ULONG";
84 case RPC_FC_FLOAT: return "FC_FLOAT";
85 case RPC_FC_HYPER: return "FC_HYPER";
86 case RPC_FC_DOUBLE: return "FC_DOUBLE";
87 case RPC_FC_ENUM16: return "FC_ENUM16";
88 case RPC_FC_ENUM32: return "FC_ENUM32";
89 case RPC_FC_IGNORE: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP: return "FC_RP";
92 case RPC_FC_UP: return "FC_UP";
93 case RPC_FC_OP: return "FC_OP";
94 case RPC_FC_FP: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY: return "FC_CARRAY";
108 case RPC_FC_CVARRAY: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112 case RPC_FC_POINTER: return "FC_POINTER";
113 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING: return "FC_CSTRING";
116 case RPC_FC_WSTRING: return "FC_WSTRING";
117 default:
118 error("string_of_type: unknown type 0x%02x\n", type);
119 return NULL;
123 static int get_struct_type(const type_t *type)
125 int has_pointer = 0;
126 int has_conformance = 0;
127 int has_variance = 0;
128 var_t *field;
129 var_list_t *fields;
131 if (type->type != RPC_FC_STRUCT) return type->type;
133 fields = type_struct_get_fields(type);
135 if (get_padding(fields))
136 return RPC_FC_BOGUS_STRUCT;
138 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
140 type_t *t = field->type;
142 if (is_user_type(t))
143 return RPC_FC_BOGUS_STRUCT;
145 if (field->type->declarray)
147 if (is_string_type(field->attrs, field->type))
149 if (is_conformant_array(field->type))
150 has_conformance = 1;
151 has_variance = 1;
152 continue;
155 if (is_array(field->type->ref))
156 return RPC_FC_BOGUS_STRUCT;
158 if (is_conformant_array(field->type))
160 has_conformance = 1;
161 if (field->type->declarray && list_next(fields, &field->entry))
162 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
163 field->name);
165 if (field->type->length_is)
166 has_variance = 1;
168 t = field->type->ref;
171 switch (get_struct_type(t))
174 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
175 * Simple types don't effect the type of struct.
176 * A struct containing a simple struct is still a simple struct.
177 * So long as we can block copy the data, we return RPC_FC_STRUCT.
179 case 0: /* void pointer */
180 case RPC_FC_BYTE:
181 case RPC_FC_CHAR:
182 case RPC_FC_SMALL:
183 case RPC_FC_USMALL:
184 case RPC_FC_WCHAR:
185 case RPC_FC_SHORT:
186 case RPC_FC_USHORT:
187 case RPC_FC_LONG:
188 case RPC_FC_ULONG:
189 case RPC_FC_INT3264:
190 case RPC_FC_UINT3264:
191 case RPC_FC_HYPER:
192 case RPC_FC_FLOAT:
193 case RPC_FC_DOUBLE:
194 case RPC_FC_STRUCT:
195 case RPC_FC_ENUM32:
196 break;
198 case RPC_FC_RP:
199 return RPC_FC_BOGUS_STRUCT;
201 case RPC_FC_UP:
202 case RPC_FC_FP:
203 case RPC_FC_OP:
204 if (pointer_size != 4)
205 return RPC_FC_BOGUS_STRUCT;
206 /* pointers to interfaces aren't really pointers and have to be
207 * marshalled specially so they make the structure complex */
208 if (t->ref->type == RPC_FC_IP)
209 return RPC_FC_BOGUS_STRUCT;
210 has_pointer = 1;
211 break;
213 case RPC_FC_CARRAY:
214 case RPC_FC_CVARRAY:
215 case RPC_FC_BOGUS_ARRAY:
217 unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
218 if (!ptr_type || ptr_type == RPC_FC_RP)
219 return RPC_FC_BOGUS_STRUCT;
220 else if (pointer_size != 4)
221 return RPC_FC_BOGUS_STRUCT;
222 has_pointer = 1;
223 break;
227 * Propagate member attributes
228 * a struct should be at least as complex as its member
230 case RPC_FC_CVSTRUCT:
231 has_conformance = 1;
232 has_variance = 1;
233 has_pointer = 1;
234 break;
236 case RPC_FC_CPSTRUCT:
237 has_conformance = 1;
238 if (list_next( fields, &field->entry ))
239 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
240 field->name);
241 has_pointer = 1;
242 break;
244 case RPC_FC_CSTRUCT:
245 has_conformance = 1;
246 if (list_next( fields, &field->entry ))
247 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
248 field->name);
249 break;
251 case RPC_FC_PSTRUCT:
252 has_pointer = 1;
253 break;
255 default:
256 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
257 /* fallthru - treat it as complex */
259 /* as soon as we see one of these these members, it's bogus... */
260 case RPC_FC_ENCAPSULATED_UNION:
261 case RPC_FC_NON_ENCAPSULATED_UNION:
262 case RPC_FC_BOGUS_STRUCT:
263 case RPC_FC_ENUM16:
264 return RPC_FC_BOGUS_STRUCT;
268 if( has_variance )
270 if ( has_conformance )
271 return RPC_FC_CVSTRUCT;
272 else
273 return RPC_FC_BOGUS_STRUCT;
275 if( has_conformance && has_pointer )
276 return RPC_FC_CPSTRUCT;
277 if( has_conformance )
278 return RPC_FC_CSTRUCT;
279 if( has_pointer )
280 return RPC_FC_PSTRUCT;
281 return RPC_FC_STRUCT;
284 static int get_array_type(const type_t *type)
286 if (is_array(type))
288 const type_t *rt = type->ref;
289 if (is_user_type(rt))
290 return RPC_FC_BOGUS_ARRAY;
291 switch (get_struct_type(rt))
293 case RPC_FC_BOGUS_STRUCT:
294 case RPC_FC_NON_ENCAPSULATED_UNION:
295 case RPC_FC_ENCAPSULATED_UNION:
296 case RPC_FC_ENUM16:
297 return RPC_FC_BOGUS_ARRAY;
298 /* FC_RP should be above, but widl overuses these, and will break things. */
299 case RPC_FC_UP:
300 case RPC_FC_RP:
301 if (rt->ref->type == RPC_FC_IP) return RPC_FC_BOGUS_ARRAY;
302 break;
305 if (type->type == RPC_FC_LGFARRAY || type->type == RPC_FC_LGVARRAY)
307 unsigned int align = 0;
308 size_t size = type_memsize(type, &align);
309 if (size * type->dim <= 0xffff)
310 return (type->type == RPC_FC_LGFARRAY) ? RPC_FC_SMFARRAY : RPC_FC_SMVARRAY;
313 return type->type;
316 int is_struct(unsigned char type)
318 switch (type)
320 case RPC_FC_STRUCT:
321 case RPC_FC_PSTRUCT:
322 case RPC_FC_CSTRUCT:
323 case RPC_FC_CPSTRUCT:
324 case RPC_FC_CVSTRUCT:
325 case RPC_FC_BOGUS_STRUCT:
326 return 1;
327 default:
328 return 0;
332 static int is_non_complex_struct(const type_t *type)
334 switch (get_struct_type(type))
336 case RPC_FC_STRUCT:
337 case RPC_FC_PSTRUCT:
338 case RPC_FC_CSTRUCT:
339 case RPC_FC_CPSTRUCT:
340 case RPC_FC_CVSTRUCT:
341 return 1;
342 default:
343 return 0;
347 int is_union(unsigned char type)
349 switch (type)
351 case RPC_FC_ENCAPSULATED_UNION:
352 case RPC_FC_NON_ENCAPSULATED_UNION:
353 return 1;
354 default:
355 return 0;
359 static int type_has_pointers(const type_t *type)
361 if (is_user_type(type))
362 return FALSE;
363 else if (is_ptr(type))
364 return TRUE;
365 else if (is_array(type))
366 return type_has_pointers(type->ref);
367 else if (is_struct(type->type))
369 var_list_t *fields = type_struct_get_fields(type);
370 const var_t *field;
371 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
373 if (type_has_pointers(field->type))
374 return TRUE;
377 else if (is_union(type->type))
379 var_list_t *fields;
380 const var_t *field;
381 fields = type_union_get_cases(type);
382 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
384 if (field->type && type_has_pointers(field->type))
385 return TRUE;
389 return FALSE;
392 static int type_has_full_pointer(const type_t *type)
394 if (is_user_type(type))
395 return FALSE;
396 else if (type->type == RPC_FC_FP)
397 return TRUE;
398 else if (is_ptr(type))
399 return FALSE;
400 else if (is_array(type))
401 return type_has_full_pointer(type->ref);
402 else if (is_struct(type->type))
404 var_list_t *fields = type_struct_get_fields(type);
405 const var_t *field;
406 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
408 if (type_has_full_pointer(field->type))
409 return TRUE;
412 else if (is_union(type->type))
414 var_list_t *fields;
415 const var_t *field;
416 fields = type_union_get_cases(type);
417 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
419 if (field->type && type_has_full_pointer(field->type))
420 return TRUE;
424 return FALSE;
427 static unsigned short user_type_offset(const char *name)
429 user_type_t *ut;
430 unsigned short off = 0;
431 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
433 if (strcmp(name, ut->name) == 0)
434 return off;
435 ++off;
437 error("user_type_offset: couldn't find type (%s)\n", name);
438 return 0;
441 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
443 type->typestring_offset = offset;
444 if (file) type->tfswrite = FALSE;
447 static void guard_rec(type_t *type)
449 /* types that contain references to themselves (like a linked list),
450 need to be shielded from infinite recursion when writing embedded
451 types */
452 if (type->typestring_offset)
453 type->tfswrite = FALSE;
454 else
455 type->typestring_offset = 1;
458 static type_t *get_user_type(const type_t *t, const char **pname)
460 for (;;)
462 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
463 if (ut)
465 if (pname)
466 *pname = t->name;
467 return ut;
470 if (t->kind == TKIND_ALIAS)
471 t = t->orig;
472 else
473 return 0;
477 int is_user_type(const type_t *t)
479 return get_user_type(t, NULL) != NULL;
482 static int is_embedded_complex(const type_t *type)
484 unsigned char tc = type->type;
485 return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
486 || (is_ptr(type) && type->ref->type == RPC_FC_IP);
489 static const char *get_context_handle_type_name(const type_t *type)
491 const type_t *t;
492 for (t = type; is_ptr(t); t = t->ref)
493 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
494 return t->name;
495 assert(0);
496 return NULL;
499 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
500 do { \
501 if (file) \
502 fprintf(file, "/* %2u */\n", typestring_offset); \
503 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
505 while (0)
507 static void print_file(FILE *file, int indent, const char *format, ...)
509 va_list va;
510 va_start(va, format);
511 print(file, indent, format, va);
512 va_end(va);
515 void print(FILE *file, int indent, const char *format, va_list va)
517 if (file)
519 if (format[0] != '\n')
520 while (0 < indent--)
521 fprintf(file, " ");
522 vfprintf(file, format, va);
527 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
529 if (decl_indirect(t))
531 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
532 local_var_prefix, n, local_var_prefix, n);
533 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
535 else if (is_ptr(t) || is_array(t))
536 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
539 void write_parameters_init(FILE *file, int indent, const func_t *func, const char *local_var_prefix)
541 const var_t *var;
543 if (!is_void(get_func_return_type(func)))
544 write_var_init(file, indent, get_func_return_type(func), "_RetVal", local_var_prefix);
546 if (!func->args)
547 return;
549 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
550 write_var_init(file, indent, var->type, var->name, local_var_prefix);
552 fprintf(file, "\n");
555 static void write_formatdesc(FILE *f, int indent, const char *str)
557 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
558 print_file(f, indent, "{\n");
559 print_file(f, indent + 1, "short Pad;\n");
560 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
561 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
562 print_file(f, indent, "\n");
565 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
567 clear_all_offsets();
569 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
570 get_size_typeformatstring(stmts, pred));
572 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
573 get_size_procformatstring(stmts, pred));
575 fprintf(f, "\n");
576 write_formatdesc(f, indent, "TYPE");
577 write_formatdesc(f, indent, "PROC");
578 fprintf(f, "\n");
579 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
580 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
581 print_file(f, indent, "\n");
584 static inline int is_base_type(unsigned char type)
586 switch (type)
588 case RPC_FC_BYTE:
589 case RPC_FC_CHAR:
590 case RPC_FC_USMALL:
591 case RPC_FC_SMALL:
592 case RPC_FC_WCHAR:
593 case RPC_FC_USHORT:
594 case RPC_FC_SHORT:
595 case RPC_FC_ULONG:
596 case RPC_FC_LONG:
597 case RPC_FC_HYPER:
598 case RPC_FC_IGNORE:
599 case RPC_FC_FLOAT:
600 case RPC_FC_DOUBLE:
601 case RPC_FC_ENUM16:
602 case RPC_FC_ENUM32:
603 case RPC_FC_ERROR_STATUS_T:
604 case RPC_FC_BIND_PRIMITIVE:
605 return TRUE;
607 default:
608 return FALSE;
612 int decl_indirect(const type_t *t)
614 return is_user_type(t)
615 || (!is_base_type(t->type)
616 && !is_ptr(t)
617 && !is_array(t));
620 static size_t write_procformatstring_type(FILE *file, int indent,
621 const char *name,
622 const type_t *type,
623 const attr_list_t *attrs,
624 int is_return)
626 size_t size;
628 int is_in = is_attr(attrs, ATTR_IN);
629 int is_out = is_attr(attrs, ATTR_OUT);
631 if (!is_in && !is_out) is_in = TRUE;
633 if (!type->declarray && is_base_type(type->type))
635 if (is_return)
636 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
637 else
638 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
640 if (type->type == RPC_FC_BIND_PRIMITIVE)
642 print_file(file, indent, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE);
643 size = 2; /* includes param type prefix */
645 else if (is_base_type(type->type))
647 print_file(file, indent, "0x%02x, /* %s */\n", type->type, string_of_type(type->type));
648 size = 2; /* includes param type prefix */
650 else
652 error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
653 size = 0;
656 else
658 if (is_return)
659 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
660 else if (is_in && is_out)
661 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
662 else if (is_out)
663 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
664 else
665 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
667 print_file(file, indent, "0x01,\n");
668 print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
669 size = 4; /* includes param type prefix */
671 return size;
674 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
676 const statement_t *stmt;
677 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
679 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
681 const func_t *func;
682 if (!pred(stmt->u.type))
683 continue;
684 if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
686 if (is_local(func->def->attrs)) continue;
687 /* emit argument data */
688 if (func->args)
690 const var_t *var;
691 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
692 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
695 /* emit return value data */
696 if (is_void(get_func_return_type(func)))
698 print_file(file, indent, "0x5b, /* FC_END */\n");
699 print_file(file, indent, "0x5c, /* FC_PAD */\n");
701 else
702 write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
705 else if (stmt->type == STMT_LIBRARY)
706 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
710 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
712 int indent = 0;
714 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
715 print_file(file, indent, "{\n");
716 indent++;
717 print_file(file, indent, "0,\n");
718 print_file(file, indent, "{\n");
719 indent++;
721 write_procformatstring_stmts(file, indent, stmts, pred);
723 print_file(file, indent, "0x0\n");
724 indent--;
725 print_file(file, indent, "}\n");
726 indent--;
727 print_file(file, indent, "};\n");
728 print_file(file, indent, "\n");
731 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
733 if (is_base_type(type->type))
735 print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
736 *typestring_offset += 1;
737 return 1;
740 return 0;
743 /* write conformance / variance descriptor */
744 static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
745 unsigned int baseoff, const type_t *type,
746 const expr_t *expr)
748 unsigned char operator_type = 0;
749 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
750 const char *conftype_string = "";
751 const char *operator_string = "no operators";
752 const expr_t *subexpr;
754 if (!expr)
756 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
757 return 4;
760 if (!structure)
762 /* Top-level conformance calculations are done inline. */
763 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
764 RPC_FC_TOP_LEVEL_CONFORMANCE);
765 print_file (file, 2, "0x0,\n");
766 print_file (file, 2, "NdrFcShort(0x0),\n");
767 return 4;
770 if (expr->is_const)
772 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
773 error("write_conf_or_var_desc: constant value %ld is greater than "
774 "the maximum constant size of %d\n", expr->cval,
775 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
777 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
778 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
779 print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
780 print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
782 return 4;
785 if (is_ptr(type) || (is_array(type) && !type->declarray))
787 conftype = RPC_FC_POINTER_CONFORMANCE;
788 conftype_string = "field pointer, ";
791 subexpr = expr;
792 switch (subexpr->type)
794 case EXPR_PPTR:
795 subexpr = subexpr->ref;
796 operator_type = RPC_FC_DEREFERENCE;
797 operator_string = "FC_DEREFERENCE";
798 break;
799 case EXPR_DIV:
800 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
802 subexpr = subexpr->ref;
803 operator_type = RPC_FC_DIV_2;
804 operator_string = "FC_DIV_2";
806 break;
807 case EXPR_MUL:
808 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
810 subexpr = subexpr->ref;
811 operator_type = RPC_FC_MULT_2;
812 operator_string = "FC_MULT_2";
814 break;
815 case EXPR_SUB:
816 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
818 subexpr = subexpr->ref;
819 operator_type = RPC_FC_SUB_1;
820 operator_string = "FC_SUB_1";
822 break;
823 case EXPR_ADD:
824 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
826 subexpr = subexpr->ref;
827 operator_type = RPC_FC_ADD_1;
828 operator_string = "FC_ADD_1";
830 break;
831 default:
832 break;
835 if (subexpr->type == EXPR_IDENTIFIER)
837 const type_t *correlation_variable = NULL;
838 unsigned char correlation_variable_type;
839 unsigned char param_type = 0;
840 size_t offset = 0;
841 const var_t *var;
842 var_list_t *fields = type_struct_get_fields(structure);
844 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
846 unsigned int align = 0;
847 /* FIXME: take alignment into account */
848 if (var->name && !strcmp(var->name, subexpr->u.sval))
850 correlation_variable = var->type;
851 break;
853 offset += type_memsize(var->type, &align);
855 if (!correlation_variable)
856 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
857 subexpr->u.sval);
859 correlation_variable = expr_resolve_type(NULL, structure, expr);
861 offset -= baseoff;
862 correlation_variable_type = correlation_variable->type;
864 switch (correlation_variable_type)
866 case RPC_FC_CHAR:
867 case RPC_FC_SMALL:
868 param_type = RPC_FC_SMALL;
869 break;
870 case RPC_FC_BYTE:
871 case RPC_FC_USMALL:
872 param_type = RPC_FC_USMALL;
873 break;
874 case RPC_FC_WCHAR:
875 case RPC_FC_SHORT:
876 case RPC_FC_ENUM16:
877 param_type = RPC_FC_SHORT;
878 break;
879 case RPC_FC_USHORT:
880 param_type = RPC_FC_USHORT;
881 break;
882 case RPC_FC_LONG:
883 case RPC_FC_ENUM32:
884 param_type = RPC_FC_LONG;
885 break;
886 case RPC_FC_ULONG:
887 param_type = RPC_FC_ULONG;
888 break;
889 default:
890 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
891 correlation_variable_type);
894 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
895 conftype | param_type, conftype_string, string_of_type(param_type));
896 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
897 print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
898 offset, offset);
900 else
902 unsigned int callback_offset = 0;
903 struct expr_eval_routine *eval;
904 int found = 0;
906 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
908 if (!strcmp (eval->structure->name, structure->name)
909 && !compare_expr (eval->expr, expr))
911 found = 1;
912 break;
914 callback_offset++;
917 if (!found)
919 eval = xmalloc (sizeof(*eval));
920 eval->structure = structure;
921 eval->baseoff = baseoff;
922 eval->expr = expr;
923 list_add_tail (&expr_eval_routines, &eval->entry);
926 if (callback_offset > USHRT_MAX)
927 error("Maximum number of callback routines reached\n");
929 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
930 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
931 print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
933 return 4;
936 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
938 int have_align = FALSE;
939 size_t size = 0;
940 const var_t *v;
942 if (!fields) return 0;
943 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
945 unsigned int falign = 0;
946 size_t fsize = type_memsize(v->type, &falign);
947 if (!have_align)
949 *align = falign;
950 have_align = TRUE;
952 size = ROUND_SIZE(size, falign);
953 size += fsize;
956 size = ROUND_SIZE(size, *align);
957 return size;
960 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
962 size_t size, maxs = 0;
963 unsigned int align = *pmaxa;
964 const var_t *v;
966 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
968 /* we could have an empty default field with NULL type */
969 if (v->type)
971 size = type_memsize(v->type, &align);
972 if (maxs < size) maxs = size;
973 if (*pmaxa < align) *pmaxa = align;
977 return maxs;
980 int get_padding(const var_list_t *fields)
982 unsigned short offset = 0;
983 int salign = -1;
984 const var_t *f;
986 if (!fields)
987 return 0;
989 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
991 type_t *ft = f->type;
992 unsigned int align = 0;
993 size_t size = type_memsize(ft, &align);
994 if (salign == -1)
995 salign = align;
996 offset = ROUND_SIZE(offset, align);
997 offset += size;
1000 return ROUNDING(offset, salign);
1003 size_t type_memsize(const type_t *t, unsigned int *align)
1005 size_t size = 0;
1007 if (t->kind == TKIND_ALIAS)
1008 size = type_memsize(t->orig, align);
1009 else if (t->declarray && is_conformant_array(t))
1011 type_memsize(t->ref, align);
1012 size = 0;
1014 else if (is_ptr(t) || is_conformant_array(t))
1016 assert( pointer_size );
1017 size = pointer_size;
1018 if (size > *align) *align = size;
1020 else switch (t->type)
1022 case RPC_FC_BYTE:
1023 case RPC_FC_CHAR:
1024 case RPC_FC_USMALL:
1025 case RPC_FC_SMALL:
1026 size = 1;
1027 if (size > *align) *align = size;
1028 break;
1029 case RPC_FC_WCHAR:
1030 case RPC_FC_USHORT:
1031 case RPC_FC_SHORT:
1032 case RPC_FC_ENUM16:
1033 size = 2;
1034 if (size > *align) *align = size;
1035 break;
1036 case RPC_FC_ULONG:
1037 case RPC_FC_LONG:
1038 case RPC_FC_ERROR_STATUS_T:
1039 case RPC_FC_ENUM32:
1040 case RPC_FC_FLOAT:
1041 size = 4;
1042 if (size > *align) *align = size;
1043 break;
1044 case RPC_FC_HYPER:
1045 case RPC_FC_DOUBLE:
1046 size = 8;
1047 if (size > *align) *align = size;
1048 break;
1049 case RPC_FC_STRUCT:
1050 case RPC_FC_CVSTRUCT:
1051 case RPC_FC_CPSTRUCT:
1052 case RPC_FC_CSTRUCT:
1053 case RPC_FC_PSTRUCT:
1054 case RPC_FC_BOGUS_STRUCT:
1055 size = fields_memsize(type_struct_get_fields(t), align);
1056 break;
1057 case RPC_FC_ENCAPSULATED_UNION:
1058 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1059 break;
1060 case RPC_FC_NON_ENCAPSULATED_UNION:
1061 size = union_memsize(type_union_get_cases(t), align);
1062 break;
1063 case RPC_FC_SMFARRAY:
1064 case RPC_FC_LGFARRAY:
1065 case RPC_FC_SMVARRAY:
1066 case RPC_FC_LGVARRAY:
1067 case RPC_FC_BOGUS_ARRAY:
1068 size = t->dim * type_memsize(t->ref, align);
1069 break;
1070 default:
1071 error("type_memsize: Unknown type %d\n", t->type);
1072 size = 0;
1075 return size;
1078 int is_full_pointer_function(const func_t *func)
1080 const var_t *var;
1081 if (type_has_full_pointer(get_func_return_type(func)))
1082 return TRUE;
1083 if (!func->args)
1084 return FALSE;
1085 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
1086 if (type_has_full_pointer( var->type ))
1087 return TRUE;
1088 return FALSE;
1091 void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
1093 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1094 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1095 fprintf(file, "\n");
1098 void write_full_pointer_free(FILE *file, int indent, const func_t *func)
1100 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1101 fprintf(file, "\n");
1104 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
1106 short absoff = type->ref->typestring_offset;
1107 short reloff = absoff - (offset + 2);
1108 int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
1110 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1111 type->type, ptr_attr, string_of_type(type->type));
1112 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1113 reloff, reloff, absoff);
1114 return 4;
1117 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1119 unsigned char fc = type->ref->type;
1120 /* for historical reasons, write_simple_pointer also handled string types,
1121 * but no longer does. catch bad uses of the function with this check */
1122 if (is_string_type(type->attrs, type))
1123 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1124 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1125 type->type, string_of_type(type->type));
1126 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1127 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1128 return 4;
1131 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1133 print_file(file, 0, "/* %u (", tfsoff);
1134 write_type_decl(file, t, NULL);
1135 print_file(file, 0, ") */\n");
1138 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1140 unsigned int offset = *typestring_offset;
1142 print_start_tfs_comment(file, type, offset);
1143 update_tfsoff(type, offset, file);
1145 if (type->ref->typestring_offset)
1146 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1147 else if (is_base_type(type->ref->type))
1148 *typestring_offset += write_simple_pointer(file, type);
1150 return offset;
1153 static int processed(const type_t *type)
1155 return type->typestring_offset && !type->tfswrite;
1158 static int user_type_has_variable_size(const type_t *t)
1160 if (is_ptr(t))
1161 return TRUE;
1162 else
1163 switch (get_struct_type(t))
1165 case RPC_FC_PSTRUCT:
1166 case RPC_FC_CSTRUCT:
1167 case RPC_FC_CPSTRUCT:
1168 case RPC_FC_CVSTRUCT:
1169 return TRUE;
1171 /* Note: Since this only applies to user types, we can't have a conformant
1172 array here, and strings should get filed under pointer in this case. */
1173 return FALSE;
1176 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1178 unsigned int start, absoff, flags;
1179 unsigned int align = 0, ualign = 0;
1180 const char *name = NULL;
1181 type_t *utype = get_user_type(type, &name);
1182 size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1183 size_t size = type_memsize(type, &align);
1184 unsigned short funoff = user_type_offset(name);
1185 short reloff;
1187 guard_rec(type);
1189 if (is_base_type(utype->type))
1191 absoff = *tfsoff;
1192 print_start_tfs_comment(file, utype, absoff);
1193 print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1194 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1195 *tfsoff += 2;
1197 else
1199 if (!processed(utype))
1200 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1201 absoff = utype->typestring_offset;
1204 if (utype->type == RPC_FC_RP)
1205 flags = 0x40;
1206 else if (utype->type == RPC_FC_UP)
1207 flags = 0x80;
1208 else
1209 flags = 0;
1211 start = *tfsoff;
1212 update_tfsoff(type, start, file);
1213 print_start_tfs_comment(file, type, start);
1214 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1215 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1216 flags | (align - 1), align - 1, flags);
1217 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1218 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1219 print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1220 *tfsoff += 8;
1221 reloff = absoff - *tfsoff;
1222 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
1223 *tfsoff += 2;
1226 static void write_member_type(FILE *file, const type_t *cont,
1227 const attr_list_t *attrs, const type_t *type,
1228 unsigned int *corroff, unsigned int *tfsoff)
1230 if (is_embedded_complex(type) && !is_conformant_array(type))
1232 size_t absoff;
1233 short reloff;
1235 if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1237 absoff = *corroff;
1238 *corroff += 8;
1240 else
1242 absoff = type->typestring_offset;
1244 reloff = absoff - (*tfsoff + 2);
1246 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1247 /* FIXME: actually compute necessary padding */
1248 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1249 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1250 reloff, reloff, absoff);
1251 *tfsoff += 4;
1253 else if (is_ptr(type) || is_conformant_array(type))
1255 unsigned char fc = (get_struct_type(cont) == RPC_FC_BOGUS_STRUCT
1256 ? RPC_FC_POINTER
1257 : RPC_FC_LONG);
1258 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1259 *tfsoff += 1;
1261 else if (!write_base_type(file, type, tfsoff))
1262 error("Unsupported member type 0x%x\n", type->type);
1265 static void write_end(FILE *file, unsigned int *tfsoff)
1267 if (*tfsoff % 2 == 0)
1269 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1270 *tfsoff += 1;
1272 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1273 *tfsoff += 1;
1276 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1278 unsigned int offset = 0;
1279 var_list_t *fs = type_struct_get_fields(type);
1280 var_t *f;
1282 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1284 unsigned int align = 0;
1285 type_t *ft = f->type;
1286 if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1288 unsigned int absoff = ft->typestring_offset;
1289 short reloff = absoff - (*tfsoff + 6);
1290 print_file(file, 0, "/* %d */\n", *tfsoff);
1291 print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1292 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1293 write_conf_or_var_desc(file, current_structure, offset, ft,
1294 get_attrp(f->attrs, ATTR_SWITCHIS));
1295 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1296 reloff, reloff, absoff);
1297 *tfsoff += 8;
1300 /* FIXME: take alignment into account */
1301 offset += type_memsize(ft, &align);
1305 static int write_no_repeat_pointer_descriptions(
1306 FILE *file, type_t *type,
1307 size_t *offset_in_memory, size_t *offset_in_buffer,
1308 unsigned int *typestring_offset)
1310 int written = 0;
1311 unsigned int align;
1313 if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1315 size_t memsize;
1317 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1318 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1320 /* pointer instance */
1321 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1322 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1323 *typestring_offset += 6;
1325 if (is_ptr(type))
1327 if (is_string_type(type->attrs, type))
1328 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1329 else
1330 write_pointer_tfs(file, type, typestring_offset);
1332 else
1334 unsigned absoff = type->typestring_offset;
1335 short reloff = absoff - (*typestring_offset + 2);
1336 /* FIXME: get pointer attributes from field */
1337 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1338 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1339 reloff, reloff, absoff);
1340 *typestring_offset += 4;
1343 align = 0;
1344 memsize = type_memsize(type, &align);
1345 *offset_in_memory += memsize;
1346 /* increment these separately as in the case of conformant (varying)
1347 * structures these start at different values */
1348 *offset_in_buffer += memsize;
1350 return 1;
1353 if (is_non_complex_struct(type))
1355 const var_t *v;
1356 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1358 if (offset_in_memory && offset_in_buffer)
1360 size_t padding;
1361 align = 0;
1362 type_memsize(v->type, &align);
1363 padding = ROUNDING(*offset_in_memory, align);
1364 *offset_in_memory += padding;
1365 *offset_in_buffer += padding;
1367 written += write_no_repeat_pointer_descriptions(
1368 file, v->type,
1369 offset_in_memory, offset_in_buffer, typestring_offset);
1372 else
1374 size_t memsize;
1375 align = 0;
1376 memsize = type_memsize(type, &align);
1377 *offset_in_memory += memsize;
1378 /* increment these separately as in the case of conformant (varying)
1379 * structures these start at different values */
1380 *offset_in_buffer += memsize;
1383 return written;
1386 static int write_pointer_description_offsets(
1387 FILE *file, const attr_list_t *attrs, type_t *type,
1388 size_t *offset_in_memory, size_t *offset_in_buffer,
1389 unsigned int *typestring_offset)
1391 int written = 0;
1392 unsigned int align;
1394 if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1396 if (offset_in_memory && offset_in_buffer)
1398 size_t memsize;
1400 /* pointer instance */
1401 /* FIXME: sometimes from end of structure, sometimes from beginning */
1402 print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1403 print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1405 align = 0;
1406 memsize = type_memsize(type, &align);
1407 *offset_in_memory += memsize;
1408 /* increment these separately as in the case of conformant (varying)
1409 * structures these start at different values */
1410 *offset_in_buffer += memsize;
1412 *typestring_offset += 4;
1414 if (is_string_type(attrs, type))
1415 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1416 else if (processed(type->ref) || is_base_type(type->ref->type))
1417 write_pointer_tfs(file, type, typestring_offset);
1418 else
1419 error("write_pointer_description_offsets: type format string unknown\n");
1421 return 1;
1424 if (is_array(type))
1426 return write_pointer_description_offsets(
1427 file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1428 typestring_offset);
1430 else if (is_non_complex_struct(type))
1432 /* otherwise search for interesting fields to parse */
1433 const var_t *v;
1434 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1436 if (offset_in_memory && offset_in_buffer)
1438 size_t padding;
1439 align = 0;
1440 type_memsize(v->type, &align);
1441 padding = ROUNDING(*offset_in_memory, align);
1442 *offset_in_memory += padding;
1443 *offset_in_buffer += padding;
1445 written += write_pointer_description_offsets(
1446 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1447 typestring_offset);
1450 else
1452 if (offset_in_memory && offset_in_buffer)
1454 size_t memsize;
1455 align = 0;
1456 memsize = type_memsize(type, &align);
1457 *offset_in_memory += memsize;
1458 /* increment these separately as in the case of conformant (varying)
1459 * structures these start at different values */
1460 *offset_in_buffer += memsize;
1464 return written;
1467 /* Note: if file is NULL return value is number of pointers to write, else
1468 * it is the number of type format characters written */
1469 static int write_fixed_array_pointer_descriptions(
1470 FILE *file, const attr_list_t *attrs, type_t *type,
1471 size_t *offset_in_memory, size_t *offset_in_buffer,
1472 unsigned int *typestring_offset)
1474 unsigned int align;
1475 int pointer_count = 0;
1476 int real_type = get_array_type( type );
1478 if (real_type == RPC_FC_SMFARRAY || real_type == RPC_FC_LGFARRAY)
1480 unsigned int temp = 0;
1481 /* unfortunately, this needs to be done in two passes to avoid
1482 * writing out redundant FC_FIXED_REPEAT descriptions */
1483 pointer_count = write_pointer_description_offsets(
1484 NULL, attrs, type->ref, NULL, NULL, &temp);
1485 if (pointer_count > 0)
1487 unsigned int increment_size;
1488 size_t offset_of_array_pointer_mem = 0;
1489 size_t offset_of_array_pointer_buf = 0;
1491 align = 0;
1492 increment_size = type_memsize(type->ref, &align);
1494 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1495 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1496 print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1497 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1498 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1499 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1500 *typestring_offset += 10;
1502 pointer_count = write_pointer_description_offsets(
1503 file, attrs, type, &offset_of_array_pointer_mem,
1504 &offset_of_array_pointer_buf, typestring_offset);
1507 else if (is_struct(type->type))
1509 const var_t *v;
1510 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1512 if (offset_in_memory && offset_in_buffer)
1514 size_t padding;
1515 align = 0;
1516 type_memsize(v->type, &align);
1517 padding = ROUNDING(*offset_in_memory, align);
1518 *offset_in_memory += padding;
1519 *offset_in_buffer += padding;
1521 pointer_count += write_fixed_array_pointer_descriptions(
1522 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1523 typestring_offset);
1526 else
1528 if (offset_in_memory && offset_in_buffer)
1530 size_t memsize;
1531 align = 0;
1532 memsize = type_memsize(type, &align);
1533 *offset_in_memory += memsize;
1534 /* increment these separately as in the case of conformant (varying)
1535 * structures these start at different values */
1536 *offset_in_buffer += memsize;
1540 return pointer_count;
1543 /* Note: if file is NULL return value is number of pointers to write, else
1544 * it is the number of type format characters written */
1545 static int write_conformant_array_pointer_descriptions(
1546 FILE *file, const attr_list_t *attrs, type_t *type,
1547 size_t offset_in_memory, unsigned int *typestring_offset)
1549 unsigned int align;
1550 int pointer_count = 0;
1552 if (is_conformant_array(type) && !type->length_is)
1554 unsigned int temp = 0;
1555 /* unfortunately, this needs to be done in two passes to avoid
1556 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1557 pointer_count = write_pointer_description_offsets(
1558 NULL, attrs, type->ref, NULL, NULL, &temp);
1559 if (pointer_count > 0)
1561 unsigned int increment_size;
1562 size_t offset_of_array_pointer_mem = offset_in_memory;
1563 size_t offset_of_array_pointer_buf = offset_in_memory;
1565 align = 0;
1566 increment_size = type_memsize(type->ref, &align);
1568 if (increment_size > USHRT_MAX)
1569 error("array size of %u bytes is too large\n", increment_size);
1571 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1572 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1573 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1574 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1575 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1576 *typestring_offset += 8;
1578 pointer_count = write_pointer_description_offsets(
1579 file, attrs, type->ref, &offset_of_array_pointer_mem,
1580 &offset_of_array_pointer_buf, typestring_offset);
1584 return pointer_count;
1587 /* Note: if file is NULL return value is number of pointers to write, else
1588 * it is the number of type format characters written */
1589 static int write_varying_array_pointer_descriptions(
1590 FILE *file, const attr_list_t *attrs, type_t *type,
1591 size_t *offset_in_memory, size_t *offset_in_buffer,
1592 unsigned int *typestring_offset)
1594 unsigned int align;
1595 int pointer_count = 0;
1597 if (is_array(type) && type->length_is)
1599 unsigned int temp = 0;
1600 /* unfortunately, this needs to be done in two passes to avoid
1601 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1602 pointer_count = write_pointer_description_offsets(
1603 NULL, attrs, type->ref, NULL, NULL, &temp);
1604 if (pointer_count > 0)
1606 unsigned int increment_size;
1608 align = 0;
1609 increment_size = type_memsize(type->ref, &align);
1611 if (increment_size > USHRT_MAX)
1612 error("array size of %u bytes is too large\n", increment_size);
1614 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1615 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1616 print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1617 print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1618 print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1619 *typestring_offset += 8;
1621 pointer_count = write_pointer_description_offsets(
1622 file, attrs, type, offset_in_memory,
1623 offset_in_buffer, typestring_offset);
1626 else if (is_struct(type->type))
1628 const var_t *v;
1629 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1631 if (offset_in_memory && offset_in_buffer)
1633 size_t padding;
1635 if (is_array(v->type) && v->type->length_is)
1637 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1638 /* skip over variance and offset in buffer */
1639 *offset_in_buffer += 8;
1642 align = 0;
1643 type_memsize(v->type, &align);
1644 padding = ROUNDING(*offset_in_memory, align);
1645 *offset_in_memory += padding;
1646 *offset_in_buffer += padding;
1648 pointer_count += write_varying_array_pointer_descriptions(
1649 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1650 typestring_offset);
1653 else
1655 if (offset_in_memory && offset_in_buffer)
1657 size_t memsize;
1658 align = 0;
1659 memsize = type_memsize(type, &align);
1660 *offset_in_memory += memsize;
1661 /* increment these separately as in the case of conformant (varying)
1662 * structures these start at different values */
1663 *offset_in_buffer += memsize;
1667 return pointer_count;
1670 static void write_pointer_description(FILE *file, type_t *type,
1671 unsigned int *typestring_offset)
1673 size_t offset_in_buffer;
1674 size_t offset_in_memory;
1676 /* pass 1: search for single instance of a pointer (i.e. don't descend
1677 * into arrays) */
1678 if (!is_array(type))
1680 offset_in_memory = 0;
1681 offset_in_buffer = 0;
1682 write_no_repeat_pointer_descriptions(
1683 file, type,
1684 &offset_in_memory, &offset_in_buffer, typestring_offset);
1687 /* pass 2: search for pointers in fixed arrays */
1688 offset_in_memory = 0;
1689 offset_in_buffer = 0;
1690 write_fixed_array_pointer_descriptions(
1691 file, NULL, type,
1692 &offset_in_memory, &offset_in_buffer, typestring_offset);
1694 /* pass 3: search for pointers in conformant only arrays (but don't descend
1695 * into conformant varying or varying arrays) */
1696 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1697 write_conformant_array_pointer_descriptions(
1698 file, NULL, type, 0, typestring_offset);
1699 else if (get_struct_type(type) == RPC_FC_CPSTRUCT)
1701 unsigned int align = 0;
1702 type_t *carray = find_array_or_string_in_struct(type)->type;
1703 write_conformant_array_pointer_descriptions(
1704 file, NULL, carray,
1705 type_memsize(type, &align),
1706 typestring_offset);
1709 /* pass 4: search for pointers in varying arrays */
1710 offset_in_memory = 0;
1711 offset_in_buffer = 0;
1712 write_varying_array_pointer_descriptions(
1713 file, NULL, type,
1714 &offset_in_memory, &offset_in_buffer, typestring_offset);
1717 int is_declptr(const type_t *t)
1719 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1722 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1723 type_t *type,
1724 const char *name, unsigned int *typestring_offset)
1726 size_t start_offset;
1727 unsigned char rtype;
1729 if (is_declptr(type))
1731 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1732 int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1733 if (!pointer_type)
1734 pointer_type = RPC_FC_RP;
1735 print_start_tfs_comment(file, type, *typestring_offset);
1736 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1737 pointer_type, flag, string_of_type(pointer_type),
1738 flag ? " [simple_pointer]" : "");
1739 *typestring_offset += 2;
1740 if (!flag)
1742 print_file(file, 2, "NdrFcShort(0x2),\n");
1743 *typestring_offset += 2;
1747 start_offset = *typestring_offset;
1748 update_tfsoff(type, start_offset, file);
1750 rtype = type->ref->type;
1752 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1754 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1755 return start_offset;
1758 if (type->declarray && !is_conformant_array(type))
1760 /* FIXME: multi-dimensional array */
1761 if (0xffffuL < type->dim)
1762 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1763 name, 0xffffu, type->dim - 0xffffu);
1765 if (rtype == RPC_FC_CHAR)
1766 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1767 else
1768 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1769 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1770 *typestring_offset += 2;
1772 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1773 *typestring_offset += 2;
1775 return start_offset;
1777 else if (type->size_is)
1779 unsigned int align = 0;
1781 if (rtype == RPC_FC_CHAR)
1782 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1783 else
1784 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1785 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1786 *typestring_offset += 2;
1788 *typestring_offset += write_conf_or_var_desc(
1789 file, current_structure,
1790 (type->declarray && current_structure
1791 ? type_memsize(current_structure, &align)
1792 : 0),
1793 type, type->size_is);
1795 return start_offset;
1797 else
1799 if (rtype == RPC_FC_WCHAR)
1800 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1801 else
1802 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1803 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1804 *typestring_offset += 2;
1806 return start_offset;
1810 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1811 const char *name, unsigned int *typestring_offset)
1813 const expr_t *length_is = type->length_is;
1814 const expr_t *size_is = type->size_is;
1815 unsigned int align = 0;
1816 size_t size;
1817 size_t start_offset;
1818 int real_type;
1819 int has_pointer;
1820 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1821 unsigned int baseoff
1822 = type->declarray && current_structure
1823 ? type_memsize(current_structure, &align)
1824 : 0;
1826 if (!pointer_type)
1827 pointer_type = RPC_FC_RP;
1829 if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1830 has_pointer = TRUE;
1831 else
1832 has_pointer = type_has_pointers(type->ref);
1834 align = 0;
1835 size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1836 real_type = get_array_type( type );
1838 start_offset = *typestring_offset;
1839 update_tfsoff(type, start_offset, file);
1840 print_start_tfs_comment(file, type, start_offset);
1841 print_file(file, 2, "0x%02x,\t/* %s */\n", real_type, string_of_type(real_type));
1842 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1843 *typestring_offset += 2;
1845 align = 0;
1846 if (real_type != RPC_FC_BOGUS_ARRAY)
1848 if (real_type == RPC_FC_LGFARRAY || real_type == RPC_FC_LGVARRAY)
1850 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1851 *typestring_offset += 4;
1853 else
1855 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1856 *typestring_offset += 2;
1859 if (is_conformant_array(type))
1860 *typestring_offset
1861 += write_conf_or_var_desc(file, current_structure, baseoff,
1862 type, size_is);
1864 if (real_type == RPC_FC_SMVARRAY || real_type == RPC_FC_LGVARRAY)
1866 unsigned int elalign = 0;
1867 size_t elsize = type_memsize(type->ref, &elalign);
1869 if (real_type == RPC_FC_LGVARRAY)
1871 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1872 *typestring_offset += 4;
1874 else
1876 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1877 *typestring_offset += 2;
1880 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1881 *typestring_offset += 2;
1884 if (length_is)
1885 *typestring_offset
1886 += write_conf_or_var_desc(file, current_structure, baseoff,
1887 type, length_is);
1889 if (has_pointer && (!type->declarray || !current_structure))
1891 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1892 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1893 *typestring_offset += 2;
1894 write_pointer_description(file, type, typestring_offset);
1895 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1896 *typestring_offset += 1;
1899 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1900 write_end(file, typestring_offset);
1902 else
1904 unsigned int dim = size_is ? 0 : type->dim;
1905 print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
1906 *typestring_offset += 2;
1907 *typestring_offset
1908 += write_conf_or_var_desc(file, current_structure, baseoff,
1909 type, size_is);
1910 *typestring_offset
1911 += write_conf_or_var_desc(file, current_structure, baseoff,
1912 type, length_is);
1913 write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1914 write_end(file, typestring_offset);
1917 return start_offset;
1920 static const var_t *find_array_or_string_in_struct(const type_t *type)
1922 const var_list_t *fields = type_struct_get_fields(type);
1923 const var_t *last_field;
1924 const type_t *ft;
1925 int real_type;
1927 if (!fields || list_empty(fields))
1928 return NULL;
1930 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
1931 ft = last_field->type;
1933 if (ft->declarray && is_conformant_array(ft))
1934 return last_field;
1936 real_type = get_struct_type( type );
1937 if (real_type == RPC_FC_CSTRUCT || real_type == RPC_FC_CPSTRUCT || real_type == RPC_FC_CVSTRUCT)
1938 return find_array_or_string_in_struct(ft);
1939 else
1940 return NULL;
1943 static void write_struct_members(FILE *file, const type_t *type,
1944 unsigned int *corroff, unsigned int *typestring_offset)
1946 const var_t *field;
1947 unsigned short offset = 0;
1948 int salign = -1;
1949 int padding;
1950 var_list_t *fields = type_struct_get_fields(type);
1952 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
1954 type_t *ft = field->type;
1955 if (!ft->declarray || !is_conformant_array(ft))
1957 unsigned int align = 0;
1958 size_t size = type_memsize(ft, &align);
1959 if (salign == -1)
1960 salign = align;
1961 if ((align - 1) & offset)
1963 unsigned char fc = 0;
1964 switch (align)
1966 case 4:
1967 fc = RPC_FC_ALIGNM4;
1968 break;
1969 case 8:
1970 fc = RPC_FC_ALIGNM8;
1971 break;
1972 default:
1973 error("write_struct_members: cannot align type %d\n", ft->type);
1975 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1976 offset = ROUND_SIZE(offset, align);
1977 *typestring_offset += 1;
1979 write_member_type(file, type, field->attrs, field->type, corroff,
1980 typestring_offset);
1981 offset += size;
1985 padding = ROUNDING(offset, salign);
1986 if (padding)
1988 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1989 RPC_FC_STRUCTPAD1 + padding - 1,
1990 padding);
1991 *typestring_offset += 1;
1994 write_end(file, typestring_offset);
1997 static size_t write_struct_tfs(FILE *file, type_t *type,
1998 const char *name, unsigned int *tfsoff)
2000 const type_t *save_current_structure = current_structure;
2001 unsigned int total_size;
2002 const var_t *array;
2003 size_t start_offset;
2004 size_t array_offset;
2005 int has_pointers = 0;
2006 unsigned int align = 0;
2007 unsigned int corroff;
2008 var_t *f;
2009 int real_type = get_struct_type( type );
2010 var_list_t *fields = type_struct_get_fields(type);
2012 guard_rec(type);
2013 current_structure = type;
2015 total_size = type_memsize(type, &align);
2016 if (total_size > USHRT_MAX)
2017 error("structure size for %s exceeds %d bytes by %d bytes\n",
2018 name, USHRT_MAX, total_size - USHRT_MAX);
2020 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2021 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2022 FALSE, tfsoff);
2023 if (!has_pointers) has_pointers = type_has_pointers(type);
2025 array = find_array_or_string_in_struct(type);
2026 if (array && !processed(array->type))
2027 array_offset
2028 = is_string_type(array->attrs, array->type)
2029 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2030 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2032 corroff = *tfsoff;
2033 write_descriptors(file, type, tfsoff);
2035 start_offset = *tfsoff;
2036 update_tfsoff(type, start_offset, file);
2037 print_start_tfs_comment(file, type, start_offset);
2038 print_file(file, 2, "0x%x,\t/* %s */\n", real_type, string_of_type(real_type));
2039 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2040 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
2041 *tfsoff += 4;
2043 if (array)
2045 unsigned int absoff = array->type->typestring_offset;
2046 short reloff = absoff - *tfsoff;
2047 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
2048 reloff, reloff, absoff);
2049 *tfsoff += 2;
2051 else if (real_type == RPC_FC_BOGUS_STRUCT)
2053 print_file(file, 2, "NdrFcShort(0x0),\n");
2054 *tfsoff += 2;
2057 if (real_type == RPC_FC_BOGUS_STRUCT)
2059 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2060 nothing is written to file yet. On the actual writing pass,
2061 this will have been updated. */
2062 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2063 int reloff = absoff - *tfsoff;
2064 assert( reloff >= 0 );
2065 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%u) */\n",
2066 reloff, reloff, absoff);
2067 *tfsoff += 2;
2069 else if ((real_type == RPC_FC_PSTRUCT) ||
2070 (real_type == RPC_FC_CPSTRUCT) ||
2071 (real_type == RPC_FC_CVSTRUCT && has_pointers))
2073 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2074 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2075 *tfsoff += 2;
2076 write_pointer_description(file, type, tfsoff);
2077 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2078 *tfsoff += 1;
2081 write_struct_members(file, type, &corroff, tfsoff);
2083 if (real_type == RPC_FC_BOGUS_STRUCT)
2085 const var_t *f;
2087 type->ptrdesc = *tfsoff;
2088 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2090 type_t *ft = f->type;
2091 if (is_ptr(ft))
2093 if (is_string_type(f->attrs, ft))
2094 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2095 else
2096 write_pointer_tfs(file, ft, tfsoff);
2098 else if (!ft->declarray && is_conformant_array(ft))
2100 unsigned int absoff = ft->typestring_offset;
2101 short reloff = absoff - (*tfsoff + 2);
2102 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2103 /* FIXME: We need to store pointer attributes for arrays
2104 so we don't lose pointer_default info. */
2105 if (ptr_type == 0)
2106 ptr_type = RPC_FC_UP;
2107 print_file(file, 0, "/* %d */\n", *tfsoff);
2108 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2109 string_of_type(ptr_type));
2110 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2111 reloff, reloff, absoff);
2112 *tfsoff += 4;
2115 if (type->ptrdesc == *tfsoff)
2116 type->ptrdesc = 0;
2119 current_structure = save_current_structure;
2120 return start_offset;
2123 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2124 unsigned char flags, size_t offset,
2125 unsigned int *typeformat_offset)
2127 size_t start_offset = *typeformat_offset;
2128 short reloff = offset - (*typeformat_offset + 2);
2129 int in_attr, out_attr;
2130 in_attr = is_attr(attrs, ATTR_IN);
2131 out_attr = is_attr(attrs, ATTR_OUT);
2132 if (!in_attr && !out_attr) in_attr = 1;
2134 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2135 flags |= 0x04;
2137 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2138 pointer_type,
2139 flags,
2140 string_of_type(pointer_type));
2141 if (file)
2143 if (flags & 0x04)
2144 fprintf(file, " [allocated_on_stack]");
2145 if (flags & 0x10)
2146 fprintf(file, " [pointer_deref]");
2147 fprintf(file, " */\n");
2150 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
2151 *typeformat_offset += 4;
2153 return start_offset;
2156 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2158 if (t == NULL)
2160 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2162 else if (is_base_type(t->type))
2164 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2165 t->type, string_of_type(t->type));
2167 else if (t->typestring_offset)
2169 short reloff = t->typestring_offset - *tfsoff;
2170 print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
2171 reloff, reloff, t->typestring_offset);
2173 else
2174 error("write_branch_type: type unimplemented (0x%x)\n", t->type);
2176 *tfsoff += 2;
2179 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2181 unsigned int align = 0;
2182 unsigned int start_offset;
2183 size_t size = type_memsize(type, &align);
2184 var_list_t *fields;
2185 size_t nbranch = 0;
2186 type_t *deftype = NULL;
2187 short nodeftype = 0xffff;
2188 var_t *f;
2190 guard_rec(type);
2192 fields = type_union_get_cases(type);
2194 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2196 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2197 if (cases)
2198 nbranch += list_count(cases);
2199 if (f->type)
2200 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2203 start_offset = *tfsoff;
2204 update_tfsoff(type, start_offset, file);
2205 print_start_tfs_comment(file, type, start_offset);
2206 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2208 const var_t *sv = type_union_get_switch_value(type);
2209 const type_t *st = sv->type;
2211 switch (st->type)
2213 case RPC_FC_CHAR:
2214 case RPC_FC_SMALL:
2215 case RPC_FC_USMALL:
2216 case RPC_FC_SHORT:
2217 case RPC_FC_USHORT:
2218 case RPC_FC_LONG:
2219 case RPC_FC_ULONG:
2220 case RPC_FC_ENUM16:
2221 case RPC_FC_ENUM32:
2222 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2223 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2224 0x40 | st->type, string_of_type(st->type));
2225 *tfsoff += 2;
2226 break;
2227 default:
2228 error("union switch type must be an integer, char, or enum\n");
2231 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2233 static const expr_t dummy_expr; /* FIXME */
2234 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2236 switch (st->type)
2238 case RPC_FC_CHAR:
2239 case RPC_FC_SMALL:
2240 case RPC_FC_USMALL:
2241 case RPC_FC_SHORT:
2242 case RPC_FC_USHORT:
2243 case RPC_FC_LONG:
2244 case RPC_FC_ULONG:
2245 case RPC_FC_ENUM16:
2246 case RPC_FC_ENUM32:
2247 print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2248 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2249 st->type, string_of_type(st->type));
2250 *tfsoff += 2;
2251 break;
2252 default:
2253 error("union switch type must be an integer, char, or enum\n");
2256 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2259 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
2260 print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
2261 *tfsoff += 4;
2263 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2265 type_t *ft = f->type;
2266 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2267 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2268 expr_t *c;
2270 if (cases == NULL && !deflt)
2271 error("union field %s with neither case nor default attribute\n", f->name);
2273 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2275 /* MIDL doesn't check for duplicate cases, even though that seems
2276 like a reasonable thing to do, it just dumps them to the TFS
2277 like we're going to do here. */
2278 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2279 *tfsoff += 4;
2280 write_branch_type(file, ft, tfsoff);
2283 /* MIDL allows multiple default branches, even though that seems
2284 illogical, it just chooses the last one, which is what we will
2285 do. */
2286 if (deflt)
2288 deftype = ft;
2289 nodeftype = 0;
2293 if (deftype)
2295 write_branch_type(file, deftype, tfsoff);
2297 else
2299 print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
2300 *tfsoff += 2;
2303 return start_offset;
2306 static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2307 unsigned int *typeformat_offset)
2309 size_t i;
2310 size_t start_offset = *typeformat_offset;
2311 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2313 if (iid)
2315 print_file(file, 2, "0x2f, /* FC_IP */\n");
2316 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2317 *typeformat_offset
2318 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2320 else
2322 const type_t *base = is_ptr(type) ? type->ref : type;
2323 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2325 if (! uuid)
2326 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2328 update_tfsoff(type, start_offset, file);
2329 print_start_tfs_comment(file, type, start_offset);
2330 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2331 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2332 print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
2333 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2334 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2335 for (i = 0; i < 8; ++i)
2336 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2338 if (file)
2339 fprintf(file, "\n");
2341 *typeformat_offset += 18;
2343 return start_offset;
2346 static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
2347 const var_t *var,
2348 unsigned int *typeformat_offset)
2350 size_t start_offset = *typeformat_offset;
2351 unsigned char flags = 0;
2353 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2354 flags |= NDR_STRICT_CONTEXT_HANDLE;
2356 if (is_ptr(type))
2357 flags |= 0x80;
2358 if (is_attr(var->attrs, ATTR_IN))
2360 flags |= 0x40;
2361 if (!is_attr(var->attrs, ATTR_OUT))
2362 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2364 if (is_attr(var->attrs, ATTR_OUT))
2365 flags |= 0x20;
2367 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2368 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2369 /* return and can't be null values overlap */
2370 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2371 print_file(file, 0, "can't be null, ");
2372 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2373 print_file(file, 0, "serialize, ");
2374 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2375 print_file(file, 0, "no serialize, ");
2376 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2377 print_file(file, 0, "strict, ");
2378 if ((flags & 0x21) == 0x20)
2379 print_file(file, 0, "out, ");
2380 if ((flags & 0x21) == 0x21)
2381 print_file(file, 0, "return, ");
2382 if (flags & 0x40)
2383 print_file(file, 0, "in, ");
2384 if (flags & 0x80)
2385 print_file(file, 0, "via ptr, ");
2386 print_file(file, 0, "*/\n");
2387 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2388 print_file(file, 2, "0, /* FIXME: param num */\n");
2389 *typeformat_offset += 4;
2391 return start_offset;
2394 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2395 type_t *type, const var_t *var,
2396 unsigned int *typeformat_offset)
2398 size_t offset;
2400 if (is_context_handle(type))
2401 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2403 if (is_user_type(type))
2405 write_user_tfs(file, type, typeformat_offset);
2406 return type->typestring_offset;
2409 if (is_string_type(var->attrs, type))
2410 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2412 if (is_array(type))
2414 int ptr_type;
2415 size_t off;
2416 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2417 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2418 /* Top level pointers to conformant arrays may be handled specially
2419 since we can bypass the pointer, but if the array is buried
2420 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2421 always need to write the pointer. */
2422 if (!ptr_type && var->type != type)
2423 /* FIXME: This should use pointer_default, but the information
2424 isn't kept around for arrays. */
2425 ptr_type = RPC_FC_UP;
2426 if (ptr_type && ptr_type != RPC_FC_RP)
2428 unsigned int absoff = type->typestring_offset;
2429 short reloff = absoff - (*typeformat_offset + 2);
2430 off = *typeformat_offset;
2431 print_file(file, 0, "/* %d */\n", off);
2432 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2433 string_of_type(ptr_type));
2434 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2435 reloff, reloff, absoff);
2436 *typeformat_offset += 4;
2438 return off;
2441 if (!is_ptr(type))
2443 /* basic types don't need a type format string */
2444 if (is_base_type(type->type))
2445 return 0;
2447 if (processed(type)) return type->typestring_offset;
2449 switch (type->type)
2451 case RPC_FC_STRUCT:
2452 case RPC_FC_PSTRUCT:
2453 case RPC_FC_CSTRUCT:
2454 case RPC_FC_CPSTRUCT:
2455 case RPC_FC_CVSTRUCT:
2456 case RPC_FC_BOGUS_STRUCT:
2457 return write_struct_tfs(file, type, var->name, typeformat_offset);
2458 case RPC_FC_ENCAPSULATED_UNION:
2459 case RPC_FC_NON_ENCAPSULATED_UNION:
2460 return write_union_tfs(file, type, typeformat_offset);
2461 case RPC_FC_IGNORE:
2462 case RPC_FC_BIND_PRIMITIVE:
2463 /* nothing to do */
2464 return 0;
2465 default:
2466 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2469 else if (last_ptr(type))
2471 size_t start_offset = *typeformat_offset;
2472 int in_attr = is_attr(var->attrs, ATTR_IN);
2473 int out_attr = is_attr(var->attrs, ATTR_OUT);
2474 const type_t *base = type->ref;
2476 if (base->type == RPC_FC_IP
2477 || (base->type == 0
2478 && is_attr(var->attrs, ATTR_IIDIS)))
2480 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2483 /* special case for pointers to base types */
2484 if (is_base_type(base->type))
2486 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2487 type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2488 string_of_type(type->type),
2489 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2490 print_file(file, indent, "0x%02x, /* %s */\n", base->type, string_of_type(base->type));
2491 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2492 *typeformat_offset += 4;
2493 return start_offset;
2497 assert(is_ptr(type));
2499 offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
2500 if (file)
2501 fprintf(file, "/* %2u */\n", *typeformat_offset);
2502 return write_pointer_only_tfs(file, var->attrs, type->type,
2503 !last_ptr(type) ? 0x10 : 0,
2504 offset, typeformat_offset);
2507 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2508 const char *name, int write_ptr, unsigned int *tfsoff)
2510 int retmask = 0;
2512 if (is_user_type(type))
2514 write_user_tfs(file, type, tfsoff);
2516 else if (is_string_type(attrs, type))
2518 write_string_tfs(file, attrs, type, name, tfsoff);
2520 else if (is_ptr(type))
2522 type_t *ref = type->ref;
2524 if (ref->type == RPC_FC_IP
2525 || (ref->type == 0
2526 && is_attr(attrs, ATTR_IIDIS)))
2528 write_ip_tfs(file, attrs, type, tfsoff);
2530 else
2532 if (!processed(ref) && !is_base_type(ref->type))
2533 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2535 if (write_ptr)
2536 write_pointer_tfs(file, type, tfsoff);
2538 retmask |= 1;
2541 else if (type->declarray && is_conformant_array(type))
2542 ; /* conformant arrays and strings are handled specially */
2543 else if (is_array(type))
2545 write_array_tfs(file, attrs, type, name, tfsoff);
2546 if (is_conformant_array(type))
2547 retmask |= 1;
2549 else if (is_struct(type->type))
2551 if (!processed(type))
2552 write_struct_tfs(file, type, name, tfsoff);
2554 else if (is_union(type->type))
2556 if (!processed(type))
2557 write_union_tfs(file, type, tfsoff);
2559 else if (!is_base_type(type->type))
2560 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2561 name, type->type);
2563 return retmask;
2566 static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2567 type_pred_t pred, unsigned int *typeformat_offset)
2569 const var_t *var;
2570 const statement_t *stmt;
2572 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2574 const type_t *iface;
2575 if (stmt->type == STMT_LIBRARY)
2577 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2578 continue;
2580 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2581 continue;
2583 iface = stmt->u.type;
2584 if (!pred(iface))
2585 continue;
2587 if (iface->funcs)
2589 const func_t *func;
2590 current_iface = iface;
2591 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2593 if (is_local(func->def->attrs)) continue;
2595 if (!is_void(get_func_return_type(func)))
2597 var_t v = *func->def;
2598 v.type = get_func_return_type(func);
2599 update_tfsoff(get_func_return_type(func),
2600 write_typeformatstring_var(
2601 file, 2, NULL, get_func_return_type(func),
2602 &v, typeformat_offset),
2603 file);
2606 current_func = func;
2607 if (func->args)
2608 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2609 update_tfsoff(
2610 var->type,
2611 write_typeformatstring_var(
2612 file, 2, func, var->type, var,
2613 typeformat_offset),
2614 file);
2619 return *typeformat_offset + 1;
2622 static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2624 unsigned int typeformat_offset = 2;
2626 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2630 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2632 int indent = 0;
2634 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2635 print_file(file, indent, "{\n");
2636 indent++;
2637 print_file(file, indent, "0,\n");
2638 print_file(file, indent, "{\n");
2639 indent++;
2640 print_file(file, indent, "NdrFcShort(0x0),\n");
2642 set_all_tfswrite(TRUE);
2643 process_tfs(file, stmts, pred);
2645 print_file(file, indent, "0x0\n");
2646 indent--;
2647 print_file(file, indent, "}\n");
2648 indent--;
2649 print_file(file, indent, "};\n");
2650 print_file(file, indent, "\n");
2653 static unsigned int get_required_buffer_size_type(
2654 const type_t *type, const char *name, unsigned int *alignment)
2656 const char *uname;
2657 const type_t *utype;
2659 *alignment = 0;
2660 if ((utype = get_user_type(type, &uname)))
2662 return get_required_buffer_size_type(utype, uname, alignment);
2664 else
2666 switch (get_struct_type(type))
2668 case RPC_FC_BYTE:
2669 case RPC_FC_CHAR:
2670 case RPC_FC_USMALL:
2671 case RPC_FC_SMALL:
2672 *alignment = 4;
2673 return 1;
2675 case RPC_FC_WCHAR:
2676 case RPC_FC_USHORT:
2677 case RPC_FC_SHORT:
2678 case RPC_FC_ENUM16:
2679 *alignment = 4;
2680 return 2;
2682 case RPC_FC_ULONG:
2683 case RPC_FC_LONG:
2684 case RPC_FC_ENUM32:
2685 case RPC_FC_FLOAT:
2686 case RPC_FC_ERROR_STATUS_T:
2687 *alignment = 4;
2688 return 4;
2690 case RPC_FC_HYPER:
2691 case RPC_FC_DOUBLE:
2692 *alignment = 8;
2693 return 8;
2695 case RPC_FC_IGNORE:
2696 case RPC_FC_BIND_PRIMITIVE:
2697 return 0;
2699 case RPC_FC_STRUCT:
2700 if (!type_struct_get_fields(type)) return 0;
2701 return fields_memsize(type_struct_get_fields(type), alignment);
2703 case RPC_FC_RP:
2704 return
2705 is_base_type( type->ref->type ) || get_struct_type(type->ref) == RPC_FC_STRUCT
2706 ? get_required_buffer_size_type( type->ref, name, alignment )
2707 : 0;
2709 case RPC_FC_SMFARRAY:
2710 case RPC_FC_LGFARRAY:
2711 return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2713 default:
2714 return 0;
2719 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2721 int in_attr = is_attr(var->attrs, ATTR_IN);
2722 int out_attr = is_attr(var->attrs, ATTR_OUT);
2724 if (!in_attr && !out_attr)
2725 in_attr = 1;
2727 *alignment = 0;
2729 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2730 pass == PASS_RETURN)
2732 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2734 *alignment = 4;
2735 return 20;
2738 if (!is_string_type(var->attrs, var->type))
2739 return get_required_buffer_size_type(var->type, var->name,
2740 alignment);
2742 return 0;
2745 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2747 const var_t *var;
2748 unsigned int total_size = 0, alignment;
2750 if (func->args)
2752 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2754 total_size += get_required_buffer_size(var, &alignment, pass);
2755 total_size += alignment;
2759 if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2761 var_t v = *func->def;
2762 v.type = get_func_return_type(func);
2763 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2764 total_size += alignment;
2766 return total_size;
2769 static void print_phase_function(FILE *file, int indent, const char *type,
2770 const char *local_var_prefix, enum remoting_phase phase,
2771 const var_t *var, unsigned int type_offset)
2773 const char *function;
2774 switch (phase)
2776 case PHASE_BUFFERSIZE:
2777 function = "BufferSize";
2778 break;
2779 case PHASE_MARSHAL:
2780 function = "Marshall";
2781 break;
2782 case PHASE_UNMARSHAL:
2783 function = "Unmarshall";
2784 break;
2785 case PHASE_FREE:
2786 function = "Free";
2787 break;
2788 default:
2789 assert(0);
2790 return;
2793 print_file(file, indent, "Ndr%s%s(\n", type, function);
2794 indent++;
2795 print_file(file, indent, "&__frame->_StubMsg,\n");
2796 print_file(file, indent, "%s%s%s%s%s,\n",
2797 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2798 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2799 local_var_prefix,
2800 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2801 var->name);
2802 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2803 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2804 if (phase == PHASE_UNMARSHAL)
2805 print_file(file, indent, "0);\n");
2806 indent--;
2809 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2810 enum remoting_phase phase, enum pass pass, const var_t *var,
2811 const char *varname)
2813 type_t *type = var->type;
2814 unsigned int size;
2815 unsigned int alignment = 0;
2816 unsigned char rtype;
2818 /* no work to do for other phases, buffer sizing is done elsewhere */
2819 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2820 return;
2822 rtype = is_ptr(type) ? type->ref->type : type->type;
2824 switch (rtype)
2826 case RPC_FC_BYTE:
2827 case RPC_FC_CHAR:
2828 case RPC_FC_SMALL:
2829 case RPC_FC_USMALL:
2830 size = 1;
2831 alignment = 1;
2832 break;
2834 case RPC_FC_WCHAR:
2835 case RPC_FC_USHORT:
2836 case RPC_FC_SHORT:
2837 case RPC_FC_ENUM16:
2838 size = 2;
2839 alignment = 2;
2840 break;
2842 case RPC_FC_ULONG:
2843 case RPC_FC_LONG:
2844 case RPC_FC_ENUM32:
2845 case RPC_FC_FLOAT:
2846 case RPC_FC_ERROR_STATUS_T:
2847 size = 4;
2848 alignment = 4;
2849 break;
2851 case RPC_FC_HYPER:
2852 case RPC_FC_DOUBLE:
2853 size = 8;
2854 alignment = 8;
2855 break;
2857 case RPC_FC_IGNORE:
2858 case RPC_FC_BIND_PRIMITIVE:
2859 /* no marshalling needed */
2860 return;
2862 default:
2863 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2864 size = 0;
2867 if (phase == PHASE_MARSHAL)
2868 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2869 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2870 alignment - 1, alignment - 1);
2872 if (phase == PHASE_MARSHAL)
2874 print_file(file, indent, "*(");
2875 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2876 if (is_ptr(type))
2877 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2878 else
2879 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2880 fprintf(file, "%s%s", local_var_prefix, varname);
2881 fprintf(file, ";\n");
2883 else if (phase == PHASE_UNMARSHAL)
2885 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2886 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2887 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2888 print_file(file, indent, "{\n");
2889 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2890 print_file(file, indent, "}\n");
2891 if (pass == PASS_IN || pass == PASS_RETURN)
2892 print_file(file, indent, "");
2893 else
2894 print_file(file, indent, "*");
2895 fprintf(file, "%s%s", local_var_prefix, varname);
2896 if (pass == PASS_IN && is_ptr(type))
2897 fprintf(file, " = (");
2898 else
2899 fprintf(file, " = *(");
2900 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2901 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2904 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2905 write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2906 fprintf(file, ");\n");
2909 /* returns whether the MaxCount, Offset or ActualCount members need to be
2910 * filled in for the specified phase */
2911 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2913 return (phase != PHASE_UNMARSHAL);
2916 expr_t *get_size_is_expr(const type_t *t, const char *name)
2918 expr_t *x = NULL;
2920 for ( ; is_ptr(t) || is_array(t); t = t->ref)
2921 if (t->size_is)
2923 if (!x)
2924 x = t->size_is;
2925 else
2926 error("%s: multidimensional conformant"
2927 " arrays not supported at the top level\n",
2928 name);
2931 return x;
2934 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
2935 enum remoting_phase phase, const var_t *var)
2937 const type_t *type = var->type;
2938 /* get fundamental type for the argument */
2939 for (;;)
2941 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2942 break;
2943 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2944 break;
2945 else if (is_array(type) || is_string_type(var->attrs, type))
2947 if (is_conformance_needed_for_phase(phase))
2949 if (type->size_is)
2951 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2952 write_expr(file, type->size_is, 1, 1, NULL, NULL, local_var_prefix);
2953 fprintf(file, ";\n\n");
2955 if (type->length_is)
2957 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
2958 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
2959 write_expr(file, type->length_is, 1, 1, NULL, NULL, local_var_prefix);
2960 fprintf(file, ";\n\n");
2963 break;
2965 else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
2967 if (is_conformance_needed_for_phase(phase))
2969 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
2970 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
2971 fprintf(file, ";\n\n");
2973 break;
2975 else if (type->type == RPC_FC_IP)
2977 expr_t *iid;
2979 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
2981 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
2982 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
2983 fprintf( file, ";\n\n" );
2985 break;
2987 else if (is_ptr(type))
2988 type = type->ref;
2989 else
2990 break;
2994 static void write_remoting_arg(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
2995 enum pass pass, enum remoting_phase phase, const var_t *var)
2997 int in_attr, out_attr, pointer_type;
2998 const type_t *type = var->type;
2999 unsigned char rtype;
3000 size_t start_offset = type->typestring_offset;
3002 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3003 if (!pointer_type)
3004 pointer_type = RPC_FC_RP;
3006 in_attr = is_attr(var->attrs, ATTR_IN);
3007 out_attr = is_attr(var->attrs, ATTR_OUT);
3008 if (!in_attr && !out_attr)
3009 in_attr = 1;
3011 if (phase != PHASE_FREE)
3012 switch (pass)
3014 case PASS_IN:
3015 if (!in_attr) return;
3016 break;
3017 case PASS_OUT:
3018 if (!out_attr) return;
3019 break;
3020 case PASS_RETURN:
3021 break;
3024 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3025 rtype = get_struct_type(type);
3027 if (is_context_handle(type))
3029 if (phase == PHASE_MARSHAL)
3031 if (pass == PASS_IN)
3033 /* if the context_handle attribute appears in the chain of types
3034 * without pointers being followed, then the context handle must
3035 * be direct, otherwise it is a pointer */
3036 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3037 print_file(file, indent, "NdrClientContextMarshall(\n");
3038 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3039 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3040 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3042 else
3044 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3045 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3046 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3047 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3048 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3051 else if (phase == PHASE_UNMARSHAL)
3053 if (pass == PASS_OUT)
3055 if (!in_attr)
3056 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3057 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3058 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3059 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3060 print_file(file, indent + 1, "__frame->_Handle);\n");
3062 else
3064 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3065 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3066 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3070 else if (is_user_type(var->type))
3072 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3074 else if (is_string_type(var->attrs, var->type))
3076 if (is_array(type) && !is_conformant_array(type))
3077 print_phase_function(file, indent, "NonConformantString", local_var_prefix,
3078 phase, var, start_offset);
3079 else
3081 if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
3082 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
3083 start_offset - (type->size_is ? 4 : 2));
3084 else
3085 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3086 phase, var, start_offset);
3089 else if (is_array(type))
3091 unsigned char tc = get_array_type( type );
3092 const char *array_type = "FixedArray";
3094 /* We already have the size_is expression since it's at the
3095 top level, but do checks for multidimensional conformant
3096 arrays. When we handle them, we'll need to extend this
3097 function to return a list, and then we'll actually use
3098 the return value. */
3099 get_size_is_expr(type, var->name);
3101 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3103 array_type = "VaryingArray";
3105 else if (tc == RPC_FC_CARRAY)
3107 array_type = "ConformantArray";
3109 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3111 array_type = (tc == RPC_FC_BOGUS_ARRAY
3112 ? "ComplexArray"
3113 : "ConformantVaryingArray");
3116 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3117 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3118 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3120 /* these are all unmarshalled by allocating memory */
3121 if (tc == RPC_FC_BOGUS_ARRAY ||
3122 tc == RPC_FC_CVARRAY ||
3123 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3124 (tc == RPC_FC_CARRAY && !in_attr))
3126 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3127 indent++;
3128 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3132 else if (!is_ptr(var->type) && is_base_type(rtype))
3134 if (phase != PHASE_FREE)
3135 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3137 else if (!is_ptr(var->type))
3139 switch (rtype)
3141 case RPC_FC_STRUCT:
3142 case RPC_FC_PSTRUCT:
3143 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3144 break;
3145 case RPC_FC_CSTRUCT:
3146 case RPC_FC_CPSTRUCT:
3147 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3148 break;
3149 case RPC_FC_CVSTRUCT:
3150 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3151 break;
3152 case RPC_FC_BOGUS_STRUCT:
3153 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3154 break;
3155 default:
3156 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
3159 else
3161 const type_t *ref = type->ref;
3162 if (type->type == RPC_FC_RP && is_base_type(ref->type))
3164 if (phase != PHASE_FREE)
3165 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3167 else if (type->type == RPC_FC_RP && get_struct_type(ref) == RPC_FC_STRUCT &&
3168 !is_user_type(ref))
3170 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3171 print_phase_function(file, indent, "SimpleStruct",
3172 local_var_prefix, phase, var,
3173 ref->typestring_offset);
3175 else
3177 if (ref->type == RPC_FC_IP)
3178 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3179 else
3180 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3183 fprintf(file, "\n");
3186 void write_remoting_arguments(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3187 enum pass pass, enum remoting_phase phase)
3189 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3191 unsigned int size = get_function_buffer_size( func, pass );
3192 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3195 if (pass == PASS_RETURN)
3197 var_t var;
3198 var = *func->def;
3199 var.type = get_func_return_type(func);
3200 var.name = xstrdup( "_RetVal" );
3201 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3202 free( var.name );
3204 else
3206 const var_t *var;
3207 if (!func->args)
3208 return;
3209 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3210 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3215 size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3217 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3221 size_t get_size_procformatstring_func(const func_t *func)
3223 const var_t *var;
3224 size_t size = 0;
3226 /* argument list size */
3227 if (func->args)
3228 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3229 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3231 /* return value size */
3232 if (is_void(get_func_return_type(func)))
3233 size += 2; /* FC_END and FC_PAD */
3234 else
3235 size += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
3237 return size;
3240 size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3242 const statement_t *stmt;
3243 size_t size = 1;
3244 const func_t *func;
3246 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3248 const type_t *iface;
3249 if (stmt->type == STMT_LIBRARY)
3251 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3252 continue;
3254 else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3255 continue;
3257 iface = stmt->u.type;
3258 if (!pred(iface))
3259 continue;
3261 if (iface->funcs)
3262 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
3263 if (!is_local(func->def->attrs))
3264 size += get_size_procformatstring_func( func );
3266 return size;
3269 size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3271 set_all_tfswrite(FALSE);
3272 return process_tfs(NULL, stmts, pred);
3275 void declare_stub_args( FILE *file, int indent, const func_t *func )
3277 int in_attr, out_attr;
3278 int i = 0;
3279 const var_t *var;
3281 /* declare return value '_RetVal' */
3282 if (!is_void(get_func_return_type(func)))
3284 print_file(file, indent, "");
3285 write_type_decl_left(file, get_func_return_type(func));
3286 fprintf(file, " _RetVal;\n");
3289 if (!func->args)
3290 return;
3292 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3294 int is_string = is_string_type(var->attrs, var->type);
3296 in_attr = is_attr(var->attrs, ATTR_IN);
3297 out_attr = is_attr(var->attrs, ATTR_OUT);
3298 if (!out_attr && !in_attr)
3299 in_attr = 1;
3301 if (is_context_handle(var->type))
3302 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3303 else
3305 if (!in_attr && !var->type->size_is && !is_string)
3307 print_file(file, indent, "");
3308 write_type_decl(file, var->type->declarray ? var->type : var->type->ref,
3309 "_W%u", i++);
3310 fprintf(file, ";\n");
3313 print_file(file, indent, "");
3314 write_type_decl_left(file, var->type);
3315 fprintf(file, " ");
3316 if (var->type->declarray) {
3317 fprintf(file, "(*%s)", var->name);
3318 } else
3319 fprintf(file, "%s", var->name);
3320 write_type_right(file, var->type, FALSE);
3321 fprintf(file, ";\n");
3323 if (decl_indirect(var->type))
3324 print_file(file, indent, "void *_p_%s;\n", var->name);
3330 void assign_stub_out_args( FILE *file, int indent, const func_t *func, const char *local_var_prefix )
3332 int in_attr, out_attr;
3333 int i = 0, sep = 0;
3334 const var_t *var;
3336 if (!func->args)
3337 return;
3339 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3341 int is_string = is_string_type(var->attrs, var->type);
3342 in_attr = is_attr(var->attrs, ATTR_IN);
3343 out_attr = is_attr(var->attrs, ATTR_OUT);
3344 if (!out_attr && !in_attr)
3345 in_attr = 1;
3347 if (!in_attr)
3349 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3351 if (is_context_handle(var->type))
3353 fprintf(file, " = NdrContextHandleInitialize(\n");
3354 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3355 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3356 var->type->typestring_offset);
3358 else if (var->type->size_is)
3360 unsigned int size, align = 0;
3361 type_t *type = var->type;
3363 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3364 for ( ; type->size_is ; type = type->ref)
3366 write_expr(file, type->size_is, TRUE, TRUE, NULL, NULL, local_var_prefix);
3367 fprintf(file, " * ");
3369 size = type_memsize(type, &align);
3370 fprintf(file, "%u);\n", size);
3372 else if (!is_string)
3374 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3375 if (is_ptr(var->type) && !last_ptr(var->type))
3376 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3377 i++;
3380 sep = 1;
3383 if (sep)
3384 fprintf(file, "\n");
3388 int write_expr_eval_routines(FILE *file, const char *iface)
3390 static const char *var_name = "pS";
3391 static const char *var_name_expr = "pS->";
3392 int result = 0;
3393 struct expr_eval_routine *eval;
3394 unsigned short callback_offset = 0;
3396 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3398 const char *name = eval->structure->name;
3399 result = 1;
3401 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3402 iface, name, callback_offset);
3403 print_file(file, 0, "{\n");
3404 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3405 name, var_name, name, eval->baseoff);
3406 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3407 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3408 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3409 fprintf(file, ";\n");
3410 print_file(file, 0, "}\n\n");
3411 callback_offset++;
3413 return result;
3416 void write_expr_eval_routine_list(FILE *file, const char *iface)
3418 struct expr_eval_routine *eval;
3419 struct expr_eval_routine *cursor;
3420 unsigned short callback_offset = 0;
3422 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3423 fprintf(file, "{\n");
3425 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3427 const char *name = eval->structure->name;
3428 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3429 callback_offset++;
3430 list_remove(&eval->entry);
3431 free(eval);
3434 fprintf(file, "};\n\n");
3437 void write_user_quad_list(FILE *file)
3439 user_type_t *ut;
3441 if (list_empty(&user_type_list))
3442 return;
3444 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3445 fprintf(file, "{\n");
3446 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3448 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3449 print_file(file, 1, "{\n");
3450 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3451 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3452 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3453 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3454 print_file(file, 1, "}%s\n", sep);
3456 fprintf(file, "};\n\n");
3459 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3461 const struct str_list_entry_t *endpoint;
3462 const char *p;
3464 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3465 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3466 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3468 print_file( f, 1, "{ (const unsigned char *)\"" );
3469 for (p = endpoint->str; *p && *p != ':'; p++)
3471 if (*p == '"' || *p == '\\') fputc( '\\', f );
3472 fputc( *p, f );
3474 if (!*p) goto error;
3475 if (p[1] != '[') goto error;
3477 fprintf( f, "\", (const unsigned char *)\"" );
3478 for (p += 2; *p && *p != ']'; p++)
3480 if (*p == '"' || *p == '\\') fputc( '\\', f );
3481 fputc( *p, f );
3483 if (*p != ']') goto error;
3484 fprintf( f, "\" },\n" );
3486 print_file( f, 0, "};\n\n" );
3487 return;
3489 error:
3490 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3493 void write_exceptions( FILE *file )
3495 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3496 fprintf( file, "\n");
3497 fprintf( file, "#include \"wine/exception.h\"\n");
3498 fprintf( file, "#undef RpcTryExcept\n");
3499 fprintf( file, "#undef RpcExcept\n");
3500 fprintf( file, "#undef RpcEndExcept\n");
3501 fprintf( file, "#undef RpcTryFinally\n");
3502 fprintf( file, "#undef RpcFinally\n");
3503 fprintf( file, "#undef RpcEndFinally\n");
3504 fprintf( file, "#undef RpcExceptionCode\n");
3505 fprintf( file, "#undef RpcAbnormalTermination\n");
3506 fprintf( file, "\n");
3507 fprintf( file, "struct __exception_frame;\n");
3508 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3509 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3510 fprintf( file, "\n");
3511 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3512 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3513 fprintf( file, " __filter_func filter; \\\n");
3514 fprintf( file, " __finally_func finally; \\\n");
3515 fprintf( file, " sigjmp_buf jmp; \\\n");
3516 fprintf( file, " DWORD code; \\\n");
3517 fprintf( file, " unsigned char abnormal_termination; \\\n");
3518 fprintf( file, " unsigned char filter_level; \\\n");
3519 fprintf( file, " unsigned char finally_level;\n");
3520 fprintf( file, "\n");
3521 fprintf( file, "struct __exception_frame\n{\n");
3522 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3523 fprintf( file, "};\n");
3524 fprintf( file, "\n");
3525 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3526 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3527 fprintf( file, " CONTEXT *context,\n");
3528 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3529 fprintf( file, "{\n");
3530 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3531 fprintf( file, "\n");
3532 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3533 fprintf( file, " {\n" );
3534 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3535 fprintf( file, " {\n" );
3536 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3537 fprintf( file, " exc_frame->finally( exc_frame );\n");
3538 fprintf( file, " }\n" );
3539 fprintf( file, " return ExceptionContinueSearch;\n");
3540 fprintf( file, " }\n" );
3541 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3542 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3543 fprintf( file, " {\n");
3544 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3545 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3546 fprintf( file, " {\n");
3547 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3548 fprintf( file, " exc_frame->finally( exc_frame );\n");
3549 fprintf( file, " __wine_pop_frame( frame );\n");
3550 fprintf( file, " }\n");
3551 fprintf( file, " exc_frame->filter_level = 0;\n");
3552 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3553 fprintf( file, " }\n");
3554 fprintf( file, " return ExceptionContinueSearch;\n");
3555 fprintf( file, "}\n");
3556 fprintf( file, "\n");
3557 fprintf( file, "#define RpcTryExcept \\\n");
3558 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3559 fprintf( file, " { \\\n");
3560 fprintf( file, " if (!__frame->finally_level) \\\n" );
3561 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3562 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3563 fprintf( file, "\n");
3564 fprintf( file, "#define RpcExcept(expr) \\\n");
3565 fprintf( file, " if (!__frame->finally_level) \\\n" );
3566 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3567 fprintf( file, " __frame->filter_level = 0; \\\n" );
3568 fprintf( file, " } \\\n");
3569 fprintf( file, " else \\\n");
3570 fprintf( file, "\n");
3571 fprintf( file, "#define RpcEndExcept\n");
3572 fprintf( file, "\n");
3573 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3574 fprintf( file, "\n");
3575 fprintf( file, "#define RpcTryFinally \\\n");
3576 fprintf( file, " if (!__frame->filter_level) \\\n");
3577 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3578 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3579 fprintf( file, "\n");
3580 fprintf( file, "#define RpcFinally \\\n");
3581 fprintf( file, " if (!__frame->filter_level) \\\n");
3582 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3583 fprintf( file, " __frame->finally_level = 0;\n");
3584 fprintf( file, "\n");
3585 fprintf( file, "#define RpcEndFinally\n");
3586 fprintf( file, "\n");
3587 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3588 fprintf( file, "\n");
3589 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3590 fprintf( file, " do { \\\n");
3591 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3592 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3593 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3594 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3595 fprintf( file, " __frame->filter_level = 0; \\\n");
3596 fprintf( file, " __frame->finally_level = 0; \\\n");
3597 fprintf( file, " } while (0)\n");
3598 fprintf( file, "\n");
3599 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3600 fprintf( file, "\n");
3601 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
3602 fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
3603 fprintf( file, "\n");
3604 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");