[WIDL] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / tools / widl / header.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2002 Ove Kaaven
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "expr.h"
37 #include "typetree.h"
38
39 static int indentation = 0;
40 static int is_object_interface = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
44
45 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name);
46
47 static void indent(FILE *h, int delta)
48 {
49 int c;
50 if (delta < 0) indentation += delta;
51 for (c=0; c<indentation; c++) fprintf(h, " ");
52 if (delta > 0) indentation += delta;
53 }
54
55 static void write_line(FILE *f, int delta, const char *fmt, ...)
56 {
57 va_list ap;
58 indent(f, delta);
59 va_start(ap, fmt);
60 vfprintf(f, fmt, ap);
61 va_end(ap);
62 fprintf(f, "\n");
63 }
64
65 int is_ptrchain_attr(const var_t *var, enum attr_type t)
66 {
67 if (is_attr(var->attrs, t))
68 return 1;
69 else
70 {
71 type_t *type = var->type;
72 for (;;)
73 {
74 if (is_attr(type->attrs, t))
75 return 1;
76 else if (type_is_alias(type))
77 type = type_alias_get_aliasee(type);
78 else if (is_ptr(type))
79 type = type_pointer_get_ref(type);
80 else return 0;
81 }
82 }
83 }
84
85 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
86 {
87 const type_t *t = type;
88 for (;;)
89 {
90 if (is_attr(t->attrs, attr))
91 return 1;
92 else if (type_is_alias(t))
93 t = type_alias_get_aliasee(t);
94 else return 0;
95 }
96 }
97
98 int is_attr(const attr_list_t *list, enum attr_type t)
99 {
100 const attr_t *attr;
101 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
102 if (attr->type == t) return 1;
103 return 0;
104 }
105
106 void *get_attrp(const attr_list_t *list, enum attr_type t)
107 {
108 const attr_t *attr;
109 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
110 if (attr->type == t) return attr->u.pval;
111 return NULL;
112 }
113
114 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
115 {
116 const attr_t *attr;
117 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
118 if (attr->type == t) return attr->u.ival;
119 return 0;
120 }
121
122 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
123 {
124 if (!uuid) return;
125 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
127 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
128 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
129 uuid->Data4[6], uuid->Data4[7]);
130 }
131
132 static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
133 {
134 char *name = format_namespace(type->namespace, "", "::", type->name);
135 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
136 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
137 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
138 name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
139 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
140 uuid->Data4[7]);
141 fprintf(f, "#endif\n");
142 free(name);
143 }
144
145 static const char *uuid_string(const UUID *uuid)
146 {
147 static char buf[37];
148
149 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
150 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
151 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
152
153 return buf;
154 }
155
156 static void write_namespace_start(FILE *header, struct namespace *namespace)
157 {
158 if(is_global_namespace(namespace)) {
159 if(use_abi_namespace)
160 write_line(header, 1, "namespace ABI {");
161 return;
162 }
163
164 write_namespace_start(header, namespace->parent);
165 write_line(header, 1, "namespace %s {", namespace->name);
166 }
167
168 static void write_namespace_end(FILE *header, struct namespace *namespace)
169 {
170 if(is_global_namespace(namespace)) {
171 if(use_abi_namespace)
172 write_line(header, -1, "}", namespace->name);
173 return;
174 }
175
176 write_line(header, -1, "}", namespace->name);
177 write_namespace_end(header, namespace->parent);
178 }
179
180 const char *get_name(const var_t *v)
181 {
182 static char buffer[256];
183
184 if (is_attr( v->attrs, ATTR_PROPGET ))
185 strcpy( buffer, "get_" );
186 else if (is_attr( v->attrs, ATTR_PROPPUT ))
187 strcpy( buffer, "put_" );
188 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
189 strcpy( buffer, "putref_" );
190 else
191 buffer[0] = 0;
192 strcat( buffer, v->name );
193 return buffer;
194 }
195
196 static void write_fields(FILE *h, var_list_t *fields)
197 {
198 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
199 const char *name;
200 char buf[32];
201 var_t *v;
202
203 if (!fields) return;
204
205 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
206 if (!v || !v->type) continue;
207
208 switch(type_get_type_detect_alias(v->type)) {
209 case TYPE_STRUCT:
210 case TYPE_ENCAPSULATED_UNION:
211 nameless_struct_cnt++;
212 break;
213 case TYPE_UNION:
214 nameless_union_cnt++;
215 break;
216 default:
217 ;
218 }
219 }
220
221 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
222 if (!v || !v->type) continue;
223
224 indent(h, 0);
225 name = v->name;
226
227 switch(type_get_type_detect_alias(v->type)) {
228 case TYPE_STRUCT:
229 case TYPE_ENCAPSULATED_UNION:
230 if(!v->name) {
231 fprintf(h, "__C89_NAMELESS ");
232 if(nameless_struct_cnt == 1) {
233 name = "__C89_NAMELESSSTRUCTNAME";
234 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
235 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
236 name = buf;
237 }
238 }
239 break;
240 case TYPE_UNION:
241 if(!v->name) {
242 fprintf(h, "__C89_NAMELESS ");
243 if(nameless_union_cnt == 1) {
244 name = "__C89_NAMELESSUNIONNAME";
245 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
246 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
247 name = buf;
248 }
249 }
250 break;
251 default:
252 ;
253 }
254 write_type_def_or_decl(h, v->type, TRUE, name);
255 fprintf(h, ";\n");
256 }
257 }
258
259 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
260 {
261 var_t *v;
262 if (!enums) return;
263 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
264 {
265 if (v->name) {
266 indent(h, 0);
267 if(!enum_name)
268 fprintf(h, "%s", get_name(v));
269 else
270 fprintf(h, "%s_%s", enum_name, get_name(v));
271 if (v->eval) {
272 fprintf(h, " = ");
273 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
274 }
275 }
276 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
277 }
278 fprintf(h, "\n");
279 }
280
281 int needs_space_after(type_t *t)
282 {
283 return (type_is_alias(t) ||
284 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
285 }
286
287 void write_type_left(FILE *h, type_t *t, enum name_type name_type, int declonly)
288 {
289 const char *name;
290
291 if (!h) return;
292
293 name = type_get_name(t, name_type);
294
295 if (is_attr(t->attrs, ATTR_CONST) &&
296 (type_is_alias(t) || !is_ptr(t)))
297 fprintf(h, "const ");
298
299 if (type_is_alias(t)) fprintf(h, "%s", t->name);
300 else {
301 switch (type_get_type_detect_alias(t)) {
302 case TYPE_ENUM:
303 if (!declonly && t->defined && !t->written) {
304 if (name) fprintf(h, "enum %s {\n", name);
305 else fprintf(h, "enum {\n");
306 t->written = TRUE;
307 indentation++;
308 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
309 indent(h, -1);
310 fprintf(h, "}");
311 }
312 else fprintf(h, "enum %s", name ? name : "");
313 break;
314 case TYPE_STRUCT:
315 case TYPE_ENCAPSULATED_UNION:
316 if (!declonly && t->defined && !t->written) {
317 if (name) fprintf(h, "struct %s {\n", name);
318 else fprintf(h, "struct {\n");
319 t->written = TRUE;
320 indentation++;
321 if (type_get_type(t) != TYPE_STRUCT)
322 write_fields(h, type_encapsulated_union_get_fields(t));
323 else
324 write_fields(h, type_struct_get_fields(t));
325 indent(h, -1);
326 fprintf(h, "}");
327 }
328 else fprintf(h, "struct %s", name ? name : "");
329 break;
330 case TYPE_UNION:
331 if (!declonly && t->defined && !t->written) {
332 if (t->name) fprintf(h, "union %s {\n", t->name);
333 else fprintf(h, "union {\n");
334 t->written = TRUE;
335 indentation++;
336 write_fields(h, type_union_get_cases(t));
337 indent(h, -1);
338 fprintf(h, "}");
339 }
340 else fprintf(h, "union %s", t->name ? t->name : "");
341 break;
342 case TYPE_POINTER:
343 write_type_left(h, type_pointer_get_ref(t), name_type, declonly);
344 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
345 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
346 break;
347 case TYPE_ARRAY:
348 if (t->name && type_array_is_decl_as_ptr(t))
349 fprintf(h, "%s", t->name);
350 else
351 {
352 write_type_left(h, type_array_get_element(t), name_type, declonly);
353 if (type_array_is_decl_as_ptr(t))
354 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
355 }
356 break;
357 case TYPE_BASIC:
358 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
359 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
360 type_basic_get_type(t) != TYPE_BASIC_HYPER)
361 {
362 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
363 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
364 }
365 switch (type_basic_get_type(t))
366 {
367 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
368 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
369 case TYPE_BASIC_INT: fprintf(h, "int"); break;
370 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
371 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
372 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
373 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
374 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
375 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
376 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
377 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
378 case TYPE_BASIC_INT32:
379 if (type_basic_get_sign(t) > 0)
380 fprintf(h, "ULONG");
381 else
382 fprintf(h, "LONG");
383 break;
384 case TYPE_BASIC_INT64:
385 if (type_basic_get_sign(t) > 0)
386 fprintf(h, "UINT64");
387 else
388 fprintf(h, "INT64");
389 break;
390 case TYPE_BASIC_HYPER:
391 if (type_basic_get_sign(t) > 0)
392 fprintf(h, "MIDL_uhyper");
393 else
394 fprintf(h, "hyper");
395 break;
396 }
397 break;
398 case TYPE_INTERFACE:
399 case TYPE_MODULE:
400 case TYPE_COCLASS:
401 fprintf(h, "%s", t->name);
402 break;
403 case TYPE_VOID:
404 fprintf(h, "void");
405 break;
406 case TYPE_BITFIELD:
407 write_type_left(h, type_bitfield_get_field(t), name_type, declonly);
408 break;
409 case TYPE_ALIAS:
410 case TYPE_FUNCTION:
411 /* handled elsewhere */
412 assert(0);
413 break;
414 }
415 }
416 }
417
418 void write_type_right(FILE *h, type_t *t, int is_field)
419 {
420 if (!h) return;
421
422 switch (type_get_type(t))
423 {
424 case TYPE_ARRAY:
425 if (!type_array_is_decl_as_ptr(t))
426 {
427 if (is_conformant_array(t))
428 {
429 fprintf(h, "[%s]", is_field ? "1" : "");
430 t = type_array_get_element(t);
431 }
432 for ( ;
433 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
434 t = type_array_get_element(t))
435 fprintf(h, "[%u]", type_array_get_dim(t));
436 }
437 break;
438 case TYPE_BITFIELD:
439 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
440 break;
441 case TYPE_VOID:
442 case TYPE_BASIC:
443 case TYPE_ENUM:
444 case TYPE_STRUCT:
445 case TYPE_ENCAPSULATED_UNION:
446 case TYPE_UNION:
447 case TYPE_ALIAS:
448 case TYPE_MODULE:
449 case TYPE_COCLASS:
450 case TYPE_FUNCTION:
451 case TYPE_INTERFACE:
452 case TYPE_POINTER:
453 break;
454 }
455 }
456
457 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
458 {
459 type_t *pt = NULL;
460 int ptr_level = 0;
461
462 if (!h) return;
463
464 if (t) {
465 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
466 ;
467
468 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
469 int i;
470 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
471 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
472 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
473 write_type_left(h, type_function_get_rettype(pt), NAME_DEFAULT, declonly);
474 fputc(' ', h);
475 if (ptr_level) fputc('(', h);
476 if (callconv) fprintf(h, "%s ", callconv);
477 for (i = 0; i < ptr_level; i++)
478 fputc('*', h);
479 } else
480 write_type_left(h, t, NAME_DEFAULT, declonly);
481 }
482
483 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
484
485 if (t) {
486 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
487 const var_list_t *args = type_function_get_args(pt);
488
489 if (ptr_level) fputc(')', h);
490 fputc('(', h);
491 if (args)
492 write_args(h, args, NULL, 0, FALSE);
493 else
494 fprintf(h, "void");
495 fputc(')', h);
496 } else
497 write_type_right(h, t, is_field);
498 }
499 }
500
501 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
502 {
503 write_type_v(f, t, field, FALSE, name);
504 }
505
506 static void write_type_definition(FILE *f, type_t *t)
507 {
508 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
509 int save_written = t->written;
510
511 if(in_namespace) {
512 fprintf(f, "#ifdef __cplusplus\n");
513 fprintf(f, "} /* extern \"C\" */\n");
514 write_namespace_start(f, t->namespace);
515 }
516 indent(f, 0);
517 write_type_left(f, t, NAME_DEFAULT, FALSE);
518 fprintf(f, ";\n");
519 if(in_namespace) {
520 t->written = save_written;
521 write_namespace_end(f, t->namespace);
522 fprintf(f, "extern \"C\" {\n");
523 fprintf(f, "#else\n");
524 write_type_left(f, t, NAME_C, FALSE);
525 fprintf(f, ";\n");
526 fprintf(f, "#endif\n\n");
527 }
528 }
529
530 void write_type_decl(FILE *f, type_t *t, const char *name)
531 {
532 write_type_v(f, t, FALSE, TRUE, name);
533 }
534
535 void write_type_decl_left(FILE *f, type_t *t)
536 {
537 write_type_left(f, t, NAME_DEFAULT, TRUE);
538 }
539
540 static int user_type_registered(const char *name)
541 {
542 user_type_t *ut;
543 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
544 if (!strcmp(name, ut->name))
545 return 1;
546 return 0;
547 }
548
549 static int context_handle_registered(const char *name)
550 {
551 context_handle_t *ch;
552 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
553 if (!strcmp(name, ch->name))
554 return 1;
555 return 0;
556 }
557
558 static int generic_handle_registered(const char *name)
559 {
560 generic_handle_t *gh;
561 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
562 if (!strcmp(name, gh->name))
563 return 1;
564 return 0;
565 }
566
567 unsigned int get_context_handle_offset( const type_t *type )
568 {
569 context_handle_t *ch;
570 unsigned int index = 0;
571
572 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
573 {
574 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
575 else if (is_ptr( type )) type = type_pointer_get_ref( type );
576 else error( "internal error: %s is not a context handle\n", type->name );
577 }
578 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
579 {
580 if (!strcmp( type->name, ch->name )) return index;
581 index++;
582 }
583 error( "internal error: %s is not registered as a context handle\n", type->name );
584 return index;
585 }
586
587 unsigned int get_generic_handle_offset( const type_t *type )
588 {
589 generic_handle_t *gh;
590 unsigned int index = 0;
591
592 while (!is_attr( type->attrs, ATTR_HANDLE ))
593 {
594 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
595 else if (is_ptr( type )) type = type_pointer_get_ref( type );
596 else error( "internal error: %s is not a generic handle\n", type->name );
597 }
598 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
599 {
600 if (!strcmp( type->name, gh->name )) return index;
601 index++;
602 }
603 error( "internal error: %s is not registered as a generic handle\n", type->name );
604 return index;
605 }
606
607 /* check for types which require additional prototypes to be generated in the
608 * header */
609 void check_for_additional_prototype_types(const var_list_t *list)
610 {
611 const var_t *v;
612
613 if (!list) return;
614 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
615 {
616 type_t *type = v->type;
617 if (!type) continue;
618 for (;;) {
619 const char *name = type->name;
620 if (type->user_types_registered) break;
621 type->user_types_registered = 1;
622 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
623 if (!context_handle_registered(name))
624 {
625 context_handle_t *ch = xmalloc(sizeof(*ch));
626 ch->name = xstrdup(name);
627 list_add_tail(&context_handle_list, &ch->entry);
628 }
629 /* don't carry on parsing fields within this type */
630 break;
631 }
632 if ((type_get_type(type) != TYPE_BASIC ||
633 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
634 is_attr(type->attrs, ATTR_HANDLE)) {
635 if (!generic_handle_registered(name))
636 {
637 generic_handle_t *gh = xmalloc(sizeof(*gh));
638 gh->name = xstrdup(name);
639 list_add_tail(&generic_handle_list, &gh->entry);
640 }
641 /* don't carry on parsing fields within this type */
642 break;
643 }
644 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
645 if (!user_type_registered(name))
646 {
647 user_type_t *ut = xmalloc(sizeof *ut);
648 ut->name = xstrdup(name);
649 list_add_tail(&user_type_list, &ut->entry);
650 }
651 /* don't carry on parsing fields within this type as we are already
652 * using a wire marshaled type */
653 break;
654 }
655 else if (type_is_complete(type))
656 {
657 var_list_t *vars;
658 switch (type_get_type_detect_alias(type))
659 {
660 case TYPE_ENUM:
661 vars = type_enum_get_values(type);
662 break;
663 case TYPE_STRUCT:
664 vars = type_struct_get_fields(type);
665 break;
666 case TYPE_UNION:
667 vars = type_union_get_cases(type);
668 break;
669 default:
670 vars = NULL;
671 break;
672 }
673 check_for_additional_prototype_types(vars);
674 }
675
676 if (type_is_alias(type))
677 type = type_alias_get_aliasee(type);
678 else if (is_ptr(type))
679 type = type_pointer_get_ref(type);
680 else if (is_array(type))
681 type = type_array_get_element(type);
682 else
683 break;
684 }
685 }
686 }
687
688 static void write_user_types(FILE *header)
689 {
690 user_type_t *ut;
691 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
692 {
693 const char *name = ut->name;
694 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
695 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
696 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
697 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
698 }
699 }
700
701 static void write_context_handle_rundowns(FILE *header)
702 {
703 context_handle_t *ch;
704 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
705 {
706 const char *name = ch->name;
707 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
708 }
709 }
710
711 static void write_generic_handle_routines(FILE *header)
712 {
713 generic_handle_t *gh;
714 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
715 {
716 const char *name = gh->name;
717 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
718 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
719 }
720 }
721
722 static void write_typedef(FILE *header, type_t *type)
723 {
724 fprintf(header, "typedef ");
725 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
726 fprintf(header, ";\n");
727 }
728
729 int is_const_decl(const var_t *var)
730 {
731 const type_t *t;
732 /* strangely, MIDL accepts a const attribute on any pointer in the
733 * declaration to mean that data isn't being instantiated. this appears
734 * to be a bug, but there is no benefit to being incompatible with MIDL,
735 * so we'll do the same thing */
736 for (t = var->type; ; )
737 {
738 if (is_attr(t->attrs, ATTR_CONST))
739 return TRUE;
740 else if (is_ptr(t))
741 t = type_pointer_get_ref(t);
742 else break;
743 }
744 return FALSE;
745 }
746
747 static void write_declaration(FILE *header, const var_t *v)
748 {
749 if (is_const_decl(v) && v->eval)
750 {
751 fprintf(header, "#define %s (", v->name);
752 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
753 fprintf(header, ")\n\n");
754 }
755 else
756 {
757 switch (v->stgclass)
758 {
759 case STG_NONE:
760 case STG_REGISTER: /* ignored */
761 break;
762 case STG_STATIC:
763 fprintf(header, "static ");
764 break;
765 case STG_EXTERN:
766 fprintf(header, "extern ");
767 break;
768 }
769 write_type_def_or_decl(header, v->type, FALSE, v->name);
770 fprintf(header, ";\n\n");
771 }
772 }
773
774 static void write_library(FILE *header, const typelib_t *typelib)
775 {
776 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
777 fprintf(header, "\n");
778 write_guid(header, "LIBID", typelib->name, uuid);
779 fprintf(header, "\n");
780 }
781
782
783 const type_t* get_explicit_generic_handle_type(const var_t* var)
784 {
785 const type_t *t;
786 for (t = var->type;
787 is_ptr(t) || type_is_alias(t);
788 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
789 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
790 is_attr(t->attrs, ATTR_HANDLE))
791 return t;
792 return NULL;
793 }
794
795 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
796 unsigned char *explicit_fc, unsigned char *implicit_fc )
797 {
798 const var_t *var;
799 const var_list_t *args = type_get_function_args( func->type );
800
801 *explicit_fc = *implicit_fc = 0;
802 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
803 {
804 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
805 if (type_get_type( var->type ) == TYPE_BASIC && type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
806 {
807 *explicit_fc = RPC_FC_BIND_PRIMITIVE;
808 return var;
809 }
810 if (get_explicit_generic_handle_type( var ))
811 {
812 *explicit_fc = RPC_FC_BIND_GENERIC;
813 return var;
814 }
815 if (is_context_handle( var->type ))
816 {
817 *explicit_fc = RPC_FC_BIND_CONTEXT;
818 return var;
819 }
820 }
821
822 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
823 {
824 if (type_get_type( var->type ) == TYPE_BASIC &&
825 type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
826 *implicit_fc = RPC_FC_BIND_PRIMITIVE;
827 else
828 *implicit_fc = RPC_FC_BIND_GENERIC;
829 return var;
830 }
831
832 *implicit_fc = RPC_FC_AUTO_HANDLE;
833 return NULL;
834 }
835
836 int has_out_arg_or_return(const var_t *func)
837 {
838 const var_t *var;
839
840 if (!is_void(type_function_get_rettype(func->type)))
841 return 1;
842
843 if (!type_get_function_args(func->type))
844 return 0;
845
846 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
847 if (is_attr(var->attrs, ATTR_OUT))
848 return 1;
849
850 return 0;
851 }
852
853
854 /********** INTERFACES **********/
855
856 int is_object(const type_t *iface)
857 {
858 const attr_t *attr;
859 if (type_is_defined(iface) && type_iface_get_inherit(iface))
860 return 1;
861 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
862 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
863 return 0;
864 }
865
866 int is_local(const attr_list_t *a)
867 {
868 return is_attr(a, ATTR_LOCAL);
869 }
870
871 const var_t *is_callas(const attr_list_t *a)
872 {
873 return get_attrp(a, ATTR_CALLAS);
874 }
875
876 static int is_inherited_method(const type_t *iface, const var_t *func)
877 {
878 while ((iface = type_iface_get_inherit(iface)))
879 {
880 const statement_t *stmt;
881 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
882 {
883 const var_t *funccmp = stmt->u.var;
884
885 if (!is_callas(func->attrs))
886 {
887 char inherit_name[256];
888 /* compare full name including property prefix */
889 strcpy(inherit_name, get_name(funccmp));
890 if (!strcmp(inherit_name, get_name(func))) return 1;
891 }
892 }
893 }
894
895 return 0;
896 }
897
898 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
899 {
900 if (iface == child)
901 return 0;
902
903 do
904 {
905 const statement_t *stmt;
906 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
907 {
908 const var_t *funccmp = stmt->u.var;
909
910 if (!is_callas(func->attrs))
911 {
912 char inherit_name[256];
913 /* compare full name including property prefix */
914 strcpy(inherit_name, get_name(funccmp));
915 if (!strcmp(inherit_name, get_name(func))) return 1;
916 }
917 }
918 }
919 while ((child = type_iface_get_inherit(child)) && child != iface);
920
921 return 0;
922 }
923
924 static int is_aggregate_return(const var_t *func)
925 {
926 enum type_type type = type_get_type(type_function_get_rettype(func->type));
927 return type == TYPE_STRUCT || type == TYPE_UNION ||
928 type == TYPE_COCLASS || type == TYPE_INTERFACE;
929 }
930
931 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
932 {
933 static char buff[255];
934 if (is_inherited_method(iface, func))
935 sprintf(buff, "%s_%s", iface->name, get_name(func));
936 else
937 sprintf(buff, "%s", get_name(func));
938 return buff;
939 }
940
941 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
942 {
943 const statement_t *stmt;
944 int first_iface = 1;
945
946 if (type_iface_get_inherit(iface))
947 write_method_macro(header, type_iface_get_inherit(iface), child, name);
948
949 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
950 {
951 const var_t *func = stmt->u.var;
952
953 if (first_iface)
954 {
955 fprintf(header, "/*** %s methods ***/\n", iface->name);
956 first_iface = 0;
957 }
958
959 if (is_override_method(iface, child, func))
960 continue;
961
962 if (!is_callas(func->attrs) && !is_aggregate_return(func)) {
963 const var_t *arg;
964
965 fprintf(header, "#define %s_%s(This", name, get_name(func));
966 if (type_get_function_args(func->type))
967 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
968 fprintf(header, ",%s", arg->name);
969 fprintf(header, ") ");
970
971 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
972 if (type_get_function_args(func->type))
973 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
974 fprintf(header, ",%s", arg->name);
975 fprintf(header, ")\n");
976 }
977 }
978 }
979
980 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
981 {
982 const var_t *arg;
983 int count = 0;
984
985 if (do_indent)
986 {
987 indentation++;
988 indent(h, 0);
989 }
990 if (method == 1) {
991 fprintf(h, "%s* This", name);
992 count++;
993 }
994 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
995 if (count) {
996 if (do_indent)
997 {
998 fprintf(h, ",\n");
999 indent(h, 0);
1000 }
1001 else fprintf(h, ",");
1002 }
1003 write_type_decl(h, arg->type, arg->name);
1004 if (method == 2) {
1005 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1006 if (expr) {
1007 const var_t *tail_arg;
1008
1009 /* Output default value only if all following arguments also have default value. */
1010 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1011 if(tail_arg == arg) {
1012 fprintf(h, " = ");
1013 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1014 break;
1015 }
1016 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1017 break;
1018 }
1019 }
1020 }
1021 count++;
1022 }
1023 if (do_indent) indentation--;
1024 }
1025
1026 static void write_cpp_method_def(FILE *header, const type_t *iface)
1027 {
1028 const statement_t *stmt;
1029
1030 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1031 {
1032 const var_t *func = stmt->u.var;
1033 if (!is_callas(func->attrs)) {
1034 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
1035 if (!callconv) callconv = "STDMETHODCALLTYPE";
1036 indent(header, 0);
1037 fprintf(header, "virtual ");
1038 write_type_decl_left(header, type_function_get_rettype(func->type));
1039 fprintf(header, " %s %s(\n", callconv, get_name(func));
1040 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
1041 fprintf(header, ") = 0;\n");
1042 fprintf(header, "\n");
1043 }
1044 }
1045 }
1046
1047 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1048 {
1049 const statement_t *stmt;
1050 int first_iface = 1;
1051
1052 if (type_iface_get_inherit(iface))
1053 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1054
1055 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1056 {
1057 const var_t *func = stmt->u.var;
1058
1059 if (first_iface)
1060 {
1061 fprintf(header, "/*** %s methods ***/\n", iface->name);
1062 first_iface = 0;
1063 }
1064
1065 if (is_override_method(iface, child, func))
1066 continue;
1067
1068 if (!is_callas(func->attrs)) {
1069 const var_t *arg;
1070
1071 fprintf(header, "FORCEINLINE ");
1072 write_type_decl_left(header, type_function_get_rettype(func->type));
1073 fprintf(header, " %s_%s(", name, get_name(func));
1074 write_args(header, type_get_function_args(func->type), name, 1, FALSE);
1075 fprintf(header, ") {\n");
1076 ++indentation;
1077 if (!is_aggregate_return(func)) {
1078 indent(header, 0);
1079 fprintf(header, "%sThis->lpVtbl->%s(This",
1080 is_void(type_function_get_rettype(func->type)) ? "" : "return ",
1081 get_vtbl_entry_name(iface, func));
1082 } else {
1083 indent(header, 0);
1084 write_type_decl_left(header, type_function_get_rettype(func->type));
1085 fprintf(header, " __ret;\n");
1086 indent(header, 0);
1087 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1088 }
1089 if (type_get_function_args(func->type))
1090 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
1091 fprintf(header, ",%s", arg->name);
1092 fprintf(header, ");\n");
1093 --indentation;
1094 fprintf(header, "}\n");
1095 }
1096 }
1097 }
1098
1099 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1100 {
1101 const statement_t *stmt;
1102 int first_iface = 1;
1103
1104 if (type_iface_get_inherit(iface))
1105 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1106 else if (type_iface_get_stmts(iface) == NULL)
1107 {
1108 fprintf(header, "#ifndef __cplusplus\n");
1109 indent(header, 0);
1110 fprintf(header, "char dummy;\n");
1111 fprintf(header, "#endif\n");
1112 fprintf(header, "\n");
1113 return;
1114 }
1115
1116 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1117 {
1118 const var_t *func = stmt->u.var;
1119 if (first_iface) {
1120 indent(header, 0);
1121 fprintf(header, "/*** %s methods ***/\n", iface->name);
1122 first_iface = 0;
1123 }
1124 if (!is_callas(func->attrs)) {
1125 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
1126 if (!callconv) callconv = "STDMETHODCALLTYPE";
1127 indent(header, 0);
1128 write_type_decl_left(header, type_function_get_rettype(func->type));
1129 if (is_aggregate_return(func))
1130 fprintf(header, " *");
1131 if (is_inherited_method(iface, func))
1132 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1133 else
1134 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1135 ++indentation;
1136 indent(header, 0);
1137 fprintf(header, "%s *This", name);
1138 if (is_aggregate_return(func)) {
1139 fprintf(header, ",\n");
1140 indent(header, 0);
1141 write_type_decl_left(header, type_function_get_rettype(func->type));
1142 fprintf(header, " *__ret");
1143 }
1144 --indentation;
1145 if (type_get_function_args(func->type)) {
1146 fprintf(header, ",\n");
1147 write_args(header, type_get_function_args(func->type), name, 0, TRUE);
1148 }
1149 fprintf(header, ");\n");
1150 fprintf(header, "\n");
1151 }
1152 }
1153 }
1154
1155 static void write_c_method_def(FILE *header, const type_t *iface)
1156 {
1157 do_write_c_method_def(header, iface, iface->c_name);
1158 }
1159
1160 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1161 {
1162 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1163 }
1164
1165 static void write_method_proto(FILE *header, const type_t *iface)
1166 {
1167 const statement_t *stmt;
1168
1169 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1170 {
1171 const var_t *func = stmt->u.var;
1172
1173 if (!is_local(func->attrs)) {
1174 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
1175 if (!callconv) callconv = "STDMETHODCALLTYPE";
1176 /* proxy prototype */
1177 write_type_decl_left(header, type_function_get_rettype(func->type));
1178 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1179 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
1180 fprintf(header, ");\n");
1181 /* stub prototype */
1182 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1183 fprintf(header, " IRpcStubBuffer* This,\n");
1184 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1185 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1186 fprintf(header, " DWORD* pdwStubPhase);\n");
1187 }
1188 }
1189 }
1190
1191 static void write_locals(FILE *fp, const type_t *iface, int body)
1192 {
1193 static const char comment[]
1194 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1195 const statement_t *stmt;
1196
1197 if (!is_object(iface))
1198 return;
1199
1200 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1201 const var_t *func = stmt->u.var;
1202 const var_t *cas = is_callas(func->attrs);
1203
1204 if (cas) {
1205 const statement_t *stmt2 = NULL;
1206 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1207 if (!strcmp(stmt2->u.var->name, cas->name))
1208 break;
1209 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1210 const var_t *m = stmt2->u.var;
1211 /* proxy prototype - use local prototype */
1212 write_type_decl_left(fp, type_function_get_rettype(m->type));
1213 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1214 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
1215 fprintf(fp, ")");
1216 if (body) {
1217 type_t *rt = type_function_get_rettype(m->type);
1218 fprintf(fp, "\n{\n");
1219 fprintf(fp, " %s\n", comment);
1220 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
1221 fprintf(fp, " return E_NOTIMPL;\n");
1222 else if (type_get_type(rt) != TYPE_VOID) {
1223 fprintf(fp, " ");
1224 write_type_decl(fp, rt, "rv");
1225 fprintf(fp, ";\n");
1226 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1227 fprintf(fp, " return rv;\n");
1228 }
1229 fprintf(fp, "}\n\n");
1230 }
1231 else
1232 fprintf(fp, ";\n");
1233 /* stub prototype - use remotable prototype */
1234 write_type_decl_left(fp, type_function_get_rettype(func->type));
1235 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1236 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
1237 fprintf(fp, ")");
1238 if (body)
1239 /* Remotable methods must all return HRESULTs. */
1240 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1241 else
1242 fprintf(fp, ";\n");
1243 }
1244 else
1245 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1246 }
1247 }
1248 }
1249
1250 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1251 {
1252 const statement_t *stmt;
1253 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1254 {
1255 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1256 write_locals(local_stubs, stmt->u.type, TRUE);
1257 }
1258 }
1259
1260 void write_local_stubs(const statement_list_t *stmts)
1261 {
1262 FILE *local_stubs;
1263
1264 if (!local_stubs_name) return;
1265
1266 local_stubs = fopen(local_stubs_name, "w");
1267 if (!local_stubs) {
1268 error("Could not open %s for output\n", local_stubs_name);
1269 return;
1270 }
1271 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1272 fprintf(local_stubs, "#include <objbase.h>\n");
1273 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1274
1275 write_local_stubs_stmts(local_stubs, stmts);
1276
1277 fclose(local_stubs);
1278 }
1279
1280 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1281 {
1282 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
1283
1284 if (!callconv) callconv = "__cdecl";
1285 /* FIXME: do we need to handle call_as? */
1286 write_type_decl_left(header, type_function_get_rettype(fun->type));
1287 fprintf(header, " %s ", callconv);
1288 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1289 if (type_get_function_args(fun->type))
1290 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
1291 else
1292 fprintf(header, " void");
1293 fprintf(header, ");\n\n");
1294 }
1295
1296 static void write_forward(FILE *header, type_t *iface)
1297 {
1298 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1299 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1300 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1301 fprintf(header, "#ifdef __cplusplus\n");
1302 write_namespace_start(header, iface->namespace);
1303 write_line(header, 0, "interface %s;", iface->name);
1304 write_namespace_end(header, iface->namespace);
1305 fprintf(header, "#endif /* __cplusplus */\n");
1306 fprintf(header, "#endif\n\n" );
1307 }
1308
1309 static void write_com_interface_start(FILE *header, const type_t *iface)
1310 {
1311 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1312 fprintf(header, "/*****************************************************************************\n");
1313 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1314 fprintf(header, " */\n");
1315 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1316 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1317 }
1318
1319 static void write_com_interface_end(FILE *header, type_t *iface)
1320 {
1321 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1322 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1323 type_t *type;
1324
1325 if (uuid)
1326 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1327
1328 /* C++ interface */
1329 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1330 if (!is_global_namespace(iface->namespace)) {
1331 write_line(header, 0, "} /* extern \"C\" */");
1332 write_namespace_start(header, iface->namespace);
1333 }
1334 if (uuid) {
1335 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1336 indent(header, 0);
1337 }else {
1338 indent(header, 0);
1339 fprintf(header, "interface ");
1340 }
1341 if (type_iface_get_inherit(iface))
1342 {
1343 fprintf(header, "%s : public %s\n", iface->name,
1344 type_iface_get_inherit(iface)->name);
1345 write_line(header, 1, "{");
1346 }
1347 else
1348 {
1349 fprintf(header, "%s\n", iface->name);
1350 write_line(header, 1, "{\n");
1351 write_line(header, 0, "BEGIN_INTERFACE\n");
1352 }
1353 /* dispinterfaces don't have real functions, so don't write C++ functions for
1354 * them */
1355 if (!dispinterface)
1356 write_cpp_method_def(header, iface);
1357 if (!type_iface_get_inherit(iface))
1358 write_line(header, 0, "END_INTERFACE\n");
1359 write_line(header, -1, "};");
1360 if (!is_global_namespace(iface->namespace)) {
1361 write_namespace_end(header, iface->namespace);
1362 write_line(header, 0, "extern \"C\" {");
1363 }
1364 if (uuid)
1365 write_uuid_decl(header, iface, uuid);
1366 fprintf(header, "#else\n");
1367 /* C interface */
1368 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1369 write_line(header, 0, "BEGIN_INTERFACE\n");
1370 if (dispinterface)
1371 write_c_disp_method_def(header, iface);
1372 else
1373 write_c_method_def(header, iface);
1374 write_line(header, 0, "END_INTERFACE");
1375 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1376 fprintf(header, "interface %s {\n", iface->c_name);
1377 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1378 fprintf(header, "};\n\n");
1379 fprintf(header, "#ifdef COBJMACROS\n");
1380 /* dispinterfaces don't have real functions, so don't write macros for them,
1381 * only for the interface this interface inherits from, i.e. IDispatch */
1382 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1383 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1384 write_method_macro(header, type, type, iface->c_name);
1385 fprintf(header, "#else\n");
1386 write_inline_wrappers(header, type, type, iface->c_name);
1387 fprintf(header, "#endif\n");
1388 fprintf(header, "#endif\n");
1389 fprintf(header, "\n");
1390 fprintf(header, "#endif\n");
1391 fprintf(header, "\n");
1392 /* dispinterfaces don't have real functions, so don't write prototypes for
1393 * them */
1394 if (!dispinterface && !winrt_mode)
1395 {
1396 write_method_proto(header, iface);
1397 write_locals(header, iface, FALSE);
1398 fprintf(header, "\n");
1399 }
1400 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->c_name, dispinterface ? "DISP" : "");
1401 }
1402
1403 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1404 {
1405 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1406 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1407
1408 fprintf(header, "/*****************************************************************************\n");
1409 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1410 fprintf(header, " */\n");
1411 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1412 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1413 if (var)
1414 {
1415 fprintf(header, "extern ");
1416 write_type_decl( header, var->type, var->name );
1417 fprintf(header, ";\n");
1418 }
1419 if (old_names)
1420 {
1421 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1422 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1423 }
1424 else
1425 {
1426 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1427 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1428 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1429 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1430 }
1431 }
1432
1433 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1434 {
1435 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1436 }
1437
1438 static void write_coclass(FILE *header, type_t *cocl)
1439 {
1440 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1441
1442 fprintf(header, "/*****************************************************************************\n");
1443 fprintf(header, " * %s coclass\n", cocl->name);
1444 fprintf(header, " */\n\n");
1445 if (uuid)
1446 write_guid(header, "CLSID", cocl->name, uuid);
1447 fprintf(header, "\n#ifdef __cplusplus\n");
1448 if (uuid)
1449 {
1450 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1451 write_uuid_decl(header, cocl, uuid);
1452 }
1453 else
1454 {
1455 fprintf(header, "class %s;\n", cocl->name);
1456 }
1457 fprintf(header, "#endif\n");
1458 fprintf(header, "\n");
1459 }
1460
1461 static void write_coclass_forward(FILE *header, type_t *cocl)
1462 {
1463 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1464 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1465 fprintf(header, "#ifdef __cplusplus\n");
1466 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1467 fprintf(header, "#else\n");
1468 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1469 fprintf(header, "#endif /* defined __cplusplus */\n");
1470 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1471 }
1472
1473 static void write_import(FILE *header, const char *fname)
1474 {
1475 char *hname, *p;
1476
1477 hname = dup_basename(fname, ".idl");
1478 p = hname + strlen(hname) - 2;
1479 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1480
1481 fprintf(header, "#include <%s>\n", hname);
1482 free(hname);
1483 }
1484
1485 static void write_imports(FILE *header, const statement_list_t *stmts)
1486 {
1487 const statement_t *stmt;
1488 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1489 {
1490 switch (stmt->type)
1491 {
1492 case STMT_TYPE:
1493 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1494 write_imports(header, type_iface_get_stmts(stmt->u.type));
1495 break;
1496 case STMT_TYPEREF:
1497 case STMT_IMPORTLIB:
1498 /* not included in header */
1499 break;
1500 case STMT_IMPORT:
1501 write_import(header, stmt->u.str);
1502 break;
1503 case STMT_TYPEDEF:
1504 case STMT_MODULE:
1505 case STMT_CPPQUOTE:
1506 case STMT_PRAGMA:
1507 case STMT_DECLARATION:
1508 /* not processed here */
1509 break;
1510 case STMT_LIBRARY:
1511 write_imports(header, stmt->u.lib->stmts);
1512 break;
1513 }
1514 }
1515 }
1516
1517 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1518 {
1519 const statement_t *stmt;
1520 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1521 {
1522 switch (stmt->type)
1523 {
1524 case STMT_TYPE:
1525 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1526 {
1527 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1528 write_forward(header, stmt->u.type);
1529 }
1530 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1531 write_coclass_forward(header, stmt->u.type);
1532 break;
1533 case STMT_TYPEREF:
1534 case STMT_IMPORTLIB:
1535 /* not included in header */
1536 break;
1537 case STMT_IMPORT:
1538 case STMT_TYPEDEF:
1539 case STMT_MODULE:
1540 case STMT_CPPQUOTE:
1541 case STMT_PRAGMA:
1542 case STMT_DECLARATION:
1543 /* not processed here */
1544 break;
1545 case STMT_LIBRARY:
1546 write_forward_decls(header, stmt->u.lib->stmts);
1547 break;
1548 }
1549 }
1550 }
1551
1552 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1553 {
1554 const statement_t *stmt;
1555 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1556 {
1557 switch (stmt->type)
1558 {
1559 case STMT_TYPE:
1560 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1561 {
1562 type_t *iface = stmt->u.type;
1563 if (is_object(iface)) is_object_interface++;
1564 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1565 {
1566 write_com_interface_start(header, iface);
1567 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1568 write_com_interface_end(header, iface);
1569 }
1570 else
1571 {
1572 write_rpc_interface_start(header, iface);
1573 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1574 write_rpc_interface_end(header, iface);
1575 }
1576 if (is_object(iface)) is_object_interface--;
1577 }
1578 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1579 write_coclass(header, stmt->u.type);
1580 else
1581 {
1582 write_type_definition(header, stmt->u.type);
1583 }
1584 break;
1585 case STMT_TYPEREF:
1586 /* FIXME: shouldn't write out forward declarations for undefined
1587 * interfaces but a number of our IDL files depend on this */
1588 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1589 write_forward(header, stmt->u.type);
1590 break;
1591 case STMT_IMPORTLIB:
1592 case STMT_MODULE:
1593 case STMT_PRAGMA:
1594 /* not included in header */
1595 break;
1596 case STMT_IMPORT:
1597 /* not processed here */
1598 break;
1599 case STMT_TYPEDEF:
1600 {
1601 const type_list_t *type_entry = stmt->u.type_list;
1602 for (; type_entry; type_entry = type_entry->next)
1603 write_typedef(header, type_entry->type);
1604 break;
1605 }
1606 case STMT_LIBRARY:
1607 write_library(header, stmt->u.lib);
1608 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1609 break;
1610 case STMT_CPPQUOTE:
1611 fprintf(header, "%s\n", stmt->u.str);
1612 break;
1613 case STMT_DECLARATION:
1614 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1615 {
1616 if (!ignore_funcs)
1617 {
1618 int prefixes_differ = strcmp(prefix_client, prefix_server);
1619
1620 if (prefixes_differ)
1621 {
1622 fprintf(header, "/* client prototype */\n");
1623 write_function_proto(header, iface, stmt->u.var, prefix_client);
1624 fprintf(header, "/* server prototype */\n");
1625 }
1626 write_function_proto(header, iface, stmt->u.var, prefix_server);
1627 }
1628 }
1629 else
1630 write_declaration(header, stmt->u.var);
1631 break;
1632 }
1633 }
1634 }
1635
1636 void write_header(const statement_list_t *stmts)
1637 {
1638 FILE *header;
1639
1640 if (!do_header) return;
1641
1642 if(!(header = fopen(header_name, "w"))) {
1643 error("Could not open %s for output\n", header_name);
1644 return;
1645 }
1646 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1647
1648 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1649 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1650 fprintf(header, "#endif\n\n");
1651
1652 fprintf(header, "#ifdef __REACTOS__\n");
1653 fprintf(header, "#define WIN32_LEAN_AND_MEAN\n");
1654 fprintf(header, "#endif\n\n");
1655
1656 fprintf(header, "#include <rpc.h>\n" );
1657 fprintf(header, "#include <rpcndr.h>\n\n" );
1658
1659 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
1660 fprintf(header, "#include <windows.h>\n");
1661 fprintf(header, "#include <ole2.h>\n");
1662 fprintf(header, "#endif\n\n");
1663
1664 fprintf(header, "#ifndef __%s__\n", header_token);
1665 fprintf(header, "#define __%s__\n\n", header_token);
1666
1667 fprintf(header, "/* Forward declarations */\n\n");
1668 write_forward_decls(header, stmts);
1669
1670 fprintf(header, "/* Headers for imported files */\n\n");
1671 write_imports(header, stmts);
1672 fprintf(header, "\n");
1673 start_cplusplus_guard(header);
1674
1675 write_header_stmts(header, stmts, NULL, FALSE);
1676
1677 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1678 fprintf(header, "\n");
1679 write_user_types(header);
1680 write_generic_handle_routines(header);
1681 write_context_handle_rundowns(header);
1682 fprintf(header, "\n");
1683 fprintf(header, "/* End additional prototypes */\n");
1684 fprintf(header, "\n");
1685
1686 end_cplusplus_guard(header);
1687 fprintf(header, "#endif /* __%s__ */\n", header_token);
1688
1689 fclose(header);
1690 }