evr/tests: Test receiving EC_COMPLETE when not connected.
[wine.git] / tools / widl / header.c
blob2a745b8a767385b80e491926a66fc1303d5872a7
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 #include <string.h>
27 #include <ctype.h>
29 #include "widl.h"
30 #include "utils.h"
31 #include "parser.h"
32 #include "header.h"
33 #include "expr.h"
34 #include "typetree.h"
35 #include "typelib.h"
37 static int indentation = 0;
38 static int is_object_interface = 0;
39 user_type_list_t user_type_list = LIST_INIT(user_type_list);
40 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
41 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
43 static void write_type_v(FILE *f, const decl_spec_t *t, int is_field, int declonly, const char *name, enum name_type name_type);
45 static void write_apicontract_guard_start(FILE *header, const expr_t *expr);
46 static void write_apicontract_guard_end(FILE *header, const expr_t *expr);
48 static void write_widl_using_macros(FILE *header, type_t *iface);
50 static void indent(FILE *h, int delta)
52 int c;
53 if (delta < 0) indentation += delta;
54 for (c=0; c<indentation; c++) fprintf(h, " ");
55 if (delta > 0) indentation += delta;
58 static void write_line(FILE *f, int delta, const char *fmt, ...)
60 va_list ap;
61 indent(f, delta);
62 va_start(ap, fmt);
63 vfprintf(f, fmt, ap);
64 va_end(ap);
65 fprintf(f, "\n");
68 int is_ptrchain_attr(const var_t *var, enum attr_type t)
70 if (is_attr(var->attrs, t))
71 return 1;
72 else
74 type_t *type = var->declspec.type;
75 for (;;)
77 if (is_attr(type->attrs, t))
78 return 1;
79 else if (type_is_alias(type))
80 type = type_alias_get_aliasee_type(type);
81 else if (is_ptr(type))
82 type = type_pointer_get_ref_type(type);
83 else return 0;
88 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
90 const type_t *t = type;
91 for (;;)
93 if (is_attr(t->attrs, attr))
94 return 1;
95 else if (type_is_alias(t))
96 t = type_alias_get_aliasee_type(t);
97 else return 0;
101 int is_attr(const attr_list_t *list, enum attr_type t)
103 const attr_t *attr;
104 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
105 if (attr->type == t) return 1;
106 return 0;
109 void *get_attrp(const attr_list_t *list, enum attr_type t)
111 const attr_t *attr;
112 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
113 if (attr->type == t) return attr->u.pval;
114 return NULL;
117 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
119 const attr_t *attr;
120 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
121 if (attr->type == t) return attr->u.ival;
122 return 0;
125 static char *format_parameterized_type_args(const type_t *type, const char *prefix, const char *suffix)
127 typeref_list_t *params;
128 typeref_t *ref;
129 size_t len = 0, pos = 0;
130 char *buf = NULL;
132 params = type->details.parameterized.params;
133 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
135 assert(ref->type->type_type != TYPE_POINTER);
136 pos += strappend(&buf, &len, pos, "%s%s%s", prefix, ref->type->name, suffix);
137 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
140 if (!buf) return xstrdup("");
141 return buf;
144 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
146 if (!uuid) return;
147 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
148 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
149 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
150 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
151 uuid->Data4[6], uuid->Data4[7]);
154 static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
156 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
157 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
158 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
159 type->c_name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
160 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
161 uuid->Data4[7]);
162 fprintf(f, "#endif\n");
165 static const char *uuid_string(const UUID *uuid)
167 static char buf[37];
169 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
170 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
171 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
173 return buf;
176 static void write_namespace_start(FILE *header, struct namespace *namespace)
178 if(is_global_namespace(namespace)) {
179 if(use_abi_namespace)
180 write_line(header, 1, "namespace ABI {");
181 return;
184 write_namespace_start(header, namespace->parent);
185 write_line(header, 1, "namespace %s {", namespace->name);
188 static void write_namespace_end(FILE *header, struct namespace *namespace)
190 if(is_global_namespace(namespace)) {
191 if(use_abi_namespace)
192 write_line(header, -1, "}");
193 return;
196 write_line(header, -1, "}");
197 write_namespace_end(header, namespace->parent);
200 const char *get_name(const var_t *v)
202 static char *buffer;
203 free( buffer );
204 if (is_attr( v->attrs, ATTR_EVENTADD ))
205 return buffer = strmake( "add_%s", v->name );
206 if (is_attr( v->attrs, ATTR_EVENTREMOVE ))
207 return buffer = strmake( "remove_%s", v->name );
208 if (is_attr( v->attrs, ATTR_PROPGET ))
209 return buffer = strmake( "get_%s", v->name );
210 if (is_attr( v->attrs, ATTR_PROPPUT ))
211 return buffer = strmake( "put_%s", v->name );
212 if (is_attr( v->attrs, ATTR_PROPPUTREF ))
213 return buffer = strmake( "putref_%s", v->name );
214 buffer = NULL;
215 return v->name;
218 static void write_fields(FILE *h, var_list_t *fields, enum name_type name_type)
220 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
221 const char *name;
222 char buf[32];
223 var_t *v;
225 if (!fields) return;
227 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
228 if (!v || !v->declspec.type) continue;
230 switch(type_get_type_detect_alias(v->declspec.type)) {
231 case TYPE_STRUCT:
232 case TYPE_ENCAPSULATED_UNION:
233 nameless_struct_cnt++;
234 break;
235 case TYPE_UNION:
236 nameless_union_cnt++;
237 break;
238 default:
243 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
244 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
245 if (!v || !v->declspec.type) continue;
246 if (contract) write_apicontract_guard_start(h, contract);
248 indent(h, 0);
249 name = v->name;
251 switch(type_get_type_detect_alias(v->declspec.type)) {
252 case TYPE_STRUCT:
253 case TYPE_ENCAPSULATED_UNION:
254 if(!v->name) {
255 fprintf(h, "__C89_NAMELESS ");
256 if(nameless_struct_cnt == 1) {
257 name = "__C89_NAMELESSSTRUCTNAME";
258 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
259 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
260 name = buf;
263 break;
264 case TYPE_UNION:
265 if(!v->name) {
266 fprintf(h, "__C89_NAMELESS ");
267 if(nameless_union_cnt == 1) {
268 name = "__C89_NAMELESSUNIONNAME";
269 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
270 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
271 name = buf;
274 break;
275 default:
278 write_type_v(h, &v->declspec, TRUE, v->declonly, name, name_type);
279 fprintf(h, ";\n");
280 if (contract) write_apicontract_guard_end(h, contract);
284 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
286 var_t *v;
287 if (!enums) return;
288 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
290 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
291 if (contract) write_apicontract_guard_start(h, contract);
292 if (v->name) {
293 indent(h, 0);
294 if(!enum_name)
295 fprintf(h, "%s", get_name(v));
296 else
297 fprintf(h, "%s_%s", enum_name, get_name(v));
298 if (v->eval) {
299 fprintf(h, " = ");
300 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
303 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
304 else fprintf(h, "\n");
305 if (contract) write_apicontract_guard_end(h, contract);
309 int needs_space_after(type_t *t)
311 return (type_is_alias(t) ||
312 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
315 static int decl_needs_parens(const type_t *t)
317 if (type_is_alias(t))
318 return FALSE;
319 if (is_array(t) && !type_array_is_decl_as_ptr(t))
320 return TRUE;
321 return is_func(t);
324 static void write_pointer_left(FILE *h, type_t *ref)
326 if (needs_space_after(ref))
327 fprintf(h, " ");
328 if (decl_needs_parens(ref))
329 fprintf(h, "(");
330 if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
332 const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
333 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
334 if (callconv) fprintf(h, "%s ", callconv);
336 fprintf(h, "*");
339 void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
341 type_t *t = ds->type;
342 const char *name;
343 char *args;
345 if (!h) return;
347 name = type_get_name(t, name_type);
349 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
350 fprintf(h, "inline ");
352 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
353 fprintf(h, "const ");
355 if (!winrt_mode && type_is_alias(t)) fprintf(h, "%s", t->name);
356 else {
357 switch (type_get_type_detect_alias(t)) {
358 case TYPE_ENUM:
359 if (!declonly && !t->written) {
360 assert(t->defined);
361 if (name) fprintf(h, "enum %s {\n", name);
362 else fprintf(h, "enum {\n");
363 t->written = TRUE;
364 indentation++;
365 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
366 indent(h, -1);
367 fprintf(h, "}");
369 else fprintf(h, "enum %s", name ? name : "");
370 break;
371 case TYPE_STRUCT:
372 case TYPE_ENCAPSULATED_UNION:
373 if (!declonly && !t->written) {
374 assert(t->defined);
375 if (name) fprintf(h, "struct %s {\n", name);
376 else fprintf(h, "struct {\n");
377 t->written = TRUE;
378 indentation++;
379 if (type_get_type(t) != TYPE_STRUCT)
380 write_fields(h, type_encapsulated_union_get_fields(t), name_type);
381 else
382 write_fields(h, type_struct_get_fields(t), name_type);
383 indent(h, -1);
384 fprintf(h, "}");
386 else fprintf(h, "struct %s", name ? name : "");
387 break;
388 case TYPE_UNION:
389 if (!declonly && !t->written) {
390 assert(t->defined);
391 if (t->name) fprintf(h, "union %s {\n", t->name);
392 else fprintf(h, "union {\n");
393 t->written = TRUE;
394 indentation++;
395 write_fields(h, type_union_get_cases(t), name_type);
396 indent(h, -1);
397 fprintf(h, "}");
399 else fprintf(h, "union %s", t->name ? t->name : "");
400 break;
401 case TYPE_POINTER:
403 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
404 write_pointer_left(h, type_pointer_get_ref_type(t));
405 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
406 break;
408 case TYPE_ARRAY:
409 if (t->name && type_array_is_decl_as_ptr(t))
410 fprintf(h, "%s", t->name);
411 else
413 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
414 if (type_array_is_decl_as_ptr(t))
415 write_pointer_left(h, type_array_get_element_type(t));
417 break;
418 case TYPE_FUNCTION:
420 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
422 /* A pointer to a function has to write the calling convention inside
423 * the parentheses. There's no way to handle that here, so we have to
424 * use an extra parameter to tell us whether to write the calling
425 * convention or not. */
426 if (write_callconv)
428 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
429 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
430 if (callconv) fprintf(h, " %s ", callconv);
432 break;
434 case TYPE_BASIC:
435 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
436 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
437 type_basic_get_type(t) != TYPE_BASIC_LONG &&
438 type_basic_get_type(t) != TYPE_BASIC_HYPER)
440 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
441 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
443 switch (type_basic_get_type(t))
445 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
446 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
447 case TYPE_BASIC_INT: fprintf(h, "int"); break;
448 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
449 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
450 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
451 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
452 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
453 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
454 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
455 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
456 case TYPE_BASIC_INT32:
457 if (type_basic_get_sign(t) > 0)
458 fprintf(h, "UINT32");
459 else
460 fprintf(h, "INT32");
461 break;
462 case TYPE_BASIC_LONG:
463 if (type_basic_get_sign(t) > 0)
464 fprintf(h, "ULONG");
465 else
466 fprintf(h, "LONG");
467 break;
468 case TYPE_BASIC_INT64:
469 if (type_basic_get_sign(t) > 0)
470 fprintf(h, "UINT64");
471 else
472 fprintf(h, "INT64");
473 break;
474 case TYPE_BASIC_HYPER:
475 if (type_basic_get_sign(t) > 0)
476 fprintf(h, "MIDL_uhyper");
477 else
478 fprintf(h, "hyper");
479 break;
481 break;
482 case TYPE_INTERFACE:
483 case TYPE_MODULE:
484 case TYPE_COCLASS:
485 fprintf(h, "%s", type_get_qualified_name(t, name_type));
486 break;
487 case TYPE_RUNTIMECLASS:
488 fprintf(h, "%s", type_get_name(type_runtimeclass_get_default_iface(t, TRUE), name_type));
489 break;
490 case TYPE_DELEGATE:
491 fprintf(h, "%s", type_get_qualified_name(type_delegate_get_iface(t), name_type));
492 break;
493 case TYPE_VOID:
494 fprintf(h, "void");
495 break;
496 case TYPE_BITFIELD:
498 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
499 write_type_left(h, &ds, name_type, declonly, TRUE);
500 break;
502 case TYPE_ALIAS:
504 const decl_spec_t *ds = type_alias_get_aliasee(t);
505 int in_namespace = ds && ds->type && ds->type->namespace && !is_global_namespace(ds->type->namespace);
506 if (!in_namespace) fprintf(h, "%s", t->name);
507 else write_type_left(h, ds, name_type, declonly, write_callconv);
508 break;
510 case TYPE_PARAMETERIZED_TYPE:
512 type_t *iface = type_parameterized_type_get_real_type(t);
513 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
514 args = format_parameterized_type_args(t, "", "_logical");
515 fprintf(h, "%s<%s>", iface->name, args);
516 free(args);
517 break;
519 case TYPE_PARAMETER:
520 fprintf(h, "%s_abi", t->name);
521 break;
522 case TYPE_APICONTRACT:
523 /* shouldn't be here */
524 assert(0);
525 break;
530 void write_type_right(FILE *h, type_t *t, int is_field)
532 if (!h) return;
533 if (type_is_alias(t)) return;
535 switch (type_get_type(t))
537 case TYPE_ARRAY:
539 type_t *elem = type_array_get_element_type(t);
540 if (type_array_is_decl_as_ptr(t))
542 if (decl_needs_parens(elem))
543 fprintf(h, ")");
545 else
547 if (is_conformant_array(t))
548 fprintf(h, "[%s]", is_field ? "1" : "");
549 else
550 fprintf(h, "[%u]", type_array_get_dim(t));
552 write_type_right(h, elem, FALSE);
553 break;
555 case TYPE_FUNCTION:
557 const var_list_t *args = type_function_get_args(t);
558 fputc('(', h);
559 if (args) write_args(h, args, NULL, 0, FALSE, NAME_DEFAULT);
560 else
561 fprintf(h, "void");
562 fputc(')', h);
563 write_type_right(h, type_function_get_rettype(t), FALSE);
564 break;
566 case TYPE_POINTER:
568 type_t *ref = type_pointer_get_ref_type(t);
569 if (decl_needs_parens(ref))
570 fprintf(h, ")");
571 write_type_right(h, ref, FALSE);
572 break;
574 case TYPE_BITFIELD:
575 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
576 break;
577 case TYPE_VOID:
578 case TYPE_BASIC:
579 case TYPE_ENUM:
580 case TYPE_STRUCT:
581 case TYPE_ENCAPSULATED_UNION:
582 case TYPE_UNION:
583 case TYPE_ALIAS:
584 case TYPE_MODULE:
585 case TYPE_COCLASS:
586 case TYPE_INTERFACE:
587 case TYPE_RUNTIMECLASS:
588 case TYPE_DELEGATE:
589 case TYPE_PARAMETERIZED_TYPE:
590 case TYPE_PARAMETER:
591 break;
592 case TYPE_APICONTRACT:
593 /* not supposed to be here */
594 assert(0);
595 break;
599 static void write_type_v(FILE *h, const decl_spec_t *ds, int is_field, int declonly, const char *name, enum name_type name_type)
601 type_t *t = ds->type;
603 if (!h) return;
605 if (t) write_type_left(h, ds, name_type, declonly, TRUE);
607 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
609 if (t)
610 write_type_right(h, t, is_field);
613 static void write_type_definition(FILE *f, type_t *t, int declonly)
615 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
616 int save_written = t->written;
617 decl_spec_t ds = {.type = t};
618 expr_t *contract = get_attrp(t->attrs, ATTR_CONTRACT);
620 if (contract) write_apicontract_guard_start(f, contract);
621 if(in_namespace) {
622 fprintf(f, "#ifdef __cplusplus\n");
623 fprintf(f, "} /* extern \"C\" */\n");
624 write_namespace_start(f, t->namespace);
626 indent(f, 0);
627 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
628 fprintf(f, ";\n");
629 if(in_namespace) {
630 t->written = save_written;
631 write_namespace_end(f, t->namespace);
632 fprintf(f, "extern \"C\" {\n");
633 fprintf(f, "#else\n");
634 write_type_left(f, &ds, NAME_C, declonly, TRUE);
635 fprintf(f, ";\n");
636 if (winrt_mode) write_widl_using_macros(f, t);
637 fprintf(f, "#endif\n\n");
639 if (contract) write_apicontract_guard_end(f, contract);
642 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
644 write_type_v(f, t, FALSE, TRUE, name, NAME_DEFAULT);
647 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
649 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
652 static int user_type_registered(const char *name)
654 user_type_t *ut;
655 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
656 if (!strcmp(name, ut->name))
657 return 1;
658 return 0;
661 static int context_handle_registered(const char *name)
663 context_handle_t *ch;
664 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
665 if (!strcmp(name, ch->name))
666 return 1;
667 return 0;
670 static int generic_handle_registered(const char *name)
672 generic_handle_t *gh;
673 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
674 if (!strcmp(name, gh->name))
675 return 1;
676 return 0;
679 unsigned int get_context_handle_offset( const type_t *type )
681 context_handle_t *ch;
682 unsigned int index = 0;
684 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
686 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
687 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
688 else error( "internal error: %s is not a context handle\n", type->name );
690 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
692 if (!strcmp( type->name, ch->name )) return index;
693 index++;
695 error( "internal error: %s is not registered as a context handle\n", type->name );
696 return index;
699 unsigned int get_generic_handle_offset( const type_t *type )
701 generic_handle_t *gh;
702 unsigned int index = 0;
704 while (!is_attr( type->attrs, ATTR_HANDLE ))
706 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
707 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
708 else error( "internal error: %s is not a generic handle\n", type->name );
710 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
712 if (!strcmp( type->name, gh->name )) return index;
713 index++;
715 error( "internal error: %s is not registered as a generic handle\n", type->name );
716 return index;
719 /* check for types which require additional prototypes to be generated in the
720 * header */
721 void check_for_additional_prototype_types(type_t *type)
723 if (!type) return;
724 for (;;) {
725 const char *name = type->name;
726 if (type->user_types_registered) break;
727 type->user_types_registered = 1;
728 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
729 if (!context_handle_registered(name))
731 context_handle_t *ch = xmalloc(sizeof(*ch));
732 ch->name = xstrdup(name);
733 list_add_tail(&context_handle_list, &ch->entry);
735 /* don't carry on parsing fields within this type */
736 break;
738 if ((type_get_type(type) != TYPE_BASIC ||
739 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
740 is_attr(type->attrs, ATTR_HANDLE)) {
741 if (!generic_handle_registered(name))
743 generic_handle_t *gh = xmalloc(sizeof(*gh));
744 gh->name = xstrdup(name);
745 list_add_tail(&generic_handle_list, &gh->entry);
747 /* don't carry on parsing fields within this type */
748 break;
750 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
751 if (!user_type_registered(name))
753 user_type_t *ut = xmalloc(sizeof *ut);
754 ut->name = xstrdup(name);
755 list_add_tail(&user_type_list, &ut->entry);
757 /* don't carry on parsing fields within this type as we are already
758 * using a wire marshaled type */
759 break;
761 else if (type_is_complete(type))
763 var_list_t *vars;
764 const var_t *v;
765 switch (type_get_type_detect_alias(type))
767 case TYPE_ENUM:
768 vars = type_enum_get_values(type);
769 break;
770 case TYPE_STRUCT:
771 vars = type_struct_get_fields(type);
772 break;
773 case TYPE_UNION:
774 vars = type_union_get_cases(type);
775 break;
776 default:
777 vars = NULL;
778 break;
780 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
781 check_for_additional_prototype_types(v->declspec.type);
784 if (type_is_alias(type))
785 type = type_alias_get_aliasee_type(type);
786 else if (is_ptr(type))
787 type = type_pointer_get_ref_type(type);
788 else if (is_array(type))
789 type = type_array_get_element_type(type);
790 else
791 break;
795 static int write_serialize_function_decl(FILE *header, const type_t *type)
797 write_serialize_functions(header, type, NULL);
798 return 1;
801 static int serializable_exists(FILE *header, const type_t *type)
803 return 0;
806 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
807 int (*proc)(FILE*, const type_t*))
809 statement_t *stmt, *iface_stmt;
810 statement_list_t *iface_stmts;
811 typeref_t *ref;
813 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
815 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
816 continue;
818 iface_stmts = type_iface_get_stmts(stmt->u.type);
819 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
821 if (iface_stmt->type != STMT_TYPEDEF) continue;
822 if (iface_stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, iface_stmt->u.type_list, typeref_t, entry)
824 if (!is_attr(ref->type->attrs, ATTR_ENCODE)
825 && !is_attr(ref->type->attrs, ATTR_DECODE))
826 continue;
827 if (!proc(header, ref->type))
828 return 0;
833 return 1;
836 static void write_user_types(FILE *header)
838 user_type_t *ut;
839 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
841 const char *name = ut->name;
842 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
843 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
844 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
845 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
849 static void write_context_handle_rundowns(FILE *header)
851 context_handle_t *ch;
852 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
854 const char *name = ch->name;
855 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
859 static void write_generic_handle_routines(FILE *header)
861 generic_handle_t *gh;
862 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
864 const char *name = gh->name;
865 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
866 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
870 static void write_typedef(FILE *header, type_t *type, int declonly)
872 type_t *t = type_alias_get_aliasee_type(type);
873 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace))
875 fprintf(header, "#ifdef __cplusplus\n");
876 write_namespace_start(header, t->namespace);
877 indent(header, 0);
879 fprintf(header, "typedef ");
880 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name, NAME_DEFAULT);
881 fprintf(header, ";\n");
882 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace))
884 write_namespace_end(header, t->namespace);
885 fprintf(header, "#else /* __cplusplus */\n");
886 fprintf(header, "typedef ");
887 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->c_name, NAME_C);
888 fprintf(header, ";\n");
889 fprintf(header, "#endif /* __cplusplus */\n\n");
893 int is_const_decl(const var_t *var)
895 const decl_spec_t *t;
896 /* strangely, MIDL accepts a const attribute on any pointer in the
897 * declaration to mean that data isn't being instantiated. this appears
898 * to be a bug, but there is no benefit to being incompatible with MIDL,
899 * so we'll do the same thing */
900 for (t = &var->declspec; ; )
902 if (t->qualifier & TYPE_QUALIFIER_CONST)
903 return TRUE;
904 else if (is_ptr(t->type))
905 t = type_pointer_get_ref(t->type);
906 else break;
908 return FALSE;
911 static void write_declaration(FILE *header, const var_t *v)
913 if (is_const_decl(v) && v->eval)
915 fprintf(header, "#define %s (", v->name);
916 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
917 fprintf(header, ")\n\n");
919 else
921 switch (v->declspec.stgclass)
923 case STG_NONE:
924 case STG_REGISTER: /* ignored */
925 break;
926 case STG_STATIC:
927 fprintf(header, "static ");
928 break;
929 case STG_EXTERN:
930 fprintf(header, "extern ");
931 break;
933 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name, NAME_DEFAULT);
934 fprintf(header, ";\n\n");
938 static void write_library(FILE *header, const typelib_t *typelib)
940 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
941 fprintf(header, "\n");
942 write_guid(header, "LIBID", typelib->name, uuid);
943 fprintf(header, "\n");
947 const type_t* get_explicit_generic_handle_type(const var_t* var)
949 const type_t *t;
950 for (t = var->declspec.type;
951 is_ptr(t) || type_is_alias(t);
952 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
953 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
954 is_attr(t->attrs, ATTR_HANDLE))
955 return t;
956 return NULL;
959 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
960 unsigned char *explicit_fc, unsigned char *implicit_fc )
962 const var_t *var;
963 const var_list_t *args = type_function_get_args( func->declspec.type );
965 *explicit_fc = *implicit_fc = 0;
966 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
968 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
969 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
971 *explicit_fc = FC_BIND_PRIMITIVE;
972 return var;
974 if (get_explicit_generic_handle_type( var ))
976 *explicit_fc = FC_BIND_GENERIC;
977 return var;
979 if (is_context_handle( var->declspec.type ))
981 *explicit_fc = FC_BIND_CONTEXT;
982 return var;
986 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
988 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
989 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
990 *implicit_fc = FC_BIND_PRIMITIVE;
991 else
992 *implicit_fc = FC_BIND_GENERIC;
993 return var;
996 *implicit_fc = FC_AUTO_HANDLE;
997 return NULL;
1000 int has_out_arg_or_return(const var_t *func)
1002 const var_t *var;
1004 if (!is_void(type_function_get_rettype(func->declspec.type)))
1005 return 1;
1007 if (!type_function_get_args(func->declspec.type))
1008 return 0;
1010 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1011 if (is_attr(var->attrs, ATTR_OUT))
1012 return 1;
1014 return 0;
1018 /********** INTERFACES **********/
1020 int is_object(const type_t *iface)
1022 const attr_t *attr;
1023 if (type_is_defined(iface) && (type_get_type(iface) == TYPE_DELEGATE || type_iface_get_inherit(iface)))
1024 return 1;
1025 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
1026 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
1027 return 0;
1030 int is_local(const attr_list_t *a)
1032 return is_attr(a, ATTR_LOCAL);
1035 const var_t *is_callas(const attr_list_t *a)
1037 return get_attrp(a, ATTR_CALLAS);
1040 static int is_inherited_method(const type_t *iface, const var_t *func)
1042 while ((iface = type_iface_get_inherit(iface)))
1044 const statement_t *stmt;
1045 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1047 const var_t *funccmp = stmt->u.var;
1049 if (!is_callas(func->attrs))
1051 char inherit_name[256];
1052 /* compare full name including property prefix */
1053 strcpy(inherit_name, get_name(funccmp));
1054 if (!strcmp(inherit_name, get_name(func))) return 1;
1059 return 0;
1062 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
1064 if (iface == child)
1065 return 0;
1069 const statement_t *stmt;
1070 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
1072 const var_t *funccmp = stmt->u.var;
1074 if (!is_callas(func->attrs))
1076 char inherit_name[256];
1077 /* compare full name including property prefix */
1078 strcpy(inherit_name, get_name(funccmp));
1079 if (!strcmp(inherit_name, get_name(func))) return 1;
1083 while ((child = type_iface_get_inherit(child)) && child != iface);
1085 return 0;
1088 static int is_aggregate_return(const var_t *func)
1090 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1091 return type == TYPE_STRUCT || type == TYPE_UNION ||
1092 type == TYPE_COCLASS || type == TYPE_INTERFACE ||
1093 type == TYPE_RUNTIMECLASS;
1096 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1098 static char buff[255];
1099 if (is_inherited_method(iface, func))
1100 sprintf(buff, "%s_%s", iface->name, get_name(func));
1101 else
1102 sprintf(buff, "%s", get_name(func));
1103 return buff;
1106 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1108 const statement_t *stmt;
1109 int first_iface = 1;
1111 if (type_iface_get_inherit(iface))
1112 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1114 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1116 const var_t *func = stmt->u.var;
1118 if (first_iface)
1120 fprintf(header, "/*** %s methods ***/\n", iface->name);
1121 first_iface = 0;
1124 if (is_override_method(iface, child, func))
1125 continue;
1127 if (!is_callas(func->attrs)) {
1128 const var_t *arg;
1130 fprintf(header, "#define %s_%s(This", name, get_name(func));
1131 if (type_function_get_args(func->declspec.type))
1132 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1133 fprintf(header, ",%s", arg->name);
1134 fprintf(header, ") ");
1136 if (is_aggregate_return(func))
1138 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1139 continue;
1142 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1143 if (type_function_get_args(func->declspec.type))
1144 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1145 fprintf(header, ",%s", arg->name);
1146 fprintf(header, ")\n");
1151 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent, enum name_type name_type)
1153 const var_t *arg;
1154 int count = 0;
1156 if (do_indent)
1158 indentation++;
1159 indent(h, 0);
1161 if (method == 1) {
1162 fprintf(h, "%s* This", name);
1163 count++;
1165 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1166 if (count) {
1167 if (do_indent)
1169 fprintf(h, ",\n");
1170 indent(h, 0);
1172 else fprintf(h, ",");
1174 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1175 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1176 * defining UDTs inside of an argument list. */
1177 write_type_v(h, &arg->declspec, FALSE, TRUE, arg->name, name_type);
1178 if (method == 2) {
1179 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1180 if (expr) {
1181 const var_t *tail_arg;
1183 /* Output default value only if all following arguments also have default value. */
1184 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1185 if(tail_arg == arg) {
1186 expr_t bstr;
1188 /* Fixup the expression type for a BSTR like midl does. */
1189 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1191 bstr = *expr;
1192 bstr.type = EXPR_WSTRLIT;
1193 expr = &bstr;
1196 fprintf(h, " = ");
1197 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1198 break;
1200 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1201 break;
1205 count++;
1207 if (do_indent) indentation--;
1210 static void write_cpp_method_def(FILE *header, const type_t *iface)
1212 const statement_t *stmt;
1214 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1216 const var_t *func = stmt->u.var;
1217 if (!is_callas(func->attrs)) {
1218 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1219 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1220 const var_list_t *args = type_function_get_args(func->declspec.type);
1221 const var_t *arg;
1223 if (!callconv) callconv = "STDMETHODCALLTYPE";
1225 if (is_aggregate_return(func)) {
1226 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1228 indent(header, 0);
1229 fprintf(header, "virtual ");
1230 write_type_decl_left(header, ret);
1231 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1232 ++indentation;
1233 indent(header, 0);
1234 write_type_decl_left(header, ret);
1235 fprintf(header, " *__ret");
1236 --indentation;
1237 if (args) {
1238 fprintf(header, ",\n");
1239 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1241 fprintf(header, ") = 0;\n");
1243 indent(header, 0);
1244 write_type_decl_left(header, ret);
1245 fprintf(header, " %s %s(\n", callconv, get_name(func));
1246 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1247 fprintf(header, ")\n");
1248 indent(header, 0);
1249 fprintf(header, "{\n");
1250 ++indentation;
1251 indent(header, 0);
1252 write_type_decl_left(header, ret);
1253 fprintf(header, " __ret;\n");
1254 indent(header, 0);
1255 fprintf(header, "return *%s(&__ret", get_name(func));
1256 if (args)
1257 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1258 fprintf(header, ", %s", arg->name);
1259 fprintf(header, ");\n");
1260 --indentation;
1261 indent(header, 0);
1262 fprintf(header, "}\n");
1264 fprintf(header, "#else\n");
1267 indent(header, 0);
1268 fprintf(header, "virtual ");
1269 write_type_decl_left(header, ret);
1270 fprintf(header, " %s %s(\n", callconv, get_name(func));
1271 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1272 fprintf(header, ") = 0;\n");
1274 if (is_aggregate_return(func))
1275 fprintf(header, "#endif\n");
1276 fprintf(header, "\n");
1281 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1283 const statement_t *stmt;
1284 int first_iface = 1;
1286 if (type_iface_get_inherit(iface))
1287 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1289 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1291 const var_t *func = stmt->u.var;
1293 if (first_iface)
1295 fprintf(header, "/*** %s methods ***/\n", iface->name);
1296 first_iface = 0;
1299 if (is_override_method(iface, child, func))
1300 continue;
1302 if (!is_callas(func->attrs)) {
1303 const var_t *arg;
1305 fprintf(header, "static FORCEINLINE ");
1306 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1307 fprintf(header, " %s_%s(", name, get_name(func));
1308 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE, NAME_C);
1309 fprintf(header, ") {\n");
1310 ++indentation;
1311 if (!is_aggregate_return(func)) {
1312 indent(header, 0);
1313 fprintf(header, "%sThis->lpVtbl->%s(This",
1314 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1315 get_vtbl_entry_name(iface, func));
1316 } else {
1317 indent(header, 0);
1318 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1319 fprintf(header, " __ret;\n");
1320 indent(header, 0);
1321 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1323 if (type_function_get_args(func->declspec.type))
1324 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1325 fprintf(header, ",%s", arg->name);
1326 fprintf(header, ");\n");
1327 --indentation;
1328 fprintf(header, "}\n");
1333 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1335 const statement_t *stmt;
1336 int first_iface = 1;
1338 if (type_iface_get_inherit(iface))
1339 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1341 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1343 const var_t *func = stmt->u.var;
1344 if (first_iface) {
1345 indent(header, 0);
1346 fprintf(header, "/*** %s methods ***/\n", iface->name);
1347 first_iface = 0;
1349 if (!is_callas(func->attrs)) {
1350 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1351 if (!callconv) callconv = "STDMETHODCALLTYPE";
1352 indent(header, 0);
1353 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1354 if (is_aggregate_return(func))
1355 fprintf(header, " *");
1356 if (is_inherited_method(iface, func))
1357 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1358 else
1359 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1360 ++indentation;
1361 indent(header, 0);
1362 fprintf(header, "%s *This", name);
1363 if (is_aggregate_return(func)) {
1364 fprintf(header, ",\n");
1365 indent(header, 0);
1366 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1367 fprintf(header, " *__ret");
1369 --indentation;
1370 if (type_function_get_args(func->declspec.type)) {
1371 fprintf(header, ",\n");
1372 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE, NAME_C);
1374 fprintf(header, ");\n");
1375 fprintf(header, "\n");
1380 static void write_c_method_def(FILE *header, const type_t *iface)
1382 do_write_c_method_def(header, iface, iface->c_name);
1385 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1387 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1390 static void write_method_proto(FILE *header, const type_t *iface)
1392 const statement_t *stmt;
1394 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1396 const var_t *func = stmt->u.var;
1398 if (is_callas(func->attrs)) {
1399 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1400 if (!callconv) callconv = "STDMETHODCALLTYPE";
1401 /* proxy prototype */
1402 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1403 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1404 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1405 fprintf(header, ");\n");
1406 /* stub prototype */
1407 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1408 fprintf(header, " IRpcStubBuffer* This,\n");
1409 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1410 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1411 fprintf(header, " DWORD* pdwStubPhase);\n");
1416 static void write_locals(FILE *fp, const type_t *iface, int body)
1418 static const char comment[]
1419 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1420 const statement_t *stmt;
1422 if (!is_object(iface))
1423 return;
1425 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1426 const var_t *func = stmt->u.var;
1427 const var_t *cas = is_callas(func->attrs);
1429 if (cas) {
1430 const statement_t *stmt2 = NULL;
1431 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1432 if (!strcmp(get_name(stmt2->u.var), cas->name))
1433 break;
1434 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1435 const var_t *m = stmt2->u.var;
1436 /* proxy prototype - use local prototype */
1437 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1438 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1439 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1440 fprintf(fp, ")");
1441 if (body) {
1442 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1443 fprintf(fp, "\n{\n");
1444 fprintf(fp, " %s\n", comment);
1445 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1446 fprintf(fp, " return E_NOTIMPL;\n");
1447 else if (type_get_type(rt->type) != TYPE_VOID) {
1448 fprintf(fp, " ");
1449 write_type_decl(fp, rt, "rv");
1450 fprintf(fp, ";\n");
1451 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1452 fprintf(fp, " return rv;\n");
1454 fprintf(fp, "}\n\n");
1456 else
1457 fprintf(fp, ";\n");
1458 /* stub prototype - use remotable prototype */
1459 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1460 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1461 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1462 fprintf(fp, ")");
1463 if (body)
1464 /* Remotable methods must all return HRESULTs. */
1465 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1466 else
1467 fprintf(fp, ";\n");
1469 else
1470 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1475 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1477 const statement_t *stmt;
1478 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1480 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1481 write_locals(local_stubs, stmt->u.type, TRUE);
1485 void write_local_stubs(const statement_list_t *stmts)
1487 FILE *local_stubs;
1489 if (!local_stubs_name) return;
1491 local_stubs = fopen(local_stubs_name, "w");
1492 if (!local_stubs) {
1493 error("Could not open %s for output\n", local_stubs_name);
1494 return;
1496 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1497 fprintf(local_stubs, "#include <objbase.h>\n");
1498 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1500 write_local_stubs_stmts(local_stubs, stmts);
1502 fclose(local_stubs);
1505 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1507 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1509 if (!callconv) callconv = "__cdecl";
1510 /* FIXME: do we need to handle call_as? */
1511 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1512 fprintf(header, " %s ", callconv);
1513 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1514 if (type_function_get_args(fun->declspec.type))
1515 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE, NAME_DEFAULT);
1516 else
1517 fprintf(header, " void");
1518 fprintf(header, ");\n\n");
1521 static void write_parameterized_type_forward(FILE *header, type_t *type)
1523 type_t *iface = type->details.parameterized.type;
1524 char *args;
1526 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1528 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1529 write_namespace_start(header, type->namespace);
1531 args = format_parameterized_type_args(type, "class ", "");
1532 write_line(header, 0, "template <%s>", args);
1533 write_line(header, 0, "struct %s_impl;\n", iface->name);
1535 write_line(header, 0, "template <%s>", args);
1536 free(args);
1537 args = format_parameterized_type_args(type, "", "");
1538 write_line(header, 0, "struct %s : %s_impl<%s> {};", iface->name, iface->name, args);
1539 free(args);
1541 write_namespace_end(header, type->namespace);
1542 fprintf(header, "#endif\n\n" );
1545 static void write_parameterized_implementation(FILE *header, type_t *type, int declonly)
1547 const statement_t *stmt;
1548 typeref_list_t *params = type->details.parameterized.params;
1549 typeref_t *ref;
1550 type_t *iface = type->details.parameterized.type, *base;
1551 char *args = NULL;
1553 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1554 write_line(header, 0, "} /* extern \"C\" */");
1555 write_namespace_start(header, type->namespace);
1557 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1558 base = type_iface_get_inherit(iface);
1560 args = format_parameterized_type_args(type, "class ", "");
1561 write_line(header, 0, "template <%s>", args);
1562 free(args);
1563 write_line(header, 0, "struct %s_impl%s", iface->name, base ? strmake(" : %s", base->name) : "");
1564 write_line(header, 0, "{");
1566 write_line(header, 1, "private:");
1567 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1569 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetAbiType<%s>::type %s_abi;", ref->type->name, ref->type->name);
1570 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetLogicalType<%s>::type %s_logical;", ref->type->name, ref->type->name);
1572 indentation -= 1;
1574 write_line(header, 1, "public:");
1575 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1576 write_line(header, 0, "typedef %s %s_complex;", ref->type->name, ref->type->name);
1578 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1580 const var_t *func = stmt->u.var;
1581 if (is_callas(func->attrs)) continue;
1582 indent(header, 1);
1583 fprintf(header, "virtual ");
1584 write_type_decl_left(header, &func->declspec);
1585 fprintf(header, "%s(", get_name(func));
1586 write_args(header, type_function_get_args(func->declspec.type), NULL, 0, 0, NAME_DEFAULT);
1587 fprintf(header, ") = 0;\n");
1588 indentation -= 1;
1590 write_line(header, -1, "};");
1592 write_namespace_end(header, type->namespace);
1593 write_line(header, 0, "extern \"C\" {");
1594 write_line(header, 0, "#endif\n");
1597 static void write_forward(FILE *header, type_t *iface)
1599 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1600 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1601 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1602 fprintf(header, "#ifdef __cplusplus\n");
1603 if (iface->namespace && !is_global_namespace(iface->namespace))
1604 fprintf(header, "#define %s %s\n", iface->c_name, iface->qualified_name);
1605 if (!iface->impl_name)
1607 write_namespace_start(header, iface->namespace);
1608 write_line(header, 0, "interface %s;", iface->name);
1609 write_namespace_end(header, iface->namespace);
1611 fprintf(header, "#endif /* __cplusplus */\n");
1612 fprintf(header, "#endif\n\n" );
1615 static char *format_apicontract_macro(const type_t *type)
1617 char *name = format_namespace(type->namespace, "", "_", type->name, NULL);
1618 int i;
1619 for (i = strlen(name); i > 0; --i) name[i - 1] = toupper(name[i - 1]);
1620 return name;
1623 static void write_apicontract_guard_start(FILE *header, const expr_t *expr)
1625 const type_t *type;
1626 char *name;
1627 int ver;
1628 if (!winrt_mode) return;
1629 type = expr->u.tref.type;
1630 ver = expr->ref->u.lval;
1631 name = format_apicontract_macro(type);
1632 fprintf(header, "#if %s_VERSION >= %#x\n", name, ver);
1633 free(name);
1636 static void write_apicontract_guard_end(FILE *header, const expr_t *expr)
1638 const type_t *type;
1639 char *name;
1640 int ver;
1641 if (!winrt_mode) return;
1642 type = expr->u.tref.type;
1643 ver = expr->ref->u.lval;
1644 name = format_apicontract_macro(type);
1645 fprintf(header, "#endif /* %s_VERSION >= %#x */\n", name, ver);
1646 free(name);
1649 static void write_com_interface_start(FILE *header, const type_t *iface)
1651 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1652 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1653 fprintf(header, "/*****************************************************************************\n");
1654 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1655 fprintf(header, " */\n");
1656 if (contract) write_apicontract_guard_start(header, contract);
1657 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1658 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1661 static void write_widl_using_method_macros(FILE *header, const type_t *iface, const type_t *top_iface)
1663 const statement_t *stmt;
1664 const char *name = top_iface->short_name ? top_iface->short_name : top_iface->name;
1666 if (type_iface_get_inherit(iface)) write_widl_using_method_macros(header, type_iface_get_inherit(iface), top_iface);
1668 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1670 const var_t *func = stmt->u.var;
1671 const char *func_name;
1673 if (is_override_method(iface, top_iface, func)) continue;
1674 if (is_callas(func->attrs)) continue;
1676 func_name = get_name(func);
1677 fprintf(header, "#define %s_%s %s_%s\n", name, func_name, top_iface->c_name, func_name);
1681 static void write_widl_using_macros(FILE *header, type_t *iface)
1683 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1684 const char *name = iface->short_name ? iface->short_name : iface->name;
1685 char *macro;
1687 if (!strcmp(iface->name, iface->c_name)) return;
1689 macro = format_namespace(iface->namespace, "WIDL_using_", "_", NULL, NULL);
1690 fprintf(header, "#ifdef %s\n", macro);
1692 if (uuid) fprintf(header, "#define IID_%s IID_%s\n", name, iface->c_name);
1693 if (iface->type_type == TYPE_INTERFACE) fprintf(header, "#define %sVtbl %sVtbl\n", name, iface->c_name);
1694 fprintf(header, "#define %s %s\n", name, iface->c_name);
1696 if (iface->type_type == TYPE_INTERFACE) write_widl_using_method_macros(header, iface, iface);
1698 fprintf(header, "#endif /* %s */\n", macro);
1699 free(macro);
1702 static void write_com_interface_end(FILE *header, type_t *iface)
1704 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1705 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1706 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1707 type_t *type;
1709 if (uuid)
1710 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1712 /* C++ interface */
1713 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1714 if (!is_global_namespace(iface->namespace)) {
1715 write_line(header, 0, "} /* extern \"C\" */");
1716 write_namespace_start(header, iface->namespace);
1718 if (uuid) {
1719 if (strchr(iface->name, '<')) write_line(header, 0, "template<>");
1720 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1721 indent(header, 0);
1722 }else {
1723 indent(header, 0);
1724 if (strchr(iface->name, '<')) fprintf(header, "template<> struct ");
1725 else fprintf(header, "interface ");
1727 if (iface->impl_name)
1729 fprintf(header, "%s : %s\n", iface->name, iface->impl_name);
1730 write_line(header, 1, "{");
1732 else if (type_iface_get_inherit(iface))
1734 fprintf(header, "%s : public %s\n", iface->name,
1735 type_iface_get_inherit(iface)->name);
1736 write_line(header, 1, "{");
1738 else
1740 fprintf(header, "%s\n", iface->name);
1741 write_line(header, 1, "{\n");
1742 write_line(header, 0, "BEGIN_INTERFACE\n");
1744 /* dispinterfaces don't have real functions, so don't write C++ functions for
1745 * them */
1746 if (!dispinterface && !iface->impl_name)
1747 write_cpp_method_def(header, iface);
1748 if (!type_iface_get_inherit(iface) && !iface->impl_name)
1749 write_line(header, 0, "END_INTERFACE\n");
1750 write_line(header, -1, "};");
1751 if (!is_global_namespace(iface->namespace)) {
1752 write_namespace_end(header, iface->namespace);
1753 write_line(header, 0, "extern \"C\" {");
1755 if (uuid)
1756 write_uuid_decl(header, iface, uuid);
1757 fprintf(header, "#else\n");
1758 /* C interface */
1759 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1760 write_line(header, 0, "BEGIN_INTERFACE\n");
1761 if (dispinterface)
1762 write_c_disp_method_def(header, iface);
1763 else
1764 write_c_method_def(header, iface);
1765 write_line(header, 0, "END_INTERFACE");
1766 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1767 fprintf(header, "interface %s {\n", iface->c_name);
1768 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1769 fprintf(header, "};\n\n");
1770 fprintf(header, "#ifdef COBJMACROS\n");
1771 /* dispinterfaces don't have real functions, so don't write macros for them,
1772 * only for the interface this interface inherits from, i.e. IDispatch */
1773 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1774 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1775 write_method_macro(header, type, type, iface->c_name);
1776 fprintf(header, "#else\n");
1777 write_inline_wrappers(header, type, type, iface->c_name);
1778 fprintf(header, "#endif\n");
1779 if (winrt_mode) write_widl_using_macros(header, iface);
1780 fprintf(header, "#endif\n");
1781 fprintf(header, "\n");
1782 fprintf(header, "#endif\n");
1783 fprintf(header, "\n");
1784 /* dispinterfaces don't have real functions, so don't write prototypes for
1785 * them */
1786 if (!dispinterface && !winrt_mode)
1788 write_method_proto(header, iface);
1789 write_locals(header, iface, FALSE);
1790 fprintf(header, "\n");
1792 fprintf(header, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface->c_name, dispinterface ? "DISP" : "");
1793 if (contract) write_apicontract_guard_end(header, contract);
1794 fprintf(header, "\n");
1797 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1799 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1800 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1801 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1803 fprintf(header, "/*****************************************************************************\n");
1804 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1805 fprintf(header, " */\n");
1806 if (contract) write_apicontract_guard_start(header, contract);
1807 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1808 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1809 if (var)
1811 fprintf(header, "extern ");
1812 write_type_decl( header, &var->declspec, var->name );
1813 fprintf(header, ";\n");
1815 if (old_names)
1817 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1818 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1820 else
1822 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1823 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1824 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1825 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1829 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1831 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1832 fprintf(header, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface->name);
1833 if (contract) write_apicontract_guard_end(header, contract);
1834 fprintf(header, "\n");
1837 static void write_coclass(FILE *header, type_t *cocl)
1839 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1841 fprintf(header, "/*****************************************************************************\n");
1842 fprintf(header, " * %s coclass\n", cocl->name);
1843 fprintf(header, " */\n\n");
1844 if (uuid)
1845 write_guid(header, "CLSID", cocl->name, uuid);
1846 fprintf(header, "\n#ifdef __cplusplus\n");
1847 if (uuid)
1849 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1850 write_uuid_decl(header, cocl, uuid);
1852 else
1854 fprintf(header, "class %s;\n", cocl->name);
1856 fprintf(header, "#endif\n");
1857 fprintf(header, "\n");
1860 static void write_coclass_forward(FILE *header, type_t *cocl)
1862 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1863 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1864 fprintf(header, "#ifdef __cplusplus\n");
1865 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1866 fprintf(header, "#else\n");
1867 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1868 fprintf(header, "#endif /* defined __cplusplus */\n");
1869 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1872 static void write_apicontract(FILE *header, type_t *apicontract)
1874 char *name = format_apicontract_macro(apicontract);
1875 fprintf(header, "#if !defined(%s_VERSION)\n", name);
1876 fprintf(header, "#define %s_VERSION %#x\n", name, get_attrv(apicontract->attrs, ATTR_CONTRACTVERSION));
1877 fprintf(header, "#endif // defined(%s_VERSION)\n\n", name);
1878 free(name);
1881 static void write_runtimeclass(FILE *header, type_t *runtimeclass)
1883 expr_t *contract = get_attrp(runtimeclass->attrs, ATTR_CONTRACT);
1884 char *name, *c_name;
1885 size_t i, len;
1886 name = format_namespace(runtimeclass->namespace, "", ".", runtimeclass->name, NULL);
1887 c_name = format_namespace(runtimeclass->namespace, "", "_", runtimeclass->name, NULL);
1888 fprintf(header, "/*\n");
1889 fprintf(header, " * Class %s\n", name);
1890 fprintf(header, " */\n");
1891 if (contract) write_apicontract_guard_start(header, contract);
1892 fprintf(header, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name);
1893 fprintf(header, "#define RUNTIMECLASS_%s_DEFINED\n", c_name);
1894 fprintf(header, "#if !defined(_MSC_VER) && !defined(__MINGW32__)\n");
1895 fprintf(header, "static const WCHAR RuntimeClass_%s[] = {", c_name);
1896 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1897 fprintf(header, "0};\n");
1898 fprintf(header, "#elif defined(__GNUC__) && !defined(__cplusplus)\n");
1899 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1900 fprintf(header, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name, name);
1901 fprintf(header, "#else\n");
1902 fprintf(header, "extern const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = {", c_name);
1903 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1904 fprintf(header, "0};\n");
1905 fprintf(header, "#endif\n");
1906 fprintf(header, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name);
1907 free(c_name);
1908 free(name);
1909 if (contract) write_apicontract_guard_end(header, contract);
1910 fprintf(header, "\n");
1913 static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
1915 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1916 fprintf(header, "#define __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1917 fprintf(header, "#ifdef __cplusplus\n");
1918 write_namespace_start(header, runtimeclass->namespace);
1919 write_line(header, 0, "class %s;", runtimeclass->name);
1920 write_namespace_end(header, runtimeclass->namespace);
1921 fprintf(header, "#else\n");
1922 fprintf(header, "typedef struct %s %s;\n", runtimeclass->c_name, runtimeclass->c_name);
1923 fprintf(header, "#endif /* defined __cplusplus */\n");
1924 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass->c_name);
1927 static void write_import(FILE *header, const char *fname)
1929 char *hname = replace_extension( get_basename(fname), ".idl", "" );
1931 if (!strendswith( hname, ".h" )) hname = strmake( "%s.h", hname );
1932 fprintf(header, "#include <%s>\n", hname);
1933 free(hname);
1936 static void write_imports(FILE *header, const statement_list_t *stmts)
1938 const statement_t *stmt;
1939 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1941 switch (stmt->type)
1943 case STMT_TYPE:
1944 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1945 write_imports(header, type_iface_get_stmts(stmt->u.type));
1946 break;
1947 case STMT_TYPEREF:
1948 case STMT_IMPORTLIB:
1949 /* not included in header */
1950 break;
1951 case STMT_IMPORT:
1952 write_import(header, stmt->u.str);
1953 break;
1954 case STMT_TYPEDEF:
1955 case STMT_MODULE:
1956 case STMT_CPPQUOTE:
1957 case STMT_PRAGMA:
1958 case STMT_DECLARATION:
1959 /* not processed here */
1960 break;
1961 case STMT_LIBRARY:
1962 write_imports(header, stmt->u.lib->stmts);
1963 break;
1968 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1970 const statement_t *stmt;
1971 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1973 switch (stmt->type)
1975 case STMT_TYPE:
1976 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
1978 type_t *iface = stmt->u.type;
1979 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1980 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1982 write_forward(header, iface);
1983 if (type_iface_get_async_iface(iface))
1984 write_forward(header, type_iface_get_async_iface(iface));
1987 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1988 write_coclass_forward(header, stmt->u.type);
1989 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
1990 write_runtimeclass_forward(header, stmt->u.type);
1991 else if (type_get_type(stmt->u.type) == TYPE_PARAMETERIZED_TYPE)
1992 write_parameterized_type_forward(header, stmt->u.type);
1993 break;
1994 case STMT_TYPEREF:
1995 case STMT_IMPORTLIB:
1996 /* not included in header */
1997 break;
1998 case STMT_IMPORT:
1999 case STMT_TYPEDEF:
2000 case STMT_MODULE:
2001 case STMT_CPPQUOTE:
2002 case STMT_PRAGMA:
2003 case STMT_DECLARATION:
2004 /* not processed here */
2005 break;
2006 case STMT_LIBRARY:
2007 write_forward_decls(header, stmt->u.lib->stmts);
2008 break;
2013 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
2015 const statement_t *stmt;
2016 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2018 switch (stmt->type)
2020 case STMT_TYPE:
2021 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
2023 type_t *iface = stmt->u.type, *async_iface;
2024 if (type_get_type(stmt->u.type) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
2025 async_iface = type_iface_get_async_iface(iface);
2026 if (is_object(iface)) is_object_interface++;
2027 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
2029 write_com_interface_start(header, iface);
2030 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
2031 write_com_interface_end(header, iface);
2032 if (async_iface)
2034 write_com_interface_start(header, async_iface);
2035 write_com_interface_end(header, async_iface);
2038 else
2040 write_rpc_interface_start(header, iface);
2041 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
2042 write_rpc_interface_end(header, iface);
2044 if (is_object(iface)) is_object_interface--;
2046 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
2047 write_coclass(header, stmt->u.type);
2048 else if (type_get_type(stmt->u.type) == TYPE_APICONTRACT)
2049 write_apicontract(header, stmt->u.type);
2050 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
2051 write_runtimeclass(header, stmt->u.type);
2052 else if (type_get_type(stmt->u.type) != TYPE_PARAMETERIZED_TYPE)
2053 write_type_definition(header, stmt->u.type, stmt->declonly);
2054 else
2056 is_object_interface++;
2057 write_parameterized_implementation(header, stmt->u.type, stmt->declonly);
2058 is_object_interface--;
2060 break;
2061 case STMT_TYPEREF:
2062 /* FIXME: shouldn't write out forward declarations for undefined
2063 * interfaces but a number of our IDL files depend on this */
2064 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
2065 write_forward(header, stmt->u.type);
2066 break;
2067 case STMT_IMPORTLIB:
2068 case STMT_MODULE:
2069 case STMT_PRAGMA:
2070 /* not included in header */
2071 break;
2072 case STMT_IMPORT:
2073 /* not processed here */
2074 break;
2075 case STMT_TYPEDEF:
2077 typeref_t *ref;
2078 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
2079 write_typedef(header, ref->type, stmt->declonly);
2080 break;
2082 case STMT_LIBRARY:
2083 fprintf(header, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2084 fprintf(header, "#define __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2085 write_library(header, stmt->u.lib);
2086 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
2087 fprintf(header, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt->u.lib->name);
2088 break;
2089 case STMT_CPPQUOTE:
2090 fprintf(header, "%s\n", stmt->u.str);
2091 break;
2092 case STMT_DECLARATION:
2093 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
2095 if (!ignore_funcs)
2097 int prefixes_differ = strcmp(prefix_client, prefix_server);
2099 if (prefixes_differ)
2101 fprintf(header, "/* client prototype */\n");
2102 write_function_proto(header, iface, stmt->u.var, prefix_client);
2103 fprintf(header, "/* server prototype */\n");
2105 write_function_proto(header, iface, stmt->u.var, prefix_server);
2108 else
2109 write_declaration(header, stmt->u.var);
2110 break;
2115 void write_header(const statement_list_t *stmts)
2117 FILE *header;
2119 if (!do_header) return;
2121 if(!(header = fopen(header_name, "w"))) {
2122 error("Could not open %s for output\n", header_name);
2123 return;
2125 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
2127 fprintf(header, "#ifdef _WIN32\n");
2128 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
2129 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
2130 fprintf(header, "#endif\n");
2131 fprintf(header, "#include <rpc.h>\n" );
2132 fprintf(header, "#include <rpcndr.h>\n" );
2133 if (!for_each_serializable(stmts, NULL, serializable_exists))
2134 fprintf(header, "#include <midles.h>\n" );
2135 fprintf(header, "#endif\n\n");
2137 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
2138 fprintf(header, "#include <windows.h>\n");
2139 fprintf(header, "#include <ole2.h>\n");
2140 fprintf(header, "#endif\n\n");
2142 fprintf(header, "#ifndef __%s__\n", header_token);
2143 fprintf(header, "#define __%s__\n\n", header_token);
2145 fprintf(header, "/* Forward declarations */\n\n");
2146 write_forward_decls(header, stmts);
2148 fprintf(header, "/* Headers for imported files */\n\n");
2149 write_imports(header, stmts);
2150 fprintf(header, "\n");
2151 start_cplusplus_guard(header);
2153 write_header_stmts(header, stmts, NULL, FALSE);
2155 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
2156 fprintf(header, "\n");
2157 for_each_serializable(stmts, header, write_serialize_function_decl);
2158 write_user_types(header);
2159 write_generic_handle_routines(header);
2160 write_context_handle_rundowns(header);
2161 fprintf(header, "\n");
2162 fprintf(header, "/* End additional prototypes */\n");
2163 fprintf(header, "\n");
2165 end_cplusplus_guard(header);
2166 fprintf(header, "#endif /* __%s__ */\n", header_token);
2168 fclose(header);