msxml3: Fold in reset_output_buffer.
[wine/zf.git] / tools / widl / header.c
blob63389440c94434a913589664c62d771aae6b0c90
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 indent(FILE *h, int delta)
53 int c;
54 if (delta < 0) indentation += delta;
55 for (c=0; c<indentation; c++) fprintf(h, " ");
56 if (delta > 0) indentation += delta;
59 static void write_line(FILE *f, int delta, const char *fmt, ...)
61 va_list ap;
62 indent(f, delta);
63 va_start(ap, fmt);
64 vfprintf(f, fmt, ap);
65 va_end(ap);
66 fprintf(f, "\n");
69 int is_ptrchain_attr(const var_t *var, enum attr_type t)
71 if (is_attr(var->attrs, t))
72 return 1;
73 else
75 type_t *type = var->declspec.type;
76 for (;;)
78 if (is_attr(type->attrs, t))
79 return 1;
80 else if (type_is_alias(type))
81 type = type_alias_get_aliasee_type(type);
82 else if (is_ptr(type))
83 type = type_pointer_get_ref_type(type);
84 else return 0;
89 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
91 const type_t *t = type;
92 for (;;)
94 if (is_attr(t->attrs, attr))
95 return 1;
96 else if (type_is_alias(t))
97 t = type_alias_get_aliasee_type(t);
98 else return 0;
102 int is_attr(const attr_list_t *list, enum attr_type t)
104 const attr_t *attr;
105 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
106 if (attr->type == t) return 1;
107 return 0;
110 void *get_attrp(const attr_list_t *list, enum attr_type t)
112 const attr_t *attr;
113 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
114 if (attr->type == t) return attr->u.pval;
115 return NULL;
118 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
120 const attr_t *attr;
121 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
122 if (attr->type == t) return attr->u.ival;
123 return 0;
126 static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
128 if (!uuid) return;
129 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
130 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
131 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
132 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
133 uuid->Data4[6], uuid->Data4[7]);
136 static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
138 char *name = format_namespace(type->namespace, "", "::", type->name, use_abi_namespace ? "ABI" : NULL);
139 fprintf(f, "#ifdef __CRT_UUID_DECL\n");
140 fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
141 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
142 name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
143 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
144 uuid->Data4[7]);
145 fprintf(f, "#endif\n");
146 free(name);
149 static const char *uuid_string(const UUID *uuid)
151 static char buf[37];
153 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
154 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
155 uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
157 return buf;
160 static void write_namespace_start(FILE *header, struct namespace *namespace)
162 if(is_global_namespace(namespace)) {
163 if(use_abi_namespace)
164 write_line(header, 1, "namespace ABI {");
165 return;
168 write_namespace_start(header, namespace->parent);
169 write_line(header, 1, "namespace %s {", namespace->name);
172 static void write_namespace_end(FILE *header, struct namespace *namespace)
174 if(is_global_namespace(namespace)) {
175 if(use_abi_namespace)
176 write_line(header, -1, "}", namespace->name);
177 return;
180 write_line(header, -1, "}", namespace->name);
181 write_namespace_end(header, namespace->parent);
184 const char *get_name(const var_t *v)
186 static char *buffer;
187 free( buffer );
188 if (is_attr( v->attrs, ATTR_EVENTADD ))
189 return buffer = strmake( "add_%s", v->name );
190 if (is_attr( v->attrs, ATTR_EVENTREMOVE ))
191 return buffer = strmake( "remove_%s", v->name );
192 if (is_attr( v->attrs, ATTR_PROPGET ))
193 return buffer = strmake( "get_%s", v->name );
194 if (is_attr( v->attrs, ATTR_PROPPUT ))
195 return buffer = strmake( "put_%s", v->name );
196 if (is_attr( v->attrs, ATTR_PROPPUTREF ))
197 return buffer = strmake( "putref_%s", v->name );
198 buffer = NULL;
199 return v->name;
202 static void write_fields(FILE *h, var_list_t *fields, enum name_type name_type)
204 unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
205 const char *name;
206 char buf[32];
207 var_t *v;
209 if (!fields) return;
211 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
212 if (!v || !v->declspec.type) continue;
214 switch(type_get_type_detect_alias(v->declspec.type)) {
215 case TYPE_STRUCT:
216 case TYPE_ENCAPSULATED_UNION:
217 nameless_struct_cnt++;
218 break;
219 case TYPE_UNION:
220 nameless_union_cnt++;
221 break;
222 default:
227 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
228 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
229 if (!v || !v->declspec.type) continue;
230 if (contract) write_apicontract_guard_start(h, contract);
232 indent(h, 0);
233 name = v->name;
235 switch(type_get_type_detect_alias(v->declspec.type)) {
236 case TYPE_STRUCT:
237 case TYPE_ENCAPSULATED_UNION:
238 if(!v->name) {
239 fprintf(h, "__C89_NAMELESS ");
240 if(nameless_struct_cnt == 1) {
241 name = "__C89_NAMELESSSTRUCTNAME";
242 }else if(nameless_struct_i < 5 /* # of supporting macros */) {
243 sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
244 name = buf;
247 break;
248 case TYPE_UNION:
249 if(!v->name) {
250 fprintf(h, "__C89_NAMELESS ");
251 if(nameless_union_cnt == 1) {
252 name = "__C89_NAMELESSUNIONNAME";
253 }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
254 sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
255 name = buf;
258 break;
259 default:
262 write_type_v(h, &v->declspec, TRUE, v->declonly, name, name_type);
263 fprintf(h, ";\n");
264 if (contract) write_apicontract_guard_end(h, contract);
268 static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
270 var_t *v;
271 if (!enums) return;
272 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
274 expr_t *contract = get_attrp(v->attrs, ATTR_CONTRACT);
275 if (contract) write_apicontract_guard_start(h, contract);
276 if (v->name) {
277 indent(h, 0);
278 if(!enum_name)
279 fprintf(h, "%s", get_name(v));
280 else
281 fprintf(h, "%s_%s", enum_name, get_name(v));
282 if (v->eval) {
283 fprintf(h, " = ");
284 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
287 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
288 else fprintf(h, "\n");
289 if (contract) write_apicontract_guard_end(h, contract);
293 int needs_space_after(type_t *t)
295 return (type_is_alias(t) ||
296 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
299 static int decl_needs_parens(const type_t *t)
301 if (type_is_alias(t))
302 return FALSE;
303 if (is_array(t) && !type_array_is_decl_as_ptr(t))
304 return TRUE;
305 return is_func(t);
308 static void write_pointer_left(FILE *h, type_t *ref)
310 if (needs_space_after(ref))
311 fprintf(h, " ");
312 if (decl_needs_parens(ref))
313 fprintf(h, "(");
314 if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
316 const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
317 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
318 if (callconv) fprintf(h, "%s ", callconv);
320 fprintf(h, "*");
323 void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
325 type_t *t = ds->type;
326 const char *name;
328 if (!h) return;
330 name = type_get_name(t, name_type);
332 if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
333 fprintf(h, "inline ");
335 if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
336 fprintf(h, "const ");
338 if (!winrt_mode && type_is_alias(t)) fprintf(h, "%s", t->name);
339 else {
340 switch (type_get_type_detect_alias(t)) {
341 case TYPE_ENUM:
342 if (!declonly && !t->written) {
343 assert(t->defined);
344 if (name) fprintf(h, "enum %s {\n", name);
345 else fprintf(h, "enum {\n");
346 t->written = TRUE;
347 indentation++;
348 write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
349 indent(h, -1);
350 fprintf(h, "}");
352 else fprintf(h, "enum %s", name ? name : "");
353 break;
354 case TYPE_STRUCT:
355 case TYPE_ENCAPSULATED_UNION:
356 if (!declonly && !t->written) {
357 assert(t->defined);
358 if (name) fprintf(h, "struct %s {\n", name);
359 else fprintf(h, "struct {\n");
360 t->written = TRUE;
361 indentation++;
362 if (type_get_type(t) != TYPE_STRUCT)
363 write_fields(h, type_encapsulated_union_get_fields(t), name_type);
364 else
365 write_fields(h, type_struct_get_fields(t), name_type);
366 indent(h, -1);
367 fprintf(h, "}");
369 else fprintf(h, "struct %s", name ? name : "");
370 break;
371 case TYPE_UNION:
372 if (!declonly && !t->written) {
373 assert(t->defined);
374 if (t->name) fprintf(h, "union %s {\n", t->name);
375 else fprintf(h, "union {\n");
376 t->written = TRUE;
377 indentation++;
378 write_fields(h, type_union_get_cases(t), name_type);
379 indent(h, -1);
380 fprintf(h, "}");
382 else fprintf(h, "union %s", t->name ? t->name : "");
383 break;
384 case TYPE_POINTER:
386 write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
387 write_pointer_left(h, type_pointer_get_ref_type(t));
388 if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
389 break;
391 case TYPE_ARRAY:
392 if (t->name && type_array_is_decl_as_ptr(t))
393 fprintf(h, "%s", t->name);
394 else
396 write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
397 if (type_array_is_decl_as_ptr(t))
398 write_pointer_left(h, type_array_get_element_type(t));
400 break;
401 case TYPE_FUNCTION:
403 write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
405 /* A pointer to a function has to write the calling convention inside
406 * the parentheses. There's no way to handle that here, so we have to
407 * use an extra parameter to tell us whether to write the calling
408 * convention or not. */
409 if (write_callconv)
411 const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
412 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
413 if (callconv) fprintf(h, " %s ", callconv);
415 break;
417 case TYPE_BASIC:
418 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
419 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
420 type_basic_get_type(t) != TYPE_BASIC_LONG &&
421 type_basic_get_type(t) != TYPE_BASIC_HYPER)
423 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
424 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
426 switch (type_basic_get_type(t))
428 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
429 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
430 case TYPE_BASIC_INT: fprintf(h, "int"); break;
431 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
432 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
433 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
434 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
435 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
436 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
437 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
438 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
439 case TYPE_BASIC_INT32:
440 if (type_basic_get_sign(t) > 0)
441 fprintf(h, "UINT32");
442 else
443 fprintf(h, "INT32");
444 break;
445 case TYPE_BASIC_LONG:
446 if (type_basic_get_sign(t) > 0)
447 fprintf(h, "ULONG");
448 else
449 fprintf(h, "LONG");
450 break;
451 case TYPE_BASIC_INT64:
452 if (type_basic_get_sign(t) > 0)
453 fprintf(h, "UINT64");
454 else
455 fprintf(h, "INT64");
456 break;
457 case TYPE_BASIC_HYPER:
458 if (type_basic_get_sign(t) > 0)
459 fprintf(h, "MIDL_uhyper");
460 else
461 fprintf(h, "hyper");
462 break;
464 break;
465 case TYPE_INTERFACE:
466 case TYPE_MODULE:
467 case TYPE_COCLASS:
468 fprintf(h, "%s", name);
469 break;
470 case TYPE_RUNTIMECLASS:
471 fprintf(h, "%s", type_get_name(type_runtimeclass_get_default_iface(t), name_type));
472 break;
473 case TYPE_VOID:
474 fprintf(h, "void");
475 break;
476 case TYPE_BITFIELD:
478 const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
479 write_type_left(h, &ds, name_type, declonly, TRUE);
480 break;
482 case TYPE_ALIAS:
484 const decl_spec_t *ds = type_alias_get_aliasee(t);
485 int in_namespace = ds && ds->type && ds->type->namespace && !is_global_namespace(ds->type->namespace);
486 if (!in_namespace) fprintf(h, "%s", t->name);
487 else write_type_left(h, ds, name_type, declonly, write_callconv);
488 break;
490 case TYPE_APICONTRACT:
491 /* shouldn't be here */
492 assert(0);
493 break;
498 void write_type_right(FILE *h, type_t *t, int is_field)
500 if (!h) return;
501 if (type_is_alias(t)) return;
503 switch (type_get_type(t))
505 case TYPE_ARRAY:
507 type_t *elem = type_array_get_element_type(t);
508 if (type_array_is_decl_as_ptr(t))
510 if (decl_needs_parens(elem))
511 fprintf(h, ")");
513 else
515 if (is_conformant_array(t))
516 fprintf(h, "[%s]", is_field ? "1" : "");
517 else
518 fprintf(h, "[%u]", type_array_get_dim(t));
520 write_type_right(h, elem, FALSE);
521 break;
523 case TYPE_FUNCTION:
525 const var_list_t *args = type_function_get_args(t);
526 fputc('(', h);
527 if (args) write_args(h, args, NULL, 0, FALSE, NAME_DEFAULT);
528 else
529 fprintf(h, "void");
530 fputc(')', h);
531 write_type_right(h, type_function_get_rettype(t), FALSE);
532 break;
534 case TYPE_POINTER:
536 type_t *ref = type_pointer_get_ref_type(t);
537 if (decl_needs_parens(ref))
538 fprintf(h, ")");
539 write_type_right(h, ref, FALSE);
540 break;
542 case TYPE_BITFIELD:
543 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
544 break;
545 case TYPE_VOID:
546 case TYPE_BASIC:
547 case TYPE_ENUM:
548 case TYPE_STRUCT:
549 case TYPE_ENCAPSULATED_UNION:
550 case TYPE_UNION:
551 case TYPE_ALIAS:
552 case TYPE_MODULE:
553 case TYPE_COCLASS:
554 case TYPE_INTERFACE:
555 case TYPE_RUNTIMECLASS:
556 break;
557 case TYPE_APICONTRACT:
558 /* not supposed to be here */
559 assert(0);
560 break;
564 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)
566 type_t *t = ds->type;
568 if (!h) return;
570 if (t) write_type_left(h, ds, name_type, declonly, TRUE);
572 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
574 if (t)
575 write_type_right(h, t, is_field);
578 static void write_type_definition(FILE *f, type_t *t, int declonly)
580 int in_namespace = t->namespace && !is_global_namespace(t->namespace);
581 int save_written = t->written;
582 decl_spec_t ds = {.type = t};
583 expr_t *contract = get_attrp(t->attrs, ATTR_CONTRACT);
585 if (contract) write_apicontract_guard_start(f, contract);
586 if(in_namespace) {
587 fprintf(f, "#ifdef __cplusplus\n");
588 fprintf(f, "} /* extern \"C\" */\n");
589 write_namespace_start(f, t->namespace);
591 indent(f, 0);
592 write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
593 fprintf(f, ";\n");
594 if(in_namespace) {
595 t->written = save_written;
596 write_namespace_end(f, t->namespace);
597 fprintf(f, "extern \"C\" {\n");
598 fprintf(f, "#else\n");
599 write_type_left(f, &ds, NAME_C, declonly, TRUE);
600 fprintf(f, ";\n");
601 fprintf(f, "#endif\n\n");
603 if (contract) write_apicontract_guard_end(f, contract);
606 void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
608 write_type_v(f, t, FALSE, TRUE, name, NAME_DEFAULT);
611 void write_type_decl_left(FILE *f, const decl_spec_t *ds)
613 write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
616 static int user_type_registered(const char *name)
618 user_type_t *ut;
619 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
620 if (!strcmp(name, ut->name))
621 return 1;
622 return 0;
625 static int context_handle_registered(const char *name)
627 context_handle_t *ch;
628 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
629 if (!strcmp(name, ch->name))
630 return 1;
631 return 0;
634 static int generic_handle_registered(const char *name)
636 generic_handle_t *gh;
637 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
638 if (!strcmp(name, gh->name))
639 return 1;
640 return 0;
643 unsigned int get_context_handle_offset( const type_t *type )
645 context_handle_t *ch;
646 unsigned int index = 0;
648 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
650 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
651 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
652 else error( "internal error: %s is not a context handle\n", type->name );
654 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
656 if (!strcmp( type->name, ch->name )) return index;
657 index++;
659 error( "internal error: %s is not registered as a context handle\n", type->name );
660 return index;
663 unsigned int get_generic_handle_offset( const type_t *type )
665 generic_handle_t *gh;
666 unsigned int index = 0;
668 while (!is_attr( type->attrs, ATTR_HANDLE ))
670 if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
671 else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
672 else error( "internal error: %s is not a generic handle\n", type->name );
674 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
676 if (!strcmp( type->name, gh->name )) return index;
677 index++;
679 error( "internal error: %s is not registered as a generic handle\n", type->name );
680 return index;
683 /* check for types which require additional prototypes to be generated in the
684 * header */
685 void check_for_additional_prototype_types(type_t *type)
687 if (!type) return;
688 for (;;) {
689 const char *name = type->name;
690 if (type->user_types_registered) break;
691 type->user_types_registered = 1;
692 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
693 if (!context_handle_registered(name))
695 context_handle_t *ch = xmalloc(sizeof(*ch));
696 ch->name = xstrdup(name);
697 list_add_tail(&context_handle_list, &ch->entry);
699 /* don't carry on parsing fields within this type */
700 break;
702 if ((type_get_type(type) != TYPE_BASIC ||
703 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
704 is_attr(type->attrs, ATTR_HANDLE)) {
705 if (!generic_handle_registered(name))
707 generic_handle_t *gh = xmalloc(sizeof(*gh));
708 gh->name = xstrdup(name);
709 list_add_tail(&generic_handle_list, &gh->entry);
711 /* don't carry on parsing fields within this type */
712 break;
714 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
715 if (!user_type_registered(name))
717 user_type_t *ut = xmalloc(sizeof *ut);
718 ut->name = xstrdup(name);
719 list_add_tail(&user_type_list, &ut->entry);
721 /* don't carry on parsing fields within this type as we are already
722 * using a wire marshaled type */
723 break;
725 else if (type_is_complete(type))
727 var_list_t *vars;
728 const var_t *v;
729 switch (type_get_type_detect_alias(type))
731 case TYPE_ENUM:
732 vars = type_enum_get_values(type);
733 break;
734 case TYPE_STRUCT:
735 vars = type_struct_get_fields(type);
736 break;
737 case TYPE_UNION:
738 vars = type_union_get_cases(type);
739 break;
740 default:
741 vars = NULL;
742 break;
744 if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
745 check_for_additional_prototype_types(v->declspec.type);
748 if (type_is_alias(type))
749 type = type_alias_get_aliasee_type(type);
750 else if (is_ptr(type))
751 type = type_pointer_get_ref_type(type);
752 else if (is_array(type))
753 type = type_array_get_element_type(type);
754 else
755 break;
759 static int write_serialize_function_decl(FILE *header, const type_t *type)
761 write_serialize_functions(header, type, NULL);
762 return 1;
765 static int serializable_exists(FILE *header, const type_t *type)
767 return 0;
770 static int for_each_serializable(const statement_list_t *stmts, FILE *header,
771 int (*proc)(FILE*, const type_t*))
773 statement_t *stmt, *iface_stmt;
774 statement_list_t *iface_stmts;
775 const type_list_t *type_entry;
777 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
779 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
780 continue;
782 iface_stmts = type_iface_get_stmts(stmt->u.type);
783 if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
785 if (iface_stmt->type != STMT_TYPEDEF) continue;
786 for (type_entry = iface_stmt->u.type_list; type_entry; type_entry = type_entry->next)
788 if (!is_attr(type_entry->type->attrs, ATTR_ENCODE)
789 && !is_attr(type_entry->type->attrs, ATTR_DECODE))
790 continue;
791 if (!proc(header, type_entry->type))
792 return 0;
797 return 1;
800 static void write_user_types(FILE *header)
802 user_type_t *ut;
803 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
805 const char *name = ut->name;
806 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
807 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
808 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
809 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
813 static void write_context_handle_rundowns(FILE *header)
815 context_handle_t *ch;
816 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
818 const char *name = ch->name;
819 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
823 static void write_generic_handle_routines(FILE *header)
825 generic_handle_t *gh;
826 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
828 const char *name = gh->name;
829 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
830 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
834 static void write_typedef(FILE *header, type_t *type, int declonly)
836 type_t *t = type_alias_get_aliasee_type(type);
837 if (winrt_mode && t->namespace && !is_global_namespace(t->namespace)) return;
838 fprintf(header, "typedef ");
839 write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name, NAME_DEFAULT);
840 fprintf(header, ";\n");
843 int is_const_decl(const var_t *var)
845 const decl_spec_t *t;
846 /* strangely, MIDL accepts a const attribute on any pointer in the
847 * declaration to mean that data isn't being instantiated. this appears
848 * to be a bug, but there is no benefit to being incompatible with MIDL,
849 * so we'll do the same thing */
850 for (t = &var->declspec; ; )
852 if (t->qualifier & TYPE_QUALIFIER_CONST)
853 return TRUE;
854 else if (is_ptr(t->type))
855 t = type_pointer_get_ref(t->type);
856 else break;
858 return FALSE;
861 static void write_declaration(FILE *header, const var_t *v)
863 if (is_const_decl(v) && v->eval)
865 fprintf(header, "#define %s (", v->name);
866 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
867 fprintf(header, ")\n\n");
869 else
871 switch (v->declspec.stgclass)
873 case STG_NONE:
874 case STG_REGISTER: /* ignored */
875 break;
876 case STG_STATIC:
877 fprintf(header, "static ");
878 break;
879 case STG_EXTERN:
880 fprintf(header, "extern ");
881 break;
883 write_type_v(header, &v->declspec, FALSE, v->declonly, v->name, NAME_DEFAULT);
884 fprintf(header, ";\n\n");
888 static void write_library(FILE *header, const typelib_t *typelib)
890 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
891 fprintf(header, "\n");
892 write_guid(header, "LIBID", typelib->name, uuid);
893 fprintf(header, "\n");
897 const type_t* get_explicit_generic_handle_type(const var_t* var)
899 const type_t *t;
900 for (t = var->declspec.type;
901 is_ptr(t) || type_is_alias(t);
902 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
903 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
904 is_attr(t->attrs, ATTR_HANDLE))
905 return t;
906 return NULL;
909 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
910 unsigned char *explicit_fc, unsigned char *implicit_fc )
912 const var_t *var;
913 const var_list_t *args = type_function_get_args( func->declspec.type );
915 *explicit_fc = *implicit_fc = 0;
916 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
918 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
919 if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
921 *explicit_fc = FC_BIND_PRIMITIVE;
922 return var;
924 if (get_explicit_generic_handle_type( var ))
926 *explicit_fc = FC_BIND_GENERIC;
927 return var;
929 if (is_context_handle( var->declspec.type ))
931 *explicit_fc = FC_BIND_CONTEXT;
932 return var;
936 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
938 if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
939 type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
940 *implicit_fc = FC_BIND_PRIMITIVE;
941 else
942 *implicit_fc = FC_BIND_GENERIC;
943 return var;
946 *implicit_fc = FC_AUTO_HANDLE;
947 return NULL;
950 int has_out_arg_or_return(const var_t *func)
952 const var_t *var;
954 if (!is_void(type_function_get_rettype(func->declspec.type)))
955 return 1;
957 if (!type_function_get_args(func->declspec.type))
958 return 0;
960 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
961 if (is_attr(var->attrs, ATTR_OUT))
962 return 1;
964 return 0;
968 /********** INTERFACES **********/
970 int is_object(const type_t *iface)
972 const attr_t *attr;
973 if (type_is_defined(iface) && type_iface_get_inherit(iface))
974 return 1;
975 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
976 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
977 return 0;
980 int is_local(const attr_list_t *a)
982 return is_attr(a, ATTR_LOCAL);
985 const var_t *is_callas(const attr_list_t *a)
987 return get_attrp(a, ATTR_CALLAS);
990 static int is_inherited_method(const type_t *iface, const var_t *func)
992 while ((iface = type_iface_get_inherit(iface)))
994 const statement_t *stmt;
995 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
997 const var_t *funccmp = stmt->u.var;
999 if (!is_callas(func->attrs))
1001 char inherit_name[256];
1002 /* compare full name including property prefix */
1003 strcpy(inherit_name, get_name(funccmp));
1004 if (!strcmp(inherit_name, get_name(func))) return 1;
1009 return 0;
1012 static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
1014 if (iface == child)
1015 return 0;
1019 const statement_t *stmt;
1020 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
1022 const var_t *funccmp = stmt->u.var;
1024 if (!is_callas(func->attrs))
1026 char inherit_name[256];
1027 /* compare full name including property prefix */
1028 strcpy(inherit_name, get_name(funccmp));
1029 if (!strcmp(inherit_name, get_name(func))) return 1;
1033 while ((child = type_iface_get_inherit(child)) && child != iface);
1035 return 0;
1038 static int is_aggregate_return(const var_t *func)
1040 enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
1041 return type == TYPE_STRUCT || type == TYPE_UNION ||
1042 type == TYPE_COCLASS || type == TYPE_INTERFACE ||
1043 type == TYPE_RUNTIMECLASS;
1046 static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
1048 static char buff[255];
1049 if (is_inherited_method(iface, func))
1050 sprintf(buff, "%s_%s", iface->name, get_name(func));
1051 else
1052 sprintf(buff, "%s", get_name(func));
1053 return buff;
1056 static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
1058 const statement_t *stmt;
1059 int first_iface = 1;
1061 if (type_iface_get_inherit(iface))
1062 write_method_macro(header, type_iface_get_inherit(iface), child, name);
1064 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1066 const var_t *func = stmt->u.var;
1068 if (first_iface)
1070 fprintf(header, "/*** %s methods ***/\n", iface->name);
1071 first_iface = 0;
1074 if (is_override_method(iface, child, func))
1075 continue;
1077 if (!is_callas(func->attrs)) {
1078 const var_t *arg;
1080 fprintf(header, "#define %s_%s(This", name, get_name(func));
1081 if (type_function_get_args(func->declspec.type))
1082 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1083 fprintf(header, ",%s", arg->name);
1084 fprintf(header, ") ");
1086 if (is_aggregate_return(func))
1088 fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
1089 continue;
1092 fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
1093 if (type_function_get_args(func->declspec.type))
1094 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1095 fprintf(header, ",%s", arg->name);
1096 fprintf(header, ")\n");
1101 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent, enum name_type name_type)
1103 const var_t *arg;
1104 int count = 0;
1106 if (do_indent)
1108 indentation++;
1109 indent(h, 0);
1111 if (method == 1) {
1112 fprintf(h, "%s* This", name);
1113 count++;
1115 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
1116 if (count) {
1117 if (do_indent)
1119 fprintf(h, ",\n");
1120 indent(h, 0);
1122 else fprintf(h, ",");
1124 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1125 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1126 * defining UDTs inside of an argument list. */
1127 write_type_v(h, &arg->declspec, FALSE, TRUE, arg->name, name_type);
1128 if (method == 2) {
1129 const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
1130 if (expr) {
1131 const var_t *tail_arg;
1133 /* Output default value only if all following arguments also have default value. */
1134 LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
1135 if(tail_arg == arg) {
1136 expr_t bstr;
1138 /* Fixup the expression type for a BSTR like midl does. */
1139 if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
1141 bstr = *expr;
1142 bstr.type = EXPR_WSTRLIT;
1143 expr = &bstr;
1146 fprintf(h, " = ");
1147 write_expr( h, expr, 0, 1, NULL, NULL, "" );
1148 break;
1150 if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
1151 break;
1155 count++;
1157 if (do_indent) indentation--;
1160 static void write_cpp_method_def(FILE *header, const type_t *iface)
1162 const statement_t *stmt;
1164 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1166 const var_t *func = stmt->u.var;
1167 if (!is_callas(func->attrs)) {
1168 const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
1169 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1170 const var_list_t *args = type_function_get_args(func->declspec.type);
1171 const var_t *arg;
1173 if (!callconv) callconv = "STDMETHODCALLTYPE";
1175 if (is_aggregate_return(func)) {
1176 fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1178 indent(header, 0);
1179 fprintf(header, "virtual ");
1180 write_type_decl_left(header, ret);
1181 fprintf(header, "* %s %s(\n", callconv, get_name(func));
1182 ++indentation;
1183 indent(header, 0);
1184 write_type_decl_left(header, ret);
1185 fprintf(header, " *__ret");
1186 --indentation;
1187 if (args) {
1188 fprintf(header, ",\n");
1189 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1191 fprintf(header, ") = 0;\n");
1193 indent(header, 0);
1194 write_type_decl_left(header, ret);
1195 fprintf(header, " %s %s(\n", callconv, get_name(func));
1196 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1197 fprintf(header, ")\n");
1198 indent(header, 0);
1199 fprintf(header, "{\n");
1200 ++indentation;
1201 indent(header, 0);
1202 write_type_decl_left(header, ret);
1203 fprintf(header, " __ret;\n");
1204 indent(header, 0);
1205 fprintf(header, "return *%s(&__ret", get_name(func));
1206 if (args)
1207 LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
1208 fprintf(header, ", %s", arg->name);
1209 fprintf(header, ");\n");
1210 --indentation;
1211 indent(header, 0);
1212 fprintf(header, "}\n");
1214 fprintf(header, "#else\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 write_args(header, args, iface->name, 2, TRUE, NAME_DEFAULT);
1222 fprintf(header, ") = 0;\n");
1224 if (is_aggregate_return(func))
1225 fprintf(header, "#endif\n");
1226 fprintf(header, "\n");
1231 static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
1233 const statement_t *stmt;
1234 int first_iface = 1;
1236 if (type_iface_get_inherit(iface))
1237 write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
1239 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1241 const var_t *func = stmt->u.var;
1243 if (first_iface)
1245 fprintf(header, "/*** %s methods ***/\n", iface->name);
1246 first_iface = 0;
1249 if (is_override_method(iface, child, func))
1250 continue;
1252 if (!is_callas(func->attrs)) {
1253 const var_t *arg;
1255 fprintf(header, "static FORCEINLINE ");
1256 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1257 fprintf(header, " %s_%s(", name, get_name(func));
1258 write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE, NAME_C);
1259 fprintf(header, ") {\n");
1260 ++indentation;
1261 if (!is_aggregate_return(func)) {
1262 indent(header, 0);
1263 fprintf(header, "%sThis->lpVtbl->%s(This",
1264 is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
1265 get_vtbl_entry_name(iface, func));
1266 } else {
1267 indent(header, 0);
1268 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1269 fprintf(header, " __ret;\n");
1270 indent(header, 0);
1271 fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
1273 if (type_function_get_args(func->declspec.type))
1274 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
1275 fprintf(header, ",%s", arg->name);
1276 fprintf(header, ");\n");
1277 --indentation;
1278 fprintf(header, "}\n");
1283 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
1285 const statement_t *stmt;
1286 int first_iface = 1;
1288 if (type_iface_get_inherit(iface))
1289 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
1291 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1293 const var_t *func = stmt->u.var;
1294 if (first_iface) {
1295 indent(header, 0);
1296 fprintf(header, "/*** %s methods ***/\n", iface->name);
1297 first_iface = 0;
1299 if (!is_callas(func->attrs)) {
1300 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1301 if (!callconv) callconv = "STDMETHODCALLTYPE";
1302 indent(header, 0);
1303 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1304 if (is_aggregate_return(func))
1305 fprintf(header, " *");
1306 if (is_inherited_method(iface, func))
1307 fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
1308 else
1309 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
1310 ++indentation;
1311 indent(header, 0);
1312 fprintf(header, "%s *This", name);
1313 if (is_aggregate_return(func)) {
1314 fprintf(header, ",\n");
1315 indent(header, 0);
1316 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1317 fprintf(header, " *__ret");
1319 --indentation;
1320 if (type_function_get_args(func->declspec.type)) {
1321 fprintf(header, ",\n");
1322 write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE, NAME_C);
1324 fprintf(header, ");\n");
1325 fprintf(header, "\n");
1330 static void write_c_method_def(FILE *header, const type_t *iface)
1332 do_write_c_method_def(header, iface, iface->c_name);
1335 static void write_c_disp_method_def(FILE *header, const type_t *iface)
1337 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
1340 static void write_method_proto(FILE *header, const type_t *iface)
1342 const statement_t *stmt;
1344 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1346 const var_t *func = stmt->u.var;
1348 if (is_callas(func->attrs)) {
1349 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
1350 if (!callconv) callconv = "STDMETHODCALLTYPE";
1351 /* proxy prototype */
1352 write_type_decl_left(header, type_function_get_ret(func->declspec.type));
1353 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
1354 write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1355 fprintf(header, ");\n");
1356 /* stub prototype */
1357 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
1358 fprintf(header, " IRpcStubBuffer* This,\n");
1359 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1360 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
1361 fprintf(header, " DWORD* pdwStubPhase);\n");
1366 static void write_locals(FILE *fp, const type_t *iface, int body)
1368 static const char comment[]
1369 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1370 const statement_t *stmt;
1372 if (!is_object(iface))
1373 return;
1375 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
1376 const var_t *func = stmt->u.var;
1377 const var_t *cas = is_callas(func->attrs);
1379 if (cas) {
1380 const statement_t *stmt2 = NULL;
1381 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
1382 if (!strcmp(get_name(stmt2->u.var), cas->name))
1383 break;
1384 if (&stmt2->entry != type_iface_get_stmts(iface)) {
1385 const var_t *m = stmt2->u.var;
1386 /* proxy prototype - use local prototype */
1387 write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
1388 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
1389 write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1390 fprintf(fp, ")");
1391 if (body) {
1392 const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
1393 fprintf(fp, "\n{\n");
1394 fprintf(fp, " %s\n", comment);
1395 if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
1396 fprintf(fp, " return E_NOTIMPL;\n");
1397 else if (type_get_type(rt->type) != TYPE_VOID) {
1398 fprintf(fp, " ");
1399 write_type_decl(fp, rt, "rv");
1400 fprintf(fp, ";\n");
1401 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
1402 fprintf(fp, " return rv;\n");
1404 fprintf(fp, "}\n\n");
1406 else
1407 fprintf(fp, ";\n");
1408 /* stub prototype - use remotable prototype */
1409 write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
1410 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
1411 write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE, NAME_DEFAULT);
1412 fprintf(fp, ")");
1413 if (body)
1414 /* Remotable methods must all return HRESULTs. */
1415 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
1416 else
1417 fprintf(fp, ";\n");
1419 else
1420 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
1425 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
1427 const statement_t *stmt;
1428 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1430 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1431 write_locals(local_stubs, stmt->u.type, TRUE);
1435 void write_local_stubs(const statement_list_t *stmts)
1437 FILE *local_stubs;
1439 if (!local_stubs_name) return;
1441 local_stubs = fopen(local_stubs_name, "w");
1442 if (!local_stubs) {
1443 error("Could not open %s for output\n", local_stubs_name);
1444 return;
1446 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
1447 fprintf(local_stubs, "#include <objbase.h>\n");
1448 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
1450 write_local_stubs_stmts(local_stubs, stmts);
1452 fclose(local_stubs);
1455 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
1457 const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
1459 if (!callconv) callconv = "__cdecl";
1460 /* FIXME: do we need to handle call_as? */
1461 write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
1462 fprintf(header, " %s ", callconv);
1463 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1464 if (type_function_get_args(fun->declspec.type))
1465 write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE, NAME_DEFAULT);
1466 else
1467 fprintf(header, " void");
1468 fprintf(header, ");\n\n");
1471 static void write_forward(FILE *header, type_t *iface)
1473 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
1474 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
1475 fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
1476 fprintf(header, "#ifdef __cplusplus\n");
1477 write_namespace_start(header, iface->namespace);
1478 write_line(header, 0, "interface %s;", iface->name);
1479 write_namespace_end(header, iface->namespace);
1480 fprintf(header, "#endif /* __cplusplus */\n");
1481 fprintf(header, "#endif\n\n" );
1484 static char *format_apicontract_macro(const type_t *type)
1486 char *name = format_namespace(type->namespace, "", "_", type->name, NULL);
1487 int i;
1488 for (i = strlen(name); i > 0; --i) name[i - 1] = toupper(name[i - 1]);
1489 return name;
1492 static void write_apicontract_guard_start(FILE *header, const expr_t *expr)
1494 const type_t *type;
1495 char *name;
1496 int ver;
1497 if (!winrt_mode) return;
1498 type = expr->u.tref.type;
1499 ver = expr->ref->u.lval;
1500 name = format_apicontract_macro(type);
1501 fprintf(header, "#if %s_VERSION >= %#x\n", name, ver);
1502 free(name);
1505 static void write_apicontract_guard_end(FILE *header, const expr_t *expr)
1507 const type_t *type;
1508 char *name;
1509 int ver;
1510 if (!winrt_mode) return;
1511 type = expr->u.tref.type;
1512 ver = expr->ref->u.lval;
1513 name = format_apicontract_macro(type);
1514 fprintf(header, "#endif /* %s_VERSION >= %#x */\n", name, ver);
1515 free(name);
1518 static void write_com_interface_start(FILE *header, const type_t *iface)
1520 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1521 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1522 fprintf(header, "/*****************************************************************************\n");
1523 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1524 fprintf(header, " */\n");
1525 if (contract) write_apicontract_guard_start(header, contract);
1526 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
1527 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
1530 static void write_com_interface_end(FILE *header, type_t *iface)
1532 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1533 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1534 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1535 type_t *type;
1537 if (uuid)
1538 write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
1540 /* C++ interface */
1541 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1542 if (!is_global_namespace(iface->namespace)) {
1543 write_line(header, 0, "} /* extern \"C\" */");
1544 write_namespace_start(header, iface->namespace);
1546 if (uuid) {
1547 write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
1548 indent(header, 0);
1549 }else {
1550 indent(header, 0);
1551 fprintf(header, "interface ");
1553 if (type_iface_get_inherit(iface))
1555 fprintf(header, "%s : public %s\n", iface->name,
1556 type_iface_get_inherit(iface)->name);
1557 write_line(header, 1, "{");
1559 else
1561 fprintf(header, "%s\n", iface->name);
1562 write_line(header, 1, "{\n");
1563 write_line(header, 0, "BEGIN_INTERFACE\n");
1565 /* dispinterfaces don't have real functions, so don't write C++ functions for
1566 * them */
1567 if (!dispinterface)
1568 write_cpp_method_def(header, iface);
1569 if (!type_iface_get_inherit(iface))
1570 write_line(header, 0, "END_INTERFACE\n");
1571 write_line(header, -1, "};");
1572 if (!is_global_namespace(iface->namespace)) {
1573 write_namespace_end(header, iface->namespace);
1574 write_line(header, 0, "extern \"C\" {");
1576 if (uuid)
1577 write_uuid_decl(header, iface, uuid);
1578 fprintf(header, "#else\n");
1579 /* C interface */
1580 write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
1581 write_line(header, 0, "BEGIN_INTERFACE\n");
1582 if (dispinterface)
1583 write_c_disp_method_def(header, iface);
1584 else
1585 write_c_method_def(header, iface);
1586 write_line(header, 0, "END_INTERFACE");
1587 write_line(header, -1, "} %sVtbl;\n", iface->c_name);
1588 fprintf(header, "interface %s {\n", iface->c_name);
1589 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
1590 fprintf(header, "};\n\n");
1591 fprintf(header, "#ifdef COBJMACROS\n");
1592 /* dispinterfaces don't have real functions, so don't write macros for them,
1593 * only for the interface this interface inherits from, i.e. IDispatch */
1594 fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1595 type = dispinterface ? type_iface_get_inherit(iface) : iface;
1596 write_method_macro(header, type, type, iface->c_name);
1597 fprintf(header, "#else\n");
1598 write_inline_wrappers(header, type, type, iface->c_name);
1599 fprintf(header, "#endif\n");
1600 fprintf(header, "#endif\n");
1601 fprintf(header, "\n");
1602 fprintf(header, "#endif\n");
1603 fprintf(header, "\n");
1604 /* dispinterfaces don't have real functions, so don't write prototypes for
1605 * them */
1606 if (!dispinterface && !winrt_mode)
1608 write_method_proto(header, iface);
1609 write_locals(header, iface, FALSE);
1610 fprintf(header, "\n");
1612 fprintf(header, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface->c_name, dispinterface ? "DISP" : "");
1613 if (contract) write_apicontract_guard_end(header, contract);
1614 fprintf(header, "\n");
1617 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1619 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1620 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1621 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1623 fprintf(header, "/*****************************************************************************\n");
1624 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1625 fprintf(header, " */\n");
1626 if (contract) write_apicontract_guard_start(header, contract);
1627 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1628 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1629 if (var)
1631 fprintf(header, "extern ");
1632 write_type_decl( header, &var->declspec, var->name );
1633 fprintf(header, ";\n");
1635 if (old_names)
1637 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1638 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1640 else
1642 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1643 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1644 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1645 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1649 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1651 expr_t *contract = get_attrp(iface->attrs, ATTR_CONTRACT);
1652 fprintf(header, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface->name);
1653 if (contract) write_apicontract_guard_end(header, contract);
1654 fprintf(header, "\n");
1657 static void write_coclass(FILE *header, type_t *cocl)
1659 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1661 fprintf(header, "/*****************************************************************************\n");
1662 fprintf(header, " * %s coclass\n", cocl->name);
1663 fprintf(header, " */\n\n");
1664 if (uuid)
1665 write_guid(header, "CLSID", cocl->name, uuid);
1666 fprintf(header, "\n#ifdef __cplusplus\n");
1667 if (uuid)
1669 fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
1670 write_uuid_decl(header, cocl, uuid);
1672 else
1674 fprintf(header, "class %s;\n", cocl->name);
1676 fprintf(header, "#endif\n");
1677 fprintf(header, "\n");
1680 static void write_coclass_forward(FILE *header, type_t *cocl)
1682 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1683 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1684 fprintf(header, "#ifdef __cplusplus\n");
1685 fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
1686 fprintf(header, "#else\n");
1687 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1688 fprintf(header, "#endif /* defined __cplusplus */\n");
1689 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1692 static void write_apicontract(FILE *header, type_t *apicontract)
1694 char *name = format_apicontract_macro(apicontract);
1695 fprintf(header, "#if !defined(%s_VERSION)\n", name);
1696 fprintf(header, "#define %s_VERSION %#x\n", name, get_attrv(apicontract->attrs, ATTR_CONTRACTVERSION));
1697 fprintf(header, "#endif // defined(%s_VERSION)\n\n", name);
1698 free(name);
1701 static void write_runtimeclass(FILE *header, type_t *runtimeclass)
1703 expr_t *contract = get_attrp(runtimeclass->attrs, ATTR_CONTRACT);
1704 char *name, *c_name;
1705 name = format_namespace(runtimeclass->namespace, "", ".", runtimeclass->name, NULL);
1706 c_name = format_namespace(runtimeclass->namespace, "", "_", runtimeclass->name, NULL);
1707 fprintf(header, "/*\n");
1708 fprintf(header, " * Class %s\n", name);
1709 fprintf(header, " */\n");
1710 if (contract) write_apicontract_guard_start(header, contract);
1711 fprintf(header, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name);
1712 fprintf(header, "#define RUNTIMECLASS_%s_DEFINED\n", c_name);
1713 fprintf(header, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name);
1714 free(c_name);
1715 free(name);
1716 if (contract) write_apicontract_guard_end(header, contract);
1717 fprintf(header, "\n");
1720 static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
1722 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1723 fprintf(header, "#define __%s_FWD_DEFINED__\n", runtimeclass->c_name);
1724 fprintf(header, "#ifdef __cplusplus\n");
1725 write_namespace_start(header, runtimeclass->namespace);
1726 write_line(header, 0, "class %s;", runtimeclass->name);
1727 write_namespace_end(header, runtimeclass->namespace);
1728 fprintf(header, "#else\n");
1729 fprintf(header, "typedef struct %s %s;\n", runtimeclass->c_name, runtimeclass->c_name);
1730 fprintf(header, "#endif /* defined __cplusplus */\n");
1731 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass->c_name);
1734 static void write_import(FILE *header, const char *fname)
1736 char *hname, *p;
1738 hname = dup_basename(fname, ".idl");
1739 p = hname + strlen(hname) - 2;
1740 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1742 fprintf(header, "#include <%s>\n", hname);
1743 free(hname);
1746 static void write_imports(FILE *header, const statement_list_t *stmts)
1748 const statement_t *stmt;
1749 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1751 switch (stmt->type)
1753 case STMT_TYPE:
1754 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1755 write_imports(header, type_iface_get_stmts(stmt->u.type));
1756 break;
1757 case STMT_TYPEREF:
1758 case STMT_IMPORTLIB:
1759 /* not included in header */
1760 break;
1761 case STMT_IMPORT:
1762 write_import(header, stmt->u.str);
1763 break;
1764 case STMT_TYPEDEF:
1765 case STMT_MODULE:
1766 case STMT_CPPQUOTE:
1767 case STMT_PRAGMA:
1768 case STMT_DECLARATION:
1769 /* not processed here */
1770 break;
1771 case STMT_LIBRARY:
1772 write_imports(header, stmt->u.lib->stmts);
1773 break;
1778 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1780 const statement_t *stmt;
1781 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1783 switch (stmt->type)
1785 case STMT_TYPE:
1786 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1788 type_t *iface = stmt->u.type;
1789 if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
1791 write_forward(header, iface);
1792 if (type_iface_get_async_iface(iface))
1793 write_forward(header, type_iface_get_async_iface(iface));
1796 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1797 write_coclass_forward(header, stmt->u.type);
1798 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
1799 write_runtimeclass_forward(header, stmt->u.type);
1800 break;
1801 case STMT_TYPEREF:
1802 case STMT_IMPORTLIB:
1803 /* not included in header */
1804 break;
1805 case STMT_IMPORT:
1806 case STMT_TYPEDEF:
1807 case STMT_MODULE:
1808 case STMT_CPPQUOTE:
1809 case STMT_PRAGMA:
1810 case STMT_DECLARATION:
1811 /* not processed here */
1812 break;
1813 case STMT_LIBRARY:
1814 write_forward_decls(header, stmt->u.lib->stmts);
1815 break;
1820 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1822 const statement_t *stmt;
1823 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1825 switch (stmt->type)
1827 case STMT_TYPE:
1828 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1830 type_t *iface = stmt->u.type;
1831 type_t *async_iface = type_iface_get_async_iface(iface);
1832 if (is_object(iface)) is_object_interface++;
1833 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1835 write_com_interface_start(header, iface);
1836 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1837 write_com_interface_end(header, iface);
1838 if (async_iface)
1840 write_com_interface_start(header, async_iface);
1841 write_com_interface_end(header, async_iface);
1844 else
1846 write_rpc_interface_start(header, iface);
1847 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1848 write_rpc_interface_end(header, iface);
1850 if (is_object(iface)) is_object_interface--;
1852 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1853 write_coclass(header, stmt->u.type);
1854 else if (type_get_type(stmt->u.type) == TYPE_APICONTRACT)
1855 write_apicontract(header, stmt->u.type);
1856 else if (type_get_type(stmt->u.type) == TYPE_RUNTIMECLASS)
1857 write_runtimeclass(header, stmt->u.type);
1858 else
1860 write_type_definition(header, stmt->u.type, stmt->declonly);
1862 break;
1863 case STMT_TYPEREF:
1864 /* FIXME: shouldn't write out forward declarations for undefined
1865 * interfaces but a number of our IDL files depend on this */
1866 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1867 write_forward(header, stmt->u.type);
1868 break;
1869 case STMT_IMPORTLIB:
1870 case STMT_MODULE:
1871 case STMT_PRAGMA:
1872 /* not included in header */
1873 break;
1874 case STMT_IMPORT:
1875 /* not processed here */
1876 break;
1877 case STMT_TYPEDEF:
1879 const type_list_t *type_entry = stmt->u.type_list;
1880 for (; type_entry; type_entry = type_entry->next)
1881 write_typedef(header, type_entry->type, stmt->declonly);
1882 break;
1884 case STMT_LIBRARY:
1885 fprintf(header, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
1886 fprintf(header, "#define __%s_LIBRARY_DEFINED__\n", stmt->u.lib->name);
1887 write_library(header, stmt->u.lib);
1888 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1889 fprintf(header, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt->u.lib->name);
1890 break;
1891 case STMT_CPPQUOTE:
1892 fprintf(header, "%s\n", stmt->u.str);
1893 break;
1894 case STMT_DECLARATION:
1895 if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
1897 if (!ignore_funcs)
1899 int prefixes_differ = strcmp(prefix_client, prefix_server);
1901 if (prefixes_differ)
1903 fprintf(header, "/* client prototype */\n");
1904 write_function_proto(header, iface, stmt->u.var, prefix_client);
1905 fprintf(header, "/* server prototype */\n");
1907 write_function_proto(header, iface, stmt->u.var, prefix_server);
1910 else
1911 write_declaration(header, stmt->u.var);
1912 break;
1917 void write_header(const statement_list_t *stmts)
1919 FILE *header;
1921 if (!do_header) return;
1923 if(!(header = fopen(header_name, "w"))) {
1924 error("Could not open %s for output\n", header_name);
1925 return;
1927 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1929 fprintf(header, "#ifdef _WIN32\n");
1930 fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1931 fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1932 fprintf(header, "#endif\n");
1933 fprintf(header, "#include <rpc.h>\n" );
1934 fprintf(header, "#include <rpcndr.h>\n" );
1935 if (!for_each_serializable(stmts, NULL, serializable_exists))
1936 fprintf(header, "#include <midles.h>\n" );
1937 fprintf(header, "#endif\n\n");
1939 fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
1940 fprintf(header, "#include <windows.h>\n");
1941 fprintf(header, "#include <ole2.h>\n");
1942 fprintf(header, "#endif\n\n");
1944 fprintf(header, "#ifndef __%s__\n", header_token);
1945 fprintf(header, "#define __%s__\n\n", header_token);
1947 fprintf(header, "/* Forward declarations */\n\n");
1948 write_forward_decls(header, stmts);
1950 fprintf(header, "/* Headers for imported files */\n\n");
1951 write_imports(header, stmts);
1952 fprintf(header, "\n");
1953 start_cplusplus_guard(header);
1955 write_header_stmts(header, stmts, NULL, FALSE);
1957 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1958 fprintf(header, "\n");
1959 for_each_serializable(stmts, header, write_serialize_function_decl);
1960 write_user_types(header);
1961 write_generic_handle_routines(header);
1962 write_context_handle_rundowns(header);
1963 fprintf(header, "\n");
1964 fprintf(header, "/* End additional prototypes */\n");
1965 fprintf(header, "\n");
1967 end_cplusplus_guard(header);
1968 fprintf(header, "#endif /* __%s__ */\n", header_token);
1970 fclose(header);