forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcl4.c
2812 lines (2516 loc) · 93.4 KB
/
dcl4.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
/*ident "@(#)cls4:src/dcl4.c 1.38" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
dcl4.c:
Declaration of class and enum
*************************************************************************/
#include "cfront.h"
#include "size.h"
#include "template.h"
static Pname get_vf(Pname n_pvf, Pclass cl) {
/*----------------------------------------------------------------------------
search the base hierarchy of class "cl" if n_pvf (Name of Pure Virtual
Function) has a definition.
returns 0 if no definition found in the hierarchy.
returns Pname if found.
*----------------------------------------------------------------------------
*/
if (!n_pvf || !cl)
return 0;
if (n_pvf->tp && !cl->has_base(Pfct(n_pvf->tp)->memof)) {
return 0; // if pvf is not in a base of "cl"
}
Pname n_vf = cl->memtbl->look(n_pvf->string, 0);
if (n_vf && n_pvf->tp && n_vf->tp) {
if (n_vf->tp->base == OVERLOAD) {
for (Plist gl = Pgen(n_vf->tp)->fct_list; gl; gl = gl->l) {
if (!Pfct(n_pvf->tp)->check(gl->f->tp, VIRTUAL))
break;
}
if (!gl)
n_vf = 0;
} else {
if (Pfct(n_pvf->tp)->check(Pfct(n_vf->tp), VIRTUAL))
n_vf = 0;
}
}
// error('d',"get_vf %n in %t for pure virt. %n",n_vf, cl, n_pvf);
if (n_vf == 0) {
for (Pbcl b = cl->baselist; b; b = b->next) {
if (b->base == NAME || b->base == VIRTUAL) {
n_vf = get_vf(n_pvf, b->bclass);
if (n_vf && !n_vf->n_initializer) {
break;
} else {
n_vf = 0;
}
}
} // for (Pbcl b=...
} // if (n_vf==0)
return n_vf;
}
Pname find_vptr(Pclass);
void fix_args(Pfct f, Pclass cl)
/*
This function is used to cope with the case where cl::cl(cl&) is
declared AFTER f has been declared
set n_xref bit for f
*/
{
for (Pname a = f->argtype; a; a = a->n_list) {
Pname n = a->tp->is_cl_obj();
if (n && same_class(Pclass(n->tp), cl))
a->n_xref = 1;
}
}
struct typedef_info {
Pname n;
typedef_info *next;
};
static typedef_info *typedef_info_head = 0, *typedef_info_tail = 0;
static Pfct get_fct_node(Pname n) {
Ptype t = n->tp ? n->tp->skiptypedefs() : 0;
if (t && t->base == RPTR)
t = Pptr(t)->typ->skiptypedefs();
while (t && t->base == PTR)
t = Pptr(t)->typ->skiptypedefs();
return t && t->base == FCT ? Pfct(t) : 0;
}
static void record_typedef(Pname tn) {
typedef_info *p = new typedef_info;
p->n = tn;
if (typedef_info_head == 0) {
typedef_info_head = typedef_info_tail = p;
} else {
typedef_info_tail->next = p;
typedef_info_tail = p;
}
}
void typedef_check(Pname n) {
Pname cn;
Pfct f = get_fct_node(n);
if (!f)
return;
for (Pname a = f->argtype; a; a = a->n_list) {
cn = a->tp ? a->tp->is_cl_obj() : 0;
if (!a->n_xref && cn && cn->tp && !(Pclass(cn->tp)->defined & DEFINED)) {
record_typedef(n);
return;
}
}
cn = f->returns ? f->returns->is_cl_obj() : 0;
if ((!f->s_returns || f->s_returns != void_type) && cn && cn->tp
&& !(Pclass(cn->tp)->defined & DEFINED))
record_typedef(n);
}
static void typedef_checkall(Pclass cl) {
for (typedef_info *p = typedef_info_head; p; p = p->next) {
Pfct f = get_fct_node(p->n);
Pname cn;
for (Pname a = f->argtype; a; a = a->n_list) {
cn = a->tp ? a->tp->is_cl_obj() : 0;
if (!a->n_xref && cn && cn->tp && same_class(Pclass(cn->tp), cl) && cl->has_itor()) {
error('s', &p->n->where,
"C%t used asAT inTdef%n before it was known that%t has a copyK", cl, p->n, cl);
break;
}
}
cn = f->returns ? f->returns->is_cl_obj() : 0;
if ((!f->s_returns || f->s_returns != void_type) && cn && cn->tp
&& same_class(Pclass(cn->tp), cl) && cl->has_itor())
error('s', &p->n->where,
"C%t used as returnT inTdef%n before it was known that%t has a copyK", cl, p->n, cl);
}
}
Pname merge_conv(Pname c1, Pname c2) {
if (c1 == 0)
return c2;
if (c2 == 0)
return c1;
if (c1 == c2)
return c1;
error('s', "cannot merge lists of conversion functions");
return c1;
}
static int Eppp;
char *get_classname(char *s)
/*
retrieve the outermost class name in a vtable name
*/
{
// error('d',"get_classname(%s)",s);
char *s1 = 0;
while (*s) {
s1 = s;
loop:
for (; s[0] && (s[0] != '_' || s[1] && s[1] != '_'); s++)
;
if (*s) {
if (strncmp(s, "___pt__", 7) == 0) {
s += 7;
goto loop;
}
if (strncmp(s, "__pt__", 6) == 0) { // parameterized class
s += 6;
goto loop;
}
if (s[1] == 0)
++s;
else
s += 2; // bypass "__"
}
}
return s1;
}
char *drop_classname(char *s)
/*
retrieve all but the outermost class name in a vtable name
*/
{
// error('d',"drop_classname(%s)",s);
char *r = new char[strlen(s) + 1];
sprintf(r, s);
s = r;
char *s1 = s;
while (*s) {
loop:
for (; s[0] && (s[0] != '_' || s[1] && s[1] != '_'); s++)
;
if (*s) {
if (strncmp(s, "___pt__", 7) == 0) {
s += 7;
goto loop;
}
if (strncmp(s, "__pt__", 6) == 0) { // parameterized class
s += 6;
goto loop;
}
if (s[1] == 0) {
s1 = ++s;
break;
}
s1 = s;
s += 2; // bypass "__"
}
}
*s1 = '\0';
return (*r) ? r : 0;
}
bit classdef::has_const_mem()
/*
does this class have any constant members
*/
{
int i;
for (Pname m = memtbl->get_mem(i = 1); m; NEXT_NAME(memtbl, m, i))
if (m && m->tp)
switch (m->tp->base) {
case FCT:
break;
case OVERLOAD:
break;
case EOBJ:
break;
default:
if (m->tp->is_const_object() && m->n_stclass != STATIC)
return 1;
}
return 0;
}
Pbcl classdef::get_base(char *s)
/*
Find the base class whose name matches the argument
*/
{
// error('d',"%t::get_base(%s) baselist%d %t",this,s,this?baselist:0,this?baselist->bclass:0);
for (Pbcl b = baselist; b; b = b->next) {
// error('d'," b%t s %s",b->bclass,s);
for (char *s1 = s, *s2 = b->bclass->string; *s1 && *s2 && *s1 == *s2; s1++, s2++)
;
if (!(*s1 || *s2))
break;
}
return b;
}
static int offset_magic_0(Pbcl b, Pbcl bb, short virt_count) {
// error('d',"offset_magic_0(%t %t %d)", b->bclass,bb->bclass,virt_count);
Pclass bcl = bb->bclass;
int offset = 0;
for (Pbcl l = b; l; l = l->next) {
if (l->base == VIRTUAL)
continue;
Pclass bc = l->bclass;
for (Pbcl ll = bc->baselist; ll; ll = ll->next) {
if (ll->base != VIRTUAL)
continue;
int diff = strcmp(bcl->string, ll->bclass->string);
// if (ll->allocated && !diff) {
if (ll->allocated == 1 && !diff) {
offset = ll->obj_offset + l->obj_offset;
// error('d',"%d offset_magic_0 returning found offset of %t",offset, bcl);
return offset;
}
// if (diff && ll->allocated) {
if (diff && ll->allocated == 1) {
offset = ll->bclass->has_allocated_base(bcl);
if (offset) {
if (!virt_count)
offset += l->obj_offset;
// error('d',"%d offset_magic_0 returning allocated offset of %t",offset, bcl);
return offset;
}
}
}
}
if (Nvbc_alloc) {
offset = bb->obj_offset - Nvbc_alloc->obj_offset;
if (virt_count)
offset += 4;
// error('d',"nvbc: %t %d bb: %d offset:
// %d",Nvbc_alloc->bclass,Nvbc_alloc->obj_offset,bb->obj_offset,offset);
}
// error('d',"offset_magic_0 offset: %d virtualBC %t",offset,bcl);
return offset;
}
static int offset_magic_1(Pbcl b, Pbcl bl) {
// error('d',"offset_magic_1( %t, %t )",b->bclass,bl->bclass);
char *str = b->bclass->string;
for (Pbcl bb = bl->bclass->baselist; bb; bb = bb->next) {
if (bb->base != VIRTUAL)
continue;
char *s = bb->bclass->string;
if (strcmp(s, str) == 0) {
int offset = bb->obj_offset - bb->ptr_offset + 1;
// error('d',"str: %s offset: %d obj_offset: %d",str,offset,bl->obj_offset);
return offset + bl->obj_offset;
}
}
// error('d',"0 offset_magic_1 for virtualBC%t contained within%t",b->bclass,bl->bclass);
return 0;
}
// cope with offsets of virtuals in deep nests: promoted, not allocated
extern int link_compat_hack;
static int Voffset;
bit Vvtab;
bit Vvbc_alloc;
bit Vvbc_inher;
int classdef::get_offset(char *s, bit rechk)
/* Get offset represented by string as viewed from "this" */
{
// error('d',"%s::get_offset(%s %d)",string,s,rechk);
if (!s)
return 0;
char *str = get_classname(s);
Pbcl b = get_base(str);
bit unalloc = 0;
char *found_virtual = 0;
if (rechk) {
// unalloc = (b->promoted && (b->allocated == 0));
unalloc = (b->promoted && (b->allocated != 1));
// error('d',"%s::get_offset unalloc: %d b->alloc: %d",string,unalloc,b->allocated);
if (!unalloc)
return 0;
Vvtab = 0; // promoted virtual base contains vtbl
Vvbc_inher = 0;
Vvbc_alloc = 0; // promoted virtual base contains vtbl
Nvbc_alloc = 0; // set by has_allocated_base
found_virtual = has_allocated_base(str);
}
if (unalloc) {
// error('d', "recheck get_offset: voffset: %d found_virtual: %s str: %s ps:
// %s",Voffset,found_virtual,str);
if (found_virtual == 0) {
if (Vvbc_alloc && b->bclass->baselist)
return b->obj_offset;
if (strcmp(s, str)) {
if (Vvbc_inher)
return 0;
if (Vvtab == 0)
return b->bclass->get_offset(drop_classname(s));
return b->obj_offset + b->bclass->get_offset(drop_classname(s));
} else {
if (Vvbc_inher) {
if (b->allocated == 2)
link_compat_hack = b->obj_offset;
return 0;
}
return offset_magic_0(baselist->next, b, virt_count);
}
}
if (found_virtual) {
if (strcmp(s, str))
return offset_magic_1(b, get_base(found_virtual));
else
return b->obj_offset;
}
}
return b->obj_offset + b->bclass->get_offset(drop_classname(s));
}
char *vtbl_str(char *s1, char *s2)
/*
combine two pieces of a vtbl name
*/
{
// error('d',"vtbl_str(%s,%s)",s1,s2);
char *s3;
if (s1)
if (s2) {
s3 = new char[strlen(s1) + strlen(s2) + 3];
sprintf(s3, "%s__%s", s1, s2);
return s3;
} else
return s1;
else
return s2;
}
void classdef::add_vtbl(velem *v, char *s, bit virt_flag, int n_init)
/*
add vtbl to virt_list
*/
{
// error('d',"%t->add_vtbl(%d,%s)",this,v,s);
Pvirt vtab = new virt(this, v, s, virt_flag, n_init);
if (virt_flag)
has_vvtab = 1;
if (!virt_list) {
virt_list = vtab;
return;
}
// If conficting vtable entries are made because of
// a virtual base class, must be considered an error.
for (Pvirt vt = virt_list; vt; vt = vt->next)
if (vt->string && strcmp(vt->string, s) == 0) {
velem *ivec = vt->virt_init;
Pname on = ivec[0].n;
Pname nn = v[0].n;
Pclass ocl, ncl;
for (int i = 0; on && nn; i++, on = ivec[i].n, nn = v[i].n) {
ocl = Pfct(on->tp)->memof;
ncl = Pfct(nn->tp)->memof;
if (on != nn)
if (!ocl->has_base(ncl))
if (!ncl->has_base(ocl))
break;
else {
ivec[i].n = nn;
ivec[i].offset = v[i].offset;
}
}
if (on || nn)
error("virtualB: ambiguous%n and%n", on, nn);
return;
}
vtab->next = virt_list->next;
virt_list->next = vtab;
}
static int baseoffset(Pclass b, Pclass d)
/*
find offset of base class object "b" in derived class object "d"
*/
{
static int level = 0;
++level;
if (!b || !d) {
error('i', "invalidA to baseoffset()");
}
if (same_class(b, d)) { // b == d
level--;
return 0;
}
Pbcl bx;
// is "b" a direct base of "d" or a promoted indirect virtual base ?
for (bx = d->baselist; bx; bx = bx->next) {
if (same_class(b, bx->bclass)) { //(b == bx->bclass)
level--;
return bx->obj_offset;
}
}
// recursively search for "b" in bases of "d"
for (bx = d->baselist; bx; bx = bx->next) {
// don't search indirect virtual bases
if (bx->promoted)
continue;
int found = baseoffset(b, bx->bclass);
if (found != -1) {
level--;
return bx->obj_offset + found;
}
}
if (level-- > 1)
return -1; // b!=d and b is not a base of d
error('i', "fall off end of baseoffset()");
return 0;
}
int vcounter;
static int vmax;
const int vpChunk = 32;
int classdef::do_virtuals(Pvirt vtab, char *str, int leftmost, bit virt_flag)
/*
make vtbl for b in "this"
match up virtuals and assign virtual indices for the base or delegate "bcl"
first base class shares ``this'' and vtbl with this class
*/
{
if (vmax < vcounter)
vmax = vcounter;
int vpsz = (vmax + vcounter) / vpChunk + 1; // fragmentation prevention
vpsz *= vpChunk;
Pname *vp = new Pname[vpsz];
velem *ivec = vtab ? vtab->virt_init : 0;
int vo = Voffset;
int vc = 0;
int changed = 0;
// error('d',"%t->do_virtuals(%d,%s) voffset %d",this,vtab,str,Voffset);
// error('d',"virt_count %d vpsz %d vcounter %d",virt_count,vpsz,vcounter);
if (ivec) { // vtbl replacement for ivec
if (vtab->is_vbase) {
str = 0;
Voffset = get_offset(vtab->string);
// error('d',"vtabl->is_vbase: %s->get_offset(%s), voffset:
// %d",string,vtab->string,Voffset);
} else
Voffset = Voffset + vtab->vclass->get_offset(vtab->string);
Pname vn;
for (int i = 0; vn = ivec[i].n; i++) {
/*
go through virtual table's list of virtuals:
first see if the function is simply inherited
if not, check for a match
if not, then add as new
*/
if (i >= vpsz) { // resize vp vector
int tvpsz = vpsz + vpChunk;
Pname *tvp = new Pname[tvpsz];
for (int j = 0; j < i; ++j)
tvp[j] = vp[j];
delete[/*vpsz*/] vp;
vp = tvp;
// error( 'd',"resizing: i: %d vpsz: %d tvpsz: %d", i, vpsz, tvpsz );
vpsz = tvpsz;
}
char *s = vn->n_gen_fct_name;
Pname n = memtbl->look(s ? s : vn->string, 0);
// error('d',"vn %n %s n %n %d",vn,s,n,Voffset);
// error('d',"n %n %k", n, n?n->base:0 );
if (n == 0 || n->base == PUBLIC) { // FCT + FCT
// base::FCT
inher: // inherit
if (vn->n_initializer) {
if (!get_vf(vn, this)) {
c_abstract = vn;
}
}
vp[i] = vn;
if (ivec[i].offset && vtab->is_vbase) {
// error('d',"voffset[1]: %d vo: %d ivec[%d].offset:
// %d",Voffset,vo,i,ivec[i].offset);
vp[i]->n_offset = Voffset - baseoffset(Pfct(vn->tp)->memof, this);
} else
vp[i]->n_offset = ivec[i].offset;
continue;
}
Pfct f = Pfct(n->tp);
if (f == 0)
continue;
if (s && f->base == OVERLOAD) { // OVERLOAD + OVERLOAD
// vn is overloaded
// and s is its name
for (Plist gl = Pgen(f)->fct_list; gl; gl = gl->l)
if (gl->f == vn)
goto inher;
}
Pfct vnf = Pfct(vn->tp);
switch (f->base) { // re-define?
default:
error('w', &n->where, "%n hides virtual%n", n, vn);
vp[i] = vn; // not a new overloaded: inherit
if (vn->n_initializer) {
if (!get_vf(vn, this)) {
c_abstract = vn;
}
}
if (ivec[i].offset && vtab->is_vbase) {
if (Voffset - vo != ivec[i].offset) {
// error('d',"voffset[2]: %d vo: %d ivec[%d].offset:
// %d",Voffset,vo,i,ivec[i].offset);
int noffset = get_offset(vtab->string, 1);
if (noffset && noffset != Voffset)
noffset -= vo;
if (noffset == 0) {
if (Vvbc_inher)
noffset = link_compat_hack ? link_compat_hack - ivec[i].offset
: ivec[i].offset;
else
noffset = Voffset - vo;
}
vp[i]->n_offset = noffset;
} else
vp[i]->n_offset = Voffset - vo;
} else
vp[i]->n_offset = ivec[i].offset;
break;
case FCT: // derived::FCT
{
if (vnf->check(f, VIRTUAL)
== 0) { // derived::FCT match base::FCT
// VTOK: virtual, but no index assigned
// you can only inherit an index from your first base
if (Vcheckerror)
error("bad virtualT match for %n", vn);
if (f->f_virtual == VTOK)
f->f_virtual = i + 1;
vp[i] = n;
vp[i]->n_offset = Voffset;
changed = 1;
} else {
if (Vcheckerror)
error("bad virtualT match for %n", vn);
else {
// if (! vn->n_initializer) // not a pure virtual ?????
// inherits a pure virtual -- make abstract
if (vn->n_initializer) {
if (!get_vf(vn, this)) {
c_abstract = vn;
}
}
error('w', &n->where, "%n hides virtual%n", n, vn);
}
vp[i] = vn; // not a new overloaded: inherit
if (ivec[i].offset && vtab->is_vbase) {
if (Voffset - vo != ivec[i].offset) {
// error('d',"voffset[3]: %d vo: %d ivec[%d].offset:
// %d",Voffset,vo,i,ivec[i].offset);
int noffset = get_offset(vtab->string, 1);
if (noffset && noffset != Voffset)
noffset -= vo;
if (noffset == 0) {
if (Vvbc_inher)
noffset = link_compat_hack ? link_compat_hack - ivec[i].offset
: ivec[i].offset;
else
noffset = Voffset - vo;
}
vp[i]->n_offset = noffset;
} else
vp[i]->n_offset = Voffset - vo;
} else
vp[i]->n_offset = ivec[i].offset;
}
break;
}
case OVERLOAD: // derived::OVERLOAD
{
int hit = 0;
for (Plist gl = Pgen(f)->fct_list; gl; gl = gl->l) {
// try each fct from derived class
Pname fn = gl->f;
Pfct f = Pfct(fn->tp);
if (f->check(vnf, VIRTUAL) == 0) { // derived::FCT
if (Vcheckerror)
error("bad virtualT match for %n", vn);
if (f->f_virtual == VTOK)
f->f_virtual = i + 1;
vp[i] = fn;
vp[i]->n_offset = Voffset;
changed = 1;
goto found;
} else {
if (Vcheckerror)
error("bad virtualT match for %n", vn);
}
if (Vcheckerror == 0)
switch (f->f_virtual) {
case 0:
case VTOK:
hit = 1;
}
}
if (hit) {
// inherits a pure virtual -- make it abstract
if (vn->n_initializer) {
if (!get_vf(vn, this)) {
c_abstract = vn;
}
}
error('w', &n->where, "%n hides virtual%n ofT %t", n, vn, vn->tp);
}
vp[i] = vn; // not a new overloaded: inherit
if (ivec[i].offset && vtab->is_vbase) {
if (Voffset - vo != ivec[i].offset) {
// error('d',"voffset[4]: %d vo: %d ivec[%d].offset:
// %d",Voffset,vo,i,ivec[i].offset);
int noffset = get_offset(vtab->string, 1);
if (noffset && noffset != Voffset)
noffset -= vo;
if (noffset == 0) {
if (Vvbc_inher)
noffset = link_compat_hack ? link_compat_hack - ivec[i].offset
: ivec[i].offset;
else
noffset = Voffset - vo;
}
vp[i]->n_offset = noffset;
} else
vp[i]->n_offset = Voffset - vo;
} else
vp[i]->n_offset = ivec[i].offset;
found:
break;
}
}
}
Voffset = vo;
vc = i;
}
// error( 'd', "do_virtuals: out of loop: vc: %d vpsz: %d changed: %d", vc, vpsz,changed );
if (leftmost) {
/*
add new virtuals:
`VTOK' marks ``new virtual, no index assigned''.
You can only be new once (no base or first base).
*/
int i;
for (Pname nn = memtbl->get_mem(i = 1); nn; NEXT_NAME(memtbl, nn, i)) {
if (nn->base == TNAME)
continue;
Pfct f = Pfct(nn->tp);
if (vc >= vpsz) { // resize vp vector
int tvpsz = vpsz + vpChunk;
Pname *tvp = new Pname[tvpsz];
for (int j = 0; j < vc; ++j)
tvp[j] = vp[j];
delete[/*vpsz*/] vp;
vp = tvp;
vpsz = tvpsz;
}
// error('d',"f %n %t",nn,f);
if (f)
switch (f->base) {
case FCT:
// error('d',"fv %d",f->f_virtual);
if (f->f_virtual == VTOK) {
// declared virtual, or
// virtual in some base
f->f_virtual = ++vc;
vp[f->f_virtual - 1] = nn;
vp[f->f_virtual - 1]->n_offset = 0;
f->f_vdef = 1;
changed = 2;
}
break;
case OVERLOAD: {
for (Plist gl = Pgen(f)->fct_list; gl; gl = gl->l) {
Pname fn = gl->f;
Pfct f = Pfct(fn->tp);
if (vc >= vpsz) { // resize vp vector
int tvpsz = vpsz + vpChunk;
Pname *tvp = new Pname[tvpsz];
for (int j = 0; j < vc; ++j) {
tvp[j] = vp[j];
}
delete[/*vpsz*/] vp;
vp = tvp;
vpsz = tvpsz;
}
if (f->f_virtual == VTOK) {
f->f_virtual = ++vc;
vp[f->f_virtual - 1] = fn;
vp[f->f_virtual - 1]->n_offset = 0;
f->f_vdef = 1;
changed = 2;
}
}
break;
}
}
}
// error('d',"%s changed %d has_vvtab %d",string,changed,has_vvtab);
// error('d',"vc %d vpsz %d",vc,vpsz);
virt_count = 0;
if (changed)
virt_count = vc;
else if (has_vvtab) {
virt_merge = 1;
if (vc && vtab->is_vbase)
leftmost = 0;
}
}
// error('d',"vc %d ch %d vp[%d] virt_count %d",vc,changed,vpsz,virt_count);
if (changed || !leftmost) {
// vc==0 if all explicit virtuals in fact were declared in base
velem *v = new velem[vc + 1];
for (int i = 0; i < vc; i++) {
v[i].n = vp[i];
v[i].offset = vp[i]->n_offset;
}
v[vc].n = 0;
if (leftmost)
add_vtbl(v, 0, 0, 0);
else
add_vtbl(v, vtbl_str(vtab->string, str), virt_flag || vtab->is_vbase, vc + 1);
delete vp;
vcounter = 0;
return 1;
}
delete vp;
vcounter = 0;
return 0;
}
int classdef::all_virt(Pclass bcl, char *s, int leftmost, bit virt_flag) {
// error('d',"%t->all_virt( %t %s leftmost: %d",this,bcl,s,leftmost);
int i = 0;
if (bcl->virt_count) {
for (Pvirt blist = bcl->virt_list; blist; blist = blist->next) {
if (virt_merge && virt_flag == 0 && blist->is_vbase == 0)
continue;
i += do_virtuals(blist, s, leftmost, virt_flag);
if (i == 0 && leftmost && virt_merge == 0)
return 0;
leftmost = 0;
}
}
// finding virt_list stops recursive step
// if vtables found and updated, return number
if (i)
return i;
for (Pbcl b = bcl->baselist; b; b = b->next) {
if (b->promoted)
continue;
// error('d',"all_virt b %t vl %d bl %d",b->bclass,b->bclass->virt_list,b->bclass->baselist);
if (leftmost && b->base == VIRTUAL) {
i += do_virtuals(0, 0, 1, 0);
if (i == 0 && virt_merge == 0) {
// be careful to propagate class abstraction
if (bcl == this && b->bclass->c_abstract)
c_abstract = b->bclass->c_abstract;
return 0;
}
leftmost = 0;
}
int vo = Voffset;
if (b->base == VIRTUAL) {
Voffset = baseoffset(b->bclass, this);
} else {
Voffset += b->obj_offset;
}
// error('d',"all_virt obj_offset %t voffset %d vo %d %d",b->bclass,Voffset,vo);
if (b->base == VIRTUAL)
i += all_virt(b->bclass, b->bclass->string, leftmost, 1);
else
i += all_virt(b->bclass, vtbl_str(b->bclass->string, s), leftmost, virt_flag);
if (i == 0 && leftmost && virt_merge == 0) {
// be careful to propagate class abstraction
if (bcl == this && !c_abstract && b->next) {
for (Pbcl bb = b; bb; bb = bb->next) {
if (bb->promoted)
continue;
if (bb->bclass->c_abstract)
c_abstract = bb->bclass->c_abstract;
}
}
return 0;
}
Voffset = vo;
leftmost = 0;
}
// if recursion updated vtables, return number
if (i)
return i;
// no vtables updated in recursion
// look for new virtuals
if (leftmost)
return do_virtuals(0, 0, 1, 0);
else
return 0;
}
Pexpr copy_obj(Pexpr l, Pexpr r, int sz)
/*
generate:
struct _s { char[sz]; };
*(struct _s*)this->m = *(struct _s*)arg.mem;
*/
{
if (!sz)
sz = 1;
// error('d',"copy_obj(%d)",sz);
char *s = make_name('S');
fprintf(out_file, "struct %s { char v[%d]; };\n", s, sz);
Pname n = new name(s);
Ptype t = new basetype(COBJ, n);
t = new ptr(PTR, t);
l = new texpr(G_CAST, t, l); // new cast(t,l);
l = l->contents();
r = new texpr(G_CAST, t, r); // new cast(t,r);
r = r->contents();
return new expr(ASSIGN, l, r);
}
Ptype find_arg_type(Pclass cl)
// first determine argument type
{
int i;
int mod = 0;
for (Pbcl b = cl->baselist; b; b = b->next) {
Pclass bcl = b->bclass;
switch (b->base) {
case VIRTUAL:
case NAME: // generate :b(*(b*)&arg)
{
Pname itor = bcl->has_itor();
if (itor && itor->tp->base == FCT) {
Pname a = Pfct(itor->tp)->argtype;
Pptr p = a->tp->is_ref();
if (p && p->typ->tconst() == 0) {
mod = 1;
goto ll1;
}
}
}
}
}
ll1:
if (mod == 0) {
for (Pname m = cl->memtbl->get_mem(i = 1); m; NEXT_NAME(cl->memtbl, m, i)) {
if (m->base == TNAME)
continue;
if (m->n_evaluated || m->n_stclass == STATIC) // ignore static members
continue;
Pname cln = (m->base == PUBLIC ? m->n_qualifier : m)->tp->is_cl_obj();
if (cln) {
Pname itor = Pclass(cln->tp)->has_itor();
if (itor && itor->tp->base == FCT) {
Pname a = Pfct(itor->tp)->argtype;
Pptr p = a->tp->is_ref();
if (p && p->typ->tconst() == 0) {