mountmgr: Return the drive pointer from get_dosdevices_path().
[wine/multimedia.git] / tools / widl / header.c
blob83fef7fcbdc54a51c0b9e0e04a466f110338a5d2
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 const char *get_name(const var_t *v)
135 static char buffer[256];
137 if (is_attr( v->attrs, ATTR_PROPGET ))
138 strcpy( buffer, "get_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUT ))
140 strcpy( buffer, "put_" );
141 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
142 strcpy( buffer, "putref_" );
143 else
144 buffer[0] = 0;
145 strcat( buffer, v->name );
146 return buffer;
149 static void write_field(FILE *h, var_t *v)
151 if (!v) return;
152 if (v->type) {
153 indent(h, 0);
154 write_type_def_or_decl(h, v->type, TRUE, "%s", v->name);
155 fprintf(h, ";\n");
159 static void write_fields(FILE *h, var_list_t *fields)
161 var_t *v;
162 if (!fields) return;
163 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
166 static void write_enums(FILE *h, var_list_t *enums)
168 var_t *v;
169 if (!enums) return;
170 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
172 if (v->name) {
173 indent(h, 0);
174 fprintf(h, "%s", get_name(v));
175 if (v->eval) {
176 fprintf(h, " = ");
177 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
180 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
182 fprintf(h, "\n");
185 int needs_space_after(type_t *t)
187 return (t->kind == TKIND_ALIAS
188 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
191 void write_type_left(FILE *h, type_t *t, int declonly)
193 if (!h) return;
195 if (is_attr(t->attrs, ATTR_CONST) &&
196 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
197 fprintf(h, "const ");
199 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
200 else if (t->declarray) write_type_left(h, t->ref, declonly);
201 else {
202 if (t->sign > 0) fprintf(h, "signed ");
203 else if (t->sign < 0) fprintf(h, "unsigned ");
204 switch (t->type) {
205 case RPC_FC_ENUM16:
206 case RPC_FC_ENUM32:
207 if (!declonly && t->defined && !t->written && !t->ignore) {
208 if (t->name) fprintf(h, "enum %s {\n", t->name);
209 else fprintf(h, "enum {\n");
210 t->written = TRUE;
211 indentation++;
212 write_enums(h, t->fields_or_args);
213 indent(h, -1);
214 fprintf(h, "}");
216 else fprintf(h, "enum %s", t->name ? t->name : "");
217 break;
218 case RPC_FC_STRUCT:
219 case RPC_FC_CVSTRUCT:
220 case RPC_FC_CPSTRUCT:
221 case RPC_FC_CSTRUCT:
222 case RPC_FC_PSTRUCT:
223 case RPC_FC_BOGUS_STRUCT:
224 case RPC_FC_ENCAPSULATED_UNION:
225 if (!declonly && t->defined && !t->written && !t->ignore) {
226 if (t->name) fprintf(h, "struct %s {\n", t->name);
227 else fprintf(h, "struct {\n");
228 t->written = TRUE;
229 indentation++;
230 write_fields(h, t->fields_or_args);
231 indent(h, -1);
232 fprintf(h, "}");
234 else fprintf(h, "struct %s", t->name ? t->name : "");
235 break;
236 case RPC_FC_NON_ENCAPSULATED_UNION:
237 if (!declonly && t->defined && !t->written && !t->ignore) {
238 if (t->name) fprintf(h, "union %s {\n", t->name);
239 else fprintf(h, "union {\n");
240 t->written = TRUE;
241 indentation++;
242 write_fields(h, t->fields_or_args);
243 indent(h, -1);
244 fprintf(h, "}");
246 else fprintf(h, "union %s", t->name ? t->name : "");
247 break;
248 case RPC_FC_RP:
249 case RPC_FC_UP:
250 case RPC_FC_FP:
251 case RPC_FC_OP:
252 case RPC_FC_CARRAY:
253 case RPC_FC_CVARRAY:
254 case RPC_FC_BOGUS_ARRAY:
255 write_type_left(h, t->ref, declonly);
256 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
257 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
258 break;
259 default:
260 fprintf(h, "%s", t->name);
265 void write_type_right(FILE *h, type_t *t, int is_field)
267 if (!h) return;
269 if (t->declarray) {
270 if (is_conformant_array(t)) {
271 fprintf(h, "[%s]", is_field ? "1" : "");
272 t = t->ref;
274 for ( ; t->declarray; t = t->ref)
275 fprintf(h, "[%lu]", t->dim);
279 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
280 const char *fmt, va_list args)
282 type_t *pt;
283 int ptr_level = 0;
285 if (!h) return;
287 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
290 if (pt->type == RPC_FC_FUNCTION) {
291 int i;
292 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
293 if (!callconv) callconv = "";
294 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
295 write_type_left(h, pt->ref, declonly);
296 fputc(' ', h);
297 if (ptr_level) fputc('(', h);
298 fprintf(h, "%s ", callconv);
299 for (i = 0; i < ptr_level; i++)
300 fputc('*', h);
301 } else
302 write_type_left(h, t, declonly);
303 if (fmt) {
304 if (needs_space_after(t))
305 fputc(' ', h);
306 vfprintf(h, fmt, args);
308 if (pt->type == RPC_FC_FUNCTION) {
309 if (ptr_level) fputc(')', h);
310 fputc('(', h);
311 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
312 fputc(')', h);
313 } else
314 write_type_right(h, t, is_field);
317 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
319 va_list args;
320 va_start(args, fmt);
321 write_type_v(f, t, field, FALSE, fmt, args);
322 va_end(args);
325 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
327 va_list args;
328 va_start(args, fmt);
329 write_type_v(f, t, FALSE, TRUE, fmt, args);
330 va_end(args);
333 void write_type_decl_left(FILE *f, type_t *t)
335 write_type_left(f, t, TRUE);
338 static int user_type_registered(const char *name)
340 user_type_t *ut;
341 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
342 if (!strcmp(name, ut->name))
343 return 1;
344 return 0;
347 static int context_handle_registered(const char *name)
349 context_handle_t *ch;
350 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
351 if (!strcmp(name, ch->name))
352 return 1;
353 return 0;
356 static int generic_handle_registered(const char *name)
358 generic_handle_t *gh;
359 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
360 if (!strcmp(name, gh->name))
361 return 1;
362 return 0;
365 /* check for types which require additional prototypes to be generated in the
366 * header */
367 void check_for_additional_prototype_types(const var_list_t *list)
369 const var_t *v;
371 if (!list) return;
372 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
374 type_t *type;
375 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
376 const char *name = type->name;
377 if (type->user_types_registered) continue;
378 type->user_types_registered = 1;
379 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
380 if (!context_handle_registered(name))
382 context_handle_t *ch = xmalloc(sizeof(*ch));
383 ch->name = xstrdup(name);
384 list_add_tail(&context_handle_list, &ch->entry);
386 /* don't carry on parsing fields within this type */
387 break;
389 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
390 if (!generic_handle_registered(name))
392 generic_handle_t *gh = xmalloc(sizeof(*gh));
393 gh->name = xstrdup(name);
394 list_add_tail(&generic_handle_list, &gh->entry);
396 /* don't carry on parsing fields within this type */
397 break;
399 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
400 if (!user_type_registered(name))
402 user_type_t *ut = xmalloc(sizeof *ut);
403 ut->name = xstrdup(name);
404 list_add_tail(&user_type_list, &ut->entry);
406 /* don't carry on parsing fields within this type as we are already
407 * using a wire marshaled type */
408 break;
410 else
412 check_for_additional_prototype_types(type->fields_or_args);
418 void write_user_types(void)
420 user_type_t *ut;
421 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
423 const char *name = ut->name;
424 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
425 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
426 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
427 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
431 void write_context_handle_rundowns(void)
433 context_handle_t *ch;
434 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
436 const char *name = ch->name;
437 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
441 void write_generic_handle_routines(void)
443 generic_handle_t *gh;
444 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
446 const char *name = gh->name;
447 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
448 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
452 void write_typedef(type_t *type)
454 fprintf(header, "typedef ");
455 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
456 fprintf(header, ";\n");
459 int is_const_decl(const var_t *var)
461 const type_t *t;
462 /* strangely, MIDL accepts a const attribute on any pointer in the
463 * declaration to mean that data isn't being instantiated. this appears
464 * to be a bug, but there is no benefit to being incompatible with MIDL,
465 * so we'll do the same thing */
466 for (t = var->type; ; )
468 if (is_attr(t->attrs, ATTR_CONST))
469 return TRUE;
470 else if (is_ptr(t))
471 t = t->ref;
472 else break;
474 return FALSE;
477 void write_declaration(const var_t *v, int is_in_interface)
479 if (is_const_decl(v) && v->eval)
481 fprintf(header, "#define %s (", v->name);
482 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
483 fprintf(header, ")\n\n");
485 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
487 switch (v->stgclass)
489 case STG_NONE:
490 case STG_REGISTER: /* ignored */
491 break;
492 case STG_STATIC:
493 fprintf(header, "static ");
494 break;
495 case STG_EXTERN:
496 fprintf(header, "extern ");
497 break;
499 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
500 fprintf(header, ";\n\n");
504 void write_library(const typelib_t *typelib)
506 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
507 fprintf(header, "\n");
508 write_guid(header, "LIBID", typelib->name, uuid);
509 fprintf(header, "\n");
513 const var_t* get_explicit_handle_var(const func_t* func)
515 const var_t* var;
517 if (!func->args)
518 return NULL;
520 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
521 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
522 return var;
524 return NULL;
527 const type_t* get_explicit_generic_handle_type(const var_t* var)
529 const type_t *t;
530 for (t = var->type; is_ptr(t); t = t->ref)
531 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
532 return t;
533 return NULL;
536 const var_t* get_explicit_generic_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 (get_explicit_generic_handle_type(var))
545 return var;
547 return NULL;
550 const var_t* get_context_handle_var(const func_t* func)
552 const var_t* var;
554 if (!func->args)
555 return NULL;
557 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
558 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
559 return var;
561 return NULL;
564 int has_out_arg_or_return(const func_t *func)
566 const var_t *var;
568 if (!is_void(get_func_return_type(func)))
569 return 1;
571 if (!func->args)
572 return 0;
574 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
575 if (is_attr(var->attrs, ATTR_OUT))
576 return 1;
578 return 0;
582 /********** INTERFACES **********/
584 int is_object(const attr_list_t *list)
586 const attr_t *attr;
587 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
588 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
589 return 0;
592 int is_local(const attr_list_t *a)
594 return is_attr(a, ATTR_LOCAL);
597 const var_t *is_callas(const attr_list_t *a)
599 return get_attrp(a, ATTR_CALLAS);
602 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
604 const func_t *cur;
606 if (iface->ref) write_method_macro(header, iface->ref, name);
608 if (!iface->funcs) return;
610 fprintf(header, "/*** %s methods ***/\n", iface->name);
611 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
613 var_t *def = cur->def;
614 if (!is_callas(def->attrs)) {
615 const var_t *arg;
617 fprintf(header, "#define %s_%s(This", name, get_name(def));
618 if (cur->args)
619 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
620 fprintf(header, ",%s", arg->name);
621 fprintf(header, ") ");
623 fprintf(header, "(This)->lpVtbl->%s(This", get_name(def));
624 if (cur->args)
625 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
626 fprintf(header, ",%s", arg->name);
627 fprintf(header, ")\n");
632 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
634 const var_t *arg;
635 int count = 0;
637 if (do_indent)
639 indentation++;
640 indent(h, 0);
642 if (method == 1) {
643 fprintf(h, "%s* This", name);
644 count++;
646 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
647 if (count) {
648 if (do_indent)
650 fprintf(h, ",\n");
651 indent(h, 0);
653 else fprintf(h, ",");
655 write_type_decl(h, arg->type, "%s", arg->name);
656 count++;
658 if (do_indent) indentation--;
661 static void write_cpp_method_def(FILE *header, const type_t *iface)
663 const func_t *cur;
665 if (!iface->funcs) return;
667 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
669 var_t *def = cur->def;
670 if (!is_callas(def->attrs)) {
671 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
672 if (!callconv) callconv = "";
673 indent(header, 0);
674 fprintf(header, "virtual ");
675 write_type_decl_left(header, get_func_return_type(cur));
676 fprintf(header, " %s %s(\n", callconv, get_name(def));
677 write_args(header, cur->args, iface->name, 2, TRUE);
678 fprintf(header, ") = 0;\n");
679 fprintf(header, "\n");
684 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
686 const func_t *cur;
688 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
690 if (!iface->funcs) return;
691 indent(header, 0);
692 fprintf(header, "/*** %s methods ***/\n", iface->name);
693 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
695 const var_t *def = cur->def;
696 if (!is_callas(def->attrs)) {
697 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
698 if (!callconv) callconv = "";
699 indent(header, 0);
700 write_type_decl_left(header, get_func_return_type(cur));
701 fprintf(header, " (%s *%s)(\n", callconv, get_name(def));
702 write_args(header, cur->args, name, 1, TRUE);
703 fprintf(header, ");\n");
704 fprintf(header, "\n");
709 static void write_c_method_def(FILE *header, const type_t *iface)
711 do_write_c_method_def(header, iface, iface->name);
714 static void write_c_disp_method_def(FILE *header, const type_t *iface)
716 do_write_c_method_def(header, iface->ref, iface->name);
719 static void write_method_proto(FILE *header, const type_t *iface)
721 const func_t *cur;
723 if (!iface->funcs) return;
724 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
726 const var_t *def = cur->def;
728 if (!is_local(def->attrs)) {
729 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
730 if (!callconv) callconv = "";
731 /* proxy prototype */
732 write_type_decl_left(header, get_func_return_type(cur));
733 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
734 write_args(header, cur->args, iface->name, 1, TRUE);
735 fprintf(header, ");\n");
736 /* stub prototype */
737 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
738 fprintf(header, " IRpcStubBuffer* This,\n");
739 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
740 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
741 fprintf(header, " DWORD* pdwStubPhase);\n");
746 void write_locals(FILE *fp, const type_t *iface, int body)
748 static const char comment[]
749 = "/* WIDL-generated stub. You must provide an implementation for this. */";
750 const func_list_t *funcs = iface->funcs;
751 const func_t *cur;
753 if (!is_object(iface->attrs) || !funcs)
754 return;
756 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
757 const var_t *def = cur->def;
758 const var_t *cas = is_callas(def->attrs);
760 if (cas) {
761 const func_t *m;
762 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
763 if (!strcmp(m->def->name, cas->name))
764 break;
765 if (&m->entry != iface->funcs) {
766 const var_t *mdef = m->def;
767 /* proxy prototype - use local prototype */
768 write_type_decl_left(fp, get_func_return_type(m));
769 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(mdef));
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_%s_Stub(\n", iface->name, get_name(mdef));
792 write_args(fp, cur->args, iface->name, 1, TRUE);
793 fprintf(fp, ")");
794 if (body)
795 /* Remotable methods must all return HRESULTs. */
796 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
797 else
798 fprintf(fp, ";\n");
800 else
801 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
806 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
808 var_t *def = fun->def;
809 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
811 /* FIXME: do we need to handle call_as? */
812 write_type_decl_left(header, get_func_return_type(fun));
813 fprintf(header, " ");
814 if (callconv) fprintf(header, "%s ", callconv);
815 fprintf(header, "%s%s(\n", prefix, get_name(def));
816 if (fun->args)
817 write_args(header, fun->args, iface->name, 0, TRUE);
818 else
819 fprintf(header, " void");
820 fprintf(header, ");\n\n");
823 static void write_function_protos(FILE *header, const type_t *iface)
825 const func_t *cur;
826 int prefixes_differ = strcmp(prefix_client, prefix_server);
828 if (!iface->funcs) return;
829 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
831 if (prefixes_differ) {
832 fprintf(header, "/* client prototype */\n");
833 write_function_proto(header, iface, cur, prefix_client);
834 fprintf(header, "/* server prototype */\n");
836 write_function_proto(header, iface, cur, prefix_server);
840 void write_forward(type_t *iface)
842 /* C/C++ forwards should only be written for object interfaces, so if we
843 * have a full definition we only write one if we find [object] among the
844 * attributes - however, if we don't have a full definition at this point
845 * (i.e. this is an IDL forward), then we also assume that it is an object
846 * interface, since non-object interfaces shouldn't need forwards */
847 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
848 && !iface->written) {
849 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
850 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
851 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
852 fprintf(header, "#endif\n\n" );
853 iface->written = TRUE;
857 static void write_iface_guid(FILE *header, const type_t *iface)
859 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
860 write_guid(header, "IID", iface->name, uuid);
863 static void write_dispiface_guid(FILE *header, const type_t *iface)
865 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
866 write_guid(header, "DIID", iface->name, uuid);
869 static void write_coclass_guid(FILE *header, const type_t *cocl)
871 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
872 write_guid(header, "CLSID", cocl->name, uuid);
875 static void write_com_interface_start(FILE *header, const type_t *iface)
877 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
878 fprintf(header, "/*****************************************************************************\n");
879 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
880 fprintf(header, " */\n");
881 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
882 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
885 static void write_com_interface_end(FILE *header, type_t *iface)
887 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
888 if (dispinterface)
889 write_dispiface_guid(header, iface);
890 else
891 write_iface_guid(header, 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");
899 else
901 fprintf(header, "interface %s\n", iface->name);
902 fprintf(header, "{\n");
903 fprintf(header, " BEGIN_INTERFACE\n");
904 fprintf(header, "\n");
906 /* dispinterfaces don't have real functions, so don't write C++ functions for
907 * them */
908 if (!dispinterface)
910 indentation++;
911 write_cpp_method_def(header, iface);
912 indentation--;
914 if (!iface->ref)
915 fprintf(header, " END_INTERFACE\n");
916 fprintf(header, "};\n");
917 fprintf(header, "#else\n");
918 /* C interface */
919 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
920 indentation++;
921 fprintf(header, " BEGIN_INTERFACE\n");
922 fprintf(header, "\n");
923 if (dispinterface)
924 write_c_disp_method_def(header, iface);
925 else
926 write_c_method_def(header, iface);
927 indentation--;
928 fprintf(header, " END_INTERFACE\n");
929 fprintf(header, "} %sVtbl;\n", iface->name);
930 fprintf(header, "interface %s {\n", iface->name);
931 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
932 fprintf(header, "};\n");
933 fprintf(header, "\n");
934 fprintf(header, "#ifdef COBJMACROS\n");
935 /* dispinterfaces don't have real functions, so don't write macros for them,
936 * only for the interface this interface inherits from, i.e. IDispatch */
937 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
938 fprintf(header, "#endif\n");
939 fprintf(header, "\n");
940 fprintf(header, "#endif\n");
941 fprintf(header, "\n");
942 /* dispinterfaces don't have real functions, so don't write prototypes for
943 * them */
944 if (!dispinterface)
946 write_method_proto(header, iface);
947 write_locals(header, iface, FALSE);
948 fprintf(header, "\n");
950 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
953 static void write_rpc_interface_start(FILE *header, const type_t *iface)
955 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
956 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
957 static int allocate_written = 0;
959 if (!allocate_written)
961 allocate_written = 1;
962 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
963 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
966 fprintf(header, "/*****************************************************************************\n");
967 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
968 fprintf(header, " */\n");
969 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
970 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
971 if (var) fprintf(header, "extern handle_t %s;\n", var);
972 if (old_names)
974 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
975 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
977 else
979 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
980 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
981 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
982 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
986 static void write_rpc_interface_end(FILE *header, const type_t *iface)
988 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
991 void write_interface(type_t *iface)
993 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
995 write_com_interface_start(header, iface);
996 write_com_interface_end(header, iface);
998 else
1000 write_rpc_interface_start(header, iface);
1001 write_function_protos(header, iface);
1002 write_rpc_interface_end(header, iface);
1006 void write_coclass(type_t *cocl)
1008 fprintf(header, "/*****************************************************************************\n");
1009 fprintf(header, " * %s coclass\n", cocl->name);
1010 fprintf(header, " */\n\n");
1011 write_coclass_guid(header, cocl);
1012 fprintf(header, "\n");
1015 void write_coclass_forward(type_t *cocl)
1017 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1018 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1019 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1020 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1023 void write_import(const char *fname)
1025 char *hname, *p;
1027 hname = dup_basename(fname, ".idl");
1028 p = hname + strlen(hname) - 2;
1029 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1031 fprintf(header, "#include <%s>\n", hname);
1032 free(hname);