iphlpapi: Link NotifyAddrChange and CancelIPChangeNotify to nsi implementation.
[wine.git] / tools / widl / header.c
blob624d38094f97f1b8a8aa9b9554d06ab06cbcec59
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 static char *format_parameterized_type_args(const type_t *type, const char *prefix, const char *suffix)
70 typeref_list_t *params;
71 typeref_t *ref;
72 size_t len = 0, pos = 0;
73 char *buf = NULL;
75 params = type->details.parameterized.params;
76 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
78 assert(ref->type->type_type != TYPE_POINTER);
79 pos += strappend(&buf, &len, pos, "%s%s%s", prefix, ref->type->name, suffix);
80 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
83 if (!buf) return xstrdup("");
84 return buf;
87 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const struct uuid *uuid)
89 if (!uuid) return;
90 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
91 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
92 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
93 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
94 uuid->Data4[6], uuid->Data4[7]);
97 static void write_uuid_decl(FILE *f, type_t *type, const struct uuid *uuid)
99 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
100 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
101 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
102 type->c_name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
103 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
104 uuid->Data4[7]);
105 fprintf(f, "#endif\n");
108 static const char *uuid_string(const struct uuid *uuid)
110 static char buf[37];
112 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
113 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
114 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
116 return buf;
119 static void write_namespace_start(FILE *header, struct namespace *namespace)
121 if(is_global_namespace(namespace)) {
122 if(use_abi_namespace)
123 write_line(header, 1, "namespace ABI {");
124 return;
127 write_namespace_start(header, namespace->parent);
128 write_line(header, 1, "namespace %s {", namespace->name);
131 static void write_namespace_end(FILE *header, struct namespace *namespace)
133 if(is_global_namespace(namespace)) {
134 if(use_abi_namespace)
135 write_line(header, -1, "}");
136 return;
139 write_line(header, -1, "}");
140 write_namespace_end(header, namespace->parent);
143 const char *get_name(const var_t *v)
145 static char *buffer;
146 free( buffer );
147 if (is_attr( v->attrs, ATTR_EVENTADD ))
148 return buffer = strmake( "add_%s", v->name );
149 if (is_attr( v->attrs, ATTR_EVENTREMOVE ))
150 return buffer = strmake( "remove_%s", v->name );
151 if (is_attr( v->attrs, ATTR_PROPGET ))
152 return buffer = strmake( "get_%s", v->name );
153 if (is_attr( v->attrs, ATTR_PROPPUT ))
154 return buffer = strmake( "put_%s", v->name );
155 if (is_attr( v->attrs, ATTR_PROPPUTREF ))
156 return buffer = strmake( "putref_%s", v->name );
157 buffer = NULL;
158 return v->name;
161 static void write_fields(FILE *h, var_list_t *fields, enum name_type name_type)
163 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
164 const char *name;
165 char buf[32];
166 var_t *v;
168 if (!fields) return;
170 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
171 if (!v->declspec.type) continue;
173 switch(type_get_type_detect_alias(v->declspec.type)) {
174 case TYPE_STRUCT:
175 case TYPE_ENCAPSULATED_UNION:
176 nameless_struct_cnt++;
177 break;
178 case TYPE_UNION:
179 nameless_union_cnt++;
180 break;
181 default:
186 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
187 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
188 if (!v->declspec.type) continue;
189 if (contract) write_apicontract_guard_start(h, contract);
191 indent(h, 0);
192 name = v->name;
194 switch(type_get_type_detect_alias(v->declspec.type)) {
195 case TYPE_STRUCT:
196 case TYPE_ENCAPSULATED_UNION:
197 if(!v->name) {
198 fprintf(h, "__C89_NAMELESS ");
199 if(nameless_struct_cnt == 1) {
200 name = "__C89_NAMELESSSTRUCTNAME";
201 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
202 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
203 name = buf;
206 break;
207 case TYPE_UNION:
208 if(!v->name) {
209 fprintf(h, "__C89_NAMELESS ");
210 if(nameless_union_cnt == 1) {
211 name = "__C89_NAMELESSUNIONNAME";
212 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
213 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
214 name = buf;
217 break;
218 default:
221 write_type_v(h, &v->declspec, TRUE, v->declonly, name, name_type);
222 fprintf(h, ";\n");
223 if (contract) write_apicontract_guard_end(h, contract);
227 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
229 var_t *v;
230 if (!enums) return;
231 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
233 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
234 if (contract) write_apicontract_guard_start(h, contract);
235 if (v->name) {
236 indent(h, 0);
237 if(!enum_name)
238 fprintf(h, "%s", get_name(v));
239 else
240 fprintf(h, "%s_%s", enum_name, get_name(v));
241 if (v->eval) {
242 fprintf(h, " = ");
243 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
246 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
247 else fprintf(h, "\n");
248 if (contract) write_apicontract_guard_end(h, contract);
252 int needs_space_after(type_t *t)
254 return (type_is_alias(t) ||
255 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
258 static int decl_needs_parens(const type_t *t)
260 if (type_is_alias(t))
261 return FALSE;
262 if (is_array(t) && !type_array_is_decl_as_ptr(t))
263 return TRUE;
264 return is_func(t);
267 static void write_pointer_left(FILE *h, type_t *ref)
269 if (needs_space_after(ref))
270 fprintf(h, " ");
271 if (decl_needs_parens(ref))
272 fprintf(h, "(");
273 if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
275 const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
276 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
277 if (callconv) fprintf(h, "%s ", callconv);
279 fprintf(h, "*");
282 void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
284 type_t *t = ds->type;
285 const char *decl_name, *name;
286 char *args;
288 if (!h) return;
290 decl_name = type_get_decl_name(t, name_type);
291 name = type_get_name(t, name_type);
293 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
294 fprintf(h, "inline ");
296 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
297 fprintf(h, "const ");
299 if (!winrt_mode && type_is_alias(t)) fprintf(h, "%s", t->name);
300 else {
301 switch (type_get_type_detect_alias(t)) {
302 case TYPE_ENUM:
303 if (declonly) fprintf(h, "enum %s", decl_name ? decl_name : "");
304 else if (!t->written) {
305 assert(t->defined);
306 if (decl_name) fprintf(h, "enum %s {\n", decl_name);
307 else fprintf(h, "enum {\n");
308 t->written = TRUE;
309 indentation++;
310 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
311 indent(h, -1);
312 fprintf(h, "}");
314 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
315 else fprintf(h, "enum %s", name ? name : "");
316 break;
317 case TYPE_STRUCT:
318 case TYPE_ENCAPSULATED_UNION:
319 if (declonly) fprintf(h, "struct %s", decl_name ? decl_name : "");
320 else if (!t->written) {
321 assert(t->defined);
322 if (decl_name) fprintf(h, "struct %s {\n", decl_name);
323 else fprintf(h, "struct {\n");
324 t->written = TRUE;
325 indentation++;
326 if (type_get_type(t) != TYPE_STRUCT)
327 write_fields(h, type_encapsulated_union_get_fields(t), name_type);
328 else
329 write_fields(h, type_struct_get_fields(t), name_type);
330 indent(h, -1);
331 fprintf(h, "}");
333 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
334 else fprintf(h, "struct %s", name ? name : "");
335 break;
336 case TYPE_UNION:
337 if (declonly) fprintf(h, "union %s", decl_name ? decl_name : "");
338 else if (!t->written) {
339 assert(t->defined);
340 if (decl_name) fprintf(h, "union %s {\n", decl_name);
341 else fprintf(h, "union {\n");
342 t->written = TRUE;
343 indentation++;
344 write_fields(h, type_union_get_cases(t), name_type);
345 indent(h, -1);
346 fprintf(h, "}");
348 else if (winrt_mode && name_type == NAME_DEFAULT && name) fprintf(h, "%s", name);
349 else fprintf(h, "union %s", name ? name : "");
350 break;
351 case TYPE_POINTER:
353 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
354 write_pointer_left(h, type_pointer_get_ref_type(t));
355 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
356 break;
358 case TYPE_ARRAY:
359 if (t->name && type_array_is_decl_as_ptr(t))
360 fprintf(h, "%s", t->name);
361 else
363 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
364 if (type_array_is_decl_as_ptr(t))
365 write_pointer_left(h, type_array_get_element_type(t));
367 break;
368 case TYPE_FUNCTION:
370 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
372 /* A pointer to a function has to write the calling convention inside
373 * the parentheses. There's no way to handle that here, so we have to
374 * use an extra parameter to tell us whether to write the calling
375 * convention or not. */
376 if (write_callconv)
378 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
379 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
380 if (callconv) fprintf(h, " %s ", callconv);
382 break;
384 case TYPE_BASIC:
385 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
386 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
387 type_basic_get_type(t) != TYPE_BASIC_LONG &&
388 type_basic_get_type(t) != TYPE_BASIC_HYPER)
390 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
391 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
393 switch (type_basic_get_type(t))
395 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
396 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
397 case TYPE_BASIC_INT: fprintf(h, "int"); break;
398 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
399 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
400 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
401 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
402 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
403 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
404 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
405 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
406 case TYPE_BASIC_INT32:
407 if (type_basic_get_sign(t) > 0)
408 fprintf(h, "UINT32");
409 else
410 fprintf(h, "INT32");
411 break;
412 case TYPE_BASIC_LONG:
413 if (type_basic_get_sign(t) > 0)
414 fprintf(h, "ULONG");
415 else
416 fprintf(h, "LONG");
417 break;
418 case TYPE_BASIC_INT64:
419 if (type_basic_get_sign(t) > 0)
420 fprintf(h, "UINT64");
421 else
422 fprintf(h, "INT64");
423 break;
424 case TYPE_BASIC_HYPER:
425 if (type_basic_get_sign(t) > 0)
426 fprintf(h, "MIDL_uhyper");
427 else
428 fprintf(h, "hyper");
429 break;
431 break;
432 case TYPE_INTERFACE:
433 case TYPE_MODULE:
434 case TYPE_COCLASS:
435 fprintf(h, "%s", type_get_name(t, name_type));
436 break;
437 case TYPE_RUNTIMECLASS:
438 fprintf(h, "%s", type_get_name(type_runtimeclass_get_default_iface(t, TRUE), name_type));
439 break;
440 case TYPE_DELEGATE:
441 fprintf(h, "%s", type_get_name(type_delegate_get_iface(t), name_type));
442 break;
443 case TYPE_VOID:
444 fprintf(h, "void");
445 break;
446 case TYPE_BITFIELD:
448 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
449 write_type_left(h, &ds, name_type, declonly, TRUE);
450 break;
452 case TYPE_ALIAS:
454 const decl_spec_t *ds = type_alias_get_aliasee(t);
455 int in_namespace = ds && ds->type && ds->type->namespace && !is_global_namespace(ds->type->namespace);
456 if (!in_namespace) fprintf(h, "%s", t->name);
457 else write_type_left(h, ds, name_type, declonly, write_callconv);
458 break;
460 case TYPE_PARAMETERIZED_TYPE:
462 type_t *iface = type_parameterized_type_get_real_type(t);
463 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
464 args = format_parameterized_type_args(t, "", "_logical");
465 fprintf(h, "%s<%s>", iface->name, args);
466 free(args);
467 break;
469 case TYPE_PARAMETER:
470 fprintf(h, "%s_abi", t->name);
471 break;
472 case TYPE_APICONTRACT:
473 /* shouldn't be here */
474 assert(0);
475 break;
480 void write_type_right(FILE *h, type_t *t, int is_field)
482 if (!h) return;
483 if (type_is_alias(t)) return;
485 switch (type_get_type(t))
487 case TYPE_ARRAY:
489 type_t *elem = type_array_get_element_type(t);
490 if (type_array_is_decl_as_ptr(t))
492 if (decl_needs_parens(elem))
493 fprintf(h, ")");
495 else
497 if (is_conformant_array(t))
498 fprintf(h, "[%s]", is_field ? "1" : "");
499 else
500 fprintf(h, "[%u]", type_array_get_dim(t));
502 write_type_right(h, elem, FALSE);
503 break;
505 case TYPE_FUNCTION:
507 const var_list_t *args = type_function_get_args(t);
508 fputc('(', h);
509 if (args) write_args(h, args, NULL, 0, FALSE, NAME_DEFAULT);
510 else
511 fprintf(h, "void");
512 fputc(')', h);
513 write_type_right(h, type_function_get_rettype(t), FALSE);
514 break;
516 case TYPE_POINTER:
518 type_t *ref = type_pointer_get_ref_type(t);
519 if (decl_needs_parens(ref))
520 fprintf(h, ")");
521 write_type_right(h, ref, FALSE);
522 break;
524 case TYPE_BITFIELD:
525 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
526 break;
527 case TYPE_VOID:
528 case TYPE_BASIC:
529 case TYPE_ENUM:
530 case TYPE_STRUCT:
531 case TYPE_ENCAPSULATED_UNION:
532 case TYPE_UNION:
533 case TYPE_ALIAS:
534 case TYPE_MODULE:
535 case TYPE_COCLASS:
536 case TYPE_INTERFACE:
537 case TYPE_RUNTIMECLASS:
538 case TYPE_DELEGATE:
539 case TYPE_PARAMETERIZED_TYPE:
540 case TYPE_PARAMETER:
541 break;
542 case TYPE_APICONTRACT:
543 /* not supposed to be here */
544 assert(0);
545 break;
549 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)
551 type_t *t = ds->type;
553 if (!h) return;
555 if (t) write_type_left(h, ds, name_type, declonly, TRUE);
557 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
559 if (t)
560 write_type_right(h, t, is_field);
563 static void write_type_definition(FILE *f, type_t *t, int declonly)
565 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
566 int save_written = t->written;
567 decl_spec_t ds = {.type = t};
568 expr_t *contract = get_attrp(t->attrs, ATTR_CONTRACT);
570 if (contract) write_apicontract_guard_start(f, contract);
571 if(in_namespace) {
572 fprintf(f, "#ifdef __cplusplus\n");
573 fprintf(f, "} /* extern \"C\" */\n");
574 write_namespace_start(f, t->namespace);
576 indent(f, 0);
577 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
578 fprintf(f, ";\n");
579 if(in_namespace) {
580 t->written = save_written;
581 write_namespace_end(f, t->namespace);
582 fprintf(f, "extern \"C\" {\n");
583 fprintf(f, "#else\n");
584 write_type_left(f, &ds, NAME_C, declonly, TRUE);
585 fprintf(f, ";\n");
586 if (winrt_mode) write_widl_using_macros(f, t);
587 fprintf(f, "#endif\n\n");
589 if (contract) write_apicontract_guard_end(f, contract);
592 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
594 write_type_v(f, t, FALSE, TRUE, name, NAME_DEFAULT);
597 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
599 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
602 static int user_type_registered(const char *name)
604 user_type_t *ut;
605 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
606 if (!strcmp(name, ut->name))
607 return 1;
608 return 0;
611 static int context_handle_registered(const char *name)
613 context_handle_t *ch;
614 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
615 if (!strcmp(name, ch->name))
616 return 1;
617 return 0;
620 static int generic_handle_registered(const char *name)
622 generic_handle_t *gh;
623 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
624 if (!strcmp(name, gh->name))
625 return 1;
626 return 0;
629 unsigned int get_context_handle_offset( const type_t *type )
631 context_handle_t *ch;
632 unsigned int index = 0;
634 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
636 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
637 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
638 else error( "internal error: %s is not a context handle\n", type->name );
640 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
642 if (!strcmp( type->name, ch->name )) return index;
643 index++;
645 error( "internal error: %s is not registered as a context handle\n", type->name );
646 return index;
649 unsigned int get_generic_handle_offset( const type_t *type )
651 generic_handle_t *gh;
652 unsigned int index = 0;
654 while (!is_attr( type->attrs, ATTR_HANDLE ))
656 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
657 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
658 else error( "internal error: %s is not a generic handle\n", type->name );
660 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
662 if (!strcmp( type->name, gh->name )) return index;
663 index++;
665 error( "internal error: %s is not registered as a generic handle\n", type->name );
666 return index;
669 /* check for types which require additional prototypes to be generated in the
670 * header */
671 void check_for_additional_prototype_types(type_t *type)
673 if (!type) return;
674 for (;;) {
675 const char *name = type->name;
676 if (type->user_types_registered) break;
677 type->user_types_registered = 1;
678 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
679 if (!context_handle_registered(name))
681 context_handle_t *ch = xmalloc(sizeof(*ch));
682 ch->name = xstrdup(name);
683 list_add_tail(&context_handle_list, &ch->entry);
685 /* don't carry on parsing fields within this type */
686 break;
688 if ((type_get_type(type) != TYPE_BASIC ||
689 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
690 is_attr(type->attrs, ATTR_HANDLE)) {
691 if (!generic_handle_registered(name))
693 generic_handle_t *gh = xmalloc(sizeof(*gh));
694 gh->name = xstrdup(name);
695 list_add_tail(&generic_handle_list, &gh->entry);
697 /* don't carry on parsing fields within this type */
698 break;
700 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
701 if (!user_type_registered(name))
703 user_type_t *ut = xmalloc(sizeof *ut);
704 ut->name = xstrdup(name);
705 list_add_tail(&user_type_list, &ut->entry);
707 /* don't carry on parsing fields within this type as we are already
708 * using a wire marshaled type */
709 break;
711 else if (type_is_complete(type))
713 var_list_t *vars;
714 const var_t *v;
715 switch (type_get_type_detect_alias(type))
717 case TYPE_ENUM:
718 vars = type_enum_get_values(type);
719 break;
720 case TYPE_STRUCT:
721 vars = type_struct_get_fields(type);
722 break;
723 case TYPE_UNION:
724 vars = type_union_get_cases(type);
725 break;
726 default:
727 vars = NULL;
728 break;
730 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
731 check_for_additional_prototype_types(v->declspec.type);
734 if (type_is_alias(type))
735 type = type_alias_get_aliasee_type(type);
736 else if (is_ptr(type))
737 type = type_pointer_get_ref_type(type);
738 else if (is_array(type))
739 type = type_array_get_element_type(type);
740 else
741 break;
745 static int write_serialize_function_decl(FILE *header, const type_t *type)
747 write_serialize_functions(header, type, NULL);
748 return 1;
751 static int serializable_exists(FILE *header, const type_t *type)
753 return 0;
756 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
757 int (*proc)(FILE*, const type_t*))
759 statement_t *stmt, *iface_stmt;
760 statement_list_t *iface_stmts;
761 typeref_t *ref;
763 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
765 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
766 continue;
768 iface_stmts = type_iface_get_stmts(stmt->u.type);
769 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
771 if (iface_stmt->type != STMT_TYPEDEF) continue;
772 if (iface_stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, iface_stmt->u.type_list, typeref_t, entry)
774 if (!is_attr(ref->type->attrs, ATTR_ENCODE)
775 && !is_attr(ref->type->attrs, ATTR_DECODE))
776 continue;
777 if (!proc(header, ref->type))
778 return 0;
783 return 1;
786 static void write_user_types(FILE *header)
788 user_type_t *ut;
789 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
791 const char *name = ut->name;
792 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
793 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
794 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
795 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
799 static void write_context_handle_rundowns(FILE *header)
801 context_handle_t *ch;
802 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
804 const char *name = ch->name;
805 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
809 static void write_generic_handle_routines(FILE *header)
811 generic_handle_t *gh;
812 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
814 const char *name = gh->name;
815 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
816 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
820 static void write_typedef(FILE *header, type_t *type, int declonly)
822 type_t *t = type_alias_get_aliasee_type(type);
823 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace))
825 fprintf(header, "#ifndef __cplusplus\n");
826 fprintf(header, "typedef ");
827 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->c_name, NAME_C);
828 fprintf(header, ";\n");
829 if (type_get_type_detect_alias(t) != TYPE_ENUM)
831 fprintf(header, "#else /* __cplusplus */\n");
832 write_namespace_start(header, t->namespace);
833 indent(header, 0);
834 fprintf(header, "typedef ");
835 write_type_v(header, type_alias_get_aliasee(type), FALSE, TRUE, type->name, NAME_DEFAULT);
836 fprintf(header, ";\n");
837 write_namespace_end(header, t->namespace);
839 fprintf(header, "#endif /* __cplusplus */\n\n");
841 else
843 fprintf(header, "typedef ");
844 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name, NAME_DEFAULT);
845 fprintf(header, ";\n");
849 int is_const_decl(const var_t *var)
851 const decl_spec_t *t;
852 /* strangely, MIDL accepts a const attribute on any pointer in the
853 * declaration to mean that data isn't being instantiated. this appears
854 * to be a bug, but there is no benefit to being incompatible with MIDL,
855 * so we'll do the same thing */
856 for (t = &var->declspec; ; )
858 if (t->qualifier & TYPE_QUALIFIER_CONST)
859 return TRUE;
860 else if (is_ptr(t->type))
861 t = type_pointer_get_ref(t->type);
862 else break;
864 return FALSE;
867 static void write_declaration(FILE *header, const var_t *v)
869 if (is_const_decl(v) && v->eval)
871 fprintf(header, "#define %s (", v->name);
872 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
873 fprintf(header, ")\n\n");
875 else
877 switch (v->declspec.stgclass)
879 case STG_NONE:
880 case STG_REGISTER: /* ignored */
881 break;
882 case STG_STATIC:
883 fprintf(header, "static ");
884 break;
885 case STG_EXTERN:
886 fprintf(header, "extern ");
887 break;
889 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name, NAME_DEFAULT);
890 fprintf(header, ";\n\n");
894 static void write_library(FILE *header, const typelib_t *typelib)
896 const struct uuid *uuid = get_attrp(typelib->attrs, ATTR_UUID);
897 fprintf(header, "\n");
898 write_guid(header, "LIBID", typelib->name, uuid);
899 fprintf(header, "\n");
903 const type_t* get_explicit_generic_handle_type(const var_t* var)
905 const type_t *t;
906 for (t = var->declspec.type;
907 is_ptr(t) || type_is_alias(t);
908 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
909 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
910 is_attr(t->attrs, ATTR_HANDLE))
911 return t;
912 return NULL;
915 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
916 unsigned char *explicit_fc, unsigned char *implicit_fc )
918 const var_t *var;
919 const var_list_t *args = type_function_get_args( func->declspec.type );
921 *explicit_fc = *implicit_fc = 0;
922 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
924 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
925 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
927 *explicit_fc = FC_BIND_PRIMITIVE;
928 return var;
930 if (get_explicit_generic_handle_type( var ))
932 *explicit_fc = FC_BIND_GENERIC;
933 return var;
935 if (is_context_handle( var->declspec.type ))
937 *explicit_fc = FC_BIND_CONTEXT;
938 return var;
942 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
944 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
945 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
946 *implicit_fc = FC_BIND_PRIMITIVE;
947 else
948 *implicit_fc = FC_BIND_GENERIC;
949 return var;
952 *implicit_fc = FC_AUTO_HANDLE;
953 return NULL;
956 int has_out_arg_or_return(const var_t *func)
958 const var_t *var;
960 if (!is_void(type_function_get_rettype(func->declspec.type)))
961 return 1;
963 if (!type_function_get_args(func->declspec.type))
964 return 0;
966 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
967 if (is_attr(var->attrs, ATTR_OUT))
968 return 1;
970 return 0;
974 /********** INTERFACES **********/
976 int is_object(const type_t *iface)
978 const attr_t *attr;
979 if (type_is_defined(iface) && (type_get_type(iface) == TYPE_DELEGATE || type_iface_get_inherit(iface)))
980 return 1;
981 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
982 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
983 return 0;
986 int is_local(const attr_list_t *a)
988 return is_attr(a, ATTR_LOCAL);
991 const var_t *is_callas(const attr_list_t *a)
993 return get_attrp(a, ATTR_CALLAS);
996 static int is_inherited_method(const type_t *iface, const var_t *func)
998 while ((iface = type_iface_get_inherit(iface)))
1000 const statement_t *stmt;
1001 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1003 const var_t *funccmp = stmt->u.var;
1005 if (!is_callas(func->attrs))
1007 char inherit_name[256];
1008 /* compare full name including property prefix */
1009 strcpy(inherit_name, get_name(funccmp));
1010 if (!strcmp(inherit_name, get_name(func))) return 1;
1015 return 0;
1018 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
1020 if (iface == child)
1021 return 0;
1025 const statement_t *stmt;
1026 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
1028 const var_t *funccmp = stmt->u.var;
1030 if (!is_callas(func->attrs))
1032 char inherit_name[256];
1033 /* compare full name including property prefix */
1034 strcpy(inherit_name, get_name(funccmp));
1035 if (!strcmp(inherit_name, get_name(func))) return 1;
1039 while ((child = type_iface_get_inherit(child)) && child != iface);
1041 return 0;
1044 static int is_aggregate_return(const var_t *func)
1046 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1047 return type == TYPE_STRUCT || type == TYPE_UNION ||
1048 type == TYPE_COCLASS || type == TYPE_INTERFACE ||
1049 type == TYPE_RUNTIMECLASS;
1052 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1054 static char buff[255];
1055 if (is_inherited_method(iface, func))
1056 sprintf(buff, "%s_%s", iface->name, get_name(func));
1057 else
1058 sprintf(buff, "%s", get_name(func));
1059 return buff;
1062 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1064 const statement_t *stmt;
1065 int first_iface = 1;
1067 if (type_iface_get_inherit(iface))
1068 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1070 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1072 const var_t *func = stmt->u.var;
1074 if (first_iface)
1076 fprintf(header, "/*** %s methods ***/\n", iface->name);
1077 first_iface = 0;
1080 if (is_override_method(iface, child, func))
1081 continue;
1083 if (!is_callas(func->attrs)) {
1084 const var_t *arg;
1086 fprintf(header, "#define %s_%s(This", name, get_name(func));
1087 if (type_function_get_args(func->declspec.type))
1088 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1089 fprintf(header, ",%s", arg->name);
1090 fprintf(header, ") ");
1092 if (is_aggregate_return(func))
1094 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1095 continue;
1098 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1099 if (type_function_get_args(func->declspec.type))
1100 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1101 fprintf(header, ",%s", arg->name);
1102 fprintf(header, ")\n");
1107 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent, enum name_type name_type)
1109 const var_t *arg;
1110 int count = 0;
1112 if (do_indent)
1114 indentation++;
1115 indent(h, 0);
1117 if (method == 1) {
1118 fprintf(h, "%s* This", name);
1119 count++;
1121 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1122 if (count) {
1123 if (do_indent)
1125 fprintf(h, ",\n");
1126 indent(h, 0);
1128 else fprintf(h, ",");
1130 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1131 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1132 * defining UDTs inside of an argument list. */
1133 write_type_v(h, &arg->declspec, FALSE, TRUE, arg->name, name_type);
1134 if (method == 2) {
1135 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1136 if (expr) {
1137 const var_t *tail_arg;
1139 /* Output default value only if all following arguments also have default value. */
1140 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1141 if(tail_arg == arg) {
1142 expr_t bstr;
1144 /* Fixup the expression type for a BSTR like midl does. */
1145 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1147 bstr = *expr;
1148 bstr.type = EXPR_WSTRLIT;
1149 expr = &bstr;
1152 fprintf(h, " = ");
1153 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1154 break;
1156 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1157 break;
1161 count++;
1163 if (do_indent) indentation--;
1166 static void write_cpp_method_def(FILE *header, const type_t *iface)
1168 const statement_t *stmt;
1170 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1172 const var_t *func = stmt->u.var;
1173 if (!is_callas(func->attrs)) {
1174 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1175 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1176 const var_list_t *args = type_function_get_args(func->declspec.type);
1177 const var_t *arg;
1179 if (!callconv) callconv = "STDMETHODCALLTYPE";
1181 if (is_aggregate_return(func)) {
1182 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1184 indent(header, 0);
1185 fprintf(header, "virtual ");
1186 write_type_decl_left(header, ret);
1187 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1188 ++indentation;
1189 indent(header, 0);
1190 write_type_decl_left(header, ret);
1191 fprintf(header, " *__ret");
1192 --indentation;
1193 if (args) {
1194 fprintf(header, ",\n");
1195 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1197 fprintf(header, ") = 0;\n");
1199 indent(header, 0);
1200 write_type_decl_left(header, ret);
1201 fprintf(header, " %s %s(\n", callconv, get_name(func));
1202 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1203 fprintf(header, ")\n");
1204 indent(header, 0);
1205 fprintf(header, "{\n");
1206 ++indentation;
1207 indent(header, 0);
1208 write_type_decl_left(header, ret);
1209 fprintf(header, " __ret;\n");
1210 indent(header, 0);
1211 fprintf(header, "return *%s(&__ret", get_name(func));
1212 if (args)
1213 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1214 fprintf(header, ", %s", arg->name);
1215 fprintf(header, ");\n");
1216 --indentation;
1217 indent(header, 0);
1218 fprintf(header, "}\n");
1220 fprintf(header, "#else\n");
1223 indent(header, 0);
1224 fprintf(header, "virtual ");
1225 write_type_decl_left(header, ret);
1226 fprintf(header, " %s %s(\n", callconv, get_name(func));
1227 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1228 fprintf(header, ") = 0;\n");
1230 if (is_aggregate_return(func))
1231 fprintf(header, "#endif\n");
1232 fprintf(header, "\n");
1237 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1239 const statement_t *stmt;
1240 int first_iface = 1;
1242 if (type_iface_get_inherit(iface))
1243 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1245 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1247 const var_t *func = stmt->u.var;
1249 if (first_iface)
1251 fprintf(header, "/*** %s methods ***/\n", iface->name);
1252 first_iface = 0;
1255 if (is_override_method(iface, child, func))
1256 continue;
1258 if (!is_callas(func->attrs)) {
1259 const var_t *arg;
1261 fprintf(header, "static __WIDL_INLINE ");
1262 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1263 fprintf(header, " %s_%s(", name, get_name(func));
1264 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE, NAME_C);
1265 fprintf(header, ") {\n");
1266 ++indentation;
1267 if (!is_aggregate_return(func)) {
1268 indent(header, 0);
1269 fprintf(header, "%sThis->lpVtbl->%s(This",
1270 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1271 get_vtbl_entry_name(iface, func));
1272 } else {
1273 indent(header, 0);
1274 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1275 fprintf(header, " __ret;\n");
1276 indent(header, 0);
1277 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1279 if (type_function_get_args(func->declspec.type))
1280 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1281 fprintf(header, ",%s", arg->name);
1282 fprintf(header, ");\n");
1283 --indentation;
1284 fprintf(header, "}\n");
1289 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1291 const statement_t *stmt;
1292 int first_iface = 1;
1294 if (type_iface_get_inherit(iface))
1295 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1297 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1299 const var_t *func = stmt->u.var;
1300 if (first_iface) {
1301 indent(header, 0);
1302 fprintf(header, "/*** %s methods ***/\n", iface->name);
1303 first_iface = 0;
1305 if (!is_callas(func->attrs)) {
1306 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1307 if (!callconv) callconv = "STDMETHODCALLTYPE";
1308 indent(header, 0);
1309 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1310 if (is_aggregate_return(func))
1311 fprintf(header, " *");
1312 if (is_inherited_method(iface, func))
1313 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1314 else
1315 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1316 ++indentation;
1317 indent(header, 0);
1318 fprintf(header, "%s *This", name);
1319 if (is_aggregate_return(func)) {
1320 fprintf(header, ",\n");
1321 indent(header, 0);
1322 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1323 fprintf(header, " *__ret");
1325 --indentation;
1326 if (type_function_get_args(func->declspec.type)) {
1327 fprintf(header, ",\n");
1328 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE, NAME_C);
1330 fprintf(header, ");\n");
1331 fprintf(header, "\n");
1336 static void write_c_method_def(FILE *header, const type_t *iface)
1338 do_write_c_method_def(header, iface, iface->c_name);
1341 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1343 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1346 static void write_method_proto(FILE *header, const type_t *iface)
1348 const statement_t *stmt;
1350 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1352 const var_t *func = stmt->u.var;
1354 if (is_callas(func->attrs)) {
1355 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1356 if (!callconv) callconv = "STDMETHODCALLTYPE";
1357 /* proxy prototype */
1358 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1359 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1360 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1361 fprintf(header, ");\n");
1362 /* stub prototype */
1363 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1364 fprintf(header, " IRpcStubBuffer* This,\n");
1365 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1366 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1367 fprintf(header, " DWORD* pdwStubPhase);\n");
1372 static void write_locals(FILE *fp, const type_t *iface, int body)
1374 static const char comment[]
1375 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1376 const statement_t *stmt;
1378 if (!is_object(iface))
1379 return;
1381 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1382 const var_t *func = stmt->u.var;
1383 const var_t *cas = is_callas(func->attrs);
1385 if (cas) {
1386 const statement_t *stmt2 = NULL;
1387 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1388 if (!strcmp(get_name(stmt2->u.var), cas->name))
1389 break;
1390 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1391 const var_t *m = stmt2->u.var;
1392 /* proxy prototype - use local prototype */
1393 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1394 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1395 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1396 fprintf(fp, ")");
1397 if (body) {
1398 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1399 fprintf(fp, "\n{\n");
1400 fprintf(fp, " %s\n", comment);
1401 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1402 fprintf(fp, " return E_NOTIMPL;\n");
1403 else if (type_get_type(rt->type) != TYPE_VOID) {
1404 fprintf(fp, " ");
1405 write_type_decl(fp, rt, "rv");
1406 fprintf(fp, ";\n");
1407 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1408 fprintf(fp, " return rv;\n");
1410 fprintf(fp, "}\n\n");
1412 else
1413 fprintf(fp, ";\n");
1414 /* stub prototype - use remotable prototype */
1415 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1416 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1417 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1418 fprintf(fp, ")");
1419 if (body)
1420 /* Remotable methods must all return HRESULTs. */
1421 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1422 else
1423 fprintf(fp, ";\n");
1425 else
1426 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1431 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1433 const statement_t *stmt;
1434 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1436 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1437 write_locals(local_stubs, stmt->u.type, TRUE);
1441 void write_local_stubs(const statement_list_t *stmts)
1443 FILE *local_stubs;
1445 if (!local_stubs_name) return;
1447 local_stubs = fopen(local_stubs_name, "w");
1448 if (!local_stubs) {
1449 error("Could not open %s for output\n", local_stubs_name);
1450 return;
1452 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1453 fprintf(local_stubs, "#include <objbase.h>\n");
1454 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1456 write_local_stubs_stmts(local_stubs, stmts);
1458 fclose(local_stubs);
1461 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1463 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1465 if (!callconv) callconv = "__cdecl";
1466 /* FIXME: do we need to handle call_as? */
1467 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1468 fprintf(header, " %s ", callconv);
1469 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1470 if (type_function_get_args(fun->declspec.type))
1471 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE, NAME_DEFAULT);
1472 else
1473 fprintf(header, " void");
1474 fprintf(header, ");\n\n");
1477 static void write_parameterized_type_forward(FILE *header, type_t *type)
1479 type_t *iface = type->details.parameterized.type;
1480 char *args;
1482 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1484 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1485 write_namespace_start(header, type->namespace);
1487 args = format_parameterized_type_args(type, "class ", "");
1488 write_line(header, 0, "template <%s>", args);
1489 write_line(header, 0, "struct %s_impl;\n", iface->name);
1491 write_line(header, 0, "template <%s>", args);
1492 free(args);
1493 args = format_parameterized_type_args(type, "", "");
1494 write_line(header, 0, "struct %s : %s_impl<%s> {};", iface->name, iface->name, args);
1495 free(args);
1497 write_namespace_end(header, type->namespace);
1498 fprintf(header, "#endif\n\n" );
1501 static void write_parameterized_implementation(FILE *header, type_t *type, int declonly)
1503 const statement_t *stmt;
1504 typeref_list_t *params = type->details.parameterized.params;
1505 typeref_t *ref;
1506 type_t *iface = type->details.parameterized.type, *base;
1507 char *args = NULL;
1509 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1510 write_line(header, 0, "} /* extern \"C\" */");
1511 write_namespace_start(header, type->namespace);
1513 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1514 base = type_iface_get_inherit(iface);
1516 args = format_parameterized_type_args(type, "class ", "");
1517 write_line(header, 0, "template <%s>", args);
1518 free(args);
1519 write_line(header, 0, "struct %s_impl%s", iface->name, base ? strmake(" : %s", base->name) : "");
1520 write_line(header, 0, "{");
1522 write_line(header, 1, "private:");
1523 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1525 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetAbiType<%s>::type %s_abi;", ref->type->name, ref->type->name);
1526 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetLogicalType<%s>::type %s_logical;", ref->type->name, ref->type->name);
1528 indentation -= 1;
1530 write_line(header, 1, "public:");
1531 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1532 write_line(header, 0, "typedef %s %s_complex;", ref->type->name, ref->type->name);
1534 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1536 const var_t *func = stmt->u.var;
1537 if (is_callas(func->attrs)) continue;
1538 indent(header, 1);
1539 fprintf(header, "virtual ");
1540 write_type_decl_left(header, &func->declspec);
1541 fprintf(header, "%s(", get_name(func));
1542 write_args(header, type_function_get_args(func->declspec.type), NULL, 0, 0, NAME_DEFAULT);
1543 fprintf(header, ") = 0;\n");
1544 indentation -= 1;
1546 write_line(header, -1, "};");
1548 write_namespace_end(header, type->namespace);
1549 write_line(header, 0, "extern \"C\" {");
1550 write_line(header, 0, "#endif\n");
1553 static void write_forward(FILE *header, type_t *iface)
1555 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1556 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1557 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1558 fprintf(header, "#ifdef __cplusplus\n");
1559 if (iface->namespace && !is_global_namespace(iface->namespace))
1560 fprintf(header, "#define %s %s\n", iface->c_name, iface->qualified_name);
1561 if (!iface->impl_name)
1563 write_namespace_start(header, iface->namespace);
1564 write_line(header, 0, "interface %s;", iface->name);
1565 write_namespace_end(header, iface->namespace);
1567 fprintf(header, "#endif /* __cplusplus */\n");
1568 fprintf(header, "#endif\n\n" );
1571 static char *format_apicontract_macro(const type_t *type)
1573 char *name = format_namespace(type->namespace, "", "_", type->name, NULL);
1574 int i;
1575 for (i = strlen(name); i > 0; --i) name[i - 1] = toupper(name[i - 1]);
1576 return name;
1579 static void write_apicontract_guard_start(FILE *header, const expr_t *expr)
1581 const type_t *type;
1582 char *name;
1583 int ver;
1584 if (!winrt_mode) return;
1585 type = expr->u.tref.type;
1586 ver = expr->ref->u.lval;
1587 name = format_apicontract_macro(type);
1588 fprintf(header, "#if %s_VERSION >= %#x\n", name, ver);
1589 free(name);
1592 static void write_apicontract_guard_end(FILE *header, const expr_t *expr)
1594 const type_t *type;
1595 char *name;
1596 int ver;
1597 if (!winrt_mode) return;
1598 type = expr->u.tref.type;
1599 ver = expr->ref->u.lval;
1600 name = format_apicontract_macro(type);
1601 fprintf(header, "#endif /* %s_VERSION >= %#x */\n", name, ver);
1602 free(name);
1605 static void write_com_interface_start(FILE *header, const type_t *iface)
1607 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1608 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1609 fprintf(header, "/*****************************************************************************\n");
1610 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1611 fprintf(header, " */\n");
1612 if (contract) write_apicontract_guard_start(header, contract);
1613 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1614 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1617 static void write_widl_using_method_macros(FILE *header, const type_t *iface, const type_t *top_iface)
1619 const statement_t *stmt;
1620 const char *name = top_iface->short_name ? top_iface->short_name : top_iface->name;
1622 if (type_iface_get_inherit(iface)) write_widl_using_method_macros(header, type_iface_get_inherit(iface), top_iface);
1624 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1626 const var_t *func = stmt->u.var;
1627 const char *func_name;
1629 if (is_override_method(iface, top_iface, func)) continue;
1630 if (is_callas(func->attrs)) continue;
1632 func_name = get_name(func);
1633 fprintf(header, "#define %s_%s %s_%s\n", name, func_name, top_iface->c_name, func_name);
1637 static void write_widl_using_macros(FILE *header, type_t *iface)
1639 const struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
1640 const char *name = iface->short_name ? iface->short_name : iface->name;
1641 char *macro;
1643 if (!strcmp(iface->name, iface->c_name)) return;
1645 macro = format_namespace(iface->namespace, "WIDL_using_", "_", NULL, NULL);
1646 fprintf(header, "#ifdef %s\n", macro);
1648 if (uuid) fprintf(header, "#define IID_%s IID_%s\n", name, iface->c_name);
1649 if (iface->type_type == TYPE_INTERFACE) fprintf(header, "#define %sVtbl %sVtbl\n", name, iface->c_name);
1650 fprintf(header, "#define %s %s\n", name, iface->c_name);
1652 if (iface->type_type == TYPE_INTERFACE) write_widl_using_method_macros(header, iface, iface);
1654 fprintf(header, "#endif /* %s */\n", macro);
1655 free(macro);
1658 static void write_com_interface_end(FILE *header, type_t *iface)
1660 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1661 const struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
1662 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1663 type_t *type;
1665 if (uuid)
1666 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1668 /* C++ interface */
1669 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1670 if (!is_global_namespace(iface->namespace)) {
1671 write_line(header, 0, "} /* extern \"C\" */");
1672 write_namespace_start(header, iface->namespace);
1674 if (uuid) {
1675 if (strchr(iface->name, '<')) write_line(header, 0, "template<>");
1676 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1677 indent(header, 0);
1678 }else {
1679 indent(header, 0);
1680 if (strchr(iface->name, '<')) fprintf(header, "template<> struct ");
1681 else fprintf(header, "interface ");
1683 if (iface->impl_name)
1685 fprintf(header, "%s : %s\n", iface->name, iface->impl_name);
1686 write_line(header, 1, "{");
1688 else if (type_iface_get_inherit(iface))
1690 fprintf(header, "%s : public %s\n", iface->name,
1691 type_iface_get_inherit(iface)->name);
1692 write_line(header, 1, "{");
1694 else
1696 fprintf(header, "%s\n", iface->name);
1697 write_line(header, 1, "{\n");
1698 write_line(header, 0, "BEGIN_INTERFACE\n");
1700 /* dispinterfaces don't have real functions, so don't write C++ functions for
1701 * them */
1702 if (!dispinterface && !iface->impl_name)
1703 write_cpp_method_def(header, iface);
1704 if (!type_iface_get_inherit(iface) && !iface->impl_name)
1705 write_line(header, 0, "END_INTERFACE\n");
1706 write_line(header, -1, "};");
1707 if (!is_global_namespace(iface->namespace)) {
1708 write_namespace_end(header, iface->namespace);
1709 write_line(header, 0, "extern \"C\" {");
1711 if (uuid)
1712 write_uuid_decl(header, iface, uuid);
1713 fprintf(header, "#else\n");
1714 /* C interface */
1715 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1716 write_line(header, 0, "BEGIN_INTERFACE\n");
1717 if (dispinterface)
1718 write_c_disp_method_def(header, iface);
1719 else
1720 write_c_method_def(header, iface);
1721 write_line(header, 0, "END_INTERFACE");
1722 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1723 fprintf(header, "interface %s {\n", iface->c_name);
1724 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1725 fprintf(header, "};\n\n");
1726 fprintf(header, "#ifdef COBJMACROS\n");
1727 /* dispinterfaces don't have real functions, so don't write macros for them,
1728 * only for the interface this interface inherits from, i.e. IDispatch */
1729 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1730 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1731 write_method_macro(header, type, type, iface->c_name);
1732 fprintf(header, "#else\n");
1733 write_inline_wrappers(header, type, type, iface->c_name);
1734 fprintf(header, "#endif\n");
1735 if (winrt_mode) write_widl_using_macros(header, iface);
1736 fprintf(header, "#endif\n");
1737 fprintf(header, "\n");
1738 fprintf(header, "#endif\n");
1739 fprintf(header, "\n");
1740 /* dispinterfaces don't have real functions, so don't write prototypes for
1741 * them */
1742 if (!dispinterface && !winrt_mode)
1744 write_method_proto(header, iface);
1745 write_locals(header, iface, FALSE);
1746 fprintf(header, "\n");
1748 fprintf(header, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface->c_name, dispinterface ? "DISP" : "");
1749 if (contract) write_apicontract_guard_end(header, contract);
1750 fprintf(header, "\n");
1753 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1755 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1756 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1757 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1759 fprintf(header, "/*****************************************************************************\n");
1760 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1761 fprintf(header, " */\n");
1762 if (contract) write_apicontract_guard_start(header, contract);
1763 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1764 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1765 if (var)
1767 fprintf(header, "extern ");
1768 write_type_decl( header, &var->declspec, var->name );
1769 fprintf(header, ";\n");
1771 if (old_names)
1773 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1774 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1776 else
1778 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1779 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1780 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1781 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1785 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1787 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1788 fprintf(header, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface->name);
1789 if (contract) write_apicontract_guard_end(header, contract);
1790 fprintf(header, "\n");
1793 static void write_coclass(FILE *header, type_t *cocl)
1795 const struct uuid *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1797 fprintf(header, "/*****************************************************************************\n");
1798 fprintf(header, " * %s coclass\n", cocl->name);
1799 fprintf(header, " */\n\n");
1800 if (uuid)
1801 write_guid(header, "CLSID", cocl->name, uuid);
1802 fprintf(header, "\n#ifdef __cplusplus\n");
1803 if (uuid)
1805 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1806 write_uuid_decl(header, cocl, uuid);
1808 else
1810 fprintf(header, "class %s;\n", cocl->name);
1812 fprintf(header, "#endif\n");
1813 fprintf(header, "\n");
1816 static void write_coclass_forward(FILE *header, type_t *cocl)
1818 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1819 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1820 fprintf(header, "#ifdef __cplusplus\n");
1821 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1822 fprintf(header, "#else\n");
1823 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1824 fprintf(header, "#endif /* defined __cplusplus */\n");
1825 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1828 static void write_apicontract(FILE *header, type_t *apicontract)
1830 char *name = format_apicontract_macro(apicontract);
1831 fprintf(header, "#if !defined(%s_VERSION)\n", name);
1832 fprintf(header, "#define %s_VERSION %#x\n", name, get_attrv(apicontract->attrs, ATTR_CONTRACTVERSION));
1833 fprintf(header, "#endif // defined(%s_VERSION)\n\n", name);
1834 free(name);
1837 static void write_runtimeclass(FILE *header, type_t *runtimeclass)
1839 expr_t *contract = get_attrp(runtimeclass->attrs, ATTR_CONTRACT);
1840 char *name, *c_name;
1841 size_t i, len;
1842 name = format_namespace(runtimeclass->namespace, "", ".", runtimeclass->name, NULL);
1843 c_name = format_namespace(runtimeclass->namespace, "", "_", runtimeclass->name, NULL);
1844 fprintf(header, "/*\n");
1845 fprintf(header, " * Class %s\n", name);
1846 fprintf(header, " */\n");
1847 if (contract) write_apicontract_guard_start(header, contract);
1848 fprintf(header, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name);
1849 fprintf(header, "#define RUNTIMECLASS_%s_DEFINED\n", c_name);
1850 fprintf(header, "#if !defined(_MSC_VER) && !defined(__MINGW32__)\n");
1851 fprintf(header, "static const WCHAR RuntimeClass_%s[] = {", c_name);
1852 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1853 fprintf(header, "0};\n");
1854 fprintf(header, "#elif defined(__GNUC__) && !defined(__cplusplus)\n");
1855 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1856 fprintf(header, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name, name);
1857 fprintf(header, "#else\n");
1858 fprintf(header, "extern const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = {", c_name);
1859 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1860 fprintf(header, "0};\n");
1861 fprintf(header, "#endif\n");
1862 fprintf(header, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name);
1863 free(c_name);
1864 free(name);
1865 if (contract) write_apicontract_guard_end(header, contract);
1866 fprintf(header, "\n");
1869 static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
1871 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1872 fprintf(header, "#define __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1873 fprintf(header, "#ifdef __cplusplus\n");
1874 write_namespace_start(header, runtimeclass->namespace);
1875 write_line(header, 0, "class %s;", runtimeclass->name);
1876 write_namespace_end(header, runtimeclass->namespace);
1877 fprintf(header, "#else\n");
1878 fprintf(header, "typedef struct %s %s;\n", runtimeclass->c_name, runtimeclass->c_name);
1879 fprintf(header, "#endif /* defined __cplusplus */\n");
1880 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass->c_name);
1883 static void write_import(FILE *header, const char *fname)
1885 char *hname = replace_extension( get_basename(fname), ".idl", "" );
1887 if (!strendswith( hname, ".h" )) hname = strmake( "%s.h", hname );
1888 fprintf(header, "#include <%s>\n", hname);
1889 free(hname);
1892 static void write_imports(FILE *header, const statement_list_t *stmts)
1894 const statement_t *stmt;
1895 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1897 switch (stmt->type)
1899 case STMT_TYPE:
1900 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1901 write_imports(header, type_iface_get_stmts(stmt->u.type));
1902 break;
1903 case STMT_TYPEREF:
1904 case STMT_IMPORTLIB:
1905 /* not included in header */
1906 break;
1907 case STMT_IMPORT:
1908 write_import(header, stmt->u.str);
1909 break;
1910 case STMT_TYPEDEF:
1911 case STMT_MODULE:
1912 case STMT_CPPQUOTE:
1913 case STMT_PRAGMA:
1914 case STMT_DECLARATION:
1915 /* not processed here */
1916 break;
1917 case STMT_LIBRARY:
1918 write_imports(header, stmt->u.lib->stmts);
1919 break;
1924 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1926 const statement_t *stmt;
1927 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1929 switch (stmt->type)
1931 case STMT_TYPE:
1932 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
1934 type_t *iface = stmt->u.type;
1935 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1936 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1938 write_forward(header, iface);
1939 if (type_iface_get_async_iface(iface))
1940 write_forward(header, type_iface_get_async_iface(iface));
1943 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1944 write_coclass_forward(header, stmt->u.type);
1945 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
1946 write_runtimeclass_forward(header, stmt->u.type);
1947 else if (type_get_type(stmt->u.type) == TYPE_PARAMETERIZED_TYPE)
1948 write_parameterized_type_forward(header, stmt->u.type);
1949 break;
1950 case STMT_TYPEREF:
1951 case STMT_IMPORTLIB:
1952 /* not included in header */
1953 break;
1954 case STMT_IMPORT:
1955 case STMT_TYPEDEF:
1956 case STMT_MODULE:
1957 case STMT_CPPQUOTE:
1958 case STMT_PRAGMA:
1959 case STMT_DECLARATION:
1960 /* not processed here */
1961 break;
1962 case STMT_LIBRARY:
1963 write_forward_decls(header, stmt->u.lib->stmts);
1964 break;
1969 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1971 const statement_t *stmt;
1972 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1974 switch (stmt->type)
1976 case STMT_TYPE:
1977 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
1979 type_t *iface = stmt->u.type, *async_iface;
1980 if (type_get_type(stmt->u.type) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1981 async_iface = type_iface_get_async_iface(iface);
1982 if (is_object(iface)) is_object_interface++;
1983 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1985 write_com_interface_start(header, iface);
1986 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1987 write_com_interface_end(header, iface);
1988 if (async_iface)
1990 write_com_interface_start(header, async_iface);
1991 write_com_interface_end(header, async_iface);
1994 else
1996 write_rpc_interface_start(header, iface);
1997 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1998 write_rpc_interface_end(header, iface);
2000 if (is_object(iface)) is_object_interface--;
2002 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
2003 write_coclass(header, stmt->u.type);
2004 else if (type_get_type(stmt->u.type) == TYPE_APICONTRACT)
2005 write_apicontract(header, stmt->u.type);
2006 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
2007 write_runtimeclass(header, stmt->u.type);
2008 else if (type_get_type(stmt->u.type) != TYPE_PARAMETERIZED_TYPE)
2009 write_type_definition(header, stmt->u.type, stmt->declonly);
2010 else
2012 is_object_interface++;
2013 write_parameterized_implementation(header, stmt->u.type, stmt->declonly);
2014 is_object_interface--;
2016 break;
2017 case STMT_TYPEREF:
2018 /* FIXME: shouldn't write out forward declarations for undefined
2019 * interfaces but a number of our IDL files depend on this */
2020 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
2021 write_forward(header, stmt->u.type);
2022 break;
2023 case STMT_IMPORTLIB:
2024 case STMT_MODULE:
2025 case STMT_PRAGMA:
2026 /* not included in header */
2027 break;
2028 case STMT_IMPORT:
2029 /* not processed here */
2030 break;
2031 case STMT_TYPEDEF:
2033 typeref_t *ref;
2034 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
2035 write_typedef(header, ref->type, stmt->declonly);
2036 break;
2038 case STMT_LIBRARY:
2039 fprintf(header, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2040 fprintf(header, "#define __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2041 write_library(header, stmt->u.lib);
2042 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
2043 fprintf(header, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt->u.lib->name);
2044 break;
2045 case STMT_CPPQUOTE:
2046 fprintf(header, "%s\n", stmt->u.str);
2047 break;
2048 case STMT_DECLARATION:
2049 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
2051 if (!ignore_funcs)
2053 int prefixes_differ = strcmp(prefix_client, prefix_server);
2055 if (prefixes_differ)
2057 fprintf(header, "/* client prototype */\n");
2058 write_function_proto(header, iface, stmt->u.var, prefix_client);
2059 fprintf(header, "/* server prototype */\n");
2061 write_function_proto(header, iface, stmt->u.var, prefix_server);
2064 else
2065 write_declaration(header, stmt->u.var);
2066 break;
2071 void write_header(const statement_list_t *stmts)
2073 FILE *header;
2075 if (!do_header) return;
2077 if(!(header = fopen(header_name, "w"))) {
2078 error("Could not open %s for output\n", header_name);
2079 return;
2081 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
2083 fprintf(header, "#ifdef _WIN32\n");
2084 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
2085 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
2086 fprintf(header, "#endif\n");
2087 fprintf(header, "#include <rpc.h>\n" );
2088 fprintf(header, "#include <rpcndr.h>\n" );
2089 if (!for_each_serializable(stmts, NULL, serializable_exists))
2090 fprintf(header, "#include <midles.h>\n" );
2091 fprintf(header, "#endif\n\n");
2093 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
2094 fprintf(header, "#include <windows.h>\n");
2095 fprintf(header, "#include <ole2.h>\n");
2096 fprintf(header, "#endif\n\n");
2098 fprintf(header, "#ifndef __%s__\n", header_token);
2099 fprintf(header, "#define __%s__\n\n", header_token);
2101 fprintf(header, "#ifndef __WIDL_INLINE\n");
2102 fprintf(header, "#if defined(__cplusplus) || defined(_MSC_VER)\n");
2103 fprintf(header, "#define __WIDL_INLINE inline\n");
2104 fprintf(header, "#elif defined(__GNUC__)\n");
2105 fprintf(header, "#define __WIDL_INLINE __inline__\n");
2106 fprintf(header, "#endif\n");
2107 fprintf(header, "#endif\n\n");
2109 fprintf(header, "/* Forward declarations */\n\n");
2110 write_forward_decls(header, stmts);
2112 fprintf(header, "/* Headers for imported files */\n\n");
2113 write_imports(header, stmts);
2114 fprintf(header, "\n");
2115 start_cplusplus_guard(header);
2117 write_header_stmts(header, stmts, NULL, FALSE);
2119 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
2120 fprintf(header, "\n");
2121 for_each_serializable(stmts, header, write_serialize_function_decl);
2122 write_user_types(header);
2123 write_generic_handle_routines(header);
2124 write_context_handle_rundowns(header);
2125 fprintf(header, "\n");
2126 fprintf(header, "/* End additional prototypes */\n");
2127 fprintf(header, "\n");
2129 end_cplusplus_guard(header);
2130 fprintf(header, "#endif /* __%s__ */\n", header_token);
2132 fclose(header);