forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.c
4489 lines (3938 loc) · 146 KB
/
template.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/template.c 1.72" */
/*******************************************************************************
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.
*******************************************************************************/
/******************************************************************************
* Copyright (c) 1989 by Object Design, Inc., Burlington, Mass.
* All rights reserved.
*******************************************************************************/
/*******************************************************************
* template.c
*
* TBD
*
* 1) The template copying process could probably be speeded up
* substantially, by only placing "graph-like" nodes in the hash
* table. The current implementation plays it safe, and places
* all nodes in the hash table.
*
* 2) remove `$' in lex.c
*
* 3) Add something to lalex to handle x<y<int>>. Currently parsed
* as a right shift rather than nested template instance.
*********************************************************************/
#include "tree_copy.h"
#include "cfront.h"
#include <string.h>
#include "template.h"
#include <stdlib.h>
#include <ctype.h>
#include <memory.h>
#include "hash.h"
extern int bound; // is not mentioned in the header file
bit tempdcl;
static Ptempl_inst dummyinst = 0;
static Ptempl_inst curr_inst = 0;
static Pfunct_inst fdummyinst = 0;
static Pfunct_inst fcurr_inst = 0;
static bit notinstflag = 0;
// static bit matchflag=0;
Ptable bound_expr_tbl;
const int max_string_size = 1024;
const int default_copy_hash_size = 1000;
const int MAX_INST_DEPTH = 16; // maximum instantiations at one time
// static data member definitions
Pfunt templ_compilation::f_list = 0;
Pbase_inst basic_inst::head = 0;
Ptempl templ_compilation::list = 0;
Pcons templ_compilation::last_cons = 0;
Pcons templ_compilation::last_friend_cons = 0;
Pcons templ_compilation::templ_refs = 0;
Pcons templ_compilation::friend_templ_refs = 0;
Ptstate templ_compilation::save_templ = 0;
Ptempl_base templ_compilation::parsed_template = 0;
Pfunt templ_compilation::f_owner = 0;
Ptempl templ_compilation::owner = 0;
Plist templ_compilation::param_end = 0;
Plist templ_compilation::params = 0;
Pexpr templ_compilation::actuals = 0;
bool templ_compilation::formals_in_progress = false;
// The canonical template compilation instance.
templ_compilation *templp;
Ptable templ_compilation::templates = 0;
// Save and restore global state around a template instantiation
void state::save() {
Cdcl = ::Cdcl;
Cstmt = ::Cstmt;
curloc = ::curloc;
curr_file = ::curr_file;
curr_expr = ::curr_expr;
curr_icall = ::curr_icall;
curr_loop = ::curr_loop;
curr_block = ::curr_block;
curr_switch = ::curr_switch;
bound = ::bound;
inline_restr = ::inline_restr;
last_line = ::last_line;
}
void state::restore() {
::Cdcl = Cdcl;
::Cstmt = Cstmt;
::curloc = curloc;
::curr_file = curr_file;
::curr_expr = curr_expr;
::curr_icall = curr_icall;
::curr_loop = curr_loop;
::curr_block = curr_block;
::curr_switch = curr_switch;
::bound = bound;
::inline_restr = inline_restr;
::last_line = last_line;
}
void state::init() {
::bound = 0;
::inline_restr = 0;
// lastline needs to be initialized probaly via a call to putline
}
#if 0
bit
basetype::parametrized_class() {
return ((base == COBJ) &&
Ptclass(Pbase(this)->b_name->tp)->class_base==UNINSTANTIATED);
}
bit
classdef::parametrized_class() {
return (class_base == UNINSTANTIATED);
}
#endif
Templ_type get_class_base(Pbase b) {
if (b->base != COBJ)
error('i', "::get_class_base: badAT(%k) cobjX", b->base);
return Ptclass(Pbase(b)->b_name->tp)->class_base;
}
Templ_type get_templ_base(Pbase b) {
if (b->base != COBJ)
error('i', "::get_templ_base: badAT(%k) cobjX", b->base);
return Pclass(Pbase(b)->b_name->tp)->templ_base;
}
Ptclass get_template_class(Pbase b) {
Templ_type t = get_class_base(b);
if (!((t == INSTANTIATED) || (t == UNINSTANTIATED)))
error('i', "C is not aYC");
return Ptclass(Pbase(b)->b_name->tp);
}
#if 0
Ptempl_inst
get_templ_inst(Pbase b) {
return (get_template_class(b))->inst;
}
#endif
static bit same_class_templ(Pclass c1, Pclass c2) {
// error('d',"same_class_templ: c1 %s c2 %s c1 %d c2 %d", c1->string, c2->string, c1->class_base,
// c2->class_base);
if (c1 == c2)
return true;
if ((c1->class_base == INSTANTIATED) && (c2->class_base == UNINSTANTIATED)
&& (Ptclass(c1)->inst->def == Ptclass(c2)->inst->def))
return true;
if ((c2->class_base == INSTANTIATED) && (c1->class_base == UNINSTANTIATED)
&& (Ptclass(c1)->inst->def == Ptclass(c2)->inst->def))
return true;
return false;
}
bit classdef::same_class(Pclass pc, int access)
/* Predicate to determine whether two classes are indeed the same.
* cfront normally relies on pointer identity, however, this test
* is insufficient when parametrized class instantiationa are involved,
* since there are potentially many instances of a COBJ and CLASS
* for a given instantiation.
*
* it would seem reasonable to always have the occurrence of
* an UNINSTANTIATED and INSTANTIATED instance be checked;
* this occurs, for example, in
* template<class T> class Tag{};
* template<class T> struct Shrub {
* struct Link { Link* next; };
* int advance(Tag<T>&); // UNINSTANTIATED
* };
*
* void f() {
* Shrub<short> s; Tag<short> t;
* s.advance(t); } // INSTANTIATED -- same class!
*
* however, it seems that same_class also determines whether or not
* an instantiation needs to be done??? and so currently making this
* a blanket check causes certain classes under certain conditions
* not to be instantiated -- if this is truly the case, this should
* be broken into two functions
*/
{
// error('d',"%t->same_class(%t, %d)",this,pc,access);
if (this == 0 || pc == 0)
return false;
if (this == pc)
return true;
if (class_base == VANILLA && pc->class_base == VANILLA) {
if (local_sig && pc->local_sig) {
return strcmp(local_sig, pc->local_sig) == 0;
}
if (nested_sig && pc->nested_sig) {
return strcmp(nested_sig, pc->nested_sig) == 0;
}
if (local_sig == pc->local_sig && nested_sig == pc->nested_sig) {
return strcmp(string, pc->string) == 0;
}
return false;
}
Templ_type this_base = this->class_base;
Templ_type pc_base = pc->class_base;
if ((this_base == CL_TEMPLATE) && (pc_base == INSTANTIATED)
&& (Ptclass(pc)->inst->def_basetype()->b_name->tp == this))
return true;
// The inverse symmetric test
if ((pc_base == CL_TEMPLATE) && (this_base == INSTANTIATED)
&& (Ptclass(this)->inst->def_basetype()->b_name->tp == pc))
return true;
// Check whether the templates were determined to be
// identical after instantiation.
if ((pc_base == INSTANTIATED) && (this_base == INSTANTIATED)
&& (strcmp(pc->string, string) == 0 || (Ptclass(this)->inst->same(Ptclass(pc)->inst))))
return true;
if (access == 0)
return false;
// change to treatment of friend instantiation
// now makes this a possibility
if (this_base == INSTANTIATED && pc_base == UNINSTANTIATED) {
char *str = Ptclass(this)->inst->def->namep->string;
// error('d',"same_class: this: %s pc: %s", str, pc->string);
return (strcmp(str, pc->string) == 0);
}
if (pc_base == INSTANTIATED && this_base == UNINSTANTIATED) {
char *str = Ptclass(pc)->inst->def->namep->string;
// error('d',"same_class: pc: %s this: %s", str, string);
return (strcmp(str, string) == 0);
}
return false;
}
bool templ_inst::same(Ptempl_inst t)
/* determine whether two instantiations are identical;
* test asumes that the templates have been instantiated. */
{
return ((forward && (forward == t->forward)) || (forward == t) || (t->forward == this)) ? true
: false;
}
Ptempl templ_compilation::is_template(Pname p)
/* Determine whether the name refers to the canonical
* template class during syntax analysis */
{
// error('d',"is_template(%n)",p );
if (p->tp && (p->tp->base == COBJ)) {
Templ_type tc = get_class_base(Pbase(p->tp));
Templ_type sc = get_templ_base(Pbase(p->tp));
if (tc == CL_TEMPLATE || (tc == INSTANTIATED && sc == CL_TEMPLATE)) {
Pname n = templates->look(p->string, 0);
return (n ? Ptempl(n->tp) : 0);
} else
return 0;
}
return 0;
}
Ptempl templ_compilation::is_template(char *s) {
// error('d',"is_template(char *%s)",s );
Pname n = templates->look(s, 0);
return (n ? Ptempl(n->tp) : 0);
}
Pfunt templ_compilation::is_template(char *s, TOK t) {
// error('d',"is_template(char *%s, %k)",s,t );
Pname n = templates->look(s, t);
return (n ? Pfunt(n->tp) : 0);
}
void templ_compilation::start()
/* Set up the environment for parsing a template.
* This involves setting up a new nesting level into
* which the "type type" template parameters can be
* entered so that the lexer can find them as TNAMEs.
* templ_compilation::collect adds new types.
*/
{
templp->in_progress = true;
params = param_end = 0;
owner = 0;
f_owner = 0;
parsed_template = 0;
// has_expr_formals = 0;
// SYM modified_tn = 0;
}
void templ_compilation::collect(TOK parm_type, Pname n)
/* Collect each parameter as it is parsed, adding
* it to the list of parms. Validate each parameter */
{
// ::error('d',"templ_compilation::collect(%k,%n)",parm_type,n);
switch (parm_type) {
case STRUCT:
case CLASS:
/* A "type type" parameter ...
* tp: ``any_type'', a wildcard match
* tdef: base==TNAME, inserts in ktbl, sets modified_tn
* lex_level: +1, so restore() can HIDE it after
*/
n->tp = ::any_type;
n = n->tdef();
n->lex_level = bl_level + 1;
n->n_template_arg = template_type_formal;
break;
default:
error("theZT for%n must beC or struct, not%k", n, parm_type);
break;
}
append_parameter(n);
}
void templ_compilation::append_parameter(
Pname n) { // append the "non-type" parameter to the end of the list
// ::error('d',"templ_compilation::append(%n)",n);
if (params) {
param_end->l = new name_list(n, 0);
param_end = param_end->l;
} else
params = param_end = new name_list(n, 0);
PERM(n);
PERM(n->tp);
}
void templ_compilation::collect(Pname n)
/* Collect non "type type" parameters.
* tp indicates type of the formal parameter */
{
// ::error('d',"templ_compilation::collect(%n)",n);
// grammar should be sufficient to protect against undesirable
// types. Any additional checks go here.
n->n_template_arg = template_expr_formal;
extern int must_be_friend;
if (must_be_friend == 0)
bound_expr_tbl->insert(n, 0);
append_parameter(n);
}
static void check_non_type_formal(Pname n) { // validate the type for a non-type formal
// error('d',"check_non_type_formal: %n %k", n, n->tp->base);
Ptype tp = n->tp->skiptypedefs();
switch (tp->base) {
// case W_CHAR:
// case W_STRING:
case FLOAT:
case DOUBLE:
case LDOUBLE:
if (tp->base == LDOUBLE && ansi_opt == 0)
error('w', "long double supported under ``+a1'' option only, generating ``double%n''",
n);
error('s', "ZE ofT float, double, or long double");
return;
case ZTYPE:
case CHAR:
case SHORT:
case INT:
case LONG:
case FIELD:
case EOBJ:
case COBJ:
// case TYPE:
case ANY: { // a basetype node
TOK bad_base = 0;
if (Pbase(n->tp)->b_volatile)
bad_base = VOLATILE;
// ? ?? ??? ???? ?????
// if (Pbase(n->tp)->b_typedef) bad_base=TYPEDEF;
if (Pbase(n->tp)->b_inline)
bad_base = INLINE;
if (Pbase(n->tp)->b_virtual)
bad_base = VIRTUAL;
if (bad_base)
error("bad %k declarator forY formal %n", bad_base, n);
goto hack;
}
case PTR: {
if (tp != n->tp) {
hack:
Pbase b = new basetype(0, 0);
*b = *Pbase(n->tp);
// see const_formal_hack() for explanation
if (b->b_const == 0)
b->b_const = 2;
n->tp = b;
break;
}
Pptr b = new ptr(0, 0);
*b = *Pptr(n->tp);
if (b->b_const == 0)
b->b_const = 2;
n->tp = b;
break;
}
case RPTR:
case VEC:
break; // constant by definition
default:
error("badZT%t for formalZ %n", tp, n);
}
return;
}
void templ_compilation::enter_parameters()
/* The template parameters, if any, have been parsed.
* Member function templates may choose to default their
* template arguments to the class arguments -- if so,
* make the defaulting happen
*/
{
for (Plist list = params; list; list = list->l) {
Pname n = list->f;
switch (n->n_template_arg) {
case template_type_formal:
// SYM -- param should be in parse table...
// Bring the names out of hiding
n->n_key = 0;
break;
case template_expr_formal:
check_non_type_formal(n);
n->tp->dcl(gtbl);
break;
default:
error('i', "bad template formal");
}
}
// SYM -- tn stuff no longer needed
// param_tn = modified_tn ;
// modified_tn = 0 ;
}
void templ::resolve_forward_decl(Plist params, Pclass c)
/* Resolve the forward declaration of a template to its
* true definition. The template and class type data
* structures must be reused, since there may be
* outstanding references to them. */
{
check_formals(params);
formals = params;
defined = true;
members = c->mem_list;
}
static bit reinstat = 0;
/* 3.1/4.0: merge there two */
void templ::instantiate_forward_decl() {
for (Ptempl_inst i = insts; i; i = i->next)
if (Ptclass(Pbase(i->tname->tp)->b_name->tp)->class_base == INSTANTIATED
&& !i->forward) { // reinstantiate it
reinstat = 1;
i->instantiate(true);
reinstat = 0;
}
}
void function_template::instantiate_forward_decl() {
for (Pfunct_inst i = insts; i; i = i->next)
if (Ptfct(i->tname->tp)->fct_base == INSTANTIATED) { // reinstantiate it
i->instantiate(true);
}
}
// verify that the qualifier used to decl the member function matches the
// template arguments in name, ie.
// template <class P, class Q, ..> c<P,Q,..>::member_function() {}
// match it's Ps and Qs.
bool templ_inst::check_qualifier(Plist formals) {
Pexpr actual = actuals;
for (Plist formal = formals; formal && actual; formal = formal->l, actual = actual->e2)
switch (formal->f->n_template_arg) {
case template_type_formal: {
Pbase b = Pbase(actual->e1->tp);
if (!((b->base == TYPE) && (b->b_name->base == TNAME)
&& (strcmp(Pname(b->b_name)->string, formal->f->string) == 0)))
return false;
break;
}
case template_expr_formal:
if (!((actual->e1->base == NAME)
&& (strcmp(Pname(actual->e1)->string, formal->f->string) == 0)))
return false;
break;
default:
error('i', "bad template formal");
}
return true;
}
extern int add_first;
void introduce_global_name(Pname ft) {
// error( 'd', "introduce_global(%n)", ft );
Pname n = gtbl->look(ft->string, 0);
if (n == 0) {
Pname nn = gtbl->insert(new name(ft->string), 0);
nn->n_template_fct = 1;
if (ft->n_sto == FRIEND) {
nn->n_scope = EXTERN;
}
nn->tp = ft->tp;
PERM(nn);
} else {
// error( 'd', "introduce_global(%n) n: %n %t", ft, n, n->tp );
switch (n->tp->base) {
default:
error("YF%n renamed object ofT%t", ft, n->tp);
break;
case OVERLOAD: {
Pname n2 = Pgen(n->tp)->add(ft);
n2->n_template_fct++;
n->n_template_fct++;
} break;
case FCT: {
Pgen g = new gen;
PERM(g);
add_first = 1;
(void) g->add(n);
add_first = 0;
Pname n2 = g->add(ft);
n2->n_template_fct++;
n->tp = g;
n->n_template_fct++;
} break;
}
}
}
void templ_compilation::introduce_class_templ(Pname namep)
/* make the class template visible when compiling the
* template class definition,
* so that it can be referenced while compiling the class body. */
{
// error('d',"introduce_class_templ: %n", namep );
/* this check is because introduce_call_templ is invoked in two places:
* st_class() in norm.c for class definition, and templ_compilation::end()
* call at end() is always for a forward declaration
*/
owner = is_template(namep);
if (!owner) {
check_formals_for_dups(params); // place for it??
// create a template definition
owner = new templ(params, namep);
Pname nn = new name(namep->string);
Pname lookup_name = templp->templates->insert(nn, 0);
lookup_name->tp = Ptype(owner); // lie, to permit use of the table
}
}
void templ_compilation::introduce_funct_templ(Pname namep) {
/* make the function template visible when compiling the
* template function definition,
* so that it can be referenced while compiling the function body.
*/
// error('d',"introduce_funct_templ: %n", namep );
Pfunt t = new function_template(params, namep);
Pname n = templp->templates->look(namep->string, FCT);
if (n == 0) {
Pname nn = new name(namep->string);
n = templp->templates->insert(nn, FCT);
} else
t->gen_list = Pfunt(n->tp);
n->tp = Ptype(t);
f_owner = t;
introduce_global_name(namep);
}
static int has_formal_type(Ptclass pt_cl, Plist list) {
// error('d',"has_formal_type: %s", pt_cl->string);
int has_formal = 0;
for (Pexpr formals = pt_cl->inst->actuals; formals; formals = formals->e2) {
Pexpr fe = formals->e1;
if (fe->base != NAME || !fe->tp || fe->tp->base != TYPE)
continue;
Pname tn = fe->tp->bname();
if (!tn->is_template_arg())
continue;
// error('d',"has_formal_type: found: %n", tn);
list = list->l = new name_list(tn, 0);
++has_formal;
}
return has_formal;
}
static Pname Tmpl;
static int has_formal_type(Pname nn, Plist list) {
// error('d',"has_formal_type: %n %t", nn, nn->tp);
Pname bn;
Ptype t, nt = nn->tp;
while (t = nt->is_ptr())
nt = Pptr(t)->typ;
while (t = nt->is_ref())
nt = Pptr(t)->typ;
if (nt->base != TYPE)
return 0;
while (nt->base == TYPE) {
bn = nt->bname();
nt = bn->tp;
}
if (nt->base == COBJ) { // declaration: X<T,...>
Pclass c1 = nt->classtype();
if (c1->is_templ_instance())
return has_formal_type(Ptclass(c1), list);
// encapsulate later ...
if (c1->class_base == CL_TEMPLATE) {
// error('d',"class template: %n set to tmpl", bn);
Tmpl = bn;
return -1;
}
}
if (bn->is_template_arg()) {
// error('d',"has_formal_type: found: %n", bn);
list = list->l = new name_list(bn, 0);
return true;
}
return false;
}
static bit hbf = 0;
void handle_bound_friend(Ptempl_base pb, Pname fn) { /* point of this is to extrapolate the template
*function declaration implicit within a
*declaration such as friend min(X<T>,X<T>);
*/
// error('d',"handle_bound_friend (%n %n)", Ptempl(pb)->namep, fn);
Plist formals, f_list = new name_list(0, 0);
int i = 0, formal_cnt = pb->get_formals_count();
Pfct f = fn->fct_type();
if (f->body)
return; // no need to extrapolate
struct bleh {
Pname n;
int used;
};
bleh *pbleh = new bleh[formal_cnt];
for (formals = pb->get_formals(); formals; formals = formals->l) {
pbleh[i].n = formals->f;
pbleh[i++].used = 0;
}
int has_formal = 0;
for (Pname n = f->argtype; n; n = n->n_list)
// build up f_list of formals in signature
has_formal += has_formal_type(n, f_list);
if (!has_formal)
return;
// *** XXX : incomplete: what if f(pb<...>,T,...)
if (has_formal == -1) { // f(pb<...>
if (strcmp(Tmpl->string, Ptempl(pb)->namep->string))
return;
// pb is formal argument to function: all formals used
// error('d',"has_formal == -1, %n == %n",Tmpl,Ptempl(pb)->namep);
} else
for (formals = f_list->l; formals; formals = formals->l) {
Pname n = formals->f;
if (!n)
break;
for (i = 0; i < formal_cnt; ++i)
if (strcmp(n->string, pbleh[i].n->string) == 0) {
pbleh[i].used++;
// error('d',"match: %n used: %d", pbleh[i].n, pbleh[i].used);
break;
}
}
templp->save_templ = new templ_state;
templp->params = templp->param_end = 0;
for (i = 0; i < formal_cnt; ++i) {
if (pbleh[i].used == 0 && has_formal != -1)
continue;
templp->append_parameter(pbleh[i].n);
// error('d',"i %d t_list: %n", i, pbleh[i].n);
}
// unnecessary when class and functions share owner
templp->owner = 0; // restored by delete, below
hbf = 1;
templp->end(fn);
f->fct_base = FCT_TEMPLATE;
Ptempl_base fr = templp->parsed_template; // set to f_owner
delete templp->save_templ;
templp->save_templ = 0;
Pclass cl = Ptempl(pb)->classtype();
cl->templ_friends = new cons(fr, cl->templ_friends);
fr->extrapolated = 1;
if (hbf == 1) // set to 2 within templ_compilation::end()
fr->templ_refs = templp->friend_templ_refs;
hbf = 0;
// error('d',"handle_bound: cl %t is_extrap: %d templ_refs: %d", cl?cl:0, fr->is_extrapolated(),
// fr->templ_refs);
}
void check_templ_funct(Pname fn) {
// error('d', "check_templ_funct( %n ): n_oper: %k",fn,fn->n_oper);
Pfct f = Pfct(fn->tp);
for (Pname al = f->argtype; al; al = al->n_list) {
if (al->n_initializer) {
error('s', "FYs do not support defaultAs");
break;
}
}
if (f->nargs_known != 1)
error('s', "ellipsis (...) inA list ofYF%n", fn);
if (fn->n_oper) {
switch (f->nargs) {
case 1:
case 2:
break;
default:
if (fn->n_oper != NEW)
error("FY%n must take 1 or 2As", fn);
}
switch (fn->n_oper) {
case CALL:
case DEREF:
case REF:
case ASSIGN:
error("YF operator%s must be aCM", keys[fn->n_oper]);
return;
case DELETE:
error("::operator %s may not be aYF", keys[fn->n_oper]);
return;
default:
fn->check_oper(0);
break;
}
}
if (strcmp("main", fn->string) == 0)
error("main() may not be aYF");
}
#if 0
static void
display_templ_refs()
{
error('d',"display_templ_refs :: templp\n");
for (Pcons p = templp->templ_refs; p; p = p->cdr) {
Ptempl_inst pt = Ptempl_inst(p->car);
if ( pt == 0 ) { error('d',"templ_refs: empty (%d)",p->car); continue; }
error('d',"\ntempl_refs: %d->car:\n",p->car);
error('d',"\ttname: %n namep: %n",pt->get_tname(),pt->get_namep());
error('d',"\tdef: %n refp: %d\n", pt->def->namep, pt->refp);
}
}
static void
display_templ_refs( basic_template *bt )
{
error('d',"display_templ_refs :: basic_template\n");
for (Pcons p = bt->templ_refs; p; p = p->cdr) {
Ptempl_inst pt = Ptempl_inst(p->car);
if ( pt == 0 ) { error('d',"templ_refs: empty (%d)",p->car); continue; }
error('d',"\ntempl_refs: %d->car:\n",p->car);
error('d',"\ttname: %n namep: %n",pt->get_tname(),pt->get_namep());
error('d',"\tdef: %n refp: %d\n", pt->def->namep, pt->refp);
}
}
#endif
void templ_compilation::end(Pname p)
/* The body of the template has been parsed.
* Finish the definition of the template class */
{
// error('d',"templ_compilation::end(%n) tp %k",p,p->tp?p->tp->base:0);
// Restore the name state to that prior to
// the processing of the template parameters
// SYM -- need to restore tables... XXXXX ???
// since the lex_level of these paramenters was lex_level+1
// each will be hidden by restore() (n_key<==HIDDEN)
// modified_tn = param_tn;
// restore();
// modified_tn = 0;
bool forward_definition = false;
if (!p->tp) {
error("aC,F,MF or static dataM definition wasX");
return;
}
switch (p->tp->base) {
case COBJ:
if (p->n_sto == FRIEND) {
p = p->tp->bname();
// error('d',"p %n tp %k ", p, p->tp->base );
// no break;
} else { // should not happen: i.e., caught in grammar
error("illegalY %n", p);
return;
}
case CLASS:
/* Create the template type to represent the parsed template,
* entering it into the global table. This is achieved simply
* by modifying the TNAME that was entered into ktbl to
* represent the class definition.
*/
{
// Pname namep = ktbl->look(p->string,0);
// Pname namep = k_find_name(p->string,Ctbl,0);//SYM
Pname namep = k_find_name(p->string, Gtbl, HIDDEN); // SYM
if (namep == 0 || namep->base == NAME) // namep = 0;
{
const char *s = (p->string ? p->string : "");
if (*s && (s[0] != '_' || s[1] != '_' || s[2] != 'C'))
error("nestedYC %s", s);
else {
error("missingYN");
return;
}
}
owner = is_template(namep);
if (owner) {
Pclass c = owner->classtype();
// ignore it, if it is a forward declaration
// following a real definition
if (owner->defined && (Pclass(p->tp)->mem_list != owner->members))
error("YC %s multiply defined", p->string);
forward_definition = bool((c->defined & DEF_SEEN) && (!owner->defined));
if (forward_definition)
owner->resolve_forward_decl(params, c);
if (pt_opt && forward_definition)
fprintf(pt_file, "t %s %s\n", p->string, curr_filename());
}
// a forward declaration
else
introduce_class_templ(namep);
if (owner->defined) {
for (Pname nn = Pclass(p->tp)->mem_list; nn;
nn = nn->n_list) { // look for implicit non-member template friend functions
if (nn->base == NAME && nn->n_sto == FRIEND && nn->n_qualifier == 0) {
switch (nn->tp->base) {
case FCT:
// error('d',"fct friend %s",nn->string);
handle_bound_friend(owner, nn);
break;
case OVERLOAD:
// error('d',"overload friend %s",nn->string);
break;
}
}
}
}
if (templ_refs)
owner->templ_refs = templ_refs;
templp->clear_friend_ref_templ();
break;
}
case FCT: {
if (pt_opt && !p->fct_type()->body && !p->n_qualifier)
fprintf(pt_file, "f %s %s\n", p->string, curr_filename());
Pname qual = p->n_qualifier;
if (!qual) { // function template
check_funct_formals(params, p);
check_templ_funct(p);
f_owner = is_template(p->string, FCT);
if (!f_owner)
introduce_funct_templ(p);
else {
Pfunt tl = f_owner;
Pname fn = 0;
Pfct n_fct = p->fct_type();
// is it an overloaded instance
int error_cnt = 0;
for (fn = tl->fn; tl; tl = tl->gen_list, fn = tl ? tl->fn : 0) {
extern bit return_error; // set in type::check
if (n_fct->check(fn->tp, PT_OVERLOAD) == 0)
break;
if (error_cnt = return_error)
break;
}
if (error_cnt)
error("FY%n: declared twice with different returnTs", p);
// error('d',"after gen_list: tl: %d", tl );
if (tl == 0) // overloaded instance
introduce_funct_templ(p);
else { // redefinition
int f1, f2, extrap = 0;
Pfct o_fct = fn->fct_type();
if ((f1 = n_fct->body != 0) && (f2 = o_fct->body != 0))
error("two definitions ofYF%n (%t %t)", fn, o_fct, n_fct);
// replace entry in template table for a forward definition
// or if original definition is extrapolated friend function
extrap = f_owner->is_extrapolated();
if ((forward_definition = (bool) (f1 && f2 == 0)) || extrap) {
tl->formals = params;
tl->fn = p;
tl->templ_refs = templ_refs;
f_owner = tl;
} else if (hbf == 1)
hbf = 2;
}
}
if (f_owner->templ_refs == 0 && hbf != 2)
f_owner->templ_refs = templ_refs;
break;
}
Pbase q = Pbase(qual->tp);
if (q && (q->base == COBJ))
switch (get_class_base(q)) {
case UNINSTANTIATED:
owner = Ptclass(q->b_name->tp)->inst->def;
/* verify that the formals specified match the
* template formals in name, note that the length
* was already matched when the instantiation was
* generated
*/
if (!get_template_class(q)->inst->check_qualifier(params))
error("QrZs must match theY formalZs");
break;
case CL_TEMPLATE:
// the template reference was without any of the formals
error("Qr%n for%n must specifyYZs", qual, p);
break;
default: {
Pclass cl = (Pclass) q->b_name->tp;
if (cl && cl->in_class && cl->in_class->class_base)
error('s', "out-of-line definition ofMF ofC nestedWinYC (%t::%n)", cl, p);
else
error("Qr%n for%n wasn't aYC", qual, p);
return;
}
}
if (!p->fct_type()->body)
error("QdN%n::%n inYFD", qual, p);
if (owner) {
Pfunt ft = owner->collect_function_member(p);
ft->templ_refs = templ_refs;
ft->formals = params;
owner->check_formals(params);
} else
error("memberFY must beQd byCYN");