5804946c111df6b55d9b6fee63a966eecd24f64d
[reactos.git] / reactos / tools / widl / typegen.c
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 type_t *current_structure;
50 static const var_t *current_func;
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 *iface;
58 const type_t *cont_type;
59 char *name;
60 unsigned int baseoff;
61 const expr_t *expr;
62 };
63
64 enum type_context
65 {
66 TYPE_CONTEXT_TOPLEVELPARAM,
67 TYPE_CONTEXT_PARAM,
68 TYPE_CONTEXT_CONTAINER,
69 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
70 };
71
72 /* parameter flags in Oif mode */
73 static const unsigned short MustSize = 0x0001;
74 static const unsigned short MustFree = 0x0002;
75 static const unsigned short IsPipe = 0x0004;
76 static const unsigned short IsIn = 0x0008;
77 static const unsigned short IsOut = 0x0010;
78 static const unsigned short IsReturn = 0x0020;
79 static const unsigned short IsBasetype = 0x0040;
80 static const unsigned short IsByValue = 0x0080;
81 static const unsigned short IsSimpleRef = 0x0100;
82 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
83 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
84
85 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
86 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
87 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align);
88 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
89 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
90 const char *name, int write_ptr, unsigned int *tfsoff);
91 static const var_t *find_array_or_string_in_struct(const type_t *type);
92 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
93 type_t *type, enum type_context context,
94 const char *name, unsigned int *typestring_offset);
95 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
96 const attr_list_t *attrs, int toplevel_param,
97 unsigned int *alignment );
98 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
99
100 static const char *string_of_type(unsigned char type)
101 {
102 switch (type)
103 {
104 case RPC_FC_BYTE: return "FC_BYTE";
105 case RPC_FC_CHAR: return "FC_CHAR";
106 case RPC_FC_SMALL: return "FC_SMALL";
107 case RPC_FC_USMALL: return "FC_USMALL";
108 case RPC_FC_WCHAR: return "FC_WCHAR";
109 case RPC_FC_SHORT: return "FC_SHORT";
110 case RPC_FC_USHORT: return "FC_USHORT";
111 case RPC_FC_LONG: return "FC_LONG";
112 case RPC_FC_ULONG: return "FC_ULONG";
113 case RPC_FC_FLOAT: return "FC_FLOAT";
114 case RPC_FC_HYPER: return "FC_HYPER";
115 case RPC_FC_DOUBLE: return "FC_DOUBLE";
116 case RPC_FC_ENUM16: return "FC_ENUM16";
117 case RPC_FC_ENUM32: return "FC_ENUM32";
118 case RPC_FC_IGNORE: return "FC_IGNORE";
119 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
120 case RPC_FC_RP: return "FC_RP";
121 case RPC_FC_UP: return "FC_UP";
122 case RPC_FC_OP: return "FC_OP";
123 case RPC_FC_FP: return "FC_FP";
124 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
125 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
126 case RPC_FC_STRUCT: return "FC_STRUCT";
127 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
128 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
129 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
130 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
131 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
132 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
133 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
134 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
135 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
136 case RPC_FC_CARRAY: return "FC_CARRAY";
137 case RPC_FC_CVARRAY: return "FC_CVARRAY";
138 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
139 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
140 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
141 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
142 case RPC_FC_POINTER: return "FC_POINTER";
143 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
144 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
145 case RPC_FC_CSTRING: return "FC_CSTRING";
146 case RPC_FC_WSTRING: return "FC_WSTRING";
147 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
148 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
149 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
150 case RPC_FC_IP: return "FC_IP";
151 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
152 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
153 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
154 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
155 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
156 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
157 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
158 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
159 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
160 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
161 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
162 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
163 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
164 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
165 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
166 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
167 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
168 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
169 case RPC_FC_PP: return "FC_PP";
170 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
171 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
172 case RPC_FC_DIV_2: return "FC_DIV_2";
173 case RPC_FC_MULT_2: return "FC_MULT_2";
174 case RPC_FC_ADD_1: return "FC_ADD_1";
175 case RPC_FC_SUB_1: return "FC_SUB_1";
176 case RPC_FC_CALLBACK: return "FC_CALLBACK";
177 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
178 case RPC_FC_END: return "FC_END";
179 case RPC_FC_PAD: return "FC_PAD";
180 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
181 case RPC_FC_RANGE: return "FC_RANGE";
182 case RPC_FC_INT3264: return "FC_INT3264";
183 case RPC_FC_UINT3264: return "FC_UINT3264";
184 default:
185 error("string_of_type: unknown type 0x%02x\n", type);
186 return NULL;
187 }
188 }
189
190 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
191 {
192 const type_t *t = type;
193 for (;;)
194 {
195 if (is_attr(t->attrs, attr))
196 return get_attrp(t->attrs, attr);
197 else if (type_is_alias(t))
198 t = type_alias_get_aliasee(t);
199 else return NULL;
200 }
201 }
202
203 unsigned char get_basic_fc(const type_t *type)
204 {
205 int sign = type_basic_get_sign(type);
206 switch (type_basic_get_type(type))
207 {
208 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
209 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
210 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
211 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
212 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
213 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
214 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
215 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
216 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
217 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
218 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
219 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
220 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
221 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
222 }
223 return 0;
224 }
225
226 static unsigned char get_basic_fc_signed(const type_t *type)
227 {
228 switch (type_basic_get_type(type))
229 {
230 case TYPE_BASIC_INT8: return RPC_FC_SMALL;
231 case TYPE_BASIC_INT16: return RPC_FC_SHORT;
232 case TYPE_BASIC_INT32: return RPC_FC_LONG;
233 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
234 case TYPE_BASIC_INT: return RPC_FC_LONG;
235 case TYPE_BASIC_INT3264: return RPC_FC_INT3264;
236 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
237 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
238 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
239 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
240 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
241 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
242 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
243 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
244 }
245 return 0;
246 }
247
248 static inline unsigned int clamp_align(unsigned int align)
249 {
250 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
251 if(align > packing) align = packing;
252 return align;
253 }
254
255 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
256 {
257 const type_t *t;
258 int pointer_type;
259
260 assert(is_ptr(type) || is_array(type));
261
262 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
263 if (pointer_type)
264 return pointer_type;
265
266 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
267 {
268 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
269 if (pointer_type)
270 return pointer_type;
271 }
272
273 if (toplevel_param)
274 return RPC_FC_RP;
275 else if (is_ptr(type))
276 return type_pointer_get_default_fc(type);
277 else
278 return type_array_get_ptr_default_fc(type);
279 }
280
281 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
282 enum type_context context )
283 {
284 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
285
286 if (pointer_fc == RPC_FC_UP && is_attr( attrs, ATTR_OUT ) &&
287 context == TYPE_CONTEXT_PARAM && is_object( current_iface ))
288 pointer_fc = RPC_FC_OP;
289
290 return pointer_fc;
291 }
292
293 static unsigned char get_enum_fc(const type_t *type)
294 {
295 assert(type_get_type(type) == TYPE_ENUM);
296 if (is_aliaschain_attr(type, ATTR_V1ENUM))
297 return RPC_FC_ENUM32;
298 else
299 return RPC_FC_ENUM16;
300 }
301
302 static type_t *get_user_type(const type_t *t, const char **pname)
303 {
304 for (;;)
305 {
306 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
307 if (ut)
308 {
309 if (pname)
310 *pname = t->name;
311 return ut;
312 }
313
314 if (type_is_alias(t))
315 t = type_alias_get_aliasee(t);
316 else
317 return NULL;
318 }
319 }
320
321 static int is_user_type(const type_t *t)
322 {
323 return get_user_type(t, NULL) != NULL;
324 }
325
326 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
327 {
328 if (is_user_type(type))
329 return TGT_USER_TYPE;
330
331 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
332 return TGT_CTXT_HANDLE;
333
334 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
335 return TGT_STRING;
336
337 switch (type_get_type(type))
338 {
339 case TYPE_BASIC:
340 if (!(flags & TDT_IGNORE_RANGES) &&
341 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
342 return TGT_RANGE;
343 return TGT_BASIC;
344 case TYPE_ENUM:
345 if (!(flags & TDT_IGNORE_RANGES) &&
346 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
347 return TGT_RANGE;
348 return TGT_ENUM;
349 case TYPE_POINTER:
350 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
351 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
352 return TGT_IFACE_POINTER;
353 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
354 return TGT_CTXT_HANDLE_POINTER;
355 else
356 return TGT_POINTER;
357 case TYPE_STRUCT:
358 return TGT_STRUCT;
359 case TYPE_ENCAPSULATED_UNION:
360 case TYPE_UNION:
361 return TGT_UNION;
362 case TYPE_ARRAY:
363 return TGT_ARRAY;
364 case TYPE_FUNCTION:
365 case TYPE_COCLASS:
366 case TYPE_INTERFACE:
367 case TYPE_MODULE:
368 case TYPE_VOID:
369 case TYPE_ALIAS:
370 case TYPE_BITFIELD:
371 break;
372 }
373 return TGT_INVALID;
374 }
375
376 static int cant_be_null(const var_t *v)
377 {
378 switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
379 {
380 case TGT_ARRAY:
381 if (!type_array_is_decl_as_ptr( v->type )) return 0;
382 /* fall through */
383 case TGT_POINTER:
384 return (get_pointer_fc(v->type, v->attrs, TRUE) == RPC_FC_RP);
385 case TGT_CTXT_HANDLE_POINTER:
386 return TRUE;
387 default:
388 return 0;
389 }
390
391 }
392
393 static int get_padding(const var_list_t *fields)
394 {
395 unsigned short offset = 0;
396 unsigned int salign = 1;
397 const var_t *f;
398
399 if (!fields)
400 return 0;
401
402 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
403 {
404 type_t *ft = f->type;
405 unsigned int align = 0;
406 unsigned int size = type_memsize_and_alignment(ft, &align);
407 align = clamp_align(align);
408 if (align > salign) salign = align;
409 offset = ROUND_SIZE(offset, align);
410 offset += size;
411 }
412
413 return ROUNDING(offset, salign);
414 }
415
416 static unsigned int get_stack_size( const var_t *var, int *by_value )
417 {
418 unsigned int stack_size;
419 int by_val;
420
421 switch (typegen_detect_type( var->type, var->attrs, TDT_ALL_TYPES ))
422 {
423 case TGT_BASIC:
424 case TGT_ENUM:
425 case TGT_RANGE:
426 case TGT_STRUCT:
427 case TGT_UNION:
428 case TGT_USER_TYPE:
429 stack_size = type_memsize( var->type );
430 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
431 break;
432 default:
433 by_val = 0;
434 break;
435 }
436 if (!by_val) stack_size = pointer_size;
437 if (by_value) *by_value = by_val;
438 return ROUND_SIZE( stack_size, pointer_size );
439 }
440
441 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
442 const type_t *type )
443 {
444 unsigned char flags = 0;
445
446 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
447
448 if (is_ptr(type) &&
449 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
450 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
451 flags |= 0x80;
452
453 if (is_attr(attrs, ATTR_IN))
454 {
455 flags |= 0x40;
456 if (!is_attr(attrs, ATTR_OUT)) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
457 }
458 if (is_attr(attrs, ATTR_OUT)) flags |= 0x20;
459
460 return flags;
461 }
462
463 static unsigned int get_rpc_flags( const attr_list_t *attrs )
464 {
465 unsigned int flags = 0;
466
467 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
468 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
469 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
470 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
471 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
472 return flags;
473 }
474
475 unsigned char get_struct_fc(const type_t *type)
476 {
477 int has_pointer = 0;
478 int has_conformance = 0;
479 int has_variance = 0;
480 var_t *field;
481 var_list_t *fields;
482
483 fields = type_struct_get_fields(type);
484
485 if (get_padding(fields))
486 return RPC_FC_BOGUS_STRUCT;
487
488 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
489 {
490 type_t *t = field->type;
491 enum typegen_type typegen_type;
492
493 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
494
495 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
496 {
497 if (is_string_type(field->attrs, field->type))
498 {
499 if (is_conformant_array(t))
500 has_conformance = 1;
501 has_variance = 1;
502 continue;
503 }
504
505 if (is_array(type_array_get_element(field->type)))
506 return RPC_FC_BOGUS_STRUCT;
507
508 if (type_array_has_conformance(field->type))
509 {
510 has_conformance = 1;
511 if (list_next(fields, &field->entry))
512 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
513 field->name);
514 }
515 if (type_array_has_variance(t))
516 has_variance = 1;
517
518 t = type_array_get_element(t);
519 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
520 }
521
522 switch (typegen_type)
523 {
524 case TGT_USER_TYPE:
525 case TGT_IFACE_POINTER:
526 return RPC_FC_BOGUS_STRUCT;
527 case TGT_BASIC:
528 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
529 return RPC_FC_BOGUS_STRUCT;
530 break;
531 case TGT_ENUM:
532 if (get_enum_fc(t) == RPC_FC_ENUM16)
533 return RPC_FC_BOGUS_STRUCT;
534 break;
535 case TGT_POINTER:
536 case TGT_ARRAY:
537 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
538 return RPC_FC_BOGUS_STRUCT;
539 has_pointer = 1;
540 break;
541 case TGT_UNION:
542 return RPC_FC_BOGUS_STRUCT;
543 case TGT_STRUCT:
544 {
545 unsigned char fc = get_struct_fc(t);
546 switch (fc)
547 {
548 case RPC_FC_STRUCT:
549 break;
550 case RPC_FC_CVSTRUCT:
551 has_conformance = 1;
552 has_variance = 1;
553 has_pointer = 1;
554 break;
555
556 case RPC_FC_CPSTRUCT:
557 has_conformance = 1;
558 if (list_next( fields, &field->entry ))
559 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
560 field->name);
561 has_pointer = 1;
562 break;
563
564 case RPC_FC_CSTRUCT:
565 has_conformance = 1;
566 if (list_next( fields, &field->entry ))
567 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
568 field->name);
569 break;
570
571 case RPC_FC_PSTRUCT:
572 has_pointer = 1;
573 break;
574
575 default:
576 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
577 /* fallthru - treat it as complex */
578
579 /* as soon as we see one of these these members, it's bogus... */
580 case RPC_FC_BOGUS_STRUCT:
581 return RPC_FC_BOGUS_STRUCT;
582 }
583 break;
584 }
585 case TGT_RANGE:
586 return RPC_FC_BOGUS_STRUCT;
587 case TGT_STRING:
588 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
589 case TGT_INVALID:
590 case TGT_CTXT_HANDLE:
591 case TGT_CTXT_HANDLE_POINTER:
592 /* checking after parsing should mean that we don't get here. if we do,
593 * it's a checker bug */
594 assert(0);
595 }
596 }
597
598 if( has_variance )
599 {
600 if ( has_conformance )
601 return RPC_FC_CVSTRUCT;
602 else
603 return RPC_FC_BOGUS_STRUCT;
604 }
605 if( has_conformance && has_pointer )
606 return RPC_FC_CPSTRUCT;
607 if( has_conformance )
608 return RPC_FC_CSTRUCT;
609 if( has_pointer )
610 return RPC_FC_PSTRUCT;
611 return RPC_FC_STRUCT;
612 }
613
614 static unsigned char get_array_fc(const type_t *type)
615 {
616 unsigned char fc;
617 const expr_t *size_is;
618 const type_t *elem_type;
619
620 elem_type = type_array_get_element(type);
621 size_is = type_array_get_conformance(type);
622
623 if (!size_is)
624 {
625 unsigned int size = type_memsize(elem_type);
626 if (size * type_array_get_dim(type) > 0xffffuL)
627 fc = RPC_FC_LGFARRAY;
628 else
629 fc = RPC_FC_SMFARRAY;
630 }
631 else
632 fc = RPC_FC_CARRAY;
633
634 if (type_array_has_variance(type))
635 {
636 if (fc == RPC_FC_SMFARRAY)
637 fc = RPC_FC_SMVARRAY;
638 else if (fc == RPC_FC_LGFARRAY)
639 fc = RPC_FC_LGVARRAY;
640 else if (fc == RPC_FC_CARRAY)
641 fc = RPC_FC_CVARRAY;
642 }
643
644 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
645 {
646 case TGT_USER_TYPE:
647 fc = RPC_FC_BOGUS_ARRAY;
648 break;
649 case TGT_BASIC:
650 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
651 pointer_size != 4)
652 fc = RPC_FC_BOGUS_ARRAY;
653 break;
654 case TGT_STRUCT:
655 switch (get_struct_fc(elem_type))
656 {
657 case RPC_FC_BOGUS_STRUCT:
658 fc = RPC_FC_BOGUS_ARRAY;
659 break;
660 }
661 break;
662 case TGT_ENUM:
663 /* is 16-bit enum - if so, wire size differs from mem size and so
664 * the array cannot be block copied, which means the array is complex */
665 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
666 fc = RPC_FC_BOGUS_ARRAY;
667 break;
668 case TGT_UNION:
669 case TGT_IFACE_POINTER:
670 fc = RPC_FC_BOGUS_ARRAY;
671 break;
672 case TGT_POINTER:
673 /* ref pointers cannot just be block copied. unique pointers to
674 * interfaces need special treatment. either case means the array is
675 * complex */
676 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
677 fc = RPC_FC_BOGUS_ARRAY;
678 break;
679 case TGT_RANGE:
680 fc = RPC_FC_BOGUS_ARRAY;
681 break;
682 case TGT_CTXT_HANDLE:
683 case TGT_CTXT_HANDLE_POINTER:
684 case TGT_STRING:
685 case TGT_INVALID:
686 case TGT_ARRAY:
687 /* nothing to do for everything else */
688 break;
689 }
690
691 return fc;
692 }
693
694 static int is_non_complex_struct(const type_t *type)
695 {
696 return (type_get_type(type) == TYPE_STRUCT &&
697 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
698 }
699
700 static int type_has_pointers(const type_t *type)
701 {
702 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
703 {
704 case TGT_USER_TYPE:
705 return FALSE;
706 case TGT_POINTER:
707 return TRUE;
708 case TGT_ARRAY:
709 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
710 case TGT_STRUCT:
711 {
712 var_list_t *fields = type_struct_get_fields(type);
713 const var_t *field;
714 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
715 {
716 if (type_has_pointers(field->type))
717 return TRUE;
718 }
719 break;
720 }
721 case TGT_UNION:
722 {
723 var_list_t *fields;
724 const var_t *field;
725 fields = type_union_get_cases(type);
726 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
727 {
728 if (field->type && type_has_pointers(field->type))
729 return TRUE;
730 }
731 break;
732 }
733 case TGT_CTXT_HANDLE:
734 case TGT_CTXT_HANDLE_POINTER:
735 case TGT_STRING:
736 case TGT_IFACE_POINTER:
737 case TGT_BASIC:
738 case TGT_ENUM:
739 case TGT_RANGE:
740 case TGT_INVALID:
741 break;
742 }
743
744 return FALSE;
745 }
746
747 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
748 int toplevel_param)
749 {
750 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
751 {
752 case TGT_USER_TYPE:
753 return FALSE;
754 case TGT_POINTER:
755 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
756 return TRUE;
757 else
758 return FALSE;
759 case TGT_ARRAY:
760 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
761 return TRUE;
762 else
763 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
764 case TGT_STRUCT:
765 {
766 var_list_t *fields = type_struct_get_fields(type);
767 const var_t *field;
768 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
769 {
770 if (type_has_full_pointer(field->type, field->attrs, FALSE))
771 return TRUE;
772 }
773 break;
774 }
775 case TGT_UNION:
776 {
777 var_list_t *fields;
778 const var_t *field;
779 fields = type_union_get_cases(type);
780 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
781 {
782 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
783 return TRUE;
784 }
785 break;
786 }
787 case TGT_CTXT_HANDLE:
788 case TGT_CTXT_HANDLE_POINTER:
789 case TGT_STRING:
790 case TGT_IFACE_POINTER:
791 case TGT_BASIC:
792 case TGT_ENUM:
793 case TGT_RANGE:
794 case TGT_INVALID:
795 break;
796 }
797
798 return FALSE;
799 }
800
801 static unsigned short user_type_offset(const char *name)
802 {
803 user_type_t *ut;
804 unsigned short off = 0;
805 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
806 {
807 if (strcmp(name, ut->name) == 0)
808 return off;
809 ++off;
810 }
811 error("user_type_offset: couldn't find type (%s)\n", name);
812 return 0;
813 }
814
815 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
816 {
817 type->typestring_offset = offset;
818 if (file) type->tfswrite = FALSE;
819 }
820
821 static void guard_rec(type_t *type)
822 {
823 /* types that contain references to themselves (like a linked list),
824 need to be shielded from infinite recursion when writing embedded
825 types */
826 if (type->typestring_offset)
827 type->tfswrite = FALSE;
828 else
829 type->typestring_offset = 1;
830 }
831
832 static int is_embedded_complex(const type_t *type)
833 {
834 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
835 {
836 case TGT_USER_TYPE:
837 case TGT_STRUCT:
838 case TGT_UNION:
839 case TGT_ARRAY:
840 case TGT_IFACE_POINTER:
841 return TRUE;
842 default:
843 return FALSE;
844 }
845 }
846
847 static const char *get_context_handle_type_name(const type_t *type)
848 {
849 const type_t *t;
850 for (t = type;
851 is_ptr(t) || type_is_alias(t);
852 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
853 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
854 return t->name;
855 assert(0);
856 return NULL;
857 }
858
859 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
860 do { \
861 if (file) \
862 fprintf(file, "/* %2u */\n", typestring_offset); \
863 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
864 } \
865 while (0)
866
867 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
868 static void print_file(FILE *file, int indent, const char *format, ...)
869 {
870 va_list va;
871 va_start(va, format);
872 print(file, indent, format, va);
873 va_end(va);
874 }
875
876 void print(FILE *file, int indent, const char *format, va_list va)
877 {
878 if (file)
879 {
880 if (format[0] != '\n')
881 while (0 < indent--)
882 fprintf(file, " ");
883 vfprintf(file, format, va);
884 }
885 }
886
887
888 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
889 {
890 if (decl_indirect(t))
891 {
892 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
893 local_var_prefix, n, local_var_prefix, n);
894 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
895 }
896 else if (is_ptr(t) || is_array(t))
897 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
898 }
899
900 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
901 {
902 const var_t *var = type_function_get_retval(func->type);
903
904 if (!is_void(var->type))
905 write_var_init(file, indent, var->type, var->name, local_var_prefix);
906
907 if (!type_get_function_args(func->type))
908 return;
909
910 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
911 write_var_init(file, indent, var->type, var->name, local_var_prefix);
912
913 fprintf(file, "\n");
914 }
915
916 static void write_formatdesc(FILE *f, int indent, const char *str)
917 {
918 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
919 print_file(f, indent, "{\n");
920 print_file(f, indent + 1, "short Pad;\n");
921 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
922 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
923 print_file(f, indent, "\n");
924 }
925
926 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
927 {
928 clear_all_offsets();
929
930 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
931 get_size_typeformatstring(stmts, pred));
932
933 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
934 get_size_procformatstring(stmts, pred));
935
936 fprintf(f, "\n");
937 write_formatdesc(f, indent, "TYPE");
938 write_formatdesc(f, indent, "PROC");
939 fprintf(f, "\n");
940 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
941 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
942 print_file(f, indent, "\n");
943 }
944
945 int decl_indirect(const type_t *t)
946 {
947 if (is_user_type(t))
948 return TRUE;
949 return (type_get_type(t) != TYPE_BASIC &&
950 type_get_type(t) != TYPE_ENUM &&
951 type_get_type(t) != TYPE_POINTER &&
952 type_get_type(t) != TYPE_ARRAY);
953 }
954
955 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
956 unsigned int *stack_size, unsigned int *typestring_offset )
957 {
958 unsigned int alignment, server_size = 0, buffer_size = 0;
959 unsigned char fc = 0;
960 int is_byval;
961 int is_in = is_attr(var->attrs, ATTR_IN);
962 int is_out = is_attr(var->attrs, ATTR_OUT);
963
964 if (is_return) is_out = TRUE;
965 else if (!is_in && !is_out) is_in = TRUE;
966
967 *flags = 0;
968 *stack_size = get_stack_size( var, &is_byval );
969 *typestring_offset = var->typestring_offset;
970
971 if (is_in) *flags |= IsIn;
972 if (is_out) *flags |= IsOut;
973 if (is_return) *flags |= IsReturn;
974
975 if (!is_string_type( var->attrs, var->type ))
976 buffer_size = get_required_buffer_size_type( var->type, NULL, var->attrs, TRUE, &alignment );
977
978 switch (typegen_detect_type( var->type, var->attrs, TDT_ALL_TYPES ))
979 {
980 case TGT_BASIC:
981 *flags |= IsBasetype;
982 fc = get_basic_fc_signed( var->type );
983 if (fc == RPC_FC_BIND_PRIMITIVE)
984 {
985 buffer_size = 4; /* actually 0 but avoids setting MustSize */
986 fc = RPC_FC_LONG;
987 }
988 break;
989 case TGT_ENUM:
990 *flags |= IsBasetype;
991 fc = get_enum_fc( var->type );
992 break;
993 case TGT_RANGE:
994 *flags |= IsByValue;
995 break;
996 case TGT_STRUCT:
997 case TGT_UNION:
998 case TGT_USER_TYPE:
999 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1000 break;
1001 case TGT_IFACE_POINTER:
1002 *flags |= MustFree;
1003 break;
1004 case TGT_ARRAY:
1005 *flags |= MustFree;
1006 if (type_array_is_decl_as_ptr(var->type) && var->type->details.array.ptr_tfsoff &&
1007 get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1008 {
1009 *typestring_offset = var->type->typestring_offset;
1010 *flags |= IsSimpleRef;
1011 }
1012 break;
1013 case TGT_STRING:
1014 *flags |= MustFree;
1015 if (is_declptr( var->type ) && get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1016 {
1017 /* skip over pointer description straight to string description */
1018 if (is_conformant_array( var->type )) *typestring_offset += 4;
1019 else *typestring_offset += 2;
1020 *flags |= IsSimpleRef;
1021 }
1022 break;
1023 case TGT_CTXT_HANDLE_POINTER:
1024 *flags |= IsSimpleRef;
1025 *typestring_offset += 4;
1026 /* fall through */
1027 case TGT_CTXT_HANDLE:
1028 buffer_size = 20;
1029 break;
1030 case TGT_POINTER:
1031 if (get_pointer_fc( var->type, var->attrs, !is_return ) == RPC_FC_RP)
1032 {
1033 const type_t *ref = type_pointer_get_ref( var->type );
1034
1035 if (!is_string_type( var->attrs, ref ))
1036 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1037
1038 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1039 {
1040 case TGT_BASIC:
1041 *flags |= IsSimpleRef | IsBasetype;
1042 fc = get_basic_fc( ref );
1043 if (!is_in && is_out) server_size = pointer_size;
1044 break;
1045 case TGT_ENUM:
1046 if ((fc = get_enum_fc( ref )) == RPC_FC_ENUM32)
1047 {
1048 *flags |= IsSimpleRef | IsBasetype;
1049 if (!is_in && is_out) server_size = pointer_size;
1050 }
1051 else
1052 {
1053 server_size = pointer_size;
1054 }
1055 break;
1056 case TGT_UNION:
1057 case TGT_USER_TYPE:
1058 case TGT_RANGE:
1059 *flags |= IsSimpleRef | MustFree;
1060 *typestring_offset = ref->typestring_offset;
1061 if (!is_in && is_out) server_size = type_memsize( ref );
1062 break;
1063 case TGT_STRING:
1064 case TGT_POINTER:
1065 case TGT_ARRAY:
1066 case TGT_CTXT_HANDLE:
1067 case TGT_CTXT_HANDLE_POINTER:
1068 *flags |= MustFree;
1069 server_size = pointer_size;
1070 break;
1071 case TGT_IFACE_POINTER:
1072 *flags |= MustFree;
1073 if (is_in && is_out) server_size = pointer_size;
1074 break;
1075 case TGT_STRUCT:
1076 *flags |= IsSimpleRef | MustFree;
1077 *typestring_offset = ref->typestring_offset;
1078 switch (get_struct_fc(ref))
1079 {
1080 case RPC_FC_STRUCT:
1081 case RPC_FC_PSTRUCT:
1082 case RPC_FC_BOGUS_STRUCT:
1083 if (!is_in && is_out) server_size = type_memsize( ref );
1084 break;
1085 default:
1086 break;
1087 }
1088 break;
1089 case TGT_INVALID:
1090 assert(0);
1091 }
1092 }
1093 else /* not ref pointer */
1094 {
1095 *flags |= MustFree;
1096 }
1097 break;
1098 case TGT_INVALID:
1099 assert(0);
1100 }
1101
1102 if (!buffer_size) *flags |= MustSize;
1103
1104 if (server_size)
1105 {
1106 server_size = (server_size + 7) / 8;
1107 if (server_size < 8) *flags |= server_size << 13;
1108 }
1109 return fc;
1110 }
1111
1112 static unsigned char get_func_oi2_flags( const var_t *func )
1113 {
1114 const var_t *var;
1115 var_list_t *args = type_get_function_args( func->type );
1116 var_t *retval = type_function_get_retval( func->type );
1117 unsigned char oi2_flags = 0x40; /* HasExtensions */
1118 unsigned short flags;
1119 unsigned int stack_size, typestring_offset;
1120
1121 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1122 {
1123 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1124 if (flags & MustSize)
1125 {
1126 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1127 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1128 }
1129 }
1130
1131 if (!is_void( retval->type ))
1132 {
1133 oi2_flags |= 0x04; /* HasRet */
1134 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1135 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1136 }
1137 return oi2_flags;
1138 }
1139
1140 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1141 int is_return, unsigned int *stack_offset)
1142 {
1143 char buffer[64];
1144 unsigned int stack_size, typestring_offset;
1145 unsigned short flags;
1146 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1147
1148 strcpy( buffer, "/* flags:" );
1149 if (flags & MustSize) strcat( buffer, " must size," );
1150 if (flags & MustFree) strcat( buffer, " must free," );
1151 if (flags & IsPipe) strcat( buffer, " pipe," );
1152 if (flags & IsIn) strcat( buffer, " in," );
1153 if (flags & IsOut) strcat( buffer, " out," );
1154 if (flags & IsReturn) strcat( buffer, " return," );
1155 if (flags & IsBasetype) strcat( buffer, " base type," );
1156 if (flags & IsByValue) strcat( buffer, " by value," );
1157 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1158 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1159 strcpy( buffer + strlen( buffer ) - 1, " */" );
1160 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1161 print_file( file, indent, "NdrFcShort(0x%hx), /* stack offset = %hu */\n",
1162 *stack_offset, *stack_offset );
1163 if (flags & IsBasetype)
1164 {
1165 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1166 print_file( file, indent, "0x0,\n" );
1167 }
1168 else
1169 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1170 typestring_offset, typestring_offset );
1171 *stack_offset += max( stack_size, pointer_size );
1172 return 6;
1173 }
1174
1175 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1176 int is_return, int is_interpreted)
1177 {
1178 unsigned int size;
1179
1180 int is_in = is_attr(var->attrs, ATTR_IN);
1181 int is_out = is_attr(var->attrs, ATTR_OUT);
1182
1183 if (!is_in && !is_out) is_in = TRUE;
1184
1185 if (type_get_type(var->type) == TYPE_BASIC ||
1186 type_get_type(var->type) == TYPE_ENUM)
1187 {
1188 unsigned char fc;
1189
1190 if (is_return)
1191 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1192 else
1193 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1194
1195 if (type_get_type(var->type) == TYPE_ENUM)
1196 {
1197 fc = get_enum_fc(var->type);
1198 }
1199 else
1200 {
1201 fc = get_basic_fc_signed(var->type);
1202
1203 if (fc == RPC_FC_BIND_PRIMITIVE)
1204 fc = RPC_FC_IGNORE;
1205 }
1206
1207 print_file(file, indent, "0x%02x, /* %s */\n",
1208 fc, string_of_type(fc));
1209 size = 2; /* includes param type prefix */
1210 }
1211 else
1212 {
1213 unsigned short offset = var->typestring_offset;
1214
1215 if (!is_interpreted && is_array(var->type) &&
1216 type_array_is_decl_as_ptr(var->type) &&
1217 var->type->details.array.ptr_tfsoff)
1218 offset = var->type->typestring_offset;
1219
1220 if (is_return)
1221 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1222 else if (is_in && is_out)
1223 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1224 else if (is_out)
1225 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1226 else
1227 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1228
1229 size = get_stack_size( var, NULL );
1230 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1231 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1232 size = 4; /* includes param type prefix */
1233 }
1234 return size;
1235 }
1236
1237 int is_interpreted_func( const type_t *iface, const var_t *func )
1238 {
1239 const char *str;
1240 const var_t *var;
1241 const var_list_t *args = type_get_function_args( func->type );
1242 const type_t *ret_type = type_function_get_rettype( func->type );
1243
1244 if (type_get_type( ret_type ) == TYPE_BASIC)
1245 {
1246 switch (type_basic_get_type( ret_type ))
1247 {
1248 case TYPE_BASIC_INT64:
1249 case TYPE_BASIC_HYPER:
1250 /* return value must fit in a long_ptr */
1251 if (pointer_size < 8) return 0;
1252 break;
1253 case TYPE_BASIC_FLOAT:
1254 case TYPE_BASIC_DOUBLE:
1255 /* floating point values can't be returned */
1256 return 0;
1257 default:
1258 break;
1259 }
1260 }
1261 if (get_stub_mode() != MODE_Oif && args)
1262 {
1263 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1264 switch (type_get_type( var->type ))
1265 {
1266 case TYPE_BASIC:
1267 switch (type_basic_get_type( var->type ))
1268 {
1269 /* floating point arguments are not supported in Oi mode */
1270 case TYPE_BASIC_FLOAT: return 0;
1271 case TYPE_BASIC_DOUBLE: return 0;
1272 default: break;
1273 }
1274 break;
1275 /* unions passed by value are not supported in Oi mode */
1276 case TYPE_UNION: return 0;
1277 case TYPE_ENCAPSULATED_UNION: return 0;
1278 default: break;
1279 }
1280 }
1281
1282 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1283 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1284 return (get_stub_mode() != MODE_Os);
1285 }
1286
1287 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1288 const var_t *func, unsigned int *offset,
1289 unsigned short num_proc )
1290 {
1291 var_t *var;
1292 var_list_t *args = type_get_function_args( func->type );
1293 unsigned char explicit_fc, implicit_fc;
1294 unsigned char handle_flags;
1295 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1296 unsigned char oi_flags = RPC_FC_PROC_OIF_RPCFLAGS | RPC_FC_PROC_OIF_NEWINIT;
1297 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1298 unsigned int nb_args = 0;
1299 unsigned int stack_size = 0;
1300 unsigned short param_num = 0;
1301 unsigned short handle_stack_offset = 0;
1302 unsigned short handle_param_num = 0;
1303
1304 if (is_full_pointer_function( func )) oi_flags |= RPC_FC_PROC_OIF_FULLPTR;
1305 if (is_object( iface ))
1306 {
1307 oi_flags |= RPC_FC_PROC_OIF_OBJECT;
1308 if (get_stub_mode() == MODE_Oif) oi_flags |= RPC_FC_PROC_OIF_OBJ_V2;
1309 stack_size += pointer_size;
1310 }
1311
1312 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1313 {
1314 if (var == handle_var)
1315 {
1316 handle_stack_offset = stack_size;
1317 handle_param_num = param_num;
1318 }
1319 stack_size += get_stack_size( var, NULL );
1320 param_num++;
1321 nb_args++;
1322 }
1323 if (!is_void( type_function_get_rettype( func->type )))
1324 {
1325 stack_size += pointer_size;
1326 nb_args++;
1327 }
1328
1329 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1330 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1331 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1332 print_file( file, indent, "0x%02x,\n", oi_flags );
1333 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1334 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1335 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack size = %hu */\n", stack_size, stack_size );
1336 *offset += 10;
1337
1338 if (!implicit_fc)
1339 {
1340 switch (explicit_fc)
1341 {
1342 case RPC_FC_BIND_PRIMITIVE:
1343 handle_flags = 0;
1344 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1345 print_file( file, indent, "0x%02x,\n", handle_flags );
1346 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1347 handle_stack_offset, handle_stack_offset );
1348 *offset += 4;
1349 break;
1350 case RPC_FC_BIND_GENERIC:
1351 handle_flags = type_memsize( handle_var->type );
1352 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1353 print_file( file, indent, "0x%02x,\n", handle_flags );
1354 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1355 handle_stack_offset, handle_stack_offset );
1356 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1357 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD);
1358 *offset += 6;
1359 break;
1360 case RPC_FC_BIND_CONTEXT:
1361 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1362 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1363 print_file( file, indent, "0x%02x,\n", handle_flags );
1364 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1365 handle_stack_offset, handle_stack_offset );
1366 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1367 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1368 *offset += 6;
1369 break;
1370 }
1371 }
1372
1373 if (get_stub_mode() == MODE_Oif)
1374 {
1375 unsigned char oi2_flags = get_func_oi2_flags( func );
1376 unsigned char ext_flags = 0;
1377 unsigned int size;
1378
1379 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1380 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1381
1382 size = get_function_buffer_size( func, PASS_IN );
1383 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %hu */\n", size, size );
1384 size = get_function_buffer_size( func, PASS_OUT );
1385 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %hu */\n", size, size );
1386 print_file( file, indent, "0x%02x,\n", oi2_flags );
1387 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1388 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1389 print_file( file, indent, "0x%02x,\n", ext_flags );
1390 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1391 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1392 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1393 *offset += 14;
1394 if (pointer_size == 8)
1395 {
1396 unsigned short pos = 0, fpu_mask = 0;
1397
1398 if (is_object( iface )) pos += 2;
1399 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1400 {
1401 if (type_get_type( var->type ) == TYPE_BASIC)
1402 {
1403 switch (type_basic_get_type( var->type ))
1404 {
1405 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1406 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1407 default: break;
1408 }
1409 }
1410 pos += 2;
1411 if (pos >= 16) break;
1412 }
1413 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1414 *offset += 2;
1415 }
1416 }
1417 }
1418
1419 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1420 const var_t *func, unsigned int *offset,
1421 unsigned short num_proc )
1422 {
1423 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1424 int is_interpreted = is_interpreted_func( iface, func );
1425 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1426 var_t *retval = type_function_get_retval( func->type );
1427
1428 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1429
1430 /* emit argument data */
1431 if (type_get_function_args(func->type))
1432 {
1433 const var_t *var;
1434 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1435 {
1436 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1437 if (is_new_style)
1438 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1439 else
1440 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1441 }
1442 }
1443
1444 /* emit return value data */
1445 if (is_void(retval->type))
1446 {
1447 if (!is_new_style)
1448 {
1449 print_file(file, 0, "/* %u (void) */\n", *offset);
1450 print_file(file, indent, "0x5b, /* FC_END */\n");
1451 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1452 *offset += 2;
1453 }
1454 }
1455 else
1456 {
1457 print_file( file, 0, "/* %u (return value) */\n", *offset );
1458 if (is_new_style)
1459 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1460 else
1461 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1462 }
1463 }
1464
1465 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1466 type_pred_t pred, unsigned int *offset)
1467 {
1468 const statement_t *stmt;
1469 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1470 {
1471 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1472 {
1473 const statement_t *stmt_func;
1474 const type_t *iface = stmt->u.type;
1475 const type_t *parent = type_iface_get_inherit( iface );
1476 int count = parent ? count_methods( parent ) : 0;
1477
1478 if (!pred(iface)) continue;
1479 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1480 {
1481 var_t *func = stmt_func->u.var;
1482 if (is_local(func->attrs)) continue;
1483 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1484 }
1485 }
1486 }
1487 }
1488
1489 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1490 {
1491 int indent = 0;
1492 unsigned int offset = 0;
1493
1494 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1495 print_file(file, indent, "{\n");
1496 indent++;
1497 print_file(file, indent, "0,\n");
1498 print_file(file, indent, "{\n");
1499 indent++;
1500
1501 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1502
1503 print_file(file, indent, "0x0\n");
1504 indent--;
1505 print_file(file, indent, "}\n");
1506 indent--;
1507 print_file(file, indent, "};\n");
1508 print_file(file, indent, "\n");
1509 }
1510
1511 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1512 {
1513 const statement_t *stmt;
1514 int indent = 0;
1515
1516 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1517 iface->name );
1518 print_file( file, indent, "{\n" );
1519 indent++;
1520 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1521 {
1522 var_t *func = stmt->u.var;
1523 if (is_local( func->attrs )) continue;
1524 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1525 }
1526 indent--;
1527 print_file( file, indent, "};\n\n" );
1528 }
1529
1530 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1531 {
1532 unsigned char fc;
1533
1534 if (type_get_type(type) == TYPE_BASIC)
1535 fc = get_basic_fc_signed(type);
1536 else if (type_get_type(type) == TYPE_ENUM)
1537 fc = get_enum_fc(type);
1538 else
1539 return 0;
1540
1541 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1542 *typestring_offset += 1;
1543 return 1;
1544 }
1545
1546 /* write conformance / variance descriptor */
1547 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1548 unsigned int baseoff, const type_t *type,
1549 const expr_t *expr)
1550 {
1551 unsigned char operator_type = 0;
1552 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1553 const char *conftype_string = "field";
1554 const expr_t *subexpr;
1555 const type_t *iface = NULL;
1556 const char *name;
1557
1558 if (!expr)
1559 {
1560 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1561 return 4;
1562 }
1563
1564 if (expr->is_const)
1565 {
1566 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1567 error("write_conf_or_var_desc: constant value %d is greater than "
1568 "the maximum constant size of %d\n", expr->cval,
1569 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1570
1571 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1572 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1573 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1574 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1575
1576 return 4;
1577 }
1578
1579 if (!cont_type) /* top-level conformance */
1580 {
1581 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1582 conftype_string = "parameter";
1583 cont_type = current_func->type;
1584 name = current_func->name;
1585 iface = current_iface;
1586 }
1587 else
1588 {
1589 name = cont_type->name;
1590 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1591 {
1592 conftype = RPC_FC_POINTER_CONFORMANCE;
1593 conftype_string = "field pointer";
1594 }
1595 }
1596
1597 subexpr = expr;
1598 switch (subexpr->type)
1599 {
1600 case EXPR_PPTR:
1601 subexpr = subexpr->ref;
1602 operator_type = RPC_FC_DEREFERENCE;
1603 break;
1604 case EXPR_DIV:
1605 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1606 {
1607 subexpr = subexpr->ref;
1608 operator_type = RPC_FC_DIV_2;
1609 }
1610 break;
1611 case EXPR_MUL:
1612 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1613 {
1614 subexpr = subexpr->ref;
1615 operator_type = RPC_FC_MULT_2;
1616 }
1617 break;
1618 case EXPR_SUB:
1619 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1620 {
1621 subexpr = subexpr->ref;
1622 operator_type = RPC_FC_SUB_1;
1623 }
1624 break;
1625 case EXPR_ADD:
1626 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1627 {
1628 subexpr = subexpr->ref;
1629 operator_type = RPC_FC_ADD_1;
1630 }
1631 break;
1632 default:
1633 break;
1634 }
1635
1636 if (subexpr->type == EXPR_IDENTIFIER)
1637 {
1638 const type_t *correlation_variable = NULL;
1639 unsigned char param_type = 0;
1640 unsigned int offset = 0;
1641 const var_t *var;
1642 struct expr_loc expr_loc;
1643
1644 if (type_get_type(cont_type) == TYPE_FUNCTION)
1645 {
1646 var_list_t *args = type_get_function_args( cont_type );
1647
1648 if (is_object( iface )) offset += pointer_size;
1649 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1650 {
1651 if (var->name && !strcmp(var->name, subexpr->u.sval))
1652 {
1653 expr_loc.v = var;
1654 correlation_variable = var->type;
1655 break;
1656 }
1657 offset += get_stack_size( var, NULL );
1658 }
1659 }
1660 else
1661 {
1662 var_list_t *fields = type_struct_get_fields( cont_type );
1663
1664 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1665 {
1666 unsigned int size = field_memsize( var->type, &offset );
1667 if (var->name && !strcmp(var->name, subexpr->u.sval))
1668 {
1669 expr_loc.v = var;
1670 correlation_variable = var->type;
1671 break;
1672 }
1673 offset += size;
1674 }
1675 }
1676
1677 if (!correlation_variable)
1678 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1679 expr_loc.attr = NULL;
1680 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1681
1682 offset -= baseoff;
1683
1684 if (type_get_type(correlation_variable) == TYPE_BASIC)
1685 {
1686 switch (get_basic_fc(correlation_variable))
1687 {
1688 case RPC_FC_CHAR:
1689 case RPC_FC_SMALL:
1690 param_type = RPC_FC_SMALL;
1691 break;
1692 case RPC_FC_BYTE:
1693 case RPC_FC_USMALL:
1694 param_type = RPC_FC_USMALL;
1695 break;
1696 case RPC_FC_WCHAR:
1697 case RPC_FC_SHORT:
1698 param_type = RPC_FC_SHORT;
1699 break;
1700 case RPC_FC_USHORT:
1701 param_type = RPC_FC_USHORT;
1702 break;
1703 case RPC_FC_LONG:
1704 param_type = RPC_FC_LONG;
1705 break;
1706 case RPC_FC_ULONG:
1707 param_type = RPC_FC_ULONG;
1708 break;
1709 default:
1710 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1711 get_basic_fc(correlation_variable));
1712 }
1713 }
1714 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1715 {
1716 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1717 param_type = RPC_FC_LONG;
1718 else
1719 param_type = RPC_FC_SHORT;
1720 }
1721 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1722 {
1723 if (pointer_size == 8)
1724 param_type = RPC_FC_HYPER;
1725 else
1726 param_type = RPC_FC_LONG;
1727 }
1728 else
1729 {
1730 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1731 subexpr->u.sval);
1732 return 0;
1733 }
1734
1735 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1736 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1737 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1738 operator_type ? string_of_type(operator_type) : "no operators");
1739 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1740 (unsigned short)offset, offset);
1741 }
1742 else if (!iface || is_interpreted_func( iface, current_func ))
1743 {
1744 unsigned int callback_offset = 0;
1745 struct expr_eval_routine *eval;
1746 int found = 0;
1747
1748 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1749 {
1750 if (eval->cont_type == cont_type ||
1751 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1752 eval->iface == iface &&
1753 eval->name && name && !strcmp(eval->name, name) &&
1754 !compare_expr(eval->expr, expr)))
1755 {
1756 found = 1;
1757 break;
1758 }
1759 callback_offset++;
1760 }
1761
1762 if (!found)
1763 {
1764 eval = xmalloc (sizeof(*eval));
1765 eval->iface = iface;
1766 eval->cont_type = cont_type;
1767 eval->name = xstrdup( name );
1768 eval->baseoff = baseoff;
1769 eval->expr = expr;
1770 list_add_tail (&expr_eval_routines, &eval->entry);
1771 }
1772
1773 if (callback_offset > USHRT_MAX)
1774 error("Maximum number of callback routines reached\n");
1775
1776 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1777 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1778 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1779 }
1780 else /* output a dummy corr desc that isn't used */
1781 {
1782 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1783 print_file(file, 2, "0x0,\n" );
1784 print_file(file, 2, "NdrFcShort(0x0),\n" );
1785 }
1786 return 4;
1787 }
1788
1789 /* return size and start offset of a data field based on current offset */
1790 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1791 {
1792 unsigned int align = 0;
1793 unsigned int size = type_memsize_and_alignment( type, &align );
1794
1795 *offset = ROUND_SIZE( *offset, align );
1796 return size;
1797 }
1798
1799 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1800 {
1801 unsigned int size = 0;
1802 unsigned int max_align;
1803 const var_t *v;
1804
1805 if (!fields) return 0;
1806 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1807 {
1808 unsigned int falign = 0;
1809 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1810 if (*align < falign) *align = falign;
1811 falign = clamp_align(falign);
1812 size = ROUND_SIZE(size, falign);
1813 size += fsize;
1814 }
1815
1816 max_align = clamp_align(*align);
1817 size = ROUND_SIZE(size, max_align);
1818
1819 return size;
1820 }
1821
1822 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1823 {
1824 unsigned int size, maxs = 0;
1825 unsigned int align = *pmaxa;
1826 const var_t *v;
1827
1828 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1829 {
1830 /* we could have an empty default field with NULL type */
1831 if (v->type)
1832 {
1833 size = type_memsize_and_alignment(v->type, &align);
1834 if (maxs < size) maxs = size;
1835 if (*pmaxa < align) *pmaxa = align;
1836 }
1837 }
1838
1839 return maxs;
1840 }
1841
1842 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1843 {
1844 unsigned int size = 0;
1845
1846 switch (type_get_type(t))
1847 {
1848 case TYPE_BASIC:
1849 switch (get_basic_fc(t))
1850 {
1851 case RPC_FC_BYTE:
1852 case RPC_FC_CHAR:
1853 case RPC_FC_USMALL:
1854 case RPC_FC_SMALL:
1855 size = 1;
1856 if (size > *align) *align = size;
1857 break;
1858 case RPC_FC_WCHAR:
1859 case RPC_FC_USHORT:
1860 case RPC_FC_SHORT:
1861 size = 2;
1862 if (size > *align) *align = size;
1863 break;
1864 case RPC_FC_ULONG:
1865 case RPC_FC_LONG:
1866 case RPC_FC_ERROR_STATUS_T:
1867 case RPC_FC_FLOAT:
1868 size = 4;
1869 if (size > *align) *align = size;
1870 break;
1871 case RPC_FC_HYPER:
1872 case RPC_FC_DOUBLE:
1873 size = 8;
1874 if (size > *align) *align = size;
1875 break;
1876 case RPC_FC_INT3264:
1877 case RPC_FC_UINT3264:
1878 case RPC_FC_BIND_PRIMITIVE:
1879 assert( pointer_size );
1880 size = pointer_size;
1881 if (size > *align) *align = size;
1882 break;
1883 default:
1884 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1885 size = 0;
1886 }
1887 break;
1888 case TYPE_ENUM:
1889 switch (get_enum_fc(t))
1890 {
1891 case RPC_FC_ENUM16:
1892 case RPC_FC_ENUM32:
1893 size = 4;
1894 if (size > *align) *align = size;
1895 break;
1896 default:
1897 error("type_memsize: Unknown enum type\n");
1898 size = 0;
1899 }
1900 break;
1901 case TYPE_STRUCT:
1902 size = fields_memsize(type_struct_get_fields(t), align);
1903 break;
1904 case TYPE_ENCAPSULATED_UNION:
1905 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1906 break;
1907 case TYPE_UNION:
1908 size = union_memsize(type_union_get_cases(t), align);
1909 break;
1910 case TYPE_POINTER:
1911 assert( pointer_size );
1912 size = pointer_size;
1913 if (size > *align) *align = size;
1914 break;
1915 case TYPE_ARRAY:
1916 if (!type_array_is_decl_as_ptr(t))
1917 {
1918 if (is_conformant_array(t))
1919 {
1920 type_memsize_and_alignment(type_array_get_element(t), align);
1921 size = 0;
1922 }
1923 else
1924 size = type_array_get_dim(t) *
1925 type_memsize_and_alignment(type_array_get_element(t), align);
1926 }
1927 else /* declared as a pointer */
1928 {
1929 assert( pointer_size );
1930 size = pointer_size;
1931 if (size > *align) *align = size;
1932 }
1933 break;
1934 case TYPE_INTERFACE:
1935 case TYPE_ALIAS:
1936 case TYPE_VOID:
1937 case TYPE_COCLASS:
1938 case TYPE_MODULE:
1939 case TYPE_FUNCTION:
1940 case TYPE_BITFIELD:
1941 /* these types should not be encountered here due to language
1942 * restrictions (interface, void, coclass, module), logical
1943 * restrictions (alias - due to type_get_type call above) or
1944 * checking restrictions (function, bitfield). */
1945 assert(0);
1946 }
1947
1948 return size;
1949 }
1950
1951 unsigned int type_memsize(const type_t *t)
1952 {
1953 unsigned int align = 0;
1954 return type_memsize_and_alignment( t, &align );
1955 }
1956
1957 static unsigned int type_buffer_alignment(const type_t *t)
1958 {
1959 const var_list_t *fields;
1960 const var_t *var;
1961 unsigned int max = 0, align;
1962
1963 switch (type_get_type(t))
1964 {
1965 case TYPE_BASIC:
1966 switch (get_basic_fc(t))
1967 {
1968 case RPC_FC_BYTE:
1969 case RPC_FC_CHAR:
1970 case RPC_FC_USMALL:
1971 case RPC_FC_SMALL:
1972 return 1;
1973 case RPC_FC_WCHAR:
1974 case RPC_FC_USHORT:
1975 case RPC_FC_SHORT:
1976 return 2;
1977 case RPC_FC_ULONG:
1978 case RPC_FC_LONG:
1979 case RPC_FC_ERROR_STATUS_T:
1980 case RPC_FC_FLOAT:
1981 case RPC_FC_INT3264:
1982 case RPC_FC_UINT3264:
1983 return 4;
1984 case RPC_FC_HYPER:
1985 case RPC_FC_DOUBLE:
1986 return 8;
1987 default:
1988 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1989 }
1990 break;
1991 case TYPE_ENUM:
1992 switch (get_enum_fc(t))
1993 {
1994 case RPC_FC_ENUM16:
1995 return 2;
1996 case RPC_FC_ENUM32:
1997 return 4;
1998 default:
1999 error("type_buffer_alignment: Unknown enum type\n");
2000 }
2001 break;
2002 case TYPE_STRUCT:
2003 if (!(fields = type_struct_get_fields(t))) break;
2004 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2005 {
2006 if (!var->type) continue;
2007 align = type_buffer_alignment( var->type );
2008 if (max < align) max = align;
2009 }
2010 break;
2011 case TYPE_ENCAPSULATED_UNION:
2012 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2013 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2014 {
2015 if (!var->type) continue;
2016 align = type_buffer_alignment( var->type );
2017 if (max < align) max = align;
2018 }
2019 break;
2020 case TYPE_UNION:
2021 if (!(fields = type_union_get_cases(t))) break;
2022 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2023 {
2024 if (!var->type) continue;
2025 align = type_buffer_alignment( var->type );
2026 if (max < align) max = align;
2027 }
2028 break;
2029 case TYPE_ARRAY:
2030 if (!type_array_is_decl_as_ptr(t))
2031 return type_buffer_alignment( type_array_get_element(t) );
2032 /* else fall through */
2033 case TYPE_POINTER:
2034 return 4;
2035 case TYPE_INTERFACE:
2036 case TYPE_ALIAS:
2037 case TYPE_VOID:
2038 case TYPE_COCLASS:
2039 case TYPE_MODULE:
2040 case TYPE_FUNCTION:
2041 case TYPE_BITFIELD:
2042 /* these types should not be encountered here due to language
2043 * restrictions (interface, void, coclass, module), logical
2044 * restrictions (alias - due to type_get_type call above) or
2045 * checking restrictions (function, bitfield). */
2046 assert(0);
2047 }
2048 return max;
2049 }
2050
2051 int is_full_pointer_function(const var_t *func)
2052 {
2053 const var_t *var;
2054 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2055 return TRUE;
2056 if (!type_get_function_args(func->type))
2057 return FALSE;
2058 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2059 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2060 return TRUE;
2061 return FALSE;
2062 }
2063
2064 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2065 {
2066 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2067 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2068 fprintf(file, "\n");
2069 }
2070
2071 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2072 {
2073 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2074 fprintf(file, "\n");
2075 }
2076
2077 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2078 const type_t *type,
2079 enum type_context context,
2080 unsigned int offset,
2081 unsigned int *typeformat_offset)
2082 {
2083 unsigned int start_offset = *typeformat_offset;
2084 short reloff = offset - (*typeformat_offset + 2);
2085 int in_attr, out_attr;
2086 int pointer_type;
2087 unsigned char flags = 0;
2088
2089 pointer_type = get_pointer_fc_context(type, attrs, context);
2090
2091 in_attr = is_attr(attrs, ATTR_IN);
2092 out_attr = is_attr(attrs, ATTR_OUT);
2093 if (!in_attr && !out_attr) in_attr = 1;
2094
2095 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2096 flags |= RPC_FC_P_ONSTACK;
2097
2098 if (is_ptr(type))
2099 {
2100 type_t *ref = type_pointer_get_ref(type);
2101 if(is_declptr(ref) && !is_user_type(ref))
2102 flags |= RPC_FC_P_DEREF;
2103 }
2104
2105 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2106 pointer_type,
2107 flags,
2108 string_of_type(pointer_type));
2109 if (file)
2110 {
2111 if (flags & RPC_FC_P_ONSTACK)
2112 fprintf(file, " [allocated_on_stack]");
2113 if (flags & RPC_FC_P_DEREF)
2114 fprintf(file, " [pointer_deref]");
2115 fprintf(file, " */\n");
2116 }
2117
2118 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2119 *typeformat_offset += 4;
2120
2121 return start_offset;
2122 }
2123
2124 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2125 const type_t *type, enum type_context context)
2126 {
2127 unsigned char fc;
2128 unsigned char pointer_fc;
2129 const type_t *ref;
2130 int in_attr = is_attr(attrs, ATTR_IN);
2131 int out_attr = is_attr(attrs, ATTR_OUT);
2132 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2133
2134 /* for historical reasons, write_simple_pointer also handled string types,
2135 * but no longer does. catch bad uses of the function with this check */
2136 if (is_string_type(attrs, type))
2137 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2138
2139 pointer_fc = get_pointer_fc_context(type, attrs, context);
2140
2141 ref = type_pointer_get_ref(type);
2142 if (type_get_type(ref) == TYPE_ENUM)
2143 fc = get_enum_fc(ref);
2144 else
2145 fc = get_basic_fc(ref);
2146
2147 if (out_attr && !in_attr)
2148 flags |= RPC_FC_P_ONSTACK;
2149
2150 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2151 pointer_fc, flags, string_of_type(pointer_fc),
2152 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2153 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2154 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2155 return 4;
2156 }
2157
2158 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2159 {
2160 print_file(file, 0, "/* %u (", tfsoff);
2161 write_type_decl(file, t, NULL);
2162 print_file(file, 0, ") */\n");
2163 }
2164
2165 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2166 type_t *type, unsigned int ref_offset,
2167 enum type_context context,
2168 unsigned int *typestring_offset)
2169 {
2170 unsigned int offset = *typestring_offset;
2171 type_t *ref = type_pointer_get_ref(type);
2172
2173 print_start_tfs_comment(file, type, offset);
2174 update_tfsoff(type, offset, file);
2175
2176 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2177 {
2178 case TGT_BASIC:
2179 case TGT_ENUM:
2180 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2181 break;
2182 default:
2183 if (ref_offset)
2184 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2185 break;
2186 }
2187
2188 return offset;
2189 }
2190
2191 static int processed(const type_t *type)
2192 {
2193 return type->typestring_offset && !type->tfswrite;
2194 }
2195
2196 static int user_type_has_variable_size(const type_t *t)
2197 {
2198 if (is_ptr(t))
2199 return TRUE;
2200 else if (type_get_type(t) == TYPE_STRUCT)
2201 {
2202 switch (get_struct_fc(t))
2203 {
2204 case RPC_FC_PSTRUCT:
2205 case RPC_FC_CSTRUCT:
2206 case RPC_FC_CPSTRUCT:
2207 case RPC_FC_CVSTRUCT:
2208 return TRUE;
2209 }
2210 }
2211 /* Note: Since this only applies to user types, we can't have a conformant
2212 array here, and strings should get filed under pointer in this case. */
2213 return FALSE;
2214 }
2215
2216 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2217 {
2218 unsigned int start, absoff, flags;
2219 const char *name = NULL;
2220 type_t *utype = get_user_type(type, &name);
2221 unsigned int usize = type_memsize(utype);
2222 unsigned int ualign = type_buffer_alignment(utype);
2223 unsigned int size = type_memsize(type);
2224 unsigned short funoff = user_type_offset(name);
2225 short reloff;
2226
2227 if (processed(type)) return type->typestring_offset;
2228
2229 guard_rec(type);
2230
2231 if(user_type_has_variable_size(utype)) usize = 0;
2232
2233 if (type_get_type(utype) == TYPE_BASIC ||
2234 type_get_type(utype) == TYPE_ENUM)
2235 {
2236 unsigned char fc;
2237
2238 if (type_get_type(utype) == TYPE_ENUM)
2239 fc = get_enum_fc(utype);
2240 else
2241 fc = get_basic_fc(utype);
2242
2243 absoff = *tfsoff;
2244 print_start_tfs_comment(file, utype, absoff);
2245 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2246 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2247 *tfsoff += 2;
2248 }
2249 else
2250 {
2251 if (!processed(utype))
2252 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2253 absoff = utype->typestring_offset;
2254 }
2255
2256 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2257 flags = 0x40;
2258 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2259 flags = 0x80;
2260 else
2261 flags = 0;
2262
2263 start = *tfsoff;
2264 update_tfsoff(type, start, file);
2265 print_start_tfs_comment(file, type, start);
2266 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2267 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2268 flags | (ualign - 1), ualign - 1, flags);
2269 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2270 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2271 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2272 *tfsoff += 8;
2273 reloff = absoff - *tfsoff;
2274 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2275 *tfsoff += 2;
2276 return start;
2277 }
2278
2279 static void write_member_type(FILE *file, const type_t *cont,
2280 int cont_is_complex, const attr_list_t *attrs,
2281 const type_t *type, unsigned int *corroff,
2282 unsigned int *tfsoff)
2283 {
2284 if (is_embedded_complex(type) && !is_conformant_array(type))
2285 {
2286 unsigned int absoff;
2287 short reloff;
2288
2289 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2290 {
2291 absoff = *corroff;
2292 *corroff += 8;
2293 }
2294 else
2295 {
2296 absoff = type->typestring_offset;
2297 }
2298 reloff = absoff - (*tfsoff + 2);
2299
2300 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2301 /* padding is represented using FC_STRUCTPAD* types, so presumably
2302 * this is left over in the format for historical purposes in MIDL
2303 * or rpcrt4. */
2304 print_file(file, 2, "0x0,\n");
2305 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2306 reloff, reloff, absoff);
2307 *tfsoff += 4;
2308 }
2309 else if (is_ptr(type) || is_conformant_array(type))
2310 {
2311 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2312 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2313 *tfsoff += 1;
2314 }
2315 else if (!write_base_type(file, type, tfsoff))
2316 error("Unsupported member type %d\n", type_get_type(type));
2317 }
2318
2319 static void write_array_element_type(FILE *file, const type_t *type,
2320 int cont_is_complex, unsigned int *tfsoff)
2321 {
2322 type_t *elem = type_array_get_element(type);
2323
2324 if (!is_embedded_complex(elem) && is_ptr(elem))
2325 {
2326 type_t *ref = type_pointer_get_ref(elem);
2327
2328 if (processed(ref))
2329 {
2330 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2331 ref->typestring_offset, tfsoff);
2332 return;
2333 }
2334 if (cont_is_complex && is_string_type(NULL, elem))
2335 {
2336 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2337 return;
2338 }
2339 if (!is_string_type(NULL, elem) &&
2340 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2341 {
2342 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2343 return;
2344 }
2345 }
2346 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2347 }
2348
2349 static void write_end(FILE *file, unsigned int *tfsoff)
2350 {
2351 if (*tfsoff % 2 == 0)
2352 {
2353 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2354 *tfsoff += 1;
2355 }
2356 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2357 *tfsoff += 1;
2358 }
2359
2360 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2361 {
2362 unsigned int offset = 0;
2363 var_list_t *fs = type_struct_get_fields(type);
2364 var_t *f;
2365
2366 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2367 {
2368 type_t *ft = f->type;
2369 unsigned int size = field_memsize( ft, &offset );
2370 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2371 {
2372 short reloff;
2373 unsigned int absoff = ft->typestring_offset;
2374 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2375 absoff += 8; /* we already have a corr descr, skip it */
2376 reloff = absoff - (*tfsoff + 6);
2377 print_file(file, 0, "/* %d */\n", *tfsoff);
2378 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2379 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2380 write_conf_or_var_desc(file, current_structure, offset, ft,
2381 get_attrp(f->attrs, ATTR_SWITCHIS));
2382 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2383 (unsigned short)reloff, reloff, absoff);
2384 *tfsoff += 8;
2385 }
2386 offset += size;
2387 }
2388 }
2389
2390 static int write_pointer_description_offsets(
2391 FILE *file, const attr_list_t *attrs, type_t *type,
2392 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2393 unsigned int *typestring_offset)
2394 {
2395 int written = 0;
2396
2397 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2398 (is_array(type) && type_array_is_decl_as_ptr(type)))
2399 {
2400 if (offset_in_memory && offset_in_buffer)
2401 {
2402 unsigned int memsize;
2403
2404 /* pointer instance
2405 *
2406 * note that MSDN states that for pointer layouts in structures,
2407 * this is a negative offset from the end of the structure, but
2408 * this statement is incorrect. all offsets are positive */
2409 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2410 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2411
2412 memsize = type_memsize(type);
2413 *offset_in_memory += memsize;
2414 /* increment these separately as in the case of conformant (varying)
2415 * structures these start at different values */
2416 *offset_in_buffer += memsize;
2417 }
2418 *typestring_offset += 4;
2419
2420 if (is_ptr(type))
2421 {
2422 type_t *ref = type_pointer_get_ref(type);
2423
2424 if (is_string_type(attrs, type))
2425 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2426 else if (processed(ref))
2427 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2428 ref->typestring_offset, typestring_offset);
2429 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2430 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2431 else
2432 error("write_pointer_description_offsets: type format string unknown\n");
2433 }
2434 else
2435 {
2436 unsigned int offset = type->typestring_offset;
2437 /* skip over the pointer that is written for strings, since a
2438 * pointer has to be written in-place here */
2439 if (is_string_type(attrs, type))
2440 offset += 4;
2441 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2442 }
2443
2444 return 1;
2445 }
2446
2447 if (is_array(type))
2448 {
2449 return write_pointer_description_offsets(
2450 file, attrs, type_array_get_element(type), offset_in_memory,
2451 offset_in_buffer, typestring_offset);
2452 }
2453 else if (is_non_complex_struct(type))
2454 {
2455 /* otherwise search for interesting fields to parse */
2456 const var_t *v;
2457 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2458 {
2459 if (offset_in_memory && offset_in_buffer)
2460 {
2461 unsigned int padding;
2462 unsigned int align = 0;
2463 type_memsize_and_alignment(v->type, &align);
2464 padding = ROUNDING(*offset_in_memory, align);
2465 *offset_in_memory += padding;
2466 *offset_in_buffer += padding;
2467 }
2468 written += write_pointer_description_offsets(
2469 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2470 typestring_offset);
2471 }
2472 }
2473 else
2474 {
2475 if (offset_in_memory && offset_in_buffer)
2476 {
2477 unsigned int memsize = type_memsize(type);
2478 *offset_in_memory += memsize;
2479 /* increment these separately as in the case of conformant (varying)
2480 * structures these start at different values */
2481 *offset_in_buffer += memsize;
2482 }
2483 }
2484
2485 return written;
2486 }
2487
2488 static int write_no_repeat_pointer_descriptions(
2489 FILE *file, const attr_list_t *attrs, type_t *type,
2490 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2491 unsigned int *typestring_offset)
2492 {
2493 int written = 0;
2494
2495 if (is_ptr(type) ||
2496 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2497 {
2498 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2499 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2500 *typestring_offset += 2;
2501
2502 return write_pointer_description_offsets(file, attrs, type,
2503 offset_in_memory, offset_in_buffer, typestring_offset);
2504 }
2505
2506 if (is_non_complex_struct(type))
2507 {
2508 const var_t *v;
2509 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2510 {
2511 if (offset_in_memory && offset_in_buffer)
2512 {
2513 unsigned int padding;
2514 unsigned int align = 0;
2515 type_memsize_and_alignment(v->type, &align);
2516 padding = ROUNDING(*offset_in_memory, align);
2517 *offset_in_memory += padding;
2518 *offset_in_buffer += padding;
2519 }
2520 written += write_no_repeat_pointer_descriptions(
2521 file, v->attrs, v->type,
2522 offset_in_memory, offset_in_buffer, typestring_offset);
2523 }
2524 }
2525 else
2526 {
2527 unsigned int memsize = type_memsize(type);
2528 *offset_in_memory += memsize;
2529 /* increment these separately as in the case of conformant (varying)
2530 * structures these start at different values */
2531 *offset_in_buffer += memsize;
2532 }
2533
2534 return written;
2535 }
2536
2537 /* Note: if file is NULL return value is number of pointers to write, else
2538 * it is the number of type format characters written */
2539 static int write_fixed_array_pointer_descriptions(
2540 FILE *file, const attr_list_t *attrs, type_t *type,
2541 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2542 unsigned int *typestring_offset)
2543 {
2544 int pointer_count = 0;
2545
2546 if (type_get_type(type) == TYPE_ARRAY &&
2547 !type_array_has_conformance(type) && !type_array_has_variance(type))
2548 {
2549 unsigned int temp = 0;
2550 /* unfortunately, this needs to be done in two passes to avoid
2551 * writing out redundant FC_FIXED_REPEAT descriptions */
2552 pointer_count = write_pointer_description_offsets(
2553 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2554 if (pointer_count > 0)
2555 {
2556 unsigned int increment_size;
2557 unsigned int offset_of_array_pointer_mem = 0;
2558 unsigned int offset_of_array_pointer_buf = 0;
2559
2560 increment_size = type_memsize(type_array_get_element(type));
2561
2562 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2563 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2564 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2565 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2566 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2567 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2568 *typestring_offset += 10;
2569
2570 pointer_count = write_pointer_description_offsets(
2571 file, attrs, type, &offset_of_array_pointer_mem,
2572 &offset_of_array_pointer_buf, typestring_offset);
2573 }
2574 }
2575 else if (type_get_type(type) == TYPE_STRUCT)
2576 {
2577 const var_t *v;
2578 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2579 {
2580 if (offset_in_memory && offset_in_buffer)
2581 {
2582 unsigned int padding;
2583 unsigned int align = 0;
2584 type_memsize_and_alignment(v->type, &align);
2585 padding = ROUNDING(*offset_in_memory, align);
2586 *offset_in_memory += padding;
2587 *offset_in_buffer += padding;
2588 }
2589 pointer_count += write_fixed_array_pointer_descriptions(
2590 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2591 typestring_offset);
2592 }
2593 }
2594 else
2595 {
2596 if (offset_in_memory && offset_in_buffer)
2597 {
2598 unsigned int memsize;
2599 memsize = type_memsize(type);
2600 *offset_in_memory += memsize;
2601 /* increment these separately as in the case of conformant (varying)
2602 * structures these start at different values */
2603 *offset_in_buffer += memsize;
2604 }
2605 }
2606
2607 return pointer_count;
2608 }
2609
2610 /* Note: if file is NULL return value is number of pointers to write, else
2611 * it is the number of type format characters written */
2612 static int write_conformant_array_pointer_descriptions(
2613 FILE *file, const attr_list_t *attrs, type_t *type,
2614 unsigned int offset_in_memory, unsigned int *typestring_offset)
2615 {
2616 int pointer_count = 0;
2617
2618 if (is_conformant_array(type) && !type_array_has_variance(type))
2619 {
2620 unsigned int temp = 0;
2621 /* unfortunately, this needs to be done in two passes to avoid
2622 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2623 pointer_count = write_pointer_description_offsets(
2624 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2625 if (pointer_count > 0)
2626 {
2627 unsigned int increment_size;
2628 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2629 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2630
2631 increment_size = type_memsize(type_array_get_element(type));
2632
2633 if (increment_size > USHRT_MAX)
2634 error("array size of %u bytes is too large\n", increment_size);
2635
2636 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2637 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2638 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2641 *typestring_offset += 8;
2642
2643 pointer_count = write_pointer_description_offsets(
2644 file, attrs, type_array_get_element(type),
2645 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2646 typestring_offset);
2647 }
2648 }
2649
2650 return pointer_count;
2651 }
2652
2653 /* Note: if file is NULL return value is number of pointers to write, else
2654 * it is the number of type format characters written */
2655 static int write_varying_array_pointer_descriptions(
2656 FILE *file, const attr_list_t *attrs, type_t *type,
2657 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2658 unsigned int *typestring_offset)
2659 {
2660 int pointer_count = 0;
2661
2662 if (is_array(type) && type_array_has_variance(type))
2663 {
2664 unsigned int temp = 0;
2665 /* unfortunately, this needs to be done in two passes to avoid
2666 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2667 pointer_count = write_pointer_description_offsets(
2668 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2669 if (pointer_count > 0)
2670 {
2671 unsigned int increment_size;
2672
2673 increment_size = type_memsize(type_array_get_element(type));
2674
2675 if (increment_size > USHRT_MAX)
2676 error("array size of %u bytes is too large\n", increment_size);
2677
2678 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2679 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2680 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2681 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2682 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2683 *typestring_offset += 8;
2684
2685 pointer_count = write_pointer_description_offsets(
2686 file, attrs, type_array_get_element(type), offset_in_memory,
2687 offset_in_buffer, typestring_offset);
2688 }
2689 }
2690 else if (type_get_type(type) == TYPE_STRUCT)
2691 {
2692 const var_t *v;
2693 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2694 {
2695 if (offset_in_memory && offset_in_buffer)
2696 {
2697 unsigned int align = 0, padding;
2698
2699 if (is_array(v->type) && type_array_has_variance(v->type))
2700 {
2701 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2702 /* skip over variance and offset in buffer */
2703 *offset_in_buffer += 8;
2704 }
2705
2706 type_memsize_and_alignment(v->type, &align);
2707 padding = ROUNDING(*offset_in_memory, align);
2708 *offset_in_memory += padding;
2709 *offset_in_buffer += padding;
2710 }
2711 pointer_count += write_varying_array_pointer_descriptions(
2712 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2713 typestring_offset);
2714 }
2715 }
2716 else
2717 {
2718 if (offset_in_memory && offset_in_buffer)
2719 {
2720 unsigned int memsize = type_memsize(type);
2721 *offset_in_memory += memsize;
2722 /* increment these separately as in the case of conformant (varying)
2723 * structures these start at different values */
2724 *offset_in_buffer += memsize;
2725 }
2726 }
2727
2728 return pointer_count;
2729 }
2730
2731 static void write_pointer_description(FILE *file, type_t *type,
2732 unsigned int *typestring_offset)
2733 {
2734 unsigned int offset_in_buffer;
2735 unsigned int offset_in_memory;
2736
2737 /* pass 1: search for single instance of a pointer (i.e. don't descend
2738 * into arrays) */
2739 if (!is_array(type))
2740 {
2741 offset_in_memory = 0;
2742 offset_in_buffer = 0;
2743 write_no_repeat_pointer_descriptions(
2744 file, NULL, type,
2745 &offset_in_memory, &offset_in_buffer, typestring_offset);
2746 }
2747
2748 /* pass 2: search for pointers in fixed arrays */
2749 offset_in_memory = 0;
2750 offset_in_buffer = 0;
2751 write_fixed_array_pointer_descriptions(
2752 file, NULL, type,
2753 &offset_in_memory, &offset_in_buffer, typestring_offset);
2754
2755 /* pass 3: search for pointers in conformant only arrays (but don't descend
2756 * into conformant varying or varying arrays) */
2757 if (is_conformant_array(type) &&
2758 (type_array_is_decl_as_ptr(type) || !current_structure))
2759 write_conformant_array_pointer_descriptions(
2760 file, NULL, type, 0, typestring_offset);
2761 else if (type_get_type(type) == TYPE_STRUCT &&
2762 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2763 {
2764 type_t *carray = find_array_or_string_in_struct(type)->type;
2765 write_conformant_array_pointer_descriptions( file, NULL, carray,
2766 type_memsize(type), typestring_offset);
2767 }
2768
2769 /* pass 4: search for pointers in varying arrays */
2770 offset_in_memory = 0;
2771 offset_in_buffer = 0;
2772 write_varying_array_pointer_descriptions(
2773 file, NULL, type,
2774 &offset_in_memory, &offset_in_buffer, typestring_offset);
2775 }
2776
2777 int is_declptr(const type_t *t)
2778 {
2779 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2780 }
2781
2782 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2783 type_t *type, enum type_context context,
2784 const char *name, unsigned int *typestring_offset)
2785 {
2786 unsigned int start_offset;
2787 unsigned char rtype;
2788 type_t *elem_type;
2789 int is_processed = processed(type);
2790
2791 start_offset = *typestring_offset;
2792
2793 if (is_declptr(type))
2794 {
2795 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2796 int pointer_type = get_pointer_fc_context(type, attrs, context);
2797 if (!pointer_type)
2798 pointer_type = RPC_FC_RP;
2799 print_start_tfs_comment(file, type, *typestring_offset);
2800 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2801 pointer_type, flag, string_of_type(pointer_type),
2802 flag ? " [simple_pointer]" : "");
2803 *typestring_offset += 2;
2804 if (!flag)
2805 {
2806 print_file(file, 2, "NdrFcShort(0x2),\n");
2807 *typestring_offset += 2;
2808 }
2809 is_processed = FALSE;
2810 }
2811
2812 if (is_array(type))
2813 elem_type = type_array_get_element(type);
2814 else
2815 elem_type = type_pointer_get_ref(type);
2816
2817 if (type_get_type(elem_type) != TYPE_BASIC)
2818 {
2819 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2820 return start_offset;
2821 }
2822
2823 rtype = get_basic_fc(elem_type);
2824 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2825 {
2826 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2827 return start_offset;
2828 }
2829
2830 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2831 {
2832 unsigned int dim = type_array_get_dim(type);
2833
2834 if (is_processed) return start_offset;
2835
2836 /* FIXME: multi-dimensional array */
2837 if (0xffffu < dim)
2838 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2839 name, 0xffffu, dim - 0xffffu);
2840
2841 if (rtype == RPC_FC_WCHAR)
2842 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2843 else
2844 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2845 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2846 *typestring_offset += 2;
2847
2848 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2849 *typestring_offset += 2;
2850
2851 update_tfsoff(type, start_offset, file);
2852 return start_offset;
2853 }
2854 else if (is_conformant_array(type))
2855 {
2856 if (rtype == RPC_FC_WCHAR)
2857 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2858 else
2859 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2860 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2861 *typestring_offset += 2;
2862
2863 *typestring_offset += write_conf_or_var_desc(
2864 file, current_structure,
2865 (!type_array_is_decl_as_ptr(type) && current_structure
2866 ? type_memsize(current_structure)
2867 : 0),
2868 type, type_array_get_conformance(type));
2869
2870 update_tfsoff(type, start_offset, file);
2871 return start_offset;
2872 }
2873 else
2874 {
2875 if (is_processed) return start_offset;
2876
2877 if (rtype == RPC_FC_WCHAR)
2878 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2879 else
2880 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2881 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2882 *typestring_offset += 2;
2883
2884 update_tfsoff(type, start_offset, file);
2885 return start_offset;
2886 }
2887 }
2888
2889 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2890 const char *name, unsigned int *typestring_offset)
2891 {
2892 const expr_t *length_is = type_array_get_variance(type);
2893 const expr_t *size_is = type_array_get_conformance(type);
2894 unsigned int align;
2895 unsigned int size;
2896 unsigned int start_offset;
2897 unsigned char fc;
2898 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2899 unsigned int baseoff
2900 = !type_array_is_decl_as_ptr(type) && current_structure
2901 ? type_memsize(current_structure)
2902 : 0;
2903
2904 if (!pointer_type)
2905 pointer_type = RPC_FC_RP;
2906
2907 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2908
2909 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2910 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2911 fc = get_array_fc(type);
2912
2913 start_offset = *typestring_offset;
2914 update_tfsoff(type, start_offset, file);
2915 print_start_tfs_comment(file, type, start_offset);
2916 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2917 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2918 *typestring_offset += 2;
2919
2920 align = 0;
2921 if (fc != RPC_FC_BOGUS_ARRAY)
2922 {
2923 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2924 {
2925 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2926 *typestring_offset += 4;
2927 }
2928 else
2929 {
2930 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2931 *typestring_offset += 2;
2932 }
2933
2934 if (is_conformant_array(type))
2935 *typestring_offset
2936 += write_conf_or_var_desc(file, current_structure, baseoff,
2937 type, size_is);
2938
2939 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2940 {
2941 unsigned int elsize = type_memsize(type_array_get_element(type));
2942 unsigned int dim = type_array_get_dim(type);
2943
2944 if (fc == RPC_FC_LGVARRAY)
2945 {
2946 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2947 *typestring_offset += 4;
2948 }
2949 else
2950 {
2951 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2952 *typestring_offset += 2;
2953 }
2954
2955 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2956 *typestring_offset += 2;
2957 }
2958
2959 if (length_is)
2960 *typestring_offset
2961 += write_conf_or_var_desc(file, current_structure, baseoff,
2962 type, length_is);
2963
2964 if (type_has_pointers(type_array_get_element(type)) &&
2965 (type_array_is_decl_as_ptr(type) || !current_structure))
2966 {
2967 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2968 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2969 *typestring_offset += 2;
2970 write_pointer_description(file, type, typestring_offset);
2971 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2972 *typestring_offset += 1;
2973 }
2974
2975 write_array_element_type(file, type, FALSE, typestring_offset);
2976 write_end(file, typestring_offset);
2977 }
2978 else
2979 {
2980 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2981 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2982 *typestring_offset += 2;
2983 *typestring_offset
2984 += write_conf_or_var_desc(file, current_structure, baseoff,
2985 type, size_is);
2986 *typestring_offset
2987 += write_conf_or_var_desc(file, current_structure, baseoff,
2988 type, length_is);
2989
2990 write_array_element_type(file, type, TRUE, typestring_offset);
2991 write_end(file, typestring_offset);
2992 }
2993
2994 return start_offset;
2995 }
2996
2997 static const var_t *find_array_or_string_in_struct(const type_t *type)
2998 {
2999 const var_list_t *fields = type_struct_get_fields(type);
3000 const var_t *last_field;
3001 const type_t *ft;
3002
3003 if (!fields || list_empty(fields))
3004 return NULL;
3005
3006 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3007 ft = last_field->type;
3008
3009 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3010 return last_field;
3011
3012 if (type_get_type(ft) == TYPE_STRUCT)
3013 return find_array_or_string_in_struct(ft);
3014 else
3015 return NULL;
3016 }
3017
3018 static void write_struct_members(FILE *file, const type_t *type,
3019 int is_complex, unsigned int *corroff,
3020 unsigned int *typestring_offset)
3021 {
3022 const var_t *field;
3023 unsigned short offset = 0;
3024 unsigned int salign = 1;
3025 int padding;
3026 var_list_t *fields = type_struct_get_fields(type);
3027
3028 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3029 {
3030 type_t *ft = field->type;
3031 unsigned int align = 0;
3032 unsigned int size = type_memsize_and_alignment(ft, &align);
3033 align = clamp_align(align);
3034 if (salign < align) salign = align;
3035
3036 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3037 {
3038 if ((align - 1) & offset)
3039 {
3040 unsigned char fc = 0;
3041 switch (align)
3042 {
3043 case 2:
3044 fc = RPC_FC_ALIGNM2;
3045 break;
3046 case 4:
3047 fc = RPC_FC_ALIGNM4;
3048 break;
3049 case 8:
3050 fc = RPC_FC_ALIGNM8;
3051 break;
3052 default:
3053 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3054 }
3055 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3056 offset = ROUND_SIZE(offset, align);
3057 *typestring_offset += 1;
3058 }
3059 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3060 typestring_offset);
3061 offset += size;
3062 }
3063 }
3064
3065 padding = ROUNDING(offset, salign);
3066 if (padding)
3067 {
3068 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3069 RPC_FC_STRUCTPAD1 + padding - 1,
3070 padding);
3071 *typestring_offset += 1;
3072 }
3073
3074 write_end(file, typestring_offset);
3075 }
3076
3077 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3078 const char *name, unsigned int *tfsoff)
3079 {
3080 const type_t *save_current_structure = current_structure;
3081 unsigned int total_size;
3082 const var_t *array;
3083 unsigned int start_offset;
3084 unsigned int align;
3085 unsigned int corroff;
3086 var_t *f;
3087 unsigned char fc = get_struct_fc(type);
3088 var_list_t *fields = type_struct_get_fields(type);
3089
3090 if (processed(type)) return type->typestring_offset;
3091
3092 guard_rec(type);
3093 current_structure = type;
3094
3095 total_size = type_memsize(type);
3096 align = type_buffer_alignment(type);
3097 if (total_size > USHRT_MAX)
3098 error("structure size for %s exceeds %d bytes by %d bytes\n",
3099 name, USHRT_MAX, total_size - USHRT_MAX);
3100
3101 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3102 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3103
3104 array = find_array_or_string_in_struct(type);
3105 if (array && !processed(array->type))
3106 {
3107 if(is_string_type(array->attrs, array->type))
3108 write_string_tfs(file, array->attrs, array->type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3109 else
3110 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3111 }
3112
3113 corroff = *tfsoff;
3114 write_descriptors(file, type, tfsoff);
3115
3116 start_offset = *tfsoff;
3117 update_tfsoff(type, start_offset, file);
3118 print_start_tfs_comment(file, type, start_offset);
3119 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3120 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3121 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3122 *tfsoff += 4;
3123
3124 if (array)
3125 {
3126 unsigned int absoff = array->type->typestring_offset;
3127 short reloff = absoff - *tfsoff;
3128 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3129 reloff, reloff, absoff);
3130 *tfsoff += 2;
3131 }
3132 else if (fc == RPC_FC_BOGUS_STRUCT)
3133 {
3134 print_file(file, 2, "NdrFcShort(0x0),\n");
3135 *tfsoff += 2;
3136 }
3137
3138 if (fc == RPC_FC_BOGUS_STRUCT)
3139 {
3140 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3141 nothing is written to file yet. On the actual writing pass,
3142 this will have been updated. */
3143 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3144 int reloff = absoff - *tfsoff;
3145 assert( reloff >= 0 );
3146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3147 (unsigned short)reloff, reloff, absoff);
3148 *tfsoff += 2;
3149 }
3150 else if ((fc == RPC_FC_PSTRUCT) ||
3151 (fc == RPC_FC_CPSTRUCT) ||
3152 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3153 {
3154 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3155 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3156 *tfsoff += 2;
3157 write_pointer_description(file, type, tfsoff);
3158 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3159 *tfsoff += 1;
3160 }
3161
3162 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3163 tfsoff);
3164
3165 if (fc == RPC_FC_BOGUS_STRUCT)
3166 {
3167 const var_t *f;
3168
3169 type->ptrdesc = *tfsoff;
3170 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3171 {
3172 type_t *ft = f->type;
3173 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3174 {
3175 case TGT_POINTER:
3176 if (is_string_type(f->attrs, ft))
3177 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3178 else
3179 write_pointer_tfs(file, f->attrs, ft,
3180 type_pointer_get_ref(ft)->typestring_offset,
3181 TYPE_CONTEXT_CONTAINER, tfsoff);
3182 break;
3183 case TGT_ARRAY:
3184 if (type_array_is_decl_as_ptr(ft))
3185 {
3186 unsigned int offset;
3187
3188 print_file(file, 0, "/* %d */\n", *tfsoff);
3189
3190 offset = ft->typestring_offset;
3191 /* skip over the pointer that is written for strings, since a
3192 * pointer has to be written in-place here */
3193 if (is_string_type(f->attrs, ft))
3194 offset += 4;
3195 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3196 }
3197 break;
3198 default:
3199 break;
3200 }
3201 }
3202 if (type->ptrdesc == *tfsoff)
3203 type->ptrdesc = 0;
3204 }
3205
3206 current_structure = save_current_structure;
3207 return start_offset;
3208 }
3209
3210 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3211 {
3212 if (t == NULL)
3213 {
3214 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3215 }
3216 else
3217 {
3218 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3219 {
3220 unsigned char fc;
3221 if (type_get_type(t) == TYPE_BASIC)
3222 fc = get_basic_fc(t);
3223 else
3224 fc = get_enum_fc(t);
3225 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3226 fc, string_of_type(fc));
3227 }
3228 else if (t->typestring_offset)
3229 {
3230 short reloff = t->typestring_offset - *tfsoff;
3231 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3232 reloff, reloff, t->typestring_offset);
3233 }
3234 else
3235 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3236 }
3237
3238 *tfsoff += 2;
3239 }
3240
3241 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3242 type_t *type, unsigned int *tfsoff)
3243 {
3244 unsigned int start_offset;
3245 unsigned int size;
3246 var_list_t *fields;
3247 unsigned int nbranch = 0;
3248 type_t *deftype = NULL;
3249 short nodeftype = 0xffff;
3250 var_t *f;
3251
3252 if (processed(type) &&
3253 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3254 return type->typestring_offset;
3255
3256 guard_rec(type);
3257
3258 size = type_memsize(type);
3259
3260 fields = type_union_get_cases(type);
3261
3262 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3263 {
3264 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3265 if (cases)
3266 nbranch += list_count(cases);
3267 if (f->type)
3268 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3269 }
3270
3271 start_offset = *tfsoff;
3272 update_tfsoff(type, start_offset, file);
3273 print_start_tfs_comment(file, type, start_offset);
3274 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3275 {
3276 const var_t *sv = type_union_get_switch_value(type);
3277 const type_t *st = sv->type;
3278 unsigned char fc;
3279
3280 if (type_get_type(st) == TYPE_BASIC)
3281 {
3282 fc = get_basic_fc(st);
3283 switch (fc)
3284 {
3285 case RPC_FC_CHAR:
3286 case RPC_FC_SMALL:
3287 case RPC_FC_BYTE:
3288 case RPC_FC_USMALL:
3289 case RPC_FC_WCHAR:
3290 case RPC_FC_SHORT:
3291 case RPC_FC_USHORT:
3292 case RPC_FC_LONG:
3293 case RPC_FC_ULONG:
3294 break;
3295 default:
3296 fc = 0;
3297 error("union switch type must be an integer, char, or enum\n");
3298 }
3299 }
3300 else if (type_get_type(st) == TYPE_ENUM)
3301 fc = get_enum_fc(st);
3302 else
3303 error("union switch type must be an integer, char, or enum\n");
3304
3305 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3306 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3307 0x40 | fc, string_of_type(fc));
3308 *tfsoff += 2;
3309 }
3310 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3311 {
3312 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3313 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3314 unsigned char fc;
3315
3316 if (type_get_type(st) == TYPE_BASIC)
3317 {
3318 fc = get_basic_fc(st);
3319 switch (fc)
3320 {
3321 case RPC_FC_CHAR:
3322 case RPC_FC_SMALL:
3323 case RPC_FC_USMALL:
3324 case RPC_FC_SHORT:
3325 case RPC_FC_USHORT:
3326 case RPC_FC_LONG:
3327 case RPC_FC_ULONG:
3328 case RPC_FC_ENUM16:
3329 case RPC_FC_ENUM32:
3330 break;
3331 default:
3332 fc = 0;
3333 error("union switch type must be an integer, char, or enum\n");
3334 }
3335 }
3336 else if (type_get_type(st) == TYPE_ENUM)
3337 fc = get_enum_fc(st);
3338 else
3339 error("union switch type must be an integer, char, or enum\n");
3340
3341 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3342 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3343 fc, string_of_type(fc));
3344 *tfsoff += 2;
3345 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3346 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3347 *tfsoff += 2;
3348 print_file(file, 0, "/* %u */\n", *tfsoff);
3349 }
3350
3351 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3352 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3353 *tfsoff += 4;
3354
3355 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3356 {
3357 type_t *ft = f->type;
3358 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3359 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3360 expr_t *c;
3361
3362 if (cases == NULL && !deflt)
3363 error("union field %s with neither case nor default attribute\n", f->name);
3364
3365 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3366 {
3367 /* MIDL doesn't check for duplicate cases, even though that seems
3368 like a reasonable thing to do, it just dumps them to the TFS
3369 like we're going to do here. */
3370 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3371 *tfsoff += 4;
3372 write_branch_type(file, ft, tfsoff);
3373 }
3374
3375 /* MIDL allows multiple default branches, even though that seems
3376 illogical, it just chooses the last one, which is what we will
3377 do. */
3378 if (deflt)
3379 {
3380 deftype = ft;
3381 nodeftype = 0;
3382 }
3383 }
3384
3385 if (deftype)
3386 {
3387 write_branch_type(file, deftype, tfsoff);
3388 }
3389 else
3390 {
3391 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3392 *tfsoff += 2;
3393 }
3394
3395 return start_offset;
3396 }
3397
3398 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3399 unsigned int *typeformat_offset)
3400 {
3401 unsigned int i;
3402 unsigned int start_offset = *typeformat_offset;
3403 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3404
3405 if (!iid && processed(type)) return type->typestring_offset;
3406
3407 print_start_tfs_comment(file, type, start_offset);
3408 update_tfsoff(type, start_offset, file);
3409
3410 if (iid)
3411 {
3412 print_file(file, 2, "0x2f, /* FC_IP */\n");
3413 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3414 *typeformat_offset
3415 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3416 }
3417 else
3418 {
3419 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3420 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3421
3422 if (! uuid)
3423 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3424
3425 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3426 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3427 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3428 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3429 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3430 for (i = 0; i < 8; ++i)
3431 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3432
3433 if (file)
3434 fprintf(file, "\n");
3435
3436 *typeformat_offset += 18;
3437 }
3438 return start_offset;
3439 }
3440
3441 static unsigned int write_contexthandle_tfs(FILE *file,
3442 const attr_list_t *attrs,
3443 type_t *type,
3444 int toplevel_param,
3445 unsigned int *typeformat_offset)
3446 {
3447 unsigned int start_offset = *typeformat_offset;
3448 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3449
3450 print_start_tfs_comment(file, type, start_offset);
3451
3452 if (flags & 0x80) /* via ptr */
3453 {
3454 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3455 if (!pointer_type) pointer_type = RPC_FC_RP;
3456 *typeformat_offset += 4;
3457 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3458 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3459 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3460 }
3461
3462 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3463 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3464 /* return and can't be null values overlap */
3465 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3466 print_file(file, 0, "can't be null, ");
3467 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3468 print_file(file, 0, "serialize, ");
3469 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3470 print_file(file, 0, "no serialize, ");
3471 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3472 print_file(file, 0, "strict, ");
3473 if ((flags & 0x21) == 0x20)
3474 print_file(file, 0, "out, ");
3475 if ((flags & 0x21) == 0x21)
3476 print_file(file, 0, "return, ");
3477 if (flags & 0x40)
3478 print_file(file, 0, "in, ");
3479 if (flags & 0x80)
3480 print_file(file, 0, "via ptr, ");
3481 print_file(file, 0, "*/\n");
3482 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3483 print_file(file, 2, "0, /* FIXME: param num */\n");
3484 *typeformat_offset += 4;
3485
3486 update_tfsoff( type, start_offset, file );
3487 return start_offset;
3488 }
3489
3490 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3491 type_t *type, expr_list_t *range_list,
3492 unsigned int *typeformat_offset)
3493 {
3494 unsigned char fc;
3495 unsigned int start_offset = *typeformat_offset;
3496 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3497 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3498
3499 if (type_get_type(type) == TYPE_BASIC)
3500 fc = get_basic_fc(type);
3501 else
3502 fc = get_enum_fc(type);
3503
3504 /* fc must fit in lower 4-bits of 8-bit field below */
3505 assert(fc <= 0xf);
3506
3507 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3508 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3509 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3510 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3511 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3512 update_tfsoff( type, start_offset, file );
3513 *typeformat_offset += 10;
3514
3515 return start_offset;
3516 }
3517
3518 static unsigned int write_type_tfs(FILE *file, int indent,
3519 const attr_list_t *attrs, type_t *type,
3520 const char *name,
3521 enum type_context context,
3522 unsigned int *typeformat_offset)
3523 {
3524 unsigned int offset;
3525
3526 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3527 {
3528 case TGT_CTXT_HANDLE:
3529 case TGT_CTXT_HANDLE_POINTER:
3530 return write_contexthandle_tfs(file, attrs, type,
3531 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3532 case TGT_USER_TYPE:
3533 return write_user_tfs(file, type, typeformat_offset);
3534 case TGT_STRING:
3535 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3536 case TGT_ARRAY:
3537 {
3538 unsigned int off;
3539 /* conformant and pointer arrays are handled specially */
3540 if ((context != TYPE_CONTEXT_CONTAINER &&
3541 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3542 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3543 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3544 else
3545 off = 0;
3546 if (context != TYPE_CONTEXT_CONTAINER &&
3547 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3548 {
3549 int ptr_type;
3550 ptr_type = get_pointer_fc(type, attrs,
3551 context == TYPE_CONTEXT_TOPLEVELPARAM);
3552 if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3553 {
3554 unsigned int absoff = type->typestring_offset;
3555 short reloff = absoff - (*typeformat_offset + 2);
3556 off = *typeformat_offset;
3557 print_file(file, 0, "/* %d */\n", off);
3558 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3559 string_of_type(ptr_type));
3560 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3561 reloff, reloff, absoff);
3562 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3563 *typeformat_offset += 4;
3564 }
3565 type->details.array.ptr_tfsoff = off;
3566 }
3567 return off;
3568 }
3569 case TGT_STRUCT:
3570 return write_struct_tfs(file, type, name, typeformat_offset);
3571 case TGT_UNION:
3572 return write_union_tfs(file, attrs, type, typeformat_offset);
3573 case TGT_ENUM:
3574 case TGT_BASIC:
3575 /* nothing to do */
3576 return 0;
3577 case TGT_RANGE:
3578 {
3579 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3580 if (!range_list)
3581 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3582 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3583 }
3584 case TGT_IFACE_POINTER:
3585 return write_ip_tfs(file, attrs, type, typeformat_offset);
3586 case TGT_POINTER:
3587 {
3588 enum type_context ref_context;
3589 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3590 ref_context = TYPE_CONTEXT_PARAM;
3591 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3592 ref_context = TYPE_CONTEXT_CONTAINER;
3593 else
3594 ref_context = context;
3595 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3596 ref_context, typeformat_offset);
3597 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3598 return 0;
3599 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3600 }
3601 case TGT_INVALID:
3602 break;
3603 }
3604 error("invalid type %s for var %s\n", type->name, name);
3605 return 0;
3606 }
3607
3608 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3609 const char *name, int write_ptr, unsigned int *tfsoff)
3610 {
3611 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3612 }
3613
3614 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3615 type_pred_t pred, unsigned int *typeformat_offset)
3616 {
3617 var_t *var;
3618 const statement_t *stmt;
3619
3620 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3621 {
3622 const type_t *iface;
3623 const statement_t *stmt_func;
3624
3625 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3626 continue;
3627
3628 iface = stmt->u.type;
3629 if (!pred(iface))
3630 continue;
3631
3632 current_iface = iface;
3633 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3634 {
3635 const var_t *func = stmt_func->u.var;
3636 current_func = func;
3637 if (is_local(func->attrs)) continue;
3638
3639 var = type_function_get_retval(func->type);
3640 if (!is_void(var->type))
3641 var->typestring_offset = write_type_tfs( file, 2, func->attrs, var->type, func->name,
3642 TYPE_CONTEXT_PARAM, typeformat_offset);
3643
3644 if (type_get_function_args(func->type))
3645 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), var_t, entry )
3646 var->typestring_offset = write_type_tfs( file, 2, var->attrs, var->type, var->name,
3647 TYPE_CONTEXT_TOPLEVELPARAM,
3648 typeformat_offset );
3649 }
3650 }
3651
3652 return *typeformat_offset + 1;
3653 }
3654
3655 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3656 {
3657 unsigned int typeformat_offset = 2;
3658
3659 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3660 }
3661
3662
3663 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3664 {
3665 int indent = 0;
3666
3667 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3668 print_file(file, indent, "{\n");
3669 indent++;
3670 print_file(file, indent, "0,\n");
3671 print_file(file, indent, "{\n");
3672 indent++;
3673 print_file(file, indent, "NdrFcShort(0x0),\n");
3674
3675 set_all_tfswrite(TRUE);
3676 process_tfs(file, stmts, pred);
3677
3678 print_file(file, indent, "0x0\n");
3679 indent--;
3680 print_file(file, indent, "}\n");
3681 indent--;
3682 print_file(file, indent, "};\n");
3683 print_file(file, indent, "\n");
3684 }
3685
3686 static unsigned int get_required_buffer_size_type(
3687 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3688 {
3689 *alignment = 0;
3690 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3691 {
3692 case TGT_USER_TYPE:
3693 {
3694 const char *uname;
3695 const type_t *utype = get_user_type(type, &uname);
3696 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3697 }
3698 case TGT_BASIC:
3699 switch (get_basic_fc(type))
3700 {
3701 case RPC_FC_BYTE:
3702 case RPC_FC_CHAR:
3703 case RPC_FC_USMALL:
3704 case RPC_FC_SMALL:
3705 *alignment = 4;
3706 return 1;
3707
3708 case RPC_FC_WCHAR:
3709 case RPC_FC_USHORT:
3710 case RPC_FC_SHORT:
3711 *alignment = 4;
3712 return 2;
3713
3714 case RPC_FC_ULONG:
3715 case RPC_FC_LONG:
3716 case RPC_FC_FLOAT:
3717 case RPC_FC_ERROR_STATUS_T:
3718 *alignment = 4;
3719 return 4;
3720
3721 case RPC_FC_HYPER:
3722 case RPC_FC_DOUBLE:
3723 *alignment = 8;
3724 return 8;
3725
3726 case RPC_FC_INT3264:
3727 case RPC_FC_UINT3264:
3728 assert( pointer_size );
3729 *alignment = pointer_size;
3730 return pointer_size;
3731
3732 case RPC_FC_IGNORE:
3733 case RPC_FC_BIND_PRIMITIVE:
3734 return 0;
3735
3736 default:
3737 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3738 get_basic_fc(type));
3739 return 0;
3740 }
3741 break;
3742
3743 case TGT_ENUM:
3744 switch (get_enum_fc(type))
3745 {
3746 case RPC_FC_ENUM32:
3747 *alignment = 4;
3748 return 4;
3749 case RPC_FC_ENUM16:
3750 *alignment = 4;
3751 return 2;
3752 }
3753 break;
3754
3755 case TGT_STRUCT:
3756 if (get_struct_fc(type) == RPC_FC_STRUCT)
3757 {
3758 if (!type_struct_get_fields(type)) return 0;
3759 return fields_memsize(type_struct_get_fields(type), alignment);
3760 }
3761 break;
3762
3763 case TGT_POINTER:
3764 {
3765 unsigned int size, align;
3766 const type_t *ref = type_pointer_get_ref(type);
3767 if (is_string_type( attrs, ref )) break;
3768 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3769 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3770 {
3771 size += 4 + align;
3772 align = 4;
3773 }
3774 *alignment = align;
3775 return size;
3776 }
3777
3778 case TGT_ARRAY:
3779 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3780 {
3781 switch (get_array_fc(type))
3782 {
3783 case RPC_FC_SMFARRAY:
3784 case RPC_FC_LGFARRAY:
3785 return type_array_get_dim(type) *
3786 get_required_buffer_size_type(type_array_get_element(type), name,
3787 NULL, FALSE, alignment);
3788 }
3789 }
3790 break;
3791
3792 default:
3793 break;
3794 }
3795 return 0;
3796 }
3797
3798 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3799 {
3800 int in_attr = is_attr(var->attrs, ATTR_IN);
3801 int out_attr = is_attr(var->attrs, ATTR_OUT);
3802
3803 if (!in_attr && !out_attr)
3804 in_attr = 1;
3805
3806 *alignment = 0;
3807
3808 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3809 pass == PASS_RETURN)
3810 {
3811 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3812 {
3813 *alignment = 4;
3814 return 20;
3815 }
3816
3817 if (!is_string_type(var->attrs, var->type))
3818 return get_required_buffer_size_type(var->type, var->name,
3819 var->attrs, TRUE, alignment);
3820 }
3821 return 0;
3822 }
3823
3824 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3825 {
3826 const var_t *var;
3827 unsigned int total_size = 0, alignment;
3828
3829 if (type_get_function_args(func->type))
3830 {
3831 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3832 {
3833 total_size += get_required_buffer_size(var, &alignment, pass);
3834 total_size += alignment;
3835 }
3836 }
3837
3838 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3839 {
3840 var_t v = *func;
3841 v.type = type_function_get_rettype(func->type);
3842 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3843 total_size += alignment;
3844 }
3845 return total_size;
3846 }
3847
3848 static void print_phase_function(FILE *file, int indent, const char *type,
3849 const char *local_var_prefix, enum remoting_phase phase,
3850 const var_t *var, unsigned int type_offset)
3851 {
3852 const char *function;
3853 switch (phase)
3854 {
3855 case PHASE_BUFFERSIZE:
3856 function = "BufferSize";
3857 break;
3858 case PHASE_MARSHAL:
3859 function = "Marshall";
3860 break;
3861 case PHASE_UNMARSHAL:
3862 function = "Unmarshall";
3863 break;
3864 case PHASE_FREE:
3865 function = "Free";
3866 break;
3867 default:
3868 assert(0);
3869 return;
3870 }
3871
3872 print_file(file, indent, "Ndr%s%s(\n", type, function);
3873 indent++;
3874 print_file(file, indent, "&__frame->_StubMsg,\n");
3875 print_file(file, indent, "%s%s%s%s%s,\n",
3876 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3877 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3878 local_var_prefix,
3879 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3880 var->name);
3881 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3882 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3883 if (phase == PHASE_UNMARSHAL)
3884 print_file(file, indent, "0);\n");
3885 indent--;
3886 }
3887
3888 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3889 enum remoting_phase phase, enum pass pass, const var_t *var,
3890 const char *varname)
3891 {
3892 type_t *type = var->type;
3893 unsigned int alignment = 0;
3894
3895 /* no work to do for other phases, buffer sizing is done elsewhere */
3896 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3897 return;
3898
3899 if (type_get_type(type) == TYPE_ENUM ||
3900 (type_get_type(type) == TYPE_BASIC &&
3901 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3902 pointer_size != 4))
3903 {
3904 unsigned char fc;
3905
3906 if (type_get_type(type) == TYPE_ENUM)
3907 fc = get_enum_fc(type);
3908 else
3909 fc = get_basic_fc(type);
3910
3911 if (phase == PHASE_MARSHAL)
3912 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3913 else
3914 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3915 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3916 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3917 local_var_prefix,
3918 var->name);
3919 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3920 }
3921 else
3922 {
3923 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3924 switch (get_basic_fc(ref))
3925 {
3926 case RPC_FC_BYTE:
3927 case RPC_FC_CHAR:
3928 case RPC_FC_SMALL:
3929 case RPC_FC_USMALL:
3930 alignment = 1;
3931 break;
3932
3933 case RPC_FC_WCHAR:
3934 case RPC_FC_USHORT:
3935 case RPC_FC_SHORT:
3936 alignment = 2;
3937 break;
3938
3939 case RPC_FC_ULONG:
3940 case RPC_FC_LONG:
3941 case RPC_FC_FLOAT:
3942 case RPC_FC_ERROR_STATUS_T:
3943 /* pointer_size must be 4 if we got here in these two cases */
3944 case RPC_FC_INT3264:
3945 case RPC_FC_UINT3264:
3946 alignment = 4;
3947 break;
3948
3949 case RPC_FC_HYPER:
3950 case RPC_FC_DOUBLE:
3951 alignment = 8;
3952 break;
3953
3954 case RPC_FC_IGNORE:
3955 case RPC_FC_BIND_PRIMITIVE:
3956 /* no marshalling needed */
3957 return;
3958
3959 default:
3960 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3961 var->name, get_basic_fc(ref));
3962 }
3963
3964 if (phase == PHASE_MARSHAL && alignment > 1)
3965 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3966 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3967 alignment - 1, alignment - 1);
3968
3969 if (phase == PHASE_MARSHAL)
3970 {
3971 print_file(file, indent, "*(");
3972 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3973 if (is_ptr(type))
3974 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3975 else
3976 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3977 fprintf(file, "%s%s", local_var_prefix, varname);
3978 fprintf(file, ";\n");
3979 }
3980 else if (phase == PHASE_UNMARSHAL)
3981 {
3982 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3983 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3984 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3985 print_file(file, indent, "{\n");
3986 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3987 print_file(file, indent, "}\n");
3988 print_file(file, indent, "%s%s%s",
3989 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3990 local_var_prefix, varname);
3991 if (pass == PASS_IN && is_ptr(type))
3992 fprintf(file, " = (");
3993 else
3994 fprintf(file, " = *(");
3995 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3996 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3997 }
3998
3999 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4000 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4001 fprintf(file, ");\n");
4002 }
4003 }
4004
4005 /* returns whether the MaxCount, Offset or ActualCount members need to be
4006 * filled in for the specified phase */
4007 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4008 {
4009 return (phase != PHASE_UNMARSHAL);
4010 }
4011
4012 expr_t *get_size_is_expr(const type_t *t, const char *name)
4013 {
4014 expr_t *x = NULL;
4015
4016 for ( ; is_array(t); t = type_array_get_element(t))
4017 if (type_array_has_conformance(t) &&
4018 type_array_get_conformance(t)->type != EXPR_VOID)
4019 {
4020 if (!x)
4021 x = type_array_get_conformance(t);
4022 else
4023 error("%s: multidimensional conformant"
4024 " arrays not supported at the top level\n",
4025 name);
4026 }
4027
4028 return x;
4029 }
4030
4031 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4032 enum remoting_phase phase, const var_t *var, int valid_variance)
4033 {
4034 const type_t *type = var->type;
4035 /* get fundamental type for the argument */
4036 for (;;)
4037 {
4038 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4039 {
4040 case TGT_ARRAY:
4041 if (is_conformance_needed_for_phase(phase))
4042 {
4043 if (type_array_has_conformance(type) &&
4044 type_array_get_conformance(type)->type != EXPR_VOID)
4045 {
4046 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4047 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4048 fprintf(file, ";\n\n");
4049 }
4050 if (type_array_has_variance(type))
4051 {
4052 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4053 if (valid_variance)
4054 {
4055 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4056 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4057 fprintf(file, ";\n\n");
4058 }
4059 else
4060 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4061 }
4062 }
4063 break;
4064 case TGT_UNION:
4065 if (type_get_type(type) == TYPE_UNION &&
4066 is_conformance_needed_for_phase(phase))
4067 {
4068 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4069 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4070 fprintf(file, ";\n\n");
4071 }
4072 break;
4073 case TGT_IFACE_POINTER:
4074 {
4075 expr_t *iid;
4076
4077 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4078 {
4079 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4080 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4081 fprintf( file, ";\n\n" );
4082 }
4083 break;
4084 }
4085 case TGT_POINTER:
4086 type = type_pointer_get_ref(type);
4087 continue;
4088 case TGT_INVALID:
4089 case TGT_USER_TYPE:
4090 case TGT_CTXT_HANDLE:
4091 case TGT_CTXT_HANDLE_POINTER:
4092 case TGT_STRING:
4093 case TGT_BASIC:
4094 case TGT_ENUM:
4095 case TGT_STRUCT:
4096 case TGT_RANGE:
4097 break;
4098 }
4099 break;
4100 }
4101 }
4102
4103 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4104 enum pass pass, enum remoting_phase phase, const var_t *var)
4105 {
4106 int in_attr, out_attr, pointer_type;
4107 const char *type_str = NULL;
4108 const type_t *type = var->type;
4109 unsigned int alignment, start_offset = type->typestring_offset;
4110
4111 if (is_ptr(type) || is_array(type))
4112 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4113 else
4114 pointer_type = 0;
4115
4116 in_attr = is_attr(var->attrs, ATTR_IN);
4117 out_attr = is_attr(var->attrs, ATTR_OUT);
4118 if (!in_attr && !out_attr)
4119 in_attr = 1;
4120
4121 if (phase != PHASE_FREE)
4122 switch (pass)
4123 {
4124 case PASS_IN:
4125 if (!in_attr) return;
4126 break;
4127 case PASS_OUT:
4128 if (!out_attr) return;
4129 break;
4130 case PASS_RETURN:
4131 break;
4132 }
4133
4134 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4135
4136 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4137
4138 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4139 {
4140 case TGT_CTXT_HANDLE:
4141 case TGT_CTXT_HANDLE_POINTER:
4142 if (phase == PHASE_MARSHAL)
4143 {
4144 if (pass == PASS_IN)
4145 {
4146 /* if the context_handle attribute appears in the chain of types
4147 * without pointers being followed, then the context handle must
4148 * be direct, otherwise it is a pointer */
4149 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4150 print_file(file, indent, "NdrClientContextMarshall(\n");
4151 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4152 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4153 var->name);
4154 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4155 }
4156 else
4157 {
4158 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4159 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4160 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4161 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4162 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4163 }
4164 }
4165 else if (phase == PHASE_UNMARSHAL)
4166 {
4167 if (pass == PASS_OUT)
4168 {
4169 if (!in_attr)
4170 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4171 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4172 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4173 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4174 print_file(file, indent + 1, "__frame->_Handle);\n");
4175 }
4176 else
4177 {
4178 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4179 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4180 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4181 }
4182 }
4183 break;
4184 case TGT_USER_TYPE:
4185 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4186 break;
4187 case TGT_STRING:
4188 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4189 pointer_type != RPC_FC_RP)
4190 {
4191 /* strings returned are assumed to be global and hence don't
4192 * need freeing */
4193 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4194 print_phase_function(file, indent, "Pointer", local_var_prefix,
4195 phase, var, start_offset);
4196 else if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4197 !in_attr && is_conformant_array(type))
4198 {
4199 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4200 indent++;
4201 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4202 }
4203 }
4204 else
4205 {
4206 unsigned int real_start_offset = start_offset;
4207 /* skip over pointer description straight to string description */
4208 if (is_declptr(type))
4209 {
4210 if (is_conformant_array(type))
4211 real_start_offset += 4;
4212 else
4213 real_start_offset += 2;
4214 }
4215 if (is_array(type) && !is_conformant_array(type))
4216 print_phase_function(file, indent, "NonConformantString",
4217 local_var_prefix, phase, var,
4218 real_start_offset);
4219 else
4220 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4221 phase, var, real_start_offset);
4222 }
4223 break;
4224 case TGT_ARRAY:
4225 {
4226 unsigned char tc = get_array_fc(type);
4227 const char *array_type = NULL;
4228
4229 /* We already have the size_is expression since it's at the
4230 top level, but do checks for multidimensional conformant
4231 arrays. When we handle them, we'll need to extend this
4232 function to return a list, and then we'll actually use
4233 the return value. */
4234 get_size_is_expr(type, var->name);
4235
4236 switch (tc)
4237 {
4238 case RPC_FC_SMFARRAY:
4239 case RPC_FC_LGFARRAY:
4240 array_type = "FixedArray";
4241 break;
4242 case RPC_FC_SMVARRAY:
4243 case RPC_FC_LGVARRAY:
4244 array_type = "VaryingArray";
4245 break;
4246 case RPC_FC_CARRAY:
4247 array_type = "ConformantArray";
4248 break;
4249 case RPC_FC_CVARRAY:
4250 array_type = "ConformantVaryingArray";
4251 break;
4252 case RPC_FC_BOGUS_ARRAY:
4253 array_type = "ComplexArray";
4254 break;
4255 }
4256
4257 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4258
4259 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4260 {
4261 /* these are all unmarshalled by allocating memory */
4262 if (tc == RPC_FC_BOGUS_ARRAY ||
4263 tc == RPC_FC_CVARRAY ||
4264 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4265 (tc == RPC_FC_CARRAY && !in_attr))
4266 {
4267 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff)
4268 {
4269 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4270 type->details.array.ptr_tfsoff);
4271 break;
4272 }
4273 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4274 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4275 indent++;
4276 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4277 break;
4278 }
4279 }
4280 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4281 break;
4282 }
4283 case TGT_BASIC:
4284 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4285 break;
4286 case TGT_ENUM:
4287 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4288 break;
4289 case TGT_RANGE:
4290 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4291 /* Note: this goes beyond what MIDL does - it only supports arguments
4292 * with the [range] attribute in Oicf mode */
4293 if (phase == PHASE_UNMARSHAL)
4294 {
4295 const expr_t *range_min;
4296 const expr_t *range_max;
4297 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4298 if (!range_list)
4299 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4300 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4301 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4302
4303 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4304 write_type_decl(file, var->type, NULL);
4305 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4306 write_type_decl(file, var->type, NULL);
4307 fprintf(file, ")0x%x))\n", range_max->cval);
4308 print_file(file, indent, "{\n");
4309 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4310 print_file(file, indent, "}\n");
4311 }
4312 break;
4313 case TGT_STRUCT:
4314 switch (get_struct_fc(type))
4315 {
4316 case RPC_FC_STRUCT:
4317 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4318 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4319 break;
4320 case RPC_FC_PSTRUCT:
4321 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4322 break;
4323 case RPC_FC_CSTRUCT:
4324 case RPC_FC_CPSTRUCT:
4325 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4326 break;
4327 case RPC_FC_CVSTRUCT:
4328 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4329 break;
4330 case RPC_FC_BOGUS_STRUCT:
4331 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4332 break;
4333 default:
4334 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4335 }
4336 break;
4337 case TGT_UNION:
4338 {
4339 const char *union_type = NULL;
4340
4341 if (type_get_type(type) == TYPE_UNION)
4342 union_type = "NonEncapsulatedUnion";
4343 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4344 union_type = "EncapsulatedUnion";
4345
4346 print_phase_function(file, indent, union_type, local_var_prefix,
4347 phase, var, start_offset);
4348 break;
4349 }
4350 case TGT_POINTER:
4351 {
4352 const type_t *ref = type_pointer_get_ref(type);
4353 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4354 {
4355 case TGT_BASIC:
4356 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4357 break;
4358 case TGT_ENUM:
4359 /* base types have known sizes, so don't need a sizing pass
4360 * and don't have any memory to free and so don't need a
4361 * freeing pass */
4362 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4363 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4364 break;
4365 case TGT_STRUCT:
4366 switch (get_struct_fc(ref))
4367 {
4368 case RPC_FC_STRUCT:
4369 /* simple structs have known sizes, so don't need a sizing
4370 * pass and don't have any memory to free and so don't
4371 * need a freeing pass */
4372 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4373 type_str = "SimpleStruct";
4374 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4375 {
4376 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4377 indent++;
4378 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4379 indent--;
4380 }
4381 break;
4382 case RPC_FC_PSTRUCT:
4383 type_str = "SimpleStruct";
4384 break;
4385 case RPC_FC_CSTRUCT:
4386 case RPC_FC_CPSTRUCT:
4387 type_str = "ConformantStruct";
4388 break;
4389 case RPC_FC_CVSTRUCT:
4390 type_str = "ConformantVaryingStruct";
4391 break;
4392 case RPC_FC_BOGUS_STRUCT:
4393 type_str = "ComplexStruct";
4394 break;
4395 default:
4396 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4397 }
4398
4399 if (type_str)
4400 {
4401 if (phase == PHASE_FREE)
4402 type_str = "Pointer";
4403 else
4404 start_offset = ref->typestring_offset;
4405 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4406 }
4407 break;
4408 case TGT_UNION:
4409 if (phase == PHASE_FREE)
4410 type_str = "Pointer";
4411 else
4412 {
4413 if (type_get_type(ref) == TYPE_UNION)
4414 type_str = "NonEncapsulatedUnion";
4415 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4416 type_str = "EncapsulatedUnion";
4417
4418 start_offset = ref->typestring_offset;
4419 }
4420
4421 print_phase_function(file, indent, type_str, local_var_prefix,
4422 phase, var, start_offset);
4423 break;
4424 case TGT_USER_TYPE:
4425 if (phase != PHASE_FREE)
4426 {
4427 type_str = "UserMarshal";
4428 start_offset = ref->typestring_offset;
4429 }
4430 else type_str = "Pointer";
4431
4432 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4433 break;
4434 case TGT_STRING:
4435 case TGT_POINTER:
4436 case TGT_ARRAY:
4437 case TGT_RANGE:
4438 case TGT_IFACE_POINTER:
4439 case TGT_CTXT_HANDLE:
4440 case TGT_CTXT_HANDLE_POINTER:
4441 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4442 break;
4443 case TGT_INVALID:
4444 assert(0);
4445 break;
4446 }
4447 else
4448 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4449 break;
4450 }
4451 case TGT_IFACE_POINTER:
4452 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4453 break;
4454 case TGT_INVALID:
4455 assert(0);
4456 break;
4457 }
4458 fprintf(file, "\n");
4459 }
4460
4461 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4462 enum pass pass, enum remoting_phase phase)
4463 {
4464 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4465 {
4466 unsigned int size = get_function_buffer_size( func, pass );
4467 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4468 }
4469
4470 if (pass == PASS_RETURN)
4471 {
4472 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4473 type_function_get_retval(func->type) );
4474 }
4475 else
4476 {
4477 const var_t *var;
4478 if (!type_get_function_args(func->type))
4479 return;
4480 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4481 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4482 }
4483 }
4484
4485
4486 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4487 {
4488 unsigned int offset = 0;
4489 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4490 return offset;
4491 }
4492
4493 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4494 {
4495 const statement_t *stmt;
4496 unsigned int size = 1;
4497
4498 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4499 {
4500 const type_t *iface;
4501 const statement_t *stmt_func;
4502
4503 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4504 continue;
4505
4506 iface = stmt->u.type;
4507 if (!pred(iface))
4508 continue;
4509
4510 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4511 {
4512 const var_t *func = stmt_func->u.var;
4513 if (!is_local(func->attrs))
4514 size += get_size_procformatstring_func( iface, func );
4515 }
4516 }
4517 return size;
4518 }
4519
4520 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4521 {
4522 set_all_tfswrite(FALSE);
4523 return process_tfs(NULL, stmts, pred);
4524 }
4525
4526 void declare_stub_args( FILE *file, int indent, const var_t *func )
4527 {
4528 int in_attr, out_attr;
4529 int i = 0;
4530 const var_t *var = type_function_get_retval(func->type);
4531
4532 /* declare return value */
4533 if (!is_void(var->type))
4534 {
4535 print_file(file, indent, "%s", "");
4536 write_type_decl(file, var->type, var->name);
4537 fprintf(file, ";\n");
4538 }
4539
4540 if (!type_get_function_args(func->type))
4541 return;
4542
4543 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4544 {
4545 in_attr = is_attr(var->attrs, ATTR_IN);
4546 out_attr = is_attr(var->attrs, ATTR_OUT);
4547 if (!out_attr && !in_attr)
4548 in_attr = 1;
4549
4550 if (is_context_handle(var->type))
4551 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4552 else
4553 {
4554 if (!in_attr && !is_conformant_array(var->type))
4555 {
4556 type_t *type_to_print;
4557 char name[16];
4558 print_file(file, indent, "%s", "");
4559 if (type_get_type(var->type) == TYPE_ARRAY &&
4560 !type_array_is_decl_as_ptr(var->type))
4561 type_to_print = var->type;
4562 else
4563 type_to_print = type_pointer_get_ref(var->type);
4564 sprintf(name, "_W%u", i++);
4565 write_type_decl(file, type_to_print, name);
4566 fprintf(file, ";\n");
4567 }
4568
4569 print_file(file, indent, "%s", "");
4570 write_type_decl_left(file, var->type);
4571 fprintf(file, " ");
4572 if (type_get_type(var->type) == TYPE_ARRAY &&
4573 !type_array_is_decl_as_ptr(var->type)) {
4574 fprintf(file, "(*%s)", var->name);
4575 } else
4576 fprintf(file, "%s", var->name);
4577 write_type_right(file, var->type, FALSE);
4578 fprintf(file, ";\n");
4579
4580 if (decl_indirect(var->type))
4581 print_file(file, indent, "void *_p_%s;\n", var->name);
4582 }
4583 }
4584 }
4585
4586
4587 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4588 {
4589 int in_attr, out_attr;
4590 int i = 0, sep = 0;
4591 const var_t *var;
4592 type_t *ref;
4593
4594 if (!type_get_function_args(func->type))
4595 return;
4596
4597 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4598 {
4599 in_attr = is_attr(var->attrs, ATTR_IN);
4600 out_attr = is_attr(var->attrs, ATTR_OUT);
4601 if (!out_attr && !in_attr)
4602 in_attr = 1;
4603
4604 if (!in_attr)
4605 {
4606 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4607
4608 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4609 {
4610 case TGT_CTXT_HANDLE_POINTER:
4611 fprintf(file, " = NdrContextHandleInitialize(\n");
4612 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4613 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4614 var->typestring_offset);
4615 break;
4616 case TGT_ARRAY:
4617 if (type_array_has_conformance(var->type))
4618 {
4619 unsigned int size;
4620 type_t *type;
4621
4622 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4623 for (type = var->type;
4624 is_array(type) && type_array_has_conformance(type);
4625 type = type_array_get_element(type))
4626 {
4627 write_expr(file, type_array_get_conformance(type), TRUE,
4628 TRUE, NULL, NULL, local_var_prefix);
4629 fprintf(file, " * ");
4630 }
4631 size = type_memsize(type);
4632 fprintf(file, "%u);\n", size);
4633
4634 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4635 for (type = var->type;
4636 is_array(type) && type_array_has_conformance(type);
4637 type = type_array_get_element(type))
4638 {
4639 write_expr(file, type_array_get_conformance(type), TRUE,
4640 TRUE, NULL, NULL, local_var_prefix);
4641 fprintf(file, " * ");
4642 }
4643 size = type_memsize(type);
4644 fprintf(file, "%u);\n", size);
4645 }
4646 else
4647 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4648 break;
4649 case TGT_POINTER:
4650 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4651 ref = type_pointer_get_ref(var->type);
4652 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4653 {
4654 case TGT_BASIC:
4655 case TGT_ENUM:
4656 case TGT_POINTER:
4657 case TGT_RANGE:
4658 case TGT_IFACE_POINTER:
4659 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4660 break;
4661 case TGT_USER_TYPE:
4662 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4663 local_var_prefix, i, local_var_prefix, i);
4664 break;
4665 case TGT_ARRAY:
4666 if (type_array_is_decl_as_ptr(ref))
4667 {
4668 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4669 break;
4670 }
4671 ref = type_array_get_element(ref);
4672 /* fall through */
4673 case TGT_STRUCT:
4674 case TGT_UNION:
4675 if (type_has_pointers(ref))
4676 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4677 local_var_prefix, i, local_var_prefix, i);
4678 break;
4679 case TGT_CTXT_HANDLE:
4680 case TGT_CTXT_HANDLE_POINTER:
4681 case TGT_INVALID:
4682 case TGT_STRING:
4683 /* not initialised */
4684 break;
4685 }
4686 i++;
4687 break;
4688 default:
4689 break;
4690 }
4691
4692 sep = 1;
4693 }
4694 }
4695 if (sep)
4696 fprintf(file, "\n");
4697 }
4698
4699
4700 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4701 const char *var_decl, int add_retval )
4702 {
4703 var_t *retval = type_function_get_retval( func );
4704 const var_list_t *args = type_get_function_args( func );
4705 const var_t *arg;
4706 int needs_packing;
4707 unsigned int align = 0;
4708
4709 if (args)
4710 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4711 if (!is_array( arg->type )) type_memsize_and_alignment( arg->type, &align );
4712
4713 needs_packing = (align > pointer_size);
4714
4715 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4716 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4717 print_file(file, 1, "{\n" );
4718 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4719
4720 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4721 {
4722 print_file(file, 2, "%s", "");
4723 write_type_left( file, (type_t *)arg->type, TRUE );
4724 if (needs_space_after( arg->type )) fputc( ' ', file );
4725 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4726
4727 /* FIXME: should check for large args being passed by pointer */
4728 align = 0;
4729 if (is_array( arg->type ) || is_ptr( arg->type )) align = pointer_size;
4730 else type_memsize_and_alignment( arg->type, &align );
4731
4732 if (align >= pointer_size)
4733 fprintf( file, "%s;\n", arg->name );
4734 else
4735 fprintf( file, "DECLSPEC_ALIGN(%u) %s;\n", pointer_size, arg->name );
4736 }
4737 if (add_retval && !is_void( retval->type ))
4738 {
4739 print_file(file, 2, "%s", "");
4740 write_type_decl( file, retval->type, retval->name );
4741 if (is_array( retval->type ) || is_ptr( retval->type ) ||
4742 type_memsize( retval->type ) == pointer_size)
4743 fprintf( file, ";\n" );
4744 else
4745 fprintf( file, " DECLSPEC_ALIGN(%u);\n", pointer_size );
4746 }
4747 print_file(file, 1, "} %s;\n", var_decl );
4748 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4749 print_file( file, 0, "\n" );
4750 }
4751
4752 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4753 {
4754 const var_list_t *args = type_get_function_args( func->type );
4755 const var_t *var;
4756
4757 if (!args) return;
4758
4759 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4760 if (cant_be_null( var ))
4761 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4762 }
4763
4764 int write_expr_eval_routines(FILE *file, const char *iface)
4765 {
4766 static const char *var_name = "pS";
4767 static const char *var_name_expr = "pS->";
4768 int result = 0;
4769 struct expr_eval_routine *eval;
4770 unsigned short callback_offset = 0;
4771
4772 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4773 {
4774 const char *name = eval->name;
4775 result = 1;
4776
4777 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4778 eval->iface ? eval->iface->name : iface, name, callback_offset);
4779 print_file(file, 0, "{\n");
4780 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4781 {
4782 write_func_param_struct( file, eval->iface, eval->cont_type,
4783 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4784 }
4785 else
4786 {
4787 print_file(file, 1, "%s", "");
4788 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4789 fprintf(file, " *%s = (", var_name);
4790 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4791 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4792 }
4793 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4794 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4795 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4796 fprintf(file, ";\n");
4797 print_file(file, 0, "}\n\n");
4798 callback_offset++;
4799 }
4800 return result;
4801 }
4802
4803 void write_expr_eval_routine_list(FILE *file, const char *iface)
4804 {
4805 struct expr_eval_routine *eval;
4806 struct expr_eval_routine *cursor;
4807 unsigned short callback_offset = 0;
4808
4809 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4810 fprintf(file, "{\n");
4811
4812 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4813 {
4814 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4815 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4816 callback_offset++;
4817 list_remove(&eval->entry);
4818 free(eval->name);
4819 free(eval);
4820 }
4821
4822 fprintf(file, "};\n\n");
4823 }
4824
4825 void write_user_quad_list(FILE *file)
4826 {
4827 user_type_t *ut;
4828
4829 if (list_empty(&user_type_list))
4830 return;
4831
4832 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4833 fprintf(file, "{\n");
4834 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4835 {
4836 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4837 print_file(file, 1, "{\n");
4838 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4839 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4840 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4841 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4842 print_file(file, 1, "}%s\n", sep);
4843 }
4844 fprintf(file, "};\n\n");
4845 }
4846
4847 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4848 {
4849 const struct str_list_entry_t *endpoint;
4850 const char *p;
4851
4852 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4853 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4854 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4855 {
4856 print_file( f, 1, "{ (const unsigned char *)\"" );
4857 for (p = endpoint->str; *p && *p != ':'; p++)
4858 {
4859 if (*p == '"' || *p == '\\') fputc( '\\', f );
4860 fputc( *p, f );
4861 }
4862 if (!*p) goto error;
4863 if (p[1] != '[') goto error;
4864
4865 fprintf( f, "\", (const unsigned char *)\"" );
4866 for (p += 2; *p && *p != ']'; p++)
4867 {
4868 if (*p == '"' || *p == '\\') fputc( '\\', f );
4869 fputc( *p, f );
4870 }
4871 if (*p != ']') goto error;
4872 fprintf( f, "\" },\n" );
4873 }
4874 print_file( f, 0, "};\n\n" );
4875 return;
4876
4877 error:
4878 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4879 }
4880
4881 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4882 const char *prefix, unsigned int proc_offset )
4883 {
4884 type_t *rettype = type_function_get_rettype( func->type );
4885 int has_ret = !is_void( rettype );
4886 const var_list_t *args = type_get_function_args( func->type );
4887 const var_t *arg;
4888 int len, needs_params = 0;
4889
4890 /* we need a param structure if we have more than one arg */
4891 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4892
4893 print_file( file, 0, "{\n");
4894 if (needs_params)
4895 {
4896 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4897 write_func_param_struct( file, iface, func->type, "__params", FALSE );
4898 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4899 if (args)
4900 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4901 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4902 }
4903 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4904
4905 len = fprintf( file, " %s%s( ",
4906 has_ret ? "_RetVal = " : "",
4907 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
4908 fprintf( file, "&%s_StubDesc,", prefix );
4909 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
4910 if (needs_params)
4911 {
4912 fprintf( file, ",\n%*s&__params", len, "" );
4913 }
4914 else if (pointer_size == 8)
4915 {
4916 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
4917 if (args)
4918 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4919 fprintf( file, ",\n%*s%s", len, "", arg->name );
4920 }
4921 else
4922 {
4923 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
4924 else if (args)
4925 {
4926 arg = LIST_ENTRY( list_head(args), const var_t, entry );
4927 fprintf( file, ",\n%*s&%s", len, "", arg->name );
4928 }
4929 }
4930 fprintf( file, " );\n" );
4931 if (has_ret)
4932 {
4933 print_file( file, 1, "return (" );
4934 write_type_decl_left(file, rettype);
4935 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
4936 }
4937 print_file( file, 0, "}\n\n");
4938 }
4939
4940 void write_exceptions( FILE *file )
4941 {
4942 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4943 fprintf( file, "\n");
4944 fprintf( file, "#include \"wine/exception.h\"\n");
4945 fprintf( file, "#undef RpcTryExcept\n");
4946 fprintf( file, "#undef RpcExcept\n");
4947 fprintf( file, "#undef RpcEndExcept\n");
4948 fprintf( file, "#undef RpcTryFinally\n");
4949 fprintf( file, "#undef RpcFinally\n");
4950 fprintf( file, "#undef RpcEndFinally\n");
4951 fprintf( file, "#undef RpcExceptionCode\n");
4952 fprintf( file, "#undef RpcAbnormalTermination\n");
4953 fprintf( file, "\n");
4954 fprintf( file, "struct __exception_frame;\n");
4955 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4956 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4957 fprintf( file, "\n");
4958 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4959 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4960 fprintf( file, " __filter_func filter; \\\n");
4961 fprintf( file, " __finally_func finally; \\\n");
4962 fprintf( file, " sigjmp_buf jmp; \\\n");
4963 fprintf( file, " DWORD code; \\\n");
4964 fprintf( file, " unsigned char abnormal_termination; \\\n");
4965 fprintf( file, " unsigned char filter_level; \\\n");
4966 fprintf( file, " unsigned char finally_level;\n");
4967 fprintf( file, "\n");
4968 fprintf( file, "struct __exception_frame\n{\n");
4969 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4970 fprintf( file, "};\n");
4971 fprintf( file, "\n");
4972 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4973 fprintf( file, "{\n");
4974 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4975 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4976 fprintf( file, " {\n");
4977 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4978 fprintf( file, " exc_frame->finally( exc_frame );\n");
4979 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4980 fprintf( file, " }\n");
4981 fprintf( file, " exc_frame->filter_level = 0;\n");
4982 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4983 fprintf( file, "}\n");
4984 fprintf( file, "\n");
4985 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4986 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4987 fprintf( file, " CONTEXT *context,\n");
4988 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4989 fprintf( file, "{\n");
4990 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4991 fprintf( file, "\n");
4992 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4993 fprintf( file, " {\n" );
4994 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4995 fprintf( file, " {\n" );
4996 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4997 fprintf( file, " exc_frame->finally( exc_frame );\n");
4998 fprintf( file, " }\n" );
4999 fprintf( file, " return ExceptionContinueSearch;\n");
5000 fprintf( file, " }\n" );
5001 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5002 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5003 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5004 fprintf( file, " return ExceptionContinueSearch;\n");
5005 fprintf( file, "}\n");
5006 fprintf( file, "\n");
5007 fprintf( file, "#define RpcTryExcept \\\n");
5008 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
5009 fprintf( file, " { \\\n");
5010 fprintf( file, " if (!__frame->finally_level) \\\n" );
5011 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5012 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5013 fprintf( file, "\n");
5014 fprintf( file, "#define RpcExcept(expr) \\\n");
5015 fprintf( file, " if (!__frame->finally_level) \\\n" );
5016 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5017 fprintf( file, " __frame->filter_level = 0; \\\n" );
5018 fprintf( file, " } \\\n");
5019 fprintf( file, " else \\\n");
5020 fprintf( file, "\n");
5021 fprintf( file, "#define RpcEndExcept\n");
5022 fprintf( file, "\n");
5023 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5024 fprintf( file, "\n");
5025 fprintf( file, "#define RpcTryFinally \\\n");
5026 fprintf( file, " if (!__frame->filter_level) \\\n");
5027 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5028 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5029 fprintf( file, "\n");
5030 fprintf( file, "#define RpcFinally \\\n");
5031 fprintf( file, " if (!__frame->filter_level) \\\n");
5032 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5033 fprintf( file, " __frame->finally_level = 0;\n");
5034 fprintf( file, "\n");
5035 fprintf( file, "#define RpcEndFinally\n");
5036 fprintf( file, "\n");
5037 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5038 fprintf( file, "\n");
5039 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5040 fprintf( file, " do { \\\n");
5041 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5042 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5043 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5044 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5045 fprintf( file, " __frame->filter_level = 0; \\\n");
5046 fprintf( file, " __frame->finally_level = 0; \\\n");
5047 fprintf( file, " } while (0)\n");
5048 fprintf( file, "\n");
5049 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5050 fprintf( file, "\n");
5051 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5052 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5053 fprintf( file, "\n");
5054 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5055 fprintf( file, " DWORD code;\n");
5056 fprintf( file, "\n");
5057 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");
5058 }