po: Update Ukrainian translation.
[wine.git] / tools / widl / header.c
blobc23faf49f96defb3ba1fcc74bb54dd7722d1c9a9
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 static int indentation = 0;
40 static int is_object_interface = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
45 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name);
47 static void indent(FILE *h, int delta)
49 int c;
50 if (delta < 0) indentation += delta;
51 for (c=0; c<indentation; c++) fprintf(h, " ");
52 if (delta > 0) indentation += delta;
55 int is_ptrchain_attr(const var_t *var, enum attr_type t)
57 if (is_attr(var->attrs, t))
58 return 1;
59 else
61 type_t *type = var->type;
62 for (;;)
64 if (is_attr(type->attrs, t))
65 return 1;
66 else if (type_is_alias(type))
67 type = type_alias_get_aliasee(type);
68 else if (is_ptr(type))
69 type = type_pointer_get_ref(type);
70 else return 0;
75 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
77 const type_t *t = type;
78 for (;;)
80 if (is_attr(t->attrs, attr))
81 return 1;
82 else if (type_is_alias(t))
83 t = type_alias_get_aliasee(t);
84 else return 0;
88 int is_attr(const attr_list_t *list, enum attr_type t)
90 const attr_t *attr;
91 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
92 if (attr->type == t) return 1;
93 return 0;
96 void *get_attrp(const attr_list_t *list, enum attr_type t)
98 const attr_t *attr;
99 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
100 if (attr->type == t) return attr->u.pval;
101 return NULL;
104 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
106 const attr_t *attr;
107 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
108 if (attr->type == t) return attr->u.ival;
109 return 0;
112 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
114 if (!uuid) return;
115 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
116 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
117 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
118 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
119 uuid->Data4[6], uuid->Data4[7]);
122 static void write_uuid_decl(FILE *f, const char *name, const UUID *uuid)
124 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
125 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
127 name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
128 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
129 uuid->Data4[7]);
130 fprintf(f, "#endif\n");
133 static const char *uuid_string(const UUID *uuid)
135 static char buf[37];
137 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
138 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
139 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
141 return buf;
144 const char *get_name(const var_t *v)
146 static char buffer[256];
148 if (is_attr( v->attrs, ATTR_PROPGET ))
149 strcpy( buffer, "get_" );
150 else if (is_attr( v->attrs, ATTR_PROPPUT ))
151 strcpy( buffer, "put_" );
152 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
153 strcpy( buffer, "putref_" );
154 else
155 buffer[0] = 0;
156 strcat( buffer, v->name );
157 return buffer;
160 static void write_fields(FILE *h, var_list_t *fields)
162 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
163 const char *name;
164 char buf[32];
165 var_t *v;
167 if (!fields) return;
169 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
170 if (!v || !v->type) continue;
172 switch(type_get_type_detect_alias(v->type)) {
173 case TYPE_STRUCT:
174 case TYPE_ENCAPSULATED_UNION:
175 nameless_struct_cnt++;
176 break;
177 case TYPE_UNION:
178 nameless_union_cnt++;
179 break;
180 default:
185 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
186 if (!v || !v->type) continue;
188 indent(h, 0);
189 name = v->name;
191 switch(type_get_type_detect_alias(v->type)) {
192 case TYPE_STRUCT:
193 case TYPE_ENCAPSULATED_UNION:
194 if(!v->name) {
195 fprintf(h, "__C89_NAMELESS ");
196 if(nameless_struct_cnt == 1) {
197 name = "__C89_NAMELESSSTRUCTNAME";
198 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
199 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
200 name = buf;
203 break;
204 case TYPE_UNION:
205 if(!v->name) {
206 fprintf(h, "__C89_NAMELESS ");
207 if(nameless_union_cnt == 1) {
208 name = "__C89_NAMELESSUNIONNAME";
209 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
210 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
211 name = buf;
214 break;
215 default:
218 write_type_def_or_decl(h, v->type, TRUE, name);
219 fprintf(h, ";\n");
223 static void write_enums(FILE *h, var_list_t *enums)
225 var_t *v;
226 if (!enums) return;
227 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
229 if (v->name) {
230 indent(h, 0);
231 fprintf(h, "%s", get_name(v));
232 if (v->eval) {
233 fprintf(h, " = ");
234 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
237 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
239 fprintf(h, "\n");
242 int needs_space_after(type_t *t)
244 return (type_is_alias(t) ||
245 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
248 void write_type_left(FILE *h, type_t *t, int declonly)
250 if (!h) return;
252 if (is_attr(t->attrs, ATTR_CONST) &&
253 (type_is_alias(t) || !is_ptr(t)))
254 fprintf(h, "const ");
256 if (type_is_alias(t)) fprintf(h, "%s", t->name);
257 else {
258 switch (type_get_type_detect_alias(t)) {
259 case TYPE_ENUM:
260 if (!declonly && t->defined && !t->written) {
261 if (t->name) fprintf(h, "enum %s {\n", t->name);
262 else fprintf(h, "enum {\n");
263 t->written = TRUE;
264 indentation++;
265 write_enums(h, type_enum_get_values(t));
266 indent(h, -1);
267 fprintf(h, "}");
269 else fprintf(h, "enum %s", t->name ? t->name : "");
270 break;
271 case TYPE_STRUCT:
272 case TYPE_ENCAPSULATED_UNION:
273 if (!declonly && t->defined && !t->written) {
274 if (t->name) fprintf(h, "struct %s {\n", t->name);
275 else fprintf(h, "struct {\n");
276 t->written = TRUE;
277 indentation++;
278 if (type_get_type(t) != TYPE_STRUCT)
279 write_fields(h, type_encapsulated_union_get_fields(t));
280 else
281 write_fields(h, type_struct_get_fields(t));
282 indent(h, -1);
283 fprintf(h, "}");
285 else fprintf(h, "struct %s", t->name ? t->name : "");
286 break;
287 case TYPE_UNION:
288 if (!declonly && t->defined && !t->written) {
289 if (t->name) fprintf(h, "union %s {\n", t->name);
290 else fprintf(h, "union {\n");
291 t->written = TRUE;
292 indentation++;
293 write_fields(h, type_union_get_cases(t));
294 indent(h, -1);
295 fprintf(h, "}");
297 else fprintf(h, "union %s", t->name ? t->name : "");
298 break;
299 case TYPE_POINTER:
300 write_type_left(h, type_pointer_get_ref(t), declonly);
301 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
302 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
303 break;
304 case TYPE_ARRAY:
305 if (t->name && type_array_is_decl_as_ptr(t))
306 fprintf(h, "%s", t->name);
307 else
309 write_type_left(h, type_array_get_element(t), declonly);
310 if (type_array_is_decl_as_ptr(t))
311 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
313 break;
314 case TYPE_BASIC:
315 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
316 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
317 type_basic_get_type(t) != TYPE_BASIC_HYPER)
319 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
320 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
322 switch (type_basic_get_type(t))
324 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
325 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
326 case TYPE_BASIC_INT: fprintf(h, "int"); break;
327 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
328 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
329 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
330 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
331 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
332 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
333 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
334 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
335 case TYPE_BASIC_INT32:
336 if (type_basic_get_sign(t) > 0)
337 fprintf(h, "ULONG");
338 else
339 fprintf(h, "LONG");
340 break;
341 case TYPE_BASIC_INT64:
342 if (type_basic_get_sign(t) > 0)
343 fprintf(h, "UINT64");
344 else
345 fprintf(h, "INT64");
346 break;
347 case TYPE_BASIC_HYPER:
348 if (type_basic_get_sign(t) > 0)
349 fprintf(h, "MIDL_uhyper");
350 else
351 fprintf(h, "hyper");
352 break;
354 break;
355 case TYPE_INTERFACE:
356 case TYPE_MODULE:
357 case TYPE_COCLASS:
358 fprintf(h, "%s", t->name);
359 break;
360 case TYPE_VOID:
361 fprintf(h, "void");
362 break;
363 case TYPE_BITFIELD:
364 write_type_left(h, type_bitfield_get_field(t), declonly);
365 break;
366 case TYPE_ALIAS:
367 case TYPE_FUNCTION:
368 /* handled elsewhere */
369 assert(0);
370 break;
375 void write_type_right(FILE *h, type_t *t, int is_field)
377 if (!h) return;
379 switch (type_get_type(t))
381 case TYPE_ARRAY:
382 if (!type_array_is_decl_as_ptr(t))
384 if (is_conformant_array(t))
386 fprintf(h, "[%s]", is_field ? "1" : "");
387 t = type_array_get_element(t);
389 for ( ;
390 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
391 t = type_array_get_element(t))
392 fprintf(h, "[%u]", type_array_get_dim(t));
394 break;
395 case TYPE_BITFIELD:
396 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
397 break;
398 case TYPE_VOID:
399 case TYPE_BASIC:
400 case TYPE_ENUM:
401 case TYPE_STRUCT:
402 case TYPE_ENCAPSULATED_UNION:
403 case TYPE_UNION:
404 case TYPE_ALIAS:
405 case TYPE_MODULE:
406 case TYPE_COCLASS:
407 case TYPE_FUNCTION:
408 case TYPE_INTERFACE:
409 case TYPE_POINTER:
410 break;
414 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
416 type_t *pt = NULL;
417 int ptr_level = 0;
419 if (!h) return;
421 if (t) {
422 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
425 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
426 int i;
427 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
428 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
429 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
430 write_type_left(h, type_function_get_rettype(pt), declonly);
431 fputc(' ', h);
432 if (ptr_level) fputc('(', h);
433 if (callconv) fprintf(h, "%s ", callconv);
434 for (i = 0; i < ptr_level; i++)
435 fputc('*', h);
436 } else
437 write_type_left(h, t, declonly);
440 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
442 if (t) {
443 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
444 const var_list_t *args = type_function_get_args(pt);
446 if (ptr_level) fputc(')', h);
447 fputc('(', h);
448 if (args)
449 write_args(h, args, NULL, 0, FALSE);
450 else
451 fprintf(h, "void");
452 fputc(')', h);
453 } else
454 write_type_right(h, t, is_field);
458 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
460 write_type_v(f, t, field, FALSE, name);
463 void write_type_decl(FILE *f, type_t *t, const char *name)
465 write_type_v(f, t, FALSE, TRUE, name);
468 void write_type_decl_left(FILE *f, type_t *t)
470 write_type_left(f, t, TRUE);
473 static int user_type_registered(const char *name)
475 user_type_t *ut;
476 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
477 if (!strcmp(name, ut->name))
478 return 1;
479 return 0;
482 static int context_handle_registered(const char *name)
484 context_handle_t *ch;
485 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
486 if (!strcmp(name, ch->name))
487 return 1;
488 return 0;
491 static int generic_handle_registered(const char *name)
493 generic_handle_t *gh;
494 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
495 if (!strcmp(name, gh->name))
496 return 1;
497 return 0;
500 unsigned int get_context_handle_offset( const type_t *type )
502 context_handle_t *ch;
503 unsigned int index = 0;
505 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
507 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
508 else if (is_ptr( type )) type = type_pointer_get_ref( type );
509 else error( "internal error: %s is not a context handle\n", type->name );
511 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
513 if (!strcmp( type->name, ch->name )) return index;
514 index++;
516 error( "internal error: %s is not registered as a context handle\n", type->name );
517 return index;
520 unsigned int get_generic_handle_offset( const type_t *type )
522 generic_handle_t *gh;
523 unsigned int index = 0;
525 while (!is_attr( type->attrs, ATTR_HANDLE ))
527 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
528 else if (is_ptr( type )) type = type_pointer_get_ref( type );
529 else error( "internal error: %s is not a generic handle\n", type->name );
531 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
533 if (!strcmp( type->name, gh->name )) return index;
534 index++;
536 error( "internal error: %s is not registered as a generic handle\n", type->name );
537 return index;
540 /* check for types which require additional prototypes to be generated in the
541 * header */
542 void check_for_additional_prototype_types(const var_list_t *list)
544 const var_t *v;
546 if (!list) return;
547 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
549 type_t *type = v->type;
550 if (!type) continue;
551 for (;;) {
552 const char *name = type->name;
553 if (type->user_types_registered) break;
554 type->user_types_registered = 1;
555 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
556 if (!context_handle_registered(name))
558 context_handle_t *ch = xmalloc(sizeof(*ch));
559 ch->name = xstrdup(name);
560 list_add_tail(&context_handle_list, &ch->entry);
562 /* don't carry on parsing fields within this type */
563 break;
565 if ((type_get_type(type) != TYPE_BASIC ||
566 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
567 is_attr(type->attrs, ATTR_HANDLE)) {
568 if (!generic_handle_registered(name))
570 generic_handle_t *gh = xmalloc(sizeof(*gh));
571 gh->name = xstrdup(name);
572 list_add_tail(&generic_handle_list, &gh->entry);
574 /* don't carry on parsing fields within this type */
575 break;
577 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
578 if (!user_type_registered(name))
580 user_type_t *ut = xmalloc(sizeof *ut);
581 ut->name = xstrdup(name);
582 list_add_tail(&user_type_list, &ut->entry);
584 /* don't carry on parsing fields within this type as we are already
585 * using a wire marshaled type */
586 break;
588 else if (type_is_complete(type))
590 var_list_t *vars;
591 switch (type_get_type_detect_alias(type))
593 case TYPE_ENUM:
594 vars = type_enum_get_values(type);
595 break;
596 case TYPE_STRUCT:
597 vars = type_struct_get_fields(type);
598 break;
599 case TYPE_UNION:
600 vars = type_union_get_cases(type);
601 break;
602 default:
603 vars = NULL;
604 break;
606 check_for_additional_prototype_types(vars);
609 if (type_is_alias(type))
610 type = type_alias_get_aliasee(type);
611 else if (is_ptr(type))
612 type = type_pointer_get_ref(type);
613 else if (is_array(type))
614 type = type_array_get_element(type);
615 else
616 break;
621 static void write_user_types(FILE *header)
623 user_type_t *ut;
624 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
626 const char *name = ut->name;
627 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
628 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
629 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
630 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
634 static void write_context_handle_rundowns(FILE *header)
636 context_handle_t *ch;
637 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
639 const char *name = ch->name;
640 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
644 static void write_generic_handle_routines(FILE *header)
646 generic_handle_t *gh;
647 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
649 const char *name = gh->name;
650 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
651 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
655 static void write_typedef(FILE *header, type_t *type)
657 fprintf(header, "typedef ");
658 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
659 fprintf(header, ";\n");
662 int is_const_decl(const var_t *var)
664 const type_t *t;
665 /* strangely, MIDL accepts a const attribute on any pointer in the
666 * declaration to mean that data isn't being instantiated. this appears
667 * to be a bug, but there is no benefit to being incompatible with MIDL,
668 * so we'll do the same thing */
669 for (t = var->type; ; )
671 if (is_attr(t->attrs, ATTR_CONST))
672 return TRUE;
673 else if (is_ptr(t))
674 t = type_pointer_get_ref(t);
675 else break;
677 return FALSE;
680 static void write_declaration(FILE *header, const var_t *v)
682 if (is_const_decl(v) && v->eval)
684 fprintf(header, "#define %s (", v->name);
685 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
686 fprintf(header, ")\n\n");
688 else
690 switch (v->stgclass)
692 case STG_NONE:
693 case STG_REGISTER: /* ignored */
694 break;
695 case STG_STATIC:
696 fprintf(header, "static ");
697 break;
698 case STG_EXTERN:
699 fprintf(header, "extern ");
700 break;
702 write_type_def_or_decl(header, v->type, FALSE, v->name);
703 fprintf(header, ";\n\n");
707 static void write_library(FILE *header, const typelib_t *typelib)
709 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
710 fprintf(header, "\n");
711 write_guid(header, "LIBID", typelib->name, uuid);
712 fprintf(header, "\n");
716 const type_t* get_explicit_generic_handle_type(const var_t* var)
718 const type_t *t;
719 for (t = var->type;
720 is_ptr(t) || type_is_alias(t);
721 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
722 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
723 is_attr(t->attrs, ATTR_HANDLE))
724 return t;
725 return NULL;
728 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
729 unsigned char *explicit_fc, unsigned char *implicit_fc )
731 const var_t *var;
732 const var_list_t *args = type_get_function_args( func->type );
734 *explicit_fc = *implicit_fc = 0;
735 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
737 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
738 if (type_get_type( var->type ) == TYPE_BASIC && type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
740 *explicit_fc = RPC_FC_BIND_PRIMITIVE;
741 return var;
743 if (get_explicit_generic_handle_type( var ))
745 *explicit_fc = RPC_FC_BIND_GENERIC;
746 return var;
748 if (is_context_handle( var->type ))
750 *explicit_fc = RPC_FC_BIND_CONTEXT;
751 return var;
755 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
757 if (type_get_type( var->type ) == TYPE_BASIC &&
758 type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
759 *implicit_fc = RPC_FC_BIND_PRIMITIVE;
760 else
761 *implicit_fc = RPC_FC_BIND_GENERIC;
762 return var;
765 *implicit_fc = RPC_FC_AUTO_HANDLE;
766 return NULL;
769 int has_out_arg_or_return(const var_t *func)
771 const var_t *var;
773 if (!is_void(type_function_get_rettype(func->type)))
774 return 1;
776 if (!type_get_function_args(func->type))
777 return 0;
779 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
780 if (is_attr(var->attrs, ATTR_OUT))
781 return 1;
783 return 0;
787 /********** INTERFACES **********/
789 int is_object(const type_t *iface)
791 const attr_t *attr;
792 if (type_is_defined(iface) && type_iface_get_inherit(iface))
793 return 1;
794 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
795 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
796 return 0;
799 int is_local(const attr_list_t *a)
801 return is_attr(a, ATTR_LOCAL);
804 const var_t *is_callas(const attr_list_t *a)
806 return get_attrp(a, ATTR_CALLAS);
809 static int is_inherited_method(const type_t *iface, const var_t *func)
811 while ((iface = type_iface_get_inherit(iface)))
813 const statement_t *stmt;
814 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
816 const var_t *funccmp = stmt->u.var;
818 if (!is_callas(func->attrs))
820 char inherit_name[256];
821 /* compare full name including property prefix */
822 strcpy(inherit_name, get_name(funccmp));
823 if (!strcmp(inherit_name, get_name(func))) return 1;
828 return 0;
831 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
833 if (iface == child)
834 return 0;
838 const statement_t *stmt;
839 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
841 const var_t *funccmp = stmt->u.var;
843 if (!is_callas(func->attrs))
845 char inherit_name[256];
846 /* compare full name including property prefix */
847 strcpy(inherit_name, get_name(funccmp));
848 if (!strcmp(inherit_name, get_name(func))) return 1;
852 while ((child = type_iface_get_inherit(child)) && child != iface);
854 return 0;
857 static int is_aggregate_return(const var_t *func)
859 enum type_type type = type_get_type(type_function_get_rettype(func->type));
860 return type == TYPE_STRUCT || type == TYPE_UNION ||
861 type == TYPE_COCLASS || type == TYPE_INTERFACE;
864 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
866 static char buff[255];
867 if (is_inherited_method(iface, func))
868 sprintf(buff, "%s_%s", iface->name, get_name(func));
869 else
870 sprintf(buff, "%s", get_name(func));
871 return buff;
874 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
876 const statement_t *stmt;
877 int first_iface = 1;
879 if (type_iface_get_inherit(iface))
880 write_method_macro(header, type_iface_get_inherit(iface), child, name);
882 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
884 const var_t *func = stmt->u.var;
886 if (first_iface)
888 fprintf(header, "/*** %s methods ***/\n", iface->name);
889 first_iface = 0;
892 if (is_override_method(iface, child, func))
893 continue;
895 if (!is_callas(func->attrs) && !is_aggregate_return(func)) {
896 const var_t *arg;
898 fprintf(header, "#define %s_%s(This", name, get_name(func));
899 if (type_get_function_args(func->type))
900 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
901 fprintf(header, ",%s", arg->name);
902 fprintf(header, ") ");
904 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
905 if (type_get_function_args(func->type))
906 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
907 fprintf(header, ",%s", arg->name);
908 fprintf(header, ")\n");
913 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
915 const var_t *arg;
916 int count = 0;
918 if (do_indent)
920 indentation++;
921 indent(h, 0);
923 if (method == 1) {
924 fprintf(h, "%s* This", name);
925 count++;
927 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
928 if (count) {
929 if (do_indent)
931 fprintf(h, ",\n");
932 indent(h, 0);
934 else fprintf(h, ",");
936 write_type_decl(h, arg->type, arg->name);
937 if (method == 2) {
938 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
939 if (expr) {
940 fprintf(h, " = ");
941 write_expr( h, expr, 0, 1, NULL, NULL, "" );
944 count++;
946 if (do_indent) indentation--;
949 static void write_cpp_method_def(FILE *header, const type_t *iface)
951 const statement_t *stmt;
953 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
955 const var_t *func = stmt->u.var;
956 if (!is_callas(func->attrs)) {
957 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
958 if (!callconv) callconv = "STDMETHODCALLTYPE";
959 indent(header, 0);
960 fprintf(header, "virtual ");
961 write_type_decl_left(header, type_function_get_rettype(func->type));
962 fprintf(header, " %s %s(\n", callconv, get_name(func));
963 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
964 fprintf(header, ") = 0;\n");
965 fprintf(header, "\n");
970 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
972 const statement_t *stmt;
973 int first_iface = 1;
975 if (type_iface_get_inherit(iface))
976 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
978 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
980 const var_t *func = stmt->u.var;
982 if (first_iface)
984 fprintf(header, "/*** %s methods ***/\n", iface->name);
985 first_iface = 0;
988 if (is_override_method(iface, child, func))
989 continue;
991 if (!is_callas(func->attrs)) {
992 const var_t *arg;
994 fprintf(header, "static FORCEINLINE ");
995 write_type_decl_left(header, type_function_get_rettype(func->type));
996 fprintf(header, " %s_%s(", name, get_name(func));
997 write_args(header, type_get_function_args(func->type), name, 1, FALSE);
998 fprintf(header, ") {\n");
999 ++indentation;
1000 if (!is_aggregate_return(func)) {
1001 indent(header, 0);
1002 fprintf(header, "%sThis->lpVtbl->%s(This",
1003 is_void(type_function_get_rettype(func->type)) ? "" : "return ",
1004 get_vtbl_entry_name(iface, func));
1005 } else {
1006 indent(header, 0);
1007 write_type_decl_left(header, type_function_get_rettype(func->type));
1008 fprintf(header, " __ret;\n");
1009 indent(header, 0);
1010 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1012 if (type_get_function_args(func->type))
1013 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
1014 fprintf(header, ",%s", arg->name);
1015 fprintf(header, ");\n");
1016 --indentation;
1017 fprintf(header, "}\n");
1022 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1024 const statement_t *stmt;
1025 int first_iface = 1;
1027 if (type_iface_get_inherit(iface))
1028 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1030 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1032 const var_t *func = stmt->u.var;
1033 if (first_iface) {
1034 indent(header, 0);
1035 fprintf(header, "/*** %s methods ***/\n", iface->name);
1036 first_iface = 0;
1038 if (!is_callas(func->attrs)) {
1039 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
1040 if (!callconv) callconv = "STDMETHODCALLTYPE";
1041 indent(header, 0);
1042 write_type_decl_left(header, type_function_get_rettype(func->type));
1043 if (is_aggregate_return(func))
1044 fprintf(header, " *");
1045 if (is_inherited_method(iface, func))
1046 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1047 else
1048 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1049 ++indentation;
1050 indent(header, 0);
1051 fprintf(header, "%s *This", name);
1052 if (is_aggregate_return(func)) {
1053 fprintf(header, ",\n");
1054 indent(header, 0);
1055 write_type_decl_left(header, type_function_get_rettype(func->type));
1056 fprintf(header, " *__ret");
1058 --indentation;
1059 if (type_get_function_args(func->type)) {
1060 fprintf(header, ",\n");
1061 write_args(header, type_get_function_args(func->type), name, 0, TRUE);
1063 fprintf(header, ");\n");
1064 fprintf(header, "\n");
1069 static void write_c_method_def(FILE *header, const type_t *iface)
1071 do_write_c_method_def(header, iface, iface->name);
1074 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1076 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
1079 static void write_method_proto(FILE *header, const type_t *iface)
1081 const statement_t *stmt;
1083 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1085 const var_t *func = stmt->u.var;
1087 if (!is_local(func->attrs)) {
1088 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
1089 if (!callconv) callconv = "STDMETHODCALLTYPE";
1090 /* proxy prototype */
1091 write_type_decl_left(header, type_function_get_rettype(func->type));
1092 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1093 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
1094 fprintf(header, ");\n");
1095 /* stub prototype */
1096 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1097 fprintf(header, " IRpcStubBuffer* This,\n");
1098 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1099 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1100 fprintf(header, " DWORD* pdwStubPhase);\n");
1105 static void write_locals(FILE *fp, const type_t *iface, int body)
1107 static const char comment[]
1108 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1109 const statement_t *stmt;
1111 if (!is_object(iface))
1112 return;
1114 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1115 const var_t *func = stmt->u.var;
1116 const var_t *cas = is_callas(func->attrs);
1118 if (cas) {
1119 const statement_t *stmt2 = NULL;
1120 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1121 if (!strcmp(stmt2->u.var->name, cas->name))
1122 break;
1123 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1124 const var_t *m = stmt2->u.var;
1125 /* proxy prototype - use local prototype */
1126 write_type_decl_left(fp, type_function_get_rettype(m->type));
1127 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1128 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
1129 fprintf(fp, ")");
1130 if (body) {
1131 type_t *rt = type_function_get_rettype(m->type);
1132 fprintf(fp, "\n{\n");
1133 fprintf(fp, " %s\n", comment);
1134 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
1135 fprintf(fp, " return E_NOTIMPL;\n");
1136 else if (type_get_type(rt) != TYPE_VOID) {
1137 fprintf(fp, " ");
1138 write_type_decl(fp, rt, "rv");
1139 fprintf(fp, ";\n");
1140 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1141 fprintf(fp, " return rv;\n");
1143 fprintf(fp, "}\n\n");
1145 else
1146 fprintf(fp, ";\n");
1147 /* stub prototype - use remotable prototype */
1148 write_type_decl_left(fp, type_function_get_rettype(func->type));
1149 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1150 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
1151 fprintf(fp, ")");
1152 if (body)
1153 /* Remotable methods must all return HRESULTs. */
1154 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1155 else
1156 fprintf(fp, ";\n");
1158 else
1159 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1164 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1166 const statement_t *stmt;
1167 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1169 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1170 write_locals(local_stubs, stmt->u.type, TRUE);
1174 void write_local_stubs(const statement_list_t *stmts)
1176 FILE *local_stubs;
1178 if (!local_stubs_name) return;
1180 local_stubs = fopen(local_stubs_name, "w");
1181 if (!local_stubs) {
1182 error("Could not open %s for output\n", local_stubs_name);
1183 return;
1185 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1186 fprintf(local_stubs, "#include <objbase.h>\n");
1187 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1189 write_local_stubs_stmts(local_stubs, stmts);
1191 fclose(local_stubs);
1194 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1196 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
1198 if (!callconv) callconv = "__cdecl";
1199 /* FIXME: do we need to handle call_as? */
1200 write_type_decl_left(header, type_function_get_rettype(fun->type));
1201 fprintf(header, " %s ", callconv);
1202 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1203 if (type_get_function_args(fun->type))
1204 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
1205 else
1206 fprintf(header, " void");
1207 fprintf(header, ");\n\n");
1210 static void write_forward(FILE *header, type_t *iface)
1212 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
1213 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
1214 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
1215 fprintf(header, "#endif\n\n" );
1218 static void write_com_interface_start(FILE *header, const type_t *iface)
1220 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1221 fprintf(header, "/*****************************************************************************\n");
1222 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1223 fprintf(header, " */\n");
1224 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1225 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1228 static void write_com_interface_end(FILE *header, type_t *iface)
1230 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1231 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1232 type_t *type;
1234 if (uuid)
1235 write_guid(header, dispinterface ? "DIID" : "IID", iface->name, uuid);
1237 /* C++ interface */
1238 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1239 if (uuid)
1240 fprintf(header, "MIDL_INTERFACE(\"%s\")\n", uuid_string(uuid));
1241 else
1242 fprintf(header, "interface ");
1243 if (type_iface_get_inherit(iface))
1245 fprintf(header, "%s : public %s\n", iface->name,
1246 type_iface_get_inherit(iface)->name);
1247 fprintf(header, "{\n");
1249 else
1251 fprintf(header, "%s\n", iface->name);
1252 fprintf(header, "{\n");
1253 fprintf(header, " BEGIN_INTERFACE\n");
1254 fprintf(header, "\n");
1256 /* dispinterfaces don't have real functions, so don't write C++ functions for
1257 * them */
1258 if (!dispinterface)
1260 indentation++;
1261 write_cpp_method_def(header, iface);
1262 indentation--;
1264 if (!type_iface_get_inherit(iface))
1265 fprintf(header, " END_INTERFACE\n");
1266 fprintf(header, "};\n");
1267 if (uuid)
1268 write_uuid_decl(header, iface->name, uuid);
1269 fprintf(header, "#else\n");
1270 /* C interface */
1271 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1272 indentation++;
1273 fprintf(header, " BEGIN_INTERFACE\n");
1274 fprintf(header, "\n");
1275 if (dispinterface)
1276 write_c_disp_method_def(header, iface);
1277 else
1278 write_c_method_def(header, iface);
1279 indentation--;
1280 fprintf(header, " END_INTERFACE\n");
1281 fprintf(header, "} %sVtbl;\n", iface->name);
1282 fprintf(header, "interface %s {\n", iface->name);
1283 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1284 fprintf(header, "};\n");
1285 fprintf(header, "\n");
1286 fprintf(header, "#ifdef COBJMACROS\n");
1287 /* dispinterfaces don't have real functions, so don't write macros for them,
1288 * only for the interface this interface inherits from, i.e. IDispatch */
1289 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1290 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1291 write_method_macro(header, type, type, iface->name);
1292 fprintf(header, "#else\n");
1293 write_inline_wrappers(header, type, type, iface->name);
1294 fprintf(header, "#endif\n");
1295 fprintf(header, "#endif\n");
1296 fprintf(header, "\n");
1297 fprintf(header, "#endif\n");
1298 fprintf(header, "\n");
1299 /* dispinterfaces don't have real functions, so don't write prototypes for
1300 * them */
1301 if (!dispinterface)
1303 write_method_proto(header, iface);
1304 write_locals(header, iface, FALSE);
1305 fprintf(header, "\n");
1307 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1310 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1312 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1313 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1315 fprintf(header, "/*****************************************************************************\n");
1316 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1317 fprintf(header, " */\n");
1318 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1319 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1320 if (var)
1322 fprintf(header, "extern ");
1323 write_type_decl( header, var->type, var->name );
1324 fprintf(header, ";\n");
1326 if (old_names)
1328 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1329 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1331 else
1333 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1334 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1335 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1336 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1340 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1342 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1345 static void write_coclass(FILE *header, type_t *cocl)
1347 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1349 fprintf(header, "/*****************************************************************************\n");
1350 fprintf(header, " * %s coclass\n", cocl->name);
1351 fprintf(header, " */\n\n");
1352 if (uuid)
1353 write_guid(header, "CLSID", cocl->name, uuid);
1354 fprintf(header, "\n#ifdef __cplusplus\n");
1355 if (uuid)
1357 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1358 write_uuid_decl(header, cocl->name, uuid);
1360 else
1362 fprintf(header, "class %s;\n", cocl->name);
1364 fprintf(header, "#endif\n");
1365 fprintf(header, "\n");
1368 static void write_coclass_forward(FILE *header, type_t *cocl)
1370 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1371 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1372 fprintf(header, "#ifdef __cplusplus\n");
1373 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1374 fprintf(header, "#else\n");
1375 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1376 fprintf(header, "#endif /* defined __cplusplus */\n");
1377 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1380 static void write_import(FILE *header, const char *fname)
1382 char *hname, *p;
1384 hname = dup_basename(fname, ".idl");
1385 p = hname + strlen(hname) - 2;
1386 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1388 fprintf(header, "#include <%s>\n", hname);
1389 free(hname);
1392 static void write_imports(FILE *header, const statement_list_t *stmts)
1394 const statement_t *stmt;
1395 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1397 switch (stmt->type)
1399 case STMT_TYPE:
1400 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1401 write_imports(header, type_iface_get_stmts(stmt->u.type));
1402 break;
1403 case STMT_TYPEREF:
1404 case STMT_IMPORTLIB:
1405 /* not included in header */
1406 break;
1407 case STMT_IMPORT:
1408 write_import(header, stmt->u.str);
1409 break;
1410 case STMT_TYPEDEF:
1411 case STMT_MODULE:
1412 case STMT_CPPQUOTE:
1413 case STMT_PRAGMA:
1414 case STMT_DECLARATION:
1415 /* not processed here */
1416 break;
1417 case STMT_LIBRARY:
1418 write_imports(header, stmt->u.lib->stmts);
1419 break;
1424 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1426 const statement_t *stmt;
1427 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1429 switch (stmt->type)
1431 case STMT_TYPE:
1432 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1434 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1435 write_forward(header, stmt->u.type);
1437 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1438 write_coclass_forward(header, stmt->u.type);
1439 break;
1440 case STMT_TYPEREF:
1441 case STMT_IMPORTLIB:
1442 /* not included in header */
1443 break;
1444 case STMT_IMPORT:
1445 case STMT_TYPEDEF:
1446 case STMT_MODULE:
1447 case STMT_CPPQUOTE:
1448 case STMT_PRAGMA:
1449 case STMT_DECLARATION:
1450 /* not processed here */
1451 break;
1452 case STMT_LIBRARY:
1453 write_forward_decls(header, stmt->u.lib->stmts);
1454 break;
1459 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1461 const statement_t *stmt;
1462 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1464 switch (stmt->type)
1466 case STMT_TYPE:
1467 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1469 type_t *iface = stmt->u.type;
1470 if (is_object(iface)) is_object_interface++;
1471 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1473 write_com_interface_start(header, iface);
1474 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1475 write_com_interface_end(header, iface);
1477 else
1479 write_rpc_interface_start(header, iface);
1480 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1481 write_rpc_interface_end(header, iface);
1483 if (is_object(iface)) is_object_interface--;
1485 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1486 write_coclass(header, stmt->u.type);
1487 else
1489 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1490 fprintf(header, ";\n\n");
1492 break;
1493 case STMT_TYPEREF:
1494 /* FIXME: shouldn't write out forward declarations for undefined
1495 * interfaces but a number of our IDL files depend on this */
1496 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1497 write_forward(header, stmt->u.type);
1498 break;
1499 case STMT_IMPORTLIB:
1500 case STMT_MODULE:
1501 case STMT_PRAGMA:
1502 /* not included in header */
1503 break;
1504 case STMT_IMPORT:
1505 /* not processed here */
1506 break;
1507 case STMT_TYPEDEF:
1509 const type_list_t *type_entry = stmt->u.type_list;
1510 for (; type_entry; type_entry = type_entry->next)
1511 write_typedef(header, type_entry->type);
1512 break;
1514 case STMT_LIBRARY:
1515 write_library(header, stmt->u.lib);
1516 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1517 break;
1518 case STMT_CPPQUOTE:
1519 fprintf(header, "%s\n", stmt->u.str);
1520 break;
1521 case STMT_DECLARATION:
1522 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1524 if (!ignore_funcs)
1526 int prefixes_differ = strcmp(prefix_client, prefix_server);
1528 if (prefixes_differ)
1530 fprintf(header, "/* client prototype */\n");
1531 write_function_proto(header, iface, stmt->u.var, prefix_client);
1532 fprintf(header, "/* server prototype */\n");
1534 write_function_proto(header, iface, stmt->u.var, prefix_server);
1537 else
1538 write_declaration(header, stmt->u.var);
1539 break;
1544 void write_header(const statement_list_t *stmts)
1546 FILE *header;
1548 if (!do_header) return;
1550 if(!(header = fopen(header_name, "w"))) {
1551 error("Could not open %s for output\n", header_name);
1552 return;
1554 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1556 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1557 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1558 fprintf(header, "#endif\n\n");
1560 fprintf(header, "#include <rpc.h>\n" );
1561 fprintf(header, "#include <rpcndr.h>\n\n" );
1563 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
1564 fprintf(header, "#include <windows.h>\n");
1565 fprintf(header, "#include <ole2.h>\n");
1566 fprintf(header, "#endif\n\n");
1568 fprintf(header, "#ifndef __%s__\n", header_token);
1569 fprintf(header, "#define __%s__\n\n", header_token);
1571 fprintf(header, "/* Forward declarations */\n\n");
1572 write_forward_decls(header, stmts);
1574 fprintf(header, "/* Headers for imported files */\n\n");
1575 write_imports(header, stmts);
1576 fprintf(header, "\n");
1577 start_cplusplus_guard(header);
1579 write_header_stmts(header, stmts, NULL, FALSE);
1581 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1582 fprintf(header, "\n");
1583 write_user_types(header);
1584 write_generic_handle_routines(header);
1585 write_context_handle_rundowns(header);
1586 fprintf(header, "\n");
1587 fprintf(header, "/* End additional prototypes */\n");
1588 fprintf(header, "\n");
1590 end_cplusplus_guard(header);
1591 fprintf(header, "#endif /* __%s__ */\n", header_token);
1593 fclose(header);