forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.c
1815 lines (1660 loc) · 63.4 KB
/
find.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/find.c 1.22" */
/*******************************************************************************
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.
find.c:
name lookup and visibility checks
*******************************************************************/
#include "cfront.h"
#include "template.h"
Pname undef(Pname n, Ptable tbl, TOK f) {
switch (f) {
case CCON:
error("illegalF call: explicit call ofK %s()", n->string);
break;
case 0:
error("%nU", n);
break;
case CALL:
error("UF%n called", n);
break;
case REF:
case DOT:
error("M%nU", n);
break;
case ADDROF:
error("address ofU%n", n);
break;
}
if (tbl == gtbl) {
Pname nn = tbl->insert(n, 0);
if (f == CALL) {
nn->tp = new fct(defa_type, 0, 0);
nn->n_sto = nn->n_scope = EXTERN;
} else
nn->tp = any_type;
delete n;
return nn;
}
n->n_table = tbl;
n->tp = any_type;
return n;
}
static int mptr; // &C::m
static Pname me; // name of fct requesting access to name using find_name()
static Pfct mef; // fct requesting access to name using find_name()
Pclass tcl; // class of original ``this''
Pclass mec; // class requesting access to name using find_name()
int mex; // 0 suppresses access error messages
static void check_local_nested_ref(Pname nn, TOK f, loc where)
// check for illegal refs to automatics, etc., in surrounding scope
{
if (processing_sizeof)
return;
// error('d',&where,"check_local_nested_ref nn%n f%k me%n mec%t",nn,f,me,mec);
// error('d',&where," nntp%t scope%k sto%k stclass%k",nn->tp,nn->n_scope,nn->n_sto,nn->n_stclass);
switch (nn->n_scope) {
case ARG:
error(&where, "automatic variable%n referenced in localC", nn);
break;
case FCT:
if (nn->n_sto != STATIC && nn->n_sto != EXTERN && nn->n_stclass != ENUM) {
switch (nn->tp->skiptypedefs()->base) {
case FCT:
case OVERLOAD:
break;
default:
if (!nn->tp->is_const_object())
error(&where, "automatic variable%n referenced in localC", nn);
else if (f == ADDROF)
error(&where, "address of local const%n in localC", nn);
}
}
break;
case 0:
case PUBLIC: // ref to member of enclosing class
// shouldn't happen ...
// this function should only be called for non-member refs...
break;
} // switch n_scope
}
Pexpr find_name(Pname n, Pclass cl, Ptable tbl, int f, Pname m)
/*
in function ``m'' find the true name for "n",
implicitly define if undefined
f==CALL: n() cl == cc->cot
f==REF: p->n cl == class of *p
f==DOT: obj.n cl == class of obj
f==ADDROF: &n cl == cc->cot
f==0 n (none of the above)
"tbl" defines local scope (block or global)
*/
{
if (n == 0)
error('i', "find_name(n==0,cl==%t,tbl==%d,f==%k,m==%n)", cl, tbl, f, m);
Pname q = n->n_qualifier;
char *s = n->string;
Pfct mf = m ? m->fct_type() : 0;
Pname tnrv;
if (mf && mf->nrv && !q && f != DOT && f != REF && strcmp(mf->nrv->string, s) == 0
&& ((tnrv = tbl->look(s, 0)) == 0 || tnrv->n_table == gtbl))
return mf->f_result->contents();
Pexpr ee;
DB(if (Ddebug >= 3) {
error('d', &n->where, "find_name(n%n, cl%t, ..., f %d, m%n)", n, cl, f, m);
error('d', &n->where, " -- q %d %k %t %s", q, q ? q->base : 0, q ? q->tp : 0, tbl->whatami());
if (Ddebug >= 4)
tbl->dump();
});
tcl = cl;
mex = 1;
if (me = m) {
mef = Pfct(me->tp);
if (mef->base != FCT)
error('i', "mef %d %k", mef, mef->base);
if (cc->cot && cc->cot->lex_level > m->lex_level)
// cc->cot is a class local to me
mec = cc->cot;
else
mec = mef->memof;
} else {
mef = 0;
if (!tbl && (f == DOT || f == REF))
mec = cc->cot;
else {
if (q == 0 && cl == 0 && tbl && tbl != gtbl && tbl->t_name) {
// hack: processing static data mem def at
// global scope -- name::dcl() sets tbl
// to class's memtbl, but doesn't push
// class context
// Ideally, dcl() should push a new context,
// but due to other ideosyncrasies,
// this doesn't work.
cl = (Pclass) tbl->t_name->tp;
if (cl->base != CLASS)
error('i', &n->where, "find_name(%n, 0, %d,%k, 0 ) --CTX for table", n, tbl, f);
}
mec = cl;
}
}
if (n->base == MDOT)
error('i', "find (mdot%n)", n);
if (n->n_table) {
me = 0;
return n;
}
if (me == 0 && (tbl == gtbl || cc->nof == 0))
me = dummy_fct; // for error message
// error('d',"q%n%t f%k",q,q?q->tp:0,f);
if (q) { // qualified name: q::s
if (q == sta_name) { // explicitly global: ::s
Pname nn = gtbl->look(s, 0);
if (nn == 0) {
me = 0;
return undef(n, gtbl, f);
}
nn->use();
delete n;
me = 0;
return nn;
}
{
Pname aq = q; // actual q
while (aq->tp->base == TYPE)
aq = Pbase(aq->tp)->b_name;
if (aq->tp->base != COBJ) {
if (aq->n_template_arg == template_type_formal)
error('s', "use of %n::%sW formalYTZ", aq, s);
else
error("Qr%n not aCN", q);
me = 0;
return undef(n, gtbl, f);
}
q = aq;
}
Pclass qcl = Pclass(Pbase(q->tp)->b_name->tp);
Pclass bcl = cl;
if (cl == 0 || f == ADDROF)
bcl = cl = qcl; // Pclass(Pbase(q->tp)->b_name->tp);
else {
if (!same_class(qcl, cl)) { // really a base?
bcl = cl->is_base(qcl->string);
if (bcl == 0 || !same_class(bcl, qcl)) {
if (f == REF || f == DOT) {
error("%t is not aBC of%t", qcl, cl);
me = 0;
return undef(n, cl->memtbl, 7);
}
goto sss;
}
// else try in base or for static
}
}
if (f == ADDROF)
mptr = 1; // &C::m
ee = cl->find_name(s, same_class(bcl, cl) ? 0 : bcl); // really a member?
if (ee && ee->base == NAME && Pname(ee)->n_stclass != STATIC && ee->tp->is_ref())
error("P toRM: %s::%s", cl->string, Pname(ee)->string);
mptr = 0;
if (ee == 0) {
sss:
// error('d',"sss %k",f);
Pname nn = qcl->memtbl->look(s, 0);
if ((f == 0)
&& (nn && nn->tp && nn->tp->base != EOBJ && nn->tp->base != FCT
&& nn->tp->base != OVERLOAD)
&& (nn->n_stclass != STATIC)
&& (q && q->tp && q->tp->base != FCT && q->tp->base != OVERLOAD)) {
error("O missing for%n", q);
if (n)
delete n;
me = 0;
return q;
}
if (f != REF && f != DOT) {
// try for static member of other class:
Pclass qcl = Pclass(Pbase(q->tp)->b_name->tp);
mptr = 1;
if (cl && cl->csu == ANON) {
mec = (cc - 1)->cot;
ee = qcl->find_name(s, qcl);
} else
ee = qcl->find_name(s, qcl);
mptr = 0;
if (ee && ee->base == NAME) {
DEL(n);
me = 0;
return ee;
}
/* better error message; excuse the goto
template <class t> class z {
public: enum y { a, b }; };
main() { int i = z::a; }
*/
if (ee == 0 && qcl->class_base == CL_TEMPLATE) {
error("YCM %t::%n requires %t<instance>::%n", qcl, n, qcl, n);
goto finishing_up;
}
}
error("QdN%n::%n not found in %t", q, n, cl);
finishing_up:
me = 0;
return undef(n, bcl ? bcl->memtbl : cl->memtbl, 7);
}
if (ee->base == REF && ee->e1 == 0) { // &C::f, no ``this''
// error('d',"ee %k %d f %k",ee->base,ee->e1,f);
switch (f) {
case 0:
case CALL: // SSS
{
Pexpr mm = ee->mem;
while (mm->base == REF || mm->base == MDOT)
mm = mm->mem;
if (mm->base == NAME)
switch (mm->tp->base) {
case FCT:
case OVERLOAD:
goto addrof;
default:
if (Pname(mm)->n_stclass == STATIC)
goto addrof;
}
}
error("O orOP missing forM%n", n);
case ADDROF:
addrof : {
Pexpr x = ee;
ee = ee->mem;
delete x;
}
case REF:
case DOT:
break;
default:
error("QdN%n::%n used in nonC context", q, n);
}
}
delete n;
me = 0;
return ee;
} // if q
if (f != DOT && f != REF) { // not .s or ->s: look for local, global, and member
/* ref to name without ::, . or ->
* tbl == current table (global, class or block)
* mec == cc->cot == nearest enclosing class
* cl == tcl == referencing class -- usually == mec, except for
* some calls from simpl.c/simpl2.c for refs to op new/delete
* In this case, search through cl to global scope, not
* through context of calling function (me) or enclosing class
* (mec).
* me == m == nearest enclosing function
* cc->c_this == nearest enclosing 'this' pointer
* - used in classdef::find_name() to construct 'this->n'
* if tbl is block scope, tbl->look() will find names
* local to the ftn containing the block, or global,
* bypassing intervening function/class scopes
* Therefore, intervening contexts must be searched explicitly.
*/
Pname save_this = cc->c_this;
Pname fn = me;
Pfct ft = mef;
Pclass dc = ft ? ft->def_context : 0;
Pname global_nn = 0;
int new_context = 0; // set when looking in enclosing contexts
if (cl && !same_class(cl, mec)) {
// error('d',"n%n cl%t fn%n mec%t",n,cl,fn,mec);
for (Pclass cx = cl; cx && cx->c_context == 0; cx = cx->in_class)
;
if (cx) {
fn = cx->in_fct;
ft = (Pfct) fn->tp;
} else {
fn = 0;
ft = 0;
}
tbl = cl->memtbl;
cc->c_this = 0;
// error('d'," cl%t fn%n",cl,fn);
}
while (mec && mec->csu == ANON)
mec = mec->in_class;
for (;;) {
Pclass cx;
Pname oth = cc->c_this;
Pexpr ee;
if (cl) {
if (tbl == cl->memtbl) {
// remove c_this so "object missing"
// missing msg will be printed if
// if n is found in nested scope
cc->c_this = 0;
} else if (ft == 0 || !same_class(cl, ft->memof)) {
// error('d',&n->where,"find_name%n: confused context cl%t fn%n tbl
// %s",n,cl,fn,tbl->whatami()); error('d',&n->where," m%n me%n mec%t",m,me,mec);
error('i', &n->where, "find_name%n: confused context cl%t fn%n tbl %s", n, cl, fn,
tbl->whatami());
} else { // check local scope first
Pname nn = tbl->look(s, 0);
if (nn && nn->base == TNAME)
nn = 0;
if (nn) { // local or global name nn
if (nn->n_table != gtbl) {
if (m == 0)
error('i', &n->where, "find_name%n: local scope but missing m", n);
// error('d',&n->where,"found%n m%n fn%n mec%t",nn,m,fn,mec);
// this should only be
// executed once for initial
// local scope -- so no need
// to execute
// if ( new context )
// check_local_nested_ref(nn,f,n->where);
nn->use();
DEL(n);
me = 0;
cc->c_this = save_this;
return nn;
}
// nn was found in gtbl, but
// intervening scopes still exist
// try them before returning nn
global_nn = nn;
} // if nn
} // else check local scope
if (dc) { // defining context for friend function
// class A { friend B::f() { ... } };
// dc==A, cl==B
// error('d',&n->where,"%n dc%t cl%t m%n",n,dc,cl,m);
cc->c_this = 0; // for "object missing" error
cx = dc;
ee = dc->find_name(s, 0);
while (ee == 0) {
if (cx->c_context)
break;
cx = cx->in_class;
if (cx == 0)
break;
if (same_class(cx, cl))
break;
ee = cx->find_name(s, 0);
} // while ee == 0
cc->c_this = oth;
dc = 0;
if (ee)
goto eee;
}
cx = cl;
ee = cl->find_name(s, 0);
while (ee == 0) {
if (cx->c_context)
break;
cx = cx->in_class;
if (cx == 0)
break;
// remove c_this so "object missing"
// missing msg will be printed if
// if n is found in nested scope
cc->c_this = 0;
ee = cx->find_name(s, 0);
} // while ee == 0
cc->c_this = oth;
if (ee) { // class member name
eee:
if (new_context) {
// class A { // defines op new/delete
// void f() {
// class B { B(){} ~B(){} };
// }
// };
// mec==B cl==A
// cfront currently wants to
// use global op, but issue warning
Pname nx = Pname(ee);
Pname nn = global_nn;
if ((nn || (nn = gtbl->look(s, 0)) != 0) && nn->n_oper) {
while (nx->base == REF)
nx = (Pname) nx->mem;
error('w', "%n andG%n are both visible within%t -- usingG%n", nx, nn, mec,
nn);
nn->use();
DEL(n);
me = 0;
cc->c_this = save_this;
return nn;
}
}
if (ee->base == REF && ee->e1 == 0) {
Pexpr mm = ee->mem;
while (mm->base == REF || mm->base == MDOT)
mm = mm->mem;
if (mm->base == NAME)
switch (mm->tp->base) {
default:
if (Pname(mm)->n_stclass != STATIC)
error("O orOP missing for%n", mm);
case FCT:
case OVERLOAD:
DEL(n);
me = 0;
cc->c_this = save_this;
return mm;
}
}
DEL(n);
me = 0;
cc->c_this = save_this;
return ee;
} // if ee
// name not found in cl or any enclosing
// class within current context
// cx should == outermost class in current context
tbl = cx ? cx->c_context : gtbl;
// now tbl must be either gtbl or a block table in fn
if (tbl == 0)
error('i', "missing context table for cl%t/cx%t", cl, cx);
if (tbl == gtbl && global_nn && !dc) {
// name found in previous iteration
global_nn->use();
DEL(n);
me = 0;
cc->c_this = save_this;
return global_nn;
}
new_context = 1;
} // if cl
// tbl should be local or global
Pname nn = tbl->look(s, 0);
if (nn && nn->base == TNAME)
nn = 0;
if (nn) { // local or global name nn
if (nn->n_table != gtbl) {
if (m == 0)
error('i', &n->where, "find_name%n: local scope but missing m", n);
if (new_context) {
// nn is a local name in
// an enclosing context
// print appropriate err
// error('d',&n->where,"found%n m%n fn%n mec%t",nn,m,fn,mec);
check_local_nested_ref(nn, f, n->where);
}
} else if (ft && (ft->memof || dc)) {
// nn was found in gtbl, but
// intervening scopes still exist
// try them before returning nn
global_nn = nn;
goto nxt;
}
nn->use();
DEL(n);
me = 0;
cc->c_this = save_this;
return nn;
} // if nn
nxt:
if (dc) { // defining context for friend
// see also if(cl) above
cc->c_this = 0; // for "object missing" error
cx = dc;
ee = dc->find_name(s, 0);
while (ee == 0) {
if (cx->c_context)
break;
cx = cx->in_class;
if (cx == 0)
break;
ee = cx->find_name(s, 0);
} // while ee == 0
cc->c_this = oth;
if (ee)
goto eee;
}
new_context = 1;
cl = ft ? ft->memof : 0;
if (cl == 0) {
if (global_nn) {
global_nn->use();
DEL(n);
me = 0;
cc->c_this = save_this;
return global_nn;
}
break;
}
// jump to next outer context
tbl = cl->memtbl;
fn = cx ? cx->in_fct : 0;
if (fn) {
ft = (Pfct) fn->tp;
cc->c_this = ft->f_this;
} else {
ft = 0;
cc->c_this = 0;
}
dc = ft ? ft->def_context : 0;
} // for(;;)
me = 0;
cc->c_this = save_this;
return undef(n, gtbl, f);
} // if not . or ->
if (ee = cl->find_name(s, cl)) { // .s or ->s
DEL(n);
me = 0;
return ee;
}
if (!strcmp(s, cl->string)) {
me = 0;
return undef(n, gtbl, CCON);
}
me = 0;
return undef(n, gtbl, f);
}
int classdef::check_dup(Pclass cl, TOK bb)
/*
see if cl is a base of this; return 0 if no clash
*/
{
for (Pbcl b = baselist; b; b = b->next) {
if (::same_class(cl, b->bclass)) {
if (bb != VIRTUAL) {
if (b->base == VIRTUAL)
error('w', "%t inaccessible because of virtual%t in%t", cl, cl, this);
else
error('w', "%t inaccessible because of%t in%t", cl, cl, this);
return 1;
} else if (b->base != VIRTUAL) {
error('w', "virtual %t inaccessible because of%t in%t", cl, cl, this);
return 1;
}
}
if (b->bclass->check_dup(cl, bb))
return 1;
}
return 0;
}
TOK Nvis;
TOK Nvirt;
TOK ppbase;
Pclass classdef::is_base(char *s, TOK *pptr, int level)
/*
is "s" a public base class of this?
*/
{
// error('d',"%s->is_base(%s) %k",string,s,ppbase);
TOK pp = ppbase;
TOK ppaccum;
// Pclass res = 0;
for (Pbcl b = baselist; b; b = b->next) {
if (b->promoted)
continue;
char *str = 0;
if (b->bclass->class_base == INSTANTIATED)
str = ((Ptclass) b->bclass)->unparametrized_tname()->string;
// error('d',"base: %s str: %s", b->bclass->string, str);
if (strcmp(s, b->bclass->string) == 0 || (str && strcmp(s, str) == 0)) {
ppaccum = PUBLIC;
if (b->ppp != PUBLIC && !::same_class(cc->cot, this)
&& (cc->nof == 0 || this->has_friend(Pfct(cc->nof->tp)) == 0))
ppaccum = b->ppp;
// Nvirt is now used with template functions to recognize B<T>, D<T>
// that is, if set, then a standard conversion is being used
Nvirt = b->base;
// error('d',"is_base: level: %d, b->base: %k res: %d ppbase: %k",level,b->base,res,ppbase);
// if (level==0 && res && b->base==VIRTUAL)
// ppbase = b->ppp>ppbase?b->ppp:ppbase;
if (pptr)
*pptr = ppaccum;
if (level == 0)
ppbase = ppaccum;
return b->bclass;
} else {
// if (b->ppp!=PUBLIC &&
// !::same_class(cc->cot,this) &&
// (cc->nof==0 || this->has_friend(Pfct(cc->nof->tp))==0))
// ppbase = b->ppp>ppbase?b->ppp:ppbase;//
//PUBLIC<PROTECTED<PRIVATE
TOK prot;
Pclass bc = b->bclass->is_base(s, &prot, level + 1);
if (bc) {
ppaccum = prot;
if (b->ppp != PUBLIC && !::same_class(cc->cot, this)
&& (cc->nof == 0 || this->has_friend(Pfct(cc->nof->tp)) == 0))
ppaccum = b->ppp > ppaccum ? b->ppp : ppaccum;
// guaranteed to have an instance in baselist of VIRTUAL
// its protection level indicates actual protection of class
// error('d',"is_base: level: %d, b->base: %k bc: %s ppbase:
// %k",level,b->base,bc->string,ppbase);
if (ppaccum == PUBLIC) {
if (pptr)
*pptr = ppaccum;
if (level == 0)
ppbase = ppaccum;
return bc;
}
// if (level==0 && Nvirt == VIRTUAL)
// { res=bc; continue; }
// return bc;
}
}
}
ppbase = pp;
return 0;
}
bit classdef::has_base(Pclass cl, int level, int ccflag)
/*
is "cl" a base of this?
*/
{
// error('d', "%t->has_base( %t ) cc->cot: %t", this, cl, cc->cot?cc->cot:0 );
if (this == 0)
return 0;
static int found = 0;
if (level == 0)
found = 0;
for (Pbcl b = baselist; b; b = b->next) {
// error('d', "has_base: b: %t ppp: %k found: %d", b->bclass, b->ppp, found);
if (::same_class(b->bclass, cl, 1)) {
if (b->ppp != PUBLIC && (ccflag || !::same_class(cc->cot, this))
&& (cc->nof == 0 || this->has_friend(Pfct(cc->nof->tp)) == 0)
&& (cc->cot == 0 || this->has_friend(cc->cot) == 0))
Nvis = b->ppp; // no standard coercion
Nvirt = b->base;
// error('d',"has_base: nvirt: %k nvis: %d", Nvirt,Nvis);
if (level == 0 && b->base == VIRTUAL && found == 1) {
// this version of Nvis wins out!
if (b->ppp != Nvis)
Nvis = b->ppp == PUBLIC ? 0 : b->ppp;
}
return 1;
}
if (b->bclass->has_base(cl, level + 1, ccflag)) {
// force it to find instance within derived baselist -- at end
if (level == 0 && Nvirt == VIRTUAL) {
found = 1;
continue;
}
return 1;
}
}
return 0;
}
int Noffset;
Pexpr Nptr;
char *Nalloc_base;
clist *vcllist;
int clist::onlist(Pclass c) {
for (clist *p = this; p; p = p->next)
if (same_class(p->cl, c))
return 1;
return 0;
}
void clist::clear() {
if (this == 0)
return;
clist *p = next;
while (p) {
clist *q = p->next;
delete p;
p = q;
};
delete this;
}
Pbcl Nvbc_alloc;
int is_unique_base(Pclass cl, char *s, int offset, int in_base, Pclass priSeen)
/*
is "s" a unique base class of this?
*/
{
int i = 0;
// error('d',"is_unique_base(cl: %t, s: %s,%d,%d)",cl,s,offset,in_base);
for (Pbcl b = cl->baselist; b; b = b->next) {
int no = 0;
if (b->base != VIRTUAL)
no = offset + b->obj_offset;
else if (in_base)
continue;
if (strcmp(s, b->bclass->string) == 0) {
Noffset = no;
i++;
if ((b->ppp != PUBLIC || priSeen)
&& (!same_class(cc->cot, cl) || !same_class(cc->cot, priSeen))
&& (cc->nof == 0 || cl->has_friend(Pfct(cc->nof->tp)) == 0)
|| (priSeen && priSeen->has_friend(Pfct(cc->nof->tp)) == 0))
Nvis = b->ppp; // no standard coercion
if (b->base == VIRTUAL) {
Nptr = new mdot(s, 0);
// if (b->allocated == 0) {
if (b->allocated != 1) {
Nvbc_alloc = 0;
Nalloc_base = cl->has_allocated_base(s);
}
}
} else {
if (b->base == VIRTUAL) {
if (vcllist->onlist(b->bclass))
continue;
vcllist = new clist(b->bclass, vcllist);
}
Pclass clscope = 0;
if (cc && cc->c_this) {
Ptype t = Pptr(cc->c_this->tp)->typ;
clscope = Pclass(Pbase(t)->b_name->tp);
}
// error('d', "cl: %t %d clscope: %t %d", cl, cl, clscope, clscope);
Pclass new_priSeen = priSeen;
if (b->ppp != PUBLIC && !same_class(cl, clscope) && priSeen == 0)
new_priSeen = cl;
int ii = is_unique_base(b->bclass, s, no, 1, new_priSeen);
// error('d',"base %t i %d ii %d",b->bclass,i,ii);
// error('d',"base %t %k allocated: %d", b->bclass, b->base, b->allocated);
i += ii;
if (ii == 1 && b->base == VIRTUAL) {
Nptr = new mdot(b->bclass->string, 0);
// if (b->allocated == 0) {
if (b->allocated != 1) {
Nvbc_alloc = 0;
Nalloc_base = cl->has_allocated_base(b->bclass->string);
}
}
}
}
return i;
}
/*
int classdef::has_allocated_base(Pclass bcl)
search the list of !first base classes for this virtual base
space will have been allocated in !first bases for virtual bases
declared in !first classes
in addition bcl may bave been specified explicitly as a base
{
int off;
for (Pbcl l = baselist; l; l=l->next) {
if (l->base == VIRTUAL) continue; // another non-allocated virtual base
if (l==baselist) continue; // first base
Pclass bc = l->bclass;
off = l->obj_offset;
for (Pbcl ll = bc->baselist; ll; ll=ll->next) {
// cannot share non-virtual base
if (ll->base != VIRTUAL) continue;
if (same_class(ll->bclass,bcl)) return off + ll->obj_offset;
}
}
return 0;
}
*/
int link_compat_hack = 0;
int classdef::has_allocated_base(Pclass bcl, bit first)
/*
search the list of base classes for this virtual base
space will be allocated in first virtual version found.
return offset.
A virtual base cannot have offset 0 (its pointer at least is ahead)
*/
{
// error('d',"%t->has_allocated_base(%t,%d) ",this,bcl,first);
for (Pbcl l = baselist; l; l = l->next) {
// if (l->base==VIRTUAL && l->bclass==bcl && l->obj_offset) return l->obj_offset;
if (l->base == VIRTUAL && ::same_class(l->bclass, bcl)) {
if (l->obj_offset && first == 0)
return l->obj_offset;
for (Pbcl ll = baselist; ll != l; ll = ll->next) {
if (ll->base == VIRTUAL)
continue;
int i = ll->bclass->has_allocated_base(bcl, ll == baselist);
// if (i) return i;
if (i) {
link_compat_hack = 1;
// error('d',"%t->has_allocated_base(%t %d) link_compat_hack set",this,bcl,first);
return 0;
}
}
}
if (l->base == VIRTUAL || l != baselist) {
// allocated as an object,
// not unravelled as a set of members
int i = l->bclass->has_allocated_base(bcl);
if (i)
return l->obj_offset + i;
}
}
return 0;
}
extern bit Vvtab;
extern bit Vvbc_inher;
extern bit Vvbc_alloc;
char *classdef::has_allocated_base(char *str)
/*
* str is an unallocated virtual base class of this
* return the name of the second or subsequent base class
* containing the member ``struct str *P<str>''
*/
{
// error('d',"%t::has_allocated_base(%s %d) baselist: %t",this,str,baselist->bclass);
for (Pbcl l = baselist; l; l = l->next) {
if (l->base == VIRTUAL) {
// link_compat_hack sets allocated to 2
// if ( l->allocated )
if (l->allocated == 1)
Nvbc_alloc = l;
else if (strcmp(str, l->bclass->string) == 0) {
// error('d',"has_allocated_base(%s): found itself as virtual",str);
Vvbc_inher = 1;
return 0;
}
continue;
}
Pclass bc = l->bclass;
for (Pbcl ll = bc->baselist; ll; ll = ll->next) {
if (ll->base != VIRTUAL)
continue;
// if (ll->allocated &&
if (ll->allocated == 1 && strcmp(str, ll->bclass->string) == 0) {
if (::same_class(bc, baselist->bclass)) {
Vvtab = ll->bclass->has_vvtab;
return 0;
}
return bc->string;
}
}
}
if (Nvbc_alloc == 0)
Vvbc_alloc = 1;
return 0;
}
/*
int allocated_base(Pclass cl,Pclass bcl)
{
static second;
int s2 = second;
for (Pbcl l = cl->baselist; l; l=l->next) {
if (l->base==VIRTUAL
&& same_class(l->bclass,bcl)
&& l->obj_offset
&& (second || l!=cl->baselist)) return (second=s2,1);
int i = allocated_base(l->bclass,bcl);
if (i) return (second=s2,1);
second = 1;
}
second = s2;
return 0;
}
*/
Pname vfct(Pclass cl, char *s)
/*
Called for each name "s" in a vtbl for "cl"
Find the "s" to go in the vtbl.
The "s" that caused the vtbl entry to be created
is found if nothing else is
*/
{
Pname n = cl->memtbl->look(s, 0);
if (n)
return n;
for (Pbcl b = cl->baselist; b; b = b->next) {
Pname nn = vfct(b->bclass, s);
if (nn) {
// error('d',"nn%n",nn);
if (n && n != nn) {
Pclass ncl = Pclass(n->n_table->t_name->tp);
Pclass nncl = Pclass(nn->n_table->t_name->tp);
// error('d',"ncl %t nncl %t",ncl,nncl);
if (nncl->is_base(ncl->string))
n = nn; // use nn
} else
n = nn;
}
}
return n;
}
Pexpr rptr(Ptype t, Pexpr p, int offset)
/*
return rvalue of offset off pointer:
(t)((char*)p+offset)
*/
{
if (t == 0)
error('i', "rptr(), t==0 (type passed for cast)");
Pexpr pp = p;
// error('d',"rptr %t %d",t,offset);
if (offset) {