widl: Make the function return value a variable.
[wine/multimedia.git] / tools / widl / typegen.c
blob1582599ae6abd2331d158d249dfb1694a47b3354
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 var_t *current_func;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *iface;
58 const type_t *cont_type;
59 char *name;
60 unsigned int baseoff;
61 const expr_t *expr;
64 enum type_context
66 TYPE_CONTEXT_TOPLEVELPARAM,
67 TYPE_CONTEXT_PARAM,
68 TYPE_CONTEXT_CONTAINER,
69 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
72 /* parameter flags in Oif mode */
73 static const unsigned short MustSize = 0x0001;
74 static const unsigned short MustFree = 0x0002;
75 static const unsigned short IsPipe = 0x0004;
76 static const unsigned short IsIn = 0x0008;
77 static const unsigned short IsOut = 0x0010;
78 static const unsigned short IsReturn = 0x0020;
79 static const unsigned short IsBasetype = 0x0040;
80 static const unsigned short IsByValue = 0x0080;
81 static const unsigned short IsSimpleRef = 0x0100;
82 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
83 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
85 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
86 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
87 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align);
88 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
89 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
90 const char *name, int write_ptr, unsigned int *tfsoff);
91 static const var_t *find_array_or_string_in_struct(const type_t *type);
92 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
93 type_t *type, enum type_context context,
94 const char *name, unsigned int *typestring_offset);
95 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
96 const attr_list_t *attrs, int toplevel_param,
97 unsigned int *alignment );
98 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
100 static const char *string_of_type(unsigned char type)
102 switch (type)
104 case RPC_FC_BYTE: return "FC_BYTE";
105 case RPC_FC_CHAR: return "FC_CHAR";
106 case RPC_FC_SMALL: return "FC_SMALL";
107 case RPC_FC_USMALL: return "FC_USMALL";
108 case RPC_FC_WCHAR: return "FC_WCHAR";
109 case RPC_FC_SHORT: return "FC_SHORT";
110 case RPC_FC_USHORT: return "FC_USHORT";
111 case RPC_FC_LONG: return "FC_LONG";
112 case RPC_FC_ULONG: return "FC_ULONG";
113 case RPC_FC_FLOAT: return "FC_FLOAT";
114 case RPC_FC_HYPER: return "FC_HYPER";
115 case RPC_FC_DOUBLE: return "FC_DOUBLE";
116 case RPC_FC_ENUM16: return "FC_ENUM16";
117 case RPC_FC_ENUM32: return "FC_ENUM32";
118 case RPC_FC_IGNORE: return "FC_IGNORE";
119 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
120 case RPC_FC_RP: return "FC_RP";
121 case RPC_FC_UP: return "FC_UP";
122 case RPC_FC_OP: return "FC_OP";
123 case RPC_FC_FP: return "FC_FP";
124 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
125 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
126 case RPC_FC_STRUCT: return "FC_STRUCT";
127 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
128 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
129 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
130 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
131 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
132 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
133 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
134 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
135 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
136 case RPC_FC_CARRAY: return "FC_CARRAY";
137 case RPC_FC_CVARRAY: return "FC_CVARRAY";
138 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
139 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
140 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
141 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
142 case RPC_FC_POINTER: return "FC_POINTER";
143 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
144 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
145 case RPC_FC_CSTRING: return "FC_CSTRING";
146 case RPC_FC_WSTRING: return "FC_WSTRING";
147 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
148 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
149 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
150 case RPC_FC_IP: return "FC_IP";
151 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
152 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
153 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
154 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
155 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
156 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
157 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
158 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
159 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
160 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
161 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
162 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
163 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
164 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
165 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
166 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
167 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
168 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
169 case RPC_FC_PP: return "FC_PP";
170 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
171 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
172 case RPC_FC_DIV_2: return "FC_DIV_2";
173 case RPC_FC_MULT_2: return "FC_MULT_2";
174 case RPC_FC_ADD_1: return "FC_ADD_1";
175 case RPC_FC_SUB_1: return "FC_SUB_1";
176 case RPC_FC_CALLBACK: return "FC_CALLBACK";
177 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
178 case RPC_FC_END: return "FC_END";
179 case RPC_FC_PAD: return "FC_PAD";
180 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
181 case RPC_FC_RANGE: return "FC_RANGE";
182 case RPC_FC_INT3264: return "FC_INT3264";
183 case RPC_FC_UINT3264: return "FC_UINT3264";
184 default:
185 error("string_of_type: unknown type 0x%02x\n", type);
186 return NULL;
190 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
192 const type_t *t = type;
193 for (;;)
195 if (is_attr(t->attrs, attr))
196 return get_attrp(t->attrs, attr);
197 else if (type_is_alias(t))
198 t = type_alias_get_aliasee(t);
199 else return NULL;
203 unsigned char get_basic_fc(const type_t *type)
205 int sign = type_basic_get_sign(type);
206 switch (type_basic_get_type(type))
208 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
209 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
210 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
211 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
212 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
213 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
214 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
215 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
216 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
217 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
218 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
219 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
220 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
221 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
223 return 0;
226 static unsigned char get_basic_fc_signed(const type_t *type)
228 switch (type_basic_get_type(type))
230 case TYPE_BASIC_INT8: return RPC_FC_SMALL;
231 case TYPE_BASIC_INT16: return RPC_FC_SHORT;
232 case TYPE_BASIC_INT32: return RPC_FC_LONG;
233 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
234 case TYPE_BASIC_INT: return RPC_FC_LONG;
235 case TYPE_BASIC_INT3264: return RPC_FC_INT3264;
236 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
237 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
238 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
239 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
240 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
241 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
242 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
243 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
245 return 0;
248 static inline unsigned int clamp_align(unsigned int align)
250 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
251 if(align > packing) align = packing;
252 return align;
255 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
257 const type_t *t;
258 int pointer_type;
260 assert(is_ptr(type) || is_array(type));
262 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
263 if (pointer_type)
264 return pointer_type;
266 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
268 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
269 if (pointer_type)
270 return pointer_type;
273 if (toplevel_param)
274 return RPC_FC_RP;
275 else if (is_ptr(type))
276 return type_pointer_get_default_fc(type);
277 else
278 return type_array_get_ptr_default_fc(type);
281 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
282 enum type_context context )
284 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
286 if (pointer_fc == RPC_FC_UP && is_attr( attrs, ATTR_OUT ) &&
287 context == TYPE_CONTEXT_PARAM && is_object( current_iface ))
288 pointer_fc = RPC_FC_OP;
290 return pointer_fc;
293 static unsigned char get_enum_fc(const type_t *type)
295 assert(type_get_type(type) == TYPE_ENUM);
296 if (is_aliaschain_attr(type, ATTR_V1ENUM))
297 return RPC_FC_ENUM32;
298 else
299 return RPC_FC_ENUM16;
302 static type_t *get_user_type(const type_t *t, const char **pname)
304 for (;;)
306 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
307 if (ut)
309 if (pname)
310 *pname = t->name;
311 return ut;
314 if (type_is_alias(t))
315 t = type_alias_get_aliasee(t);
316 else
317 return NULL;
321 static int is_user_type(const type_t *t)
323 return get_user_type(t, NULL) != NULL;
326 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
328 if (is_user_type(type))
329 return TGT_USER_TYPE;
331 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
332 return TGT_CTXT_HANDLE;
334 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
335 return TGT_STRING;
337 switch (type_get_type(type))
339 case TYPE_BASIC:
340 if (!(flags & TDT_IGNORE_RANGES) &&
341 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
342 return TGT_RANGE;
343 return TGT_BASIC;
344 case TYPE_ENUM:
345 if (!(flags & TDT_IGNORE_RANGES) &&
346 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
347 return TGT_RANGE;
348 return TGT_ENUM;
349 case TYPE_POINTER:
350 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
351 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
352 return TGT_IFACE_POINTER;
353 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
354 return TGT_CTXT_HANDLE_POINTER;
355 else
356 return TGT_POINTER;
357 case TYPE_STRUCT:
358 return TGT_STRUCT;
359 case TYPE_ENCAPSULATED_UNION:
360 case TYPE_UNION:
361 return TGT_UNION;
362 case TYPE_ARRAY:
363 return TGT_ARRAY;
364 case TYPE_FUNCTION:
365 case TYPE_COCLASS:
366 case TYPE_INTERFACE:
367 case TYPE_MODULE:
368 case TYPE_VOID:
369 case TYPE_ALIAS:
370 case TYPE_BITFIELD:
371 break;
373 return TGT_INVALID;
376 static int cant_be_null(const var_t *v)
378 switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
380 case TGT_ARRAY:
381 if (!type_array_is_decl_as_ptr( v->type )) return 0;
382 /* fall through */
383 case TGT_POINTER:
384 return (get_pointer_fc(v->type, v->attrs, TRUE) == RPC_FC_RP);
385 case TGT_CTXT_HANDLE_POINTER:
386 return TRUE;
387 default:
388 return 0;
393 static int get_padding(const var_list_t *fields)
395 unsigned short offset = 0;
396 unsigned int salign = 1;
397 const var_t *f;
399 if (!fields)
400 return 0;
402 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
404 type_t *ft = f->type;
405 unsigned int align = 0;
406 unsigned int size = type_memsize_and_alignment(ft, &align);
407 align = clamp_align(align);
408 if (align > salign) salign = align;
409 offset = ROUND_SIZE(offset, align);
410 offset += size;
413 return ROUNDING(offset, salign);
416 static unsigned int get_stack_size( const type_t *type, const attr_list_t *attrs, int *by_value )
418 unsigned int stack_size;
419 int by_val;
421 switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
423 case TGT_BASIC:
424 case TGT_ENUM:
425 case TGT_RANGE:
426 case TGT_STRUCT:
427 case TGT_UNION:
428 case TGT_USER_TYPE:
429 stack_size = type_memsize( type );
430 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
431 break;
432 default:
433 by_val = 0;
434 break;
436 if (!by_val) stack_size = pointer_size;
437 if (by_value) *by_value = by_val;
438 return ROUND_SIZE( stack_size, pointer_size );
441 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
442 const type_t *type )
444 unsigned char flags = 0;
446 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
448 if (is_ptr(type) &&
449 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
450 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
451 flags |= 0x80;
453 if (is_attr(attrs, ATTR_IN))
455 flags |= 0x40;
456 if (!is_attr(attrs, ATTR_OUT)) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
458 if (is_attr(attrs, ATTR_OUT)) flags |= 0x20;
460 return flags;
463 static unsigned int get_rpc_flags( const attr_list_t *attrs )
465 unsigned int flags = 0;
467 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
468 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
469 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
470 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
471 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
472 return flags;
475 unsigned char get_struct_fc(const type_t *type)
477 int has_pointer = 0;
478 int has_conformance = 0;
479 int has_variance = 0;
480 var_t *field;
481 var_list_t *fields;
483 fields = type_struct_get_fields(type);
485 if (get_padding(fields))
486 return RPC_FC_BOGUS_STRUCT;
488 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
490 type_t *t = field->type;
491 enum typegen_type typegen_type;
493 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
495 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
497 if (is_string_type(field->attrs, field->type))
499 if (is_conformant_array(t))
500 has_conformance = 1;
501 has_variance = 1;
502 continue;
505 if (is_array(type_array_get_element(field->type)))
506 return RPC_FC_BOGUS_STRUCT;
508 if (type_array_has_conformance(field->type))
510 has_conformance = 1;
511 if (list_next(fields, &field->entry))
512 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
513 field->name);
515 if (type_array_has_variance(t))
516 has_variance = 1;
518 t = type_array_get_element(t);
519 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
522 switch (typegen_type)
524 case TGT_USER_TYPE:
525 case TGT_IFACE_POINTER:
526 return RPC_FC_BOGUS_STRUCT;
527 case TGT_BASIC:
528 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
529 return RPC_FC_BOGUS_STRUCT;
530 break;
531 case TGT_ENUM:
532 if (get_enum_fc(t) == RPC_FC_ENUM16)
533 return RPC_FC_BOGUS_STRUCT;
534 break;
535 case TGT_POINTER:
536 case TGT_ARRAY:
537 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
538 return RPC_FC_BOGUS_STRUCT;
539 has_pointer = 1;
540 break;
541 case TGT_UNION:
542 return RPC_FC_BOGUS_STRUCT;
543 case TGT_STRUCT:
545 unsigned char fc = get_struct_fc(t);
546 switch (fc)
548 case RPC_FC_STRUCT:
549 break;
550 case RPC_FC_CVSTRUCT:
551 has_conformance = 1;
552 has_variance = 1;
553 has_pointer = 1;
554 break;
556 case RPC_FC_CPSTRUCT:
557 has_conformance = 1;
558 if (list_next( fields, &field->entry ))
559 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
560 field->name);
561 has_pointer = 1;
562 break;
564 case RPC_FC_CSTRUCT:
565 has_conformance = 1;
566 if (list_next( fields, &field->entry ))
567 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
568 field->name);
569 break;
571 case RPC_FC_PSTRUCT:
572 has_pointer = 1;
573 break;
575 default:
576 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
577 /* fallthru - treat it as complex */
579 /* as soon as we see one of these these members, it's bogus... */
580 case RPC_FC_BOGUS_STRUCT:
581 return RPC_FC_BOGUS_STRUCT;
583 break;
585 case TGT_RANGE:
586 return RPC_FC_BOGUS_STRUCT;
587 case TGT_STRING:
588 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
589 case TGT_INVALID:
590 case TGT_CTXT_HANDLE:
591 case TGT_CTXT_HANDLE_POINTER:
592 /* checking after parsing should mean that we don't get here. if we do,
593 * it's a checker bug */
594 assert(0);
598 if( has_variance )
600 if ( has_conformance )
601 return RPC_FC_CVSTRUCT;
602 else
603 return RPC_FC_BOGUS_STRUCT;
605 if( has_conformance && has_pointer )
606 return RPC_FC_CPSTRUCT;
607 if( has_conformance )
608 return RPC_FC_CSTRUCT;
609 if( has_pointer )
610 return RPC_FC_PSTRUCT;
611 return RPC_FC_STRUCT;
614 static unsigned char get_array_fc(const type_t *type)
616 unsigned char fc;
617 const expr_t *size_is;
618 const type_t *elem_type;
620 elem_type = type_array_get_element(type);
621 size_is = type_array_get_conformance(type);
623 if (!size_is)
625 unsigned int size = type_memsize(elem_type);
626 if (size * type_array_get_dim(type) > 0xffffuL)
627 fc = RPC_FC_LGFARRAY;
628 else
629 fc = RPC_FC_SMFARRAY;
631 else
632 fc = RPC_FC_CARRAY;
634 if (type_array_has_variance(type))
636 if (fc == RPC_FC_SMFARRAY)
637 fc = RPC_FC_SMVARRAY;
638 else if (fc == RPC_FC_LGFARRAY)
639 fc = RPC_FC_LGVARRAY;
640 else if (fc == RPC_FC_CARRAY)
641 fc = RPC_FC_CVARRAY;
644 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
646 case TGT_USER_TYPE:
647 fc = RPC_FC_BOGUS_ARRAY;
648 break;
649 case TGT_BASIC:
650 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
651 pointer_size != 4)
652 fc = RPC_FC_BOGUS_ARRAY;
653 break;
654 case TGT_STRUCT:
655 switch (get_struct_fc(elem_type))
657 case RPC_FC_BOGUS_STRUCT:
658 fc = RPC_FC_BOGUS_ARRAY;
659 break;
661 break;
662 case TGT_ENUM:
663 /* is 16-bit enum - if so, wire size differs from mem size and so
664 * the array cannot be block copied, which means the array is complex */
665 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
666 fc = RPC_FC_BOGUS_ARRAY;
667 break;
668 case TGT_UNION:
669 case TGT_IFACE_POINTER:
670 fc = RPC_FC_BOGUS_ARRAY;
671 break;
672 case TGT_POINTER:
673 /* ref pointers cannot just be block copied. unique pointers to
674 * interfaces need special treatment. either case means the array is
675 * complex */
676 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
677 fc = RPC_FC_BOGUS_ARRAY;
678 break;
679 case TGT_RANGE:
680 fc = RPC_FC_BOGUS_ARRAY;
681 break;
682 case TGT_CTXT_HANDLE:
683 case TGT_CTXT_HANDLE_POINTER:
684 case TGT_STRING:
685 case TGT_INVALID:
686 case TGT_ARRAY:
687 /* nothing to do for everything else */
688 break;
691 return fc;
694 static int is_non_complex_struct(const type_t *type)
696 return (type_get_type(type) == TYPE_STRUCT &&
697 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
700 static int type_has_pointers(const type_t *type)
702 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
704 case TGT_USER_TYPE:
705 return FALSE;
706 case TGT_POINTER:
707 return TRUE;
708 case TGT_ARRAY:
709 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
710 case TGT_STRUCT:
712 var_list_t *fields = type_struct_get_fields(type);
713 const var_t *field;
714 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
716 if (type_has_pointers(field->type))
717 return TRUE;
719 break;
721 case TGT_UNION:
723 var_list_t *fields;
724 const var_t *field;
725 fields = type_union_get_cases(type);
726 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
728 if (field->type && type_has_pointers(field->type))
729 return TRUE;
731 break;
733 case TGT_CTXT_HANDLE:
734 case TGT_CTXT_HANDLE_POINTER:
735 case TGT_STRING:
736 case TGT_IFACE_POINTER:
737 case TGT_BASIC:
738 case TGT_ENUM:
739 case TGT_RANGE:
740 case TGT_INVALID:
741 break;
744 return FALSE;
747 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
748 int toplevel_param)
750 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
752 case TGT_USER_TYPE:
753 return FALSE;
754 case TGT_POINTER:
755 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
756 return TRUE;
757 else
758 return FALSE;
759 case TGT_ARRAY:
760 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
761 return TRUE;
762 else
763 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
764 case TGT_STRUCT:
766 var_list_t *fields = type_struct_get_fields(type);
767 const var_t *field;
768 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
770 if (type_has_full_pointer(field->type, field->attrs, FALSE))
771 return TRUE;
773 break;
775 case TGT_UNION:
777 var_list_t *fields;
778 const var_t *field;
779 fields = type_union_get_cases(type);
780 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
782 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
783 return TRUE;
785 break;
787 case TGT_CTXT_HANDLE:
788 case TGT_CTXT_HANDLE_POINTER:
789 case TGT_STRING:
790 case TGT_IFACE_POINTER:
791 case TGT_BASIC:
792 case TGT_ENUM:
793 case TGT_RANGE:
794 case TGT_INVALID:
795 break;
798 return FALSE;
801 static unsigned short user_type_offset(const char *name)
803 user_type_t *ut;
804 unsigned short off = 0;
805 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
807 if (strcmp(name, ut->name) == 0)
808 return off;
809 ++off;
811 error("user_type_offset: couldn't find type (%s)\n", name);
812 return 0;
815 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
817 type->typestring_offset = offset;
818 if (file) type->tfswrite = FALSE;
821 static void guard_rec(type_t *type)
823 /* types that contain references to themselves (like a linked list),
824 need to be shielded from infinite recursion when writing embedded
825 types */
826 if (type->typestring_offset)
827 type->tfswrite = FALSE;
828 else
829 type->typestring_offset = 1;
832 static int is_embedded_complex(const type_t *type)
834 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
836 case TGT_USER_TYPE:
837 case TGT_STRUCT:
838 case TGT_UNION:
839 case TGT_ARRAY:
840 case TGT_IFACE_POINTER:
841 return TRUE;
842 default:
843 return FALSE;
847 static const char *get_context_handle_type_name(const type_t *type)
849 const type_t *t;
850 for (t = type;
851 is_ptr(t) || type_is_alias(t);
852 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
853 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
854 return t->name;
855 assert(0);
856 return NULL;
859 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
860 do { \
861 if (file) \
862 fprintf(file, "/* %2u */\n", typestring_offset); \
863 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
865 while (0)
867 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
868 static void print_file(FILE *file, int indent, const char *format, ...)
870 va_list va;
871 va_start(va, format);
872 print(file, indent, format, va);
873 va_end(va);
876 void print(FILE *file, int indent, const char *format, va_list va)
878 if (file)
880 if (format[0] != '\n')
881 while (0 < indent--)
882 fprintf(file, " ");
883 vfprintf(file, format, va);
888 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
890 if (decl_indirect(t))
892 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
893 local_var_prefix, n, local_var_prefix, n);
894 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
896 else if (is_ptr(t) || is_array(t))
897 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
900 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
902 const var_t *var = type_function_get_retval(func->type);
904 if (!is_void(var->type))
905 write_var_init(file, indent, var->type, var->name, local_var_prefix);
907 if (!type_get_function_args(func->type))
908 return;
910 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
911 write_var_init(file, indent, var->type, var->name, local_var_prefix);
913 fprintf(file, "\n");
916 static void write_formatdesc(FILE *f, int indent, const char *str)
918 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
919 print_file(f, indent, "{\n");
920 print_file(f, indent + 1, "short Pad;\n");
921 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
922 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
923 print_file(f, indent, "\n");
926 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
928 clear_all_offsets();
930 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
931 get_size_typeformatstring(stmts, pred));
933 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
934 get_size_procformatstring(stmts, pred));
936 fprintf(f, "\n");
937 write_formatdesc(f, indent, "TYPE");
938 write_formatdesc(f, indent, "PROC");
939 fprintf(f, "\n");
940 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
941 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
942 print_file(f, indent, "\n");
945 int decl_indirect(const type_t *t)
947 if (is_user_type(t))
948 return TRUE;
949 return (type_get_type(t) != TYPE_BASIC &&
950 type_get_type(t) != TYPE_ENUM &&
951 type_get_type(t) != TYPE_POINTER &&
952 type_get_type(t) != TYPE_ARRAY);
955 static unsigned char get_parameter_fc( const type_t *type, const attr_list_t *attrs, int is_return,
956 unsigned short *flags, unsigned int *stack_size,
957 unsigned int *typestring_offset )
959 unsigned int alignment, server_size = 0, buffer_size = 0;
960 unsigned char fc = 0;
961 int is_byval;
962 int is_in = is_attr(attrs, ATTR_IN);
963 int is_out = is_attr(attrs, ATTR_OUT);
965 if (is_return) is_out = TRUE;
966 else if (!is_in && !is_out) is_in = TRUE;
968 *flags = 0;
969 *stack_size = get_stack_size( type, attrs, &is_byval );
970 *typestring_offset = type->typestring_offset;
972 if (is_in) *flags |= IsIn;
973 if (is_out) *flags |= IsOut;
974 if (is_return) *flags |= IsReturn;
976 if (!is_string_type( attrs, type ))
977 buffer_size = get_required_buffer_size_type( type, NULL, attrs, TRUE, &alignment );
979 switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
981 case TGT_BASIC:
982 *flags |= IsBasetype;
983 fc = get_basic_fc_signed( type );
984 if (fc == RPC_FC_BIND_PRIMITIVE)
986 buffer_size = 4; /* actually 0 but avoids setting MustSize */
987 fc = RPC_FC_LONG;
989 break;
990 case TGT_ENUM:
991 *flags |= IsBasetype;
992 fc = get_enum_fc( type );
993 break;
994 case TGT_RANGE:
995 *flags |= IsByValue;
996 break;
997 case TGT_STRUCT:
998 case TGT_UNION:
999 case TGT_USER_TYPE:
1000 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1001 break;
1002 case TGT_IFACE_POINTER:
1003 *flags |= MustFree;
1004 break;
1005 case TGT_ARRAY:
1006 *flags |= MustFree;
1007 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff &&
1008 get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
1009 *flags |= IsSimpleRef;
1010 break;
1011 case TGT_STRING:
1012 *flags |= MustFree;
1013 if (is_declptr( type ) && get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
1015 /* skip over pointer description straight to string description */
1016 if (is_conformant_array( type )) *typestring_offset += 4;
1017 else *typestring_offset += 2;
1018 *flags |= IsSimpleRef;
1020 break;
1021 case TGT_CTXT_HANDLE_POINTER:
1022 *flags |= IsSimpleRef;
1023 *typestring_offset += 4;
1024 /* fall through */
1025 case TGT_CTXT_HANDLE:
1026 buffer_size = 20;
1027 break;
1028 case TGT_POINTER:
1029 if (get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
1031 const type_t *ref = type_pointer_get_ref( type );
1033 if (!is_string_type( attrs, ref ))
1034 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1036 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1038 case TGT_BASIC:
1039 *flags |= IsSimpleRef | IsBasetype;
1040 fc = get_basic_fc( ref );
1041 if (!is_in && is_out) server_size = pointer_size;
1042 break;
1043 case TGT_ENUM:
1044 if ((fc = get_enum_fc( ref )) == RPC_FC_ENUM32)
1046 *flags |= IsSimpleRef | IsBasetype;
1047 if (!is_in && is_out) server_size = pointer_size;
1049 else
1051 server_size = pointer_size;
1053 break;
1054 case TGT_UNION:
1055 case TGT_USER_TYPE:
1056 case TGT_RANGE:
1057 *flags |= IsSimpleRef | MustFree;
1058 *typestring_offset = ref->typestring_offset;
1059 if (!is_in && is_out) server_size = type_memsize( ref );
1060 break;
1061 case TGT_STRING:
1062 case TGT_POINTER:
1063 case TGT_ARRAY:
1064 case TGT_CTXT_HANDLE:
1065 case TGT_CTXT_HANDLE_POINTER:
1066 *flags |= MustFree;
1067 server_size = pointer_size;
1068 break;
1069 case TGT_IFACE_POINTER:
1070 *flags |= MustFree;
1071 if (is_in && is_out) server_size = pointer_size;
1072 break;
1073 case TGT_STRUCT:
1074 *flags |= IsSimpleRef | MustFree;
1075 *typestring_offset = ref->typestring_offset;
1076 switch (get_struct_fc(ref))
1078 case RPC_FC_STRUCT:
1079 case RPC_FC_PSTRUCT:
1080 case RPC_FC_BOGUS_STRUCT:
1081 if (!is_in && is_out) server_size = type_memsize( ref );
1082 break;
1083 default:
1084 break;
1086 break;
1087 case TGT_INVALID:
1088 assert(0);
1091 else /* not ref pointer */
1093 *flags |= MustFree;
1095 break;
1096 case TGT_INVALID:
1097 assert(0);
1100 if (!buffer_size) *flags |= MustSize;
1102 if (server_size)
1104 server_size = (server_size + 7) / 8;
1105 if (server_size < 8) *flags |= server_size << 13;
1107 return fc;
1110 static unsigned char get_func_oi2_flags( const var_t *func )
1112 const var_t *var;
1113 var_list_t *args = type_get_function_args( func->type );
1114 type_t *ret_type = type_function_get_rettype( func->type );
1115 unsigned char oi2_flags = 0x40; /* HasExtensions */
1116 unsigned short flags;
1117 unsigned int stack_size, typestring_offset;
1119 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1121 get_parameter_fc( var->type, var->attrs, 0, &flags, &stack_size, &typestring_offset );
1122 if (flags & MustSize)
1124 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1125 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1129 if (!is_void( ret_type ))
1131 oi2_flags |= 0x04; /* HasRet */
1132 get_parameter_fc( ret_type, NULL, 1, &flags, &stack_size, &typestring_offset );
1133 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1135 return oi2_flags;
1138 static unsigned int write_new_procformatstring_type(FILE *file, int indent,
1139 const type_t *type,
1140 const attr_list_t *attrs,
1141 int is_return, unsigned int *stack_offset)
1143 char buffer[64];
1144 unsigned int stack_size, typestring_offset;
1145 unsigned short flags;
1146 unsigned char fc = get_parameter_fc( type, attrs, is_return, &flags, &stack_size, &typestring_offset );
1148 strcpy( buffer, "/* flags:" );
1149 if (flags & MustSize) strcat( buffer, " must size," );
1150 if (flags & MustFree) strcat( buffer, " must free," );
1151 if (flags & IsPipe) strcat( buffer, " pipe," );
1152 if (flags & IsIn) strcat( buffer, " in," );
1153 if (flags & IsOut) strcat( buffer, " out," );
1154 if (flags & IsReturn) strcat( buffer, " return," );
1155 if (flags & IsBasetype) strcat( buffer, " base type," );
1156 if (flags & IsByValue) strcat( buffer, " by value," );
1157 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1158 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1159 strcpy( buffer + strlen( buffer ) - 1, " */" );
1160 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1161 print_file( file, indent, "NdrFcShort(0x%hx), /* stack offset = %hu */\n",
1162 *stack_offset, *stack_offset );
1163 if (flags & IsBasetype)
1165 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1166 print_file( file, indent, "0x0,\n" );
1168 else
1169 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1170 typestring_offset, typestring_offset );
1171 *stack_offset += max( stack_size, pointer_size );
1172 return 6;
1175 static unsigned int write_old_procformatstring_type(FILE *file, int indent,
1176 const type_t *type,
1177 const attr_list_t *attrs,
1178 int is_return, int is_interpreted)
1180 unsigned int size;
1182 int is_in = is_attr(attrs, ATTR_IN);
1183 int is_out = is_attr(attrs, ATTR_OUT);
1185 if (!is_in && !is_out) is_in = TRUE;
1187 if (type_get_type(type) == TYPE_BASIC ||
1188 type_get_type(type) == TYPE_ENUM)
1190 unsigned char fc;
1192 if (is_return)
1193 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1194 else
1195 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1197 if (type_get_type(type) == TYPE_ENUM)
1199 fc = get_enum_fc(type);
1201 else
1203 fc = get_basic_fc_signed(type);
1205 if (fc == RPC_FC_BIND_PRIMITIVE)
1206 fc = RPC_FC_IGNORE;
1209 print_file(file, indent, "0x%02x, /* %s */\n",
1210 fc, string_of_type(fc));
1211 size = 2; /* includes param type prefix */
1213 else
1215 unsigned short offset = type->typestring_offset;
1217 if (is_interpreted && is_array(type) &&
1218 type_array_is_decl_as_ptr(type) &&
1219 type->details.array.ptr_tfsoff)
1220 offset = type->details.array.ptr_tfsoff;
1222 if (is_return)
1223 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1224 else if (is_in && is_out)
1225 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1226 else if (is_out)
1227 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1228 else
1229 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1231 size = get_stack_size( type, attrs, NULL );
1232 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1233 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1234 size = 4; /* includes param type prefix */
1236 return size;
1239 int is_interpreted_func( const type_t *iface, const var_t *func )
1241 const char *str;
1242 const var_t *var;
1243 const var_list_t *args = type_get_function_args( func->type );
1244 const type_t *ret_type = type_function_get_rettype( func->type );
1246 if (type_get_type( ret_type ) == TYPE_BASIC)
1248 switch (type_basic_get_type( ret_type ))
1250 case TYPE_BASIC_INT64:
1251 case TYPE_BASIC_HYPER:
1252 /* return value must fit in a long_ptr */
1253 if (pointer_size < 8) return 0;
1254 break;
1255 case TYPE_BASIC_FLOAT:
1256 case TYPE_BASIC_DOUBLE:
1257 /* floating point values can't be returned */
1258 return 0;
1259 default:
1260 break;
1263 if (get_stub_mode() != MODE_Oif && args)
1265 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1266 switch (type_get_type( var->type ))
1268 case TYPE_BASIC:
1269 switch (type_basic_get_type( var->type ))
1271 /* floating point arguments are not supported in Oi mode */
1272 case TYPE_BASIC_FLOAT: return 0;
1273 case TYPE_BASIC_DOUBLE: return 0;
1274 default: break;
1276 break;
1277 /* unions passed by value are not supported in Oi mode */
1278 case TYPE_UNION: return 0;
1279 case TYPE_ENCAPSULATED_UNION: return 0;
1280 default: break;
1284 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1285 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1286 return (get_stub_mode() != MODE_Os);
1289 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1290 const var_t *func, unsigned int *offset,
1291 unsigned short num_proc )
1293 var_t *var;
1294 var_list_t *args = type_get_function_args( func->type );
1295 unsigned char explicit_fc, implicit_fc;
1296 unsigned char handle_flags;
1297 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1298 unsigned char oi_flags = RPC_FC_PROC_OIF_RPCFLAGS | RPC_FC_PROC_OIF_NEWINIT;
1299 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1300 unsigned int nb_args = 0;
1301 unsigned int stack_size = 0;
1302 unsigned short param_num = 0;
1303 unsigned short handle_stack_offset = 0;
1304 unsigned short handle_param_num = 0;
1306 if (is_full_pointer_function( func )) oi_flags |= RPC_FC_PROC_OIF_FULLPTR;
1307 if (is_object( iface ))
1309 oi_flags |= RPC_FC_PROC_OIF_OBJECT;
1310 if (get_stub_mode() == MODE_Oif) oi_flags |= RPC_FC_PROC_OIF_OBJ_V2;
1311 stack_size += pointer_size;
1314 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1316 if (var == handle_var)
1318 handle_stack_offset = stack_size;
1319 handle_param_num = param_num;
1321 stack_size += get_stack_size( var->type, var->attrs, NULL );
1322 param_num++;
1323 nb_args++;
1325 if (!is_void( type_function_get_rettype( func->type )))
1327 stack_size += pointer_size;
1328 nb_args++;
1331 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1332 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1333 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1334 print_file( file, indent, "0x%02x,\n", oi_flags );
1335 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1336 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1337 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack size = %hu */\n", stack_size, stack_size );
1338 *offset += 10;
1340 if (!implicit_fc)
1342 switch (explicit_fc)
1344 case RPC_FC_BIND_PRIMITIVE:
1345 handle_flags = 0;
1346 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1347 print_file( file, indent, "0x%02x,\n", handle_flags );
1348 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1349 handle_stack_offset, handle_stack_offset );
1350 *offset += 4;
1351 break;
1352 case RPC_FC_BIND_GENERIC:
1353 handle_flags = type_memsize( handle_var->type );
1354 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1355 print_file( file, indent, "0x%02x,\n", handle_flags );
1356 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1357 handle_stack_offset, handle_stack_offset );
1358 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1359 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD);
1360 *offset += 6;
1361 break;
1362 case RPC_FC_BIND_CONTEXT:
1363 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1364 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1365 print_file( file, indent, "0x%02x,\n", handle_flags );
1366 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1367 handle_stack_offset, handle_stack_offset );
1368 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1369 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1370 *offset += 6;
1371 break;
1375 if (get_stub_mode() == MODE_Oif)
1377 unsigned char oi2_flags = get_func_oi2_flags( func );
1378 unsigned char ext_flags = 0;
1379 unsigned int size;
1381 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1382 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1384 size = get_function_buffer_size( func, PASS_IN );
1385 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %hu */\n", size, size );
1386 size = get_function_buffer_size( func, PASS_OUT );
1387 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %hu */\n", size, size );
1388 print_file( file, indent, "0x%02x,\n", oi2_flags );
1389 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1390 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1391 print_file( file, indent, "0x%02x,\n", ext_flags );
1392 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1393 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1394 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1395 *offset += 14;
1396 if (pointer_size == 8)
1398 unsigned short pos = 0, fpu_mask = 0;
1400 if (is_object( iface )) pos += 2;
1401 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1403 if (type_get_type( var->type ) == TYPE_BASIC)
1405 switch (type_basic_get_type( var->type ))
1407 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1408 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1409 default: break;
1412 pos += 2;
1413 if (pos >= 16) break;
1415 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1416 *offset += 2;
1421 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1422 const var_t *func, unsigned int *offset,
1423 unsigned short num_proc )
1425 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1426 int is_interpreted = is_interpreted_func( iface, func );
1427 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1429 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1431 /* emit argument data */
1432 if (type_get_function_args(func->type))
1434 const var_t *var;
1435 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1437 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1438 if (is_new_style)
1439 *offset += write_new_procformatstring_type(file, indent, var->type, var->attrs,
1440 FALSE, &stack_offset);
1441 else
1442 *offset += write_old_procformatstring_type(file, indent, var->type, var->attrs,
1443 FALSE, is_interpreted);
1447 /* emit return value data */
1448 if (is_void(type_function_get_rettype(func->type)))
1450 if (!is_new_style)
1452 print_file(file, 0, "/* %u (void) */\n", *offset);
1453 print_file(file, indent, "0x5b, /* FC_END */\n");
1454 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1455 *offset += 2;
1458 else
1460 print_file( file, 0, "/* %u (return value) */\n", *offset );
1461 if (is_new_style)
1462 *offset += write_new_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1463 NULL, TRUE, &stack_offset);
1464 else
1465 *offset += write_old_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1466 NULL, TRUE, is_interpreted);
1470 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1471 type_pred_t pred, unsigned int *offset)
1473 const statement_t *stmt;
1474 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1476 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1478 const statement_t *stmt_func;
1479 const type_t *iface = stmt->u.type;
1480 const type_t *parent = type_iface_get_inherit( iface );
1481 int count = parent ? count_methods( parent ) : 0;
1483 if (!pred(iface)) continue;
1484 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1486 var_t *func = stmt_func->u.var;
1487 if (is_local(func->attrs)) continue;
1488 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1494 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1496 int indent = 0;
1497 unsigned int offset = 0;
1499 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1500 print_file(file, indent, "{\n");
1501 indent++;
1502 print_file(file, indent, "0,\n");
1503 print_file(file, indent, "{\n");
1504 indent++;
1506 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1508 print_file(file, indent, "0x0\n");
1509 indent--;
1510 print_file(file, indent, "}\n");
1511 indent--;
1512 print_file(file, indent, "};\n");
1513 print_file(file, indent, "\n");
1516 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1518 const statement_t *stmt;
1519 int indent = 0;
1521 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1522 iface->name );
1523 print_file( file, indent, "{\n" );
1524 indent++;
1525 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1527 var_t *func = stmt->u.var;
1528 if (is_local( func->attrs )) continue;
1529 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1531 indent--;
1532 print_file( file, indent, "};\n\n" );
1535 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1537 unsigned char fc;
1539 if (type_get_type(type) == TYPE_BASIC)
1540 fc = get_basic_fc_signed(type);
1541 else if (type_get_type(type) == TYPE_ENUM)
1542 fc = get_enum_fc(type);
1543 else
1544 return 0;
1546 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1547 *typestring_offset += 1;
1548 return 1;
1551 /* write conformance / variance descriptor */
1552 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1553 unsigned int baseoff, const type_t *type,
1554 const expr_t *expr)
1556 unsigned char operator_type = 0;
1557 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1558 const char *conftype_string = "field";
1559 const expr_t *subexpr;
1560 const type_t *iface = NULL;
1561 const char *name;
1563 if (!expr)
1565 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1566 return 4;
1569 if (expr->is_const)
1571 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1572 error("write_conf_or_var_desc: constant value %d is greater than "
1573 "the maximum constant size of %d\n", expr->cval,
1574 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1576 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1577 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1578 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1579 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1581 return 4;
1584 if (!cont_type) /* top-level conformance */
1586 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1587 conftype_string = "parameter";
1588 cont_type = current_func->type;
1589 name = current_func->name;
1590 iface = current_iface;
1592 else
1594 name = cont_type->name;
1595 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1597 conftype = RPC_FC_POINTER_CONFORMANCE;
1598 conftype_string = "field pointer";
1602 subexpr = expr;
1603 switch (subexpr->type)
1605 case EXPR_PPTR:
1606 subexpr = subexpr->ref;
1607 operator_type = RPC_FC_DEREFERENCE;
1608 break;
1609 case EXPR_DIV:
1610 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1612 subexpr = subexpr->ref;
1613 operator_type = RPC_FC_DIV_2;
1615 break;
1616 case EXPR_MUL:
1617 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1619 subexpr = subexpr->ref;
1620 operator_type = RPC_FC_MULT_2;
1622 break;
1623 case EXPR_SUB:
1624 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1626 subexpr = subexpr->ref;
1627 operator_type = RPC_FC_SUB_1;
1629 break;
1630 case EXPR_ADD:
1631 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1633 subexpr = subexpr->ref;
1634 operator_type = RPC_FC_ADD_1;
1636 break;
1637 default:
1638 break;
1641 if (subexpr->type == EXPR_IDENTIFIER)
1643 const type_t *correlation_variable = NULL;
1644 unsigned char param_type = 0;
1645 unsigned int offset = 0;
1646 const var_t *var;
1647 struct expr_loc expr_loc;
1649 if (type_get_type(cont_type) == TYPE_FUNCTION)
1651 var_list_t *args = type_get_function_args( cont_type );
1653 if (is_object( iface )) offset += pointer_size;
1654 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1656 if (var->name && !strcmp(var->name, subexpr->u.sval))
1658 expr_loc.v = var;
1659 correlation_variable = var->type;
1660 break;
1662 offset += get_stack_size( var->type, var->attrs, NULL );
1665 else
1667 var_list_t *fields = type_struct_get_fields( cont_type );
1669 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1671 unsigned int size = field_memsize( var->type, &offset );
1672 if (var->name && !strcmp(var->name, subexpr->u.sval))
1674 expr_loc.v = var;
1675 correlation_variable = var->type;
1676 break;
1678 offset += size;
1682 if (!correlation_variable)
1683 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1684 expr_loc.attr = NULL;
1685 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1687 offset -= baseoff;
1689 if (type_get_type(correlation_variable) == TYPE_BASIC)
1691 switch (get_basic_fc(correlation_variable))
1693 case RPC_FC_CHAR:
1694 case RPC_FC_SMALL:
1695 param_type = RPC_FC_SMALL;
1696 break;
1697 case RPC_FC_BYTE:
1698 case RPC_FC_USMALL:
1699 param_type = RPC_FC_USMALL;
1700 break;
1701 case RPC_FC_WCHAR:
1702 case RPC_FC_SHORT:
1703 param_type = RPC_FC_SHORT;
1704 break;
1705 case RPC_FC_USHORT:
1706 param_type = RPC_FC_USHORT;
1707 break;
1708 case RPC_FC_LONG:
1709 param_type = RPC_FC_LONG;
1710 break;
1711 case RPC_FC_ULONG:
1712 param_type = RPC_FC_ULONG;
1713 break;
1714 default:
1715 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1716 get_basic_fc(correlation_variable));
1719 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1721 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1722 param_type = RPC_FC_LONG;
1723 else
1724 param_type = RPC_FC_SHORT;
1726 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1728 if (pointer_size == 8)
1729 param_type = RPC_FC_HYPER;
1730 else
1731 param_type = RPC_FC_LONG;
1733 else
1735 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1736 subexpr->u.sval);
1737 return 0;
1740 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1741 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1742 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1743 operator_type ? string_of_type(operator_type) : "no operators");
1744 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1745 (unsigned short)offset, offset);
1747 else if (!iface || is_interpreted_func( iface, current_func ))
1749 unsigned int callback_offset = 0;
1750 struct expr_eval_routine *eval;
1751 int found = 0;
1753 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1755 if (eval->cont_type == cont_type ||
1756 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1757 eval->iface == iface &&
1758 eval->name && name && !strcmp(eval->name, name) &&
1759 !compare_expr(eval->expr, expr)))
1761 found = 1;
1762 break;
1764 callback_offset++;
1767 if (!found)
1769 eval = xmalloc (sizeof(*eval));
1770 eval->iface = iface;
1771 eval->cont_type = cont_type;
1772 eval->name = xstrdup( name );
1773 eval->baseoff = baseoff;
1774 eval->expr = expr;
1775 list_add_tail (&expr_eval_routines, &eval->entry);
1778 if (callback_offset > USHRT_MAX)
1779 error("Maximum number of callback routines reached\n");
1781 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1782 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1783 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1785 else /* output a dummy corr desc that isn't used */
1787 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1788 print_file(file, 2, "0x0,\n" );
1789 print_file(file, 2, "NdrFcShort(0x0),\n" );
1791 return 4;
1794 /* return size and start offset of a data field based on current offset */
1795 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1797 unsigned int align = 0;
1798 unsigned int size = type_memsize_and_alignment( type, &align );
1800 *offset = ROUND_SIZE( *offset, align );
1801 return size;
1804 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1806 unsigned int size = 0;
1807 unsigned int max_align;
1808 const var_t *v;
1810 if (!fields) return 0;
1811 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1813 unsigned int falign = 0;
1814 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1815 if (*align < falign) *align = falign;
1816 falign = clamp_align(falign);
1817 size = ROUND_SIZE(size, falign);
1818 size += fsize;
1821 max_align = clamp_align(*align);
1822 size = ROUND_SIZE(size, max_align);
1824 return size;
1827 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1829 unsigned int size, maxs = 0;
1830 unsigned int align = *pmaxa;
1831 const var_t *v;
1833 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1835 /* we could have an empty default field with NULL type */
1836 if (v->type)
1838 size = type_memsize_and_alignment(v->type, &align);
1839 if (maxs < size) maxs = size;
1840 if (*pmaxa < align) *pmaxa = align;
1844 return maxs;
1847 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1849 unsigned int size = 0;
1851 switch (type_get_type(t))
1853 case TYPE_BASIC:
1854 switch (get_basic_fc(t))
1856 case RPC_FC_BYTE:
1857 case RPC_FC_CHAR:
1858 case RPC_FC_USMALL:
1859 case RPC_FC_SMALL:
1860 size = 1;
1861 if (size > *align) *align = size;
1862 break;
1863 case RPC_FC_WCHAR:
1864 case RPC_FC_USHORT:
1865 case RPC_FC_SHORT:
1866 size = 2;
1867 if (size > *align) *align = size;
1868 break;
1869 case RPC_FC_ULONG:
1870 case RPC_FC_LONG:
1871 case RPC_FC_ERROR_STATUS_T:
1872 case RPC_FC_FLOAT:
1873 size = 4;
1874 if (size > *align) *align = size;
1875 break;
1876 case RPC_FC_HYPER:
1877 case RPC_FC_DOUBLE:
1878 size = 8;
1879 if (size > *align) *align = size;
1880 break;
1881 case RPC_FC_INT3264:
1882 case RPC_FC_UINT3264:
1883 case RPC_FC_BIND_PRIMITIVE:
1884 assert( pointer_size );
1885 size = pointer_size;
1886 if (size > *align) *align = size;
1887 break;
1888 default:
1889 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1890 size = 0;
1892 break;
1893 case TYPE_ENUM:
1894 switch (get_enum_fc(t))
1896 case RPC_FC_ENUM16:
1897 case RPC_FC_ENUM32:
1898 size = 4;
1899 if (size > *align) *align = size;
1900 break;
1901 default:
1902 error("type_memsize: Unknown enum type\n");
1903 size = 0;
1905 break;
1906 case TYPE_STRUCT:
1907 size = fields_memsize(type_struct_get_fields(t), align);
1908 break;
1909 case TYPE_ENCAPSULATED_UNION:
1910 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1911 break;
1912 case TYPE_UNION:
1913 size = union_memsize(type_union_get_cases(t), align);
1914 break;
1915 case TYPE_POINTER:
1916 assert( pointer_size );
1917 size = pointer_size;
1918 if (size > *align) *align = size;
1919 break;
1920 case TYPE_ARRAY:
1921 if (!type_array_is_decl_as_ptr(t))
1923 if (is_conformant_array(t))
1925 type_memsize_and_alignment(type_array_get_element(t), align);
1926 size = 0;
1928 else
1929 size = type_array_get_dim(t) *
1930 type_memsize_and_alignment(type_array_get_element(t), align);
1932 else /* declared as a pointer */
1934 assert( pointer_size );
1935 size = pointer_size;
1936 if (size > *align) *align = size;
1938 break;
1939 case TYPE_INTERFACE:
1940 case TYPE_ALIAS:
1941 case TYPE_VOID:
1942 case TYPE_COCLASS:
1943 case TYPE_MODULE:
1944 case TYPE_FUNCTION:
1945 case TYPE_BITFIELD:
1946 /* these types should not be encountered here due to language
1947 * restrictions (interface, void, coclass, module), logical
1948 * restrictions (alias - due to type_get_type call above) or
1949 * checking restrictions (function, bitfield). */
1950 assert(0);
1953 return size;
1956 unsigned int type_memsize(const type_t *t)
1958 unsigned int align = 0;
1959 return type_memsize_and_alignment( t, &align );
1962 static unsigned int type_buffer_alignment(const type_t *t)
1964 const var_list_t *fields;
1965 const var_t *var;
1966 unsigned int max = 0, align;
1968 switch (type_get_type(t))
1970 case TYPE_BASIC:
1971 switch (get_basic_fc(t))
1973 case RPC_FC_BYTE:
1974 case RPC_FC_CHAR:
1975 case RPC_FC_USMALL:
1976 case RPC_FC_SMALL:
1977 return 1;
1978 case RPC_FC_WCHAR:
1979 case RPC_FC_USHORT:
1980 case RPC_FC_SHORT:
1981 return 2;
1982 case RPC_FC_ULONG:
1983 case RPC_FC_LONG:
1984 case RPC_FC_ERROR_STATUS_T:
1985 case RPC_FC_FLOAT:
1986 case RPC_FC_INT3264:
1987 case RPC_FC_UINT3264:
1988 return 4;
1989 case RPC_FC_HYPER:
1990 case RPC_FC_DOUBLE:
1991 return 8;
1992 default:
1993 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1995 break;
1996 case TYPE_ENUM:
1997 switch (get_enum_fc(t))
1999 case RPC_FC_ENUM16:
2000 return 2;
2001 case RPC_FC_ENUM32:
2002 return 4;
2003 default:
2004 error("type_buffer_alignment: Unknown enum type\n");
2006 break;
2007 case TYPE_STRUCT:
2008 if (!(fields = type_struct_get_fields(t))) break;
2009 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2011 if (!var->type) continue;
2012 align = type_buffer_alignment( var->type );
2013 if (max < align) max = align;
2015 break;
2016 case TYPE_ENCAPSULATED_UNION:
2017 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2018 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2020 if (!var->type) continue;
2021 align = type_buffer_alignment( var->type );
2022 if (max < align) max = align;
2024 break;
2025 case TYPE_UNION:
2026 if (!(fields = type_union_get_cases(t))) break;
2027 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2029 if (!var->type) continue;
2030 align = type_buffer_alignment( var->type );
2031 if (max < align) max = align;
2033 break;
2034 case TYPE_ARRAY:
2035 if (!type_array_is_decl_as_ptr(t))
2036 return type_buffer_alignment( type_array_get_element(t) );
2037 /* else fall through */
2038 case TYPE_POINTER:
2039 return 4;
2040 case TYPE_INTERFACE:
2041 case TYPE_ALIAS:
2042 case TYPE_VOID:
2043 case TYPE_COCLASS:
2044 case TYPE_MODULE:
2045 case TYPE_FUNCTION:
2046 case TYPE_BITFIELD:
2047 /* these types should not be encountered here due to language
2048 * restrictions (interface, void, coclass, module), logical
2049 * restrictions (alias - due to type_get_type call above) or
2050 * checking restrictions (function, bitfield). */
2051 assert(0);
2053 return max;
2056 int is_full_pointer_function(const var_t *func)
2058 const var_t *var;
2059 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2060 return TRUE;
2061 if (!type_get_function_args(func->type))
2062 return FALSE;
2063 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2064 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2065 return TRUE;
2066 return FALSE;
2069 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2071 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2072 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2073 fprintf(file, "\n");
2076 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2078 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2079 fprintf(file, "\n");
2082 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2083 const type_t *type,
2084 enum type_context context,
2085 unsigned int offset,
2086 unsigned int *typeformat_offset)
2088 unsigned int start_offset = *typeformat_offset;
2089 short reloff = offset - (*typeformat_offset + 2);
2090 int in_attr, out_attr;
2091 int pointer_type;
2092 unsigned char flags = 0;
2094 pointer_type = get_pointer_fc_context(type, attrs, context);
2096 in_attr = is_attr(attrs, ATTR_IN);
2097 out_attr = is_attr(attrs, ATTR_OUT);
2098 if (!in_attr && !out_attr) in_attr = 1;
2100 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2101 flags |= RPC_FC_P_ONSTACK;
2103 if (is_ptr(type))
2105 type_t *ref = type_pointer_get_ref(type);
2106 if(is_declptr(ref) && !is_user_type(ref))
2107 flags |= RPC_FC_P_DEREF;
2110 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2111 pointer_type,
2112 flags,
2113 string_of_type(pointer_type));
2114 if (file)
2116 if (flags & RPC_FC_P_ONSTACK)
2117 fprintf(file, " [allocated_on_stack]");
2118 if (flags & RPC_FC_P_DEREF)
2119 fprintf(file, " [pointer_deref]");
2120 fprintf(file, " */\n");
2123 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2124 *typeformat_offset += 4;
2126 return start_offset;
2129 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2130 const type_t *type, enum type_context context)
2132 unsigned char fc;
2133 unsigned char pointer_fc;
2134 const type_t *ref;
2135 int in_attr = is_attr(attrs, ATTR_IN);
2136 int out_attr = is_attr(attrs, ATTR_OUT);
2137 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2139 /* for historical reasons, write_simple_pointer also handled string types,
2140 * but no longer does. catch bad uses of the function with this check */
2141 if (is_string_type(attrs, type))
2142 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2144 pointer_fc = get_pointer_fc_context(type, attrs, context);
2146 ref = type_pointer_get_ref(type);
2147 if (type_get_type(ref) == TYPE_ENUM)
2148 fc = get_enum_fc(ref);
2149 else
2150 fc = get_basic_fc(ref);
2152 if (out_attr && !in_attr)
2153 flags |= RPC_FC_P_ONSTACK;
2155 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2156 pointer_fc, flags, string_of_type(pointer_fc),
2157 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2158 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2159 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2160 return 4;
2163 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2165 print_file(file, 0, "/* %u (", tfsoff);
2166 write_type_decl(file, t, NULL);
2167 print_file(file, 0, ") */\n");
2170 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2171 type_t *type, unsigned int ref_offset,
2172 enum type_context context,
2173 unsigned int *typestring_offset)
2175 unsigned int offset = *typestring_offset;
2176 type_t *ref = type_pointer_get_ref(type);
2178 print_start_tfs_comment(file, type, offset);
2179 update_tfsoff(type, offset, file);
2181 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2183 case TGT_BASIC:
2184 case TGT_ENUM:
2185 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2186 break;
2187 default:
2188 if (ref_offset)
2189 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2190 break;
2193 return offset;
2196 static int processed(const type_t *type)
2198 return type->typestring_offset && !type->tfswrite;
2201 static int user_type_has_variable_size(const type_t *t)
2203 if (is_ptr(t))
2204 return TRUE;
2205 else if (type_get_type(t) == TYPE_STRUCT)
2207 switch (get_struct_fc(t))
2209 case RPC_FC_PSTRUCT:
2210 case RPC_FC_CSTRUCT:
2211 case RPC_FC_CPSTRUCT:
2212 case RPC_FC_CVSTRUCT:
2213 return TRUE;
2216 /* Note: Since this only applies to user types, we can't have a conformant
2217 array here, and strings should get filed under pointer in this case. */
2218 return FALSE;
2221 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2223 unsigned int start, absoff, flags;
2224 const char *name = NULL;
2225 type_t *utype = get_user_type(type, &name);
2226 unsigned int usize = type_memsize(utype);
2227 unsigned int ualign = type_buffer_alignment(utype);
2228 unsigned int size = type_memsize(type);
2229 unsigned short funoff = user_type_offset(name);
2230 short reloff;
2232 if (processed(type)) return type->typestring_offset;
2234 guard_rec(type);
2236 if(user_type_has_variable_size(utype)) usize = 0;
2238 if (type_get_type(utype) == TYPE_BASIC ||
2239 type_get_type(utype) == TYPE_ENUM)
2241 unsigned char fc;
2243 if (type_get_type(utype) == TYPE_ENUM)
2244 fc = get_enum_fc(utype);
2245 else
2246 fc = get_basic_fc(utype);
2248 absoff = *tfsoff;
2249 print_start_tfs_comment(file, utype, absoff);
2250 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2251 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2252 *tfsoff += 2;
2254 else
2256 if (!processed(utype))
2257 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2258 absoff = utype->typestring_offset;
2261 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2262 flags = 0x40;
2263 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2264 flags = 0x80;
2265 else
2266 flags = 0;
2268 start = *tfsoff;
2269 update_tfsoff(type, start, file);
2270 print_start_tfs_comment(file, type, start);
2271 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2272 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2273 flags | (ualign - 1), ualign - 1, flags);
2274 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2275 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2276 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2277 *tfsoff += 8;
2278 reloff = absoff - *tfsoff;
2279 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2280 *tfsoff += 2;
2281 return start;
2284 static void write_member_type(FILE *file, const type_t *cont,
2285 int cont_is_complex, const attr_list_t *attrs,
2286 const type_t *type, unsigned int *corroff,
2287 unsigned int *tfsoff)
2289 if (is_embedded_complex(type) && !is_conformant_array(type))
2291 unsigned int absoff;
2292 short reloff;
2294 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2296 absoff = *corroff;
2297 *corroff += 8;
2299 else
2301 absoff = type->typestring_offset;
2303 reloff = absoff - (*tfsoff + 2);
2305 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2306 /* padding is represented using FC_STRUCTPAD* types, so presumably
2307 * this is left over in the format for historical purposes in MIDL
2308 * or rpcrt4. */
2309 print_file(file, 2, "0x0,\n");
2310 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2311 reloff, reloff, absoff);
2312 *tfsoff += 4;
2314 else if (is_ptr(type) || is_conformant_array(type))
2316 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2317 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2318 *tfsoff += 1;
2320 else if (!write_base_type(file, type, tfsoff))
2321 error("Unsupported member type %d\n", type_get_type(type));
2324 static void write_array_element_type(FILE *file, const type_t *type,
2325 int cont_is_complex, unsigned int *tfsoff)
2327 type_t *elem = type_array_get_element(type);
2329 if (!is_embedded_complex(elem) && is_ptr(elem))
2331 type_t *ref = type_pointer_get_ref(elem);
2333 if (processed(ref))
2335 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2336 ref->typestring_offset, tfsoff);
2337 return;
2339 if (cont_is_complex && is_string_type(NULL, elem))
2341 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2342 return;
2344 if (!is_string_type(NULL, elem) &&
2345 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2347 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2348 return;
2351 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2354 static void write_end(FILE *file, unsigned int *tfsoff)
2356 if (*tfsoff % 2 == 0)
2358 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2359 *tfsoff += 1;
2361 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2362 *tfsoff += 1;
2365 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2367 unsigned int offset = 0;
2368 var_list_t *fs = type_struct_get_fields(type);
2369 var_t *f;
2371 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2373 type_t *ft = f->type;
2374 unsigned int size = field_memsize( ft, &offset );
2375 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2377 short reloff;
2378 unsigned int absoff = ft->typestring_offset;
2379 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2380 absoff += 8; /* we already have a corr descr, skip it */
2381 reloff = absoff - (*tfsoff + 6);
2382 print_file(file, 0, "/* %d */\n", *tfsoff);
2383 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2384 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2385 write_conf_or_var_desc(file, current_structure, offset, ft,
2386 get_attrp(f->attrs, ATTR_SWITCHIS));
2387 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
2388 reloff, reloff, absoff);
2389 *tfsoff += 8;
2391 offset += size;
2395 static int write_pointer_description_offsets(
2396 FILE *file, const attr_list_t *attrs, type_t *type,
2397 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2398 unsigned int *typestring_offset)
2400 int written = 0;
2402 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2403 (is_array(type) && type_array_is_decl_as_ptr(type)))
2405 if (offset_in_memory && offset_in_buffer)
2407 unsigned int memsize;
2409 /* pointer instance
2411 * note that MSDN states that for pointer layouts in structures,
2412 * this is a negative offset from the end of the structure, but
2413 * this statement is incorrect. all offsets are positive */
2414 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2415 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2417 memsize = type_memsize(type);
2418 *offset_in_memory += memsize;
2419 /* increment these separately as in the case of conformant (varying)
2420 * structures these start at different values */
2421 *offset_in_buffer += memsize;
2423 *typestring_offset += 4;
2425 if (is_ptr(type))
2427 type_t *ref = type_pointer_get_ref(type);
2429 if (is_string_type(attrs, type))
2430 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2431 else if (processed(ref))
2432 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2433 ref->typestring_offset, typestring_offset);
2434 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2435 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2436 else
2437 error("write_pointer_description_offsets: type format string unknown\n");
2439 else
2441 unsigned int offset = type->typestring_offset;
2442 /* skip over the pointer that is written for strings, since a
2443 * pointer has to be written in-place here */
2444 if (is_string_type(attrs, type))
2445 offset += 4;
2446 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2449 return 1;
2452 if (is_array(type))
2454 return write_pointer_description_offsets(
2455 file, attrs, type_array_get_element(type), offset_in_memory,
2456 offset_in_buffer, typestring_offset);
2458 else if (is_non_complex_struct(type))
2460 /* otherwise search for interesting fields to parse */
2461 const var_t *v;
2462 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2464 if (offset_in_memory && offset_in_buffer)
2466 unsigned int padding;
2467 unsigned int align = 0;
2468 type_memsize_and_alignment(v->type, &align);
2469 padding = ROUNDING(*offset_in_memory, align);
2470 *offset_in_memory += padding;
2471 *offset_in_buffer += padding;
2473 written += write_pointer_description_offsets(
2474 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2475 typestring_offset);
2478 else
2480 if (offset_in_memory && offset_in_buffer)
2482 unsigned int memsize = type_memsize(type);
2483 *offset_in_memory += memsize;
2484 /* increment these separately as in the case of conformant (varying)
2485 * structures these start at different values */
2486 *offset_in_buffer += memsize;
2490 return written;
2493 static int write_no_repeat_pointer_descriptions(
2494 FILE *file, const attr_list_t *attrs, type_t *type,
2495 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2496 unsigned int *typestring_offset)
2498 int written = 0;
2500 if (is_ptr(type) ||
2501 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2503 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2504 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2505 *typestring_offset += 2;
2507 return write_pointer_description_offsets(file, attrs, type,
2508 offset_in_memory, offset_in_buffer, typestring_offset);
2511 if (is_non_complex_struct(type))
2513 const var_t *v;
2514 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2516 if (offset_in_memory && offset_in_buffer)
2518 unsigned int padding;
2519 unsigned int align = 0;
2520 type_memsize_and_alignment(v->type, &align);
2521 padding = ROUNDING(*offset_in_memory, align);
2522 *offset_in_memory += padding;
2523 *offset_in_buffer += padding;
2525 written += write_no_repeat_pointer_descriptions(
2526 file, v->attrs, v->type,
2527 offset_in_memory, offset_in_buffer, typestring_offset);
2530 else
2532 unsigned int memsize = type_memsize(type);
2533 *offset_in_memory += memsize;
2534 /* increment these separately as in the case of conformant (varying)
2535 * structures these start at different values */
2536 *offset_in_buffer += memsize;
2539 return written;
2542 /* Note: if file is NULL return value is number of pointers to write, else
2543 * it is the number of type format characters written */
2544 static int write_fixed_array_pointer_descriptions(
2545 FILE *file, const attr_list_t *attrs, type_t *type,
2546 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2547 unsigned int *typestring_offset)
2549 int pointer_count = 0;
2551 if (type_get_type(type) == TYPE_ARRAY &&
2552 !type_array_has_conformance(type) && !type_array_has_variance(type))
2554 unsigned int temp = 0;
2555 /* unfortunately, this needs to be done in two passes to avoid
2556 * writing out redundant FC_FIXED_REPEAT descriptions */
2557 pointer_count = write_pointer_description_offsets(
2558 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2559 if (pointer_count > 0)
2561 unsigned int increment_size;
2562 unsigned int offset_of_array_pointer_mem = 0;
2563 unsigned int offset_of_array_pointer_buf = 0;
2565 increment_size = type_memsize(type_array_get_element(type));
2567 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2568 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2569 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2570 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2571 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2572 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2573 *typestring_offset += 10;
2575 pointer_count = write_pointer_description_offsets(
2576 file, attrs, type, &offset_of_array_pointer_mem,
2577 &offset_of_array_pointer_buf, typestring_offset);
2580 else if (type_get_type(type) == TYPE_STRUCT)
2582 const var_t *v;
2583 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2585 if (offset_in_memory && offset_in_buffer)
2587 unsigned int padding;
2588 unsigned int align = 0;
2589 type_memsize_and_alignment(v->type, &align);
2590 padding = ROUNDING(*offset_in_memory, align);
2591 *offset_in_memory += padding;
2592 *offset_in_buffer += padding;
2594 pointer_count += write_fixed_array_pointer_descriptions(
2595 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2596 typestring_offset);
2599 else
2601 if (offset_in_memory && offset_in_buffer)
2603 unsigned int memsize;
2604 memsize = type_memsize(type);
2605 *offset_in_memory += memsize;
2606 /* increment these separately as in the case of conformant (varying)
2607 * structures these start at different values */
2608 *offset_in_buffer += memsize;
2612 return pointer_count;
2615 /* Note: if file is NULL return value is number of pointers to write, else
2616 * it is the number of type format characters written */
2617 static int write_conformant_array_pointer_descriptions(
2618 FILE *file, const attr_list_t *attrs, type_t *type,
2619 unsigned int offset_in_memory, unsigned int *typestring_offset)
2621 int pointer_count = 0;
2623 if (is_conformant_array(type) && !type_array_has_variance(type))
2625 unsigned int temp = 0;
2626 /* unfortunately, this needs to be done in two passes to avoid
2627 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2628 pointer_count = write_pointer_description_offsets(
2629 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2630 if (pointer_count > 0)
2632 unsigned int increment_size;
2633 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2634 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2636 increment_size = type_memsize(type_array_get_element(type));
2638 if (increment_size > USHRT_MAX)
2639 error("array size of %u bytes is too large\n", increment_size);
2641 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2642 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2643 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2644 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2645 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2646 *typestring_offset += 8;
2648 pointer_count = write_pointer_description_offsets(
2649 file, attrs, type_array_get_element(type),
2650 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2651 typestring_offset);
2655 return pointer_count;
2658 /* Note: if file is NULL return value is number of pointers to write, else
2659 * it is the number of type format characters written */
2660 static int write_varying_array_pointer_descriptions(
2661 FILE *file, const attr_list_t *attrs, type_t *type,
2662 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2663 unsigned int *typestring_offset)
2665 int pointer_count = 0;
2667 if (is_array(type) && type_array_has_variance(type))
2669 unsigned int temp = 0;
2670 /* unfortunately, this needs to be done in two passes to avoid
2671 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2672 pointer_count = write_pointer_description_offsets(
2673 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2674 if (pointer_count > 0)
2676 unsigned int increment_size;
2678 increment_size = type_memsize(type_array_get_element(type));
2680 if (increment_size > USHRT_MAX)
2681 error("array size of %u bytes is too large\n", increment_size);
2683 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2684 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2685 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2686 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2687 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2688 *typestring_offset += 8;
2690 pointer_count = write_pointer_description_offsets(
2691 file, attrs, type_array_get_element(type), offset_in_memory,
2692 offset_in_buffer, typestring_offset);
2695 else if (type_get_type(type) == TYPE_STRUCT)
2697 const var_t *v;
2698 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2700 if (offset_in_memory && offset_in_buffer)
2702 unsigned int align = 0, padding;
2704 if (is_array(v->type) && type_array_has_variance(v->type))
2706 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2707 /* skip over variance and offset in buffer */
2708 *offset_in_buffer += 8;
2711 type_memsize_and_alignment(v->type, &align);
2712 padding = ROUNDING(*offset_in_memory, align);
2713 *offset_in_memory += padding;
2714 *offset_in_buffer += padding;
2716 pointer_count += write_varying_array_pointer_descriptions(
2717 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2718 typestring_offset);
2721 else
2723 if (offset_in_memory && offset_in_buffer)
2725 unsigned int memsize = type_memsize(type);
2726 *offset_in_memory += memsize;
2727 /* increment these separately as in the case of conformant (varying)
2728 * structures these start at different values */
2729 *offset_in_buffer += memsize;
2733 return pointer_count;
2736 static void write_pointer_description(FILE *file, type_t *type,
2737 unsigned int *typestring_offset)
2739 unsigned int offset_in_buffer;
2740 unsigned int offset_in_memory;
2742 /* pass 1: search for single instance of a pointer (i.e. don't descend
2743 * into arrays) */
2744 if (!is_array(type))
2746 offset_in_memory = 0;
2747 offset_in_buffer = 0;
2748 write_no_repeat_pointer_descriptions(
2749 file, NULL, type,
2750 &offset_in_memory, &offset_in_buffer, typestring_offset);
2753 /* pass 2: search for pointers in fixed arrays */
2754 offset_in_memory = 0;
2755 offset_in_buffer = 0;
2756 write_fixed_array_pointer_descriptions(
2757 file, NULL, type,
2758 &offset_in_memory, &offset_in_buffer, typestring_offset);
2760 /* pass 3: search for pointers in conformant only arrays (but don't descend
2761 * into conformant varying or varying arrays) */
2762 if (is_conformant_array(type) &&
2763 (type_array_is_decl_as_ptr(type) || !current_structure))
2764 write_conformant_array_pointer_descriptions(
2765 file, NULL, type, 0, typestring_offset);
2766 else if (type_get_type(type) == TYPE_STRUCT &&
2767 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2769 type_t *carray = find_array_or_string_in_struct(type)->type;
2770 write_conformant_array_pointer_descriptions( file, NULL, carray,
2771 type_memsize(type), typestring_offset);
2774 /* pass 4: search for pointers in varying arrays */
2775 offset_in_memory = 0;
2776 offset_in_buffer = 0;
2777 write_varying_array_pointer_descriptions(
2778 file, NULL, type,
2779 &offset_in_memory, &offset_in_buffer, typestring_offset);
2782 int is_declptr(const type_t *t)
2784 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2787 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2788 type_t *type, enum type_context context,
2789 const char *name, unsigned int *typestring_offset)
2791 unsigned int start_offset;
2792 unsigned char rtype;
2793 type_t *elem_type;
2794 int is_processed = processed(type);
2796 start_offset = *typestring_offset;
2798 if (is_declptr(type))
2800 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2801 int pointer_type = get_pointer_fc_context(type, attrs, context);
2802 if (!pointer_type)
2803 pointer_type = RPC_FC_RP;
2804 print_start_tfs_comment(file, type, *typestring_offset);
2805 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2806 pointer_type, flag, string_of_type(pointer_type),
2807 flag ? " [simple_pointer]" : "");
2808 *typestring_offset += 2;
2809 if (!flag)
2811 print_file(file, 2, "NdrFcShort(0x2),\n");
2812 *typestring_offset += 2;
2814 is_processed = FALSE;
2817 if (is_array(type))
2818 elem_type = type_array_get_element(type);
2819 else
2820 elem_type = type_pointer_get_ref(type);
2822 if (type_get_type(elem_type) != TYPE_BASIC)
2824 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2825 return start_offset;
2828 rtype = get_basic_fc(elem_type);
2829 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2831 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2832 return start_offset;
2835 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2837 unsigned int dim = type_array_get_dim(type);
2839 if (is_processed) return start_offset;
2841 /* FIXME: multi-dimensional array */
2842 if (0xffffu < dim)
2843 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2844 name, 0xffffu, dim - 0xffffu);
2846 if (rtype == RPC_FC_WCHAR)
2847 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2848 else
2849 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2850 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2851 *typestring_offset += 2;
2853 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2854 *typestring_offset += 2;
2856 update_tfsoff(type, start_offset, file);
2857 return start_offset;
2859 else if (is_conformant_array(type))
2861 if (rtype == RPC_FC_WCHAR)
2862 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2863 else
2864 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2865 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2866 *typestring_offset += 2;
2868 *typestring_offset += write_conf_or_var_desc(
2869 file, current_structure,
2870 (!type_array_is_decl_as_ptr(type) && current_structure
2871 ? type_memsize(current_structure)
2872 : 0),
2873 type, type_array_get_conformance(type));
2875 update_tfsoff(type, start_offset, file);
2876 return start_offset;
2878 else
2880 if (is_processed) return start_offset;
2882 if (rtype == RPC_FC_WCHAR)
2883 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2884 else
2885 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2886 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2887 *typestring_offset += 2;
2889 update_tfsoff(type, start_offset, file);
2890 return start_offset;
2894 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2895 const char *name, unsigned int *typestring_offset)
2897 const expr_t *length_is = type_array_get_variance(type);
2898 const expr_t *size_is = type_array_get_conformance(type);
2899 unsigned int align;
2900 unsigned int size;
2901 unsigned int start_offset;
2902 unsigned char fc;
2903 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2904 unsigned int baseoff
2905 = !type_array_is_decl_as_ptr(type) && current_structure
2906 ? type_memsize(current_structure)
2907 : 0;
2909 if (!pointer_type)
2910 pointer_type = RPC_FC_RP;
2912 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2914 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2915 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2916 fc = get_array_fc(type);
2918 start_offset = *typestring_offset;
2919 update_tfsoff(type, start_offset, file);
2920 print_start_tfs_comment(file, type, start_offset);
2921 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2922 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2923 *typestring_offset += 2;
2925 align = 0;
2926 if (fc != RPC_FC_BOGUS_ARRAY)
2928 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2930 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2931 *typestring_offset += 4;
2933 else
2935 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2936 *typestring_offset += 2;
2939 if (is_conformant_array(type))
2940 *typestring_offset
2941 += write_conf_or_var_desc(file, current_structure, baseoff,
2942 type, size_is);
2944 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2946 unsigned int elsize = type_memsize(type_array_get_element(type));
2947 unsigned int dim = type_array_get_dim(type);
2949 if (fc == RPC_FC_LGVARRAY)
2951 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2952 *typestring_offset += 4;
2954 else
2956 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2957 *typestring_offset += 2;
2960 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2961 *typestring_offset += 2;
2964 if (length_is)
2965 *typestring_offset
2966 += write_conf_or_var_desc(file, current_structure, baseoff,
2967 type, length_is);
2969 if (type_has_pointers(type_array_get_element(type)) &&
2970 (type_array_is_decl_as_ptr(type) || !current_structure))
2972 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2973 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2974 *typestring_offset += 2;
2975 write_pointer_description(file, type, typestring_offset);
2976 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2977 *typestring_offset += 1;
2980 write_array_element_type(file, type, FALSE, typestring_offset);
2981 write_end(file, typestring_offset);
2983 else
2985 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2986 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2987 *typestring_offset += 2;
2988 *typestring_offset
2989 += write_conf_or_var_desc(file, current_structure, baseoff,
2990 type, size_is);
2991 *typestring_offset
2992 += write_conf_or_var_desc(file, current_structure, baseoff,
2993 type, length_is);
2995 write_array_element_type(file, type, TRUE, typestring_offset);
2996 write_end(file, typestring_offset);
2999 return start_offset;
3002 static const var_t *find_array_or_string_in_struct(const type_t *type)
3004 const var_list_t *fields = type_struct_get_fields(type);
3005 const var_t *last_field;
3006 const type_t *ft;
3008 if (!fields || list_empty(fields))
3009 return NULL;
3011 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3012 ft = last_field->type;
3014 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3015 return last_field;
3017 if (type_get_type(ft) == TYPE_STRUCT)
3018 return find_array_or_string_in_struct(ft);
3019 else
3020 return NULL;
3023 static void write_struct_members(FILE *file, const type_t *type,
3024 int is_complex, unsigned int *corroff,
3025 unsigned int *typestring_offset)
3027 const var_t *field;
3028 unsigned short offset = 0;
3029 unsigned int salign = 1;
3030 int padding;
3031 var_list_t *fields = type_struct_get_fields(type);
3033 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3035 type_t *ft = field->type;
3036 unsigned int align = 0;
3037 unsigned int size = type_memsize_and_alignment(ft, &align);
3038 align = clamp_align(align);
3039 if (salign < align) salign = align;
3041 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3043 if ((align - 1) & offset)
3045 unsigned char fc = 0;
3046 switch (align)
3048 case 2:
3049 fc = RPC_FC_ALIGNM2;
3050 break;
3051 case 4:
3052 fc = RPC_FC_ALIGNM4;
3053 break;
3054 case 8:
3055 fc = RPC_FC_ALIGNM8;
3056 break;
3057 default:
3058 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3060 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3061 offset = ROUND_SIZE(offset, align);
3062 *typestring_offset += 1;
3064 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3065 typestring_offset);
3066 offset += size;
3070 padding = ROUNDING(offset, salign);
3071 if (padding)
3073 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3074 RPC_FC_STRUCTPAD1 + padding - 1,
3075 padding);
3076 *typestring_offset += 1;
3079 write_end(file, typestring_offset);
3082 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3083 const char *name, unsigned int *tfsoff)
3085 const type_t *save_current_structure = current_structure;
3086 unsigned int total_size;
3087 const var_t *array;
3088 unsigned int start_offset;
3089 unsigned int align;
3090 unsigned int corroff;
3091 var_t *f;
3092 unsigned char fc = get_struct_fc(type);
3093 var_list_t *fields = type_struct_get_fields(type);
3095 if (processed(type)) return type->typestring_offset;
3097 guard_rec(type);
3098 current_structure = type;
3100 total_size = type_memsize(type);
3101 align = type_buffer_alignment(type);
3102 if (total_size > USHRT_MAX)
3103 error("structure size for %s exceeds %d bytes by %d bytes\n",
3104 name, USHRT_MAX, total_size - USHRT_MAX);
3106 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3107 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3109 array = find_array_or_string_in_struct(type);
3110 if (array && !processed(array->type))
3112 if(is_string_type(array->attrs, array->type))
3113 write_string_tfs(file, array->attrs, array->type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3114 else
3115 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3118 corroff = *tfsoff;
3119 write_descriptors(file, type, tfsoff);
3121 start_offset = *tfsoff;
3122 update_tfsoff(type, start_offset, file);
3123 print_start_tfs_comment(file, type, start_offset);
3124 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3125 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3126 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3127 *tfsoff += 4;
3129 if (array)
3131 unsigned int absoff = array->type->typestring_offset;
3132 short reloff = absoff - *tfsoff;
3133 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3134 reloff, reloff, absoff);
3135 *tfsoff += 2;
3137 else if (fc == RPC_FC_BOGUS_STRUCT)
3139 print_file(file, 2, "NdrFcShort(0x0),\n");
3140 *tfsoff += 2;
3143 if (fc == RPC_FC_BOGUS_STRUCT)
3145 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3146 nothing is written to file yet. On the actual writing pass,
3147 this will have been updated. */
3148 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3149 int reloff = absoff - *tfsoff;
3150 assert( reloff >= 0 );
3151 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3152 (unsigned short)reloff, reloff, absoff);
3153 *tfsoff += 2;
3155 else if ((fc == RPC_FC_PSTRUCT) ||
3156 (fc == RPC_FC_CPSTRUCT) ||
3157 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3159 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3160 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3161 *tfsoff += 2;
3162 write_pointer_description(file, type, tfsoff);
3163 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3164 *tfsoff += 1;
3167 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3168 tfsoff);
3170 if (fc == RPC_FC_BOGUS_STRUCT)
3172 const var_t *f;
3174 type->ptrdesc = *tfsoff;
3175 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3177 type_t *ft = f->type;
3178 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3180 case TGT_POINTER:
3181 if (is_string_type(f->attrs, ft))
3182 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3183 else
3184 write_pointer_tfs(file, f->attrs, ft,
3185 type_pointer_get_ref(ft)->typestring_offset,
3186 TYPE_CONTEXT_CONTAINER, tfsoff);
3187 break;
3188 case TGT_ARRAY:
3189 if (type_array_is_decl_as_ptr(ft))
3191 unsigned int offset;
3193 print_file(file, 0, "/* %d */\n", *tfsoff);
3195 offset = ft->typestring_offset;
3196 /* skip over the pointer that is written for strings, since a
3197 * pointer has to be written in-place here */
3198 if (is_string_type(f->attrs, ft))
3199 offset += 4;
3200 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3202 break;
3203 default:
3204 break;
3207 if (type->ptrdesc == *tfsoff)
3208 type->ptrdesc = 0;
3211 current_structure = save_current_structure;
3212 return start_offset;
3215 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3217 if (t == NULL)
3219 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3221 else
3223 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3225 unsigned char fc;
3226 if (type_get_type(t) == TYPE_BASIC)
3227 fc = get_basic_fc(t);
3228 else
3229 fc = get_enum_fc(t);
3230 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3231 fc, string_of_type(fc));
3233 else if (t->typestring_offset)
3235 short reloff = t->typestring_offset - *tfsoff;
3236 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3237 reloff, reloff, t->typestring_offset);
3239 else
3240 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3243 *tfsoff += 2;
3246 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3247 type_t *type, unsigned int *tfsoff)
3249 unsigned int start_offset;
3250 unsigned int size;
3251 var_list_t *fields;
3252 unsigned int nbranch = 0;
3253 type_t *deftype = NULL;
3254 short nodeftype = 0xffff;
3255 var_t *f;
3257 if (processed(type) &&
3258 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3259 return type->typestring_offset;
3261 guard_rec(type);
3263 size = type_memsize(type);
3265 fields = type_union_get_cases(type);
3267 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3269 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3270 if (cases)
3271 nbranch += list_count(cases);
3272 if (f->type)
3273 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3276 start_offset = *tfsoff;
3277 update_tfsoff(type, start_offset, file);
3278 print_start_tfs_comment(file, type, start_offset);
3279 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3281 const var_t *sv = type_union_get_switch_value(type);
3282 const type_t *st = sv->type;
3283 unsigned char fc;
3285 if (type_get_type(st) == TYPE_BASIC)
3287 fc = get_basic_fc(st);
3288 switch (fc)
3290 case RPC_FC_CHAR:
3291 case RPC_FC_SMALL:
3292 case RPC_FC_BYTE:
3293 case RPC_FC_USMALL:
3294 case RPC_FC_WCHAR:
3295 case RPC_FC_SHORT:
3296 case RPC_FC_USHORT:
3297 case RPC_FC_LONG:
3298 case RPC_FC_ULONG:
3299 break;
3300 default:
3301 fc = 0;
3302 error("union switch type must be an integer, char, or enum\n");
3305 else if (type_get_type(st) == TYPE_ENUM)
3306 fc = get_enum_fc(st);
3307 else
3308 error("union switch type must be an integer, char, or enum\n");
3310 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3311 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3312 0x40 | fc, string_of_type(fc));
3313 *tfsoff += 2;
3315 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3317 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3318 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3319 unsigned char fc;
3321 if (type_get_type(st) == TYPE_BASIC)
3323 fc = get_basic_fc(st);
3324 switch (fc)
3326 case RPC_FC_CHAR:
3327 case RPC_FC_SMALL:
3328 case RPC_FC_USMALL:
3329 case RPC_FC_SHORT:
3330 case RPC_FC_USHORT:
3331 case RPC_FC_LONG:
3332 case RPC_FC_ULONG:
3333 case RPC_FC_ENUM16:
3334 case RPC_FC_ENUM32:
3335 break;
3336 default:
3337 fc = 0;
3338 error("union switch type must be an integer, char, or enum\n");
3341 else if (type_get_type(st) == TYPE_ENUM)
3342 fc = get_enum_fc(st);
3343 else
3344 error("union switch type must be an integer, char, or enum\n");
3346 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3347 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3348 fc, string_of_type(fc));
3349 *tfsoff += 2;
3350 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3351 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3352 *tfsoff += 2;
3353 print_file(file, 0, "/* %u */\n", *tfsoff);
3356 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3357 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3358 *tfsoff += 4;
3360 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3362 type_t *ft = f->type;
3363 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3364 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3365 expr_t *c;
3367 if (cases == NULL && !deflt)
3368 error("union field %s with neither case nor default attribute\n", f->name);
3370 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3372 /* MIDL doesn't check for duplicate cases, even though that seems
3373 like a reasonable thing to do, it just dumps them to the TFS
3374 like we're going to do here. */
3375 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3376 *tfsoff += 4;
3377 write_branch_type(file, ft, tfsoff);
3380 /* MIDL allows multiple default branches, even though that seems
3381 illogical, it just chooses the last one, which is what we will
3382 do. */
3383 if (deflt)
3385 deftype = ft;
3386 nodeftype = 0;
3390 if (deftype)
3392 write_branch_type(file, deftype, tfsoff);
3394 else
3396 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3397 *tfsoff += 2;
3400 return start_offset;
3403 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3404 unsigned int *typeformat_offset)
3406 unsigned int i;
3407 unsigned int start_offset = *typeformat_offset;
3408 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3410 if (!iid && processed(type)) return type->typestring_offset;
3412 print_start_tfs_comment(file, type, start_offset);
3413 update_tfsoff(type, start_offset, file);
3415 if (iid)
3417 print_file(file, 2, "0x2f, /* FC_IP */\n");
3418 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3419 *typeformat_offset
3420 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3422 else
3424 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3425 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3427 if (! uuid)
3428 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3430 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3431 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3432 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3433 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3434 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3435 for (i = 0; i < 8; ++i)
3436 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3438 if (file)
3439 fprintf(file, "\n");
3441 *typeformat_offset += 18;
3443 return start_offset;
3446 static unsigned int write_contexthandle_tfs(FILE *file,
3447 const attr_list_t *attrs,
3448 type_t *type,
3449 int toplevel_param,
3450 unsigned int *typeformat_offset)
3452 unsigned int start_offset = *typeformat_offset;
3453 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3455 print_start_tfs_comment(file, type, start_offset);
3457 if (flags & 0x80) /* via ptr */
3459 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3460 if (!pointer_type) pointer_type = RPC_FC_RP;
3461 *typeformat_offset += 4;
3462 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3463 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3464 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3467 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3468 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3469 /* return and can't be null values overlap */
3470 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3471 print_file(file, 0, "can't be null, ");
3472 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3473 print_file(file, 0, "serialize, ");
3474 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3475 print_file(file, 0, "no serialize, ");
3476 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3477 print_file(file, 0, "strict, ");
3478 if ((flags & 0x21) == 0x20)
3479 print_file(file, 0, "out, ");
3480 if ((flags & 0x21) == 0x21)
3481 print_file(file, 0, "return, ");
3482 if (flags & 0x40)
3483 print_file(file, 0, "in, ");
3484 if (flags & 0x80)
3485 print_file(file, 0, "via ptr, ");
3486 print_file(file, 0, "*/\n");
3487 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3488 print_file(file, 2, "0, /* FIXME: param num */\n");
3489 *typeformat_offset += 4;
3491 update_tfsoff( type, start_offset, file );
3492 return start_offset;
3495 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3496 type_t *type, expr_list_t *range_list,
3497 unsigned int *typeformat_offset)
3499 unsigned char fc;
3500 unsigned int start_offset = *typeformat_offset;
3501 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3502 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3504 if (type_get_type(type) == TYPE_BASIC)
3505 fc = get_basic_fc(type);
3506 else
3507 fc = get_enum_fc(type);
3509 /* fc must fit in lower 4-bits of 8-bit field below */
3510 assert(fc <= 0xf);
3512 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3513 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3514 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3515 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3516 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3517 update_tfsoff( type, start_offset, file );
3518 *typeformat_offset += 10;
3520 return start_offset;
3523 static unsigned int write_type_tfs(FILE *file, int indent,
3524 const attr_list_t *attrs, type_t *type,
3525 const char *name,
3526 enum type_context context,
3527 unsigned int *typeformat_offset)
3529 unsigned int offset;
3531 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3533 case TGT_CTXT_HANDLE:
3534 case TGT_CTXT_HANDLE_POINTER:
3535 return write_contexthandle_tfs(file, attrs, type,
3536 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3537 case TGT_USER_TYPE:
3538 return write_user_tfs(file, type, typeformat_offset);
3539 case TGT_STRING:
3540 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3541 case TGT_ARRAY:
3543 unsigned int off;
3544 /* conformant and pointer arrays are handled specially */
3545 if ((context != TYPE_CONTEXT_CONTAINER &&
3546 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3547 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3548 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3549 else
3550 off = 0;
3551 if (context != TYPE_CONTEXT_CONTAINER &&
3552 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3554 int ptr_type;
3555 ptr_type = get_pointer_fc(type, attrs,
3556 context == TYPE_CONTEXT_TOPLEVELPARAM);
3557 if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3559 unsigned int absoff = type->typestring_offset;
3560 short reloff = absoff - (*typeformat_offset + 2);
3561 off = *typeformat_offset;
3562 print_file(file, 0, "/* %d */\n", off);
3563 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3564 string_of_type(ptr_type));
3565 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3566 reloff, reloff, absoff);
3567 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3568 *typeformat_offset += 4;
3570 type->details.array.ptr_tfsoff = off;
3572 return off;
3574 case TGT_STRUCT:
3575 return write_struct_tfs(file, type, name, typeformat_offset);
3576 case TGT_UNION:
3577 return write_union_tfs(file, attrs, type, typeformat_offset);
3578 case TGT_ENUM:
3579 case TGT_BASIC:
3580 /* nothing to do */
3581 return 0;
3582 case TGT_RANGE:
3584 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3585 if (!range_list)
3586 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3587 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3589 case TGT_IFACE_POINTER:
3590 return write_ip_tfs(file, attrs, type, typeformat_offset);
3591 case TGT_POINTER:
3593 enum type_context ref_context;
3594 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3595 ref_context = TYPE_CONTEXT_PARAM;
3596 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3597 ref_context = TYPE_CONTEXT_CONTAINER;
3598 else
3599 ref_context = context;
3600 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3601 ref_context, typeformat_offset);
3602 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3603 return 0;
3604 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3606 case TGT_INVALID:
3607 break;
3609 error("invalid type %s for var %s\n", type->name, name);
3610 return 0;
3613 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3614 const char *name, int write_ptr, unsigned int *tfsoff)
3616 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3619 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3620 type_pred_t pred, unsigned int *typeformat_offset)
3622 const var_t *var;
3623 const statement_t *stmt;
3625 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3627 const type_t *iface;
3628 const statement_t *stmt_func;
3630 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3631 continue;
3633 iface = stmt->u.type;
3634 if (!pred(iface))
3635 continue;
3637 current_iface = iface;
3638 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3640 const var_t *func = stmt_func->u.var;
3641 current_func = func;
3642 if (is_local(func->attrs)) continue;
3644 if (!is_void(type_function_get_rettype(func->type)))
3646 write_type_tfs( file, 2, func->attrs, type_function_get_rettype(func->type),
3647 func->name, TYPE_CONTEXT_PARAM, typeformat_offset);
3650 if (type_get_function_args(func->type))
3651 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3652 write_type_tfs( file, 2, var->attrs, var->type, var->name,
3653 TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset );
3657 return *typeformat_offset + 1;
3660 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3662 unsigned int typeformat_offset = 2;
3664 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3668 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3670 int indent = 0;
3672 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3673 print_file(file, indent, "{\n");
3674 indent++;
3675 print_file(file, indent, "0,\n");
3676 print_file(file, indent, "{\n");
3677 indent++;
3678 print_file(file, indent, "NdrFcShort(0x0),\n");
3680 set_all_tfswrite(TRUE);
3681 process_tfs(file, stmts, pred);
3683 print_file(file, indent, "0x0\n");
3684 indent--;
3685 print_file(file, indent, "}\n");
3686 indent--;
3687 print_file(file, indent, "};\n");
3688 print_file(file, indent, "\n");
3691 static unsigned int get_required_buffer_size_type(
3692 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3694 *alignment = 0;
3695 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3697 case TGT_USER_TYPE:
3699 const char *uname;
3700 const type_t *utype = get_user_type(type, &uname);
3701 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3703 case TGT_BASIC:
3704 switch (get_basic_fc(type))
3706 case RPC_FC_BYTE:
3707 case RPC_FC_CHAR:
3708 case RPC_FC_USMALL:
3709 case RPC_FC_SMALL:
3710 *alignment = 4;
3711 return 1;
3713 case RPC_FC_WCHAR:
3714 case RPC_FC_USHORT:
3715 case RPC_FC_SHORT:
3716 *alignment = 4;
3717 return 2;
3719 case RPC_FC_ULONG:
3720 case RPC_FC_LONG:
3721 case RPC_FC_FLOAT:
3722 case RPC_FC_ERROR_STATUS_T:
3723 *alignment = 4;
3724 return 4;
3726 case RPC_FC_HYPER:
3727 case RPC_FC_DOUBLE:
3728 *alignment = 8;
3729 return 8;
3731 case RPC_FC_INT3264:
3732 case RPC_FC_UINT3264:
3733 assert( pointer_size );
3734 *alignment = pointer_size;
3735 return pointer_size;
3737 case RPC_FC_IGNORE:
3738 case RPC_FC_BIND_PRIMITIVE:
3739 return 0;
3741 default:
3742 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3743 get_basic_fc(type));
3744 return 0;
3746 break;
3748 case TGT_ENUM:
3749 switch (get_enum_fc(type))
3751 case RPC_FC_ENUM32:
3752 *alignment = 4;
3753 return 4;
3754 case RPC_FC_ENUM16:
3755 *alignment = 4;
3756 return 2;
3758 break;
3760 case TGT_STRUCT:
3761 if (get_struct_fc(type) == RPC_FC_STRUCT)
3763 if (!type_struct_get_fields(type)) return 0;
3764 return fields_memsize(type_struct_get_fields(type), alignment);
3766 break;
3768 case TGT_POINTER:
3770 unsigned int size, align;
3771 const type_t *ref = type_pointer_get_ref(type);
3772 if (is_string_type( attrs, ref )) break;
3773 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3774 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3776 size += 4 + align;
3777 align = 4;
3779 *alignment = align;
3780 return size;
3783 case TGT_ARRAY:
3784 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3786 switch (get_array_fc(type))
3788 case RPC_FC_SMFARRAY:
3789 case RPC_FC_LGFARRAY:
3790 return type_array_get_dim(type) *
3791 get_required_buffer_size_type(type_array_get_element(type), name,
3792 NULL, FALSE, alignment);
3795 break;
3797 default:
3798 break;
3800 return 0;
3803 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3805 int in_attr = is_attr(var->attrs, ATTR_IN);
3806 int out_attr = is_attr(var->attrs, ATTR_OUT);
3808 if (!in_attr && !out_attr)
3809 in_attr = 1;
3811 *alignment = 0;
3813 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3814 pass == PASS_RETURN)
3816 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3818 *alignment = 4;
3819 return 20;
3822 if (!is_string_type(var->attrs, var->type))
3823 return get_required_buffer_size_type(var->type, var->name,
3824 var->attrs, TRUE, alignment);
3826 return 0;
3829 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3831 const var_t *var;
3832 unsigned int total_size = 0, alignment;
3834 if (type_get_function_args(func->type))
3836 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3838 total_size += get_required_buffer_size(var, &alignment, pass);
3839 total_size += alignment;
3843 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3845 var_t v = *func;
3846 v.type = type_function_get_rettype(func->type);
3847 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3848 total_size += alignment;
3850 return total_size;
3853 static void print_phase_function(FILE *file, int indent, const char *type,
3854 const char *local_var_prefix, enum remoting_phase phase,
3855 const var_t *var, unsigned int type_offset)
3857 const char *function;
3858 switch (phase)
3860 case PHASE_BUFFERSIZE:
3861 function = "BufferSize";
3862 break;
3863 case PHASE_MARSHAL:
3864 function = "Marshall";
3865 break;
3866 case PHASE_UNMARSHAL:
3867 function = "Unmarshall";
3868 break;
3869 case PHASE_FREE:
3870 function = "Free";
3871 break;
3872 default:
3873 assert(0);
3874 return;
3877 print_file(file, indent, "Ndr%s%s(\n", type, function);
3878 indent++;
3879 print_file(file, indent, "&__frame->_StubMsg,\n");
3880 print_file(file, indent, "%s%s%s%s%s,\n",
3881 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3882 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3883 local_var_prefix,
3884 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3885 var->name);
3886 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3887 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3888 if (phase == PHASE_UNMARSHAL)
3889 print_file(file, indent, "0);\n");
3890 indent--;
3893 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3894 enum remoting_phase phase, enum pass pass, const var_t *var,
3895 const char *varname)
3897 type_t *type = var->type;
3898 unsigned int alignment = 0;
3900 /* no work to do for other phases, buffer sizing is done elsewhere */
3901 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3902 return;
3904 if (type_get_type(type) == TYPE_ENUM ||
3905 (type_get_type(type) == TYPE_BASIC &&
3906 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3907 pointer_size != 4))
3909 unsigned char fc;
3911 if (type_get_type(type) == TYPE_ENUM)
3912 fc = get_enum_fc(type);
3913 else
3914 fc = get_basic_fc(type);
3916 if (phase == PHASE_MARSHAL)
3917 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3918 else
3919 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3920 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3921 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3922 local_var_prefix,
3923 var->name);
3924 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3926 else
3928 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3929 switch (get_basic_fc(ref))
3931 case RPC_FC_BYTE:
3932 case RPC_FC_CHAR:
3933 case RPC_FC_SMALL:
3934 case RPC_FC_USMALL:
3935 alignment = 1;
3936 break;
3938 case RPC_FC_WCHAR:
3939 case RPC_FC_USHORT:
3940 case RPC_FC_SHORT:
3941 alignment = 2;
3942 break;
3944 case RPC_FC_ULONG:
3945 case RPC_FC_LONG:
3946 case RPC_FC_FLOAT:
3947 case RPC_FC_ERROR_STATUS_T:
3948 /* pointer_size must be 4 if we got here in these two cases */
3949 case RPC_FC_INT3264:
3950 case RPC_FC_UINT3264:
3951 alignment = 4;
3952 break;
3954 case RPC_FC_HYPER:
3955 case RPC_FC_DOUBLE:
3956 alignment = 8;
3957 break;
3959 case RPC_FC_IGNORE:
3960 case RPC_FC_BIND_PRIMITIVE:
3961 /* no marshalling needed */
3962 return;
3964 default:
3965 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3966 var->name, get_basic_fc(ref));
3969 if (phase == PHASE_MARSHAL && alignment > 1)
3970 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3971 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3972 alignment - 1, alignment - 1);
3974 if (phase == PHASE_MARSHAL)
3976 print_file(file, indent, "*(");
3977 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3978 if (is_ptr(type))
3979 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3980 else
3981 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3982 fprintf(file, "%s%s", local_var_prefix, varname);
3983 fprintf(file, ";\n");
3985 else if (phase == PHASE_UNMARSHAL)
3987 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3988 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3989 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3990 print_file(file, indent, "{\n");
3991 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3992 print_file(file, indent, "}\n");
3993 print_file(file, indent, "%s%s%s",
3994 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3995 local_var_prefix, varname);
3996 if (pass == PASS_IN && is_ptr(type))
3997 fprintf(file, " = (");
3998 else
3999 fprintf(file, " = *(");
4000 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4001 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4004 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4005 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4006 fprintf(file, ");\n");
4010 /* returns whether the MaxCount, Offset or ActualCount members need to be
4011 * filled in for the specified phase */
4012 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4014 return (phase != PHASE_UNMARSHAL);
4017 expr_t *get_size_is_expr(const type_t *t, const char *name)
4019 expr_t *x = NULL;
4021 for ( ; is_array(t); t = type_array_get_element(t))
4022 if (type_array_has_conformance(t) &&
4023 type_array_get_conformance(t)->type != EXPR_VOID)
4025 if (!x)
4026 x = type_array_get_conformance(t);
4027 else
4028 error("%s: multidimensional conformant"
4029 " arrays not supported at the top level\n",
4030 name);
4033 return x;
4036 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4037 enum remoting_phase phase, const var_t *var, int valid_variance)
4039 const type_t *type = var->type;
4040 /* get fundamental type for the argument */
4041 for (;;)
4043 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4045 case TGT_ARRAY:
4046 if (is_conformance_needed_for_phase(phase))
4048 if (type_array_has_conformance(type) &&
4049 type_array_get_conformance(type)->type != EXPR_VOID)
4051 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4052 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4053 fprintf(file, ";\n\n");
4055 if (type_array_has_variance(type))
4057 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4058 if (valid_variance)
4060 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4061 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4062 fprintf(file, ";\n\n");
4064 else
4065 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4068 break;
4069 case TGT_UNION:
4070 if (type_get_type(type) == TYPE_UNION &&
4071 is_conformance_needed_for_phase(phase))
4073 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4074 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4075 fprintf(file, ";\n\n");
4077 break;
4078 case TGT_IFACE_POINTER:
4080 expr_t *iid;
4082 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4084 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4085 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4086 fprintf( file, ";\n\n" );
4088 break;
4090 case TGT_POINTER:
4091 type = type_pointer_get_ref(type);
4092 continue;
4093 case TGT_INVALID:
4094 case TGT_USER_TYPE:
4095 case TGT_CTXT_HANDLE:
4096 case TGT_CTXT_HANDLE_POINTER:
4097 case TGT_STRING:
4098 case TGT_BASIC:
4099 case TGT_ENUM:
4100 case TGT_STRUCT:
4101 case TGT_RANGE:
4102 break;
4104 break;
4108 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4109 enum pass pass, enum remoting_phase phase, const var_t *var)
4111 int in_attr, out_attr, pointer_type;
4112 const char *type_str = NULL;
4113 const type_t *type = var->type;
4114 unsigned int alignment, start_offset = type->typestring_offset;
4116 if (is_ptr(type) || is_array(type))
4117 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4118 else
4119 pointer_type = 0;
4121 in_attr = is_attr(var->attrs, ATTR_IN);
4122 out_attr = is_attr(var->attrs, ATTR_OUT);
4123 if (!in_attr && !out_attr)
4124 in_attr = 1;
4126 if (phase != PHASE_FREE)
4127 switch (pass)
4129 case PASS_IN:
4130 if (!in_attr) return;
4131 break;
4132 case PASS_OUT:
4133 if (!out_attr) return;
4134 break;
4135 case PASS_RETURN:
4136 break;
4139 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4141 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4143 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4145 case TGT_CTXT_HANDLE:
4146 case TGT_CTXT_HANDLE_POINTER:
4147 if (phase == PHASE_MARSHAL)
4149 if (pass == PASS_IN)
4151 /* if the context_handle attribute appears in the chain of types
4152 * without pointers being followed, then the context handle must
4153 * be direct, otherwise it is a pointer */
4154 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
4155 print_file(file, indent, "NdrClientContextMarshall(\n");
4156 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4157 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
4158 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4160 else
4162 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4163 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4164 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4165 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4166 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4169 else if (phase == PHASE_UNMARSHAL)
4171 if (pass == PASS_OUT)
4173 if (!in_attr)
4174 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4175 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4176 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4177 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4178 print_file(file, indent + 1, "__frame->_Handle);\n");
4180 else
4182 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4183 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4184 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4187 break;
4188 case TGT_USER_TYPE:
4189 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4190 break;
4191 case TGT_STRING:
4192 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4193 pointer_type != RPC_FC_RP)
4195 /* strings returned are assumed to be global and hence don't
4196 * need freeing */
4197 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4198 print_phase_function(file, indent, "Pointer", local_var_prefix,
4199 phase, var, start_offset);
4200 else if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4201 !in_attr && is_conformant_array(type))
4203 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4204 indent++;
4205 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4208 else
4210 unsigned int real_start_offset = start_offset;
4211 /* skip over pointer description straight to string description */
4212 if (is_declptr(type))
4214 if (is_conformant_array(type))
4215 real_start_offset += 4;
4216 else
4217 real_start_offset += 2;
4219 if (is_array(type) && !is_conformant_array(type))
4220 print_phase_function(file, indent, "NonConformantString",
4221 local_var_prefix, phase, var,
4222 real_start_offset);
4223 else
4224 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4225 phase, var, real_start_offset);
4227 break;
4228 case TGT_ARRAY:
4230 unsigned char tc = get_array_fc(type);
4231 const char *array_type = NULL;
4233 /* We already have the size_is expression since it's at the
4234 top level, but do checks for multidimensional conformant
4235 arrays. When we handle them, we'll need to extend this
4236 function to return a list, and then we'll actually use
4237 the return value. */
4238 get_size_is_expr(type, var->name);
4240 switch (tc)
4242 case RPC_FC_SMFARRAY:
4243 case RPC_FC_LGFARRAY:
4244 array_type = "FixedArray";
4245 break;
4246 case RPC_FC_SMVARRAY:
4247 case RPC_FC_LGVARRAY:
4248 array_type = "VaryingArray";
4249 break;
4250 case RPC_FC_CARRAY:
4251 array_type = "ConformantArray";
4252 break;
4253 case RPC_FC_CVARRAY:
4254 array_type = "ConformantVaryingArray";
4255 break;
4256 case RPC_FC_BOGUS_ARRAY:
4257 array_type = "ComplexArray";
4258 break;
4261 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4263 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4265 /* these are all unmarshalled by allocating memory */
4266 if (tc == RPC_FC_BOGUS_ARRAY ||
4267 tc == RPC_FC_CVARRAY ||
4268 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4269 (tc == RPC_FC_CARRAY && !in_attr))
4271 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff)
4273 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4274 type->details.array.ptr_tfsoff);
4275 break;
4277 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4278 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4279 indent++;
4280 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4281 break;
4284 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4285 break;
4287 case TGT_BASIC:
4288 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4289 break;
4290 case TGT_ENUM:
4291 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4292 break;
4293 case TGT_RANGE:
4294 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4295 /* Note: this goes beyond what MIDL does - it only supports arguments
4296 * with the [range] attribute in Oicf mode */
4297 if (phase == PHASE_UNMARSHAL)
4299 const expr_t *range_min;
4300 const expr_t *range_max;
4301 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4302 if (!range_list)
4303 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4304 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4305 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4307 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4308 write_type_decl(file, var->type, NULL);
4309 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4310 write_type_decl(file, var->type, NULL);
4311 fprintf(file, ")0x%x))\n", range_max->cval);
4312 print_file(file, indent, "{\n");
4313 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4314 print_file(file, indent, "}\n");
4316 break;
4317 case TGT_STRUCT:
4318 switch (get_struct_fc(type))
4320 case RPC_FC_STRUCT:
4321 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4322 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4323 break;
4324 case RPC_FC_PSTRUCT:
4325 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4326 break;
4327 case RPC_FC_CSTRUCT:
4328 case RPC_FC_CPSTRUCT:
4329 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4330 break;
4331 case RPC_FC_CVSTRUCT:
4332 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4333 break;
4334 case RPC_FC_BOGUS_STRUCT:
4335 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4336 break;
4337 default:
4338 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4340 break;
4341 case TGT_UNION:
4343 const char *union_type = NULL;
4345 if (type_get_type(type) == TYPE_UNION)
4346 union_type = "NonEncapsulatedUnion";
4347 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4348 union_type = "EncapsulatedUnion";
4350 print_phase_function(file, indent, union_type, local_var_prefix,
4351 phase, var, start_offset);
4352 break;
4354 case TGT_POINTER:
4356 const type_t *ref = type_pointer_get_ref(type);
4357 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4359 case TGT_BASIC:
4360 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4361 break;
4362 case TGT_ENUM:
4363 /* base types have known sizes, so don't need a sizing pass
4364 * and don't have any memory to free and so don't need a
4365 * freeing pass */
4366 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4367 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4368 break;
4369 case TGT_STRUCT:
4370 switch (get_struct_fc(ref))
4372 case RPC_FC_STRUCT:
4373 /* simple structs have known sizes, so don't need a sizing
4374 * pass and don't have any memory to free and so don't
4375 * need a freeing pass */
4376 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4377 type_str = "SimpleStruct";
4378 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4380 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4381 indent++;
4382 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4383 indent--;
4385 break;
4386 case RPC_FC_PSTRUCT:
4387 type_str = "SimpleStruct";
4388 break;
4389 case RPC_FC_CSTRUCT:
4390 case RPC_FC_CPSTRUCT:
4391 type_str = "ConformantStruct";
4392 break;
4393 case RPC_FC_CVSTRUCT:
4394 type_str = "ConformantVaryingStruct";
4395 break;
4396 case RPC_FC_BOGUS_STRUCT:
4397 type_str = "ComplexStruct";
4398 break;
4399 default:
4400 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4403 if (type_str)
4405 if (phase == PHASE_FREE)
4406 type_str = "Pointer";
4407 else
4408 start_offset = ref->typestring_offset;
4409 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4411 break;
4412 case TGT_UNION:
4413 if (phase == PHASE_FREE)
4414 type_str = "Pointer";
4415 else
4417 if (type_get_type(ref) == TYPE_UNION)
4418 type_str = "NonEncapsulatedUnion";
4419 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4420 type_str = "EncapsulatedUnion";
4422 start_offset = ref->typestring_offset;
4425 print_phase_function(file, indent, type_str, local_var_prefix,
4426 phase, var, start_offset);
4427 break;
4428 case TGT_USER_TYPE:
4429 if (phase != PHASE_FREE)
4431 type_str = "UserMarshal";
4432 start_offset = ref->typestring_offset;
4434 else type_str = "Pointer";
4436 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4437 break;
4438 case TGT_STRING:
4439 case TGT_POINTER:
4440 case TGT_ARRAY:
4441 case TGT_RANGE:
4442 case TGT_IFACE_POINTER:
4443 case TGT_CTXT_HANDLE:
4444 case TGT_CTXT_HANDLE_POINTER:
4445 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4446 break;
4447 case TGT_INVALID:
4448 assert(0);
4449 break;
4451 else
4452 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4453 break;
4455 case TGT_IFACE_POINTER:
4456 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4457 break;
4458 case TGT_INVALID:
4459 assert(0);
4460 break;
4462 fprintf(file, "\n");
4465 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4466 enum pass pass, enum remoting_phase phase)
4468 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4470 unsigned int size = get_function_buffer_size( func, pass );
4471 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4474 if (pass == PASS_RETURN)
4476 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4477 type_function_get_retval(func->type) );
4479 else
4481 const var_t *var;
4482 if (!type_get_function_args(func->type))
4483 return;
4484 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4485 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4490 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4492 unsigned int offset = 0;
4493 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4494 return offset;
4497 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4499 const statement_t *stmt;
4500 unsigned int size = 1;
4502 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4504 const type_t *iface;
4505 const statement_t *stmt_func;
4507 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4508 continue;
4510 iface = stmt->u.type;
4511 if (!pred(iface))
4512 continue;
4514 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4516 const var_t *func = stmt_func->u.var;
4517 if (!is_local(func->attrs))
4518 size += get_size_procformatstring_func( iface, func );
4521 return size;
4524 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4526 set_all_tfswrite(FALSE);
4527 return process_tfs(NULL, stmts, pred);
4530 void declare_stub_args( FILE *file, int indent, const var_t *func )
4532 int in_attr, out_attr;
4533 int i = 0;
4534 const var_t *var = type_function_get_retval(func->type);
4536 /* declare return value */
4537 if (!is_void(var->type))
4539 print_file(file, indent, "%s", "");
4540 write_type_decl(file, var->type, var->name);
4541 fprintf(file, ";\n");
4544 if (!type_get_function_args(func->type))
4545 return;
4547 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4549 in_attr = is_attr(var->attrs, ATTR_IN);
4550 out_attr = is_attr(var->attrs, ATTR_OUT);
4551 if (!out_attr && !in_attr)
4552 in_attr = 1;
4554 if (is_context_handle(var->type))
4555 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4556 else
4558 if (!in_attr && !is_conformant_array(var->type))
4560 type_t *type_to_print;
4561 char name[16];
4562 print_file(file, indent, "%s", "");
4563 if (type_get_type(var->type) == TYPE_ARRAY &&
4564 !type_array_is_decl_as_ptr(var->type))
4565 type_to_print = var->type;
4566 else
4567 type_to_print = type_pointer_get_ref(var->type);
4568 sprintf(name, "_W%u", i++);
4569 write_type_decl(file, type_to_print, name);
4570 fprintf(file, ";\n");
4573 print_file(file, indent, "%s", "");
4574 write_type_decl_left(file, var->type);
4575 fprintf(file, " ");
4576 if (type_get_type(var->type) == TYPE_ARRAY &&
4577 !type_array_is_decl_as_ptr(var->type)) {
4578 fprintf(file, "(*%s)", var->name);
4579 } else
4580 fprintf(file, "%s", var->name);
4581 write_type_right(file, var->type, FALSE);
4582 fprintf(file, ";\n");
4584 if (decl_indirect(var->type))
4585 print_file(file, indent, "void *_p_%s;\n", var->name);
4591 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4593 int in_attr, out_attr;
4594 int i = 0, sep = 0;
4595 const var_t *var;
4597 if (!type_get_function_args(func->type))
4598 return;
4600 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4602 in_attr = is_attr(var->attrs, ATTR_IN);
4603 out_attr = is_attr(var->attrs, ATTR_OUT);
4604 if (!out_attr && !in_attr)
4605 in_attr = 1;
4607 if (!in_attr)
4609 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4611 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4613 case TGT_CTXT_HANDLE_POINTER:
4614 fprintf(file, " = NdrContextHandleInitialize(\n");
4615 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4616 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4617 var->type->typestring_offset);
4618 break;
4619 case TGT_ARRAY:
4620 if (type_array_has_conformance(var->type))
4622 unsigned int size;
4623 type_t *type;
4625 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4626 for (type = var->type;
4627 is_array(type) && type_array_has_conformance(type);
4628 type = type_array_get_element(type))
4630 write_expr(file, type_array_get_conformance(type), TRUE,
4631 TRUE, NULL, NULL, local_var_prefix);
4632 fprintf(file, " * ");
4634 size = type_memsize(type);
4635 fprintf(file, "%u);\n", size);
4637 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4638 for (type = var->type;
4639 is_array(type) && type_array_has_conformance(type);
4640 type = type_array_get_element(type))
4642 write_expr(file, type_array_get_conformance(type), TRUE,
4643 TRUE, NULL, NULL, local_var_prefix);
4644 fprintf(file, " * ");
4646 size = type_memsize(type);
4647 fprintf(file, "%u);\n", size);
4649 else
4650 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4651 break;
4652 case TGT_POINTER:
4653 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4654 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
4656 case TGT_BASIC:
4657 case TGT_ENUM:
4658 case TGT_POINTER:
4659 case TGT_RANGE:
4660 case TGT_IFACE_POINTER:
4661 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4662 break;
4663 case TGT_USER_TYPE:
4664 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4665 local_var_prefix, i, local_var_prefix, i);
4666 break;
4667 case TGT_STRUCT:
4668 case TGT_UNION:
4669 case TGT_ARRAY:
4670 case TGT_CTXT_HANDLE:
4671 case TGT_CTXT_HANDLE_POINTER:
4672 case TGT_INVALID:
4673 case TGT_STRING:
4674 /* not initialised */
4675 break;
4677 i++;
4678 break;
4679 default:
4680 break;
4683 sep = 1;
4686 if (sep)
4687 fprintf(file, "\n");
4691 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4692 const char *var_decl, int add_retval )
4694 var_t *retval = type_function_get_retval( func );
4695 const var_list_t *args = type_get_function_args( func );
4696 const var_t *arg;
4697 int needs_packing;
4698 unsigned int align = 0;
4700 if (args)
4701 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4702 if (!is_array( arg->type )) type_memsize_and_alignment( arg->type, &align );
4704 needs_packing = (align > pointer_size);
4706 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4707 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4708 print_file(file, 1, "{\n" );
4709 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4711 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4713 print_file(file, 2, "%s", "");
4714 write_type_left( file, (type_t *)arg->type, TRUE );
4715 if (needs_space_after( arg->type )) fputc( ' ', file );
4716 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4718 /* FIXME: should check for large args being passed by pointer */
4719 align = 0;
4720 if (is_array( arg->type ) || is_ptr( arg->type )) align = pointer_size;
4721 else type_memsize_and_alignment( arg->type, &align );
4723 if (align >= pointer_size)
4724 fprintf( file, "%s;\n", arg->name );
4725 else
4726 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4728 if (add_retval && !is_void( retval->type ))
4730 print_file(file, 2, "%s", "");
4731 write_type_decl( file, retval->type, retval->name );
4732 if (is_array( retval->type ) || is_ptr( retval->type ) ||
4733 type_memsize( retval->type ) == pointer_size)
4734 fprintf( file, ";\n" );
4735 else
4736 fprintf( file, " DECLSPEC_ALIGN(%u);\n", pointer_size );
4738 print_file(file, 1, "} %s;\n", var_decl );
4739 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4740 print_file( file, 0, "\n" );
4743 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4745 const var_list_t *args = type_get_function_args( func->type );
4746 const var_t *var;
4748 if (!args) return;
4750 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4751 if (cant_be_null( var ))
4752 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4755 int write_expr_eval_routines(FILE *file, const char *iface)
4757 static const char *var_name = "pS";
4758 static const char *var_name_expr = "pS->";
4759 int result = 0;
4760 struct expr_eval_routine *eval;
4761 unsigned short callback_offset = 0;
4763 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4765 const char *name = eval->name;
4766 result = 1;
4768 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4769 eval->iface ? eval->iface->name : iface, name, callback_offset);
4770 print_file(file, 0, "{\n");
4771 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4773 write_func_param_struct( file, eval->iface, eval->cont_type,
4774 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4776 else
4778 print_file(file, 1, "%s", "");
4779 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4780 fprintf(file, " *%s = (", var_name);
4781 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4782 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4784 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4785 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4786 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4787 fprintf(file, ";\n");
4788 print_file(file, 0, "}\n\n");
4789 callback_offset++;
4791 return result;
4794 void write_expr_eval_routine_list(FILE *file, const char *iface)
4796 struct expr_eval_routine *eval;
4797 struct expr_eval_routine *cursor;
4798 unsigned short callback_offset = 0;
4800 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4801 fprintf(file, "{\n");
4803 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4805 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4806 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4807 callback_offset++;
4808 list_remove(&eval->entry);
4809 free(eval->name);
4810 free(eval);
4813 fprintf(file, "};\n\n");
4816 void write_user_quad_list(FILE *file)
4818 user_type_t *ut;
4820 if (list_empty(&user_type_list))
4821 return;
4823 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4824 fprintf(file, "{\n");
4825 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4827 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4828 print_file(file, 1, "{\n");
4829 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4830 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4831 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4832 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4833 print_file(file, 1, "}%s\n", sep);
4835 fprintf(file, "};\n\n");
4838 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4840 const struct str_list_entry_t *endpoint;
4841 const char *p;
4843 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4844 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4845 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4847 print_file( f, 1, "{ (const unsigned char *)\"" );
4848 for (p = endpoint->str; *p && *p != ':'; p++)
4850 if (*p == '"' || *p == '\\') fputc( '\\', f );
4851 fputc( *p, f );
4853 if (!*p) goto error;
4854 if (p[1] != '[') goto error;
4856 fprintf( f, "\", (const unsigned char *)\"" );
4857 for (p += 2; *p && *p != ']'; p++)
4859 if (*p == '"' || *p == '\\') fputc( '\\', f );
4860 fputc( *p, f );
4862 if (*p != ']') goto error;
4863 fprintf( f, "\" },\n" );
4865 print_file( f, 0, "};\n\n" );
4866 return;
4868 error:
4869 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4872 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4873 const char *prefix, unsigned int proc_offset )
4875 type_t *rettype = type_function_get_rettype( func->type );
4876 int has_ret = !is_void( rettype );
4877 const var_list_t *args = type_get_function_args( func->type );
4878 const var_t *arg;
4879 int len, needs_params = 0;
4881 /* we need a param structure if we have more than one arg */
4882 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4884 print_file( file, 0, "{\n");
4885 if (needs_params)
4887 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4888 write_func_param_struct( file, iface, func->type, "__params", FALSE );
4889 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4890 if (args)
4891 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4892 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4894 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4896 len = fprintf( file, " %s%s( ",
4897 has_ret ? "_RetVal = " : "",
4898 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
4899 fprintf( file, "&%s_StubDesc,", prefix );
4900 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
4901 if (needs_params)
4903 fprintf( file, ",\n%*s&__params", len, "" );
4905 else if (pointer_size == 8)
4907 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
4908 if (args)
4909 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4910 fprintf( file, ",\n%*s%s", len, "", arg->name );
4912 else
4914 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
4915 else if (args)
4917 arg = LIST_ENTRY( list_head(args), const var_t, entry );
4918 fprintf( file, ",\n%*s&%s", len, "", arg->name );
4921 fprintf( file, " );\n" );
4922 if (has_ret)
4924 print_file( file, 1, "return (" );
4925 write_type_decl_left(file, rettype);
4926 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
4928 print_file( file, 0, "}\n\n");
4931 void write_exceptions( FILE *file )
4933 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4934 fprintf( file, "\n");
4935 fprintf( file, "#include \"wine/exception.h\"\n");
4936 fprintf( file, "#undef RpcTryExcept\n");
4937 fprintf( file, "#undef RpcExcept\n");
4938 fprintf( file, "#undef RpcEndExcept\n");
4939 fprintf( file, "#undef RpcTryFinally\n");
4940 fprintf( file, "#undef RpcFinally\n");
4941 fprintf( file, "#undef RpcEndFinally\n");
4942 fprintf( file, "#undef RpcExceptionCode\n");
4943 fprintf( file, "#undef RpcAbnormalTermination\n");
4944 fprintf( file, "\n");
4945 fprintf( file, "struct __exception_frame;\n");
4946 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4947 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4948 fprintf( file, "\n");
4949 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4950 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4951 fprintf( file, " __filter_func filter; \\\n");
4952 fprintf( file, " __finally_func finally; \\\n");
4953 fprintf( file, " sigjmp_buf jmp; \\\n");
4954 fprintf( file, " DWORD code; \\\n");
4955 fprintf( file, " unsigned char abnormal_termination; \\\n");
4956 fprintf( file, " unsigned char filter_level; \\\n");
4957 fprintf( file, " unsigned char finally_level;\n");
4958 fprintf( file, "\n");
4959 fprintf( file, "struct __exception_frame\n{\n");
4960 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4961 fprintf( file, "};\n");
4962 fprintf( file, "\n");
4963 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4964 fprintf( file, "{\n");
4965 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4966 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4967 fprintf( file, " {\n");
4968 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4969 fprintf( file, " exc_frame->finally( exc_frame );\n");
4970 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4971 fprintf( file, " }\n");
4972 fprintf( file, " exc_frame->filter_level = 0;\n");
4973 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4974 fprintf( file, "}\n");
4975 fprintf( file, "\n");
4976 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4977 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4978 fprintf( file, " CONTEXT *context,\n");
4979 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4980 fprintf( file, "{\n");
4981 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4982 fprintf( file, "\n");
4983 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4984 fprintf( file, " {\n" );
4985 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4986 fprintf( file, " {\n" );
4987 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4988 fprintf( file, " exc_frame->finally( exc_frame );\n");
4989 fprintf( file, " }\n" );
4990 fprintf( file, " return ExceptionContinueSearch;\n");
4991 fprintf( file, " }\n" );
4992 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4993 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4994 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4995 fprintf( file, " return ExceptionContinueSearch;\n");
4996 fprintf( file, "}\n");
4997 fprintf( file, "\n");
4998 fprintf( file, "#define RpcTryExcept \\\n");
4999 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
5000 fprintf( file, " { \\\n");
5001 fprintf( file, " if (!__frame->finally_level) \\\n" );
5002 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5003 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5004 fprintf( file, "\n");
5005 fprintf( file, "#define RpcExcept(expr) \\\n");
5006 fprintf( file, " if (!__frame->finally_level) \\\n" );
5007 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5008 fprintf( file, " __frame->filter_level = 0; \\\n" );
5009 fprintf( file, " } \\\n");
5010 fprintf( file, " else \\\n");
5011 fprintf( file, "\n");
5012 fprintf( file, "#define RpcEndExcept\n");
5013 fprintf( file, "\n");
5014 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5015 fprintf( file, "\n");
5016 fprintf( file, "#define RpcTryFinally \\\n");
5017 fprintf( file, " if (!__frame->filter_level) \\\n");
5018 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5019 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5020 fprintf( file, "\n");
5021 fprintf( file, "#define RpcFinally \\\n");
5022 fprintf( file, " if (!__frame->filter_level) \\\n");
5023 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5024 fprintf( file, " __frame->finally_level = 0;\n");
5025 fprintf( file, "\n");
5026 fprintf( file, "#define RpcEndFinally\n");
5027 fprintf( file, "\n");
5028 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5029 fprintf( file, "\n");
5030 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5031 fprintf( file, " do { \\\n");
5032 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5033 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5034 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5035 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5036 fprintf( file, " __frame->filter_level = 0; \\\n");
5037 fprintf( file, " __frame->finally_level = 0; \\\n");
5038 fprintf( file, " } while (0)\n");
5039 fprintf( file, "\n");
5040 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5041 fprintf( file, "\n");
5042 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5043 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5044 fprintf( file, "\n");
5045 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5046 fprintf( file, " DWORD code;\n");
5047 fprintf( file, "\n");
5048 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");