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