winegstreamer: Add format field to wmv wg_format.
[wine.git] / tools / widl / header.c
blob35163bfa86bb8e059f3912e66488cd14cb029d84
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 struct 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 struct 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 struct 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 *decl_name, *name;
343 char *args;
345 if (!h) return;
347 decl_name = type_get_decl_name(t, name_type);
348 name = type_get_name(t, name_type);
350 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
351 fprintf(h, "inline ");
353 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
354 fprintf(h, "const ");
356 if (!winrt_mode && type_is_alias(t)) fprintf(h, "%s", t->name);
357 else {
358 switch (type_get_type_detect_alias(t)) {
359 case TYPE_ENUM:
360 if (declonly) fprintf(h, "enum %s", decl_name ? decl_name : "");
361 else if (!t->written) {
362 assert(t->defined);
363 if (decl_name) fprintf(h, "enum %s {\n", decl_name);
364 else fprintf(h, "enum {\n");
365 t->written = TRUE;
366 indentation++;
367 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
368 indent(h, -1);
369 fprintf(h, "}");
371 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
372 else fprintf(h, "enum %s", name ? name : "");
373 break;
374 case TYPE_STRUCT:
375 case TYPE_ENCAPSULATED_UNION:
376 if (declonly) fprintf(h, "struct %s", decl_name ? decl_name : "");
377 else if (!t->written) {
378 assert(t->defined);
379 if (decl_name) fprintf(h, "struct %s {\n", decl_name);
380 else fprintf(h, "struct {\n");
381 t->written = TRUE;
382 indentation++;
383 if (type_get_type(t) != TYPE_STRUCT)
384 write_fields(h, type_encapsulated_union_get_fields(t), name_type);
385 else
386 write_fields(h, type_struct_get_fields(t), name_type);
387 indent(h, -1);
388 fprintf(h, "}");
390 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
391 else fprintf(h, "struct %s", name ? name : "");
392 break;
393 case TYPE_UNION:
394 if (declonly) fprintf(h, "union %s", decl_name ? decl_name : "");
395 else if (!t->written) {
396 assert(t->defined);
397 if (decl_name) fprintf(h, "union %s {\n", decl_name);
398 else fprintf(h, "union {\n");
399 t->written = TRUE;
400 indentation++;
401 write_fields(h, type_union_get_cases(t), name_type);
402 indent(h, -1);
403 fprintf(h, "}");
405 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
406 else fprintf(h, "union %s", name ? name : "");
407 break;
408 case TYPE_POINTER:
410 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
411 write_pointer_left(h, type_pointer_get_ref_type(t));
412 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
413 break;
415 case TYPE_ARRAY:
416 if (t->name && type_array_is_decl_as_ptr(t))
417 fprintf(h, "%s", t->name);
418 else
420 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
421 if (type_array_is_decl_as_ptr(t))
422 write_pointer_left(h, type_array_get_element_type(t));
424 break;
425 case TYPE_FUNCTION:
427 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
429 /* A pointer to a function has to write the calling convention inside
430 * the parentheses. There's no way to handle that here, so we have to
431 * use an extra parameter to tell us whether to write the calling
432 * convention or not. */
433 if (write_callconv)
435 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
436 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
437 if (callconv) fprintf(h, " %s ", callconv);
439 break;
441 case TYPE_BASIC:
442 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
443 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
444 type_basic_get_type(t) != TYPE_BASIC_LONG &&
445 type_basic_get_type(t) != TYPE_BASIC_HYPER)
447 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
448 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
450 switch (type_basic_get_type(t))
452 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
453 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
454 case TYPE_BASIC_INT: fprintf(h, "int"); break;
455 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
456 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
457 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
458 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
459 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
460 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
461 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
462 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
463 case TYPE_BASIC_INT32:
464 if (type_basic_get_sign(t) > 0)
465 fprintf(h, "UINT32");
466 else
467 fprintf(h, "INT32");
468 break;
469 case TYPE_BASIC_LONG:
470 if (type_basic_get_sign(t) > 0)
471 fprintf(h, "ULONG");
472 else
473 fprintf(h, "LONG");
474 break;
475 case TYPE_BASIC_INT64:
476 if (type_basic_get_sign(t) > 0)
477 fprintf(h, "UINT64");
478 else
479 fprintf(h, "INT64");
480 break;
481 case TYPE_BASIC_HYPER:
482 if (type_basic_get_sign(t) > 0)
483 fprintf(h, "MIDL_uhyper");
484 else
485 fprintf(h, "hyper");
486 break;
488 break;
489 case TYPE_INTERFACE:
490 case TYPE_MODULE:
491 case TYPE_COCLASS:
492 fprintf(h, "%s", type_get_name(t, name_type));
493 break;
494 case TYPE_RUNTIMECLASS:
495 fprintf(h, "%s", type_get_name(type_runtimeclass_get_default_iface(t, TRUE), name_type));
496 break;
497 case TYPE_DELEGATE:
498 fprintf(h, "%s", type_get_name(type_delegate_get_iface(t), name_type));
499 break;
500 case TYPE_VOID:
501 fprintf(h, "void");
502 break;
503 case TYPE_BITFIELD:
505 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
506 write_type_left(h, &ds, name_type, declonly, TRUE);
507 break;
509 case TYPE_ALIAS:
511 const decl_spec_t *ds = type_alias_get_aliasee(t);
512 int in_namespace = ds && ds->type && ds->type->namespace && !is_global_namespace(ds->type->namespace);
513 if (!in_namespace) fprintf(h, "%s", t->name);
514 else write_type_left(h, ds, name_type, declonly, write_callconv);
515 break;
517 case TYPE_PARAMETERIZED_TYPE:
519 type_t *iface = type_parameterized_type_get_real_type(t);
520 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
521 args = format_parameterized_type_args(t, "", "_logical");
522 fprintf(h, "%s<%s>", iface->name, args);
523 free(args);
524 break;
526 case TYPE_PARAMETER:
527 fprintf(h, "%s_abi", t->name);
528 break;
529 case TYPE_APICONTRACT:
530 /* shouldn't be here */
531 assert(0);
532 break;
537 void write_type_right(FILE *h, type_t *t, int is_field)
539 if (!h) return;
540 if (type_is_alias(t)) return;
542 switch (type_get_type(t))
544 case TYPE_ARRAY:
546 type_t *elem = type_array_get_element_type(t);
547 if (type_array_is_decl_as_ptr(t))
549 if (decl_needs_parens(elem))
550 fprintf(h, ")");
552 else
554 if (is_conformant_array(t))
555 fprintf(h, "[%s]", is_field ? "1" : "");
556 else
557 fprintf(h, "[%u]", type_array_get_dim(t));
559 write_type_right(h, elem, FALSE);
560 break;
562 case TYPE_FUNCTION:
564 const var_list_t *args = type_function_get_args(t);
565 fputc('(', h);
566 if (args) write_args(h, args, NULL, 0, FALSE, NAME_DEFAULT);
567 else
568 fprintf(h, "void");
569 fputc(')', h);
570 write_type_right(h, type_function_get_rettype(t), FALSE);
571 break;
573 case TYPE_POINTER:
575 type_t *ref = type_pointer_get_ref_type(t);
576 if (decl_needs_parens(ref))
577 fprintf(h, ")");
578 write_type_right(h, ref, FALSE);
579 break;
581 case TYPE_BITFIELD:
582 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
583 break;
584 case TYPE_VOID:
585 case TYPE_BASIC:
586 case TYPE_ENUM:
587 case TYPE_STRUCT:
588 case TYPE_ENCAPSULATED_UNION:
589 case TYPE_UNION:
590 case TYPE_ALIAS:
591 case TYPE_MODULE:
592 case TYPE_COCLASS:
593 case TYPE_INTERFACE:
594 case TYPE_RUNTIMECLASS:
595 case TYPE_DELEGATE:
596 case TYPE_PARAMETERIZED_TYPE:
597 case TYPE_PARAMETER:
598 break;
599 case TYPE_APICONTRACT:
600 /* not supposed to be here */
601 assert(0);
602 break;
606 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)
608 type_t *t = ds->type;
610 if (!h) return;
612 if (t) write_type_left(h, ds, name_type, declonly, TRUE);
614 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
616 if (t)
617 write_type_right(h, t, is_field);
620 static void write_type_definition(FILE *f, type_t *t, int declonly)
622 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
623 int save_written = t->written;
624 decl_spec_t ds = {.type = t};
625 expr_t *contract = get_attrp(t->attrs, ATTR_CONTRACT);
627 if (contract) write_apicontract_guard_start(f, contract);
628 if(in_namespace) {
629 fprintf(f, "#ifdef __cplusplus\n");
630 fprintf(f, "} /* extern \"C\" */\n");
631 write_namespace_start(f, t->namespace);
633 indent(f, 0);
634 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
635 fprintf(f, ";\n");
636 if(in_namespace) {
637 t->written = save_written;
638 write_namespace_end(f, t->namespace);
639 fprintf(f, "extern \"C\" {\n");
640 fprintf(f, "#else\n");
641 write_type_left(f, &ds, NAME_C, declonly, TRUE);
642 fprintf(f, ";\n");
643 if (winrt_mode) write_widl_using_macros(f, t);
644 fprintf(f, "#endif\n\n");
646 if (contract) write_apicontract_guard_end(f, contract);
649 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
651 write_type_v(f, t, FALSE, TRUE, name, NAME_DEFAULT);
654 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
656 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
659 static int user_type_registered(const char *name)
661 user_type_t *ut;
662 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
663 if (!strcmp(name, ut->name))
664 return 1;
665 return 0;
668 static int context_handle_registered(const char *name)
670 context_handle_t *ch;
671 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
672 if (!strcmp(name, ch->name))
673 return 1;
674 return 0;
677 static int generic_handle_registered(const char *name)
679 generic_handle_t *gh;
680 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
681 if (!strcmp(name, gh->name))
682 return 1;
683 return 0;
686 unsigned int get_context_handle_offset( const type_t *type )
688 context_handle_t *ch;
689 unsigned int index = 0;
691 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
693 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
694 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
695 else error( "internal error: %s is not a context handle\n", type->name );
697 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
699 if (!strcmp( type->name, ch->name )) return index;
700 index++;
702 error( "internal error: %s is not registered as a context handle\n", type->name );
703 return index;
706 unsigned int get_generic_handle_offset( const type_t *type )
708 generic_handle_t *gh;
709 unsigned int index = 0;
711 while (!is_attr( type->attrs, ATTR_HANDLE ))
713 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
714 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
715 else error( "internal error: %s is not a generic handle\n", type->name );
717 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
719 if (!strcmp( type->name, gh->name )) return index;
720 index++;
722 error( "internal error: %s is not registered as a generic handle\n", type->name );
723 return index;
726 /* check for types which require additional prototypes to be generated in the
727 * header */
728 void check_for_additional_prototype_types(type_t *type)
730 if (!type) return;
731 for (;;) {
732 const char *name = type->name;
733 if (type->user_types_registered) break;
734 type->user_types_registered = 1;
735 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
736 if (!context_handle_registered(name))
738 context_handle_t *ch = xmalloc(sizeof(*ch));
739 ch->name = xstrdup(name);
740 list_add_tail(&context_handle_list, &ch->entry);
742 /* don't carry on parsing fields within this type */
743 break;
745 if ((type_get_type(type) != TYPE_BASIC ||
746 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
747 is_attr(type->attrs, ATTR_HANDLE)) {
748 if (!generic_handle_registered(name))
750 generic_handle_t *gh = xmalloc(sizeof(*gh));
751 gh->name = xstrdup(name);
752 list_add_tail(&generic_handle_list, &gh->entry);
754 /* don't carry on parsing fields within this type */
755 break;
757 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
758 if (!user_type_registered(name))
760 user_type_t *ut = xmalloc(sizeof *ut);
761 ut->name = xstrdup(name);
762 list_add_tail(&user_type_list, &ut->entry);
764 /* don't carry on parsing fields within this type as we are already
765 * using a wire marshaled type */
766 break;
768 else if (type_is_complete(type))
770 var_list_t *vars;
771 const var_t *v;
772 switch (type_get_type_detect_alias(type))
774 case TYPE_ENUM:
775 vars = type_enum_get_values(type);
776 break;
777 case TYPE_STRUCT:
778 vars = type_struct_get_fields(type);
779 break;
780 case TYPE_UNION:
781 vars = type_union_get_cases(type);
782 break;
783 default:
784 vars = NULL;
785 break;
787 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
788 check_for_additional_prototype_types(v->declspec.type);
791 if (type_is_alias(type))
792 type = type_alias_get_aliasee_type(type);
793 else if (is_ptr(type))
794 type = type_pointer_get_ref_type(type);
795 else if (is_array(type))
796 type = type_array_get_element_type(type);
797 else
798 break;
802 static int write_serialize_function_decl(FILE *header, const type_t *type)
804 write_serialize_functions(header, type, NULL);
805 return 1;
808 static int serializable_exists(FILE *header, const type_t *type)
810 return 0;
813 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
814 int (*proc)(FILE*, const type_t*))
816 statement_t *stmt, *iface_stmt;
817 statement_list_t *iface_stmts;
818 typeref_t *ref;
820 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
822 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
823 continue;
825 iface_stmts = type_iface_get_stmts(stmt->u.type);
826 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
828 if (iface_stmt->type != STMT_TYPEDEF) continue;
829 if (iface_stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, iface_stmt->u.type_list, typeref_t, entry)
831 if (!is_attr(ref->type->attrs, ATTR_ENCODE)
832 && !is_attr(ref->type->attrs, ATTR_DECODE))
833 continue;
834 if (!proc(header, ref->type))
835 return 0;
840 return 1;
843 static void write_user_types(FILE *header)
845 user_type_t *ut;
846 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
848 const char *name = ut->name;
849 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
850 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
851 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
852 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
856 static void write_context_handle_rundowns(FILE *header)
858 context_handle_t *ch;
859 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
861 const char *name = ch->name;
862 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
866 static void write_generic_handle_routines(FILE *header)
868 generic_handle_t *gh;
869 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
871 const char *name = gh->name;
872 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
873 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
877 static void write_typedef(FILE *header, type_t *type, int declonly)
879 type_t *t = type_alias_get_aliasee_type(type);
880 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace))
882 fprintf(header, "#ifndef __cplusplus\n");
883 fprintf(header, "typedef ");
884 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->c_name, NAME_C);
885 fprintf(header, ";\n");
886 if (type_get_type_detect_alias(t) != TYPE_ENUM)
888 fprintf(header, "#else /* __cplusplus */\n");
889 write_namespace_start(header, t->namespace);
890 indent(header, 0);
891 fprintf(header, "typedef ");
892 write_type_v(header, type_alias_get_aliasee(type), FALSE, TRUE, type->name, NAME_DEFAULT);
893 fprintf(header, ";\n");
894 write_namespace_end(header, t->namespace);
896 fprintf(header, "#endif /* __cplusplus */\n\n");
898 else
900 fprintf(header, "typedef ");
901 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name, NAME_DEFAULT);
902 fprintf(header, ";\n");
906 int is_const_decl(const var_t *var)
908 const decl_spec_t *t;
909 /* strangely, MIDL accepts a const attribute on any pointer in the
910 * declaration to mean that data isn't being instantiated. this appears
911 * to be a bug, but there is no benefit to being incompatible with MIDL,
912 * so we'll do the same thing */
913 for (t = &var->declspec; ; )
915 if (t->qualifier & TYPE_QUALIFIER_CONST)
916 return TRUE;
917 else if (is_ptr(t->type))
918 t = type_pointer_get_ref(t->type);
919 else break;
921 return FALSE;
924 static void write_declaration(FILE *header, const var_t *v)
926 if (is_const_decl(v) && v->eval)
928 fprintf(header, "#define %s (", v->name);
929 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
930 fprintf(header, ")\n\n");
932 else
934 switch (v->declspec.stgclass)
936 case STG_NONE:
937 case STG_REGISTER: /* ignored */
938 break;
939 case STG_STATIC:
940 fprintf(header, "static ");
941 break;
942 case STG_EXTERN:
943 fprintf(header, "extern ");
944 break;
946 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name, NAME_DEFAULT);
947 fprintf(header, ";\n\n");
951 static void write_library(FILE *header, const typelib_t *typelib)
953 const struct uuid *uuid = get_attrp(typelib->attrs, ATTR_UUID);
954 fprintf(header, "\n");
955 write_guid(header, "LIBID", typelib->name, uuid);
956 fprintf(header, "\n");
960 const type_t* get_explicit_generic_handle_type(const var_t* var)
962 const type_t *t;
963 for (t = var->declspec.type;
964 is_ptr(t) || type_is_alias(t);
965 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
966 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
967 is_attr(t->attrs, ATTR_HANDLE))
968 return t;
969 return NULL;
972 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
973 unsigned char *explicit_fc, unsigned char *implicit_fc )
975 const var_t *var;
976 const var_list_t *args = type_function_get_args( func->declspec.type );
978 *explicit_fc = *implicit_fc = 0;
979 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
981 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
982 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
984 *explicit_fc = FC_BIND_PRIMITIVE;
985 return var;
987 if (get_explicit_generic_handle_type( var ))
989 *explicit_fc = FC_BIND_GENERIC;
990 return var;
992 if (is_context_handle( var->declspec.type ))
994 *explicit_fc = FC_BIND_CONTEXT;
995 return var;
999 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
1001 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
1002 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
1003 *implicit_fc = FC_BIND_PRIMITIVE;
1004 else
1005 *implicit_fc = FC_BIND_GENERIC;
1006 return var;
1009 *implicit_fc = FC_AUTO_HANDLE;
1010 return NULL;
1013 int has_out_arg_or_return(const var_t *func)
1015 const var_t *var;
1017 if (!is_void(type_function_get_rettype(func->declspec.type)))
1018 return 1;
1020 if (!type_function_get_args(func->declspec.type))
1021 return 0;
1023 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1024 if (is_attr(var->attrs, ATTR_OUT))
1025 return 1;
1027 return 0;
1031 /********** INTERFACES **********/
1033 int is_object(const type_t *iface)
1035 const attr_t *attr;
1036 if (type_is_defined(iface) && (type_get_type(iface) == TYPE_DELEGATE || type_iface_get_inherit(iface)))
1037 return 1;
1038 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
1039 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
1040 return 0;
1043 int is_local(const attr_list_t *a)
1045 return is_attr(a, ATTR_LOCAL);
1048 const var_t *is_callas(const attr_list_t *a)
1050 return get_attrp(a, ATTR_CALLAS);
1053 static int is_inherited_method(const type_t *iface, const var_t *func)
1055 while ((iface = type_iface_get_inherit(iface)))
1057 const statement_t *stmt;
1058 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1060 const var_t *funccmp = stmt->u.var;
1062 if (!is_callas(func->attrs))
1064 char inherit_name[256];
1065 /* compare full name including property prefix */
1066 strcpy(inherit_name, get_name(funccmp));
1067 if (!strcmp(inherit_name, get_name(func))) return 1;
1072 return 0;
1075 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
1077 if (iface == child)
1078 return 0;
1082 const statement_t *stmt;
1083 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
1085 const var_t *funccmp = stmt->u.var;
1087 if (!is_callas(func->attrs))
1089 char inherit_name[256];
1090 /* compare full name including property prefix */
1091 strcpy(inherit_name, get_name(funccmp));
1092 if (!strcmp(inherit_name, get_name(func))) return 1;
1096 while ((child = type_iface_get_inherit(child)) && child != iface);
1098 return 0;
1101 static int is_aggregate_return(const var_t *func)
1103 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1104 return type == TYPE_STRUCT || type == TYPE_UNION ||
1105 type == TYPE_COCLASS || type == TYPE_INTERFACE ||
1106 type == TYPE_RUNTIMECLASS;
1109 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1111 static char buff[255];
1112 if (is_inherited_method(iface, func))
1113 sprintf(buff, "%s_%s", iface->name, get_name(func));
1114 else
1115 sprintf(buff, "%s", get_name(func));
1116 return buff;
1119 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1121 const statement_t *stmt;
1122 int first_iface = 1;
1124 if (type_iface_get_inherit(iface))
1125 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1127 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1129 const var_t *func = stmt->u.var;
1131 if (first_iface)
1133 fprintf(header, "/*** %s methods ***/\n", iface->name);
1134 first_iface = 0;
1137 if (is_override_method(iface, child, func))
1138 continue;
1140 if (!is_callas(func->attrs)) {
1141 const var_t *arg;
1143 fprintf(header, "#define %s_%s(This", name, get_name(func));
1144 if (type_function_get_args(func->declspec.type))
1145 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1146 fprintf(header, ",%s", arg->name);
1147 fprintf(header, ") ");
1149 if (is_aggregate_return(func))
1151 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1152 continue;
1155 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1156 if (type_function_get_args(func->declspec.type))
1157 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1158 fprintf(header, ",%s", arg->name);
1159 fprintf(header, ")\n");
1164 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent, enum name_type name_type)
1166 const var_t *arg;
1167 int count = 0;
1169 if (do_indent)
1171 indentation++;
1172 indent(h, 0);
1174 if (method == 1) {
1175 fprintf(h, "%s* This", name);
1176 count++;
1178 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1179 if (count) {
1180 if (do_indent)
1182 fprintf(h, ",\n");
1183 indent(h, 0);
1185 else fprintf(h, ",");
1187 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1188 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1189 * defining UDTs inside of an argument list. */
1190 write_type_v(h, &arg->declspec, FALSE, TRUE, arg->name, name_type);
1191 if (method == 2) {
1192 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1193 if (expr) {
1194 const var_t *tail_arg;
1196 /* Output default value only if all following arguments also have default value. */
1197 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1198 if(tail_arg == arg) {
1199 expr_t bstr;
1201 /* Fixup the expression type for a BSTR like midl does. */
1202 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1204 bstr = *expr;
1205 bstr.type = EXPR_WSTRLIT;
1206 expr = &bstr;
1209 fprintf(h, " = ");
1210 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1211 break;
1213 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1214 break;
1218 count++;
1220 if (do_indent) indentation--;
1223 static void write_cpp_method_def(FILE *header, const type_t *iface)
1225 const statement_t *stmt;
1227 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1229 const var_t *func = stmt->u.var;
1230 if (!is_callas(func->attrs)) {
1231 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1232 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1233 const var_list_t *args = type_function_get_args(func->declspec.type);
1234 const var_t *arg;
1236 if (!callconv) callconv = "STDMETHODCALLTYPE";
1238 if (is_aggregate_return(func)) {
1239 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1241 indent(header, 0);
1242 fprintf(header, "virtual ");
1243 write_type_decl_left(header, ret);
1244 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1245 ++indentation;
1246 indent(header, 0);
1247 write_type_decl_left(header, ret);
1248 fprintf(header, " *__ret");
1249 --indentation;
1250 if (args) {
1251 fprintf(header, ",\n");
1252 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1254 fprintf(header, ") = 0;\n");
1256 indent(header, 0);
1257 write_type_decl_left(header, ret);
1258 fprintf(header, " %s %s(\n", callconv, get_name(func));
1259 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1260 fprintf(header, ")\n");
1261 indent(header, 0);
1262 fprintf(header, "{\n");
1263 ++indentation;
1264 indent(header, 0);
1265 write_type_decl_left(header, ret);
1266 fprintf(header, " __ret;\n");
1267 indent(header, 0);
1268 fprintf(header, "return *%s(&__ret", get_name(func));
1269 if (args)
1270 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1271 fprintf(header, ", %s", arg->name);
1272 fprintf(header, ");\n");
1273 --indentation;
1274 indent(header, 0);
1275 fprintf(header, "}\n");
1277 fprintf(header, "#else\n");
1280 indent(header, 0);
1281 fprintf(header, "virtual ");
1282 write_type_decl_left(header, ret);
1283 fprintf(header, " %s %s(\n", callconv, get_name(func));
1284 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1285 fprintf(header, ") = 0;\n");
1287 if (is_aggregate_return(func))
1288 fprintf(header, "#endif\n");
1289 fprintf(header, "\n");
1294 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1296 const statement_t *stmt;
1297 int first_iface = 1;
1299 if (type_iface_get_inherit(iface))
1300 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1302 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1304 const var_t *func = stmt->u.var;
1306 if (first_iface)
1308 fprintf(header, "/*** %s methods ***/\n", iface->name);
1309 first_iface = 0;
1312 if (is_override_method(iface, child, func))
1313 continue;
1315 if (!is_callas(func->attrs)) {
1316 const var_t *arg;
1318 fprintf(header, "static __WIDL_INLINE ");
1319 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1320 fprintf(header, " %s_%s(", name, get_name(func));
1321 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE, NAME_C);
1322 fprintf(header, ") {\n");
1323 ++indentation;
1324 if (!is_aggregate_return(func)) {
1325 indent(header, 0);
1326 fprintf(header, "%sThis->lpVtbl->%s(This",
1327 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1328 get_vtbl_entry_name(iface, func));
1329 } else {
1330 indent(header, 0);
1331 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1332 fprintf(header, " __ret;\n");
1333 indent(header, 0);
1334 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1336 if (type_function_get_args(func->declspec.type))
1337 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1338 fprintf(header, ",%s", arg->name);
1339 fprintf(header, ");\n");
1340 --indentation;
1341 fprintf(header, "}\n");
1346 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1348 const statement_t *stmt;
1349 int first_iface = 1;
1351 if (type_iface_get_inherit(iface))
1352 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1354 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1356 const var_t *func = stmt->u.var;
1357 if (first_iface) {
1358 indent(header, 0);
1359 fprintf(header, "/*** %s methods ***/\n", iface->name);
1360 first_iface = 0;
1362 if (!is_callas(func->attrs)) {
1363 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1364 if (!callconv) callconv = "STDMETHODCALLTYPE";
1365 indent(header, 0);
1366 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1367 if (is_aggregate_return(func))
1368 fprintf(header, " *");
1369 if (is_inherited_method(iface, func))
1370 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1371 else
1372 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1373 ++indentation;
1374 indent(header, 0);
1375 fprintf(header, "%s *This", name);
1376 if (is_aggregate_return(func)) {
1377 fprintf(header, ",\n");
1378 indent(header, 0);
1379 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1380 fprintf(header, " *__ret");
1382 --indentation;
1383 if (type_function_get_args(func->declspec.type)) {
1384 fprintf(header, ",\n");
1385 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE, NAME_C);
1387 fprintf(header, ");\n");
1388 fprintf(header, "\n");
1393 static void write_c_method_def(FILE *header, const type_t *iface)
1395 do_write_c_method_def(header, iface, iface->c_name);
1398 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1400 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1403 static void write_method_proto(FILE *header, const type_t *iface)
1405 const statement_t *stmt;
1407 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1409 const var_t *func = stmt->u.var;
1411 if (is_callas(func->attrs)) {
1412 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1413 if (!callconv) callconv = "STDMETHODCALLTYPE";
1414 /* proxy prototype */
1415 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1416 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1417 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1418 fprintf(header, ");\n");
1419 /* stub prototype */
1420 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1421 fprintf(header, " IRpcStubBuffer* This,\n");
1422 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1423 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1424 fprintf(header, " DWORD* pdwStubPhase);\n");
1429 static void write_locals(FILE *fp, const type_t *iface, int body)
1431 static const char comment[]
1432 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1433 const statement_t *stmt;
1435 if (!is_object(iface))
1436 return;
1438 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1439 const var_t *func = stmt->u.var;
1440 const var_t *cas = is_callas(func->attrs);
1442 if (cas) {
1443 const statement_t *stmt2 = NULL;
1444 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1445 if (!strcmp(get_name(stmt2->u.var), cas->name))
1446 break;
1447 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1448 const var_t *m = stmt2->u.var;
1449 /* proxy prototype - use local prototype */
1450 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1451 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1452 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1453 fprintf(fp, ")");
1454 if (body) {
1455 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1456 fprintf(fp, "\n{\n");
1457 fprintf(fp, " %s\n", comment);
1458 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1459 fprintf(fp, " return E_NOTIMPL;\n");
1460 else if (type_get_type(rt->type) != TYPE_VOID) {
1461 fprintf(fp, " ");
1462 write_type_decl(fp, rt, "rv");
1463 fprintf(fp, ";\n");
1464 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1465 fprintf(fp, " return rv;\n");
1467 fprintf(fp, "}\n\n");
1469 else
1470 fprintf(fp, ";\n");
1471 /* stub prototype - use remotable prototype */
1472 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1473 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1474 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1475 fprintf(fp, ")");
1476 if (body)
1477 /* Remotable methods must all return HRESULTs. */
1478 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1479 else
1480 fprintf(fp, ";\n");
1482 else
1483 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1488 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1490 const statement_t *stmt;
1491 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1493 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1494 write_locals(local_stubs, stmt->u.type, TRUE);
1498 void write_local_stubs(const statement_list_t *stmts)
1500 FILE *local_stubs;
1502 if (!local_stubs_name) return;
1504 local_stubs = fopen(local_stubs_name, "w");
1505 if (!local_stubs) {
1506 error("Could not open %s for output\n", local_stubs_name);
1507 return;
1509 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1510 fprintf(local_stubs, "#include <objbase.h>\n");
1511 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1513 write_local_stubs_stmts(local_stubs, stmts);
1515 fclose(local_stubs);
1518 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1520 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1522 if (!callconv) callconv = "__cdecl";
1523 /* FIXME: do we need to handle call_as? */
1524 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1525 fprintf(header, " %s ", callconv);
1526 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1527 if (type_function_get_args(fun->declspec.type))
1528 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE, NAME_DEFAULT);
1529 else
1530 fprintf(header, " void");
1531 fprintf(header, ");\n\n");
1534 static void write_parameterized_type_forward(FILE *header, type_t *type)
1536 type_t *iface = type->details.parameterized.type;
1537 char *args;
1539 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1541 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1542 write_namespace_start(header, type->namespace);
1544 args = format_parameterized_type_args(type, "class ", "");
1545 write_line(header, 0, "template <%s>", args);
1546 write_line(header, 0, "struct %s_impl;\n", iface->name);
1548 write_line(header, 0, "template <%s>", args);
1549 free(args);
1550 args = format_parameterized_type_args(type, "", "");
1551 write_line(header, 0, "struct %s : %s_impl<%s> {};", iface->name, iface->name, args);
1552 free(args);
1554 write_namespace_end(header, type->namespace);
1555 fprintf(header, "#endif\n\n" );
1558 static void write_parameterized_implementation(FILE *header, type_t *type, int declonly)
1560 const statement_t *stmt;
1561 typeref_list_t *params = type->details.parameterized.params;
1562 typeref_t *ref;
1563 type_t *iface = type->details.parameterized.type, *base;
1564 char *args = NULL;
1566 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1567 write_line(header, 0, "} /* extern \"C\" */");
1568 write_namespace_start(header, type->namespace);
1570 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1571 base = type_iface_get_inherit(iface);
1573 args = format_parameterized_type_args(type, "class ", "");
1574 write_line(header, 0, "template <%s>", args);
1575 free(args);
1576 write_line(header, 0, "struct %s_impl%s", iface->name, base ? strmake(" : %s", base->name) : "");
1577 write_line(header, 0, "{");
1579 write_line(header, 1, "private:");
1580 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1582 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetAbiType<%s>::type %s_abi;", ref->type->name, ref->type->name);
1583 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetLogicalType<%s>::type %s_logical;", ref->type->name, ref->type->name);
1585 indentation -= 1;
1587 write_line(header, 1, "public:");
1588 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1589 write_line(header, 0, "typedef %s %s_complex;", ref->type->name, ref->type->name);
1591 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1593 const var_t *func = stmt->u.var;
1594 if (is_callas(func->attrs)) continue;
1595 indent(header, 1);
1596 fprintf(header, "virtual ");
1597 write_type_decl_left(header, &func->declspec);
1598 fprintf(header, "%s(", get_name(func));
1599 write_args(header, type_function_get_args(func->declspec.type), NULL, 0, 0, NAME_DEFAULT);
1600 fprintf(header, ") = 0;\n");
1601 indentation -= 1;
1603 write_line(header, -1, "};");
1605 write_namespace_end(header, type->namespace);
1606 write_line(header, 0, "extern \"C\" {");
1607 write_line(header, 0, "#endif\n");
1610 static void write_forward(FILE *header, type_t *iface)
1612 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1613 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1614 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1615 fprintf(header, "#ifdef __cplusplus\n");
1616 if (iface->namespace && !is_global_namespace(iface->namespace))
1617 fprintf(header, "#define %s %s\n", iface->c_name, iface->qualified_name);
1618 if (!iface->impl_name)
1620 write_namespace_start(header, iface->namespace);
1621 write_line(header, 0, "interface %s;", iface->name);
1622 write_namespace_end(header, iface->namespace);
1624 fprintf(header, "#endif /* __cplusplus */\n");
1625 fprintf(header, "#endif\n\n" );
1628 static char *format_apicontract_macro(const type_t *type)
1630 char *name = format_namespace(type->namespace, "", "_", type->name, NULL);
1631 int i;
1632 for (i = strlen(name); i > 0; --i) name[i - 1] = toupper(name[i - 1]);
1633 return name;
1636 static void write_apicontract_guard_start(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, "#if %s_VERSION >= %#x\n", name, ver);
1646 free(name);
1649 static void write_apicontract_guard_end(FILE *header, const expr_t *expr)
1651 const type_t *type;
1652 char *name;
1653 int ver;
1654 if (!winrt_mode) return;
1655 type = expr->u.tref.type;
1656 ver = expr->ref->u.lval;
1657 name = format_apicontract_macro(type);
1658 fprintf(header, "#endif /* %s_VERSION >= %#x */\n", name, ver);
1659 free(name);
1662 static void write_com_interface_start(FILE *header, const type_t *iface)
1664 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1665 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1666 fprintf(header, "/*****************************************************************************\n");
1667 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1668 fprintf(header, " */\n");
1669 if (contract) write_apicontract_guard_start(header, contract);
1670 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1671 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1674 static void write_widl_using_method_macros(FILE *header, const type_t *iface, const type_t *top_iface)
1676 const statement_t *stmt;
1677 const char *name = top_iface->short_name ? top_iface->short_name : top_iface->name;
1679 if (type_iface_get_inherit(iface)) write_widl_using_method_macros(header, type_iface_get_inherit(iface), top_iface);
1681 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1683 const var_t *func = stmt->u.var;
1684 const char *func_name;
1686 if (is_override_method(iface, top_iface, func)) continue;
1687 if (is_callas(func->attrs)) continue;
1689 func_name = get_name(func);
1690 fprintf(header, "#define %s_%s %s_%s\n", name, func_name, top_iface->c_name, func_name);
1694 static void write_widl_using_macros(FILE *header, type_t *iface)
1696 const struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
1697 const char *name = iface->short_name ? iface->short_name : iface->name;
1698 char *macro;
1700 if (!strcmp(iface->name, iface->c_name)) return;
1702 macro = format_namespace(iface->namespace, "WIDL_using_", "_", NULL, NULL);
1703 fprintf(header, "#ifdef %s\n", macro);
1705 if (uuid) fprintf(header, "#define IID_%s IID_%s\n", name, iface->c_name);
1706 if (iface->type_type == TYPE_INTERFACE) fprintf(header, "#define %sVtbl %sVtbl\n", name, iface->c_name);
1707 fprintf(header, "#define %s %s\n", name, iface->c_name);
1709 if (iface->type_type == TYPE_INTERFACE) write_widl_using_method_macros(header, iface, iface);
1711 fprintf(header, "#endif /* %s */\n", macro);
1712 free(macro);
1715 static void write_com_interface_end(FILE *header, type_t *iface)
1717 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1718 const struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
1719 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1720 type_t *type;
1722 if (uuid)
1723 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1725 /* C++ interface */
1726 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1727 if (!is_global_namespace(iface->namespace)) {
1728 write_line(header, 0, "} /* extern \"C\" */");
1729 write_namespace_start(header, iface->namespace);
1731 if (uuid) {
1732 if (strchr(iface->name, '<')) write_line(header, 0, "template<>");
1733 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1734 indent(header, 0);
1735 }else {
1736 indent(header, 0);
1737 if (strchr(iface->name, '<')) fprintf(header, "template<> struct ");
1738 else fprintf(header, "interface ");
1740 if (iface->impl_name)
1742 fprintf(header, "%s : %s\n", iface->name, iface->impl_name);
1743 write_line(header, 1, "{");
1745 else if (type_iface_get_inherit(iface))
1747 fprintf(header, "%s : public %s\n", iface->name,
1748 type_iface_get_inherit(iface)->name);
1749 write_line(header, 1, "{");
1751 else
1753 fprintf(header, "%s\n", iface->name);
1754 write_line(header, 1, "{\n");
1755 write_line(header, 0, "BEGIN_INTERFACE\n");
1757 /* dispinterfaces don't have real functions, so don't write C++ functions for
1758 * them */
1759 if (!dispinterface && !iface->impl_name)
1760 write_cpp_method_def(header, iface);
1761 if (!type_iface_get_inherit(iface) && !iface->impl_name)
1762 write_line(header, 0, "END_INTERFACE\n");
1763 write_line(header, -1, "};");
1764 if (!is_global_namespace(iface->namespace)) {
1765 write_namespace_end(header, iface->namespace);
1766 write_line(header, 0, "extern \"C\" {");
1768 if (uuid)
1769 write_uuid_decl(header, iface, uuid);
1770 fprintf(header, "#else\n");
1771 /* C interface */
1772 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1773 write_line(header, 0, "BEGIN_INTERFACE\n");
1774 if (dispinterface)
1775 write_c_disp_method_def(header, iface);
1776 else
1777 write_c_method_def(header, iface);
1778 write_line(header, 0, "END_INTERFACE");
1779 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1780 fprintf(header, "interface %s {\n", iface->c_name);
1781 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1782 fprintf(header, "};\n\n");
1783 fprintf(header, "#ifdef COBJMACROS\n");
1784 /* dispinterfaces don't have real functions, so don't write macros for them,
1785 * only for the interface this interface inherits from, i.e. IDispatch */
1786 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1787 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1788 write_method_macro(header, type, type, iface->c_name);
1789 fprintf(header, "#else\n");
1790 write_inline_wrappers(header, type, type, iface->c_name);
1791 fprintf(header, "#endif\n");
1792 if (winrt_mode) write_widl_using_macros(header, iface);
1793 fprintf(header, "#endif\n");
1794 fprintf(header, "\n");
1795 fprintf(header, "#endif\n");
1796 fprintf(header, "\n");
1797 /* dispinterfaces don't have real functions, so don't write prototypes for
1798 * them */
1799 if (!dispinterface && !winrt_mode)
1801 write_method_proto(header, iface);
1802 write_locals(header, iface, FALSE);
1803 fprintf(header, "\n");
1805 fprintf(header, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface->c_name, dispinterface ? "DISP" : "");
1806 if (contract) write_apicontract_guard_end(header, contract);
1807 fprintf(header, "\n");
1810 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1812 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1813 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1814 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1816 fprintf(header, "/*****************************************************************************\n");
1817 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1818 fprintf(header, " */\n");
1819 if (contract) write_apicontract_guard_start(header, contract);
1820 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1821 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1822 if (var)
1824 fprintf(header, "extern ");
1825 write_type_decl( header, &var->declspec, var->name );
1826 fprintf(header, ";\n");
1828 if (old_names)
1830 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1831 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1833 else
1835 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1836 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1837 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1838 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1842 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1844 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1845 fprintf(header, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface->name);
1846 if (contract) write_apicontract_guard_end(header, contract);
1847 fprintf(header, "\n");
1850 static void write_coclass(FILE *header, type_t *cocl)
1852 const struct uuid *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1854 fprintf(header, "/*****************************************************************************\n");
1855 fprintf(header, " * %s coclass\n", cocl->name);
1856 fprintf(header, " */\n\n");
1857 if (uuid)
1858 write_guid(header, "CLSID", cocl->name, uuid);
1859 fprintf(header, "\n#ifdef __cplusplus\n");
1860 if (uuid)
1862 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1863 write_uuid_decl(header, cocl, uuid);
1865 else
1867 fprintf(header, "class %s;\n", cocl->name);
1869 fprintf(header, "#endif\n");
1870 fprintf(header, "\n");
1873 static void write_coclass_forward(FILE *header, type_t *cocl)
1875 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1876 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1877 fprintf(header, "#ifdef __cplusplus\n");
1878 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1879 fprintf(header, "#else\n");
1880 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1881 fprintf(header, "#endif /* defined __cplusplus */\n");
1882 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1885 static void write_apicontract(FILE *header, type_t *apicontract)
1887 char *name = format_apicontract_macro(apicontract);
1888 fprintf(header, "#if !defined(%s_VERSION)\n", name);
1889 fprintf(header, "#define %s_VERSION %#x\n", name, get_attrv(apicontract->attrs, ATTR_CONTRACTVERSION));
1890 fprintf(header, "#endif // defined(%s_VERSION)\n\n", name);
1891 free(name);
1894 static void write_runtimeclass(FILE *header, type_t *runtimeclass)
1896 expr_t *contract = get_attrp(runtimeclass->attrs, ATTR_CONTRACT);
1897 char *name, *c_name;
1898 size_t i, len;
1899 name = format_namespace(runtimeclass->namespace, "", ".", runtimeclass->name, NULL);
1900 c_name = format_namespace(runtimeclass->namespace, "", "_", runtimeclass->name, NULL);
1901 fprintf(header, "/*\n");
1902 fprintf(header, " * Class %s\n", name);
1903 fprintf(header, " */\n");
1904 if (contract) write_apicontract_guard_start(header, contract);
1905 fprintf(header, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name);
1906 fprintf(header, "#define RUNTIMECLASS_%s_DEFINED\n", c_name);
1907 fprintf(header, "#if !defined(_MSC_VER) && !defined(__MINGW32__)\n");
1908 fprintf(header, "static const WCHAR RuntimeClass_%s[] = {", c_name);
1909 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1910 fprintf(header, "0};\n");
1911 fprintf(header, "#elif defined(__GNUC__) && !defined(__cplusplus)\n");
1912 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1913 fprintf(header, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name, name);
1914 fprintf(header, "#else\n");
1915 fprintf(header, "extern const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = {", c_name);
1916 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1917 fprintf(header, "0};\n");
1918 fprintf(header, "#endif\n");
1919 fprintf(header, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name);
1920 free(c_name);
1921 free(name);
1922 if (contract) write_apicontract_guard_end(header, contract);
1923 fprintf(header, "\n");
1926 static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
1928 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1929 fprintf(header, "#define __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1930 fprintf(header, "#ifdef __cplusplus\n");
1931 write_namespace_start(header, runtimeclass->namespace);
1932 write_line(header, 0, "class %s;", runtimeclass->name);
1933 write_namespace_end(header, runtimeclass->namespace);
1934 fprintf(header, "#else\n");
1935 fprintf(header, "typedef struct %s %s;\n", runtimeclass->c_name, runtimeclass->c_name);
1936 fprintf(header, "#endif /* defined __cplusplus */\n");
1937 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass->c_name);
1940 static void write_import(FILE *header, const char *fname)
1942 char *hname = replace_extension( get_basename(fname), ".idl", "" );
1944 if (!strendswith( hname, ".h" )) hname = strmake( "%s.h", hname );
1945 fprintf(header, "#include <%s>\n", hname);
1946 free(hname);
1949 static void write_imports(FILE *header, const statement_list_t *stmts)
1951 const statement_t *stmt;
1952 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1954 switch (stmt->type)
1956 case STMT_TYPE:
1957 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1958 write_imports(header, type_iface_get_stmts(stmt->u.type));
1959 break;
1960 case STMT_TYPEREF:
1961 case STMT_IMPORTLIB:
1962 /* not included in header */
1963 break;
1964 case STMT_IMPORT:
1965 write_import(header, stmt->u.str);
1966 break;
1967 case STMT_TYPEDEF:
1968 case STMT_MODULE:
1969 case STMT_CPPQUOTE:
1970 case STMT_PRAGMA:
1971 case STMT_DECLARATION:
1972 /* not processed here */
1973 break;
1974 case STMT_LIBRARY:
1975 write_imports(header, stmt->u.lib->stmts);
1976 break;
1981 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1983 const statement_t *stmt;
1984 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1986 switch (stmt->type)
1988 case STMT_TYPE:
1989 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
1991 type_t *iface = stmt->u.type;
1992 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1993 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1995 write_forward(header, iface);
1996 if (type_iface_get_async_iface(iface))
1997 write_forward(header, type_iface_get_async_iface(iface));
2000 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
2001 write_coclass_forward(header, stmt->u.type);
2002 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
2003 write_runtimeclass_forward(header, stmt->u.type);
2004 else if (type_get_type(stmt->u.type) == TYPE_PARAMETERIZED_TYPE)
2005 write_parameterized_type_forward(header, stmt->u.type);
2006 break;
2007 case STMT_TYPEREF:
2008 case STMT_IMPORTLIB:
2009 /* not included in header */
2010 break;
2011 case STMT_IMPORT:
2012 case STMT_TYPEDEF:
2013 case STMT_MODULE:
2014 case STMT_CPPQUOTE:
2015 case STMT_PRAGMA:
2016 case STMT_DECLARATION:
2017 /* not processed here */
2018 break;
2019 case STMT_LIBRARY:
2020 write_forward_decls(header, stmt->u.lib->stmts);
2021 break;
2026 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
2028 const statement_t *stmt;
2029 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2031 switch (stmt->type)
2033 case STMT_TYPE:
2034 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
2036 type_t *iface = stmt->u.type, *async_iface;
2037 if (type_get_type(stmt->u.type) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
2038 async_iface = type_iface_get_async_iface(iface);
2039 if (is_object(iface)) is_object_interface++;
2040 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
2042 write_com_interface_start(header, iface);
2043 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
2044 write_com_interface_end(header, iface);
2045 if (async_iface)
2047 write_com_interface_start(header, async_iface);
2048 write_com_interface_end(header, async_iface);
2051 else
2053 write_rpc_interface_start(header, iface);
2054 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
2055 write_rpc_interface_end(header, iface);
2057 if (is_object(iface)) is_object_interface--;
2059 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
2060 write_coclass(header, stmt->u.type);
2061 else if (type_get_type(stmt->u.type) == TYPE_APICONTRACT)
2062 write_apicontract(header, stmt->u.type);
2063 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
2064 write_runtimeclass(header, stmt->u.type);
2065 else if (type_get_type(stmt->u.type) != TYPE_PARAMETERIZED_TYPE)
2066 write_type_definition(header, stmt->u.type, stmt->declonly);
2067 else
2069 is_object_interface++;
2070 write_parameterized_implementation(header, stmt->u.type, stmt->declonly);
2071 is_object_interface--;
2073 break;
2074 case STMT_TYPEREF:
2075 /* FIXME: shouldn't write out forward declarations for undefined
2076 * interfaces but a number of our IDL files depend on this */
2077 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
2078 write_forward(header, stmt->u.type);
2079 break;
2080 case STMT_IMPORTLIB:
2081 case STMT_MODULE:
2082 case STMT_PRAGMA:
2083 /* not included in header */
2084 break;
2085 case STMT_IMPORT:
2086 /* not processed here */
2087 break;
2088 case STMT_TYPEDEF:
2090 typeref_t *ref;
2091 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
2092 write_typedef(header, ref->type, stmt->declonly);
2093 break;
2095 case STMT_LIBRARY:
2096 fprintf(header, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2097 fprintf(header, "#define __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2098 write_library(header, stmt->u.lib);
2099 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
2100 fprintf(header, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt->u.lib->name);
2101 break;
2102 case STMT_CPPQUOTE:
2103 fprintf(header, "%s\n", stmt->u.str);
2104 break;
2105 case STMT_DECLARATION:
2106 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
2108 if (!ignore_funcs)
2110 int prefixes_differ = strcmp(prefix_client, prefix_server);
2112 if (prefixes_differ)
2114 fprintf(header, "/* client prototype */\n");
2115 write_function_proto(header, iface, stmt->u.var, prefix_client);
2116 fprintf(header, "/* server prototype */\n");
2118 write_function_proto(header, iface, stmt->u.var, prefix_server);
2121 else
2122 write_declaration(header, stmt->u.var);
2123 break;
2128 void write_header(const statement_list_t *stmts)
2130 FILE *header;
2132 if (!do_header) return;
2134 if(!(header = fopen(header_name, "w"))) {
2135 error("Could not open %s for output\n", header_name);
2136 return;
2138 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
2140 fprintf(header, "#ifdef _WIN32\n");
2141 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
2142 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
2143 fprintf(header, "#endif\n");
2144 fprintf(header, "#include <rpc.h>\n" );
2145 fprintf(header, "#include <rpcndr.h>\n" );
2146 if (!for_each_serializable(stmts, NULL, serializable_exists))
2147 fprintf(header, "#include <midles.h>\n" );
2148 fprintf(header, "#endif\n\n");
2150 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
2151 fprintf(header, "#include <windows.h>\n");
2152 fprintf(header, "#include <ole2.h>\n");
2153 fprintf(header, "#endif\n\n");
2155 fprintf(header, "#ifndef __%s__\n", header_token);
2156 fprintf(header, "#define __%s__\n\n", header_token);
2158 fprintf(header, "#ifndef __WIDL_INLINE\n");
2159 fprintf(header, "#if defined(__cplusplus) || defined(_MSC_VER)\n");
2160 fprintf(header, "#define __WIDL_INLINE inline\n");
2161 fprintf(header, "#elif defined(__GNUC__)\n");
2162 fprintf(header, "#define __WIDL_INLINE __inline__\n");
2163 fprintf(header, "#endif\n");
2164 fprintf(header, "#endif\n\n");
2166 fprintf(header, "/* Forward declarations */\n\n");
2167 write_forward_decls(header, stmts);
2169 fprintf(header, "/* Headers for imported files */\n\n");
2170 write_imports(header, stmts);
2171 fprintf(header, "\n");
2172 start_cplusplus_guard(header);
2174 write_header_stmts(header, stmts, NULL, FALSE);
2176 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
2177 fprintf(header, "\n");
2178 for_each_serializable(stmts, header, write_serialize_function_decl);
2179 write_user_types(header);
2180 write_generic_handle_routines(header);
2181 write_context_handle_rundowns(header);
2182 fprintf(header, "\n");
2183 fprintf(header, "/* End additional prototypes */\n");
2184 fprintf(header, "\n");
2186 end_cplusplus_guard(header);
2187 fprintf(header, "#endif /* __%s__ */\n", header_token);
2189 fclose(header);