push eb25bf65c4616aa55a810ed5c29198b1a080b208
[wine/hacks.git] / tools / widl / header.c
blob9b6167e1913c64d444d9812d21a1c2851973b1ed
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"
37 #include "typetree.h"
39 typedef struct _user_type_t generic_handle_t;
41 static int indentation = 0;
42 user_type_list_t user_type_list = LIST_INIT(user_type_list);
43 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
44 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
46 static void indent(FILE *h, int delta)
48 int c;
49 if (delta < 0) indentation += delta;
50 for (c=0; c<indentation; c++) fprintf(h, " ");
51 if (delta > 0) indentation += delta;
54 int is_ptrchain_attr(const var_t *var, enum attr_type t)
56 if (is_attr(var->attrs, t))
57 return 1;
58 else
60 type_t *type = var->type;
61 for (;;)
63 if (is_attr(type->attrs, t))
64 return 1;
65 else if (type_is_alias(type))
66 type = type_alias_get_aliasee(type);
67 else if (is_ptr(type))
68 type = type_pointer_get_ref(type);
69 else return 0;
74 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
76 const type_t *t = type;
77 for (;;)
79 if (is_attr(t->attrs, attr))
80 return 1;
81 else if (type_is_alias(t))
82 t = type_alias_get_aliasee(t);
83 else return 0;
87 int is_attr(const attr_list_t *list, enum attr_type t)
89 const attr_t *attr;
90 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
91 if (attr->type == t) return 1;
92 return 0;
95 void *get_attrp(const attr_list_t *list, enum attr_type t)
97 const attr_t *attr;
98 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
99 if (attr->type == t) return attr->u.pval;
100 return NULL;
103 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
105 const attr_t *attr;
106 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
107 if (attr->type == t) return attr->u.ival;
108 return 0;
111 int is_void(const type_t *t)
113 return type_get_type(t) == TYPE_VOID;
116 int is_conformant_array(const type_t *t)
118 return is_array(t) && type_array_has_conformance(t);
121 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
123 if (!uuid) return;
124 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
125 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
126 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
127 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
128 uuid->Data4[6], uuid->Data4[7]);
131 const char *get_name(const var_t *v)
133 static char buffer[256];
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 strcpy( buffer, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 strcpy( buffer, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 strcpy( buffer, "putref_" );
141 else
142 buffer[0] = 0;
143 strcat( buffer, v->name );
144 return buffer;
147 static void write_field(FILE *h, var_t *v)
149 if (!v) return;
150 if (v->type) {
151 indent(h, 0);
152 write_type_def_or_decl(h, v->type, TRUE, v->name);
153 fprintf(h, ";\n");
157 static void write_fields(FILE *h, var_list_t *fields)
159 var_t *v;
160 if (!fields) return;
161 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
164 static void write_enums(FILE *h, var_list_t *enums)
166 var_t *v;
167 if (!enums) return;
168 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
170 if (v->name) {
171 indent(h, 0);
172 fprintf(h, "%s", get_name(v));
173 if (v->eval) {
174 fprintf(h, " = ");
175 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
178 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
180 fprintf(h, "\n");
183 int needs_space_after(type_t *t)
185 return (type_is_alias(t) ||
186 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
189 void write_type_left(FILE *h, type_t *t, int declonly)
191 if (!h) return;
193 if (is_attr(t->attrs, ATTR_CONST) &&
194 (type_is_alias(t) || !is_ptr(t)))
195 fprintf(h, "const ");
197 if (type_is_alias(t)) fprintf(h, "%s", t->name);
198 else {
199 switch (type_get_type_detect_alias(t)) {
200 case TYPE_ENUM:
201 if (!declonly && t->defined && !t->written) {
202 if (t->name) fprintf(h, "enum %s {\n", t->name);
203 else fprintf(h, "enum {\n");
204 t->written = TRUE;
205 indentation++;
206 write_enums(h, type_enum_get_values(t));
207 indent(h, -1);
208 fprintf(h, "}");
210 else fprintf(h, "enum %s", t->name ? t->name : "");
211 break;
212 case TYPE_STRUCT:
213 case TYPE_ENCAPSULATED_UNION:
214 if (!declonly && t->defined && !t->written) {
215 if (t->name) fprintf(h, "struct %s {\n", t->name);
216 else fprintf(h, "struct {\n");
217 t->written = TRUE;
218 indentation++;
219 if (type_get_type(t) != TYPE_STRUCT)
220 write_fields(h, type_encapsulated_union_get_fields(t));
221 else
222 write_fields(h, type_struct_get_fields(t));
223 indent(h, -1);
224 fprintf(h, "}");
226 else fprintf(h, "struct %s", t->name ? t->name : "");
227 break;
228 case TYPE_UNION:
229 if (!declonly && t->defined && !t->written) {
230 if (t->name) fprintf(h, "union %s {\n", t->name);
231 else fprintf(h, "union {\n");
232 t->written = TRUE;
233 indentation++;
234 write_fields(h, type_union_get_cases(t));
235 indent(h, -1);
236 fprintf(h, "}");
238 else fprintf(h, "union %s", t->name ? t->name : "");
239 break;
240 case TYPE_POINTER:
241 write_type_left(h, type_pointer_get_ref(t), declonly);
242 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
243 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
244 break;
245 case TYPE_ARRAY:
246 if (t->name && type_array_is_decl_as_ptr(t))
247 fprintf(h, "%s", t->name);
248 else
250 write_type_left(h, type_array_get_element(t), declonly);
251 if (type_array_is_decl_as_ptr(t))
252 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
254 break;
255 case TYPE_BASIC:
256 if (type_basic_get_type(t) != TYPE_BASIC_HYPER)
258 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
259 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
261 switch (type_basic_get_type(t))
263 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
264 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
265 case TYPE_BASIC_INT32: fprintf(h, "long"); break;
266 case TYPE_BASIC_INT: fprintf(h, "int"); break;
267 case TYPE_BASIC_INT64: fprintf(h, "__int64"); break;
268 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
269 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
270 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
271 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
272 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
273 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
274 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
275 case TYPE_BASIC_HYPER:
276 if (type_basic_get_sign(t) > 0)
277 fprintf(h, "MIDL_uhyper");
278 else
279 fprintf(h, "hyper");
280 break;
282 break;
283 case TYPE_INTERFACE:
284 case TYPE_MODULE:
285 case TYPE_COCLASS:
286 fprintf(h, "%s", t->name);
287 break;
288 case TYPE_VOID:
289 fprintf(h, "void");
290 break;
291 case TYPE_ALIAS:
292 case TYPE_FUNCTION:
293 /* handled elsewhere */
294 assert(0);
295 break;
300 void write_type_right(FILE *h, type_t *t, int is_field)
302 if (!h) return;
304 if (type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t)) {
305 if (is_conformant_array(t)) {
306 fprintf(h, "[%s]", is_field ? "1" : "");
307 t = type_array_get_element(t);
309 for ( ;
310 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
311 t = type_array_get_element(t))
312 fprintf(h, "[%u]", type_array_get_dim(t));
316 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
318 type_t *pt;
319 int ptr_level = 0;
321 if (!h) return;
323 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
326 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
327 int i;
328 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
329 if (!callconv) callconv = "";
330 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
331 write_type_left(h, type_function_get_rettype(pt), declonly);
332 fputc(' ', h);
333 if (ptr_level) fputc('(', h);
334 fprintf(h, "%s ", callconv);
335 for (i = 0; i < ptr_level; i++)
336 fputc('*', h);
337 } else
338 write_type_left(h, t, declonly);
340 if (name) fprintf(h, "%s%s", needs_space_after(t) ? " " : "", name );
342 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
343 if (ptr_level) fputc(')', h);
344 fputc('(', h);
345 write_args(h, type_function_get_args(pt), NULL, 0, FALSE);
346 fputc(')', h);
347 } else
348 write_type_right(h, t, is_field);
351 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
353 write_type_v(f, t, field, FALSE, name);
356 void write_type_decl(FILE *f, type_t *t, const char *name)
358 write_type_v(f, t, FALSE, TRUE, name);
361 void write_type_decl_left(FILE *f, type_t *t)
363 write_type_left(f, t, TRUE);
366 static int user_type_registered(const char *name)
368 user_type_t *ut;
369 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
370 if (!strcmp(name, ut->name))
371 return 1;
372 return 0;
375 static int context_handle_registered(const char *name)
377 context_handle_t *ch;
378 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
379 if (!strcmp(name, ch->name))
380 return 1;
381 return 0;
384 static int generic_handle_registered(const char *name)
386 generic_handle_t *gh;
387 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
388 if (!strcmp(name, gh->name))
389 return 1;
390 return 0;
393 /* check for types which require additional prototypes to be generated in the
394 * header */
395 void check_for_additional_prototype_types(const var_list_t *list)
397 const var_t *v;
399 if (!list) return;
400 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
402 type_t *type = v->type;
403 if (!type) continue;
404 for (;;) {
405 const char *name = type->name;
406 if (type->user_types_registered) break;
407 type->user_types_registered = 1;
408 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
409 if (!context_handle_registered(name))
411 context_handle_t *ch = xmalloc(sizeof(*ch));
412 ch->name = xstrdup(name);
413 list_add_tail(&context_handle_list, &ch->entry);
415 /* don't carry on parsing fields within this type */
416 break;
418 if ((type_get_type(type) != TYPE_BASIC ||
419 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
420 is_attr(type->attrs, ATTR_HANDLE)) {
421 if (!generic_handle_registered(name))
423 generic_handle_t *gh = xmalloc(sizeof(*gh));
424 gh->name = xstrdup(name);
425 list_add_tail(&generic_handle_list, &gh->entry);
427 /* don't carry on parsing fields within this type */
428 break;
430 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
431 if (!user_type_registered(name))
433 user_type_t *ut = xmalloc(sizeof *ut);
434 ut->name = xstrdup(name);
435 list_add_tail(&user_type_list, &ut->entry);
437 /* don't carry on parsing fields within this type as we are already
438 * using a wire marshaled type */
439 break;
441 else if (type_is_complete(type))
443 var_list_t *vars;
444 switch (type_get_type_detect_alias(type))
446 case TYPE_ENUM:
447 vars = type_enum_get_values(type);
448 break;
449 case TYPE_STRUCT:
450 vars = type_struct_get_fields(type);
451 break;
452 case TYPE_UNION:
453 vars = type_union_get_cases(type);
454 break;
455 default:
456 vars = NULL;
457 break;
459 check_for_additional_prototype_types(vars);
462 if (type_is_alias(type))
463 type = type_alias_get_aliasee(type);
464 else if (is_ptr(type))
465 type = type_pointer_get_ref(type);
466 else if (is_array(type))
467 type = type_array_get_element(type);
468 else
469 break;
474 static void write_user_types(FILE *header)
476 user_type_t *ut;
477 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
479 const char *name = ut->name;
480 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
481 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
482 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
483 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
487 static void write_context_handle_rundowns(FILE *header)
489 context_handle_t *ch;
490 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
492 const char *name = ch->name;
493 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
497 static void write_generic_handle_routines(FILE *header)
499 generic_handle_t *gh;
500 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
502 const char *name = gh->name;
503 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
504 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
508 static void write_typedef(FILE *header, type_t *type)
510 fprintf(header, "typedef ");
511 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
512 fprintf(header, ";\n");
515 int is_const_decl(const var_t *var)
517 const type_t *t;
518 /* strangely, MIDL accepts a const attribute on any pointer in the
519 * declaration to mean that data isn't being instantiated. this appears
520 * to be a bug, but there is no benefit to being incompatible with MIDL,
521 * so we'll do the same thing */
522 for (t = var->type; ; )
524 if (is_attr(t->attrs, ATTR_CONST))
525 return TRUE;
526 else if (is_ptr(t))
527 t = type_pointer_get_ref(t);
528 else break;
530 return FALSE;
533 static void write_declaration(FILE *header, const var_t *v)
535 if (is_const_decl(v) && v->eval)
537 fprintf(header, "#define %s (", v->name);
538 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
539 fprintf(header, ")\n\n");
541 else
543 switch (v->stgclass)
545 case STG_NONE:
546 case STG_REGISTER: /* ignored */
547 break;
548 case STG_STATIC:
549 fprintf(header, "static ");
550 break;
551 case STG_EXTERN:
552 fprintf(header, "extern ");
553 break;
555 write_type_def_or_decl(header, v->type, FALSE, v->name);
556 fprintf(header, ";\n\n");
560 static void write_library(FILE *header, const typelib_t *typelib)
562 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
563 fprintf(header, "\n");
564 write_guid(header, "LIBID", typelib->name, uuid);
565 fprintf(header, "\n");
569 const var_t* get_explicit_handle_var(const var_t *func)
571 const var_t* var;
573 if (!type_get_function_args(func->type))
574 return NULL;
576 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
578 const type_t *type = var->type;
579 if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
580 return var;
583 return NULL;
586 const type_t* get_explicit_generic_handle_type(const var_t* var)
588 const type_t *t;
589 for (t = var->type;
590 is_ptr(t) || type_is_alias(t);
591 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
592 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
593 is_attr(t->attrs, ATTR_HANDLE))
594 return t;
595 return NULL;
598 const var_t* get_explicit_generic_handle_var(const var_t *func)
600 const var_t* var;
602 if (!type_get_function_args(func->type))
603 return NULL;
605 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
606 if (get_explicit_generic_handle_type(var))
607 return var;
609 return NULL;
612 const var_t* get_context_handle_var(const var_t *func)
614 const var_t* var;
616 if (!type_get_function_args(func->type))
617 return NULL;
619 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
620 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
621 return var;
623 return NULL;
626 int has_out_arg_or_return(const var_t *func)
628 const var_t *var;
630 if (!is_void(type_function_get_rettype(func->type)))
631 return 1;
633 if (!type_get_function_args(func->type))
634 return 0;
636 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
637 if (is_attr(var->attrs, ATTR_OUT))
638 return 1;
640 return 0;
644 /********** INTERFACES **********/
646 int is_object(const attr_list_t *list)
648 const attr_t *attr;
649 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
650 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
651 return 0;
654 int is_local(const attr_list_t *a)
656 return is_attr(a, ATTR_LOCAL);
659 const var_t *is_callas(const attr_list_t *a)
661 return get_attrp(a, ATTR_CALLAS);
664 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
666 const statement_t *stmt;
667 int first_iface = 1;
669 if (type_iface_get_inherit(iface))
670 write_method_macro(header, type_iface_get_inherit(iface), name);
672 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
674 const var_t *func = stmt->u.var;
676 if (first_iface)
678 fprintf(header, "/*** %s methods ***/\n", iface->name);
679 first_iface = 0;
682 if (!is_callas(func->attrs)) {
683 const var_t *arg;
685 fprintf(header, "#define %s_%s(This", name, get_name(func));
686 if (type_get_function_args(func->type))
687 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
688 fprintf(header, ",%s", arg->name);
689 fprintf(header, ") ");
691 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
692 if (type_get_function_args(func->type))
693 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
694 fprintf(header, ",%s", arg->name);
695 fprintf(header, ")\n");
700 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
702 const var_t *arg;
703 int count = 0;
705 if (do_indent)
707 indentation++;
708 indent(h, 0);
710 if (method == 1) {
711 fprintf(h, "%s* This", name);
712 count++;
714 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
715 if (count) {
716 if (do_indent)
718 fprintf(h, ",\n");
719 indent(h, 0);
721 else fprintf(h, ",");
723 write_type_decl(h, arg->type, arg->name);
724 count++;
726 if (do_indent) indentation--;
729 static void write_cpp_method_def(FILE *header, const type_t *iface)
731 const statement_t *stmt;
733 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
735 const var_t *func = stmt->u.var;
736 if (!is_callas(func->attrs)) {
737 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
738 if (!callconv) callconv = "";
739 indent(header, 0);
740 fprintf(header, "virtual ");
741 write_type_decl_left(header, type_function_get_rettype(func->type));
742 fprintf(header, " %s %s(\n", callconv, get_name(func));
743 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
744 fprintf(header, ") = 0;\n");
745 fprintf(header, "\n");
750 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
752 const statement_t *stmt;
753 int first_iface = 1;
755 if (type_iface_get_inherit(iface))
756 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
758 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
760 const var_t *func = stmt->u.var;
761 if (first_iface) {
762 indent(header, 0);
763 fprintf(header, "/*** %s methods ***/\n", iface->name);
764 first_iface = 0;
766 if (!is_callas(func->attrs)) {
767 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
768 if (!callconv) callconv = "";
769 indent(header, 0);
770 write_type_decl_left(header, type_function_get_rettype(func->type));
771 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
772 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
773 fprintf(header, ");\n");
774 fprintf(header, "\n");
779 static void write_c_method_def(FILE *header, const type_t *iface)
781 do_write_c_method_def(header, iface, iface->name);
784 static void write_c_disp_method_def(FILE *header, const type_t *iface)
786 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
789 static void write_method_proto(FILE *header, const type_t *iface)
791 const statement_t *stmt;
793 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
795 const var_t *func = stmt->u.var;
797 if (!is_local(func->attrs)) {
798 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
799 if (!callconv) callconv = "";
800 /* proxy prototype */
801 write_type_decl_left(header, type_function_get_rettype(func->type));
802 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
803 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
804 fprintf(header, ");\n");
805 /* stub prototype */
806 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
807 fprintf(header, " IRpcStubBuffer* This,\n");
808 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
809 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
810 fprintf(header, " DWORD* pdwStubPhase);\n");
815 static void write_locals(FILE *fp, const type_t *iface, int body)
817 static const char comment[]
818 = "/* WIDL-generated stub. You must provide an implementation for this. */";
819 const statement_t *stmt;
821 if (!is_object(iface->attrs))
822 return;
824 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
825 const var_t *func = stmt->u.var;
826 const var_t *cas = is_callas(func->attrs);
828 if (cas) {
829 const statement_t *stmt2 = NULL;
830 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
831 if (!strcmp(stmt2->u.var->name, cas->name))
832 break;
833 if (&stmt2->entry != type_iface_get_stmts(iface)) {
834 const var_t *m = stmt2->u.var;
835 /* proxy prototype - use local prototype */
836 write_type_decl_left(fp, type_function_get_rettype(m->type));
837 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
838 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
839 fprintf(fp, ")");
840 if (body) {
841 type_t *rt = type_function_get_rettype(m->type);
842 fprintf(fp, "\n{\n");
843 fprintf(fp, " %s\n", comment);
844 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
845 fprintf(fp, " return E_NOTIMPL;\n");
846 else if (type_get_type(rt) != TYPE_VOID) {
847 fprintf(fp, " ");
848 write_type_decl(fp, rt, "rv");
849 fprintf(fp, ";\n");
850 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
851 fprintf(fp, " return rv;\n");
853 fprintf(fp, "}\n\n");
855 else
856 fprintf(fp, ";\n");
857 /* stub prototype - use remotable prototype */
858 write_type_decl_left(fp, type_function_get_rettype(func->type));
859 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
860 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
861 fprintf(fp, ")");
862 if (body)
863 /* Remotable methods must all return HRESULTs. */
864 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
865 else
866 fprintf(fp, ";\n");
868 else
869 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
874 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
876 const statement_t *stmt;
877 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
879 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
880 write_locals(local_stubs, stmt->u.type, TRUE);
881 else if (stmt->type == STMT_LIBRARY)
882 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
886 void write_local_stubs(const statement_list_t *stmts)
888 FILE *local_stubs;
890 if (!local_stubs_name) return;
892 local_stubs = fopen(local_stubs_name, "w");
893 if (!local_stubs) {
894 error("Could not open %s for output\n", local_stubs_name);
895 return;
897 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
898 fprintf(local_stubs, "#include <objbase.h>\n");
899 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
901 write_local_stubs_stmts(local_stubs, stmts);
903 fclose(local_stubs);
906 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
908 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
910 /* FIXME: do we need to handle call_as? */
911 write_type_decl_left(header, type_function_get_rettype(fun->type));
912 fprintf(header, " ");
913 if (callconv) fprintf(header, "%s ", callconv);
914 fprintf(header, "%s%s(\n", prefix, get_name(fun));
915 if (type_get_function_args(fun->type))
916 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
917 else
918 fprintf(header, " void");
919 fprintf(header, ");\n\n");
922 static void write_forward(FILE *header, type_t *iface)
924 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
925 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
926 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
927 fprintf(header, "#endif\n\n" );
930 static void write_iface_guid(FILE *header, const type_t *iface)
932 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
933 write_guid(header, "IID", iface->name, uuid);
936 static void write_dispiface_guid(FILE *header, const type_t *iface)
938 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
939 write_guid(header, "DIID", iface->name, uuid);
942 static void write_coclass_guid(FILE *header, const type_t *cocl)
944 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
945 write_guid(header, "CLSID", cocl->name, uuid);
948 static void write_com_interface_start(FILE *header, const type_t *iface)
950 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
951 fprintf(header, "/*****************************************************************************\n");
952 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
953 fprintf(header, " */\n");
954 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
955 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
958 static void write_com_interface_end(FILE *header, type_t *iface)
960 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
961 if (dispinterface)
962 write_dispiface_guid(header, iface);
963 else
964 write_iface_guid(header, iface);
965 /* C++ interface */
966 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
967 if (type_iface_get_inherit(iface))
969 fprintf(header, "interface %s : public %s\n", iface->name,
970 type_iface_get_inherit(iface)->name);
971 fprintf(header, "{\n");
973 else
975 fprintf(header, "interface %s\n", iface->name);
976 fprintf(header, "{\n");
977 fprintf(header, " BEGIN_INTERFACE\n");
978 fprintf(header, "\n");
980 /* dispinterfaces don't have real functions, so don't write C++ functions for
981 * them */
982 if (!dispinterface)
984 indentation++;
985 write_cpp_method_def(header, iface);
986 indentation--;
988 if (!type_iface_get_inherit(iface))
989 fprintf(header, " END_INTERFACE\n");
990 fprintf(header, "};\n");
991 fprintf(header, "#else\n");
992 /* C interface */
993 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
994 indentation++;
995 fprintf(header, " BEGIN_INTERFACE\n");
996 fprintf(header, "\n");
997 if (dispinterface)
998 write_c_disp_method_def(header, iface);
999 else
1000 write_c_method_def(header, iface);
1001 indentation--;
1002 fprintf(header, " END_INTERFACE\n");
1003 fprintf(header, "} %sVtbl;\n", iface->name);
1004 fprintf(header, "interface %s {\n", iface->name);
1005 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1006 fprintf(header, "};\n");
1007 fprintf(header, "\n");
1008 fprintf(header, "#ifdef COBJMACROS\n");
1009 /* dispinterfaces don't have real functions, so don't write macros for them,
1010 * only for the interface this interface inherits from, i.e. IDispatch */
1011 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1012 fprintf(header, "#endif\n");
1013 fprintf(header, "\n");
1014 fprintf(header, "#endif\n");
1015 fprintf(header, "\n");
1016 /* dispinterfaces don't have real functions, so don't write prototypes for
1017 * them */
1018 if (!dispinterface)
1020 write_method_proto(header, iface);
1021 write_locals(header, iface, FALSE);
1022 fprintf(header, "\n");
1024 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1027 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1029 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1030 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1031 static int allocate_written = 0;
1033 if (!allocate_written)
1035 allocate_written = 1;
1036 fprintf(header, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
1037 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1040 fprintf(header, "/*****************************************************************************\n");
1041 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1042 fprintf(header, " */\n");
1043 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1044 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1045 if (var) fprintf(header, "extern handle_t %s;\n", var);
1046 if (old_names)
1048 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1049 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1051 else
1053 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1054 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1055 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1056 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1060 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1062 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1065 static void write_coclass(FILE *header, type_t *cocl)
1067 fprintf(header, "/*****************************************************************************\n");
1068 fprintf(header, " * %s coclass\n", cocl->name);
1069 fprintf(header, " */\n\n");
1070 write_coclass_guid(header, cocl);
1071 fprintf(header, "\n");
1074 static void write_coclass_forward(FILE *header, type_t *cocl)
1076 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1077 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1078 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1079 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1082 static void write_import(FILE *header, const char *fname)
1084 char *hname, *p;
1086 hname = dup_basename(fname, ".idl");
1087 p = hname + strlen(hname) - 2;
1088 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1090 fprintf(header, "#include <%s>\n", hname);
1091 free(hname);
1094 static void write_imports(FILE *header, const statement_list_t *stmts)
1096 const statement_t *stmt;
1097 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1099 switch (stmt->type)
1101 case STMT_TYPE:
1102 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1103 write_imports(header, type_iface_get_stmts(stmt->u.type));
1104 break;
1105 case STMT_TYPEREF:
1106 case STMT_IMPORTLIB:
1107 /* not included in header */
1108 break;
1109 case STMT_IMPORT:
1110 write_import(header, stmt->u.str);
1111 break;
1112 case STMT_TYPEDEF:
1113 case STMT_MODULE:
1114 case STMT_CPPQUOTE:
1115 case STMT_DECLARATION:
1116 /* not processed here */
1117 break;
1118 case STMT_LIBRARY:
1119 write_imports(header, stmt->u.lib->stmts);
1120 break;
1125 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1127 const statement_t *stmt;
1128 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1130 switch (stmt->type)
1132 case STMT_TYPE:
1133 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1135 if (is_object(stmt->u.type->attrs) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1136 write_forward(header, stmt->u.type);
1138 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1139 write_coclass_forward(header, stmt->u.type);
1140 break;
1141 case STMT_TYPEREF:
1142 case STMT_IMPORTLIB:
1143 /* not included in header */
1144 break;
1145 case STMT_IMPORT:
1146 case STMT_TYPEDEF:
1147 case STMT_MODULE:
1148 case STMT_CPPQUOTE:
1149 case STMT_DECLARATION:
1150 /* not processed here */
1151 break;
1152 case STMT_LIBRARY:
1153 write_forward_decls(header, stmt->u.lib->stmts);
1154 break;
1159 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1161 const statement_t *stmt;
1162 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1164 switch (stmt->type)
1166 case STMT_TYPE:
1167 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1169 type_t *iface = stmt->u.type;
1170 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type->attrs))
1172 write_com_interface_start(header, iface);
1173 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1174 write_com_interface_end(header, iface);
1176 else
1178 write_rpc_interface_start(header, iface);
1179 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1180 write_rpc_interface_end(header, iface);
1183 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1184 write_coclass(header, stmt->u.type);
1185 else
1187 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1188 fprintf(header, ";\n\n");
1190 break;
1191 case STMT_TYPEREF:
1192 /* FIXME: shouldn't write out forward declarations for undefined
1193 * interfaces but a number of our IDL files depend on this */
1194 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1195 write_forward(header, stmt->u.type);
1196 break;
1197 case STMT_IMPORTLIB:
1198 case STMT_MODULE:
1199 /* not included in header */
1200 break;
1201 case STMT_IMPORT:
1202 /* not processed here */
1203 break;
1204 case STMT_TYPEDEF:
1206 const type_list_t *type_entry = stmt->u.type_list;
1207 for (; type_entry; type_entry = type_entry->next)
1208 write_typedef(header, type_entry->type);
1209 break;
1211 case STMT_LIBRARY:
1212 write_library(header, stmt->u.lib);
1213 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1214 break;
1215 case STMT_CPPQUOTE:
1216 fprintf(header, "%s\n", stmt->u.str);
1217 break;
1218 case STMT_DECLARATION:
1219 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1221 if (!ignore_funcs)
1223 int prefixes_differ = strcmp(prefix_client, prefix_server);
1225 if (prefixes_differ)
1227 fprintf(header, "/* client prototype */\n");
1228 write_function_proto(header, iface, stmt->u.var, prefix_client);
1229 fprintf(header, "/* server prototype */\n");
1231 write_function_proto(header, iface, stmt->u.var, prefix_server);
1234 else
1235 write_declaration(header, stmt->u.var);
1236 break;
1241 void write_header(const statement_list_t *stmts)
1243 FILE *header;
1245 if (!do_header) return;
1247 if(!(header = fopen(header_name, "w"))) {
1248 error("Could not open %s for output\n", header_name);
1249 return;
1251 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1252 fprintf(header, "#include <rpc.h>\n" );
1253 fprintf(header, "#include <rpcndr.h>\n\n" );
1255 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1256 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1257 start_cplusplus_guard(header);
1259 fprintf(header, "/* Headers for imported files */\n\n");
1260 write_imports(header, stmts);
1261 fprintf(header, "\n");
1263 /* FIXME: should be before imported file includes */
1264 fprintf(header, "/* Forward declarations */\n\n");
1265 write_forward_decls(header, stmts);
1266 fprintf(header, "\n");
1268 write_header_stmts(header, stmts, NULL, FALSE);
1270 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1271 fprintf(header, "\n");
1272 write_user_types(header);
1273 write_generic_handle_routines(header);
1274 write_context_handle_rundowns(header);
1275 fprintf(header, "\n");
1276 fprintf(header, "/* End additional prototypes */\n");
1277 fprintf(header, "\n");
1279 end_cplusplus_guard(header);
1280 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1282 fclose(header);