widl: Get rid of the tname field of var_t, simplify code.
[wine/testsucceed.git] / tools / widl / header.c
blob24ce8a0725942d30a423f0600c633e6470a3ce8d
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_list_t *list, enum attr_type t)
51 const attr_t *attr;
52 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
53 if (attr->type == t) return 1;
54 return 0;
57 void *get_attrp(const attr_list_t *list, enum attr_type t)
59 const attr_t *attr;
60 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
61 if (attr->type == t) return attr->u.pval;
62 return NULL;
65 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
67 const attr_t *attr;
68 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
69 if (attr->type == t) return attr->u.ival;
70 return 0;
73 int is_void(const type_t *t, const var_t *v)
75 if (v && v->ptr_level) return 0;
76 if (!t->type && !t->ref) return 1;
77 return 0;
80 int is_conformant_array( const array_dims_t *array )
82 expr_t *dim;
83 if (!array) return 0;
84 dim = LIST_ENTRY( list_head( array ), expr_t, entry );
85 return !dim->is_const;
88 int is_non_void(const expr_list_t *list)
90 const expr_t *expr;
92 if (list)
93 LIST_FOR_EACH_ENTRY( expr, list, const expr_t, entry )
94 if (expr->type != EXPR_VOID) return 1;
95 return 0;
98 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
100 if (!uuid) return;
101 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
102 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
103 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
104 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
105 uuid->Data4[6], uuid->Data4[7]);
108 static void write_pident(FILE *h, const var_t *v)
110 int c;
111 for (c=0; c<v->ptr_level; c++) {
112 fprintf(h, "*");
114 if (v->name) fprintf(h, "%s", v->name);
117 void write_name(FILE *h, const var_t *v)
119 if (is_attr( v->attrs, ATTR_PROPGET ))
120 fprintf(h, "get_" );
121 else if (is_attr( v->attrs, ATTR_PROPPUT ))
122 fprintf(h, "put_" );
123 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
124 fprintf(h, "putref_" );
125 fprintf(h, "%s", v->name);
128 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
130 fprintf(h, "%s", prefix);
131 write_name(h, v);
134 const char* get_name(const var_t *v)
136 return v->name;
139 void write_array(FILE *h, array_dims_t *dims, int field)
141 expr_t *v;
143 if (!dims) return;
144 fprintf(h, "[");
145 LIST_FOR_EACH_ENTRY( v, dims, expr_t, entry )
147 if (v->is_const)
148 fprintf(h, "%ld", v->cval); /* statically sized array */
149 else
150 if (field) fprintf(h, "1"); /* dynamically sized array */
151 if (list_next( dims, &v->entry ))
152 fprintf(h, ", ");
154 fprintf(h, "]");
157 static void write_field(FILE *h, var_t *v)
159 if (!v) return;
160 if (v->type) {
161 indent(h, 0);
162 write_type(h, v->type, NULL);
163 if (get_name(v)) {
164 fprintf(h, " ");
165 write_pident(h, v);
167 else {
168 /* not all C/C++ compilers support anonymous structs and unions */
169 switch (v->type->type) {
170 case RPC_FC_STRUCT:
171 case RPC_FC_CVSTRUCT:
172 case RPC_FC_CPSTRUCT:
173 case RPC_FC_CSTRUCT:
174 case RPC_FC_PSTRUCT:
175 case RPC_FC_BOGUS_STRUCT:
176 case RPC_FC_ENCAPSULATED_UNION:
177 fprintf(h, " DUMMYSTRUCTNAME");
178 break;
179 case RPC_FC_NON_ENCAPSULATED_UNION:
180 fprintf(h, " DUMMYUNIONNAME");
181 break;
182 default:
183 /* ? */
184 break;
187 write_array(h, v->array, 1);
188 fprintf(h, ";\n");
192 static void write_fields(FILE *h, var_list_t *fields)
194 var_t *v;
195 if (!fields) return;
196 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
199 static void write_enums(FILE *h, var_list_t *enums)
201 var_t *v;
202 if (!enums) return;
203 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
205 if (get_name(v)) {
206 indent(h, 0);
207 write_name(h, v);
208 if (v->eval) {
209 fprintf(h, " = ");
210 write_expr(h, v->eval, 0);
213 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
215 fprintf(h, "\n");
218 static int needs_space_after(type_t *t)
220 return t->kind == TKIND_ALIAS || ! is_ptr(t);
223 void write_type(FILE *h, type_t *t, const var_t *v)
225 int c;
227 if (t->is_const) fprintf(h, "const ");
229 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
230 else {
231 if (t->sign > 0) fprintf(h, "signed ");
232 else if (t->sign < 0) fprintf(h, "unsigned ");
233 switch (t->type) {
234 case RPC_FC_ENUM16:
235 case RPC_FC_ENUM32:
236 if (t->defined && !t->written && !t->ignore) {
237 if (t->name) fprintf(h, "enum %s {\n", t->name);
238 else fprintf(h, "enum {\n");
239 t->written = TRUE;
240 indentation++;
241 write_enums(h, t->fields);
242 indent(h, -1);
243 fprintf(h, "}");
245 else fprintf(h, "enum %s", t->name);
246 break;
247 case RPC_FC_STRUCT:
248 case RPC_FC_CVSTRUCT:
249 case RPC_FC_CPSTRUCT:
250 case RPC_FC_CSTRUCT:
251 case RPC_FC_PSTRUCT:
252 case RPC_FC_BOGUS_STRUCT:
253 case RPC_FC_ENCAPSULATED_UNION:
254 if (t->defined && !t->written && !t->ignore) {
255 if (t->name) fprintf(h, "struct %s {\n", t->name);
256 else fprintf(h, "struct {\n");
257 t->written = TRUE;
258 indentation++;
259 write_fields(h, t->fields);
260 indent(h, -1);
261 fprintf(h, "}");
263 else fprintf(h, "struct %s", t->name);
264 break;
265 case RPC_FC_NON_ENCAPSULATED_UNION:
266 if (t->defined && !t->written && !t->ignore) {
267 if (t->name) fprintf(h, "union %s {\n", t->name);
268 else fprintf(h, "union {\n");
269 t->written = TRUE;
270 indentation++;
271 write_fields(h, t->fields);
272 indent(h, -1);
273 fprintf(h, "}");
275 else fprintf(h, "union %s", t->name);
276 break;
277 case RPC_FC_RP:
278 case RPC_FC_UP:
279 case RPC_FC_FP:
280 case RPC_FC_OP:
281 if (t->ref) write_type(h, t->ref, NULL);
282 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
283 break;
284 default:
285 fprintf(h, "%s", t->name);
288 if (v) {
289 for (c=0; c<v->ptr_level; c++) {
290 fprintf(h, "*");
296 struct user_type
298 struct user_type *next;
299 char name[1];
302 static struct user_type *user_type_list;
304 static int user_type_registered(const char *name)
306 struct user_type *ut;
307 for (ut = user_type_list; ut; ut = ut->next)
308 if (!strcmp(name, ut->name))
309 return 1;
310 return 0;
313 static void check_for_user_types(const var_list_t *list)
315 const var_t *v;
317 if (!list) return;
318 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
320 type_t *type;
321 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
322 const char *name = type->name;
323 if (type->user_types_registered) continue;
324 type->user_types_registered = 1;
325 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
326 if (!user_type_registered(name))
328 struct user_type *ut = xmalloc(sizeof(struct user_type) + strlen(name));
329 strcpy(ut->name, name);
330 ut->next = user_type_list;
331 user_type_list = ut;
333 /* don't carry on parsing fields within this type as we are already
334 * using a wire marshaled type */
335 break;
337 else
339 check_for_user_types(type->fields);
345 void write_user_types(void)
347 struct user_type *ut;
348 for (ut = user_type_list; ut; ut = ut->next)
350 const char *name = ut->name;
351 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
352 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
353 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
354 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
358 void write_typedef(type_t *type)
360 fprintf(header, "typedef ");
361 write_type(header, type->orig, NULL);
362 fprintf(header, "%s%s;\n", needs_space_after(type->orig) ? " " : "", type->name);
365 void write_expr(FILE *h, const expr_t *e, int brackets)
367 switch (e->type) {
368 case EXPR_VOID:
369 break;
370 case EXPR_NUM:
371 fprintf(h, "%lu", e->u.lval);
372 break;
373 case EXPR_HEXNUM:
374 fprintf(h, "0x%lx", e->u.lval);
375 break;
376 case EXPR_TRUEFALSE:
377 if (e->u.lval == 0)
378 fprintf(h, "FALSE");
379 else
380 fprintf(h, "TRUE");
381 break;
382 case EXPR_IDENTIFIER:
383 fprintf(h, "%s", e->u.sval);
384 break;
385 case EXPR_NEG:
386 fprintf(h, "-");
387 write_expr(h, e->ref, 1);
388 break;
389 case EXPR_NOT:
390 fprintf(h, "~");
391 write_expr(h, e->ref, 1);
392 break;
393 case EXPR_PPTR:
394 fprintf(h, "*");
395 write_expr(h, e->ref, 1);
396 break;
397 case EXPR_CAST:
398 fprintf(h, "(");
399 write_type(h, e->u.tref, NULL);
400 fprintf(h, ")");
401 write_expr(h, e->ref, 1);
402 break;
403 case EXPR_SIZEOF:
404 fprintf(h, "sizeof(");
405 write_type(h, e->u.tref, NULL);
406 fprintf(h, ")");
407 break;
408 case EXPR_SHL:
409 case EXPR_SHR:
410 case EXPR_MUL:
411 case EXPR_DIV:
412 case EXPR_ADD:
413 case EXPR_SUB:
414 case EXPR_AND:
415 case EXPR_OR:
416 if (brackets) fprintf(h, "(");
417 write_expr(h, e->ref, 1);
418 switch (e->type) {
419 case EXPR_SHL: fprintf(h, " << "); break;
420 case EXPR_SHR: fprintf(h, " >> "); break;
421 case EXPR_MUL: fprintf(h, " * "); break;
422 case EXPR_DIV: fprintf(h, " / "); break;
423 case EXPR_ADD: fprintf(h, " + "); break;
424 case EXPR_SUB: fprintf(h, " - "); break;
425 case EXPR_AND: fprintf(h, " & "); break;
426 case EXPR_OR: fprintf(h, " | "); break;
427 default: break;
429 write_expr(h, e->u.ext, 1);
430 if (brackets) fprintf(h, ")");
431 break;
432 case EXPR_COND:
433 if (brackets) fprintf(h, "(");
434 write_expr(h, e->ref, 1);
435 fprintf(h, " ? ");
436 write_expr(h, e->u.ext, 1);
437 fprintf(h, " : ");
438 write_expr(h, e->ext2, 1);
439 if (brackets) fprintf(h, ")");
440 break;
444 void write_constdef(const var_t *v)
446 fprintf(header, "#define %s (", get_name(v));
447 write_expr(header, v->eval, 0);
448 fprintf(header, ")\n\n");
451 void write_externdef(const var_t *v)
453 fprintf(header, "extern const ");
454 write_type(header, v->type, NULL);
455 if (get_name(v)) {
456 fprintf(header, " ");
457 write_pident(header, v);
459 fprintf(header, ";\n\n");
462 void write_library(const char *name, const attr_list_t *attr)
464 const UUID *uuid = get_attrp(attr, ATTR_UUID);
465 fprintf(header, "\n");
466 write_guid(header, "LIBID", name, uuid);
467 fprintf(header, "\n");
471 const var_t* get_explicit_handle_var(const func_t* func)
473 const var_t* var;
475 if (!func->args)
476 return NULL;
478 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
479 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
480 return var;
482 return NULL;
485 int has_out_arg_or_return(const func_t *func)
487 const var_t *var;
489 if (!is_void(func->def->type, NULL))
490 return 1;
492 if (!func->args)
493 return 0;
495 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
496 if (is_attr(var->attrs, ATTR_OUT))
497 return 1;
499 return 0;
503 /********** INTERFACES **********/
505 int is_object(const attr_list_t *list)
507 const attr_t *attr;
508 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
509 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
510 return 0;
513 int is_local(const attr_list_t *a)
515 return is_attr(a, ATTR_LOCAL);
518 const var_t *is_callas(const attr_list_t *a)
520 return get_attrp(a, ATTR_CALLAS);
523 static void write_method_macro(const type_t *iface, const char *name)
525 const func_t *cur;
527 if (iface->ref) write_method_macro(iface->ref, name);
529 if (!iface->funcs) return;
531 fprintf(header, "/*** %s methods ***/\n", iface->name);
532 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
534 var_t *def = cur->def;
535 if (!is_callas(def->attrs)) {
536 const var_t *arg;
537 int argc = 0;
538 int c;
540 if (cur->args) LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry ) argc++;
542 fprintf(header, "#define %s_", name);
543 write_name(header,def);
544 fprintf(header, "(p");
545 for (c=0; c<argc; c++)
546 fprintf(header, ",%c", c+'a');
547 fprintf(header, ") ");
549 fprintf(header, "(p)->lpVtbl->");
550 write_name(header, def);
551 fprintf(header, "(p");
552 for (c=0; c<argc; c++)
553 fprintf(header, ",%c", c+'a');
554 fprintf(header, ")\n");
559 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
561 const var_t *arg;
562 int count = 0;
564 if (do_indent)
566 indentation++;
567 indent(h, 0);
569 if (method == 1) {
570 fprintf(h, "%s* This", name);
571 count++;
573 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
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);
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 count++;
600 if (do_indent) indentation--;
603 static void write_cpp_method_def(const type_t *iface)
605 const func_t *cur;
607 if (!iface->funcs) return;
609 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
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);
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");
626 static void do_write_c_method_def(const type_t *iface, const char *name)
628 const func_t *cur;
630 if (iface->ref) do_write_c_method_def(iface->ref, name);
632 if (!iface->funcs) return;
633 indent(header, 0);
634 fprintf(header, "/*** %s methods ***/\n", iface->name);
635 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
637 const var_t *def = cur->def;
638 if (!is_callas(def->attrs)) {
639 indent(header, 0);
640 write_type(header, def->type, def);
641 fprintf(header, " (STDMETHODCALLTYPE *");
642 write_name(header, def);
643 fprintf(header, ")(\n");
644 write_args(header, cur->args, name, 1, TRUE);
645 fprintf(header, ");\n");
646 fprintf(header, "\n");
651 static void write_c_method_def(const type_t *iface)
653 do_write_c_method_def(iface, iface->name);
656 static void write_c_disp_method_def(const type_t *iface)
658 do_write_c_method_def(iface->ref, iface->name);
661 static void write_method_proto(const type_t *iface)
663 const func_t *cur;
665 if (!iface->funcs) return;
666 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
668 const var_t *def = cur->def;
669 const var_t *cas = is_callas(def->attrs);
671 if (!is_local(def->attrs)) {
672 /* proxy prototype */
673 write_type(header, def->type, def);
674 fprintf(header, " CALLBACK %s_", iface->name);
675 write_name(header, def);
676 fprintf(header, "_Proxy(\n");
677 write_args(header, cur->args, iface->name, 1, TRUE);
678 fprintf(header, ");\n");
679 /* stub prototype */
680 fprintf(header, "void __RPC_STUB %s_", iface->name);
681 write_name(header,def);
682 fprintf(header, "_Stub(\n");
683 fprintf(header, " IRpcStubBuffer* This,\n");
684 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
685 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
686 fprintf(header, " DWORD* pdwStubPhase);\n");
687 check_for_user_types(cur->args);
689 if (cas) {
690 const func_t *m;
691 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
692 if (!strcmp(get_name(m->def), cas->name)) break;
693 if (&m->entry != iface->funcs) {
694 const var_t *mdef = m->def;
695 /* proxy prototype - use local prototype */
696 write_type(header, mdef->type, mdef);
697 fprintf(header, " CALLBACK %s_", iface->name);
698 write_name(header, mdef);
699 fprintf(header, "_Proxy(\n");
700 write_args(header, m->args, iface->name, 1, TRUE);
701 fprintf(header, ");\n");
702 /* stub prototype - use remotable prototype */
703 write_type(header, def->type, def);
704 fprintf(header, " __RPC_STUB %s_", iface->name);
705 write_name(header, mdef);
706 fprintf(header, "_Stub(\n");
707 write_args(header, cur->args, iface->name, 1, TRUE);
708 fprintf(header, ");\n");
710 else {
711 parser_warning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
717 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
719 var_t *def = fun->def;
721 /* FIXME: do we need to handle call_as? */
722 write_type(header, def->type, def);
723 fprintf(header, " ");
724 write_prefix_name(header, prefix, def);
725 fprintf(header, "(\n");
726 if (fun->args)
727 write_args(header, fun->args, iface->name, 0, TRUE);
728 else
729 fprintf(header, " void");
730 fprintf(header, ");\n");
733 static void write_function_protos(const type_t *iface)
735 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
736 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
737 const var_t* explicit_handle_var;
738 const func_t *cur;
739 int prefixes_differ = strcmp(prefix_client, prefix_server);
741 if (!iface->funcs) return;
742 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
744 var_t *def = cur->def;
746 /* check for a defined binding handle */
747 explicit_handle_var = get_explicit_handle_var(cur);
748 if (explicit_handle) {
749 if (!explicit_handle_var) {
750 error("%s() does not define an explicit binding handle!\n", def->name);
751 return;
753 } else if (implicit_handle) {
754 if (explicit_handle_var) {
755 error("%s() must not define a binding handle!\n", def->name);
756 return;
760 if (prefixes_differ) {
761 fprintf(header, "/* client prototype */\n");
762 write_function_proto(iface, cur, prefix_client);
763 fprintf(header, "/* server prototype */\n");
765 write_function_proto(iface, cur, prefix_server);
769 void write_forward(type_t *iface)
771 /* C/C++ forwards should only be written for object interfaces, so if we
772 * have a full definition we only write one if we find [object] among the
773 * attributes - however, if we don't have a full definition at this point
774 * (i.e. this is an IDL forward), then we also assume that it is an object
775 * interface, since non-object interfaces shouldn't need forwards */
776 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
777 && !iface->written) {
778 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
779 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
780 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
781 fprintf(header, "#endif\n\n" );
782 iface->written = TRUE;
786 static void write_iface_guid(const type_t *iface)
788 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
789 write_guid(header, "IID", iface->name, uuid);
792 static void write_dispiface_guid(const type_t *iface)
794 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
795 write_guid(header, "DIID", iface->name, uuid);
798 static void write_coclass_guid(type_t *cocl)
800 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
801 write_guid(header, "CLSID", cocl->name, uuid);
804 static void write_com_interface(type_t *iface)
806 if (!iface->funcs && !iface->ref) {
807 parser_warning("%s has no methods", iface->name);
808 return;
811 fprintf(header, "/*****************************************************************************\n");
812 fprintf(header, " * %s interface\n", iface->name);
813 fprintf(header, " */\n");
814 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
815 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
816 write_iface_guid(iface);
817 write_forward(iface);
818 /* C++ interface */
819 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
820 if (iface->ref)
822 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
823 fprintf(header, "{\n");
824 indentation++;
825 write_cpp_method_def(iface);
826 indentation--;
827 fprintf(header, "};\n");
829 else
831 fprintf(header, "interface %s\n", iface->name);
832 fprintf(header, "{\n");
833 fprintf(header, " BEGIN_INTERFACE\n");
834 fprintf(header, "\n");
835 indentation++;
836 write_cpp_method_def(iface);
837 indentation--;
838 fprintf(header, " END_INTERFACE\n");
839 fprintf(header, "};\n");
841 fprintf(header, "#else\n");
842 /* C interface */
843 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
844 indentation++;
845 fprintf(header, " BEGIN_INTERFACE\n");
846 fprintf(header, "\n");
847 write_c_method_def(iface);
848 indentation--;
849 fprintf(header, " END_INTERFACE\n");
850 fprintf(header, "} %sVtbl;\n", iface->name);
851 fprintf(header, "interface %s {\n", iface->name);
852 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
853 fprintf(header, "};\n");
854 fprintf(header, "\n");
855 fprintf(header, "#ifdef COBJMACROS\n");
856 write_method_macro(iface, iface->name);
857 fprintf(header, "#endif\n");
858 fprintf(header, "\n");
859 fprintf(header, "#endif\n");
860 fprintf(header, "\n");
861 write_method_proto(iface);
862 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
865 static void write_rpc_interface(const type_t *iface)
867 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
868 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
869 static int allocate_written = 0;
871 if (!allocate_written)
873 allocate_written = 1;
874 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
875 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
878 fprintf(header, "/*****************************************************************************\n");
879 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
880 fprintf(header, " */\n");
881 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
882 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
883 if (iface->funcs)
885 write_iface_guid(iface);
886 if (var) fprintf(header, "extern handle_t %s;\n", var);
887 if (old_names)
889 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
890 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
892 else
894 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
895 prefix_client, iface->name, LOWORD(ver), HIWORD(ver));
896 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
897 prefix_server, iface->name, LOWORD(ver), HIWORD(ver));
899 write_function_protos(iface);
901 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
903 /* FIXME: server/client code */
906 void write_interface(type_t *iface)
908 if (is_object(iface->attrs))
909 write_com_interface(iface);
910 else
911 write_rpc_interface(iface);
914 void write_dispinterface(type_t *iface)
916 fprintf(header, "/*****************************************************************************\n");
917 fprintf(header, " * %s dispinterface\n", iface->name);
918 fprintf(header, " */\n");
919 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
920 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
921 write_dispiface_guid(iface);
922 write_forward(iface);
923 /* C++ interface */
924 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
925 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
926 fprintf(header, "{\n");
927 fprintf(header, "};\n");
928 fprintf(header, "#else\n");
929 /* C interface */
930 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
931 indentation++;
932 fprintf(header, " BEGIN_INTERFACE\n");
933 fprintf(header, "\n");
934 write_c_disp_method_def(iface);
935 indentation--;
936 fprintf(header, " END_INTERFACE\n");
937 fprintf(header, "} %sVtbl;\n", iface->name);
938 fprintf(header, "interface %s {\n", iface->name);
939 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
940 fprintf(header, "};\n");
941 fprintf(header, "\n");
942 fprintf(header, "#ifdef COBJMACROS\n");
943 write_method_macro(iface->ref, iface->name);
944 fprintf(header, "#endif\n");
945 fprintf(header, "\n");
946 fprintf(header, "#endif\n");
947 fprintf(header, "\n");
948 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
951 void write_coclass(type_t *cocl)
953 fprintf(header, "/*****************************************************************************\n");
954 fprintf(header, " * %s coclass\n", cocl->name);
955 fprintf(header, " */\n\n");
956 write_coclass_guid(cocl);
957 fprintf(header, "\n");
960 void write_coclass_forward(type_t *cocl)
962 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
963 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
964 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
965 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );