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
39 static int indentation
= 0;
41 static void indent(int delta
)
44 if (delta
< 0) indentation
+= delta
;
45 for (c
=0; c
<indentation
; c
++) fprintf(header
, " ");
46 if (delta
> 0) indentation
+= delta
;
49 int is_attr(attr_t
*a
, enum attr_type t
)
52 if (a
->type
== t
) return 1;
58 void *get_attrp(attr_t
*a
, enum attr_type t
)
61 if (a
->type
== t
) return a
->u
.pval
;
67 DWORD
get_attrv(attr_t
*a
, enum attr_type t
)
70 if (a
->type
== t
) return a
->u
.ival
;
76 int is_void(type_t
*t
, var_t
*v
)
78 if (v
&& v
->ptr_level
) return 0;
79 if (!t
->type
&& !t
->ref
) return 1;
83 static void write_pident(FILE *h
, var_t
*v
)
86 for (c
=0; c
<v
->ptr_level
; c
++) {
89 if (v
->name
) fprintf(h
, "%s", v
->name
);
92 void write_name(FILE *h
, var_t
*v
)
94 fprintf(h
, "%s", v
->name
);
97 char* get_name(var_t
*v
)
102 static void write_array(FILE *h
, expr_t
*v
, int field
)
105 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
109 fprintf(h
, "%ld", v
->cval
); /* statically sized array */
111 if (field
) fprintf(h
, "1"); /* dynamically sized array */
119 static void write_field(FILE *h
, var_t
*v
)
124 write_type(h
, v
->type
, NULL
, v
->tname
);
130 /* not all C/C++ compilers support anonymous structs and unions */
131 switch (v
->type
->type
) {
133 case RPC_FC_ENCAPSULATED_UNION
:
134 fprintf(h
, " DUMMYSTRUCTNAME");
136 case RPC_FC_NON_ENCAPSULATED_UNION
:
137 fprintf(h
, " DUMMYUNIONNAME");
144 write_array(h
, v
->array
, 1);
149 static void write_fields(FILE *h
, var_t
*v
)
153 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
156 if (v
== first
) break;
161 static void write_enums(FILE *h
, var_t
*v
)
164 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
171 write_expr(h
, v
->eval
);
181 void write_type(FILE *h
, type_t
*t
, var_t
*v
, char *n
)
185 if (n
) fprintf(h
, "%s", n
);
187 if (t
->is_const
) fprintf(h
, "const ");
189 if (t
->sign
> 0) fprintf(h
, "signed ");
190 else if (t
->sign
< 0) fprintf(h
, "unsigned ");
193 if (t
->ref
) fprintf(h
, t
->ref
->name
);
194 else fprintf(h
, "byte");
197 if (t
->ref
) fprintf(h
, t
->ref
->name
);
198 else fprintf(h
, "char");
201 fprintf(h
, "wchar_t");
205 if (t
->ref
) fprintf(h
, t
->ref
->name
);
206 else fprintf(h
, "short");
210 if (t
->ref
) fprintf(h
, t
->ref
->name
);
211 else fprintf(h
, "long");
214 if (t
->ref
) fprintf(h
, t
->ref
->name
);
215 else fprintf(h
, "hyper");
221 fprintf(h
, "double");
225 if (t
->defined
&& !t
->written
) {
226 if (t
->name
) fprintf(h
, "enum %s {\n", t
->name
);
227 else fprintf(h
, "enum {\n");
230 write_enums(h
, t
->fields
);
234 else fprintf(h
, "enum %s", t
->name
);
236 case RPC_FC_ERROR_STATUS_T
:
237 if (t
->ref
) fprintf(h
, t
->ref
->name
);
238 else fprintf(h
, "error_status_t");
240 case RPC_FC_BIND_PRIMITIVE
:
241 if (t
->ref
) fprintf(h
, t
->ref
->name
);
242 else fprintf(h
, "handle_t");
245 case RPC_FC_ENCAPSULATED_UNION
:
246 if (t
->defined
&& !t
->written
) {
247 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
248 else fprintf(h
, "struct {\n");
251 write_fields(h
, t
->fields
);
255 else fprintf(h
, "struct %s", t
->name
);
257 case RPC_FC_NON_ENCAPSULATED_UNION
:
258 if (t
->defined
&& !t
->written
) {
259 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
260 else fprintf(h
, "union {\n");
263 write_fields(h
, t
->fields
);
267 else fprintf(h
, "union %s", t
->name
);
270 fprintf(h
, "(unknown-type:%d)", t
->type
);
275 write_type(h
, t
->ref
, NULL
, t
->name
);
277 else fprintf(h
, "void");
281 for (c
=0; c
<v
->ptr_level
; c
++) {
287 void write_typedef(type_t
*type
, var_t
*names
)
289 char *tname
= names
->tname
;
291 while (NEXT_LINK(names
)) names
= NEXT_LINK(names
);
293 fprintf(header
, "typedef ");
294 write_type(header
, type
, NULL
, tname
);
295 fprintf(header
, " ");
297 write_pident(header
, names
);
298 if (PREV_LINK(names
))
299 fprintf(header
, ", ");
300 names
= PREV_LINK(names
);
302 fprintf(header
, ";\n");
304 if (get_attrp(type
->attrs
, ATTR_WIREMARSHAL
)) {
307 char *name
= get_name(names
);
308 fprintf(header
, "unsigned long __RPC_USER %s_UserSize (unsigned long *, unsigned long, %s *);\n", name
, name
);
309 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (unsigned long *, unsigned char *, %s *);\n", name
, name
);
310 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name
, name
);
311 fprintf(header
, "void __RPC_USER %s_UserFree (unsigned long *, %s *);\n", name
, name
);
312 if (PREV_LINK(names
))
313 fprintf(header
, ", ");
314 names
= PREV_LINK(names
);
318 fprintf(header
, "\n");
321 static void do_write_expr(FILE *h
, expr_t
*e
, int p
)
327 fprintf(h
, "%ld", e
->u
.lval
);
330 fprintf(h
, "0x%lx", e
->u
.lval
);
332 case EXPR_IDENTIFIER
:
333 fprintf(h
, "%s", e
->u
.sval
);
337 do_write_expr(h
, e
->ref
, 1);
341 do_write_expr(h
, e
->ref
, 1);
345 do_write_expr(h
, e
->ref
, 1);
349 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
351 do_write_expr(h
, e
->ref
, 1);
354 fprintf(h
, "sizeof(");
355 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
366 if (p
) fprintf(h
, "(");
367 do_write_expr(h
, e
->ref
, 1);
369 case EXPR_SHL
: fprintf(h
, " << "); break;
370 case EXPR_SHR
: fprintf(h
, " >> "); break;
371 case EXPR_MUL
: fprintf(h
, " * "); break;
372 case EXPR_DIV
: fprintf(h
, " / "); break;
373 case EXPR_ADD
: fprintf(h
, " + "); break;
374 case EXPR_SUB
: fprintf(h
, " - "); break;
375 case EXPR_AND
: fprintf(h
, " & "); break;
376 case EXPR_OR
: fprintf(h
, " | "); break;
379 do_write_expr(h
, e
->u
.ext
, 1);
380 if (p
) fprintf(h
, ")");
385 void write_expr(FILE *h
, expr_t
*e
)
387 do_write_expr(h
, e
, 0);
390 void write_constdef(var_t
*v
)
392 fprintf(header
, "#define %s (", get_name(v
));
393 write_expr(header
, v
->eval
);
394 fprintf(header
, ")\n\n");
397 void write_externdef(var_t
*v
)
399 fprintf(header
, "extern const ");
400 write_type(header
, v
->type
, NULL
, v
->tname
);
402 fprintf(header
, " ");
403 write_pident(header
, v
);
405 fprintf(header
, ";\n\n");
408 /********** INTERFACES **********/
410 int is_object(attr_t
*a
)
412 return is_attr(a
, ATTR_OBJECT
);
415 int is_local(attr_t
*a
)
417 return is_attr(a
, ATTR_LOCAL
);
420 var_t
*is_callas(attr_t
*a
)
422 return get_attrp(a
, ATTR_CALLAS
);
425 static void write_icom_method_def(type_t
*iface
)
427 func_t
*cur
= iface
->funcs
;
428 if (iface
->ref
) write_icom_method_def( iface
->ref
);
429 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
430 if (cur
) fprintf( header
, " \\\n /*** %s methods ***/", iface
->name
);
432 var_t
*def
= cur
->def
;
433 if (!is_callas(def
->attrs
)) {
434 var_t
*arg
= cur
->args
;
437 while (NEXT_LINK(arg
)) {
438 arg
= NEXT_LINK(arg
);
441 fprintf(header
, " \\\n STDMETHOD_(");
442 write_type(header
, def
->type
, def
, def
->tname
);
443 fprintf(header
, ",");
444 write_name(header
, def
);
445 fprintf(header
, ")(%s", arg
? "THIS_ " : "THIS" );
447 write_type(header
, arg
->type
, arg
, arg
->tname
);
448 fprintf(header
, " ");
449 write_name(header
,arg
);
450 write_array(header
, arg
->array
, 0);
451 arg
= PREV_LINK(arg
);
452 if (arg
) fprintf(header
, ", ");
454 fprintf(header
, ") PURE;");
456 cur
= PREV_LINK(cur
);
460 static int write_method_macro(type_t
*iface
, char *name
)
463 func_t
*cur
= iface
->funcs
;
464 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
466 if (iface
->ref
) idx
= write_method_macro(iface
->ref
, name
);
468 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
470 var_t
*def
= cur
->def
;
471 if (!is_callas(def
->attrs
)) {
472 var_t
*arg
= cur
->args
;
476 arg
= NEXT_LINK(arg
);
480 fprintf(header
, "#define %s_", name
);
481 write_name(header
,def
);
482 fprintf(header
, "(p");
483 for (c
=0; c
<argc
; c
++)
484 fprintf(header
, ",%c", c
+'a');
485 fprintf(header
, ") ");
487 fprintf(header
, "(p)->lpVtbl->");
488 write_name(header
, def
);
489 fprintf(header
, "(p");
490 for (c
=0; c
<argc
; c
++)
491 fprintf(header
, ",%c", c
+'a');
492 fprintf(header
, ")\n");
493 if (cur
->idx
== -1) cur
->idx
= idx
;
494 else if (cur
->idx
!= idx
) yyerror("BUG: method index mismatch in write_method_macro");
497 cur
= PREV_LINK(cur
);
502 void write_args(FILE *h
, var_t
*arg
, char *name
, int method
)
506 while (NEXT_LINK(arg
))
507 arg
= NEXT_LINK(arg
);
512 } else fprintf(h
, " ");
514 fprintf(h
, "%s* This", name
);
520 if (h
== header
) indent(0);
521 else fprintf(h
, " ");
523 write_type(h
, arg
->type
, arg
, arg
->tname
);
526 write_array(h
, arg
->array
, 0);
527 arg
= PREV_LINK(arg
);
530 if (h
== header
) indentation
--;
533 static void write_cpp_method_def(type_t
*iface
)
535 func_t
*cur
= iface
->funcs
;
536 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
538 var_t
*def
= cur
->def
;
539 if (!is_callas(def
->attrs
)) {
541 fprintf(header
, "virtual ");
542 write_type(header
, def
->type
, def
, def
->tname
);
543 fprintf(header
, " STDMETHODCALLTYPE ");
544 write_name(header
, def
);
545 fprintf(header
, "(\n");
546 write_args(header
, cur
->args
, iface
->name
, 2);
547 fprintf(header
, ") = 0;\n");
548 fprintf(header
, "\n");
550 cur
= PREV_LINK(cur
);
554 static void do_write_c_method_def(type_t
*iface
, char *name
)
556 func_t
*cur
= iface
->funcs
;
557 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
559 if (iface
->ref
) do_write_c_method_def(iface
->ref
, name
);
561 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
563 var_t
*def
= cur
->def
;
564 if (!is_callas(def
->attrs
)) {
566 write_type(header
, def
->type
, def
, def
->tname
);
567 fprintf(header
, " (STDMETHODCALLTYPE *");
568 write_name(header
, def
);
569 fprintf(header
, ")(\n");
570 write_args(header
, cur
->args
, name
, 1);
571 fprintf(header
, ");\n");
572 fprintf(header
, "\n");
574 cur
= PREV_LINK(cur
);
578 static void write_c_method_def(type_t
*iface
)
580 do_write_c_method_def(iface
, iface
->name
);
583 static void write_method_proto(type_t
*iface
)
585 func_t
*cur
= iface
->funcs
;
586 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
588 var_t
*def
= cur
->def
;
589 var_t
*cas
= is_callas(def
->attrs
);
590 if (!is_local(def
->attrs
)) {
591 /* proxy prototype */
592 write_type(header
, def
->type
, def
, def
->tname
);
593 fprintf(header
, " CALLBACK %s_", iface
->name
);
594 write_name(header
, def
);
595 fprintf(header
, "_Proxy(\n");
596 write_args(header
, cur
->args
, iface
->name
, 1);
597 fprintf(header
, ");\n");
599 fprintf(header
, "void __RPC_STUB %s_", iface
->name
);
600 write_name(header
,def
);
601 fprintf(header
, "_Stub(\n");
602 fprintf(header
, " struct IRpcStubBuffer* This,\n");
603 fprintf(header
, " struct IRpcChannelBuffer* pRpcChannelBuffer,\n");
604 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
605 fprintf(header
, " DWORD* pdwStubPhase);\n");
608 func_t
*m
= iface
->funcs
;
609 while (m
&& strcmp(get_name(m
->def
), cas
->name
))
612 var_t
*mdef
= m
->def
;
613 /* proxy prototype - use local prototype */
614 write_type(header
, mdef
->type
, mdef
, mdef
->tname
);
615 fprintf(header
, " CALLBACK %s_", iface
->name
);
616 write_name(header
, mdef
);
617 fprintf(header
, "_Proxy(\n");
618 write_args(header
, m
->args
, iface
->name
, 1);
619 fprintf(header
, ");\n");
620 /* stub prototype - use remotable prototype */
621 write_type(header
, def
->type
, def
, def
->tname
);
622 fprintf(header
, " __RPC_STUB %s_", iface
->name
);
623 write_name(header
, mdef
);
624 fprintf(header
, "_Stub(\n");
625 write_args(header
, cur
->args
, iface
->name
, 1);
626 fprintf(header
, ");\n");
629 yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def
), cas
->name
);
633 cur
= PREV_LINK(cur
);
637 static void write_function_proto(type_t
*iface
)
639 func_t
*cur
= iface
->funcs
;
640 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
642 var_t
*def
= cur
->def
;
643 /* FIXME: do we need to handle call_as? */
644 write_type(header
, def
->type
, def
, def
->tname
);
645 fprintf(header
, " ");
646 write_name(header
, def
);
647 fprintf(header
, "(\n");
648 write_args(header
, cur
->args
, iface
->name
, 0);
649 fprintf(header
, ");\n");
651 cur
= PREV_LINK(cur
);
655 void write_forward(type_t
*iface
)
657 /* C/C++ forwards should only be written for object interfaces, so if we
658 * have a full definition we only write one if we find [object] among the
659 * attributes - however, if we don't have a full definition at this point
660 * (i.e. this is an IDL forward), then we also assume that it is an object
661 * interface, since non-object interfaces shouldn't need forwards */
662 if ((!iface
->defined
|| is_object(iface
->attrs
)) && !iface
->written
) {
663 fprintf(header
,"#ifndef __%s_FWD_DEFINED__\n", iface
->name
);
664 fprintf(header
,"#define __%s_FWD_DEFINED__\n", iface
->name
);
665 fprintf(header
, "typedef struct %s %s;\n", iface
->name
, iface
->name
);
666 fprintf(header
, "#endif\n\n" );
667 iface
->written
= TRUE
;
671 void write_guid(type_t
*iface
)
673 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
675 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",
676 iface
->name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
677 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
680 void write_com_interface(type_t
*iface
)
683 yywarning("%s has no methods", iface
->name
);
687 fprintf(header
, "/*****************************************************************************\n");
688 fprintf(header
, " * %s interface\n", iface
->name
);
689 fprintf(header
, " */\n");
690 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
691 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
693 write_forward(iface
);
695 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
697 fprintf(header
, "struct %s : public %s\n", iface
->name
, iface
->ref
->name
);
700 fprintf(header
, "#ifdef ICOM_USE_COM_INTERFACE_ATTRIBUTE\n");
701 fprintf(header
, "struct __attribute__((com_interface)) %s\n", iface
->name
);
702 fprintf(header
, "#else\n");
703 fprintf(header
, "struct %s\n", iface
->name
);
704 fprintf(header
, "#endif\n");
706 fprintf(header
, "{\n");
708 write_cpp_method_def(iface
);
710 fprintf(header
, "};\n");
711 fprintf(header
, "#else\n");
713 fprintf(header
, "typedef struct %sVtbl %sVtbl;\n", iface
->name
, iface
->name
);
714 fprintf(header
, "struct %s {\n", iface
->name
);
715 fprintf(header
, " const %sVtbl* lpVtbl;\n", iface
->name
);
716 fprintf(header
, "};\n");
717 fprintf(header
, "struct %sVtbl {\n", iface
->name
);
719 fprintf(header
, " ICOM_MSVTABLE_COMPAT_FIELDS\n");
720 fprintf(header
, "\n");
721 write_c_method_def(iface
);
723 fprintf(header
, "};\n");
724 fprintf(header
, "\n");
725 write_method_macro(iface
, iface
->name
);
726 fprintf(header
, "\n");
727 fprintf(header
, "#endif\n");
728 fprintf(header
, "\n");
730 fprintf(header
, "#define %s_METHODS \\\n", iface
->name
);
731 fprintf(header
, " ICOM_MSVTABLE_COMPAT_FIELDS");
732 write_icom_method_def(iface
);
733 fprintf(header
, "\n\n");
735 write_method_proto(iface
);
736 fprintf(header
, "\n");
738 if (!is_local(iface
->attrs
))
740 fprintf(header
,"#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
743 void write_rpc_interface(type_t
*iface
)
745 DWORD ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
747 if (!iface
->funcs
) return;
749 fprintf(header
, "/*****************************************************************************\n");
750 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
751 fprintf(header
, " */\n");
753 fprintf(header
, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
754 fprintf(header
, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
755 write_function_proto(iface
);
756 fprintf(header
, "\n");
758 /* FIXME: server/client code */
761 void write_interface(type_t
*iface
)
763 if (is_object(iface
->attrs
))
764 write_com_interface(iface
);
766 write_rpc_interface(iface
);