forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.c
1766 lines (1591 loc) · 53.9 KB
/
header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* IDL Compiler
*
* Copyright 2002 Ove Kaaven
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <ctype.h>
#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
#include "expr.h"
#include "typetree.h"
#include "typelib.h"
static int indentation = 0;
static int is_object_interface = 0;
user_type_list_t user_type_list = LIST_INIT(user_type_list);
context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name);
static void indent(FILE *h, int delta)
{
int c;
if (delta < 0) indentation += delta;
for (c=0; c<indentation; c++) fprintf(h, " ");
if (delta > 0) indentation += delta;
}
static void write_line(FILE *f, int delta, const char *fmt, ...)
{
va_list ap;
indent(f, delta);
va_start(ap, fmt);
vfprintf(f, fmt, ap);
va_end(ap);
fprintf(f, "\n");
}
int is_ptrchain_attr(const var_t *var, enum attr_type t)
{
if (is_attr(var->attrs, t))
return 1;
else
{
type_t *type = var->type;
for (;;)
{
if (is_attr(type->attrs, t))
return 1;
else if (type_is_alias(type))
type = type_alias_get_aliasee(type);
else if (is_ptr(type))
type = type_pointer_get_ref(type);
else return 0;
}
}
}
int is_aliaschain_attr(const type_t *type, enum attr_type attr)
{
const type_t *t = type;
for (;;)
{
if (is_attr(t->attrs, attr))
return 1;
else if (type_is_alias(t))
t = type_alias_get_aliasee(t);
else return 0;
}
}
int is_attr(const attr_list_t *list, enum attr_type t)
{
const attr_t *attr;
if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
if (attr->type == t) return 1;
return 0;
}
void *get_attrp(const attr_list_t *list, enum attr_type t)
{
const attr_t *attr;
if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
if (attr->type == t) return attr->u.pval;
return NULL;
}
unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
{
const attr_t *attr;
if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
if (attr->type == t) return attr->u.ival;
return 0;
}
static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
{
if (!uuid) return;
fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
"0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
uuid->Data4[6], uuid->Data4[7]);
}
static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
{
char *name = format_namespace(type->namespace, "", "::", type->name);
fprintf(f, "#ifdef __CRT_UUID_DECL\n");
fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
"0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
uuid->Data4[7]);
fprintf(f, "#endif\n");
free(name);
}
static const char *uuid_string(const UUID *uuid)
{
static char buf[37];
sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
return buf;
}
static void write_namespace_start(FILE *header, struct namespace *namespace)
{
if(is_global_namespace(namespace)) {
if(use_abi_namespace)
write_line(header, 1, "namespace ABI {");
return;
}
write_namespace_start(header, namespace->parent);
write_line(header, 1, "namespace %s {", namespace->name);
}
static void write_namespace_end(FILE *header, struct namespace *namespace)
{
if(is_global_namespace(namespace)) {
if(use_abi_namespace)
write_line(header, -1, "}", namespace->name);
return;
}
write_line(header, -1, "}", namespace->name);
write_namespace_end(header, namespace->parent);
}
const char *get_name(const var_t *v)
{
static char buffer[256];
if (is_attr( v->attrs, ATTR_PROPGET ))
strcpy( buffer, "get_" );
else if (is_attr( v->attrs, ATTR_PROPPUT ))
strcpy( buffer, "put_" );
else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
strcpy( buffer, "putref_" );
else
buffer[0] = 0;
strcat( buffer, v->name );
return buffer;
}
static void write_fields(FILE *h, var_list_t *fields)
{
unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
const char *name;
char buf[32];
var_t *v;
if (!fields) return;
LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
if (!v || !v->type) continue;
switch(type_get_type_detect_alias(v->type)) {
case TYPE_STRUCT:
case TYPE_ENCAPSULATED_UNION:
nameless_struct_cnt++;
break;
case TYPE_UNION:
nameless_union_cnt++;
break;
default:
;
}
}
LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
if (!v || !v->type) continue;
indent(h, 0);
name = v->name;
switch(type_get_type_detect_alias(v->type)) {
case TYPE_STRUCT:
case TYPE_ENCAPSULATED_UNION:
if(!v->name) {
fprintf(h, "__C89_NAMELESS ");
if(nameless_struct_cnt == 1) {
name = "__C89_NAMELESSSTRUCTNAME";
}else if(nameless_struct_i < 5 /* # of supporting macros */) {
sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
name = buf;
}
}
break;
case TYPE_UNION:
if(!v->name) {
fprintf(h, "__C89_NAMELESS ");
if(nameless_union_cnt == 1) {
name = "__C89_NAMELESSUNIONNAME";
}else if(nameless_union_i < 8 /* # of supporting macros */ ) {
sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
name = buf;
}
}
break;
default:
;
}
write_type_def_or_decl(h, v->type, TRUE, name);
fprintf(h, ";\n");
}
}
static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
{
var_t *v;
if (!enums) return;
LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
{
if (v->name) {
indent(h, 0);
if(!enum_name)
fprintf(h, "%s", get_name(v));
else
fprintf(h, "%s_%s", enum_name, get_name(v));
if (v->eval) {
fprintf(h, " = ");
write_expr(h, v->eval, 0, 1, NULL, NULL, "");
}
}
if (list_next( enums, &v->entry )) fprintf(h, ",\n");
}
fprintf(h, "\n");
}
int needs_space_after(type_t *t)
{
return (type_is_alias(t) ||
(!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
}
static void write_pointer_left(FILE *h, type_t *ref)
{
if (needs_space_after(ref))
fprintf(h, " ");
if (!type_is_alias(ref) && is_array(ref) && !type_array_is_decl_as_ptr(ref))
fprintf(h, "(");
fprintf(h, "*");
}
void write_type_left(FILE *h, type_t *t, enum name_type name_type, int declonly)
{
const char *name;
if (!h) return;
name = type_get_name(t, name_type);
if (is_attr(t->attrs, ATTR_CONST) &&
(type_is_alias(t) || !is_ptr(t)))
fprintf(h, "const ");
if (type_is_alias(t)) fprintf(h, "%s", t->name);
else {
switch (type_get_type_detect_alias(t)) {
case TYPE_ENUM:
if (!declonly && t->defined && !t->written) {
if (name) fprintf(h, "enum %s {\n", name);
else fprintf(h, "enum {\n");
t->written = TRUE;
indentation++;
write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
indent(h, -1);
fprintf(h, "}");
}
else fprintf(h, "enum %s", name ? name : "");
break;
case TYPE_STRUCT:
case TYPE_ENCAPSULATED_UNION:
if (!declonly && t->defined && !t->written) {
if (name) fprintf(h, "struct %s {\n", name);
else fprintf(h, "struct {\n");
t->written = TRUE;
indentation++;
if (type_get_type(t) != TYPE_STRUCT)
write_fields(h, type_encapsulated_union_get_fields(t));
else
write_fields(h, type_struct_get_fields(t));
indent(h, -1);
fprintf(h, "}");
}
else fprintf(h, "struct %s", name ? name : "");
break;
case TYPE_UNION:
if (!declonly && t->defined && !t->written) {
if (t->name) fprintf(h, "union %s {\n", t->name);
else fprintf(h, "union {\n");
t->written = TRUE;
indentation++;
write_fields(h, type_union_get_cases(t));
indent(h, -1);
fprintf(h, "}");
}
else fprintf(h, "union %s", t->name ? t->name : "");
break;
case TYPE_POINTER:
{
write_type_left(h, type_pointer_get_ref(t), name_type, declonly);
write_pointer_left(h, type_pointer_get_ref(t));
if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
break;
}
case TYPE_ARRAY:
if (t->name && type_array_is_decl_as_ptr(t))
fprintf(h, "%s", t->name);
else
{
write_type_left(h, type_array_get_element(t), name_type, declonly);
if (type_array_is_decl_as_ptr(t))
write_pointer_left(h, type_array_get_element(t));
}
break;
case TYPE_BASIC:
if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
type_basic_get_type(t) != TYPE_BASIC_INT64 &&
type_basic_get_type(t) != TYPE_BASIC_HYPER)
{
if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
}
switch (type_basic_get_type(t))
{
case TYPE_BASIC_INT8: fprintf(h, "small"); break;
case TYPE_BASIC_INT16: fprintf(h, "short"); break;
case TYPE_BASIC_INT: fprintf(h, "int"); break;
case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
case TYPE_BASIC_INT32:
if (type_basic_get_sign(t) > 0)
fprintf(h, "ULONG");
else
fprintf(h, "LONG");
break;
case TYPE_BASIC_INT64:
if (type_basic_get_sign(t) > 0)
fprintf(h, "UINT64");
else
fprintf(h, "INT64");
break;
case TYPE_BASIC_HYPER:
if (type_basic_get_sign(t) > 0)
fprintf(h, "MIDL_uhyper");
else
fprintf(h, "hyper");
break;
}
break;
case TYPE_INTERFACE:
case TYPE_MODULE:
case TYPE_COCLASS:
fprintf(h, "%s", t->name);
break;
case TYPE_VOID:
fprintf(h, "void");
break;
case TYPE_BITFIELD:
write_type_left(h, type_bitfield_get_field(t), name_type, declonly);
break;
case TYPE_ALIAS:
case TYPE_FUNCTION:
/* handled elsewhere */
assert(0);
break;
}
}
}
void write_type_right(FILE *h, type_t *t, int is_field)
{
if (!h) return;
if (type_is_alias(t)) return;
switch (type_get_type(t))
{
case TYPE_ARRAY:
{
type_t *elem = type_array_get_element(t);
if (type_array_is_decl_as_ptr(t))
{
if (!type_is_alias(elem) && is_array(elem) && !type_array_is_decl_as_ptr(elem))
fprintf(h, ")");
}
else
{
if (is_conformant_array(t))
fprintf(h, "[%s]", is_field ? "1" : "");
else
fprintf(h, "[%u]", type_array_get_dim(t));
}
write_type_right(h, elem, FALSE);
break;
}
case TYPE_POINTER:
{
type_t *ref = type_pointer_get_ref(t);
if (!type_is_alias(ref) && is_array(ref) && !type_array_is_decl_as_ptr(ref))
fprintf(h, ")");
write_type_right(h, ref, FALSE);
break;
}
case TYPE_BITFIELD:
fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
break;
case TYPE_VOID:
case TYPE_BASIC:
case TYPE_ENUM:
case TYPE_STRUCT:
case TYPE_ENCAPSULATED_UNION:
case TYPE_UNION:
case TYPE_ALIAS:
case TYPE_MODULE:
case TYPE_COCLASS:
case TYPE_FUNCTION:
case TYPE_INTERFACE:
break;
}
}
static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
{
type_t *pt = NULL;
int ptr_level = 0;
if (!h) return;
if (t) {
for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
;
if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
int i;
const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
write_type_left(h, type_function_get_rettype(pt), NAME_DEFAULT, declonly);
fputc(' ', h);
if (ptr_level) fputc('(', h);
if (callconv) fprintf(h, "%s ", callconv);
for (i = 0; i < ptr_level; i++)
fputc('*', h);
} else
write_type_left(h, t, NAME_DEFAULT, declonly);
}
if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
if (t) {
if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
const var_list_t *args = type_function_get_args(pt);
if (ptr_level) fputc(')', h);
fputc('(', h);
if (args)
write_args(h, args, NULL, 0, FALSE);
else
fprintf(h, "void");
fputc(')', h);
} else
write_type_right(h, t, is_field);
}
}
static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
{
write_type_v(f, t, field, FALSE, name);
}
static void write_type_definition(FILE *f, type_t *t)
{
int in_namespace = t->namespace && !is_global_namespace(t->namespace);
int save_written = t->written;
if(in_namespace) {
fprintf(f, "#ifdef __cplusplus\n");
fprintf(f, "} /* extern \"C\" */\n");
write_namespace_start(f, t->namespace);
}
indent(f, 0);
write_type_left(f, t, NAME_DEFAULT, FALSE);
fprintf(f, ";\n");
if(in_namespace) {
t->written = save_written;
write_namespace_end(f, t->namespace);
fprintf(f, "extern \"C\" {\n");
fprintf(f, "#else\n");
write_type_left(f, t, NAME_C, FALSE);
fprintf(f, ";\n");
fprintf(f, "#endif\n\n");
}
}
void write_type_decl(FILE *f, type_t *t, const char *name)
{
write_type_v(f, t, FALSE, TRUE, name);
}
void write_type_decl_left(FILE *f, type_t *t)
{
write_type_left(f, t, NAME_DEFAULT, TRUE);
}
static int user_type_registered(const char *name)
{
user_type_t *ut;
LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
if (!strcmp(name, ut->name))
return 1;
return 0;
}
static int context_handle_registered(const char *name)
{
context_handle_t *ch;
LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
if (!strcmp(name, ch->name))
return 1;
return 0;
}
static int generic_handle_registered(const char *name)
{
generic_handle_t *gh;
LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
if (!strcmp(name, gh->name))
return 1;
return 0;
}
unsigned int get_context_handle_offset( const type_t *type )
{
context_handle_t *ch;
unsigned int index = 0;
while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
{
if (type_is_alias( type )) type = type_alias_get_aliasee( type );
else if (is_ptr( type )) type = type_pointer_get_ref( type );
else error( "internal error: %s is not a context handle\n", type->name );
}
LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
{
if (!strcmp( type->name, ch->name )) return index;
index++;
}
error( "internal error: %s is not registered as a context handle\n", type->name );
return index;
}
unsigned int get_generic_handle_offset( const type_t *type )
{
generic_handle_t *gh;
unsigned int index = 0;
while (!is_attr( type->attrs, ATTR_HANDLE ))
{
if (type_is_alias( type )) type = type_alias_get_aliasee( type );
else if (is_ptr( type )) type = type_pointer_get_ref( type );
else error( "internal error: %s is not a generic handle\n", type->name );
}
LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
{
if (!strcmp( type->name, gh->name )) return index;
index++;
}
error( "internal error: %s is not registered as a generic handle\n", type->name );
return index;
}
/* check for types which require additional prototypes to be generated in the
* header */
void check_for_additional_prototype_types(const var_list_t *list)
{
const var_t *v;
if (!list) return;
LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
{
type_t *type = v->type;
if (!type) continue;
for (;;) {
const char *name = type->name;
if (type->user_types_registered) break;
type->user_types_registered = 1;
if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
if (!context_handle_registered(name))
{
context_handle_t *ch = xmalloc(sizeof(*ch));
ch->name = xstrdup(name);
list_add_tail(&context_handle_list, &ch->entry);
}
/* don't carry on parsing fields within this type */
break;
}
if ((type_get_type(type) != TYPE_BASIC ||
type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
is_attr(type->attrs, ATTR_HANDLE)) {
if (!generic_handle_registered(name))
{
generic_handle_t *gh = xmalloc(sizeof(*gh));
gh->name = xstrdup(name);
list_add_tail(&generic_handle_list, &gh->entry);
}
/* don't carry on parsing fields within this type */
break;
}
if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
if (!user_type_registered(name))
{
user_type_t *ut = xmalloc(sizeof *ut);
ut->name = xstrdup(name);
list_add_tail(&user_type_list, &ut->entry);
}
/* don't carry on parsing fields within this type as we are already
* using a wire marshaled type */
break;
}
else if (type_is_complete(type))
{
var_list_t *vars;
switch (type_get_type_detect_alias(type))
{
case TYPE_ENUM:
vars = type_enum_get_values(type);
break;
case TYPE_STRUCT:
vars = type_struct_get_fields(type);
break;
case TYPE_UNION:
vars = type_union_get_cases(type);
break;
default:
vars = NULL;
break;
}
check_for_additional_prototype_types(vars);
}
if (type_is_alias(type))
type = type_alias_get_aliasee(type);
else if (is_ptr(type))
type = type_pointer_get_ref(type);
else if (is_array(type))
type = type_array_get_element(type);
else
break;
}
}
}
static void write_user_types(FILE *header)
{
user_type_t *ut;
LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
{
const char *name = ut->name;
fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
}
}
static void write_context_handle_rundowns(FILE *header)
{
context_handle_t *ch;
LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
{
const char *name = ch->name;
fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
}
}
static void write_generic_handle_routines(FILE *header)
{
generic_handle_t *gh;
LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
{
const char *name = gh->name;
fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
}
}
static void write_typedef(FILE *header, type_t *type)
{
fprintf(header, "typedef ");
write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
fprintf(header, ";\n");
}
int is_const_decl(const var_t *var)
{
const type_t *t;
/* strangely, MIDL accepts a const attribute on any pointer in the
* declaration to mean that data isn't being instantiated. this appears
* to be a bug, but there is no benefit to being incompatible with MIDL,
* so we'll do the same thing */
for (t = var->type; ; )
{
if (is_attr(t->attrs, ATTR_CONST))
return TRUE;
else if (is_ptr(t))
t = type_pointer_get_ref(t);
else break;
}
return FALSE;
}
static void write_declaration(FILE *header, const var_t *v)
{
if (is_const_decl(v) && v->eval)
{
fprintf(header, "#define %s (", v->name);
write_expr(header, v->eval, 0, 1, NULL, NULL, "");
fprintf(header, ")\n\n");
}
else
{
switch (v->stgclass)
{
case STG_NONE:
case STG_REGISTER: /* ignored */
break;
case STG_STATIC:
fprintf(header, "static ");
break;
case STG_EXTERN:
fprintf(header, "extern ");
break;
}
write_type_def_or_decl(header, v->type, FALSE, v->name);
fprintf(header, ";\n\n");
}
}
static void write_library(FILE *header, const typelib_t *typelib)
{
const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
fprintf(header, "\n");
write_guid(header, "LIBID", typelib->name, uuid);
fprintf(header, "\n");
}
const type_t* get_explicit_generic_handle_type(const var_t* var)
{
const type_t *t;
for (t = var->type;
is_ptr(t) || type_is_alias(t);
t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
is_attr(t->attrs, ATTR_HANDLE))
return t;
return NULL;
}
const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
unsigned char *explicit_fc, unsigned char *implicit_fc )
{
const var_t *var;
const var_list_t *args = type_get_function_args( func->type );
*explicit_fc = *implicit_fc = 0;
if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
{
if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
if (type_get_type( var->type ) == TYPE_BASIC && type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
{
*explicit_fc = FC_BIND_PRIMITIVE;
return var;
}
if (get_explicit_generic_handle_type( var ))
{
*explicit_fc = FC_BIND_GENERIC;
return var;
}
if (is_context_handle( var->type ))
{
*explicit_fc = FC_BIND_CONTEXT;
return var;
}
}
if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
{
if (type_get_type( var->type ) == TYPE_BASIC &&
type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
*implicit_fc = FC_BIND_PRIMITIVE;
else
*implicit_fc = FC_BIND_GENERIC;
return var;
}
*implicit_fc = FC_AUTO_HANDLE;
return NULL;
}
int has_out_arg_or_return(const var_t *func)
{
const var_t *var;
if (!is_void(type_function_get_rettype(func->type)))
return 1;
if (!type_get_function_args(func->type))
return 0;
LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
if (is_attr(var->attrs, ATTR_OUT))
return 1;
return 0;
}
/********** INTERFACES **********/
int is_object(const type_t *iface)
{
const attr_t *attr;
if (type_is_defined(iface) && type_iface_get_inherit(iface))
return 1;
if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
return 0;
}
int is_local(const attr_list_t *a)
{
return is_attr(a, ATTR_LOCAL);
}
const var_t *is_callas(const attr_list_t *a)
{
return get_attrp(a, ATTR_CALLAS);
}
static int is_inherited_method(const type_t *iface, const var_t *func)
{
while ((iface = type_iface_get_inherit(iface)))
{
const statement_t *stmt;
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
{
const var_t *funccmp = stmt->u.var;
if (!is_callas(func->attrs))
{
char inherit_name[256];
/* compare full name including property prefix */
strcpy(inherit_name, get_name(funccmp));
if (!strcmp(inherit_name, get_name(func))) return 1;
}
}
}
return 0;
}
static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
{
if (iface == child)
return 0;
do
{
const statement_t *stmt;
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
{
const var_t *funccmp = stmt->u.var;
if (!is_callas(func->attrs))
{
char inherit_name[256];
/* compare full name including property prefix */
strcpy(inherit_name, get_name(funccmp));
if (!strcmp(inherit_name, get_name(func))) return 1;
}
}
}
while ((child = type_iface_get_inherit(child)) && child != iface);
return 0;
}
static int is_aggregate_return(const var_t *func)
{
enum type_type type = type_get_type(type_function_get_rettype(func->type));
return type == TYPE_STRUCT || type == TYPE_UNION ||
type == TYPE_COCLASS || type == TYPE_INTERFACE;
}
static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
{
static char buff[255];
if (is_inherited_method(iface, func))
sprintf(buff, "%s_%s", iface->name, get_name(func));
else
sprintf(buff, "%s", get_name(func));
return buff;
}
static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
{
const statement_t *stmt;
int first_iface = 1;
if (type_iface_get_inherit(iface))
write_method_macro(header, type_iface_get_inherit(iface), child, name);
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
{
const var_t *func = stmt->u.var;
if (first_iface)
{
fprintf(header, "/*** %s methods ***/\n", iface->name);
first_iface = 0;
}
if (is_override_method(iface, child, func))
continue;
if (!is_callas(func->attrs)) {
const var_t *arg;
fprintf(header, "#define %s_%s(This", name, get_name(func));
if (type_get_function_args(func->type))
LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
fprintf(header, ",%s", arg->name);
fprintf(header, ") ");
if (is_aggregate_return(func))
{
fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
continue;
}