forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcl3.c
2868 lines (2613 loc) · 95 KB
/
dcl3.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/dcl3.c 1.30" */
/*******************************************************************************
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) 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.
*******************************************************************************/
#include "cfront.h"
#include "size.h"
#include "template.h"
#include <ctype.h>
Pblock top_block;
int New_in_arg_list = 0;
// check for 0 as ptr to mem initializer
static int is_zero(Pexpr e) {
if (!e)
return 0;
if (e->base == ZERO)
return 1;
if (e->base != CAST && e->base != G_CAST)
return 0;
return e->e1->base == ZERO;
}
static void vbase_pointers(Pname fn, Pclass cl)
/*
insert argument for virtual base pointers (if any)
after f_this and before f_argtype
*/
{
// error('d',"vbase_pointers(%n,%t) %d %k",fn,cl,fn->tp,fn->n_oper);
Pfct f = Pfct(fn->tp);
if (fn->n_oper == CTOR) {
Pname d = 0;
for (Pbcl b = cl->baselist; b; b = b->next) {
if (b->base != VIRTUAL)
continue;
Pname a = new name(b->bclass->string);
a->tp = b->bclass->this_type;
a->n_list = d;
a->n_table = f->body ? f->body->memtbl : 0;
a->where = fn->where;
d = a;
}
if (d) {
for (Pname dd = d;;) {
if (d->n_list == 0) {
d->n_list = f->f_args->n_list;
break;
}
d = d->n_list;
}
f->f_args->n_list = dd;
}
}
if (fn->n_oper == DTOR) { // add __free argument
// error('d',"add __free to%n",fn);
Pname fa = new name;
fa->tp = int_type;
fa->n_scope = ARG;
fa->where = fn->where;
Pname a = f->f_args;
if (a == 0)
f->f_args = fa;
else {
for (;; a = a->n_list) {
// error('d',"a %d %t",a,a->tp);
if (a->n_list == 0) {
a->n_list = fa;
break;
}
}
}
}
}
void make_res(Pfct f)
/*
returns X where X(X&) has been declared
add "_result" argument of type X*
*/
{
Pname cl = f->returns->is_cl_obj();
if (cl == 0 || Pclass(cl->tp)->has_itor() == 0)
return;
Pname rv = new name("_result");
rv->tp = f->returns->addrof();
PERM(rv);
PERM(rv->tp);
rv->n_scope = FCT; // not a ``real'' argument
rv->n_used = 1;
rv->n_list = f->argtype;
if (f->f_this)
f->f_this->n_list = rv;
else
f->f_args = rv;
f->f_result = rv;
f->s_returns = void_type;
}
void name::check_oper(Pname cn)
/*
check declarations of operators, ctors, dtors
*/
{
DB(if (Ddebug >= 2) error('d', &where, "%n->check_oper(%n ): n_oper:%k", this, cn, n_oper););
switch (n_oper) {
case CALL:
case DEREF:
case REF:
if (cn == 0)
error("operator%s must be aM", keys[n_oper]);
break;
case ASPLUS:
case ASMINUS:
case ASMUL:
case ASDIV:
case ASMOD:
case ASAND:
case ASOR:
case ASER:
case ASLS:
case ASRS:
if (warning_opt) {
if (cn == 0 || Pfct(tp)->f_static)
error('w', "operator%s should be a non-staticMF", keys[n_oper]);
}
break;
case ASSIGN:
if (cn == 0)
error("non-member operator%k()", n_oper);
break;
case LT:
case LE:
case GT:
case GE:
case EQ:
case NE:
if (cn && Pfct(tp)->f_static) {
error("operator%s cannot be static", keys[n_oper]);
Pfct(tp)->f_static = 0;
}
break;
case NOT: /* unary operators only */
case COMPL: {
Pfct f = Pfct(tp);
if (cn && f->argtype)
error("%n::%n takes noAs", cn, this);
else if (f->nargs == 2)
error("%n takes 1A only", this);
if (cn && Pfct(tp)->f_static) {
error("operator%s cannot be static", keys[n_oper]);
Pfct(tp)->f_static = 0;
}
break;
}
case INCR:
case DECR:
// check for postscript instance
{
Pfct f = Pfct(tp);
if (cn) { // member
if (f->argtype && f->nargs == 1) {
Pname n = f->argtype;
Ptype at = n->tp->skiptypedefs();
if (at->base != INT || Pbase(at)->b_unsigned)
error("%n must takeA ofT int, not %t", this, n->tp);
}
} else if (f->nargs == 2) { // non-member
Pname n = f->argtype->n_list;
Ptype at = n->tp->skiptypedefs();
if (at->base != INT || Pbase(at)->b_unsigned)
error("%n must takeA ofT int, not %t", this, n->tp);
}
break;
}
case 0:
case TNAME: // may be a constructor
if (cn
&& (strcmp(cn->string, string) == 0
|| Pclass(cn->tp)->class_base == INSTANTIATED
&& strcmp(string, Ptclass(cn->tp)->unparametrized_tname()->string) == 0)) {
if (tp->base == FCT) {
Pfct f = Pfct(tp);
if (f->returns != defa_type)
error("%s::%s()W returnT", string, string);
f->returns = void_type;
string = "__ct";
n_oper = CTOR;
} else
error('s', "struct%nM%n", cn, cn);
} else
n_oper = 0;
break;
case DTOR: /* must be a destructor */
// error('d',"dtor %s",string);
if (cn == 0) {
n_oper = 0;
error("destructor ~%s() not inC", string);
} else if (strcmp(cn->string, string) == 0
|| Pclass(cn->tp)->class_base == INSTANTIATED
&& strcmp(string, Ptclass(cn->tp)->unparametrized_tname()->string) == 0) {
dto:
Pfct f = (Pfct) tp;
string = "__dt";
if (tp->base != FCT) {
error("%s::~%s notF", cn->string, cn->string);
tp = new fct(void_type, 0, 1);
} else if (f->returns != defa_type) {
if (f->returns != void_type || f->body != 0 || friend_in_class == 0)
error("%s::~%s()W returnT", cn->string, cn->string);
}
if (f->argtype || f->nargs_known == ELLIPSIS) {
error("%s::~%s()WAs", cn->string, cn->string);
f->nargs = 0;
f->nargs_known = 1;
f->argtype = 0;
}
f->returns = void_type;
} else {
if (strcmp(string, "__dt") == 0)
goto dto;
error("~%s in %s", string, cn->string);
n_oper = 0;
}
break;
case TYPE: // cond stores the type of the operator function
// error('d',"this%n cn%n cond%t tp%t",this,cn,cond,tp);
if (cn == 0) {
error("operator%t() not aM", Ptype(cond));
n_oper = 0;
cond = 0;
} else // note: could be called twice in friend declaration
// -- only process once
if (cond) {
Pfct f = Pfct(tp);
Ptype tx = Ptype(cond);
cond = 0;
if (tx->skiptypedefs()->base == FCT)
error("badT for%n::operator%t() - cannot return aF", cn, tx);
if (f->base != FCT)
error("badT for%n::operator%t()", cn, tx);
if (f->returns != defa_type) {
error("resultT for%n::operator%t()", cn, tx);
DEL(f->returns);
}
if (f->argtype || f->nargs_known == ELLIPSIS) {
error("%n::operator%t()WAs", cn, tx);
f->argtype = 0;
}
f->returns = tx;
char buf[1024];
char *bb = tx->signature(buf);
int l2 = bb - buf;
if (1023 < l2)
error('i', "N::check_oper():N buffer overflow");
char *p = new char[l2 + 5];
p[0] = '_';
p[1] = '_';
p[2] = 'o';
p[3] = 'p';
strcpy(p + 4, buf);
string = p;
}
break;
}
}
Pexpr vbase_args(Pfct a, Pname bn)
/*
constructor a calls the constructor bn for a base class
generate argument list needed for virtual base arguments
*/
{
Pfct b = Pfct(bn->tp);
// error('d',"vbase_args%n: %t %k",bn,b,b->base);
Pexpr args = 0;
Pexpr tail = 0;
if (b->base == OVERLOAD)
b = Pfct(Pgen(b)->fct_list->f->tp); // doesn't matter which
for (Pname d = b->f_args->n_list; d != b->argtype; d = d->n_list) {
for (Pname dd = a->f_args->n_list; dd; dd = dd->n_list)
if (strcmp(dd->string, d->string) == 0) // using strcmp is a trick
break;
Pexpr aa = new expr(ELIST, dd, 0);
if (args == 0)
args = aa;
else
tail->e2 = aa;
tail = aa;
}
return args;
}
void fct::init_bases(Pclass cl, Pexpr)
/*
in "cl"'s constructor "this" generate code to initialize base classes
and members using the initializers "f->f_init"
this->f_init == list of names of classes to be initialized
COLON(b) => base class b
=> constructor call in f_init->n_initializer
COLON() => unnamed base class
=> constructor call in f_init->n_initializer
NAME(m) => member m
=> constructor call in m->n_initializer
*/
{
Ptable ftbl = body->memtbl;
DB(if (Ddebug >= 1) error('d', "init_bases %t init %d", cl, f_init););
// explicit initializers
if (cl && cl->csu == UNION && f_init && f_init->n_list)
error(&f_init->where, "multipleIrs in unionK %s:: %s", cl->string, cl->string);
for (Pname nx, nn = f_init; nn; delete nn, (nn = nx)) {
Pexpr i = nn->n_initializer;
nn->n_initializer = 0;
nx = nn->n_list;
// error('d',"init_base %s %d",nn->string,i);
if (nn->string) {
// lookup in case type name hides a "real" member
{
Pname mmm = cl->memtbl->look(nn->string, 0);
if (mmm)
nn->base = mmm->base;
}
if (nn->base == TNAME) { // base class
char *bn;
while (nn->tp && nn->tp->base == TYPE)
nn->tp = Pbase(nn->tp)->b_name->tp;
if (nn->tp && nn->tp->base == COBJ)
bn = Pbase(nn->tp)->b_name->string;
else
bn = nn->string;
for (Pbcl l = cl->baselist; l; l = l->next) {
Pclass bcl = l->bclass;
if (strcmp(bcl->string, bn) == 0
|| bcl->class_base == INSTANTIATED
&& strcmp(Ptclass(bcl)->inst->def->classtype()->string, bn) == 0) {
// l->init is zeroed out in ctor_simpl
// if error_count, simpl() not invoked
if (l->init && error_count == 0)
error("twoIrs for%t", bcl);
else
l->init = base_init(bcl, i, ftbl, l->obj_offset);
goto con;
}
}
if (nn->base == TNAME && !nn->tpdef
&& (!nn->tp || (nn->tp->base != COBJ && nn->tp->base != EOBJ)))
error(&nn->where, "unexpectedAL: noBC %s", nn->string);
else
error(&nn->where, "unexpectedAL: noBC%n", nn);
con:
continue;
} else { // member initializer
Pname m = cl->memtbl->look(nn->string, 0);
if (m && m->n_table == cl->memtbl)
m->n_initializer = mem_init(m, i, ftbl);
else
error(&nn->where, "%n not inC %s", nn, cl->string);
}
} else { // unnamed base class
Pbcl l = cl->baselist;
if (l == 0) {
error("unexpectedAL: noBC called");
continue;
}
if (l->next) {
bit cnt = 0, rvb = 0; // remote virtual base classes
for (Pbcl ll = l; ll; ll = ll->next, ++cnt)
if (ll->base == VIRTUAL && ll->promoted)
++rvb;
if (rvb)
error("unnamedBCIr: %dBCes(%d non-explicit virtualBC%s)", cnt, rvb,
rvb == 1 ? "" : "es");
else
error("unnamedBCIr: %dBCes", cnt);
continue;
}
if (l->init)
error("twoIrs for%t", l->bclass);
else {
error(strict_opt ? 0 : 'w', &nn->where, "N ofBC%t missing from BCIr (anachronism)",
l->bclass);
l->init = base_init(l->bclass, i, ftbl, l->obj_offset);
}
}
} // for
for (Pbcl l = cl->baselist; l; l = l->next) {
// default initialization of base classes
Pname ctor;
Pclass bcl = l->bclass;
if (l->init == 0 && (ctor = bcl->has_ctor()))
l->init = base_init(bcl, 0, ftbl, l->obj_offset);
}
}
int inline_restr; /* report use of constructs that the inline expanded cannot handle here */
void fct::dcl(Pname n) {
int nmem = TBLSIZE;
Pname a;
Pname ll;
Ptable ftbl;
int const_old = const_save;
int bit_old = bit_offset;
int byte_old = byte_offset;
int max_old = max_align;
if (base != FCT)
error('i', "F::dcl(%d)", base);
if (body == 0)
error('i', "F::dcl(body=%d)", body);
if (n == 0 || n->base != NAME)
error('i', "F::dcl(N=%d %d)", n, (n) ? n->base : 0);
DB(if (Ddebug >= 1)
error('d', &n->where, "fct::dcl(%n) %k %d %t", n, n->n_scope, body->own_tbl, this););
if (body->own_tbl)
return; // done already
if (body->memtbl == 0)
body->memtbl = new table(nmem + 3, gtbl, 0);
body->own_tbl = 1;
ftbl = body->memtbl;
ftbl->real_block = body;
extern void for_check_delete();
for_check_delete();
max_align = 0; // AL_FRAME;
bit_offset = 0;
cc->stack();
cc->nof = n;
cc->ftbl = ftbl;
switch (n->n_scope) {
case 0:
case PUBLIC: {
cc->not = n->n_table->t_name;
cc->cot = Pclass(cc->not ->tp);
cc->tot = cc->cot->this_type;
if (f_this)
f_this->n_table = ftbl; // fake for inline printout
cc->c_this = f_this;
Pclass cl = Pclass(cc->not ->tp);
if (cl->c_body == 3 && n->n_initializer == 0 && n->n_sto != STATIC && f_inline == 0
&& f_imeasure == 0 && f_virtual != 0) { // could be the function where we need to
// output the vtbl
int i;
for (Pname nn = cl->memtbl->get_mem(i = 1); nn; NEXT_NAME(cl->memtbl, nn, i)) {
if (nn->base == TNAME)
continue;
Ptype t = nn->tp;
if (t == 0)
continue;
switch (t->base) {
case FCT:
if (nn == n)
goto prnt;
if (nn->n_initializer || nn->n_sto == STATIC || Pfct(nn->tp)->f_inline
|| Pfct(nn->tp)->f_imeasure || Pfct(nn->tp)->f_virtual == 0)
break;
goto zaq;
case OVERLOAD: {
for (Plist gl = Pgen(t)->fct_list; gl; gl = gl->l) {
Pname nn = gl->f;
if (nn == n)
goto prnt;
if (nn->n_initializer || nn->n_sto == STATIC || Pfct(nn->tp)->f_inline
|| Pfct(nn->tp)->f_imeasure || Pfct(nn->tp)->f_virtual == 0)
continue;
goto zaq;
}
}
}
}
goto zaq;
prnt:
cl->print_all_vtbls(cl);
goto zaq;
}
}
}
zaq: // protect against: class x; x f(); class x { x(x&); ....
if (f_result == 0)
make_res(this);
if (f_result)
f_result->n_table = ftbl; // fake for inline printout
returns->tsizeof(); // make sure size is known
Pname ax;
for (a = argtype, ll = 0; a; a = ax) {
ax = a->n_list;
Pname nn = a->dcl(ftbl, ARG);
++nn->lex_level;
Pname cn = nn->tp->is_cl_obj();
if (cn == 0)
cn = cl_obj_vec;
if (cn)
(void) cn->tp->tsizeof(); // make sure it is printed
nn->n_assigned_to = nn->n_used = nn->n_addr_taken = 0;
nn->n_list = 0;
switch (nn->tp->base) {
case CLASS:
case ENUM: /* unlink types declared in arg list */
nn->dcl_print(0);
break;
default:
if (ll)
ll->n_list = nn;
else
f_args = argtype = nn;
ll = nn;
}
delete a;
}
if (f_result) { // link in f_result
f_args = f_result;
f_result->n_list = argtype;
}
if (f_this) { // link in f_this
if (body)
f_this->where.line = n->where.line;
f_args = f_this;
f_this->n_list = f_result ? f_result : argtype;
}
if (n->n_oper == CTOR || n->n_oper == DTOR)
vbase_pointers(n, cc->cot);
if (n->n_oper == CTOR) {
const_save = 1;
init_bases(cc->cot, f_init);
} else if (f_init)
error(&f_init->where, "unexpectedAL: not aK");
PERM(returns);
const_save = f_inline && debug_opt == 0;
inline_restr = 0;
top_block = body;
if (se_opt && (n->inst_body() == 0 || n->finst_body() == 0))
suppress_error++;
body->dcl(ftbl);
if (se_opt && (n->inst_body() == 0 || n->finst_body() == 0))
suppress_error--;
defined |= DEFINED;
if (f_inline && inline_restr && returns->base != VOID) {
f_inline = 0;
const char *s =
(inline_restr & 32)
? "continue"
: (inline_restr & 16)
? "break"
: (inline_restr & 8)
? "loop"
: (inline_restr & 4)
? "switch"
: (inline_restr & 2) ? "goto" : (inline_restr & 1) ? "label" : "";
if (warning_opt) {
error('w', "\"inline\" ignored,%n contains %s", n, s);
error('w', "out-of-line copy of%n created", n);
}
n->simpl(); // BS6
if (cc->cot && cc->cot->class_base == INSTANTIATED && cc->cot->c_body == 1) {
current_instantiation = cc->cot;
cc->not ->dcl_print(0);
current_instantiation = 0;
}
n->dcl_print(0);
}
const_save = const_old;
if (f_inline && debug_opt == 0) {
if (dtpt_opt && n->inst_body() == 0) {
f_inline = 0;
n->fct_type()->body = 0;
}
isf_list = new name_list(n, isf_list);
}
bit_offset = bit_old;
byte_offset = byte_old;
max_align = max_old;
cc->unstack();
// error('d',"fct-> returns %t",returns);
}
Pexpr this_handler = 0;
Pexpr fct::base_init(Pclass bcl, Pexpr i, Ptable ftbl, int offset)
/*
have base class bcl and expr list i
return "( *(base*)this ) . ctor( i )"
ctor call generated in expr.typ()
*/
{
Ptype ty = bcl->this_type;
Pexpr th = rptr(ty, f_this, offset); // base*
Pname ctor = bcl->has_ctor();
// error('d',"fct::B_init(C %t, i %d, %d) ctor%n",bcl,i,i?i->tp:0,ctor);
Pexpr ii = (i && i->base == ELIST) ? i->e1 : i;
if (ii && ii->base == DEREF && (ii->e1->base == CAST || ii->e1->base == G_CAST)
&& (th->base == CAST || th->base == G_CAST))
th->i2 = ii->e1->i2;
if (ctor == 0) {
if (i && i->base != ELIST)
i = new expr(ELIST, i, 0);
Pexpr v = new texpr(VALUE, bcl, i); // ?.base(i)
v->e2 = new expr(DEREF, th, 0); // (*(base*)this).base(i)
v = v->typ(ftbl); // *base(&*(base*)this,i)
// error('d',"v %k",v->base);
switch (v->base) {
case DEREF:
return v->e1; // base(&*(base*)this,i)
case ASSIGN: // degenerate base(base&): *(base*)this=i
th = new texpr(G_CAST, ty, f_this);
v = new expr(CM, v, th); // (*(base*)this=i,(base*)this);
return v->typ(ftbl);
default:
return 0;
}
}
Pname icn;
if (i) {
int na = (i->base != ELIST ? 1 : (i->e2 != 0 ? 2 : 1));
ii = ii->typ(ftbl);
if (bcl->has_itor() == 0 && (icn = ii->tp->is_cl_obj()) && na == 1
&& (same_class(Pclass(icn->tp), bcl) || Pclass(icn->tp)->has_base(bcl))) {
// degenerate base(base&): *(base*)this=i
// memberwise copy
// error('d',"copy %t",ty);
this_handler = th;
th = new texpr(G_CAST, ty, f_this);
th = th->contents();
th = th->typ(ftbl);
if (!same_class(Pclass(icn->tp), bcl)) { // cast needed
Pptr r = new ptr(RPTR, Pptr(ty)->typ);
ii = new texpr(G_CAST, r, ii);
ii = ii->typ(ftbl);
}
ii = new expr(ASSIGN, th, ii);
ii->tp = th->tp;
// simulate `return this':
// *(base*)this=i,(base*)this
// ii = new expr(CM,ii,new cast(ty,f_this));
// ii->tp = th->tp;
ii = new expr(CM, ii, this_handler);
this_handler = 0;
ii->tp = ty;
return ii;
}
if (i->base == ELIST)
i->e1 = ii;
}
// error('d',"call%n%t -> %d%k",ctor,ctor->tp,x,x->base);
return call_ctor(ftbl, th, ctor, i, REF, vbase_args(this, ctor));
}
Pexpr fct::mem_init(Pname mn, Pexpr i, Ptable ftbl)
/*
return "member_ctor( m, i )"
*/
{
// a new entry for B::B_pub, in general, has no tp and no
// real info: all the tp-> only work on our systems because
// 0 pointer dereference isn't system memory. it core dumps
// in set_const since no test is made on this == 0.
// error('d',"mem_init%n",mn);
// if (mn->n_assigned_to) // Disallow multiple member initializations
// error("%n multiply initialized",mn);
switch (mn->n_stclass) {
case STATIC:
error("MIr for static%n", mn);
break;
case ENUM:
error("MIr for enumeration constant%n", mn);
break;
}
Pname member = (mn->base == PUBLIC && mn->n_qualifier) ? mn->n_qualifier : mn;
if (i)
i = i->typ(ftbl);
Pname cn = member->tp->is_cl_obj(); // first find the class name
Pref tn = new ref(REF, f_this, member);
tn->tp = member->tp;
// error('d',"MI for%n%t =%t",member,member->tp,i?i->tp:0);
// error('d',"fthis %d %t member%n tp%t",f_this,f_this->tp,member,tn->tp);
if (cn) {
Pclass mcl = Pclass(cn->tp); // then find the classdef
Pname ctor = mcl->has_ctor();
Pname icn;
if (i && mcl->has_itor() == 0 && (icn = i->tp->is_cl_obj())
&& same_class(Pclass(icn->tp), mcl)) { // bitwise copy
Pexpr init = new expr(ASSIGN, tn, i);
init->tp = tn->tp;
member->assign();
return init;
}
if (ctor)
return call_ctor(ftbl, tn, ctor, i, DOT);
error("Ir forM%nW noK", member);
return 0;
}
if (cl_obj_vec) {
if (i && i->base == ELIST)
error("illegalIrL for %t%nWinM initializationL", mn->tp, mn);
else
error('s', "Ir forCM %t%nWK", mn->tp, mn);
return 0;
}
if (i && i->base == ELIST) {
if (i->e2)
error("Ir for%n not a simpleE", member);
i = i->e1;
}
if (member->tp->is_ref() && (i == 0)) {
error("emptyIr for reference%n", member);
return 0;
}
// error( 'd', "fct_mem_init:%n%k", member, member->tp->base );
switch (member->tp->base) {
case VEC:
case FCT:
case OVERLOAD:
error("Ir for%n ofT %t", member, member->tp);
return 0;
}
// error('d',"tp %t",member->tp);
if (member->tp->tconst()) {
ignore_const++;
i = new expr(ASSIGN, tn, i);
i = i->typ(ftbl);
ignore_const--;
return i;
}
Pptr pt;
if (pt = member->tp->is_ref()) {
switch (pt->typ->base) {
case FCT:
case OVERLOAD:
i = ptr_init(pt, i, ftbl);
break;
default:
i = ref_init(pt, i, ftbl);
}
i = new expr(ASSIGN, tn, i);
i->tp = tn->tp;
member->assign(); // cannot call typ: would cause dereference
return i;
}
i = new expr(ASSIGN, tn, i);
return i->typ(ftbl); // typ performs the type check on the assignment
}
Pexpr replace_temp(Pexpr e, Pexpr n)
/*
e is on the form
f(&temp,arg) , temp
or
&temp->ctor(arg) , temp
or
x->f(&temp,arg) , temp
change it to
f(n,arg)
or
n->ctor(arg)
*/
{
Pexpr c = e->e1; // f(&temp,arg) or &temp->ctor(args)
Pexpr ff = c->e1;
Pexpr a = c->e2; // maybe ELIST(&temp,arg)
Pexpr tmp = e->e2;
// error('d',"suppress(%d%k)%n",tmp->base,tmp->base,tmp->base==NAME?tmp:0);
if (tmp->base == DEREF)
tmp = tmp->e1;
if (tmp->base == CAST || tmp->base == G_CAST)
tmp = tmp->e1;
if (tmp->base == ADDROF || tmp->base == G_ADDROF)
tmp = tmp->e2;
if (tmp->base != NAME)
return e; // error('i',"replace %k",tmp->base);
tmp->tp = any_type; // temporary not used: suppress it
// error('d',"replace_temp(%k %k) c %k ff %k",e->base,n->base,c->base,ff->base);
switch (ff->base) {
case REF:
if (ff->e1->base == G_ADDROF && ff->e1->e2 == tmp)
a = ff; // &tmp -> f()
break;
case DOT:
if (ff->e1->base == NAME && ff->e1 == tmp) {
a = ff; // tmp . f()
a->base = REF;
}
break;
}
a->e1 = n;
return c;
}
Pname classdef::has_ictor()
/*
does this class have a constructor taking no arguments?
*/
{
Pname c = has_ctor();
if (c == 0)
return 0;
Pfct f = Pfct(c->tp);
switch (f->base) {
default:
error('i', "%s: badK (%k)", string, c->tp->base);
case FCT:
switch (f->nargs) {
case 0:
return c;
default:
if (f->argtype->n_initializer)
return c;
}
return 0;
case OVERLOAD: {
for (Plist l = Pgen(f)->fct_list; l; l = l->l) {
Pname n = l->f;
f = (Pfct) n->tp;
switch (f->nargs) {
case 0:
return n;
default:
if (f->argtype->n_initializer)
return n;
}
}
return 0;
}
}
}
int add_first; // fudge, use ctor arg instead
static int add_without_find; // specialized instance
Pname gen::add(Pname n)
/*
add "n" to the tail of "fct_list"
(overloaded names are searched in declaration order)
detect: multiple identical declarations
declaration after use
multiple definitions
*/
{
Pfct f = Pfct(n->tp);
Pname nx;
// error('d',"add(%n) %d",n,add_first);
if (f->base != FCT)
error("%n: overloaded nonF", n);
// holds_templ keeps track of fct_list:
// pure_templ: no attempt at argument resolution
// some_temp: try, but skip each template instance
// no_templ: ordinary vanilla match
if (f->is_templ()) {
if (!has_templ()) {
if (fct_list)
holds_templ = some_templ;
else
holds_templ = all_templ;
}
} else {
if (pure_templ())
holds_templ = some_templ;
}
// error('d',"gen::add(%n) is_templ: %d holds_templ: %d", n,f->is_templ(),holds_templ);
if (fct_list && add_without_find == 0 && (nx = find(f, 1))) {
// error('d',"gen::add: found%n%t",nx,nx->tp);
Linkage l1 = Pfct(nx->tp)->f_linkage;
Linkage l2 = f->f_linkage;
if (l2 != linkage_default && l1 != l2)
error("inconsistent linkage specifications for%n", n);
Nold = 1;
} else {
if (add_first == 0 && f->f_signature == 0 && f->fct_base != FCT_TEMPLATE)
f->sign();
// error('d',"signature: %d \"%s\" fct_list %d",f->f_signature,f->f_signature,fct_list);
nx = new name;
*nx = *n;
nx->n_gen_fct_name = n->string;
PERM(nx);
Nold = 0;
if (fct_list) {
int clink = (f->f_linkage == linkage_C);
Plist gl = fct_list;
for (;;) {
if (clink && Pfct(gl->f->tp)->f_linkage == linkage_C) {
error("two%ns with c linkage", n);
if (f->f_signature == 0)
f->sign();
}
if (gl->l)
gl = gl->l;
else
break;
}
gl->l = new name_list(nx, 0);
} else
fct_list = new name_list(nx, 0);
nx->n_list = 0;
}
return nx;
}
void fct::sign() {
switch (f_linkage) {
case linkage_C:
f_signature = "";
return;
case linkage_Cplusplus:
case linkage_default:
break;
}
char buf[1024];
char *bb = signature(buf);
int ll = bb - buf;
if (1023 < ll)
error('i', "gen::add():N buffer overflow");
char *p = new char[ll + 1];
strcpy(p, buf);
f_signature = p;
// error('d',"fct::sign %s",p);
}
Pname gen::find(Pfct f, bit warn) {
for (Plist gl = fct_list; gl; gl = gl->l) {
Pname n = match(gl->f, f, warn);
if (n)
return n;
}