Release 1.1.37.
[wine/gsoc-2012-control.git] / tools / widl / header.c
blob5602b6da572c624bc089611f2e97dc140d7a5c1c
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_INT32 &&
257 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
258 type_basic_get_type(t) != TYPE_BASIC_HYPER)
260 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
261 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
263 switch (type_basic_get_type(t))
265 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
266 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
267 case TYPE_BASIC_INT: fprintf(h, "int"); break;
268 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
269 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
270 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
271 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
272 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
273 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
274 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
275 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
276 case TYPE_BASIC_INT32:
277 if (type_basic_get_sign(t) > 0)
278 fprintf(h, "ULONG");
279 else
280 fprintf(h, "LONG");
281 break;
282 case TYPE_BASIC_INT64:
283 if (type_basic_get_sign(t) > 0)
284 fprintf(h, "UINT64");
285 else
286 fprintf(h, "INT64");
287 break;
288 case TYPE_BASIC_HYPER:
289 if (type_basic_get_sign(t) > 0)
290 fprintf(h, "MIDL_uhyper");
291 else
292 fprintf(h, "hyper");
293 break;
295 break;
296 case TYPE_INTERFACE:
297 case TYPE_MODULE:
298 case TYPE_COCLASS:
299 fprintf(h, "%s", t->name);
300 break;
301 case TYPE_VOID:
302 fprintf(h, "void");
303 break;
304 case TYPE_BITFIELD:
305 write_type_left(h, type_bitfield_get_field(t), declonly);
306 break;
307 case TYPE_ALIAS:
308 case TYPE_FUNCTION:
309 /* handled elsewhere */
310 assert(0);
311 break;
316 void write_type_right(FILE *h, type_t *t, int is_field)
318 if (!h) return;
320 switch (type_get_type(t))
322 case TYPE_ARRAY:
323 if (!type_array_is_decl_as_ptr(t))
325 if (is_conformant_array(t))
327 fprintf(h, "[%s]", is_field ? "1" : "");
328 t = type_array_get_element(t);
330 for ( ;
331 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
332 t = type_array_get_element(t))
333 fprintf(h, "[%u]", type_array_get_dim(t));
335 break;
336 case TYPE_BITFIELD:
337 fprintf(h, " : %lu", type_bitfield_get_bits(t)->cval);
338 break;
339 case TYPE_VOID:
340 case TYPE_BASIC:
341 case TYPE_ENUM:
342 case TYPE_STRUCT:
343 case TYPE_ENCAPSULATED_UNION:
344 case TYPE_UNION:
345 case TYPE_ALIAS:
346 case TYPE_MODULE:
347 case TYPE_COCLASS:
348 case TYPE_FUNCTION:
349 case TYPE_INTERFACE:
350 case TYPE_POINTER:
351 break;
355 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
357 type_t *pt = NULL;
358 int ptr_level = 0;
360 if (!h) return;
362 if (t) {
363 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
366 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
367 int i;
368 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
369 if (!callconv) callconv = "";
370 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
371 write_type_left(h, type_function_get_rettype(pt), declonly);
372 fputc(' ', h);
373 if (ptr_level) fputc('(', h);
374 fprintf(h, "%s ", callconv);
375 for (i = 0; i < ptr_level; i++)
376 fputc('*', h);
377 } else
378 write_type_left(h, t, declonly);
381 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
383 if (t) {
384 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
385 const var_list_t *args = type_function_get_args(pt);
387 if (ptr_level) fputc(')', h);
388 fputc('(', h);
389 if (args)
390 write_args(h, args, NULL, 0, FALSE);
391 else
392 fprintf(h, "void");
393 fputc(')', h);
394 } else
395 write_type_right(h, t, is_field);
399 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
401 write_type_v(f, t, field, FALSE, name);
404 void write_type_decl(FILE *f, type_t *t, const char *name)
406 write_type_v(f, t, FALSE, TRUE, name);
409 void write_type_decl_left(FILE *f, type_t *t)
411 write_type_left(f, t, TRUE);
414 static int user_type_registered(const char *name)
416 user_type_t *ut;
417 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
418 if (!strcmp(name, ut->name))
419 return 1;
420 return 0;
423 static int context_handle_registered(const char *name)
425 context_handle_t *ch;
426 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
427 if (!strcmp(name, ch->name))
428 return 1;
429 return 0;
432 static int generic_handle_registered(const char *name)
434 generic_handle_t *gh;
435 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
436 if (!strcmp(name, gh->name))
437 return 1;
438 return 0;
441 /* check for types which require additional prototypes to be generated in the
442 * header */
443 void check_for_additional_prototype_types(const var_list_t *list)
445 const var_t *v;
447 if (!list) return;
448 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
450 type_t *type = v->type;
451 if (!type) continue;
452 for (;;) {
453 const char *name = type->name;
454 if (type->user_types_registered) break;
455 type->user_types_registered = 1;
456 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
457 if (!context_handle_registered(name))
459 context_handle_t *ch = xmalloc(sizeof(*ch));
460 ch->name = xstrdup(name);
461 list_add_tail(&context_handle_list, &ch->entry);
463 /* don't carry on parsing fields within this type */
464 break;
466 if ((type_get_type(type) != TYPE_BASIC ||
467 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
468 is_attr(type->attrs, ATTR_HANDLE)) {
469 if (!generic_handle_registered(name))
471 generic_handle_t *gh = xmalloc(sizeof(*gh));
472 gh->name = xstrdup(name);
473 list_add_tail(&generic_handle_list, &gh->entry);
475 /* don't carry on parsing fields within this type */
476 break;
478 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
479 if (!user_type_registered(name))
481 user_type_t *ut = xmalloc(sizeof *ut);
482 ut->name = xstrdup(name);
483 list_add_tail(&user_type_list, &ut->entry);
485 /* don't carry on parsing fields within this type as we are already
486 * using a wire marshaled type */
487 break;
489 else if (type_is_complete(type))
491 var_list_t *vars;
492 switch (type_get_type_detect_alias(type))
494 case TYPE_ENUM:
495 vars = type_enum_get_values(type);
496 break;
497 case TYPE_STRUCT:
498 vars = type_struct_get_fields(type);
499 break;
500 case TYPE_UNION:
501 vars = type_union_get_cases(type);
502 break;
503 default:
504 vars = NULL;
505 break;
507 check_for_additional_prototype_types(vars);
510 if (type_is_alias(type))
511 type = type_alias_get_aliasee(type);
512 else if (is_ptr(type))
513 type = type_pointer_get_ref(type);
514 else if (is_array(type))
515 type = type_array_get_element(type);
516 else
517 break;
522 static void write_user_types(FILE *header)
524 user_type_t *ut;
525 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
527 const char *name = ut->name;
528 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
529 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
530 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
531 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
535 static void write_context_handle_rundowns(FILE *header)
537 context_handle_t *ch;
538 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
540 const char *name = ch->name;
541 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
545 static void write_generic_handle_routines(FILE *header)
547 generic_handle_t *gh;
548 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
550 const char *name = gh->name;
551 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
552 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
556 static void write_typedef(FILE *header, type_t *type)
558 fprintf(header, "typedef ");
559 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
560 fprintf(header, ";\n");
563 int is_const_decl(const var_t *var)
565 const type_t *t;
566 /* strangely, MIDL accepts a const attribute on any pointer in the
567 * declaration to mean that data isn't being instantiated. this appears
568 * to be a bug, but there is no benefit to being incompatible with MIDL,
569 * so we'll do the same thing */
570 for (t = var->type; ; )
572 if (is_attr(t->attrs, ATTR_CONST))
573 return TRUE;
574 else if (is_ptr(t))
575 t = type_pointer_get_ref(t);
576 else break;
578 return FALSE;
581 static void write_declaration(FILE *header, const var_t *v)
583 if (is_const_decl(v) && v->eval)
585 fprintf(header, "#define %s (", v->name);
586 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
587 fprintf(header, ")\n\n");
589 else
591 switch (v->stgclass)
593 case STG_NONE:
594 case STG_REGISTER: /* ignored */
595 break;
596 case STG_STATIC:
597 fprintf(header, "static ");
598 break;
599 case STG_EXTERN:
600 fprintf(header, "extern ");
601 break;
603 write_type_def_or_decl(header, v->type, FALSE, v->name);
604 fprintf(header, ";\n\n");
608 static void write_library(FILE *header, const typelib_t *typelib)
610 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
611 fprintf(header, "\n");
612 write_guid(header, "LIBID", typelib->name, uuid);
613 fprintf(header, "\n");
617 const var_t* get_explicit_handle_var(const var_t *func)
619 const var_t* var;
621 if (!type_get_function_args(func->type))
622 return NULL;
624 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
626 const type_t *type = var->type;
627 if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
628 return var;
631 return NULL;
634 const type_t* get_explicit_generic_handle_type(const var_t* var)
636 const type_t *t;
637 for (t = var->type;
638 is_ptr(t) || type_is_alias(t);
639 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
640 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
641 is_attr(t->attrs, ATTR_HANDLE))
642 return t;
643 return NULL;
646 const var_t* get_explicit_generic_handle_var(const var_t *func)
648 const var_t* var;
650 if (!type_get_function_args(func->type))
651 return NULL;
653 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
654 if (get_explicit_generic_handle_type(var))
655 return var;
657 return NULL;
660 const var_t* get_context_handle_var(const var_t *func)
662 const var_t* var;
664 if (!type_get_function_args(func->type))
665 return NULL;
667 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
668 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
669 return var;
671 return NULL;
674 int has_out_arg_or_return(const var_t *func)
676 const var_t *var;
678 if (!is_void(type_function_get_rettype(func->type)))
679 return 1;
681 if (!type_get_function_args(func->type))
682 return 0;
684 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
685 if (is_attr(var->attrs, ATTR_OUT))
686 return 1;
688 return 0;
692 /********** INTERFACES **********/
694 int is_object(const type_t *iface)
696 const attr_t *attr;
697 if (type_is_defined(iface) && type_iface_get_inherit(iface))
698 return 1;
699 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
700 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
701 return 0;
704 int is_local(const attr_list_t *a)
706 return is_attr(a, ATTR_LOCAL);
709 const var_t *is_callas(const attr_list_t *a)
711 return get_attrp(a, ATTR_CALLAS);
714 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
716 const statement_t *stmt;
717 int first_iface = 1;
719 if (type_iface_get_inherit(iface))
720 write_method_macro(header, type_iface_get_inherit(iface), name);
722 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
724 const var_t *func = stmt->u.var;
726 if (first_iface)
728 fprintf(header, "/*** %s methods ***/\n", iface->name);
729 first_iface = 0;
732 if (!is_callas(func->attrs)) {
733 const var_t *arg;
735 fprintf(header, "#define %s_%s(This", name, get_name(func));
736 if (type_get_function_args(func->type))
737 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
738 fprintf(header, ",%s", arg->name);
739 fprintf(header, ") ");
741 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
742 if (type_get_function_args(func->type))
743 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
744 fprintf(header, ",%s", arg->name);
745 fprintf(header, ")\n");
750 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
752 const var_t *arg;
753 int count = 0;
755 if (do_indent)
757 indentation++;
758 indent(h, 0);
760 if (method == 1) {
761 fprintf(h, "%s* This", name);
762 count++;
764 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
765 if (count) {
766 if (do_indent)
768 fprintf(h, ",\n");
769 indent(h, 0);
771 else fprintf(h, ",");
773 write_type_decl(h, arg->type, arg->name);
774 count++;
776 if (do_indent) indentation--;
779 static void write_cpp_method_def(FILE *header, const type_t *iface)
781 const statement_t *stmt;
783 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
785 const var_t *func = stmt->u.var;
786 if (!is_callas(func->attrs)) {
787 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
788 if (!callconv) callconv = "";
789 indent(header, 0);
790 fprintf(header, "virtual ");
791 write_type_decl_left(header, type_function_get_rettype(func->type));
792 fprintf(header, " %s %s(\n", callconv, get_name(func));
793 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
794 fprintf(header, ") = 0;\n");
795 fprintf(header, "\n");
800 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
802 const statement_t *stmt;
803 int first_iface = 1;
805 if (type_iface_get_inherit(iface))
806 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
808 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
810 const var_t *func = stmt->u.var;
811 if (first_iface) {
812 indent(header, 0);
813 fprintf(header, "/*** %s methods ***/\n", iface->name);
814 first_iface = 0;
816 if (!is_callas(func->attrs)) {
817 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
818 if (!callconv) callconv = "";
819 indent(header, 0);
820 write_type_decl_left(header, type_function_get_rettype(func->type));
821 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
822 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
823 fprintf(header, ");\n");
824 fprintf(header, "\n");
829 static void write_c_method_def(FILE *header, const type_t *iface)
831 do_write_c_method_def(header, iface, iface->name);
834 static void write_c_disp_method_def(FILE *header, const type_t *iface)
836 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
839 static void write_method_proto(FILE *header, const type_t *iface)
841 const statement_t *stmt;
843 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
845 const var_t *func = stmt->u.var;
847 if (!is_local(func->attrs)) {
848 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
849 if (!callconv) callconv = "";
850 /* proxy prototype */
851 write_type_decl_left(header, type_function_get_rettype(func->type));
852 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
853 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
854 fprintf(header, ");\n");
855 /* stub prototype */
856 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
857 fprintf(header, " IRpcStubBuffer* This,\n");
858 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
859 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
860 fprintf(header, " DWORD* pdwStubPhase);\n");
865 static void write_locals(FILE *fp, const type_t *iface, int body)
867 static const char comment[]
868 = "/* WIDL-generated stub. You must provide an implementation for this. */";
869 const statement_t *stmt;
871 if (!is_object(iface))
872 return;
874 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
875 const var_t *func = stmt->u.var;
876 const var_t *cas = is_callas(func->attrs);
878 if (cas) {
879 const statement_t *stmt2 = NULL;
880 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
881 if (!strcmp(stmt2->u.var->name, cas->name))
882 break;
883 if (&stmt2->entry != type_iface_get_stmts(iface)) {
884 const var_t *m = stmt2->u.var;
885 /* proxy prototype - use local prototype */
886 write_type_decl_left(fp, type_function_get_rettype(m->type));
887 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
888 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
889 fprintf(fp, ")");
890 if (body) {
891 type_t *rt = type_function_get_rettype(m->type);
892 fprintf(fp, "\n{\n");
893 fprintf(fp, " %s\n", comment);
894 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
895 fprintf(fp, " return E_NOTIMPL;\n");
896 else if (type_get_type(rt) != TYPE_VOID) {
897 fprintf(fp, " ");
898 write_type_decl(fp, rt, "rv");
899 fprintf(fp, ";\n");
900 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
901 fprintf(fp, " return rv;\n");
903 fprintf(fp, "}\n\n");
905 else
906 fprintf(fp, ";\n");
907 /* stub prototype - use remotable prototype */
908 write_type_decl_left(fp, type_function_get_rettype(func->type));
909 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
910 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
911 fprintf(fp, ")");
912 if (body)
913 /* Remotable methods must all return HRESULTs. */
914 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
915 else
916 fprintf(fp, ";\n");
918 else
919 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
924 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
926 const statement_t *stmt;
927 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
929 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
930 write_locals(local_stubs, stmt->u.type, TRUE);
931 else if (stmt->type == STMT_LIBRARY)
932 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
936 void write_local_stubs(const statement_list_t *stmts)
938 FILE *local_stubs;
940 if (!local_stubs_name) return;
942 local_stubs = fopen(local_stubs_name, "w");
943 if (!local_stubs) {
944 error("Could not open %s for output\n", local_stubs_name);
945 return;
947 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
948 fprintf(local_stubs, "#include <objbase.h>\n");
949 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
951 write_local_stubs_stmts(local_stubs, stmts);
953 fclose(local_stubs);
956 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
958 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
960 /* FIXME: do we need to handle call_as? */
961 write_type_decl_left(header, type_function_get_rettype(fun->type));
962 fprintf(header, " ");
963 if (callconv) fprintf(header, "%s ", callconv);
964 fprintf(header, "%s%s(\n", prefix, get_name(fun));
965 if (type_get_function_args(fun->type))
966 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
967 else
968 fprintf(header, " void");
969 fprintf(header, ");\n\n");
972 static void write_forward(FILE *header, type_t *iface)
974 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
975 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
976 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
977 fprintf(header, "#endif\n\n" );
980 static void write_iface_guid(FILE *header, const type_t *iface)
982 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
983 write_guid(header, "IID", iface->name, uuid);
986 static void write_dispiface_guid(FILE *header, const type_t *iface)
988 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
989 write_guid(header, "DIID", iface->name, uuid);
992 static void write_coclass_guid(FILE *header, const type_t *cocl)
994 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
995 write_guid(header, "CLSID", cocl->name, uuid);
998 static void write_com_interface_start(FILE *header, const type_t *iface)
1000 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1001 fprintf(header, "/*****************************************************************************\n");
1002 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1003 fprintf(header, " */\n");
1004 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1005 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1008 static void write_com_interface_end(FILE *header, type_t *iface)
1010 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1011 if (dispinterface)
1012 write_dispiface_guid(header, iface);
1013 else
1014 write_iface_guid(header, iface);
1015 /* C++ interface */
1016 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1017 if (type_iface_get_inherit(iface))
1019 fprintf(header, "interface %s : public %s\n", iface->name,
1020 type_iface_get_inherit(iface)->name);
1021 fprintf(header, "{\n");
1023 else
1025 fprintf(header, "interface %s\n", iface->name);
1026 fprintf(header, "{\n");
1027 fprintf(header, " BEGIN_INTERFACE\n");
1028 fprintf(header, "\n");
1030 /* dispinterfaces don't have real functions, so don't write C++ functions for
1031 * them */
1032 if (!dispinterface)
1034 indentation++;
1035 write_cpp_method_def(header, iface);
1036 indentation--;
1038 if (!type_iface_get_inherit(iface))
1039 fprintf(header, " END_INTERFACE\n");
1040 fprintf(header, "};\n");
1041 fprintf(header, "#else\n");
1042 /* C interface */
1043 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1044 indentation++;
1045 fprintf(header, " BEGIN_INTERFACE\n");
1046 fprintf(header, "\n");
1047 if (dispinterface)
1048 write_c_disp_method_def(header, iface);
1049 else
1050 write_c_method_def(header, iface);
1051 indentation--;
1052 fprintf(header, " END_INTERFACE\n");
1053 fprintf(header, "} %sVtbl;\n", iface->name);
1054 fprintf(header, "interface %s {\n", iface->name);
1055 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1056 fprintf(header, "};\n");
1057 fprintf(header, "\n");
1058 fprintf(header, "#ifdef COBJMACROS\n");
1059 /* dispinterfaces don't have real functions, so don't write macros for them,
1060 * only for the interface this interface inherits from, i.e. IDispatch */
1061 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1062 fprintf(header, "#endif\n");
1063 fprintf(header, "\n");
1064 fprintf(header, "#endif\n");
1065 fprintf(header, "\n");
1066 /* dispinterfaces don't have real functions, so don't write prototypes for
1067 * them */
1068 if (!dispinterface)
1070 write_method_proto(header, iface);
1071 write_locals(header, iface, FALSE);
1072 fprintf(header, "\n");
1074 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1077 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1079 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1080 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1081 static int allocate_written = 0;
1083 if (!allocate_written)
1085 allocate_written = 1;
1086 fprintf(header, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
1087 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1090 fprintf(header, "/*****************************************************************************\n");
1091 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1092 fprintf(header, " */\n");
1093 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1094 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1095 if (var) fprintf(header, "extern handle_t %s;\n", var);
1096 if (old_names)
1098 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1099 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1101 else
1103 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1104 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1105 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1106 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1110 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1112 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1115 static void write_coclass(FILE *header, type_t *cocl)
1117 fprintf(header, "/*****************************************************************************\n");
1118 fprintf(header, " * %s coclass\n", cocl->name);
1119 fprintf(header, " */\n\n");
1120 write_coclass_guid(header, cocl);
1121 fprintf(header, "\n");
1124 static void write_coclass_forward(FILE *header, type_t *cocl)
1126 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1127 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1128 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1129 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1132 static void write_import(FILE *header, const char *fname)
1134 char *hname, *p;
1136 hname = dup_basename(fname, ".idl");
1137 p = hname + strlen(hname) - 2;
1138 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1140 fprintf(header, "#include <%s>\n", hname);
1141 free(hname);
1144 static void write_imports(FILE *header, const statement_list_t *stmts)
1146 const statement_t *stmt;
1147 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1149 switch (stmt->type)
1151 case STMT_TYPE:
1152 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1153 write_imports(header, type_iface_get_stmts(stmt->u.type));
1154 break;
1155 case STMT_TYPEREF:
1156 case STMT_IMPORTLIB:
1157 /* not included in header */
1158 break;
1159 case STMT_IMPORT:
1160 write_import(header, stmt->u.str);
1161 break;
1162 case STMT_TYPEDEF:
1163 case STMT_MODULE:
1164 case STMT_CPPQUOTE:
1165 case STMT_DECLARATION:
1166 /* not processed here */
1167 break;
1168 case STMT_LIBRARY:
1169 write_imports(header, stmt->u.lib->stmts);
1170 break;
1175 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1177 const statement_t *stmt;
1178 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1180 switch (stmt->type)
1182 case STMT_TYPE:
1183 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1185 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1186 write_forward(header, stmt->u.type);
1188 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1189 write_coclass_forward(header, stmt->u.type);
1190 break;
1191 case STMT_TYPEREF:
1192 case STMT_IMPORTLIB:
1193 /* not included in header */
1194 break;
1195 case STMT_IMPORT:
1196 case STMT_TYPEDEF:
1197 case STMT_MODULE:
1198 case STMT_CPPQUOTE:
1199 case STMT_DECLARATION:
1200 /* not processed here */
1201 break;
1202 case STMT_LIBRARY:
1203 write_forward_decls(header, stmt->u.lib->stmts);
1204 break;
1209 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1211 const statement_t *stmt;
1212 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1214 switch (stmt->type)
1216 case STMT_TYPE:
1217 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1219 type_t *iface = stmt->u.type;
1220 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1222 write_com_interface_start(header, iface);
1223 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1224 write_com_interface_end(header, iface);
1226 else
1228 write_rpc_interface_start(header, iface);
1229 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1230 write_rpc_interface_end(header, iface);
1233 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1234 write_coclass(header, stmt->u.type);
1235 else
1237 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1238 fprintf(header, ";\n\n");
1240 break;
1241 case STMT_TYPEREF:
1242 /* FIXME: shouldn't write out forward declarations for undefined
1243 * interfaces but a number of our IDL files depend on this */
1244 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1245 write_forward(header, stmt->u.type);
1246 break;
1247 case STMT_IMPORTLIB:
1248 case STMT_MODULE:
1249 /* not included in header */
1250 break;
1251 case STMT_IMPORT:
1252 /* not processed here */
1253 break;
1254 case STMT_TYPEDEF:
1256 const type_list_t *type_entry = stmt->u.type_list;
1257 for (; type_entry; type_entry = type_entry->next)
1258 write_typedef(header, type_entry->type);
1259 break;
1261 case STMT_LIBRARY:
1262 write_library(header, stmt->u.lib);
1263 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1264 break;
1265 case STMT_CPPQUOTE:
1266 fprintf(header, "%s\n", stmt->u.str);
1267 break;
1268 case STMT_DECLARATION:
1269 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1271 if (!ignore_funcs)
1273 int prefixes_differ = strcmp(prefix_client, prefix_server);
1275 if (prefixes_differ)
1277 fprintf(header, "/* client prototype */\n");
1278 write_function_proto(header, iface, stmt->u.var, prefix_client);
1279 fprintf(header, "/* server prototype */\n");
1281 write_function_proto(header, iface, stmt->u.var, prefix_server);
1284 else
1285 write_declaration(header, stmt->u.var);
1286 break;
1291 void write_header(const statement_list_t *stmts)
1293 FILE *header;
1295 if (!do_header) return;
1297 if(!(header = fopen(header_name, "w"))) {
1298 error("Could not open %s for output\n", header_name);
1299 return;
1301 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1302 fprintf(header, "#include <rpc.h>\n" );
1303 fprintf(header, "#include <rpcndr.h>\n\n" );
1305 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1306 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1307 start_cplusplus_guard(header);
1309 fprintf(header, "/* Headers for imported files */\n\n");
1310 write_imports(header, stmts);
1311 fprintf(header, "\n");
1313 /* FIXME: should be before imported file includes */
1314 fprintf(header, "/* Forward declarations */\n\n");
1315 write_forward_decls(header, stmts);
1316 fprintf(header, "\n");
1318 write_header_stmts(header, stmts, NULL, FALSE);
1320 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1321 fprintf(header, "\n");
1322 write_user_types(header);
1323 write_generic_handle_routines(header);
1324 write_context_handle_rundowns(header);
1325 fprintf(header, "\n");
1326 fprintf(header, "/* End additional prototypes */\n");
1327 fprintf(header, "\n");
1329 end_cplusplus_guard(header);
1330 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1332 fclose(header);