d3d9: Get rid of IDirect3DVertexDeclaration9Impl.
[wine/multimedia.git] / tools / widl / typegen.c
blob516d8e7bbe8cbb6f25aeddae24f21f3d26b51a20
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 var_t *var, int *by_value )
418 unsigned int stack_size;
419 int by_val;
421 switch (typegen_detect_type( var->type, var->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( var->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 var_t *var, int is_return, unsigned short *flags,
956 unsigned int *stack_size, unsigned int *typestring_offset )
958 unsigned int alignment, server_size = 0, buffer_size = 0;
959 unsigned char fc = 0;
960 int is_byval;
961 int is_in = is_attr(var->attrs, ATTR_IN);
962 int is_out = is_attr(var->attrs, ATTR_OUT);
964 if (is_return) is_out = TRUE;
965 else if (!is_in && !is_out) is_in = TRUE;
967 *flags = 0;
968 *stack_size = get_stack_size( var, &is_byval );
969 *typestring_offset = var->typestring_offset;
971 if (is_in) *flags |= IsIn;
972 if (is_out) *flags |= IsOut;
973 if (is_return) *flags |= IsReturn;
975 if (!is_string_type( var->attrs, var->type ))
976 buffer_size = get_required_buffer_size_type( var->type, NULL, var->attrs, TRUE, &alignment );
978 switch (typegen_detect_type( var->type, var->attrs, TDT_ALL_TYPES ))
980 case TGT_BASIC:
981 *flags |= IsBasetype;
982 fc = get_basic_fc_signed( var->type );
983 if (fc == RPC_FC_BIND_PRIMITIVE)
985 buffer_size = 4; /* actually 0 but avoids setting MustSize */
986 fc = RPC_FC_LONG;
988 break;
989 case TGT_ENUM:
990 *flags |= IsBasetype;
991 fc = get_enum_fc( var->type );
992 break;
993 case TGT_RANGE:
994 *flags |= IsByValue;
995 break;
996 case TGT_STRUCT:
997 case TGT_UNION:
998 case TGT_USER_TYPE:
999 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1000 break;
1001 case TGT_IFACE_POINTER:
1002 *flags |= MustFree;
1003 break;
1004 case TGT_ARRAY:
1005 *flags |= MustFree;
1006 if (type_array_is_decl_as_ptr(var->type) && var->type->details.array.ptr_tfsoff &&
1007 get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1009 *typestring_offset = var->type->typestring_offset;
1010 *flags |= IsSimpleRef;
1012 break;
1013 case TGT_STRING:
1014 *flags |= MustFree;
1015 if (is_declptr( var->type ) && get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1017 /* skip over pointer description straight to string description */
1018 if (is_conformant_array( var->type )) *typestring_offset += 4;
1019 else *typestring_offset += 2;
1020 *flags |= IsSimpleRef;
1022 break;
1023 case TGT_CTXT_HANDLE_POINTER:
1024 *flags |= IsSimpleRef;
1025 *typestring_offset += 4;
1026 /* fall through */
1027 case TGT_CTXT_HANDLE:
1028 buffer_size = 20;
1029 break;
1030 case TGT_POINTER:
1031 if (get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1033 const type_t *ref = type_pointer_get_ref( var->type );
1035 if (!is_string_type( var->attrs, ref ))
1036 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1038 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1040 case TGT_BASIC:
1041 *flags |= IsSimpleRef | IsBasetype;
1042 fc = get_basic_fc( ref );
1043 if (!is_in && is_out) server_size = pointer_size;
1044 break;
1045 case TGT_ENUM:
1046 if ((fc = get_enum_fc( ref )) == RPC_FC_ENUM32)
1048 *flags |= IsSimpleRef | IsBasetype;
1049 if (!is_in && is_out) server_size = pointer_size;
1051 else
1053 server_size = pointer_size;
1055 break;
1056 case TGT_UNION:
1057 case TGT_USER_TYPE:
1058 case TGT_RANGE:
1059 *flags |= IsSimpleRef | MustFree;
1060 *typestring_offset = ref->typestring_offset;
1061 if (!is_in && is_out) server_size = type_memsize( ref );
1062 break;
1063 case TGT_STRING:
1064 case TGT_POINTER:
1065 case TGT_ARRAY:
1066 case TGT_CTXT_HANDLE:
1067 case TGT_CTXT_HANDLE_POINTER:
1068 *flags |= MustFree;
1069 server_size = pointer_size;
1070 break;
1071 case TGT_IFACE_POINTER:
1072 *flags |= MustFree;
1073 if (is_in && is_out) server_size = pointer_size;
1074 break;
1075 case TGT_STRUCT:
1076 *flags |= IsSimpleRef | MustFree;
1077 *typestring_offset = ref->typestring_offset;
1078 switch (get_struct_fc(ref))
1080 case RPC_FC_STRUCT:
1081 case RPC_FC_PSTRUCT:
1082 case RPC_FC_BOGUS_STRUCT:
1083 if (!is_in && is_out) server_size = type_memsize( ref );
1084 break;
1085 default:
1086 break;
1088 break;
1089 case TGT_INVALID:
1090 assert(0);
1093 else /* not ref pointer */
1095 *flags |= MustFree;
1097 break;
1098 case TGT_INVALID:
1099 assert(0);
1102 if (!buffer_size) *flags |= MustSize;
1104 if (server_size)
1106 server_size = (server_size + 7) / 8;
1107 if (server_size < 8) *flags |= server_size << 13;
1109 return fc;
1112 static unsigned char get_func_oi2_flags( const var_t *func )
1114 const var_t *var;
1115 var_list_t *args = type_get_function_args( func->type );
1116 var_t *retval = type_function_get_retval( func->type );
1117 unsigned char oi2_flags = 0x40; /* HasExtensions */
1118 unsigned short flags;
1119 unsigned int stack_size, typestring_offset;
1121 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1123 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1124 if (flags & MustSize)
1126 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1127 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1131 if (!is_void( retval->type ))
1133 oi2_flags |= 0x04; /* HasRet */
1134 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1135 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1137 return oi2_flags;
1140 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
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( var, 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, const var_t *var,
1176 int is_return, int is_interpreted)
1178 unsigned int size;
1180 int is_in = is_attr(var->attrs, ATTR_IN);
1181 int is_out = is_attr(var->attrs, ATTR_OUT);
1183 if (!is_in && !is_out) is_in = TRUE;
1185 if (type_get_type(var->type) == TYPE_BASIC ||
1186 type_get_type(var->type) == TYPE_ENUM)
1188 unsigned char fc;
1190 if (is_return)
1191 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1192 else
1193 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1195 if (type_get_type(var->type) == TYPE_ENUM)
1197 fc = get_enum_fc(var->type);
1199 else
1201 fc = get_basic_fc_signed(var->type);
1203 if (fc == RPC_FC_BIND_PRIMITIVE)
1204 fc = RPC_FC_IGNORE;
1207 print_file(file, indent, "0x%02x, /* %s */\n",
1208 fc, string_of_type(fc));
1209 size = 2; /* includes param type prefix */
1211 else
1213 unsigned short offset = var->typestring_offset;
1215 if (!is_interpreted && is_array(var->type) &&
1216 type_array_is_decl_as_ptr(var->type) &&
1217 var->type->details.array.ptr_tfsoff)
1218 offset = var->type->typestring_offset;
1220 if (is_return)
1221 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1222 else if (is_in && is_out)
1223 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1224 else if (is_out)
1225 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1226 else
1227 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1229 size = get_stack_size( var, NULL );
1230 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1231 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1232 size = 4; /* includes param type prefix */
1234 return size;
1237 int is_interpreted_func( const type_t *iface, const var_t *func )
1239 const char *str;
1240 const var_t *var;
1241 const var_list_t *args = type_get_function_args( func->type );
1242 const type_t *ret_type = type_function_get_rettype( func->type );
1244 if (type_get_type( ret_type ) == TYPE_BASIC)
1246 switch (type_basic_get_type( ret_type ))
1248 case TYPE_BASIC_INT64:
1249 case TYPE_BASIC_HYPER:
1250 /* return value must fit in a long_ptr */
1251 if (pointer_size < 8) return 0;
1252 break;
1253 case TYPE_BASIC_FLOAT:
1254 case TYPE_BASIC_DOUBLE:
1255 /* floating point values can't be returned */
1256 return 0;
1257 default:
1258 break;
1261 if (get_stub_mode() != MODE_Oif && args)
1263 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1264 switch (type_get_type( var->type ))
1266 case TYPE_BASIC:
1267 switch (type_basic_get_type( var->type ))
1269 /* floating point arguments are not supported in Oi mode */
1270 case TYPE_BASIC_FLOAT: return 0;
1271 case TYPE_BASIC_DOUBLE: return 0;
1272 default: break;
1274 break;
1275 /* unions passed by value are not supported in Oi mode */
1276 case TYPE_UNION: return 0;
1277 case TYPE_ENCAPSULATED_UNION: return 0;
1278 default: break;
1282 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1283 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1284 return (get_stub_mode() != MODE_Os);
1287 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1288 const var_t *func, unsigned int *offset,
1289 unsigned short num_proc )
1291 var_t *var;
1292 var_list_t *args = type_get_function_args( func->type );
1293 unsigned char explicit_fc, implicit_fc;
1294 unsigned char handle_flags;
1295 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1296 unsigned char oi_flags = RPC_FC_PROC_OIF_RPCFLAGS | RPC_FC_PROC_OIF_NEWINIT;
1297 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1298 unsigned int nb_args = 0;
1299 unsigned int stack_size = 0;
1300 unsigned short param_num = 0;
1301 unsigned short handle_stack_offset = 0;
1302 unsigned short handle_param_num = 0;
1304 if (is_full_pointer_function( func )) oi_flags |= RPC_FC_PROC_OIF_FULLPTR;
1305 if (is_object( iface ))
1307 oi_flags |= RPC_FC_PROC_OIF_OBJECT;
1308 if (get_stub_mode() == MODE_Oif) oi_flags |= RPC_FC_PROC_OIF_OBJ_V2;
1309 stack_size += pointer_size;
1312 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1314 if (var == handle_var)
1316 handle_stack_offset = stack_size;
1317 handle_param_num = param_num;
1319 stack_size += get_stack_size( var, NULL );
1320 param_num++;
1321 nb_args++;
1323 if (!is_void( type_function_get_rettype( func->type )))
1325 stack_size += pointer_size;
1326 nb_args++;
1329 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1330 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1331 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1332 print_file( file, indent, "0x%02x,\n", oi_flags );
1333 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1334 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1335 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack size = %hu */\n", stack_size, stack_size );
1336 *offset += 10;
1338 if (!implicit_fc)
1340 switch (explicit_fc)
1342 case RPC_FC_BIND_PRIMITIVE:
1343 handle_flags = 0;
1344 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1345 print_file( file, indent, "0x%02x,\n", handle_flags );
1346 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1347 handle_stack_offset, handle_stack_offset );
1348 *offset += 4;
1349 break;
1350 case RPC_FC_BIND_GENERIC:
1351 handle_flags = type_memsize( handle_var->type );
1352 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1353 print_file( file, indent, "0x%02x,\n", handle_flags );
1354 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1355 handle_stack_offset, handle_stack_offset );
1356 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1357 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD);
1358 *offset += 6;
1359 break;
1360 case RPC_FC_BIND_CONTEXT:
1361 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1362 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1363 print_file( file, indent, "0x%02x,\n", handle_flags );
1364 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1365 handle_stack_offset, handle_stack_offset );
1366 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1367 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1368 *offset += 6;
1369 break;
1373 if (get_stub_mode() == MODE_Oif)
1375 unsigned char oi2_flags = get_func_oi2_flags( func );
1376 unsigned char ext_flags = 0;
1377 unsigned int size;
1379 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1380 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1382 size = get_function_buffer_size( func, PASS_IN );
1383 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %hu */\n", size, size );
1384 size = get_function_buffer_size( func, PASS_OUT );
1385 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %hu */\n", size, size );
1386 print_file( file, indent, "0x%02x,\n", oi2_flags );
1387 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1388 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1389 print_file( file, indent, "0x%02x,\n", ext_flags );
1390 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1391 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1392 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1393 *offset += 14;
1394 if (pointer_size == 8)
1396 unsigned short pos = 0, fpu_mask = 0;
1398 if (is_object( iface )) pos += 2;
1399 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1401 if (type_get_type( var->type ) == TYPE_BASIC)
1403 switch (type_basic_get_type( var->type ))
1405 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1406 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1407 default: break;
1410 pos += 2;
1411 if (pos >= 16) break;
1413 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1414 *offset += 2;
1419 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1420 const var_t *func, unsigned int *offset,
1421 unsigned short num_proc )
1423 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1424 int is_interpreted = is_interpreted_func( iface, func );
1425 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1426 var_t *retval = type_function_get_retval( func->type );
1428 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1430 /* emit argument data */
1431 if (type_get_function_args(func->type))
1433 const var_t *var;
1434 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1436 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1437 if (is_new_style)
1438 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1439 else
1440 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1444 /* emit return value data */
1445 if (is_void(retval->type))
1447 if (!is_new_style)
1449 print_file(file, 0, "/* %u (void) */\n", *offset);
1450 print_file(file, indent, "0x5b, /* FC_END */\n");
1451 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1452 *offset += 2;
1455 else
1457 print_file( file, 0, "/* %u (return value) */\n", *offset );
1458 if (is_new_style)
1459 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1460 else
1461 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1465 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1466 type_pred_t pred, unsigned int *offset)
1468 const statement_t *stmt;
1469 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1471 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1473 const statement_t *stmt_func;
1474 const type_t *iface = stmt->u.type;
1475 const type_t *parent = type_iface_get_inherit( iface );
1476 int count = parent ? count_methods( parent ) : 0;
1478 if (!pred(iface)) continue;
1479 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1481 var_t *func = stmt_func->u.var;
1482 if (is_local(func->attrs)) continue;
1483 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1489 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1491 int indent = 0;
1492 unsigned int offset = 0;
1494 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1495 print_file(file, indent, "{\n");
1496 indent++;
1497 print_file(file, indent, "0,\n");
1498 print_file(file, indent, "{\n");
1499 indent++;
1501 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1503 print_file(file, indent, "0x0\n");
1504 indent--;
1505 print_file(file, indent, "}\n");
1506 indent--;
1507 print_file(file, indent, "};\n");
1508 print_file(file, indent, "\n");
1511 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1513 const statement_t *stmt;
1514 int indent = 0;
1516 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1517 iface->name );
1518 print_file( file, indent, "{\n" );
1519 indent++;
1520 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1522 var_t *func = stmt->u.var;
1523 if (is_local( func->attrs )) continue;
1524 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1526 indent--;
1527 print_file( file, indent, "};\n\n" );
1530 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1532 unsigned char fc;
1534 if (type_get_type(type) == TYPE_BASIC)
1535 fc = get_basic_fc_signed(type);
1536 else if (type_get_type(type) == TYPE_ENUM)
1537 fc = get_enum_fc(type);
1538 else
1539 return 0;
1541 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1542 *typestring_offset += 1;
1543 return 1;
1546 /* write conformance / variance descriptor */
1547 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1548 unsigned int baseoff, const type_t *type,
1549 const expr_t *expr)
1551 unsigned char operator_type = 0;
1552 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1553 const char *conftype_string = "field";
1554 const expr_t *subexpr;
1555 const type_t *iface = NULL;
1556 const char *name;
1558 if (!expr)
1560 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1561 return 4;
1564 if (expr->is_const)
1566 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1567 error("write_conf_or_var_desc: constant value %d is greater than "
1568 "the maximum constant size of %d\n", expr->cval,
1569 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1571 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1572 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1573 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1574 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1576 return 4;
1579 if (!cont_type) /* top-level conformance */
1581 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1582 conftype_string = "parameter";
1583 cont_type = current_func->type;
1584 name = current_func->name;
1585 iface = current_iface;
1587 else
1589 name = cont_type->name;
1590 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1592 conftype = RPC_FC_POINTER_CONFORMANCE;
1593 conftype_string = "field pointer";
1597 subexpr = expr;
1598 switch (subexpr->type)
1600 case EXPR_PPTR:
1601 subexpr = subexpr->ref;
1602 operator_type = RPC_FC_DEREFERENCE;
1603 break;
1604 case EXPR_DIV:
1605 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1607 subexpr = subexpr->ref;
1608 operator_type = RPC_FC_DIV_2;
1610 break;
1611 case EXPR_MUL:
1612 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1614 subexpr = subexpr->ref;
1615 operator_type = RPC_FC_MULT_2;
1617 break;
1618 case EXPR_SUB:
1619 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1621 subexpr = subexpr->ref;
1622 operator_type = RPC_FC_SUB_1;
1624 break;
1625 case EXPR_ADD:
1626 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1628 subexpr = subexpr->ref;
1629 operator_type = RPC_FC_ADD_1;
1631 break;
1632 default:
1633 break;
1636 if (subexpr->type == EXPR_IDENTIFIER)
1638 const type_t *correlation_variable = NULL;
1639 unsigned char param_type = 0;
1640 unsigned int offset = 0;
1641 const var_t *var;
1642 struct expr_loc expr_loc;
1644 if (type_get_type(cont_type) == TYPE_FUNCTION)
1646 var_list_t *args = type_get_function_args( cont_type );
1648 if (is_object( iface )) offset += pointer_size;
1649 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1651 if (var->name && !strcmp(var->name, subexpr->u.sval))
1653 expr_loc.v = var;
1654 correlation_variable = var->type;
1655 break;
1657 offset += get_stack_size( var, NULL );
1660 else
1662 var_list_t *fields = type_struct_get_fields( cont_type );
1664 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1666 unsigned int size = field_memsize( var->type, &offset );
1667 if (var->name && !strcmp(var->name, subexpr->u.sval))
1669 expr_loc.v = var;
1670 correlation_variable = var->type;
1671 break;
1673 offset += size;
1677 if (!correlation_variable)
1678 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1679 expr_loc.attr = NULL;
1680 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1682 offset -= baseoff;
1684 if (type_get_type(correlation_variable) == TYPE_BASIC)
1686 switch (get_basic_fc(correlation_variable))
1688 case RPC_FC_CHAR:
1689 case RPC_FC_SMALL:
1690 param_type = RPC_FC_SMALL;
1691 break;
1692 case RPC_FC_BYTE:
1693 case RPC_FC_USMALL:
1694 param_type = RPC_FC_USMALL;
1695 break;
1696 case RPC_FC_WCHAR:
1697 case RPC_FC_SHORT:
1698 param_type = RPC_FC_SHORT;
1699 break;
1700 case RPC_FC_USHORT:
1701 param_type = RPC_FC_USHORT;
1702 break;
1703 case RPC_FC_LONG:
1704 param_type = RPC_FC_LONG;
1705 break;
1706 case RPC_FC_ULONG:
1707 param_type = RPC_FC_ULONG;
1708 break;
1709 default:
1710 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1711 get_basic_fc(correlation_variable));
1714 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1716 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1717 param_type = RPC_FC_LONG;
1718 else
1719 param_type = RPC_FC_SHORT;
1721 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1723 if (pointer_size == 8)
1724 param_type = RPC_FC_HYPER;
1725 else
1726 param_type = RPC_FC_LONG;
1728 else
1730 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1731 subexpr->u.sval);
1732 return 0;
1735 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1736 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1737 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1738 operator_type ? string_of_type(operator_type) : "no operators");
1739 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1740 (unsigned short)offset, offset);
1742 else if (!iface || is_interpreted_func( iface, current_func ))
1744 unsigned int callback_offset = 0;
1745 struct expr_eval_routine *eval;
1746 int found = 0;
1748 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1750 if (eval->cont_type == cont_type ||
1751 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1752 eval->iface == iface &&
1753 eval->name && name && !strcmp(eval->name, name) &&
1754 !compare_expr(eval->expr, expr)))
1756 found = 1;
1757 break;
1759 callback_offset++;
1762 if (!found)
1764 eval = xmalloc (sizeof(*eval));
1765 eval->iface = iface;
1766 eval->cont_type = cont_type;
1767 eval->name = xstrdup( name );
1768 eval->baseoff = baseoff;
1769 eval->expr = expr;
1770 list_add_tail (&expr_eval_routines, &eval->entry);
1773 if (callback_offset > USHRT_MAX)
1774 error("Maximum number of callback routines reached\n");
1776 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1777 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1778 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1780 else /* output a dummy corr desc that isn't used */
1782 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1783 print_file(file, 2, "0x0,\n" );
1784 print_file(file, 2, "NdrFcShort(0x0),\n" );
1786 return 4;
1789 /* return size and start offset of a data field based on current offset */
1790 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1792 unsigned int align = 0;
1793 unsigned int size = type_memsize_and_alignment( type, &align );
1795 *offset = ROUND_SIZE( *offset, align );
1796 return size;
1799 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1801 unsigned int size = 0;
1802 unsigned int max_align;
1803 const var_t *v;
1805 if (!fields) return 0;
1806 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1808 unsigned int falign = 0;
1809 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1810 if (*align < falign) *align = falign;
1811 falign = clamp_align(falign);
1812 size = ROUND_SIZE(size, falign);
1813 size += fsize;
1816 max_align = clamp_align(*align);
1817 size = ROUND_SIZE(size, max_align);
1819 return size;
1822 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1824 unsigned int size, maxs = 0;
1825 unsigned int align = *pmaxa;
1826 const var_t *v;
1828 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1830 /* we could have an empty default field with NULL type */
1831 if (v->type)
1833 size = type_memsize_and_alignment(v->type, &align);
1834 if (maxs < size) maxs = size;
1835 if (*pmaxa < align) *pmaxa = align;
1839 return maxs;
1842 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1844 unsigned int size = 0;
1846 switch (type_get_type(t))
1848 case TYPE_BASIC:
1849 switch (get_basic_fc(t))
1851 case RPC_FC_BYTE:
1852 case RPC_FC_CHAR:
1853 case RPC_FC_USMALL:
1854 case RPC_FC_SMALL:
1855 size = 1;
1856 if (size > *align) *align = size;
1857 break;
1858 case RPC_FC_WCHAR:
1859 case RPC_FC_USHORT:
1860 case RPC_FC_SHORT:
1861 size = 2;
1862 if (size > *align) *align = size;
1863 break;
1864 case RPC_FC_ULONG:
1865 case RPC_FC_LONG:
1866 case RPC_FC_ERROR_STATUS_T:
1867 case RPC_FC_FLOAT:
1868 size = 4;
1869 if (size > *align) *align = size;
1870 break;
1871 case RPC_FC_HYPER:
1872 case RPC_FC_DOUBLE:
1873 size = 8;
1874 if (size > *align) *align = size;
1875 break;
1876 case RPC_FC_INT3264:
1877 case RPC_FC_UINT3264:
1878 case RPC_FC_BIND_PRIMITIVE:
1879 assert( pointer_size );
1880 size = pointer_size;
1881 if (size > *align) *align = size;
1882 break;
1883 default:
1884 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1885 size = 0;
1887 break;
1888 case TYPE_ENUM:
1889 switch (get_enum_fc(t))
1891 case RPC_FC_ENUM16:
1892 case RPC_FC_ENUM32:
1893 size = 4;
1894 if (size > *align) *align = size;
1895 break;
1896 default:
1897 error("type_memsize: Unknown enum type\n");
1898 size = 0;
1900 break;
1901 case TYPE_STRUCT:
1902 size = fields_memsize(type_struct_get_fields(t), align);
1903 break;
1904 case TYPE_ENCAPSULATED_UNION:
1905 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1906 break;
1907 case TYPE_UNION:
1908 size = union_memsize(type_union_get_cases(t), align);
1909 break;
1910 case TYPE_POINTER:
1911 assert( pointer_size );
1912 size = pointer_size;
1913 if (size > *align) *align = size;
1914 break;
1915 case TYPE_ARRAY:
1916 if (!type_array_is_decl_as_ptr(t))
1918 if (is_conformant_array(t))
1920 type_memsize_and_alignment(type_array_get_element(t), align);
1921 size = 0;
1923 else
1924 size = type_array_get_dim(t) *
1925 type_memsize_and_alignment(type_array_get_element(t), align);
1927 else /* declared as a pointer */
1929 assert( pointer_size );
1930 size = pointer_size;
1931 if (size > *align) *align = size;
1933 break;
1934 case TYPE_INTERFACE:
1935 case TYPE_ALIAS:
1936 case TYPE_VOID:
1937 case TYPE_COCLASS:
1938 case TYPE_MODULE:
1939 case TYPE_FUNCTION:
1940 case TYPE_BITFIELD:
1941 /* these types should not be encountered here due to language
1942 * restrictions (interface, void, coclass, module), logical
1943 * restrictions (alias - due to type_get_type call above) or
1944 * checking restrictions (function, bitfield). */
1945 assert(0);
1948 return size;
1951 unsigned int type_memsize(const type_t *t)
1953 unsigned int align = 0;
1954 return type_memsize_and_alignment( t, &align );
1957 static unsigned int type_buffer_alignment(const type_t *t)
1959 const var_list_t *fields;
1960 const var_t *var;
1961 unsigned int max = 0, align;
1963 switch (type_get_type(t))
1965 case TYPE_BASIC:
1966 switch (get_basic_fc(t))
1968 case RPC_FC_BYTE:
1969 case RPC_FC_CHAR:
1970 case RPC_FC_USMALL:
1971 case RPC_FC_SMALL:
1972 return 1;
1973 case RPC_FC_WCHAR:
1974 case RPC_FC_USHORT:
1975 case RPC_FC_SHORT:
1976 return 2;
1977 case RPC_FC_ULONG:
1978 case RPC_FC_LONG:
1979 case RPC_FC_ERROR_STATUS_T:
1980 case RPC_FC_FLOAT:
1981 case RPC_FC_INT3264:
1982 case RPC_FC_UINT3264:
1983 return 4;
1984 case RPC_FC_HYPER:
1985 case RPC_FC_DOUBLE:
1986 return 8;
1987 default:
1988 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1990 break;
1991 case TYPE_ENUM:
1992 switch (get_enum_fc(t))
1994 case RPC_FC_ENUM16:
1995 return 2;
1996 case RPC_FC_ENUM32:
1997 return 4;
1998 default:
1999 error("type_buffer_alignment: Unknown enum type\n");
2001 break;
2002 case TYPE_STRUCT:
2003 if (!(fields = type_struct_get_fields(t))) break;
2004 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2006 if (!var->type) continue;
2007 align = type_buffer_alignment( var->type );
2008 if (max < align) max = align;
2010 break;
2011 case TYPE_ENCAPSULATED_UNION:
2012 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2013 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2015 if (!var->type) continue;
2016 align = type_buffer_alignment( var->type );
2017 if (max < align) max = align;
2019 break;
2020 case TYPE_UNION:
2021 if (!(fields = type_union_get_cases(t))) break;
2022 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2024 if (!var->type) continue;
2025 align = type_buffer_alignment( var->type );
2026 if (max < align) max = align;
2028 break;
2029 case TYPE_ARRAY:
2030 if (!type_array_is_decl_as_ptr(t))
2031 return type_buffer_alignment( type_array_get_element(t) );
2032 /* else fall through */
2033 case TYPE_POINTER:
2034 return 4;
2035 case TYPE_INTERFACE:
2036 case TYPE_ALIAS:
2037 case TYPE_VOID:
2038 case TYPE_COCLASS:
2039 case TYPE_MODULE:
2040 case TYPE_FUNCTION:
2041 case TYPE_BITFIELD:
2042 /* these types should not be encountered here due to language
2043 * restrictions (interface, void, coclass, module), logical
2044 * restrictions (alias - due to type_get_type call above) or
2045 * checking restrictions (function, bitfield). */
2046 assert(0);
2048 return max;
2051 int is_full_pointer_function(const var_t *func)
2053 const var_t *var;
2054 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2055 return TRUE;
2056 if (!type_get_function_args(func->type))
2057 return FALSE;
2058 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2059 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2060 return TRUE;
2061 return FALSE;
2064 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2066 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2067 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2068 fprintf(file, "\n");
2071 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2073 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2074 fprintf(file, "\n");
2077 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2078 const type_t *type,
2079 enum type_context context,
2080 unsigned int offset,
2081 unsigned int *typeformat_offset)
2083 unsigned int start_offset = *typeformat_offset;
2084 short reloff = offset - (*typeformat_offset + 2);
2085 int in_attr, out_attr;
2086 int pointer_type;
2087 unsigned char flags = 0;
2089 pointer_type = get_pointer_fc_context(type, attrs, context);
2091 in_attr = is_attr(attrs, ATTR_IN);
2092 out_attr = is_attr(attrs, ATTR_OUT);
2093 if (!in_attr && !out_attr) in_attr = 1;
2095 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2096 flags |= RPC_FC_P_ONSTACK;
2098 if (is_ptr(type))
2100 type_t *ref = type_pointer_get_ref(type);
2101 if(is_declptr(ref) && !is_user_type(ref))
2102 flags |= RPC_FC_P_DEREF;
2105 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2106 pointer_type,
2107 flags,
2108 string_of_type(pointer_type));
2109 if (file)
2111 if (flags & RPC_FC_P_ONSTACK)
2112 fprintf(file, " [allocated_on_stack]");
2113 if (flags & RPC_FC_P_DEREF)
2114 fprintf(file, " [pointer_deref]");
2115 fprintf(file, " */\n");
2118 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2119 *typeformat_offset += 4;
2121 return start_offset;
2124 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2125 const type_t *type, enum type_context context)
2127 unsigned char fc;
2128 unsigned char pointer_fc;
2129 const type_t *ref;
2130 int in_attr = is_attr(attrs, ATTR_IN);
2131 int out_attr = is_attr(attrs, ATTR_OUT);
2132 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2134 /* for historical reasons, write_simple_pointer also handled string types,
2135 * but no longer does. catch bad uses of the function with this check */
2136 if (is_string_type(attrs, type))
2137 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2139 pointer_fc = get_pointer_fc_context(type, attrs, context);
2141 ref = type_pointer_get_ref(type);
2142 if (type_get_type(ref) == TYPE_ENUM)
2143 fc = get_enum_fc(ref);
2144 else
2145 fc = get_basic_fc(ref);
2147 if (out_attr && !in_attr)
2148 flags |= RPC_FC_P_ONSTACK;
2150 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2151 pointer_fc, flags, string_of_type(pointer_fc),
2152 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2153 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2154 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2155 return 4;
2158 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2160 print_file(file, 0, "/* %u (", tfsoff);
2161 write_type_decl(file, t, NULL);
2162 print_file(file, 0, ") */\n");
2165 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2166 type_t *type, unsigned int ref_offset,
2167 enum type_context context,
2168 unsigned int *typestring_offset)
2170 unsigned int offset = *typestring_offset;
2171 type_t *ref = type_pointer_get_ref(type);
2173 print_start_tfs_comment(file, type, offset);
2174 update_tfsoff(type, offset, file);
2176 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2178 case TGT_BASIC:
2179 case TGT_ENUM:
2180 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2181 break;
2182 default:
2183 if (ref_offset)
2184 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2185 break;
2188 return offset;
2191 static int processed(const type_t *type)
2193 return type->typestring_offset && !type->tfswrite;
2196 static int user_type_has_variable_size(const type_t *t)
2198 if (is_ptr(t))
2199 return TRUE;
2200 else if (type_get_type(t) == TYPE_STRUCT)
2202 switch (get_struct_fc(t))
2204 case RPC_FC_PSTRUCT:
2205 case RPC_FC_CSTRUCT:
2206 case RPC_FC_CPSTRUCT:
2207 case RPC_FC_CVSTRUCT:
2208 return TRUE;
2211 /* Note: Since this only applies to user types, we can't have a conformant
2212 array here, and strings should get filed under pointer in this case. */
2213 return FALSE;
2216 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2218 unsigned int start, absoff, flags;
2219 const char *name = NULL;
2220 type_t *utype = get_user_type(type, &name);
2221 unsigned int usize = type_memsize(utype);
2222 unsigned int ualign = type_buffer_alignment(utype);
2223 unsigned int size = type_memsize(type);
2224 unsigned short funoff = user_type_offset(name);
2225 short reloff;
2227 if (processed(type)) return type->typestring_offset;
2229 guard_rec(type);
2231 if(user_type_has_variable_size(utype)) usize = 0;
2233 if (type_get_type(utype) == TYPE_BASIC ||
2234 type_get_type(utype) == TYPE_ENUM)
2236 unsigned char fc;
2238 if (type_get_type(utype) == TYPE_ENUM)
2239 fc = get_enum_fc(utype);
2240 else
2241 fc = get_basic_fc(utype);
2243 absoff = *tfsoff;
2244 print_start_tfs_comment(file, utype, absoff);
2245 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2246 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2247 *tfsoff += 2;
2249 else
2251 if (!processed(utype))
2252 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2253 absoff = utype->typestring_offset;
2256 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2257 flags = 0x40;
2258 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2259 flags = 0x80;
2260 else
2261 flags = 0;
2263 start = *tfsoff;
2264 update_tfsoff(type, start, file);
2265 print_start_tfs_comment(file, type, start);
2266 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2267 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2268 flags | (ualign - 1), ualign - 1, flags);
2269 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2270 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2271 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2272 *tfsoff += 8;
2273 reloff = absoff - *tfsoff;
2274 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2275 *tfsoff += 2;
2276 return start;
2279 static void write_member_type(FILE *file, const type_t *cont,
2280 int cont_is_complex, const attr_list_t *attrs,
2281 const type_t *type, unsigned int *corroff,
2282 unsigned int *tfsoff)
2284 if (is_embedded_complex(type) && !is_conformant_array(type))
2286 unsigned int absoff;
2287 short reloff;
2289 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2291 absoff = *corroff;
2292 *corroff += 8;
2294 else
2296 absoff = type->typestring_offset;
2298 reloff = absoff - (*tfsoff + 2);
2300 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2301 /* padding is represented using FC_STRUCTPAD* types, so presumably
2302 * this is left over in the format for historical purposes in MIDL
2303 * or rpcrt4. */
2304 print_file(file, 2, "0x0,\n");
2305 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2306 reloff, reloff, absoff);
2307 *tfsoff += 4;
2309 else if (is_ptr(type) || is_conformant_array(type))
2311 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2312 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2313 *tfsoff += 1;
2315 else if (!write_base_type(file, type, tfsoff))
2316 error("Unsupported member type %d\n", type_get_type(type));
2319 static void write_array_element_type(FILE *file, const type_t *type,
2320 int cont_is_complex, unsigned int *tfsoff)
2322 type_t *elem = type_array_get_element(type);
2324 if (!is_embedded_complex(elem) && is_ptr(elem))
2326 type_t *ref = type_pointer_get_ref(elem);
2328 if (processed(ref))
2330 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2331 ref->typestring_offset, tfsoff);
2332 return;
2334 if (cont_is_complex && is_string_type(NULL, elem))
2336 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2337 return;
2339 if (!is_string_type(NULL, elem) &&
2340 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2342 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2343 return;
2346 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2349 static void write_end(FILE *file, unsigned int *tfsoff)
2351 if (*tfsoff % 2 == 0)
2353 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2354 *tfsoff += 1;
2356 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2357 *tfsoff += 1;
2360 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2362 unsigned int offset = 0;
2363 var_list_t *fs = type_struct_get_fields(type);
2364 var_t *f;
2366 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2368 type_t *ft = f->type;
2369 unsigned int size = field_memsize( ft, &offset );
2370 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2372 short reloff;
2373 unsigned int absoff = ft->typestring_offset;
2374 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2375 absoff += 8; /* we already have a corr descr, skip it */
2376 reloff = absoff - (*tfsoff + 6);
2377 print_file(file, 0, "/* %d */\n", *tfsoff);
2378 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2379 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2380 write_conf_or_var_desc(file, current_structure, offset, ft,
2381 get_attrp(f->attrs, ATTR_SWITCHIS));
2382 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2383 (unsigned short)reloff, reloff, absoff);
2384 *tfsoff += 8;
2386 offset += size;
2390 static int write_pointer_description_offsets(
2391 FILE *file, const attr_list_t *attrs, type_t *type,
2392 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2393 unsigned int *typestring_offset)
2395 int written = 0;
2397 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2398 (is_array(type) && type_array_is_decl_as_ptr(type)))
2400 if (offset_in_memory && offset_in_buffer)
2402 unsigned int memsize;
2404 /* pointer instance
2406 * note that MSDN states that for pointer layouts in structures,
2407 * this is a negative offset from the end of the structure, but
2408 * this statement is incorrect. all offsets are positive */
2409 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2410 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2412 memsize = type_memsize(type);
2413 *offset_in_memory += memsize;
2414 /* increment these separately as in the case of conformant (varying)
2415 * structures these start at different values */
2416 *offset_in_buffer += memsize;
2418 *typestring_offset += 4;
2420 if (is_ptr(type))
2422 type_t *ref = type_pointer_get_ref(type);
2424 if (is_string_type(attrs, type))
2425 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2426 else if (processed(ref))
2427 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2428 ref->typestring_offset, typestring_offset);
2429 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2430 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2431 else
2432 error("write_pointer_description_offsets: type format string unknown\n");
2434 else
2436 unsigned int offset = type->typestring_offset;
2437 /* skip over the pointer that is written for strings, since a
2438 * pointer has to be written in-place here */
2439 if (is_string_type(attrs, type))
2440 offset += 4;
2441 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2444 return 1;
2447 if (is_array(type))
2449 return write_pointer_description_offsets(
2450 file, attrs, type_array_get_element(type), offset_in_memory,
2451 offset_in_buffer, typestring_offset);
2453 else if (is_non_complex_struct(type))
2455 /* otherwise search for interesting fields to parse */
2456 const var_t *v;
2457 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2459 if (offset_in_memory && offset_in_buffer)
2461 unsigned int padding;
2462 unsigned int align = 0;
2463 type_memsize_and_alignment(v->type, &align);
2464 padding = ROUNDING(*offset_in_memory, align);
2465 *offset_in_memory += padding;
2466 *offset_in_buffer += padding;
2468 written += write_pointer_description_offsets(
2469 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2470 typestring_offset);
2473 else
2475 if (offset_in_memory && offset_in_buffer)
2477 unsigned int memsize = type_memsize(type);
2478 *offset_in_memory += memsize;
2479 /* increment these separately as in the case of conformant (varying)
2480 * structures these start at different values */
2481 *offset_in_buffer += memsize;
2485 return written;
2488 static int write_no_repeat_pointer_descriptions(
2489 FILE *file, const attr_list_t *attrs, type_t *type,
2490 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2491 unsigned int *typestring_offset)
2493 int written = 0;
2495 if (is_ptr(type) ||
2496 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2498 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2499 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2500 *typestring_offset += 2;
2502 return write_pointer_description_offsets(file, attrs, type,
2503 offset_in_memory, offset_in_buffer, typestring_offset);
2506 if (is_non_complex_struct(type))
2508 const var_t *v;
2509 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2511 if (offset_in_memory && offset_in_buffer)
2513 unsigned int padding;
2514 unsigned int align = 0;
2515 type_memsize_and_alignment(v->type, &align);
2516 padding = ROUNDING(*offset_in_memory, align);
2517 *offset_in_memory += padding;
2518 *offset_in_buffer += padding;
2520 written += write_no_repeat_pointer_descriptions(
2521 file, v->attrs, v->type,
2522 offset_in_memory, offset_in_buffer, typestring_offset);
2525 else
2527 unsigned int memsize = type_memsize(type);
2528 *offset_in_memory += memsize;
2529 /* increment these separately as in the case of conformant (varying)
2530 * structures these start at different values */
2531 *offset_in_buffer += memsize;
2534 return written;
2537 /* Note: if file is NULL return value is number of pointers to write, else
2538 * it is the number of type format characters written */
2539 static int write_fixed_array_pointer_descriptions(
2540 FILE *file, const attr_list_t *attrs, type_t *type,
2541 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2542 unsigned int *typestring_offset)
2544 int pointer_count = 0;
2546 if (type_get_type(type) == TYPE_ARRAY &&
2547 !type_array_has_conformance(type) && !type_array_has_variance(type))
2549 unsigned int temp = 0;
2550 /* unfortunately, this needs to be done in two passes to avoid
2551 * writing out redundant FC_FIXED_REPEAT descriptions */
2552 pointer_count = write_pointer_description_offsets(
2553 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2554 if (pointer_count > 0)
2556 unsigned int increment_size;
2557 unsigned int offset_of_array_pointer_mem = 0;
2558 unsigned int offset_of_array_pointer_buf = 0;
2560 increment_size = type_memsize(type_array_get_element(type));
2562 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2563 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2564 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2565 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2566 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2567 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2568 *typestring_offset += 10;
2570 pointer_count = write_pointer_description_offsets(
2571 file, attrs, type, &offset_of_array_pointer_mem,
2572 &offset_of_array_pointer_buf, typestring_offset);
2575 else if (type_get_type(type) == TYPE_STRUCT)
2577 const var_t *v;
2578 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2580 if (offset_in_memory && offset_in_buffer)
2582 unsigned int padding;
2583 unsigned int align = 0;
2584 type_memsize_and_alignment(v->type, &align);
2585 padding = ROUNDING(*offset_in_memory, align);
2586 *offset_in_memory += padding;
2587 *offset_in_buffer += padding;
2589 pointer_count += write_fixed_array_pointer_descriptions(
2590 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2591 typestring_offset);
2594 else
2596 if (offset_in_memory && offset_in_buffer)
2598 unsigned int memsize;
2599 memsize = type_memsize(type);
2600 *offset_in_memory += memsize;
2601 /* increment these separately as in the case of conformant (varying)
2602 * structures these start at different values */
2603 *offset_in_buffer += memsize;
2607 return pointer_count;
2610 /* Note: if file is NULL return value is number of pointers to write, else
2611 * it is the number of type format characters written */
2612 static int write_conformant_array_pointer_descriptions(
2613 FILE *file, const attr_list_t *attrs, type_t *type,
2614 unsigned int offset_in_memory, unsigned int *typestring_offset)
2616 int pointer_count = 0;
2618 if (is_conformant_array(type) && !type_array_has_variance(type))
2620 unsigned int temp = 0;
2621 /* unfortunately, this needs to be done in two passes to avoid
2622 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2623 pointer_count = write_pointer_description_offsets(
2624 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2625 if (pointer_count > 0)
2627 unsigned int increment_size;
2628 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2629 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2631 increment_size = type_memsize(type_array_get_element(type));
2633 if (increment_size > USHRT_MAX)
2634 error("array size of %u bytes is too large\n", increment_size);
2636 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2637 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2638 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2641 *typestring_offset += 8;
2643 pointer_count = write_pointer_description_offsets(
2644 file, attrs, type_array_get_element(type),
2645 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2646 typestring_offset);
2650 return pointer_count;
2653 /* Note: if file is NULL return value is number of pointers to write, else
2654 * it is the number of type format characters written */
2655 static int write_varying_array_pointer_descriptions(
2656 FILE *file, const attr_list_t *attrs, type_t *type,
2657 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2658 unsigned int *typestring_offset)
2660 int pointer_count = 0;
2662 if (is_array(type) && type_array_has_variance(type))
2664 unsigned int temp = 0;
2665 /* unfortunately, this needs to be done in two passes to avoid
2666 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2667 pointer_count = write_pointer_description_offsets(
2668 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2669 if (pointer_count > 0)
2671 unsigned int increment_size;
2673 increment_size = type_memsize(type_array_get_element(type));
2675 if (increment_size > USHRT_MAX)
2676 error("array size of %u bytes is too large\n", increment_size);
2678 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2679 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2680 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2681 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2682 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2683 *typestring_offset += 8;
2685 pointer_count = write_pointer_description_offsets(
2686 file, attrs, type_array_get_element(type), offset_in_memory,
2687 offset_in_buffer, typestring_offset);
2690 else if (type_get_type(type) == TYPE_STRUCT)
2692 const var_t *v;
2693 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2695 if (offset_in_memory && offset_in_buffer)
2697 unsigned int align = 0, padding;
2699 if (is_array(v->type) && type_array_has_variance(v->type))
2701 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2702 /* skip over variance and offset in buffer */
2703 *offset_in_buffer += 8;
2706 type_memsize_and_alignment(v->type, &align);
2707 padding = ROUNDING(*offset_in_memory, align);
2708 *offset_in_memory += padding;
2709 *offset_in_buffer += padding;
2711 pointer_count += write_varying_array_pointer_descriptions(
2712 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2713 typestring_offset);
2716 else
2718 if (offset_in_memory && offset_in_buffer)
2720 unsigned int memsize = type_memsize(type);
2721 *offset_in_memory += memsize;
2722 /* increment these separately as in the case of conformant (varying)
2723 * structures these start at different values */
2724 *offset_in_buffer += memsize;
2728 return pointer_count;
2731 static void write_pointer_description(FILE *file, type_t *type,
2732 unsigned int *typestring_offset)
2734 unsigned int offset_in_buffer;
2735 unsigned int offset_in_memory;
2737 /* pass 1: search for single instance of a pointer (i.e. don't descend
2738 * into arrays) */
2739 if (!is_array(type))
2741 offset_in_memory = 0;
2742 offset_in_buffer = 0;
2743 write_no_repeat_pointer_descriptions(
2744 file, NULL, type,
2745 &offset_in_memory, &offset_in_buffer, typestring_offset);
2748 /* pass 2: search for pointers in fixed arrays */
2749 offset_in_memory = 0;
2750 offset_in_buffer = 0;
2751 write_fixed_array_pointer_descriptions(
2752 file, NULL, type,
2753 &offset_in_memory, &offset_in_buffer, typestring_offset);
2755 /* pass 3: search for pointers in conformant only arrays (but don't descend
2756 * into conformant varying or varying arrays) */
2757 if (is_conformant_array(type) &&
2758 (type_array_is_decl_as_ptr(type) || !current_structure))
2759 write_conformant_array_pointer_descriptions(
2760 file, NULL, type, 0, typestring_offset);
2761 else if (type_get_type(type) == TYPE_STRUCT &&
2762 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2764 type_t *carray = find_array_or_string_in_struct(type)->type;
2765 write_conformant_array_pointer_descriptions( file, NULL, carray,
2766 type_memsize(type), typestring_offset);
2769 /* pass 4: search for pointers in varying arrays */
2770 offset_in_memory = 0;
2771 offset_in_buffer = 0;
2772 write_varying_array_pointer_descriptions(
2773 file, NULL, type,
2774 &offset_in_memory, &offset_in_buffer, typestring_offset);
2777 int is_declptr(const type_t *t)
2779 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2782 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2783 type_t *type, enum type_context context,
2784 const char *name, unsigned int *typestring_offset)
2786 unsigned int start_offset;
2787 unsigned char rtype;
2788 type_t *elem_type;
2789 int is_processed = processed(type);
2791 start_offset = *typestring_offset;
2793 if (is_declptr(type))
2795 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2796 int pointer_type = get_pointer_fc_context(type, attrs, context);
2797 if (!pointer_type)
2798 pointer_type = RPC_FC_RP;
2799 print_start_tfs_comment(file, type, *typestring_offset);
2800 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2801 pointer_type, flag, string_of_type(pointer_type),
2802 flag ? " [simple_pointer]" : "");
2803 *typestring_offset += 2;
2804 if (!flag)
2806 print_file(file, 2, "NdrFcShort(0x2),\n");
2807 *typestring_offset += 2;
2809 is_processed = FALSE;
2812 if (is_array(type))
2813 elem_type = type_array_get_element(type);
2814 else
2815 elem_type = type_pointer_get_ref(type);
2817 if (type_get_type(elem_type) != TYPE_BASIC)
2819 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2820 return start_offset;
2823 rtype = get_basic_fc(elem_type);
2824 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2826 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2827 return start_offset;
2830 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2832 unsigned int dim = type_array_get_dim(type);
2834 if (is_processed) return start_offset;
2836 /* FIXME: multi-dimensional array */
2837 if (0xffffu < dim)
2838 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2839 name, 0xffffu, dim - 0xffffu);
2841 if (rtype == RPC_FC_WCHAR)
2842 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2843 else
2844 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2845 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2846 *typestring_offset += 2;
2848 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2849 *typestring_offset += 2;
2851 update_tfsoff(type, start_offset, file);
2852 return start_offset;
2854 else if (is_conformant_array(type))
2856 if (rtype == RPC_FC_WCHAR)
2857 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2858 else
2859 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2860 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2861 *typestring_offset += 2;
2863 *typestring_offset += write_conf_or_var_desc(
2864 file, current_structure,
2865 (!type_array_is_decl_as_ptr(type) && current_structure
2866 ? type_memsize(current_structure)
2867 : 0),
2868 type, type_array_get_conformance(type));
2870 update_tfsoff(type, start_offset, file);
2871 return start_offset;
2873 else
2875 if (is_processed) return start_offset;
2877 if (rtype == RPC_FC_WCHAR)
2878 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2879 else
2880 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2881 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2882 *typestring_offset += 2;
2884 update_tfsoff(type, start_offset, file);
2885 return start_offset;
2889 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2890 const char *name, unsigned int *typestring_offset)
2892 const expr_t *length_is = type_array_get_variance(type);
2893 const expr_t *size_is = type_array_get_conformance(type);
2894 unsigned int align;
2895 unsigned int size;
2896 unsigned int start_offset;
2897 unsigned char fc;
2898 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2899 unsigned int baseoff
2900 = !type_array_is_decl_as_ptr(type) && current_structure
2901 ? type_memsize(current_structure)
2902 : 0;
2904 if (!pointer_type)
2905 pointer_type = RPC_FC_RP;
2907 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2909 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2910 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2911 fc = get_array_fc(type);
2913 start_offset = *typestring_offset;
2914 update_tfsoff(type, start_offset, file);
2915 print_start_tfs_comment(file, type, start_offset);
2916 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2917 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2918 *typestring_offset += 2;
2920 align = 0;
2921 if (fc != RPC_FC_BOGUS_ARRAY)
2923 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2925 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2926 *typestring_offset += 4;
2928 else
2930 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2931 *typestring_offset += 2;
2934 if (is_conformant_array(type))
2935 *typestring_offset
2936 += write_conf_or_var_desc(file, current_structure, baseoff,
2937 type, size_is);
2939 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2941 unsigned int elsize = type_memsize(type_array_get_element(type));
2942 unsigned int dim = type_array_get_dim(type);
2944 if (fc == RPC_FC_LGVARRAY)
2946 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2947 *typestring_offset += 4;
2949 else
2951 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2952 *typestring_offset += 2;
2955 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2956 *typestring_offset += 2;
2959 if (length_is)
2960 *typestring_offset
2961 += write_conf_or_var_desc(file, current_structure, baseoff,
2962 type, length_is);
2964 if (type_has_pointers(type_array_get_element(type)) &&
2965 (type_array_is_decl_as_ptr(type) || !current_structure))
2967 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2968 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2969 *typestring_offset += 2;
2970 write_pointer_description(file, type, typestring_offset);
2971 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2972 *typestring_offset += 1;
2975 write_array_element_type(file, type, FALSE, typestring_offset);
2976 write_end(file, typestring_offset);
2978 else
2980 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2981 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2982 *typestring_offset += 2;
2983 *typestring_offset
2984 += write_conf_or_var_desc(file, current_structure, baseoff,
2985 type, size_is);
2986 *typestring_offset
2987 += write_conf_or_var_desc(file, current_structure, baseoff,
2988 type, length_is);
2990 write_array_element_type(file, type, TRUE, typestring_offset);
2991 write_end(file, typestring_offset);
2994 return start_offset;
2997 static const var_t *find_array_or_string_in_struct(const type_t *type)
2999 const var_list_t *fields = type_struct_get_fields(type);
3000 const var_t *last_field;
3001 const type_t *ft;
3003 if (!fields || list_empty(fields))
3004 return NULL;
3006 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3007 ft = last_field->type;
3009 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3010 return last_field;
3012 if (type_get_type(ft) == TYPE_STRUCT)
3013 return find_array_or_string_in_struct(ft);
3014 else
3015 return NULL;
3018 static void write_struct_members(FILE *file, const type_t *type,
3019 int is_complex, unsigned int *corroff,
3020 unsigned int *typestring_offset)
3022 const var_t *field;
3023 unsigned short offset = 0;
3024 unsigned int salign = 1;
3025 int padding;
3026 var_list_t *fields = type_struct_get_fields(type);
3028 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3030 type_t *ft = field->type;
3031 unsigned int align = 0;
3032 unsigned int size = type_memsize_and_alignment(ft, &align);
3033 align = clamp_align(align);
3034 if (salign < align) salign = align;
3036 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3038 if ((align - 1) & offset)
3040 unsigned char fc = 0;
3041 switch (align)
3043 case 2:
3044 fc = RPC_FC_ALIGNM2;
3045 break;
3046 case 4:
3047 fc = RPC_FC_ALIGNM4;
3048 break;
3049 case 8:
3050 fc = RPC_FC_ALIGNM8;
3051 break;
3052 default:
3053 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3055 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3056 offset = ROUND_SIZE(offset, align);
3057 *typestring_offset += 1;
3059 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3060 typestring_offset);
3061 offset += size;
3065 padding = ROUNDING(offset, salign);
3066 if (padding)
3068 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3069 RPC_FC_STRUCTPAD1 + padding - 1,
3070 padding);
3071 *typestring_offset += 1;
3074 write_end(file, typestring_offset);
3077 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3078 const char *name, unsigned int *tfsoff)
3080 const type_t *save_current_structure = current_structure;
3081 unsigned int total_size;
3082 const var_t *array;
3083 unsigned int start_offset;
3084 unsigned int align;
3085 unsigned int corroff;
3086 var_t *f;
3087 unsigned char fc = get_struct_fc(type);
3088 var_list_t *fields = type_struct_get_fields(type);
3090 if (processed(type)) return type->typestring_offset;
3092 guard_rec(type);
3093 current_structure = type;
3095 total_size = type_memsize(type);
3096 align = type_buffer_alignment(type);
3097 if (total_size > USHRT_MAX)
3098 error("structure size for %s exceeds %d bytes by %d bytes\n",
3099 name, USHRT_MAX, total_size - USHRT_MAX);
3101 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3102 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3104 array = find_array_or_string_in_struct(type);
3105 if (array && !processed(array->type))
3107 if(is_string_type(array->attrs, array->type))
3108 write_string_tfs(file, array->attrs, array->type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3109 else
3110 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3113 corroff = *tfsoff;
3114 write_descriptors(file, type, tfsoff);
3116 start_offset = *tfsoff;
3117 update_tfsoff(type, start_offset, file);
3118 print_start_tfs_comment(file, type, start_offset);
3119 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3120 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3121 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3122 *tfsoff += 4;
3124 if (array)
3126 unsigned int absoff = array->type->typestring_offset;
3127 short reloff = absoff - *tfsoff;
3128 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3129 reloff, reloff, absoff);
3130 *tfsoff += 2;
3132 else if (fc == RPC_FC_BOGUS_STRUCT)
3134 print_file(file, 2, "NdrFcShort(0x0),\n");
3135 *tfsoff += 2;
3138 if (fc == RPC_FC_BOGUS_STRUCT)
3140 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3141 nothing is written to file yet. On the actual writing pass,
3142 this will have been updated. */
3143 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3144 int reloff = absoff - *tfsoff;
3145 assert( reloff >= 0 );
3146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3147 (unsigned short)reloff, reloff, absoff);
3148 *tfsoff += 2;
3150 else if ((fc == RPC_FC_PSTRUCT) ||
3151 (fc == RPC_FC_CPSTRUCT) ||
3152 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3154 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3155 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3156 *tfsoff += 2;
3157 write_pointer_description(file, type, tfsoff);
3158 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3159 *tfsoff += 1;
3162 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3163 tfsoff);
3165 if (fc == RPC_FC_BOGUS_STRUCT)
3167 const var_t *f;
3169 type->ptrdesc = *tfsoff;
3170 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3172 type_t *ft = f->type;
3173 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3175 case TGT_POINTER:
3176 if (is_string_type(f->attrs, ft))
3177 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3178 else
3179 write_pointer_tfs(file, f->attrs, ft,
3180 type_pointer_get_ref(ft)->typestring_offset,
3181 TYPE_CONTEXT_CONTAINER, tfsoff);
3182 break;
3183 case TGT_ARRAY:
3184 if (type_array_is_decl_as_ptr(ft))
3186 unsigned int offset;
3188 print_file(file, 0, "/* %d */\n", *tfsoff);
3190 offset = ft->typestring_offset;
3191 /* skip over the pointer that is written for strings, since a
3192 * pointer has to be written in-place here */
3193 if (is_string_type(f->attrs, ft))
3194 offset += 4;
3195 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3197 break;
3198 default:
3199 break;
3202 if (type->ptrdesc == *tfsoff)
3203 type->ptrdesc = 0;
3206 current_structure = save_current_structure;
3207 return start_offset;
3210 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3212 if (t == NULL)
3214 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3216 else
3218 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3220 unsigned char fc;
3221 if (type_get_type(t) == TYPE_BASIC)
3222 fc = get_basic_fc(t);
3223 else
3224 fc = get_enum_fc(t);
3225 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3226 fc, string_of_type(fc));
3228 else if (t->typestring_offset)
3230 short reloff = t->typestring_offset - *tfsoff;
3231 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3232 reloff, reloff, t->typestring_offset);
3234 else
3235 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3238 *tfsoff += 2;
3241 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3242 type_t *type, unsigned int *tfsoff)
3244 unsigned int start_offset;
3245 unsigned int size;
3246 var_list_t *fields;
3247 unsigned int nbranch = 0;
3248 type_t *deftype = NULL;
3249 short nodeftype = 0xffff;
3250 var_t *f;
3252 if (processed(type) &&
3253 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3254 return type->typestring_offset;
3256 guard_rec(type);
3258 size = type_memsize(type);
3260 fields = type_union_get_cases(type);
3262 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3264 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3265 if (cases)
3266 nbranch += list_count(cases);
3267 if (f->type)
3268 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3271 start_offset = *tfsoff;
3272 update_tfsoff(type, start_offset, file);
3273 print_start_tfs_comment(file, type, start_offset);
3274 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3276 const var_t *sv = type_union_get_switch_value(type);
3277 const type_t *st = sv->type;
3278 unsigned char fc;
3280 if (type_get_type(st) == TYPE_BASIC)
3282 fc = get_basic_fc(st);
3283 switch (fc)
3285 case RPC_FC_CHAR:
3286 case RPC_FC_SMALL:
3287 case RPC_FC_BYTE:
3288 case RPC_FC_USMALL:
3289 case RPC_FC_WCHAR:
3290 case RPC_FC_SHORT:
3291 case RPC_FC_USHORT:
3292 case RPC_FC_LONG:
3293 case RPC_FC_ULONG:
3294 break;
3295 default:
3296 fc = 0;
3297 error("union switch type must be an integer, char, or enum\n");
3300 else if (type_get_type(st) == TYPE_ENUM)
3301 fc = get_enum_fc(st);
3302 else
3303 error("union switch type must be an integer, char, or enum\n");
3305 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3306 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3307 0x40 | fc, string_of_type(fc));
3308 *tfsoff += 2;
3310 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3312 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3313 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3314 unsigned char fc;
3316 if (type_get_type(st) == TYPE_BASIC)
3318 fc = get_basic_fc(st);
3319 switch (fc)
3321 case RPC_FC_CHAR:
3322 case RPC_FC_SMALL:
3323 case RPC_FC_USMALL:
3324 case RPC_FC_SHORT:
3325 case RPC_FC_USHORT:
3326 case RPC_FC_LONG:
3327 case RPC_FC_ULONG:
3328 case RPC_FC_ENUM16:
3329 case RPC_FC_ENUM32:
3330 break;
3331 default:
3332 fc = 0;
3333 error("union switch type must be an integer, char, or enum\n");
3336 else if (type_get_type(st) == TYPE_ENUM)
3337 fc = get_enum_fc(st);
3338 else
3339 error("union switch type must be an integer, char, or enum\n");
3341 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3342 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3343 fc, string_of_type(fc));
3344 *tfsoff += 2;
3345 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3346 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3347 *tfsoff += 2;
3348 print_file(file, 0, "/* %u */\n", *tfsoff);
3351 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3352 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3353 *tfsoff += 4;
3355 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3357 type_t *ft = f->type;
3358 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3359 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3360 expr_t *c;
3362 if (cases == NULL && !deflt)
3363 error("union field %s with neither case nor default attribute\n", f->name);
3365 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3367 /* MIDL doesn't check for duplicate cases, even though that seems
3368 like a reasonable thing to do, it just dumps them to the TFS
3369 like we're going to do here. */
3370 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3371 *tfsoff += 4;
3372 write_branch_type(file, ft, tfsoff);
3375 /* MIDL allows multiple default branches, even though that seems
3376 illogical, it just chooses the last one, which is what we will
3377 do. */
3378 if (deflt)
3380 deftype = ft;
3381 nodeftype = 0;
3385 if (deftype)
3387 write_branch_type(file, deftype, tfsoff);
3389 else
3391 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3392 *tfsoff += 2;
3395 return start_offset;
3398 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3399 unsigned int *typeformat_offset)
3401 unsigned int i;
3402 unsigned int start_offset = *typeformat_offset;
3403 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3405 if (!iid && processed(type)) return type->typestring_offset;
3407 print_start_tfs_comment(file, type, start_offset);
3408 update_tfsoff(type, start_offset, file);
3410 if (iid)
3412 print_file(file, 2, "0x2f, /* FC_IP */\n");
3413 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3414 *typeformat_offset
3415 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3417 else
3419 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3420 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3422 if (! uuid)
3423 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3425 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3426 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3427 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3428 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3429 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3430 for (i = 0; i < 8; ++i)
3431 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3433 if (file)
3434 fprintf(file, "\n");
3436 *typeformat_offset += 18;
3438 return start_offset;
3441 static unsigned int write_contexthandle_tfs(FILE *file,
3442 const attr_list_t *attrs,
3443 type_t *type,
3444 int toplevel_param,
3445 unsigned int *typeformat_offset)
3447 unsigned int start_offset = *typeformat_offset;
3448 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3450 print_start_tfs_comment(file, type, start_offset);
3452 if (flags & 0x80) /* via ptr */
3454 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3455 if (!pointer_type) pointer_type = RPC_FC_RP;
3456 *typeformat_offset += 4;
3457 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3458 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3459 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3462 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3463 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3464 /* return and can't be null values overlap */
3465 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3466 print_file(file, 0, "can't be null, ");
3467 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3468 print_file(file, 0, "serialize, ");
3469 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3470 print_file(file, 0, "no serialize, ");
3471 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3472 print_file(file, 0, "strict, ");
3473 if ((flags & 0x21) == 0x20)
3474 print_file(file, 0, "out, ");
3475 if ((flags & 0x21) == 0x21)
3476 print_file(file, 0, "return, ");
3477 if (flags & 0x40)
3478 print_file(file, 0, "in, ");
3479 if (flags & 0x80)
3480 print_file(file, 0, "via ptr, ");
3481 print_file(file, 0, "*/\n");
3482 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3483 print_file(file, 2, "0, /* FIXME: param num */\n");
3484 *typeformat_offset += 4;
3486 update_tfsoff( type, start_offset, file );
3487 return start_offset;
3490 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3491 type_t *type, expr_list_t *range_list,
3492 unsigned int *typeformat_offset)
3494 unsigned char fc;
3495 unsigned int start_offset = *typeformat_offset;
3496 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3497 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3499 if (type_get_type(type) == TYPE_BASIC)
3500 fc = get_basic_fc(type);
3501 else
3502 fc = get_enum_fc(type);
3504 /* fc must fit in lower 4-bits of 8-bit field below */
3505 assert(fc <= 0xf);
3507 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3508 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3509 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3510 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3511 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3512 update_tfsoff( type, start_offset, file );
3513 *typeformat_offset += 10;
3515 return start_offset;
3518 static unsigned int write_type_tfs(FILE *file, int indent,
3519 const attr_list_t *attrs, type_t *type,
3520 const char *name,
3521 enum type_context context,
3522 unsigned int *typeformat_offset)
3524 unsigned int offset;
3526 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3528 case TGT_CTXT_HANDLE:
3529 case TGT_CTXT_HANDLE_POINTER:
3530 return write_contexthandle_tfs(file, attrs, type,
3531 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3532 case TGT_USER_TYPE:
3533 return write_user_tfs(file, type, typeformat_offset);
3534 case TGT_STRING:
3535 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3536 case TGT_ARRAY:
3538 unsigned int off;
3539 /* conformant and pointer arrays are handled specially */
3540 if ((context != TYPE_CONTEXT_CONTAINER &&
3541 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3542 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3543 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3544 else
3545 off = 0;
3546 if (context != TYPE_CONTEXT_CONTAINER &&
3547 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3549 int ptr_type;
3550 ptr_type = get_pointer_fc(type, attrs,
3551 context == TYPE_CONTEXT_TOPLEVELPARAM);
3552 if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3554 unsigned int absoff = type->typestring_offset;
3555 short reloff = absoff - (*typeformat_offset + 2);
3556 off = *typeformat_offset;
3557 print_file(file, 0, "/* %d */\n", off);
3558 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3559 string_of_type(ptr_type));
3560 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3561 reloff, reloff, absoff);
3562 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3563 *typeformat_offset += 4;
3565 type->details.array.ptr_tfsoff = off;
3567 return off;
3569 case TGT_STRUCT:
3570 return write_struct_tfs(file, type, name, typeformat_offset);
3571 case TGT_UNION:
3572 return write_union_tfs(file, attrs, type, typeformat_offset);
3573 case TGT_ENUM:
3574 case TGT_BASIC:
3575 /* nothing to do */
3576 return 0;
3577 case TGT_RANGE:
3579 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3580 if (!range_list)
3581 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3582 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3584 case TGT_IFACE_POINTER:
3585 return write_ip_tfs(file, attrs, type, typeformat_offset);
3586 case TGT_POINTER:
3588 enum type_context ref_context;
3589 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3590 ref_context = TYPE_CONTEXT_PARAM;
3591 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3592 ref_context = TYPE_CONTEXT_CONTAINER;
3593 else
3594 ref_context = context;
3595 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3596 ref_context, typeformat_offset);
3597 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3598 return 0;
3599 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3601 case TGT_INVALID:
3602 break;
3604 error("invalid type %s for var %s\n", type->name, name);
3605 return 0;
3608 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3609 const char *name, int write_ptr, unsigned int *tfsoff)
3611 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3614 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3615 type_pred_t pred, unsigned int *typeformat_offset)
3617 var_t *var;
3618 const statement_t *stmt;
3620 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3622 const type_t *iface;
3623 const statement_t *stmt_func;
3625 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3626 continue;
3628 iface = stmt->u.type;
3629 if (!pred(iface))
3630 continue;
3632 current_iface = iface;
3633 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3635 const var_t *func = stmt_func->u.var;
3636 current_func = func;
3637 if (is_local(func->attrs)) continue;
3639 var = type_function_get_retval(func->type);
3640 if (!is_void(var->type))
3641 var->typestring_offset = write_type_tfs( file, 2, func->attrs, var->type, func->name,
3642 TYPE_CONTEXT_PARAM, typeformat_offset);
3644 if (type_get_function_args(func->type))
3645 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), var_t, entry )
3646 var->typestring_offset = write_type_tfs( file, 2, var->attrs, var->type, var->name,
3647 TYPE_CONTEXT_TOPLEVELPARAM,
3648 typeformat_offset );
3652 return *typeformat_offset + 1;
3655 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3657 unsigned int typeformat_offset = 2;
3659 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3663 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3665 int indent = 0;
3667 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3668 print_file(file, indent, "{\n");
3669 indent++;
3670 print_file(file, indent, "0,\n");
3671 print_file(file, indent, "{\n");
3672 indent++;
3673 print_file(file, indent, "NdrFcShort(0x0),\n");
3675 set_all_tfswrite(TRUE);
3676 process_tfs(file, stmts, pred);
3678 print_file(file, indent, "0x0\n");
3679 indent--;
3680 print_file(file, indent, "}\n");
3681 indent--;
3682 print_file(file, indent, "};\n");
3683 print_file(file, indent, "\n");
3686 static unsigned int get_required_buffer_size_type(
3687 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3689 *alignment = 0;
3690 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3692 case TGT_USER_TYPE:
3694 const char *uname;
3695 const type_t *utype = get_user_type(type, &uname);
3696 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3698 case TGT_BASIC:
3699 switch (get_basic_fc(type))
3701 case RPC_FC_BYTE:
3702 case RPC_FC_CHAR:
3703 case RPC_FC_USMALL:
3704 case RPC_FC_SMALL:
3705 *alignment = 4;
3706 return 1;
3708 case RPC_FC_WCHAR:
3709 case RPC_FC_USHORT:
3710 case RPC_FC_SHORT:
3711 *alignment = 4;
3712 return 2;
3714 case RPC_FC_ULONG:
3715 case RPC_FC_LONG:
3716 case RPC_FC_FLOAT:
3717 case RPC_FC_ERROR_STATUS_T:
3718 *alignment = 4;
3719 return 4;
3721 case RPC_FC_HYPER:
3722 case RPC_FC_DOUBLE:
3723 *alignment = 8;
3724 return 8;
3726 case RPC_FC_INT3264:
3727 case RPC_FC_UINT3264:
3728 assert( pointer_size );
3729 *alignment = pointer_size;
3730 return pointer_size;
3732 case RPC_FC_IGNORE:
3733 case RPC_FC_BIND_PRIMITIVE:
3734 return 0;
3736 default:
3737 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3738 get_basic_fc(type));
3739 return 0;
3741 break;
3743 case TGT_ENUM:
3744 switch (get_enum_fc(type))
3746 case RPC_FC_ENUM32:
3747 *alignment = 4;
3748 return 4;
3749 case RPC_FC_ENUM16:
3750 *alignment = 4;
3751 return 2;
3753 break;
3755 case TGT_STRUCT:
3756 if (get_struct_fc(type) == RPC_FC_STRUCT)
3758 if (!type_struct_get_fields(type)) return 0;
3759 return fields_memsize(type_struct_get_fields(type), alignment);
3761 break;
3763 case TGT_POINTER:
3765 unsigned int size, align;
3766 const type_t *ref = type_pointer_get_ref(type);
3767 if (is_string_type( attrs, ref )) break;
3768 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3769 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3771 size += 4 + align;
3772 align = 4;
3774 *alignment = align;
3775 return size;
3778 case TGT_ARRAY:
3779 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3781 switch (get_array_fc(type))
3783 case RPC_FC_SMFARRAY:
3784 case RPC_FC_LGFARRAY:
3785 return type_array_get_dim(type) *
3786 get_required_buffer_size_type(type_array_get_element(type), name,
3787 NULL, FALSE, alignment);
3790 break;
3792 default:
3793 break;
3795 return 0;
3798 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3800 int in_attr = is_attr(var->attrs, ATTR_IN);
3801 int out_attr = is_attr(var->attrs, ATTR_OUT);
3803 if (!in_attr && !out_attr)
3804 in_attr = 1;
3806 *alignment = 0;
3808 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3809 pass == PASS_RETURN)
3811 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3813 *alignment = 4;
3814 return 20;
3817 if (!is_string_type(var->attrs, var->type))
3818 return get_required_buffer_size_type(var->type, var->name,
3819 var->attrs, TRUE, alignment);
3821 return 0;
3824 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3826 const var_t *var;
3827 unsigned int total_size = 0, alignment;
3829 if (type_get_function_args(func->type))
3831 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3833 total_size += get_required_buffer_size(var, &alignment, pass);
3834 total_size += alignment;
3838 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3840 var_t v = *func;
3841 v.type = type_function_get_rettype(func->type);
3842 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3843 total_size += alignment;
3845 return total_size;
3848 static void print_phase_function(FILE *file, int indent, const char *type,
3849 const char *local_var_prefix, enum remoting_phase phase,
3850 const var_t *var, unsigned int type_offset)
3852 const char *function;
3853 switch (phase)
3855 case PHASE_BUFFERSIZE:
3856 function = "BufferSize";
3857 break;
3858 case PHASE_MARSHAL:
3859 function = "Marshall";
3860 break;
3861 case PHASE_UNMARSHAL:
3862 function = "Unmarshall";
3863 break;
3864 case PHASE_FREE:
3865 function = "Free";
3866 break;
3867 default:
3868 assert(0);
3869 return;
3872 print_file(file, indent, "Ndr%s%s(\n", type, function);
3873 indent++;
3874 print_file(file, indent, "&__frame->_StubMsg,\n");
3875 print_file(file, indent, "%s%s%s%s%s,\n",
3876 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3877 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3878 local_var_prefix,
3879 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3880 var->name);
3881 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3882 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3883 if (phase == PHASE_UNMARSHAL)
3884 print_file(file, indent, "0);\n");
3885 indent--;
3888 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3889 enum remoting_phase phase, enum pass pass, const var_t *var,
3890 const char *varname)
3892 type_t *type = var->type;
3893 unsigned int alignment = 0;
3895 /* no work to do for other phases, buffer sizing is done elsewhere */
3896 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3897 return;
3899 if (type_get_type(type) == TYPE_ENUM ||
3900 (type_get_type(type) == TYPE_BASIC &&
3901 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3902 pointer_size != 4))
3904 unsigned char fc;
3906 if (type_get_type(type) == TYPE_ENUM)
3907 fc = get_enum_fc(type);
3908 else
3909 fc = get_basic_fc(type);
3911 if (phase == PHASE_MARSHAL)
3912 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3913 else
3914 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3915 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3916 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3917 local_var_prefix,
3918 var->name);
3919 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3921 else
3923 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3924 switch (get_basic_fc(ref))
3926 case RPC_FC_BYTE:
3927 case RPC_FC_CHAR:
3928 case RPC_FC_SMALL:
3929 case RPC_FC_USMALL:
3930 alignment = 1;
3931 break;
3933 case RPC_FC_WCHAR:
3934 case RPC_FC_USHORT:
3935 case RPC_FC_SHORT:
3936 alignment = 2;
3937 break;
3939 case RPC_FC_ULONG:
3940 case RPC_FC_LONG:
3941 case RPC_FC_FLOAT:
3942 case RPC_FC_ERROR_STATUS_T:
3943 /* pointer_size must be 4 if we got here in these two cases */
3944 case RPC_FC_INT3264:
3945 case RPC_FC_UINT3264:
3946 alignment = 4;
3947 break;
3949 case RPC_FC_HYPER:
3950 case RPC_FC_DOUBLE:
3951 alignment = 8;
3952 break;
3954 case RPC_FC_IGNORE:
3955 case RPC_FC_BIND_PRIMITIVE:
3956 /* no marshalling needed */
3957 return;
3959 default:
3960 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3961 var->name, get_basic_fc(ref));
3964 if (phase == PHASE_MARSHAL && alignment > 1)
3965 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3966 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3967 alignment - 1, alignment - 1);
3969 if (phase == PHASE_MARSHAL)
3971 print_file(file, indent, "*(");
3972 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3973 if (is_ptr(type))
3974 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3975 else
3976 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3977 fprintf(file, "%s%s", local_var_prefix, varname);
3978 fprintf(file, ";\n");
3980 else if (phase == PHASE_UNMARSHAL)
3982 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3983 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3984 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3985 print_file(file, indent, "{\n");
3986 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3987 print_file(file, indent, "}\n");
3988 print_file(file, indent, "%s%s%s",
3989 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3990 local_var_prefix, varname);
3991 if (pass == PASS_IN && is_ptr(type))
3992 fprintf(file, " = (");
3993 else
3994 fprintf(file, " = *(");
3995 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3996 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3999 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4000 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4001 fprintf(file, ");\n");
4005 /* returns whether the MaxCount, Offset or ActualCount members need to be
4006 * filled in for the specified phase */
4007 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4009 return (phase != PHASE_UNMARSHAL);
4012 expr_t *get_size_is_expr(const type_t *t, const char *name)
4014 expr_t *x = NULL;
4016 for ( ; is_array(t); t = type_array_get_element(t))
4017 if (type_array_has_conformance(t) &&
4018 type_array_get_conformance(t)->type != EXPR_VOID)
4020 if (!x)
4021 x = type_array_get_conformance(t);
4022 else
4023 error("%s: multidimensional conformant"
4024 " arrays not supported at the top level\n",
4025 name);
4028 return x;
4031 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4032 enum remoting_phase phase, const var_t *var, int valid_variance)
4034 const type_t *type = var->type;
4035 /* get fundamental type for the argument */
4036 for (;;)
4038 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4040 case TGT_ARRAY:
4041 if (is_conformance_needed_for_phase(phase))
4043 if (type_array_has_conformance(type) &&
4044 type_array_get_conformance(type)->type != EXPR_VOID)
4046 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4047 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4048 fprintf(file, ";\n\n");
4050 if (type_array_has_variance(type))
4052 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4053 if (valid_variance)
4055 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4056 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4057 fprintf(file, ";\n\n");
4059 else
4060 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4063 break;
4064 case TGT_UNION:
4065 if (type_get_type(type) == TYPE_UNION &&
4066 is_conformance_needed_for_phase(phase))
4068 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4069 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4070 fprintf(file, ";\n\n");
4072 break;
4073 case TGT_IFACE_POINTER:
4075 expr_t *iid;
4077 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4079 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4080 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4081 fprintf( file, ";\n\n" );
4083 break;
4085 case TGT_POINTER:
4086 type = type_pointer_get_ref(type);
4087 continue;
4088 case TGT_INVALID:
4089 case TGT_USER_TYPE:
4090 case TGT_CTXT_HANDLE:
4091 case TGT_CTXT_HANDLE_POINTER:
4092 case TGT_STRING:
4093 case TGT_BASIC:
4094 case TGT_ENUM:
4095 case TGT_STRUCT:
4096 case TGT_RANGE:
4097 break;
4099 break;
4103 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4104 enum pass pass, enum remoting_phase phase, const var_t *var)
4106 int in_attr, out_attr, pointer_type;
4107 const char *type_str = NULL;
4108 const type_t *type = var->type;
4109 unsigned int alignment, start_offset = type->typestring_offset;
4111 if (is_ptr(type) || is_array(type))
4112 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4113 else
4114 pointer_type = 0;
4116 in_attr = is_attr(var->attrs, ATTR_IN);
4117 out_attr = is_attr(var->attrs, ATTR_OUT);
4118 if (!in_attr && !out_attr)
4119 in_attr = 1;
4121 if (phase != PHASE_FREE)
4122 switch (pass)
4124 case PASS_IN:
4125 if (!in_attr) return;
4126 break;
4127 case PASS_OUT:
4128 if (!out_attr) return;
4129 break;
4130 case PASS_RETURN:
4131 break;
4134 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4136 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4138 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4140 case TGT_CTXT_HANDLE:
4141 case TGT_CTXT_HANDLE_POINTER:
4142 if (phase == PHASE_MARSHAL)
4144 if (pass == PASS_IN)
4146 /* if the context_handle attribute appears in the chain of types
4147 * without pointers being followed, then the context handle must
4148 * be direct, otherwise it is a pointer */
4149 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
4150 print_file(file, indent, "NdrClientContextMarshall(\n");
4151 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4152 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
4153 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4155 else
4157 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4158 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4159 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4160 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4161 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4164 else if (phase == PHASE_UNMARSHAL)
4166 if (pass == PASS_OUT)
4168 if (!in_attr)
4169 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4170 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4171 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4172 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4173 print_file(file, indent + 1, "__frame->_Handle);\n");
4175 else
4177 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4178 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4179 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4182 break;
4183 case TGT_USER_TYPE:
4184 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4185 break;
4186 case TGT_STRING:
4187 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4188 pointer_type != RPC_FC_RP)
4190 /* strings returned are assumed to be global and hence don't
4191 * need freeing */
4192 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4193 print_phase_function(file, indent, "Pointer", local_var_prefix,
4194 phase, var, start_offset);
4195 else if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4196 !in_attr && is_conformant_array(type))
4198 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4199 indent++;
4200 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4203 else
4205 unsigned int real_start_offset = start_offset;
4206 /* skip over pointer description straight to string description */
4207 if (is_declptr(type))
4209 if (is_conformant_array(type))
4210 real_start_offset += 4;
4211 else
4212 real_start_offset += 2;
4214 if (is_array(type) && !is_conformant_array(type))
4215 print_phase_function(file, indent, "NonConformantString",
4216 local_var_prefix, phase, var,
4217 real_start_offset);
4218 else
4219 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4220 phase, var, real_start_offset);
4222 break;
4223 case TGT_ARRAY:
4225 unsigned char tc = get_array_fc(type);
4226 const char *array_type = NULL;
4228 /* We already have the size_is expression since it's at the
4229 top level, but do checks for multidimensional conformant
4230 arrays. When we handle them, we'll need to extend this
4231 function to return a list, and then we'll actually use
4232 the return value. */
4233 get_size_is_expr(type, var->name);
4235 switch (tc)
4237 case RPC_FC_SMFARRAY:
4238 case RPC_FC_LGFARRAY:
4239 array_type = "FixedArray";
4240 break;
4241 case RPC_FC_SMVARRAY:
4242 case RPC_FC_LGVARRAY:
4243 array_type = "VaryingArray";
4244 break;
4245 case RPC_FC_CARRAY:
4246 array_type = "ConformantArray";
4247 break;
4248 case RPC_FC_CVARRAY:
4249 array_type = "ConformantVaryingArray";
4250 break;
4251 case RPC_FC_BOGUS_ARRAY:
4252 array_type = "ComplexArray";
4253 break;
4256 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4258 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4260 /* these are all unmarshalled by allocating memory */
4261 if (tc == RPC_FC_BOGUS_ARRAY ||
4262 tc == RPC_FC_CVARRAY ||
4263 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4264 (tc == RPC_FC_CARRAY && !in_attr))
4266 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff)
4268 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4269 type->details.array.ptr_tfsoff);
4270 break;
4272 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4273 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4274 indent++;
4275 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4276 break;
4279 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4280 break;
4282 case TGT_BASIC:
4283 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4284 break;
4285 case TGT_ENUM:
4286 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4287 break;
4288 case TGT_RANGE:
4289 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4290 /* Note: this goes beyond what MIDL does - it only supports arguments
4291 * with the [range] attribute in Oicf mode */
4292 if (phase == PHASE_UNMARSHAL)
4294 const expr_t *range_min;
4295 const expr_t *range_max;
4296 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4297 if (!range_list)
4298 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4299 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4300 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4302 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4303 write_type_decl(file, var->type, NULL);
4304 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4305 write_type_decl(file, var->type, NULL);
4306 fprintf(file, ")0x%x))\n", range_max->cval);
4307 print_file(file, indent, "{\n");
4308 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4309 print_file(file, indent, "}\n");
4311 break;
4312 case TGT_STRUCT:
4313 switch (get_struct_fc(type))
4315 case RPC_FC_STRUCT:
4316 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4317 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4318 break;
4319 case RPC_FC_PSTRUCT:
4320 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4321 break;
4322 case RPC_FC_CSTRUCT:
4323 case RPC_FC_CPSTRUCT:
4324 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4325 break;
4326 case RPC_FC_CVSTRUCT:
4327 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4328 break;
4329 case RPC_FC_BOGUS_STRUCT:
4330 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4331 break;
4332 default:
4333 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4335 break;
4336 case TGT_UNION:
4338 const char *union_type = NULL;
4340 if (type_get_type(type) == TYPE_UNION)
4341 union_type = "NonEncapsulatedUnion";
4342 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4343 union_type = "EncapsulatedUnion";
4345 print_phase_function(file, indent, union_type, local_var_prefix,
4346 phase, var, start_offset);
4347 break;
4349 case TGT_POINTER:
4351 const type_t *ref = type_pointer_get_ref(type);
4352 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4354 case TGT_BASIC:
4355 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4356 break;
4357 case TGT_ENUM:
4358 /* base types have known sizes, so don't need a sizing pass
4359 * and don't have any memory to free and so don't need a
4360 * freeing pass */
4361 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4362 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4363 break;
4364 case TGT_STRUCT:
4365 switch (get_struct_fc(ref))
4367 case RPC_FC_STRUCT:
4368 /* simple structs have known sizes, so don't need a sizing
4369 * pass and don't have any memory to free and so don't
4370 * need a freeing pass */
4371 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4372 type_str = "SimpleStruct";
4373 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4375 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4376 indent++;
4377 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4378 indent--;
4380 break;
4381 case RPC_FC_PSTRUCT:
4382 type_str = "SimpleStruct";
4383 break;
4384 case RPC_FC_CSTRUCT:
4385 case RPC_FC_CPSTRUCT:
4386 type_str = "ConformantStruct";
4387 break;
4388 case RPC_FC_CVSTRUCT:
4389 type_str = "ConformantVaryingStruct";
4390 break;
4391 case RPC_FC_BOGUS_STRUCT:
4392 type_str = "ComplexStruct";
4393 break;
4394 default:
4395 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4398 if (type_str)
4400 if (phase == PHASE_FREE)
4401 type_str = "Pointer";
4402 else
4403 start_offset = ref->typestring_offset;
4404 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4406 break;
4407 case TGT_UNION:
4408 if (phase == PHASE_FREE)
4409 type_str = "Pointer";
4410 else
4412 if (type_get_type(ref) == TYPE_UNION)
4413 type_str = "NonEncapsulatedUnion";
4414 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4415 type_str = "EncapsulatedUnion";
4417 start_offset = ref->typestring_offset;
4420 print_phase_function(file, indent, type_str, local_var_prefix,
4421 phase, var, start_offset);
4422 break;
4423 case TGT_USER_TYPE:
4424 if (phase != PHASE_FREE)
4426 type_str = "UserMarshal";
4427 start_offset = ref->typestring_offset;
4429 else type_str = "Pointer";
4431 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4432 break;
4433 case TGT_STRING:
4434 case TGT_POINTER:
4435 case TGT_ARRAY:
4436 case TGT_RANGE:
4437 case TGT_IFACE_POINTER:
4438 case TGT_CTXT_HANDLE:
4439 case TGT_CTXT_HANDLE_POINTER:
4440 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4441 break;
4442 case TGT_INVALID:
4443 assert(0);
4444 break;
4446 else
4447 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4448 break;
4450 case TGT_IFACE_POINTER:
4451 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4452 break;
4453 case TGT_INVALID:
4454 assert(0);
4455 break;
4457 fprintf(file, "\n");
4460 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4461 enum pass pass, enum remoting_phase phase)
4463 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4465 unsigned int size = get_function_buffer_size( func, pass );
4466 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4469 if (pass == PASS_RETURN)
4471 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4472 type_function_get_retval(func->type) );
4474 else
4476 const var_t *var;
4477 if (!type_get_function_args(func->type))
4478 return;
4479 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4480 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4485 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4487 unsigned int offset = 0;
4488 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4489 return offset;
4492 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4494 const statement_t *stmt;
4495 unsigned int size = 1;
4497 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4499 const type_t *iface;
4500 const statement_t *stmt_func;
4502 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4503 continue;
4505 iface = stmt->u.type;
4506 if (!pred(iface))
4507 continue;
4509 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4511 const var_t *func = stmt_func->u.var;
4512 if (!is_local(func->attrs))
4513 size += get_size_procformatstring_func( iface, func );
4516 return size;
4519 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4521 set_all_tfswrite(FALSE);
4522 return process_tfs(NULL, stmts, pred);
4525 void declare_stub_args( FILE *file, int indent, const var_t *func )
4527 int in_attr, out_attr;
4528 int i = 0;
4529 const var_t *var = type_function_get_retval(func->type);
4531 /* declare return value */
4532 if (!is_void(var->type))
4534 print_file(file, indent, "%s", "");
4535 write_type_decl(file, var->type, var->name);
4536 fprintf(file, ";\n");
4539 if (!type_get_function_args(func->type))
4540 return;
4542 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4544 in_attr = is_attr(var->attrs, ATTR_IN);
4545 out_attr = is_attr(var->attrs, ATTR_OUT);
4546 if (!out_attr && !in_attr)
4547 in_attr = 1;
4549 if (is_context_handle(var->type))
4550 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4551 else
4553 if (!in_attr && !is_conformant_array(var->type))
4555 type_t *type_to_print;
4556 char name[16];
4557 print_file(file, indent, "%s", "");
4558 if (type_get_type(var->type) == TYPE_ARRAY &&
4559 !type_array_is_decl_as_ptr(var->type))
4560 type_to_print = var->type;
4561 else
4562 type_to_print = type_pointer_get_ref(var->type);
4563 sprintf(name, "_W%u", i++);
4564 write_type_decl(file, type_to_print, name);
4565 fprintf(file, ";\n");
4568 print_file(file, indent, "%s", "");
4569 write_type_decl_left(file, var->type);
4570 fprintf(file, " ");
4571 if (type_get_type(var->type) == TYPE_ARRAY &&
4572 !type_array_is_decl_as_ptr(var->type)) {
4573 fprintf(file, "(*%s)", var->name);
4574 } else
4575 fprintf(file, "%s", var->name);
4576 write_type_right(file, var->type, FALSE);
4577 fprintf(file, ";\n");
4579 if (decl_indirect(var->type))
4580 print_file(file, indent, "void *_p_%s;\n", var->name);
4586 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4588 int in_attr, out_attr;
4589 int i = 0, sep = 0;
4590 const var_t *var;
4591 type_t *ref;
4593 if (!type_get_function_args(func->type))
4594 return;
4596 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4598 in_attr = is_attr(var->attrs, ATTR_IN);
4599 out_attr = is_attr(var->attrs, ATTR_OUT);
4600 if (!out_attr && !in_attr)
4601 in_attr = 1;
4603 if (!in_attr)
4605 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4607 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4609 case TGT_CTXT_HANDLE_POINTER:
4610 fprintf(file, " = NdrContextHandleInitialize(\n");
4611 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4612 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4613 var->typestring_offset);
4614 break;
4615 case TGT_ARRAY:
4616 if (type_array_has_conformance(var->type))
4618 unsigned int size;
4619 type_t *type;
4621 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4622 for (type = var->type;
4623 is_array(type) && type_array_has_conformance(type);
4624 type = type_array_get_element(type))
4626 write_expr(file, type_array_get_conformance(type), TRUE,
4627 TRUE, NULL, NULL, local_var_prefix);
4628 fprintf(file, " * ");
4630 size = type_memsize(type);
4631 fprintf(file, "%u);\n", size);
4633 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4634 for (type = var->type;
4635 is_array(type) && type_array_has_conformance(type);
4636 type = type_array_get_element(type))
4638 write_expr(file, type_array_get_conformance(type), TRUE,
4639 TRUE, NULL, NULL, local_var_prefix);
4640 fprintf(file, " * ");
4642 size = type_memsize(type);
4643 fprintf(file, "%u);\n", size);
4645 else
4646 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4647 break;
4648 case TGT_POINTER:
4649 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4650 ref = type_pointer_get_ref(var->type);
4651 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4653 case TGT_BASIC:
4654 case TGT_ENUM:
4655 case TGT_POINTER:
4656 case TGT_RANGE:
4657 case TGT_IFACE_POINTER:
4658 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4659 break;
4660 case TGT_USER_TYPE:
4661 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4662 local_var_prefix, i, local_var_prefix, i);
4663 break;
4664 case TGT_ARRAY:
4665 if (type_array_is_decl_as_ptr(ref))
4667 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4668 break;
4670 ref = type_array_get_element(ref);
4671 /* fall through */
4672 case TGT_STRUCT:
4673 case TGT_UNION:
4674 if (type_has_pointers(ref))
4675 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4676 local_var_prefix, i, local_var_prefix, i);
4677 break;
4678 case TGT_CTXT_HANDLE:
4679 case TGT_CTXT_HANDLE_POINTER:
4680 case TGT_INVALID:
4681 case TGT_STRING:
4682 /* not initialised */
4683 break;
4685 i++;
4686 break;
4687 default:
4688 break;
4691 sep = 1;
4694 if (sep)
4695 fprintf(file, "\n");
4699 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4700 const char *var_decl, int add_retval )
4702 var_t *retval = type_function_get_retval( func );
4703 const var_list_t *args = type_get_function_args( func );
4704 const var_t *arg;
4705 int needs_packing;
4706 unsigned int align = 0;
4708 if (args)
4709 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4710 if (!is_array( arg->type )) type_memsize_and_alignment( arg->type, &align );
4712 needs_packing = (align > pointer_size);
4714 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4715 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4716 print_file(file, 1, "{\n" );
4717 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4719 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4721 print_file(file, 2, "%s", "");
4722 write_type_left( file, (type_t *)arg->type, TRUE );
4723 if (needs_space_after( arg->type )) fputc( ' ', file );
4724 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4726 /* FIXME: should check for large args being passed by pointer */
4727 align = 0;
4728 if (is_array( arg->type ) || is_ptr( arg->type )) align = pointer_size;
4729 else type_memsize_and_alignment( arg->type, &align );
4731 if (align >= pointer_size)
4732 fprintf( file, "%s;\n", arg->name );
4733 else
4734 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4736 if (add_retval && !is_void( retval->type ))
4738 print_file(file, 2, "%s", "");
4739 write_type_decl( file, retval->type, retval->name );
4740 if (is_array( retval->type ) || is_ptr( retval->type ) ||
4741 type_memsize( retval->type ) == pointer_size)
4742 fprintf( file, ";\n" );
4743 else
4744 fprintf( file, " DECLSPEC_ALIGN(%u);\n", pointer_size );
4746 print_file(file, 1, "} %s;\n", var_decl );
4747 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4748 print_file( file, 0, "\n" );
4751 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4753 const var_list_t *args = type_get_function_args( func->type );
4754 const var_t *var;
4756 if (!args) return;
4758 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4759 if (cant_be_null( var ))
4760 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4763 int write_expr_eval_routines(FILE *file, const char *iface)
4765 static const char *var_name = "pS";
4766 static const char *var_name_expr = "pS->";
4767 int result = 0;
4768 struct expr_eval_routine *eval;
4769 unsigned short callback_offset = 0;
4771 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4773 const char *name = eval->name;
4774 result = 1;
4776 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4777 eval->iface ? eval->iface->name : iface, name, callback_offset);
4778 print_file(file, 0, "{\n");
4779 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4781 write_func_param_struct( file, eval->iface, eval->cont_type,
4782 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4784 else
4786 print_file(file, 1, "%s", "");
4787 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4788 fprintf(file, " *%s = (", var_name);
4789 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4790 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4792 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4793 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4794 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4795 fprintf(file, ";\n");
4796 print_file(file, 0, "}\n\n");
4797 callback_offset++;
4799 return result;
4802 void write_expr_eval_routine_list(FILE *file, const char *iface)
4804 struct expr_eval_routine *eval;
4805 struct expr_eval_routine *cursor;
4806 unsigned short callback_offset = 0;
4808 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4809 fprintf(file, "{\n");
4811 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4813 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4814 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4815 callback_offset++;
4816 list_remove(&eval->entry);
4817 free(eval->name);
4818 free(eval);
4821 fprintf(file, "};\n\n");
4824 void write_user_quad_list(FILE *file)
4826 user_type_t *ut;
4828 if (list_empty(&user_type_list))
4829 return;
4831 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4832 fprintf(file, "{\n");
4833 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4835 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4836 print_file(file, 1, "{\n");
4837 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4838 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4839 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4840 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4841 print_file(file, 1, "}%s\n", sep);
4843 fprintf(file, "};\n\n");
4846 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4848 const struct str_list_entry_t *endpoint;
4849 const char *p;
4851 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4852 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4853 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4855 print_file( f, 1, "{ (const unsigned char *)\"" );
4856 for (p = endpoint->str; *p && *p != ':'; p++)
4858 if (*p == '"' || *p == '\\') fputc( '\\', f );
4859 fputc( *p, f );
4861 if (!*p) goto error;
4862 if (p[1] != '[') goto error;
4864 fprintf( f, "\", (const unsigned char *)\"" );
4865 for (p += 2; *p && *p != ']'; p++)
4867 if (*p == '"' || *p == '\\') fputc( '\\', f );
4868 fputc( *p, f );
4870 if (*p != ']') goto error;
4871 fprintf( f, "\" },\n" );
4873 print_file( f, 0, "};\n\n" );
4874 return;
4876 error:
4877 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4880 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4881 const char *prefix, unsigned int proc_offset )
4883 type_t *rettype = type_function_get_rettype( func->type );
4884 int has_ret = !is_void( rettype );
4885 const var_list_t *args = type_get_function_args( func->type );
4886 const var_t *arg;
4887 int len, needs_params = 0;
4889 /* we need a param structure if we have more than one arg */
4890 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4892 print_file( file, 0, "{\n");
4893 if (needs_params)
4895 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4896 write_func_param_struct( file, iface, func->type, "__params", FALSE );
4897 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4898 if (args)
4899 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4900 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4902 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4904 len = fprintf( file, " %s%s( ",
4905 has_ret ? "_RetVal = " : "",
4906 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
4907 fprintf( file, "&%s_StubDesc,", prefix );
4908 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
4909 if (needs_params)
4911 fprintf( file, ",\n%*s&__params", len, "" );
4913 else if (pointer_size == 8)
4915 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
4916 if (args)
4917 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4918 fprintf( file, ",\n%*s%s", len, "", arg->name );
4920 else
4922 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
4923 else if (args)
4925 arg = LIST_ENTRY( list_head(args), const var_t, entry );
4926 fprintf( file, ",\n%*s&%s", len, "", arg->name );
4929 fprintf( file, " );\n" );
4930 if (has_ret)
4932 print_file( file, 1, "return (" );
4933 write_type_decl_left(file, rettype);
4934 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
4936 print_file( file, 0, "}\n\n");
4939 void write_exceptions( FILE *file )
4941 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4942 fprintf( file, "\n");
4943 fprintf( file, "#include \"wine/exception.h\"\n");
4944 fprintf( file, "#undef RpcTryExcept\n");
4945 fprintf( file, "#undef RpcExcept\n");
4946 fprintf( file, "#undef RpcEndExcept\n");
4947 fprintf( file, "#undef RpcTryFinally\n");
4948 fprintf( file, "#undef RpcFinally\n");
4949 fprintf( file, "#undef RpcEndFinally\n");
4950 fprintf( file, "#undef RpcExceptionCode\n");
4951 fprintf( file, "#undef RpcAbnormalTermination\n");
4952 fprintf( file, "\n");
4953 fprintf( file, "struct __exception_frame;\n");
4954 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4955 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4956 fprintf( file, "\n");
4957 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4958 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4959 fprintf( file, " __filter_func filter; \\\n");
4960 fprintf( file, " __finally_func finally; \\\n");
4961 fprintf( file, " sigjmp_buf jmp; \\\n");
4962 fprintf( file, " DWORD code; \\\n");
4963 fprintf( file, " unsigned char abnormal_termination; \\\n");
4964 fprintf( file, " unsigned char filter_level; \\\n");
4965 fprintf( file, " unsigned char finally_level;\n");
4966 fprintf( file, "\n");
4967 fprintf( file, "struct __exception_frame\n{\n");
4968 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4969 fprintf( file, "};\n");
4970 fprintf( file, "\n");
4971 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4972 fprintf( file, "{\n");
4973 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4974 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4975 fprintf( file, " {\n");
4976 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4977 fprintf( file, " exc_frame->finally( exc_frame );\n");
4978 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4979 fprintf( file, " }\n");
4980 fprintf( file, " exc_frame->filter_level = 0;\n");
4981 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4982 fprintf( file, "}\n");
4983 fprintf( file, "\n");
4984 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4985 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4986 fprintf( file, " CONTEXT *context,\n");
4987 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4988 fprintf( file, "{\n");
4989 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4990 fprintf( file, "\n");
4991 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4992 fprintf( file, " {\n" );
4993 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4994 fprintf( file, " {\n" );
4995 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4996 fprintf( file, " exc_frame->finally( exc_frame );\n");
4997 fprintf( file, " }\n" );
4998 fprintf( file, " return ExceptionContinueSearch;\n");
4999 fprintf( file, " }\n" );
5000 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5001 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5002 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5003 fprintf( file, " return ExceptionContinueSearch;\n");
5004 fprintf( file, "}\n");
5005 fprintf( file, "\n");
5006 fprintf( file, "#define RpcTryExcept \\\n");
5007 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
5008 fprintf( file, " { \\\n");
5009 fprintf( file, " if (!__frame->finally_level) \\\n" );
5010 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5011 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5012 fprintf( file, "\n");
5013 fprintf( file, "#define RpcExcept(expr) \\\n");
5014 fprintf( file, " if (!__frame->finally_level) \\\n" );
5015 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5016 fprintf( file, " __frame->filter_level = 0; \\\n" );
5017 fprintf( file, " } \\\n");
5018 fprintf( file, " else \\\n");
5019 fprintf( file, "\n");
5020 fprintf( file, "#define RpcEndExcept\n");
5021 fprintf( file, "\n");
5022 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5023 fprintf( file, "\n");
5024 fprintf( file, "#define RpcTryFinally \\\n");
5025 fprintf( file, " if (!__frame->filter_level) \\\n");
5026 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5027 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5028 fprintf( file, "\n");
5029 fprintf( file, "#define RpcFinally \\\n");
5030 fprintf( file, " if (!__frame->filter_level) \\\n");
5031 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5032 fprintf( file, " __frame->finally_level = 0;\n");
5033 fprintf( file, "\n");
5034 fprintf( file, "#define RpcEndFinally\n");
5035 fprintf( file, "\n");
5036 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5037 fprintf( file, "\n");
5038 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5039 fprintf( file, " do { \\\n");
5040 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5041 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5042 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5043 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5044 fprintf( file, " __frame->filter_level = 0; \\\n");
5045 fprintf( file, " __frame->finally_level = 0; \\\n");
5046 fprintf( file, " } while (0)\n");
5047 fprintf( file, "\n");
5048 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5049 fprintf( file, "\n");
5050 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5051 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5052 fprintf( file, "\n");
5053 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5054 fprintf( file, " DWORD code;\n");
5055 fprintf( file, "\n");
5056 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");