push 9ed7f32abbb93ea3a813fd6b1650e5bcfc506606
[wine/hacks.git] / tools / widl / header.c
blob40fda80e1bce9f8a547a210e8c73c9ab17106555
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
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.
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.
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
21 #include "config.h"
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 <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
34 #include "windef.h"
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
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);
44 static void indent(FILE *h, int delta)
46 int c;
47 if (delta < 0) indentation += delta;
48 for (c=0; c<indentation; c++) fprintf(h, " ");
49 if (delta > 0) indentation += delta;
52 int is_ptrchain_attr(const var_t *var, enum attr_type t)
54 if (is_attr(var->attrs, t))
55 return 1;
56 else
58 type_t *type = var->type;
59 for (;;)
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;
72 int is_attr(const attr_list_t *list, enum attr_type t)
74 const attr_t *attr;
75 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
76 if (attr->type == t) return 1;
77 return 0;
80 void *get_attrp(const attr_list_t *list, enum attr_type t)
82 const attr_t *attr;
83 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
84 if (attr->type == t) return attr->u.pval;
85 return NULL;
88 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
90 const attr_t *attr;
91 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
92 if (attr->type == t) return attr->u.ival;
93 return 0;
96 int is_void(const type_t *t)
98 if (!t->type && !t->ref) return 1;
99 return 0;
102 int is_conformant_array(const type_t *t)
104 return t->type == RPC_FC_CARRAY
105 || t->type == RPC_FC_CVARRAY
106 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
109 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
111 if (!uuid) return;
112 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
113 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
114 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
115 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
116 uuid->Data4[6], uuid->Data4[7]);
119 void write_name(FILE *h, const var_t *v)
121 if (is_attr( v->attrs, ATTR_PROPGET ))
122 fprintf(h, "get_" );
123 else if (is_attr( v->attrs, ATTR_PROPPUT ))
124 fprintf(h, "put_" );
125 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
126 fprintf(h, "putref_" );
127 fprintf(h, "%s", v->name);
130 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
132 fprintf(h, "%s", prefix);
133 write_name(h, v);
136 static void write_field(FILE *h, var_t *v)
138 if (!v) return;
139 if (v->type) {
140 const char *name = v->name;
141 if (name == NULL) {
142 switch (v->type->type) {
143 case RPC_FC_STRUCT:
144 case RPC_FC_CVSTRUCT:
145 case RPC_FC_CPSTRUCT:
146 case RPC_FC_CSTRUCT:
147 case RPC_FC_PSTRUCT:
148 case RPC_FC_BOGUS_STRUCT:
149 case RPC_FC_ENCAPSULATED_UNION:
150 name = "DUMMYSTRUCTNAME";
151 break;
152 case RPC_FC_NON_ENCAPSULATED_UNION:
153 name = "DUMMYUNIONNAME";
154 break;
155 default:
156 /* ? */
157 break;
160 indent(h, 0);
161 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
162 fprintf(h, ";\n");
166 static void write_fields(FILE *h, var_list_t *fields)
168 var_t *v;
169 if (!fields) return;
170 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
173 static void write_enums(FILE *h, var_list_t *enums)
175 var_t *v;
176 if (!enums) return;
177 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
179 if (v->name) {
180 indent(h, 0);
181 write_name(h, v);
182 if (v->eval) {
183 fprintf(h, " = ");
184 write_expr(h, v->eval, 0);
187 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
189 fprintf(h, "\n");
192 int needs_space_after(type_t *t)
194 return (t->kind == TKIND_ALIAS
195 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
198 void write_type_left(FILE *h, type_t *t, int declonly)
200 if (!h) return;
202 if (t->is_const) fprintf(h, "const ");
204 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
205 else if (t->declarray) write_type_left(h, t->ref, declonly);
206 else {
207 if (t->sign > 0) fprintf(h, "signed ");
208 else if (t->sign < 0) fprintf(h, "unsigned ");
209 switch (t->type) {
210 case RPC_FC_ENUM16:
211 case RPC_FC_ENUM32:
212 if (!declonly && t->defined && !t->written && !t->ignore) {
213 if (t->name) fprintf(h, "enum %s {\n", t->name);
214 else fprintf(h, "enum {\n");
215 t->written = TRUE;
216 indentation++;
217 write_enums(h, t->fields);
218 indent(h, -1);
219 fprintf(h, "}");
221 else fprintf(h, "enum %s", t->name ? t->name : "");
222 break;
223 case RPC_FC_STRUCT:
224 case RPC_FC_CVSTRUCT:
225 case RPC_FC_CPSTRUCT:
226 case RPC_FC_CSTRUCT:
227 case RPC_FC_PSTRUCT:
228 case RPC_FC_BOGUS_STRUCT:
229 case RPC_FC_ENCAPSULATED_UNION:
230 if (!declonly && t->defined && !t->written && !t->ignore) {
231 if (t->name) fprintf(h, "struct %s {\n", t->name);
232 else fprintf(h, "struct {\n");
233 t->written = TRUE;
234 indentation++;
235 write_fields(h, t->fields);
236 indent(h, -1);
237 fprintf(h, "}");
239 else fprintf(h, "struct %s", t->name ? t->name : "");
240 break;
241 case RPC_FC_NON_ENCAPSULATED_UNION:
242 if (!declonly && t->defined && !t->written && !t->ignore) {
243 if (t->name) fprintf(h, "union %s {\n", t->name);
244 else fprintf(h, "union {\n");
245 t->written = TRUE;
246 indentation++;
247 write_fields(h, t->fields);
248 indent(h, -1);
249 fprintf(h, "}");
251 else fprintf(h, "union %s", t->name ? t->name : "");
252 break;
253 case RPC_FC_RP:
254 case RPC_FC_UP:
255 case RPC_FC_FP:
256 case RPC_FC_OP:
257 case RPC_FC_CARRAY:
258 case RPC_FC_CVARRAY:
259 case RPC_FC_BOGUS_ARRAY:
260 write_type_left(h, t->ref, declonly);
261 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
262 break;
263 default:
264 fprintf(h, "%s", t->name);
269 void write_type_right(FILE *h, type_t *t, int is_field)
271 if (!h) return;
273 if (t->declarray) {
274 if (is_conformant_array(t)) {
275 fprintf(h, "[%s]", is_field ? "1" : "");
276 t = t->ref;
278 for ( ; t->declarray; t = t->ref)
279 fprintf(h, "[%lu]", t->dim);
283 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
284 const char *fmt, va_list args)
286 if (!h) return;
288 write_type_left(h, t, declonly);
289 if (fmt) {
290 if (needs_space_after(t))
291 fprintf(h, " ");
292 vfprintf(h, fmt, args);
294 write_type_right(h, t, is_field);
297 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
299 va_list args;
300 va_start(args, fmt);
301 write_type_v(f, t, field, FALSE, fmt, args);
302 va_end(args);
305 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
307 va_list args;
308 va_start(args, fmt);
309 write_type_v(f, t, FALSE, TRUE, fmt, args);
310 va_end(args);
313 void write_type_decl_left(FILE *f, type_t *t)
315 write_type_left(f, t, TRUE);
318 static int user_type_registered(const char *name)
320 user_type_t *ut;
321 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
322 if (!strcmp(name, ut->name))
323 return 1;
324 return 0;
327 static int context_handle_registered(const char *name)
329 context_handle_t *ch;
330 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
331 if (!strcmp(name, ch->name))
332 return 1;
333 return 0;
336 void check_for_user_types_and_context_handles(const var_list_t *list)
338 const var_t *v;
340 if (!list) return;
341 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
343 type_t *type;
344 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
345 const char *name = type->name;
346 if (type->user_types_registered) continue;
347 type->user_types_registered = 1;
348 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
349 if (!context_handle_registered(name))
351 context_handle_t *ch = xmalloc(sizeof(*ch));
352 ch->name = xstrdup(name);
353 list_add_tail(&context_handle_list, &ch->entry);
355 /* don't carry on parsing fields within this type */
356 break;
358 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
359 if (!user_type_registered(name))
361 user_type_t *ut = xmalloc(sizeof *ut);
362 ut->name = xstrdup(name);
363 list_add_tail(&user_type_list, &ut->entry);
365 /* don't carry on parsing fields within this type as we are already
366 * using a wire marshaled type */
367 break;
369 else
371 check_for_user_types_and_context_handles(type->fields);
377 void write_user_types(void)
379 user_type_t *ut;
380 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
382 const char *name = ut->name;
383 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
384 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
385 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
386 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
390 void write_context_handle_rundowns(void)
392 context_handle_t *ch;
393 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
395 const char *name = ch->name;
396 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
400 void write_typedef(type_t *type)
402 fprintf(header, "typedef ");
403 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
404 fprintf(header, ";\n");
407 void write_expr(FILE *h, const expr_t *e, int brackets)
409 switch (e->type) {
410 case EXPR_VOID:
411 break;
412 case EXPR_NUM:
413 fprintf(h, "%lu", e->u.lval);
414 break;
415 case EXPR_HEXNUM:
416 fprintf(h, "0x%lx", e->u.lval);
417 break;
418 case EXPR_DOUBLE:
419 fprintf(h, "%#.15g", e->u.dval);
420 break;
421 case EXPR_TRUEFALSE:
422 if (e->u.lval == 0)
423 fprintf(h, "FALSE");
424 else
425 fprintf(h, "TRUE");
426 break;
427 case EXPR_IDENTIFIER:
428 fprintf(h, "%s", e->u.sval);
429 break;
430 case EXPR_NEG:
431 fprintf(h, "-");
432 write_expr(h, e->ref, 1);
433 break;
434 case EXPR_NOT:
435 fprintf(h, "~");
436 write_expr(h, e->ref, 1);
437 break;
438 case EXPR_PPTR:
439 fprintf(h, "*");
440 write_expr(h, e->ref, 1);
441 break;
442 case EXPR_CAST:
443 fprintf(h, "(");
444 write_type_decl(h, e->u.tref, NULL);
445 fprintf(h, ")");
446 write_expr(h, e->ref, 1);
447 break;
448 case EXPR_SIZEOF:
449 fprintf(h, "sizeof(");
450 write_type_decl(h, e->u.tref, NULL);
451 fprintf(h, ")");
452 break;
453 case EXPR_SHL:
454 case EXPR_SHR:
455 case EXPR_MUL:
456 case EXPR_DIV:
457 case EXPR_ADD:
458 case EXPR_SUB:
459 case EXPR_AND:
460 case EXPR_OR:
461 if (brackets) fprintf(h, "(");
462 write_expr(h, e->ref, 1);
463 switch (e->type) {
464 case EXPR_SHL: fprintf(h, " << "); break;
465 case EXPR_SHR: fprintf(h, " >> "); break;
466 case EXPR_MUL: fprintf(h, " * "); break;
467 case EXPR_DIV: fprintf(h, " / "); break;
468 case EXPR_ADD: fprintf(h, " + "); break;
469 case EXPR_SUB: fprintf(h, " - "); break;
470 case EXPR_AND: fprintf(h, " & "); break;
471 case EXPR_OR: fprintf(h, " | "); break;
472 default: break;
474 write_expr(h, e->u.ext, 1);
475 if (brackets) fprintf(h, ")");
476 break;
477 case EXPR_COND:
478 if (brackets) fprintf(h, "(");
479 write_expr(h, e->ref, 1);
480 fprintf(h, " ? ");
481 write_expr(h, e->u.ext, 1);
482 fprintf(h, " : ");
483 write_expr(h, e->ext2, 1);
484 if (brackets) fprintf(h, ")");
485 break;
489 void write_constdef(const var_t *v)
491 fprintf(header, "#define %s (", v->name);
492 write_expr(header, v->eval, 0);
493 fprintf(header, ")\n\n");
496 void write_externdef(const var_t *v)
498 fprintf(header, "extern const ");
499 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
500 fprintf(header, ";\n\n");
503 void write_library(const char *name, const attr_list_t *attr)
505 const UUID *uuid = get_attrp(attr, ATTR_UUID);
506 fprintf(header, "\n");
507 write_guid(header, "LIBID", name, uuid);
508 fprintf(header, "\n");
512 const var_t* get_explicit_handle_var(const func_t* func)
514 const var_t* var;
516 if (!func->args)
517 return NULL;
519 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
520 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
521 return var;
523 return NULL;
526 int has_out_arg_or_return(const func_t *func)
528 const var_t *var;
530 if (!is_void(func->def->type))
531 return 1;
533 if (!func->args)
534 return 0;
536 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
537 if (is_attr(var->attrs, ATTR_OUT))
538 return 1;
540 return 0;
544 /********** INTERFACES **********/
546 int is_object(const attr_list_t *list)
548 const attr_t *attr;
549 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
550 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
551 return 0;
554 int is_local(const attr_list_t *a)
556 return is_attr(a, ATTR_LOCAL);
559 const var_t *is_callas(const attr_list_t *a)
561 return get_attrp(a, ATTR_CALLAS);
564 static void write_method_macro(const type_t *iface, const char *name)
566 const func_t *cur;
568 if (iface->ref) write_method_macro(iface->ref, name);
570 if (!iface->funcs) return;
572 fprintf(header, "/*** %s methods ***/\n", iface->name);
573 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
575 var_t *def = cur->def;
576 if (!is_callas(def->attrs)) {
577 const var_t *arg;
578 int argc = 0;
579 int c;
581 if (cur->args) LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry ) argc++;
583 fprintf(header, "#define %s_", name);
584 write_name(header,def);
585 fprintf(header, "(p");
586 for (c=0; c<argc; c++)
587 fprintf(header, ",%c", c+'a');
588 fprintf(header, ") ");
590 fprintf(header, "(p)->lpVtbl->");
591 write_name(header, def);
592 fprintf(header, "(p");
593 for (c=0; c<argc; c++)
594 fprintf(header, ",%c", c+'a');
595 fprintf(header, ")\n");
600 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
602 const var_t *arg;
603 int count = 0;
605 if (do_indent)
607 indentation++;
608 indent(h, 0);
610 if (method == 1) {
611 fprintf(h, "%s* This", name);
612 count++;
614 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
615 if (count) {
616 if (do_indent)
618 fprintf(h, ",\n");
619 indent(h, 0);
621 else fprintf(h, ",");
623 if (arg->args)
625 write_type_decl_left(h, arg->type);
626 fprintf(h, " (STDMETHODCALLTYPE *");
627 write_name(h,arg);
628 fprintf(h, ")(");
629 write_args(h, arg->args, NULL, 0, FALSE);
630 fprintf(h, ")");
632 else
633 write_type_decl(h, arg->type, "%s", arg->name);
634 count++;
636 if (do_indent) indentation--;
639 static void write_cpp_method_def(const type_t *iface)
641 const func_t *cur;
643 if (!iface->funcs) return;
645 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
647 var_t *def = cur->def;
648 if (!is_callas(def->attrs)) {
649 indent(header, 0);
650 fprintf(header, "virtual ");
651 write_type_decl_left(header, def->type);
652 fprintf(header, " STDMETHODCALLTYPE ");
653 write_name(header, def);
654 fprintf(header, "(\n");
655 write_args(header, cur->args, iface->name, 2, TRUE);
656 fprintf(header, ") = 0;\n");
657 fprintf(header, "\n");
662 static void do_write_c_method_def(const type_t *iface, const char *name)
664 const func_t *cur;
666 if (iface->ref) do_write_c_method_def(iface->ref, name);
668 if (!iface->funcs) return;
669 indent(header, 0);
670 fprintf(header, "/*** %s methods ***/\n", iface->name);
671 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
673 const var_t *def = cur->def;
674 if (!is_callas(def->attrs)) {
675 indent(header, 0);
676 write_type_decl_left(header, def->type);
677 fprintf(header, " (STDMETHODCALLTYPE *");
678 write_name(header, def);
679 fprintf(header, ")(\n");
680 write_args(header, cur->args, name, 1, TRUE);
681 fprintf(header, ");\n");
682 fprintf(header, "\n");
687 static void write_c_method_def(const type_t *iface)
689 do_write_c_method_def(iface, iface->name);
692 static void write_c_disp_method_def(const type_t *iface)
694 do_write_c_method_def(iface->ref, iface->name);
697 static void write_method_proto(const type_t *iface)
699 const func_t *cur;
701 if (!iface->funcs) return;
702 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
704 const var_t *def = cur->def;
705 const var_t *cas = is_callas(def->attrs);
707 if (!is_local(def->attrs)) {
708 /* proxy prototype */
709 write_type_decl_left(header, def->type);
710 fprintf(header, " CALLBACK %s_", iface->name);
711 write_name(header, def);
712 fprintf(header, "_Proxy(\n");
713 write_args(header, cur->args, iface->name, 1, TRUE);
714 fprintf(header, ");\n");
715 /* stub prototype */
716 fprintf(header, "void __RPC_STUB %s_", iface->name);
717 write_name(header,def);
718 fprintf(header, "_Stub(\n");
719 fprintf(header, " IRpcStubBuffer* This,\n");
720 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
721 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
722 fprintf(header, " DWORD* pdwStubPhase);\n");
724 if (cas) {
725 const func_t *m;
726 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
727 if (!strcmp(m->def->name, cas->name)) break;
728 if (&m->entry != iface->funcs) {
729 const var_t *mdef = m->def;
730 /* proxy prototype - use local prototype */
731 write_type_decl_left(header, mdef->type);
732 fprintf(header, " CALLBACK %s_", iface->name);
733 write_name(header, mdef);
734 fprintf(header, "_Proxy(\n");
735 write_args(header, m->args, iface->name, 1, TRUE);
736 fprintf(header, ");\n");
737 /* stub prototype - use remotable prototype */
738 write_type_decl_left(header, def->type);
739 fprintf(header, " __RPC_STUB %s_", iface->name);
740 write_name(header, mdef);
741 fprintf(header, "_Stub(\n");
742 write_args(header, cur->args, iface->name, 1, TRUE);
743 fprintf(header, ");\n");
745 else {
746 parser_warning("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
752 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
754 var_t *def = fun->def;
756 /* FIXME: do we need to handle call_as? */
757 write_type_decl_left(header, def->type);
758 fprintf(header, " ");
759 write_prefix_name(header, prefix, def);
760 fprintf(header, "(\n");
761 if (fun->args)
762 write_args(header, fun->args, iface->name, 0, TRUE);
763 else
764 fprintf(header, " void");
765 fprintf(header, ");\n");
768 static void write_function_protos(const type_t *iface)
770 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
771 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
772 const var_t* explicit_handle_var;
773 const func_t *cur;
774 int prefixes_differ = strcmp(prefix_client, prefix_server);
776 if (!iface->funcs) return;
777 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
779 var_t *def = cur->def;
781 /* check for a defined binding handle */
782 explicit_handle_var = get_explicit_handle_var(cur);
783 if (explicit_handle) {
784 if (!explicit_handle_var) {
785 error("%s() does not define an explicit binding handle!\n", def->name);
786 return;
788 } else if (implicit_handle) {
789 if (explicit_handle_var) {
790 error("%s() must not define a binding handle!\n", def->name);
791 return;
795 if (prefixes_differ) {
796 fprintf(header, "/* client prototype */\n");
797 write_function_proto(iface, cur, prefix_client);
798 fprintf(header, "/* server prototype */\n");
800 write_function_proto(iface, cur, prefix_server);
804 void write_forward(type_t *iface)
806 /* C/C++ forwards should only be written for object interfaces, so if we
807 * have a full definition we only write one if we find [object] among the
808 * attributes - however, if we don't have a full definition at this point
809 * (i.e. this is an IDL forward), then we also assume that it is an object
810 * interface, since non-object interfaces shouldn't need forwards */
811 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
812 && !iface->written) {
813 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
814 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
815 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
816 fprintf(header, "#endif\n\n" );
817 iface->written = TRUE;
821 static void write_iface_guid(const type_t *iface)
823 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
824 write_guid(header, "IID", iface->name, uuid);
827 static void write_dispiface_guid(const type_t *iface)
829 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
830 write_guid(header, "DIID", iface->name, uuid);
833 static void write_coclass_guid(type_t *cocl)
835 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
836 write_guid(header, "CLSID", cocl->name, uuid);
839 static void write_com_interface(type_t *iface)
841 if (!iface->funcs && !iface->ref) {
842 parser_warning("%s has no methods\n", iface->name);
843 return;
846 fprintf(header, "/*****************************************************************************\n");
847 fprintf(header, " * %s interface\n", iface->name);
848 fprintf(header, " */\n");
849 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
850 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
851 write_iface_guid(iface);
852 write_forward(iface);
853 /* C++ interface */
854 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
855 if (iface->ref)
857 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
858 fprintf(header, "{\n");
859 indentation++;
860 write_cpp_method_def(iface);
861 indentation--;
862 fprintf(header, "};\n");
864 else
866 fprintf(header, "interface %s\n", iface->name);
867 fprintf(header, "{\n");
868 fprintf(header, " BEGIN_INTERFACE\n");
869 fprintf(header, "\n");
870 indentation++;
871 write_cpp_method_def(iface);
872 indentation--;
873 fprintf(header, " END_INTERFACE\n");
874 fprintf(header, "};\n");
876 fprintf(header, "#else\n");
877 /* C interface */
878 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
879 indentation++;
880 fprintf(header, " BEGIN_INTERFACE\n");
881 fprintf(header, "\n");
882 write_c_method_def(iface);
883 indentation--;
884 fprintf(header, " END_INTERFACE\n");
885 fprintf(header, "} %sVtbl;\n", iface->name);
886 fprintf(header, "interface %s {\n", iface->name);
887 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
888 fprintf(header, "};\n");
889 fprintf(header, "\n");
890 fprintf(header, "#ifdef COBJMACROS\n");
891 write_method_macro(iface, iface->name);
892 fprintf(header, "#endif\n");
893 fprintf(header, "\n");
894 fprintf(header, "#endif\n");
895 fprintf(header, "\n");
896 write_method_proto(iface);
897 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
900 static void write_rpc_interface(const type_t *iface)
902 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
903 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
904 static int allocate_written = 0;
906 if (!allocate_written)
908 allocate_written = 1;
909 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
910 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
913 fprintf(header, "/*****************************************************************************\n");
914 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
915 fprintf(header, " */\n");
916 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
917 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
918 if (iface->funcs)
920 write_iface_guid(iface);
921 if (var) fprintf(header, "extern handle_t %s;\n", var);
922 if (old_names)
924 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
925 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
927 else
929 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
930 prefix_client, iface->name, LOWORD(ver), HIWORD(ver));
931 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
932 prefix_server, iface->name, LOWORD(ver), HIWORD(ver));
934 write_function_protos(iface);
936 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
938 /* FIXME: server/client code */
941 void write_interface(type_t *iface)
943 if (is_object(iface->attrs))
944 write_com_interface(iface);
945 else
946 write_rpc_interface(iface);
949 void write_dispinterface(type_t *iface)
951 fprintf(header, "/*****************************************************************************\n");
952 fprintf(header, " * %s dispinterface\n", iface->name);
953 fprintf(header, " */\n");
954 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
955 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
956 write_dispiface_guid(iface);
957 write_forward(iface);
958 /* C++ interface */
959 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
960 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
961 fprintf(header, "{\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 write_c_disp_method_def(iface);
970 indentation--;
971 fprintf(header, " END_INTERFACE\n");
972 fprintf(header, "} %sVtbl;\n", iface->name);
973 fprintf(header, "interface %s {\n", iface->name);
974 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
975 fprintf(header, "};\n");
976 fprintf(header, "\n");
977 fprintf(header, "#ifdef COBJMACROS\n");
978 write_method_macro(iface->ref, iface->name);
979 fprintf(header, "#endif\n");
980 fprintf(header, "\n");
981 fprintf(header, "#endif\n");
982 fprintf(header, "\n");
983 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
986 void write_coclass(type_t *cocl)
988 fprintf(header, "/*****************************************************************************\n");
989 fprintf(header, " * %s coclass\n", cocl->name);
990 fprintf(header, " */\n\n");
991 write_coclass_guid(cocl);
992 fprintf(header, "\n");
995 void write_coclass_forward(type_t *cocl)
997 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
998 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
999 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1000 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );