include: Fix out of place comment for DLGC_* codes.
[wine/dibdrv.git] / tools / widl / header.c
blob9d816ef09ae2ae2ef492597615868f360716e90d
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 <stdio.h>
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <signal.h>
33 #include "windef.h"
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 static int indentation = 0;
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_attr(const attr_t *a, enum attr_type t)
51 while (a) {
52 if (a->type == t) return 1;
53 a = NEXT_LINK(a);
55 return 0;
58 void *get_attrp(const attr_t *a, enum attr_type t)
60 while (a) {
61 if (a->type == t) return a->u.pval;
62 a = NEXT_LINK(a);
64 return NULL;
67 unsigned long get_attrv(const attr_t *a, enum attr_type t)
69 while (a) {
70 if (a->type == t) return a->u.ival;
71 a = NEXT_LINK(a);
73 return 0;
76 int is_void(const type_t *t, const var_t *v)
78 if (v && v->ptr_level) return 0;
79 if (!t->type && !t->ref) return 1;
80 return 0;
83 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
85 if (!uuid) return;
86 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
87 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
88 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
89 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
90 uuid->Data4[6], uuid->Data4[7]);
93 static void write_pident(FILE *h, const var_t *v)
95 int c;
96 for (c=0; c<v->ptr_level; c++) {
97 fprintf(h, "*");
99 if (v->name) fprintf(h, "%s", v->name);
102 void write_name(FILE *h, const var_t *v)
104 if (is_attr( v->attrs, ATTR_PROPGET ))
105 fprintf(h, "get_" );
106 else if (is_attr( v->attrs, ATTR_PROPPUT ))
107 fprintf(h, "put_" );
108 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
109 fprintf(h, "putref_" );
110 fprintf(h, "%s", v->name);
113 const char* get_name(const var_t *v)
115 return v->name;
118 void write_array(FILE *h, const expr_t *v, int field)
120 if (!v) return;
121 while (NEXT_LINK(v)) v = NEXT_LINK(v);
122 fprintf(h, "[");
123 while (v) {
124 if (v->is_const)
125 fprintf(h, "%ld", v->cval); /* statically sized array */
126 else
127 if (field) fprintf(h, "1"); /* dynamically sized array */
128 if (PREV_LINK(v))
129 fprintf(h, ", ");
130 v = PREV_LINK(v);
132 fprintf(h, "]");
135 static void write_field(FILE *h, var_t *v)
137 if (!v) return;
138 if (v->type) {
139 indent(h, 0);
140 write_type(h, v->type, NULL, v->tname);
141 if (get_name(v)) {
142 fprintf(h, " ");
143 write_pident(h, v);
145 else {
146 /* not all C/C++ compilers support anonymous structs and unions */
147 switch (v->type->type) {
148 case RPC_FC_STRUCT:
149 case RPC_FC_CVSTRUCT:
150 case RPC_FC_CPSTRUCT:
151 case RPC_FC_CSTRUCT:
152 case RPC_FC_PSTRUCT:
153 case RPC_FC_BOGUS_STRUCT:
154 case RPC_FC_ENCAPSULATED_UNION:
155 fprintf(h, " DUMMYSTRUCTNAME");
156 break;
157 case RPC_FC_NON_ENCAPSULATED_UNION:
158 fprintf(h, " DUMMYUNIONNAME");
159 break;
160 default:
161 /* ? */
162 break;
165 write_array(h, v->array, 1);
166 fprintf(h, ";\n");
170 static void write_fields(FILE *h, var_t *v)
172 var_t *first = v;
173 if (!v) return;
174 while (NEXT_LINK(v)) v = NEXT_LINK(v);
175 while (v) {
176 write_field(h, v);
177 if (v == first) break;
178 v = PREV_LINK(v);
182 static void write_enums(FILE *h, var_t *v)
184 if (!v) return;
185 while (NEXT_LINK(v)) v = NEXT_LINK(v);
186 while (v) {
187 if (get_name(v)) {
188 indent(h, 0);
189 write_name(h, v);
190 if (v->eval) {
191 fprintf(h, " = ");
192 write_expr(h, v->eval, 0);
195 if (PREV_LINK(v))
196 fprintf(h, ",\n");
197 v = PREV_LINK(v);
199 fprintf(h, "\n");
202 int needs_space_after(type_t *t)
204 return t->kind == TKIND_ALIAS || ! is_ptr(t);
207 void write_type(FILE *h, type_t *t, const var_t *v, const char *n)
209 int c;
211 if (t->is_const) fprintf(h, "const ");
213 if (n) fprintf(h, "%s", n);
214 else if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
215 else {
216 if (t->sign > 0) fprintf(h, "signed ");
217 else if (t->sign < 0) fprintf(h, "unsigned ");
218 switch (t->type) {
219 case RPC_FC_ENUM16:
220 case RPC_FC_ENUM32:
221 if (t->defined && !t->written && !t->ignore) {
222 if (t->name) fprintf(h, "enum %s {\n", t->name);
223 else fprintf(h, "enum {\n");
224 t->written = TRUE;
225 indentation++;
226 write_enums(h, t->fields);
227 indent(h, -1);
228 fprintf(h, "}");
230 else fprintf(h, "enum %s", t->name);
231 break;
232 case RPC_FC_STRUCT:
233 case RPC_FC_CVSTRUCT:
234 case RPC_FC_CPSTRUCT:
235 case RPC_FC_CSTRUCT:
236 case RPC_FC_PSTRUCT:
237 case RPC_FC_BOGUS_STRUCT:
238 case RPC_FC_ENCAPSULATED_UNION:
239 if (t->defined && !t->written && !t->ignore) {
240 if (t->name) fprintf(h, "struct %s {\n", t->name);
241 else fprintf(h, "struct {\n");
242 t->written = TRUE;
243 indentation++;
244 write_fields(h, t->fields);
245 indent(h, -1);
246 fprintf(h, "}");
248 else fprintf(h, "struct %s", t->name);
249 break;
250 case RPC_FC_NON_ENCAPSULATED_UNION:
251 if (t->defined && !t->written && !t->ignore) {
252 if (t->name) fprintf(h, "union %s {\n", t->name);
253 else fprintf(h, "union {\n");
254 t->written = TRUE;
255 indentation++;
256 write_fields(h, t->fields);
257 indent(h, -1);
258 fprintf(h, "}");
260 else fprintf(h, "union %s", t->name);
261 break;
262 case RPC_FC_RP:
263 case RPC_FC_UP:
264 case RPC_FC_FP:
265 case RPC_FC_OP:
266 if (t->ref) write_type(h, t->ref, NULL, t->name);
267 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
268 break;
269 default:
270 fprintf(h, "%s", t->name);
273 if (v) {
274 for (c=0; c<v->ptr_level; c++) {
275 fprintf(h, "*");
281 struct user_type
283 struct user_type *next;
284 char name[1];
287 static struct user_type *user_type_list;
289 static int user_type_registered(const char *name)
291 struct user_type *ut;
292 for (ut = user_type_list; ut; ut = ut->next)
293 if (!strcmp(name, ut->name))
294 return 1;
295 return 0;
298 static void check_for_user_types(const var_t *v)
300 while (v) {
301 type_t *type;
302 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
303 const char *name = type->name;
304 if (type->user_types_registered) continue;
305 type->user_types_registered = 1;
306 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
307 if (!user_type_registered(name))
309 struct user_type *ut = xmalloc(sizeof(struct user_type) + strlen(name));
310 strcpy(ut->name, name);
311 ut->next = user_type_list;
312 user_type_list = ut;
314 /* don't carry on parsing fields within this type as we are already
315 * using a wire marshaled type */
316 break;
318 else if (type->fields)
320 const var_t *fields = type->fields;
321 while (NEXT_LINK(fields)) fields = NEXT_LINK(fields);
322 check_for_user_types(fields);
325 v = PREV_LINK(v);
329 void write_user_types(void)
331 struct user_type *ut;
332 for (ut = user_type_list; ut; ut = ut->next)
334 const char *name = ut->name;
335 fprintf(header, "unsigned long __RPC_USER %s_UserSize (unsigned long *, unsigned long, %s *);\n", name, name);
336 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (unsigned long *, unsigned char *, %s *);\n", name, name);
337 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name, name);
338 fprintf(header, "void __RPC_USER %s_UserFree (unsigned long *, %s *);\n", name, name);
342 void write_typedef(type_t *type)
344 fprintf(header, "typedef ");
345 write_type(header, type->orig, NULL, NULL);
346 fprintf(header, "%s%s;\n", needs_space_after(type->orig) ? " " : "", type->name);
349 void write_expr(FILE *h, const expr_t *e, int brackets)
351 switch (e->type) {
352 case EXPR_VOID:
353 break;
354 case EXPR_NUM:
355 fprintf(h, "%ld", e->u.lval);
356 break;
357 case EXPR_HEXNUM:
358 fprintf(h, "0x%lx", e->u.lval);
359 break;
360 case EXPR_TRUEFALSE:
361 if (e->u.lval == 0)
362 fprintf(h, "FALSE");
363 else
364 fprintf(h, "TRUE");
365 break;
366 case EXPR_IDENTIFIER:
367 fprintf(h, "%s", e->u.sval);
368 break;
369 case EXPR_NEG:
370 fprintf(h, "-");
371 write_expr(h, e->ref, 1);
372 break;
373 case EXPR_NOT:
374 fprintf(h, "~");
375 write_expr(h, e->ref, 1);
376 break;
377 case EXPR_PPTR:
378 fprintf(h, "*");
379 write_expr(h, e->ref, 1);
380 break;
381 case EXPR_CAST:
382 fprintf(h, "(");
383 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
384 fprintf(h, ")");
385 write_expr(h, e->ref, 1);
386 break;
387 case EXPR_SIZEOF:
388 fprintf(h, "sizeof(");
389 write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
390 fprintf(h, ")");
391 break;
392 case EXPR_SHL:
393 case EXPR_SHR:
394 case EXPR_MUL:
395 case EXPR_DIV:
396 case EXPR_ADD:
397 case EXPR_SUB:
398 case EXPR_AND:
399 case EXPR_OR:
400 if (brackets) fprintf(h, "(");
401 write_expr(h, e->ref, 1);
402 switch (e->type) {
403 case EXPR_SHL: fprintf(h, " << "); break;
404 case EXPR_SHR: fprintf(h, " >> "); break;
405 case EXPR_MUL: fprintf(h, " * "); break;
406 case EXPR_DIV: fprintf(h, " / "); break;
407 case EXPR_ADD: fprintf(h, " + "); break;
408 case EXPR_SUB: fprintf(h, " - "); break;
409 case EXPR_AND: fprintf(h, " & "); break;
410 case EXPR_OR: fprintf(h, " | "); break;
411 default: break;
413 write_expr(h, e->u.ext, 1);
414 if (brackets) fprintf(h, ")");
415 break;
416 case EXPR_COND:
417 if (brackets) fprintf(h, "(");
418 write_expr(h, e->ref, 1);
419 fprintf(h, " ? ");
420 write_expr(h, e->u.ext, 1);
421 fprintf(h, " : ");
422 write_expr(h, e->ext2, 1);
423 if (brackets) fprintf(h, ")");
424 break;
428 void write_constdef(const var_t *v)
430 fprintf(header, "#define %s (", get_name(v));
431 write_expr(header, v->eval, 0);
432 fprintf(header, ")\n\n");
435 void write_externdef(const var_t *v)
437 fprintf(header, "extern const ");
438 write_type(header, v->type, NULL, v->tname);
439 if (get_name(v)) {
440 fprintf(header, " ");
441 write_pident(header, v);
443 fprintf(header, ";\n\n");
446 void write_library(const char *name, const attr_t *attr) {
447 const UUID *uuid = get_attrp(attr, ATTR_UUID);
448 fprintf(header, "\n");
449 write_guid(header, "LIBID", name, uuid);
450 fprintf(header, "\n");
454 const var_t* get_explicit_handle_var(const func_t* func)
456 const var_t* var;
458 if (!func->args)
459 return NULL;
461 var = func->args;
462 while (NEXT_LINK(var)) var = NEXT_LINK(var);
463 while (var)
465 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
466 return var;
468 var = PREV_LINK(var);
471 return NULL;
474 int has_out_arg_or_return(const func_t *func)
476 var_t *var;
478 if (!is_void(func->def->type, NULL))
479 return 1;
481 if (!func->args)
482 return 0;
484 var = func->args;
485 while (NEXT_LINK(var)) var = NEXT_LINK(var);
486 while (var)
488 if (is_attr(var->attrs, ATTR_OUT))
489 return 1;
491 var = PREV_LINK(var);
493 return 0;
497 /********** INTERFACES **********/
499 int is_object(const attr_t *a)
501 while (a) {
502 if (a->type == ATTR_OBJECT || a->type == ATTR_ODL) return 1;
503 a = NEXT_LINK(a);
505 return 0;
508 int is_local(const attr_t *a)
510 return is_attr(a, ATTR_LOCAL);
513 const var_t *is_callas(const attr_t *a)
515 return get_attrp(a, ATTR_CALLAS);
518 static void write_method_macro(const type_t *iface, const char *name)
520 func_t *cur = iface->funcs;
522 if (iface->ref) write_method_macro(iface->ref, name);
524 if (!cur) return;
525 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
527 fprintf(header, "/*** %s methods ***/\n", iface->name);
528 while (cur) {
529 var_t *def = cur->def;
530 if (!is_callas(def->attrs)) {
531 var_t *arg = cur->args;
532 int argc = 0;
533 int c;
534 while (arg) {
535 arg = NEXT_LINK(arg);
536 argc++;
539 fprintf(header, "#define %s_", name);
540 write_name(header,def);
541 fprintf(header, "(p");
542 for (c=0; c<argc; c++)
543 fprintf(header, ",%c", c+'a');
544 fprintf(header, ") ");
546 fprintf(header, "(p)->lpVtbl->");
547 write_name(header, def);
548 fprintf(header, "(p");
549 for (c=0; c<argc; c++)
550 fprintf(header, ",%c", c+'a');
551 fprintf(header, ")\n");
553 cur = PREV_LINK(cur);
557 void write_args(FILE *h, var_t *arg, const char *name, int method, int do_indent)
559 int count = 0;
560 if (arg) {
561 while (NEXT_LINK(arg))
562 arg = NEXT_LINK(arg);
564 if (do_indent)
566 indentation++;
567 indent(h, 0);
569 if (method == 1) {
570 fprintf(h, "%s* This", name);
571 count++;
573 while (arg) {
574 if (count) {
575 if (do_indent)
577 fprintf(h, ",\n");
578 indent(h, 0);
580 else fprintf(h, ",");
582 write_type(h, arg->type, arg, arg->tname);
583 if (arg->args)
585 fprintf(h, " (STDMETHODCALLTYPE *");
586 write_name(h,arg);
587 fprintf(h, ")(");
588 write_args(h, arg->args, NULL, 0, FALSE);
589 fprintf(h, ")");
591 else
593 if (needs_space_after(arg->type))
594 fprintf(h, " ");
595 write_name(h, arg);
597 write_array(h, arg->array, 0);
598 arg = PREV_LINK(arg);
599 count++;
601 if (do_indent) indentation--;
604 static void write_cpp_method_def(const type_t *iface)
606 func_t *cur = iface->funcs;
608 if (!cur) return;
609 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
610 while (cur) {
611 var_t *def = cur->def;
612 if (!is_callas(def->attrs)) {
613 indent(header, 0);
614 fprintf(header, "virtual ");
615 write_type(header, def->type, def, def->tname);
616 fprintf(header, " STDMETHODCALLTYPE ");
617 write_name(header, def);
618 fprintf(header, "(\n");
619 write_args(header, cur->args, iface->name, 2, TRUE);
620 fprintf(header, ") = 0;\n");
621 fprintf(header, "\n");
623 cur = PREV_LINK(cur);
627 static void do_write_c_method_def(const type_t *iface, const char *name)
629 const func_t *cur = iface->funcs;
631 if (iface->ref) do_write_c_method_def(iface->ref, name);
633 if (!cur) return;
634 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
635 indent(header, 0);
636 fprintf(header, "/*** %s methods ***/\n", iface->name);
637 while (cur) {
638 const var_t *def = cur->def;
639 if (!is_callas(def->attrs)) {
640 indent(header, 0);
641 write_type(header, def->type, def, def->tname);
642 fprintf(header, " (STDMETHODCALLTYPE *");
643 write_name(header, def);
644 fprintf(header, ")(\n");
645 write_args(header, cur->args, name, 1, TRUE);
646 fprintf(header, ");\n");
647 fprintf(header, "\n");
649 cur = PREV_LINK(cur);
653 static void write_c_method_def(const type_t *iface)
655 do_write_c_method_def(iface, iface->name);
658 static void write_c_disp_method_def(const type_t *iface)
660 do_write_c_method_def(iface->ref, iface->name);
663 static void write_method_proto(const type_t *iface)
665 const func_t *cur = iface->funcs;
667 if (!cur) return;
668 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
669 while (cur) {
670 const var_t *def = cur->def;
671 const var_t *cas = is_callas(def->attrs);
672 const var_t *args;
673 if (!is_local(def->attrs)) {
674 /* proxy prototype */
675 write_type(header, def->type, def, def->tname);
676 fprintf(header, " CALLBACK %s_", iface->name);
677 write_name(header, def);
678 fprintf(header, "_Proxy(\n");
679 write_args(header, cur->args, iface->name, 1, TRUE);
680 fprintf(header, ");\n");
681 /* stub prototype */
682 fprintf(header, "void __RPC_STUB %s_", iface->name);
683 write_name(header,def);
684 fprintf(header, "_Stub(\n");
685 fprintf(header, " IRpcStubBuffer* This,\n");
686 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
687 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
688 fprintf(header, " DWORD* pdwStubPhase);\n");
690 args = cur->args;
691 if (args) {
692 while (NEXT_LINK(args))
693 args = NEXT_LINK(args);
695 check_for_user_types(args);
697 if (cas) {
698 const func_t *m = iface->funcs;
699 while (m && strcmp(get_name(m->def), cas->name))
700 m = NEXT_LINK(m);
701 if (m) {
702 const var_t *mdef = m->def;
703 /* proxy prototype - use local prototype */
704 write_type(header, mdef->type, mdef, mdef->tname);
705 fprintf(header, " CALLBACK %s_", iface->name);
706 write_name(header, mdef);
707 fprintf(header, "_Proxy(\n");
708 write_args(header, m->args, iface->name, 1, TRUE);
709 fprintf(header, ");\n");
710 /* stub prototype - use remotable prototype */
711 write_type(header, def->type, def, def->tname);
712 fprintf(header, " __RPC_STUB %s_", iface->name);
713 write_name(header, mdef);
714 fprintf(header, "_Stub(\n");
715 write_args(header, cur->args, iface->name, 1, TRUE);
716 fprintf(header, ");\n");
718 else {
719 parser_warning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
723 cur = PREV_LINK(cur);
727 static void write_function_proto(const type_t *iface)
729 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
730 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
731 const var_t* explicit_handle_var;
733 func_t *cur = iface->funcs;
734 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
735 while (cur) {
736 var_t *def = cur->def;
738 /* check for a defined binding handle */
739 explicit_handle_var = get_explicit_handle_var(cur);
740 if (explicit_handle) {
741 if (!explicit_handle_var) {
742 error("%s() does not define an explicit binding handle!\n", def->name);
743 return;
745 } else if (implicit_handle) {
746 if (explicit_handle_var) {
747 error("%s() must not define a binding handle!\n", def->name);
748 return;
752 /* FIXME: do we need to handle call_as? */
753 write_type(header, def->type, def, def->tname);
754 fprintf(header, " ");
755 write_name(header, def);
756 fprintf(header, "(\n");
757 if (cur->args)
758 write_args(header, cur->args, iface->name, 0, TRUE);
759 else
760 fprintf(header, " void");
761 fprintf(header, ");\n");
763 cur = PREV_LINK(cur);
767 void write_forward(type_t *iface)
769 /* C/C++ forwards should only be written for object interfaces, so if we
770 * have a full definition we only write one if we find [object] among the
771 * attributes - however, if we don't have a full definition at this point
772 * (i.e. this is an IDL forward), then we also assume that it is an object
773 * interface, since non-object interfaces shouldn't need forwards */
774 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
775 && !iface->written) {
776 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
777 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
778 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
779 fprintf(header, "#endif\n\n" );
780 iface->written = TRUE;
784 static void write_iface_guid(const type_t *iface)
786 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
787 write_guid(header, "IID", iface->name, uuid);
790 static void write_dispiface_guid(const type_t *iface)
792 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
793 write_guid(header, "DIID", iface->name, uuid);
796 static void write_coclass_guid(type_t *cocl)
798 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
799 write_guid(header, "CLSID", cocl->name, uuid);
802 static void write_com_interface(type_t *iface)
804 if (!iface->funcs && !iface->ref) {
805 parser_warning("%s has no methods", iface->name);
806 return;
809 fprintf(header, "/*****************************************************************************\n");
810 fprintf(header, " * %s interface\n", iface->name);
811 fprintf(header, " */\n");
812 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
813 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
814 write_iface_guid(iface);
815 write_forward(iface);
816 /* C++ interface */
817 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
818 if (iface->ref)
820 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
821 fprintf(header, "{\n");
822 indentation++;
823 write_cpp_method_def(iface);
824 indentation--;
825 fprintf(header, "};\n");
827 else
829 fprintf(header, "interface %s\n", iface->name);
830 fprintf(header, "{\n");
831 fprintf(header, " BEGIN_INTERFACE\n");
832 fprintf(header, "\n");
833 indentation++;
834 write_cpp_method_def(iface);
835 indentation--;
836 fprintf(header, " END_INTERFACE\n");
837 fprintf(header, "};\n");
839 fprintf(header, "#else\n");
840 /* C interface */
841 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
842 indentation++;
843 fprintf(header, " BEGIN_INTERFACE\n");
844 fprintf(header, "\n");
845 write_c_method_def(iface);
846 indentation--;
847 fprintf(header, " END_INTERFACE\n");
848 fprintf(header, "} %sVtbl;\n", iface->name);
849 fprintf(header, "interface %s {\n", iface->name);
850 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
851 fprintf(header, "};\n");
852 fprintf(header, "\n");
853 fprintf(header, "#ifdef COBJMACROS\n");
854 write_method_macro(iface, iface->name);
855 fprintf(header, "#endif\n");
856 fprintf(header, "\n");
857 fprintf(header, "#endif\n");
858 fprintf(header, "\n");
859 write_method_proto(iface);
860 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
863 static void write_rpc_interface(const type_t *iface)
865 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
866 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
867 static int allocate_written = 0;
869 if (!allocate_written)
871 allocate_written = 1;
872 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
873 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
876 fprintf(header, "/*****************************************************************************\n");
877 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
878 fprintf(header, " */\n");
879 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
880 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
881 if (iface->funcs)
883 write_iface_guid(iface);
884 if (var) fprintf(header, "extern handle_t %s;\n", var);
885 if (old_names)
887 fprintf(header, "extern RPC_IF_HANDLE %s_ClientIfHandle;\n", iface->name);
888 fprintf(header, "extern RPC_IF_HANDLE %s_ServerIfHandle;\n", iface->name);
890 else
892 fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
893 fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
895 write_function_proto(iface);
897 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
899 /* FIXME: server/client code */
902 void write_interface(type_t *iface)
904 if (is_object(iface->attrs))
905 write_com_interface(iface);
906 else
907 write_rpc_interface(iface);
910 void write_dispinterface(type_t *iface)
912 fprintf(header, "/*****************************************************************************\n");
913 fprintf(header, " * %s dispinterface\n", iface->name);
914 fprintf(header, " */\n");
915 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
916 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
917 write_dispiface_guid(iface);
918 write_forward(iface);
919 /* C++ interface */
920 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
921 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
922 fprintf(header, "{\n");
923 fprintf(header, "};\n");
924 fprintf(header, "#else\n");
925 /* C interface */
926 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
927 indentation++;
928 fprintf(header, " BEGIN_INTERFACE\n");
929 fprintf(header, "\n");
930 write_c_disp_method_def(iface);
931 indentation--;
932 fprintf(header, " END_INTERFACE\n");
933 fprintf(header, "} %sVtbl;\n", iface->name);
934 fprintf(header, "interface %s {\n", iface->name);
935 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
936 fprintf(header, "};\n");
937 fprintf(header, "\n");
938 fprintf(header, "#ifdef COBJMACROS\n");
939 write_method_macro(iface->ref, iface->name);
940 fprintf(header, "#endif\n");
941 fprintf(header, "\n");
942 fprintf(header, "#endif\n");
943 fprintf(header, "\n");
944 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
947 void write_coclass(type_t *cocl)
949 fprintf(header, "/*****************************************************************************\n");
950 fprintf(header, " * %s coclass\n", cocl->name);
951 fprintf(header, " */\n\n");
952 write_coclass_guid(cocl);
953 fprintf(header, "\n");
956 void write_coclass_forward(type_t *cocl)
958 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
959 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
960 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
961 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );