Sync to trunk (r44371)
[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 <ctype.h>
31
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
39 typedef struct _user_type_t generic_handle_t;
40
41 static int indentation = 0;
42 user_type_list_t user_type_list = LIST_INIT(user_type_list);
43 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
44 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
45
46 static void indent(FILE *h, int delta)
47 {
48 int c;
49 if (delta < 0) indentation += delta;
50 for (c=0; c<indentation; c++) fprintf(h, " ");
51 if (delta > 0) indentation += delta;
52 }
53
54 int is_ptrchain_attr(const var_t *var, enum attr_type t)
55 {
56 if (is_attr(var->attrs, t))
57 return 1;
58 else
59 {
60 type_t *type = var->type;
61 for (;;)
62 {
63 if (is_attr(type->attrs, t))
64 return 1;
65 else if (type_is_alias(type))
66 type = type_alias_get_aliasee(type);
67 else if (is_ptr(type))
68 type = type_pointer_get_ref(type);
69 else return 0;
70 }
71 }
72 }
73
74 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
75 {
76 const type_t *t = type;
77 for (;;)
78 {
79 if (is_attr(t->attrs, attr))
80 return 1;
81 else if (type_is_alias(t))
82 t = type_alias_get_aliasee(t);
83 else return 0;
84 }
85 }
86
87 int is_attr(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 1;
92 return 0;
93 }
94
95 void *get_attrp(const attr_list_t *list, enum attr_type t)
96 {
97 const attr_t *attr;
98 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
99 if (attr->type == t) return attr->u.pval;
100 return NULL;
101 }
102
103 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
104 {
105 const attr_t *attr;
106 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
107 if (attr->type == t) return attr->u.ival;
108 return 0;
109 }
110
111 int is_void(const type_t *t)
112 {
113 return type_get_type(t) == TYPE_VOID;
114 }
115
116 int is_conformant_array(const type_t *t)
117 {
118 return is_array(t) && type_array_has_conformance(t);
119 }
120
121 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
122 {
123 if (!uuid) return;
124 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
125 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
126 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
127 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
128 uuid->Data4[6], uuid->Data4[7]);
129 }
130
131 const char *get_name(const var_t *v)
132 {
133 static char buffer[256];
134
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 strcpy( buffer, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 strcpy( buffer, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 strcpy( buffer, "putref_" );
141 else
142 buffer[0] = 0;
143 strcat( buffer, v->name );
144 return buffer;
145 }
146
147 static void write_field(FILE *h, var_t *v)
148 {
149 if (!v) return;
150 if (v->type) {
151 indent(h, 0);
152 write_type_def_or_decl(h, v->type, TRUE, v->name);
153 fprintf(h, ";\n");
154 }
155 }
156
157 static void write_fields(FILE *h, var_list_t *fields)
158 {
159 var_t *v;
160 if (!fields) return;
161 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
162 }
163
164 static void write_enums(FILE *h, var_list_t *enums)
165 {
166 var_t *v;
167 if (!enums) return;
168 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
169 {
170 if (v->name) {
171 indent(h, 0);
172 fprintf(h, "%s", get_name(v));
173 if (v->eval) {
174 fprintf(h, " = ");
175 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
176 }
177 }
178 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
179 }
180 fprintf(h, "\n");
181 }
182
183 int needs_space_after(type_t *t)
184 {
185 return (type_is_alias(t) ||
186 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
187 }
188
189 void write_type_left(FILE *h, type_t *t, int declonly)
190 {
191 if (!h) return;
192
193 if (is_attr(t->attrs, ATTR_CONST) &&
194 (type_is_alias(t) || !is_ptr(t)))
195 fprintf(h, "const ");
196
197 if (type_is_alias(t)) fprintf(h, "%s", t->name);
198 else {
199 switch (type_get_type_detect_alias(t)) {
200 case TYPE_ENUM:
201 if (!declonly && t->defined && !t->written) {
202 if (t->name) fprintf(h, "enum %s {\n", t->name);
203 else fprintf(h, "enum {\n");
204 t->written = TRUE;
205 indentation++;
206 write_enums(h, type_enum_get_values(t));
207 indent(h, -1);
208 fprintf(h, "}");
209 }
210 else fprintf(h, "enum %s", t->name ? t->name : "");
211 break;
212 case TYPE_STRUCT:
213 case TYPE_ENCAPSULATED_UNION:
214 if (!declonly && t->defined && !t->written) {
215 if (t->name) fprintf(h, "struct %s {\n", t->name);
216 else fprintf(h, "struct {\n");
217 t->written = TRUE;
218 indentation++;
219 if (type_get_type(t) != TYPE_STRUCT)
220 write_fields(h, type_encapsulated_union_get_fields(t));
221 else
222 write_fields(h, type_struct_get_fields(t));
223 indent(h, -1);
224 fprintf(h, "}");
225 }
226 else fprintf(h, "struct %s", t->name ? t->name : "");
227 break;
228 case TYPE_UNION:
229 if (!declonly && t->defined && !t->written) {
230 if (t->name) fprintf(h, "union %s {\n", t->name);
231 else fprintf(h, "union {\n");
232 t->written = TRUE;
233 indentation++;
234 write_fields(h, type_union_get_cases(t));
235 indent(h, -1);
236 fprintf(h, "}");
237 }
238 else fprintf(h, "union %s", t->name ? t->name : "");
239 break;
240 case TYPE_POINTER:
241 write_type_left(h, type_pointer_get_ref(t), declonly);
242 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
243 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
244 break;
245 case TYPE_ARRAY:
246 if (t->name && type_array_is_decl_as_ptr(t))
247 fprintf(h, "%s", t->name);
248 else
249 {
250 write_type_left(h, type_array_get_element(t), declonly);
251 if (type_array_is_decl_as_ptr(t))
252 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
253 }
254 break;
255 case TYPE_BASIC:
256 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
257 type_basic_get_type(t) != TYPE_BASIC_HYPER)
258 {
259 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
260 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
261 }
262 switch (type_basic_get_type(t))
263 {
264 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
265 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
266 case TYPE_BASIC_INT: fprintf(h, "int"); break;
267 case TYPE_BASIC_INT64: fprintf(h, "__int64"); break;
268 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
269 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
270 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
271 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
272 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
273 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
274 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
275 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
276 case TYPE_BASIC_INT32:
277 if (type_basic_get_sign(t) > 0)
278 fprintf(h, "ULONG");
279 else
280 fprintf(h, "LONG");
281 break;
282 case TYPE_BASIC_HYPER:
283 if (type_basic_get_sign(t) > 0)
284 fprintf(h, "MIDL_uhyper");
285 else
286 fprintf(h, "hyper");
287 break;
288 }
289 break;
290 case TYPE_INTERFACE:
291 case TYPE_MODULE:
292 case TYPE_COCLASS:
293 fprintf(h, "%s", t->name);
294 break;
295 case TYPE_VOID:
296 fprintf(h, "void");
297 break;
298 case TYPE_BITFIELD:
299 write_type_left(h, type_bitfield_get_field(t), declonly);
300 break;
301 case TYPE_ALIAS:
302 case TYPE_FUNCTION:
303 /* handled elsewhere */
304 assert(0);
305 break;
306 }
307 }
308 }
309
310 void write_type_right(FILE *h, type_t *t, int is_field)
311 {
312 if (!h) return;
313
314 switch (type_get_type(t))
315 {
316 case TYPE_ARRAY:
317 if (!type_array_is_decl_as_ptr(t))
318 {
319 if (is_conformant_array(t))
320 {
321 fprintf(h, "[%s]", is_field ? "1" : "");
322 t = type_array_get_element(t);
323 }
324 for ( ;
325 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
326 t = type_array_get_element(t))
327 fprintf(h, "[%u]", type_array_get_dim(t));
328 }
329 break;
330 case TYPE_BITFIELD:
331 fprintf(h, " : %lu", type_bitfield_get_bits(t)->cval);
332 break;
333 case TYPE_VOID:
334 case TYPE_BASIC:
335 case TYPE_ENUM:
336 case TYPE_STRUCT:
337 case TYPE_ENCAPSULATED_UNION:
338 case TYPE_UNION:
339 case TYPE_ALIAS:
340 case TYPE_MODULE:
341 case TYPE_COCLASS:
342 case TYPE_FUNCTION:
343 case TYPE_INTERFACE:
344 case TYPE_POINTER:
345 break;
346 }
347 }
348
349 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
350 {
351 type_t *pt = NULL;
352 int ptr_level = 0;
353
354 if (!h) return;
355
356 if (t) {
357 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
358 ;
359
360 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
361 int i;
362 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
363 if (!callconv) callconv = "";
364 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
365 write_type_left(h, type_function_get_rettype(pt), declonly);
366 fputc(' ', h);
367 if (ptr_level) fputc('(', h);
368 fprintf(h, "%s ", callconv);
369 for (i = 0; i < ptr_level; i++)
370 fputc('*', h);
371 } else
372 write_type_left(h, t, declonly);
373 }
374
375 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
376
377 if (t) {
378 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
379 const var_list_t *args = type_function_get_args(pt);
380
381 if (ptr_level) fputc(')', h);
382 fputc('(', h);
383 if (args)
384 write_args(h, args, NULL, 0, FALSE);
385 else
386 fprintf(h, "void");
387 fputc(')', h);
388 } else
389 write_type_right(h, t, is_field);
390 }
391 }
392
393 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
394 {
395 write_type_v(f, t, field, FALSE, name);
396 }
397
398 void write_type_decl(FILE *f, type_t *t, const char *name)
399 {
400 write_type_v(f, t, FALSE, TRUE, name);
401 }
402
403 void write_type_decl_left(FILE *f, type_t *t)
404 {
405 write_type_left(f, t, TRUE);
406 }
407
408 static int user_type_registered(const char *name)
409 {
410 user_type_t *ut;
411 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
412 if (!strcmp(name, ut->name))
413 return 1;
414 return 0;
415 }
416
417 static int context_handle_registered(const char *name)
418 {
419 context_handle_t *ch;
420 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
421 if (!strcmp(name, ch->name))
422 return 1;
423 return 0;
424 }
425
426 static int generic_handle_registered(const char *name)
427 {
428 generic_handle_t *gh;
429 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
430 if (!strcmp(name, gh->name))
431 return 1;
432 return 0;
433 }
434
435 /* check for types which require additional prototypes to be generated in the
436 * header */
437 void check_for_additional_prototype_types(const var_list_t *list)
438 {
439 const var_t *v;
440
441 if (!list) return;
442 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
443 {
444 type_t *type = v->type;
445 if (!type) continue;
446 for (;;) {
447 const char *name = type->name;
448 if (type->user_types_registered) break;
449 type->user_types_registered = 1;
450 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
451 if (!context_handle_registered(name))
452 {
453 context_handle_t *ch = xmalloc(sizeof(*ch));
454 ch->name = xstrdup(name);
455 list_add_tail(&context_handle_list, &ch->entry);
456 }
457 /* don't carry on parsing fields within this type */
458 break;
459 }
460 if ((type_get_type(type) != TYPE_BASIC ||
461 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
462 is_attr(type->attrs, ATTR_HANDLE)) {
463 if (!generic_handle_registered(name))
464 {
465 generic_handle_t *gh = xmalloc(sizeof(*gh));
466 gh->name = xstrdup(name);
467 list_add_tail(&generic_handle_list, &gh->entry);
468 }
469 /* don't carry on parsing fields within this type */
470 break;
471 }
472 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
473 if (!user_type_registered(name))
474 {
475 user_type_t *ut = xmalloc(sizeof *ut);
476 ut->name = xstrdup(name);
477 list_add_tail(&user_type_list, &ut->entry);
478 }
479 /* don't carry on parsing fields within this type as we are already
480 * using a wire marshaled type */
481 break;
482 }
483 else if (type_is_complete(type))
484 {
485 var_list_t *vars;
486 switch (type_get_type_detect_alias(type))
487 {
488 case TYPE_ENUM:
489 vars = type_enum_get_values(type);
490 break;
491 case TYPE_STRUCT:
492 vars = type_struct_get_fields(type);
493 break;
494 case TYPE_UNION:
495 vars = type_union_get_cases(type);
496 break;
497 default:
498 vars = NULL;
499 break;
500 }
501 check_for_additional_prototype_types(vars);
502 }
503
504 if (type_is_alias(type))
505 type = type_alias_get_aliasee(type);
506 else if (is_ptr(type))
507 type = type_pointer_get_ref(type);
508 else if (is_array(type))
509 type = type_array_get_element(type);
510 else
511 break;
512 }
513 }
514 }
515
516 static void write_user_types(FILE *header)
517 {
518 user_type_t *ut;
519 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
520 {
521 const char *name = ut->name;
522 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
523 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
524 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
525 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
526 }
527 }
528
529 static void write_context_handle_rundowns(FILE *header)
530 {
531 context_handle_t *ch;
532 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
533 {
534 const char *name = ch->name;
535 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
536 }
537 }
538
539 static void write_generic_handle_routines(FILE *header)
540 {
541 generic_handle_t *gh;
542 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
543 {
544 const char *name = gh->name;
545 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
546 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
547 }
548 }
549
550 static void write_typedef(FILE *header, type_t *type)
551 {
552 fprintf(header, "typedef ");
553 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
554 fprintf(header, ";\n");
555 }
556
557 int is_const_decl(const var_t *var)
558 {
559 const type_t *t;
560 /* strangely, MIDL accepts a const attribute on any pointer in the
561 * declaration to mean that data isn't being instantiated. this appears
562 * to be a bug, but there is no benefit to being incompatible with MIDL,
563 * so we'll do the same thing */
564 for (t = var->type; ; )
565 {
566 if (is_attr(t->attrs, ATTR_CONST))
567 return TRUE;
568 else if (is_ptr(t))
569 t = type_pointer_get_ref(t);
570 else break;
571 }
572 return FALSE;
573 }
574
575 static void write_declaration(FILE *header, const var_t *v)
576 {
577 if (is_const_decl(v) && v->eval)
578 {
579 fprintf(header, "#define %s (", v->name);
580 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
581 fprintf(header, ")\n\n");
582 }
583 else
584 {
585 switch (v->stgclass)
586 {
587 case STG_NONE:
588 case STG_REGISTER: /* ignored */
589 break;
590 case STG_STATIC:
591 fprintf(header, "static ");
592 break;
593 case STG_EXTERN:
594 fprintf(header, "extern ");
595 break;
596 }
597 write_type_def_or_decl(header, v->type, FALSE, v->name);
598 fprintf(header, ";\n\n");
599 }
600 }
601
602 static void write_library(FILE *header, const typelib_t *typelib)
603 {
604 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
605 fprintf(header, "\n");
606 write_guid(header, "LIBID", typelib->name, uuid);
607 fprintf(header, "\n");
608 }
609
610
611 const var_t* get_explicit_handle_var(const var_t *func)
612 {
613 const var_t* var;
614
615 if (!type_get_function_args(func->type))
616 return NULL;
617
618 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
619 {
620 const type_t *type = var->type;
621 if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
622 return var;
623 }
624
625 return NULL;
626 }
627
628 const type_t* get_explicit_generic_handle_type(const var_t* var)
629 {
630 const type_t *t;
631 for (t = var->type;
632 is_ptr(t) || type_is_alias(t);
633 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
634 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
635 is_attr(t->attrs, ATTR_HANDLE))
636 return t;
637 return NULL;
638 }
639
640 const var_t* get_explicit_generic_handle_var(const var_t *func)
641 {
642 const var_t* var;
643
644 if (!type_get_function_args(func->type))
645 return NULL;
646
647 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
648 if (get_explicit_generic_handle_type(var))
649 return var;
650
651 return NULL;
652 }
653
654 const var_t* get_context_handle_var(const var_t *func)
655 {
656 const var_t* var;
657
658 if (!type_get_function_args(func->type))
659 return NULL;
660
661 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
662 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
663 return var;
664
665 return NULL;
666 }
667
668 int has_out_arg_or_return(const var_t *func)
669 {
670 const var_t *var;
671
672 if (!is_void(type_function_get_rettype(func->type)))
673 return 1;
674
675 if (!type_get_function_args(func->type))
676 return 0;
677
678 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
679 if (is_attr(var->attrs, ATTR_OUT))
680 return 1;
681
682 return 0;
683 }
684
685
686 /********** INTERFACES **********/
687
688 int is_object(const attr_list_t *list)
689 {
690 const attr_t *attr;
691 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
692 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
693 return 0;
694 }
695
696 int is_local(const attr_list_t *a)
697 {
698 return is_attr(a, ATTR_LOCAL);
699 }
700
701 const var_t *is_callas(const attr_list_t *a)
702 {
703 return get_attrp(a, ATTR_CALLAS);
704 }
705
706 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
707 {
708 const statement_t *stmt;
709 int first_iface = 1;
710
711 if (type_iface_get_inherit(iface))
712 write_method_macro(header, type_iface_get_inherit(iface), name);
713
714 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
715 {
716 const var_t *func = stmt->u.var;
717
718 if (first_iface)
719 {
720 fprintf(header, "/*** %s methods ***/\n", iface->name);
721 first_iface = 0;
722 }
723
724 if (!is_callas(func->attrs)) {
725 const var_t *arg;
726
727 fprintf(header, "#define %s_%s(This", name, get_name(func));
728 if (type_get_function_args(func->type))
729 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
730 fprintf(header, ",%s", arg->name);
731 fprintf(header, ") ");
732
733 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
734 if (type_get_function_args(func->type))
735 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
736 fprintf(header, ",%s", arg->name);
737 fprintf(header, ")\n");
738 }
739 }
740 }
741
742 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
743 {
744 const var_t *arg;
745 int count = 0;
746
747 if (do_indent)
748 {
749 indentation++;
750 indent(h, 0);
751 }
752 if (method == 1) {
753 fprintf(h, "%s* This", name);
754 count++;
755 }
756 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
757 if (count) {
758 if (do_indent)
759 {
760 fprintf(h, ",\n");
761 indent(h, 0);
762 }
763 else fprintf(h, ",");
764 }
765 write_type_decl(h, arg->type, arg->name);
766 count++;
767 }
768 if (do_indent) indentation--;
769 }
770
771 static void write_cpp_method_def(FILE *header, const type_t *iface)
772 {
773 const statement_t *stmt;
774
775 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
776 {
777 const var_t *func = stmt->u.var;
778 if (!is_callas(func->attrs)) {
779 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
780 if (!callconv) callconv = "";
781 indent(header, 0);
782 fprintf(header, "virtual ");
783 write_type_decl_left(header, type_function_get_rettype(func->type));
784 fprintf(header, " %s %s(\n", callconv, get_name(func));
785 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
786 fprintf(header, ") = 0;\n");
787 fprintf(header, "\n");
788 }
789 }
790 }
791
792 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
793 {
794 const statement_t *stmt;
795 int first_iface = 1;
796
797 if (type_iface_get_inherit(iface))
798 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
799
800 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
801 {
802 const var_t *func = stmt->u.var;
803 if (first_iface) {
804 indent(header, 0);
805 fprintf(header, "/*** %s methods ***/\n", iface->name);
806 first_iface = 0;
807 }
808 if (!is_callas(func->attrs)) {
809 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
810 if (!callconv) callconv = "";
811 indent(header, 0);
812 write_type_decl_left(header, type_function_get_rettype(func->type));
813 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
814 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
815 fprintf(header, ");\n");
816 fprintf(header, "\n");
817 }
818 }
819 }
820
821 static void write_c_method_def(FILE *header, const type_t *iface)
822 {
823 do_write_c_method_def(header, iface, iface->name);
824 }
825
826 static void write_c_disp_method_def(FILE *header, const type_t *iface)
827 {
828 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
829 }
830
831 static void write_method_proto(FILE *header, const type_t *iface)
832 {
833 const statement_t *stmt;
834
835 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
836 {
837 const var_t *func = stmt->u.var;
838
839 if (!is_local(func->attrs)) {
840 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
841 if (!callconv) callconv = "";
842 /* proxy prototype */
843 write_type_decl_left(header, type_function_get_rettype(func->type));
844 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
845 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
846 fprintf(header, ");\n");
847 /* stub prototype */
848 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
849 fprintf(header, " IRpcStubBuffer* This,\n");
850 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
851 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
852 fprintf(header, " DWORD* pdwStubPhase);\n");
853 }
854 }
855 }
856
857 static void write_locals(FILE *fp, const type_t *iface, int body)
858 {
859 static const char comment[]
860 = "/* WIDL-generated stub. You must provide an implementation for this. */";
861 const statement_t *stmt;
862
863 if (!is_object(iface->attrs))
864 return;
865
866 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
867 const var_t *func = stmt->u.var;
868 const var_t *cas = is_callas(func->attrs);
869
870 if (cas) {
871 const statement_t *stmt2 = NULL;
872 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
873 if (!strcmp(stmt2->u.var->name, cas->name))
874 break;
875 if (&stmt2->entry != type_iface_get_stmts(iface)) {
876 const var_t *m = stmt2->u.var;
877 /* proxy prototype - use local prototype */
878 write_type_decl_left(fp, type_function_get_rettype(m->type));
879 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
880 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
881 fprintf(fp, ")");
882 if (body) {
883 type_t *rt = type_function_get_rettype(m->type);
884 fprintf(fp, "\n{\n");
885 fprintf(fp, " %s\n", comment);
886 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
887 fprintf(fp, " return E_NOTIMPL;\n");
888 else if (type_get_type(rt) != TYPE_VOID) {
889 fprintf(fp, " ");
890 write_type_decl(fp, rt, "rv");
891 fprintf(fp, ";\n");
892 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
893 fprintf(fp, " return rv;\n");
894 }
895 fprintf(fp, "}\n\n");
896 }
897 else
898 fprintf(fp, ";\n");
899 /* stub prototype - use remotable prototype */
900 write_type_decl_left(fp, type_function_get_rettype(func->type));
901 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
902 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
903 fprintf(fp, ")");
904 if (body)
905 /* Remotable methods must all return HRESULTs. */
906 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
907 else
908 fprintf(fp, ";\n");
909 }
910 else
911 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
912 }
913 }
914 }
915
916 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
917 {
918 const statement_t *stmt;
919 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
920 {
921 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
922 write_locals(local_stubs, stmt->u.type, TRUE);
923 else if (stmt->type == STMT_LIBRARY)
924 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
925 }
926 }
927
928 void write_local_stubs(const statement_list_t *stmts)
929 {
930 FILE *local_stubs;
931
932 if (!local_stubs_name) return;
933
934 local_stubs = fopen(local_stubs_name, "w");
935 if (!local_stubs) {
936 error("Could not open %s for output\n", local_stubs_name);
937 return;
938 }
939 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
940 fprintf(local_stubs, "#include <objbase.h>\n");
941 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
942
943 write_local_stubs_stmts(local_stubs, stmts);
944
945 fclose(local_stubs);
946 }
947
948 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
949 {
950 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
951
952 /* FIXME: do we need to handle call_as? */
953 write_type_decl_left(header, type_function_get_rettype(fun->type));
954 fprintf(header, " ");
955 if (callconv) fprintf(header, "%s ", callconv);
956 fprintf(header, "%s%s(\n", prefix, get_name(fun));
957 if (type_get_function_args(fun->type))
958 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
959 else
960 fprintf(header, " void");
961 fprintf(header, ");\n\n");
962 }
963
964 static void write_forward(FILE *header, type_t *iface)
965 {
966 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
967 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
968 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
969 fprintf(header, "#endif\n\n" );
970 }
971
972 static void write_iface_guid(FILE *header, const type_t *iface)
973 {
974 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
975 write_guid(header, "IID", iface->name, uuid);
976 }
977
978 static void write_dispiface_guid(FILE *header, const type_t *iface)
979 {
980 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
981 write_guid(header, "DIID", iface->name, uuid);
982 }
983
984 static void write_coclass_guid(FILE *header, const type_t *cocl)
985 {
986 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
987 write_guid(header, "CLSID", cocl->name, uuid);
988 }
989
990 static void write_com_interface_start(FILE *header, const type_t *iface)
991 {
992 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
993 fprintf(header, "/*****************************************************************************\n");
994 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
995 fprintf(header, " */\n");
996 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
997 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
998 }
999
1000 static void write_com_interface_end(FILE *header, type_t *iface)
1001 {
1002 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1003 if (dispinterface)
1004 write_dispiface_guid(header, iface);
1005 else
1006 write_iface_guid(header, iface);
1007 /* C++ interface */
1008 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1009 if (type_iface_get_inherit(iface))
1010 {
1011 fprintf(header, "interface %s : public %s\n", iface->name,
1012 type_iface_get_inherit(iface)->name);
1013 fprintf(header, "{\n");
1014 }
1015 else
1016 {
1017 fprintf(header, "interface %s\n", iface->name);
1018 fprintf(header, "{\n");
1019 fprintf(header, " BEGIN_INTERFACE\n");
1020 fprintf(header, "\n");
1021 }
1022 /* dispinterfaces don't have real functions, so don't write C++ functions for
1023 * them */
1024 if (!dispinterface)
1025 {
1026 indentation++;
1027 write_cpp_method_def(header, iface);
1028 indentation--;
1029 }
1030 if (!type_iface_get_inherit(iface))
1031 fprintf(header, " END_INTERFACE\n");
1032 fprintf(header, "};\n");
1033 fprintf(header, "#else\n");
1034 /* C interface */
1035 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1036 indentation++;
1037 fprintf(header, " BEGIN_INTERFACE\n");
1038 fprintf(header, "\n");
1039 if (dispinterface)
1040 write_c_disp_method_def(header, iface);
1041 else
1042 write_c_method_def(header, iface);
1043 indentation--;
1044 fprintf(header, " END_INTERFACE\n");
1045 fprintf(header, "} %sVtbl;\n", iface->name);
1046 fprintf(header, "interface %s {\n", iface->name);
1047 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1048 fprintf(header, "};\n");
1049 fprintf(header, "\n");
1050 fprintf(header, "#ifdef COBJMACROS\n");
1051 /* dispinterfaces don't have real functions, so don't write macros for them,
1052 * only for the interface this interface inherits from, i.e. IDispatch */
1053 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1054 fprintf(header, "#endif\n");
1055 fprintf(header, "\n");
1056 fprintf(header, "#endif\n");
1057 fprintf(header, "\n");
1058 /* dispinterfaces don't have real functions, so don't write prototypes for
1059 * them */
1060 if (!dispinterface)
1061 {
1062 write_method_proto(header, iface);
1063 write_locals(header, iface, FALSE);
1064 fprintf(header, "\n");
1065 }
1066 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1067 }
1068
1069 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1070 {
1071 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1072 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1073 static int allocate_written = 0;
1074
1075 if (!allocate_written)
1076 {
1077 allocate_written = 1;
1078 fprintf(header, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
1079 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1080 }
1081
1082 fprintf(header, "/*****************************************************************************\n");
1083 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1084 fprintf(header, " */\n");
1085 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1086 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1087 if (var) fprintf(header, "extern handle_t %s;\n", var);
1088 if (old_names)
1089 {
1090 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1091 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1092 }
1093 else
1094 {
1095 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1096 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1097 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1098 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1099 }
1100 }
1101
1102 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1103 {
1104 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1105 }
1106
1107 static void write_coclass(FILE *header, type_t *cocl)
1108 {
1109 fprintf(header, "/*****************************************************************************\n");
1110 fprintf(header, " * %s coclass\n", cocl->name);
1111 fprintf(header, " */\n\n");
1112 write_coclass_guid(header, cocl);
1113 fprintf(header, "\n");
1114 }
1115
1116 static void write_coclass_forward(FILE *header, type_t *cocl)
1117 {
1118 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1119 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1120 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1121 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1122 }
1123
1124 static void write_import(FILE *header, const char *fname)
1125 {
1126 char *hname, *p;
1127
1128 hname = dup_basename(fname, ".idl");
1129 p = hname + strlen(hname) - 2;
1130 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1131
1132 fprintf(header, "#include <%s>\n", hname);
1133 free(hname);
1134 }
1135
1136 static void write_imports(FILE *header, const statement_list_t *stmts)
1137 {
1138 const statement_t *stmt;
1139 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1140 {
1141 switch (stmt->type)
1142 {
1143 case STMT_TYPE:
1144 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1145 write_imports(header, type_iface_get_stmts(stmt->u.type));
1146 break;
1147 case STMT_TYPEREF:
1148 case STMT_IMPORTLIB:
1149 /* not included in header */
1150 break;
1151 case STMT_IMPORT:
1152 write_import(header, stmt->u.str);
1153 break;
1154 case STMT_TYPEDEF:
1155 case STMT_MODULE:
1156 case STMT_CPPQUOTE:
1157 case STMT_DECLARATION:
1158 /* not processed here */
1159 break;
1160 case STMT_LIBRARY:
1161 write_imports(header, stmt->u.lib->stmts);
1162 break;
1163 }
1164 }
1165 }
1166
1167 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1168 {
1169 const statement_t *stmt;
1170 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1171 {
1172 switch (stmt->type)
1173 {
1174 case STMT_TYPE:
1175 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1176 {
1177 if (is_object(stmt->u.type->attrs) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1178 write_forward(header, stmt->u.type);
1179 }
1180 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1181 write_coclass_forward(header, stmt->u.type);
1182 break;
1183 case STMT_TYPEREF:
1184 case STMT_IMPORTLIB:
1185 /* not included in header */
1186 break;
1187 case STMT_IMPORT:
1188 case STMT_TYPEDEF:
1189 case STMT_MODULE:
1190 case STMT_CPPQUOTE:
1191 case STMT_DECLARATION:
1192 /* not processed here */
1193 break;
1194 case STMT_LIBRARY:
1195 write_forward_decls(header, stmt->u.lib->stmts);
1196 break;
1197 }
1198 }
1199 }
1200
1201 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1202 {
1203 const statement_t *stmt;
1204 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1205 {
1206 switch (stmt->type)
1207 {
1208 case STMT_TYPE:
1209 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1210 {
1211 type_t *iface = stmt->u.type;
1212 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type->attrs))
1213 {
1214 write_com_interface_start(header, iface);
1215 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1216 write_com_interface_end(header, iface);
1217 }
1218 else
1219 {
1220 write_rpc_interface_start(header, iface);
1221 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1222 write_rpc_interface_end(header, iface);
1223 }
1224 }
1225 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1226 write_coclass(header, stmt->u.type);
1227 else
1228 {
1229 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1230 fprintf(header, ";\n\n");
1231 }
1232 break;
1233 case STMT_TYPEREF:
1234 /* FIXME: shouldn't write out forward declarations for undefined
1235 * interfaces but a number of our IDL files depend on this */
1236 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1237 write_forward(header, stmt->u.type);
1238 break;
1239 case STMT_IMPORTLIB:
1240 case STMT_MODULE:
1241 /* not included in header */
1242 break;
1243 case STMT_IMPORT:
1244 /* not processed here */
1245 break;
1246 case STMT_TYPEDEF:
1247 {
1248 const type_list_t *type_entry = stmt->u.type_list;
1249 for (; type_entry; type_entry = type_entry->next)
1250 write_typedef(header, type_entry->type);
1251 break;
1252 }
1253 case STMT_LIBRARY:
1254 write_library(header, stmt->u.lib);
1255 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1256 break;
1257 case STMT_CPPQUOTE:
1258 fprintf(header, "%s\n", stmt->u.str);
1259 break;
1260 case STMT_DECLARATION:
1261 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1262 {
1263 if (!ignore_funcs)
1264 {
1265 int prefixes_differ = strcmp(prefix_client, prefix_server);
1266
1267 if (prefixes_differ)
1268 {
1269 fprintf(header, "/* client prototype */\n");
1270 write_function_proto(header, iface, stmt->u.var, prefix_client);
1271 fprintf(header, "/* server prototype */\n");
1272 }
1273 write_function_proto(header, iface, stmt->u.var, prefix_server);
1274 }
1275 }
1276 else
1277 write_declaration(header, stmt->u.var);
1278 break;
1279 }
1280 }
1281 }
1282
1283 void write_header(const statement_list_t *stmts)
1284 {
1285 FILE *header;
1286
1287 if (!do_header) return;
1288
1289 if(!(header = fopen(header_name, "w"))) {
1290 error("Could not open %s for output\n", header_name);
1291 return;
1292 }
1293 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1294 fprintf(header, "#include <rpc.h>\n" );
1295 fprintf(header, "#include <rpcndr.h>\n\n" );
1296
1297 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1298 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1299 start_cplusplus_guard(header);
1300
1301 fprintf(header, "/* Headers for imported files */\n\n");
1302 write_imports(header, stmts);
1303 fprintf(header, "\n");
1304
1305 /* FIXME: should be before imported file includes */
1306 fprintf(header, "/* Forward declarations */\n\n");
1307 write_forward_decls(header, stmts);
1308 fprintf(header, "\n");
1309
1310 write_header_stmts(header, stmts, NULL, FALSE);
1311
1312 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1313 fprintf(header, "\n");
1314 write_user_types(header);
1315 write_generic_handle_routines(header);
1316 write_context_handle_rundowns(header);
1317 fprintf(header, "\n");
1318 fprintf(header, "/* End additional prototypes */\n");
1319 fprintf(header, "\n");
1320
1321 end_cplusplus_guard(header);
1322 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1323
1324 fclose(header);
1325 }