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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 static int indentation
= 0;
40 static void indent(int delta
)
43 if (delta
< 0) indentation
+= delta
;
44 for (c
=0; c
<indentation
; c
++) fprintf(header
, " ");
45 if (delta
> 0) indentation
+= delta
;
48 int is_attr(attr_t
*a
, enum attr_type t
)
51 if (a
->type
== t
) return 1;
57 void *get_attrp(attr_t
*a
, enum attr_type t
)
60 if (a
->type
== t
) return a
->u
.pval
;
66 unsigned long get_attrv(attr_t
*a
, enum attr_type t
)
69 if (a
->type
== t
) return a
->u
.ival
;
75 int is_void(type_t
*t
, var_t
*v
)
77 if (v
&& v
->ptr_level
) return 0;
78 if (!t
->type
&& !t
->ref
) return 1;
82 static void write_pident(FILE *h
, var_t
*v
)
85 for (c
=0; c
<v
->ptr_level
; c
++) {
88 if (v
->name
) fprintf(h
, "%s", v
->name
);
91 void write_name(FILE *h
, var_t
*v
)
93 fprintf(h
, "%s", v
->name
);
96 char* get_name(var_t
*v
)
101 static void write_array(FILE *h
, expr_t
*v
, int field
)
104 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
108 fprintf(h
, "%ld", v
->cval
); /* statically sized array */
110 if (field
) fprintf(h
, "1"); /* dynamically sized array */
118 static void write_field(FILE *h
, var_t
*v
)
123 write_type(h
, v
->type
, NULL
, v
->tname
);
129 /* not all C/C++ compilers support anonymous structs and unions */
130 switch (v
->type
->type
) {
132 case RPC_FC_CVSTRUCT
:
133 case RPC_FC_CPSTRUCT
:
136 case RPC_FC_BOGUS_STRUCT
:
137 case RPC_FC_ENCAPSULATED_UNION
:
138 fprintf(h
, " DUMMYSTRUCTNAME");
140 case RPC_FC_NON_ENCAPSULATED_UNION
:
141 fprintf(h
, " DUMMYUNIONNAME");
148 write_array(h
, v
->array
, 1);
153 static void write_fields(FILE *h
, var_t
*v
)
157 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
160 if (v
== first
) break;
165 static void write_enums(FILE *h
, var_t
*v
)
168 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
175 write_expr(h
, v
->eval
);
185 void write_type(FILE *h
, type_t
*t
, var_t
*v
, char *n
)
189 if (n
) fprintf(h
, "%s", n
);
191 if (t
->is_const
) fprintf(h
, "const ");
193 if (t
->sign
> 0) fprintf(h
, "signed ");
194 else if (t
->sign
< 0) fprintf(h
, "unsigned ");
197 if (t
->ref
) fprintf(h
, t
->ref
->name
);
198 else fprintf(h
, "byte");
201 if (t
->ref
) fprintf(h
, t
->ref
->name
);
202 else fprintf(h
, "char");
205 fprintf(h
, "wchar_t");
209 if (t
->ref
) fprintf(h
, t
->ref
->name
);
210 else fprintf(h
, "short");
214 if (t
->ref
) fprintf(h
, t
->ref
->name
);
215 else fprintf(h
, "long");
218 if (t
->ref
) fprintf(h
, t
->ref
->name
);
219 else fprintf(h
, "hyper");
225 fprintf(h
, "double");
229 if (t
->defined
&& !t
->written
) {
230 if (t
->name
) fprintf(h
, "enum %s {\n", t
->name
);
231 else fprintf(h
, "enum {\n");
234 write_enums(h
, t
->fields
);
238 else fprintf(h
, "enum %s", t
->name
);
240 case RPC_FC_ERROR_STATUS_T
:
241 if (t
->ref
) fprintf(h
, t
->ref
->name
);
242 else fprintf(h
, "error_status_t");
244 case RPC_FC_BIND_PRIMITIVE
:
245 if (t
->ref
) fprintf(h
, t
->ref
->name
);
246 else fprintf(h
, "handle_t");
249 case RPC_FC_CVSTRUCT
:
250 case RPC_FC_CPSTRUCT
:
253 case RPC_FC_BOGUS_STRUCT
:
254 case RPC_FC_ENCAPSULATED_UNION
:
255 if (t
->defined
&& !t
->written
) {
256 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
257 else fprintf(h
, "struct {\n");
260 write_fields(h
, t
->fields
);
264 else fprintf(h
, "struct %s", t
->name
);
266 case RPC_FC_NON_ENCAPSULATED_UNION
:
267 if (t
->defined
&& !t
->written
) {
268 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
269 else fprintf(h
, "union {\n");
272 write_fields(h
, t
->fields
);
276 else fprintf(h
, "union %s", t
->name
);
279 fprintf(h
, "(unknown-type:%d)", t
->type
);
284 write_type(h
, t
->ref
, NULL
, t
->name
);
286 else fprintf(h
, "void");
290 for (c
=0; c
<v
->ptr_level
; c
++) {
296 void write_typedef(type_t
*type
, var_t
*names
)
298 char *tname
= names
->tname
;
300 while (NEXT_LINK(names
)) names
= NEXT_LINK(names
);
302 fprintf(header
, "typedef ");
303 write_type(header
, type
, NULL
, tname
);
304 fprintf(header
, " ");
306 write_pident(header
, names
);
307 if (PREV_LINK(names
))
308 fprintf(header
, ", ");
309 names
= PREV_LINK(names
);
311 fprintf(header
, ";\n");
313 if (get_attrp(type
->attrs
, ATTR_WIREMARSHAL
)) {
316 char *name
= get_name(names
);
317 fprintf(header
, "unsigned long __RPC_USER %s_UserSize (unsigned long *, unsigned long, %s *);\n", name
, name
);
318 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (unsigned long *, unsigned char *, %s *);\n", name
, name
);
319 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name
, name
);
320 fprintf(header
, "void __RPC_USER %s_UserFree (unsigned long *, %s *);\n", name
, name
);
321 if (PREV_LINK(names
))
322 fprintf(header
, ", ");
323 names
= PREV_LINK(names
);
327 fprintf(header
, "\n");
330 static void do_write_expr(FILE *h
, expr_t
*e
, int p
)
336 fprintf(h
, "%ld", e
->u
.lval
);
339 fprintf(h
, "0x%lx", e
->u
.lval
);
341 case EXPR_IDENTIFIER
:
342 fprintf(h
, "%s", e
->u
.sval
);
346 do_write_expr(h
, e
->ref
, 1);
350 do_write_expr(h
, e
->ref
, 1);
354 do_write_expr(h
, e
->ref
, 1);
358 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
360 do_write_expr(h
, e
->ref
, 1);
363 fprintf(h
, "sizeof(");
364 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
375 if (p
) fprintf(h
, "(");
376 do_write_expr(h
, e
->ref
, 1);
378 case EXPR_SHL
: fprintf(h
, " << "); break;
379 case EXPR_SHR
: fprintf(h
, " >> "); break;
380 case EXPR_MUL
: fprintf(h
, " * "); break;
381 case EXPR_DIV
: fprintf(h
, " / "); break;
382 case EXPR_ADD
: fprintf(h
, " + "); break;
383 case EXPR_SUB
: fprintf(h
, " - "); break;
384 case EXPR_AND
: fprintf(h
, " & "); break;
385 case EXPR_OR
: fprintf(h
, " | "); break;
388 do_write_expr(h
, e
->u
.ext
, 1);
389 if (p
) fprintf(h
, ")");
392 if (p
) fprintf(h
, "(");
393 do_write_expr(h
, e
->ref
, 1);
395 do_write_expr(h
, e
->u
.ext
, 1);
397 do_write_expr(h
, e
->ext2
, 1);
398 if (p
) fprintf(h
, ")");
403 void write_expr(FILE *h
, expr_t
*e
)
405 do_write_expr(h
, e
, 0);
408 void write_constdef(var_t
*v
)
410 fprintf(header
, "#define %s (", get_name(v
));
411 write_expr(header
, v
->eval
);
412 fprintf(header
, ")\n\n");
415 void write_externdef(var_t
*v
)
417 fprintf(header
, "extern const ");
418 write_type(header
, v
->type
, NULL
, v
->tname
);
420 fprintf(header
, " ");
421 write_pident(header
, v
);
423 fprintf(header
, ";\n\n");
426 /********** INTERFACES **********/
428 int is_object(attr_t
*a
)
431 if (a
->type
== ATTR_OBJECT
|| a
->type
== ATTR_ODL
) return 1;
437 int is_local(attr_t
*a
)
439 return is_attr(a
, ATTR_LOCAL
);
442 var_t
*is_callas(attr_t
*a
)
444 return get_attrp(a
, ATTR_CALLAS
);
447 static void write_icom_method_def(type_t
*iface
)
449 func_t
*cur
= iface
->funcs
;
450 if (iface
->ref
) write_icom_method_def( iface
->ref
);
452 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
453 if (cur
) fprintf( header
, " \\\n /*** %s methods ***/", iface
->name
);
455 var_t
*def
= cur
->def
;
456 if (!is_callas(def
->attrs
)) {
457 var_t
*arg
= cur
->args
;
460 while (NEXT_LINK(arg
)) {
461 arg
= NEXT_LINK(arg
);
464 fprintf(header
, " \\\n STDMETHOD_(");
465 write_type(header
, def
->type
, def
, def
->tname
);
466 fprintf(header
, ",");
467 write_name(header
, def
);
468 fprintf(header
, ")(%s", arg
? "THIS_ " : "THIS" );
470 write_type(header
, arg
->type
, arg
, arg
->tname
);
473 fprintf(header
, " (STDMETHODCALLTYPE *");
474 write_name(header
,arg
);
475 fprintf( header
,")(");
476 write_args(header
, arg
->args
, NULL
, 0, FALSE
);
481 fprintf(header
, " ");
482 write_name(header
,arg
);
484 write_array(header
, arg
->array
, 0);
485 arg
= PREV_LINK(arg
);
486 if (arg
) fprintf(header
, ", ");
488 fprintf(header
, ") PURE;");
490 cur
= PREV_LINK(cur
);
494 static int write_method_macro(type_t
*iface
, char *name
)
497 func_t
*cur
= iface
->funcs
;
499 if (iface
->ref
) idx
= write_method_macro(iface
->ref
, name
);
502 if (!cur
) return idx
;
503 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
505 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
507 var_t
*def
= cur
->def
;
508 if (!is_callas(def
->attrs
)) {
509 var_t
*arg
= cur
->args
;
513 arg
= NEXT_LINK(arg
);
517 fprintf(header
, "#define %s_", name
);
518 write_name(header
,def
);
519 fprintf(header
, "(p");
520 for (c
=0; c
<argc
; c
++)
521 fprintf(header
, ",%c", c
+'a');
522 fprintf(header
, ") ");
524 fprintf(header
, "(p)->lpVtbl->");
525 write_name(header
, def
);
526 fprintf(header
, "(p");
527 for (c
=0; c
<argc
; c
++)
528 fprintf(header
, ",%c", c
+'a');
529 fprintf(header
, ")\n");
530 if (cur
->idx
== -1) cur
->idx
= idx
;
531 else if (cur
->idx
!= idx
) yyerror("BUG: method index mismatch in write_method_macro");
534 cur
= PREV_LINK(cur
);
539 void write_args(FILE *h
, var_t
*arg
, char *name
, int method
, int do_indent
)
543 while (NEXT_LINK(arg
))
544 arg
= NEXT_LINK(arg
);
551 } else fprintf(h
, " ");
554 fprintf(h
, "%s* This", name
);
562 if (h
== header
) indent(0);
563 else fprintf(h
, " ");
565 else fprintf(h
, ",");
567 write_type(h
, arg
->type
, arg
, arg
->tname
);
570 fprintf(h
, " (STDMETHODCALLTYPE *");
573 write_args(h
, arg
->args
, NULL
, 0, FALSE
);
581 write_array(h
, arg
->array
, 0);
582 arg
= PREV_LINK(arg
);
585 if (do_indent
&& h
== header
) indentation
--;
588 static void write_cpp_method_def(type_t
*iface
)
590 func_t
*cur
= iface
->funcs
;
593 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
595 var_t
*def
= cur
->def
;
596 if (!is_callas(def
->attrs
)) {
598 fprintf(header
, "virtual ");
599 write_type(header
, def
->type
, def
, def
->tname
);
600 fprintf(header
, " STDMETHODCALLTYPE ");
601 write_name(header
, def
);
602 fprintf(header
, "(\n");
603 write_args(header
, cur
->args
, iface
->name
, 2, TRUE
);
604 fprintf(header
, ") = 0;\n");
605 fprintf(header
, "\n");
607 cur
= PREV_LINK(cur
);
611 static void do_write_c_method_def(type_t
*iface
, char *name
)
613 func_t
*cur
= iface
->funcs
;
615 if (iface
->ref
) do_write_c_method_def(iface
->ref
, name
);
618 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
620 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
622 var_t
*def
= cur
->def
;
623 if (!is_callas(def
->attrs
)) {
625 write_type(header
, def
->type
, def
, def
->tname
);
626 fprintf(header
, " (STDMETHODCALLTYPE *");
627 write_name(header
, def
);
628 fprintf(header
, ")(\n");
629 write_args(header
, cur
->args
, name
, 1, TRUE
);
630 fprintf(header
, ");\n");
631 fprintf(header
, "\n");
633 cur
= PREV_LINK(cur
);
637 static void write_c_method_def(type_t
*iface
)
639 do_write_c_method_def(iface
, iface
->name
);
642 static void write_method_proto(type_t
*iface
)
644 func_t
*cur
= iface
->funcs
;
647 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
649 var_t
*def
= cur
->def
;
650 var_t
*cas
= is_callas(def
->attrs
);
651 if (!is_local(def
->attrs
)) {
652 /* proxy prototype */
653 write_type(header
, def
->type
, def
, def
->tname
);
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");
660 fprintf(header
, "void __RPC_STUB %s_", iface
->name
);
661 write_name(header
,def
);
662 fprintf(header
, "_Stub(\n");
663 fprintf(header
, " struct IRpcStubBuffer* This,\n");
664 fprintf(header
, " struct IRpcChannelBuffer* pRpcChannelBuffer,\n");
665 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
666 fprintf(header
, " DWORD* pdwStubPhase);\n");
669 func_t
*m
= iface
->funcs
;
670 while (m
&& strcmp(get_name(m
->def
), cas
->name
))
673 var_t
*mdef
= m
->def
;
674 /* proxy prototype - use local prototype */
675 write_type(header
, mdef
->type
, mdef
, mdef
->tname
);
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(header
, def
->type
, def
, def
->tname
);
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");
690 yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def
), cas
->name
);
694 cur
= PREV_LINK(cur
);
698 static void write_function_proto(type_t
*iface
)
700 func_t
*cur
= iface
->funcs
;
701 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
703 var_t
*def
= cur
->def
;
704 /* FIXME: do we need to handle call_as? */
705 write_type(header
, def
->type
, def
, def
->tname
);
706 fprintf(header
, " ");
707 write_name(header
, def
);
708 fprintf(header
, "(\n");
709 write_args(header
, cur
->args
, iface
->name
, 0, TRUE
);
710 fprintf(header
, ");\n");
712 cur
= PREV_LINK(cur
);
716 void write_forward(type_t
*iface
)
718 /* C/C++ forwards should only be written for object interfaces, so if we
719 * have a full definition we only write one if we find [object] among the
720 * attributes - however, if we don't have a full definition at this point
721 * (i.e. this is an IDL forward), then we also assume that it is an object
722 * interface, since non-object interfaces shouldn't need forwards */
723 if ((!iface
->defined
|| is_object(iface
->attrs
)) && !iface
->written
) {
724 fprintf(header
,"#ifndef __%s_FWD_DEFINED__\n", iface
->name
);
725 fprintf(header
,"#define __%s_FWD_DEFINED__\n", iface
->name
);
726 fprintf(header
, "typedef struct %s %s;\n", iface
->name
, iface
->name
);
727 fprintf(header
, "#endif\n\n" );
728 iface
->written
= TRUE
;
732 void write_guid(type_t
*iface
)
734 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
736 fprintf(header
, "DEFINE_GUID(IID_%s, 0x%08lx, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
737 iface
->name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
738 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
741 void write_com_interface(type_t
*iface
)
743 if (!iface
->funcs
&& !iface
->ref
) {
744 yywarning("%s has no methods", iface
->name
);
748 fprintf(header
, "/*****************************************************************************\n");
749 fprintf(header
, " * %s interface\n", iface
->name
);
750 fprintf(header
, " */\n");
751 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
752 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
754 write_forward(iface
);
756 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
759 fprintf(header
, "struct %s : public %s\n", iface
->name
, iface
->ref
->name
);
760 fprintf(header
, "{\n");
762 write_cpp_method_def(iface
);
764 fprintf(header
, "};\n");
768 fprintf(header
, "struct %s\n", iface
->name
);
769 fprintf(header
, "{\n");
770 fprintf(header
, " BEGIN_INTERFACE\n");
771 fprintf(header
, "\n");
773 write_cpp_method_def(iface
);
775 fprintf(header
, " END_INTERFACE\n");
776 fprintf(header
, "};\n");
778 fprintf(header
, "#else\n");
780 fprintf(header
, "typedef struct %sVtbl %sVtbl;\n", iface
->name
, iface
->name
);
781 fprintf(header
, "struct %s {\n", iface
->name
);
782 fprintf(header
, " const %sVtbl* lpVtbl;\n", iface
->name
);
783 fprintf(header
, "};\n");
784 fprintf(header
, "struct %sVtbl {\n", iface
->name
);
786 fprintf(header
, " BEGIN_INTERFACE\n");
787 fprintf(header
, "\n");
788 write_c_method_def(iface
);
790 fprintf(header
, " END_INTERFACE\n");
791 fprintf(header
, "};\n");
792 fprintf(header
, "\n");
793 fprintf(header
, "#ifdef COBJMACROS\n");
794 write_method_macro(iface
, iface
->name
);
795 fprintf(header
, "#endif\n");
796 fprintf(header
, "\n");
797 fprintf(header
, "#endif\n");
798 fprintf(header
, "\n");
800 fprintf(header
, "#define %s_METHODS", iface
->name
);
801 write_icom_method_def(iface
);
802 fprintf(header
, "\n\n");
804 write_method_proto(iface
);
805 fprintf(header
,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
808 void write_rpc_interface(type_t
*iface
)
810 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
812 if (!iface
->funcs
) return;
814 fprintf(header
, "/*****************************************************************************\n");
815 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
816 fprintf(header
, " */\n");
818 fprintf(header
, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
819 fprintf(header
, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
820 write_function_proto(iface
);
821 fprintf(header
, "\n");
823 /* FIXME: server/client code */
826 void write_interface(type_t
*iface
)
828 if (is_object(iface
->attrs
))
829 write_com_interface(iface
);
831 write_rpc_interface(iface
);