widl: Create a list of statements in the whole IDL file, instead of just a list of...
[wine/wine64.git] / tools / widl / header.c
blob0d7e0b2f94546f8b3849bb36ae1bf4def30bce61
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 <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "expr.h"
38 typedef struct _user_type_t generic_handle_t;
40 static int indentation = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
45 static void indent(FILE *h, int delta)
47 int c;
48 if (delta < 0) indentation += delta;
49 for (c=0; c<indentation; c++) fprintf(h, " ");
50 if (delta > 0) indentation += delta;
53 int is_ptrchain_attr(const var_t *var, enum attr_type t)
55 if (is_attr(var->attrs, t))
56 return 1;
57 else
59 type_t *type = var->type;
60 for (;;)
62 if (is_attr(type->attrs, t))
63 return 1;
64 else if (type->kind == TKIND_ALIAS)
65 type = type->orig;
66 else if (is_ptr(type))
67 type = type->ref;
68 else return 0;
73 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
75 const type_t *t = type;
76 for (;;)
78 if (is_attr(t->attrs, attr))
79 return 1;
80 else if (t->kind == TKIND_ALIAS)
81 t = t->orig;
82 else return 0;
86 int is_attr(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 1;
91 return 0;
94 void *get_attrp(const attr_list_t *list, enum attr_type t)
96 const attr_t *attr;
97 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
98 if (attr->type == t) return attr->u.pval;
99 return NULL;
102 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
104 const attr_t *attr;
105 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
106 if (attr->type == t) return attr->u.ival;
107 return 0;
110 int is_void(const type_t *t)
112 if (!t->type && !t->ref) return 1;
113 return 0;
116 int is_conformant_array(const type_t *t)
118 return t->type == RPC_FC_CARRAY
119 || t->type == RPC_FC_CVARRAY
120 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
123 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
125 if (!uuid) return;
126 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
127 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
128 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
129 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
130 uuid->Data4[6], uuid->Data4[7]);
133 void write_name(FILE *h, const var_t *v)
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 fprintf(h, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 fprintf(h, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 fprintf(h, "putref_" );
141 fprintf(h, "%s", v->name);
144 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
146 fprintf(h, "%s", prefix);
147 write_name(h, v);
150 static void write_field(FILE *h, var_t *v)
152 if (!v) return;
153 if (v->type) {
154 const char *name = v->name;
155 if (name == NULL) {
156 switch (v->type->type) {
157 case RPC_FC_STRUCT:
158 case RPC_FC_CVSTRUCT:
159 case RPC_FC_CPSTRUCT:
160 case RPC_FC_CSTRUCT:
161 case RPC_FC_PSTRUCT:
162 case RPC_FC_BOGUS_STRUCT:
163 case RPC_FC_ENCAPSULATED_UNION:
164 name = "DUMMYSTRUCTNAME";
165 break;
166 case RPC_FC_NON_ENCAPSULATED_UNION:
167 name = "DUMMYUNIONNAME";
168 break;
169 default:
170 /* ? */
171 break;
174 indent(h, 0);
175 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
176 fprintf(h, ";\n");
180 static void write_fields(FILE *h, var_list_t *fields)
182 var_t *v;
183 if (!fields) return;
184 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
187 static void write_enums(FILE *h, var_list_t *enums)
189 var_t *v;
190 if (!enums) return;
191 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
193 if (v->name) {
194 indent(h, 0);
195 write_name(h, v);
196 if (v->eval) {
197 fprintf(h, " = ");
198 write_expr(h, v->eval, 0, 1, NULL, NULL);
201 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
203 fprintf(h, "\n");
206 int needs_space_after(type_t *t)
208 return (t->kind == TKIND_ALIAS
209 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
212 void write_type_left(FILE *h, type_t *t, int declonly)
214 if (!h) return;
216 if (t->is_const) fprintf(h, "const ");
218 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
219 else if (t->declarray) write_type_left(h, t->ref, declonly);
220 else {
221 if (t->sign > 0) fprintf(h, "signed ");
222 else if (t->sign < 0) fprintf(h, "unsigned ");
223 switch (t->type) {
224 case RPC_FC_ENUM16:
225 case RPC_FC_ENUM32:
226 if (!declonly && t->defined && !t->written && !t->ignore) {
227 if (t->name) fprintf(h, "enum %s {\n", t->name);
228 else fprintf(h, "enum {\n");
229 t->written = TRUE;
230 indentation++;
231 write_enums(h, t->fields_or_args);
232 indent(h, -1);
233 fprintf(h, "}");
235 else fprintf(h, "enum %s", t->name ? t->name : "");
236 break;
237 case RPC_FC_STRUCT:
238 case RPC_FC_CVSTRUCT:
239 case RPC_FC_CPSTRUCT:
240 case RPC_FC_CSTRUCT:
241 case RPC_FC_PSTRUCT:
242 case RPC_FC_BOGUS_STRUCT:
243 case RPC_FC_ENCAPSULATED_UNION:
244 if (!declonly && t->defined && !t->written && !t->ignore) {
245 if (t->name) fprintf(h, "struct %s {\n", t->name);
246 else fprintf(h, "struct {\n");
247 t->written = TRUE;
248 indentation++;
249 write_fields(h, t->fields_or_args);
250 indent(h, -1);
251 fprintf(h, "}");
253 else fprintf(h, "struct %s", t->name ? t->name : "");
254 break;
255 case RPC_FC_NON_ENCAPSULATED_UNION:
256 if (!declonly && t->defined && !t->written && !t->ignore) {
257 if (t->name) fprintf(h, "union %s {\n", t->name);
258 else fprintf(h, "union {\n");
259 t->written = TRUE;
260 indentation++;
261 write_fields(h, t->fields_or_args);
262 indent(h, -1);
263 fprintf(h, "}");
265 else fprintf(h, "union %s", t->name ? t->name : "");
266 break;
267 case RPC_FC_RP:
268 case RPC_FC_UP:
269 case RPC_FC_FP:
270 case RPC_FC_OP:
271 case RPC_FC_CARRAY:
272 case RPC_FC_CVARRAY:
273 case RPC_FC_BOGUS_ARRAY:
274 write_type_left(h, t->ref, declonly);
275 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
276 break;
277 default:
278 fprintf(h, "%s", t->name);
283 void write_type_right(FILE *h, type_t *t, int is_field)
285 if (!h) return;
287 if (t->declarray) {
288 if (is_conformant_array(t)) {
289 fprintf(h, "[%s]", is_field ? "1" : "");
290 t = t->ref;
292 for ( ; t->declarray; t = t->ref)
293 fprintf(h, "[%lu]", t->dim);
297 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
298 const char *fmt, va_list args)
300 type_t *pt;
301 int ptr_level = 0;
303 if (!h) return;
305 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
308 if (pt->type == RPC_FC_FUNCTION) {
309 int i;
310 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
311 if (!callconv) callconv = "";
312 write_type_left(h, pt->ref, declonly);
313 fputc(' ', h);
314 if (ptr_level) fputc('(', h);
315 fprintf(h, "%s ", callconv);
316 for (i = 0; i < ptr_level; i++)
317 fputc('*', h);
318 } else
319 write_type_left(h, t, declonly);
320 if (fmt) {
321 if (needs_space_after(t))
322 fputc(' ', h);
323 vfprintf(h, fmt, args);
325 if (pt->type == RPC_FC_FUNCTION) {
326 if (ptr_level) fputc(')', h);
327 fputc('(', h);
328 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
329 fputc(')', h);
330 } else
331 write_type_right(h, t, is_field);
334 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
336 va_list args;
337 va_start(args, fmt);
338 write_type_v(f, t, field, FALSE, fmt, args);
339 va_end(args);
342 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
344 va_list args;
345 va_start(args, fmt);
346 write_type_v(f, t, FALSE, TRUE, fmt, args);
347 va_end(args);
350 void write_type_decl_left(FILE *f, type_t *t)
352 write_type_left(f, t, TRUE);
355 static int user_type_registered(const char *name)
357 user_type_t *ut;
358 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
359 if (!strcmp(name, ut->name))
360 return 1;
361 return 0;
364 static int context_handle_registered(const char *name)
366 context_handle_t *ch;
367 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
368 if (!strcmp(name, ch->name))
369 return 1;
370 return 0;
373 static int generic_handle_registered(const char *name)
375 generic_handle_t *gh;
376 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
377 if (!strcmp(name, gh->name))
378 return 1;
379 return 0;
382 /* check for types which require additional prototypes to be generated in the
383 * header */
384 void check_for_additional_prototype_types(const var_list_t *list)
386 const var_t *v;
388 if (!list) return;
389 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
391 type_t *type;
392 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
393 const char *name = type->name;
394 if (type->user_types_registered) continue;
395 type->user_types_registered = 1;
396 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
397 if (!context_handle_registered(name))
399 context_handle_t *ch = xmalloc(sizeof(*ch));
400 ch->name = xstrdup(name);
401 list_add_tail(&context_handle_list, &ch->entry);
403 /* don't carry on parsing fields within this type */
404 break;
406 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
407 if (!generic_handle_registered(name))
409 generic_handle_t *gh = xmalloc(sizeof(*gh));
410 gh->name = xstrdup(name);
411 list_add_tail(&generic_handle_list, &gh->entry);
413 /* don't carry on parsing fields within this type */
414 break;
416 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
417 if (!user_type_registered(name))
419 user_type_t *ut = xmalloc(sizeof *ut);
420 ut->name = xstrdup(name);
421 list_add_tail(&user_type_list, &ut->entry);
423 /* don't carry on parsing fields within this type as we are already
424 * using a wire marshaled type */
425 break;
427 else
429 check_for_additional_prototype_types(type->fields_or_args);
435 void write_user_types(void)
437 user_type_t *ut;
438 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
440 const char *name = ut->name;
441 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
442 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
443 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
444 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
448 void write_context_handle_rundowns(void)
450 context_handle_t *ch;
451 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
453 const char *name = ch->name;
454 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
458 void write_generic_handle_routines(void)
460 generic_handle_t *gh;
461 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
463 const char *name = gh->name;
464 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
465 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
469 void write_typedef(type_t *type)
471 fprintf(header, "typedef ");
472 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
473 fprintf(header, ";\n");
476 void write_constdef(const var_t *v)
478 fprintf(header, "#define %s (", v->name);
479 write_expr(header, v->eval, 0, 1, NULL, NULL);
480 fprintf(header, ")\n\n");
483 void write_externdef(const var_t *v)
485 fprintf(header, "extern const ");
486 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
487 fprintf(header, ";\n\n");
490 void write_library(const typelib_t *typelib)
492 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
493 fprintf(header, "\n");
494 write_guid(header, "LIBID", typelib->name, uuid);
495 fprintf(header, "\n");
499 const var_t* get_explicit_handle_var(const func_t* func)
501 const var_t* var;
503 if (!func->args)
504 return NULL;
506 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
507 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
508 return var;
510 return NULL;
513 const type_t* get_explicit_generic_handle_type(const var_t* var)
515 const type_t *t;
516 for (t = var->type; is_ptr(t); t = t->ref)
517 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
518 return t;
519 return NULL;
522 const var_t* get_explicit_generic_handle_var(const func_t* func)
524 const var_t* var;
526 if (!func->args)
527 return NULL;
529 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
530 if (get_explicit_generic_handle_type(var))
531 return var;
533 return NULL;
536 const var_t* get_context_handle_var(const func_t* func)
538 const var_t* var;
540 if (!func->args)
541 return NULL;
543 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
544 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
545 return var;
547 return NULL;
550 int has_out_arg_or_return(const func_t *func)
552 const var_t *var;
554 if (!is_void(get_func_return_type(func)))
555 return 1;
557 if (!func->args)
558 return 0;
560 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
561 if (is_attr(var->attrs, ATTR_OUT))
562 return 1;
564 return 0;
568 /********** INTERFACES **********/
570 int is_object(const attr_list_t *list)
572 const attr_t *attr;
573 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
574 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
575 return 0;
578 int is_local(const attr_list_t *a)
580 return is_attr(a, ATTR_LOCAL);
583 const var_t *is_callas(const attr_list_t *a)
585 return get_attrp(a, ATTR_CALLAS);
588 static void write_method_macro(const type_t *iface, const char *name)
590 const func_t *cur;
592 if (iface->ref) write_method_macro(iface->ref, name);
594 if (!iface->funcs) return;
596 fprintf(header, "/*** %s methods ***/\n", iface->name);
597 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
599 var_t *def = cur->def;
600 if (!is_callas(def->attrs)) {
601 const var_t *arg;
603 fprintf(header, "#define %s_", name);
604 write_name(header,def);
605 fprintf(header, "(This");
606 if (cur->args)
607 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
608 fprintf(header, ",%s", arg->name);
609 fprintf(header, ") ");
611 fprintf(header, "(This)->lpVtbl->");
612 write_name(header, def);
613 fprintf(header, "(This");
614 if (cur->args)
615 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
616 fprintf(header, ",%s", arg->name);
617 fprintf(header, ")\n");
622 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
624 const var_t *arg;
625 int count = 0;
627 if (do_indent)
629 indentation++;
630 indent(h, 0);
632 if (method == 1) {
633 fprintf(h, "%s* This", name);
634 count++;
636 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
637 if (count) {
638 if (do_indent)
640 fprintf(h, ",\n");
641 indent(h, 0);
643 else fprintf(h, ",");
645 write_type_decl(h, arg->type, "%s", arg->name);
646 count++;
648 if (do_indent) indentation--;
651 static void write_cpp_method_def(const type_t *iface)
653 const func_t *cur;
655 if (!iface->funcs) return;
657 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
659 var_t *def = cur->def;
660 if (!is_callas(def->attrs)) {
661 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
662 if (!callconv) callconv = "";
663 indent(header, 0);
664 fprintf(header, "virtual ");
665 write_type_decl_left(header, get_func_return_type(cur));
666 fprintf(header, " %s ", callconv);
667 write_name(header, def);
668 fprintf(header, "(\n");
669 write_args(header, cur->args, iface->name, 2, TRUE);
670 fprintf(header, ") = 0;\n");
671 fprintf(header, "\n");
676 static void do_write_c_method_def(const type_t *iface, const char *name)
678 const func_t *cur;
680 if (iface->ref) do_write_c_method_def(iface->ref, name);
682 if (!iface->funcs) return;
683 indent(header, 0);
684 fprintf(header, "/*** %s methods ***/\n", iface->name);
685 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
687 const var_t *def = cur->def;
688 if (!is_callas(def->attrs)) {
689 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
690 if (!callconv) callconv = "";
691 indent(header, 0);
692 write_type_decl_left(header, get_func_return_type(cur));
693 fprintf(header, " (%s *", callconv);
694 write_name(header, def);
695 fprintf(header, ")(\n");
696 write_args(header, cur->args, name, 1, TRUE);
697 fprintf(header, ");\n");
698 fprintf(header, "\n");
703 static void write_c_method_def(const type_t *iface)
705 do_write_c_method_def(iface, iface->name);
708 static void write_c_disp_method_def(const type_t *iface)
710 do_write_c_method_def(iface->ref, iface->name);
713 static void write_method_proto(const type_t *iface)
715 const func_t *cur;
717 if (!iface->funcs) return;
718 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
720 const var_t *def = cur->def;
722 if (!is_local(def->attrs)) {
723 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
724 if (!callconv) callconv = "";
725 /* proxy prototype */
726 write_type_decl_left(header, get_func_return_type(cur));
727 fprintf(header, " %s %s_", callconv, iface->name);
728 write_name(header, def);
729 fprintf(header, "_Proxy(\n");
730 write_args(header, cur->args, iface->name, 1, TRUE);
731 fprintf(header, ");\n");
732 /* stub prototype */
733 fprintf(header, "void __RPC_STUB %s_", iface->name);
734 write_name(header,def);
735 fprintf(header, "_Stub(\n");
736 fprintf(header, " IRpcStubBuffer* This,\n");
737 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
738 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
739 fprintf(header, " DWORD* pdwStubPhase);\n");
744 void write_locals(FILE *fp, const type_t *iface, int body)
746 static const char comment[]
747 = "/* WIDL-generated stub. You must provide an implementation for this. */";
748 const func_list_t *funcs = iface->funcs;
749 const func_t *cur;
751 if (!is_object(iface->attrs) || !funcs)
752 return;
754 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
755 const var_t *def = cur->def;
756 const var_t *cas = is_callas(def->attrs);
758 if (cas) {
759 const func_t *m;
760 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
761 if (!strcmp(m->def->name, cas->name))
762 break;
763 if (&m->entry != iface->funcs) {
764 const var_t *mdef = m->def;
765 /* proxy prototype - use local prototype */
766 write_type_decl_left(fp, get_func_return_type(m));
767 fprintf(fp, " CALLBACK %s_", iface->name);
768 write_name(fp, mdef);
769 fprintf(fp, "_Proxy(\n");
770 write_args(fp, m->args, iface->name, 1, TRUE);
771 fprintf(fp, ")");
772 if (body) {
773 type_t *rt = get_func_return_type(m);
774 fprintf(fp, "\n{\n");
775 fprintf(fp, " %s\n", comment);
776 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
777 fprintf(fp, " return E_NOTIMPL;\n");
778 else if (rt->type) {
779 fprintf(fp, " ");
780 write_type_decl(fp, rt, "rv");
781 fprintf(fp, ";\n");
782 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
783 fprintf(fp, " return rv;\n");
785 fprintf(fp, "}\n\n");
787 else
788 fprintf(fp, ";\n");
789 /* stub prototype - use remotable prototype */
790 write_type_decl_left(fp, get_func_return_type(cur));
791 fprintf(fp, " __RPC_STUB %s_", iface->name);
792 write_name(fp, mdef);
793 fprintf(fp, "_Stub(\n");
794 write_args(fp, cur->args, iface->name, 1, TRUE);
795 fprintf(fp, ")");
796 if (body)
797 /* Remotable methods must all return HRESULTs. */
798 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
799 else
800 fprintf(fp, ";\n");
802 else
803 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
808 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
810 var_t *def = fun->def;
811 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
813 /* FIXME: do we need to handle call_as? */
814 write_type_decl_left(header, get_func_return_type(fun));
815 fprintf(header, " ");
816 if (callconv) fprintf(header, "%s ", callconv);
817 write_prefix_name(header, prefix, def);
818 fprintf(header, "(\n");
819 if (fun->args)
820 write_args(header, fun->args, iface->name, 0, TRUE);
821 else
822 fprintf(header, " void");
823 fprintf(header, ");\n\n");
826 static void write_function_protos(const type_t *iface)
828 const func_t *cur;
829 int prefixes_differ = strcmp(prefix_client, prefix_server);
831 if (!iface->funcs) return;
832 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
834 if (prefixes_differ) {
835 fprintf(header, "/* client prototype */\n");
836 write_function_proto(iface, cur, prefix_client);
837 fprintf(header, "/* server prototype */\n");
839 write_function_proto(iface, cur, prefix_server);
843 void write_forward(type_t *iface)
845 /* C/C++ forwards should only be written for object interfaces, so if we
846 * have a full definition we only write one if we find [object] among the
847 * attributes - however, if we don't have a full definition at this point
848 * (i.e. this is an IDL forward), then we also assume that it is an object
849 * interface, since non-object interfaces shouldn't need forwards */
850 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
851 && !iface->written) {
852 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
853 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
854 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
855 fprintf(header, "#endif\n\n" );
856 iface->written = TRUE;
860 static void write_iface_guid(const type_t *iface)
862 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
863 write_guid(header, "IID", iface->name, uuid);
866 static void write_dispiface_guid(const type_t *iface)
868 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
869 write_guid(header, "DIID", iface->name, uuid);
872 static void write_coclass_guid(type_t *cocl)
874 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
875 write_guid(header, "CLSID", cocl->name, uuid);
878 static void write_com_interface(type_t *iface)
880 if (!iface->funcs && !iface->ref) {
881 parser_warning("%s has no methods\n", iface->name);
882 return;
885 fprintf(header, "/*****************************************************************************\n");
886 fprintf(header, " * %s interface\n", iface->name);
887 fprintf(header, " */\n");
888 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
889 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
890 write_iface_guid(iface);
891 write_forward(iface);
892 /* C++ interface */
893 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
894 if (iface->ref)
896 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
897 fprintf(header, "{\n");
898 indentation++;
899 write_cpp_method_def(iface);
900 indentation--;
901 fprintf(header, "};\n");
903 else
905 fprintf(header, "interface %s\n", iface->name);
906 fprintf(header, "{\n");
907 fprintf(header, " BEGIN_INTERFACE\n");
908 fprintf(header, "\n");
909 indentation++;
910 write_cpp_method_def(iface);
911 indentation--;
912 fprintf(header, " END_INTERFACE\n");
913 fprintf(header, "};\n");
915 fprintf(header, "#else\n");
916 /* C interface */
917 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
918 indentation++;
919 fprintf(header, " BEGIN_INTERFACE\n");
920 fprintf(header, "\n");
921 write_c_method_def(iface);
922 indentation--;
923 fprintf(header, " END_INTERFACE\n");
924 fprintf(header, "} %sVtbl;\n", iface->name);
925 fprintf(header, "interface %s {\n", iface->name);
926 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
927 fprintf(header, "};\n");
928 fprintf(header, "\n");
929 fprintf(header, "#ifdef COBJMACROS\n");
930 write_method_macro(iface, iface->name);
931 fprintf(header, "#endif\n");
932 fprintf(header, "\n");
933 fprintf(header, "#endif\n");
934 fprintf(header, "\n");
935 write_method_proto(iface);
936 write_locals(header, iface, FALSE);
937 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
940 static void write_rpc_interface(const type_t *iface)
942 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
943 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
944 static int allocate_written = 0;
946 if (!allocate_written)
948 allocate_written = 1;
949 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
950 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
953 fprintf(header, "/*****************************************************************************\n");
954 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
955 fprintf(header, " */\n");
956 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
957 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
958 if (iface->funcs)
960 write_iface_guid(iface);
961 if (var) fprintf(header, "extern handle_t %s;\n", var);
962 if (old_names)
964 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
965 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
967 else
969 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
970 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
971 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
972 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
974 write_function_protos(iface);
976 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
978 /* FIXME: server/client code */
981 void write_interface(type_t *iface)
983 if (is_object(iface->attrs))
984 write_com_interface(iface);
985 else
986 write_rpc_interface(iface);
989 void write_dispinterface(type_t *iface)
991 fprintf(header, "/*****************************************************************************\n");
992 fprintf(header, " * %s dispinterface\n", iface->name);
993 fprintf(header, " */\n");
994 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
995 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
996 write_dispiface_guid(iface);
997 write_forward(iface);
998 /* C++ interface */
999 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1000 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
1001 fprintf(header, "{\n");
1002 fprintf(header, "};\n");
1003 fprintf(header, "#else\n");
1004 /* C interface */
1005 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1006 indentation++;
1007 fprintf(header, " BEGIN_INTERFACE\n");
1008 fprintf(header, "\n");
1009 write_c_disp_method_def(iface);
1010 indentation--;
1011 fprintf(header, " END_INTERFACE\n");
1012 fprintf(header, "} %sVtbl;\n", iface->name);
1013 fprintf(header, "interface %s {\n", iface->name);
1014 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1015 fprintf(header, "};\n");
1016 fprintf(header, "\n");
1017 fprintf(header, "#ifdef COBJMACROS\n");
1018 write_method_macro(iface->ref, iface->name);
1019 fprintf(header, "#endif\n");
1020 fprintf(header, "\n");
1021 fprintf(header, "#endif\n");
1022 fprintf(header, "\n");
1023 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
1026 void write_coclass(type_t *cocl)
1028 fprintf(header, "/*****************************************************************************\n");
1029 fprintf(header, " * %s coclass\n", cocl->name);
1030 fprintf(header, " */\n\n");
1031 write_coclass_guid(cocl);
1032 fprintf(header, "\n");
1035 void write_coclass_forward(type_t *cocl)
1037 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1038 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1039 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1040 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );