push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / tools / widl / header.c
blobb669ae2e7b56c627b43410218029b5b37a632fc6
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, "%s", 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_conformant_array(t) || t->declarray || (is_array(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) || t->declarray || !is_ptr(t)))
195 fprintf(h, "const ");
197 if (type_is_alias(t)) fprintf(h, "%s", t->name);
198 else if (t->declarray) write_type_left(h, type_array_get_element(t), declonly);
199 else {
200 if (t->sign > 0) fprintf(h, "signed ");
201 else if (t->sign < 0) fprintf(h, "unsigned ");
203 if (is_array(t) && !t->name) {
204 write_type_left(h, type_array_get_element(t), declonly);
205 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
206 } else switch (t->type) {
207 case RPC_FC_ENUM16:
208 case RPC_FC_ENUM32:
209 if (!declonly && t->defined && !t->written) {
210 if (t->name) fprintf(h, "enum %s {\n", t->name);
211 else fprintf(h, "enum {\n");
212 t->written = TRUE;
213 indentation++;
214 write_enums(h, type_enum_get_values(t));
215 indent(h, -1);
216 fprintf(h, "}");
218 else fprintf(h, "enum %s", t->name ? t->name : "");
219 break;
220 case RPC_FC_STRUCT:
221 case RPC_FC_CVSTRUCT:
222 case RPC_FC_CPSTRUCT:
223 case RPC_FC_CSTRUCT:
224 case RPC_FC_PSTRUCT:
225 case RPC_FC_BOGUS_STRUCT:
226 case RPC_FC_ENCAPSULATED_UNION:
227 if (!declonly && t->defined && !t->written) {
228 if (t->name) fprintf(h, "struct %s {\n", t->name);
229 else fprintf(h, "struct {\n");
230 t->written = TRUE;
231 indentation++;
232 if (t->type == RPC_FC_ENCAPSULATED_UNION)
233 write_fields(h, type_encapsulated_union_get_fields(t));
234 else
235 write_fields(h, type_struct_get_fields(t));
236 indent(h, -1);
237 fprintf(h, "}");
239 else fprintf(h, "struct %s", t->name ? t->name : "");
240 break;
241 case RPC_FC_NON_ENCAPSULATED_UNION:
242 if (!declonly && t->defined && !t->written) {
243 if (t->name) fprintf(h, "union %s {\n", t->name);
244 else fprintf(h, "union {\n");
245 t->written = TRUE;
246 indentation++;
247 write_fields(h, type_union_get_cases(t));
248 indent(h, -1);
249 fprintf(h, "}");
251 else fprintf(h, "union %s", t->name ? t->name : "");
252 break;
253 case RPC_FC_RP:
254 case RPC_FC_UP:
255 case RPC_FC_FP:
256 case RPC_FC_OP:
257 write_type_left(h, type_pointer_get_ref(t), declonly);
258 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
259 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
260 break;
261 default:
262 fprintf(h, "%s", t->name);
267 void write_type_right(FILE *h, type_t *t, int is_field)
269 if (!h) return;
271 if (t->declarray) {
272 if (is_conformant_array(t)) {
273 fprintf(h, "[%s]", is_field ? "1" : "");
274 t = type_array_get_element(t);
276 for ( ; t->declarray; t = type_array_get_element(t))
277 fprintf(h, "[%lu]", type_array_get_dim(t));
281 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
282 const char *fmt, va_list args)
284 type_t *pt;
285 int ptr_level = 0;
287 if (!h) return;
289 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
292 if (pt->type == RPC_FC_FUNCTION) {
293 int i;
294 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
295 if (!callconv) callconv = "";
296 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
297 write_type_left(h, type_function_get_rettype(pt), declonly);
298 fputc(' ', h);
299 if (ptr_level) fputc('(', h);
300 fprintf(h, "%s ", callconv);
301 for (i = 0; i < ptr_level; i++)
302 fputc('*', h);
303 } else
304 write_type_left(h, t, declonly);
305 if (fmt) {
306 if (needs_space_after(t))
307 fputc(' ', h);
308 vfprintf(h, fmt, args);
310 if (pt->type == RPC_FC_FUNCTION) {
311 if (ptr_level) fputc(')', h);
312 fputc('(', h);
313 write_args(h, type_function_get_args(pt), NULL, 0, FALSE);
314 fputc(')', h);
315 } else
316 write_type_right(h, t, is_field);
319 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
321 va_list args;
322 va_start(args, fmt);
323 write_type_v(f, t, field, FALSE, fmt, args);
324 va_end(args);
327 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
329 va_list args;
330 va_start(args, fmt);
331 write_type_v(f, t, FALSE, TRUE, fmt, args);
332 va_end(args);
335 void write_type_decl_left(FILE *f, type_t *t)
337 write_type_left(f, t, TRUE);
340 static int user_type_registered(const char *name)
342 user_type_t *ut;
343 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
344 if (!strcmp(name, ut->name))
345 return 1;
346 return 0;
349 static int context_handle_registered(const char *name)
351 context_handle_t *ch;
352 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
353 if (!strcmp(name, ch->name))
354 return 1;
355 return 0;
358 static int generic_handle_registered(const char *name)
360 generic_handle_t *gh;
361 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
362 if (!strcmp(name, gh->name))
363 return 1;
364 return 0;
367 /* check for types which require additional prototypes to be generated in the
368 * header */
369 void check_for_additional_prototype_types(const var_list_t *list)
371 const var_t *v;
373 if (!list) return;
374 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
376 type_t *type = v->type;
377 if (!type) continue;
378 for (;;) {
379 const char *name = type->name;
380 if (type->user_types_registered) break;
381 type->user_types_registered = 1;
382 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
383 if (!context_handle_registered(name))
385 context_handle_t *ch = xmalloc(sizeof(*ch));
386 ch->name = xstrdup(name);
387 list_add_tail(&context_handle_list, &ch->entry);
389 /* don't carry on parsing fields within this type */
390 break;
392 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
393 if (!generic_handle_registered(name))
395 generic_handle_t *gh = xmalloc(sizeof(*gh));
396 gh->name = xstrdup(name);
397 list_add_tail(&generic_handle_list, &gh->entry);
399 /* don't carry on parsing fields within this type */
400 break;
402 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
403 if (!user_type_registered(name))
405 user_type_t *ut = xmalloc(sizeof *ut);
406 ut->name = xstrdup(name);
407 list_add_tail(&user_type_list, &ut->entry);
409 /* don't carry on parsing fields within this type as we are already
410 * using a wire marshaled type */
411 break;
413 else if (type_is_complete(type))
415 var_list_t *vars = NULL;
416 if (type->type == RPC_FC_ENUM16 || type->type == RPC_FC_ENUM32)
417 vars = type_enum_get_values(type);
418 else if (is_struct(type->type))
419 vars = type_struct_get_fields(type);
420 else if (is_union(type->type))
421 vars = type_union_get_cases(type);
422 check_for_additional_prototype_types(vars);
425 if (type_is_alias(type))
426 type = type_alias_get_aliasee(type);
427 else if (is_ptr(type))
428 type = type_pointer_get_ref(type);
429 else if (is_array(type))
430 type = type_array_get_element(type);
431 else
432 break;
437 static void write_user_types(FILE *header)
439 user_type_t *ut;
440 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
442 const char *name = ut->name;
443 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
444 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
445 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
446 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
450 static void write_context_handle_rundowns(FILE *header)
452 context_handle_t *ch;
453 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
455 const char *name = ch->name;
456 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
460 static void write_generic_handle_routines(FILE *header)
462 generic_handle_t *gh;
463 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
465 const char *name = gh->name;
466 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
467 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
471 static void write_typedef(FILE *header, type_t *type)
473 fprintf(header, "typedef ");
474 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, "%s", type->name);
475 fprintf(header, ";\n");
478 int is_const_decl(const var_t *var)
480 const type_t *t;
481 /* strangely, MIDL accepts a const attribute on any pointer in the
482 * declaration to mean that data isn't being instantiated. this appears
483 * to be a bug, but there is no benefit to being incompatible with MIDL,
484 * so we'll do the same thing */
485 for (t = var->type; ; )
487 if (is_attr(t->attrs, ATTR_CONST))
488 return TRUE;
489 else if (is_ptr(t))
490 t = type_pointer_get_ref(t);
491 else break;
493 return FALSE;
496 static void write_declaration(FILE *header, const var_t *v)
498 if (is_const_decl(v) && v->eval)
500 fprintf(header, "#define %s (", v->name);
501 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
502 fprintf(header, ")\n\n");
504 else
506 switch (v->stgclass)
508 case STG_NONE:
509 case STG_REGISTER: /* ignored */
510 break;
511 case STG_STATIC:
512 fprintf(header, "static ");
513 break;
514 case STG_EXTERN:
515 fprintf(header, "extern ");
516 break;
518 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
519 fprintf(header, ";\n\n");
523 static void write_library(FILE *header, const typelib_t *typelib)
525 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
526 fprintf(header, "\n");
527 write_guid(header, "LIBID", typelib->name, uuid);
528 fprintf(header, "\n");
532 const var_t* get_explicit_handle_var(const var_t *func)
534 const var_t* var;
536 if (!type_get_function_args(func->type))
537 return NULL;
539 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
540 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
541 return var;
543 return NULL;
546 const type_t* get_explicit_generic_handle_type(const var_t* var)
548 const type_t *t;
549 for (t = var->type; is_ptr(t); t = type_pointer_get_ref(t))
550 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
551 return t;
552 return NULL;
555 const var_t* get_explicit_generic_handle_var(const var_t *func)
557 const var_t* var;
559 if (!type_get_function_args(func->type))
560 return NULL;
562 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
563 if (get_explicit_generic_handle_type(var))
564 return var;
566 return NULL;
569 const var_t* get_context_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 )
577 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
578 return var;
580 return NULL;
583 int has_out_arg_or_return(const var_t *func)
585 const var_t *var;
587 if (!is_void(type_function_get_rettype(func->type)))
588 return 1;
590 if (!type_get_function_args(func->type))
591 return 0;
593 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
594 if (is_attr(var->attrs, ATTR_OUT))
595 return 1;
597 return 0;
601 /********** INTERFACES **********/
603 int is_object(const attr_list_t *list)
605 const attr_t *attr;
606 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
607 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
608 return 0;
611 int is_local(const attr_list_t *a)
613 return is_attr(a, ATTR_LOCAL);
616 const var_t *is_callas(const attr_list_t *a)
618 return get_attrp(a, ATTR_CALLAS);
621 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
623 const statement_t *stmt;
624 int first_iface = 1;
626 if (type_iface_get_inherit(iface))
627 write_method_macro(header, type_iface_get_inherit(iface), name);
629 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
631 const var_t *func = stmt->u.var;
633 if (first_iface)
635 fprintf(header, "/*** %s methods ***/\n", iface->name);
636 first_iface = 0;
639 if (!is_callas(func->attrs)) {
640 const var_t *arg;
642 fprintf(header, "#define %s_%s(This", name, get_name(func));
643 if (type_get_function_args(func->type))
644 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
645 fprintf(header, ",%s", arg->name);
646 fprintf(header, ") ");
648 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
649 if (type_get_function_args(func->type))
650 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
651 fprintf(header, ",%s", arg->name);
652 fprintf(header, ")\n");
657 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
659 const var_t *arg;
660 int count = 0;
662 if (do_indent)
664 indentation++;
665 indent(h, 0);
667 if (method == 1) {
668 fprintf(h, "%s* This", name);
669 count++;
671 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
672 if (count) {
673 if (do_indent)
675 fprintf(h, ",\n");
676 indent(h, 0);
678 else fprintf(h, ",");
680 write_type_decl(h, arg->type, "%s", arg->name);
681 count++;
683 if (do_indent) indentation--;
686 static void write_cpp_method_def(FILE *header, const type_t *iface)
688 const statement_t *stmt;
690 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
692 const var_t *func = stmt->u.var;
693 if (!is_callas(func->attrs)) {
694 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
695 if (!callconv) callconv = "";
696 indent(header, 0);
697 fprintf(header, "virtual ");
698 write_type_decl_left(header, type_function_get_rettype(func->type));
699 fprintf(header, " %s %s(\n", callconv, get_name(func));
700 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
701 fprintf(header, ") = 0;\n");
702 fprintf(header, "\n");
707 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
709 const statement_t *stmt;
710 int first_iface = 1;
712 if (type_iface_get_inherit(iface))
713 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
715 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
717 const var_t *func = stmt->u.var;
718 if (first_iface) {
719 indent(header, 0);
720 fprintf(header, "/*** %s methods ***/\n", iface->name);
721 first_iface = 0;
723 if (!is_callas(func->attrs)) {
724 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
725 if (!callconv) callconv = "";
726 indent(header, 0);
727 write_type_decl_left(header, type_function_get_rettype(func->type));
728 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
729 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
730 fprintf(header, ");\n");
731 fprintf(header, "\n");
736 static void write_c_method_def(FILE *header, const type_t *iface)
738 do_write_c_method_def(header, iface, iface->name);
741 static void write_c_disp_method_def(FILE *header, const type_t *iface)
743 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
746 static void write_method_proto(FILE *header, const type_t *iface)
748 const statement_t *stmt;
750 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
752 const var_t *func = stmt->u.var;
754 if (!is_local(func->attrs)) {
755 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
756 if (!callconv) callconv = "";
757 /* proxy prototype */
758 write_type_decl_left(header, type_function_get_rettype(func->type));
759 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
760 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
761 fprintf(header, ");\n");
762 /* stub prototype */
763 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
764 fprintf(header, " IRpcStubBuffer* This,\n");
765 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
766 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
767 fprintf(header, " DWORD* pdwStubPhase);\n");
772 static void write_locals(FILE *fp, const type_t *iface, int body)
774 static const char comment[]
775 = "/* WIDL-generated stub. You must provide an implementation for this. */";
776 const statement_t *stmt;
778 if (!is_object(iface->attrs))
779 return;
781 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
782 const var_t *func = stmt->u.var;
783 const var_t *cas = is_callas(func->attrs);
785 if (cas) {
786 const statement_t *stmt2 = NULL;
787 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
788 if (!strcmp(stmt2->u.var->name, cas->name))
789 break;
790 if (&stmt2->entry != type_iface_get_stmts(iface)) {
791 const var_t *m = stmt2->u.var;
792 /* proxy prototype - use local prototype */
793 write_type_decl_left(fp, type_function_get_rettype(m->type));
794 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
795 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
796 fprintf(fp, ")");
797 if (body) {
798 type_t *rt = type_function_get_rettype(m->type);
799 fprintf(fp, "\n{\n");
800 fprintf(fp, " %s\n", comment);
801 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
802 fprintf(fp, " return E_NOTIMPL;\n");
803 else if (rt->type) {
804 fprintf(fp, " ");
805 write_type_decl(fp, rt, "rv");
806 fprintf(fp, ";\n");
807 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
808 fprintf(fp, " return rv;\n");
810 fprintf(fp, "}\n\n");
812 else
813 fprintf(fp, ";\n");
814 /* stub prototype - use remotable prototype */
815 write_type_decl_left(fp, type_function_get_rettype(func->type));
816 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
817 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
818 fprintf(fp, ")");
819 if (body)
820 /* Remotable methods must all return HRESULTs. */
821 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
822 else
823 fprintf(fp, ";\n");
825 else
826 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
831 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
833 const statement_t *stmt;
834 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
836 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
837 write_locals(local_stubs, stmt->u.type, TRUE);
838 else if (stmt->type == STMT_LIBRARY)
839 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
843 void write_local_stubs(const statement_list_t *stmts)
845 FILE *local_stubs;
847 if (!local_stubs_name) return;
849 local_stubs = fopen(local_stubs_name, "w");
850 if (!local_stubs) {
851 error("Could not open %s for output\n", local_stubs_name);
852 return;
854 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
855 fprintf(local_stubs, "#include <objbase.h>\n");
856 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
858 write_local_stubs_stmts(local_stubs, stmts);
860 fclose(local_stubs);
863 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
865 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
867 /* FIXME: do we need to handle call_as? */
868 write_type_decl_left(header, type_function_get_rettype(fun->type));
869 fprintf(header, " ");
870 if (callconv) fprintf(header, "%s ", callconv);
871 fprintf(header, "%s%s(\n", prefix, get_name(fun));
872 if (type_get_function_args(fun->type))
873 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
874 else
875 fprintf(header, " void");
876 fprintf(header, ");\n\n");
879 static void write_forward(FILE *header, type_t *iface)
881 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
882 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
883 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
884 fprintf(header, "#endif\n\n" );
887 static void write_iface_guid(FILE *header, const type_t *iface)
889 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
890 write_guid(header, "IID", iface->name, uuid);
893 static void write_dispiface_guid(FILE *header, const type_t *iface)
895 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
896 write_guid(header, "DIID", iface->name, uuid);
899 static void write_coclass_guid(FILE *header, const type_t *cocl)
901 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
902 write_guid(header, "CLSID", cocl->name, uuid);
905 static void write_com_interface_start(FILE *header, const type_t *iface)
907 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
908 fprintf(header, "/*****************************************************************************\n");
909 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
910 fprintf(header, " */\n");
911 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
912 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
915 static void write_com_interface_end(FILE *header, type_t *iface)
917 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
918 if (dispinterface)
919 write_dispiface_guid(header, iface);
920 else
921 write_iface_guid(header, iface);
922 /* C++ interface */
923 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
924 if (type_iface_get_inherit(iface))
926 fprintf(header, "interface %s : public %s\n", iface->name,
927 type_iface_get_inherit(iface)->name);
928 fprintf(header, "{\n");
930 else
932 fprintf(header, "interface %s\n", iface->name);
933 fprintf(header, "{\n");
934 fprintf(header, " BEGIN_INTERFACE\n");
935 fprintf(header, "\n");
937 /* dispinterfaces don't have real functions, so don't write C++ functions for
938 * them */
939 if (!dispinterface)
941 indentation++;
942 write_cpp_method_def(header, iface);
943 indentation--;
945 if (!type_iface_get_inherit(iface))
946 fprintf(header, " END_INTERFACE\n");
947 fprintf(header, "};\n");
948 fprintf(header, "#else\n");
949 /* C interface */
950 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
951 indentation++;
952 fprintf(header, " BEGIN_INTERFACE\n");
953 fprintf(header, "\n");
954 if (dispinterface)
955 write_c_disp_method_def(header, iface);
956 else
957 write_c_method_def(header, iface);
958 indentation--;
959 fprintf(header, " END_INTERFACE\n");
960 fprintf(header, "} %sVtbl;\n", iface->name);
961 fprintf(header, "interface %s {\n", iface->name);
962 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
963 fprintf(header, "};\n");
964 fprintf(header, "\n");
965 fprintf(header, "#ifdef COBJMACROS\n");
966 /* dispinterfaces don't have real functions, so don't write macros for them,
967 * only for the interface this interface inherits from, i.e. IDispatch */
968 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
969 fprintf(header, "#endif\n");
970 fprintf(header, "\n");
971 fprintf(header, "#endif\n");
972 fprintf(header, "\n");
973 /* dispinterfaces don't have real functions, so don't write prototypes for
974 * them */
975 if (!dispinterface)
977 write_method_proto(header, iface);
978 write_locals(header, iface, FALSE);
979 fprintf(header, "\n");
981 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
984 static void write_rpc_interface_start(FILE *header, const type_t *iface)
986 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
987 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
988 static int allocate_written = 0;
990 if (!allocate_written)
992 allocate_written = 1;
993 fprintf(header, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
994 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
997 fprintf(header, "/*****************************************************************************\n");
998 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
999 fprintf(header, " */\n");
1000 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1001 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1002 if (var) fprintf(header, "extern handle_t %s;\n", var);
1003 if (old_names)
1005 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1006 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1008 else
1010 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1011 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1012 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1013 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1017 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1019 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1022 static void write_coclass(FILE *header, type_t *cocl)
1024 fprintf(header, "/*****************************************************************************\n");
1025 fprintf(header, " * %s coclass\n", cocl->name);
1026 fprintf(header, " */\n\n");
1027 write_coclass_guid(header, cocl);
1028 fprintf(header, "\n");
1031 static void write_coclass_forward(FILE *header, type_t *cocl)
1033 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1034 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1035 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1036 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1039 static void write_import(FILE *header, const char *fname)
1041 char *hname, *p;
1043 hname = dup_basename(fname, ".idl");
1044 p = hname + strlen(hname) - 2;
1045 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1047 fprintf(header, "#include <%s>\n", hname);
1048 free(hname);
1051 static void write_imports(FILE *header, const statement_list_t *stmts)
1053 const statement_t *stmt;
1054 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1056 switch (stmt->type)
1058 case STMT_TYPE:
1059 if (stmt->u.type->type == RPC_FC_IP)
1060 write_imports(header, type_iface_get_stmts(stmt->u.type));
1061 break;
1062 case STMT_TYPEREF:
1063 case STMT_IMPORTLIB:
1064 /* not included in header */
1065 break;
1066 case STMT_IMPORT:
1067 write_import(header, stmt->u.str);
1068 break;
1069 case STMT_TYPEDEF:
1070 case STMT_MODULE:
1071 case STMT_CPPQUOTE:
1072 case STMT_DECLARATION:
1073 /* not processed here */
1074 break;
1075 case STMT_LIBRARY:
1076 write_imports(header, stmt->u.lib->stmts);
1077 break;
1082 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1084 const statement_t *stmt;
1085 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1087 switch (stmt->type)
1089 case STMT_TYPE:
1090 if (stmt->u.type->type == RPC_FC_IP)
1092 if (is_object(stmt->u.type->attrs) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1093 write_forward(header, stmt->u.type);
1095 else if (stmt->u.type->type == RPC_FC_COCLASS)
1096 write_coclass_forward(header, stmt->u.type);
1097 break;
1098 case STMT_TYPEREF:
1099 case STMT_IMPORTLIB:
1100 /* not included in header */
1101 break;
1102 case STMT_IMPORT:
1103 case STMT_TYPEDEF:
1104 case STMT_MODULE:
1105 case STMT_CPPQUOTE:
1106 case STMT_DECLARATION:
1107 /* not processed here */
1108 break;
1109 case STMT_LIBRARY:
1110 write_forward_decls(header, stmt->u.lib->stmts);
1111 break;
1116 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1118 const statement_t *stmt;
1119 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1121 switch (stmt->type)
1123 case STMT_TYPE:
1124 if (stmt->u.type->type == RPC_FC_IP)
1126 type_t *iface = stmt->u.type;
1127 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type->attrs))
1129 write_com_interface_start(header, iface);
1130 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1131 write_com_interface_end(header, iface);
1133 else
1135 write_rpc_interface_start(header, iface);
1136 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1137 write_rpc_interface_end(header, iface);
1140 else if (stmt->u.type->type == RPC_FC_COCLASS)
1141 write_coclass(header, stmt->u.type);
1142 else
1144 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1145 fprintf(header, ";\n\n");
1147 break;
1148 case STMT_TYPEREF:
1149 /* FIXME: shouldn't write out forward declarations for undefined
1150 * interfaces but a number of our IDL files depend on this */
1151 if (stmt->u.type->type == RPC_FC_IP && !stmt->u.type->written)
1152 write_forward(header, stmt->u.type);
1153 break;
1154 case STMT_IMPORTLIB:
1155 case STMT_MODULE:
1156 /* not included in header */
1157 break;
1158 case STMT_IMPORT:
1159 /* not processed here */
1160 break;
1161 case STMT_TYPEDEF:
1163 const type_list_t *type_entry = stmt->u.type_list;
1164 for (; type_entry; type_entry = type_entry->next)
1165 write_typedef(header, type_entry->type);
1166 break;
1168 case STMT_LIBRARY:
1169 write_library(header, stmt->u.lib);
1170 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1171 break;
1172 case STMT_CPPQUOTE:
1173 fprintf(header, "%s\n", stmt->u.str);
1174 break;
1175 case STMT_DECLARATION:
1176 if (iface && stmt->u.var->type->type == RPC_FC_FUNCTION)
1178 if (!ignore_funcs)
1180 int prefixes_differ = strcmp(prefix_client, prefix_server);
1182 if (prefixes_differ)
1184 fprintf(header, "/* client prototype */\n");
1185 write_function_proto(header, iface, stmt->u.var, prefix_client);
1186 fprintf(header, "/* server prototype */\n");
1188 write_function_proto(header, iface, stmt->u.var, prefix_server);
1191 else
1192 write_declaration(header, stmt->u.var);
1193 break;
1198 void write_header(const statement_list_t *stmts)
1200 FILE *header;
1202 if (!do_header) return;
1204 if(!(header = fopen(header_name, "w"))) {
1205 error("Could not open %s for output\n", header_name);
1206 return;
1208 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1209 fprintf(header, "#include <rpc.h>\n" );
1210 fprintf(header, "#include <rpcndr.h>\n\n" );
1212 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1213 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1214 start_cplusplus_guard(header);
1216 fprintf(header, "/* Headers for imported files */\n\n");
1217 write_imports(header, stmts);
1218 fprintf(header, "\n");
1220 /* FIXME: should be before imported file includes */
1221 fprintf(header, "/* Forward declarations */\n\n");
1222 write_forward_decls(header, stmts);
1223 fprintf(header, "\n");
1225 write_header_stmts(header, stmts, NULL, FALSE);
1227 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1228 fprintf(header, "\n");
1229 write_user_types(header);
1230 write_generic_handle_routines(header);
1231 write_context_handle_rundowns(header);
1232 fprintf(header, "\n");
1233 fprintf(header, "/* End additional prototypes */\n");
1234 fprintf(header, "\n");
1236 end_cplusplus_guard(header);
1237 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1239 fclose(header);