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