ntdll: Execute .so constructors during module initialization.
[wine/zf.git] / tools / widl / header.c
blobc00a3e1bb3e3d083bc444739f15a1b54a681e006
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"
38 #include "typelib.h"
40 static int indentation = 0;
41 static int is_object_interface = 0;
42 user_type_list_t user_type_list = LIST_INIT(user_type_list);
43 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
44 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
46 static void write_type_v(FILE *f, const decl_spec_t *t, int is_field, int declonly, const char *name);
48 static void indent(FILE *h, int delta)
50 int c;
51 if (delta < 0) indentation += delta;
52 for (c=0; c<indentation; c++) fprintf(h, " ");
53 if (delta > 0) indentation += delta;
56 static void write_line(FILE *f, int delta, const char *fmt, ...)
58 va_list ap;
59 indent(f, delta);
60 va_start(ap, fmt);
61 vfprintf(f, fmt, ap);
62 va_end(ap);
63 fprintf(f, "\n");
66 int is_ptrchain_attr(const var_t *var, enum attr_type t)
68 if (is_attr(var->attrs, t))
69 return 1;
70 else
72 type_t *type = var->declspec.type;
73 for (;;)
75 if (is_attr(type->attrs, t))
76 return 1;
77 else if (type_is_alias(type))
78 type = type_alias_get_aliasee_type(type);
79 else if (is_ptr(type))
80 type = type_pointer_get_ref_type(type);
81 else return 0;
86 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
88 const type_t *t = type;
89 for (;;)
91 if (is_attr(t->attrs, attr))
92 return 1;
93 else if (type_is_alias(t))
94 t = type_alias_get_aliasee_type(t);
95 else return 0;
99 int is_attr(const attr_list_t *list, enum attr_type t)
101 const attr_t *attr;
102 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
103 if (attr->type == t) return 1;
104 return 0;
107 void *get_attrp(const attr_list_t *list, enum attr_type t)
109 const attr_t *attr;
110 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
111 if (attr->type == t) return attr->u.pval;
112 return NULL;
115 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
117 const attr_t *attr;
118 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
119 if (attr->type == t) return attr->u.ival;
120 return 0;
123 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
125 if (!uuid) return;
126 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
127 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
128 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
129 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
130 uuid->Data4[6], uuid->Data4[7]);
133 static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
135 char *name = format_namespace(type->namespace, "", "::", type->name);
136 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
137 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
138 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
139 name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
140 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
141 uuid->Data4[7]);
142 fprintf(f, "#endif\n");
143 free(name);
146 static const char *uuid_string(const UUID *uuid)
148 static char buf[37];
150 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
151 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
152 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
154 return buf;
157 static void write_namespace_start(FILE *header, struct namespace *namespace)
159 if(is_global_namespace(namespace)) {
160 if(use_abi_namespace)
161 write_line(header, 1, "namespace ABI {");
162 return;
165 write_namespace_start(header, namespace->parent);
166 write_line(header, 1, "namespace %s {", namespace->name);
169 static void write_namespace_end(FILE *header, struct namespace *namespace)
171 if(is_global_namespace(namespace)) {
172 if(use_abi_namespace)
173 write_line(header, -1, "}", namespace->name);
174 return;
177 write_line(header, -1, "}", namespace->name);
178 write_namespace_end(header, namespace->parent);
181 const char *get_name(const var_t *v)
183 static char *buffer;
184 free( buffer );
185 if (is_attr( v->attrs, ATTR_PROPGET ))
186 return buffer = strmake( "get_%s", v->name );
187 if (is_attr( v->attrs, ATTR_PROPPUT ))
188 return buffer = strmake( "put_%s", v->name );
189 if (is_attr( v->attrs, ATTR_PROPPUTREF ))
190 return buffer = strmake( "putref_%s", v->name );
191 buffer = NULL;
192 return v->name;
195 static void write_fields(FILE *h, var_list_t *fields)
197 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
198 const char *name;
199 char buf[32];
200 var_t *v;
202 if (!fields) return;
204 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
205 if (!v || !v->declspec.type) continue;
207 switch(type_get_type_detect_alias(v->declspec.type)) {
208 case TYPE_STRUCT:
209 case TYPE_ENCAPSULATED_UNION:
210 nameless_struct_cnt++;
211 break;
212 case TYPE_UNION:
213 nameless_union_cnt++;
214 break;
215 default:
220 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
221 if (!v || !v->declspec.type) continue;
223 indent(h, 0);
224 name = v->name;
226 switch(type_get_type_detect_alias(v->declspec.type)) {
227 case TYPE_STRUCT:
228 case TYPE_ENCAPSULATED_UNION:
229 if(!v->name) {
230 fprintf(h, "__C89_NAMELESS ");
231 if(nameless_struct_cnt == 1) {
232 name = "__C89_NAMELESSSTRUCTNAME";
233 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
234 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
235 name = buf;
238 break;
239 case TYPE_UNION:
240 if(!v->name) {
241 fprintf(h, "__C89_NAMELESS ");
242 if(nameless_union_cnt == 1) {
243 name = "__C89_NAMELESSUNIONNAME";
244 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
245 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
246 name = buf;
249 break;
250 default:
253 write_type_v(h, &v->declspec, TRUE, v->declonly, name);
254 fprintf(h, ";\n");
258 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
260 var_t *v;
261 if (!enums) return;
262 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
264 if (v->name) {
265 indent(h, 0);
266 if(!enum_name)
267 fprintf(h, "%s", get_name(v));
268 else
269 fprintf(h, "%s_%s", enum_name, get_name(v));
270 if (v->eval) {
271 fprintf(h, " = ");
272 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
275 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
277 fprintf(h, "\n");
280 int needs_space_after(type_t *t)
282 return (type_is_alias(t) ||
283 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
286 int decl_needs_parens(const type_t *t)
288 if (type_is_alias(t))
289 return FALSE;
290 if (is_array(t) && !type_array_is_decl_as_ptr(t))
291 return TRUE;
292 return is_func(t);
295 static void write_pointer_left(FILE *h, type_t *ref)
297 if (needs_space_after(ref))
298 fprintf(h, " ");
299 if (decl_needs_parens(ref))
300 fprintf(h, "(");
301 if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
303 const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
304 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
305 if (callconv) fprintf(h, "%s ", callconv);
307 fprintf(h, "*");
310 void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
312 type_t *t = ds->type;
313 const char *name;
315 if (!h) return;
317 name = type_get_name(t, name_type);
319 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
320 fprintf(h, "inline ");
322 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
323 fprintf(h, "const ");
325 if (type_is_alias(t)) fprintf(h, "%s", t->name);
326 else {
327 switch (type_get_type_detect_alias(t)) {
328 case TYPE_ENUM:
329 if (!declonly && !t->written) {
330 assert(t->defined);
331 if (name) fprintf(h, "enum %s {\n", name);
332 else fprintf(h, "enum {\n");
333 t->written = TRUE;
334 indentation++;
335 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
336 indent(h, -1);
337 fprintf(h, "}");
339 else fprintf(h, "enum %s", name ? name : "");
340 break;
341 case TYPE_STRUCT:
342 case TYPE_ENCAPSULATED_UNION:
343 if (!declonly && !t->written) {
344 assert(t->defined);
345 if (name) fprintf(h, "struct %s {\n", name);
346 else fprintf(h, "struct {\n");
347 t->written = TRUE;
348 indentation++;
349 if (type_get_type(t) != TYPE_STRUCT)
350 write_fields(h, type_encapsulated_union_get_fields(t));
351 else
352 write_fields(h, type_struct_get_fields(t));
353 indent(h, -1);
354 fprintf(h, "}");
356 else fprintf(h, "struct %s", name ? name : "");
357 break;
358 case TYPE_UNION:
359 if (!declonly && !t->written) {
360 assert(t->defined);
361 if (t->name) fprintf(h, "union %s {\n", t->name);
362 else fprintf(h, "union {\n");
363 t->written = TRUE;
364 indentation++;
365 write_fields(h, type_union_get_cases(t));
366 indent(h, -1);
367 fprintf(h, "}");
369 else fprintf(h, "union %s", t->name ? t->name : "");
370 break;
371 case TYPE_POINTER:
373 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
374 write_pointer_left(h, type_pointer_get_ref_type(t));
375 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
376 break;
378 case TYPE_ARRAY:
379 if (t->name && type_array_is_decl_as_ptr(t))
380 fprintf(h, "%s", t->name);
381 else
383 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
384 if (type_array_is_decl_as_ptr(t))
385 write_pointer_left(h, type_array_get_element_type(t));
387 break;
388 case TYPE_FUNCTION:
390 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
392 /* A pointer to a function has to write the calling convention inside
393 * the parentheses. There's no way to handle that here, so we have to
394 * use an extra parameter to tell us whether to write the calling
395 * convention or not. */
396 if (write_callconv)
398 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
399 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
400 if (callconv) fprintf(h, " %s ", callconv);
402 break;
404 case TYPE_BASIC:
405 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
406 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
407 type_basic_get_type(t) != TYPE_BASIC_LONG &&
408 type_basic_get_type(t) != TYPE_BASIC_HYPER)
410 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
411 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
413 switch (type_basic_get_type(t))
415 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
416 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
417 case TYPE_BASIC_INT: fprintf(h, "int"); break;
418 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
419 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
420 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
421 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
422 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
423 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
424 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
425 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
426 case TYPE_BASIC_INT32:
427 if (type_basic_get_sign(t) > 0)
428 fprintf(h, "UINT32");
429 else
430 fprintf(h, "INT32");
431 break;
432 case TYPE_BASIC_LONG:
433 if (type_basic_get_sign(t) > 0)
434 fprintf(h, "ULONG");
435 else
436 fprintf(h, "LONG");
437 break;
438 case TYPE_BASIC_INT64:
439 if (type_basic_get_sign(t) > 0)
440 fprintf(h, "UINT64");
441 else
442 fprintf(h, "INT64");
443 break;
444 case TYPE_BASIC_HYPER:
445 if (type_basic_get_sign(t) > 0)
446 fprintf(h, "MIDL_uhyper");
447 else
448 fprintf(h, "hyper");
449 break;
451 break;
452 case TYPE_INTERFACE:
453 case TYPE_MODULE:
454 case TYPE_COCLASS:
455 fprintf(h, "%s", t->name);
456 break;
457 case TYPE_VOID:
458 fprintf(h, "void");
459 break;
460 case TYPE_BITFIELD:
462 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
463 write_type_left(h, &ds, name_type, declonly, TRUE);
464 break;
466 case TYPE_ALIAS:
467 /* handled elsewhere */
468 assert(0);
469 break;
474 void write_type_right(FILE *h, type_t *t, int is_field)
476 if (!h) return;
477 if (type_is_alias(t)) return;
479 switch (type_get_type(t))
481 case TYPE_ARRAY:
483 type_t *elem = type_array_get_element_type(t);
484 if (type_array_is_decl_as_ptr(t))
486 if (decl_needs_parens(elem))
487 fprintf(h, ")");
489 else
491 if (is_conformant_array(t))
492 fprintf(h, "[%s]", is_field ? "1" : "");
493 else
494 fprintf(h, "[%u]", type_array_get_dim(t));
496 write_type_right(h, elem, FALSE);
497 break;
499 case TYPE_FUNCTION:
501 const var_list_t *args = type_function_get_args(t);
502 fputc('(', h);
503 if (args)
504 write_args(h, args, NULL, 0, FALSE);
505 else
506 fprintf(h, "void");
507 fputc(')', h);
508 write_type_right(h, type_function_get_rettype(t), FALSE);
509 break;
511 case TYPE_POINTER:
513 type_t *ref = type_pointer_get_ref_type(t);
514 if (decl_needs_parens(ref))
515 fprintf(h, ")");
516 write_type_right(h, ref, FALSE);
517 break;
519 case TYPE_BITFIELD:
520 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
521 break;
522 case TYPE_VOID:
523 case TYPE_BASIC:
524 case TYPE_ENUM:
525 case TYPE_STRUCT:
526 case TYPE_ENCAPSULATED_UNION:
527 case TYPE_UNION:
528 case TYPE_ALIAS:
529 case TYPE_MODULE:
530 case TYPE_COCLASS:
531 case TYPE_INTERFACE:
532 break;
536 static void write_type_v(FILE *h, const decl_spec_t *ds, int is_field, int declonly, const char *name)
538 type_t *t = ds->type;
540 if (!h) return;
542 if (t)
543 write_type_left(h, ds, NAME_DEFAULT, declonly, TRUE);
545 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
547 if (t)
548 write_type_right(h, t, is_field);
551 static void write_type_definition(FILE *f, type_t *t, int declonly)
553 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
554 int save_written = t->written;
555 decl_spec_t ds = {.type = t};
557 if(in_namespace) {
558 fprintf(f, "#ifdef __cplusplus\n");
559 fprintf(f, "} /* extern \"C\" */\n");
560 write_namespace_start(f, t->namespace);
562 indent(f, 0);
563 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
564 fprintf(f, ";\n");
565 if(in_namespace) {
566 t->written = save_written;
567 write_namespace_end(f, t->namespace);
568 fprintf(f, "extern \"C\" {\n");
569 fprintf(f, "#else\n");
570 write_type_left(f, &ds, NAME_C, declonly, TRUE);
571 fprintf(f, ";\n");
572 fprintf(f, "#endif\n\n");
576 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
578 write_type_v(f, t, FALSE, TRUE, name);
581 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
583 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
586 static int user_type_registered(const char *name)
588 user_type_t *ut;
589 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
590 if (!strcmp(name, ut->name))
591 return 1;
592 return 0;
595 static int context_handle_registered(const char *name)
597 context_handle_t *ch;
598 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
599 if (!strcmp(name, ch->name))
600 return 1;
601 return 0;
604 static int generic_handle_registered(const char *name)
606 generic_handle_t *gh;
607 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
608 if (!strcmp(name, gh->name))
609 return 1;
610 return 0;
613 unsigned int get_context_handle_offset( const type_t *type )
615 context_handle_t *ch;
616 unsigned int index = 0;
618 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
620 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
621 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
622 else error( "internal error: %s is not a context handle\n", type->name );
624 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
626 if (!strcmp( type->name, ch->name )) return index;
627 index++;
629 error( "internal error: %s is not registered as a context handle\n", type->name );
630 return index;
633 unsigned int get_generic_handle_offset( const type_t *type )
635 generic_handle_t *gh;
636 unsigned int index = 0;
638 while (!is_attr( type->attrs, ATTR_HANDLE ))
640 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
641 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
642 else error( "internal error: %s is not a generic handle\n", type->name );
644 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
646 if (!strcmp( type->name, gh->name )) return index;
647 index++;
649 error( "internal error: %s is not registered as a generic handle\n", type->name );
650 return index;
653 /* check for types which require additional prototypes to be generated in the
654 * header */
655 void check_for_additional_prototype_types(type_t *type)
657 if (!type) return;
658 for (;;) {
659 const char *name = type->name;
660 if (type->user_types_registered) break;
661 type->user_types_registered = 1;
662 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
663 if (!context_handle_registered(name))
665 context_handle_t *ch = xmalloc(sizeof(*ch));
666 ch->name = xstrdup(name);
667 list_add_tail(&context_handle_list, &ch->entry);
669 /* don't carry on parsing fields within this type */
670 break;
672 if ((type_get_type(type) != TYPE_BASIC ||
673 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
674 is_attr(type->attrs, ATTR_HANDLE)) {
675 if (!generic_handle_registered(name))
677 generic_handle_t *gh = xmalloc(sizeof(*gh));
678 gh->name = xstrdup(name);
679 list_add_tail(&generic_handle_list, &gh->entry);
681 /* don't carry on parsing fields within this type */
682 break;
684 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
685 if (!user_type_registered(name))
687 user_type_t *ut = xmalloc(sizeof *ut);
688 ut->name = xstrdup(name);
689 list_add_tail(&user_type_list, &ut->entry);
691 /* don't carry on parsing fields within this type as we are already
692 * using a wire marshaled type */
693 break;
695 else if (type_is_complete(type))
697 var_list_t *vars;
698 const var_t *v;
699 switch (type_get_type_detect_alias(type))
701 case TYPE_ENUM:
702 vars = type_enum_get_values(type);
703 break;
704 case TYPE_STRUCT:
705 vars = type_struct_get_fields(type);
706 break;
707 case TYPE_UNION:
708 vars = type_union_get_cases(type);
709 break;
710 default:
711 vars = NULL;
712 break;
714 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
715 check_for_additional_prototype_types(v->declspec.type);
718 if (type_is_alias(type))
719 type = type_alias_get_aliasee_type(type);
720 else if (is_ptr(type))
721 type = type_pointer_get_ref_type(type);
722 else if (is_array(type))
723 type = type_array_get_element_type(type);
724 else
725 break;
729 static int write_serialize_function_decl(FILE *header, const type_t *type)
731 write_serialize_functions(header, type, NULL);
732 return 1;
735 static int serializable_exists(FILE *header, const type_t *type)
737 return 0;
740 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
741 int (*proc)(FILE*, const type_t*))
743 statement_t *stmt, *iface_stmt;
744 statement_list_t *iface_stmts;
745 const type_list_t *type_entry;
747 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
749 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
750 continue;
752 iface_stmts = type_iface_get_stmts(stmt->u.type);
753 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
755 if (iface_stmt->type != STMT_TYPEDEF) continue;
756 for (type_entry = iface_stmt->u.type_list; type_entry; type_entry = type_entry->next)
758 if (!is_attr(type_entry->type->attrs, ATTR_ENCODE)
759 && !is_attr(type_entry->type->attrs, ATTR_DECODE))
760 continue;
761 if (!proc(header, type_entry->type))
762 return 0;
767 return 1;
770 static void write_user_types(FILE *header)
772 user_type_t *ut;
773 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
775 const char *name = ut->name;
776 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
777 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
778 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
779 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
783 static void write_context_handle_rundowns(FILE *header)
785 context_handle_t *ch;
786 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
788 const char *name = ch->name;
789 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
793 static void write_generic_handle_routines(FILE *header)
795 generic_handle_t *gh;
796 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
798 const char *name = gh->name;
799 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
800 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
804 static void write_typedef(FILE *header, type_t *type, int declonly)
806 fprintf(header, "typedef ");
807 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name);
808 fprintf(header, ";\n");
811 int is_const_decl(const var_t *var)
813 const decl_spec_t *t;
814 /* strangely, MIDL accepts a const attribute on any pointer in the
815 * declaration to mean that data isn't being instantiated. this appears
816 * to be a bug, but there is no benefit to being incompatible with MIDL,
817 * so we'll do the same thing */
818 for (t = &var->declspec; ; )
820 if (t->qualifier & TYPE_QUALIFIER_CONST)
821 return TRUE;
822 else if (is_ptr(t->type))
823 t = type_pointer_get_ref(t->type);
824 else break;
826 return FALSE;
829 static void write_declaration(FILE *header, const var_t *v)
831 if (is_const_decl(v) && v->eval)
833 fprintf(header, "#define %s (", v->name);
834 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
835 fprintf(header, ")\n\n");
837 else
839 switch (v->declspec.stgclass)
841 case STG_NONE:
842 case STG_REGISTER: /* ignored */
843 break;
844 case STG_STATIC:
845 fprintf(header, "static ");
846 break;
847 case STG_EXTERN:
848 fprintf(header, "extern ");
849 break;
851 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name);
852 fprintf(header, ";\n\n");
856 static void write_library(FILE *header, const typelib_t *typelib)
858 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
859 fprintf(header, "\n");
860 write_guid(header, "LIBID", typelib->name, uuid);
861 fprintf(header, "\n");
865 const type_t* get_explicit_generic_handle_type(const var_t* var)
867 const type_t *t;
868 for (t = var->declspec.type;
869 is_ptr(t) || type_is_alias(t);
870 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
871 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
872 is_attr(t->attrs, ATTR_HANDLE))
873 return t;
874 return NULL;
877 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
878 unsigned char *explicit_fc, unsigned char *implicit_fc )
880 const var_t *var;
881 const var_list_t *args = type_function_get_args( func->declspec.type );
883 *explicit_fc = *implicit_fc = 0;
884 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
886 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
887 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
889 *explicit_fc = FC_BIND_PRIMITIVE;
890 return var;
892 if (get_explicit_generic_handle_type( var ))
894 *explicit_fc = FC_BIND_GENERIC;
895 return var;
897 if (is_context_handle( var->declspec.type ))
899 *explicit_fc = FC_BIND_CONTEXT;
900 return var;
904 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
906 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
907 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
908 *implicit_fc = FC_BIND_PRIMITIVE;
909 else
910 *implicit_fc = FC_BIND_GENERIC;
911 return var;
914 *implicit_fc = FC_AUTO_HANDLE;
915 return NULL;
918 int has_out_arg_or_return(const var_t *func)
920 const var_t *var;
922 if (!is_void(type_function_get_rettype(func->declspec.type)))
923 return 1;
925 if (!type_function_get_args(func->declspec.type))
926 return 0;
928 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
929 if (is_attr(var->attrs, ATTR_OUT))
930 return 1;
932 return 0;
936 /********** INTERFACES **********/
938 int is_object(const type_t *iface)
940 const attr_t *attr;
941 if (type_is_defined(iface) && type_iface_get_inherit(iface))
942 return 1;
943 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
944 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
945 return 0;
948 int is_local(const attr_list_t *a)
950 return is_attr(a, ATTR_LOCAL);
953 const var_t *is_callas(const attr_list_t *a)
955 return get_attrp(a, ATTR_CALLAS);
958 static int is_inherited_method(const type_t *iface, const var_t *func)
960 while ((iface = type_iface_get_inherit(iface)))
962 const statement_t *stmt;
963 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
965 const var_t *funccmp = stmt->u.var;
967 if (!is_callas(func->attrs))
969 char inherit_name[256];
970 /* compare full name including property prefix */
971 strcpy(inherit_name, get_name(funccmp));
972 if (!strcmp(inherit_name, get_name(func))) return 1;
977 return 0;
980 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
982 if (iface == child)
983 return 0;
987 const statement_t *stmt;
988 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
990 const var_t *funccmp = stmt->u.var;
992 if (!is_callas(func->attrs))
994 char inherit_name[256];
995 /* compare full name including property prefix */
996 strcpy(inherit_name, get_name(funccmp));
997 if (!strcmp(inherit_name, get_name(func))) return 1;
1001 while ((child = type_iface_get_inherit(child)) && child != iface);
1003 return 0;
1006 static int is_aggregate_return(const var_t *func)
1008 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1009 return type == TYPE_STRUCT || type == TYPE_UNION ||
1010 type == TYPE_COCLASS || type == TYPE_INTERFACE;
1013 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1015 static char buff[255];
1016 if (is_inherited_method(iface, func))
1017 sprintf(buff, "%s_%s", iface->name, get_name(func));
1018 else
1019 sprintf(buff, "%s", get_name(func));
1020 return buff;
1023 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1025 const statement_t *stmt;
1026 int first_iface = 1;
1028 if (type_iface_get_inherit(iface))
1029 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1031 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1033 const var_t *func = stmt->u.var;
1035 if (first_iface)
1037 fprintf(header, "/*** %s methods ***/\n", iface->name);
1038 first_iface = 0;
1041 if (is_override_method(iface, child, func))
1042 continue;
1044 if (!is_callas(func->attrs)) {
1045 const var_t *arg;
1047 fprintf(header, "#define %s_%s(This", name, get_name(func));
1048 if (type_function_get_args(func->declspec.type))
1049 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1050 fprintf(header, ",%s", arg->name);
1051 fprintf(header, ") ");
1053 if (is_aggregate_return(func))
1055 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1056 continue;
1059 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1060 if (type_function_get_args(func->declspec.type))
1061 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1062 fprintf(header, ",%s", arg->name);
1063 fprintf(header, ")\n");
1068 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
1070 const var_t *arg;
1071 int count = 0;
1073 if (do_indent)
1075 indentation++;
1076 indent(h, 0);
1078 if (method == 1) {
1079 fprintf(h, "%s* This", name);
1080 count++;
1082 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1083 if (count) {
1084 if (do_indent)
1086 fprintf(h, ",\n");
1087 indent(h, 0);
1089 else fprintf(h, ",");
1091 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1092 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1093 * defining UDTs inside of an argument list. */
1094 write_type_decl(h, &arg->declspec, arg->name);
1095 if (method == 2) {
1096 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1097 if (expr) {
1098 const var_t *tail_arg;
1100 /* Output default value only if all following arguments also have default value. */
1101 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1102 if(tail_arg == arg) {
1103 expr_t bstr;
1105 /* Fixup the expression type for a BSTR like midl does. */
1106 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1108 bstr = *expr;
1109 bstr.type = EXPR_WSTRLIT;
1110 expr = &bstr;
1113 fprintf(h, " = ");
1114 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1115 break;
1117 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1118 break;
1122 count++;
1124 if (do_indent) indentation--;
1127 static void write_cpp_method_def(FILE *header, const type_t *iface)
1129 const statement_t *stmt;
1131 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1133 const var_t *func = stmt->u.var;
1134 if (!is_callas(func->attrs)) {
1135 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1136 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1137 const var_list_t *args = type_function_get_args(func->declspec.type);
1138 const var_t *arg;
1140 if (!callconv) callconv = "STDMETHODCALLTYPE";
1142 if (is_aggregate_return(func)) {
1143 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1145 indent(header, 0);
1146 fprintf(header, "virtual ");
1147 write_type_decl_left(header, ret);
1148 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1149 ++indentation;
1150 indent(header, 0);
1151 write_type_decl_left(header, ret);
1152 fprintf(header, " *__ret");
1153 --indentation;
1154 if (args) {
1155 fprintf(header, ",\n");
1156 write_args(header, args, iface->name, 2, TRUE);
1158 fprintf(header, ") = 0;\n");
1160 indent(header, 0);
1161 write_type_decl_left(header, ret);
1162 fprintf(header, " %s %s(\n", callconv, get_name(func));
1163 write_args(header, args, iface->name, 2, TRUE);
1164 fprintf(header, ")\n");
1165 indent(header, 0);
1166 fprintf(header, "{\n");
1167 ++indentation;
1168 indent(header, 0);
1169 write_type_decl_left(header, ret);
1170 fprintf(header, " __ret;\n");
1171 indent(header, 0);
1172 fprintf(header, "return *%s(&__ret", get_name(func));
1173 if (args)
1174 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1175 fprintf(header, ", %s", arg->name);
1176 fprintf(header, ");\n");
1177 --indentation;
1178 indent(header, 0);
1179 fprintf(header, "}\n");
1181 fprintf(header, "#else\n");
1184 indent(header, 0);
1185 fprintf(header, "virtual ");
1186 write_type_decl_left(header, ret);
1187 fprintf(header, " %s %s(\n", callconv, get_name(func));
1188 write_args(header, args, iface->name, 2, TRUE);
1189 fprintf(header, ") = 0;\n");
1191 if (is_aggregate_return(func))
1192 fprintf(header, "#endif\n");
1193 fprintf(header, "\n");
1198 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1200 const statement_t *stmt;
1201 int first_iface = 1;
1203 if (type_iface_get_inherit(iface))
1204 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1206 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1208 const var_t *func = stmt->u.var;
1210 if (first_iface)
1212 fprintf(header, "/*** %s methods ***/\n", iface->name);
1213 first_iface = 0;
1216 if (is_override_method(iface, child, func))
1217 continue;
1219 if (!is_callas(func->attrs)) {
1220 const var_t *arg;
1222 fprintf(header, "static FORCEINLINE ");
1223 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1224 fprintf(header, " %s_%s(", name, get_name(func));
1225 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE);
1226 fprintf(header, ") {\n");
1227 ++indentation;
1228 if (!is_aggregate_return(func)) {
1229 indent(header, 0);
1230 fprintf(header, "%sThis->lpVtbl->%s(This",
1231 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1232 get_vtbl_entry_name(iface, func));
1233 } else {
1234 indent(header, 0);
1235 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1236 fprintf(header, " __ret;\n");
1237 indent(header, 0);
1238 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1240 if (type_function_get_args(func->declspec.type))
1241 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1242 fprintf(header, ",%s", arg->name);
1243 fprintf(header, ");\n");
1244 --indentation;
1245 fprintf(header, "}\n");
1250 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1252 const statement_t *stmt;
1253 int first_iface = 1;
1255 if (type_iface_get_inherit(iface))
1256 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1258 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1260 const var_t *func = stmt->u.var;
1261 if (first_iface) {
1262 indent(header, 0);
1263 fprintf(header, "/*** %s methods ***/\n", iface->name);
1264 first_iface = 0;
1266 if (!is_callas(func->attrs)) {
1267 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1268 if (!callconv) callconv = "STDMETHODCALLTYPE";
1269 indent(header, 0);
1270 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1271 if (is_aggregate_return(func))
1272 fprintf(header, " *");
1273 if (is_inherited_method(iface, func))
1274 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1275 else
1276 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1277 ++indentation;
1278 indent(header, 0);
1279 fprintf(header, "%s *This", name);
1280 if (is_aggregate_return(func)) {
1281 fprintf(header, ",\n");
1282 indent(header, 0);
1283 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1284 fprintf(header, " *__ret");
1286 --indentation;
1287 if (type_function_get_args(func->declspec.type)) {
1288 fprintf(header, ",\n");
1289 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE);
1291 fprintf(header, ");\n");
1292 fprintf(header, "\n");
1297 static void write_c_method_def(FILE *header, const type_t *iface)
1299 do_write_c_method_def(header, iface, iface->c_name);
1302 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1304 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1307 static void write_method_proto(FILE *header, const type_t *iface)
1309 const statement_t *stmt;
1311 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1313 const var_t *func = stmt->u.var;
1315 if (is_callas(func->attrs)) {
1316 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1317 if (!callconv) callconv = "STDMETHODCALLTYPE";
1318 /* proxy prototype */
1319 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1320 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1321 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE);
1322 fprintf(header, ");\n");
1323 /* stub prototype */
1324 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1325 fprintf(header, " IRpcStubBuffer* This,\n");
1326 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1327 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1328 fprintf(header, " DWORD* pdwStubPhase);\n");
1333 static void write_locals(FILE *fp, const type_t *iface, int body)
1335 static const char comment[]
1336 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1337 const statement_t *stmt;
1339 if (!is_object(iface))
1340 return;
1342 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1343 const var_t *func = stmt->u.var;
1344 const var_t *cas = is_callas(func->attrs);
1346 if (cas) {
1347 const statement_t *stmt2 = NULL;
1348 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1349 if (!strcmp(get_name(stmt2->u.var), cas->name))
1350 break;
1351 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1352 const var_t *m = stmt2->u.var;
1353 /* proxy prototype - use local prototype */
1354 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1355 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1356 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE);
1357 fprintf(fp, ")");
1358 if (body) {
1359 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1360 fprintf(fp, "\n{\n");
1361 fprintf(fp, " %s\n", comment);
1362 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1363 fprintf(fp, " return E_NOTIMPL;\n");
1364 else if (type_get_type(rt->type) != TYPE_VOID) {
1365 fprintf(fp, " ");
1366 write_type_decl(fp, rt, "rv");
1367 fprintf(fp, ";\n");
1368 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1369 fprintf(fp, " return rv;\n");
1371 fprintf(fp, "}\n\n");
1373 else
1374 fprintf(fp, ";\n");
1375 /* stub prototype - use remotable prototype */
1376 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1377 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1378 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE);
1379 fprintf(fp, ")");
1380 if (body)
1381 /* Remotable methods must all return HRESULTs. */
1382 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1383 else
1384 fprintf(fp, ";\n");
1386 else
1387 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1392 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1394 const statement_t *stmt;
1395 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1397 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1398 write_locals(local_stubs, stmt->u.type, TRUE);
1402 void write_local_stubs(const statement_list_t *stmts)
1404 FILE *local_stubs;
1406 if (!local_stubs_name) return;
1408 local_stubs = fopen(local_stubs_name, "w");
1409 if (!local_stubs) {
1410 error("Could not open %s for output\n", local_stubs_name);
1411 return;
1413 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1414 fprintf(local_stubs, "#include <objbase.h>\n");
1415 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1417 write_local_stubs_stmts(local_stubs, stmts);
1419 fclose(local_stubs);
1422 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1424 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1426 if (!callconv) callconv = "__cdecl";
1427 /* FIXME: do we need to handle call_as? */
1428 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1429 fprintf(header, " %s ", callconv);
1430 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1431 if (type_function_get_args(fun->declspec.type))
1432 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE);
1433 else
1434 fprintf(header, " void");
1435 fprintf(header, ");\n\n");
1438 static void write_forward(FILE *header, type_t *iface)
1440 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1441 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1442 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1443 fprintf(header, "#ifdef __cplusplus\n");
1444 write_namespace_start(header, iface->namespace);
1445 write_line(header, 0, "interface %s;", iface->name);
1446 write_namespace_end(header, iface->namespace);
1447 fprintf(header, "#endif /* __cplusplus */\n");
1448 fprintf(header, "#endif\n\n" );
1451 static void write_com_interface_start(FILE *header, const type_t *iface)
1453 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1454 fprintf(header, "/*****************************************************************************\n");
1455 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1456 fprintf(header, " */\n");
1457 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1458 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1461 static void write_com_interface_end(FILE *header, type_t *iface)
1463 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1464 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1465 type_t *type;
1467 if (uuid)
1468 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1470 /* C++ interface */
1471 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1472 if (!is_global_namespace(iface->namespace)) {
1473 write_line(header, 0, "} /* extern \"C\" */");
1474 write_namespace_start(header, iface->namespace);
1476 if (uuid) {
1477 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1478 indent(header, 0);
1479 }else {
1480 indent(header, 0);
1481 fprintf(header, "interface ");
1483 if (type_iface_get_inherit(iface))
1485 fprintf(header, "%s : public %s\n", iface->name,
1486 type_iface_get_inherit(iface)->name);
1487 write_line(header, 1, "{");
1489 else
1491 fprintf(header, "%s\n", iface->name);
1492 write_line(header, 1, "{\n");
1493 write_line(header, 0, "BEGIN_INTERFACE\n");
1495 /* dispinterfaces don't have real functions, so don't write C++ functions for
1496 * them */
1497 if (!dispinterface)
1498 write_cpp_method_def(header, iface);
1499 if (!type_iface_get_inherit(iface))
1500 write_line(header, 0, "END_INTERFACE\n");
1501 write_line(header, -1, "};");
1502 if (!is_global_namespace(iface->namespace)) {
1503 write_namespace_end(header, iface->namespace);
1504 write_line(header, 0, "extern \"C\" {");
1506 if (uuid)
1507 write_uuid_decl(header, iface, uuid);
1508 fprintf(header, "#else\n");
1509 /* C interface */
1510 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1511 write_line(header, 0, "BEGIN_INTERFACE\n");
1512 if (dispinterface)
1513 write_c_disp_method_def(header, iface);
1514 else
1515 write_c_method_def(header, iface);
1516 write_line(header, 0, "END_INTERFACE");
1517 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1518 fprintf(header, "interface %s {\n", iface->c_name);
1519 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1520 fprintf(header, "};\n\n");
1521 fprintf(header, "#ifdef COBJMACROS\n");
1522 /* dispinterfaces don't have real functions, so don't write macros for them,
1523 * only for the interface this interface inherits from, i.e. IDispatch */
1524 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1525 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1526 write_method_macro(header, type, type, iface->c_name);
1527 fprintf(header, "#else\n");
1528 write_inline_wrappers(header, type, type, iface->c_name);
1529 fprintf(header, "#endif\n");
1530 fprintf(header, "#endif\n");
1531 fprintf(header, "\n");
1532 fprintf(header, "#endif\n");
1533 fprintf(header, "\n");
1534 /* dispinterfaces don't have real functions, so don't write prototypes for
1535 * them */
1536 if (!dispinterface && !winrt_mode)
1538 write_method_proto(header, iface);
1539 write_locals(header, iface, FALSE);
1540 fprintf(header, "\n");
1542 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->c_name, dispinterface ? "DISP" : "");
1545 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1547 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1548 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1550 fprintf(header, "/*****************************************************************************\n");
1551 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1552 fprintf(header, " */\n");
1553 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1554 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1555 if (var)
1557 fprintf(header, "extern ");
1558 write_type_decl( header, &var->declspec, var->name );
1559 fprintf(header, ";\n");
1561 if (old_names)
1563 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1564 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1566 else
1568 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1569 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1570 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1571 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1575 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1577 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1580 static void write_coclass(FILE *header, type_t *cocl)
1582 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1584 fprintf(header, "/*****************************************************************************\n");
1585 fprintf(header, " * %s coclass\n", cocl->name);
1586 fprintf(header, " */\n\n");
1587 if (uuid)
1588 write_guid(header, "CLSID", cocl->name, uuid);
1589 fprintf(header, "\n#ifdef __cplusplus\n");
1590 if (uuid)
1592 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1593 write_uuid_decl(header, cocl, uuid);
1595 else
1597 fprintf(header, "class %s;\n", cocl->name);
1599 fprintf(header, "#endif\n");
1600 fprintf(header, "\n");
1603 static void write_coclass_forward(FILE *header, type_t *cocl)
1605 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1606 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1607 fprintf(header, "#ifdef __cplusplus\n");
1608 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1609 fprintf(header, "#else\n");
1610 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1611 fprintf(header, "#endif /* defined __cplusplus */\n");
1612 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1615 static void write_import(FILE *header, const char *fname)
1617 char *hname, *p;
1619 hname = dup_basename(fname, ".idl");
1620 p = hname + strlen(hname) - 2;
1621 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1623 fprintf(header, "#include <%s>\n", hname);
1624 free(hname);
1627 static void write_imports(FILE *header, const statement_list_t *stmts)
1629 const statement_t *stmt;
1630 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1632 switch (stmt->type)
1634 case STMT_TYPE:
1635 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1636 write_imports(header, type_iface_get_stmts(stmt->u.type));
1637 break;
1638 case STMT_TYPEREF:
1639 case STMT_IMPORTLIB:
1640 /* not included in header */
1641 break;
1642 case STMT_IMPORT:
1643 write_import(header, stmt->u.str);
1644 break;
1645 case STMT_TYPEDEF:
1646 case STMT_MODULE:
1647 case STMT_CPPQUOTE:
1648 case STMT_PRAGMA:
1649 case STMT_DECLARATION:
1650 /* not processed here */
1651 break;
1652 case STMT_LIBRARY:
1653 write_imports(header, stmt->u.lib->stmts);
1654 break;
1659 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1661 const statement_t *stmt;
1662 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1664 switch (stmt->type)
1666 case STMT_TYPE:
1667 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1669 type_t *iface = stmt->u.type;
1670 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1672 write_forward(header, iface);
1673 if (type_iface_get_async_iface(iface))
1674 write_forward(header, type_iface_get_async_iface(iface));
1677 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1678 write_coclass_forward(header, stmt->u.type);
1679 break;
1680 case STMT_TYPEREF:
1681 case STMT_IMPORTLIB:
1682 /* not included in header */
1683 break;
1684 case STMT_IMPORT:
1685 case STMT_TYPEDEF:
1686 case STMT_MODULE:
1687 case STMT_CPPQUOTE:
1688 case STMT_PRAGMA:
1689 case STMT_DECLARATION:
1690 /* not processed here */
1691 break;
1692 case STMT_LIBRARY:
1693 write_forward_decls(header, stmt->u.lib->stmts);
1694 break;
1699 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1701 const statement_t *stmt;
1702 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1704 switch (stmt->type)
1706 case STMT_TYPE:
1707 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1709 type_t *iface = stmt->u.type;
1710 type_t *async_iface = type_iface_get_async_iface(iface);
1711 if (is_object(iface)) is_object_interface++;
1712 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1714 write_com_interface_start(header, iface);
1715 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1716 write_com_interface_end(header, iface);
1717 if (async_iface)
1719 write_com_interface_start(header, async_iface);
1720 write_com_interface_end(header, async_iface);
1723 else
1725 write_rpc_interface_start(header, iface);
1726 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1727 write_rpc_interface_end(header, iface);
1729 if (is_object(iface)) is_object_interface--;
1731 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1732 write_coclass(header, stmt->u.type);
1733 else
1735 write_type_definition(header, stmt->u.type, stmt->declonly);
1737 break;
1738 case STMT_TYPEREF:
1739 /* FIXME: shouldn't write out forward declarations for undefined
1740 * interfaces but a number of our IDL files depend on this */
1741 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1742 write_forward(header, stmt->u.type);
1743 break;
1744 case STMT_IMPORTLIB:
1745 case STMT_MODULE:
1746 case STMT_PRAGMA:
1747 /* not included in header */
1748 break;
1749 case STMT_IMPORT:
1750 /* not processed here */
1751 break;
1752 case STMT_TYPEDEF:
1754 const type_list_t *type_entry = stmt->u.type_list;
1755 for (; type_entry; type_entry = type_entry->next)
1756 write_typedef(header, type_entry->type, stmt->declonly);
1757 break;
1759 case STMT_LIBRARY:
1760 write_library(header, stmt->u.lib);
1761 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1762 break;
1763 case STMT_CPPQUOTE:
1764 fprintf(header, "%s\n", stmt->u.str);
1765 break;
1766 case STMT_DECLARATION:
1767 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
1769 if (!ignore_funcs)
1771 int prefixes_differ = strcmp(prefix_client, prefix_server);
1773 if (prefixes_differ)
1775 fprintf(header, "/* client prototype */\n");
1776 write_function_proto(header, iface, stmt->u.var, prefix_client);
1777 fprintf(header, "/* server prototype */\n");
1779 write_function_proto(header, iface, stmt->u.var, prefix_server);
1782 else
1783 write_declaration(header, stmt->u.var);
1784 break;
1789 void write_header(const statement_list_t *stmts)
1791 FILE *header;
1793 if (!do_header) return;
1795 if(!(header = fopen(header_name, "w"))) {
1796 error("Could not open %s for output\n", header_name);
1797 return;
1799 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1801 fprintf(header, "#ifdef _WIN32\n");
1802 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1803 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1804 fprintf(header, "#endif\n");
1805 fprintf(header, "#include <rpc.h>\n" );
1806 fprintf(header, "#include <rpcndr.h>\n" );
1807 if (!for_each_serializable(stmts, NULL, serializable_exists))
1808 fprintf(header, "#include <midles.h>\n" );
1809 fprintf(header, "#endif\n\n");
1811 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
1812 fprintf(header, "#include <windows.h>\n");
1813 fprintf(header, "#include <ole2.h>\n");
1814 fprintf(header, "#endif\n\n");
1816 fprintf(header, "#ifndef __%s__\n", header_token);
1817 fprintf(header, "#define __%s__\n\n", header_token);
1819 fprintf(header, "/* Forward declarations */\n\n");
1820 write_forward_decls(header, stmts);
1822 fprintf(header, "/* Headers for imported files */\n\n");
1823 write_imports(header, stmts);
1824 fprintf(header, "\n");
1825 start_cplusplus_guard(header);
1827 write_header_stmts(header, stmts, NULL, FALSE);
1829 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1830 fprintf(header, "\n");
1831 for_each_serializable(stmts, header, write_serialize_function_decl);
1832 write_user_types(header);
1833 write_generic_handle_routines(header);
1834 write_context_handle_rundowns(header);
1835 fprintf(header, "\n");
1836 fprintf(header, "/* End additional prototypes */\n");
1837 fprintf(header, "\n");
1839 end_cplusplus_guard(header);
1840 fprintf(header, "#endif /* __%s__ */\n", header_token);
1842 fclose(header);