widl: Check whether an expression is a constant one before assuming it is a toplevel...
[wine/hacks.git] / tools / widl / typegen.c
blobbf7c662006e207548964e386b91c3e6e899a2327
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 type_t *current_structure;
50 static const type_t *current_iface;
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
53 struct expr_eval_routine
55 struct list entry;
56 const type_t *structure;
57 unsigned int baseoff;
58 const expr_t *expr;
61 enum type_context
63 TYPE_CONTEXT_TOPLEVELPARAM,
64 TYPE_CONTEXT_PARAM,
65 TYPE_CONTEXT_CONTAINER,
66 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
69 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
70 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
71 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
72 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
73 const char *name, int write_ptr, unsigned int *tfsoff);
74 static const var_t *find_array_or_string_in_struct(const type_t *type);
75 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
76 type_t *type, int toplevel_param,
77 const char *name, unsigned int *typestring_offset);
79 static const char *string_of_type(unsigned char type)
81 switch (type)
83 case RPC_FC_BYTE: return "FC_BYTE";
84 case RPC_FC_CHAR: return "FC_CHAR";
85 case RPC_FC_SMALL: return "FC_SMALL";
86 case RPC_FC_USMALL: return "FC_USMALL";
87 case RPC_FC_WCHAR: return "FC_WCHAR";
88 case RPC_FC_SHORT: return "FC_SHORT";
89 case RPC_FC_USHORT: return "FC_USHORT";
90 case RPC_FC_LONG: return "FC_LONG";
91 case RPC_FC_ULONG: return "FC_ULONG";
92 case RPC_FC_FLOAT: return "FC_FLOAT";
93 case RPC_FC_HYPER: return "FC_HYPER";
94 case RPC_FC_DOUBLE: return "FC_DOUBLE";
95 case RPC_FC_ENUM16: return "FC_ENUM16";
96 case RPC_FC_ENUM32: return "FC_ENUM32";
97 case RPC_FC_IGNORE: return "FC_IGNORE";
98 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
99 case RPC_FC_RP: return "FC_RP";
100 case RPC_FC_UP: return "FC_UP";
101 case RPC_FC_OP: return "FC_OP";
102 case RPC_FC_FP: return "FC_FP";
103 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
104 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
105 case RPC_FC_STRUCT: return "FC_STRUCT";
106 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
107 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
108 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
109 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
110 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
111 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
112 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
113 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
114 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
115 case RPC_FC_CARRAY: return "FC_CARRAY";
116 case RPC_FC_CVARRAY: return "FC_CVARRAY";
117 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
118 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
119 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
120 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
121 case RPC_FC_POINTER: return "FC_POINTER";
122 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
123 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
124 case RPC_FC_CSTRING: return "FC_CSTRING";
125 case RPC_FC_WSTRING: return "FC_WSTRING";
126 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
127 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
128 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
129 case RPC_FC_IP: return "FC_IP";
130 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
131 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
132 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
133 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
134 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
135 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
136 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
137 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
138 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
139 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
140 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
141 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
142 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
143 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
144 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
145 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
146 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
147 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
148 case RPC_FC_PP: return "FC_PP";
149 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
150 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
151 case RPC_FC_DIV_2: return "FC_DIV_2";
152 case RPC_FC_MULT_2: return "FC_MULT_2";
153 case RPC_FC_ADD_1: return "FC_ADD_1";
154 case RPC_FC_SUB_1: return "FC_SUB_1";
155 case RPC_FC_CALLBACK: return "FC_CALLBACK";
156 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
157 case RPC_FC_END: return "FC_END";
158 case RPC_FC_PAD: return "FC_PAD";
159 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
160 case RPC_FC_RANGE: return "FC_RANGE";
161 case RPC_FC_INT3264: return "FC_INT3264";
162 case RPC_FC_UINT3264: return "FC_UINT3264";
163 default:
164 error("string_of_type: unknown type 0x%02x\n", type);
165 return NULL;
169 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
171 const type_t *t = type;
172 for (;;)
174 if (is_attr(t->attrs, attr))
175 return get_attrp(t->attrs, attr);
176 else if (type_is_alias(t))
177 t = type_alias_get_aliasee(t);
178 else return NULL;
182 unsigned char get_basic_fc(const type_t *type)
184 int sign = type_basic_get_sign(type);
185 switch (type_basic_get_type(type))
187 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
188 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
189 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
190 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
191 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
192 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
193 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
194 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
195 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
196 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
197 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
198 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
199 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
200 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
202 return 0;
205 static inline unsigned int clamp_align(unsigned int align)
207 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
208 if(align > packing) align = packing;
209 return align;
212 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
214 const type_t *t;
215 int pointer_type;
217 assert(is_ptr(type) || is_array(type));
219 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
220 if (pointer_type)
221 return pointer_type;
223 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
225 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
226 if (pointer_type)
227 return pointer_type;
230 if (toplevel_param)
231 return RPC_FC_RP;
232 else if (is_ptr(type))
233 return type_pointer_get_default_fc(type);
234 else
235 return type_array_get_ptr_default_fc(type);
238 static unsigned char get_enum_fc(const type_t *type)
240 assert(type_get_type(type) == TYPE_ENUM);
241 if (is_aliaschain_attr(type, ATTR_V1ENUM))
242 return RPC_FC_ENUM32;
243 else
244 return RPC_FC_ENUM16;
247 static type_t *get_user_type(const type_t *t, const char **pname)
249 for (;;)
251 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
252 if (ut)
254 if (pname)
255 *pname = t->name;
256 return ut;
259 if (type_is_alias(t))
260 t = type_alias_get_aliasee(t);
261 else
262 return NULL;
266 static int is_user_type(const type_t *t)
268 return get_user_type(t, NULL) != NULL;
271 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
273 if (is_user_type(type))
274 return TGT_USER_TYPE;
276 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
277 return TGT_CTXT_HANDLE;
279 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
280 return TGT_STRING;
282 switch (type_get_type(type))
284 case TYPE_BASIC:
285 if (!(flags & TDT_IGNORE_RANGES) &&
286 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
287 return TGT_RANGE;
288 return TGT_BASIC;
289 case TYPE_ENUM:
290 if (!(flags & TDT_IGNORE_RANGES) &&
291 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
292 return TGT_RANGE;
293 return TGT_ENUM;
294 case TYPE_POINTER:
295 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
296 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
297 return TGT_IFACE_POINTER;
298 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
299 return TGT_CTXT_HANDLE_POINTER;
300 else
301 return TGT_POINTER;
302 case TYPE_STRUCT:
303 return TGT_STRUCT;
304 case TYPE_ENCAPSULATED_UNION:
305 case TYPE_UNION:
306 return TGT_UNION;
307 case TYPE_ARRAY:
308 return TGT_ARRAY;
309 case TYPE_FUNCTION:
310 case TYPE_COCLASS:
311 case TYPE_INTERFACE:
312 case TYPE_MODULE:
313 case TYPE_VOID:
314 case TYPE_ALIAS:
315 case TYPE_BITFIELD:
316 break;
318 return TGT_INVALID;
321 static int get_padding(const var_list_t *fields)
323 unsigned short offset = 0;
324 unsigned int salign = 1;
325 const var_t *f;
327 if (!fields)
328 return 0;
330 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
332 type_t *ft = f->type;
333 unsigned int align = 0;
334 unsigned int size = type_memsize(ft, &align);
335 align = clamp_align(align);
336 if (align > salign) salign = align;
337 offset = ROUND_SIZE(offset, align);
338 offset += size;
341 return ROUNDING(offset, salign);
344 unsigned char get_struct_fc(const type_t *type)
346 int has_pointer = 0;
347 int has_conformance = 0;
348 int has_variance = 0;
349 var_t *field;
350 var_list_t *fields;
352 fields = type_struct_get_fields(type);
354 if (get_padding(fields))
355 return RPC_FC_BOGUS_STRUCT;
357 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
359 type_t *t = field->type;
360 enum typegen_type typegen_type;
362 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
364 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
366 if (is_string_type(field->attrs, field->type))
368 if (is_conformant_array(t))
369 has_conformance = 1;
370 has_variance = 1;
371 continue;
374 if (is_array(type_array_get_element(field->type)))
375 return RPC_FC_BOGUS_STRUCT;
377 if (type_array_has_conformance(field->type))
379 has_conformance = 1;
380 if (list_next(fields, &field->entry))
381 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
382 field->name);
384 if (type_array_has_variance(t))
385 has_variance = 1;
387 t = type_array_get_element(t);
388 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
391 switch (typegen_type)
393 case TGT_USER_TYPE:
394 case TGT_IFACE_POINTER:
395 return RPC_FC_BOGUS_STRUCT;
396 case TGT_BASIC:
397 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
398 return RPC_FC_BOGUS_STRUCT;
399 break;
400 case TGT_ENUM:
401 if (get_enum_fc(t) == RPC_FC_ENUM16)
402 return RPC_FC_BOGUS_STRUCT;
403 break;
404 case TGT_POINTER:
405 case TGT_ARRAY:
406 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
407 return RPC_FC_BOGUS_STRUCT;
408 has_pointer = 1;
409 break;
410 case TGT_UNION:
411 return RPC_FC_BOGUS_STRUCT;
412 case TGT_STRUCT:
414 unsigned char fc = get_struct_fc(t);
415 switch (fc)
417 case RPC_FC_STRUCT:
418 break;
419 case RPC_FC_CVSTRUCT:
420 has_conformance = 1;
421 has_variance = 1;
422 has_pointer = 1;
423 break;
425 case RPC_FC_CPSTRUCT:
426 has_conformance = 1;
427 if (list_next( fields, &field->entry ))
428 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
429 field->name);
430 has_pointer = 1;
431 break;
433 case RPC_FC_CSTRUCT:
434 has_conformance = 1;
435 if (list_next( fields, &field->entry ))
436 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
437 field->name);
438 break;
440 case RPC_FC_PSTRUCT:
441 has_pointer = 1;
442 break;
444 default:
445 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
446 /* fallthru - treat it as complex */
448 /* as soon as we see one of these these members, it's bogus... */
449 case RPC_FC_BOGUS_STRUCT:
450 return RPC_FC_BOGUS_STRUCT;
452 break;
454 case TGT_RANGE:
455 return RPC_FC_BOGUS_STRUCT;
456 case TGT_STRING:
457 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
458 case TGT_INVALID:
459 case TGT_CTXT_HANDLE:
460 case TGT_CTXT_HANDLE_POINTER:
461 /* checking after parsing should mean that we don't get here. if we do,
462 * it's a checker bug */
463 assert(0);
467 if( has_variance )
469 if ( has_conformance )
470 return RPC_FC_CVSTRUCT;
471 else
472 return RPC_FC_BOGUS_STRUCT;
474 if( has_conformance && has_pointer )
475 return RPC_FC_CPSTRUCT;
476 if( has_conformance )
477 return RPC_FC_CSTRUCT;
478 if( has_pointer )
479 return RPC_FC_PSTRUCT;
480 return RPC_FC_STRUCT;
483 static unsigned char get_array_fc(const type_t *type)
485 unsigned char fc;
486 const expr_t *size_is;
487 const type_t *elem_type;
489 elem_type = type_array_get_element(type);
490 size_is = type_array_get_conformance(type);
492 if (!size_is)
494 unsigned int align = 0;
495 unsigned int size = type_memsize(elem_type, &align);
496 if (size * type_array_get_dim(type) > 0xffffuL)
497 fc = RPC_FC_LGFARRAY;
498 else
499 fc = RPC_FC_SMFARRAY;
501 else
502 fc = RPC_FC_CARRAY;
504 if (type_array_has_variance(type))
506 if (fc == RPC_FC_SMFARRAY)
507 fc = RPC_FC_SMVARRAY;
508 else if (fc == RPC_FC_LGFARRAY)
509 fc = RPC_FC_LGVARRAY;
510 else if (fc == RPC_FC_CARRAY)
511 fc = RPC_FC_CVARRAY;
514 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
516 case TGT_USER_TYPE:
517 fc = RPC_FC_BOGUS_ARRAY;
518 break;
519 case TGT_BASIC:
520 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
521 pointer_size != 4)
522 fc = RPC_FC_BOGUS_ARRAY;
523 break;
524 case TGT_STRUCT:
525 switch (get_struct_fc(elem_type))
527 case RPC_FC_BOGUS_STRUCT:
528 fc = RPC_FC_BOGUS_ARRAY;
529 break;
531 break;
532 case TGT_ENUM:
533 /* is 16-bit enum - if so, wire size differs from mem size and so
534 * the array cannot be block copied, which means the array is complex */
535 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
536 fc = RPC_FC_BOGUS_ARRAY;
537 break;
538 case TGT_UNION:
539 case TGT_IFACE_POINTER:
540 fc = RPC_FC_BOGUS_ARRAY;
541 break;
542 case TGT_POINTER:
543 /* ref pointers cannot just be block copied. unique pointers to
544 * interfaces need special treatment. either case means the array is
545 * complex */
546 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
547 fc = RPC_FC_BOGUS_ARRAY;
548 break;
549 case TGT_RANGE:
550 fc = RPC_FC_BOGUS_ARRAY;
551 break;
552 case TGT_CTXT_HANDLE:
553 case TGT_CTXT_HANDLE_POINTER:
554 case TGT_STRING:
555 case TGT_INVALID:
556 case TGT_ARRAY:
557 /* nothing to do for everything else */
558 break;
561 return fc;
564 static int is_non_complex_struct(const type_t *type)
566 return (type_get_type(type) == TYPE_STRUCT &&
567 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
570 static int type_has_pointers(const type_t *type)
572 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
574 case TGT_USER_TYPE:
575 return FALSE;
576 case TGT_POINTER:
577 return TRUE;
578 case TGT_ARRAY:
579 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
580 case TGT_STRUCT:
582 var_list_t *fields = type_struct_get_fields(type);
583 const var_t *field;
584 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
586 if (type_has_pointers(field->type))
587 return TRUE;
589 break;
591 case TGT_UNION:
593 var_list_t *fields;
594 const var_t *field;
595 fields = type_union_get_cases(type);
596 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
598 if (field->type && type_has_pointers(field->type))
599 return TRUE;
601 break;
603 case TGT_CTXT_HANDLE:
604 case TGT_CTXT_HANDLE_POINTER:
605 case TGT_STRING:
606 case TGT_IFACE_POINTER:
607 case TGT_BASIC:
608 case TGT_ENUM:
609 case TGT_RANGE:
610 case TGT_INVALID:
611 break;
614 return FALSE;
617 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
618 int toplevel_param)
620 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
622 case TGT_USER_TYPE:
623 return FALSE;
624 case TGT_POINTER:
625 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
626 return TRUE;
627 else
628 return FALSE;
629 case TGT_ARRAY:
630 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
631 return TRUE;
632 else
633 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
634 case TGT_STRUCT:
636 var_list_t *fields = type_struct_get_fields(type);
637 const var_t *field;
638 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
640 if (type_has_full_pointer(field->type, field->attrs, FALSE))
641 return TRUE;
643 break;
645 case TGT_UNION:
647 var_list_t *fields;
648 const var_t *field;
649 fields = type_union_get_cases(type);
650 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
652 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
653 return TRUE;
655 break;
657 case TGT_CTXT_HANDLE:
658 case TGT_CTXT_HANDLE_POINTER:
659 case TGT_STRING:
660 case TGT_IFACE_POINTER:
661 case TGT_BASIC:
662 case TGT_ENUM:
663 case TGT_RANGE:
664 case TGT_INVALID:
665 break;
668 return FALSE;
671 static unsigned short user_type_offset(const char *name)
673 user_type_t *ut;
674 unsigned short off = 0;
675 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
677 if (strcmp(name, ut->name) == 0)
678 return off;
679 ++off;
681 error("user_type_offset: couldn't find type (%s)\n", name);
682 return 0;
685 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
687 type->typestring_offset = offset;
688 if (file) type->tfswrite = FALSE;
691 static void guard_rec(type_t *type)
693 /* types that contain references to themselves (like a linked list),
694 need to be shielded from infinite recursion when writing embedded
695 types */
696 if (type->typestring_offset)
697 type->tfswrite = FALSE;
698 else
699 type->typestring_offset = 1;
702 static int is_embedded_complex(const type_t *type)
704 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
706 case TGT_USER_TYPE:
707 case TGT_STRUCT:
708 case TGT_UNION:
709 case TGT_ARRAY:
710 case TGT_IFACE_POINTER:
711 return TRUE;
712 default:
713 return FALSE;
717 static const char *get_context_handle_type_name(const type_t *type)
719 const type_t *t;
720 for (t = type;
721 is_ptr(t) || type_is_alias(t);
722 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
723 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
724 return t->name;
725 assert(0);
726 return NULL;
729 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
730 do { \
731 if (file) \
732 fprintf(file, "/* %2u */\n", typestring_offset); \
733 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
735 while (0)
737 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
738 static void print_file(FILE *file, int indent, const char *format, ...)
740 va_list va;
741 va_start(va, format);
742 print(file, indent, format, va);
743 va_end(va);
746 void print(FILE *file, int indent, const char *format, va_list va)
748 if (file)
750 if (format[0] != '\n')
751 while (0 < indent--)
752 fprintf(file, " ");
753 vfprintf(file, format, va);
758 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
760 if (decl_indirect(t))
762 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
763 local_var_prefix, n, local_var_prefix, n);
764 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
766 else if (is_ptr(t) || is_array(t))
767 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
770 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
772 const var_t *var;
774 if (!is_void(type_function_get_rettype(func->type)))
775 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
777 if (!type_get_function_args(func->type))
778 return;
780 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
781 write_var_init(file, indent, var->type, var->name, local_var_prefix);
783 fprintf(file, "\n");
786 static void write_formatdesc(FILE *f, int indent, const char *str)
788 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
789 print_file(f, indent, "{\n");
790 print_file(f, indent + 1, "short Pad;\n");
791 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
792 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
793 print_file(f, indent, "\n");
796 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
798 clear_all_offsets();
800 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
801 get_size_typeformatstring(stmts, pred));
803 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
804 get_size_procformatstring(stmts, pred));
806 fprintf(f, "\n");
807 write_formatdesc(f, indent, "TYPE");
808 write_formatdesc(f, indent, "PROC");
809 fprintf(f, "\n");
810 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
811 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
812 print_file(f, indent, "\n");
815 int decl_indirect(const type_t *t)
817 if (is_user_type(t))
818 return TRUE;
819 return (type_get_type(t) != TYPE_BASIC &&
820 type_get_type(t) != TYPE_ENUM &&
821 type_get_type(t) != TYPE_POINTER &&
822 type_get_type(t) != TYPE_ARRAY);
825 static unsigned int write_procformatstring_type(FILE *file, int indent,
826 const char *name,
827 const type_t *type,
828 const attr_list_t *attrs,
829 int is_return)
831 unsigned int size;
833 int is_in = is_attr(attrs, ATTR_IN);
834 int is_out = is_attr(attrs, ATTR_OUT);
836 if (!is_in && !is_out) is_in = TRUE;
838 if (type_get_type(type) == TYPE_BASIC ||
839 type_get_type(type) == TYPE_ENUM)
841 unsigned char fc;
843 if (is_return)
844 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
845 else
846 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
848 if (type_get_type(type) == TYPE_ENUM)
850 fc = get_enum_fc(type);
852 else
854 fc = get_basic_fc(type);
856 if (fc == RPC_FC_BIND_PRIMITIVE)
857 fc = RPC_FC_IGNORE;
860 print_file(file, indent, "0x%02x, /* %s */\n",
861 fc, string_of_type(fc));
862 size = 2; /* includes param type prefix */
864 else
866 if (is_return)
867 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
868 else if (is_in && is_out)
869 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
870 else if (is_out)
871 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
872 else
873 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
875 print_file(file, indent, "0x01,\n");
876 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
877 size = 4; /* includes param type prefix */
879 return size;
882 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
884 const statement_t *stmt;
885 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
887 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
889 const statement_t *stmt_func;
890 if (!pred(stmt->u.type))
891 continue;
892 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
894 const var_t *func = stmt_func->u.var;
895 if (is_local(func->attrs)) continue;
896 /* emit argument data */
897 if (type_get_function_args(func->type))
899 const var_t *var;
900 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
901 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
904 /* emit return value data */
905 if (is_void(type_function_get_rettype(func->type)))
907 print_file(file, indent, "0x5b, /* FC_END */\n");
908 print_file(file, indent, "0x5c, /* FC_PAD */\n");
910 else
911 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
914 else if (stmt->type == STMT_LIBRARY)
915 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
919 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
921 int indent = 0;
923 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
924 print_file(file, indent, "{\n");
925 indent++;
926 print_file(file, indent, "0,\n");
927 print_file(file, indent, "{\n");
928 indent++;
930 write_procformatstring_stmts(file, indent, stmts, pred);
932 print_file(file, indent, "0x0\n");
933 indent--;
934 print_file(file, indent, "}\n");
935 indent--;
936 print_file(file, indent, "};\n");
937 print_file(file, indent, "\n");
940 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
942 unsigned char fc;
944 if (type_get_type(type) == TYPE_BASIC)
945 fc = get_basic_fc(type);
946 else if (type_get_type(type) == TYPE_ENUM)
947 fc = get_enum_fc(type);
948 else
949 return 0;
951 if (convert_to_signed_type)
953 switch(fc)
955 case RPC_FC_USMALL:
956 fc = RPC_FC_SMALL;
957 break;
958 case RPC_FC_USHORT:
959 fc = RPC_FC_SHORT;
960 break;
961 case RPC_FC_ULONG:
962 fc = RPC_FC_LONG;
963 break;
967 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
968 *typestring_offset += 1;
969 return 1;
972 /* write conformance / variance descriptor */
973 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
974 unsigned int baseoff, const type_t *type,
975 const expr_t *expr)
977 unsigned char operator_type = 0;
978 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
979 const char *conftype_string = "";
980 const expr_t *subexpr;
982 if (!expr)
984 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
985 return 4;
988 if (expr->is_const)
990 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
991 error("write_conf_or_var_desc: constant value %ld is greater than "
992 "the maximum constant size of %d\n", expr->cval,
993 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
995 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
996 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
997 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
998 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1000 return 4;
1003 if (!structure)
1005 /* Top-level conformance calculations are done inline. */
1006 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
1007 RPC_FC_TOP_LEVEL_CONFORMANCE);
1008 print_file (file, 2, "0x0,\n");
1009 print_file (file, 2, "NdrFcShort(0x0),\n");
1010 return 4;
1013 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1015 conftype = RPC_FC_POINTER_CONFORMANCE;
1016 conftype_string = "field pointer, ";
1019 subexpr = expr;
1020 switch (subexpr->type)
1022 case EXPR_PPTR:
1023 subexpr = subexpr->ref;
1024 operator_type = RPC_FC_DEREFERENCE;
1025 break;
1026 case EXPR_DIV:
1027 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1029 subexpr = subexpr->ref;
1030 operator_type = RPC_FC_DIV_2;
1032 break;
1033 case EXPR_MUL:
1034 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1036 subexpr = subexpr->ref;
1037 operator_type = RPC_FC_MULT_2;
1039 break;
1040 case EXPR_SUB:
1041 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1043 subexpr = subexpr->ref;
1044 operator_type = RPC_FC_SUB_1;
1046 break;
1047 case EXPR_ADD:
1048 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1050 subexpr = subexpr->ref;
1051 operator_type = RPC_FC_ADD_1;
1053 break;
1054 default:
1055 break;
1058 if (subexpr->type == EXPR_IDENTIFIER)
1060 const type_t *correlation_variable = NULL;
1061 unsigned char param_type = 0;
1062 unsigned int offset = 0;
1063 const var_t *var;
1064 var_list_t *fields = type_struct_get_fields(structure);
1066 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1068 unsigned int size = field_memsize( var->type, &offset );
1069 if (var->name && !strcmp(var->name, subexpr->u.sval))
1071 correlation_variable = var->type;
1072 break;
1074 offset += size;
1076 if (!correlation_variable)
1077 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1078 subexpr->u.sval);
1080 correlation_variable = expr_resolve_type(NULL, structure, expr);
1082 offset -= baseoff;
1084 if (type_get_type(correlation_variable) == TYPE_BASIC)
1086 switch (get_basic_fc(correlation_variable))
1088 case RPC_FC_CHAR:
1089 case RPC_FC_SMALL:
1090 param_type = RPC_FC_SMALL;
1091 break;
1092 case RPC_FC_BYTE:
1093 case RPC_FC_USMALL:
1094 param_type = RPC_FC_USMALL;
1095 break;
1096 case RPC_FC_WCHAR:
1097 case RPC_FC_SHORT:
1098 param_type = RPC_FC_SHORT;
1099 break;
1100 case RPC_FC_USHORT:
1101 param_type = RPC_FC_USHORT;
1102 break;
1103 case RPC_FC_LONG:
1104 param_type = RPC_FC_LONG;
1105 break;
1106 case RPC_FC_ULONG:
1107 param_type = RPC_FC_ULONG;
1108 break;
1109 default:
1110 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1111 get_basic_fc(correlation_variable));
1114 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1116 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1117 param_type = RPC_FC_LONG;
1118 else
1119 param_type = RPC_FC_SHORT;
1121 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1123 if (pointer_size == 8)
1124 param_type = RPC_FC_HYPER;
1125 else
1126 param_type = RPC_FC_LONG;
1128 else
1130 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1131 subexpr->u.sval);
1132 return 0;
1135 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1136 conftype | param_type, conftype_string, string_of_type(param_type));
1137 print_file(file, 2, "0x%x, /* %s */\n", operator_type,
1138 operator_type ? string_of_type(operator_type) : "no operators");
1139 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1140 offset, offset);
1142 else
1144 unsigned int callback_offset = 0;
1145 struct expr_eval_routine *eval;
1146 int found = 0;
1148 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1150 if (eval->structure == structure ||
1151 (eval->structure->name && structure->name &&
1152 !strcmp(eval->structure->name, structure->name) &&
1153 !compare_expr(eval->expr, expr)))
1155 found = 1;
1156 break;
1158 callback_offset++;
1161 if (!found)
1163 eval = xmalloc (sizeof(*eval));
1164 eval->structure = structure;
1165 eval->baseoff = baseoff;
1166 eval->expr = expr;
1167 list_add_tail (&expr_eval_routines, &eval->entry);
1170 if (callback_offset > USHRT_MAX)
1171 error("Maximum number of callback routines reached\n");
1173 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1174 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1175 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1177 return 4;
1180 /* return size and start offset of a data field based on current offset */
1181 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1183 unsigned int align = 0;
1184 unsigned int size = type_memsize( type, &align );
1186 *offset = ROUND_SIZE( *offset, align );
1187 return size;
1190 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1192 unsigned int size = 0;
1193 unsigned int max_align;
1194 const var_t *v;
1196 if (!fields) return 0;
1197 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1199 unsigned int falign = 0;
1200 unsigned int fsize = type_memsize(v->type, &falign);
1201 if (*align < falign) *align = falign;
1202 falign = clamp_align(falign);
1203 size = ROUND_SIZE(size, falign);
1204 size += fsize;
1207 max_align = clamp_align(*align);
1208 size = ROUND_SIZE(size, max_align);
1210 return size;
1213 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1215 unsigned int size, maxs = 0;
1216 unsigned int align = *pmaxa;
1217 const var_t *v;
1219 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1221 /* we could have an empty default field with NULL type */
1222 if (v->type)
1224 size = type_memsize(v->type, &align);
1225 if (maxs < size) maxs = size;
1226 if (*pmaxa < align) *pmaxa = align;
1230 return maxs;
1233 unsigned int type_memsize(const type_t *t, unsigned int *align)
1235 unsigned int size = 0;
1237 switch (type_get_type(t))
1239 case TYPE_BASIC:
1240 switch (get_basic_fc(t))
1242 case RPC_FC_BYTE:
1243 case RPC_FC_CHAR:
1244 case RPC_FC_USMALL:
1245 case RPC_FC_SMALL:
1246 size = 1;
1247 if (size > *align) *align = size;
1248 break;
1249 case RPC_FC_WCHAR:
1250 case RPC_FC_USHORT:
1251 case RPC_FC_SHORT:
1252 size = 2;
1253 if (size > *align) *align = size;
1254 break;
1255 case RPC_FC_ULONG:
1256 case RPC_FC_LONG:
1257 case RPC_FC_ERROR_STATUS_T:
1258 case RPC_FC_FLOAT:
1259 size = 4;
1260 if (size > *align) *align = size;
1261 break;
1262 case RPC_FC_HYPER:
1263 case RPC_FC_DOUBLE:
1264 size = 8;
1265 if (size > *align) *align = size;
1266 break;
1267 case RPC_FC_INT3264:
1268 case RPC_FC_UINT3264:
1269 assert( pointer_size );
1270 size = pointer_size;
1271 if (size > *align) *align = size;
1272 break;
1273 default:
1274 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1275 size = 0;
1277 break;
1278 case TYPE_ENUM:
1279 switch (get_enum_fc(t))
1281 case RPC_FC_ENUM16:
1282 case RPC_FC_ENUM32:
1283 size = 4;
1284 if (size > *align) *align = size;
1285 break;
1286 default:
1287 error("type_memsize: Unknown enum type\n");
1288 size = 0;
1290 break;
1291 case TYPE_STRUCT:
1292 size = fields_memsize(type_struct_get_fields(t), align);
1293 break;
1294 case TYPE_ENCAPSULATED_UNION:
1295 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1296 break;
1297 case TYPE_UNION:
1298 size = union_memsize(type_union_get_cases(t), align);
1299 break;
1300 case TYPE_POINTER:
1301 assert( pointer_size );
1302 size = pointer_size;
1303 if (size > *align) *align = size;
1304 break;
1305 case TYPE_ARRAY:
1306 if (!type_array_is_decl_as_ptr(t))
1308 if (is_conformant_array(t))
1310 type_memsize(type_array_get_element(t), align);
1311 size = 0;
1313 else
1314 size = type_array_get_dim(t) *
1315 type_memsize(type_array_get_element(t), align);
1317 else /* declared as a pointer */
1319 assert( pointer_size );
1320 size = pointer_size;
1321 if (size > *align) *align = size;
1323 break;
1324 case TYPE_INTERFACE:
1325 case TYPE_ALIAS:
1326 case TYPE_VOID:
1327 case TYPE_COCLASS:
1328 case TYPE_MODULE:
1329 case TYPE_FUNCTION:
1330 case TYPE_BITFIELD:
1331 /* these types should not be encountered here due to language
1332 * restrictions (interface, void, coclass, module), logical
1333 * restrictions (alias - due to type_get_type call above) or
1334 * checking restrictions (function, bitfield). */
1335 assert(0);
1338 return size;
1341 int is_full_pointer_function(const var_t *func)
1343 const var_t *var;
1344 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1345 return TRUE;
1346 if (!type_get_function_args(func->type))
1347 return FALSE;
1348 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1349 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1350 return TRUE;
1351 return FALSE;
1354 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1356 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1357 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1358 fprintf(file, "\n");
1361 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1363 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1364 fprintf(file, "\n");
1367 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1368 const type_t *type,
1369 int toplevel_param,
1370 unsigned int offset,
1371 unsigned int *typeformat_offset)
1373 unsigned int start_offset = *typeformat_offset;
1374 short reloff = offset - (*typeformat_offset + 2);
1375 int in_attr, out_attr;
1376 int pointer_type;
1377 unsigned char flags = 0;
1379 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1381 in_attr = is_attr(attrs, ATTR_IN);
1382 out_attr = is_attr(attrs, ATTR_OUT);
1383 if (!in_attr && !out_attr) in_attr = 1;
1385 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1386 flags |= RPC_FC_P_ONSTACK;
1388 if (is_ptr(type) && is_declptr(type_pointer_get_ref(type)))
1389 flags |= RPC_FC_P_DEREF;
1391 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1392 pointer_type,
1393 flags,
1394 string_of_type(pointer_type));
1395 if (file)
1397 if (flags & RPC_FC_P_ONSTACK)
1398 fprintf(file, " [allocated_on_stack]");
1399 if (flags & RPC_FC_P_DEREF)
1400 fprintf(file, " [pointer_deref]");
1401 fprintf(file, " */\n");
1404 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1405 *typeformat_offset += 4;
1407 return start_offset;
1410 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1412 unsigned char fc;
1413 unsigned char pointer_fc;
1414 const type_t *ref;
1415 int in_attr = is_attr(attrs, ATTR_IN);
1416 int out_attr = is_attr(attrs, ATTR_OUT);
1417 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
1419 /* for historical reasons, write_simple_pointer also handled string types,
1420 * but no longer does. catch bad uses of the function with this check */
1421 if (is_string_type(attrs, type))
1422 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1424 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1426 ref = type_pointer_get_ref(type);
1427 if (type_get_type(ref) == TYPE_ENUM)
1428 fc = get_enum_fc(ref);
1429 else
1430 fc = get_basic_fc(ref);
1432 if (out_attr && !in_attr)
1433 flags |= RPC_FC_P_ONSTACK;
1435 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
1436 pointer_fc, flags, string_of_type(pointer_fc),
1437 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
1438 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1439 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1440 return 4;
1443 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1445 print_file(file, 0, "/* %u (", tfsoff);
1446 write_type_decl(file, t, NULL);
1447 print_file(file, 0, ") */\n");
1450 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1451 type_t *type, unsigned int ref_offset,
1452 int toplevel_param,
1453 unsigned int *typestring_offset)
1455 unsigned int offset = *typestring_offset;
1456 type_t *ref = type_pointer_get_ref(type);
1458 print_start_tfs_comment(file, type, offset);
1459 update_tfsoff(type, offset, file);
1461 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
1463 case TGT_BASIC:
1464 case TGT_ENUM:
1465 *typestring_offset += write_simple_pointer(file, attrs, type,
1466 toplevel_param);
1467 break;
1468 default:
1469 if (ref_offset)
1470 write_nonsimple_pointer(file, attrs, type,
1471 toplevel_param,
1472 ref_offset,
1473 typestring_offset);
1474 break;
1477 return offset;
1480 static int processed(const type_t *type)
1482 return type->typestring_offset && !type->tfswrite;
1485 static int user_type_has_variable_size(const type_t *t)
1487 if (is_ptr(t))
1488 return TRUE;
1489 else if (type_get_type(t) == TYPE_STRUCT)
1491 switch (get_struct_fc(t))
1493 case RPC_FC_PSTRUCT:
1494 case RPC_FC_CSTRUCT:
1495 case RPC_FC_CPSTRUCT:
1496 case RPC_FC_CVSTRUCT:
1497 return TRUE;
1500 /* Note: Since this only applies to user types, we can't have a conformant
1501 array here, and strings should get filed under pointer in this case. */
1502 return FALSE;
1505 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1507 unsigned int start, absoff, flags;
1508 unsigned int align = 0, ualign = 0;
1509 const char *name = NULL;
1510 type_t *utype = get_user_type(type, &name);
1511 unsigned int usize = type_memsize(utype, &ualign);
1512 unsigned int size = type_memsize(type, &align);
1513 unsigned short funoff = user_type_offset(name);
1514 short reloff;
1516 guard_rec(type);
1518 if(user_type_has_variable_size(utype)) usize = 0;
1520 if (type_get_type(utype) == TYPE_BASIC ||
1521 type_get_type(utype) == TYPE_ENUM)
1523 unsigned char fc;
1525 if (type_get_type(utype) == TYPE_ENUM)
1526 fc = get_enum_fc(utype);
1527 else
1528 fc = get_basic_fc(utype);
1530 absoff = *tfsoff;
1531 print_start_tfs_comment(file, utype, absoff);
1532 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1533 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1534 *tfsoff += 2;
1536 else
1538 if (!processed(utype))
1539 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1540 absoff = utype->typestring_offset;
1543 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1544 flags = 0x40;
1545 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1546 flags = 0x80;
1547 else
1548 flags = 0;
1550 start = *tfsoff;
1551 update_tfsoff(type, start, file);
1552 print_start_tfs_comment(file, type, start);
1553 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1554 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1555 flags | (ualign - 1), ualign - 1, flags);
1556 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1557 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1558 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1559 *tfsoff += 8;
1560 reloff = absoff - *tfsoff;
1561 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1562 *tfsoff += 2;
1565 static void write_member_type(FILE *file, const type_t *cont,
1566 int cont_is_complex, const attr_list_t *attrs,
1567 const type_t *type, unsigned int *corroff,
1568 unsigned int *tfsoff)
1570 if (is_embedded_complex(type) && !is_conformant_array(type))
1572 unsigned int absoff;
1573 short reloff;
1575 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1577 absoff = *corroff;
1578 *corroff += 8;
1580 else
1582 absoff = type->typestring_offset;
1584 reloff = absoff - (*tfsoff + 2);
1586 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1587 /* padding is represented using FC_STRUCTPAD* types, so presumably
1588 * this is left over in the format for historical purposes in MIDL
1589 * or rpcrt4. */
1590 print_file(file, 2, "0x0,\n");
1591 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1592 reloff, reloff, absoff);
1593 *tfsoff += 4;
1595 else if (is_ptr(type) || is_conformant_array(type))
1597 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1598 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1599 *tfsoff += 1;
1601 else if (!write_base_type(file, type, TRUE, tfsoff))
1602 error("Unsupported member type %d\n", type_get_type(type));
1605 static void write_array_element_type(FILE *file, const type_t *type,
1606 int cont_is_complex, unsigned int *tfsoff)
1608 type_t *elem = type_array_get_element(type);
1610 if (!is_embedded_complex(elem) && is_ptr(elem))
1612 type_t *ref = type_pointer_get_ref(elem);
1614 if (processed(ref))
1616 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1617 return;
1619 if (!is_string_type(NULL, elem) &&
1620 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1622 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1623 return;
1626 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1629 static void write_end(FILE *file, unsigned int *tfsoff)
1631 if (*tfsoff % 2 == 0)
1633 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1634 *tfsoff += 1;
1636 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1637 *tfsoff += 1;
1640 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1642 unsigned int offset = 0;
1643 var_list_t *fs = type_struct_get_fields(type);
1644 var_t *f;
1646 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1648 type_t *ft = f->type;
1649 unsigned int size = field_memsize( ft, &offset );
1650 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1652 short reloff;
1653 unsigned int absoff = ft->typestring_offset;
1654 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1655 absoff += 8; /* we already have a corr descr, skip it */
1656 reloff = absoff - (*tfsoff + 6);
1657 print_file(file, 0, "/* %d */\n", *tfsoff);
1658 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1659 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1660 write_conf_or_var_desc(file, current_structure, offset, ft,
1661 get_attrp(f->attrs, ATTR_SWITCHIS));
1662 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1663 reloff, reloff, absoff);
1664 *tfsoff += 8;
1666 offset += size;
1670 static int write_pointer_description_offsets(
1671 FILE *file, const attr_list_t *attrs, type_t *type,
1672 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1673 unsigned int *typestring_offset)
1675 int written = 0;
1676 unsigned int align;
1678 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1679 (is_array(type) && type_array_is_decl_as_ptr(type)))
1681 if (offset_in_memory && offset_in_buffer)
1683 unsigned int memsize;
1685 /* pointer instance
1687 * note that MSDN states that for pointer layouts in structures,
1688 * this is a negative offset from the end of the structure, but
1689 * this statement is incorrect. all offsets are positive */
1690 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1691 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1693 align = 0;
1694 memsize = type_memsize(type, &align);
1695 *offset_in_memory += memsize;
1696 /* increment these separately as in the case of conformant (varying)
1697 * structures these start at different values */
1698 *offset_in_buffer += memsize;
1700 *typestring_offset += 4;
1702 if (is_ptr(type))
1704 type_t *ref = type_pointer_get_ref(type);
1706 if (is_string_type(attrs, type))
1707 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1708 else if (processed(ref))
1709 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1710 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1711 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1712 else
1713 error("write_pointer_description_offsets: type format string unknown\n");
1715 else
1717 unsigned int offset = type->typestring_offset;
1718 /* skip over the pointer that is written for strings, since a
1719 * pointer has to be written in-place here */
1720 if (is_string_type(attrs, type))
1721 offset += 4;
1722 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1725 return 1;
1728 if (is_array(type))
1730 return write_pointer_description_offsets(
1731 file, attrs, type_array_get_element(type), offset_in_memory,
1732 offset_in_buffer, typestring_offset);
1734 else if (is_non_complex_struct(type))
1736 /* otherwise search for interesting fields to parse */
1737 const var_t *v;
1738 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1740 if (offset_in_memory && offset_in_buffer)
1742 unsigned int padding;
1743 align = 0;
1744 type_memsize(v->type, &align);
1745 padding = ROUNDING(*offset_in_memory, align);
1746 *offset_in_memory += padding;
1747 *offset_in_buffer += padding;
1749 written += write_pointer_description_offsets(
1750 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1751 typestring_offset);
1754 else
1756 if (offset_in_memory && offset_in_buffer)
1758 unsigned int memsize;
1759 align = 0;
1760 memsize = type_memsize(type, &align);
1761 *offset_in_memory += memsize;
1762 /* increment these separately as in the case of conformant (varying)
1763 * structures these start at different values */
1764 *offset_in_buffer += memsize;
1768 return written;
1771 static int write_no_repeat_pointer_descriptions(
1772 FILE *file, const attr_list_t *attrs, type_t *type,
1773 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1774 unsigned int *typestring_offset)
1776 int written = 0;
1777 unsigned int align;
1779 if (is_ptr(type) ||
1780 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1782 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1783 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1784 *typestring_offset += 2;
1786 return write_pointer_description_offsets(file, attrs, type,
1787 offset_in_memory, offset_in_buffer, typestring_offset);
1790 if (is_non_complex_struct(type))
1792 const var_t *v;
1793 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1795 if (offset_in_memory && offset_in_buffer)
1797 unsigned int padding;
1798 align = 0;
1799 type_memsize(v->type, &align);
1800 padding = ROUNDING(*offset_in_memory, align);
1801 *offset_in_memory += padding;
1802 *offset_in_buffer += padding;
1804 written += write_no_repeat_pointer_descriptions(
1805 file, v->attrs, v->type,
1806 offset_in_memory, offset_in_buffer, typestring_offset);
1809 else
1811 unsigned int memsize;
1812 align = 0;
1813 memsize = type_memsize(type, &align);
1814 *offset_in_memory += memsize;
1815 /* increment these separately as in the case of conformant (varying)
1816 * structures these start at different values */
1817 *offset_in_buffer += memsize;
1820 return written;
1823 /* Note: if file is NULL return value is number of pointers to write, else
1824 * it is the number of type format characters written */
1825 static int write_fixed_array_pointer_descriptions(
1826 FILE *file, const attr_list_t *attrs, type_t *type,
1827 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1828 unsigned int *typestring_offset)
1830 unsigned int align;
1831 int pointer_count = 0;
1833 if (type_get_type(type) == TYPE_ARRAY &&
1834 !type_array_has_conformance(type) && !type_array_has_variance(type))
1836 unsigned int temp = 0;
1837 /* unfortunately, this needs to be done in two passes to avoid
1838 * writing out redundant FC_FIXED_REPEAT descriptions */
1839 pointer_count = write_pointer_description_offsets(
1840 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1841 if (pointer_count > 0)
1843 unsigned int increment_size;
1844 unsigned int offset_of_array_pointer_mem = 0;
1845 unsigned int offset_of_array_pointer_buf = 0;
1847 align = 0;
1848 increment_size = type_memsize(type_array_get_element(type), &align);
1850 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1851 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1852 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1853 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1854 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1855 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1856 *typestring_offset += 10;
1858 pointer_count = write_pointer_description_offsets(
1859 file, attrs, type, &offset_of_array_pointer_mem,
1860 &offset_of_array_pointer_buf, typestring_offset);
1863 else if (type_get_type(type) == TYPE_STRUCT)
1865 const var_t *v;
1866 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1868 if (offset_in_memory && offset_in_buffer)
1870 unsigned int padding;
1871 align = 0;
1872 type_memsize(v->type, &align);
1873 padding = ROUNDING(*offset_in_memory, align);
1874 *offset_in_memory += padding;
1875 *offset_in_buffer += padding;
1877 pointer_count += write_fixed_array_pointer_descriptions(
1878 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1879 typestring_offset);
1882 else
1884 if (offset_in_memory && offset_in_buffer)
1886 unsigned int memsize;
1887 align = 0;
1888 memsize = type_memsize(type, &align);
1889 *offset_in_memory += memsize;
1890 /* increment these separately as in the case of conformant (varying)
1891 * structures these start at different values */
1892 *offset_in_buffer += memsize;
1896 return pointer_count;
1899 /* Note: if file is NULL return value is number of pointers to write, else
1900 * it is the number of type format characters written */
1901 static int write_conformant_array_pointer_descriptions(
1902 FILE *file, const attr_list_t *attrs, type_t *type,
1903 unsigned int offset_in_memory, unsigned int *typestring_offset)
1905 unsigned int align;
1906 int pointer_count = 0;
1908 if (is_conformant_array(type) && !type_array_has_variance(type))
1910 unsigned int temp = 0;
1911 /* unfortunately, this needs to be done in two passes to avoid
1912 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1913 pointer_count = write_pointer_description_offsets(
1914 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1915 if (pointer_count > 0)
1917 unsigned int increment_size;
1918 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1919 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1921 align = 0;
1922 increment_size = type_memsize(type_array_get_element(type), &align);
1924 if (increment_size > USHRT_MAX)
1925 error("array size of %u bytes is too large\n", increment_size);
1927 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1928 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1929 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1930 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1931 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1932 *typestring_offset += 8;
1934 pointer_count = write_pointer_description_offsets(
1935 file, attrs, type_array_get_element(type),
1936 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1937 typestring_offset);
1941 return pointer_count;
1944 /* Note: if file is NULL return value is number of pointers to write, else
1945 * it is the number of type format characters written */
1946 static int write_varying_array_pointer_descriptions(
1947 FILE *file, const attr_list_t *attrs, type_t *type,
1948 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1949 unsigned int *typestring_offset)
1951 unsigned int align;
1952 int pointer_count = 0;
1954 if (is_array(type) && type_array_has_variance(type))
1956 unsigned int temp = 0;
1957 /* unfortunately, this needs to be done in two passes to avoid
1958 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1959 pointer_count = write_pointer_description_offsets(
1960 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1961 if (pointer_count > 0)
1963 unsigned int increment_size;
1965 align = 0;
1966 increment_size = type_memsize(type_array_get_element(type), &align);
1968 if (increment_size > USHRT_MAX)
1969 error("array size of %u bytes is too large\n", increment_size);
1971 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1972 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1973 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1974 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1975 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1976 *typestring_offset += 8;
1978 pointer_count = write_pointer_description_offsets(
1979 file, attrs, type_array_get_element(type), offset_in_memory,
1980 offset_in_buffer, typestring_offset);
1983 else if (type_get_type(type) == TYPE_STRUCT)
1985 const var_t *v;
1986 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1988 if (offset_in_memory && offset_in_buffer)
1990 unsigned int padding;
1992 if (is_array(v->type) && type_array_has_variance(v->type))
1994 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1995 /* skip over variance and offset in buffer */
1996 *offset_in_buffer += 8;
1999 align = 0;
2000 type_memsize(v->type, &align);
2001 padding = ROUNDING(*offset_in_memory, align);
2002 *offset_in_memory += padding;
2003 *offset_in_buffer += padding;
2005 pointer_count += write_varying_array_pointer_descriptions(
2006 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2007 typestring_offset);
2010 else
2012 if (offset_in_memory && offset_in_buffer)
2014 unsigned int memsize;
2015 align = 0;
2016 memsize = type_memsize(type, &align);
2017 *offset_in_memory += memsize;
2018 /* increment these separately as in the case of conformant (varying)
2019 * structures these start at different values */
2020 *offset_in_buffer += memsize;
2024 return pointer_count;
2027 static void write_pointer_description(FILE *file, type_t *type,
2028 unsigned int *typestring_offset)
2030 unsigned int offset_in_buffer;
2031 unsigned int offset_in_memory;
2033 /* pass 1: search for single instance of a pointer (i.e. don't descend
2034 * into arrays) */
2035 if (!is_array(type))
2037 offset_in_memory = 0;
2038 offset_in_buffer = 0;
2039 write_no_repeat_pointer_descriptions(
2040 file, NULL, type,
2041 &offset_in_memory, &offset_in_buffer, typestring_offset);
2044 /* pass 2: search for pointers in fixed arrays */
2045 offset_in_memory = 0;
2046 offset_in_buffer = 0;
2047 write_fixed_array_pointer_descriptions(
2048 file, NULL, type,
2049 &offset_in_memory, &offset_in_buffer, typestring_offset);
2051 /* pass 3: search for pointers in conformant only arrays (but don't descend
2052 * into conformant varying or varying arrays) */
2053 if (is_conformant_array(type) &&
2054 (type_array_is_decl_as_ptr(type) || !current_structure))
2055 write_conformant_array_pointer_descriptions(
2056 file, NULL, type, 0, typestring_offset);
2057 else if (type_get_type(type) == TYPE_STRUCT &&
2058 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2060 unsigned int align = 0;
2061 type_t *carray = find_array_or_string_in_struct(type)->type;
2062 write_conformant_array_pointer_descriptions(
2063 file, NULL, carray,
2064 type_memsize(type, &align),
2065 typestring_offset);
2068 /* pass 4: search for pointers in varying arrays */
2069 offset_in_memory = 0;
2070 offset_in_buffer = 0;
2071 write_varying_array_pointer_descriptions(
2072 file, NULL, type,
2073 &offset_in_memory, &offset_in_buffer, typestring_offset);
2076 int is_declptr(const type_t *t)
2078 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2081 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2082 type_t *type, int toplevel_param,
2083 const char *name, unsigned int *typestring_offset)
2085 unsigned int start_offset;
2086 unsigned char rtype;
2087 type_t *elem_type;
2089 start_offset = *typestring_offset;
2090 update_tfsoff(type, start_offset, file);
2092 if (is_declptr(type))
2094 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2095 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2096 if (!pointer_type)
2097 pointer_type = RPC_FC_RP;
2098 print_start_tfs_comment(file, type, *typestring_offset);
2099 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2100 pointer_type, flag, string_of_type(pointer_type),
2101 flag ? " [simple_pointer]" : "");
2102 *typestring_offset += 2;
2103 if (!flag)
2105 print_file(file, 2, "NdrFcShort(0x2),\n");
2106 *typestring_offset += 2;
2110 if (is_array(type))
2111 elem_type = type_array_get_element(type);
2112 else
2113 elem_type = type_pointer_get_ref(type);
2115 if (type_get_type(elem_type) != TYPE_BASIC)
2117 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2118 return start_offset;
2121 rtype = get_basic_fc(elem_type);
2122 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2124 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2125 return start_offset;
2128 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2130 unsigned int dim = type_array_get_dim(type);
2132 /* FIXME: multi-dimensional array */
2133 if (0xffffu < dim)
2134 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2135 name, 0xffffu, dim - 0xffffu);
2137 if (rtype == RPC_FC_WCHAR)
2138 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2139 else
2140 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2141 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2142 *typestring_offset += 2;
2144 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2145 *typestring_offset += 2;
2147 return start_offset;
2149 else if (is_conformant_array(type))
2151 unsigned int align = 0;
2153 if (rtype == RPC_FC_WCHAR)
2154 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2155 else
2156 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2157 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2158 *typestring_offset += 2;
2160 *typestring_offset += write_conf_or_var_desc(
2161 file, current_structure,
2162 (!type_array_is_decl_as_ptr(type) && current_structure
2163 ? type_memsize(current_structure, &align)
2164 : 0),
2165 type, type_array_get_conformance(type));
2167 return start_offset;
2169 else
2171 if (rtype == RPC_FC_WCHAR)
2172 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2173 else
2174 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2175 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2176 *typestring_offset += 2;
2178 return start_offset;
2182 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2183 const char *name, unsigned int *typestring_offset)
2185 const expr_t *length_is = type_array_get_variance(type);
2186 const expr_t *size_is = type_array_get_conformance(type);
2187 unsigned int align = 0;
2188 unsigned int size;
2189 unsigned int start_offset;
2190 unsigned char fc;
2191 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2192 unsigned int baseoff
2193 = !type_array_is_decl_as_ptr(type) && current_structure
2194 ? type_memsize(current_structure, &align)
2195 : 0;
2197 if (!pointer_type)
2198 pointer_type = RPC_FC_RP;
2200 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2202 align = 0;
2203 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2204 fc = get_array_fc(type);
2206 start_offset = *typestring_offset;
2207 update_tfsoff(type, start_offset, file);
2208 print_start_tfs_comment(file, type, start_offset);
2209 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2210 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2211 *typestring_offset += 2;
2213 align = 0;
2214 if (fc != RPC_FC_BOGUS_ARRAY)
2216 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2218 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2219 *typestring_offset += 4;
2221 else
2223 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2224 *typestring_offset += 2;
2227 if (is_conformant_array(type))
2228 *typestring_offset
2229 += write_conf_or_var_desc(file, current_structure, baseoff,
2230 type, size_is);
2232 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2234 unsigned int elalign = 0;
2235 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2236 unsigned int dim = type_array_get_dim(type);
2238 if (fc == RPC_FC_LGVARRAY)
2240 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2241 *typestring_offset += 4;
2243 else
2245 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2246 *typestring_offset += 2;
2249 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2250 *typestring_offset += 2;
2253 if (length_is)
2254 *typestring_offset
2255 += write_conf_or_var_desc(file, current_structure, baseoff,
2256 type, length_is);
2258 if (type_has_pointers(type_array_get_element(type)) &&
2259 (type_array_is_decl_as_ptr(type) || !current_structure))
2261 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2262 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2263 *typestring_offset += 2;
2264 write_pointer_description(file, type, typestring_offset);
2265 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2266 *typestring_offset += 1;
2269 write_array_element_type(file, type, FALSE, typestring_offset);
2270 write_end(file, typestring_offset);
2272 else
2274 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2275 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2276 *typestring_offset += 2;
2277 *typestring_offset
2278 += write_conf_or_var_desc(file, current_structure, baseoff,
2279 type, size_is);
2280 *typestring_offset
2281 += write_conf_or_var_desc(file, current_structure, baseoff,
2282 type, length_is);
2284 write_array_element_type(file, type, TRUE, typestring_offset);
2285 write_end(file, typestring_offset);
2288 return start_offset;
2291 static const var_t *find_array_or_string_in_struct(const type_t *type)
2293 const var_list_t *fields = type_struct_get_fields(type);
2294 const var_t *last_field;
2295 const type_t *ft;
2297 if (!fields || list_empty(fields))
2298 return NULL;
2300 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2301 ft = last_field->type;
2303 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2304 return last_field;
2306 if (type_get_type(ft) == TYPE_STRUCT)
2307 return find_array_or_string_in_struct(ft);
2308 else
2309 return NULL;
2312 static void write_struct_members(FILE *file, const type_t *type,
2313 int is_complex, unsigned int *corroff,
2314 unsigned int *typestring_offset)
2316 const var_t *field;
2317 unsigned short offset = 0;
2318 unsigned int salign = 1;
2319 int padding;
2320 var_list_t *fields = type_struct_get_fields(type);
2322 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2324 type_t *ft = field->type;
2325 unsigned int align = 0;
2326 unsigned int size = type_memsize(ft, &align);
2327 align = clamp_align(align);
2328 if (salign < align) salign = align;
2330 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2332 if ((align - 1) & offset)
2334 unsigned char fc = 0;
2335 switch (align)
2337 case 2:
2338 fc = RPC_FC_ALIGNM2;
2339 break;
2340 case 4:
2341 fc = RPC_FC_ALIGNM4;
2342 break;
2343 case 8:
2344 fc = RPC_FC_ALIGNM8;
2345 break;
2346 default:
2347 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2349 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2350 offset = ROUND_SIZE(offset, align);
2351 *typestring_offset += 1;
2353 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2354 typestring_offset);
2355 offset += size;
2359 padding = ROUNDING(offset, salign);
2360 if (padding)
2362 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2363 RPC_FC_STRUCTPAD1 + padding - 1,
2364 padding);
2365 *typestring_offset += 1;
2368 write_end(file, typestring_offset);
2371 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2372 const char *name, unsigned int *tfsoff)
2374 const type_t *save_current_structure = current_structure;
2375 unsigned int total_size;
2376 const var_t *array;
2377 unsigned int start_offset;
2378 unsigned int array_offset;
2379 unsigned int align = 0;
2380 unsigned int corroff;
2381 var_t *f;
2382 unsigned char fc = get_struct_fc(type);
2383 var_list_t *fields = type_struct_get_fields(type);
2385 guard_rec(type);
2386 current_structure = type;
2388 total_size = type_memsize(type, &align);
2389 if (total_size > USHRT_MAX)
2390 error("structure size for %s exceeds %d bytes by %d bytes\n",
2391 name, USHRT_MAX, total_size - USHRT_MAX);
2393 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2394 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
2396 array = find_array_or_string_in_struct(type);
2397 if (array && !processed(array->type))
2398 array_offset
2399 = is_string_type(array->attrs, array->type)
2400 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2401 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2403 corroff = *tfsoff;
2404 write_descriptors(file, type, tfsoff);
2406 start_offset = *tfsoff;
2407 update_tfsoff(type, start_offset, file);
2408 print_start_tfs_comment(file, type, start_offset);
2409 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2410 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2411 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2412 *tfsoff += 4;
2414 if (array)
2416 unsigned int absoff = array->type->typestring_offset;
2417 short reloff = absoff - *tfsoff;
2418 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2419 reloff, reloff, absoff);
2420 *tfsoff += 2;
2422 else if (fc == RPC_FC_BOGUS_STRUCT)
2424 print_file(file, 2, "NdrFcShort(0x0),\n");
2425 *tfsoff += 2;
2428 if (fc == RPC_FC_BOGUS_STRUCT)
2430 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2431 nothing is written to file yet. On the actual writing pass,
2432 this will have been updated. */
2433 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2434 int reloff = absoff - *tfsoff;
2435 assert( reloff >= 0 );
2436 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2437 reloff, reloff, absoff);
2438 *tfsoff += 2;
2440 else if ((fc == RPC_FC_PSTRUCT) ||
2441 (fc == RPC_FC_CPSTRUCT) ||
2442 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
2444 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2445 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2446 *tfsoff += 2;
2447 write_pointer_description(file, type, tfsoff);
2448 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2449 *tfsoff += 1;
2452 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2453 tfsoff);
2455 if (fc == RPC_FC_BOGUS_STRUCT)
2457 const var_t *f;
2459 type->ptrdesc = *tfsoff;
2460 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2462 type_t *ft = f->type;
2463 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
2465 case TGT_POINTER:
2466 if (is_string_type(f->attrs, ft))
2467 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2468 else
2469 write_pointer_tfs(file, f->attrs, ft,
2470 type_pointer_get_ref(ft)->typestring_offset,
2471 FALSE, tfsoff);
2472 break;
2473 case TGT_ARRAY:
2474 if (type_array_is_decl_as_ptr(ft))
2476 unsigned int offset;
2478 print_file(file, 0, "/* %d */\n", *tfsoff);
2480 offset = ft->typestring_offset;
2481 /* skip over the pointer that is written for strings, since a
2482 * pointer has to be written in-place here */
2483 if (is_string_type(f->attrs, ft))
2484 offset += 4;
2485 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2487 break;
2488 default:
2489 break;
2492 if (type->ptrdesc == *tfsoff)
2493 type->ptrdesc = 0;
2496 current_structure = save_current_structure;
2497 return start_offset;
2500 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2502 if (t == NULL)
2504 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2506 else
2508 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2510 unsigned char fc;
2511 if (type_get_type(t) == TYPE_BASIC)
2512 fc = get_basic_fc(t);
2513 else
2514 fc = get_enum_fc(t);
2515 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2516 fc, string_of_type(fc));
2518 else if (t->typestring_offset)
2520 short reloff = t->typestring_offset - *tfsoff;
2521 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2522 reloff, reloff, t->typestring_offset);
2524 else
2525 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2528 *tfsoff += 2;
2531 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2533 unsigned int align;
2534 unsigned int start_offset;
2535 unsigned int size;
2536 var_list_t *fields;
2537 unsigned int nbranch = 0;
2538 type_t *deftype = NULL;
2539 short nodeftype = 0xffff;
2540 var_t *f;
2542 guard_rec(type);
2544 align = 0;
2545 size = type_memsize(type, &align);
2547 fields = type_union_get_cases(type);
2549 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2551 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2552 if (cases)
2553 nbranch += list_count(cases);
2554 if (f->type)
2555 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2558 start_offset = *tfsoff;
2559 update_tfsoff(type, start_offset, file);
2560 print_start_tfs_comment(file, type, start_offset);
2561 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2563 const var_t *sv = type_union_get_switch_value(type);
2564 const type_t *st = sv->type;
2565 unsigned char fc;
2567 if (type_get_type(st) == TYPE_BASIC)
2569 switch (get_basic_fc(st))
2571 case RPC_FC_CHAR:
2572 case RPC_FC_SMALL:
2573 case RPC_FC_BYTE:
2574 case RPC_FC_USMALL:
2575 case RPC_FC_WCHAR:
2576 case RPC_FC_SHORT:
2577 case RPC_FC_USHORT:
2578 case RPC_FC_LONG:
2579 case RPC_FC_ULONG:
2580 fc = get_basic_fc(st);
2581 break;
2582 default:
2583 fc = 0;
2584 error("union switch type must be an integer, char, or enum\n");
2587 else if (type_get_type(st) == TYPE_ENUM)
2588 fc = get_enum_fc(st);
2589 else
2590 error("union switch type must be an integer, char, or enum\n");
2592 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2593 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2594 0x40 | fc, string_of_type(fc));
2595 *tfsoff += 2;
2597 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2599 static const expr_t dummy_expr; /* FIXME */
2600 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2601 unsigned char fc;
2603 if (type_get_type(st) == TYPE_BASIC)
2605 switch (get_basic_fc(st))
2607 case RPC_FC_CHAR:
2608 case RPC_FC_SMALL:
2609 case RPC_FC_USMALL:
2610 case RPC_FC_SHORT:
2611 case RPC_FC_USHORT:
2612 case RPC_FC_LONG:
2613 case RPC_FC_ULONG:
2614 case RPC_FC_ENUM16:
2615 case RPC_FC_ENUM32:
2616 fc = get_basic_fc(st);
2617 break;
2618 default:
2619 fc = 0;
2620 error("union switch type must be an integer, char, or enum\n");
2623 else if (type_get_type(st) == TYPE_ENUM)
2624 fc = get_enum_fc(st);
2625 else
2626 error("union switch type must be an integer, char, or enum\n");
2628 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2629 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2630 fc, string_of_type(fc));
2631 *tfsoff += 2;
2633 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2634 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2635 *tfsoff += 2;
2636 print_file(file, 0, "/* %u */\n", *tfsoff);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2641 *tfsoff += 4;
2643 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2645 type_t *ft = f->type;
2646 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2647 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2648 expr_t *c;
2650 if (cases == NULL && !deflt)
2651 error("union field %s with neither case nor default attribute\n", f->name);
2653 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2655 /* MIDL doesn't check for duplicate cases, even though that seems
2656 like a reasonable thing to do, it just dumps them to the TFS
2657 like we're going to do here. */
2658 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2659 *tfsoff += 4;
2660 write_branch_type(file, ft, tfsoff);
2663 /* MIDL allows multiple default branches, even though that seems
2664 illogical, it just chooses the last one, which is what we will
2665 do. */
2666 if (deflt)
2668 deftype = ft;
2669 nodeftype = 0;
2673 if (deftype)
2675 write_branch_type(file, deftype, tfsoff);
2677 else
2679 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2680 *tfsoff += 2;
2683 return start_offset;
2686 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2687 unsigned int *typeformat_offset)
2689 unsigned int i;
2690 unsigned int start_offset = *typeformat_offset;
2691 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2693 print_start_tfs_comment(file, type, start_offset);
2695 if (iid)
2697 print_file(file, 2, "0x2f, /* FC_IP */\n");
2698 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2699 *typeformat_offset
2700 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
2702 else
2704 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2705 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2707 if (! uuid)
2708 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2710 update_tfsoff(type, start_offset, file);
2711 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2712 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2713 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2714 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2715 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2716 for (i = 0; i < 8; ++i)
2717 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2719 if (file)
2720 fprintf(file, "\n");
2722 *typeformat_offset += 18;
2724 return start_offset;
2727 static unsigned int write_contexthandle_tfs(FILE *file,
2728 const attr_list_t *attrs,
2729 const type_t *type,
2730 unsigned int *typeformat_offset)
2732 unsigned int start_offset = *typeformat_offset;
2733 unsigned char flags = 0;
2735 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2736 flags |= NDR_STRICT_CONTEXT_HANDLE;
2738 if (is_ptr(type))
2739 flags |= 0x80;
2740 if (is_attr(attrs, ATTR_IN))
2742 flags |= 0x40;
2743 if (!is_attr(attrs, ATTR_OUT))
2744 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2746 if (is_attr(attrs, ATTR_OUT))
2747 flags |= 0x20;
2749 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2750 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2751 /* return and can't be null values overlap */
2752 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2753 print_file(file, 0, "can't be null, ");
2754 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2755 print_file(file, 0, "serialize, ");
2756 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2757 print_file(file, 0, "no serialize, ");
2758 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2759 print_file(file, 0, "strict, ");
2760 if ((flags & 0x21) == 0x20)
2761 print_file(file, 0, "out, ");
2762 if ((flags & 0x21) == 0x21)
2763 print_file(file, 0, "return, ");
2764 if (flags & 0x40)
2765 print_file(file, 0, "in, ");
2766 if (flags & 0x80)
2767 print_file(file, 0, "via ptr, ");
2768 print_file(file, 0, "*/\n");
2769 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2770 print_file(file, 2, "0, /* FIXME: param num */\n");
2771 *typeformat_offset += 4;
2773 return start_offset;
2776 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
2777 type_t *type, expr_list_t *range_list,
2778 unsigned int *typeformat_offset)
2780 unsigned char fc;
2781 unsigned int start_offset = *typeformat_offset;
2782 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
2783 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
2785 if (type_get_type(type) == TYPE_BASIC)
2786 fc = get_basic_fc(type);
2787 else
2788 fc = get_enum_fc(type);
2790 /* fc must fit in lower 4-bits of 8-bit field below */
2791 assert(fc <= 0xf);
2793 print_file(file, 0, "/* %u */\n", *typeformat_offset);
2794 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
2795 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2796 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_min->cval, range_min->cval);
2797 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_max->cval, range_max->cval);
2798 *typeformat_offset += 10;
2800 return start_offset;
2803 static unsigned int write_type_tfs(FILE *file, int indent,
2804 const attr_list_t *attrs, type_t *type,
2805 const char *name,
2806 enum type_context context,
2807 unsigned int *typeformat_offset)
2809 unsigned int offset;
2811 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2813 case TGT_CTXT_HANDLE:
2814 case TGT_CTXT_HANDLE_POINTER:
2815 return write_contexthandle_tfs(file, attrs, type, typeformat_offset);
2816 case TGT_USER_TYPE:
2817 write_user_tfs(file, type, typeformat_offset);
2818 return type->typestring_offset;
2819 case TGT_STRING:
2820 return write_string_tfs(file, attrs, type,
2821 context == TYPE_CONTEXT_TOPLEVELPARAM,
2822 name, typeformat_offset);
2823 case TGT_ARRAY:
2825 unsigned int off;
2826 /* conformant and pointer arrays are handled specially */
2827 if ((context != TYPE_CONTEXT_CONTAINER &&
2828 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
2829 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
2830 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
2831 else
2832 off = 0;
2833 if (context != TYPE_CONTEXT_CONTAINER &&
2834 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2836 int ptr_type;
2837 ptr_type = get_pointer_fc(type, attrs,
2838 context == TYPE_CONTEXT_TOPLEVELPARAM);
2839 if (ptr_type != RPC_FC_RP)
2841 unsigned int absoff = type->typestring_offset;
2842 short reloff = absoff - (*typeformat_offset + 2);
2843 off = *typeformat_offset;
2844 print_file(file, 0, "/* %d */\n", off);
2845 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2846 string_of_type(ptr_type));
2847 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2848 reloff, reloff, absoff);
2849 *typeformat_offset += 4;
2852 return off;
2854 case TGT_STRUCT:
2855 if (processed(type)) return type->typestring_offset;
2856 return write_struct_tfs(file, type, name, typeformat_offset);
2857 case TGT_UNION:
2858 if (processed(type)) return type->typestring_offset;
2859 return write_union_tfs(file, type, typeformat_offset);
2860 case TGT_ENUM:
2861 case TGT_BASIC:
2862 /* nothing to do */
2863 return 0;
2864 case TGT_RANGE:
2866 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
2867 if (!range_list)
2868 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2869 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
2871 case TGT_IFACE_POINTER:
2872 return write_ip_tfs(file, attrs, type, typeformat_offset);
2873 case TGT_POINTER:
2874 if (processed(type_pointer_get_ref(type)))
2875 offset = type_pointer_get_ref(type)->typestring_offset;
2876 else
2878 enum type_context ref_context;
2879 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
2880 ref_context = TYPE_CONTEXT_PARAM;
2881 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2882 ref_context = TYPE_CONTEXT_CONTAINER;
2883 else
2884 ref_context = context;
2885 offset = write_type_tfs(
2886 file, indent, attrs, type_pointer_get_ref(type), name,
2887 ref_context, typeformat_offset);
2889 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2890 return 0;
2891 else
2892 return write_pointer_tfs(file, attrs, type, offset,
2893 context == TYPE_CONTEXT_TOPLEVELPARAM,
2894 typeformat_offset);
2895 case TGT_INVALID:
2896 break;
2898 error("invalid type %s for var %s\n", type->name, name);
2899 return 0;
2902 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2903 const char *name, int write_ptr, unsigned int *tfsoff)
2905 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
2908 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2909 type_pred_t pred, unsigned int *typeformat_offset)
2911 const var_t *var;
2912 const statement_t *stmt;
2914 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2916 const type_t *iface;
2917 const statement_t *stmt_func;
2919 if (stmt->type == STMT_LIBRARY)
2921 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2922 continue;
2924 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2925 continue;
2927 iface = stmt->u.type;
2928 if (!pred(iface))
2929 continue;
2931 current_iface = iface;
2932 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2934 const var_t *func = stmt_func->u.var;
2935 if (is_local(func->attrs)) continue;
2937 if (!is_void(type_function_get_rettype(func->type)))
2939 update_tfsoff(type_function_get_rettype(func->type),
2940 write_type_tfs(
2941 file, 2, func->attrs,
2942 type_function_get_rettype(func->type),
2943 func->name, TYPE_CONTEXT_PARAM,
2944 typeformat_offset),
2945 file);
2948 if (type_get_function_args(func->type))
2949 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2950 update_tfsoff(
2951 var->type,
2952 write_type_tfs(
2953 file, 2, var->attrs, var->type, var->name,
2954 TYPE_CONTEXT_TOPLEVELPARAM,
2955 typeformat_offset),
2956 file);
2960 return *typeformat_offset + 1;
2963 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2965 unsigned int typeformat_offset = 2;
2967 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2971 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2973 int indent = 0;
2975 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2976 print_file(file, indent, "{\n");
2977 indent++;
2978 print_file(file, indent, "0,\n");
2979 print_file(file, indent, "{\n");
2980 indent++;
2981 print_file(file, indent, "NdrFcShort(0x0),\n");
2983 set_all_tfswrite(TRUE);
2984 process_tfs(file, stmts, pred);
2986 print_file(file, indent, "0x0\n");
2987 indent--;
2988 print_file(file, indent, "}\n");
2989 indent--;
2990 print_file(file, indent, "};\n");
2991 print_file(file, indent, "\n");
2994 static unsigned int get_required_buffer_size_type(
2995 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2997 *alignment = 0;
2998 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3000 case TGT_USER_TYPE:
3002 const char *uname;
3003 const type_t *utype = get_user_type(type, &uname);
3004 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3006 case TGT_BASIC:
3007 switch (get_basic_fc(type))
3009 case RPC_FC_BYTE:
3010 case RPC_FC_CHAR:
3011 case RPC_FC_USMALL:
3012 case RPC_FC_SMALL:
3013 *alignment = 4;
3014 return 1;
3016 case RPC_FC_WCHAR:
3017 case RPC_FC_USHORT:
3018 case RPC_FC_SHORT:
3019 *alignment = 4;
3020 return 2;
3022 case RPC_FC_ULONG:
3023 case RPC_FC_LONG:
3024 case RPC_FC_FLOAT:
3025 case RPC_FC_ERROR_STATUS_T:
3026 *alignment = 4;
3027 return 4;
3029 case RPC_FC_HYPER:
3030 case RPC_FC_DOUBLE:
3031 *alignment = 8;
3032 return 8;
3034 case RPC_FC_INT3264:
3035 case RPC_FC_UINT3264:
3036 assert( pointer_size );
3037 *alignment = pointer_size;
3038 return pointer_size;
3040 case RPC_FC_IGNORE:
3041 case RPC_FC_BIND_PRIMITIVE:
3042 return 0;
3044 default:
3045 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3046 get_basic_fc(type));
3047 return 0;
3049 break;
3051 case TGT_ENUM:
3052 switch (get_enum_fc(type))
3054 case RPC_FC_ENUM32:
3055 *alignment = 4;
3056 return 4;
3057 case RPC_FC_ENUM16:
3058 *alignment = 4;
3059 return 2;
3061 break;
3063 case TGT_STRUCT:
3064 if (get_struct_fc(type) == RPC_FC_STRUCT)
3066 if (!type_struct_get_fields(type)) return 0;
3067 return fields_memsize(type_struct_get_fields(type), alignment);
3069 break;
3071 case TGT_POINTER:
3072 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3074 const type_t *ref = type_pointer_get_ref(type);
3075 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3077 case TGT_BASIC:
3078 case TGT_ENUM:
3079 case TGT_RANGE:
3080 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3081 case TGT_STRUCT:
3082 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3083 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3084 break;
3085 case TGT_USER_TYPE:
3086 case TGT_CTXT_HANDLE:
3087 case TGT_CTXT_HANDLE_POINTER:
3088 case TGT_STRING:
3089 case TGT_POINTER:
3090 case TGT_ARRAY:
3091 case TGT_IFACE_POINTER:
3092 case TGT_UNION:
3093 case TGT_INVALID:
3094 break;
3097 break;
3099 case TGT_ARRAY:
3100 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3101 return type_array_get_dim(type) *
3102 get_required_buffer_size_type(type_array_get_element(type), name,
3103 NULL, FALSE, alignment);
3105 default:
3106 break;
3108 return 0;
3111 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3113 int in_attr = is_attr(var->attrs, ATTR_IN);
3114 int out_attr = is_attr(var->attrs, ATTR_OUT);
3116 if (!in_attr && !out_attr)
3117 in_attr = 1;
3119 *alignment = 0;
3121 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3122 pass == PASS_RETURN)
3124 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3126 *alignment = 4;
3127 return 20;
3130 if (!is_string_type(var->attrs, var->type))
3131 return get_required_buffer_size_type(var->type, var->name,
3132 var->attrs, TRUE, alignment);
3134 return 0;
3137 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3139 const var_t *var;
3140 unsigned int total_size = 0, alignment;
3142 if (type_get_function_args(func->type))
3144 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3146 total_size += get_required_buffer_size(var, &alignment, pass);
3147 total_size += alignment;
3151 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3153 var_t v = *func;
3154 v.type = type_function_get_rettype(func->type);
3155 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3156 total_size += alignment;
3158 return total_size;
3161 static void print_phase_function(FILE *file, int indent, const char *type,
3162 const char *local_var_prefix, enum remoting_phase phase,
3163 const var_t *var, unsigned int type_offset)
3165 const char *function;
3166 switch (phase)
3168 case PHASE_BUFFERSIZE:
3169 function = "BufferSize";
3170 break;
3171 case PHASE_MARSHAL:
3172 function = "Marshall";
3173 break;
3174 case PHASE_UNMARSHAL:
3175 function = "Unmarshall";
3176 break;
3177 case PHASE_FREE:
3178 function = "Free";
3179 break;
3180 default:
3181 assert(0);
3182 return;
3185 print_file(file, indent, "Ndr%s%s(\n", type, function);
3186 indent++;
3187 print_file(file, indent, "&__frame->_StubMsg,\n");
3188 print_file(file, indent, "%s%s%s%s%s,\n",
3189 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3190 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3191 local_var_prefix,
3192 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3193 var->name);
3194 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3195 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3196 if (phase == PHASE_UNMARSHAL)
3197 print_file(file, indent, "0);\n");
3198 indent--;
3201 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3202 enum remoting_phase phase, enum pass pass, const var_t *var,
3203 const char *varname)
3205 type_t *type = var->type;
3206 unsigned int size;
3207 unsigned int alignment = 0;
3209 /* no work to do for other phases, buffer sizing is done elsewhere */
3210 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3211 return;
3213 if (type_get_type(type) == TYPE_ENUM ||
3214 (type_get_type(type) == TYPE_BASIC &&
3215 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3216 pointer_size != 4))
3218 unsigned char fc;
3220 if (type_get_type(type) == TYPE_ENUM)
3221 fc = get_enum_fc(type);
3222 else
3223 fc = get_basic_fc(type);
3225 if (phase == PHASE_MARSHAL)
3226 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3227 else
3228 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3229 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3230 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3231 local_var_prefix,
3232 var->name);
3233 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3235 else
3237 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3238 switch (get_basic_fc(ref))
3240 case RPC_FC_BYTE:
3241 case RPC_FC_CHAR:
3242 case RPC_FC_SMALL:
3243 case RPC_FC_USMALL:
3244 size = 1;
3245 alignment = 1;
3246 break;
3248 case RPC_FC_WCHAR:
3249 case RPC_FC_USHORT:
3250 case RPC_FC_SHORT:
3251 size = 2;
3252 alignment = 2;
3253 break;
3255 case RPC_FC_ULONG:
3256 case RPC_FC_LONG:
3257 case RPC_FC_FLOAT:
3258 case RPC_FC_ERROR_STATUS_T:
3259 /* pointer_size must be 4 if we got here in these two cases */
3260 case RPC_FC_INT3264:
3261 case RPC_FC_UINT3264:
3262 size = 4;
3263 alignment = 4;
3264 break;
3266 case RPC_FC_HYPER:
3267 case RPC_FC_DOUBLE:
3268 size = 8;
3269 alignment = 8;
3270 break;
3272 case RPC_FC_IGNORE:
3273 case RPC_FC_BIND_PRIMITIVE:
3274 /* no marshalling needed */
3275 return;
3277 default:
3278 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3279 var->name, get_basic_fc(ref));
3280 size = 0;
3283 if (phase == PHASE_MARSHAL && alignment > 1)
3284 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3285 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3286 alignment - 1, alignment - 1);
3288 if (phase == PHASE_MARSHAL)
3290 print_file(file, indent, "*(");
3291 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3292 if (is_ptr(type))
3293 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3294 else
3295 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3296 fprintf(file, "%s%s", local_var_prefix, varname);
3297 fprintf(file, ";\n");
3299 else if (phase == PHASE_UNMARSHAL)
3301 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3302 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3303 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3304 print_file(file, indent, "{\n");
3305 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3306 print_file(file, indent, "}\n");
3307 print_file(file, indent, "%s%s%s",
3308 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3309 local_var_prefix, varname);
3310 if (pass == PASS_IN && is_ptr(type))
3311 fprintf(file, " = (");
3312 else
3313 fprintf(file, " = *(");
3314 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3315 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3318 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3319 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3320 fprintf(file, ");\n");
3324 /* returns whether the MaxCount, Offset or ActualCount members need to be
3325 * filled in for the specified phase */
3326 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3328 return (phase != PHASE_UNMARSHAL);
3331 expr_t *get_size_is_expr(const type_t *t, const char *name)
3333 expr_t *x = NULL;
3335 for ( ; is_array(t); t = type_array_get_element(t))
3336 if (type_array_has_conformance(t) &&
3337 type_array_get_conformance(t)->type != EXPR_VOID)
3339 if (!x)
3340 x = type_array_get_conformance(t);
3341 else
3342 error("%s: multidimensional conformant"
3343 " arrays not supported at the top level\n",
3344 name);
3347 return x;
3350 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3351 enum remoting_phase phase, const var_t *var)
3353 const type_t *type = var->type;
3354 /* get fundamental type for the argument */
3355 for (;;)
3357 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3359 case TGT_ARRAY:
3360 if (is_conformance_needed_for_phase(phase))
3362 if (type_array_has_conformance(type) &&
3363 type_array_get_conformance(type)->type != EXPR_VOID)
3365 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3366 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3367 fprintf(file, ";\n\n");
3369 if (type_array_has_variance(type))
3371 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3372 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3373 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3374 fprintf(file, ";\n\n");
3377 break;
3378 case TGT_UNION:
3379 if (type_get_type(type) == TYPE_UNION &&
3380 is_conformance_needed_for_phase(phase))
3382 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3383 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3384 fprintf(file, ";\n\n");
3386 break;
3387 case TGT_IFACE_POINTER:
3389 expr_t *iid;
3391 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3393 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3394 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3395 fprintf( file, ";\n\n" );
3397 break;
3399 case TGT_POINTER:
3400 type = type_pointer_get_ref(type);
3401 continue;
3402 case TGT_INVALID:
3403 case TGT_USER_TYPE:
3404 case TGT_CTXT_HANDLE:
3405 case TGT_CTXT_HANDLE_POINTER:
3406 case TGT_STRING:
3407 case TGT_BASIC:
3408 case TGT_ENUM:
3409 case TGT_STRUCT:
3410 case TGT_RANGE:
3411 break;
3413 break;
3417 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3418 enum pass pass, enum remoting_phase phase, const var_t *var)
3420 int in_attr, out_attr, pointer_type;
3421 const type_t *type = var->type;
3422 unsigned int start_offset = type->typestring_offset;
3424 if (is_ptr(type) || is_array(type))
3425 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3426 else
3427 pointer_type = 0;
3429 in_attr = is_attr(var->attrs, ATTR_IN);
3430 out_attr = is_attr(var->attrs, ATTR_OUT);
3431 if (!in_attr && !out_attr)
3432 in_attr = 1;
3434 if (phase != PHASE_FREE)
3435 switch (pass)
3437 case PASS_IN:
3438 if (!in_attr) return;
3439 break;
3440 case PASS_OUT:
3441 if (!out_attr) return;
3442 break;
3443 case PASS_RETURN:
3444 break;
3447 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3449 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3451 case TGT_CTXT_HANDLE:
3452 case TGT_CTXT_HANDLE_POINTER:
3453 if (phase == PHASE_MARSHAL)
3455 if (pass == PASS_IN)
3457 /* if the context_handle attribute appears in the chain of types
3458 * without pointers being followed, then the context handle must
3459 * be direct, otherwise it is a pointer */
3460 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3461 print_file(file, indent, "NdrClientContextMarshall(\n");
3462 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3463 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3464 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3466 else
3468 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3469 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3470 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3471 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3472 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3475 else if (phase == PHASE_UNMARSHAL)
3477 if (pass == PASS_OUT)
3479 if (!in_attr)
3480 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3481 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3482 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3483 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3484 print_file(file, indent + 1, "__frame->_Handle);\n");
3486 else
3488 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3489 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3490 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3493 break;
3494 case TGT_USER_TYPE:
3495 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3496 break;
3497 case TGT_STRING:
3498 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3499 pointer_type != RPC_FC_RP)
3501 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3502 !in_attr && is_conformant_array(type))
3504 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3505 indent++;
3506 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3508 /* strings returned are assumed to be global and hence don't
3509 * need freeing */
3510 else if (is_declptr(type) &&
3511 !(phase == PHASE_FREE && pass == PASS_RETURN))
3512 print_phase_function(file, indent, "Pointer", local_var_prefix,
3513 phase, var, start_offset);
3515 else
3517 unsigned int real_start_offset = start_offset;
3518 /* skip over pointer description straight to string description */
3519 if (is_declptr(type))
3521 if (is_conformant_array(type))
3522 real_start_offset += 4;
3523 else
3524 real_start_offset += 2;
3526 if (is_array(type) && !is_conformant_array(type))
3527 print_phase_function(file, indent, "NonConformantString",
3528 local_var_prefix, phase, var,
3529 real_start_offset);
3530 else
3531 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3532 phase, var, real_start_offset);
3534 break;
3535 case TGT_ARRAY:
3537 unsigned char tc = get_array_fc(type);
3538 const char *array_type = NULL;
3540 /* We already have the size_is expression since it's at the
3541 top level, but do checks for multidimensional conformant
3542 arrays. When we handle them, we'll need to extend this
3543 function to return a list, and then we'll actually use
3544 the return value. */
3545 get_size_is_expr(type, var->name);
3547 switch (tc)
3549 case RPC_FC_SMFARRAY:
3550 case RPC_FC_LGFARRAY:
3551 array_type = "FixedArray";
3552 break;
3553 case RPC_FC_SMVARRAY:
3554 case RPC_FC_LGVARRAY:
3555 array_type = "VaryingArray";
3556 break;
3557 case RPC_FC_CARRAY:
3558 array_type = "ConformantArray";
3559 break;
3560 case RPC_FC_CVARRAY:
3561 array_type = "ConformantVaryingArray";
3562 break;
3563 case RPC_FC_BOGUS_ARRAY:
3564 array_type = "ComplexArray";
3565 break;
3568 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3569 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3570 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3572 /* these are all unmarshalled by allocating memory */
3573 if (tc == RPC_FC_BOGUS_ARRAY ||
3574 tc == RPC_FC_CVARRAY ||
3575 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3576 (tc == RPC_FC_CARRAY && !in_attr))
3578 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3579 indent++;
3580 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3583 break;
3585 case TGT_BASIC:
3586 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3587 break;
3588 case TGT_ENUM:
3589 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3590 break;
3591 case TGT_RANGE:
3592 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3593 /* Note: this goes beyond what MIDL does - it only supports arguments
3594 * with the [range] attribute in Oicf mode */
3595 if (phase == PHASE_UNMARSHAL)
3597 const expr_t *range_min;
3598 const expr_t *range_max;
3599 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
3600 if (!range_list)
3601 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3602 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3603 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3605 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
3606 write_type_decl(file, var->type, NULL);
3607 fprintf(file, ")0x%lx) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
3608 write_type_decl(file, var->type, NULL);
3609 fprintf(file, ")0x%lx))\n", range_max->cval);
3610 print_file(file, indent, "{\n");
3611 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3612 print_file(file, indent, "}\n");
3614 break;
3615 case TGT_STRUCT:
3616 switch (get_struct_fc(type))
3618 case RPC_FC_STRUCT:
3619 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3620 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3621 break;
3622 case RPC_FC_PSTRUCT:
3623 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3624 break;
3625 case RPC_FC_CSTRUCT:
3626 case RPC_FC_CPSTRUCT:
3627 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3628 break;
3629 case RPC_FC_CVSTRUCT:
3630 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3631 break;
3632 case RPC_FC_BOGUS_STRUCT:
3633 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3634 break;
3635 default:
3636 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3638 break;
3639 case TGT_UNION:
3641 const char *union_type = NULL;
3643 if (type_get_type(type) == TYPE_UNION)
3644 union_type = "NonEncapsulatedUnion";
3645 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3646 union_type = "EncapsulatedUnion";
3648 print_phase_function(file, indent, union_type, local_var_prefix,
3649 phase, var, start_offset);
3650 break;
3652 case TGT_POINTER:
3654 const type_t *ref = type_pointer_get_ref(type);
3655 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3657 case TGT_BASIC:
3658 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3659 break;
3660 case TGT_ENUM:
3661 /* base types have known sizes, so don't need a sizing pass
3662 * and don't have any memory to free and so don't need a
3663 * freeing pass */
3664 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3665 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3666 break;
3667 case TGT_STRUCT:
3669 const char *struct_type = NULL;
3670 switch (get_struct_fc(ref))
3672 case RPC_FC_STRUCT:
3673 /* simple structs have known sizes, so don't need a sizing
3674 * pass and don't have any memory to free and so don't
3675 * need a freeing pass */
3676 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3677 struct_type = "SimpleStruct";
3678 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3680 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3681 indent++;
3682 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3683 indent--;
3685 break;
3686 case RPC_FC_PSTRUCT:
3687 struct_type = "SimpleStruct";
3688 break;
3689 case RPC_FC_CSTRUCT:
3690 case RPC_FC_CPSTRUCT:
3691 struct_type = "ConformantStruct";
3692 break;
3693 case RPC_FC_CVSTRUCT:
3694 struct_type = "ConformantVaryingStruct";
3695 break;
3696 case RPC_FC_BOGUS_STRUCT:
3697 struct_type = "ComplexStruct";
3698 break;
3699 default:
3700 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3703 if (struct_type)
3705 if (phase == PHASE_FREE)
3706 struct_type = "Pointer";
3707 else
3708 start_offset = ref->typestring_offset;
3709 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3711 break;
3713 case TGT_UNION:
3715 const char *union_type = NULL;
3716 if (phase == PHASE_FREE)
3717 union_type = "Pointer";
3718 else
3720 if (type_get_type(ref) == TYPE_UNION)
3721 union_type = "NonEncapsulatedUnion";
3722 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3723 union_type = "EncapsulatedUnion";
3725 start_offset = ref->typestring_offset;
3728 print_phase_function(file, indent, union_type, local_var_prefix,
3729 phase, var, start_offset);
3730 break;
3732 case TGT_STRING:
3733 case TGT_POINTER:
3734 case TGT_ARRAY:
3735 case TGT_RANGE:
3736 case TGT_IFACE_POINTER:
3737 case TGT_USER_TYPE:
3738 case TGT_CTXT_HANDLE:
3739 case TGT_CTXT_HANDLE_POINTER:
3740 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3741 break;
3742 case TGT_INVALID:
3743 assert(0);
3744 break;
3746 else
3747 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3748 break;
3750 case TGT_IFACE_POINTER:
3751 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3752 break;
3753 case TGT_INVALID:
3754 assert(0);
3755 break;
3757 fprintf(file, "\n");
3760 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3761 enum pass pass, enum remoting_phase phase)
3763 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3765 unsigned int size = get_function_buffer_size( func, pass );
3766 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3769 if (pass == PASS_RETURN)
3771 var_t var;
3772 var = *func;
3773 var.type = type_function_get_rettype(func->type);
3774 var.name = xstrdup( "_RetVal" );
3775 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3776 free( var.name );
3778 else
3780 const var_t *var;
3781 if (!type_get_function_args(func->type))
3782 return;
3783 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3784 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3789 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3791 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3795 unsigned int get_size_procformatstring_func(const var_t *func)
3797 const var_t *var;
3798 unsigned int size = 0;
3800 /* argument list size */
3801 if (type_get_function_args(func->type))
3802 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3803 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3805 /* return value size */
3806 if (is_void(type_function_get_rettype(func->type)))
3807 size += 2; /* FC_END and FC_PAD */
3808 else
3809 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3811 return size;
3814 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3816 const statement_t *stmt;
3817 unsigned int size = 1;
3819 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3821 const type_t *iface;
3822 const statement_t *stmt_func;
3824 if (stmt->type == STMT_LIBRARY)
3826 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3827 continue;
3829 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3830 continue;
3832 iface = stmt->u.type;
3833 if (!pred(iface))
3834 continue;
3836 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3838 const var_t *func = stmt_func->u.var;
3839 if (!is_local(func->attrs))
3840 size += get_size_procformatstring_func( func );
3843 return size;
3846 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3848 set_all_tfswrite(FALSE);
3849 return process_tfs(NULL, stmts, pred);
3852 void declare_stub_args( FILE *file, int indent, const var_t *func )
3854 int in_attr, out_attr;
3855 int i = 0;
3856 const var_t *var;
3858 /* declare return value '_RetVal' */
3859 if (!is_void(type_function_get_rettype(func->type)))
3861 print_file(file, indent, "%s", "");
3862 write_type_decl_left(file, type_function_get_rettype(func->type));
3863 fprintf(file, " _RetVal;\n");
3866 if (!type_get_function_args(func->type))
3867 return;
3869 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3871 in_attr = is_attr(var->attrs, ATTR_IN);
3872 out_attr = is_attr(var->attrs, ATTR_OUT);
3873 if (!out_attr && !in_attr)
3874 in_attr = 1;
3876 if (is_context_handle(var->type))
3877 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3878 else
3880 if (!in_attr && !is_conformant_array(var->type))
3882 type_t *type_to_print;
3883 char name[16];
3884 print_file(file, indent, "%s", "");
3885 if (type_get_type(var->type) == TYPE_ARRAY &&
3886 !type_array_is_decl_as_ptr(var->type))
3887 type_to_print = var->type;
3888 else
3889 type_to_print = type_pointer_get_ref(var->type);
3890 sprintf(name, "_W%u", i++);
3891 write_type_decl(file, type_to_print, name);
3892 fprintf(file, ";\n");
3895 print_file(file, indent, "%s", "");
3896 write_type_decl_left(file, var->type);
3897 fprintf(file, " ");
3898 if (type_get_type(var->type) == TYPE_ARRAY &&
3899 !type_array_is_decl_as_ptr(var->type)) {
3900 fprintf(file, "(*%s)", var->name);
3901 } else
3902 fprintf(file, "%s", var->name);
3903 write_type_right(file, var->type, FALSE);
3904 fprintf(file, ";\n");
3906 if (decl_indirect(var->type))
3907 print_file(file, indent, "void *_p_%s;\n", var->name);
3913 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3915 int in_attr, out_attr;
3916 int i = 0, sep = 0;
3917 const var_t *var;
3919 if (!type_get_function_args(func->type))
3920 return;
3922 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3924 in_attr = is_attr(var->attrs, ATTR_IN);
3925 out_attr = is_attr(var->attrs, ATTR_OUT);
3926 if (!out_attr && !in_attr)
3927 in_attr = 1;
3929 if (!in_attr)
3931 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3933 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
3935 case TGT_CTXT_HANDLE_POINTER:
3936 fprintf(file, " = NdrContextHandleInitialize(\n");
3937 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3938 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3939 var->type->typestring_offset);
3940 break;
3941 case TGT_ARRAY:
3942 if (type_array_has_conformance(var->type))
3944 unsigned int size, align = 0;
3945 type_t *type = var->type;
3947 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3948 for ( ;
3949 is_array(type) && type_array_has_conformance(type);
3950 type = type_array_get_element(type))
3952 write_expr(file, type_array_get_conformance(type), TRUE,
3953 TRUE, NULL, NULL, local_var_prefix);
3954 fprintf(file, " * ");
3956 size = type_memsize(type, &align);
3957 fprintf(file, "%u);\n", size);
3959 else
3960 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
3961 break;
3962 case TGT_POINTER:
3963 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3964 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3966 case TGT_BASIC:
3967 case TGT_ENUM:
3968 case TGT_POINTER:
3969 case TGT_RANGE:
3970 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3971 break;
3972 case TGT_STRUCT:
3973 case TGT_UNION:
3974 case TGT_USER_TYPE:
3975 case TGT_IFACE_POINTER:
3976 case TGT_ARRAY:
3977 case TGT_CTXT_HANDLE:
3978 case TGT_CTXT_HANDLE_POINTER:
3979 case TGT_INVALID:
3980 case TGT_STRING:
3981 /* not initialised */
3982 break;
3984 i++;
3985 break;
3986 default:
3987 break;
3990 sep = 1;
3993 if (sep)
3994 fprintf(file, "\n");
3998 int write_expr_eval_routines(FILE *file, const char *iface)
4000 static const char *var_name = "pS";
4001 static const char *var_name_expr = "pS->";
4002 int result = 0;
4003 struct expr_eval_routine *eval;
4004 unsigned short callback_offset = 0;
4006 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4008 const char *name = eval->structure->name;
4009 result = 1;
4011 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4012 iface, name, callback_offset);
4013 print_file(file, 0, "{\n");
4014 print_file(file, 1, "%s", "");
4015 write_type_left(file, (type_t *)eval->structure, TRUE);
4016 fprintf(file, " *%s = (", var_name);
4017 write_type_left(file, (type_t *)eval->structure, TRUE);
4018 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4019 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4020 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4021 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
4022 fprintf(file, ";\n");
4023 print_file(file, 0, "}\n\n");
4024 callback_offset++;
4026 return result;
4029 void write_expr_eval_routine_list(FILE *file, const char *iface)
4031 struct expr_eval_routine *eval;
4032 struct expr_eval_routine *cursor;
4033 unsigned short callback_offset = 0;
4035 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4036 fprintf(file, "{\n");
4038 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4040 const char *name = eval->structure->name;
4041 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
4042 callback_offset++;
4043 list_remove(&eval->entry);
4044 free(eval);
4047 fprintf(file, "};\n\n");
4050 void write_user_quad_list(FILE *file)
4052 user_type_t *ut;
4054 if (list_empty(&user_type_list))
4055 return;
4057 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4058 fprintf(file, "{\n");
4059 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4061 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4062 print_file(file, 1, "{\n");
4063 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4064 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4065 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4066 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4067 print_file(file, 1, "}%s\n", sep);
4069 fprintf(file, "};\n\n");
4072 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4074 const struct str_list_entry_t *endpoint;
4075 const char *p;
4077 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4078 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4079 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4081 print_file( f, 1, "{ (const unsigned char *)\"" );
4082 for (p = endpoint->str; *p && *p != ':'; p++)
4084 if (*p == '"' || *p == '\\') fputc( '\\', f );
4085 fputc( *p, f );
4087 if (!*p) goto error;
4088 if (p[1] != '[') goto error;
4090 fprintf( f, "\", (const unsigned char *)\"" );
4091 for (p += 2; *p && *p != ']'; p++)
4093 if (*p == '"' || *p == '\\') fputc( '\\', f );
4094 fputc( *p, f );
4096 if (*p != ']') goto error;
4097 fprintf( f, "\" },\n" );
4099 print_file( f, 0, "};\n\n" );
4100 return;
4102 error:
4103 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4106 void write_exceptions( FILE *file )
4108 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4109 fprintf( file, "\n");
4110 fprintf( file, "#include \"wine/exception.h\"\n");
4111 fprintf( file, "#undef RpcTryExcept\n");
4112 fprintf( file, "#undef RpcExcept\n");
4113 fprintf( file, "#undef RpcEndExcept\n");
4114 fprintf( file, "#undef RpcTryFinally\n");
4115 fprintf( file, "#undef RpcFinally\n");
4116 fprintf( file, "#undef RpcEndFinally\n");
4117 fprintf( file, "#undef RpcExceptionCode\n");
4118 fprintf( file, "#undef RpcAbnormalTermination\n");
4119 fprintf( file, "\n");
4120 fprintf( file, "struct __exception_frame;\n");
4121 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4122 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4123 fprintf( file, "\n");
4124 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4125 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4126 fprintf( file, " __filter_func filter; \\\n");
4127 fprintf( file, " __finally_func finally; \\\n");
4128 fprintf( file, " sigjmp_buf jmp; \\\n");
4129 fprintf( file, " DWORD code; \\\n");
4130 fprintf( file, " unsigned char abnormal_termination; \\\n");
4131 fprintf( file, " unsigned char filter_level; \\\n");
4132 fprintf( file, " unsigned char finally_level;\n");
4133 fprintf( file, "\n");
4134 fprintf( file, "struct __exception_frame\n{\n");
4135 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4136 fprintf( file, "};\n");
4137 fprintf( file, "\n");
4138 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4139 fprintf( file, "{\n");
4140 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4141 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4142 fprintf( file, " {\n");
4143 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4144 fprintf( file, " exc_frame->finally( exc_frame );\n");
4145 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4146 fprintf( file, " }\n");
4147 fprintf( file, " exc_frame->filter_level = 0;\n");
4148 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4149 fprintf( file, "}\n");
4150 fprintf( file, "\n");
4151 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4152 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4153 fprintf( file, " CONTEXT *context,\n");
4154 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4155 fprintf( file, "{\n");
4156 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4157 fprintf( file, "\n");
4158 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4159 fprintf( file, " {\n" );
4160 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4161 fprintf( file, " {\n" );
4162 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4163 fprintf( file, " exc_frame->finally( exc_frame );\n");
4164 fprintf( file, " }\n" );
4165 fprintf( file, " return ExceptionContinueSearch;\n");
4166 fprintf( file, " }\n" );
4167 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4168 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4169 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4170 fprintf( file, " return ExceptionContinueSearch;\n");
4171 fprintf( file, "}\n");
4172 fprintf( file, "\n");
4173 fprintf( file, "#define RpcTryExcept \\\n");
4174 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4175 fprintf( file, " { \\\n");
4176 fprintf( file, " if (!__frame->finally_level) \\\n" );
4177 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4178 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4179 fprintf( file, "\n");
4180 fprintf( file, "#define RpcExcept(expr) \\\n");
4181 fprintf( file, " if (!__frame->finally_level) \\\n" );
4182 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4183 fprintf( file, " __frame->filter_level = 0; \\\n" );
4184 fprintf( file, " } \\\n");
4185 fprintf( file, " else \\\n");
4186 fprintf( file, "\n");
4187 fprintf( file, "#define RpcEndExcept\n");
4188 fprintf( file, "\n");
4189 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4190 fprintf( file, "\n");
4191 fprintf( file, "#define RpcTryFinally \\\n");
4192 fprintf( file, " if (!__frame->filter_level) \\\n");
4193 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4194 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4195 fprintf( file, "\n");
4196 fprintf( file, "#define RpcFinally \\\n");
4197 fprintf( file, " if (!__frame->filter_level) \\\n");
4198 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4199 fprintf( file, " __frame->finally_level = 0;\n");
4200 fprintf( file, "\n");
4201 fprintf( file, "#define RpcEndFinally\n");
4202 fprintf( file, "\n");
4203 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4204 fprintf( file, "\n");
4205 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4206 fprintf( file, " do { \\\n");
4207 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4208 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4209 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4210 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4211 fprintf( file, " __frame->filter_level = 0; \\\n");
4212 fprintf( file, " __frame->finally_level = 0; \\\n");
4213 fprintf( file, " } while (0)\n");
4214 fprintf( file, "\n");
4215 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4216 fprintf( file, "\n");
4217 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4218 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4219 fprintf( file, "\n");
4220 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4221 fprintf( file, " DWORD code;\n");
4222 fprintf( file, "\n");
4223 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");