widl: Add a declonly argument to write_type_left.
[wine/winequartzdrv.git] / tools / widl / header.c
blob07a562b670410211ab78481bdc61422119d8d489
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 || t->type == RPC_FC_CVARRAY;
107 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
109 if (!uuid) return;
110 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
111 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
112 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
113 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
114 uuid->Data4[6], uuid->Data4[7]);
117 void write_name(FILE *h, const var_t *v)
119 if (is_attr( v->attrs, ATTR_PROPGET ))
120 fprintf(h, "get_" );
121 else if (is_attr( v->attrs, ATTR_PROPPUT ))
122 fprintf(h, "put_" );
123 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
124 fprintf(h, "putref_" );
125 fprintf(h, "%s", v->name);
128 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
130 fprintf(h, "%s", prefix);
131 write_name(h, v);
134 static void write_field(FILE *h, var_t *v)
136 if (!v) return;
137 if (v->type) {
138 const char *name = v->name;
139 if (name == NULL) {
140 switch (v->type->type) {
141 case RPC_FC_STRUCT:
142 case RPC_FC_CVSTRUCT:
143 case RPC_FC_CPSTRUCT:
144 case RPC_FC_CSTRUCT:
145 case RPC_FC_PSTRUCT:
146 case RPC_FC_BOGUS_STRUCT:
147 case RPC_FC_ENCAPSULATED_UNION:
148 name = "DUMMYSTRUCTNAME";
149 break;
150 case RPC_FC_NON_ENCAPSULATED_UNION:
151 name = "DUMMYUNIONNAME";
152 break;
153 default:
154 /* ? */
155 break;
158 indent(h, 0);
159 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
160 fprintf(h, ";\n");
164 static void write_fields(FILE *h, var_list_t *fields)
166 var_t *v;
167 if (!fields) return;
168 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
171 static void write_enums(FILE *h, var_list_t *enums)
173 var_t *v;
174 if (!enums) return;
175 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
177 if (v->name) {
178 indent(h, 0);
179 write_name(h, v);
180 if (v->eval) {
181 fprintf(h, " = ");
182 write_expr(h, v->eval, 0);
185 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
187 fprintf(h, "\n");
190 int needs_space_after(type_t *t)
192 return (t->kind == TKIND_ALIAS
193 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
196 void write_type_left(FILE *h, type_t *t, int declonly)
198 if (t->is_const) fprintf(h, "const ");
200 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
201 else if (t->declarray) write_type_left(h, t->ref, declonly);
202 else {
203 if (t->sign > 0) fprintf(h, "signed ");
204 else if (t->sign < 0) fprintf(h, "unsigned ");
205 switch (t->type) {
206 case RPC_FC_ENUM16:
207 case RPC_FC_ENUM32:
208 if (!declonly && t->defined && !t->written && !t->ignore) {
209 if (t->name) fprintf(h, "enum %s {\n", t->name);
210 else fprintf(h, "enum {\n");
211 t->written = TRUE;
212 indentation++;
213 write_enums(h, t->fields);
214 indent(h, -1);
215 fprintf(h, "}");
217 else fprintf(h, "enum %s", t->name);
218 break;
219 case RPC_FC_STRUCT:
220 case RPC_FC_CVSTRUCT:
221 case RPC_FC_CPSTRUCT:
222 case RPC_FC_CSTRUCT:
223 case RPC_FC_PSTRUCT:
224 case RPC_FC_BOGUS_STRUCT:
225 case RPC_FC_ENCAPSULATED_UNION:
226 if (!declonly && t->defined && !t->written && !t->ignore) {
227 if (t->name) fprintf(h, "struct %s {\n", t->name);
228 else fprintf(h, "struct {\n");
229 t->written = TRUE;
230 indentation++;
231 write_fields(h, t->fields);
232 indent(h, -1);
233 fprintf(h, "}");
235 else fprintf(h, "struct %s", t->name);
236 break;
237 case RPC_FC_NON_ENCAPSULATED_UNION:
238 if (!declonly && t->defined && !t->written && !t->ignore) {
239 if (t->name) fprintf(h, "union %s {\n", t->name);
240 else fprintf(h, "union {\n");
241 t->written = TRUE;
242 indentation++;
243 write_fields(h, t->fields);
244 indent(h, -1);
245 fprintf(h, "}");
247 else fprintf(h, "union %s", t->name);
248 break;
249 case RPC_FC_RP:
250 case RPC_FC_UP:
251 case RPC_FC_FP:
252 case RPC_FC_OP:
253 case RPC_FC_CARRAY:
254 case RPC_FC_CVARRAY:
255 case RPC_FC_BOGUS_ARRAY:
256 write_type_left(h, t->ref, declonly);
257 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
258 break;
259 default:
260 fprintf(h, "%s", t->name);
265 void write_type_right(FILE *h, type_t *t, int is_field)
267 if (t->declarray) {
268 if (is_conformant_array(t)) {
269 fprintf(h, "[%s]", is_field ? "1" : "");
270 t = t->ref;
272 for ( ; t->declarray; t = t->ref)
273 fprintf(h, "[%lu]", t->dim);
277 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
278 const char *fmt, va_list args)
280 write_type_left(h, t, declonly);
281 if (fmt) {
282 if (needs_space_after(t))
283 fprintf(h, " ");
284 vfprintf(h, fmt, args);
286 write_type_right(h, t, is_field);
289 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
291 va_list args;
292 va_start(args, fmt);
293 write_type_v(f, t, field, FALSE, fmt, args);
294 va_end(args);
297 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
299 va_list args;
300 va_start(args, fmt);
301 write_type_v(f, t, FALSE, TRUE, fmt, args);
302 va_end(args);
305 void write_type_decl_left(FILE *f, type_t *t)
307 write_type_left(f, t, TRUE);
310 static int user_type_registered(const char *name)
312 user_type_t *ut;
313 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
314 if (!strcmp(name, ut->name))
315 return 1;
316 return 0;
319 static int context_handle_registered(const char *name)
321 context_handle_t *ch;
322 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
323 if (!strcmp(name, ch->name))
324 return 1;
325 return 0;
328 void check_for_user_types_and_context_handles(const var_list_t *list)
330 const var_t *v;
332 if (!list) return;
333 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
335 type_t *type;
336 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
337 const char *name = type->name;
338 if (type->user_types_registered) continue;
339 type->user_types_registered = 1;
340 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
341 if (!context_handle_registered(name))
343 context_handle_t *ch = xmalloc(sizeof(*ch));
344 ch->name = xstrdup(name);
345 list_add_tail(&context_handle_list, &ch->entry);
347 /* don't carry on parsing fields within this type */
348 break;
350 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
351 if (!user_type_registered(name))
353 user_type_t *ut = xmalloc(sizeof *ut);
354 ut->name = xstrdup(name);
355 list_add_tail(&user_type_list, &ut->entry);
357 /* don't carry on parsing fields within this type as we are already
358 * using a wire marshaled type */
359 break;
361 else
363 check_for_user_types_and_context_handles(type->fields);
369 void write_user_types(void)
371 user_type_t *ut;
372 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
374 const char *name = ut->name;
375 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
376 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
377 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
378 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
382 void write_context_handle_rundowns(void)
384 context_handle_t *ch;
385 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
387 const char *name = ch->name;
388 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
392 void write_typedef(type_t *type)
394 fprintf(header, "typedef ");
395 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
396 fprintf(header, ";\n");
399 void write_expr(FILE *h, const expr_t *e, int brackets)
401 switch (e->type) {
402 case EXPR_VOID:
403 break;
404 case EXPR_NUM:
405 fprintf(h, "%lu", e->u.lval);
406 break;
407 case EXPR_HEXNUM:
408 fprintf(h, "0x%lx", e->u.lval);
409 break;
410 case EXPR_DOUBLE:
411 fprintf(h, "%#.15g", e->u.dval);
412 break;
413 case EXPR_TRUEFALSE:
414 if (e->u.lval == 0)
415 fprintf(h, "FALSE");
416 else
417 fprintf(h, "TRUE");
418 break;
419 case EXPR_IDENTIFIER:
420 fprintf(h, "%s", e->u.sval);
421 break;
422 case EXPR_NEG:
423 fprintf(h, "-");
424 write_expr(h, e->ref, 1);
425 break;
426 case EXPR_NOT:
427 fprintf(h, "~");
428 write_expr(h, e->ref, 1);
429 break;
430 case EXPR_PPTR:
431 fprintf(h, "*");
432 write_expr(h, e->ref, 1);
433 break;
434 case EXPR_CAST:
435 fprintf(h, "(");
436 write_type_decl(h, e->u.tref, NULL);
437 fprintf(h, ")");
438 write_expr(h, e->ref, 1);
439 break;
440 case EXPR_SIZEOF:
441 fprintf(h, "sizeof(");
442 write_type_decl(h, e->u.tref, NULL);
443 fprintf(h, ")");
444 break;
445 case EXPR_SHL:
446 case EXPR_SHR:
447 case EXPR_MUL:
448 case EXPR_DIV:
449 case EXPR_ADD:
450 case EXPR_SUB:
451 case EXPR_AND:
452 case EXPR_OR:
453 if (brackets) fprintf(h, "(");
454 write_expr(h, e->ref, 1);
455 switch (e->type) {
456 case EXPR_SHL: fprintf(h, " << "); break;
457 case EXPR_SHR: fprintf(h, " >> "); break;
458 case EXPR_MUL: fprintf(h, " * "); break;
459 case EXPR_DIV: fprintf(h, " / "); break;
460 case EXPR_ADD: fprintf(h, " + "); break;
461 case EXPR_SUB: fprintf(h, " - "); break;
462 case EXPR_AND: fprintf(h, " & "); break;
463 case EXPR_OR: fprintf(h, " | "); break;
464 default: break;
466 write_expr(h, e->u.ext, 1);
467 if (brackets) fprintf(h, ")");
468 break;
469 case EXPR_COND:
470 if (brackets) fprintf(h, "(");
471 write_expr(h, e->ref, 1);
472 fprintf(h, " ? ");
473 write_expr(h, e->u.ext, 1);
474 fprintf(h, " : ");
475 write_expr(h, e->ext2, 1);
476 if (brackets) fprintf(h, ")");
477 break;
481 void write_constdef(const var_t *v)
483 fprintf(header, "#define %s (", v->name);
484 write_expr(header, v->eval, 0);
485 fprintf(header, ")\n\n");
488 void write_externdef(const var_t *v)
490 fprintf(header, "extern const ");
491 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
492 fprintf(header, ";\n\n");
495 void write_library(const char *name, const attr_list_t *attr)
497 const UUID *uuid = get_attrp(attr, ATTR_UUID);
498 fprintf(header, "\n");
499 write_guid(header, "LIBID", name, uuid);
500 fprintf(header, "\n");
504 const var_t* get_explicit_handle_var(const func_t* func)
506 const var_t* var;
508 if (!func->args)
509 return NULL;
511 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
512 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
513 return var;
515 return NULL;
518 int has_out_arg_or_return(const func_t *func)
520 const var_t *var;
522 if (!is_void(func->def->type))
523 return 1;
525 if (!func->args)
526 return 0;
528 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
529 if (is_attr(var->attrs, ATTR_OUT))
530 return 1;
532 return 0;
536 /********** INTERFACES **********/
538 int is_object(const attr_list_t *list)
540 const attr_t *attr;
541 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
542 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
543 return 0;
546 int is_local(const attr_list_t *a)
548 return is_attr(a, ATTR_LOCAL);
551 const var_t *is_callas(const attr_list_t *a)
553 return get_attrp(a, ATTR_CALLAS);
556 static void write_method_macro(const type_t *iface, const char *name)
558 const func_t *cur;
560 if (iface->ref) write_method_macro(iface->ref, name);
562 if (!iface->funcs) return;
564 fprintf(header, "/*** %s methods ***/\n", iface->name);
565 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
567 var_t *def = cur->def;
568 if (!is_callas(def->attrs)) {
569 const var_t *arg;
570 int argc = 0;
571 int c;
573 if (cur->args) LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry ) argc++;
575 fprintf(header, "#define %s_", name);
576 write_name(header,def);
577 fprintf(header, "(p");
578 for (c=0; c<argc; c++)
579 fprintf(header, ",%c", c+'a');
580 fprintf(header, ") ");
582 fprintf(header, "(p)->lpVtbl->");
583 write_name(header, def);
584 fprintf(header, "(p");
585 for (c=0; c<argc; c++)
586 fprintf(header, ",%c", c+'a');
587 fprintf(header, ")\n");
592 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
594 const var_t *arg;
595 int count = 0;
597 if (do_indent)
599 indentation++;
600 indent(h, 0);
602 if (method == 1) {
603 fprintf(h, "%s* This", name);
604 count++;
606 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
607 if (count) {
608 if (do_indent)
610 fprintf(h, ",\n");
611 indent(h, 0);
613 else fprintf(h, ",");
615 if (arg->args)
617 write_type_decl_left(h, arg->type);
618 fprintf(h, " (STDMETHODCALLTYPE *");
619 write_name(h,arg);
620 fprintf(h, ")(");
621 write_args(h, arg->args, NULL, 0, FALSE);
622 fprintf(h, ")");
624 else
625 write_type_decl(h, arg->type, "%s", arg->name);
626 count++;
628 if (do_indent) indentation--;
631 static void write_cpp_method_def(const type_t *iface)
633 const func_t *cur;
635 if (!iface->funcs) return;
637 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
639 var_t *def = cur->def;
640 if (!is_callas(def->attrs)) {
641 indent(header, 0);
642 fprintf(header, "virtual ");
643 write_type_decl_left(header, def->type);
644 fprintf(header, " STDMETHODCALLTYPE ");
645 write_name(header, def);
646 fprintf(header, "(\n");
647 write_args(header, cur->args, iface->name, 2, TRUE);
648 fprintf(header, ") = 0;\n");
649 fprintf(header, "\n");
654 static void do_write_c_method_def(const type_t *iface, const char *name)
656 const func_t *cur;
658 if (iface->ref) do_write_c_method_def(iface->ref, name);
660 if (!iface->funcs) return;
661 indent(header, 0);
662 fprintf(header, "/*** %s methods ***/\n", iface->name);
663 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
665 const var_t *def = cur->def;
666 if (!is_callas(def->attrs)) {
667 indent(header, 0);
668 write_type_decl_left(header, def->type);
669 fprintf(header, " (STDMETHODCALLTYPE *");
670 write_name(header, def);
671 fprintf(header, ")(\n");
672 write_args(header, cur->args, name, 1, TRUE);
673 fprintf(header, ");\n");
674 fprintf(header, "\n");
679 static void write_c_method_def(const type_t *iface)
681 do_write_c_method_def(iface, iface->name);
684 static void write_c_disp_method_def(const type_t *iface)
686 do_write_c_method_def(iface->ref, iface->name);
689 static void write_method_proto(const type_t *iface)
691 const func_t *cur;
693 if (!iface->funcs) return;
694 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
696 const var_t *def = cur->def;
697 const var_t *cas = is_callas(def->attrs);
699 if (!is_local(def->attrs)) {
700 /* proxy prototype */
701 write_type_decl_left(header, def->type);
702 fprintf(header, " CALLBACK %s_", iface->name);
703 write_name(header, def);
704 fprintf(header, "_Proxy(\n");
705 write_args(header, cur->args, iface->name, 1, TRUE);
706 fprintf(header, ");\n");
707 /* stub prototype */
708 fprintf(header, "void __RPC_STUB %s_", iface->name);
709 write_name(header,def);
710 fprintf(header, "_Stub(\n");
711 fprintf(header, " IRpcStubBuffer* This,\n");
712 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
713 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
714 fprintf(header, " DWORD* pdwStubPhase);\n");
716 if (cas) {
717 const func_t *m;
718 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
719 if (!strcmp(m->def->name, cas->name)) break;
720 if (&m->entry != iface->funcs) {
721 const var_t *mdef = m->def;
722 /* proxy prototype - use local prototype */
723 write_type_decl_left(header, mdef->type);
724 fprintf(header, " CALLBACK %s_", iface->name);
725 write_name(header, mdef);
726 fprintf(header, "_Proxy(\n");
727 write_args(header, m->args, iface->name, 1, TRUE);
728 fprintf(header, ");\n");
729 /* stub prototype - use remotable prototype */
730 write_type_decl_left(header, def->type);
731 fprintf(header, " __RPC_STUB %s_", iface->name);
732 write_name(header, mdef);
733 fprintf(header, "_Stub(\n");
734 write_args(header, cur->args, iface->name, 1, TRUE);
735 fprintf(header, ");\n");
737 else {
738 parser_warning("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
744 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
746 var_t *def = fun->def;
748 /* FIXME: do we need to handle call_as? */
749 write_type_decl_left(header, def->type);
750 fprintf(header, " ");
751 write_prefix_name(header, prefix, def);
752 fprintf(header, "(\n");
753 if (fun->args)
754 write_args(header, fun->args, iface->name, 0, TRUE);
755 else
756 fprintf(header, " void");
757 fprintf(header, ");\n");
760 static void write_function_protos(const type_t *iface)
762 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
763 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
764 const var_t* explicit_handle_var;
765 const func_t *cur;
766 int prefixes_differ = strcmp(prefix_client, prefix_server);
768 if (!iface->funcs) return;
769 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
771 var_t *def = cur->def;
773 /* check for a defined binding handle */
774 explicit_handle_var = get_explicit_handle_var(cur);
775 if (explicit_handle) {
776 if (!explicit_handle_var) {
777 error("%s() does not define an explicit binding handle!\n", def->name);
778 return;
780 } else if (implicit_handle) {
781 if (explicit_handle_var) {
782 error("%s() must not define a binding handle!\n", def->name);
783 return;
787 if (prefixes_differ) {
788 fprintf(header, "/* client prototype */\n");
789 write_function_proto(iface, cur, prefix_client);
790 fprintf(header, "/* server prototype */\n");
792 write_function_proto(iface, cur, prefix_server);
796 void write_forward(type_t *iface)
798 /* C/C++ forwards should only be written for object interfaces, so if we
799 * have a full definition we only write one if we find [object] among the
800 * attributes - however, if we don't have a full definition at this point
801 * (i.e. this is an IDL forward), then we also assume that it is an object
802 * interface, since non-object interfaces shouldn't need forwards */
803 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
804 && !iface->written) {
805 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
806 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
807 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
808 fprintf(header, "#endif\n\n" );
809 iface->written = TRUE;
813 static void write_iface_guid(const type_t *iface)
815 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
816 write_guid(header, "IID", iface->name, uuid);
819 static void write_dispiface_guid(const type_t *iface)
821 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
822 write_guid(header, "DIID", iface->name, uuid);
825 static void write_coclass_guid(type_t *cocl)
827 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
828 write_guid(header, "CLSID", cocl->name, uuid);
831 static void write_com_interface(type_t *iface)
833 if (!iface->funcs && !iface->ref) {
834 parser_warning("%s has no methods", iface->name);
835 return;
838 fprintf(header, "/*****************************************************************************\n");
839 fprintf(header, " * %s interface\n", iface->name);
840 fprintf(header, " */\n");
841 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
842 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
843 write_iface_guid(iface);
844 write_forward(iface);
845 /* C++ interface */
846 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
847 if (iface->ref)
849 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
850 fprintf(header, "{\n");
851 indentation++;
852 write_cpp_method_def(iface);
853 indentation--;
854 fprintf(header, "};\n");
856 else
858 fprintf(header, "interface %s\n", iface->name);
859 fprintf(header, "{\n");
860 fprintf(header, " BEGIN_INTERFACE\n");
861 fprintf(header, "\n");
862 indentation++;
863 write_cpp_method_def(iface);
864 indentation--;
865 fprintf(header, " END_INTERFACE\n");
866 fprintf(header, "};\n");
868 fprintf(header, "#else\n");
869 /* C interface */
870 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
871 indentation++;
872 fprintf(header, " BEGIN_INTERFACE\n");
873 fprintf(header, "\n");
874 write_c_method_def(iface);
875 indentation--;
876 fprintf(header, " END_INTERFACE\n");
877 fprintf(header, "} %sVtbl;\n", iface->name);
878 fprintf(header, "interface %s {\n", iface->name);
879 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
880 fprintf(header, "};\n");
881 fprintf(header, "\n");
882 fprintf(header, "#ifdef COBJMACROS\n");
883 write_method_macro(iface, iface->name);
884 fprintf(header, "#endif\n");
885 fprintf(header, "\n");
886 fprintf(header, "#endif\n");
887 fprintf(header, "\n");
888 write_method_proto(iface);
889 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
892 static void write_rpc_interface(const type_t *iface)
894 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
895 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
896 static int allocate_written = 0;
898 if (!allocate_written)
900 allocate_written = 1;
901 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
902 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
905 fprintf(header, "/*****************************************************************************\n");
906 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
907 fprintf(header, " */\n");
908 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
909 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
910 if (iface->funcs)
912 write_iface_guid(iface);
913 if (var) fprintf(header, "extern handle_t %s;\n", var);
914 if (old_names)
916 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
917 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
919 else
921 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
922 prefix_client, iface->name, LOWORD(ver), HIWORD(ver));
923 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
924 prefix_server, iface->name, LOWORD(ver), HIWORD(ver));
926 write_function_protos(iface);
928 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
930 /* FIXME: server/client code */
933 void write_interface(type_t *iface)
935 if (is_object(iface->attrs))
936 write_com_interface(iface);
937 else
938 write_rpc_interface(iface);
941 void write_dispinterface(type_t *iface)
943 fprintf(header, "/*****************************************************************************\n");
944 fprintf(header, " * %s dispinterface\n", iface->name);
945 fprintf(header, " */\n");
946 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
947 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
948 write_dispiface_guid(iface);
949 write_forward(iface);
950 /* C++ interface */
951 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
952 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
953 fprintf(header, "{\n");
954 fprintf(header, "};\n");
955 fprintf(header, "#else\n");
956 /* C interface */
957 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
958 indentation++;
959 fprintf(header, " BEGIN_INTERFACE\n");
960 fprintf(header, "\n");
961 write_c_disp_method_def(iface);
962 indentation--;
963 fprintf(header, " END_INTERFACE\n");
964 fprintf(header, "} %sVtbl;\n", iface->name);
965 fprintf(header, "interface %s {\n", iface->name);
966 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
967 fprintf(header, "};\n");
968 fprintf(header, "\n");
969 fprintf(header, "#ifdef COBJMACROS\n");
970 write_method_macro(iface->ref, iface->name);
971 fprintf(header, "#endif\n");
972 fprintf(header, "\n");
973 fprintf(header, "#endif\n");
974 fprintf(header, "\n");
975 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
978 void write_coclass(type_t *cocl)
980 fprintf(header, "/*****************************************************************************\n");
981 fprintf(header, " * %s coclass\n", cocl->name);
982 fprintf(header, " */\n\n");
983 write_coclass_guid(cocl);
984 fprintf(header, "\n");
987 void write_coclass_forward(type_t *cocl)
989 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
990 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
991 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
992 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );