winhttp: The last parameter of WinHttpWriteData is optional.
[wine/multimedia.git] / tools / widl / header.c
blob0a2a98a8a27b1d87d337dde1ffea6f6343ad33de
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 const char *name = v->name;
154 if (name == NULL) {
155 switch (v->type->type) {
156 case RPC_FC_STRUCT:
157 case RPC_FC_CVSTRUCT:
158 case RPC_FC_CPSTRUCT:
159 case RPC_FC_CSTRUCT:
160 case RPC_FC_PSTRUCT:
161 case RPC_FC_BOGUS_STRUCT:
162 case RPC_FC_ENCAPSULATED_UNION:
163 name = "DUMMYSTRUCTNAME";
164 break;
165 case RPC_FC_NON_ENCAPSULATED_UNION:
166 name = "DUMMYUNIONNAME";
167 break;
168 default:
169 /* ? */
170 break;
173 indent(h, 0);
174 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
175 fprintf(h, ";\n");
179 static void write_fields(FILE *h, var_list_t *fields)
181 var_t *v;
182 if (!fields) return;
183 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
186 static void write_enums(FILE *h, var_list_t *enums)
188 var_t *v;
189 if (!enums) return;
190 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
192 if (v->name) {
193 indent(h, 0);
194 fprintf(h, "%s", get_name(v));
195 if (v->eval) {
196 fprintf(h, " = ");
197 write_expr(h, v->eval, 0, 1, NULL, NULL);
200 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
202 fprintf(h, "\n");
205 int needs_space_after(type_t *t)
207 return (t->kind == TKIND_ALIAS
208 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
211 void write_type_left(FILE *h, type_t *t, int declonly)
213 if (!h) return;
215 if (is_attr(t->attrs, ATTR_CONST) &&
216 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
217 fprintf(h, "const ");
219 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
220 else if (t->declarray) write_type_left(h, t->ref, declonly);
221 else {
222 if (t->sign > 0) fprintf(h, "signed ");
223 else if (t->sign < 0) fprintf(h, "unsigned ");
224 switch (t->type) {
225 case RPC_FC_ENUM16:
226 case RPC_FC_ENUM32:
227 if (!declonly && t->defined && !t->written && !t->ignore) {
228 if (t->name) fprintf(h, "enum %s {\n", t->name);
229 else fprintf(h, "enum {\n");
230 t->written = TRUE;
231 indentation++;
232 write_enums(h, t->fields_or_args);
233 indent(h, -1);
234 fprintf(h, "}");
236 else fprintf(h, "enum %s", t->name ? t->name : "");
237 break;
238 case RPC_FC_STRUCT:
239 case RPC_FC_CVSTRUCT:
240 case RPC_FC_CPSTRUCT:
241 case RPC_FC_CSTRUCT:
242 case RPC_FC_PSTRUCT:
243 case RPC_FC_BOGUS_STRUCT:
244 case RPC_FC_ENCAPSULATED_UNION:
245 if (!declonly && t->defined && !t->written && !t->ignore) {
246 if (t->name) fprintf(h, "struct %s {\n", t->name);
247 else fprintf(h, "struct {\n");
248 t->written = TRUE;
249 indentation++;
250 write_fields(h, t->fields_or_args);
251 indent(h, -1);
252 fprintf(h, "}");
254 else fprintf(h, "struct %s", t->name ? t->name : "");
255 break;
256 case RPC_FC_NON_ENCAPSULATED_UNION:
257 if (!declonly && t->defined && !t->written && !t->ignore) {
258 if (t->name) fprintf(h, "union %s {\n", t->name);
259 else fprintf(h, "union {\n");
260 t->written = TRUE;
261 indentation++;
262 write_fields(h, t->fields_or_args);
263 indent(h, -1);
264 fprintf(h, "}");
266 else fprintf(h, "union %s", t->name ? t->name : "");
267 break;
268 case RPC_FC_RP:
269 case RPC_FC_UP:
270 case RPC_FC_FP:
271 case RPC_FC_OP:
272 case RPC_FC_CARRAY:
273 case RPC_FC_CVARRAY:
274 case RPC_FC_BOGUS_ARRAY:
275 write_type_left(h, t->ref, declonly);
276 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
277 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
278 break;
279 default:
280 fprintf(h, "%s", t->name);
285 void write_type_right(FILE *h, type_t *t, int is_field)
287 if (!h) return;
289 if (t->declarray) {
290 if (is_conformant_array(t)) {
291 fprintf(h, "[%s]", is_field ? "1" : "");
292 t = t->ref;
294 for ( ; t->declarray; t = t->ref)
295 fprintf(h, "[%lu]", t->dim);
299 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
300 const char *fmt, va_list args)
302 type_t *pt;
303 int ptr_level = 0;
305 if (!h) return;
307 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
310 if (pt->type == RPC_FC_FUNCTION) {
311 int i;
312 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
313 if (!callconv) callconv = "";
314 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
315 write_type_left(h, pt->ref, declonly);
316 fputc(' ', h);
317 if (ptr_level) fputc('(', h);
318 fprintf(h, "%s ", callconv);
319 for (i = 0; i < ptr_level; i++)
320 fputc('*', h);
321 } else
322 write_type_left(h, t, declonly);
323 if (fmt) {
324 if (needs_space_after(t))
325 fputc(' ', h);
326 vfprintf(h, fmt, args);
328 if (pt->type == RPC_FC_FUNCTION) {
329 if (ptr_level) fputc(')', h);
330 fputc('(', h);
331 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
332 fputc(')', h);
333 } else
334 write_type_right(h, t, is_field);
337 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
339 va_list args;
340 va_start(args, fmt);
341 write_type_v(f, t, field, FALSE, fmt, args);
342 va_end(args);
345 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
347 va_list args;
348 va_start(args, fmt);
349 write_type_v(f, t, FALSE, TRUE, fmt, args);
350 va_end(args);
353 void write_type_decl_left(FILE *f, type_t *t)
355 write_type_left(f, t, TRUE);
358 static int user_type_registered(const char *name)
360 user_type_t *ut;
361 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
362 if (!strcmp(name, ut->name))
363 return 1;
364 return 0;
367 static int context_handle_registered(const char *name)
369 context_handle_t *ch;
370 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
371 if (!strcmp(name, ch->name))
372 return 1;
373 return 0;
376 static int generic_handle_registered(const char *name)
378 generic_handle_t *gh;
379 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
380 if (!strcmp(name, gh->name))
381 return 1;
382 return 0;
385 /* check for types which require additional prototypes to be generated in the
386 * header */
387 void check_for_additional_prototype_types(const var_list_t *list)
389 const var_t *v;
391 if (!list) return;
392 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
394 type_t *type;
395 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
396 const char *name = type->name;
397 if (type->user_types_registered) continue;
398 type->user_types_registered = 1;
399 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
400 if (!context_handle_registered(name))
402 context_handle_t *ch = xmalloc(sizeof(*ch));
403 ch->name = xstrdup(name);
404 list_add_tail(&context_handle_list, &ch->entry);
406 /* don't carry on parsing fields within this type */
407 break;
409 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
410 if (!generic_handle_registered(name))
412 generic_handle_t *gh = xmalloc(sizeof(*gh));
413 gh->name = xstrdup(name);
414 list_add_tail(&generic_handle_list, &gh->entry);
416 /* don't carry on parsing fields within this type */
417 break;
419 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
420 if (!user_type_registered(name))
422 user_type_t *ut = xmalloc(sizeof *ut);
423 ut->name = xstrdup(name);
424 list_add_tail(&user_type_list, &ut->entry);
426 /* don't carry on parsing fields within this type as we are already
427 * using a wire marshaled type */
428 break;
430 else
432 check_for_additional_prototype_types(type->fields_or_args);
438 void write_user_types(void)
440 user_type_t *ut;
441 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
443 const char *name = ut->name;
444 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
445 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
446 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
447 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
451 void write_context_handle_rundowns(void)
453 context_handle_t *ch;
454 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
456 const char *name = ch->name;
457 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
461 void write_generic_handle_routines(void)
463 generic_handle_t *gh;
464 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
466 const char *name = gh->name;
467 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
468 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
472 void write_typedef(type_t *type)
474 fprintf(header, "typedef ");
475 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
476 fprintf(header, ";\n");
479 int is_const_decl(const var_t *var)
481 const type_t *t;
482 /* strangely, MIDL accepts a const attribute on any pointer in the
483 * declaration to mean that data isn't being instantiated. this appears
484 * to be a bug, but there is no benefit to being incompatible with MIDL,
485 * so we'll do the same thing */
486 for (t = var->type; ; )
488 if (is_attr(t->attrs, ATTR_CONST))
489 return TRUE;
490 else if (is_ptr(t))
491 t = t->ref;
492 else break;
494 return FALSE;
497 void write_declaration(const var_t *v, int is_in_interface)
499 if (is_const_decl(v) && v->eval)
501 fprintf(header, "#define %s (", v->name);
502 write_expr(header, v->eval, 0, 1, NULL, NULL);
503 fprintf(header, ")\n\n");
505 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
507 switch (v->stgclass)
509 case STG_NONE:
510 case STG_REGISTER: /* ignored */
511 break;
512 case STG_STATIC:
513 fprintf(header, "static ");
514 break;
515 case STG_EXTERN:
516 fprintf(header, "extern ");
517 break;
519 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
520 fprintf(header, ";\n\n");
524 void write_library(const typelib_t *typelib)
526 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
527 fprintf(header, "\n");
528 write_guid(header, "LIBID", typelib->name, uuid);
529 fprintf(header, "\n");
533 const var_t* get_explicit_handle_var(const func_t* func)
535 const var_t* var;
537 if (!func->args)
538 return NULL;
540 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
541 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
542 return var;
544 return NULL;
547 const type_t* get_explicit_generic_handle_type(const var_t* var)
549 const type_t *t;
550 for (t = var->type; is_ptr(t); t = t->ref)
551 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
552 return t;
553 return NULL;
556 const var_t* get_explicit_generic_handle_var(const func_t* func)
558 const var_t* var;
560 if (!func->args)
561 return NULL;
563 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
564 if (get_explicit_generic_handle_type(var))
565 return var;
567 return NULL;
570 const var_t* get_context_handle_var(const func_t* func)
572 const var_t* var;
574 if (!func->args)
575 return NULL;
577 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
578 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
579 return var;
581 return NULL;
584 int has_out_arg_or_return(const func_t *func)
586 const var_t *var;
588 if (!is_void(get_func_return_type(func)))
589 return 1;
591 if (!func->args)
592 return 0;
594 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
595 if (is_attr(var->attrs, ATTR_OUT))
596 return 1;
598 return 0;
602 /********** INTERFACES **********/
604 int is_object(const attr_list_t *list)
606 const attr_t *attr;
607 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
608 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
609 return 0;
612 int is_local(const attr_list_t *a)
614 return is_attr(a, ATTR_LOCAL);
617 const var_t *is_callas(const attr_list_t *a)
619 return get_attrp(a, ATTR_CALLAS);
622 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
624 const func_t *cur;
626 if (iface->ref) write_method_macro(header, iface->ref, name);
628 if (!iface->funcs) return;
630 fprintf(header, "/*** %s methods ***/\n", iface->name);
631 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
633 var_t *def = cur->def;
634 if (!is_callas(def->attrs)) {
635 const var_t *arg;
637 fprintf(header, "#define %s_%s(This", name, get_name(def));
638 if (cur->args)
639 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
640 fprintf(header, ",%s", arg->name);
641 fprintf(header, ") ");
643 fprintf(header, "(This)->lpVtbl->%s(This", get_name(def));
644 if (cur->args)
645 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
646 fprintf(header, ",%s", arg->name);
647 fprintf(header, ")\n");
652 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
654 const var_t *arg;
655 int count = 0;
657 if (do_indent)
659 indentation++;
660 indent(h, 0);
662 if (method == 1) {
663 fprintf(h, "%s* This", name);
664 count++;
666 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
667 if (count) {
668 if (do_indent)
670 fprintf(h, ",\n");
671 indent(h, 0);
673 else fprintf(h, ",");
675 write_type_decl(h, arg->type, "%s", arg->name);
676 count++;
678 if (do_indent) indentation--;
681 static void write_cpp_method_def(FILE *header, const type_t *iface)
683 const func_t *cur;
685 if (!iface->funcs) return;
687 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
689 var_t *def = cur->def;
690 if (!is_callas(def->attrs)) {
691 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
692 if (!callconv) callconv = "";
693 indent(header, 0);
694 fprintf(header, "virtual ");
695 write_type_decl_left(header, get_func_return_type(cur));
696 fprintf(header, " %s %s(\n", callconv, get_name(def));
697 write_args(header, cur->args, iface->name, 2, TRUE);
698 fprintf(header, ") = 0;\n");
699 fprintf(header, "\n");
704 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
706 const func_t *cur;
708 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
710 if (!iface->funcs) return;
711 indent(header, 0);
712 fprintf(header, "/*** %s methods ***/\n", iface->name);
713 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
715 const var_t *def = cur->def;
716 if (!is_callas(def->attrs)) {
717 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
718 if (!callconv) callconv = "";
719 indent(header, 0);
720 write_type_decl_left(header, get_func_return_type(cur));
721 fprintf(header, " (%s *%s)(\n", callconv, get_name(def));
722 write_args(header, cur->args, name, 1, TRUE);
723 fprintf(header, ");\n");
724 fprintf(header, "\n");
729 static void write_c_method_def(FILE *header, const type_t *iface)
731 do_write_c_method_def(header, iface, iface->name);
734 static void write_c_disp_method_def(FILE *header, const type_t *iface)
736 do_write_c_method_def(header, iface->ref, iface->name);
739 static void write_method_proto(FILE *header, const type_t *iface)
741 const func_t *cur;
743 if (!iface->funcs) return;
744 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
746 const var_t *def = cur->def;
748 if (!is_local(def->attrs)) {
749 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
750 if (!callconv) callconv = "";
751 /* proxy prototype */
752 write_type_decl_left(header, get_func_return_type(cur));
753 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
754 write_args(header, cur->args, iface->name, 1, TRUE);
755 fprintf(header, ");\n");
756 /* stub prototype */
757 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
758 fprintf(header, " IRpcStubBuffer* This,\n");
759 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
760 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
761 fprintf(header, " DWORD* pdwStubPhase);\n");
766 void write_locals(FILE *fp, const type_t *iface, int body)
768 static const char comment[]
769 = "/* WIDL-generated stub. You must provide an implementation for this. */";
770 const func_list_t *funcs = iface->funcs;
771 const func_t *cur;
773 if (!is_object(iface->attrs) || !funcs)
774 return;
776 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
777 const var_t *def = cur->def;
778 const var_t *cas = is_callas(def->attrs);
780 if (cas) {
781 const func_t *m;
782 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
783 if (!strcmp(m->def->name, cas->name))
784 break;
785 if (&m->entry != iface->funcs) {
786 const var_t *mdef = m->def;
787 /* proxy prototype - use local prototype */
788 write_type_decl_left(fp, get_func_return_type(m));
789 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(mdef));
790 write_args(fp, m->args, iface->name, 1, TRUE);
791 fprintf(fp, ")");
792 if (body) {
793 type_t *rt = get_func_return_type(m);
794 fprintf(fp, "\n{\n");
795 fprintf(fp, " %s\n", comment);
796 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
797 fprintf(fp, " return E_NOTIMPL;\n");
798 else if (rt->type) {
799 fprintf(fp, " ");
800 write_type_decl(fp, rt, "rv");
801 fprintf(fp, ";\n");
802 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
803 fprintf(fp, " return rv;\n");
805 fprintf(fp, "}\n\n");
807 else
808 fprintf(fp, ";\n");
809 /* stub prototype - use remotable prototype */
810 write_type_decl_left(fp, get_func_return_type(cur));
811 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(mdef));
812 write_args(fp, cur->args, iface->name, 1, TRUE);
813 fprintf(fp, ")");
814 if (body)
815 /* Remotable methods must all return HRESULTs. */
816 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
817 else
818 fprintf(fp, ";\n");
820 else
821 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
826 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
828 var_t *def = fun->def;
829 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
831 /* FIXME: do we need to handle call_as? */
832 write_type_decl_left(header, get_func_return_type(fun));
833 fprintf(header, " ");
834 if (callconv) fprintf(header, "%s ", callconv);
835 fprintf(header, "%s%s(\n", prefix, get_name(def));
836 if (fun->args)
837 write_args(header, fun->args, iface->name, 0, TRUE);
838 else
839 fprintf(header, " void");
840 fprintf(header, ");\n\n");
843 static void write_function_protos(FILE *header, const type_t *iface)
845 const func_t *cur;
846 int prefixes_differ = strcmp(prefix_client, prefix_server);
848 if (!iface->funcs) return;
849 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
851 if (prefixes_differ) {
852 fprintf(header, "/* client prototype */\n");
853 write_function_proto(header, iface, cur, prefix_client);
854 fprintf(header, "/* server prototype */\n");
856 write_function_proto(header, iface, cur, prefix_server);
860 void write_forward(type_t *iface)
862 /* C/C++ forwards should only be written for object interfaces, so if we
863 * have a full definition we only write one if we find [object] among the
864 * attributes - however, if we don't have a full definition at this point
865 * (i.e. this is an IDL forward), then we also assume that it is an object
866 * interface, since non-object interfaces shouldn't need forwards */
867 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
868 && !iface->written) {
869 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
870 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
871 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
872 fprintf(header, "#endif\n\n" );
873 iface->written = TRUE;
877 static void write_iface_guid(FILE *header, const type_t *iface)
879 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
880 write_guid(header, "IID", iface->name, uuid);
883 static void write_dispiface_guid(FILE *header, const type_t *iface)
885 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
886 write_guid(header, "DIID", iface->name, uuid);
889 static void write_coclass_guid(FILE *header, const type_t *cocl)
891 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
892 write_guid(header, "CLSID", cocl->name, uuid);
895 static void write_com_interface_start(FILE *header, const type_t *iface)
897 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
898 fprintf(header, "/*****************************************************************************\n");
899 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
900 fprintf(header, " */\n");
901 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
902 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
905 static void write_com_interface_end(FILE *header, type_t *iface)
907 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
908 if (dispinterface)
909 write_dispiface_guid(header, iface);
910 else
911 write_iface_guid(header, iface);
912 /* C++ interface */
913 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
914 if (iface->ref)
916 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
917 fprintf(header, "{\n");
919 else
921 fprintf(header, "interface %s\n", iface->name);
922 fprintf(header, "{\n");
923 fprintf(header, " BEGIN_INTERFACE\n");
924 fprintf(header, "\n");
926 /* dispinterfaces don't have real functions, so don't write C++ functions for
927 * them */
928 if (!dispinterface)
930 indentation++;
931 write_cpp_method_def(header, iface);
932 indentation--;
934 if (!iface->ref)
935 fprintf(header, " END_INTERFACE\n");
936 fprintf(header, "};\n");
937 fprintf(header, "#else\n");
938 /* C interface */
939 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
940 indentation++;
941 fprintf(header, " BEGIN_INTERFACE\n");
942 fprintf(header, "\n");
943 if (dispinterface)
944 write_c_disp_method_def(header, iface);
945 else
946 write_c_method_def(header, iface);
947 indentation--;
948 fprintf(header, " END_INTERFACE\n");
949 fprintf(header, "} %sVtbl;\n", iface->name);
950 fprintf(header, "interface %s {\n", iface->name);
951 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
952 fprintf(header, "};\n");
953 fprintf(header, "\n");
954 fprintf(header, "#ifdef COBJMACROS\n");
955 /* dispinterfaces don't have real functions, so don't write macros for them,
956 * only for the interface this interface inherits from, i.e. IDispatch */
957 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
958 fprintf(header, "#endif\n");
959 fprintf(header, "\n");
960 fprintf(header, "#endif\n");
961 fprintf(header, "\n");
962 /* dispinterfaces don't have real functions, so don't write prototypes for
963 * them */
964 if (!dispinterface)
966 write_method_proto(header, iface);
967 write_locals(header, iface, FALSE);
968 fprintf(header, "\n");
970 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
973 static void write_rpc_interface_start(FILE *header, const type_t *iface)
975 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
976 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
977 static int allocate_written = 0;
979 if (!allocate_written)
981 allocate_written = 1;
982 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
983 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
986 fprintf(header, "/*****************************************************************************\n");
987 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
988 fprintf(header, " */\n");
989 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
990 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
991 if (var) fprintf(header, "extern handle_t %s;\n", var);
992 if (old_names)
994 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
995 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
997 else
999 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1000 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1001 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1002 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1006 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1008 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1011 void write_interface(type_t *iface)
1013 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1015 write_com_interface_start(header, iface);
1016 write_com_interface_end(header, iface);
1018 else
1020 write_rpc_interface_start(header, iface);
1021 write_function_protos(header, iface);
1022 write_rpc_interface_end(header, iface);
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(header, 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 );
1043 void write_import(const char *fname)
1045 char *hname, *p;
1047 hname = dup_basename(fname, ".idl");
1048 p = hname + strlen(hname) - 2;
1049 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1051 fprintf(header, "#include <%s>\n", hname);
1052 free(hname);