1 /*
2 * Format String Generator for IDL Compiler
3 *
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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>
34
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
40
41 #include "typegen.h"
42 #include "expr.h"
43
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)))
48
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
52
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
55 {
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
60 };
61
62 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
63 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
64 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
65 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
66 const char *name, int write_ptr, unsigned int *tfsoff);
67 static const var_t *find_array_or_string_in_struct(const type_t *type);
68 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
69 type_t *type, int toplevel_param,
70 const char *name, unsigned int *typestring_offset);
71
72 const char *string_of_type(unsigned char type)
73 {
74 switch (type)
75 {
76 case RPC_FC_BYTE: return "FC_BYTE";
77 case RPC_FC_CHAR: return "FC_CHAR";
78 case RPC_FC_SMALL: return "FC_SMALL";
79 case RPC_FC_USMALL: return "FC_USMALL";
80 case RPC_FC_WCHAR: return "FC_WCHAR";
81 case RPC_FC_SHORT: return "FC_SHORT";
82 case RPC_FC_USHORT: return "FC_USHORT";
83 case RPC_FC_LONG: return "FC_LONG";
84 case RPC_FC_ULONG: return "FC_ULONG";
85 case RPC_FC_FLOAT: return "FC_FLOAT";
86 case RPC_FC_HYPER: return "FC_HYPER";
87 case RPC_FC_DOUBLE: return "FC_DOUBLE";
88 case RPC_FC_ENUM16: return "FC_ENUM16";
89 case RPC_FC_ENUM32: return "FC_ENUM32";
90 case RPC_FC_IGNORE: return "FC_IGNORE";
91 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
92 case RPC_FC_RP: return "FC_RP";
93 case RPC_FC_UP: return "FC_UP";
94 case RPC_FC_OP: return "FC_OP";
95 case RPC_FC_FP: return "FC_FP";
96 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
97 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
98 case RPC_FC_STRUCT: return "FC_STRUCT";
99 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
100 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
101 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
102 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
103 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
104 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
105 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
106 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
107 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
108 case RPC_FC_CARRAY: return "FC_CARRAY";
109 case RPC_FC_CVARRAY: return "FC_CVARRAY";
110 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
111 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
112 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
113 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
114 case RPC_FC_POINTER: return "FC_POINTER";
115 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
116 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
117 case RPC_FC_CSTRING: return "FC_CSTRING";
118 case RPC_FC_WSTRING: return "FC_WSTRING";
119 case RPC_FC_INT3264: return "FC_INT3264";
120 case RPC_FC_UINT3264: return "FC_UINT3264";
121 default:
122 error("string_of_type: unknown type 0x%02x\n", type);
123 return NULL;
124 }
125 }
126
127 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
128 {
129 const type_t *t = type;
130 for (;;)
131 {
132 if (is_attr(t->attrs, attr))
133 return get_attrp(t->attrs, attr);
134 else if (type_is_alias(t))
135 t = type_alias_get_aliasee(t);
136 else return 0;
137 }
138 }
139
140 unsigned char get_basic_fc(const type_t *type)
141 {
142 int sign = type_basic_get_sign(type);
143 switch (type_basic_get_type(type))
144 {
145 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
146 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
147 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
148 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
149 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
150 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
151 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
152 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
153 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
154 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
155 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
156 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
157 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
158 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
159 }
160 return 0;
161 }
162
163 static inline unsigned int clamp_align(unsigned int align)
164 {
165 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
166 if(align > packing) align = packing;
167 return align;
168 }
169
170 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
171 {
172 const type_t *t;
173 int pointer_type;
174
175 assert(is_ptr(type) || is_array(type));
176
177 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
178 if (pointer_type)
179 return pointer_type;
180
181 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
182 {
183 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
184 if (pointer_type)
185 return pointer_type;
186 }
187
188 if (toplevel_param)
189 return RPC_FC_RP;
190 else if (is_ptr(type))
191 return type_pointer_get_default_fc(type);
192 else
193 return type_array_get_ptr_default_fc(type);
194 }
195
196 static unsigned char get_enum_fc(const type_t *type)
197 {
198 assert(type_get_type(type) == TYPE_ENUM);
199 if (is_aliaschain_attr(type, ATTR_V1ENUM))
200 return RPC_FC_ENUM32;
201 else
202 return RPC_FC_ENUM16;
203 }
204
205 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
206 {
207 if (is_user_type(type))
208 return TGT_USER_TYPE;
209
210 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
211 return TGT_CTXT_HANDLE;
212
213 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
214 return TGT_STRING;
215
216 switch (type_get_type(type))
217 {
218 case TYPE_BASIC:
219 if (!(flags & TDT_IGNORE_RANGES) &&
220 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
221 return TGT_RANGE;
222 return TGT_BASIC;
223 case TYPE_ENUM:
224 if (!(flags & TDT_IGNORE_RANGES) &&
225 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
226 return TGT_RANGE;
227 return TGT_ENUM;
228 case TYPE_POINTER:
229 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
230 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
231 return TGT_IFACE_POINTER;
232 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
233 return TGT_CTXT_HANDLE_POINTER;
234 else
235 return TGT_POINTER;
236 case TYPE_STRUCT:
237 return TGT_STRUCT;
238 case TYPE_ENCAPSULATED_UNION:
239 case TYPE_UNION:
240 return TGT_UNION;
241 case TYPE_ARRAY:
242 return TGT_ARRAY;
243 case TYPE_FUNCTION:
244 case TYPE_COCLASS:
245 case TYPE_INTERFACE:
246 case TYPE_MODULE:
247 case TYPE_VOID:
248 case TYPE_ALIAS:
249 case TYPE_BITFIELD:
250 break;
251 }
252 return TGT_INVALID;
253 }
254
255 unsigned char get_struct_fc(const type_t *type)
256 {
257 int has_pointer = 0;
258 int has_conformance = 0;
259 int has_variance = 0;
260 var_t *field;
261 var_list_t *fields;
262
263 fields = type_struct_get_fields(type);
264
265 if (get_padding(fields))
266 return RPC_FC_BOGUS_STRUCT;
267
268 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
269 {
270 type_t *t = field->type;
271 enum typegen_type typegen_type;
272
273 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
274
275 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
276 {
277 if (is_string_type(field->attrs, field->type))
278 {
279 if (is_conformant_array(t))
280 has_conformance = 1;
281 has_variance = 1;
282 continue;
283 }
284
285 if (is_array(type_array_get_element(field->type)))
286 return RPC_FC_BOGUS_STRUCT;
287
288 if (type_array_has_conformance(field->type))
289 {
290 has_conformance = 1;
291 if (list_next(fields, &field->entry))
292 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
293 field->name);
294 }
295 if (type_array_has_variance(t))
296 has_variance = 1;
297
298 t = type_array_get_element(t);
299 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
300 }
301
302 switch (typegen_type)
303 {
304 case TGT_USER_TYPE:
305 case TGT_IFACE_POINTER:
306 return RPC_FC_BOGUS_STRUCT;
307 case TGT_BASIC:
308 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
309 return RPC_FC_BOGUS_STRUCT;
310 break;
311 case TGT_ENUM:
312 if (get_enum_fc(t) == RPC_FC_ENUM16)
313 return RPC_FC_BOGUS_STRUCT;
314 break;
315 case TGT_POINTER:
316 case TGT_ARRAY:
317 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
318 return RPC_FC_BOGUS_STRUCT;
319 has_pointer = 1;
320 break;
321 case TGT_UNION:
322 return RPC_FC_BOGUS_STRUCT;
323 case TGT_STRUCT:
324 {
325 unsigned char fc = get_struct_fc(t);
326 switch (fc)
327 {
328 case RPC_FC_STRUCT:
329 break;
330 case RPC_FC_CVSTRUCT:
331 has_conformance = 1;
332 has_variance = 1;
333 has_pointer = 1;
334 break;
335
336 case RPC_FC_CPSTRUCT:
337 has_conformance = 1;
338 if (list_next( fields, &field->entry ))
339 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
340 field->name);
341 has_pointer = 1;
342 break;
343
344 case RPC_FC_CSTRUCT:
345 has_conformance = 1;
346 if (list_next( fields, &field->entry ))
347 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
348 field->name);
349 break;
350
351 case RPC_FC_PSTRUCT:
352 has_pointer = 1;
353 break;
354
355 default:
356 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
357 /* fallthru - treat it as complex */
358
359 /* as soon as we see one of these these members, it's bogus... */
360 case RPC_FC_BOGUS_STRUCT:
361 return RPC_FC_BOGUS_STRUCT;
362 }
363 break;
364 }
365 case TGT_RANGE:
366 return RPC_FC_BOGUS_STRUCT;
367 case TGT_STRING:
368 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
369 case TGT_INVALID:
370 case TGT_CTXT_HANDLE:
371 case TGT_CTXT_HANDLE_POINTER:
372 /* checking after parsing should mean that we don't get here. if we do,
373 * it's a checker bug */
374 assert(0);
375 }
376 }
377
378 if( has_variance )
379 {
380 if ( has_conformance )
381 return RPC_FC_CVSTRUCT;
382 else
383 return RPC_FC_BOGUS_STRUCT;
384 }
385 if( has_conformance && has_pointer )
386 return RPC_FC_CPSTRUCT;
387 if( has_conformance )
388 return RPC_FC_CSTRUCT;
389 if( has_pointer )
390 return RPC_FC_PSTRUCT;
391 return RPC_FC_STRUCT;
392 }
393
394 unsigned char get_array_fc(const type_t *type)
395 {
396 unsigned char fc;
397 const expr_t *size_is;
398 const type_t *elem_type;
399
400 elem_type = type_array_get_element(type);
401 size_is = type_array_get_conformance(type);
402
403 if (!size_is)
404 {
405 unsigned int align = 0;
406 unsigned int size = type_memsize(elem_type, &align);
407 if (size * type_array_get_dim(type) > 0xffffuL)
408 fc = RPC_FC_LGFARRAY;
409 else
410 fc = RPC_FC_SMFARRAY;
411 }
412 else
413 fc = RPC_FC_CARRAY;
414
415 if (type_array_has_variance(type))
416 {
417 if (fc == RPC_FC_SMFARRAY)
418 fc = RPC_FC_SMVARRAY;
419 else if (fc == RPC_FC_LGFARRAY)
420 fc = RPC_FC_LGVARRAY;
421 else if (fc == RPC_FC_CARRAY)
422 fc = RPC_FC_CVARRAY;
423 }
424
425 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
426 {
427 case TGT_USER_TYPE:
428 fc = RPC_FC_BOGUS_ARRAY;
429 break;
430 case TGT_BASIC:
431 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
432 pointer_size != 4)
433 fc = RPC_FC_BOGUS_ARRAY;
434 break;
435 case TGT_STRUCT:
436 switch (get_struct_fc(elem_type))
437 {
438 case RPC_FC_BOGUS_STRUCT:
439 fc = RPC_FC_BOGUS_ARRAY;
440 break;
441 }
442 break;
443 case TGT_ENUM:
444 /* is 16-bit enum - if so, wire size differs from mem size and so
445 * the array cannot be block copied, which means the array is complex */
446 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
447 fc = RPC_FC_BOGUS_ARRAY;
448 break;
449 case TGT_UNION:
450 case TGT_IFACE_POINTER:
451 fc = RPC_FC_BOGUS_ARRAY;
452 break;
453 case TGT_POINTER:
454 /* ref pointers cannot just be block copied. unique pointers to
455 * interfaces need special treatment. either case means the array is
456 * complex */
457 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
458 fc = RPC_FC_BOGUS_ARRAY;
459 break;
460 case TGT_RANGE:
461 fc = RPC_FC_BOGUS_ARRAY;
462 break;
463 case TGT_CTXT_HANDLE:
464 case TGT_CTXT_HANDLE_POINTER:
465 case TGT_STRING:
466 case TGT_INVALID:
467 case TGT_ARRAY:
468 /* nothing to do for everything else */
469 break;
470 }
471
472 return fc;
473 }
474
475 int is_struct(unsigned char type)
476 {
477 switch (type)
478 {
479 case RPC_FC_STRUCT:
480 case RPC_FC_PSTRUCT:
481 case RPC_FC_CSTRUCT:
482 case RPC_FC_CPSTRUCT:
483 case RPC_FC_CVSTRUCT:
484 case RPC_FC_BOGUS_STRUCT:
485 return 1;
486 default:
487 return 0;
488 }
489 }
490
491 static int is_non_complex_struct(const type_t *type)
492 {
493 return (type_get_type(type) == TYPE_STRUCT &&
494 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
495 }
496
497 static int type_has_pointers(const type_t *type)
498 {
499 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
500 {
501 case TGT_USER_TYPE:
502 return FALSE;
503 case TGT_POINTER:
504 return TRUE;
505 case TGT_ARRAY:
506 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
507 case TGT_STRUCT:
508 {
509 var_list_t *fields = type_struct_get_fields(type);
510 const var_t *field;
511 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
512 {
513 if (type_has_pointers(field->type))
514 return TRUE;
515 }
516 break;
517 }
518 case TGT_UNION:
519 {
520 var_list_t *fields;
521 const var_t *field;
522 fields = type_union_get_cases(type);
523 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
524 {
525 if (field->type && type_has_pointers(field->type))
526 return TRUE;
527 }
528 break;
529 }
530 case TGT_CTXT_HANDLE:
531 case TGT_CTXT_HANDLE_POINTER:
532 case TGT_STRING:
533 case TGT_IFACE_POINTER:
534 case TGT_BASIC:
535 case TGT_ENUM:
536 case TGT_RANGE:
537 case TGT_INVALID:
538 break;
539 }
540
541 return FALSE;
542 }
543
544 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
545 int toplevel_param)
546 {
547 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
548 {
549 case TGT_USER_TYPE:
550 return FALSE;
551 case TGT_POINTER:
552 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
553 return TRUE;
554 else
555 return FALSE;
556 case TGT_ARRAY:
557 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
558 return TRUE;
559 else
560 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
561 case TGT_STRUCT:
562 {
563 var_list_t *fields = type_struct_get_fields(type);
564 const var_t *field;
565 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
566 {
567 if (type_has_full_pointer(field->type, field->attrs, FALSE))
568 return TRUE;
569 }
570 break;
571 }
572 case TGT_UNION:
573 {
574 var_list_t *fields;
575 const var_t *field;
576 fields = type_union_get_cases(type);
577 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
578 {
579 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
580 return TRUE;
581 }
582 break;
583 }
584 case TGT_CTXT_HANDLE:
585 case TGT_CTXT_HANDLE_POINTER:
586 case TGT_STRING:
587 case TGT_IFACE_POINTER:
588 case TGT_BASIC:
589 case TGT_ENUM:
590 case TGT_RANGE:
591 case TGT_INVALID:
592 break;
593 }
594
595 return FALSE;
596 }
597
598 static unsigned short user_type_offset(const char *name)
599 {
600 user_type_t *ut;
601 unsigned short off = 0;
602 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
603 {
604 if (strcmp(name, ut->name) == 0)
605 return off;
606 ++off;
607 }
608 error("user_type_offset: couldn't find type (%s)\n", name);
609 return 0;
610 }
611
612 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
613 {
614 type->typestring_offset = offset;
615 if (file) type->tfswrite = FALSE;
616 }
617
618 static void guard_rec(type_t *type)
619 {
620 /* types that contain references to themselves (like a linked list),
621 need to be shielded from infinite recursion when writing embedded
622 types */
623 if (type->typestring_offset)
624 type->tfswrite = FALSE;
625 else
626 type->typestring_offset = 1;
627 }
628
629 static type_t *get_user_type(const type_t *t, const char **pname)
630 {
631 for (;;)
632 {
633 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
634 if (ut)
635 {
636 if (pname)
637 *pname = t->name;
638 return ut;
639 }
640
641 if (type_is_alias(t))
642 t = type_alias_get_aliasee(t);
643 else
644 return 0;
645 }
646 }
647
648 int is_user_type(const type_t *t)
649 {
650 return get_user_type(t, NULL) != NULL;
651 }
652
653 static int is_embedded_complex(const type_t *type)
654 {
655 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
656 {
657 case TGT_USER_TYPE:
658 case TGT_STRUCT:
659 case TGT_UNION:
660 case TGT_ARRAY:
661 case TGT_IFACE_POINTER:
662 return TRUE;
663 default:
664 return FALSE;
665 }
666 }
667
668 static const char *get_context_handle_type_name(const type_t *type)
669 {
670 const type_t *t;
671 for (t = type;
672 is_ptr(t) || type_is_alias(t);
673 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
674 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
675 return t->name;
676 assert(0);
677 return NULL;
678 }
679
680 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
681 do { \
682 if (file) \
683 fprintf(file, "/* %2u */\n", typestring_offset); \
684 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
685 } \
686 while (0)
687
688 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
689 static void print_file(FILE *file, int indent, const char *format, ...)
690 {
691 va_list va;
692 va_start(va, format);
693 print(file, indent, format, va);
694 va_end(va);
695 }
696
697 void print(FILE *file, int indent, const char *format, va_list va)
698 {
699 if (file)
700 {
701 if (format[0] != '\n')
702 while (0 < indent--)
703 fprintf(file, " ");
704 vfprintf(file, format, va);
705 }
706 }
707
708
709 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
710 {
711 if (decl_indirect(t))
712 {
713 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
714 local_var_prefix, n, local_var_prefix, n);
715 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
716 }
717 else if (is_ptr(t) || is_array(t))
718 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
719 }
720
721 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
722 {
723 const var_t *var;
724
725 if (!is_void(type_function_get_rettype(func->type)))
726 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
727
728 if (!type_get_function_args(func->type))
729 return;
730
731 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
732 write_var_init(file, indent, var->type, var->name, local_var_prefix);
733
734 fprintf(file, "\n");
735 }
736
737 static void write_formatdesc(FILE *f, int indent, const char *str)
738 {
739 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
740 print_file(f, indent, "{\n");
741 print_file(f, indent + 1, "short Pad;\n");
742 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
743 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
744 print_file(f, indent, "\n");
745 }
746
747 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
748 {
749 clear_all_offsets();
750
751 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
752 get_size_typeformatstring(stmts, pred));
753
754 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
755 get_size_procformatstring(stmts, pred));
756
757 fprintf(f, "\n");
758 write_formatdesc(f, indent, "TYPE");
759 write_formatdesc(f, indent, "PROC");
760 fprintf(f, "\n");
761 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
762 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
763 print_file(f, indent, "\n");
764 }
765
766 int decl_indirect(const type_t *t)
767 {
768 if (is_user_type(t))
769 return TRUE;
770 return (type_get_type(t) != TYPE_BASIC &&
771 type_get_type(t) != TYPE_ENUM &&
772 type_get_type(t) != TYPE_POINTER &&
773 type_get_type(t) != TYPE_ARRAY);
774 }
775
776 static unsigned int write_procformatstring_type(FILE *file, int indent,
777 const char *name,
778 const type_t *type,
779 const attr_list_t *attrs,
780 int is_return)
781 {
782 unsigned int size;
783
784 int is_in = is_attr(attrs, ATTR_IN);
785 int is_out = is_attr(attrs, ATTR_OUT);
786
787 if (!is_in && !is_out) is_in = TRUE;
788
789 if (type_get_type(type) == TYPE_BASIC ||
790 type_get_type(type) == TYPE_ENUM)
791 {
792 unsigned char fc;
793
794 if (is_return)
795 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
796 else
797 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
798
799 if (type_get_type(type) == TYPE_ENUM)
800 {
801 fc = get_enum_fc(type);
802 }
803 else
804 {
805 fc = get_basic_fc(type);
806
807 if (fc == RPC_FC_BIND_PRIMITIVE)
808 fc = RPC_FC_IGNORE;
809 }
810
811 print_file(file, indent, "0x%02x, /* %s */\n",
812 fc, string_of_type(fc));
813 size = 2; /* includes param type prefix */
814 }
815 else
816 {
817 if (is_return)
818 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
819 else if (is_in && is_out)
820 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
821 else if (is_out)
822 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
823 else
824 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
825
826 print_file(file, indent, "0x01,\n");
827 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
828 size = 4; /* includes param type prefix */
829 }
830 return size;
831 }
832
833 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
834 {
835 const statement_t *stmt;
836 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
837 {
838 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
839 {
840 const statement_t *stmt_func;
841 if (!pred(stmt->u.type))
842 continue;
843 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
844 {
845 const var_t *func = stmt_func->u.var;
846 if (is_local(func->attrs)) continue;
847 /* emit argument data */
848 if (type_get_function_args(func->type))
849 {
850 const var_t *var;
851 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
852 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
853 }
854
855 /* emit return value data */
856 if (is_void(type_function_get_rettype(func->type)))
857 {
858 print_file(file, indent, "0x5b, /* FC_END */\n");
859 print_file(file, indent, "0x5c, /* FC_PAD */\n");
860 }
861 else
862 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
863 }
864 }
865 else if (stmt->type == STMT_LIBRARY)
866 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
867 }
868 }
869
870 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
871 {
872 int indent = 0;
873
874 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
875 print_file(file, indent, "{\n");
876 indent++;
877 print_file(file, indent, "0,\n");
878 print_file(file, indent, "{\n");
879 indent++;
880
881 write_procformatstring_stmts(file, indent, stmts, pred);
882
883 print_file(file, indent, "0x0\n");
884 indent--;
885 print_file(file, indent, "}\n");
886 indent--;
887 print_file(file, indent, "};\n");
888 print_file(file, indent, "\n");
889 }
890
891 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
892 {
893 unsigned char fc;
894
895 if (type_get_type(type) == TYPE_BASIC)
896 fc = get_basic_fc(type);
897 else if (type_get_type(type) == TYPE_ENUM)
898 fc = get_enum_fc(type);
899 else
900 return 0;
901
902 if (convert_to_signed_type)
903 {
904 switch(fc)
905 {
906 case RPC_FC_USMALL:
907 fc = RPC_FC_SMALL;
908 break;
909 case RPC_FC_USHORT:
910 fc = RPC_FC_SHORT;
911 break;
912 case RPC_FC_ULONG:
913 fc = RPC_FC_LONG;
914 break;
915 }
916 }
917
918 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
919 *typestring_offset += 1;
920 return 1;
921 }
922
923 /* write conformance / variance descriptor */
924 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
925 unsigned int baseoff, const type_t *type,
926 const expr_t *expr)
927 {
928 unsigned char operator_type = 0;
929 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
930 const char *conftype_string = "";
931 const char *operator_string = "no operators";
932 const expr_t *subexpr;
933
934 if (!expr)
935 {
936 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
937 return 4;
938 }
939
940 if (!structure)
941 {
942 /* Top-level conformance calculations are done inline. */
943 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
944 RPC_FC_TOP_LEVEL_CONFORMANCE);
945 print_file (file, 2, "0x0,\n");
946 print_file (file, 2, "NdrFcShort(0x0),\n");
947 return 4;
948 }
949
950 if (expr->is_const)
951 {
952 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
953 error("write_conf_or_var_desc: constant value %ld is greater than "
954 "the maximum constant size of %d\n", expr->cval,
955 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
956
957 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
958 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
959 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
960 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
961
962 return 4;
963 }
964
965 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
966 {
967 conftype = RPC_FC_POINTER_CONFORMANCE;
968 conftype_string = "field pointer, ";
969 }
970
971 subexpr = expr;
972 switch (subexpr->type)
973 {
974 case EXPR_PPTR:
975 subexpr = subexpr->ref;
976 operator_type = RPC_FC_DEREFERENCE;
977 operator_string = "FC_DEREFERENCE";
978 break;
979 case EXPR_DIV:
980 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
981 {
982 subexpr = subexpr->ref;
983 operator_type = RPC_FC_DIV_2;
984 operator_string = "FC_DIV_2";
985 }
986 break;
987 case EXPR_MUL:
988 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
989 {
990 subexpr = subexpr->ref;
991 operator_type = RPC_FC_MULT_2;
992 operator_string = "FC_MULT_2";
993 }
994 break;
995 case EXPR_SUB:
996 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
997 {
998 subexpr = subexpr->ref;
999 operator_type = RPC_FC_SUB_1;
1000 operator_string = "FC_SUB_1";
1001 }
1002 break;
1003 case EXPR_ADD:
1004 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1005 {
1006 subexpr = subexpr->ref;
1007 operator_type = RPC_FC_ADD_1;
1008 operator_string = "FC_ADD_1";
1009 }
1010 break;
1011 default:
1012 break;
1013 }
1014
1015 if (subexpr->type == EXPR_IDENTIFIER)
1016 {
1017 const type_t *correlation_variable = NULL;
1018 unsigned char param_type = 0;
1019 unsigned int offset = 0;
1020 const var_t *var;
1021 var_list_t *fields = type_struct_get_fields(structure);
1022
1023 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1024 {
1025 unsigned int size = field_memsize( var->type, &offset );
1026 if (var->name && !strcmp(var->name, subexpr->u.sval))
1027 {
1028 correlation_variable = var->type;
1029 break;
1030 }
1031 offset += size;
1032 }
1033 if (!correlation_variable)
1034 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1035 subexpr->u.sval);
1036
1037 correlation_variable = expr_resolve_type(NULL, structure, expr);
1038
1039 offset -= baseoff;
1040
1041 if (type_get_type(correlation_variable) == TYPE_BASIC)
1042 {
1043 switch (get_basic_fc(correlation_variable))
1044 {
1045 case RPC_FC_CHAR:
1046 case RPC_FC_SMALL:
1047 param_type = RPC_FC_SMALL;
1048 break;
1049 case RPC_FC_BYTE:
1050 case RPC_FC_USMALL:
1051 param_type = RPC_FC_USMALL;
1052 break;
1053 case RPC_FC_WCHAR:
1054 case RPC_FC_SHORT:
1055 param_type = RPC_FC_SHORT;
1056 break;
1057 case RPC_FC_USHORT:
1058 param_type = RPC_FC_USHORT;
1059 break;
1060 case RPC_FC_LONG:
1061 param_type = RPC_FC_LONG;
1062 break;
1063 case RPC_FC_ULONG:
1064 param_type = RPC_FC_ULONG;
1065 break;
1066 default:
1067 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1068 get_basic_fc(correlation_variable));
1069 }
1070 }
1071 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1072 {
1073 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1074 param_type = RPC_FC_LONG;
1075 else
1076 param_type = RPC_FC_SHORT;
1077 }
1078 else
1079 {
1080 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1081 subexpr->u.sval);
1082 return 0;
1083 }
1084
1085 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1086 conftype | param_type, conftype_string, string_of_type(param_type));
1087 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1088 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1089 offset, offset);
1090 }
1091 else
1092 {
1093 unsigned int callback_offset = 0;
1094 struct expr_eval_routine *eval;
1095 int found = 0;
1096
1097 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1098 {
1099 if (!strcmp (eval->structure->name, structure->name)
1100 && !compare_expr (eval->expr, expr))
1101 {
1102 found = 1;
1103 break;
1104 }
1105 callback_offset++;
1106 }
1107
1108 if (!found)
1109 {
1110 eval = xmalloc (sizeof(*eval));
1111 eval->structure = structure;
1112 eval->baseoff = baseoff;
1113 eval->expr = expr;
1114 list_add_tail (&expr_eval_routines, &eval->entry);
1115 }
1116
1117 if (callback_offset > USHRT_MAX)
1118 error("Maximum number of callback routines reached\n");
1119
1120 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1121 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1122 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1123 }
1124 return 4;
1125 }
1126
1127 /* return size and start offset of a data field based on current offset */
1128 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1129 {
1130 unsigned int align = 0;
1131 unsigned int size = type_memsize( type, &align );
1132
1133 *offset = ROUND_SIZE( *offset, align );
1134 return size;
1135 }
1136
1137 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1138 {
1139 unsigned int size = 0;
1140 unsigned int max_align;
1141 const var_t *v;
1142
1143 if (!fields) return 0;
1144 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1145 {
1146 unsigned int falign = 0;
1147 unsigned int fsize = type_memsize(v->type, &falign);
1148 if (*align < falign) *align = falign;
1149 falign = clamp_align(falign);
1150 size = ROUND_SIZE(size, falign);
1151 size += fsize;
1152 }
1153
1154 max_align = clamp_align(*align);
1155 size = ROUND_SIZE(size, max_align);
1156
1157 return size;
1158 }
1159
1160 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1161 {
1162 unsigned int size, maxs = 0;
1163 unsigned int align = *pmaxa;
1164 const var_t *v;
1165
1166 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1167 {
1168 /* we could have an empty default field with NULL type */
1169 if (v->type)
1170 {
1171 size = type_memsize(v->type, &align);
1172 if (maxs < size) maxs = size;
1173 if (*pmaxa < align) *pmaxa = align;
1174 }
1175 }
1176
1177 return maxs;
1178 }
1179
1180 int get_padding(const var_list_t *fields)
1181 {
1182 unsigned short offset = 0;
1183 unsigned int salign = 1;
1184 const var_t *f;
1185
1186 if (!fields)
1187 return 0;
1188
1189 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1190 {
1191 type_t *ft = f->type;
1192 unsigned int align = 0;
1193 unsigned int size = type_memsize(ft, &align);
1194 align = clamp_align(align);
1195 if (align > salign) salign = align;
1196 offset = ROUND_SIZE(offset, align);
1197 offset += size;
1198 }
1199
1200 return ROUNDING(offset, salign);
1201 }
1202
1203 unsigned int type_memsize(const type_t *t, unsigned int *align)
1204 {
1205 unsigned int size = 0;
1206
1207 switch (type_get_type(t))
1208 {
1209 case TYPE_BASIC:
1210 switch (get_basic_fc(t))
1211 {
1212 case RPC_FC_BYTE:
1213 case RPC_FC_CHAR:
1214 case RPC_FC_USMALL:
1215 case RPC_FC_SMALL:
1216 size = 1;
1217 if (size > *align) *align = size;
1218 break;
1219 case RPC_FC_WCHAR:
1220 case RPC_FC_USHORT:
1221 case RPC_FC_SHORT:
1222 size = 2;
1223 if (size > *align) *align = size;
1224 break;
1225 case RPC_FC_ULONG:
1226 case RPC_FC_LONG:
1227 case RPC_FC_ERROR_STATUS_T:
1228 case RPC_FC_FLOAT:
1229 size = 4;
1230 if (size > *align) *align = size;
1231 break;
1232 case RPC_FC_HYPER:
1233 case RPC_FC_DOUBLE:
1234 size = 8;
1235 if (size > *align) *align = size;
1236 break;
1237 case RPC_FC_INT3264:
1238 case RPC_FC_UINT3264:
1239 assert( pointer_size );
1240 size = pointer_size;
1241 if (size > *align) *align = size;
1242 break;
1243 default:
1244 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1245 size = 0;
1246 }
1247 break;
1248 case TYPE_ENUM:
1249 switch (get_enum_fc(t))
1250 {
1251 case RPC_FC_ENUM16:
1252 case RPC_FC_ENUM32:
1253 size = 4;
1254 if (size > *align) *align = size;
1255 break;
1256 default:
1257 error("type_memsize: Unknown enum type\n");
1258 size = 0;
1259 }
1260 break;
1261 case TYPE_STRUCT:
1262 size = fields_memsize(type_struct_get_fields(t), align);
1263 break;
1264 case TYPE_ENCAPSULATED_UNION:
1265 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1266 break;
1267 case TYPE_UNION:
1268 size = union_memsize(type_union_get_cases(t), align);
1269 break;
1270 case TYPE_POINTER:
1271 assert( pointer_size );
1272 size = pointer_size;
1273 if (size > *align) *align = size;
1274 break;
1275 case TYPE_ARRAY:
1276 if (!type_array_is_decl_as_ptr(t))
1277 {
1278 if (is_conformant_array(t))
1279 {
1280 type_memsize(type_array_get_element(t), align);
1281 size = 0;
1282 }
1283 else
1284 size = type_array_get_dim(t) *
1285 type_memsize(type_array_get_element(t), align);
1286 }
1287 else /* declared as a pointer */
1288 {
1289 assert( pointer_size );
1290 size = pointer_size;
1291 if (size > *align) *align = size;
1292 }
1293 break;
1294 case TYPE_INTERFACE:
1295 case TYPE_ALIAS:
1296 case TYPE_VOID:
1297 case TYPE_COCLASS:
1298 case TYPE_MODULE:
1299 case TYPE_FUNCTION:
1300 case TYPE_BITFIELD:
1301 /* these types should not be encountered here due to language
1302 * restrictions (interface, void, coclass, module), logical
1303 * restrictions (alias - due to type_get_type call above) or
1304 * checking restrictions (function, bitfield). */
1305 assert(0);
1306 }
1307
1308 return size;
1309 }
1310
1311 int is_full_pointer_function(const var_t *func)
1312 {
1313 const var_t *var;
1314 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1315 return TRUE;
1316 if (!type_get_function_args(func->type))
1317 return FALSE;
1318 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1319 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1320 return TRUE;
1321 return FALSE;
1322 }
1323
1324 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1325 {
1326 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1327 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1328 fprintf(file, "\n");
1329 }
1330
1331 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1332 {
1333 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1334 fprintf(file, "\n");
1335 }
1336
1337 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1338 const type_t *type,
1339 int toplevel_param,
1340 unsigned int offset,
1341 unsigned int *typeformat_offset)
1342 {
1343 unsigned int start_offset = *typeformat_offset;
1344 short reloff = offset - (*typeformat_offset + 2);
1345 int in_attr, out_attr;
1346 int pointer_type;
1347 unsigned char flags = 0;
1348
1349 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1350
1351 in_attr = is_attr(attrs, ATTR_IN);
1352 out_attr = is_attr(attrs, ATTR_OUT);
1353 if (!in_attr && !out_attr) in_attr = 1;
1354
1355 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1356 flags |= RPC_FC_P_ONSTACK;
1357
1358 if (is_ptr(type) && !last_ptr(type))
1359 flags |= RPC_FC_P_DEREF;
1360
1361 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1362 pointer_type,
1363 flags,
1364 string_of_type(pointer_type));
1365 if (file)
1366 {
1367 if (flags & RPC_FC_P_ONSTACK)
1368 fprintf(file, " [allocated_on_stack]");
1369 if (flags & RPC_FC_P_DEREF)
1370 fprintf(file, " [pointer_deref]");
1371 fprintf(file, " */\n");
1372 }
1373
1374 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1375 *typeformat_offset += 4;
1376
1377 return start_offset;
1378 }
1379
1380 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1381 {
1382 unsigned char fc;
1383 unsigned char pointer_fc;
1384 const type_t *ref;
1385
1386 /* for historical reasons, write_simple_pointer also handled string types,
1387 * but no longer does. catch bad uses of the function with this check */
1388 if (is_string_type(attrs, type))
1389 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1390
1391 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1392
1393 ref = type_pointer_get_ref(type);
1394 if (type_get_type(ref) == TYPE_ENUM)
1395 fc = get_enum_fc(ref);
1396 else
1397 fc = get_basic_fc(ref);
1398
1399 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1400 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1401 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1402 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1403 return 4;
1404 }
1405
1406 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1407 {
1408 print_file(file, 0, "/* %u (", tfsoff);
1409 write_type_decl(file, t, NULL);
1410 print_file(file, 0, ") */\n");
1411 }
1412
1413 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1414 type_t *type, int toplevel_param,
1415 unsigned int *typestring_offset)
1416 {
1417 unsigned int offset = *typestring_offset;
1418 type_t *ref = type_pointer_get_ref(type);
1419
1420 print_start_tfs_comment(file, type, offset);
1421 update_tfsoff(type, offset, file);
1422
1423 if (ref->typestring_offset)
1424 write_nonsimple_pointer(file, attrs, type,
1425 toplevel_param,
1426 type_pointer_get_ref(type)->typestring_offset,
1427 typestring_offset);
1428 else if (type_get_type(ref) == TYPE_BASIC ||
1429 type_get_type(ref) == TYPE_ENUM)
1430 *typestring_offset += write_simple_pointer(file, attrs, type,
1431 toplevel_param);
1432
1433 return offset;
1434 }
1435
1436 static int processed(const type_t *type)
1437 {
1438 return type->typestring_offset && !type->tfswrite;
1439 }
1440
1441 static int user_type_has_variable_size(const type_t *t)
1442 {
1443 if (is_ptr(t))
1444 return TRUE;
1445 else if (type_get_type(t) == TYPE_STRUCT)
1446 {
1447 switch (get_struct_fc(t))
1448 {
1449 case RPC_FC_PSTRUCT:
1450 case RPC_FC_CSTRUCT:
1451 case RPC_FC_CPSTRUCT:
1452 case RPC_FC_CVSTRUCT:
1453 return TRUE;
1454 }
1455 }
1456 /* Note: Since this only applies to user types, we can't have a conformant
1457 array here, and strings should get filed under pointer in this case. */
1458 return FALSE;
1459 }
1460
1461 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1462 {
1463 unsigned int start, absoff, flags;
1464 unsigned int align = 0, ualign = 0;
1465 const char *name = NULL;
1466 type_t *utype = get_user_type(type, &name);
1467 unsigned int usize = type_memsize(utype, &ualign);
1468 unsigned int size = type_memsize(type, &align);
1469 unsigned short funoff = user_type_offset(name);
1470 short reloff;
1471
1472 guard_rec(type);
1473
1474 if(user_type_has_variable_size(utype)) usize = 0;
1475
1476 if (type_get_type(utype) == TYPE_BASIC ||
1477 type_get_type(utype) == TYPE_ENUM)
1478 {
1479 unsigned char fc;
1480
1481 if (type_get_type(utype) == TYPE_ENUM)
1482 fc = get_enum_fc(utype);
1483 else
1484 fc = get_basic_fc(utype);
1485
1486 absoff = *tfsoff;
1487 print_start_tfs_comment(file, utype, absoff);
1488 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1489 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1490 *tfsoff += 2;
1491 }
1492 else
1493 {
1494 if (!processed(utype))
1495 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1496 absoff = utype->typestring_offset;
1497 }
1498
1499 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1500 flags = 0x40;
1501 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1502 flags = 0x80;
1503 else
1504 flags = 0;
1505
1506 start = *tfsoff;
1507 update_tfsoff(type, start, file);
1508 print_start_tfs_comment(file, type, start);
1509 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1510 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1511 flags | (ualign - 1), ualign - 1, flags);
1512 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1513 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1514 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1515 *tfsoff += 8;
1516 reloff = absoff - *tfsoff;
1517 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1518 *tfsoff += 2;
1519 }
1520
1521 static void write_member_type(FILE *file, const type_t *cont,
1522 int cont_is_complex, const attr_list_t *attrs,
1523 const type_t *type, unsigned int *corroff,
1524 unsigned int *tfsoff)
1525 {
1526 if (is_embedded_complex(type) && !is_conformant_array(type))
1527 {
1528 unsigned int absoff;
1529 short reloff;
1530
1531 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1532 {
1533 absoff = *corroff;
1534 *corroff += 8;
1535 }
1536 else
1537 {
1538 absoff = type->typestring_offset;
1539 }
1540 reloff = absoff - (*tfsoff + 2);
1541
1542 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1543 /* FIXME: actually compute necessary padding */
1544 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1545 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1546 reloff, reloff, absoff);
1547 *tfsoff += 4;
1548 }
1549 else if (is_ptr(type) || is_conformant_array(type))
1550 {
1551 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1552 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1553 *tfsoff += 1;
1554 }
1555 else if (!write_base_type(file, type, TRUE, tfsoff))
1556 error("Unsupported member type %d\n", type_get_type(type));
1557 }
1558
1559 static void write_array_element_type(FILE *file, const type_t *type,
1560 int cont_is_complex, unsigned int *tfsoff)
1561 {
1562 type_t *elem = type_array_get_element(type);
1563
1564 if (!is_embedded_complex(elem) && is_ptr(elem))
1565 {
1566 type_t *ref = type_pointer_get_ref(elem);
1567
1568 if (processed(ref))
1569 {
1570 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1571 return;
1572 }
1573 if (!is_string_type(NULL, elem) &&
1574 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1575 {
1576 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1577 return;
1578 }
1579 }
1580 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1581 }
1582
1583 static void write_end(FILE *file, unsigned int *tfsoff)
1584 {
1585 if (*tfsoff % 2 == 0)
1586 {
1587 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1588 *tfsoff += 1;
1589 }
1590 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1591 *tfsoff += 1;
1592 }
1593
1594 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1595 {
1596 unsigned int offset = 0;
1597 var_list_t *fs = type_struct_get_fields(type);
1598 var_t *f;
1599
1600 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1601 {
1602 type_t *ft = f->type;
1603 unsigned int size = field_memsize( ft, &offset );
1604 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1605 {
1606 short reloff;
1607 unsigned int absoff = ft->typestring_offset;
1608 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1609 absoff += 8; /* we already have a corr descr, skip it */
1610 reloff = absoff - (*tfsoff + 6);
1611 print_file(file, 0, "/* %d */\n", *tfsoff);
1612 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1613 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1614 write_conf_or_var_desc(file, current_structure, offset, ft,
1615 get_attrp(f->attrs, ATTR_SWITCHIS));
1616 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1617 reloff, reloff, absoff);
1618 *tfsoff += 8;
1619 }
1620 offset += size;
1621 }
1622 }
1623
1624 static int write_pointer_description_offsets(
1625 FILE *file, const attr_list_t *attrs, type_t *type,
1626 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1627 unsigned int *typestring_offset)
1628 {
1629 int written = 0;
1630 unsigned int align;
1631
1632 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1633 (is_array(type) && type_array_is_decl_as_ptr(type)))
1634 {
1635 if (offset_in_memory && offset_in_buffer)
1636 {
1637 unsigned int memsize;
1638
1639 /* pointer instance */
1640 /* FIXME: sometimes from end of structure, sometimes from beginning */
1641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1642 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1643
1644 align = 0;
1645 memsize = type_memsize(type, &align);
1646 *offset_in_memory += memsize;
1647 /* increment these separately as in the case of conformant (varying)
1648 * structures these start at different values */
1649 *offset_in_buffer += memsize;
1650 }
1651 *typestring_offset += 4;
1652
1653 if (is_ptr(type))
1654 {
1655 type_t *ref = type_pointer_get_ref(type);
1656
1657 if (is_string_type(attrs, type))
1658 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1659 else if (processed(ref))
1660 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1661 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1662 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1663 else
1664 error("write_pointer_description_offsets: type format string unknown\n");
1665 }
1666 else
1667 {
1668 unsigned int offset = type->typestring_offset;
1669 /* skip over the pointer that is written for strings, since a
1670 * pointer has to be written in-place here */
1671 if (is_string_type(attrs, type))
1672 offset += 4;
1673 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1674 }
1675
1676 return 1;
1677 }
1678
1679 if (is_array(type))
1680 {
1681 return write_pointer_description_offsets(
1682 file, attrs, type_array_get_element(type), offset_in_memory,
1683 offset_in_buffer, typestring_offset);
1684 }
1685 else if (is_non_complex_struct(type))
1686 {
1687 /* otherwise search for interesting fields to parse */
1688 const var_t *v;
1689 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1690 {
1691 if (offset_in_memory && offset_in_buffer)
1692 {
1693 unsigned int padding;
1694 align = 0;
1695 type_memsize(v->type, &align);
1696 padding = ROUNDING(*offset_in_memory, align);
1697 *offset_in_memory += padding;
1698 *offset_in_buffer += padding;
1699 }
1700 written += write_pointer_description_offsets(
1701 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1702 typestring_offset);
1703 }
1704 }
1705 else
1706 {
1707 if (offset_in_memory && offset_in_buffer)
1708 {
1709 unsigned int memsize;
1710 align = 0;
1711 memsize = type_memsize(type, &align);
1712 *offset_in_memory += memsize;
1713 /* increment these separately as in the case of conformant (varying)
1714 * structures these start at different values */
1715 *offset_in_buffer += memsize;
1716 }
1717 }
1718
1719 return written;
1720 }
1721
1722 static int write_no_repeat_pointer_descriptions(
1723 FILE *file, const attr_list_t *attrs, type_t *type,
1724 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1725 unsigned int *typestring_offset)
1726 {
1727 int written = 0;
1728 unsigned int align;
1729
1730 if (is_ptr(type) ||
1731 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1732 {
1733 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1734 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1735 *typestring_offset += 2;
1736
1737 return write_pointer_description_offsets(file, attrs, type,
1738 offset_in_memory, offset_in_buffer, typestring_offset);
1739 }
1740
1741 if (is_non_complex_struct(type))
1742 {
1743 const var_t *v;
1744 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1745 {
1746 if (offset_in_memory && offset_in_buffer)
1747 {
1748 unsigned int padding;
1749 align = 0;
1750 type_memsize(v->type, &align);
1751 padding = ROUNDING(*offset_in_memory, align);
1752 *offset_in_memory += padding;
1753 *offset_in_buffer += padding;
1754 }
1755 written += write_no_repeat_pointer_descriptions(
1756 file, v->attrs, v->type,
1757 offset_in_memory, offset_in_buffer, typestring_offset);
1758 }
1759 }
1760 else
1761 {
1762 unsigned int memsize;
1763 align = 0;
1764 memsize = type_memsize(type, &align);
1765 *offset_in_memory += memsize;
1766 /* increment these separately as in the case of conformant (varying)
1767 * structures these start at different values */
1768 *offset_in_buffer += memsize;
1769 }
1770
1771 return written;
1772 }
1773
1774 /* Note: if file is NULL return value is number of pointers to write, else
1775 * it is the number of type format characters written */
1776 static int write_fixed_array_pointer_descriptions(
1777 FILE *file, const attr_list_t *attrs, type_t *type,
1778 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1779 unsigned int *typestring_offset)
1780 {
1781 unsigned int align;
1782 int pointer_count = 0;
1783
1784 if (type_get_type(type) == TYPE_ARRAY &&
1785 !type_array_has_conformance(type) && !type_array_has_variance(type))
1786 {
1787 unsigned int temp = 0;
1788 /* unfortunately, this needs to be done in two passes to avoid
1789 * writing out redundant FC_FIXED_REPEAT descriptions */
1790 pointer_count = write_pointer_description_offsets(
1791 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1792 if (pointer_count > 0)
1793 {
1794 unsigned int increment_size;
1795 unsigned int offset_of_array_pointer_mem = 0;
1796 unsigned int offset_of_array_pointer_buf = 0;
1797
1798 align = 0;
1799 increment_size = type_memsize(type_array_get_element(type), &align);
1800
1801 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1802 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1803 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1804 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1805 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1806 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1807 *typestring_offset += 10;
1808
1809 pointer_count = write_pointer_description_offsets(
1810 file, attrs, type, &offset_of_array_pointer_mem,
1811 &offset_of_array_pointer_buf, typestring_offset);
1812 }
1813 }
1814 else if (type_get_type(type) == TYPE_STRUCT)
1815 {
1816 const var_t *v;
1817 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1818 {
1819 if (offset_in_memory && offset_in_buffer)
1820 {
1821 unsigned int padding;
1822 align = 0;
1823 type_memsize(v->type, &align);
1824 padding = ROUNDING(*offset_in_memory, align);
1825 *offset_in_memory += padding;
1826 *offset_in_buffer += padding;
1827 }
1828 pointer_count += write_fixed_array_pointer_descriptions(
1829 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1830 typestring_offset);
1831 }
1832 }
1833 else
1834 {
1835 if (offset_in_memory && offset_in_buffer)
1836 {
1837 unsigned int memsize;
1838 align = 0;
1839 memsize = type_memsize(type, &align);
1840 *offset_in_memory += memsize;
1841 /* increment these separately as in the case of conformant (varying)
1842 * structures these start at different values */
1843 *offset_in_buffer += memsize;
1844 }
1845 }
1846
1847 return pointer_count;
1848 }
1849
1850 /* Note: if file is NULL return value is number of pointers to write, else
1851 * it is the number of type format characters written */
1852 static int write_conformant_array_pointer_descriptions(
1853 FILE *file, const attr_list_t *attrs, type_t *type,
1854 unsigned int offset_in_memory, unsigned int *typestring_offset)
1855 {
1856 unsigned int align;
1857 int pointer_count = 0;
1858
1859 if (is_conformant_array(type) && !type_array_has_variance(type))
1860 {
1861 unsigned int temp = 0;
1862 /* unfortunately, this needs to be done in two passes to avoid
1863 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1864 pointer_count = write_pointer_description_offsets(
1865 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1866 if (pointer_count > 0)
1867 {
1868 unsigned int increment_size;
1869 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1870 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1871
1872 align = 0;
1873 increment_size = type_memsize(type_array_get_element(type), &align);
1874
1875 if (increment_size > USHRT_MAX)
1876 error("array size of %u bytes is too large\n", increment_size);
1877
1878 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1879 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1880 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1881 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1882 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1883 *typestring_offset += 8;
1884
1885 pointer_count = write_pointer_description_offsets(
1886 file, attrs, type_array_get_element(type),
1887 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1888 typestring_offset);
1889 }
1890 }
1891
1892 return pointer_count;
1893 }
1894
1895 /* Note: if file is NULL return value is number of pointers to write, else
1896 * it is the number of type format characters written */
1897 static int write_varying_array_pointer_descriptions(
1898 FILE *file, const attr_list_t *attrs, type_t *type,
1899 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1900 unsigned int *typestring_offset)
1901 {
1902 unsigned int align;
1903 int pointer_count = 0;
1904
1905 if (is_array(type) && type_array_has_variance(type))
1906 {
1907 unsigned int temp = 0;
1908 /* unfortunately, this needs to be done in two passes to avoid
1909 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1910 pointer_count = write_pointer_description_offsets(
1911 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1912 if (pointer_count > 0)
1913 {
1914 unsigned int increment_size;
1915
1916 align = 0;
1917 increment_size = type_memsize(type_array_get_element(type), &align);
1918
1919 if (increment_size > USHRT_MAX)
1920 error("array size of %u bytes is too large\n", increment_size);
1921
1922 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1923 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1924 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1925 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1926 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1927 *typestring_offset += 8;
1928
1929 pointer_count = write_pointer_description_offsets(
1930 file, attrs, type_array_get_element(type), offset_in_memory,
1931 offset_in_buffer, typestring_offset);
1932 }
1933 }
1934 else if (type_get_type(type) == TYPE_STRUCT)
1935 {
1936 const var_t *v;
1937 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1938 {
1939 if (offset_in_memory && offset_in_buffer)
1940 {
1941 unsigned int padding;
1942
1943 if (is_array(v->type) && type_array_has_variance(v->type))
1944 {
1945 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1946 /* skip over variance and offset in buffer */
1947 *offset_in_buffer += 8;
1948 }
1949
1950 align = 0;
1951 type_memsize(v->type, &align);
1952 padding = ROUNDING(*offset_in_memory, align);
1953 *offset_in_memory += padding;
1954 *offset_in_buffer += padding;
1955 }
1956 pointer_count += write_varying_array_pointer_descriptions(
1957 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1958 typestring_offset);
1959 }
1960 }
1961 else
1962 {
1963 if (offset_in_memory && offset_in_buffer)
1964 {
1965 unsigned int memsize;
1966 align = 0;
1967 memsize = type_memsize(type, &align);
1968 *offset_in_memory += memsize;
1969 /* increment these separately as in the case of conformant (varying)
1970 * structures these start at different values */
1971 *offset_in_buffer += memsize;
1972 }
1973 }
1974
1975 return pointer_count;
1976 }
1977
1978 static void write_pointer_description(FILE *file, type_t *type,
1979 unsigned int *typestring_offset)
1980 {
1981 unsigned int offset_in_buffer;
1982 unsigned int offset_in_memory;
1983
1984 /* pass 1: search for single instance of a pointer (i.e. don't descend
1985 * into arrays) */
1986 if (!is_array(type))
1987 {
1988 offset_in_memory = 0;
1989 offset_in_buffer = 0;
1990 write_no_repeat_pointer_descriptions(
1991 file, NULL, type,
1992 &offset_in_memory, &offset_in_buffer, typestring_offset);
1993 }
1994
1995 /* pass 2: search for pointers in fixed arrays */
1996 offset_in_memory = 0;
1997 offset_in_buffer = 0;
1998 write_fixed_array_pointer_descriptions(
1999 file, NULL, type,
2000 &offset_in_memory, &offset_in_buffer, typestring_offset);
2001
2002 /* pass 3: search for pointers in conformant only arrays (but don't descend
2003 * into conformant varying or varying arrays) */
2004 if (is_conformant_array(type) &&
2005 (type_array_is_decl_as_ptr(type) || !current_structure))
2006 write_conformant_array_pointer_descriptions(
2007 file, NULL, type, 0, typestring_offset);
2008 else if (type_get_type(type) == TYPE_STRUCT &&
2009 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2010 {
2011 unsigned int align = 0;
2012 type_t *carray = find_array_or_string_in_struct(type)->type;
2013 write_conformant_array_pointer_descriptions(
2014 file, NULL, carray,
2015 type_memsize(type, &align),
2016 typestring_offset);
2017 }
2018
2019 /* pass 4: search for pointers in varying arrays */
2020 offset_in_memory = 0;
2021 offset_in_buffer = 0;
2022 write_varying_array_pointer_descriptions(
2023 file, NULL, type,
2024 &offset_in_memory, &offset_in_buffer, typestring_offset);
2025 }
2026
2027 int is_declptr(const type_t *t)
2028 {
2029 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2030 }
2031
2032 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2033 type_t *type, int toplevel_param,
2034 const char *name, unsigned int *typestring_offset)
2035 {
2036 unsigned int start_offset;
2037 unsigned char rtype;
2038 type_t *elem_type;
2039
2040 start_offset = *typestring_offset;
2041 update_tfsoff(type, start_offset, file);
2042
2043 if (is_declptr(type))
2044 {
2045 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2046 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2047 if (!pointer_type)
2048 pointer_type = RPC_FC_RP;
2049 print_start_tfs_comment(file, type, *typestring_offset);
2050 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2051 pointer_type, flag, string_of_type(pointer_type),
2052 flag ? " [simple_pointer]" : "");
2053 *typestring_offset += 2;
2054 if (!flag)
2055 {
2056 print_file(file, 2, "NdrFcShort(0x2),\n");
2057 *typestring_offset += 2;
2058 }
2059 }
2060
2061 if (is_array(type))
2062 elem_type = type_array_get_element(type);
2063 else
2064 elem_type = type_pointer_get_ref(type);
2065
2066 if (type_get_type(elem_type) != TYPE_BASIC)
2067 {
2068 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2069 return start_offset;
2070 }
2071
2072 rtype = get_basic_fc(elem_type);
2073 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2074 {
2075 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2076 return start_offset;
2077 }
2078
2079 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2080 {
2081 unsigned int dim = type_array_get_dim(type);
2082
2083 /* FIXME: multi-dimensional array */
2084 if (0xffffu < dim)
2085 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2086 name, 0xffffu, dim - 0xffffu);
2087
2088 if (rtype == RPC_FC_WCHAR)
2089 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2090 else
2091 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2092 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2093 *typestring_offset += 2;
2094
2095 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2096 *typestring_offset += 2;
2097
2098 return start_offset;
2099 }
2100 else if (is_conformant_array(type))
2101 {
2102 unsigned int align = 0;
2103
2104 if (rtype == RPC_FC_WCHAR)
2105 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2106 else
2107 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2108 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2109 *typestring_offset += 2;
2110
2111 *typestring_offset += write_conf_or_var_desc(
2112 file, current_structure,
2113 (!type_array_is_decl_as_ptr(type) && current_structure
2114 ? type_memsize(current_structure, &align)
2115 : 0),
2116 type, type_array_get_conformance(type));
2117
2118 return start_offset;
2119 }
2120 else
2121 {
2122 if (rtype == RPC_FC_WCHAR)
2123 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2124 else
2125 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2126 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2127 *typestring_offset += 2;
2128
2129 return start_offset;
2130 }
2131 }
2132
2133 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2134 const char *name, unsigned int *typestring_offset)
2135 {
2136 const expr_t *length_is = type_array_get_variance(type);
2137 const expr_t *size_is = type_array_get_conformance(type);
2138 unsigned int align = 0;
2139 unsigned int size;
2140 unsigned int start_offset;
2141 unsigned char fc;
2142 int has_pointer;
2143 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2144 unsigned int baseoff
2145 = !type_array_is_decl_as_ptr(type) && current_structure
2146 ? type_memsize(current_structure, &align)
2147 : 0;
2148
2149 if (!pointer_type)
2150 pointer_type = RPC_FC_RP;
2151
2152 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2153 has_pointer = TRUE;
2154 else
2155 has_pointer = type_has_pointers(type_array_get_element(type));
2156
2157 align = 0;
2158 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2159 fc = get_array_fc(type);
2160
2161 start_offset = *typestring_offset;
2162 update_tfsoff(type, start_offset, file);
2163 print_start_tfs_comment(file, type, start_offset);
2164 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2165 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2166 *typestring_offset += 2;
2167
2168 align = 0;
2169 if (fc != RPC_FC_BOGUS_ARRAY)
2170 {
2171 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2172 {
2173 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2174 *typestring_offset += 4;
2175 }
2176 else
2177 {
2178 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2179 *typestring_offset += 2;
2180 }
2181
2182 if (is_conformant_array(type))
2183 *typestring_offset
2184 += write_conf_or_var_desc(file, current_structure, baseoff,
2185 type, size_is);
2186
2187 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2188 {
2189 unsigned int elalign = 0;
2190 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2191 unsigned int dim = type_array_get_dim(type);
2192
2193 if (fc == RPC_FC_LGVARRAY)
2194 {
2195 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2196 *typestring_offset += 4;
2197 }
2198 else
2199 {
2200 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2201 *typestring_offset += 2;
2202 }
2203
2204 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2205 *typestring_offset += 2;
2206 }
2207
2208 if (length_is)
2209 *typestring_offset
2210 += write_conf_or_var_desc(file, current_structure, baseoff,
2211 type, length_is);
2212
2213 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2214 {
2215 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2216 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2217 *typestring_offset += 2;
2218 write_pointer_description(file, type, typestring_offset);
2219 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2220 *typestring_offset += 1;
2221 }
2222
2223 write_array_element_type(file, type, FALSE, typestring_offset);
2224 write_end(file, typestring_offset);
2225 }
2226 else
2227 {
2228 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2229 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2230 *typestring_offset += 2;
2231 *typestring_offset
2232 += write_conf_or_var_desc(file, current_structure, baseoff,
2233 type, size_is);
2234 *typestring_offset
2235 += write_conf_or_var_desc(file, current_structure, baseoff,
2236 type, length_is);
2237
2238 write_array_element_type(file, type, TRUE, typestring_offset);
2239 write_end(file, typestring_offset);
2240 }
2241
2242 return start_offset;
2243 }
2244
2245 static const var_t *find_array_or_string_in_struct(const type_t *type)
2246 {
2247 const var_list_t *fields = type_struct_get_fields(type);
2248 const var_t *last_field;
2249 const type_t *ft;
2250
2251 if (!fields || list_empty(fields))
2252 return NULL;
2253
2254 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2255 ft = last_field->type;
2256
2257 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2258 return last_field;
2259
2260 if (type_get_type(ft) == TYPE_STRUCT)
2261 return find_array_or_string_in_struct(ft);
2262 else
2263 return NULL;
2264 }
2265
2266 static void write_struct_members(FILE *file, const type_t *type,
2267 int is_complex, unsigned int *corroff,
2268 unsigned int *typestring_offset)
2269 {
2270 const var_t *field;
2271 unsigned short offset = 0;
2272 unsigned int salign = 1;
2273 int padding;
2274 var_list_t *fields = type_struct_get_fields(type);
2275
2276 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2277 {
2278 type_t *ft = field->type;
2279 unsigned int align = 0;
2280 unsigned int size = type_memsize(ft, &align);
2281 align = clamp_align(align);
2282 if (salign < align) salign = align;
2283
2284 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2285 {
2286 if ((align - 1) & offset)
2287 {
2288 unsigned char fc = 0;
2289 switch (align)
2290 {
2291 case 2:
2292 fc = RPC_FC_ALIGNM2;
2293 break;
2294 case 4:
2295 fc = RPC_FC_ALIGNM4;
2296 break;
2297 case 8:
2298 fc = RPC_FC_ALIGNM8;
2299 break;
2300 default:
2301 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2302 }
2303 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2304 offset = ROUND_SIZE(offset, align);
2305 *typestring_offset += 1;
2306 }
2307 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2308 typestring_offset);
2309 offset += size;
2310 }
2311 }
2312
2313 padding = ROUNDING(offset, salign);
2314 if (padding)
2315 {
2316 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2317 RPC_FC_STRUCTPAD1 + padding - 1,
2318 padding);
2319 *typestring_offset += 1;
2320 }
2321
2322 write_end(file, typestring_offset);
2323 }
2324
2325 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2326 const char *name, unsigned int *tfsoff)
2327 {
2328 const type_t *save_current_structure = current_structure;
2329 unsigned int total_size;
2330 const var_t *array;
2331 unsigned int start_offset;
2332 unsigned int array_offset;
2333 int has_pointers = 0;
2334 unsigned int align = 0;
2335 unsigned int corroff;
2336 var_t *f;
2337 unsigned char fc = get_struct_fc(type);
2338 var_list_t *fields = type_struct_get_fields(type);
2339
2340 guard_rec(type);
2341 current_structure = type;
2342
2343 total_size = type_memsize(type, &align);
2344 if (total_size > USHRT_MAX)
2345 error("structure size for %s exceeds %d bytes by %d bytes\n",
2346 name, USHRT_MAX, total_size - USHRT_MAX);
2347
2348 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2349 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2350 FALSE, tfsoff);
2351 if (!has_pointers) has_pointers = type_has_pointers(type);
2352
2353 array = find_array_or_string_in_struct(type);
2354 if (array && !processed(array->type))
2355 array_offset
2356 = is_string_type(array->attrs, array->type)
2357 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2358 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2359
2360 corroff = *tfsoff;
2361 write_descriptors(file, type, tfsoff);
2362
2363 start_offset = *tfsoff;
2364 update_tfsoff(type, start_offset, file);
2365 print_start_tfs_comment(file, type, start_offset);
2366 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2367 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2368 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2369 *tfsoff += 4;
2370
2371 if (array)
2372 {
2373 unsigned int absoff = array->type->typestring_offset;
2374 short reloff = absoff - *tfsoff;
2375 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2376 reloff, reloff, absoff);
2377 *tfsoff += 2;
2378 }
2379 else if (fc == RPC_FC_BOGUS_STRUCT)
2380 {
2381 print_file(file, 2, "NdrFcShort(0x0),\n");
2382 *tfsoff += 2;
2383 }
2384
2385 if (fc == RPC_FC_BOGUS_STRUCT)
2386 {
2387 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2388 nothing is written to file yet. On the actual writing pass,
2389 this will have been updated. */
2390 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2391 int reloff = absoff - *tfsoff;
2392 assert( reloff >= 0 );
2393 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2394 reloff, reloff, absoff);
2395 *tfsoff += 2;
2396 }
2397 else if ((fc == RPC_FC_PSTRUCT) ||
2398 (fc == RPC_FC_CPSTRUCT) ||
2399 (fc == RPC_FC_CVSTRUCT && has_pointers))
2400 {
2401 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2402 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2403 *tfsoff += 2;
2404 write_pointer_description(file, type, tfsoff);
2405 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2406 *tfsoff += 1;
2407 }
2408
2409 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2410 tfsoff);
2411
2412 if (fc == RPC_FC_BOGUS_STRUCT)
2413 {
2414 const var_t *f;
2415
2416 type->ptrdesc = *tfsoff;
2417 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2418 {
2419 type_t *ft = f->type;
2420 if (is_ptr(ft))
2421 {
2422 if (is_string_type(f->attrs, ft))
2423 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2424 else
2425 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2426 }
2427 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2428 {
2429 unsigned int offset;
2430
2431 print_file(file, 0, "/* %d */\n", *tfsoff);
2432
2433 offset = ft->typestring_offset;
2434 /* skip over the pointer that is written for strings, since a
2435 * pointer has to be written in-place here */
2436 if (is_string_type(f->attrs, ft))
2437 offset += 4;
2438 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2439 }
2440 }
2441 if (type->ptrdesc == *tfsoff)
2442 type->ptrdesc = 0;
2443 }
2444
2445 current_structure = save_current_structure;
2446 return start_offset;
2447 }
2448
2449 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2450 {
2451 if (t == NULL)
2452 {
2453 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2454 }
2455 else
2456 {
2457 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2458 {
2459 unsigned char fc;
2460 if (type_get_type(t) == TYPE_BASIC)
2461 fc = get_basic_fc(t);
2462 else
2463 fc = get_enum_fc(t);
2464 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2465 fc, string_of_type(fc));
2466 }
2467 else if (t->typestring_offset)
2468 {
2469 short reloff = t->typestring_offset - *tfsoff;
2470 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2471 reloff, reloff, t->typestring_offset);
2472 }
2473 else
2474 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2475 }
2476
2477 *tfsoff += 2;
2478 }
2479
2480 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2481 {
2482 unsigned int align;
2483 unsigned int start_offset;
2484 unsigned int size;
2485 var_list_t *fields;
2486 unsigned int nbranch = 0;
2487 type_t *deftype = NULL;
2488 short nodeftype = 0xffff;
2489 var_t *f;
2490
2491 guard_rec(type);
2492
2493 align = 0;
2494 size = type_memsize(type, &align);
2495
2496 fields = type_union_get_cases(type);
2497
2498 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2499 {
2500 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2501 if (cases)
2502 nbranch += list_count(cases);
2503 if (f->type)
2504 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2505 }
2506
2507 start_offset = *tfsoff;
2508 update_tfsoff(type, start_offset, file);
2509 print_start_tfs_comment(file, type, start_offset);
2510 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2511 {
2512 const var_t *sv = type_union_get_switch_value(type);
2513 const type_t *st = sv->type;
2514 unsigned char fc;
2515
2516 if (type_get_type(st) == TYPE_BASIC)
2517 {
2518 switch (get_basic_fc(st))
2519 {
2520 case RPC_FC_CHAR:
2521 case RPC_FC_SMALL:
2522 case RPC_FC_BYTE:
2523 case RPC_FC_USMALL:
2524 case RPC_FC_WCHAR:
2525 case RPC_FC_SHORT:
2526 case RPC_FC_USHORT:
2527 case RPC_FC_LONG:
2528 case RPC_FC_ULONG:
2529 fc = get_basic_fc(st);
2530 break;
2531 default:
2532 fc = 0;
2533 error("union switch type must be an integer, char, or enum\n");
2534 }
2535 }
2536 else if (type_get_type(st) == TYPE_ENUM)
2537 fc = get_enum_fc(st);
2538 else
2539 error("union switch type must be an integer, char, or enum\n");
2540
2541 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2542 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2543 0x40 | fc, string_of_type(fc));
2544 *tfsoff += 2;
2545 }
2546 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2547 {
2548 static const expr_t dummy_expr; /* FIXME */
2549 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2550 unsigned char fc;
2551
2552 if (type_get_type(st) == TYPE_BASIC)
2553 {
2554 switch (get_basic_fc(st))
2555 {
2556 case RPC_FC_CHAR:
2557 case RPC_FC_SMALL:
2558 case RPC_FC_USMALL:
2559 case RPC_FC_SHORT:
2560 case RPC_FC_USHORT:
2561 case RPC_FC_LONG:
2562 case RPC_FC_ULONG:
2563 case RPC_FC_ENUM16:
2564 case RPC_FC_ENUM32:
2565 fc = get_basic_fc(st);
2566 break;
2567 default:
2568 fc = 0;
2569 error("union switch type must be an integer, char, or enum\n");
2570 }
2571 }
2572 else if (type_get_type(st) == TYPE_ENUM)
2573 fc = get_enum_fc(st);
2574 else
2575 error("union switch type must be an integer, char, or enum\n");
2576
2577 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2578 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2579 fc, string_of_type(fc));
2580 *tfsoff += 2;
2581
2582 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2583 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2584 *tfsoff += 2;
2585 print_file(file, 0, "/* %u */\n", *tfsoff);
2586 }
2587
2588 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2589 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2590 *tfsoff += 4;
2591
2592 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2593 {
2594 type_t *ft = f->type;
2595 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2596 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2597 expr_t *c;
2598
2599 if (cases == NULL && !deflt)
2600 error("union field %s with neither case nor default attribute\n", f->name);
2601
2602 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2603 {
2604 /* MIDL doesn't check for duplicate cases, even though that seems
2605 like a reasonable thing to do, it just dumps them to the TFS
2606 like we're going to do here. */
2607 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2608 *tfsoff += 4;
2609 write_branch_type(file, ft, tfsoff);
2610 }
2611
2612 /* MIDL allows multiple default branches, even though that seems
2613 illogical, it just chooses the last one, which is what we will
2614 do. */
2615 if (deflt)
2616 {
2617 deftype = ft;
2618 nodeftype = 0;
2619 }
2620 }
2621
2622 if (deftype)
2623 {
2624 write_branch_type(file, deftype, tfsoff);
2625 }
2626 else
2627 {
2628 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2629 *tfsoff += 2;
2630 }
2631
2632 return start_offset;
2633 }
2634
2635 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2636 unsigned int *typeformat_offset)
2637 {
2638 unsigned int i;
2639 unsigned int start_offset = *typeformat_offset;
2640 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2641
2642 if (iid)
2643 {
2644 print_file(file, 2, "0x2f, /* FC_IP */\n");
2645 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2646 *typeformat_offset
2647 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2648 }
2649 else
2650 {
2651 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2652 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2653
2654 if (! uuid)
2655 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2656
2657 update_tfsoff(type, start_offset, file);
2658 print_start_tfs_comment(file, type, start_offset);
2659 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2660 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2661 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2662 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2663 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2664 for (i = 0; i < 8; ++i)
2665 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2666
2667 if (file)
2668 fprintf(file, "\n");
2669
2670 *typeformat_offset += 18;
2671 }
2672 return start_offset;
2673 }
2674
2675 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2676 const var_t *var,
2677 unsigned int *typeformat_offset)
2678 {
2679 unsigned int start_offset = *typeformat_offset;
2680 unsigned char flags = 0;
2681
2682 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2683 flags |= NDR_STRICT_CONTEXT_HANDLE;
2684
2685 if (is_ptr(type))
2686 flags |= 0x80;
2687 if (is_attr(var->attrs, ATTR_IN))
2688 {
2689 flags |= 0x40;
2690 if (!is_attr(var->attrs, ATTR_OUT))
2691 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2692 }
2693 if (is_attr(var->attrs, ATTR_OUT))
2694 flags |= 0x20;
2695
2696 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2697 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2698 /* return and can't be null values overlap */
2699 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2700 print_file(file, 0, "can't be null, ");
2701 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2702 print_file(file, 0, "serialize, ");
2703 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2704 print_file(file, 0, "no serialize, ");
2705 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2706 print_file(file, 0, "strict, ");
2707 if ((flags & 0x21) == 0x20)
2708 print_file(file, 0, "out, ");
2709 if ((flags & 0x21) == 0x21)
2710 print_file(file, 0, "return, ");
2711 if (flags & 0x40)
2712 print_file(file, 0, "in, ");
2713 if (flags & 0x80)
2714 print_file(file, 0, "via ptr, ");
2715 print_file(file, 0, "*/\n");
2716 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2717 print_file(file, 2, "0, /* FIXME: param num */\n");
2718 *typeformat_offset += 4;
2719
2720 return start_offset;
2721 }
2722
2723 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
2724 type_t *type, expr_list_t *range_list,
2725 unsigned int *typeformat_offset)
2726 {
2727 unsigned char fc;
2728 unsigned int start_offset = *typeformat_offset;
2729 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
2730 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
2731
2732 if (type_get_type(type) == TYPE_BASIC)
2733 fc = get_basic_fc(type);
2734 else
2735 fc = get_enum_fc(type);
2736
2737 /* fc must fit in lower 4-bits of 8-bit field below */
2738 assert(fc <= 0xf);
2739
2740 print_file(file, 0, "/* %u */\n", *typeformat_offset);
2741 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
2742 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2743 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_min->cval, range_min->cval);
2744 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_max->cval, range_max->cval);
2745 *typeformat_offset += 10;
2746
2747 return start_offset;
2748 }
2749
2750 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2751 type_t *type, const var_t *var,
2752 int toplevel_param,
2753 unsigned int *typeformat_offset)
2754 {
2755 unsigned int offset;
2756
2757 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2758 {
2759 case TGT_CTXT_HANDLE:
2760 case TGT_CTXT_HANDLE_POINTER:
2761 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2762 case TGT_USER_TYPE:
2763 write_user_tfs(file, type, typeformat_offset);
2764 return type->typestring_offset;
2765 case TGT_STRING:
2766 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2767 case TGT_ARRAY:
2768 {
2769 int ptr_type;
2770 unsigned int off;
2771 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2772 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2773 if (ptr_type != RPC_FC_RP)
2774 {
2775 unsigned int absoff = type->typestring_offset;
2776 short reloff = absoff - (*typeformat_offset + 2);
2777 off = *typeformat_offset;
2778 print_file(file, 0, "/* %d */\n", off);
2779 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2780 string_of_type(ptr_type));
2781 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2782 reloff, reloff, absoff);
2783 *typeformat_offset += 4;
2784 }
2785 return off;
2786 }
2787 case TGT_STRUCT:
2788 if (processed(type)) return type->typestring_offset;
2789 return write_struct_tfs(file, type, var->name, typeformat_offset);
2790 case TGT_UNION:
2791 if (processed(type)) return type->typestring_offset;
2792 return write_union_tfs(file, type, typeformat_offset);
2793 case TGT_ENUM:
2794 case TGT_BASIC:
2795 /* nothing to do */
2796 return 0;
2797 case TGT_RANGE:
2798 {
2799 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
2800 if (!range_list)
2801 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2802 return write_range_tfs(file, var->attrs, type, range_list, typeformat_offset);
2803 }
2804 case TGT_IFACE_POINTER:
2805 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2806 case TGT_POINTER:
2807 if (last_ptr(type))
2808 {
2809 size_t start_offset = *typeformat_offset;
2810 int in_attr = is_attr(var->attrs, ATTR_IN);
2811 int out_attr = is_attr(var->attrs, ATTR_OUT);
2812 const type_t *ref = type_pointer_get_ref(type);
2813
2814 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2815 {
2816 /* special case for pointers to base types */
2817 case TGT_BASIC:
2818 case TGT_ENUM:
2819 {
2820 unsigned char fc;
2821
2822 if (type_get_type(ref) == TYPE_ENUM)
2823 fc = get_enum_fc(ref);
2824 else
2825 fc = get_basic_fc(ref);
2826
2827 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2828 get_pointer_fc(type, var->attrs, toplevel_param),
2829 (!in_attr && out_attr) ? 0x0C : 0x08,
2830 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2831 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2832 print_file(file, indent, "0x%02x, /* %s */\n",
2833 fc, string_of_type(fc));
2834 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2835 *typeformat_offset += 4;
2836 return start_offset;
2837 }
2838 default:
2839 break;
2840 }
2841 }
2842
2843 offset = write_typeformatstring_var(file, indent, func,
2844 type_pointer_get_ref(type), var,
2845 FALSE, typeformat_offset);
2846 if (file)
2847 fprintf(file, "/* %2u */\n", *typeformat_offset);
2848 return write_nonsimple_pointer(file, var->attrs, type,
2849 toplevel_param,
2850 offset, typeformat_offset);
2851 case TGT_INVALID:
2852 break;
2853 }
2854 error("invalid type %s for var %s\n", type->name, var->name);
2855 return 0;
2856 }
2857
2858 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2859 const char *name, int write_ptr, unsigned int *tfsoff)
2860 {
2861 int retmask = 0;
2862
2863 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2864 {
2865 case TGT_USER_TYPE:
2866 write_user_tfs(file, type, tfsoff);
2867 break;
2868 case TGT_STRING:
2869 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2870 break;
2871 case TGT_IFACE_POINTER:
2872 write_ip_tfs(file, attrs, type, tfsoff);
2873 break;
2874 case TGT_POINTER:
2875 {
2876 type_t *ref = type_pointer_get_ref(type);
2877
2878 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2879 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2880
2881 if (write_ptr)
2882 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2883
2884 retmask |= 1;
2885 break;
2886 }
2887 case TGT_ARRAY:
2888 /* conformant arrays and strings are handled specially */
2889 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2890 {
2891 write_array_tfs(file, attrs, type, name, tfsoff);
2892 if (is_conformant_array(type))
2893 retmask |= 1;
2894 }
2895 break;
2896 case TGT_STRUCT:
2897 if (!processed(type))
2898 write_struct_tfs(file, type, name, tfsoff);
2899 break;
2900 case TGT_UNION:
2901 if (!processed(type))
2902 write_union_tfs(file, type, tfsoff);
2903 break;
2904 case TGT_ENUM:
2905 case TGT_BASIC:
2906 /* nothing to do */
2907 break;
2908 case TGT_RANGE:
2909 {
2910 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
2911 if (!range_list)
2912 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2913 write_range_tfs(file, attrs, type, range_list, tfsoff);
2914 break;
2915 }
2916 case TGT_CTXT_HANDLE:
2917 case TGT_CTXT_HANDLE_POINTER:
2918 case TGT_INVALID:
2919 error("invalid type %s for var %s\n", type->name, name);
2920 break;
2921 }
2922
2923 return retmask;
2924 }
2925
2926 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2927 type_pred_t pred, unsigned int *typeformat_offset)
2928 {
2929 const var_t *var;
2930 const statement_t *stmt;
2931
2932 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2933 {
2934 const type_t *iface;
2935 const statement_t *stmt_func;
2936
2937 if (stmt->type == STMT_LIBRARY)
2938 {
2939 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2940 continue;
2941 }
2942 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2943 continue;
2944
2945 iface = stmt->u.type;
2946 if (!pred(iface))
2947 continue;
2948
2949 current_iface = iface;
2950 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2951 {
2952 const var_t *func = stmt_func->u.var;
2953 if (is_local(func->attrs)) continue;
2954
2955 if (!is_void(type_function_get_rettype(func->type)))
2956 {
2957 var_t v = *func;
2958 v.type = type_function_get_rettype(func->type);
2959 update_tfsoff(type_function_get_rettype(func->type),
2960 write_typeformatstring_var(
2961 file, 2, NULL,
2962 type_function_get_rettype(func->type),
2963 &v, FALSE, typeformat_offset),
2964 file);
2965 }
2966
2967 current_func = func;
2968 if (type_get_function_args(func->type))
2969 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2970 update_tfsoff(
2971 var->type,
2972 write_typeformatstring_var(
2973 file, 2, func, var->type, var,
2974 TRUE, typeformat_offset),
2975 file);
2976 }
2977 }
2978
2979 return *typeformat_offset + 1;
2980 }
2981
2982 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2983 {
2984 unsigned int typeformat_offset = 2;
2985
2986 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2987 }
2988
2989
2990 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2991 {
2992 int indent = 0;
2993
2994 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2995 print_file(file, indent, "{\n");
2996 indent++;
2997 print_file(file, indent, "0,\n");
2998 print_file(file, indent, "{\n");
2999 indent++;
3000 print_file(file, indent, "NdrFcShort(0x0),\n");
3001
3002 set_all_tfswrite(TRUE);
3003 process_tfs(file, stmts, pred);
3004
3005 print_file(file, indent, "0x0\n");
3006 indent--;
3007 print_file(file, indent, "}\n");
3008 indent--;
3009 print_file(file, indent, "};\n");
3010 print_file(file, indent, "\n");
3011 }
3012
3013 static unsigned int get_required_buffer_size_type(
3014 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3015 {
3016 *alignment = 0;
3017 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3018 {
3019 case TGT_USER_TYPE:
3020 {
3021 const char *uname;
3022 const type_t *utype = get_user_type(type, &uname);
3023 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3024 }
3025 case TGT_BASIC:
3026 switch (get_basic_fc(type))
3027 {
3028 case RPC_FC_BYTE:
3029 case RPC_FC_CHAR:
3030 case RPC_FC_USMALL:
3031 case RPC_FC_SMALL:
3032 *alignment = 4;
3033 return 1;
3034
3035 case RPC_FC_WCHAR:
3036 case RPC_FC_USHORT:
3037 case RPC_FC_SHORT:
3038 *alignment = 4;
3039 return 2;
3040
3041 case RPC_FC_ULONG:
3042 case RPC_FC_LONG:
3043 case RPC_FC_FLOAT:
3044 case RPC_FC_ERROR_STATUS_T:
3045 *alignment = 4;
3046 return 4;
3047
3048 case RPC_FC_HYPER:
3049 case RPC_FC_DOUBLE:
3050 *alignment = 8;
3051 return 8;
3052
3053 case RPC_FC_INT3264:
3054 case RPC_FC_UINT3264:
3055 assert( pointer_size );
3056 *alignment = pointer_size;
3057 return pointer_size;
3058
3059 case RPC_FC_IGNORE:
3060 case RPC_FC_BIND_PRIMITIVE:
3061 return 0;
3062
3063 default:
3064 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3065 get_basic_fc(type));
3066 return 0;
3067 }
3068 break;
3069
3070 case TGT_ENUM:
3071 switch (get_enum_fc(type))
3072 {
3073 case RPC_FC_ENUM32:
3074 *alignment = 4;
3075 return 4;
3076 case RPC_FC_ENUM16:
3077 *alignment = 4;
3078 return 2;
3079 }
3080 break;
3081
3082 case TGT_STRUCT:
3083 if (get_struct_fc(type) == RPC_FC_STRUCT)
3084 {
3085 if (!type_struct_get_fields(type)) return 0;
3086 return fields_memsize(type_struct_get_fields(type), alignment);
3087 }
3088 break;
3089
3090 case TGT_POINTER:
3091 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3092 {
3093 const type_t *ref = type_pointer_get_ref(type);
3094 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3095 {
3096 case TGT_BASIC:
3097 case TGT_ENUM:
3098 case TGT_RANGE:
3099 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3100 case TGT_STRUCT:
3101 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3102 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3103 break;
3104 case TGT_USER_TYPE:
3105 case TGT_CTXT_HANDLE:
3106 case TGT_CTXT_HANDLE_POINTER:
3107 case TGT_STRING:
3108 case TGT_POINTER:
3109 case TGT_ARRAY:
3110 case TGT_IFACE_POINTER:
3111 case TGT_UNION:
3112 case TGT_INVALID:
3113 break;
3114 }
3115 }
3116 break;
3117
3118 case TGT_ARRAY:
3119 /* FIXME: depends on pointer type */
3120 return type_array_get_dim(type) *
3121 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3122
3123 default:
3124 break;
3125 }
3126 return 0;
3127 }
3128
3129 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3130 {
3131 int in_attr = is_attr(var->attrs, ATTR_IN);
3132 int out_attr = is_attr(var->attrs, ATTR_OUT);
3133
3134 if (!in_attr && !out_attr)
3135 in_attr = 1;
3136
3137 *alignment = 0;
3138
3139 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3140 pass == PASS_RETURN)
3141 {
3142 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3143 {
3144 *alignment = 4;
3145 return 20;
3146 }
3147
3148 if (!is_string_type(var->attrs, var->type))
3149 return get_required_buffer_size_type(var->type, var->name,
3150 var->attrs, TRUE, alignment);
3151 }
3152 return 0;
3153 }
3154
3155 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3156 {
3157 const var_t *var;
3158 unsigned int total_size = 0, alignment;
3159
3160 if (type_get_function_args(func->type))
3161 {
3162 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3163 {
3164 total_size += get_required_buffer_size(var, &alignment, pass);
3165 total_size += alignment;
3166 }
3167 }
3168
3169 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3170 {
3171 var_t v = *func;
3172 v.type = type_function_get_rettype(func->type);
3173 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3174 total_size += alignment;
3175 }
3176 return total_size;
3177 }
3178
3179 static void print_phase_function(FILE *file, int indent, const char *type,
3180 const char *local_var_prefix, enum remoting_phase phase,
3181 const var_t *var, unsigned int type_offset)
3182 {
3183 const char *function;
3184 switch (phase)
3185 {
3186 case PHASE_BUFFERSIZE:
3187 function = "BufferSize";
3188 break;
3189 case PHASE_MARSHAL:
3190 function = "Marshall";
3191 break;
3192 case PHASE_UNMARSHAL:
3193 function = "Unmarshall";
3194 break;
3195 case PHASE_FREE:
3196 function = "Free";
3197 break;
3198 default:
3199 assert(0);
3200 return;
3201 }
3202
3203 print_file(file, indent, "Ndr%s%s(\n", type, function);
3204 indent++;
3205 print_file(file, indent, "&__frame->_StubMsg,\n");
3206 print_file(file, indent, "%s%s%s%s%s,\n",
3207 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3208 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3209 local_var_prefix,
3210 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3211 var->name);
3212 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3213 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3214 if (phase == PHASE_UNMARSHAL)
3215 print_file(file, indent, "0);\n");
3216 indent--;
3217 }
3218
3219 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3220 enum remoting_phase phase, enum pass pass, const var_t *var,
3221 const char *varname)
3222 {
3223 type_t *type = var->type;
3224 unsigned int size;
3225 unsigned int alignment = 0;
3226
3227 /* no work to do for other phases, buffer sizing is done elsewhere */
3228 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3229 return;
3230
3231 if (type_get_type(type) == TYPE_ENUM ||
3232 (type_get_type(type) == TYPE_BASIC &&
3233 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3234 pointer_size != 4))
3235 {
3236 unsigned char fc;
3237
3238 if (type_get_type(type) == TYPE_ENUM)
3239 fc = get_enum_fc(type);
3240 else
3241 fc = get_basic_fc(type);
3242
3243 if (phase == PHASE_MARSHAL)
3244 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3245 else
3246 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3247 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3248 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3249 local_var_prefix,
3250 var->name);
3251 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3252 }
3253 else
3254 {
3255 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3256 switch (get_basic_fc(ref))
3257 {
3258 case RPC_FC_BYTE:
3259 case RPC_FC_CHAR:
3260 case RPC_FC_SMALL:
3261 case RPC_FC_USMALL:
3262 size = 1;
3263 alignment = 1;
3264 break;
3265
3266 case RPC_FC_WCHAR:
3267 case RPC_FC_USHORT:
3268 case RPC_FC_SHORT:
3269 size = 2;
3270 alignment = 2;
3271 break;
3272
3273 case RPC_FC_ULONG:
3274 case RPC_FC_LONG:
3275 case RPC_FC_FLOAT:
3276 case RPC_FC_ERROR_STATUS_T:
3277 /* pointer_size must be 4 if we got here in these two cases */
3278 case RPC_FC_INT3264:
3279 case RPC_FC_UINT3264:
3280 size = 4;
3281 alignment = 4;
3282 break;
3283
3284 case RPC_FC_HYPER:
3285 case RPC_FC_DOUBLE:
3286 size = 8;
3287 alignment = 8;
3288 break;
3289
3290 case RPC_FC_IGNORE:
3291 case RPC_FC_BIND_PRIMITIVE:
3292 /* no marshalling needed */
3293 return;
3294
3295 default:
3296 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3297 var->name, get_basic_fc(ref));
3298 size = 0;
3299 }
3300
3301 if (phase == PHASE_MARSHAL)
3302 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3303 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3304 alignment - 1, alignment - 1);
3305
3306 if (phase == PHASE_MARSHAL)
3307 {
3308 print_file(file, indent, "*(");
3309 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3310 if (is_ptr(type))
3311 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3312 else
3313 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3314 fprintf(file, "%s%s", local_var_prefix, varname);
3315 fprintf(file, ";\n");
3316 }
3317 else if (phase == PHASE_UNMARSHAL)
3318 {
3319 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3320 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3321 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3322 print_file(file, indent, "{\n");
3323 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3324 print_file(file, indent, "}\n");
3325 print_file(file, indent, "%s%s%s",
3326 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3327 local_var_prefix, varname);
3328 if (pass == PASS_IN && is_ptr(type))
3329 fprintf(file, " = (");
3330 else
3331 fprintf(file, " = *(");
3332 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3333 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3334 }
3335
3336 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3337 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3338 fprintf(file, ");\n");
3339 }
3340 }
3341
3342 /* returns whether the MaxCount, Offset or ActualCount members need to be
3343 * filled in for the specified phase */
3344 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3345 {
3346 return (phase != PHASE_UNMARSHAL);
3347 }
3348
3349 expr_t *get_size_is_expr(const type_t *t, const char *name)
3350 {
3351 expr_t *x = NULL;
3352
3353 for ( ; is_array(t); t = type_array_get_element(t))
3354 if (type_array_has_conformance(t))
3355 {
3356 if (!x)
3357 x = type_array_get_conformance(t);
3358 else
3359 error("%s: multidimensional conformant"
3360 " arrays not supported at the top level\n",
3361 name);
3362 }
3363
3364 return x;
3365 }
3366
3367 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3368 enum remoting_phase phase, const var_t *var)
3369 {
3370 const type_t *type = var->type;
3371 /* get fundamental type for the argument */
3372 for (;;)
3373 {
3374 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3375 break;
3376 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3377 break;
3378 else if (type_is_alias(type))
3379 type = type_alias_get_aliasee(type);
3380 else if (is_array(type))
3381 {
3382 if (is_conformance_needed_for_phase(phase) && is_array(type))
3383 {
3384 if (type_array_has_conformance(type))
3385 {
3386 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3387 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3388 fprintf(file, ";\n\n");
3389 }
3390 if (type_array_has_variance(type))
3391 {
3392 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3393 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3394 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3395 fprintf(file, ";\n\n");
3396 }
3397 }
3398 break;
3399 }
3400 else if (type_get_type(type) == TYPE_UNION)
3401 {
3402 if (is_conformance_needed_for_phase(phase))
3403 {
3404 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3405 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3406 fprintf(file, ";\n\n");
3407 }
3408 break;
3409 }
3410 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3411 {
3412 expr_t *iid;
3413
3414 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3415 {
3416 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3417 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3418 fprintf( file, ";\n\n" );
3419 }
3420 break;
3421 }
3422 else if (is_ptr(type))
3423 type = type_pointer_get_ref(type);
3424 else
3425 break;
3426 }
3427 }
3428
3429 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3430 enum pass pass, enum remoting_phase phase, const var_t *var)
3431 {
3432 int in_attr, out_attr, pointer_type;
3433 const type_t *type = var->type;
3434 unsigned int start_offset = type->typestring_offset;
3435
3436 if (is_ptr(type) || is_array(type))
3437 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3438 else
3439 pointer_type = 0;
3440
3441 in_attr = is_attr(var->attrs, ATTR_IN);
3442 out_attr = is_attr(var->attrs, ATTR_OUT);
3443 if (!in_attr && !out_attr)
3444 in_attr = 1;
3445
3446 if (phase != PHASE_FREE)
3447 switch (pass)
3448 {
3449 case PASS_IN:
3450 if (!in_attr) return;
3451 break;
3452 case PASS_OUT:
3453 if (!out_attr) return;
3454 break;
3455 case PASS_RETURN:
3456 break;
3457 }
3458
3459 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3460
3461 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3462 {
3463 case TGT_CTXT_HANDLE:
3464 case TGT_CTXT_HANDLE_POINTER:
3465 if (phase == PHASE_MARSHAL)
3466 {
3467 if (pass == PASS_IN)
3468 {
3469 /* if the context_handle attribute appears in the chain of types
3470 * without pointers being followed, then the context handle must
3471 * be direct, otherwise it is a pointer */
3472 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3473 print_file(file, indent, "NdrClientContextMarshall(\n");
3474 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3475 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3476 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "");
3477 }
3478 else
3479 {
3480 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3481 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3482 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3483 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3484 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3485 }
3486 }
3487 else if (phase == PHASE_UNMARSHAL)
3488 {
3489 if (pass == PASS_OUT)
3490 {
3491 if (!in_attr)
3492 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3493 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3494 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3495 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3496 print_file(file, indent + 1, "__frame->_Handle);\n");
3497 }
3498 else
3499 {
3500 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3501 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3502 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3503 }
3504 }
3505 break;
3506 case TGT_USER_TYPE:
3507 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3508 break;
3509 case TGT_STRING:
3510 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3511 pointer_type != RPC_FC_RP)
3512 {
3513 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3514 !in_attr && is_conformant_array(type))
3515 {
3516 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3517 indent++;
3518 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3519 }
3520 /* strings returned are assumed to be global and hence don't
3521 * need freeing */
3522 else if (is_declptr(type) &&
3523 !(phase == PHASE_FREE && pass == PASS_RETURN))
3524 print_phase_function(file, indent, "Pointer", local_var_prefix,
3525 phase, var, start_offset);
3526 }
3527 else
3528 {
3529 unsigned int real_start_offset = start_offset;
3530 /* skip over pointer description straight to string description */
3531 if (is_declptr(type))
3532 {
3533 if (is_conformant_array(type))
3534 real_start_offset += 4;
3535 else
3536 real_start_offset += 2;
3537 }
3538 if (is_array(type) && !is_conformant_array(type))
3539 print_phase_function(file, indent, "NonConformantString",
3540 local_var_prefix, phase, var,
3541 real_start_offset);
3542 else
3543 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3544 phase, var, real_start_offset);
3545 }
3546 break;
3547 case TGT_ARRAY:
3548 {
3549 unsigned char tc = get_array_fc(type);
3550 const char *array_type = "FixedArray";
3551
3552 /* We already have the size_is expression since it's at the
3553 top level, but do checks for multidimensional conformant
3554 arrays. When we handle them, we'll need to extend this
3555 function to return a list, and then we'll actually use
3556 the return value. */
3557 get_size_is_expr(type, var->name);
3558
3559 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3560 {
3561 array_type = "VaryingArray";
3562 }
3563 else if (tc == RPC_FC_CARRAY)
3564 {
3565 array_type = "ConformantArray";
3566 }
3567 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3568 {
3569 array_type = (tc == RPC_FC_BOGUS_ARRAY
3570 ? "ComplexArray"
3571 : "ConformantVaryingArray");
3572 }
3573
3574 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3575 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3576 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3577 {
3578 /* these are all unmarshalled by allocating memory */
3579 if (tc == RPC_FC_BOGUS_ARRAY ||
3580 tc == RPC_FC_CVARRAY ||
3581 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3582 (tc == RPC_FC_CARRAY && !in_attr))
3583 {
3584 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3585 indent++;
3586 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3587 }
3588 }
3589 break;
3590 }
3591 case TGT_BASIC:
3592 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3593 break;
3594 case TGT_ENUM:
3595 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3596 break;
3597 case TGT_RANGE:
3598 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3599 /* Note: this goes beyond what MIDL does - it only supports arguments
3600 * with the [range] attribute in Oicf mode */
3601 if (phase == PHASE_UNMARSHAL)
3602 {
3603 const expr_t *range_min;
3604 const expr_t *range_max;
3605 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
3606 if (!range_list)
3607 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3608 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3609 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3610
3611 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
3612 write_type_decl(file, var->type, NULL);
3613 fprintf(file, ")0x%lx) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
3614 write_type_decl(file, var->type, NULL);
3615 fprintf(file, ")0x%lx))\n", range_max->cval);
3616 print_file(file, indent, "{\n");
3617 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3618 print_file(file, indent, "}\n");
3619 }
3620 break;
3621 case TGT_STRUCT:
3622 switch (get_struct_fc(type))
3623 {
3624 case RPC_FC_STRUCT:
3625 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3626 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3627 break;
3628 case RPC_FC_PSTRUCT:
3629 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3630 break;
3631 case RPC_FC_CSTRUCT:
3632 case RPC_FC_CPSTRUCT:
3633 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3634 break;
3635 case RPC_FC_CVSTRUCT:
3636 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3637 break;
3638 case RPC_FC_BOGUS_STRUCT:
3639 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3640 break;
3641 default:
3642 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3643 }
3644 break;
3645 case TGT_UNION:
3646 {
3647 const char *union_type = NULL;
3648
3649 if (type_get_type(type) == TYPE_UNION)
3650 union_type = "NonEncapsulatedUnion";
3651 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3652 union_type = "EncapsulatedUnion";
3653
3654 print_phase_function(file, indent, union_type, local_var_prefix,
3655 phase, var, start_offset);
3656 break;
3657 }
3658 case TGT_POINTER:
3659 {
3660 const type_t *ref = type_pointer_get_ref(type);
3661 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3662 {
3663 case TGT_BASIC:
3664 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3665 break;
3666 case TGT_ENUM:
3667 /* base types have known sizes, so don't need a sizing pass
3668 * and don't have any memory to free and so don't need a
3669 * freeing pass */
3670 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3671 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3672 break;
3673 case TGT_STRUCT:
3674 {
3675 const char *struct_type = NULL;
3676 switch (get_struct_fc(ref))
3677 {
3678 case RPC_FC_STRUCT:
3679 /* simple structs have known sizes, so don't need a sizing
3680 * pass and don't have any memory to free and so don't
3681 * need a freeing pass */
3682 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3683 struct_type = "SimpleStruct";
3684 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3685 {
3686 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3687 indent++;
3688 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3689 indent--;
3690 }
3691 break;
3692 case RPC_FC_PSTRUCT:
3693 struct_type = "SimpleStruct";
3694 break;
3695 case RPC_FC_CSTRUCT:
3696 case RPC_FC_CPSTRUCT:
3697 struct_type = "ConformantStruct";
3698 break;
3699 case RPC_FC_CVSTRUCT:
3700 struct_type = "ConformantVaryingStruct";
3701 break;
3702 case RPC_FC_BOGUS_STRUCT:
3703 struct_type = "ComplexStruct";
3704 break;
3705 default:
3706 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3707 }
3708
3709 if (struct_type)
3710 {
3711 if (phase == PHASE_FREE)
3712 struct_type = "Pointer";
3713 else
3714 start_offset = ref->typestring_offset;
3715 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3716 }
3717 break;
3718 }
3719 case TGT_UNION:
3720 {
3721 const char *union_type = NULL;
3722 if (phase == PHASE_FREE)
3723 union_type = "Pointer";
3724 else
3725 {
3726 if (type_get_type(ref) == TYPE_UNION)
3727 union_type = "NonEncapsulatedUnion";
3728 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3729 union_type = "EncapsulatedUnion";
3730
3731 start_offset = ref->typestring_offset;
3732 }
3733
3734 print_phase_function(file, indent, union_type, local_var_prefix,
3735 phase, var, start_offset);
3736 break;
3737 }
3738 case TGT_STRING:
3739 case TGT_POINTER:
3740 case TGT_ARRAY:
3741 case TGT_RANGE:
3742 case TGT_IFACE_POINTER:
3743 case TGT_USER_TYPE:
3744 case TGT_CTXT_HANDLE:
3745 case TGT_CTXT_HANDLE_POINTER:
3746 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3747 break;
3748 case TGT_INVALID:
3749 assert(0);
3750 break;
3751 }
3752 else
3753 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3754 break;
3755 }
3756 case TGT_IFACE_POINTER:
3757 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3758 break;
3759 case TGT_INVALID:
3760 assert(0);
3761 break;
3762 }
3763 fprintf(file, "\n");
3764 }
3765
3766 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3767 enum pass pass, enum remoting_phase phase)
3768 {
3769 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3770 {
3771 unsigned int size = get_function_buffer_size( func, pass );
3772 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3773 }
3774
3775 if (pass == PASS_RETURN)
3776 {
3777 var_t var;
3778 var = *func;
3779 var.type = type_function_get_rettype(func->type);
3780 var.name = xstrdup( "_RetVal" );
3781 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3782 free( var.name );
3783 }
3784 else
3785 {
3786 const var_t *var;
3787 if (!type_get_function_args(func->type))
3788 return;
3789 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3790 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3791 }
3792 }
3793
3794
3795 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3796 {
3797 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3798 }
3799
3800
3801 unsigned int get_size_procformatstring_func(const var_t *func)
3802 {
3803 const var_t *var;
3804 unsigned int size = 0;
3805
3806 /* argument list size */
3807 if (type_get_function_args(func->type))
3808 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3809 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3810
3811 /* return value size */
3812 if (is_void(type_function_get_rettype(func->type)))
3813 size += 2; /* FC_END and FC_PAD */
3814 else
3815 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3816
3817 return size;
3818 }
3819
3820 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3821 {
3822 const statement_t *stmt;
3823 unsigned int size = 1;
3824
3825 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3826 {
3827 const type_t *iface;
3828 const statement_t *stmt_func;
3829
3830 if (stmt->type == STMT_LIBRARY)
3831 {
3832 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3833 continue;
3834 }
3835 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3836 continue;
3837
3838 iface = stmt->u.type;
3839 if (!pred(iface))
3840 continue;
3841
3842 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3843 {
3844 const var_t *func = stmt_func->u.var;
3845 if (!is_local(func->attrs))
3846 size += get_size_procformatstring_func( func );
3847 }
3848 }
3849 return size;
3850 }
3851
3852 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3853 {
3854 set_all_tfswrite(FALSE);
3855 return process_tfs(NULL, stmts, pred);
3856 }
3857
3858 void declare_stub_args( FILE *file, int indent, const var_t *func )
3859 {
3860 int in_attr, out_attr;
3861 int i = 0;
3862 const var_t *var;
3863
3864 /* declare return value '_RetVal' */
3865 if (!is_void(type_function_get_rettype(func->type)))
3866 {
3867 print_file(file, indent, "%s", "");
3868 write_type_decl_left(file, type_function_get_rettype(func->type));
3869 fprintf(file, " _RetVal;\n");
3870 }
3871
3872 if (!type_get_function_args(func->type))
3873 return;
3874
3875 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3876 {
3877 in_attr = is_attr(var->attrs, ATTR_IN);
3878 out_attr = is_attr(var->attrs, ATTR_OUT);
3879 if (!out_attr && !in_attr)
3880 in_attr = 1;
3881
3882 if (is_context_handle(var->type))
3883 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3884 else
3885 {
3886 if (!in_attr && !is_conformant_array(var->type))
3887 {
3888 type_t *type_to_print;
3889 char name[16];
3890 print_file(file, indent, "%s", "");
3891 if (type_get_type(var->type) == TYPE_ARRAY &&
3892 !type_array_is_decl_as_ptr(var->type))
3893 type_to_print = var->type;
3894 else
3895 type_to_print = type_pointer_get_ref(var->type);
3896 sprintf(name, "_W%u", i++);
3897 write_type_decl(file, type_to_print, name);
3898 fprintf(file, ";\n");
3899 }
3900
3901 print_file(file, indent, "%s", "");
3902 write_type_decl_left(file, var->type);
3903 fprintf(file, " ");
3904 if (type_get_type(var->type) == TYPE_ARRAY &&
3905 !type_array_is_decl_as_ptr(var->type)) {
3906 fprintf(file, "(*%s)", var->name);
3907 } else
3908 fprintf(file, "%s", var->name);
3909 write_type_right(file, var->type, FALSE);
3910 fprintf(file, ";\n");
3911
3912 if (decl_indirect(var->type))
3913 print_file(file, indent, "void *_p_%s;\n", var->name);
3914 }
3915 }
3916 }
3917
3918
3919 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3920 {
3921 int in_attr, out_attr;
3922 int i = 0, sep = 0;
3923 const var_t *var;
3924
3925 if (!type_get_function_args(func->type))
3926 return;
3927
3928 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3929 {
3930 in_attr = is_attr(var->attrs, ATTR_IN);
3931 out_attr = is_attr(var->attrs, ATTR_OUT);
3932 if (!out_attr && !in_attr)
3933 in_attr = 1;
3934
3935 if (!in_attr)
3936 {
3937 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3938
3939 if (is_context_handle(var->type))
3940 {
3941 fprintf(file, " = NdrContextHandleInitialize(\n");
3942 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3943 print_file(