push ab3cc77eb114d2bd400734a9dccad24563f936a6
[wine/hacks.git] / tools / widl / header.c
blob971b30fc54333cfdf18a6a0be9b9649cc51c2766
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;
42 static void indent(FILE *h, int delta)
44 int c;
45 if (delta < 0) indentation += delta;
46 for (c=0; c<indentation; c++) fprintf(h, " ");
47 if (delta > 0) indentation += delta;
50 int is_ptrchain_attr(const var_t *var, enum attr_type t)
52 if (is_attr(var->attrs, t))
53 return 1;
54 else
56 type_t *type = var->type;
57 for (;;)
59 if (is_attr(type->attrs, t))
60 return 1;
61 else if (type->kind == TKIND_ALIAS)
62 type = type->orig;
63 else if (is_ptr(type))
64 type = type->ref;
65 else return 0;
70 int is_attr(const attr_list_t *list, enum attr_type t)
72 const attr_t *attr;
73 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
74 if (attr->type == t) return 1;
75 return 0;
78 void *get_attrp(const attr_list_t *list, enum attr_type t)
80 const attr_t *attr;
81 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
82 if (attr->type == t) return attr->u.pval;
83 return NULL;
86 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
88 const attr_t *attr;
89 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
90 if (attr->type == t) return attr->u.ival;
91 return 0;
94 int is_void(const type_t *t)
96 if (!t->type && !t->ref) return 1;
97 return 0;
100 int is_conformant_array(const type_t *t)
102 return t->type == RPC_FC_CARRAY || t->type == RPC_FC_CVARRAY;
105 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
107 if (!uuid) return;
108 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
109 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
110 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
111 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
112 uuid->Data4[6], uuid->Data4[7]);
115 void write_name(FILE *h, const var_t *v)
117 if (is_attr( v->attrs, ATTR_PROPGET ))
118 fprintf(h, "get_" );
119 else if (is_attr( v->attrs, ATTR_PROPPUT ))
120 fprintf(h, "put_" );
121 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
122 fprintf(h, "putref_" );
123 fprintf(h, "%s", v->name);
126 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
128 fprintf(h, "%s", prefix);
129 write_name(h, v);
132 static void write_field(FILE *h, var_t *v)
134 if (!v) return;
135 if (v->type) {
136 const char *name = v->name;
137 if (name == NULL) {
138 switch (v->type->type) {
139 case RPC_FC_STRUCT:
140 case RPC_FC_CVSTRUCT:
141 case RPC_FC_CPSTRUCT:
142 case RPC_FC_CSTRUCT:
143 case RPC_FC_PSTRUCT:
144 case RPC_FC_BOGUS_STRUCT:
145 case RPC_FC_ENCAPSULATED_UNION:
146 name = "DUMMYSTRUCTNAME";
147 break;
148 case RPC_FC_NON_ENCAPSULATED_UNION:
149 name = "DUMMYUNIONNAME";
150 break;
151 default:
152 /* ? */
153 break;
156 indent(h, 0);
157 write_type(h, v->type, TRUE, "%s", name);
158 fprintf(h, ";\n");
162 static void write_fields(FILE *h, var_list_t *fields)
164 var_t *v;
165 if (!fields) return;
166 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
169 static void write_enums(FILE *h, var_list_t *enums)
171 var_t *v;
172 if (!enums) return;
173 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
175 if (v->name) {
176 indent(h, 0);
177 write_name(h, v);
178 if (v->eval) {
179 fprintf(h, " = ");
180 write_expr(h, v->eval, 0);
183 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
185 fprintf(h, "\n");
188 int needs_space_after(type_t *t)
190 return (t->kind == TKIND_ALIAS
191 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
194 void write_type_left(FILE *h, type_t *t)
196 if (t->is_const) fprintf(h, "const ");
198 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
199 else if (t->declarray) write_type_left(h, t->ref);
200 else {
201 if (t->sign > 0) fprintf(h, "signed ");
202 else if (t->sign < 0) fprintf(h, "unsigned ");
203 switch (t->type) {
204 case RPC_FC_ENUM16:
205 case RPC_FC_ENUM32:
206 if (t->defined && !t->written && !t->ignore) {
207 if (t->name) fprintf(h, "enum %s {\n", t->name);
208 else fprintf(h, "enum {\n");
209 t->written = TRUE;
210 indentation++;
211 write_enums(h, t->fields);
212 indent(h, -1);
213 fprintf(h, "}");
215 else fprintf(h, "enum %s", t->name);
216 break;
217 case RPC_FC_STRUCT:
218 case RPC_FC_CVSTRUCT:
219 case RPC_FC_CPSTRUCT:
220 case RPC_FC_CSTRUCT:
221 case RPC_FC_PSTRUCT:
222 case RPC_FC_BOGUS_STRUCT:
223 case RPC_FC_ENCAPSULATED_UNION:
224 if (t->defined && !t->written && !t->ignore) {
225 if (t->name) fprintf(h, "struct %s {\n", t->name);
226 else fprintf(h, "struct {\n");
227 t->written = TRUE;
228 indentation++;
229 write_fields(h, t->fields);
230 indent(h, -1);
231 fprintf(h, "}");
233 else fprintf(h, "struct %s", t->name);
234 break;
235 case RPC_FC_NON_ENCAPSULATED_UNION:
236 if (t->defined && !t->written && !t->ignore) {
237 if (t->name) fprintf(h, "union %s {\n", t->name);
238 else fprintf(h, "union {\n");
239 t->written = TRUE;
240 indentation++;
241 write_fields(h, t->fields);
242 indent(h, -1);
243 fprintf(h, "}");
245 else fprintf(h, "union %s", t->name);
246 break;
247 case RPC_FC_RP:
248 case RPC_FC_UP:
249 case RPC_FC_FP:
250 case RPC_FC_OP:
251 case RPC_FC_CARRAY:
252 case RPC_FC_CVARRAY:
253 case RPC_FC_BOGUS_ARRAY:
254 write_type_left(h, t->ref);
255 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
256 break;
257 default:
258 fprintf(h, "%s", t->name);
263 void write_type_right(FILE *h, type_t *t, int is_field)
265 if (t->declarray) {
266 if (is_conformant_array(t)) {
267 fprintf(h, "[%s]", is_field ? "1" : "");
268 t = t->ref;
270 for ( ; t->declarray; t = t->ref)
271 fprintf(h, "[%lu]", t->dim);
275 void write_type(FILE *h, type_t *t, int is_field, const char *fmt, ...)
277 write_type_left(h, t);
278 if (fmt) {
279 va_list args;
280 va_start(args, fmt);
281 if (needs_space_after(t))
282 fprintf(h, " ");
283 vfprintf(h, fmt, args);
284 va_end(args);
286 write_type_right(h, t, is_field);
289 user_type_list_t user_type_list = LIST_INIT(user_type_list);
291 static int user_type_registered(const char *name)
293 user_type_t *ut;
294 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
295 if (!strcmp(name, ut->name))
296 return 1;
297 return 0;
300 void check_for_user_types(const var_list_t *list)
302 const var_t *v;
304 if (!list) return;
305 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
307 type_t *type;
308 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
309 const char *name = type->name;
310 if (type->user_types_registered) continue;
311 type->user_types_registered = 1;
312 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
313 if (!user_type_registered(name))
315 user_type_t *ut = xmalloc(sizeof *ut);
316 ut->name = xstrdup(name);
317 list_add_tail(&user_type_list, &ut->entry);
319 /* don't carry on parsing fields within this type as we are already
320 * using a wire marshaled type */
321 break;
323 else
325 check_for_user_types(type->fields);
331 void write_user_types(void)
333 user_type_t *ut;
334 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
336 const char *name = ut->name;
337 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
338 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
339 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
340 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
344 void write_typedef(type_t *type)
346 fprintf(header, "typedef ");
347 write_type(header, type->orig, FALSE, "%s", type->name);
348 fprintf(header, ";\n");
351 void write_expr(FILE *h, const expr_t *e, int brackets)
353 switch (e->type) {
354 case EXPR_VOID:
355 break;
356 case EXPR_NUM:
357 fprintf(h, "%lu", e->u.lval);
358 break;
359 case EXPR_HEXNUM:
360 fprintf(h, "0x%lx", e->u.lval);
361 break;
362 case EXPR_DOUBLE:
363 fprintf(h, "%#.15g", e->u.dval);
364 break;
365 case EXPR_TRUEFALSE:
366 if (e->u.lval == 0)
367 fprintf(h, "FALSE");
368 else
369 fprintf(h, "TRUE");
370 break;
371 case EXPR_IDENTIFIER:
372 fprintf(h, "%s", e->u.sval);
373 break;
374 case EXPR_NEG:
375 fprintf(h, "-");
376 write_expr(h, e->ref, 1);
377 break;
378 case EXPR_NOT:
379 fprintf(h, "~");
380 write_expr(h, e->ref, 1);
381 break;
382 case EXPR_PPTR:
383 fprintf(h, "*");
384 write_expr(h, e->ref, 1);
385 break;
386 case EXPR_CAST:
387 fprintf(h, "(");
388 write_type(h, e->u.tref, FALSE, NULL);
389 fprintf(h, ")");
390 write_expr(h, e->ref, 1);
391 break;
392 case EXPR_SIZEOF:
393 fprintf(h, "sizeof(");
394 write_type(h, e->u.tref, FALSE, NULL);
395 fprintf(h, ")");
396 break;
397 case EXPR_SHL:
398 case EXPR_SHR:
399 case EXPR_MUL:
400 case EXPR_DIV:
401 case EXPR_ADD:
402 case EXPR_SUB:
403 case EXPR_AND:
404 case EXPR_OR:
405 if (brackets) fprintf(h, "(");
406 write_expr(h, e->ref, 1);
407 switch (e->type) {
408 case EXPR_SHL: fprintf(h, " << "); break;
409 case EXPR_SHR: fprintf(h, " >> "); break;
410 case EXPR_MUL: fprintf(h, " * "); break;
411 case EXPR_DIV: fprintf(h, " / "); break;
412 case EXPR_ADD: fprintf(h, " + "); break;
413 case EXPR_SUB: fprintf(h, " - "); break;
414 case EXPR_AND: fprintf(h, " & "); break;
415 case EXPR_OR: fprintf(h, " | "); break;
416 default: break;
418 write_expr(h, e->u.ext, 1);
419 if (brackets) fprintf(h, ")");
420 break;
421 case EXPR_COND:
422 if (brackets) fprintf(h, "(");
423 write_expr(h, e->ref, 1);
424 fprintf(h, " ? ");
425 write_expr(h, e->u.ext, 1);
426 fprintf(h, " : ");
427 write_expr(h, e->ext2, 1);
428 if (brackets) fprintf(h, ")");
429 break;
433 void write_constdef(const var_t *v)
435 fprintf(header, "#define %s (", v->name);
436 write_expr(header, v->eval, 0);
437 fprintf(header, ")\n\n");
440 void write_externdef(const var_t *v)
442 fprintf(header, "extern const ");
443 write_type(header, v->type, FALSE, "%s", v->name);
444 fprintf(header, ";\n\n");
447 void write_library(const char *name, const attr_list_t *attr)
449 const UUID *uuid = get_attrp(attr, ATTR_UUID);
450 fprintf(header, "\n");
451 write_guid(header, "LIBID", name, uuid);
452 fprintf(header, "\n");
456 const var_t* get_explicit_handle_var(const func_t* func)
458 const var_t* var;
460 if (!func->args)
461 return NULL;
463 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
464 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
465 return var;
467 return NULL;
470 int has_out_arg_or_return(const func_t *func)
472 const var_t *var;
474 if (!is_void(func->def->type))
475 return 1;
477 if (!func->args)
478 return 0;
480 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
481 if (is_attr(var->attrs, ATTR_OUT))
482 return 1;
484 return 0;
488 /********** INTERFACES **********/
490 int is_object(const attr_list_t *list)
492 const attr_t *attr;
493 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
494 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
495 return 0;
498 int is_local(const attr_list_t *a)
500 return is_attr(a, ATTR_LOCAL);
503 const var_t *is_callas(const attr_list_t *a)
505 return get_attrp(a, ATTR_CALLAS);
508 static void write_method_macro(const type_t *iface, const char *name)
510 const func_t *cur;
512 if (iface->ref) write_method_macro(iface->ref, name);
514 if (!iface->funcs) return;
516 fprintf(header, "/*** %s methods ***/\n", iface->name);
517 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
519 var_t *def = cur->def;
520 if (!is_callas(def->attrs)) {
521 const var_t *arg;
522 int argc = 0;
523 int c;
525 if (cur->args) LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry ) argc++;
527 fprintf(header, "#define %s_", name);
528 write_name(header,def);
529 fprintf(header, "(p");
530 for (c=0; c<argc; c++)
531 fprintf(header, ",%c", c+'a');
532 fprintf(header, ") ");
534 fprintf(header, "(p)->lpVtbl->");
535 write_name(header, def);
536 fprintf(header, "(p");
537 for (c=0; c<argc; c++)
538 fprintf(header, ",%c", c+'a');
539 fprintf(header, ")\n");
544 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
546 const var_t *arg;
547 int count = 0;
549 if (do_indent)
551 indentation++;
552 indent(h, 0);
554 if (method == 1) {
555 fprintf(h, "%s* This", name);
556 count++;
558 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
559 if (count) {
560 if (do_indent)
562 fprintf(h, ",\n");
563 indent(h, 0);
565 else fprintf(h, ",");
567 if (arg->args)
569 write_type_left(h, arg->type);
570 fprintf(h, " (STDMETHODCALLTYPE *");
571 write_name(h,arg);
572 fprintf(h, ")(");
573 write_args(h, arg->args, NULL, 0, FALSE);
574 fprintf(h, ")");
576 else
577 write_type(h, arg->type, FALSE, "%s", arg->name);
578 count++;
580 if (do_indent) indentation--;
583 static void write_cpp_method_def(const type_t *iface)
585 const func_t *cur;
587 if (!iface->funcs) return;
589 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
591 var_t *def = cur->def;
592 if (!is_callas(def->attrs)) {
593 indent(header, 0);
594 fprintf(header, "virtual ");
595 write_type_left(header, def->type);
596 fprintf(header, " STDMETHODCALLTYPE ");
597 write_name(header, def);
598 fprintf(header, "(\n");
599 write_args(header, cur->args, iface->name, 2, TRUE);
600 fprintf(header, ") = 0;\n");
601 fprintf(header, "\n");
606 static void do_write_c_method_def(const type_t *iface, const char *name)
608 const func_t *cur;
610 if (iface->ref) do_write_c_method_def(iface->ref, name);
612 if (!iface->funcs) return;
613 indent(header, 0);
614 fprintf(header, "/*** %s methods ***/\n", iface->name);
615 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
617 const var_t *def = cur->def;
618 if (!is_callas(def->attrs)) {
619 indent(header, 0);
620 write_type_left(header, def->type);
621 fprintf(header, " (STDMETHODCALLTYPE *");
622 write_name(header, def);
623 fprintf(header, ")(\n");
624 write_args(header, cur->args, name, 1, TRUE);
625 fprintf(header, ");\n");
626 fprintf(header, "\n");
631 static void write_c_method_def(const type_t *iface)
633 do_write_c_method_def(iface, iface->name);
636 static void write_c_disp_method_def(const type_t *iface)
638 do_write_c_method_def(iface->ref, iface->name);
641 static void write_method_proto(const type_t *iface)
643 const func_t *cur;
645 if (!iface->funcs) return;
646 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
648 const var_t *def = cur->def;
649 const var_t *cas = is_callas(def->attrs);
651 if (!is_local(def->attrs)) {
652 /* proxy prototype */
653 write_type_left(header, def->type);
654 fprintf(header, " CALLBACK %s_", iface->name);
655 write_name(header, def);
656 fprintf(header, "_Proxy(\n");
657 write_args(header, cur->args, iface->name, 1, TRUE);
658 fprintf(header, ");\n");
659 /* stub prototype */
660 fprintf(header, "void __RPC_STUB %s_", iface->name);
661 write_name(header,def);
662 fprintf(header, "_Stub(\n");
663 fprintf(header, " IRpcStubBuffer* This,\n");
664 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
665 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
666 fprintf(header, " DWORD* pdwStubPhase);\n");
668 if (cas) {
669 const func_t *m;
670 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
671 if (!strcmp(m->def->name, cas->name)) break;
672 if (&m->entry != iface->funcs) {
673 const var_t *mdef = m->def;
674 /* proxy prototype - use local prototype */
675 write_type_left(header, mdef->type);
676 fprintf(header, " CALLBACK %s_", iface->name);
677 write_name(header, mdef);
678 fprintf(header, "_Proxy(\n");
679 write_args(header, m->args, iface->name, 1, TRUE);
680 fprintf(header, ");\n");
681 /* stub prototype - use remotable prototype */
682 write_type_left(header, def->type);
683 fprintf(header, " __RPC_STUB %s_", iface->name);
684 write_name(header, mdef);
685 fprintf(header, "_Stub(\n");
686 write_args(header, cur->args, iface->name, 1, TRUE);
687 fprintf(header, ");\n");
689 else {
690 parser_warning("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
696 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
698 var_t *def = fun->def;
700 /* FIXME: do we need to handle call_as? */
701 write_type_left(header, def->type);
702 fprintf(header, " ");
703 write_prefix_name(header, prefix, def);
704 fprintf(header, "(\n");
705 if (fun->args)
706 write_args(header, fun->args, iface->name, 0, TRUE);
707 else
708 fprintf(header, " void");
709 fprintf(header, ");\n");
712 static void write_function_protos(const type_t *iface)
714 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
715 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
716 const var_t* explicit_handle_var;
717 const func_t *cur;
718 int prefixes_differ = strcmp(prefix_client, prefix_server);
720 if (!iface->funcs) return;
721 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
723 var_t *def = cur->def;
725 /* check for a defined binding handle */
726 explicit_handle_var = get_explicit_handle_var(cur);
727 if (explicit_handle) {
728 if (!explicit_handle_var) {
729 error("%s() does not define an explicit binding handle!\n", def->name);
730 return;
732 } else if (implicit_handle) {
733 if (explicit_handle_var) {
734 error("%s() must not define a binding handle!\n", def->name);
735 return;
739 if (prefixes_differ) {
740 fprintf(header, "/* client prototype */\n");
741 write_function_proto(iface, cur, prefix_client);
742 fprintf(header, "/* server prototype */\n");
744 write_function_proto(iface, cur, prefix_server);
748 void write_forward(type_t *iface)
750 /* C/C++ forwards should only be written for object interfaces, so if we
751 * have a full definition we only write one if we find [object] among the
752 * attributes - however, if we don't have a full definition at this point
753 * (i.e. this is an IDL forward), then we also assume that it is an object
754 * interface, since non-object interfaces shouldn't need forwards */
755 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
756 && !iface->written) {
757 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
758 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
759 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
760 fprintf(header, "#endif\n\n" );
761 iface->written = TRUE;
765 static void write_iface_guid(const type_t *iface)
767 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
768 write_guid(header, "IID", iface->name, uuid);
771 static void write_dispiface_guid(const type_t *iface)
773 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
774 write_guid(header, "DIID", iface->name, uuid);
777 static void write_coclass_guid(type_t *cocl)
779 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
780 write_guid(header, "CLSID", cocl->name, uuid);
783 static void write_com_interface(type_t *iface)
785 if (!iface->funcs && !iface->ref) {
786 parser_warning("%s has no methods", iface->name);
787 return;
790 fprintf(header, "/*****************************************************************************\n");
791 fprintf(header, " * %s interface\n", iface->name);
792 fprintf(header, " */\n");
793 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
794 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
795 write_iface_guid(iface);
796 write_forward(iface);
797 /* C++ interface */
798 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
799 if (iface->ref)
801 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
802 fprintf(header, "{\n");
803 indentation++;
804 write_cpp_method_def(iface);
805 indentation--;
806 fprintf(header, "};\n");
808 else
810 fprintf(header, "interface %s\n", iface->name);
811 fprintf(header, "{\n");
812 fprintf(header, " BEGIN_INTERFACE\n");
813 fprintf(header, "\n");
814 indentation++;
815 write_cpp_method_def(iface);
816 indentation--;
817 fprintf(header, " END_INTERFACE\n");
818 fprintf(header, "};\n");
820 fprintf(header, "#else\n");
821 /* C interface */
822 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
823 indentation++;
824 fprintf(header, " BEGIN_INTERFACE\n");
825 fprintf(header, "\n");
826 write_c_method_def(iface);
827 indentation--;
828 fprintf(header, " END_INTERFACE\n");
829 fprintf(header, "} %sVtbl;\n", iface->name);
830 fprintf(header, "interface %s {\n", iface->name);
831 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
832 fprintf(header, "};\n");
833 fprintf(header, "\n");
834 fprintf(header, "#ifdef COBJMACROS\n");
835 write_method_macro(iface, iface->name);
836 fprintf(header, "#endif\n");
837 fprintf(header, "\n");
838 fprintf(header, "#endif\n");
839 fprintf(header, "\n");
840 write_method_proto(iface);
841 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
844 static void write_rpc_interface(const type_t *iface)
846 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
847 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
848 static int allocate_written = 0;
850 if (!allocate_written)
852 allocate_written = 1;
853 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
854 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
857 fprintf(header, "/*****************************************************************************\n");
858 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
859 fprintf(header, " */\n");
860 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
861 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
862 if (iface->funcs)
864 write_iface_guid(iface);
865 if (var) fprintf(header, "extern handle_t %s;\n", var);
866 if (old_names)
868 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
869 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
871 else
873 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
874 prefix_client, iface->name, LOWORD(ver), HIWORD(ver));
875 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
876 prefix_server, iface->name, LOWORD(ver), HIWORD(ver));
878 write_function_protos(iface);
880 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
882 /* FIXME: server/client code */
885 void write_interface(type_t *iface)
887 if (is_object(iface->attrs))
888 write_com_interface(iface);
889 else
890 write_rpc_interface(iface);
893 void write_dispinterface(type_t *iface)
895 fprintf(header, "/*****************************************************************************\n");
896 fprintf(header, " * %s dispinterface\n", iface->name);
897 fprintf(header, " */\n");
898 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
899 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
900 write_dispiface_guid(iface);
901 write_forward(iface);
902 /* C++ interface */
903 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
904 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
905 fprintf(header, "{\n");
906 fprintf(header, "};\n");
907 fprintf(header, "#else\n");
908 /* C interface */
909 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
910 indentation++;
911 fprintf(header, " BEGIN_INTERFACE\n");
912 fprintf(header, "\n");
913 write_c_disp_method_def(iface);
914 indentation--;
915 fprintf(header, " END_INTERFACE\n");
916 fprintf(header, "} %sVtbl;\n", iface->name);
917 fprintf(header, "interface %s {\n", iface->name);
918 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
919 fprintf(header, "};\n");
920 fprintf(header, "\n");
921 fprintf(header, "#ifdef COBJMACROS\n");
922 write_method_macro(iface->ref, iface->name);
923 fprintf(header, "#endif\n");
924 fprintf(header, "\n");
925 fprintf(header, "#endif\n");
926 fprintf(header, "\n");
927 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
930 void write_coclass(type_t *cocl)
932 fprintf(header, "/*****************************************************************************\n");
933 fprintf(header, " * %s coclass\n", cocl->name);
934 fprintf(header, " */\n\n");
935 write_coclass_guid(cocl);
936 fprintf(header, "\n");
939 void write_coclass_forward(type_t *cocl)
941 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
942 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
943 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
944 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );