wined3d: Disable WINED3DFMT_D16_LOCKABLE on backbuffer ORM.
[wine.git] / tools / widl / header.c
blob40c1f9d008eb0f57d8f029a1b5e72a83055dac43
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "expr.h"
37 #include "typetree.h"
38 #include "typelib.h"
40 static int indentation = 0;
41 static int is_object_interface = 0;
42 user_type_list_t user_type_list = LIST_INIT(user_type_list);
43 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
44 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
46 static void write_type_v(FILE *f, const decl_spec_t *t, int is_field, int declonly, const char *name, enum name_type name_type);
48 static void write_apicontract_guard_start(FILE *header, const expr_t *expr);
49 static void write_apicontract_guard_end(FILE *header, const expr_t *expr);
51 static void write_widl_using_macros(FILE *header, type_t *iface);
53 static void indent(FILE *h, int delta)
55 int c;
56 if (delta < 0) indentation += delta;
57 for (c=0; c<indentation; c++) fprintf(h, " ");
58 if (delta > 0) indentation += delta;
61 static void write_line(FILE *f, int delta, const char *fmt, ...)
63 va_list ap;
64 indent(f, delta);
65 va_start(ap, fmt);
66 vfprintf(f, fmt, ap);
67 va_end(ap);
68 fprintf(f, "\n");
71 int is_ptrchain_attr(const var_t *var, enum attr_type t)
73 if (is_attr(var->attrs, t))
74 return 1;
75 else
77 type_t *type = var->declspec.type;
78 for (;;)
80 if (is_attr(type->attrs, t))
81 return 1;
82 else if (type_is_alias(type))
83 type = type_alias_get_aliasee_type(type);
84 else if (is_ptr(type))
85 type = type_pointer_get_ref_type(type);
86 else return 0;
91 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
93 const type_t *t = type;
94 for (;;)
96 if (is_attr(t->attrs, attr))
97 return 1;
98 else if (type_is_alias(t))
99 t = type_alias_get_aliasee_type(t);
100 else return 0;
104 int is_attr(const attr_list_t *list, enum attr_type t)
106 const attr_t *attr;
107 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
108 if (attr->type == t) return 1;
109 return 0;
112 void *get_attrp(const attr_list_t *list, enum attr_type t)
114 const attr_t *attr;
115 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
116 if (attr->type == t) return attr->u.pval;
117 return NULL;
120 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
122 const attr_t *attr;
123 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
124 if (attr->type == t) return attr->u.ival;
125 return 0;
128 static char *format_parameterized_type_args(const type_t *type, const char *prefix, const char *suffix)
130 typeref_list_t *params;
131 typeref_t *ref;
132 size_t len = 0, pos = 0;
133 char *buf = NULL;
135 params = type->details.parameterized.params;
136 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
138 assert(ref->type->type_type != TYPE_POINTER);
139 pos += strappend(&buf, &len, pos, "%s%s%s", prefix, ref->type->name, suffix);
140 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
143 if (!buf) return xstrdup("");
144 return buf;
147 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
149 if (!uuid) return;
150 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
151 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
152 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
153 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
154 uuid->Data4[6], uuid->Data4[7]);
157 static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
159 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
160 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
161 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
162 type->c_name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
163 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
164 uuid->Data4[7]);
165 fprintf(f, "#endif\n");
168 static const char *uuid_string(const UUID *uuid)
170 static char buf[37];
172 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
173 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
174 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
176 return buf;
179 static void write_namespace_start(FILE *header, struct namespace *namespace)
181 if(is_global_namespace(namespace)) {
182 if(use_abi_namespace)
183 write_line(header, 1, "namespace ABI {");
184 return;
187 write_namespace_start(header, namespace->parent);
188 write_line(header, 1, "namespace %s {", namespace->name);
191 static void write_namespace_end(FILE *header, struct namespace *namespace)
193 if(is_global_namespace(namespace)) {
194 if(use_abi_namespace)
195 write_line(header, -1, "}", namespace->name);
196 return;
199 write_line(header, -1, "}", namespace->name);
200 write_namespace_end(header, namespace->parent);
203 const char *get_name(const var_t *v)
205 static char *buffer;
206 free( buffer );
207 if (is_attr( v->attrs, ATTR_EVENTADD ))
208 return buffer = strmake( "add_%s", v->name );
209 if (is_attr( v->attrs, ATTR_EVENTREMOVE ))
210 return buffer = strmake( "remove_%s", v->name );
211 if (is_attr( v->attrs, ATTR_PROPGET ))
212 return buffer = strmake( "get_%s", v->name );
213 if (is_attr( v->attrs, ATTR_PROPPUT ))
214 return buffer = strmake( "put_%s", v->name );
215 if (is_attr( v->attrs, ATTR_PROPPUTREF ))
216 return buffer = strmake( "putref_%s", v->name );
217 buffer = NULL;
218 return v->name;
221 static void write_fields(FILE *h, var_list_t *fields, enum name_type name_type)
223 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
224 const char *name;
225 char buf[32];
226 var_t *v;
228 if (!fields) return;
230 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
231 if (!v || !v->declspec.type) continue;
233 switch(type_get_type_detect_alias(v->declspec.type)) {
234 case TYPE_STRUCT:
235 case TYPE_ENCAPSULATED_UNION:
236 nameless_struct_cnt++;
237 break;
238 case TYPE_UNION:
239 nameless_union_cnt++;
240 break;
241 default:
246 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
247 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
248 if (!v || !v->declspec.type) continue;
249 if (contract) write_apicontract_guard_start(h, contract);
251 indent(h, 0);
252 name = v->name;
254 switch(type_get_type_detect_alias(v->declspec.type)) {
255 case TYPE_STRUCT:
256 case TYPE_ENCAPSULATED_UNION:
257 if(!v->name) {
258 fprintf(h, "__C89_NAMELESS ");
259 if(nameless_struct_cnt == 1) {
260 name = "__C89_NAMELESSSTRUCTNAME";
261 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
262 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
263 name = buf;
266 break;
267 case TYPE_UNION:
268 if(!v->name) {
269 fprintf(h, "__C89_NAMELESS ");
270 if(nameless_union_cnt == 1) {
271 name = "__C89_NAMELESSUNIONNAME";
272 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
273 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
274 name = buf;
277 break;
278 default:
281 write_type_v(h, &v->declspec, TRUE, v->declonly, name, name_type);
282 fprintf(h, ";\n");
283 if (contract) write_apicontract_guard_end(h, contract);
287 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
289 var_t *v;
290 if (!enums) return;
291 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
293 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
294 if (contract) write_apicontract_guard_start(h, contract);
295 if (v->name) {
296 indent(h, 0);
297 if(!enum_name)
298 fprintf(h, "%s", get_name(v));
299 else
300 fprintf(h, "%s_%s", enum_name, get_name(v));
301 if (v->eval) {
302 fprintf(h, " = ");
303 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
306 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
307 else fprintf(h, "\n");
308 if (contract) write_apicontract_guard_end(h, contract);
312 int needs_space_after(type_t *t)
314 return (type_is_alias(t) ||
315 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
318 static int decl_needs_parens(const type_t *t)
320 if (type_is_alias(t))
321 return FALSE;
322 if (is_array(t) && !type_array_is_decl_as_ptr(t))
323 return TRUE;
324 return is_func(t);
327 static void write_pointer_left(FILE *h, type_t *ref)
329 if (needs_space_after(ref))
330 fprintf(h, " ");
331 if (decl_needs_parens(ref))
332 fprintf(h, "(");
333 if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
335 const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
336 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
337 if (callconv) fprintf(h, "%s ", callconv);
339 fprintf(h, "*");
342 void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
344 type_t *t = ds->type;
345 const char *name;
346 char *args;
348 if (!h) return;
350 name = type_get_name(t, name_type);
352 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
353 fprintf(h, "inline ");
355 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
356 fprintf(h, "const ");
358 if (!winrt_mode && type_is_alias(t)) fprintf(h, "%s", t->name);
359 else {
360 switch (type_get_type_detect_alias(t)) {
361 case TYPE_ENUM:
362 if (!declonly && !t->written) {
363 assert(t->defined);
364 if (name) fprintf(h, "enum %s {\n", name);
365 else fprintf(h, "enum {\n");
366 t->written = TRUE;
367 indentation++;
368 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
369 indent(h, -1);
370 fprintf(h, "}");
372 else fprintf(h, "enum %s", name ? name : "");
373 break;
374 case TYPE_STRUCT:
375 case TYPE_ENCAPSULATED_UNION:
376 if (!declonly && !t->written) {
377 assert(t->defined);
378 if (name) fprintf(h, "struct %s {\n", name);
379 else fprintf(h, "struct {\n");
380 t->written = TRUE;
381 indentation++;
382 if (type_get_type(t) != TYPE_STRUCT)
383 write_fields(h, type_encapsulated_union_get_fields(t), name_type);
384 else
385 write_fields(h, type_struct_get_fields(t), name_type);
386 indent(h, -1);
387 fprintf(h, "}");
389 else fprintf(h, "struct %s", name ? name : "");
390 break;
391 case TYPE_UNION:
392 if (!declonly && !t->written) {
393 assert(t->defined);
394 if (t->name) fprintf(h, "union %s {\n", t->name);
395 else fprintf(h, "union {\n");
396 t->written = TRUE;
397 indentation++;
398 write_fields(h, type_union_get_cases(t), name_type);
399 indent(h, -1);
400 fprintf(h, "}");
402 else fprintf(h, "union %s", t->name ? t->name : "");
403 break;
404 case TYPE_POINTER:
406 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
407 write_pointer_left(h, type_pointer_get_ref_type(t));
408 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
409 break;
411 case TYPE_ARRAY:
412 if (t->name && type_array_is_decl_as_ptr(t))
413 fprintf(h, "%s", t->name);
414 else
416 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
417 if (type_array_is_decl_as_ptr(t))
418 write_pointer_left(h, type_array_get_element_type(t));
420 break;
421 case TYPE_FUNCTION:
423 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
425 /* A pointer to a function has to write the calling convention inside
426 * the parentheses. There's no way to handle that here, so we have to
427 * use an extra parameter to tell us whether to write the calling
428 * convention or not. */
429 if (write_callconv)
431 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
432 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
433 if (callconv) fprintf(h, " %s ", callconv);
435 break;
437 case TYPE_BASIC:
438 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
439 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
440 type_basic_get_type(t) != TYPE_BASIC_LONG &&
441 type_basic_get_type(t) != TYPE_BASIC_HYPER)
443 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
444 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
446 switch (type_basic_get_type(t))
448 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
449 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
450 case TYPE_BASIC_INT: fprintf(h, "int"); break;
451 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
452 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
453 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
454 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
455 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
456 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
457 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
458 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
459 case TYPE_BASIC_INT32:
460 if (type_basic_get_sign(t) > 0)
461 fprintf(h, "UINT32");
462 else
463 fprintf(h, "INT32");
464 break;
465 case TYPE_BASIC_LONG:
466 if (type_basic_get_sign(t) > 0)
467 fprintf(h, "ULONG");
468 else
469 fprintf(h, "LONG");
470 break;
471 case TYPE_BASIC_INT64:
472 if (type_basic_get_sign(t) > 0)
473 fprintf(h, "UINT64");
474 else
475 fprintf(h, "INT64");
476 break;
477 case TYPE_BASIC_HYPER:
478 if (type_basic_get_sign(t) > 0)
479 fprintf(h, "MIDL_uhyper");
480 else
481 fprintf(h, "hyper");
482 break;
484 break;
485 case TYPE_INTERFACE:
486 case TYPE_MODULE:
487 case TYPE_COCLASS:
488 fprintf(h, "%s", type_get_qualified_name(t, name_type));
489 break;
490 case TYPE_RUNTIMECLASS:
491 fprintf(h, "%s", type_get_name(type_runtimeclass_get_default_iface(t), name_type));
492 break;
493 case TYPE_DELEGATE:
494 fprintf(h, "%s", type_get_qualified_name(type_delegate_get_iface(t), name_type));
495 break;
496 case TYPE_VOID:
497 fprintf(h, "void");
498 break;
499 case TYPE_BITFIELD:
501 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
502 write_type_left(h, &ds, name_type, declonly, TRUE);
503 break;
505 case TYPE_ALIAS:
507 const decl_spec_t *ds = type_alias_get_aliasee(t);
508 int in_namespace = ds && ds->type && ds->type->namespace && !is_global_namespace(ds->type->namespace);
509 if (!in_namespace) fprintf(h, "%s", t->name);
510 else write_type_left(h, ds, name_type, declonly, write_callconv);
511 break;
513 case TYPE_PARAMETERIZED_TYPE:
515 type_t *iface = type_parameterized_type_get_real_type(t);
516 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
517 args = format_parameterized_type_args(t, "", "_logical");
518 fprintf(h, "%s<%s>", iface->name, args);
519 free(args);
520 break;
522 case TYPE_PARAMETER:
523 fprintf(h, "%s_abi", t->name);
524 break;
525 case TYPE_APICONTRACT:
526 /* shouldn't be here */
527 assert(0);
528 break;
533 void write_type_right(FILE *h, type_t *t, int is_field)
535 if (!h) return;
536 if (type_is_alias(t)) return;
538 switch (type_get_type(t))
540 case TYPE_ARRAY:
542 type_t *elem = type_array_get_element_type(t);
543 if (type_array_is_decl_as_ptr(t))
545 if (decl_needs_parens(elem))
546 fprintf(h, ")");
548 else
550 if (is_conformant_array(t))
551 fprintf(h, "[%s]", is_field ? "1" : "");
552 else
553 fprintf(h, "[%u]", type_array_get_dim(t));
555 write_type_right(h, elem, FALSE);
556 break;
558 case TYPE_FUNCTION:
560 const var_list_t *args = type_function_get_args(t);
561 fputc('(', h);
562 if (args) write_args(h, args, NULL, 0, FALSE, NAME_DEFAULT);
563 else
564 fprintf(h, "void");
565 fputc(')', h);
566 write_type_right(h, type_function_get_rettype(t), FALSE);
567 break;
569 case TYPE_POINTER:
571 type_t *ref = type_pointer_get_ref_type(t);
572 if (decl_needs_parens(ref))
573 fprintf(h, ")");
574 write_type_right(h, ref, FALSE);
575 break;
577 case TYPE_BITFIELD:
578 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
579 break;
580 case TYPE_VOID:
581 case TYPE_BASIC:
582 case TYPE_ENUM:
583 case TYPE_STRUCT:
584 case TYPE_ENCAPSULATED_UNION:
585 case TYPE_UNION:
586 case TYPE_ALIAS:
587 case TYPE_MODULE:
588 case TYPE_COCLASS:
589 case TYPE_INTERFACE:
590 case TYPE_RUNTIMECLASS:
591 case TYPE_DELEGATE:
592 case TYPE_PARAMETERIZED_TYPE:
593 case TYPE_PARAMETER:
594 break;
595 case TYPE_APICONTRACT:
596 /* not supposed to be here */
597 assert(0);
598 break;
602 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)
604 type_t *t = ds->type;
606 if (!h) return;
608 if (t) write_type_left(h, ds, name_type, declonly, TRUE);
610 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
612 if (t)
613 write_type_right(h, t, is_field);
616 static void write_type_definition(FILE *f, type_t *t, int declonly)
618 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
619 int save_written = t->written;
620 decl_spec_t ds = {.type = t};
621 expr_t *contract = get_attrp(t->attrs, ATTR_CONTRACT);
623 if (contract) write_apicontract_guard_start(f, contract);
624 if(in_namespace) {
625 fprintf(f, "#ifdef __cplusplus\n");
626 fprintf(f, "} /* extern \"C\" */\n");
627 write_namespace_start(f, t->namespace);
629 indent(f, 0);
630 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
631 fprintf(f, ";\n");
632 if(in_namespace) {
633 t->written = save_written;
634 write_namespace_end(f, t->namespace);
635 fprintf(f, "extern \"C\" {\n");
636 fprintf(f, "#else\n");
637 write_type_left(f, &ds, NAME_C, declonly, TRUE);
638 fprintf(f, ";\n");
639 if (winrt_mode) write_widl_using_macros(f, t);
640 fprintf(f, "#endif\n\n");
642 if (contract) write_apicontract_guard_end(f, contract);
645 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
647 write_type_v(f, t, FALSE, TRUE, name, NAME_DEFAULT);
650 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
652 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
655 static int user_type_registered(const char *name)
657 user_type_t *ut;
658 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
659 if (!strcmp(name, ut->name))
660 return 1;
661 return 0;
664 static int context_handle_registered(const char *name)
666 context_handle_t *ch;
667 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
668 if (!strcmp(name, ch->name))
669 return 1;
670 return 0;
673 static int generic_handle_registered(const char *name)
675 generic_handle_t *gh;
676 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
677 if (!strcmp(name, gh->name))
678 return 1;
679 return 0;
682 unsigned int get_context_handle_offset( const type_t *type )
684 context_handle_t *ch;
685 unsigned int index = 0;
687 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
689 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
690 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
691 else error( "internal error: %s is not a context handle\n", type->name );
693 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
695 if (!strcmp( type->name, ch->name )) return index;
696 index++;
698 error( "internal error: %s is not registered as a context handle\n", type->name );
699 return index;
702 unsigned int get_generic_handle_offset( const type_t *type )
704 generic_handle_t *gh;
705 unsigned int index = 0;
707 while (!is_attr( type->attrs, ATTR_HANDLE ))
709 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
710 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
711 else error( "internal error: %s is not a generic handle\n", type->name );
713 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
715 if (!strcmp( type->name, gh->name )) return index;
716 index++;
718 error( "internal error: %s is not registered as a generic handle\n", type->name );
719 return index;
722 /* check for types which require additional prototypes to be generated in the
723 * header */
724 void check_for_additional_prototype_types(type_t *type)
726 if (!type) return;
727 for (;;) {
728 const char *name = type->name;
729 if (type->user_types_registered) break;
730 type->user_types_registered = 1;
731 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
732 if (!context_handle_registered(name))
734 context_handle_t *ch = xmalloc(sizeof(*ch));
735 ch->name = xstrdup(name);
736 list_add_tail(&context_handle_list, &ch->entry);
738 /* don't carry on parsing fields within this type */
739 break;
741 if ((type_get_type(type) != TYPE_BASIC ||
742 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
743 is_attr(type->attrs, ATTR_HANDLE)) {
744 if (!generic_handle_registered(name))
746 generic_handle_t *gh = xmalloc(sizeof(*gh));
747 gh->name = xstrdup(name);
748 list_add_tail(&generic_handle_list, &gh->entry);
750 /* don't carry on parsing fields within this type */
751 break;
753 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
754 if (!user_type_registered(name))
756 user_type_t *ut = xmalloc(sizeof *ut);
757 ut->name = xstrdup(name);
758 list_add_tail(&user_type_list, &ut->entry);
760 /* don't carry on parsing fields within this type as we are already
761 * using a wire marshaled type */
762 break;
764 else if (type_is_complete(type))
766 var_list_t *vars;
767 const var_t *v;
768 switch (type_get_type_detect_alias(type))
770 case TYPE_ENUM:
771 vars = type_enum_get_values(type);
772 break;
773 case TYPE_STRUCT:
774 vars = type_struct_get_fields(type);
775 break;
776 case TYPE_UNION:
777 vars = type_union_get_cases(type);
778 break;
779 default:
780 vars = NULL;
781 break;
783 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
784 check_for_additional_prototype_types(v->declspec.type);
787 if (type_is_alias(type))
788 type = type_alias_get_aliasee_type(type);
789 else if (is_ptr(type))
790 type = type_pointer_get_ref_type(type);
791 else if (is_array(type))
792 type = type_array_get_element_type(type);
793 else
794 break;
798 static int write_serialize_function_decl(FILE *header, const type_t *type)
800 write_serialize_functions(header, type, NULL);
801 return 1;
804 static int serializable_exists(FILE *header, const type_t *type)
806 return 0;
809 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
810 int (*proc)(FILE*, const type_t*))
812 statement_t *stmt, *iface_stmt;
813 statement_list_t *iface_stmts;
814 typeref_t *ref;
816 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
818 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
819 continue;
821 iface_stmts = type_iface_get_stmts(stmt->u.type);
822 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
824 if (iface_stmt->type != STMT_TYPEDEF) continue;
825 if (iface_stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, iface_stmt->u.type_list, typeref_t, entry)
827 if (!is_attr(ref->type->attrs, ATTR_ENCODE)
828 && !is_attr(ref->type->attrs, ATTR_DECODE))
829 continue;
830 if (!proc(header, ref->type))
831 return 0;
836 return 1;
839 static void write_user_types(FILE *header)
841 user_type_t *ut;
842 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
844 const char *name = ut->name;
845 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
846 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
847 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
848 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
852 static void write_context_handle_rundowns(FILE *header)
854 context_handle_t *ch;
855 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
857 const char *name = ch->name;
858 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
862 static void write_generic_handle_routines(FILE *header)
864 generic_handle_t *gh;
865 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
867 const char *name = gh->name;
868 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
869 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
873 static void write_typedef(FILE *header, type_t *type, int declonly)
875 type_t *t = type_alias_get_aliasee_type(type);
876 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace)) return;
877 fprintf(header, "typedef ");
878 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name, NAME_DEFAULT);
879 fprintf(header, ";\n");
882 int is_const_decl(const var_t *var)
884 const decl_spec_t *t;
885 /* strangely, MIDL accepts a const attribute on any pointer in the
886 * declaration to mean that data isn't being instantiated. this appears
887 * to be a bug, but there is no benefit to being incompatible with MIDL,
888 * so we'll do the same thing */
889 for (t = &var->declspec; ; )
891 if (t->qualifier & TYPE_QUALIFIER_CONST)
892 return TRUE;
893 else if (is_ptr(t->type))
894 t = type_pointer_get_ref(t->type);
895 else break;
897 return FALSE;
900 static void write_declaration(FILE *header, const var_t *v)
902 if (is_const_decl(v) && v->eval)
904 fprintf(header, "#define %s (", v->name);
905 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
906 fprintf(header, ")\n\n");
908 else
910 switch (v->declspec.stgclass)
912 case STG_NONE:
913 case STG_REGISTER: /* ignored */
914 break;
915 case STG_STATIC:
916 fprintf(header, "static ");
917 break;
918 case STG_EXTERN:
919 fprintf(header, "extern ");
920 break;
922 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name, NAME_DEFAULT);
923 fprintf(header, ";\n\n");
927 static void write_library(FILE *header, const typelib_t *typelib)
929 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
930 fprintf(header, "\n");
931 write_guid(header, "LIBID", typelib->name, uuid);
932 fprintf(header, "\n");
936 const type_t* get_explicit_generic_handle_type(const var_t* var)
938 const type_t *t;
939 for (t = var->declspec.type;
940 is_ptr(t) || type_is_alias(t);
941 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
942 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
943 is_attr(t->attrs, ATTR_HANDLE))
944 return t;
945 return NULL;
948 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
949 unsigned char *explicit_fc, unsigned char *implicit_fc )
951 const var_t *var;
952 const var_list_t *args = type_function_get_args( func->declspec.type );
954 *explicit_fc = *implicit_fc = 0;
955 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
957 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
958 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
960 *explicit_fc = FC_BIND_PRIMITIVE;
961 return var;
963 if (get_explicit_generic_handle_type( var ))
965 *explicit_fc = FC_BIND_GENERIC;
966 return var;
968 if (is_context_handle( var->declspec.type ))
970 *explicit_fc = FC_BIND_CONTEXT;
971 return var;
975 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
977 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
978 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
979 *implicit_fc = FC_BIND_PRIMITIVE;
980 else
981 *implicit_fc = FC_BIND_GENERIC;
982 return var;
985 *implicit_fc = FC_AUTO_HANDLE;
986 return NULL;
989 int has_out_arg_or_return(const var_t *func)
991 const var_t *var;
993 if (!is_void(type_function_get_rettype(func->declspec.type)))
994 return 1;
996 if (!type_function_get_args(func->declspec.type))
997 return 0;
999 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1000 if (is_attr(var->attrs, ATTR_OUT))
1001 return 1;
1003 return 0;
1007 /********** INTERFACES **********/
1009 int is_object(const type_t *iface)
1011 const attr_t *attr;
1012 if (type_is_defined(iface) && (type_get_type(iface) == TYPE_DELEGATE || type_iface_get_inherit(iface)))
1013 return 1;
1014 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
1015 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
1016 return 0;
1019 int is_local(const attr_list_t *a)
1021 return is_attr(a, ATTR_LOCAL);
1024 const var_t *is_callas(const attr_list_t *a)
1026 return get_attrp(a, ATTR_CALLAS);
1029 static int is_inherited_method(const type_t *iface, const var_t *func)
1031 while ((iface = type_iface_get_inherit(iface)))
1033 const statement_t *stmt;
1034 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1036 const var_t *funccmp = stmt->u.var;
1038 if (!is_callas(func->attrs))
1040 char inherit_name[256];
1041 /* compare full name including property prefix */
1042 strcpy(inherit_name, get_name(funccmp));
1043 if (!strcmp(inherit_name, get_name(func))) return 1;
1048 return 0;
1051 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
1053 if (iface == child)
1054 return 0;
1058 const statement_t *stmt;
1059 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
1061 const var_t *funccmp = stmt->u.var;
1063 if (!is_callas(func->attrs))
1065 char inherit_name[256];
1066 /* compare full name including property prefix */
1067 strcpy(inherit_name, get_name(funccmp));
1068 if (!strcmp(inherit_name, get_name(func))) return 1;
1072 while ((child = type_iface_get_inherit(child)) && child != iface);
1074 return 0;
1077 static int is_aggregate_return(const var_t *func)
1079 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1080 return type == TYPE_STRUCT || type == TYPE_UNION ||
1081 type == TYPE_COCLASS || type == TYPE_INTERFACE ||
1082 type == TYPE_RUNTIMECLASS;
1085 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1087 static char buff[255];
1088 if (is_inherited_method(iface, func))
1089 sprintf(buff, "%s_%s", iface->name, get_name(func));
1090 else
1091 sprintf(buff, "%s", get_name(func));
1092 return buff;
1095 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1097 const statement_t *stmt;
1098 int first_iface = 1;
1100 if (type_iface_get_inherit(iface))
1101 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1103 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1105 const var_t *func = stmt->u.var;
1107 if (first_iface)
1109 fprintf(header, "/*** %s methods ***/\n", iface->name);
1110 first_iface = 0;
1113 if (is_override_method(iface, child, func))
1114 continue;
1116 if (!is_callas(func->attrs)) {
1117 const var_t *arg;
1119 fprintf(header, "#define %s_%s(This", name, get_name(func));
1120 if (type_function_get_args(func->declspec.type))
1121 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1122 fprintf(header, ",%s", arg->name);
1123 fprintf(header, ") ");
1125 if (is_aggregate_return(func))
1127 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1128 continue;
1131 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1132 if (type_function_get_args(func->declspec.type))
1133 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1134 fprintf(header, ",%s", arg->name);
1135 fprintf(header, ")\n");
1140 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent, enum name_type name_type)
1142 const var_t *arg;
1143 int count = 0;
1145 if (do_indent)
1147 indentation++;
1148 indent(h, 0);
1150 if (method == 1) {
1151 fprintf(h, "%s* This", name);
1152 count++;
1154 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1155 if (count) {
1156 if (do_indent)
1158 fprintf(h, ",\n");
1159 indent(h, 0);
1161 else fprintf(h, ",");
1163 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1164 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1165 * defining UDTs inside of an argument list. */
1166 write_type_v(h, &arg->declspec, FALSE, TRUE, arg->name, name_type);
1167 if (method == 2) {
1168 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1169 if (expr) {
1170 const var_t *tail_arg;
1172 /* Output default value only if all following arguments also have default value. */
1173 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1174 if(tail_arg == arg) {
1175 expr_t bstr;
1177 /* Fixup the expression type for a BSTR like midl does. */
1178 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1180 bstr = *expr;
1181 bstr.type = EXPR_WSTRLIT;
1182 expr = &bstr;
1185 fprintf(h, " = ");
1186 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1187 break;
1189 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1190 break;
1194 count++;
1196 if (do_indent) indentation--;
1199 static void write_cpp_method_def(FILE *header, const type_t *iface)
1201 const statement_t *stmt;
1203 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1205 const var_t *func = stmt->u.var;
1206 if (!is_callas(func->attrs)) {
1207 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1208 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1209 const var_list_t *args = type_function_get_args(func->declspec.type);
1210 const var_t *arg;
1212 if (!callconv) callconv = "STDMETHODCALLTYPE";
1214 if (is_aggregate_return(func)) {
1215 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1217 indent(header, 0);
1218 fprintf(header, "virtual ");
1219 write_type_decl_left(header, ret);
1220 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1221 ++indentation;
1222 indent(header, 0);
1223 write_type_decl_left(header, ret);
1224 fprintf(header, " *__ret");
1225 --indentation;
1226 if (args) {
1227 fprintf(header, ",\n");
1228 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1230 fprintf(header, ") = 0;\n");
1232 indent(header, 0);
1233 write_type_decl_left(header, ret);
1234 fprintf(header, " %s %s(\n", callconv, get_name(func));
1235 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1236 fprintf(header, ")\n");
1237 indent(header, 0);
1238 fprintf(header, "{\n");
1239 ++indentation;
1240 indent(header, 0);
1241 write_type_decl_left(header, ret);
1242 fprintf(header, " __ret;\n");
1243 indent(header, 0);
1244 fprintf(header, "return *%s(&__ret", get_name(func));
1245 if (args)
1246 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1247 fprintf(header, ", %s", arg->name);
1248 fprintf(header, ");\n");
1249 --indentation;
1250 indent(header, 0);
1251 fprintf(header, "}\n");
1253 fprintf(header, "#else\n");
1256 indent(header, 0);
1257 fprintf(header, "virtual ");
1258 write_type_decl_left(header, ret);
1259 fprintf(header, " %s %s(\n", callconv, get_name(func));
1260 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1261 fprintf(header, ") = 0;\n");
1263 if (is_aggregate_return(func))
1264 fprintf(header, "#endif\n");
1265 fprintf(header, "\n");
1270 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1272 const statement_t *stmt;
1273 int first_iface = 1;
1275 if (type_iface_get_inherit(iface))
1276 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1278 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1280 const var_t *func = stmt->u.var;
1282 if (first_iface)
1284 fprintf(header, "/*** %s methods ***/\n", iface->name);
1285 first_iface = 0;
1288 if (is_override_method(iface, child, func))
1289 continue;
1291 if (!is_callas(func->attrs)) {
1292 const var_t *arg;
1294 fprintf(header, "static FORCEINLINE ");
1295 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1296 fprintf(header, " %s_%s(", name, get_name(func));
1297 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE, NAME_C);
1298 fprintf(header, ") {\n");
1299 ++indentation;
1300 if (!is_aggregate_return(func)) {
1301 indent(header, 0);
1302 fprintf(header, "%sThis->lpVtbl->%s(This",
1303 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1304 get_vtbl_entry_name(iface, func));
1305 } else {
1306 indent(header, 0);
1307 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1308 fprintf(header, " __ret;\n");
1309 indent(header, 0);
1310 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1312 if (type_function_get_args(func->declspec.type))
1313 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1314 fprintf(header, ",%s", arg->name);
1315 fprintf(header, ");\n");
1316 --indentation;
1317 fprintf(header, "}\n");
1322 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1324 const statement_t *stmt;
1325 int first_iface = 1;
1327 if (type_iface_get_inherit(iface))
1328 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1330 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1332 const var_t *func = stmt->u.var;
1333 if (first_iface) {
1334 indent(header, 0);
1335 fprintf(header, "/*** %s methods ***/\n", iface->name);
1336 first_iface = 0;
1338 if (!is_callas(func->attrs)) {
1339 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1340 if (!callconv) callconv = "STDMETHODCALLTYPE";
1341 indent(header, 0);
1342 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1343 if (is_aggregate_return(func))
1344 fprintf(header, " *");
1345 if (is_inherited_method(iface, func))
1346 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1347 else
1348 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1349 ++indentation;
1350 indent(header, 0);
1351 fprintf(header, "%s *This", name);
1352 if (is_aggregate_return(func)) {
1353 fprintf(header, ",\n");
1354 indent(header, 0);
1355 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1356 fprintf(header, " *__ret");
1358 --indentation;
1359 if (type_function_get_args(func->declspec.type)) {
1360 fprintf(header, ",\n");
1361 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE, NAME_C);
1363 fprintf(header, ");\n");
1364 fprintf(header, "\n");
1369 static void write_c_method_def(FILE *header, const type_t *iface)
1371 do_write_c_method_def(header, iface, iface->c_name);
1374 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1376 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1379 static void write_method_proto(FILE *header, const type_t *iface)
1381 const statement_t *stmt;
1383 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1385 const var_t *func = stmt->u.var;
1387 if (is_callas(func->attrs)) {
1388 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1389 if (!callconv) callconv = "STDMETHODCALLTYPE";
1390 /* proxy prototype */
1391 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1392 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1393 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1394 fprintf(header, ");\n");
1395 /* stub prototype */
1396 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1397 fprintf(header, " IRpcStubBuffer* This,\n");
1398 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1399 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1400 fprintf(header, " DWORD* pdwStubPhase);\n");
1405 static void write_locals(FILE *fp, const type_t *iface, int body)
1407 static const char comment[]
1408 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1409 const statement_t *stmt;
1411 if (!is_object(iface))
1412 return;
1414 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1415 const var_t *func = stmt->u.var;
1416 const var_t *cas = is_callas(func->attrs);
1418 if (cas) {
1419 const statement_t *stmt2 = NULL;
1420 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1421 if (!strcmp(get_name(stmt2->u.var), cas->name))
1422 break;
1423 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1424 const var_t *m = stmt2->u.var;
1425 /* proxy prototype - use local prototype */
1426 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1427 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1428 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1429 fprintf(fp, ")");
1430 if (body) {
1431 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1432 fprintf(fp, "\n{\n");
1433 fprintf(fp, " %s\n", comment);
1434 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1435 fprintf(fp, " return E_NOTIMPL;\n");
1436 else if (type_get_type(rt->type) != TYPE_VOID) {
1437 fprintf(fp, " ");
1438 write_type_decl(fp, rt, "rv");
1439 fprintf(fp, ";\n");
1440 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1441 fprintf(fp, " return rv;\n");
1443 fprintf(fp, "}\n\n");
1445 else
1446 fprintf(fp, ";\n");
1447 /* stub prototype - use remotable prototype */
1448 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1449 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1450 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1451 fprintf(fp, ")");
1452 if (body)
1453 /* Remotable methods must all return HRESULTs. */
1454 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1455 else
1456 fprintf(fp, ";\n");
1458 else
1459 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1464 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1466 const statement_t *stmt;
1467 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1469 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1470 write_locals(local_stubs, stmt->u.type, TRUE);
1474 void write_local_stubs(const statement_list_t *stmts)
1476 FILE *local_stubs;
1478 if (!local_stubs_name) return;
1480 local_stubs = fopen(local_stubs_name, "w");
1481 if (!local_stubs) {
1482 error("Could not open %s for output\n", local_stubs_name);
1483 return;
1485 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1486 fprintf(local_stubs, "#include <objbase.h>\n");
1487 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1489 write_local_stubs_stmts(local_stubs, stmts);
1491 fclose(local_stubs);
1494 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1496 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1498 if (!callconv) callconv = "__cdecl";
1499 /* FIXME: do we need to handle call_as? */
1500 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1501 fprintf(header, " %s ", callconv);
1502 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1503 if (type_function_get_args(fun->declspec.type))
1504 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE, NAME_DEFAULT);
1505 else
1506 fprintf(header, " void");
1507 fprintf(header, ");\n\n");
1510 static void write_parameterized_type_forward(FILE *header, type_t *type)
1512 type_t *iface = type->details.parameterized.type;
1513 char *args;
1515 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1517 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1518 write_namespace_start(header, type->namespace);
1520 args = format_parameterized_type_args(type, "class ", "");
1521 write_line(header, 0, "template <%s>", args);
1522 write_line(header, 0, "struct %s_impl;\n", iface->name);
1524 write_line(header, 0, "template <%s>", args);
1525 free(args);
1526 args = format_parameterized_type_args(type, "", "");
1527 write_line(header, 0, "struct %s : %s_impl<%s> {};", iface->name, iface->name, args);
1528 free(args);
1530 write_namespace_end(header, type->namespace);
1531 fprintf(header, "#endif\n\n" );
1534 static void write_parameterized_implementation(FILE *header, type_t *type, int declonly)
1536 const statement_t *stmt;
1537 typeref_list_t *params = type->details.parameterized.params;
1538 typeref_t *ref;
1539 type_t *iface = type->details.parameterized.type, *base;
1540 char *args = NULL;
1542 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1543 write_line(header, 0, "} /* extern \"C\" */");
1544 write_namespace_start(header, type->namespace);
1546 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1547 base = type_iface_get_inherit(iface);
1549 args = format_parameterized_type_args(type, "class ", "");
1550 write_line(header, 0, "template <%s>", args);
1551 free(args);
1552 write_line(header, 0, "struct %s_impl%s", iface->name, base ? strmake(" : %s", base->name) : "");
1553 write_line(header, 0, "{");
1555 write_line(header, 1, "private:");
1556 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1558 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetAbiType<%s>::type %s_abi;", ref->type->name, ref->type->name);
1559 write_line(header, 0, "typedef typename Windows::Foundation::Internal::GetLogicalType<%s>::type %s_logical;", ref->type->name, ref->type->name);
1561 indentation -= 1;
1563 write_line(header, 1, "public:");
1564 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
1565 write_line(header, 0, "typedef %s %s_complex;", ref->type->name, ref->type->name);
1567 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1569 const var_t *func = stmt->u.var;
1570 if (is_callas(func->attrs)) continue;
1571 indent(header, 1);
1572 fprintf(header, "virtual ");
1573 write_type_decl_left(header, &func->declspec);
1574 fprintf(header, "%s(", get_name(func));
1575 write_args(header, type_function_get_args(func->declspec.type), NULL, 0, 0, NAME_DEFAULT);
1576 fprintf(header, ") = 0;\n");
1577 indentation -= 1;
1579 write_line(header, -1, "};");
1581 write_namespace_end(header, type->namespace);
1582 write_line(header, 0, "extern \"C\" {");
1583 write_line(header, 0, "#endif\n");
1586 static void write_forward(FILE *header, type_t *iface)
1588 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1589 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1590 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1591 fprintf(header, "#ifdef __cplusplus\n");
1592 if (iface->namespace && !is_global_namespace(iface->namespace))
1593 fprintf(header, "#define %s %s\n", iface->c_name, iface->qualified_name);
1594 if (!iface->impl_name)
1596 write_namespace_start(header, iface->namespace);
1597 write_line(header, 0, "interface %s;", iface->name);
1598 write_namespace_end(header, iface->namespace);
1600 fprintf(header, "#endif /* __cplusplus */\n");
1601 fprintf(header, "#endif\n\n" );
1604 static char *format_apicontract_macro(const type_t *type)
1606 char *name = format_namespace(type->namespace, "", "_", type->name, NULL);
1607 int i;
1608 for (i = strlen(name); i > 0; --i) name[i - 1] = toupper(name[i - 1]);
1609 return name;
1612 static void write_apicontract_guard_start(FILE *header, const expr_t *expr)
1614 const type_t *type;
1615 char *name;
1616 int ver;
1617 if (!winrt_mode) return;
1618 type = expr->u.tref.type;
1619 ver = expr->ref->u.lval;
1620 name = format_apicontract_macro(type);
1621 fprintf(header, "#if %s_VERSION >= %#x\n", name, ver);
1622 free(name);
1625 static void write_apicontract_guard_end(FILE *header, const expr_t *expr)
1627 const type_t *type;
1628 char *name;
1629 int ver;
1630 if (!winrt_mode) return;
1631 type = expr->u.tref.type;
1632 ver = expr->ref->u.lval;
1633 name = format_apicontract_macro(type);
1634 fprintf(header, "#endif /* %s_VERSION >= %#x */\n", name, ver);
1635 free(name);
1638 static void write_com_interface_start(FILE *header, const type_t *iface)
1640 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1641 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1642 fprintf(header, "/*****************************************************************************\n");
1643 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1644 fprintf(header, " */\n");
1645 if (contract) write_apicontract_guard_start(header, contract);
1646 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1647 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1650 static void write_widl_using_method_macros(FILE *header, const type_t *iface, const type_t *top_iface)
1652 const statement_t *stmt;
1653 const char *name = top_iface->short_name ? top_iface->short_name : top_iface->name;
1655 if (type_iface_get_inherit(iface)) write_widl_using_method_macros(header, type_iface_get_inherit(iface), top_iface);
1657 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1659 const var_t *func = stmt->u.var;
1660 const char *func_name;
1662 if (is_override_method(iface, top_iface, func)) continue;
1663 if (is_callas(func->attrs)) continue;
1665 func_name = get_name(func);
1666 fprintf(header, "#define %s_%s %s_%s\n", name, func_name, top_iface->c_name, func_name);
1670 static void write_widl_using_macros(FILE *header, type_t *iface)
1672 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1673 const char *name = iface->short_name ? iface->short_name : iface->name;
1674 char *macro;
1676 if (!strcmp(iface->name, iface->c_name)) return;
1678 macro = format_namespace(iface->namespace, "WIDL_using_", "_", NULL, NULL);
1679 fprintf(header, "#ifdef %s\n", macro);
1681 if (uuid) fprintf(header, "#define IID_%s IID_%s\n", name, iface->c_name);
1682 if (iface->type_type == TYPE_INTERFACE) fprintf(header, "#define %sVtbl %sVtbl\n", name, iface->c_name);
1683 fprintf(header, "#define %s %s\n", name, iface->c_name);
1685 if (iface->type_type == TYPE_INTERFACE) write_widl_using_method_macros(header, iface, iface);
1687 fprintf(header, "#endif /* %s */\n", macro);
1688 free(macro);
1691 static void write_com_interface_end(FILE *header, type_t *iface)
1693 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1694 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1695 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1696 type_t *type;
1698 if (uuid)
1699 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1701 /* C++ interface */
1702 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1703 if (!is_global_namespace(iface->namespace)) {
1704 write_line(header, 0, "} /* extern \"C\" */");
1705 write_namespace_start(header, iface->namespace);
1707 if (uuid) {
1708 if (strchr(iface->name, '<')) write_line(header, 0, "template<>");
1709 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1710 indent(header, 0);
1711 }else {
1712 indent(header, 0);
1713 if (strchr(iface->name, '<')) fprintf(header, "template<> struct ");
1714 else fprintf(header, "interface ");
1716 if (iface->impl_name)
1718 fprintf(header, "%s : %s\n", iface->name, iface->impl_name);
1719 write_line(header, 1, "{");
1721 else if (type_iface_get_inherit(iface))
1723 fprintf(header, "%s : public %s\n", iface->name,
1724 type_iface_get_inherit(iface)->name);
1725 write_line(header, 1, "{");
1727 else
1729 fprintf(header, "%s\n", iface->name);
1730 write_line(header, 1, "{\n");
1731 write_line(header, 0, "BEGIN_INTERFACE\n");
1733 /* dispinterfaces don't have real functions, so don't write C++ functions for
1734 * them */
1735 if (!dispinterface && !iface->impl_name)
1736 write_cpp_method_def(header, iface);
1737 if (!type_iface_get_inherit(iface) && !iface->impl_name)
1738 write_line(header, 0, "END_INTERFACE\n");
1739 write_line(header, -1, "};");
1740 if (!is_global_namespace(iface->namespace)) {
1741 write_namespace_end(header, iface->namespace);
1742 write_line(header, 0, "extern \"C\" {");
1744 if (uuid)
1745 write_uuid_decl(header, iface, uuid);
1746 fprintf(header, "#else\n");
1747 /* C interface */
1748 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1749 write_line(header, 0, "BEGIN_INTERFACE\n");
1750 if (dispinterface)
1751 write_c_disp_method_def(header, iface);
1752 else
1753 write_c_method_def(header, iface);
1754 write_line(header, 0, "END_INTERFACE");
1755 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1756 fprintf(header, "interface %s {\n", iface->c_name);
1757 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1758 fprintf(header, "};\n\n");
1759 fprintf(header, "#ifdef COBJMACROS\n");
1760 /* dispinterfaces don't have real functions, so don't write macros for them,
1761 * only for the interface this interface inherits from, i.e. IDispatch */
1762 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1763 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1764 write_method_macro(header, type, type, iface->c_name);
1765 fprintf(header, "#else\n");
1766 write_inline_wrappers(header, type, type, iface->c_name);
1767 fprintf(header, "#endif\n");
1768 if (winrt_mode) write_widl_using_macros(header, iface);
1769 fprintf(header, "#endif\n");
1770 fprintf(header, "\n");
1771 fprintf(header, "#endif\n");
1772 fprintf(header, "\n");
1773 /* dispinterfaces don't have real functions, so don't write prototypes for
1774 * them */
1775 if (!dispinterface && !winrt_mode)
1777 write_method_proto(header, iface);
1778 write_locals(header, iface, FALSE);
1779 fprintf(header, "\n");
1781 fprintf(header, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface->c_name, dispinterface ? "DISP" : "");
1782 if (contract) write_apicontract_guard_end(header, contract);
1783 fprintf(header, "\n");
1786 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1788 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1789 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1790 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1792 fprintf(header, "/*****************************************************************************\n");
1793 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1794 fprintf(header, " */\n");
1795 if (contract) write_apicontract_guard_start(header, contract);
1796 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1797 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1798 if (var)
1800 fprintf(header, "extern ");
1801 write_type_decl( header, &var->declspec, var->name );
1802 fprintf(header, ";\n");
1804 if (old_names)
1806 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1807 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1809 else
1811 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1812 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1813 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1814 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1818 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1820 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1821 fprintf(header, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface->name);
1822 if (contract) write_apicontract_guard_end(header, contract);
1823 fprintf(header, "\n");
1826 static void write_coclass(FILE *header, type_t *cocl)
1828 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1830 fprintf(header, "/*****************************************************************************\n");
1831 fprintf(header, " * %s coclass\n", cocl->name);
1832 fprintf(header, " */\n\n");
1833 if (uuid)
1834 write_guid(header, "CLSID", cocl->name, uuid);
1835 fprintf(header, "\n#ifdef __cplusplus\n");
1836 if (uuid)
1838 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1839 write_uuid_decl(header, cocl, uuid);
1841 else
1843 fprintf(header, "class %s;\n", cocl->name);
1845 fprintf(header, "#endif\n");
1846 fprintf(header, "\n");
1849 static void write_coclass_forward(FILE *header, type_t *cocl)
1851 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1852 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1853 fprintf(header, "#ifdef __cplusplus\n");
1854 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1855 fprintf(header, "#else\n");
1856 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1857 fprintf(header, "#endif /* defined __cplusplus */\n");
1858 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1861 static void write_apicontract(FILE *header, type_t *apicontract)
1863 char *name = format_apicontract_macro(apicontract);
1864 fprintf(header, "#if !defined(%s_VERSION)\n", name);
1865 fprintf(header, "#define %s_VERSION %#x\n", name, get_attrv(apicontract->attrs, ATTR_CONTRACTVERSION));
1866 fprintf(header, "#endif // defined(%s_VERSION)\n\n", name);
1867 free(name);
1870 static void write_runtimeclass(FILE *header, type_t *runtimeclass)
1872 expr_t *contract = get_attrp(runtimeclass->attrs, ATTR_CONTRACT);
1873 char *name, *c_name;
1874 size_t i, len;
1875 name = format_namespace(runtimeclass->namespace, "", ".", runtimeclass->name, NULL);
1876 c_name = format_namespace(runtimeclass->namespace, "", "_", runtimeclass->name, NULL);
1877 fprintf(header, "/*\n");
1878 fprintf(header, " * Class %s\n", name);
1879 fprintf(header, " */\n");
1880 if (contract) write_apicontract_guard_start(header, contract);
1881 fprintf(header, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name);
1882 fprintf(header, "#define RUNTIMECLASS_%s_DEFINED\n", c_name);
1883 fprintf(header, "#if !defined(_MSC_VER) && !defined(__MINGW32__)\n");
1884 fprintf(header, "static const WCHAR RuntimeClass_%s[] = {", c_name);
1885 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1886 fprintf(header, "0};\n");
1887 fprintf(header, "#elif defined(__GNUC__) && !defined(__cplusplus)\n");
1888 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1889 fprintf(header, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name, name);
1890 fprintf(header, "#else\n");
1891 fprintf(header, "extern const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = {", c_name);
1892 for (i = 0, len = strlen(name); i < len; ++i) fprintf(header, "'%c',", name[i]);
1893 fprintf(header, "0};\n");
1894 fprintf(header, "#endif\n");
1895 fprintf(header, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name);
1896 free(c_name);
1897 free(name);
1898 if (contract) write_apicontract_guard_end(header, contract);
1899 fprintf(header, "\n");
1902 static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
1904 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1905 fprintf(header, "#define __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1906 fprintf(header, "#ifdef __cplusplus\n");
1907 write_namespace_start(header, runtimeclass->namespace);
1908 write_line(header, 0, "class %s;", runtimeclass->name);
1909 write_namespace_end(header, runtimeclass->namespace);
1910 fprintf(header, "#else\n");
1911 fprintf(header, "typedef struct %s %s;\n", runtimeclass->c_name, runtimeclass->c_name);
1912 fprintf(header, "#endif /* defined __cplusplus */\n");
1913 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass->c_name);
1916 static void write_import(FILE *header, const char *fname)
1918 char *hname, *p;
1920 hname = dup_basename(fname, ".idl");
1921 p = hname + strlen(hname) - 2;
1922 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1924 fprintf(header, "#include <%s>\n", hname);
1925 free(hname);
1928 static void write_imports(FILE *header, const statement_list_t *stmts)
1930 const statement_t *stmt;
1931 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1933 switch (stmt->type)
1935 case STMT_TYPE:
1936 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1937 write_imports(header, type_iface_get_stmts(stmt->u.type));
1938 break;
1939 case STMT_TYPEREF:
1940 case STMT_IMPORTLIB:
1941 /* not included in header */
1942 break;
1943 case STMT_IMPORT:
1944 write_import(header, stmt->u.str);
1945 break;
1946 case STMT_TYPEDEF:
1947 case STMT_MODULE:
1948 case STMT_CPPQUOTE:
1949 case STMT_PRAGMA:
1950 case STMT_DECLARATION:
1951 /* not processed here */
1952 break;
1953 case STMT_LIBRARY:
1954 write_imports(header, stmt->u.lib->stmts);
1955 break;
1960 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1962 const statement_t *stmt;
1963 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1965 switch (stmt->type)
1967 case STMT_TYPE:
1968 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
1970 type_t *iface = stmt->u.type;
1971 if (type_get_type(iface) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
1972 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1974 write_forward(header, iface);
1975 if (type_iface_get_async_iface(iface))
1976 write_forward(header, type_iface_get_async_iface(iface));
1979 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1980 write_coclass_forward(header, stmt->u.type);
1981 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
1982 write_runtimeclass_forward(header, stmt->u.type);
1983 else if (type_get_type(stmt->u.type) == TYPE_PARAMETERIZED_TYPE)
1984 write_parameterized_type_forward(header, stmt->u.type);
1985 break;
1986 case STMT_TYPEREF:
1987 case STMT_IMPORTLIB:
1988 /* not included in header */
1989 break;
1990 case STMT_IMPORT:
1991 case STMT_TYPEDEF:
1992 case STMT_MODULE:
1993 case STMT_CPPQUOTE:
1994 case STMT_PRAGMA:
1995 case STMT_DECLARATION:
1996 /* not processed here */
1997 break;
1998 case STMT_LIBRARY:
1999 write_forward_decls(header, stmt->u.lib->stmts);
2000 break;
2005 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
2007 const statement_t *stmt;
2008 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2010 switch (stmt->type)
2012 case STMT_TYPE:
2013 if (type_get_type(stmt->u.type) == TYPE_INTERFACE || type_get_type(stmt->u.type) == TYPE_DELEGATE)
2015 type_t *iface = stmt->u.type, *async_iface;
2016 if (type_get_type(stmt->u.type) == TYPE_DELEGATE) iface = type_delegate_get_iface(iface);
2017 async_iface = type_iface_get_async_iface(iface);
2018 if (is_object(iface)) is_object_interface++;
2019 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
2021 write_com_interface_start(header, iface);
2022 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
2023 write_com_interface_end(header, iface);
2024 if (async_iface)
2026 write_com_interface_start(header, async_iface);
2027 write_com_interface_end(header, async_iface);
2030 else
2032 write_rpc_interface_start(header, iface);
2033 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
2034 write_rpc_interface_end(header, iface);
2036 if (is_object(iface)) is_object_interface--;
2038 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
2039 write_coclass(header, stmt->u.type);
2040 else if (type_get_type(stmt->u.type) == TYPE_APICONTRACT)
2041 write_apicontract(header, stmt->u.type);
2042 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
2043 write_runtimeclass(header, stmt->u.type);
2044 else if (type_get_type(stmt->u.type) != TYPE_PARAMETERIZED_TYPE)
2045 write_type_definition(header, stmt->u.type, stmt->declonly);
2046 else
2048 is_object_interface++;
2049 write_parameterized_implementation(header, stmt->u.type, stmt->declonly);
2050 is_object_interface--;
2052 break;
2053 case STMT_TYPEREF:
2054 /* FIXME: shouldn't write out forward declarations for undefined
2055 * interfaces but a number of our IDL files depend on this */
2056 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
2057 write_forward(header, stmt->u.type);
2058 break;
2059 case STMT_IMPORTLIB:
2060 case STMT_MODULE:
2061 case STMT_PRAGMA:
2062 /* not included in header */
2063 break;
2064 case STMT_IMPORT:
2065 /* not processed here */
2066 break;
2067 case STMT_TYPEDEF:
2069 typeref_t *ref;
2070 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
2071 write_typedef(header, ref->type, stmt->declonly);
2072 break;
2074 case STMT_LIBRARY:
2075 fprintf(header, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2076 fprintf(header, "#define __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
2077 write_library(header, stmt->u.lib);
2078 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
2079 fprintf(header, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt->u.lib->name);
2080 break;
2081 case STMT_CPPQUOTE:
2082 fprintf(header, "%s\n", stmt->u.str);
2083 break;
2084 case STMT_DECLARATION:
2085 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
2087 if (!ignore_funcs)
2089 int prefixes_differ = strcmp(prefix_client, prefix_server);
2091 if (prefixes_differ)
2093 fprintf(header, "/* client prototype */\n");
2094 write_function_proto(header, iface, stmt->u.var, prefix_client);
2095 fprintf(header, "/* server prototype */\n");
2097 write_function_proto(header, iface, stmt->u.var, prefix_server);
2100 else
2101 write_declaration(header, stmt->u.var);
2102 break;
2107 void write_header(const statement_list_t *stmts)
2109 FILE *header;
2111 if (!do_header) return;
2113 if(!(header = fopen(header_name, "w"))) {
2114 error("Could not open %s for output\n", header_name);
2115 return;
2117 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
2119 fprintf(header, "#ifdef _WIN32\n");
2120 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
2121 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
2122 fprintf(header, "#endif\n");
2123 fprintf(header, "#include <rpc.h>\n" );
2124 fprintf(header, "#include <rpcndr.h>\n" );
2125 if (!for_each_serializable(stmts, NULL, serializable_exists))
2126 fprintf(header, "#include <midles.h>\n" );
2127 fprintf(header, "#endif\n\n");
2129 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
2130 fprintf(header, "#include <windows.h>\n");
2131 fprintf(header, "#include <ole2.h>\n");
2132 fprintf(header, "#endif\n\n");
2134 fprintf(header, "#ifndef __%s__\n", header_token);
2135 fprintf(header, "#define __%s__\n\n", header_token);
2137 fprintf(header, "/* Forward declarations */\n\n");
2138 write_forward_decls(header, stmts);
2140 fprintf(header, "/* Headers for imported files */\n\n");
2141 write_imports(header, stmts);
2142 fprintf(header, "\n");
2143 start_cplusplus_guard(header);
2145 write_header_stmts(header, stmts, NULL, FALSE);
2147 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
2148 fprintf(header, "\n");
2149 for_each_serializable(stmts, header, write_serialize_function_decl);
2150 write_user_types(header);
2151 write_generic_handle_routines(header);
2152 write_context_handle_rundowns(header);
2153 fprintf(header, "\n");
2154 fprintf(header, "/* End additional prototypes */\n");
2155 fprintf(header, "\n");
2157 end_cplusplus_guard(header);
2158 fprintf(header, "#endif /* __%s__ */\n", header_token);
2160 fclose(header);