Sync to wine-0.9.61:
[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
38 typedef struct _user_type_t generic_handle_t;
39
40 static int indentation = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
44
45 static void indent(FILE *h, int delta)
46 {
47 int c;
48 if (delta < 0) indentation += delta;
49 for (c=0; c<indentation; c++) fprintf(h, " ");
50 if (delta > 0) indentation += delta;
51 }
52
53 int is_ptrchain_attr(const var_t *var, enum attr_type t)
54 {
55 if (is_attr(var->attrs, t))
56 return 1;
57 else
58 {
59 type_t *type = var->type;
60 for (;;)
61 {
62 if (is_attr(type->attrs, t))
63 return 1;
64 else if (type->kind == TKIND_ALIAS)
65 type = type->orig;
66 else if (is_ptr(type))
67 type = type->ref;
68 else return 0;
69 }
70 }
71 }
72
73 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
74 {
75 const type_t *t = type;
76 for (;;)
77 {
78 if (is_attr(t->attrs, attr))
79 return 1;
80 else if (t->kind == TKIND_ALIAS)
81 t = t->orig;
82 else return 0;
83 }
84 }
85
86 int is_attr(const attr_list_t *list, enum attr_type t)
87 {
88 const attr_t *attr;
89 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
90 if (attr->type == t) return 1;
91 return 0;
92 }
93
94 void *get_attrp(const attr_list_t *list, enum attr_type t)
95 {
96 const attr_t *attr;
97 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
98 if (attr->type == t) return attr->u.pval;
99 return NULL;
100 }
101
102 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
103 {
104 const attr_t *attr;
105 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
106 if (attr->type == t) return attr->u.ival;
107 return 0;
108 }
109
110 int is_void(const type_t *t)
111 {
112 if (!t->type && !t->ref) return 1;
113 return 0;
114 }
115
116 int is_conformant_array(const type_t *t)
117 {
118 return t->type == RPC_FC_CARRAY
119 || t->type == RPC_FC_CVARRAY
120 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
121 }
122
123 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
124 {
125 if (!uuid) return;
126 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
127 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
128 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
129 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
130 uuid->Data4[6], uuid->Data4[7]);
131 }
132
133 void write_name(FILE *h, const var_t *v)
134 {
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 fprintf(h, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 fprintf(h, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 fprintf(h, "putref_" );
141 fprintf(h, "%s", v->name);
142 }
143
144 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
145 {
146 fprintf(h, "%s", prefix);
147 write_name(h, v);
148 }
149
150 static void write_field(FILE *h, var_t *v)
151 {
152 if (!v) return;
153 if (v->type) {
154 const char *name = v->name;
155 if (name == NULL) {
156 switch (v->type->type) {
157 case RPC_FC_STRUCT:
158 case RPC_FC_CVSTRUCT:
159 case RPC_FC_CPSTRUCT:
160 case RPC_FC_CSTRUCT:
161 case RPC_FC_PSTRUCT:
162 case RPC_FC_BOGUS_STRUCT:
163 case RPC_FC_ENCAPSULATED_UNION:
164 name = "DUMMYSTRUCTNAME";
165 break;
166 case RPC_FC_NON_ENCAPSULATED_UNION:
167 name = "DUMMYUNIONNAME";
168 break;
169 default:
170 /* ? */
171 break;
172 }
173 }
174 indent(h, 0);
175 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
176 fprintf(h, ";\n");
177 }
178 }
179
180 static void write_fields(FILE *h, var_list_t *fields)
181 {
182 var_t *v;
183 if (!fields) return;
184 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
185 }
186
187 static void write_enums(FILE *h, var_list_t *enums)
188 {
189 var_t *v;
190 if (!enums) return;
191 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
192 {
193 if (v->name) {
194 indent(h, 0);
195 write_name(h, v);
196 if (v->eval) {
197 fprintf(h, " = ");
198 write_expr(h, v->eval, 0, 1, NULL, NULL);
199 }
200 }
201 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
202 }
203 fprintf(h, "\n");
204 }
205
206 int needs_space_after(type_t *t)
207 {
208 return (t->kind == TKIND_ALIAS
209 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
210 }
211
212 void write_type_left(FILE *h, type_t *t, int declonly)
213 {
214 if (!h) return;
215
216 if (is_attr(t->attrs, ATTR_CONST) &&
217 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
218 fprintf(h, "const ");
219
220 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
221 else if (t->declarray) write_type_left(h, t->ref, declonly);
222 else {
223 if (t->sign > 0) fprintf(h, "signed ");
224 else if (t->sign < 0) fprintf(h, "unsigned ");
225 switch (t->type) {
226 case RPC_FC_ENUM16:
227 case RPC_FC_ENUM32:
228 if (!declonly && t->defined && !t->written && !t->ignore) {
229 if (t->name) fprintf(h, "enum %s {\n", t->name);
230 else fprintf(h, "enum {\n");
231 t->written = TRUE;
232 indentation++;
233 write_enums(h, t->fields_or_args);
234 indent(h, -1);
235 fprintf(h, "}");
236 }
237 else fprintf(h, "enum %s", t->name ? t->name : "");
238 break;
239 case RPC_FC_STRUCT:
240 case RPC_FC_CVSTRUCT:
241 case RPC_FC_CPSTRUCT:
242 case RPC_FC_CSTRUCT:
243 case RPC_FC_PSTRUCT:
244 case RPC_FC_BOGUS_STRUCT:
245 case RPC_FC_ENCAPSULATED_UNION:
246 if (!declonly && t->defined && !t->written && !t->ignore) {
247 if (t->name) fprintf(h, "struct %s {\n", t->name);
248 else fprintf(h, "struct {\n");
249 t->written = TRUE;
250 indentation++;
251 write_fields(h, t->fields_or_args);
252 indent(h, -1);
253 fprintf(h, "}");
254 }
255 else fprintf(h, "struct %s", t->name ? t->name : "");
256 break;
257 case RPC_FC_NON_ENCAPSULATED_UNION:
258 if (!declonly && t->defined && !t->written && !t->ignore) {
259 if (t->name) fprintf(h, "union %s {\n", t->name);
260 else fprintf(h, "union {\n");
261 t->written = TRUE;
262 indentation++;
263 write_fields(h, t->fields_or_args);
264 indent(h, -1);
265 fprintf(h, "}");
266 }
267 else fprintf(h, "union %s", t->name ? t->name : "");
268 break;
269 case RPC_FC_RP:
270 case RPC_FC_UP:
271 case RPC_FC_FP:
272 case RPC_FC_OP:
273 case RPC_FC_CARRAY:
274 case RPC_FC_CVARRAY:
275 case RPC_FC_BOGUS_ARRAY:
276 write_type_left(h, t->ref, declonly);
277 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
278 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
279 break;
280 default:
281 fprintf(h, "%s", t->name);
282 }
283 }
284 }
285
286 void write_type_right(FILE *h, type_t *t, int is_field)
287 {
288 if (!h) return;
289
290 if (t->declarray) {
291 if (is_conformant_array(t)) {
292 fprintf(h, "[%s]", is_field ? "1" : "");
293 t = t->ref;
294 }
295 for ( ; t->declarray; t = t->ref)
296 fprintf(h, "[%lu]", t->dim);
297 }
298 }
299
300 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
301 const char *fmt, va_list args)
302 {
303 type_t *pt;
304 int ptr_level = 0;
305
306 if (!h) return;
307
308 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
309 ;
310
311 if (pt->type == RPC_FC_FUNCTION) {
312 int i;
313 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
314 if (!callconv) callconv = "";
315 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
316 write_type_left(h, pt->ref, declonly);
317 fputc(' ', h);
318 if (ptr_level) fputc('(', h);
319 fprintf(h, "%s ", callconv);
320 for (i = 0; i < ptr_level; i++)
321 fputc('*', h);
322 } else
323 write_type_left(h, t, declonly);
324 if (fmt) {
325 if (needs_space_after(t))
326 fputc(' ', h);
327 vfprintf(h, fmt, args);
328 }
329 if (pt->type == RPC_FC_FUNCTION) {
330 if (ptr_level) fputc(')', h);
331 fputc('(', h);
332 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
333 fputc(')', h);
334 } else
335 write_type_right(h, t, is_field);
336 }
337
338 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
339 {
340 va_list args;
341 va_start(args, fmt);
342 write_type_v(f, t, field, FALSE, fmt, args);
343 va_end(args);
344 }
345
346 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
347 {
348 va_list args;
349 va_start(args, fmt);
350 write_type_v(f, t, FALSE, TRUE, fmt, args);
351 va_end(args);
352 }
353
354 void write_type_decl_left(FILE *f, type_t *t)
355 {
356 write_type_left(f, t, TRUE);
357 }
358
359 static int user_type_registered(const char *name)
360 {
361 user_type_t *ut;
362 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
363 if (!strcmp(name, ut->name))
364 return 1;
365 return 0;
366 }
367
368 static int context_handle_registered(const char *name)
369 {
370 context_handle_t *ch;
371 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
372 if (!strcmp(name, ch->name))
373 return 1;
374 return 0;
375 }
376
377 static int generic_handle_registered(const char *name)
378 {
379 generic_handle_t *gh;
380 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
381 if (!strcmp(name, gh->name))
382 return 1;
383 return 0;
384 }
385
386 /* check for types which require additional prototypes to be generated in the
387 * header */
388 void check_for_additional_prototype_types(const var_list_t *list)
389 {
390 const var_t *v;
391
392 if (!list) return;
393 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
394 {
395 type_t *type;
396 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
397 const char *name = type->name;
398 if (type->user_types_registered) continue;
399 type->user_types_registered = 1;
400 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
401 if (!context_handle_registered(name))
402 {
403 context_handle_t *ch = xmalloc(sizeof(*ch));
404 ch->name = xstrdup(name);
405 list_add_tail(&context_handle_list, &ch->entry);
406 }
407 /* don't carry on parsing fields within this type */
408 break;
409 }
410 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
411 if (!generic_handle_registered(name))
412 {
413 generic_handle_t *gh = xmalloc(sizeof(*gh));
414 gh->name = xstrdup(name);
415 list_add_tail(&generic_handle_list, &gh->entry);
416 }
417 /* don't carry on parsing fields within this type */
418 break;
419 }
420 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
421 if (!user_type_registered(name))
422 {
423 user_type_t *ut = xmalloc(sizeof *ut);
424 ut->name = xstrdup(name);
425 list_add_tail(&user_type_list, &ut->entry);
426 }
427 /* don't carry on parsing fields within this type as we are already
428 * using a wire marshaled type */
429 break;
430 }
431 else
432 {
433 check_for_additional_prototype_types(type->fields_or_args);
434 }
435 }
436 }
437 }
438
439 void write_user_types(void)
440 {
441 user_type_t *ut;
442 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
443 {
444 const char *name = ut->name;
445 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
446 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
447 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
448 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
449 }
450 }
451
452 void write_context_handle_rundowns(void)
453 {
454 context_handle_t *ch;
455 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
456 {
457 const char *name = ch->name;
458 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
459 }
460 }
461
462 void write_generic_handle_routines(void)
463 {
464 generic_handle_t *gh;
465 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
466 {
467 const char *name = gh->name;
468 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
469 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
470 }
471 }
472
473 void write_typedef(type_t *type)
474 {
475 fprintf(header, "typedef ");
476 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
477 fprintf(header, ";\n");
478 }
479
480 int is_const_decl(const var_t *var)
481 {
482 const type_t *t;
483 /* strangely, MIDL accepts a const attribute on any pointer in the
484 * declaration to mean that data isn't being instantiated. this appears
485 * to be a bug, but there is no benefit to being incompatible with MIDL,
486 * so we'll do the same thing */
487 for (t = var->type; ; )
488 {
489 if (is_attr(t->attrs, ATTR_CONST))
490 return TRUE;
491 else if (is_ptr(t))
492 t = t->ref;
493 else break;
494 }
495 return FALSE;
496 }
497
498 void write_declaration(const var_t *v, int is_in_interface)
499 {
500 if (is_const_decl(v) && v->eval)
501 {
502 fprintf(header, "#define %s (", v->name);
503 write_expr(header, v->eval, 0, 1, NULL, NULL);
504 fprintf(header, ")\n\n");
505 }
506 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
507 {
508 switch (v->stgclass)
509 {
510 case STG_NONE:
511 case STG_REGISTER: /* ignored */
512 break;
513 case STG_STATIC:
514 fprintf(header, "static ");
515 break;
516 case STG_EXTERN:
517 fprintf(header, "extern ");
518 break;
519 }
520 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
521 fprintf(header, ";\n\n");
522 }
523 }
524
525 void write_library(const typelib_t *typelib)
526 {
527 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
528 fprintf(header, "\n");
529 write_guid(header, "LIBID", typelib->name, uuid);
530 fprintf(header, "\n");
531 }
532
533
534 const var_t* get_explicit_handle_var(const func_t* func)
535 {
536 const var_t* var;
537
538 if (!func->args)
539 return NULL;
540
541 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
542 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
543 return var;
544
545 return NULL;
546 }
547
548 const type_t* get_explicit_generic_handle_type(const var_t* var)
549 {
550 const type_t *t = var->type;
551
552 if (t->type == RPC_FC_BIND_PRIMITIVE)
553 return NULL;
554
555 if (!is_ptr(t) && is_attr(t->attrs, ATTR_HANDLE))
556 return t;
557 else
558 for (; is_ptr(t); t = t->ref)
559 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
560 return t;
561
562 return NULL;
563 }
564
565 const var_t* get_explicit_generic_handle_var(const func_t* func)
566 {
567 const var_t* var;
568
569 if (!func->args)
570 return NULL;
571
572 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
573 if (get_explicit_generic_handle_type(var))
574 return var;
575
576 return NULL;
577 }
578
579 const var_t* get_context_handle_var(const func_t* func)
580 {
581 const var_t* var;
582
583 if (!func->args)
584 return NULL;
585
586 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
587 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
588 return var;
589
590 return NULL;
591 }
592
593 int has_out_arg_or_return(const func_t *func)
594 {
595 const var_t *var;
596
597 if (!is_void(get_func_return_type(func)))
598 return 1;
599
600 if (!func->args)
601 return 0;
602
603 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
604 if (is_attr(var->attrs, ATTR_OUT))
605 return 1;
606
607 return 0;
608 }
609
610
611 /********** INTERFACES **********/
612
613 int is_object(const attr_list_t *list)
614 {
615 const attr_t *attr;
616 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
617 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
618 return 0;
619 }
620
621 int is_local(const attr_list_t *a)
622 {
623 return is_attr(a, ATTR_LOCAL);
624 }
625
626 const var_t *is_callas(const attr_list_t *a)
627 {
628 return get_attrp(a, ATTR_CALLAS);
629 }
630
631 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
632 {
633 const func_t *cur;
634
635 if (iface->ref) write_method_macro(header, iface->ref, name);
636
637 if (!iface->funcs) return;
638
639 fprintf(header, "/*** %s methods ***/\n", iface->name);
640 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
641 {
642 var_t *def = cur->def;
643 if (!is_callas(def->attrs)) {
644 const var_t *arg;
645
646 fprintf(header, "#define %s_", name);
647 write_name(header,def);
648 fprintf(header, "(This");
649 if (cur->args)
650 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
651 fprintf(header, ",%s", arg->name);
652 fprintf(header, ") ");
653
654 fprintf(header, "(This)->lpVtbl->");
655 write_name(header, def);
656 fprintf(header, "(This");
657 if (cur->args)
658 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
659 fprintf(header, ",%s", arg->name);
660 fprintf(header, ")\n");
661 }
662 }
663 }
664
665 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
666 {
667 const var_t *arg;
668 int count = 0;
669
670 if (do_indent)
671 {
672 indentation++;
673 indent(h, 0);
674 }
675 if (method == 1) {
676 fprintf(h, "%s* This", name);
677 count++;
678 }
679 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
680 if (count) {
681 if (do_indent)
682 {
683 fprintf(h, ",\n");
684 indent(h, 0);
685 }
686 else fprintf(h, ",");
687 }
688 write_type_decl(h, arg->type, "%s", arg->name);
689 count++;
690 }
691 if (do_indent) indentation--;
692 }
693
694 static void write_cpp_method_def(FILE *header, const type_t *iface)
695 {
696 const func_t *cur;
697
698 if (!iface->funcs) return;
699
700 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
701 {
702 var_t *def = cur->def;
703 if (!is_callas(def->attrs)) {
704 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
705 if (!callconv) callconv = "";
706 indent(header, 0);
707 fprintf(header, "virtual ");
708 write_type_decl_left(header, get_func_return_type(cur));
709 fprintf(header, " %s ", callconv);
710 write_name(header, def);
711 fprintf(header, "(\n");
712 write_args(header, cur->args, iface->name, 2, TRUE);
713 fprintf(header, ") = 0;\n");
714 fprintf(header, "\n");
715 }
716 }
717 }
718
719 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
720 {
721 const func_t *cur;
722
723 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
724
725 if (!iface->funcs) return;
726 indent(header, 0);
727 fprintf(header, "/*** %s methods ***/\n", iface->name);
728 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
729 {
730 const var_t *def = cur->def;
731 if (!is_callas(def->attrs)) {
732 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
733 if (!callconv) callconv = "";
734 indent(header, 0);
735 write_type_decl_left(header, get_func_return_type(cur));
736 fprintf(header, " (%s *", callconv);
737 write_name(header, def);
738 fprintf(header, ")(\n");
739 write_args(header, cur->args, name, 1, TRUE);
740 fprintf(header, ");\n");
741 fprintf(header, "\n");
742 }
743 }
744 }
745
746 static void write_c_method_def(FILE *header, const type_t *iface)
747 {
748 do_write_c_method_def(header, iface, iface->name);
749 }
750
751 static void write_c_disp_method_def(FILE *header, const type_t *iface)
752 {
753 do_write_c_method_def(header, iface->ref, iface->name);
754 }
755
756 static void write_method_proto(FILE *header, const type_t *iface)
757 {
758 const func_t *cur;
759
760 if (!iface->funcs) return;
761 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
762 {
763 const var_t *def = cur->def;
764
765 if (!is_local(def->attrs)) {
766 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
767 if (!callconv) callconv = "";
768 /* proxy prototype */
769 write_type_decl_left(header, get_func_return_type(cur));
770 fprintf(header, " %s %s_", callconv, iface->name);
771 write_name(header, def);
772 fprintf(header, "_Proxy(\n");
773 write_args(header, cur->args, iface->name, 1, TRUE);
774 fprintf(header, ");\n");
775 /* stub prototype */
776 fprintf(header, "void __RPC_STUB %s_", iface->name);
777 write_name(header,def);
778 fprintf(header, "_Stub(\n");
779 fprintf(header, " IRpcStubBuffer* This,\n");
780 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
781 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
782 fprintf(header, " DWORD* pdwStubPhase);\n");
783 }
784 }
785 }
786
787 void write_locals(FILE *fp, const type_t *iface, int body)
788 {
789 static const char comment[]
790 = "/* WIDL-generated stub. You must provide an implementation for this. */";
791 const func_list_t *funcs = iface->funcs;
792 const func_t *cur;
793
794 if (!is_object(iface->attrs) || !funcs)
795 return;
796
797 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
798 const var_t *def = cur->def;
799 const var_t *cas = is_callas(def->attrs);
800
801 if (cas) {
802 const func_t *m;
803 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
804 if (!strcmp(m->def->name, cas->name))
805 break;
806 if (&m->entry != iface->funcs) {
807 const var_t *mdef = m->def;
808 /* proxy prototype - use local prototype */
809 write_type_decl_left(fp, get_func_return_type(m));
810 fprintf(fp, " CALLBACK %s_", iface->name);
811 write_name(fp, mdef);
812 fprintf(fp, "_Proxy(\n");
813 write_args(fp, m->args, iface->name, 1, TRUE);
814 fprintf(fp, ")");
815 if (body) {
816 type_t *rt = get_func_return_type(m);
817 fprintf(fp, "\n{\n");
818 fprintf(fp, " %s\n", comment);
819 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
820 fprintf(fp, " return E_NOTIMPL;\n");
821 else if (rt->type) {
822 fprintf(fp, " ");
823 write_type_decl(fp, rt, "rv");
824 fprintf(fp, ";\n");
825 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
826 fprintf(fp, " return rv;\n");
827 }
828 fprintf(fp, "}\n\n");
829 }
830 else
831 fprintf(fp, ";\n");
832 /* stub prototype - use remotable prototype */
833 write_type_decl_left(fp, get_func_return_type(cur));
834 fprintf(fp, " __RPC_STUB %s_", iface->name);
835 write_name(fp, mdef);
836 fprintf(fp, "_Stub(\n");
837 write_args(fp, cur->args, iface->name, 1, TRUE);
838 fprintf(fp, ")");
839 if (body)
840 /* Remotable methods must all return HRESULTs. */
841 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
842 else
843 fprintf(fp, ";\n");
844 }
845 else
846 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
847 }
848 }
849 }
850
851 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
852 {
853 var_t *def = fun->def;
854 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
855
856 /* FIXME: do we need to handle call_as? */
857 write_type_decl_left(header, get_func_return_type(fun));
858 fprintf(header, " ");
859 if (callconv) fprintf(header, "%s ", callconv);
860 write_prefix_name(header, prefix, def);
861 fprintf(header, "(\n");
862 if (fun->args)
863 write_args(header, fun->args, iface->name, 0, TRUE);
864 else
865 fprintf(header, " void");
866 fprintf(header, ");\n\n");
867 }
868
869 static void write_function_protos(FILE *header, const type_t *iface)
870 {
871 const func_t *cur;
872 int prefixes_differ = strcmp(prefix_client, prefix_server);
873
874 if (!iface->funcs) return;
875 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
876 {
877 if (prefixes_differ) {
878 fprintf(header, "/* client prototype */\n");
879 write_function_proto(header, iface, cur, prefix_client);
880 fprintf(header, "/* server prototype */\n");
881 }
882 write_function_proto(header, iface, cur, prefix_server);
883 }
884 }
885
886 void write_forward(type_t *iface)
887 {
888 /* C/C++ forwards should only be written for object interfaces, so if we
889 * have a full definition we only write one if we find [object] among the
890 * attributes - however, if we don't have a full definition at this point
891 * (i.e. this is an IDL forward), then we also assume that it is an object
892 * interface, since non-object interfaces shouldn't need forwards */
893 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
894 && !iface->written) {
895 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
896 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
897 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
898 fprintf(header, "#endif\n\n" );
899 iface->written = TRUE;
900 }
901 }
902
903 static void write_iface_guid(FILE *header, const type_t *iface)
904 {
905 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
906 write_guid(header, "IID", iface->name, uuid);
907 }
908
909 static void write_dispiface_guid(FILE *header, const type_t *iface)
910 {
911 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
912 write_guid(header, "DIID", iface->name, uuid);
913 }
914
915 static void write_coclass_guid(FILE *header, const type_t *cocl)
916 {
917 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
918 write_guid(header, "CLSID", cocl->name, uuid);
919 }
920
921 static void write_com_interface_start(FILE *header, const type_t *iface)
922 {
923 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
924 fprintf(header, "/*****************************************************************************\n");
925 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
926 fprintf(header, " */\n");
927 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
928 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
929 }
930
931 static void write_com_interface_end(FILE *header, type_t *iface)
932 {
933 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
934 if (dispinterface)
935 write_dispiface_guid(header, iface);
936 else
937 write_iface_guid(header, iface);
938 /* C++ interface */
939 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
940 if (iface->ref)
941 {
942 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
943 fprintf(header, "{\n");
944 }
945 else
946 {
947 fprintf(header, "interface %s\n", iface->name);
948 fprintf(header, "{\n");
949 fprintf(header, " BEGIN_INTERFACE\n");
950 fprintf(header, "\n");
951 }
952 /* dispinterfaces don't have real functions, so don't write C++ functions for
953 * them */
954 if (!dispinterface)
955 {
956 indentation++;
957 write_cpp_method_def(header, iface);
958 indentation--;
959 }
960 if (!iface->ref)
961 fprintf(header, " END_INTERFACE\n");
962 fprintf(header, "};\n");
963 fprintf(header, "#else\n");
964 /* C interface */
965 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
966 indentation++;
967 fprintf(header, " BEGIN_INTERFACE\n");
968 fprintf(header, "\n");
969 if (dispinterface)
970 write_c_disp_method_def(header, iface);
971 else
972 write_c_method_def(header, iface);
973 indentation--;
974 fprintf(header, " END_INTERFACE\n");
975 fprintf(header, "} %sVtbl;\n", iface->name);
976 fprintf(header, "interface %s {\n", iface->name);
977 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
978 fprintf(header, "};\n");
979 fprintf(header, "\n");
980 fprintf(header, "#ifdef COBJMACROS\n");
981 /* dispinterfaces don't have real functions, so don't write macros for them,
982 * only for the interface this interface inherits from, i.e. IDispatch */
983 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
984 fprintf(header, "#endif\n");
985 fprintf(header, "\n");
986 fprintf(header, "#endif\n");
987 fprintf(header, "\n");
988 /* dispinterfaces don't have real functions, so don't write prototypes for
989 * them */
990 if (!dispinterface)
991 {
992 write_method_proto(header, iface);
993 write_locals(header, iface, FALSE);
994 fprintf(header, "\n");
995 }
996 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
997 }
998
999 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1000 {
1001 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1002 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1003 static int allocate_written = 0;
1004
1005 if (!allocate_written)
1006 {
1007 allocate_written = 1;
1008 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
1009 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1010 }
1011
1012 fprintf(header, "/*****************************************************************************\n");
1013 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1014 fprintf(header, " */\n");
1015 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1016 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1017 if (var) fprintf(header, "extern handle_t %s;\n", var);
1018 if (old_names)
1019 {
1020 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1021 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1022 }
1023 else
1024 {
1025 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1026 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1027 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1028 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1029 }
1030 }
1031
1032 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1033 {
1034 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1035 }
1036
1037 void write_interface(type_t *iface)
1038 {
1039 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1040 {
1041 write_com_interface_start(header, iface);
1042 write_com_interface_end(header, iface);
1043 }
1044 else
1045 {
1046 write_rpc_interface_start(header, iface);
1047 write_function_protos(header, iface);
1048 write_rpc_interface_end(header, iface);
1049 }
1050 }
1051
1052 void write_coclass(type_t *cocl)
1053 {
1054 fprintf(header, "/*****************************************************************************\n");
1055 fprintf(header, " * %s coclass\n", cocl->name);
1056 fprintf(header, " */\n\n");
1057 write_coclass_guid(header, cocl);
1058 fprintf(header, "\n");
1059 }
1060
1061 void write_coclass_forward(type_t *cocl)
1062 {
1063 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1064 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1065 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1066 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1067 }
1068
1069 void write_import(const char *fname)
1070 {
1071 char *hname, *p;
1072
1073 hname = dup_basename(fname, ".idl");
1074 p = hname + strlen(hname) - 2;
1075 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1076
1077 fprintf(header, "#include <%s>\n", hname);
1078 free(hname);
1079 }