widl: Use is_string_type for detecting strings in write_typeformatstring_var to make...
[wine/multimedia.git] / tools / widl / header.c
blob5ffa549425bdca623a2041c6b033c7600248539f
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 <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
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);
41 static void indent(FILE *h, int delta)
43 int c;
44 if (delta < 0) indentation += delta;
45 for (c=0; c<indentation; c++) fprintf(h, " ");
46 if (delta > 0) indentation += delta;
49 int is_ptrchain_attr(const var_t *var, enum attr_type t)
51 if (is_attr(var->attrs, t))
52 return 1;
53 else
55 type_t *type = var->type;
56 for (;;)
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;
69 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
71 const type_t *t = type;
72 for (;;)
74 if (is_attr(t->attrs, attr))
75 return 1;
76 else if (t->kind == TKIND_ALIAS)
77 t = t->orig;
78 else return 0;
82 int is_attr(const attr_list_t *list, enum attr_type t)
84 const attr_t *attr;
85 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
86 if (attr->type == t) return 1;
87 return 0;
90 void *get_attrp(const attr_list_t *list, enum attr_type t)
92 const attr_t *attr;
93 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
94 if (attr->type == t) return attr->u.pval;
95 return NULL;
98 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
100 const attr_t *attr;
101 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
102 if (attr->type == t) return attr->u.ival;
103 return 0;
106 int is_void(const type_t *t)
108 if (!t->type && !t->ref) return 1;
109 return 0;
112 int is_conformant_array(const type_t *t)
114 return t->type == RPC_FC_CARRAY
115 || t->type == RPC_FC_CVARRAY
116 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
119 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
121 if (!uuid) return;
122 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
123 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
124 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
125 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
126 uuid->Data4[6], uuid->Data4[7]);
129 void write_name(FILE *h, const var_t *v)
131 if (is_attr( v->attrs, ATTR_PROPGET ))
132 fprintf(h, "get_" );
133 else if (is_attr( v->attrs, ATTR_PROPPUT ))
134 fprintf(h, "put_" );
135 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
136 fprintf(h, "putref_" );
137 fprintf(h, "%s", v->name);
140 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
142 fprintf(h, "%s", prefix);
143 write_name(h, v);
146 static void write_field(FILE *h, var_t *v)
148 if (!v) return;
149 if (v->type) {
150 const char *name = v->name;
151 if (name == NULL) {
152 switch (v->type->type) {
153 case RPC_FC_STRUCT:
154 case RPC_FC_CVSTRUCT:
155 case RPC_FC_CPSTRUCT:
156 case RPC_FC_CSTRUCT:
157 case RPC_FC_PSTRUCT:
158 case RPC_FC_BOGUS_STRUCT:
159 case RPC_FC_ENCAPSULATED_UNION:
160 name = "DUMMYSTRUCTNAME";
161 break;
162 case RPC_FC_NON_ENCAPSULATED_UNION:
163 name = "DUMMYUNIONNAME";
164 break;
165 default:
166 /* ? */
167 break;
170 indent(h, 0);
171 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
172 fprintf(h, ";\n");
176 static void write_fields(FILE *h, var_list_t *fields)
178 var_t *v;
179 if (!fields) return;
180 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
183 static void write_enums(FILE *h, var_list_t *enums)
185 var_t *v;
186 if (!enums) return;
187 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
189 if (v->name) {
190 indent(h, 0);
191 write_name(h, v);
192 if (v->eval) {
193 fprintf(h, " = ");
194 write_expr(h, v->eval, 0);
197 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
199 fprintf(h, "\n");
202 int needs_space_after(type_t *t)
204 return (t->kind == TKIND_ALIAS
205 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
208 void write_type_left(FILE *h, type_t *t, int declonly)
210 if (!h) return;
212 if (t->is_const) fprintf(h, "const ");
214 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
215 else if (t->declarray) write_type_left(h, t->ref, declonly);
216 else {
217 if (t->sign > 0) fprintf(h, "signed ");
218 else if (t->sign < 0) fprintf(h, "unsigned ");
219 switch (t->type) {
220 case RPC_FC_ENUM16:
221 case RPC_FC_ENUM32:
222 if (!declonly && t->defined && !t->written && !t->ignore) {
223 if (t->name) fprintf(h, "enum %s {\n", t->name);
224 else fprintf(h, "enum {\n");
225 t->written = TRUE;
226 indentation++;
227 write_enums(h, t->fields);
228 indent(h, -1);
229 fprintf(h, "}");
231 else fprintf(h, "enum %s", t->name ? t->name : "");
232 break;
233 case RPC_FC_STRUCT:
234 case RPC_FC_CVSTRUCT:
235 case RPC_FC_CPSTRUCT:
236 case RPC_FC_CSTRUCT:
237 case RPC_FC_PSTRUCT:
238 case RPC_FC_BOGUS_STRUCT:
239 case RPC_FC_ENCAPSULATED_UNION:
240 if (!declonly && t->defined && !t->written && !t->ignore) {
241 if (t->name) fprintf(h, "struct %s {\n", t->name);
242 else fprintf(h, "struct {\n");
243 t->written = TRUE;
244 indentation++;
245 write_fields(h, t->fields);
246 indent(h, -1);
247 fprintf(h, "}");
249 else fprintf(h, "struct %s", t->name ? t->name : "");
250 break;
251 case RPC_FC_NON_ENCAPSULATED_UNION:
252 if (!declonly && t->defined && !t->written && !t->ignore) {
253 if (t->name) fprintf(h, "union %s {\n", t->name);
254 else fprintf(h, "union {\n");
255 t->written = TRUE;
256 indentation++;
257 write_fields(h, t->fields);
258 indent(h, -1);
259 fprintf(h, "}");
261 else fprintf(h, "union %s", t->name ? t->name : "");
262 break;
263 case RPC_FC_RP:
264 case RPC_FC_UP:
265 case RPC_FC_FP:
266 case RPC_FC_OP:
267 case RPC_FC_CARRAY:
268 case RPC_FC_CVARRAY:
269 case RPC_FC_BOGUS_ARRAY:
270 write_type_left(h, t->ref, declonly);
271 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
272 break;
273 default:
274 fprintf(h, "%s", t->name);
279 void write_type_right(FILE *h, type_t *t, int is_field)
281 if (!h) return;
283 if (t->declarray) {
284 if (is_conformant_array(t)) {
285 fprintf(h, "[%s]", is_field ? "1" : "");
286 t = t->ref;
288 for ( ; t->declarray; t = t->ref)
289 fprintf(h, "[%lu]", t->dim);
293 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
294 const char *fmt, va_list args)
296 if (!h) return;
298 write_type_left(h, t, declonly);
299 if (fmt) {
300 if (needs_space_after(t))
301 fprintf(h, " ");
302 vfprintf(h, fmt, args);
304 write_type_right(h, t, is_field);
307 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
309 va_list args;
310 va_start(args, fmt);
311 write_type_v(f, t, field, FALSE, fmt, args);
312 va_end(args);
315 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
317 va_list args;
318 va_start(args, fmt);
319 write_type_v(f, t, FALSE, TRUE, fmt, args);
320 va_end(args);
323 void write_type_decl_left(FILE *f, type_t *t)
325 write_type_left(f, t, TRUE);
328 static int user_type_registered(const char *name)
330 user_type_t *ut;
331 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
332 if (!strcmp(name, ut->name))
333 return 1;
334 return 0;
337 static int context_handle_registered(const char *name)
339 context_handle_t *ch;
340 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
341 if (!strcmp(name, ch->name))
342 return 1;
343 return 0;
346 void check_for_user_types_and_context_handles(const var_list_t *list)
348 const var_t *v;
350 if (!list) return;
351 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
353 type_t *type;
354 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
355 const char *name = type->name;
356 if (type->user_types_registered) continue;
357 type->user_types_registered = 1;
358 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
359 if (!context_handle_registered(name))
361 context_handle_t *ch = xmalloc(sizeof(*ch));
362 ch->name = xstrdup(name);
363 list_add_tail(&context_handle_list, &ch->entry);
365 /* don't carry on parsing fields within this type */
366 break;
368 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
369 if (!user_type_registered(name))
371 user_type_t *ut = xmalloc(sizeof *ut);
372 ut->name = xstrdup(name);
373 list_add_tail(&user_type_list, &ut->entry);
375 /* don't carry on parsing fields within this type as we are already
376 * using a wire marshaled type */
377 break;
379 else
381 check_for_user_types_and_context_handles(type->fields);
387 void write_user_types(void)
389 user_type_t *ut;
390 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
392 const char *name = ut->name;
393 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
394 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
395 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
396 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
400 void write_context_handle_rundowns(void)
402 context_handle_t *ch;
403 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
405 const char *name = ch->name;
406 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
410 void write_typedef(type_t *type)
412 fprintf(header, "typedef ");
413 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
414 fprintf(header, ";\n");
417 void write_expr(FILE *h, const expr_t *e, int brackets)
419 switch (e->type) {
420 case EXPR_VOID:
421 break;
422 case EXPR_NUM:
423 fprintf(h, "%lu", e->u.lval);
424 break;
425 case EXPR_HEXNUM:
426 fprintf(h, "0x%lx", e->u.lval);
427 break;
428 case EXPR_DOUBLE:
429 fprintf(h, "%#.15g", e->u.dval);
430 break;
431 case EXPR_TRUEFALSE:
432 if (e->u.lval == 0)
433 fprintf(h, "FALSE");
434 else
435 fprintf(h, "TRUE");
436 break;
437 case EXPR_IDENTIFIER:
438 fprintf(h, "%s", e->u.sval);
439 break;
440 case EXPR_NEG:
441 fprintf(h, "-");
442 write_expr(h, e->ref, 1);
443 break;
444 case EXPR_NOT:
445 fprintf(h, "~");
446 write_expr(h, e->ref, 1);
447 break;
448 case EXPR_PPTR:
449 fprintf(h, "*");
450 write_expr(h, e->ref, 1);
451 break;
452 case EXPR_CAST:
453 fprintf(h, "(");
454 write_type_decl(h, e->u.tref, NULL);
455 fprintf(h, ")");
456 write_expr(h, e->ref, 1);
457 break;
458 case EXPR_SIZEOF:
459 fprintf(h, "sizeof(");
460 write_type_decl(h, e->u.tref, NULL);
461 fprintf(h, ")");
462 break;
463 case EXPR_SHL:
464 case EXPR_SHR:
465 case EXPR_MUL:
466 case EXPR_DIV:
467 case EXPR_ADD:
468 case EXPR_SUB:
469 case EXPR_AND:
470 case EXPR_OR:
471 if (brackets) fprintf(h, "(");
472 write_expr(h, e->ref, 1);
473 switch (e->type) {
474 case EXPR_SHL: fprintf(h, " << "); break;
475 case EXPR_SHR: fprintf(h, " >> "); break;
476 case EXPR_MUL: fprintf(h, " * "); break;
477 case EXPR_DIV: fprintf(h, " / "); break;
478 case EXPR_ADD: fprintf(h, " + "); break;
479 case EXPR_SUB: fprintf(h, " - "); break;
480 case EXPR_AND: fprintf(h, " & "); break;
481 case EXPR_OR: fprintf(h, " | "); break;
482 default: break;
484 write_expr(h, e->u.ext, 1);
485 if (brackets) fprintf(h, ")");
486 break;
487 case EXPR_COND:
488 if (brackets) fprintf(h, "(");
489 write_expr(h, e->ref, 1);
490 fprintf(h, " ? ");
491 write_expr(h, e->u.ext, 1);
492 fprintf(h, " : ");
493 write_expr(h, e->ext2, 1);
494 if (brackets) fprintf(h, ")");
495 break;
496 case EXPR_ADDRESSOF:
497 fprintf(h, "&");
498 write_expr(h, e->ref, 1);
499 break;
503 void write_constdef(const var_t *v)
505 fprintf(header, "#define %s (", v->name);
506 write_expr(header, v->eval, 0);
507 fprintf(header, ")\n\n");
510 void write_externdef(const var_t *v)
512 fprintf(header, "extern const ");
513 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
514 fprintf(header, ";\n\n");
517 void write_library(const char *name, const attr_list_t *attr)
519 const UUID *uuid = get_attrp(attr, ATTR_UUID);
520 fprintf(header, "\n");
521 write_guid(header, "LIBID", name, uuid);
522 fprintf(header, "\n");
526 const var_t* get_explicit_handle_var(const func_t* func)
528 const var_t* var;
530 if (!func->args)
531 return NULL;
533 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
534 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
535 return var;
537 return NULL;
540 int has_out_arg_or_return(const func_t *func)
542 const var_t *var;
544 if (!is_void(func->def->type))
545 return 1;
547 if (!func->args)
548 return 0;
550 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
551 if (is_attr(var->attrs, ATTR_OUT))
552 return 1;
554 return 0;
558 /********** INTERFACES **********/
560 int is_object(const attr_list_t *list)
562 const attr_t *attr;
563 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
564 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
565 return 0;
568 int is_local(const attr_list_t *a)
570 return is_attr(a, ATTR_LOCAL);
573 const var_t *is_callas(const attr_list_t *a)
575 return get_attrp(a, ATTR_CALLAS);
578 static void write_method_macro(const type_t *iface, const char *name)
580 const func_t *cur;
582 if (iface->ref) write_method_macro(iface->ref, name);
584 if (!iface->funcs) return;
586 fprintf(header, "/*** %s methods ***/\n", iface->name);
587 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
589 var_t *def = cur->def;
590 if (!is_callas(def->attrs)) {
591 const var_t *arg;
593 fprintf(header, "#define %s_", name);
594 write_name(header,def);
595 fprintf(header, "(This");
596 if (cur->args)
597 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
598 fprintf(header, ",%s", arg->name);
599 fprintf(header, ") ");
601 fprintf(header, "(This)->lpVtbl->");
602 write_name(header, def);
603 fprintf(header, "(This");
604 if (cur->args)
605 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
606 fprintf(header, ",%s", arg->name);
607 fprintf(header, ")\n");
612 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
614 const var_t *arg;
615 int count = 0;
617 if (do_indent)
619 indentation++;
620 indent(h, 0);
622 if (method == 1) {
623 fprintf(h, "%s* This", name);
624 count++;
626 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
627 if (count) {
628 if (do_indent)
630 fprintf(h, ",\n");
631 indent(h, 0);
633 else fprintf(h, ",");
635 if (arg->args)
637 write_type_decl_left(h, arg->type);
638 fprintf(h, " (STDMETHODCALLTYPE *");
639 write_name(h,arg);
640 fprintf(h, ")(");
641 write_args(h, arg->args, NULL, 0, FALSE);
642 fprintf(h, ")");
644 else
645 write_type_decl(h, arg->type, "%s", arg->name);
646 count++;
648 if (do_indent) indentation--;
651 static void write_cpp_method_def(const type_t *iface)
653 const func_t *cur;
655 if (!iface->funcs) return;
657 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
659 var_t *def = cur->def;
660 if (!is_callas(def->attrs)) {
661 indent(header, 0);
662 fprintf(header, "virtual ");
663 write_type_decl_left(header, def->type);
664 fprintf(header, " STDMETHODCALLTYPE ");
665 write_name(header, def);
666 fprintf(header, "(\n");
667 write_args(header, cur->args, iface->name, 2, TRUE);
668 fprintf(header, ") = 0;\n");
669 fprintf(header, "\n");
674 static void do_write_c_method_def(const type_t *iface, const char *name)
676 const func_t *cur;
678 if (iface->ref) do_write_c_method_def(iface->ref, name);
680 if (!iface->funcs) return;
681 indent(header, 0);
682 fprintf(header, "/*** %s methods ***/\n", iface->name);
683 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
685 const var_t *def = cur->def;
686 if (!is_callas(def->attrs)) {
687 indent(header, 0);
688 write_type_decl_left(header, def->type);
689 fprintf(header, " (STDMETHODCALLTYPE *");
690 write_name(header, def);
691 fprintf(header, ")(\n");
692 write_args(header, cur->args, name, 1, TRUE);
693 fprintf(header, ");\n");
694 fprintf(header, "\n");
699 static void write_c_method_def(const type_t *iface)
701 do_write_c_method_def(iface, iface->name);
704 static void write_c_disp_method_def(const type_t *iface)
706 do_write_c_method_def(iface->ref, iface->name);
709 static void write_method_proto(const type_t *iface)
711 const func_t *cur;
713 if (!iface->funcs) return;
714 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
716 const var_t *def = cur->def;
718 if (!is_local(def->attrs)) {
719 /* proxy prototype */
720 write_type_decl_left(header, def->type);
721 fprintf(header, " CALLBACK %s_", iface->name);
722 write_name(header, def);
723 fprintf(header, "_Proxy(\n");
724 write_args(header, cur->args, iface->name, 1, TRUE);
725 fprintf(header, ");\n");
726 /* stub prototype */
727 fprintf(header, "void __RPC_STUB %s_", iface->name);
728 write_name(header,def);
729 fprintf(header, "_Stub(\n");
730 fprintf(header, " IRpcStubBuffer* This,\n");
731 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
732 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
733 fprintf(header, " DWORD* pdwStubPhase);\n");
738 void write_locals(FILE *fp, const type_t *iface, int body)
740 static const char comment[]
741 = "/* WIDL-generated stub. You must provide an implementation for this. */";
742 const func_list_t *funcs = iface->funcs;
743 const func_t *cur;
745 if (!is_object(iface->attrs) || !funcs)
746 return;
748 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
749 const var_t *def = cur->def;
750 const var_t *cas = is_callas(def->attrs);
752 if (cas) {
753 const func_t *m;
754 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
755 if (!strcmp(m->def->name, cas->name))
756 break;
757 if (&m->entry != iface->funcs) {
758 const var_t *mdef = m->def;
759 /* proxy prototype - use local prototype */
760 write_type_decl_left(fp, mdef->type);
761 fprintf(fp, " CALLBACK %s_", iface->name);
762 write_name(fp, mdef);
763 fprintf(fp, "_Proxy(\n");
764 write_args(fp, m->args, iface->name, 1, TRUE);
765 fprintf(fp, ")");
766 if (body) {
767 type_t *rt = mdef->type;
768 fprintf(fp, "\n{\n");
769 fprintf(fp, " %s\n", comment);
770 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
771 fprintf(fp, " return E_NOTIMPL;\n");
772 else if (rt->type) {
773 fprintf(fp, " ");
774 write_type_decl(fp, rt, "rv");
775 fprintf(fp, ";\n");
776 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
777 fprintf(fp, " return rv;\n");
779 fprintf(fp, "}\n\n");
781 else
782 fprintf(fp, ";\n");
783 /* stub prototype - use remotable prototype */
784 write_type_decl_left(fp, def->type);
785 fprintf(fp, " __RPC_STUB %s_", iface->name);
786 write_name(fp, mdef);
787 fprintf(fp, "_Stub(\n");
788 write_args(fp, cur->args, iface->name, 1, TRUE);
789 fprintf(fp, ")");
790 if (body)
791 /* Remotable methods must all return HRESULTs. */
792 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
793 else
794 fprintf(fp, ";\n");
796 else
797 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
802 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
804 var_t *def = fun->def;
806 /* FIXME: do we need to handle call_as? */
807 write_type_decl_left(header, def->type);
808 fprintf(header, " ");
809 write_prefix_name(header, prefix, def);
810 fprintf(header, "(\n");
811 if (fun->args)
812 write_args(header, fun->args, iface->name, 0, TRUE);
813 else
814 fprintf(header, " void");
815 fprintf(header, ");\n");
818 static void write_function_protos(const type_t *iface)
820 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
821 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
822 const var_t* explicit_handle_var;
823 const func_t *cur;
824 int prefixes_differ = strcmp(prefix_client, prefix_server);
826 if (!iface->funcs) return;
827 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
829 var_t *def = cur->def;
831 /* check for a defined binding handle */
832 explicit_handle_var = get_explicit_handle_var(cur);
833 if (explicit_handle) {
834 if (!explicit_handle_var) {
835 error("%s() does not define an explicit binding handle!\n", def->name);
836 return;
838 } else if (implicit_handle) {
839 if (explicit_handle_var) {
840 error("%s() must not define a binding handle!\n", def->name);
841 return;
845 if (prefixes_differ) {
846 fprintf(header, "/* client prototype */\n");
847 write_function_proto(iface, cur, prefix_client);
848 fprintf(header, "/* server prototype */\n");
850 write_function_proto(iface, cur, prefix_server);
854 void write_forward(type_t *iface)
856 /* C/C++ forwards should only be written for object interfaces, so if we
857 * have a full definition we only write one if we find [object] among the
858 * attributes - however, if we don't have a full definition at this point
859 * (i.e. this is an IDL forward), then we also assume that it is an object
860 * interface, since non-object interfaces shouldn't need forwards */
861 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
862 && !iface->written) {
863 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
864 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
865 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
866 fprintf(header, "#endif\n\n" );
867 iface->written = TRUE;
871 static void write_iface_guid(const type_t *iface)
873 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
874 write_guid(header, "IID", iface->name, uuid);
877 static void write_dispiface_guid(const type_t *iface)
879 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
880 write_guid(header, "DIID", iface->name, uuid);
883 static void write_coclass_guid(type_t *cocl)
885 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
886 write_guid(header, "CLSID", cocl->name, uuid);
889 static void write_com_interface(type_t *iface)
891 if (!iface->funcs && !iface->ref) {
892 parser_warning("%s has no methods\n", iface->name);
893 return;
896 fprintf(header, "/*****************************************************************************\n");
897 fprintf(header, " * %s interface\n", iface->name);
898 fprintf(header, " */\n");
899 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
900 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
901 write_iface_guid(iface);
902 write_forward(iface);
903 /* C++ interface */
904 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
905 if (iface->ref)
907 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
908 fprintf(header, "{\n");
909 indentation++;
910 write_cpp_method_def(iface);
911 indentation--;
912 fprintf(header, "};\n");
914 else
916 fprintf(header, "interface %s\n", iface->name);
917 fprintf(header, "{\n");
918 fprintf(header, " BEGIN_INTERFACE\n");
919 fprintf(header, "\n");
920 indentation++;
921 write_cpp_method_def(iface);
922 indentation--;
923 fprintf(header, " END_INTERFACE\n");
924 fprintf(header, "};\n");
926 fprintf(header, "#else\n");
927 /* C interface */
928 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
929 indentation++;
930 fprintf(header, " BEGIN_INTERFACE\n");
931 fprintf(header, "\n");
932 write_c_method_def(iface);
933 indentation--;
934 fprintf(header, " END_INTERFACE\n");
935 fprintf(header, "} %sVtbl;\n", iface->name);
936 fprintf(header, "interface %s {\n", iface->name);
937 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
938 fprintf(header, "};\n");
939 fprintf(header, "\n");
940 fprintf(header, "#ifdef COBJMACROS\n");
941 write_method_macro(iface, iface->name);
942 fprintf(header, "#endif\n");
943 fprintf(header, "\n");
944 fprintf(header, "#endif\n");
945 fprintf(header, "\n");
946 write_method_proto(iface);
947 write_locals(header, iface, FALSE);
948 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
951 static void write_rpc_interface(const type_t *iface)
953 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
954 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
955 static int allocate_written = 0;
957 if (!allocate_written)
959 allocate_written = 1;
960 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
961 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
964 fprintf(header, "/*****************************************************************************\n");
965 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
966 fprintf(header, " */\n");
967 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
968 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
969 if (iface->funcs)
971 write_iface_guid(iface);
972 if (var) fprintf(header, "extern handle_t %s;\n", var);
973 if (old_names)
975 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
976 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
978 else
980 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
981 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
982 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
983 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
985 write_function_protos(iface);
987 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
989 /* FIXME: server/client code */
992 void write_interface(type_t *iface)
994 if (is_object(iface->attrs))
995 write_com_interface(iface);
996 else
997 write_rpc_interface(iface);
1000 void write_dispinterface(type_t *iface)
1002 fprintf(header, "/*****************************************************************************\n");
1003 fprintf(header, " * %s dispinterface\n", iface->name);
1004 fprintf(header, " */\n");
1005 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
1006 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
1007 write_dispiface_guid(iface);
1008 write_forward(iface);
1009 /* C++ interface */
1010 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1011 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
1012 fprintf(header, "{\n");
1013 fprintf(header, "};\n");
1014 fprintf(header, "#else\n");
1015 /* C interface */
1016 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1017 indentation++;
1018 fprintf(header, " BEGIN_INTERFACE\n");
1019 fprintf(header, "\n");
1020 write_c_disp_method_def(iface);
1021 indentation--;
1022 fprintf(header, " END_INTERFACE\n");
1023 fprintf(header, "} %sVtbl;\n", iface->name);
1024 fprintf(header, "interface %s {\n", iface->name);
1025 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1026 fprintf(header, "};\n");
1027 fprintf(header, "\n");
1028 fprintf(header, "#ifdef COBJMACROS\n");
1029 write_method_macro(iface->ref, iface->name);
1030 fprintf(header, "#endif\n");
1031 fprintf(header, "\n");
1032 fprintf(header, "#endif\n");
1033 fprintf(header, "\n");
1034 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
1037 void write_coclass(type_t *cocl)
1039 fprintf(header, "/*****************************************************************************\n");
1040 fprintf(header, " * %s coclass\n", cocl->name);
1041 fprintf(header, " */\n\n");
1042 write_coclass_guid(cocl);
1043 fprintf(header, "\n");
1046 void write_coclass_forward(type_t *cocl)
1048 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1049 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1050 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1051 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );