widl: Support parsing calling conventions for function identifiers.
[wine/wine-gecko.git] / tools / widl / header.c
blobd2f7ae4b92ecf88c2ebb0b2c46dc2af28b446a78
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 typedef struct _user_type_t generic_handle_t;
39 static int indentation = 0;
40 user_type_list_t user_type_list = LIST_INIT(user_type_list);
41 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
42 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
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_aliaschain_attr(const type_t *type, enum attr_type attr)
74 const type_t *t = type;
75 for (;;)
77 if (is_attr(t->attrs, attr))
78 return 1;
79 else if (t->kind == TKIND_ALIAS)
80 t = t->orig;
81 else return 0;
85 int is_attr(const attr_list_t *list, enum attr_type t)
87 const attr_t *attr;
88 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
89 if (attr->type == t) return 1;
90 return 0;
93 void *get_attrp(const attr_list_t *list, enum attr_type t)
95 const attr_t *attr;
96 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
97 if (attr->type == t) return attr->u.pval;
98 return NULL;
101 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
103 const attr_t *attr;
104 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
105 if (attr->type == t) return attr->u.ival;
106 return 0;
109 int is_void(const type_t *t)
111 if (!t->type && !t->ref) return 1;
112 return 0;
115 int is_conformant_array(const type_t *t)
117 return t->type == RPC_FC_CARRAY
118 || t->type == RPC_FC_CVARRAY
119 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
122 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
124 if (!uuid) return;
125 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
127 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
128 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
129 uuid->Data4[6], uuid->Data4[7]);
132 void write_name(FILE *h, const var_t *v)
134 if (is_attr( v->attrs, ATTR_PROPGET ))
135 fprintf(h, "get_" );
136 else if (is_attr( v->attrs, ATTR_PROPPUT ))
137 fprintf(h, "put_" );
138 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
139 fprintf(h, "putref_" );
140 fprintf(h, "%s", v->name);
143 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
145 fprintf(h, "%s", prefix);
146 write_name(h, v);
149 static void write_field(FILE *h, var_t *v)
151 if (!v) return;
152 if (v->type) {
153 const char *name = v->name;
154 if (name == NULL) {
155 switch (v->type->type) {
156 case RPC_FC_STRUCT:
157 case RPC_FC_CVSTRUCT:
158 case RPC_FC_CPSTRUCT:
159 case RPC_FC_CSTRUCT:
160 case RPC_FC_PSTRUCT:
161 case RPC_FC_BOGUS_STRUCT:
162 case RPC_FC_ENCAPSULATED_UNION:
163 name = "DUMMYSTRUCTNAME";
164 break;
165 case RPC_FC_NON_ENCAPSULATED_UNION:
166 name = "DUMMYUNIONNAME";
167 break;
168 default:
169 /* ? */
170 break;
173 indent(h, 0);
174 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
175 fprintf(h, ";\n");
179 static void write_fields(FILE *h, var_list_t *fields)
181 var_t *v;
182 if (!fields) return;
183 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
186 static void write_enums(FILE *h, var_list_t *enums)
188 var_t *v;
189 if (!enums) return;
190 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
192 if (v->name) {
193 indent(h, 0);
194 write_name(h, v);
195 if (v->eval) {
196 fprintf(h, " = ");
197 write_expr(h, v->eval, 0);
200 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
202 fprintf(h, "\n");
205 int needs_space_after(type_t *t)
207 return (t->kind == TKIND_ALIAS
208 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
211 void write_type_left(FILE *h, type_t *t, int declonly)
213 if (!h) return;
215 if (t->is_const) fprintf(h, "const ");
217 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
218 else if (t->declarray) write_type_left(h, t->ref, declonly);
219 else {
220 if (t->sign > 0) fprintf(h, "signed ");
221 else if (t->sign < 0) fprintf(h, "unsigned ");
222 switch (t->type) {
223 case RPC_FC_ENUM16:
224 case RPC_FC_ENUM32:
225 if (!declonly && t->defined && !t->written && !t->ignore) {
226 if (t->name) fprintf(h, "enum %s {\n", t->name);
227 else fprintf(h, "enum {\n");
228 t->written = TRUE;
229 indentation++;
230 write_enums(h, t->fields_or_args);
231 indent(h, -1);
232 fprintf(h, "}");
234 else fprintf(h, "enum %s", t->name ? t->name : "");
235 break;
236 case RPC_FC_STRUCT:
237 case RPC_FC_CVSTRUCT:
238 case RPC_FC_CPSTRUCT:
239 case RPC_FC_CSTRUCT:
240 case RPC_FC_PSTRUCT:
241 case RPC_FC_BOGUS_STRUCT:
242 case RPC_FC_ENCAPSULATED_UNION:
243 if (!declonly && t->defined && !t->written && !t->ignore) {
244 if (t->name) fprintf(h, "struct %s {\n", t->name);
245 else fprintf(h, "struct {\n");
246 t->written = TRUE;
247 indentation++;
248 write_fields(h, t->fields_or_args);
249 indent(h, -1);
250 fprintf(h, "}");
252 else fprintf(h, "struct %s", t->name ? t->name : "");
253 break;
254 case RPC_FC_NON_ENCAPSULATED_UNION:
255 if (!declonly && t->defined && !t->written && !t->ignore) {
256 if (t->name) fprintf(h, "union %s {\n", t->name);
257 else fprintf(h, "union {\n");
258 t->written = TRUE;
259 indentation++;
260 write_fields(h, t->fields_or_args);
261 indent(h, -1);
262 fprintf(h, "}");
264 else fprintf(h, "union %s", t->name ? t->name : "");
265 break;
266 case RPC_FC_RP:
267 case RPC_FC_UP:
268 case RPC_FC_FP:
269 case RPC_FC_OP:
270 case RPC_FC_CARRAY:
271 case RPC_FC_CVARRAY:
272 case RPC_FC_BOGUS_ARRAY:
273 write_type_left(h, t->ref, declonly);
274 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
275 break;
276 default:
277 fprintf(h, "%s", t->name);
282 void write_type_right(FILE *h, type_t *t, int is_field)
284 if (!h) return;
286 if (t->declarray) {
287 if (is_conformant_array(t)) {
288 fprintf(h, "[%s]", is_field ? "1" : "");
289 t = t->ref;
291 for ( ; t->declarray; t = t->ref)
292 fprintf(h, "[%lu]", t->dim);
296 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
297 const char *fmt, va_list args)
299 if (!h) return;
301 if (t->type == RPC_FC_FUNCTION) {
302 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
303 if (!callconv) callconv = "";
304 write_type_left(h, t->ref, declonly);
305 fprintf(h, " (%s *", callconv);
306 } else
307 write_type_left(h, t, declonly);
308 if (fmt) {
309 if (needs_space_after(t))
310 fprintf(h, " ");
311 vfprintf(h, fmt, args);
313 if (t->type == RPC_FC_FUNCTION) {
314 fprintf(h, ")(");
315 write_args(h, t->fields_or_args, NULL, 0, FALSE);
316 fprintf(h, ")");
317 } else
318 write_type_right(h, t, is_field);
321 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
323 va_list args;
324 va_start(args, fmt);
325 write_type_v(f, t, field, FALSE, fmt, args);
326 va_end(args);
329 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
331 va_list args;
332 va_start(args, fmt);
333 write_type_v(f, t, FALSE, TRUE, fmt, args);
334 va_end(args);
337 void write_type_decl_left(FILE *f, type_t *t)
339 write_type_left(f, t, TRUE);
342 static int user_type_registered(const char *name)
344 user_type_t *ut;
345 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
346 if (!strcmp(name, ut->name))
347 return 1;
348 return 0;
351 static int context_handle_registered(const char *name)
353 context_handle_t *ch;
354 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
355 if (!strcmp(name, ch->name))
356 return 1;
357 return 0;
360 static int generic_handle_registered(const char *name)
362 generic_handle_t *gh;
363 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
364 if (!strcmp(name, gh->name))
365 return 1;
366 return 0;
369 /* check for types which require additional prototypes to be generated in the
370 * header */
371 void check_for_additional_prototype_types(const var_list_t *list)
373 const var_t *v;
375 if (!list) return;
376 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
378 type_t *type;
379 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
380 const char *name = type->name;
381 if (type->user_types_registered) continue;
382 type->user_types_registered = 1;
383 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
384 if (!context_handle_registered(name))
386 context_handle_t *ch = xmalloc(sizeof(*ch));
387 ch->name = xstrdup(name);
388 list_add_tail(&context_handle_list, &ch->entry);
390 /* don't carry on parsing fields within this type */
391 break;
393 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
394 if (!generic_handle_registered(name))
396 generic_handle_t *gh = xmalloc(sizeof(*gh));
397 gh->name = xstrdup(name);
398 list_add_tail(&generic_handle_list, &gh->entry);
400 /* don't carry on parsing fields within this type */
401 break;
403 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
404 if (!user_type_registered(name))
406 user_type_t *ut = xmalloc(sizeof *ut);
407 ut->name = xstrdup(name);
408 list_add_tail(&user_type_list, &ut->entry);
410 /* don't carry on parsing fields within this type as we are already
411 * using a wire marshaled type */
412 break;
414 else
416 check_for_additional_prototype_types(type->fields_or_args);
422 void write_user_types(void)
424 user_type_t *ut;
425 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
427 const char *name = ut->name;
428 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
429 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
430 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
431 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
435 void write_context_handle_rundowns(void)
437 context_handle_t *ch;
438 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
440 const char *name = ch->name;
441 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
445 void write_generic_handle_routines(void)
447 generic_handle_t *gh;
448 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
450 const char *name = gh->name;
451 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
452 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
456 void write_typedef(type_t *type)
458 fprintf(header, "typedef ");
459 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
460 fprintf(header, ";\n");
463 void write_expr(FILE *h, const expr_t *e, int brackets)
465 switch (e->type) {
466 case EXPR_VOID:
467 break;
468 case EXPR_NUM:
469 fprintf(h, "%lu", e->u.lval);
470 break;
471 case EXPR_HEXNUM:
472 fprintf(h, "0x%lx", e->u.lval);
473 break;
474 case EXPR_DOUBLE:
475 fprintf(h, "%#.15g", e->u.dval);
476 break;
477 case EXPR_TRUEFALSE:
478 if (e->u.lval == 0)
479 fprintf(h, "FALSE");
480 else
481 fprintf(h, "TRUE");
482 break;
483 case EXPR_IDENTIFIER:
484 fprintf(h, "%s", e->u.sval);
485 break;
486 case EXPR_NEG:
487 fprintf(h, "-");
488 write_expr(h, e->ref, 1);
489 break;
490 case EXPR_NOT:
491 fprintf(h, "~");
492 write_expr(h, e->ref, 1);
493 break;
494 case EXPR_PPTR:
495 fprintf(h, "*");
496 write_expr(h, e->ref, 1);
497 break;
498 case EXPR_CAST:
499 fprintf(h, "(");
500 write_type_decl(h, e->u.tref, NULL);
501 fprintf(h, ")");
502 write_expr(h, e->ref, 1);
503 break;
504 case EXPR_SIZEOF:
505 fprintf(h, "sizeof(");
506 write_type_decl(h, e->u.tref, NULL);
507 fprintf(h, ")");
508 break;
509 case EXPR_SHL:
510 case EXPR_SHR:
511 case EXPR_MUL:
512 case EXPR_DIV:
513 case EXPR_ADD:
514 case EXPR_SUB:
515 case EXPR_AND:
516 case EXPR_OR:
517 if (brackets) fprintf(h, "(");
518 write_expr(h, e->ref, 1);
519 switch (e->type) {
520 case EXPR_SHL: fprintf(h, " << "); break;
521 case EXPR_SHR: fprintf(h, " >> "); break;
522 case EXPR_MUL: fprintf(h, " * "); break;
523 case EXPR_DIV: fprintf(h, " / "); break;
524 case EXPR_ADD: fprintf(h, " + "); break;
525 case EXPR_SUB: fprintf(h, " - "); break;
526 case EXPR_AND: fprintf(h, " & "); break;
527 case EXPR_OR: fprintf(h, " | "); break;
528 default: break;
530 write_expr(h, e->u.ext, 1);
531 if (brackets) fprintf(h, ")");
532 break;
533 case EXPR_COND:
534 if (brackets) fprintf(h, "(");
535 write_expr(h, e->ref, 1);
536 fprintf(h, " ? ");
537 write_expr(h, e->u.ext, 1);
538 fprintf(h, " : ");
539 write_expr(h, e->ext2, 1);
540 if (brackets) fprintf(h, ")");
541 break;
542 case EXPR_ADDRESSOF:
543 fprintf(h, "&");
544 write_expr(h, e->ref, 1);
545 break;
549 void write_constdef(const var_t *v)
551 fprintf(header, "#define %s (", v->name);
552 write_expr(header, v->eval, 0);
553 fprintf(header, ")\n\n");
556 void write_externdef(const var_t *v)
558 fprintf(header, "extern const ");
559 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
560 fprintf(header, ";\n\n");
563 void write_library(const char *name, const attr_list_t *attr)
565 const UUID *uuid = get_attrp(attr, ATTR_UUID);
566 fprintf(header, "\n");
567 write_guid(header, "LIBID", name, uuid);
568 fprintf(header, "\n");
572 const var_t* get_explicit_handle_var(const func_t* func)
574 const var_t* var;
576 if (!func->args)
577 return NULL;
579 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
580 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
581 return var;
583 return NULL;
586 const type_t* get_explicit_generic_handle_type(const var_t* var)
588 const type_t *t;
589 for (t = var->type; is_ptr(t); t = t->ref)
590 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
591 return t;
592 return NULL;
595 const var_t* get_explicit_generic_handle_var(const func_t* func)
597 const var_t* var;
599 if (!func->args)
600 return NULL;
602 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
603 if (get_explicit_generic_handle_type(var))
604 return var;
606 return NULL;
609 int has_out_arg_or_return(const func_t *func)
611 const var_t *var;
613 if (!is_void(get_func_return_type(func)))
614 return 1;
616 if (!func->args)
617 return 0;
619 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
620 if (is_attr(var->attrs, ATTR_OUT))
621 return 1;
623 return 0;
627 /********** INTERFACES **********/
629 int is_object(const attr_list_t *list)
631 const attr_t *attr;
632 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
633 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
634 return 0;
637 int is_local(const attr_list_t *a)
639 return is_attr(a, ATTR_LOCAL);
642 const var_t *is_callas(const attr_list_t *a)
644 return get_attrp(a, ATTR_CALLAS);
647 static void write_method_macro(const type_t *iface, const char *name)
649 const func_t *cur;
651 if (iface->ref) write_method_macro(iface->ref, name);
653 if (!iface->funcs) return;
655 fprintf(header, "/*** %s methods ***/\n", iface->name);
656 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
658 var_t *def = cur->def;
659 if (!is_callas(def->attrs)) {
660 const var_t *arg;
662 fprintf(header, "#define %s_", name);
663 write_name(header,def);
664 fprintf(header, "(This");
665 if (cur->args)
666 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
667 fprintf(header, ",%s", arg->name);
668 fprintf(header, ") ");
670 fprintf(header, "(This)->lpVtbl->");
671 write_name(header, def);
672 fprintf(header, "(This");
673 if (cur->args)
674 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
675 fprintf(header, ",%s", arg->name);
676 fprintf(header, ")\n");
681 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
683 const var_t *arg;
684 int count = 0;
686 if (do_indent)
688 indentation++;
689 indent(h, 0);
691 if (method == 1) {
692 fprintf(h, "%s* This", name);
693 count++;
695 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
696 if (count) {
697 if (do_indent)
699 fprintf(h, ",\n");
700 indent(h, 0);
702 else fprintf(h, ",");
704 write_type_decl(h, arg->type, "%s", arg->name);
705 count++;
707 if (do_indent) indentation--;
710 static void write_cpp_method_def(const type_t *iface)
712 const func_t *cur;
714 if (!iface->funcs) return;
716 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
718 var_t *def = cur->def;
719 if (!is_callas(def->attrs)) {
720 indent(header, 0);
721 fprintf(header, "virtual ");
722 write_type_decl_left(header, get_func_return_type(cur));
723 fprintf(header, " STDMETHODCALLTYPE ");
724 write_name(header, def);
725 fprintf(header, "(\n");
726 write_args(header, cur->args, iface->name, 2, TRUE);
727 fprintf(header, ") = 0;\n");
728 fprintf(header, "\n");
733 static void do_write_c_method_def(const type_t *iface, const char *name)
735 const func_t *cur;
737 if (iface->ref) do_write_c_method_def(iface->ref, name);
739 if (!iface->funcs) return;
740 indent(header, 0);
741 fprintf(header, "/*** %s methods ***/\n", iface->name);
742 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
744 const var_t *def = cur->def;
745 if (!is_callas(def->attrs)) {
746 indent(header, 0);
747 write_type_decl_left(header, get_func_return_type(cur));
748 fprintf(header, " (STDMETHODCALLTYPE *");
749 write_name(header, def);
750 fprintf(header, ")(\n");
751 write_args(header, cur->args, name, 1, TRUE);
752 fprintf(header, ");\n");
753 fprintf(header, "\n");
758 static void write_c_method_def(const type_t *iface)
760 do_write_c_method_def(iface, iface->name);
763 static void write_c_disp_method_def(const type_t *iface)
765 do_write_c_method_def(iface->ref, iface->name);
768 static void write_method_proto(const type_t *iface)
770 const func_t *cur;
772 if (!iface->funcs) return;
773 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
775 const var_t *def = cur->def;
777 if (!is_local(def->attrs)) {
778 /* proxy prototype */
779 write_type_decl_left(header, get_func_return_type(cur));
780 fprintf(header, " CALLBACK %s_", iface->name);
781 write_name(header, def);
782 fprintf(header, "_Proxy(\n");
783 write_args(header, cur->args, iface->name, 1, TRUE);
784 fprintf(header, ");\n");
785 /* stub prototype */
786 fprintf(header, "void __RPC_STUB %s_", iface->name);
787 write_name(header,def);
788 fprintf(header, "_Stub(\n");
789 fprintf(header, " IRpcStubBuffer* This,\n");
790 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
791 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
792 fprintf(header, " DWORD* pdwStubPhase);\n");
797 void write_locals(FILE *fp, const type_t *iface, int body)
799 static const char comment[]
800 = "/* WIDL-generated stub. You must provide an implementation for this. */";
801 const func_list_t *funcs = iface->funcs;
802 const func_t *cur;
804 if (!is_object(iface->attrs) || !funcs)
805 return;
807 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
808 const var_t *def = cur->def;
809 const var_t *cas = is_callas(def->attrs);
811 if (cas) {
812 const func_t *m;
813 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
814 if (!strcmp(m->def->name, cas->name))
815 break;
816 if (&m->entry != iface->funcs) {
817 const var_t *mdef = m->def;
818 /* proxy prototype - use local prototype */
819 write_type_decl_left(fp, get_func_return_type(m));
820 fprintf(fp, " CALLBACK %s_", iface->name);
821 write_name(fp, mdef);
822 fprintf(fp, "_Proxy(\n");
823 write_args(fp, m->args, iface->name, 1, TRUE);
824 fprintf(fp, ")");
825 if (body) {
826 type_t *rt = get_func_return_type(m);
827 fprintf(fp, "\n{\n");
828 fprintf(fp, " %s\n", comment);
829 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
830 fprintf(fp, " return E_NOTIMPL;\n");
831 else if (rt->type) {
832 fprintf(fp, " ");
833 write_type_decl(fp, rt, "rv");
834 fprintf(fp, ";\n");
835 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
836 fprintf(fp, " return rv;\n");
838 fprintf(fp, "}\n\n");
840 else
841 fprintf(fp, ";\n");
842 /* stub prototype - use remotable prototype */
843 write_type_decl_left(fp, get_func_return_type(cur));
844 fprintf(fp, " __RPC_STUB %s_", iface->name);
845 write_name(fp, mdef);
846 fprintf(fp, "_Stub(\n");
847 write_args(fp, cur->args, iface->name, 1, TRUE);
848 fprintf(fp, ")");
849 if (body)
850 /* Remotable methods must all return HRESULTs. */
851 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
852 else
853 fprintf(fp, ";\n");
855 else
856 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
861 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
863 var_t *def = fun->def;
865 /* FIXME: do we need to handle call_as? */
866 write_type_decl_left(header, get_func_return_type(fun));
867 fprintf(header, " ");
868 write_prefix_name(header, prefix, def);
869 fprintf(header, "(\n");
870 if (fun->args)
871 write_args(header, fun->args, iface->name, 0, TRUE);
872 else
873 fprintf(header, " void");
874 fprintf(header, ");\n");
877 static void write_function_protos(const type_t *iface)
879 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
880 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
881 const var_t* explicit_handle_var;
882 const func_t *cur;
883 int prefixes_differ = strcmp(prefix_client, prefix_server);
885 if (!iface->funcs) return;
886 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
888 var_t *def = cur->def;
890 /* check for a defined binding handle */
891 explicit_handle_var = get_explicit_handle_var(cur);
892 if (explicit_handle) {
893 if (!explicit_handle_var) {
894 error("%s() does not define an explicit binding handle!\n", def->name);
895 return;
897 } else if (implicit_handle) {
898 if (explicit_handle_var) {
899 error("%s() must not define a binding handle!\n", def->name);
900 return;
904 if (prefixes_differ) {
905 fprintf(header, "/* client prototype */\n");
906 write_function_proto(iface, cur, prefix_client);
907 fprintf(header, "/* server prototype */\n");
909 write_function_proto(iface, cur, prefix_server);
913 void write_forward(type_t *iface)
915 /* C/C++ forwards should only be written for object interfaces, so if we
916 * have a full definition we only write one if we find [object] among the
917 * attributes - however, if we don't have a full definition at this point
918 * (i.e. this is an IDL forward), then we also assume that it is an object
919 * interface, since non-object interfaces shouldn't need forwards */
920 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
921 && !iface->written) {
922 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
923 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
924 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
925 fprintf(header, "#endif\n\n" );
926 iface->written = TRUE;
930 static void write_iface_guid(const type_t *iface)
932 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
933 write_guid(header, "IID", iface->name, uuid);
936 static void write_dispiface_guid(const type_t *iface)
938 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
939 write_guid(header, "DIID", iface->name, uuid);
942 static void write_coclass_guid(type_t *cocl)
944 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
945 write_guid(header, "CLSID", cocl->name, uuid);
948 static void write_com_interface(type_t *iface)
950 if (!iface->funcs && !iface->ref) {
951 parser_warning("%s has no methods\n", iface->name);
952 return;
955 fprintf(header, "/*****************************************************************************\n");
956 fprintf(header, " * %s interface\n", iface->name);
957 fprintf(header, " */\n");
958 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
959 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
960 write_iface_guid(iface);
961 write_forward(iface);
962 /* C++ interface */
963 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
964 if (iface->ref)
966 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
967 fprintf(header, "{\n");
968 indentation++;
969 write_cpp_method_def(iface);
970 indentation--;
971 fprintf(header, "};\n");
973 else
975 fprintf(header, "interface %s\n", iface->name);
976 fprintf(header, "{\n");
977 fprintf(header, " BEGIN_INTERFACE\n");
978 fprintf(header, "\n");
979 indentation++;
980 write_cpp_method_def(iface);
981 indentation--;
982 fprintf(header, " END_INTERFACE\n");
983 fprintf(header, "};\n");
985 fprintf(header, "#else\n");
986 /* C interface */
987 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
988 indentation++;
989 fprintf(header, " BEGIN_INTERFACE\n");
990 fprintf(header, "\n");
991 write_c_method_def(iface);
992 indentation--;
993 fprintf(header, " END_INTERFACE\n");
994 fprintf(header, "} %sVtbl;\n", iface->name);
995 fprintf(header, "interface %s {\n", iface->name);
996 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
997 fprintf(header, "};\n");
998 fprintf(header, "\n");
999 fprintf(header, "#ifdef COBJMACROS\n");
1000 write_method_macro(iface, iface->name);
1001 fprintf(header, "#endif\n");
1002 fprintf(header, "\n");
1003 fprintf(header, "#endif\n");
1004 fprintf(header, "\n");
1005 write_method_proto(iface);
1006 write_locals(header, iface, FALSE);
1007 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1010 static void write_rpc_interface(const type_t *iface)
1012 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1013 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1014 static int allocate_written = 0;
1016 if (!allocate_written)
1018 allocate_written = 1;
1019 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
1020 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1023 fprintf(header, "/*****************************************************************************\n");
1024 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1025 fprintf(header, " */\n");
1026 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1027 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1028 if (iface->funcs)
1030 write_iface_guid(iface);
1031 if (var) fprintf(header, "extern handle_t %s;\n", var);
1032 if (old_names)
1034 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1035 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1037 else
1039 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1040 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1041 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1042 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1044 write_function_protos(iface);
1046 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1048 /* FIXME: server/client code */
1051 void write_interface(type_t *iface)
1053 if (is_object(iface->attrs))
1054 write_com_interface(iface);
1055 else
1056 write_rpc_interface(iface);
1059 void write_dispinterface(type_t *iface)
1061 fprintf(header, "/*****************************************************************************\n");
1062 fprintf(header, " * %s dispinterface\n", iface->name);
1063 fprintf(header, " */\n");
1064 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
1065 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
1066 write_dispiface_guid(iface);
1067 write_forward(iface);
1068 /* C++ interface */
1069 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1070 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
1071 fprintf(header, "{\n");
1072 fprintf(header, "};\n");
1073 fprintf(header, "#else\n");
1074 /* C interface */
1075 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1076 indentation++;
1077 fprintf(header, " BEGIN_INTERFACE\n");
1078 fprintf(header, "\n");
1079 write_c_disp_method_def(iface);
1080 indentation--;
1081 fprintf(header, " END_INTERFACE\n");
1082 fprintf(header, "} %sVtbl;\n", iface->name);
1083 fprintf(header, "interface %s {\n", iface->name);
1084 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1085 fprintf(header, "};\n");
1086 fprintf(header, "\n");
1087 fprintf(header, "#ifdef COBJMACROS\n");
1088 write_method_macro(iface->ref, iface->name);
1089 fprintf(header, "#endif\n");
1090 fprintf(header, "\n");
1091 fprintf(header, "#endif\n");
1092 fprintf(header, "\n");
1093 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
1096 void write_coclass(type_t *cocl)
1098 fprintf(header, "/*****************************************************************************\n");
1099 fprintf(header, " * %s coclass\n", cocl->name);
1100 fprintf(header, " */\n\n");
1101 write_coclass_guid(cocl);
1102 fprintf(header, "\n");
1105 void write_coclass_forward(type_t *cocl)
1107 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1108 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1109 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1110 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );