- Sync up Mm interface with WinLdr branch (introduce the concept of a memory type...
[reactos.git] / reactos / tools / widl / header.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2002 Ove Kaaven
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
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 <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38
39 static int indentation = 0;
40 user_type_list_t user_type_list = LIST_INIT(user_type_list);
41 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
42
43 static void indent(FILE *h, int delta)
44 {
45 int c;
46 if (delta < 0) indentation += delta;
47 for (c=0; c<indentation; c++) fprintf(h, " ");
48 if (delta > 0) indentation += delta;
49 }
50
51 int is_ptrchain_attr(const var_t *var, enum attr_type t)
52 {
53 if (is_attr(var->attrs, t))
54 return 1;
55 else
56 {
57 type_t *type = var->type;
58 for (;;)
59 {
60 if (is_attr(type->attrs, t))
61 return 1;
62 else if (type->kind == TKIND_ALIAS)
63 type = type->orig;
64 else if (is_ptr(type))
65 type = type->ref;
66 else return 0;
67 }
68 }
69 }
70
71 int is_attr(const attr_list_t *list, enum attr_type t)
72 {
73 const attr_t *attr;
74 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
75 if (attr->type == t) return 1;
76 return 0;
77 }
78
79 void *get_attrp(const attr_list_t *list, enum attr_type t)
80 {
81 const attr_t *attr;
82 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
83 if (attr->type == t) return attr->u.pval;
84 return NULL;
85 }
86
87 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
88 {
89 const attr_t *attr;
90 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
91 if (attr->type == t) return attr->u.ival;
92 return 0;
93 }
94
95 int is_void(const type_t *t)
96 {
97 if (!t->type && !t->ref) return 1;
98 return 0;
99 }
100
101 int is_conformant_array(const type_t *t)
102 {
103 return t->type == RPC_FC_CARRAY
104 || t->type == RPC_FC_CVARRAY
105 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
106 }
107
108 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
109 {
110 if (!uuid) return;
111 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
112 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
113 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
114 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
115 uuid->Data4[6], uuid->Data4[7]);
116 }
117
118 void write_name(FILE *h, const var_t *v)
119 {
120 if (is_attr( v->attrs, ATTR_PROPGET ))
121 fprintf(h, "get_" );
122 else if (is_attr( v->attrs, ATTR_PROPPUT ))
123 fprintf(h, "put_" );
124 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
125 fprintf(h, "putref_" );
126 fprintf(h, "%s", v->name);
127 }
128
129 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
130 {
131 fprintf(h, "%s", prefix);
132 write_name(h, v);
133 }
134
135 static void write_field(FILE *h, var_t *v)
136 {
137 if (!v) return;
138 if (v->type) {
139 const char *name = v->name;
140 if (name == NULL) {
141 switch (v->type->type) {
142 case RPC_FC_STRUCT:
143 case RPC_FC_CVSTRUCT:
144 case RPC_FC_CPSTRUCT:
145 case RPC_FC_CSTRUCT:
146 case RPC_FC_PSTRUCT:
147 case RPC_FC_BOGUS_STRUCT:
148 case RPC_FC_ENCAPSULATED_UNION:
149 name = "DUMMYSTRUCTNAME";
150 break;
151 case RPC_FC_NON_ENCAPSULATED_UNION:
152 name = "DUMMYUNIONNAME";
153 break;
154 default:
155 /* ? */
156 break;
157 }
158 }
159 indent(h, 0);
160 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
161 fprintf(h, ";\n");
162 }
163 }
164
165 static void write_fields(FILE *h, var_list_t *fields)
166 {
167 var_t *v;
168 if (!fields) return;
169 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
170 }
171
172 static void write_enums(FILE *h, var_list_t *enums)
173 {
174 var_t *v;
175 if (!enums) return;
176 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
177 {
178 if (v->name) {
179 indent(h, 0);
180 write_name(h, v);
181 if (v->eval) {
182 fprintf(h, " = ");
183 write_expr(h, v->eval, 0);
184 }
185 }
186 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
187 }
188 fprintf(h, "\n");
189 }
190
191 int needs_space_after(type_t *t)
192 {
193 return (t->kind == TKIND_ALIAS
194 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
195 }
196
197 void write_type_left(FILE *h, type_t *t, int declonly)
198 {
199 if (!h) return;
200
201 if (t->is_const) fprintf(h, "const ");
202
203 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
204 else if (t->declarray) write_type_left(h, t->ref, declonly);
205 else {
206 if (t->sign > 0) fprintf(h, "signed ");
207 else if (t->sign < 0) fprintf(h, "unsigned ");
208 switch (t->type) {
209 case RPC_FC_ENUM16:
210 case RPC_FC_ENUM32:
211 if (!declonly && t->defined && !t->written && !t->ignore) {
212 if (t->name) fprintf(h, "enum %s {\n", t->name);
213 else fprintf(h, "enum {\n");
214 t->written = TRUE;
215 indentation++;
216 write_enums(h, t->fields);
217 indent(h, -1);
218 fprintf(h, "}");
219 }
220 else fprintf(h, "enum %s", t->name ? t->name : "");
221 break;
222 case RPC_FC_STRUCT:
223 case RPC_FC_CVSTRUCT:
224 case RPC_FC_CPSTRUCT:
225 case RPC_FC_CSTRUCT:
226 case RPC_FC_PSTRUCT:
227 case RPC_FC_BOGUS_STRUCT:
228 case RPC_FC_ENCAPSULATED_UNION:
229 if (!declonly && t->defined && !t->written && !t->ignore) {
230 if (t->name) fprintf(h, "struct %s {\n", t->name);
231 else fprintf(h, "struct {\n");
232 t->written = TRUE;
233 indentation++;
234 write_fields(h, t->fields);
235 indent(h, -1);
236 fprintf(h, "}");
237 }
238 else fprintf(h, "struct %s", t->name ? t->name : "");
239 break;
240 case RPC_FC_NON_ENCAPSULATED_UNION:
241 if (!declonly && t->defined && !t->written && !t->ignore) {
242 if (t->name) fprintf(h, "union %s {\n", t->name);
243 else fprintf(h, "union {\n");
244 t->written = TRUE;
245 indentation++;
246 write_fields(h, t->fields);
247 indent(h, -1);
248 fprintf(h, "}");
249 }
250 else fprintf(h, "union %s", t->name ? t->name : "");
251 break;
252 case RPC_FC_RP:
253 case RPC_FC_UP:
254 case RPC_FC_FP:
255 case RPC_FC_OP:
256 case RPC_FC_CARRAY:
257 case RPC_FC_CVARRAY:
258 case RPC_FC_BOGUS_ARRAY:
259 write_type_left(h, t->ref, declonly);
260 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
261 break;
262 default:
263 fprintf(h, "%s", t->name);
264 }
265 }
266 }
267
268 void write_type_right(FILE *h, type_t *t, int is_field)
269 {
270 if (!h) return;
271
272 if (t->declarray) {
273 if (is_conformant_array(t)) {
274 fprintf(h, "[%s]", is_field ? "1" : "");
275 t = t->ref;
276 }
277 for ( ; t->declarray; t = t->ref)
278 fprintf(h, "[%lu]", t->dim);
279 }
280 }
281
282 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
283 const char *fmt, va_list args)
284 {
285 if (!h) return;
286
287 write_type_left(h, t, declonly);
288 if (fmt) {
289 if (needs_space_after(t))
290 fprintf(h, " ");
291 vfprintf(h, fmt, args);
292 }
293 write_type_right(h, t, is_field);
294 }
295
296 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
297 {
298 va_list args;
299 va_start(args, fmt);
300 write_type_v(f, t, field, FALSE, fmt, args);
301 va_end(args);
302 }
303
304 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
305 {
306 va_list args;
307 va_start(args, fmt);
308 write_type_v(f, t, FALSE, TRUE, fmt, args);
309 va_end(args);
310 }
311
312 void write_type_decl_left(FILE *f, type_t *t)
313 {
314 write_type_left(f, t, TRUE);
315 }
316
317 static int user_type_registered(const char *name)
318 {
319 user_type_t *ut;
320 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
321 if (!strcmp(name, ut->name))
322 return 1;
323 return 0;
324 }
325
326 static int context_handle_registered(const char *name)
327 {
328 context_handle_t *ch;
329 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
330 if (!strcmp(name, ch->name))
331 return 1;
332 return 0;
333 }
334
335 void check_for_user_types_and_context_handles(const var_list_t *list)
336 {
337 const var_t *v;
338
339 if (!list) return;
340 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
341 {
342 type_t *type;
343 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
344 const char *name = type->name;
345 if (type->user_types_registered) continue;
346 type->user_types_registered = 1;
347 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
348 if (!context_handle_registered(name))
349 {
350 context_handle_t *ch = xmalloc(sizeof(*ch));
351 ch->name = xstrdup(name);
352 list_add_tail(&context_handle_list, &ch->entry);
353 }
354 /* don't carry on parsing fields within this type */
355 break;
356 }
357 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
358 if (!user_type_registered(name))
359 {
360 user_type_t *ut = xmalloc(sizeof *ut);
361 ut->name = xstrdup(name);
362 list_add_tail(&user_type_list, &ut->entry);
363 }
364 /* don't carry on parsing fields within this type as we are already
365 * using a wire marshaled type */
366 break;
367 }
368 else
369 {
370 check_for_user_types_and_context_handles(type->fields);
371 }
372 }
373 }
374 }
375
376 void write_user_types(void)
377 {
378 user_type_t *ut;
379 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
380 {
381 const char *name = ut->name;
382 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
383 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
384 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
385 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
386 }
387 }
388
389 void write_context_handle_rundowns(void)
390 {
391 context_handle_t *ch;
392 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
393 {
394 const char *name = ch->name;
395 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
396 }
397 }
398
399 void write_typedef(type_t *type)
400 {
401 fprintf(header, "typedef ");
402 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
403 fprintf(header, ";\n");
404 }
405
406 void write_expr(FILE *h, const expr_t *e, int brackets)
407 {
408 switch (e->type) {
409 case EXPR_VOID:
410 break;
411 case EXPR_NUM:
412 fprintf(h, "%lu", e->u.lval);
413 break;
414 case EXPR_HEXNUM:
415 fprintf(h, "0x%lx", e->u.lval);
416 break;
417 case EXPR_DOUBLE:
418 fprintf(h, "%#.15g", e->u.dval);
419 break;
420 case EXPR_TRUEFALSE:
421 if (e->u.lval == 0)
422 fprintf(h, "FALSE");
423 else
424 fprintf(h, "TRUE");
425 break;
426 case EXPR_IDENTIFIER:
427 fprintf(h, "%s", e->u.sval);
428 break;
429 case EXPR_NEG:
430 fprintf(h, "-");
431 write_expr(h, e->ref, 1);
432 break;
433 case EXPR_NOT:
434 fprintf(h, "~");
435 write_expr(h, e->ref, 1);
436 break;
437 case EXPR_PPTR:
438 fprintf(h, "*");
439 write_expr(h, e->ref, 1);
440 break;
441 case EXPR_CAST:
442 fprintf(h, "(");
443 write_type_decl(h, e->u.tref, NULL);
444 fprintf(h, ")");
445 write_expr(h, e->ref, 1);
446 break;
447 case EXPR_SIZEOF:
448 fprintf(h, "sizeof(");
449 write_type_decl(h, e->u.tref, NULL);
450 fprintf(h, ")");
451 break;
452 case EXPR_SHL:
453 case EXPR_SHR:
454 case EXPR_MUL:
455 case EXPR_DIV:
456 case EXPR_ADD:
457 case EXPR_SUB:
458 case EXPR_AND:
459 case EXPR_OR:
460 if (brackets) fprintf(h, "(");
461 write_expr(h, e->ref, 1);
462 switch (e->type) {
463 case EXPR_SHL: fprintf(h, " << "); break;
464 case EXPR_SHR: fprintf(h, " >> "); break;
465 case EXPR_MUL: fprintf(h, " * "); break;
466 case EXPR_DIV: fprintf(h, " / "); break;
467 case EXPR_ADD: fprintf(h, " + "); break;
468 case EXPR_SUB: fprintf(h, " - "); break;
469 case EXPR_AND: fprintf(h, " & "); break;
470 case EXPR_OR: fprintf(h, " | "); break;
471 default: break;
472 }
473 write_expr(h, e->u.ext, 1);
474 if (brackets) fprintf(h, ")");
475 break;
476 case EXPR_COND:
477 if (brackets) fprintf(h, "(");
478 write_expr(h, e->ref, 1);
479 fprintf(h, " ? ");
480 write_expr(h, e->u.ext, 1);
481 fprintf(h, " : ");
482 write_expr(h, e->ext2, 1);
483 if (brackets) fprintf(h, ")");
484 break;
485 case EXPR_ADDRESSOF:
486 fprintf(h, "&");
487 write_expr(h, e->ref, 1);
488 break;
489 }
490 }
491
492 void write_constdef(const var_t *v)
493 {
494 fprintf(header, "#define %s (", v->name);
495 write_expr(header, v->eval, 0);
496 fprintf(header, ")\n\n");
497 }
498
499 void write_externdef(const var_t *v)
500 {
501 fprintf(header, "extern const ");
502 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
503 fprintf(header, ";\n\n");
504 }
505
506 void write_library(const char *name, const attr_list_t *attr)
507 {
508 const UUID *uuid = get_attrp(attr, ATTR_UUID);
509 fprintf(header, "\n");
510 write_guid(header, "LIBID", name, uuid);
511 fprintf(header, "\n");
512 }
513
514
515 const var_t* get_explicit_handle_var(const func_t* func)
516 {
517 const var_t* var;
518
519 if (!func->args)
520 return NULL;
521
522 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
523 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
524 return var;
525
526 return NULL;
527 }
528
529 int has_out_arg_or_return(const func_t *func)
530 {
531 const var_t *var;
532
533 if (!is_void(func->def->type))
534 return 1;
535
536 if (!func->args)
537 return 0;
538
539 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
540 if (is_attr(var->attrs, ATTR_OUT))
541 return 1;
542
543 return 0;
544 }
545
546
547 /********** INTERFACES **********/
548
549 int is_object(const attr_list_t *list)
550 {
551 const attr_t *attr;
552 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
553 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
554 return 0;
555 }
556
557 int is_local(const attr_list_t *a)
558 {
559 return is_attr(a, ATTR_LOCAL);
560 }
561
562 const var_t *is_callas(const attr_list_t *a)
563 {
564 return get_attrp(a, ATTR_CALLAS);
565 }
566
567 static void write_method_macro(const type_t *iface, const char *name)
568 {
569 const func_t *cur;
570
571 if (iface->ref) write_method_macro(iface->ref, name);
572
573 if (!iface->funcs) return;
574
575 fprintf(header, "/*** %s methods ***/\n", iface->name);
576 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
577 {
578 var_t *def = cur->def;
579 if (!is_callas(def->attrs)) {
580 const var_t *arg;
581 int argc = 0;
582 int c;
583
584 if (cur->args) LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry ) argc++;
585
586 fprintf(header, "#define %s_", name);
587 write_name(header,def);
588 fprintf(header, "(p");
589 for (c=0; c<argc; c++)
590 fprintf(header, ",%c", c+'a');
591 fprintf(header, ") ");
592
593 fprintf(header, "(p)->lpVtbl->");
594 write_name(header, def);
595 fprintf(header, "(p");
596 for (c=0; c<argc; c++)
597 fprintf(header, ",%c", c+'a');
598 fprintf(header, ")\n");
599 }
600 }
601 }
602
603 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
604 {
605 const var_t *arg;
606 int count = 0;
607
608 if (do_indent)
609 {
610 indentation++;
611 indent(h, 0);
612 }
613 if (method == 1) {
614 fprintf(h, "%s* This", name);
615 count++;
616 }
617 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
618 if (count) {
619 if (do_indent)
620 {
621 fprintf(h, ",\n");
622 indent(h, 0);
623 }
624 else fprintf(h, ",");
625 }
626 if (arg->args)
627 {
628 write_type_decl_left(h, arg->type);
629 fprintf(h, " (STDMETHODCALLTYPE *");
630 write_name(h,arg);
631 fprintf(h, ")(");
632 write_args(h, arg->args, NULL, 0, FALSE);
633 fprintf(h, ")");
634 }
635 else
636 write_type_decl(h, arg->type, "%s", arg->name);
637 count++;
638 }
639 if (do_indent) indentation--;
640 }
641
642 static void write_cpp_method_def(const type_t *iface)
643 {
644 const func_t *cur;
645
646 if (!iface->funcs) return;
647
648 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
649 {
650 var_t *def = cur->def;
651 if (!is_callas(def->attrs)) {
652 indent(header, 0);
653 fprintf(header, "virtual ");
654 write_type_decl_left(header, def->type);
655 fprintf(header, " STDMETHODCALLTYPE ");
656 write_name(header, def);
657 fprintf(header, "(\n");
658 write_args(header, cur->args, iface->name, 2, TRUE);
659 fprintf(header, ") = 0;\n");
660 fprintf(header, "\n");
661 }
662 }
663 }
664
665 static void do_write_c_method_def(const type_t *iface, const char *name)
666 {
667 const func_t *cur;
668
669 if (iface->ref) do_write_c_method_def(iface->ref, name);
670
671 if (!iface->funcs) return;
672 indent(header, 0);
673 fprintf(header, "/*** %s methods ***/\n", iface->name);
674 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
675 {
676 const var_t *def = cur->def;
677 if (!is_callas(def->attrs)) {
678 indent(header, 0);
679 write_type_decl_left(header, def->type);
680 fprintf(header, " (STDMETHODCALLTYPE *");
681 write_name(header, def);
682 fprintf(header, ")(\n");
683 write_args(header, cur->args, name, 1, TRUE);
684 fprintf(header, ");\n");
685 fprintf(header, "\n");
686 }
687 }
688 }
689
690 static void write_c_method_def(const type_t *iface)
691 {
692 do_write_c_method_def(iface, iface->name);
693 }
694
695 static void write_c_disp_method_def(const type_t *iface)
696 {
697 do_write_c_method_def(iface->ref, iface->name);
698 }
699
700 static void write_method_proto(const type_t *iface)
701 {
702 const func_t *cur;
703
704 if (!iface->funcs) return;
705 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
706 {
707 const var_t *def = cur->def;
708
709 if (!is_local(def->attrs)) {
710 /* proxy prototype */
711 write_type_decl_left(header, def->type);
712 fprintf(header, " CALLBACK %s_", iface->name);
713 write_name(header, def);
714 fprintf(header, "_Proxy(\n");
715 write_args(header, cur->args, iface->name, 1, TRUE);
716 fprintf(header, ");\n");
717 /* stub prototype */
718 fprintf(header, "void __RPC_STUB %s_", iface->name);
719 write_name(header,def);
720 fprintf(header, "_Stub(\n");
721 fprintf(header, " IRpcStubBuffer* This,\n");
722 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
723 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
724 fprintf(header, " DWORD* pdwStubPhase);\n");
725 }
726 }
727 }
728
729 void write_locals(FILE *fp, const type_t *iface, int body)
730 {
731 static const char comment[]
732 = "/* WIDL-generated stub. You must provide an implementation for this. */";
733 const func_list_t *funcs = iface->funcs;
734 const func_t *cur;
735
736 if (!is_object(iface->attrs) || !funcs)
737 return;
738
739 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
740 const var_t *def = cur->def;
741 const var_t *cas = is_callas(def->attrs);
742
743 if (cas) {
744 const func_t *m;
745 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
746 if (!strcmp(m->def->name, cas->name))
747 break;
748 if (&m->entry != iface->funcs) {
749 const var_t *mdef = m->def;
750 /* proxy prototype - use local prototype */
751 write_type_decl_left(fp, mdef->type);
752 fprintf(fp, " CALLBACK %s_", iface->name);
753 write_name(fp, mdef);
754 fprintf(fp, "_Proxy(\n");
755 write_args(fp, m->args, iface->name, 1, TRUE);
756 fprintf(fp, ")");
757 if (body) {
758 type_t *rt = mdef->type;
759 fprintf(fp, "\n{\n");
760 fprintf(fp, " %s\n", comment);
761 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
762 fprintf(fp, " return E_NOTIMPL;\n");
763 else if (rt->type) {
764 fprintf(fp, " ");
765 write_type_decl(fp, rt, "rv");
766 fprintf(fp, ";\n");
767 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
768 fprintf(fp, " return rv;\n");
769 }
770 fprintf(fp, "}\n\n");
771 }
772 else
773 fprintf(fp, ";\n");
774 /* stub prototype - use remotable prototype */
775 write_type_decl_left(fp, def->type);
776 fprintf(fp, " __RPC_STUB %s_", iface->name);
777 write_name(fp, mdef);
778 fprintf(fp, "_Stub(\n");
779 write_args(fp, cur->args, iface->name, 1, TRUE);
780 fprintf(fp, ")");
781 if (body)
782 /* Remotable methods must all return HRESULTs. */
783 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
784 else
785 fprintf(fp, ";\n");
786 }
787 else
788 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
789 }
790 }
791 }
792
793 static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
794 {
795 var_t *def = fun->def;
796
797 /* FIXME: do we need to handle call_as? */
798 write_type_decl_left(header, def->type);
799 fprintf(header, " ");
800 write_prefix_name(header, prefix, def);
801 fprintf(header, "(\n");
802 if (fun->args)
803 write_args(header, fun->args, iface->name, 0, TRUE);
804 else
805 fprintf(header, " void");
806 fprintf(header, ");\n");
807 }
808
809 static void write_function_protos(const type_t *iface)
810 {
811 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
812 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
813 const var_t* explicit_handle_var;
814 const func_t *cur;
815 int prefixes_differ = strcmp(prefix_client, prefix_server);
816
817 if (!iface->funcs) return;
818 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
819 {
820 var_t *def = cur->def;
821
822 /* check for a defined binding handle */
823 explicit_handle_var = get_explicit_handle_var(cur);
824 if (explicit_handle) {
825 if (!explicit_handle_var) {
826 error("%s() does not define an explicit binding handle!\n", def->name);
827 return;
828 }
829 } else if (implicit_handle) {
830 if (explicit_handle_var) {
831 error("%s() must not define a binding handle!\n", def->name);
832 return;
833 }
834 }
835
836 if (prefixes_differ) {
837 fprintf(header, "/* client prototype */\n");
838 write_function_proto(iface, cur, prefix_client);
839 fprintf(header, "/* server prototype */\n");
840 }
841 write_function_proto(iface, cur, prefix_server);
842 }
843 }
844
845 void write_forward(type_t *iface)
846 {
847 /* C/C++ forwards should only be written for object interfaces, so if we
848 * have a full definition we only write one if we find [object] among the
849 * attributes - however, if we don't have a full definition at this point
850 * (i.e. this is an IDL forward), then we also assume that it is an object
851 * interface, since non-object interfaces shouldn't need forwards */
852 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
853 && !iface->written) {
854 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
855 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
856 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
857 fprintf(header, "#endif\n\n" );
858 iface->written = TRUE;
859 }
860 }
861
862 static void write_iface_guid(const type_t *iface)
863 {
864 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
865 write_guid(header, "IID", iface->name, uuid);
866 }
867
868 static void write_dispiface_guid(const type_t *iface)
869 {
870 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
871 write_guid(header, "DIID", iface->name, uuid);
872 }
873
874 static void write_coclass_guid(type_t *cocl)
875 {
876 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
877 write_guid(header, "CLSID", cocl->name, uuid);
878 }
879
880 static void write_com_interface(type_t *iface)
881 {
882 if (!iface->funcs && !iface->ref) {
883 parser_warning("%s has no methods\n", iface->name);
884 return;
885 }
886
887 fprintf(header, "/*****************************************************************************\n");
888 fprintf(header, " * %s interface\n", iface->name);
889 fprintf(header, " */\n");
890 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
891 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
892 write_iface_guid(iface);
893 write_forward(iface);
894 /* C++ interface */
895 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
896 if (iface->ref)
897 {
898 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
899 fprintf(header, "{\n");
900 indentation++;
901 write_cpp_method_def(iface);
902 indentation--;
903 fprintf(header, "};\n");
904 }
905 else
906 {
907 fprintf(header, "interface %s\n", iface->name);
908 fprintf(header, "{\n");
909 fprintf(header, " BEGIN_INTERFACE\n");
910 fprintf(header, "\n");
911 indentation++;
912 write_cpp_method_def(iface);
913 indentation--;
914 fprintf(header, " END_INTERFACE\n");
915 fprintf(header, "};\n");
916 }
917 fprintf(header, "#else\n");
918 /* C interface */
919 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
920 indentation++;
921 fprintf(header, " BEGIN_INTERFACE\n");
922 fprintf(header, "\n");
923 write_c_method_def(iface);
924 indentation--;
925 fprintf(header, " END_INTERFACE\n");
926 fprintf(header, "} %sVtbl;\n", iface->name);
927 fprintf(header, "interface %s {\n", iface->name);
928 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
929 fprintf(header, "};\n");
930 fprintf(header, "\n");
931 fprintf(header, "#ifdef COBJMACROS\n");
932 write_method_macro(iface, iface->name);
933 fprintf(header, "#endif\n");
934 fprintf(header, "\n");
935 fprintf(header, "#endif\n");
936 fprintf(header, "\n");
937 write_method_proto(iface);
938 write_locals(header, iface, FALSE);
939 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
940 }
941
942 static void write_rpc_interface(const type_t *iface)
943 {
944 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
945 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
946 static int allocate_written = 0;
947
948 if (!allocate_written)
949 {
950 allocate_written = 1;
951 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
952 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
953 }
954
955 fprintf(header, "/*****************************************************************************\n");
956 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
957 fprintf(header, " */\n");
958 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
959 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
960 if (iface->funcs)
961 {
962 write_iface_guid(iface);
963 if (var) fprintf(header, "extern handle_t %s;\n", var);
964 if (old_names)
965 {
966 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
967 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
968 }
969 else
970 {
971 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
972 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
973 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
974 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
975 }
976 write_function_protos(iface);
977 }
978 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
979
980 /* FIXME: server/client code */
981 }
982
983 void write_interface(type_t *iface)
984 {
985 if (is_object(iface->attrs))
986 write_com_interface(iface);
987 else
988 write_rpc_interface(iface);
989 }
990
991 void write_dispinterface(type_t *iface)
992 {
993 fprintf(header, "/*****************************************************************************\n");
994 fprintf(header, " * %s dispinterface\n", iface->name);
995 fprintf(header, " */\n");
996 fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
997 fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
998 write_dispiface_guid(iface);
999 write_forward(iface);
1000 /* C++ interface */
1001 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1002 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
1003 fprintf(header, "{\n");
1004 fprintf(header, "};\n");
1005 fprintf(header, "#else\n");
1006 /* C interface */
1007 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1008 indentation++;
1009 fprintf(header, " BEGIN_INTERFACE\n");
1010 fprintf(header, "\n");
1011 write_c_disp_method_def(iface);
1012 indentation--;
1013 fprintf(header, " END_INTERFACE\n");
1014 fprintf(header, "} %sVtbl;\n", iface->name);
1015 fprintf(header, "interface %s {\n", iface->name);
1016 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1017 fprintf(header, "};\n");
1018 fprintf(header, "\n");
1019 fprintf(header, "#ifdef COBJMACROS\n");
1020 write_method_macro(iface->ref, iface->name);
1021 fprintf(header, "#endif\n");
1022 fprintf(header, "\n");
1023 fprintf(header, "#endif\n");
1024 fprintf(header, "\n");
1025 fprintf(header,"#endif /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
1026 }
1027
1028 void write_coclass(type_t *cocl)
1029 {
1030 fprintf(header, "/*****************************************************************************\n");
1031 fprintf(header, " * %s coclass\n", cocl->name);
1032 fprintf(header, " */\n\n");
1033 write_coclass_guid(cocl);
1034 fprintf(header, "\n");
1035 }
1036
1037 void write_coclass_forward(type_t *cocl)
1038 {
1039 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1040 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1041 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1042 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1043 }