widl: Add a new function, type_pointer_get_ref.
[wine/multimedia.git] / tools / widl / header.c
blob7c784fad66a257d0a1460a932d728fe0e6f3e8bf
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->orig;
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 = t->orig;
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 if (!t->type && !t->ref) return 1;
114 return 0;
117 int is_conformant_array(const type_t *t)
119 return t->type == RPC_FC_CARRAY
120 || t->type == RPC_FC_CVARRAY
121 || (t->type == RPC_FC_BOGUS_ARRAY && type_array_has_conformance(t));
124 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
126 if (!uuid) return;
127 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
128 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
129 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
130 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
131 uuid->Data4[6], uuid->Data4[7]);
134 const char *get_name(const var_t *v)
136 static char buffer[256];
138 if (is_attr( v->attrs, ATTR_PROPGET ))
139 strcpy( buffer, "get_" );
140 else if (is_attr( v->attrs, ATTR_PROPPUT ))
141 strcpy( buffer, "put_" );
142 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
143 strcpy( buffer, "putref_" );
144 else
145 buffer[0] = 0;
146 strcat( buffer, v->name );
147 return buffer;
150 static void write_field(FILE *h, var_t *v)
152 if (!v) return;
153 if (v->type) {
154 indent(h, 0);
155 write_type_def_or_decl(h, v->type, TRUE, "%s", v->name);
156 fprintf(h, ";\n");
160 static void write_fields(FILE *h, var_list_t *fields)
162 var_t *v;
163 if (!fields) return;
164 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
167 static void write_enums(FILE *h, var_list_t *enums)
169 var_t *v;
170 if (!enums) return;
171 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
173 if (v->name) {
174 indent(h, 0);
175 fprintf(h, "%s", get_name(v));
176 if (v->eval) {
177 fprintf(h, " = ");
178 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
181 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
183 fprintf(h, "\n");
186 int needs_space_after(type_t *t)
188 return (type_is_alias(t) ||
189 (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
192 void write_type_left(FILE *h, type_t *t, int declonly)
194 if (!h) return;
196 if (is_attr(t->attrs, ATTR_CONST) &&
197 (type_is_alias(t) || t->declarray || !is_ptr(t)))
198 fprintf(h, "const ");
200 if (type_is_alias(t)) fprintf(h, "%s", t->name);
201 else if (t->declarray) write_type_left(h, type_array_get_element(t), declonly);
202 else {
203 if (t->sign > 0) fprintf(h, "signed ");
204 else if (t->sign < 0) fprintf(h, "unsigned ");
205 switch (t->type) {
206 case RPC_FC_ENUM16:
207 case RPC_FC_ENUM32:
208 if (!declonly && t->defined && !t->written) {
209 if (t->name) fprintf(h, "enum %s {\n", t->name);
210 else fprintf(h, "enum {\n");
211 t->written = TRUE;
212 indentation++;
213 write_enums(h, type_enum_get_values(t));
214 indent(h, -1);
215 fprintf(h, "}");
217 else fprintf(h, "enum %s", t->name ? t->name : "");
218 break;
219 case RPC_FC_STRUCT:
220 case RPC_FC_CVSTRUCT:
221 case RPC_FC_CPSTRUCT:
222 case RPC_FC_CSTRUCT:
223 case RPC_FC_PSTRUCT:
224 case RPC_FC_BOGUS_STRUCT:
225 case RPC_FC_ENCAPSULATED_UNION:
226 if (!declonly && t->defined && !t->written) {
227 if (t->name) fprintf(h, "struct %s {\n", t->name);
228 else fprintf(h, "struct {\n");
229 t->written = TRUE;
230 indentation++;
231 if (t->type == RPC_FC_ENCAPSULATED_UNION)
232 write_fields(h, type_encapsulated_union_get_fields(t));
233 else
234 write_fields(h, type_struct_get_fields(t));
235 indent(h, -1);
236 fprintf(h, "}");
238 else fprintf(h, "struct %s", t->name ? t->name : "");
239 break;
240 case RPC_FC_NON_ENCAPSULATED_UNION:
241 if (!declonly && t->defined && !t->written) {
242 if (t->name) fprintf(h, "union %s {\n", t->name);
243 else fprintf(h, "union {\n");
244 t->written = TRUE;
245 indentation++;
246 write_fields(h, type_union_get_cases(t));
247 indent(h, -1);
248 fprintf(h, "}");
250 else fprintf(h, "union %s", t->name ? t->name : "");
251 break;
252 case RPC_FC_RP:
253 case RPC_FC_UP:
254 case RPC_FC_FP:
255 case RPC_FC_OP:
256 write_type_left(h, type_pointer_get_ref(t), declonly);
257 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
258 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
259 break;
260 case RPC_FC_CARRAY:
261 case RPC_FC_CVARRAY:
262 case RPC_FC_BOGUS_ARRAY:
263 write_type_left(h, type_array_get_element(t), declonly);
264 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
265 break;
266 default:
267 fprintf(h, "%s", t->name);
272 void write_type_right(FILE *h, type_t *t, int is_field)
274 if (!h) return;
276 if (t->declarray) {
277 if (is_conformant_array(t)) {
278 fprintf(h, "[%s]", is_field ? "1" : "");
279 t = type_array_get_element(t);
281 for ( ; t->declarray; t = type_array_get_element(t))
282 fprintf(h, "[%lu]", type_array_get_dim(t));
286 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
287 const char *fmt, va_list args)
289 type_t *pt;
290 int ptr_level = 0;
292 if (!h) return;
294 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
297 if (pt->type == RPC_FC_FUNCTION) {
298 int i;
299 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
300 if (!callconv) callconv = "";
301 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
302 write_type_left(h, pt->ref, declonly);
303 fputc(' ', h);
304 if (ptr_level) fputc('(', h);
305 fprintf(h, "%s ", callconv);
306 for (i = 0; i < ptr_level; i++)
307 fputc('*', h);
308 } else
309 write_type_left(h, t, declonly);
310 if (fmt) {
311 if (needs_space_after(t))
312 fputc(' ', h);
313 vfprintf(h, fmt, args);
315 if (pt->type == RPC_FC_FUNCTION) {
316 if (ptr_level) fputc(')', h);
317 fputc('(', h);
318 write_args(h, type_function_get_args(pt), NULL, 0, FALSE);
319 fputc(')', h);
320 } else
321 write_type_right(h, t, is_field);
324 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
326 va_list args;
327 va_start(args, fmt);
328 write_type_v(f, t, field, FALSE, fmt, args);
329 va_end(args);
332 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
334 va_list args;
335 va_start(args, fmt);
336 write_type_v(f, t, FALSE, TRUE, fmt, args);
337 va_end(args);
340 void write_type_decl_left(FILE *f, type_t *t)
342 write_type_left(f, t, TRUE);
345 static int user_type_registered(const char *name)
347 user_type_t *ut;
348 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
349 if (!strcmp(name, ut->name))
350 return 1;
351 return 0;
354 static int context_handle_registered(const char *name)
356 context_handle_t *ch;
357 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
358 if (!strcmp(name, ch->name))
359 return 1;
360 return 0;
363 static int generic_handle_registered(const char *name)
365 generic_handle_t *gh;
366 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
367 if (!strcmp(name, gh->name))
368 return 1;
369 return 0;
372 /* check for types which require additional prototypes to be generated in the
373 * header */
374 void check_for_additional_prototype_types(const var_list_t *list)
376 const var_t *v;
378 if (!list) return;
379 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
381 type_t *type = v->type;
382 if (!type) continue;
383 for (;;) {
384 const char *name = type->name;
385 if (type->user_types_registered) break;
386 type->user_types_registered = 1;
387 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
388 if (!context_handle_registered(name))
390 context_handle_t *ch = xmalloc(sizeof(*ch));
391 ch->name = xstrdup(name);
392 list_add_tail(&context_handle_list, &ch->entry);
394 /* don't carry on parsing fields within this type */
395 break;
397 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
398 if (!generic_handle_registered(name))
400 generic_handle_t *gh = xmalloc(sizeof(*gh));
401 gh->name = xstrdup(name);
402 list_add_tail(&generic_handle_list, &gh->entry);
404 /* don't carry on parsing fields within this type */
405 break;
407 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
408 if (!user_type_registered(name))
410 user_type_t *ut = xmalloc(sizeof *ut);
411 ut->name = xstrdup(name);
412 list_add_tail(&user_type_list, &ut->entry);
414 /* don't carry on parsing fields within this type as we are already
415 * using a wire marshaled type */
416 break;
418 else if (type_is_complete(type))
420 var_list_t *vars = NULL;
421 if (type->type == RPC_FC_ENUM16 || type->type == RPC_FC_ENUM32)
422 vars = type_enum_get_values(type);
423 else if (is_struct(type->type))
424 vars = type_struct_get_fields(type);
425 else if (is_union(type->type))
426 vars = type_union_get_cases(type);
427 check_for_additional_prototype_types(vars);
430 if (type_is_alias(type))
431 type = type->orig;
432 else if (is_ptr(type))
433 type = type_pointer_get_ref(type);
434 else if (is_array(type))
435 type = type_array_get_element(type);
436 else
437 break;
442 static void write_user_types(FILE *header)
444 user_type_t *ut;
445 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
447 const char *name = ut->name;
448 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
449 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
450 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
451 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
455 static void write_context_handle_rundowns(FILE *header)
457 context_handle_t *ch;
458 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
460 const char *name = ch->name;
461 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
465 static void write_generic_handle_routines(FILE *header)
467 generic_handle_t *gh;
468 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
470 const char *name = gh->name;
471 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
472 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
476 static void write_typedef(FILE *header, type_t *type)
478 fprintf(header, "typedef ");
479 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
480 fprintf(header, ";\n");
483 int is_const_decl(const var_t *var)
485 const type_t *t;
486 /* strangely, MIDL accepts a const attribute on any pointer in the
487 * declaration to mean that data isn't being instantiated. this appears
488 * to be a bug, but there is no benefit to being incompatible with MIDL,
489 * so we'll do the same thing */
490 for (t = var->type; ; )
492 if (is_attr(t->attrs, ATTR_CONST))
493 return TRUE;
494 else if (is_ptr(t))
495 t = type_pointer_get_ref(t);
496 else break;
498 return FALSE;
501 static void write_declaration(FILE *header, const var_t *v)
503 if (is_const_decl(v) && v->eval)
505 fprintf(header, "#define %s (", v->name);
506 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
507 fprintf(header, ")\n\n");
509 else
511 switch (v->stgclass)
513 case STG_NONE:
514 case STG_REGISTER: /* ignored */
515 break;
516 case STG_STATIC:
517 fprintf(header, "static ");
518 break;
519 case STG_EXTERN:
520 fprintf(header, "extern ");
521 break;
523 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
524 fprintf(header, ";\n\n");
528 static void write_library(FILE *header, const typelib_t *typelib)
530 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
531 fprintf(header, "\n");
532 write_guid(header, "LIBID", typelib->name, uuid);
533 fprintf(header, "\n");
537 const var_t* get_explicit_handle_var(const var_t *func)
539 const var_t* var;
541 if (!type_get_function_args(func->type))
542 return NULL;
544 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
545 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
546 return var;
548 return NULL;
551 const type_t* get_explicit_generic_handle_type(const var_t* var)
553 const type_t *t;
554 for (t = var->type; is_ptr(t); t = type_pointer_get_ref(t))
555 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
556 return t;
557 return NULL;
560 const var_t* get_explicit_generic_handle_var(const var_t *func)
562 const var_t* var;
564 if (!type_get_function_args(func->type))
565 return NULL;
567 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
568 if (get_explicit_generic_handle_type(var))
569 return var;
571 return NULL;
574 const var_t* get_context_handle_var(const var_t *func)
576 const var_t* var;
578 if (!type_get_function_args(func->type))
579 return NULL;
581 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
582 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
583 return var;
585 return NULL;
588 int has_out_arg_or_return(const var_t *func)
590 const var_t *var;
592 if (!is_void(get_func_return_type(func)))
593 return 1;
595 if (!type_get_function_args(func->type))
596 return 0;
598 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
599 if (is_attr(var->attrs, ATTR_OUT))
600 return 1;
602 return 0;
606 /********** INTERFACES **********/
608 int is_object(const attr_list_t *list)
610 const attr_t *attr;
611 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
612 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
613 return 0;
616 int is_local(const attr_list_t *a)
618 return is_attr(a, ATTR_LOCAL);
621 const var_t *is_callas(const attr_list_t *a)
623 return get_attrp(a, ATTR_CALLAS);
626 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
628 const statement_t *stmt;
629 int first_iface = 1;
631 if (iface->ref) write_method_macro(header, iface->ref, name);
633 STATEMENTS_FOR_EACH_FUNC(stmt, iface->details.iface->stmts)
635 const var_t *func = stmt->u.var;
637 if (first_iface)
639 fprintf(header, "/*** %s methods ***/\n", iface->name);
640 first_iface = 0;
643 if (!is_callas(func->attrs)) {
644 const var_t *arg;
646 fprintf(header, "#define %s_%s(This", name, get_name(func));
647 if (type_get_function_args(func->type))
648 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
649 fprintf(header, ",%s", arg->name);
650 fprintf(header, ") ");
652 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
653 if (type_get_function_args(func->type))
654 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
655 fprintf(header, ",%s", arg->name);
656 fprintf(header, ")\n");
661 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
663 const var_t *arg;
664 int count = 0;
666 if (do_indent)
668 indentation++;
669 indent(h, 0);
671 if (method == 1) {
672 fprintf(h, "%s* This", name);
673 count++;
675 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
676 if (count) {
677 if (do_indent)
679 fprintf(h, ",\n");
680 indent(h, 0);
682 else fprintf(h, ",");
684 write_type_decl(h, arg->type, "%s", arg->name);
685 count++;
687 if (do_indent) indentation--;
690 static void write_cpp_method_def(FILE *header, const type_t *iface)
692 const statement_t *stmt;
694 STATEMENTS_FOR_EACH_FUNC(stmt, iface->details.iface->stmts)
696 const var_t *func = stmt->u.var;
697 if (!is_callas(func->attrs)) {
698 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
699 if (!callconv) callconv = "";
700 indent(header, 0);
701 fprintf(header, "virtual ");
702 write_type_decl_left(header, get_func_return_type(func));
703 fprintf(header, " %s %s(\n", callconv, get_name(func));
704 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
705 fprintf(header, ") = 0;\n");
706 fprintf(header, "\n");
711 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
713 const statement_t *stmt;
714 int first_iface = 1;
716 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
718 STATEMENTS_FOR_EACH_FUNC(stmt, iface->details.iface->stmts)
720 const var_t *func = stmt->u.var;
721 if (first_iface) {
722 indent(header, 0);
723 fprintf(header, "/*** %s methods ***/\n", iface->name);
724 first_iface = 0;
726 if (!is_callas(func->attrs)) {
727 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
728 if (!callconv) callconv = "";
729 indent(header, 0);
730 write_type_decl_left(header, get_func_return_type(func));
731 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
732 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
733 fprintf(header, ");\n");
734 fprintf(header, "\n");
739 static void write_c_method_def(FILE *header, const type_t *iface)
741 do_write_c_method_def(header, iface, iface->name);
744 static void write_c_disp_method_def(FILE *header, const type_t *iface)
746 do_write_c_method_def(header, iface->ref, iface->name);
749 static void write_method_proto(FILE *header, const type_t *iface)
751 const statement_t *stmt;
753 STATEMENTS_FOR_EACH_FUNC(stmt, iface->details.iface->stmts)
755 const var_t *func = stmt->u.var;
757 if (!is_local(func->attrs)) {
758 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
759 if (!callconv) callconv = "";
760 /* proxy prototype */
761 write_type_decl_left(header, get_func_return_type(func));
762 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
763 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
764 fprintf(header, ");\n");
765 /* stub prototype */
766 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
767 fprintf(header, " IRpcStubBuffer* This,\n");
768 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
769 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
770 fprintf(header, " DWORD* pdwStubPhase);\n");
775 static void write_locals(FILE *fp, const type_t *iface, int body)
777 static const char comment[]
778 = "/* WIDL-generated stub. You must provide an implementation for this. */";
779 const statement_t *stmt;
781 if (!is_object(iface->attrs))
782 return;
784 STATEMENTS_FOR_EACH_FUNC(stmt, iface->details.iface->stmts) {
785 const var_t *func = stmt->u.var;
786 const var_t *cas = is_callas(func->attrs);
788 if (cas) {
789 const statement_t *stmt2 = NULL;
790 STATEMENTS_FOR_EACH_FUNC(stmt2, iface->details.iface->stmts)
791 if (!strcmp(stmt2->u.var->name, cas->name))
792 break;
793 if (&stmt2->entry != iface->details.iface->stmts) {
794 const var_t *m = stmt2->u.var;
795 /* proxy prototype - use local prototype */
796 write_type_decl_left(fp, get_func_return_type(m));
797 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
798 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
799 fprintf(fp, ")");
800 if (body) {
801 type_t *rt = get_func_return_type(m);
802 fprintf(fp, "\n{\n");
803 fprintf(fp, " %s\n", comment);
804 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
805 fprintf(fp, " return E_NOTIMPL;\n");
806 else if (rt->type) {
807 fprintf(fp, " ");
808 write_type_decl(fp, rt, "rv");
809 fprintf(fp, ";\n");
810 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
811 fprintf(fp, " return rv;\n");
813 fprintf(fp, "}\n\n");
815 else
816 fprintf(fp, ";\n");
817 /* stub prototype - use remotable prototype */
818 write_type_decl_left(fp, get_func_return_type(func));
819 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
820 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
821 fprintf(fp, ")");
822 if (body)
823 /* Remotable methods must all return HRESULTs. */
824 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
825 else
826 fprintf(fp, ";\n");
828 else
829 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
834 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
836 const statement_t *stmt;
837 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
839 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
840 write_locals(local_stubs, stmt->u.type, TRUE);
841 else if (stmt->type == STMT_LIBRARY)
842 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
846 void write_local_stubs(const statement_list_t *stmts)
848 FILE *local_stubs;
850 if (!local_stubs_name) return;
852 local_stubs = fopen(local_stubs_name, "w");
853 if (!local_stubs) {
854 error("Could not open %s for output\n", local_stubs_name);
855 return;
857 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
858 fprintf(local_stubs, "#include <objbase.h>\n");
859 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
861 write_local_stubs_stmts(local_stubs, stmts);
863 fclose(local_stubs);
866 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
868 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
870 /* FIXME: do we need to handle call_as? */
871 write_type_decl_left(header, fun->type->ref);
872 fprintf(header, " ");
873 if (callconv) fprintf(header, "%s ", callconv);
874 fprintf(header, "%s%s(\n", prefix, get_name(fun));
875 if (type_get_function_args(fun->type))
876 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
877 else
878 fprintf(header, " void");
879 fprintf(header, ");\n\n");
882 static void write_forward(FILE *header, type_t *iface)
884 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
885 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
886 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
887 fprintf(header, "#endif\n\n" );
890 static void write_iface_guid(FILE *header, const type_t *iface)
892 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
893 write_guid(header, "IID", iface->name, uuid);
896 static void write_dispiface_guid(FILE *header, const type_t *iface)
898 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
899 write_guid(header, "DIID", iface->name, uuid);
902 static void write_coclass_guid(FILE *header, const type_t *cocl)
904 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
905 write_guid(header, "CLSID", cocl->name, uuid);
908 static void write_com_interface_start(FILE *header, const type_t *iface)
910 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
911 fprintf(header, "/*****************************************************************************\n");
912 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
913 fprintf(header, " */\n");
914 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
915 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
918 static void write_com_interface_end(FILE *header, type_t *iface)
920 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
921 if (dispinterface)
922 write_dispiface_guid(header, iface);
923 else
924 write_iface_guid(header, iface);
925 /* C++ interface */
926 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
927 if (iface->ref)
929 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
930 fprintf(header, "{\n");
932 else
934 fprintf(header, "interface %s\n", iface->name);
935 fprintf(header, "{\n");
936 fprintf(header, " BEGIN_INTERFACE\n");
937 fprintf(header, "\n");
939 /* dispinterfaces don't have real functions, so don't write C++ functions for
940 * them */
941 if (!dispinterface)
943 indentation++;
944 write_cpp_method_def(header, iface);
945 indentation--;
947 if (!iface->ref)
948 fprintf(header, " END_INTERFACE\n");
949 fprintf(header, "};\n");
950 fprintf(header, "#else\n");
951 /* C interface */
952 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
953 indentation++;
954 fprintf(header, " BEGIN_INTERFACE\n");
955 fprintf(header, "\n");
956 if (dispinterface)
957 write_c_disp_method_def(header, iface);
958 else
959 write_c_method_def(header, iface);
960 indentation--;
961 fprintf(header, " END_INTERFACE\n");
962 fprintf(header, "} %sVtbl;\n", iface->name);
963 fprintf(header, "interface %s {\n", iface->name);
964 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
965 fprintf(header, "};\n");
966 fprintf(header, "\n");
967 fprintf(header, "#ifdef COBJMACROS\n");
968 /* dispinterfaces don't have real functions, so don't write macros for them,
969 * only for the interface this interface inherits from, i.e. IDispatch */
970 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
971 fprintf(header, "#endif\n");
972 fprintf(header, "\n");
973 fprintf(header, "#endif\n");
974 fprintf(header, "\n");
975 /* dispinterfaces don't have real functions, so don't write prototypes for
976 * them */
977 if (!dispinterface)
979 write_method_proto(header, iface);
980 write_locals(header, iface, FALSE);
981 fprintf(header, "\n");
983 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
986 static void write_rpc_interface_start(FILE *header, const type_t *iface)
988 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
989 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
990 static int allocate_written = 0;
992 if (!allocate_written)
994 allocate_written = 1;
995 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
996 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
999 fprintf(header, "/*****************************************************************************\n");
1000 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1001 fprintf(header, " */\n");
1002 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1003 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1004 if (var) fprintf(header, "extern handle_t %s;\n", var);
1005 if (old_names)
1007 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1008 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1010 else
1012 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1013 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1014 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1015 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1019 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1021 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1024 static void write_coclass(FILE *header, type_t *cocl)
1026 fprintf(header, "/*****************************************************************************\n");
1027 fprintf(header, " * %s coclass\n", cocl->name);
1028 fprintf(header, " */\n\n");
1029 write_coclass_guid(header, cocl);
1030 fprintf(header, "\n");
1033 static void write_coclass_forward(FILE *header, type_t *cocl)
1035 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1036 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1037 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1038 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1041 static void write_import(FILE *header, const char *fname)
1043 char *hname, *p;
1045 hname = dup_basename(fname, ".idl");
1046 p = hname + strlen(hname) - 2;
1047 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1049 fprintf(header, "#include <%s>\n", hname);
1050 free(hname);
1053 static void write_imports(FILE *header, const statement_list_t *stmts)
1055 const statement_t *stmt;
1056 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1058 switch (stmt->type)
1060 case STMT_TYPE:
1061 if (stmt->u.type->type == RPC_FC_IP)
1062 write_imports(header, stmt->u.type->details.iface->stmts);
1063 break;
1064 case STMT_TYPEREF:
1065 case STMT_IMPORTLIB:
1066 /* not included in header */
1067 break;
1068 case STMT_IMPORT:
1069 write_import(header, stmt->u.str);
1070 break;
1071 case STMT_TYPEDEF:
1072 case STMT_MODULE:
1073 case STMT_CPPQUOTE:
1074 case STMT_DECLARATION:
1075 /* not processed here */
1076 break;
1077 case STMT_LIBRARY:
1078 write_imports(header, stmt->u.lib->stmts);
1079 break;
1084 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1086 const statement_t *stmt;
1087 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1089 switch (stmt->type)
1091 case STMT_TYPE:
1092 if (stmt->u.type->type == RPC_FC_IP)
1094 if (is_object(stmt->u.type->attrs) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1095 write_forward(header, stmt->u.type);
1097 else if (stmt->u.type->type == RPC_FC_COCLASS)
1098 write_coclass_forward(header, stmt->u.type);
1099 break;
1100 case STMT_TYPEREF:
1101 case STMT_IMPORTLIB:
1102 /* not included in header */
1103 break;
1104 case STMT_IMPORT:
1105 case STMT_TYPEDEF:
1106 case STMT_MODULE:
1107 case STMT_CPPQUOTE:
1108 case STMT_DECLARATION:
1109 /* not processed here */
1110 break;
1111 case STMT_LIBRARY:
1112 write_forward_decls(header, stmt->u.lib->stmts);
1113 break;
1118 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1120 const statement_t *stmt;
1121 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1123 switch (stmt->type)
1125 case STMT_TYPE:
1126 if (stmt->u.type->type == RPC_FC_IP)
1128 type_t *iface = stmt->u.type;
1129 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type->attrs))
1131 write_com_interface_start(header, iface);
1132 write_header_stmts(header, iface->details.iface->stmts, stmt->u.type, TRUE);
1133 write_com_interface_end(header, iface);
1135 else
1137 write_rpc_interface_start(header, iface);
1138 write_header_stmts(header, iface->details.iface->stmts, iface, FALSE);
1139 write_rpc_interface_end(header, iface);
1142 else if (stmt->u.type->type == RPC_FC_COCLASS)
1143 write_coclass(header, stmt->u.type);
1144 else
1146 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1147 fprintf(header, ";\n\n");
1149 break;
1150 case STMT_TYPEREF:
1151 /* FIXME: shouldn't write out forward declarations for undefined
1152 * interfaces but a number of our IDL files depend on this */
1153 if (stmt->u.type->type == RPC_FC_IP && !stmt->u.type->written)
1154 write_forward(header, stmt->u.type);
1155 break;
1156 case STMT_IMPORTLIB:
1157 case STMT_MODULE:
1158 /* not included in header */
1159 break;
1160 case STMT_IMPORT:
1161 /* not processed here */
1162 break;
1163 case STMT_TYPEDEF:
1165 const type_list_t *type_entry = stmt->u.type_list;
1166 for (; type_entry; type_entry = type_entry->next)
1167 write_typedef(header, type_entry->type);
1168 break;
1170 case STMT_LIBRARY:
1171 write_library(header, stmt->u.lib);
1172 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1173 break;
1174 case STMT_CPPQUOTE:
1175 fprintf(header, "%s\n", stmt->u.str);
1176 break;
1177 case STMT_DECLARATION:
1178 if (iface && stmt->u.var->type->type == RPC_FC_FUNCTION)
1180 if (!ignore_funcs)
1182 int prefixes_differ = strcmp(prefix_client, prefix_server);
1184 if (prefixes_differ)
1186 fprintf(header, "/* client prototype */\n");
1187 write_function_proto(header, iface, stmt->u.var, prefix_client);
1188 fprintf(header, "/* server prototype */\n");
1190 write_function_proto(header, iface, stmt->u.var, prefix_server);
1193 else
1194 write_declaration(header, stmt->u.var);
1195 break;
1200 void write_header(const statement_list_t *stmts)
1202 FILE *header;
1204 if (!do_header) return;
1206 if(!(header = fopen(header_name, "w"))) {
1207 error("Could not open %s for output\n", header_name);
1208 return;
1210 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1211 fprintf(header, "#include <rpc.h>\n" );
1212 fprintf(header, "#include <rpcndr.h>\n\n" );
1214 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1215 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1216 start_cplusplus_guard(header);
1218 fprintf(header, "/* Headers for imported files */\n\n");
1219 write_imports(header, stmts);
1220 fprintf(header, "\n");
1222 /* FIXME: should be before imported file includes */
1223 fprintf(header, "/* Forward declarations */\n\n");
1224 write_forward_decls(header, stmts);
1225 fprintf(header, "\n");
1227 write_header_stmts(header, stmts, NULL, FALSE);
1229 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1230 fprintf(header, "\n");
1231 write_user_types(header);
1232 write_generic_handle_routines(header);
1233 write_context_handle_rundowns(header);
1234 fprintf(header, "\n");
1235 fprintf(header, "/* End additional prototypes */\n");
1236 fprintf(header, "\n");
1238 end_cplusplus_guard(header);
1239 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1241 fclose(header);