Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997, 1998
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "c_types.h"
33
#include "ctype_ops.h"
34
#include "exp_ops.h"
35
#include "graph_ops.h"
36
#include "hashid_ops.h"
37
#include "id_ops.h"
38
#include "member_ops.h"
39
#include "nspace_ops.h"
40
#include "off_ops.h"
41
#include "type_ops.h"
42
#include "error.h"
43
#include "catalog.h"
44
#include "option.h"
45
#include "access.h"
46
#include "allocate.h"
47
#include "assign.h"
48
#include "basetype.h"
49
#include "capsule.h"
50
#include "cast.h"
51
#include "check.h"
52
#include "chktype.h"
53
#include "class.h"
54
#include "compile.h"
55
#include "constant.h"
56
#include "construct.h"
57
#include "convert.h"
58
#include "copy.h"
59
#include "chktype.h"
60
#include "declare.h"
61
#include "derive.h"
62
#include "destroy.h"
63
#include "dump.h"
64
#include "exception.h"
65
#include "expression.h"
66
#include "function.h"
67
#include "hash.h"
68
#include "identifier.h"
69
#include "initialise.h"
70
#include "instance.h"
71
#include "label.h"
72
#include "namespace.h"
73
#include "operator.h"
74
#include "overload.h"
75
#include "predict.h"
76
#include "statement.h"
77
#include "syntax.h"
78
#include "template.h"
79
#include "token.h"
80
 
81
 
82
/*
83
    SET AN INFERRED FUNCTION RETURN TYPE
84
 
85
    If t is a function type with an inferred return type then the actual
86
    return type is set according to id.  The routine returns the original
87
    inferred return type.
88
*/
89
 
90
TYPE inferred_return
91
    PROTO_N ( ( t, id ) )
92
    PROTO_T ( TYPE t X IDENTIFIER id )
93
{
94
    if ( IS_type_func ( t ) ) {
95
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
96
	if ( is_type_inferred ( r ) == INFERRED_EMPTY ) {
97
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
98
	    switch ( TAG_hashid ( nm ) ) {
99
		case hashid_constr_tag :
100
		case hashid_destr_tag : {
101
		    /* Constructors and destructors */
102
		    COPY_type ( type_func_ret ( t ), type_void ) ;
103
		    break ;
104
		}
105
		case hashid_conv_tag : {
106
		    /* Conversion functions */
107
		    TYPE s = DEREF_type ( hashid_conv_type ( nm ) ) ;
108
		    COPY_type ( type_func_ret ( t ), s ) ;
109
		    break ;
110
		}
111
	    }
112
	    return ( r ) ;
113
	}
114
    }
115
    return ( NULL_type ) ;
116
}
117
 
118
 
119
/*
120
    CHECK A COPY CONSTRUCTOR OR ASSIGNMENT OPERATOR TYPE
121
 
122
    This routine checks whether the function type fn has first parameter
123
    of type '[volatile] ct &', 'const [volatile] ct &' or '[const]
124
    [volatile] ct' with any subsequent parameters being optional.  It
125
    returns 1, 2 and 3 respectively in these cases, or 0 if fn does not
126
    match this description.
127
*/
128
 
129
static int check_copy_constr
130
    PROTO_N ( ( fn, ct ) )
131
    PROTO_T ( TYPE fn X CLASS_TYPE ct )
132
{
133
    int c = 0 ;
134
    if ( IS_type_templ ( fn ) ) {
135
	/* Allow for template constructors */
136
	in_template_decl++ ;
137
	fn = DEREF_type ( type_templ_defn ( fn ) ) ;
138
	c = check_copy_constr ( fn, ct ) ;
139
	in_template_decl-- ;
140
    } else {
141
	LIST ( IDENTIFIER ) pids = DEREF_list ( type_func_pids ( fn ) ) ;
142
	if ( !IS_NULL_list ( pids ) ) {
143
	    /* Have at least one parameter */
144
	    IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
145
	    TYPE t = DEREF_type ( id_parameter_type ( pid ) ) ;
146
 
147
	    /* Check for second parameter */
148
	    pids = TAIL_list ( pids ) ;
149
	    if ( !IS_NULL_list ( pids ) ) {
150
		/* Second parameter must have a default argument */
151
		EXP e ;
152
		pid = DEREF_id ( HEAD_list ( pids ) ) ;
153
		e = DEREF_exp ( id_parameter_init ( pid ) ) ;
154
		if ( IS_NULL_exp ( e ) ) return ( 0 ) ;
155
	    }
156
 
157
	    /* Check parameter type */
158
	    if ( IS_type_ref ( t ) ) {
159
		TYPE s = DEREF_type ( type_ref_sub ( t ) ) ;
160
		if ( IS_type_compound ( s ) ) {
161
		    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
162
		    if ( eq_ctype ( cs, ct ) ) {
163
			/* Reference to same class */
164
			CV_SPEC qual = DEREF_cv ( type_qual ( s ) ) ;
165
			c = ( ( qual & cv_const ) ? 2 : 1 ) ;
166
		    }
167
		} else if ( is_templ_type ( s ) ) {
168
		    /* Reference to template parameter */
169
		    CV_SPEC qual = DEREF_cv ( type_qual ( s ) ) ;
170
		    c = ( ( qual & cv_const ) ? 2 : 1 ) ;
171
		}
172
	    } else if ( IS_type_compound ( t ) ) {
173
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( t ) ) ;
174
		if ( eq_ctype ( cs, ct ) ) c = 3 ;
175
	    } else if ( is_templ_type ( t ) ) {
176
		/* Template parameter */
177
		c = 1 ;
178
	    }
179
	}
180
    }
181
    return ( c ) ;
182
}
183
 
184
 
185
/*
186
    CHECK A CONSTRUCTOR FUNCTION TYPE
187
 
188
    This routine checks the function type t for the constructor id declared
189
    in namespace ns.  The check that the constructor is a non-static member
190
    function is carried out elsewhere.  Note that no return type can be given
191
    for id - it is implicitly void.  Remedial action is taken to transform
192
    illegal copy constructors such as 'X::X ( X )' into valid constructors
193
    such as 'X::X ( X & )'.  This is to avoid having to check for infinite
194
    loops later.
195
*/
196
 
197
TYPE check_constr
198
    PROTO_N ( ( t, id, ns ) )
199
    PROTO_T ( TYPE t X IDENTIFIER id X NAMESPACE ns )
200
{
201
    if ( IS_type_templ ( t ) ) {
202
	/* Allow for template types */
203
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
204
	s = check_constr ( s, id, ns ) ;
205
	COPY_type ( type_templ_defn ( t ), s ) ;
206
    } else {
207
	/* Decompose function type */
208
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
209
	CV_SPEC cv = DEREF_cv ( type_func_mqual ( t ) ) ;
210
 
211
	/* Find underlying class */
212
	CLASS_TYPE ct = namespace_class ( ns ) ;
213
 
214
	/* No return type can be given for a constructor */
215
	if ( is_type_inferred ( r ) != INFERRED_EMPTY ) {
216
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
217
	    report ( crt_loc, ERR_class_ctor_ret ( nm ) ) ;
218
	}
219
	COPY_type ( type_func_ret ( t ), type_void ) ;
220
 
221
	/* Check for invalid copy constructors */
222
	if ( check_copy_constr ( t, ct ) == 3 ) {
223
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
224
	    report ( crt_loc, ERR_class_copy_bad ( nm ) ) ;
225
	}
226
 
227
	/* A constructor cannot be cv-qualified */
228
	if ( cv & cv_qual ) {
229
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
230
	    report ( crt_loc, ERR_class_ctor_qual ( nm, cv ) ) ;
231
	}
232
    }
233
    return ( t ) ;
234
}
235
 
236
 
237
/*
238
    CHECK A DESTRUCTOR FUNCTION TYPE
239
 
240
    This routine checks the function type t for the destructor id.  The
241
    check that the destructor is a non-static member function is carried
242
    out elsewhere.  Note that no return type can be given for id - it is
243
    implicitly void.
244
*/
245
 
246
TYPE check_destr
247
    PROTO_N ( ( t, id, ns ) )
248
    PROTO_T ( TYPE t X IDENTIFIER id X NAMESPACE ns )
249
{
250
    if ( IS_type_templ ( t ) ) {
251
	/* Allow for template types */
252
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
253
	s = check_destr ( s, id, ns ) ;
254
	COPY_type ( type_templ_defn ( t ), s ) ;
255
    } else {
256
	/* Decompose function type */
257
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
258
	CV_SPEC cv = DEREF_cv ( type_func_mqual ( t ) ) ;
259
	int ell = DEREF_int ( type_func_ellipsis ( t ) ) ;
260
	LIST ( TYPE ) p = DEREF_list ( type_func_ptypes ( t ) ) ;
261
 
262
	/* Check namespace */
263
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
264
	TYPE dt = DEREF_type ( hashid_destr_type ( nm ) ) ;
265
	if ( IS_type_compound ( dt ) ) {
266
	    /* Check inheritance */
267
	    CLASS_TYPE dct = DEREF_ctype ( type_compound_defn ( dt ) ) ;
268
	    NAMESPACE dns = DEREF_nspace ( ctype_member ( dct ) ) ;
269
	    if ( !EQ_nspace ( dns, ns ) ) {
270
		report ( crt_loc, ERR_class_dtor_inherit ( nm, ns ) ) ;
271
	    }
272
	}
273
 
274
	/* No return type can be given for a destructor */
275
	if ( is_type_inferred ( r ) != INFERRED_EMPTY ) {
276
	    report ( crt_loc, ERR_class_dtor_ret ( nm ) ) ;
277
	}
278
	COPY_type ( type_func_ret ( t ), type_void ) ;
279
 
280
	/* No parameter types can be given for a destructor */
281
	if ( !IS_NULL_list ( p ) || ell ) {
282
	    report ( crt_loc, ERR_class_dtor_pars ( nm ) ) ;
283
	}
284
 
285
	/* A destructor cannot be cv-qualified */
286
	if ( cv & cv_qual ) {
287
	    report ( crt_loc, ERR_class_dtor_qual ( nm, cv ) ) ;
288
	}
289
    }
290
    return ( t ) ;
291
}
292
 
293
 
294
/*
295
    CHECK A CONVERSION FUNCTION TYPE
296
 
297
    This routine checks the function type t for the conversion function id.
298
    The check that the converter is a non-static member function is carried
299
    out elsewhere.  Note that no return type can be given for id - it is
300
    inferred from the type used in id.  This may not be a legal return type,
301
    so inject_pre_type is used to check it.
302
*/
303
 
304
TYPE check_conv
305
    PROTO_N ( ( t, id ) )
306
    PROTO_T ( TYPE t X IDENTIFIER id )
307
{
308
    if ( IS_type_templ ( t ) ) {
309
	/* Allow for template types */
310
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
311
	s = check_conv ( s, id ) ;
312
	COPY_type ( type_templ_defn ( t ), s ) ;
313
    } else {
314
	/* Find the conversion type */
315
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
316
	TYPE s = DEREF_type ( hashid_conv_type ( nm ) ) ;
317
 
318
	/* Decompose function type */
319
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
320
	int ell = DEREF_int ( type_func_ellipsis ( t ) ) ;
321
	LIST ( TYPE ) p = DEREF_list ( type_func_ptypes ( t ) ) ;
322
 
323
	/* No return type can be given for a conversion function */
324
	if ( is_type_inferred ( r ) != INFERRED_EMPTY ) {
325
	    /* If a return type is given it might as well be right */
326
	    if ( eq_type ( r, s ) ) {
327
		report ( crt_loc, ERR_class_conv_fct_ret ( nm ) ) ;
328
	    } else {
329
		report ( crt_loc, ERR_class_conv_fct_ret_bad ( nm, r ) ) ;
330
	    }
331
	}
332
	COPY_type ( type_func_ret ( t ), NULL_type ) ;
333
	t = inject_pre_type ( t, s, 0 ) ;
334
 
335
	/* No parameter types can be given for a conversion function */
336
	if ( !IS_NULL_list ( p ) || ell ) {
337
	    report ( crt_loc, ERR_class_conv_fct_pars ( nm ) ) ;
338
	}
339
 
340
	/* Can't have conversion to cv-qualified void */
341
	if ( IS_type_top_etc ( s ) ) {
342
	    report ( crt_loc, ERR_class_conv_fct_void ( nm ) ) ;
343
	}
344
    }
345
    return ( t ) ;
346
}
347
 
348
 
349
/*
350
    LOOK UP AN OVERLOADED OPERATOR FUNCTION
351
 
352
    This routine looks up the overloaded operator function 'operator op'
353
    in the class ct.
354
*/
355
 
356
IDENTIFIER find_operator
357
    PROTO_N ( ( ct, op ) )
358
    PROTO_T ( CLASS_TYPE ct X int op )
359
{
360
    HASHID nm = lookup_op ( op ) ;
361
    NAMESPACE cns = DEREF_nspace ( ctype_member ( ct ) ) ;
362
    IDENTIFIER id = search_field ( cns, nm, 0, 0 ) ;
363
    return ( id ) ;
364
}
365
 
366
 
367
/*
368
    FIND A DEFAULT CONSTRUCTOR OR DESTRUCTOR
369
 
370
    This routine finds either a default constructor, a copy constructor,
371
    a default destructor or a copy assignment operator of the class ct,
372
    depending on the value of n.  The null identifier is returned and
373
    an error is added to err if such a function does not exist (including
374
    for incomplete types).
375
*/
376
 
377
static IDENTIFIER find_constr
378
    PROTO_N ( ( ct, n, err ) )
379
    PROTO_T ( CLASS_TYPE ct X int n X ERROR *err )
380
{
381
    CLASS_INFO ci ;
382
    int match = 0 ;
383
    int nargs = 0 ;
384
    int kind = KIND_FUNC ;
385
    IDENTIFIER id = NULL_id ;
386
    IDENTIFIER qid = NULL_id ;
387
 
388
    /* Check for complete types */
389
    complete_class ( ct, 1 ) ;
390
    ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
391
    if ( !( ci & cinfo_complete ) ) return ( NULL_id ) ;
392
 
393
    /* Find the basic identifier */
394
    switch ( n ) {
395
	case DEFAULT_CONSTR :
396
	case DEFAULT_COPY :
397
	case DEFAULT_USR : {
398
	    /* Find constructors */
399
	    id = DEREF_id ( ctype_constr ( ct ) ) ;
400
	    if ( n == DEFAULT_COPY ) nargs = 1 ;
401
	    kind = KIND_CONSTR ;
402
	    break ;
403
	}
404
	case DEFAULT_DESTR :
405
	case DEFAULT_DELETE : {
406
	    /* Find destructors */
407
	    id = DEREF_id ( ctype_destr ( ct ) ) ;
408
	    nargs = 1 ;
409
	    break ;
410
	}
411
	case DEFAULT_ASSIGN : {
412
	    /* Find 'operator =' */
413
	    id = find_operator ( ct, lex_assign ) ;
414
	    nargs = 2 ;
415
	    break ;
416
	}
417
    }
418
 
419
    /* Find appropriate function */
420
    if ( !IS_NULL_id ( id ) && IS_id_mem_func ( id ) ) {
421
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
422
	if ( ds & dspec_template ) {
423
	    /* Allow for template constructors */
424
	    match = 2 ;
425
	} else {
426
	    switch ( n ) {
427
		case DEFAULT_CONSTR :
428
		case DEFAULT_DESTR :
429
		case DEFAULT_DELETE :
430
		case DEFAULT_USR : {
431
		    /* Should take no arguments */
432
		    IDENTIFIER pid = id ;
433
		    while ( !IS_NULL_id ( pid ) ) {
434
			TYPE t = DEREF_type ( id_mem_func_type ( pid ) ) ;
435
			if ( min_no_args ( t ) == 1 ) {
436
			    qid = pid ;
437
			    match++ ;
438
			}
439
			pid = DEREF_id ( id_mem_func_over ( pid ) ) ;
440
		    }
441
		    break ;
442
		}
443
		case DEFAULT_COPY :
444
		case DEFAULT_ASSIGN : {
445
		    /* Should be a copy constructor */
446
		    IDENTIFIER pid = id ;
447
		    while ( !IS_NULL_id ( pid ) ) {
448
			TYPE t = DEREF_type ( id_mem_func_type ( pid ) ) ;
449
			if ( check_copy_constr ( t, ct ) ) {
450
			    qid = pid ;
451
			    match++ ;
452
			}
453
			pid = DEREF_id ( id_mem_func_over ( pid ) ) ;
454
		    }
455
		    break ;
456
		}
457
	    }
458
	}
459
    } else {
460
	/* This can only happen for dummy classes */
461
	return ( NULL_id ) ;
462
    }
463
 
464
    /* Deal with ambiguous cases */
465
    if ( match > 1 ) {
466
	CANDIDATE_LIST *p = &candidates ;
467
	p->size = 0 ;
468
	add_candidates ( p, id, 1, KIND_CONSTR ) ;
469
	if ( p->size ) {
470
	    CANDIDATE *q ;
471
	    unsigned rank ;
472
	    TYPE t = make_class_type ( ct ) ;
473
	    LIST ( EXP ) args = NULL_list ( EXP ) ;
474
	    while ( nargs ) {
475
		/* Create dummy arguments */
476
		EXP a ;
477
		MAKE_exp_value ( t, a ) ;
478
		CONS_exp ( a, args, args ) ;
479
		nargs-- ;
480
	    }
481
	    if ( kind == KIND_CONSTR ) {
482
		swap_candidates ( p, ( unsigned ) 0 ) ;
483
	    }
484
	    q = resolve_overload ( p, args, t, 0 ) ;
485
	    if ( kind == KIND_CONSTR ) {
486
		swap_candidates ( p, ( unsigned ) 0 ) ;
487
	    }
488
	    rank = q->rank ;
489
	    if ( rank >= RANK_VIABLE ) {
490
		qid = q->func ;
491
		if ( rank == RANK_BEST ) {
492
		    /* Best match */
493
		    if ( match_no_viable > 1 ) {
494
			ERROR err2 ;
495
			if ( kind == KIND_CONSTR ) {
496
			    err2 = ERR_over_match_ctor_ok ( qid ) ;
497
			} else {
498
			    err2 = ERR_over_match_call_ok ( qid ) ;
499
			}
500
			add_error ( err, err2 ) ;
501
		    }
502
		    match = 1 ;
503
		} else {
504
		    /* Ambiguous call */
505
		    ERROR err2 ;
506
		    if ( kind == KIND_CONSTR ) {
507
			err2 = ERR_over_match_ctor_ambig ( qid ) ;
508
		    } else {
509
			err2 = ERR_over_match_call_ambig ( qid ) ;
510
		    }
511
		    err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
512
		    add_error ( err, err2 ) ;
513
		}
514
	    } else {
515
		/* No viable call */
516
		qid = NULL_id ;
517
		match = 0 ;
518
	    }
519
	    if ( !IS_NULL_list ( args ) ) free_exp_list ( args, 1 ) ;
520
	} else {
521
	    /* No viable call */
522
	    qid = NULL_id ;
523
	    match = 0 ;
524
	}
525
    }
526
 
527
    /* Report error */
528
    if ( match == 0 ) {
529
	switch ( n ) {
530
	    case DEFAULT_CONSTR :
531
	    case DEFAULT_USR : {
532
		add_error ( err, ERR_class_ctor_default ( ct ) ) ;
533
		break ;
534
	    }
535
	    case DEFAULT_COPY : {
536
		add_error ( err, ERR_class_copy_constr ( ct ) ) ;
537
		break ;
538
	    }
539
	    case DEFAULT_DESTR :
540
	    case DEFAULT_DELETE : {
541
		add_error ( err, ERR_class_dtor_default ( ct ) ) ;
542
		break ;
543
	    }
544
	    case DEFAULT_ASSIGN : {
545
		add_error ( err, ERR_class_copy_assign ( ct ) ) ;
546
		break ;
547
	    }
548
	}
549
    }
550
    return ( qid ) ;
551
}
552
 
553
 
554
/*
555
    FIND NUMBER OF EXTRA CONSTRUCTOR ARGUMENTS
556
 
557
    This routine finds the number of extra constructor arguments required
558
    by the member function id of the class ct.  For constructors and
559
    assignment operators for classes with a virtual base the extra
560
    argument is true to indicate that the virtual components should be
561
    initialised.  For non-trivial destructors bit 1 of the extra argument
562
    is used to indicate that any virtual components should be destroyed,
563
    while bit 0 is used to indicate that 'operator delete' should be
564
    called on the argument.  The macros EXTRA_* are used to represent
565
    these values.
566
*/
567
 
568
unsigned extra_constr_args
569
    PROTO_N ( ( id, ct ) )
570
    PROTO_T ( IDENTIFIER id X CLASS_TYPE ct )
571
{
572
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
573
    if ( !( ds & dspec_trivial ) ) {
574
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
575
	unsigned tag = TAG_hashid ( nm ) ;
576
	if ( tag == hashid_constr_tag ) {
577
	    /* Constructors */
578
	    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
579
	    if ( ci & cinfo_virtual_base ) return ( 1 ) ;
580
	} else if ( tag == hashid_destr_tag ) {
581
	    /* Destructors */
582
	    return ( 1 ) ;
583
	}
584
    }
585
    return ( 0 ) ;
586
}
587
 
588
 
589
/*
590
    CONSTRUCTOR BASE CLASS INITIALISER FLAG
591
 
592
    This flag is set to true in a constructor base class initialiser.
593
    This overrides the value of any extra arguments passed to the
594
    constructor.
595
*/
596
 
597
static int in_ctor_base_init = 0 ;
598
 
599
 
600
/*
601
    ADD EXTRA CONSTRUCTOR ARGUMENTS
602
 
603
    This routine adds any necessary extra constructor arguments to the
604
    member function call expression e.  ct gives the associate class
605
    type and v is the value to be passed to such arguments.
606
*/
607
 
608
EXP add_constr_args
609
    PROTO_N ( ( e, ct, v ) )
610
    PROTO_T ( EXP e X CLASS_TYPE ct X int v )
611
{
612
    if ( IS_exp_func_id ( e ) ) {
613
	IDENTIFIER id = DEREF_id ( exp_func_id_id ( e ) ) ;
614
	unsigned n = extra_constr_args ( id, ct ) ;
615
	if ( n ) {
616
	    LIST ( EXP ) args = DEREF_list ( exp_func_id_args ( e ) ) ;
617
	    LIST ( EXP ) extra = NULL_list ( EXP ) ;
618
	    if ( in_ctor_base_init ) v = 0 ;
619
	    while ( n ) {
620
		EXP a ;
621
		NAT c = small_nat [v] ;
622
		MAKE_exp_int_lit ( type_sint, c, exp_int_lit_tag, a ) ;
623
		CONS_exp ( a, extra, extra ) ;
624
		n-- ;
625
	    }
626
	    args = APPEND_list ( args, extra ) ;
627
	    COPY_list ( exp_func_id_args ( e ), args ) ;
628
	}
629
    }
630
    return ( e ) ;
631
}
632
 
633
 
634
/*
635
    CALL A CONSTRUCTOR OR DESTRUCTOR
636
 
637
    This routine constructs a default call of the constructor or destructor
638
    id of type n.  ct gives the corresponding class type and pb gives the
639
    second argument for copy constructors and assignment operators.
640
*/
641
 
642
static EXP call_constr
643
    PROTO_N ( ( id, pb, n, v, ct ) )
644
    PROTO_T ( IDENTIFIER id X EXP *pb X int n X int v X CLASS_TYPE ct )
645
{
646
    /* Create argument list */
647
    TYPE t ;
648
    EXP e, a, b ;
649
    LIST ( EXP ) args = NULL_list ( EXP ) ;
650
    IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
651
    MAKE_type_compound ( cv_lvalue, ct, t ) ;
652
    COPY_id ( type_name ( t ), cid ) ;
653
    MAKE_exp_dummy ( t, NULL_exp, LINK_NONE, NULL_off, 0, a ) ;
654
    if ( n == DEFAULT_ASSIGN || n == DEFAULT_DELETE ) {
655
	/* Don't know object type */
656
	COPY_int ( exp_dummy_virt ( a ), 1 ) ;
657
    }
658
    if ( n == DEFAULT_COPY || n == DEFAULT_ASSIGN ) {
659
	/* These have two arguments */
660
	TYPE s = t ;
661
	int virt = 1 ;
662
	b = *pb ;
663
	if ( !IS_NULL_exp ( b ) ) {
664
	    s = DEREF_type ( exp_type ( b ) ) ;
665
	    while ( IS_type_array ( s ) ) {
666
		s = DEREF_type ( type_array_sub ( s ) ) ;
667
	    }
668
	    s = lvalue_type ( s ) ;
669
	    if ( know_type ( b ) ) virt = 0 ;
670
	}
671
	MAKE_exp_dummy ( s, b, LINK_NONE, NULL_off, DUMMY_copy, b ) ;
672
	COPY_int ( exp_dummy_virt ( b ), virt ) ;
673
	CONS_exp ( b, args, args ) ;
674
	*pb = b ;
675
    } else {
676
	/* These have one argument */
677
	b = a ;
678
    }
679
    CONS_exp ( a, args, args ) ;
680
 
681
    /* Call the function */
682
    use_func_id ( id, 0, suppress_usage ) ;
683
    e = apply_func_id ( id, qual_none, NULL_graph, args ) ;
684
    e = add_constr_args ( e, ct, v ) ;
685
    if ( n == DEFAULT_DESTR || n == DEFAULT_DELETE ) {
686
	MAKE_exp_destr ( type_void, e, a, e ) ;
687
    } else {
688
	t = make_class_type ( ct ) ;
689
	MAKE_exp_constr ( t, e, a, b, n, e ) ;
690
    }
691
    return ( e ) ;
692
}
693
 
694
 
695
/*
696
    CREATE A DEFAULT INITIALISER
697
 
698
    This routine creates a default initialiser for an object of type t
699
    in a constructor, destructor or assignment operator (as indicated
700
    by n).  Any resultant errors are added to err.
701
*/
702
 
703
EXP init_default
704
    PROTO_N ( ( t, pa, n, v, err ) )
705
    PROTO_T ( TYPE t X EXP *pa X int n X int v X ERROR *err )
706
{
707
    CV_SPEC cv ;
708
    EXP e = NULL_exp ;
709
    unsigned tag = TAG_type ( t ) ;
710
 
711
    /* Deal with classes */
712
    if ( tag == type_compound_tag ) {
713
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
714
	IDENTIFIER id = find_constr ( ct, n, err ) ;
715
	if ( !IS_NULL_id ( id ) ) {
716
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
717
	    if ( !( ds & dspec_trivial ) ) {
718
		/* Non-trivial constructor */
719
		e = call_constr ( id, pa, n, v, ct ) ;
720
	    } else if ( ds & dspec_implicit ) {
721
		if ( !( ds & dspec_defn ) ) {
722
		    /* Trivial constructor */
723
		    implicit_defn ( id, n ) ;
724
		}
725
	    }
726
	}
727
	return ( e ) ;
728
    }
729
 
730
    /* Deal with arrays */
731
    if ( tag == type_array_tag ) {
732
	NAT m = DEREF_nat ( type_array_size ( t ) ) ;
733
	TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
734
	e = init_default ( s, pa, n, v, err ) ;
735
	if ( !IS_NULL_exp ( e ) ) {
736
	    /* Apply to each array element */
737
	    MAKE_exp_nof ( t, NULL_exp, m, e, NULL_exp, e ) ;
738
	}
739
	return ( e ) ;
740
    }
741
 
742
    /* Everything else is alright in these cases */
743
    if ( n == DEFAULT_COPY || n == DEFAULT_DESTR || n == DEFAULT_DELETE ) {
744
	return ( NULL_exp ) ;
745
    }
746
 
747
    /* Deal with references */
748
    if ( tag == type_ref_tag ) {
749
	add_error ( err, ERR_dcl_init_ref_none () ) ;
750
	if ( n == DEFAULT_ASSIGN ) return ( NULL_exp ) ;
751
	MAKE_exp_null ( t, e ) ;
752
	return ( e ) ;
753
    }
754
 
755
    /* Deal with const objects */
756
    cv = DEREF_cv ( type_qual ( t ) ) ;
757
    if ( cv & cv_const ) add_error ( err, ERR_dcl_init_const () ) ;
758
    return ( NULL_exp ) ;
759
}
760
 
761
 
762
/*
763
    REPORT AN INITIALISATION ERROR
764
 
765
    This routine reports the error err which occurred in the initialisation
766
    of id in constructor or destructor fn of type n.
767
*/
768
 
769
static void constr_error
770
    PROTO_N ( ( err, id, fn, n ) )
771
    PROTO_T ( ERROR err X IDENTIFIER id X IDENTIFIER fn X int n )
772
{
773
    ERROR err1 ;
774
    ERROR err2 = ERR_dcl_init_decl ( id, NULL_string ) ;
775
    if ( n == DEFAULT_USR ) {
776
	err1 = ERR_class_base_init_err ( fn ) ;
777
    } else {
778
	err1 = ERR_class_base_init_impl ( fn ) ;
779
    }
780
    err = concat_error ( err2, err ) ;
781
    err = concat_error ( err1, err ) ;
782
    report ( crt_loc, err ) ;
783
    return ;
784
}
785
 
786
 
787
/*
788
    CREATE A BASE CLASS INITIALISER
789
 
790
    This routine creates a default initialiser for the base class gr
791
    for the constructor or destructor fn of type n.  For copy constructors
792
    and assignment operators a dummy expression is used to indicate that
793
    the base should be initialised from the corresponding base of the
794
    second argument (see enc_ctor_exp).
795
*/
796
 
797
static EXP init_empty_base
798
    PROTO_N ( ( gr, fn, n, m ) )
799
    PROTO_T ( GRAPH gr X IDENTIFIER fn X int n X int m )
800
{
801
    EXP e = NULL_exp ;
802
    ERROR err = NULL_err ;
803
    CLASS_TYPE ct = DEREF_ctype ( graph_head ( gr ) ) ;
804
    TYPE t = make_class_type ( ct ) ;
805
    e = init_default ( t, &e, m, EXTRA_NONE, &err ) ;
806
    if ( IS_NULL_exp ( e ) ) {
807
	if ( n == DEFAULT_USR && m != DEFAULT_DESTR ) {
808
	    /* Warn about uninitialised bases */
809
	    if ( !is_empty_class ( t ) ) {
810
		IDENTIFIER id = DEREF_id ( ctype_name ( ct ) ) ;
811
		err = concat_error ( err, ERR_class_base_init_none ( id ) ) ;
812
	    }
813
	}
814
	if ( m == DEFAULT_COPY || m == DEFAULT_ASSIGN ) {
815
	    /* Dummy value for copy constructor */
816
	    MAKE_exp_value ( t, e ) ;
817
	}
818
    }
819
    if ( !IS_NULL_err ( err ) ) {
820
	IDENTIFIER id = DEREF_id ( ctype_name ( ct ) ) ;
821
	constr_error ( err, id, fn, n ) ;
822
    }
823
    return ( e ) ;
824
}
825
 
826
 
827
/*
828
    CREATE A CLASS MEMBER INITIALISER
829
 
830
    This routine creates a default initialiser for a class member id
831
    for the constructor or destructor fn of type n.  Again a dummy value
832
    is used in copy constructors and assignment operators.
833
*/
834
 
835
static EXP init_empty_mem
836
    PROTO_N ( ( id, fn, n, m ) )
837
    PROTO_T ( IDENTIFIER id X IDENTIFIER fn X int n X int m )
838
{
839
    EXP e = NULL_exp ;
840
    ERROR err = NULL_err ;
841
    TYPE t = DEREF_type ( id_member_type ( id ) ) ;
842
    int v = ( m == DEFAULT_DESTR ? EXTRA_DESTR : EXTRA_CONSTR ) ;
843
    e = init_default ( t, &e, m, v, &err ) ;
844
    if ( IS_NULL_exp ( e ) ) {
845
	if ( m == DEFAULT_COPY || m == DEFAULT_ASSIGN ) {
846
	    /* Dummy value for copy constructor */
847
	    if ( IS_type_ptr ( t ) ) {
848
		DECL_SPEC ds = DEREF_dspec ( id_storage ( fn ) ) ;
849
		if ( !( ds & dspec_trivial ) ) {
850
		    /* Warn about shallow copies */
851
		    err = concat_error ( err, ERR_class_copy_ptr () ) ;
852
		}
853
	    }
854
	    MAKE_exp_value ( t, e ) ;
855
	}
856
    }
857
    if ( !IS_NULL_err ( err ) ) {
858
	constr_error ( err, id, fn, n ) ;
859
    }
860
    return ( e ) ;
861
}
862
 
863
 
864
/*
865
    LISTS OF CONSTRUCTOR INITIALISERS
866
 
867
    These lists are built up by the constructor initialisers.
868
*/
869
 
870
static LIST ( GRAPH ) init_bases = NULL_list ( GRAPH ) ;
871
static LIST ( EXP ) val_bases = NULL_list ( EXP ) ;
872
static LIST ( IDENTIFIER ) init_mems = NULL_list ( IDENTIFIER ) ;
873
static LIST ( EXP ) val_mems = NULL_list ( EXP ) ;
874
static unsigned long no_ctor_init = 0 ;
875
static int init_base_last = 1 ;
876
 
877
 
878
/*
879
    CLEAR THE LISTS OF CONSTRUCTOR INITIALISERS
880
 
881
    This routine clears the lists of constructor initialisers above.
882
*/
883
 
884
static void destroy_ctor_lists
885
    PROTO_Z ()
886
{
887
    IGNORE check_value ( OPT_VAL_ctor_initializers, no_ctor_init ) ;
888
    DESTROY_list ( init_bases, SIZE_graph ) ;
889
    DESTROY_list ( val_bases, SIZE_exp ) ;
890
    DESTROY_list ( init_mems, SIZE_id ) ;
891
    DESTROY_list ( val_mems, SIZE_exp ) ;
892
    init_bases = NULL_list ( GRAPH ) ;
893
    val_bases = NULL_list ( EXP ) ;
894
    init_mems = NULL_list ( IDENTIFIER ) ;
895
    val_mems = NULL_list ( EXP ) ;
896
    init_base_last = 1 ;
897
    no_ctor_init = 0 ;
898
    return ;
899
}
900
 
901
 
902
/*
903
    FIND A BASE CLASS INITIALISER
904
 
905
    This routine finds the base class initialiser for gr in the lists
906
    given above.  If gr is a member of init_bases then the corresponding
907
    element of val_bases is returned, otherwise the null expression is
908
    returned.
909
*/
910
 
911
static EXP find_base_init
912
    PROTO_N ( ( gr ) )
913
    PROTO_T ( GRAPH gr )
914
{
915
    LIST ( GRAPH ) p = init_bases ;
916
    LIST ( EXP ) q = val_bases ;
917
    while ( !IS_NULL_list ( p ) ) {
918
	GRAPH gs = DEREF_graph ( HEAD_list ( p ) ) ;
919
	if ( eq_graph ( gs, gr ) ) {
920
	    /* Found graph - return corresponding expression */
921
	    EXP e = DEREF_exp ( HEAD_list ( q ) ) ;
922
	    return ( e ) ;
923
	}
924
	q = TAIL_list ( q ) ;
925
	p = TAIL_list ( p ) ;
926
    }
927
    return ( NULL_exp ) ;
928
}
929
 
930
 
931
/*
932
    FIND A CLASS MEMBER INITIALISER
933
 
934
    This routine finds the class member initialiser for id in the lists
935
    given above.  If id is a member of init_mems then the corresponding
936
    element of val_mems is returned, otherwise the null expression is
937
    returned.
938
*/
939
 
940
static EXP find_mem_init
941
    PROTO_N ( ( id ) )
942
    PROTO_T ( IDENTIFIER id )
943
{
944
    LIST ( IDENTIFIER ) p = init_mems ;
945
    LIST ( EXP ) q = val_mems ;
946
    while ( !IS_NULL_list ( p ) ) {
947
	IDENTIFIER mid = DEREF_id ( HEAD_list ( p ) ) ;
948
	if ( EQ_id ( mid, id ) ) {
949
	    /* Found identifier - return corresponding expression */
950
	    EXP e = DEREF_exp ( HEAD_list ( q ) ) ;
951
	    return ( e ) ;
952
	}
953
	q = TAIL_list ( q ) ;
954
	p = TAIL_list ( p ) ;
955
    }
956
    return ( NULL_exp ) ;
957
}
958
 
959
 
960
/*
961
    MARK A DESTRUCTOR
962
 
963
    This routine marks the initialiser expression e for an object of type t
964
    if t has a non-trivial destructor by enclosing it in parentheses.
965
*/
966
 
967
static EXP destr_init
968
    PROTO_N ( ( t, e ) )
969
    PROTO_T ( TYPE t X EXP e )
970
{
971
    TYPE s = t ;
972
    while ( IS_type_array ( s ) ) {
973
	s = DEREF_type ( type_array_sub ( s ) ) ;
974
    }
975
    if ( IS_type_compound ( s ) ) {
976
	CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
977
	IDENTIFIER id = DEREF_id ( ctype_destr ( cs ) ) ;
978
	if ( IS_id_mem_func ( id ) ) {
979
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
980
	    if ( !( ds & dspec_trivial ) ) {
981
		if ( ds & dspec_implicit ) {
982
		    if ( !( ds & dspec_defn ) ) {
983
			/* Trivial constructor */
984
			implicit_defn ( id, DEFAULT_DESTR ) ;
985
		    }
986
		}
987
		MAKE_exp_paren ( t, e, e ) ;
988
	    }
989
	}
990
    }
991
    return ( e ) ;
992
}
993
 
994
 
995
/*
996
    CREATE A CONSTRUCTOR OR DESTRUCTOR
997
 
998
    This routine creates a constructor or destructor initialiser list.
999
    cns gives the corresponding class namespace and fn is the function
1000
    name.  n and m indicate the constructor or destructor type.  This can
1001
    be an implicitly declared constructor, destructor or assignment
1002
    function, or an explicitly declared constructor.  In the latter
1003
    case the ctor-initialiser lists above are used to indicate the
1004
    initialiser values.
1005
*/
1006
 
1007
static EXP make_constr
1008
    PROTO_N ( ( cns, fn, n, m ) )
1009
    PROTO_T ( NAMESPACE cns X IDENTIFIER fn X int n X int m )
1010
{
1011
    EXP r ;
1012
    int usr = 0 ;
1013
    int str = 1 ;
1014
    unsigned nv = 0 ;
1015
    unsigned nb = 0 ;
1016
    unsigned long no = 0 ;
1017
    int templ = in_template_decl ;
1018
    LIST ( EXP ) p = NULL_list ( EXP ) ;
1019
    LIST ( OFFSET ) q = NULL_list ( OFFSET ) ;
1020
 
1021
    /* Deconstruct class type */
1022
    CLASS_TYPE ct = namespace_class ( cns ) ;
1023
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1024
    GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
1025
    LIST ( GRAPH ) br = DEREF_list ( graph_tails ( gr ) ) ;
1026
    LIST ( GRAPH ) bv = DEREF_list ( ctype_vbase ( ct ) ) ;
1027
    MEMBER mem = DEREF_member ( nspace_ctype_first ( cns ) ) ;
1028
 
1029
    /* Mark 'this' parameter as used */
1030
    crt_access_list.inherit++ ;
1031
    IGNORE this_param ( fn, 1 ) ;
1032
 
1033
    /* Check for user-defined constructors */
1034
    if ( n == DEFAULT_USR && m == DEFAULT_CONSTR ) usr = 1 ;
1035
 
1036
    /* Initialise virtual bases */
1037
    if ( m != DEFAULT_ASSIGN ) {
1038
	while ( !IS_NULL_list ( bv ) ) {
1039
	    EXP e = NULL_exp ;
1040
	    GRAPH gs = DEREF_graph ( HEAD_list ( bv ) ) ;
1041
	    DECL_SPEC acc = DEREF_dspec ( graph_access ( gs ) ) ;
1042
	    if ( usr ) e = find_base_init ( gs ) ;
1043
	    if ( IS_NULL_exp ( e ) && !( acc & dspec_template ) ) {
1044
		e = init_empty_base ( gs, fn, n, m ) ;
1045
		if ( templ ) {
1046
		    /* Only testing for template classes */
1047
		    free_exp ( e, 1 ) ;
1048
		    e = NULL_exp ;
1049
		}
1050
	    }
1051
	    if ( m == DEFAULT_CONSTR && !templ ) {
1052
		CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
1053
		TYPE s = make_class_type ( cs ) ;
1054
		e = destr_init ( s, e ) ;
1055
	    }
1056
	    if ( !IS_NULL_exp ( e ) ) {
1057
		OFFSET off = DEREF_off ( graph_off ( gs ) ) ;
1058
		CONS_off ( off, q, q ) ;
1059
		CONS_exp ( e, p, p ) ;
1060
		nv++ ;
1061
	    }
1062
	    bv = TAIL_list ( bv ) ;
1063
	}
1064
    }
1065
 
1066
    /* Initialise direct bases */
1067
    while ( !IS_NULL_list ( br ) ) {
1068
	EXP e = NULL_exp ;
1069
	GRAPH gs = DEREF_graph ( HEAD_list ( br ) ) ;
1070
	DECL_SPEC acc = DEREF_dspec ( graph_access ( gs ) ) ;
1071
	if ( !( acc & dspec_virtual ) || m == DEFAULT_ASSIGN ) {
1072
	    if ( usr ) e = find_base_init ( gs ) ;
1073
	    if ( IS_NULL_exp ( e ) && !( acc & dspec_template ) ) {
1074
		e = init_empty_base ( gs, fn, n, m ) ;
1075
		if ( templ ) {
1076
		    /* Only testing for template classes */
1077
		    free_exp ( e, 1 ) ;
1078
		    e = NULL_exp ;
1079
		}
1080
	    }
1081
	    if ( m == DEFAULT_CONSTR && !templ ) {
1082
		CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
1083
		TYPE s = make_class_type ( cs ) ;
1084
		e = destr_init ( s, e ) ;
1085
	    }
1086
	    if ( !IS_NULL_exp ( e ) ) {
1087
		OFFSET off = DEREF_off ( graph_off ( gs ) ) ;
1088
		CONS_off ( off, q, q ) ;
1089
		CONS_exp ( e, p, p ) ;
1090
		nb++ ;
1091
	    }
1092
	}
1093
	br = TAIL_list ( br ) ;
1094
    }
1095
 
1096
    /* Initialise data members */
1097
    member_no = no ;
1098
    mem = next_data_member ( mem, 2 ) ;
1099
    if ( usr && ( ci & cinfo_union ) ) {
1100
	/* Check union initialisers */
1101
	unsigned ni = LENGTH_list ( init_mems ) ;
1102
	if ( ni ) {
1103
	    if ( ni > 1 ) {
1104
		/* More than one initialiser for union */
1105
		report ( crt_loc, ERR_class_base_init_union ( ct ) ) ;
1106
	    }
1107
	    str = 0 ;
1108
	}
1109
    }
1110
    while ( !IS_NULL_member ( mem ) ) {
1111
	EXP e = NULL_exp ;
1112
	IDENTIFIER id = DEREF_id ( member_id ( mem ) ) ;
1113
	if ( usr ) {
1114
	    e = find_mem_init ( id ) ;
1115
	    if ( !IS_NULL_exp ( e ) ) {
1116
		if ( no == member_no ) {
1117
		    /* More than one initialiser for anonymous union */
1118
		    report ( crt_loc, ERR_class_base_init_anon ( id ) ) ;
1119
		}
1120
		no = member_no ;
1121
	    }
1122
	}
1123
	if ( IS_NULL_exp ( e ) && str ) {
1124
	    e = init_empty_mem ( id, fn, n, m ) ;
1125
	    if ( templ ) {
1126
		/* Only testing for template classes */
1127
		free_exp ( e, 1 ) ;
1128
		e = NULL_exp ;
1129
	    }
1130
	}
1131
	if ( m == DEFAULT_CONSTR && !templ ) {
1132
	    TYPE s = DEREF_type ( id_member_type ( id ) ) ;
1133
	    e = destr_init ( s, e ) ;
1134
	}
1135
	if ( !IS_NULL_exp ( e ) ) {
1136
	    OFFSET off = DEREF_off ( id_member_off ( id ) ) ;
1137
	    e = check_init ( e ) ;
1138
	    CONS_off ( off, q, q ) ;
1139
	    CONS_exp ( e, p, p ) ;
1140
	}
1141
	if ( ci & cinfo_union ) {
1142
	    if ( !usr ) break ;
1143
	    str = 0 ;
1144
	}
1145
	mem = DEREF_member ( member_next ( mem ) ) ;
1146
	mem = next_data_member ( mem, 2 ) ;
1147
    }
1148
 
1149
    /* Construct the result */
1150
    crt_access_list.inherit-- ;
1151
    if ( IS_NULL_list ( p ) ) {
1152
	DECL_SPEC ds = DEREF_dspec ( id_storage ( fn ) ) ;
1153
	if ( ds & dspec_trivial ) return ( NULL_exp ) ;
1154
	if ( m != DEFAULT_DESTR && !templ ) {
1155
	    if ( ci & ( cinfo_virtual_base | cinfo_polymorphic ) ) {
1156
		/* These require an initialiser */
1157
		/* EMPTY */
1158
	    } else {
1159
		return ( NULL_exp ) ;
1160
	    }
1161
	}
1162
    }
1163
    if ( m != DEFAULT_DESTR ) {
1164
	/* Initialisers are built in reverse order */
1165
	p = REVERSE_list ( p ) ;
1166
	q = REVERSE_list ( q ) ;
1167
    }
1168
    MAKE_exp_initialiser ( type_void, p, q, m, nv, nb, r ) ;
1169
    return ( r ) ;
1170
}
1171
 
1172
 
1173
/*
1174
    CREATE A DESTRUCTOR PRELUDE EXPRESSION
1175
 
1176
    The destruction of the base classes and members of a class takes
1177
    place at the end of the destructor, and it is this which is handled
1178
    by make_constr.  However for polymorphic classes some action is
1179
    also required at the start of the destructor, namely the resetting
1180
    of the virtual function tables.  This routine creates a dummy
1181
    initialiser expression which performs this action.
1182
*/
1183
 
1184
static EXP make_destr_prelude
1185
    PROTO_N ( ( cns ) )
1186
    PROTO_T ( NAMESPACE cns )
1187
{
1188
    EXP r = NULL_exp ;
1189
    CLASS_TYPE ct = namespace_class ( cns ) ;
1190
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1191
    if ( ci & cinfo_polymorphic ) {
1192
	/* Create dummy initialiser */
1193
	MAKE_exp_initialiser ( type_void, NULL_list ( EXP ),
1194
		NULL_list ( OFFSET ), DEFAULT_PRELUDE, 0, 0, r ) ;
1195
    }
1196
    return ( r ) ;
1197
}
1198
 
1199
 
1200
/*
1201
    COPY A CONSTRUCTOR INITIALISER LIST
1202
 
1203
    This routine copies the constructor initialiser list of type m.
1204
*/
1205
 
1206
EXP copy_ctor
1207
    PROTO_N ( ( e, m ) )
1208
    PROTO_T ( EXP e X int m )
1209
{
1210
    IDENTIFIER fn = crt_func_id ;
1211
    NAMESPACE cns = DEREF_nspace ( id_parent ( fn ) ) ;
1212
    LIST ( EXP ) p = DEREF_list ( exp_initialiser_args ( e ) ) ;
1213
    LIST ( OFFSET ) q = DEREF_list ( exp_initialiser_offs ( e ) ) ;
1214
    if ( !IS_NULL_list ( p ) ) {
1215
	/* Copy initialisers */
1216
	while ( !IS_NULL_list ( p ) && !IS_NULL_list ( q ) ) {
1217
	    int redo = 0 ;
1218
	    IDENTIFIER id = NULL_id ;
1219
	    EXP a = DEREF_exp ( HEAD_list ( p ) ) ;
1220
	    OFFSET off = DEREF_off ( HEAD_list ( q ) ) ;
1221
	    EXP b = implicit_cast_exp ( a ) ;
1222
	    if ( !IS_NULL_exp ( b ) && !EQ_exp ( b, a ) ) {
1223
		redo = 1 ;
1224
		a = b ;
1225
	    }
1226
	    a = copy_exp ( a, NULL_type, NULL_type ) ;
1227
	    if ( redo && !IS_exp_initialiser ( a ) ) {
1228
		/* Turn expression into ctor-initialiser list */
1229
		LIST ( EXP ) r = NULL_list ( EXP ) ;
1230
		LIST ( OFFSET ) s = NULL_list ( OFFSET ) ;
1231
		CONS_exp ( a, r, r ) ;
1232
		MAKE_exp_initialiser ( type_void, r, s, 0, 0, 0, a ) ;
1233
	    }
1234
	    off = copy_offset ( off, lex_plus ) ;
1235
	    switch ( TAG_off ( off ) ) {
1236
		case off_member_tag : {
1237
		    /* Member initialiser */
1238
		    id = DEREF_id ( off_member_id ( off ) ) ;
1239
		    break ;
1240
		}
1241
		case off_base_tag : {
1242
		    /* Direct base class initialiser */
1243
		    GRAPH gr = DEREF_graph ( off_base_graph ( off ) ) ;
1244
		    CLASS_TYPE ct = DEREF_ctype ( graph_head ( gr ) ) ;
1245
		    id = DEREF_id ( ctype_name ( ct ) ) ;
1246
		    break ;
1247
		}
1248
		case off_deriv_tag : {
1249
		    /* Indirect base class initialiser */
1250
		    GRAPH gr = DEREF_graph ( off_deriv_graph ( off ) ) ;
1251
		    CLASS_TYPE ct = DEREF_ctype ( graph_head ( gr ) ) ;
1252
		    id = DEREF_id ( ctype_name ( ct ) ) ;
1253
		    break ;
1254
		}
1255
	    }
1256
	    if ( !IS_NULL_id ( id ) ) {
1257
		/* Add initialiser to list */
1258
		ctor_initialise ( cns, id, a ) ;
1259
	    }
1260
	    q = TAIL_list ( q ) ;
1261
	    p = TAIL_list ( p ) ;
1262
	}
1263
    }
1264
    e = make_constr ( cns, fn, DEFAULT_USR, m ) ;
1265
    destroy_ctor_lists () ;
1266
    return ( e ) ;
1267
}
1268
 
1269
 
1270
/*
1271
    BEGIN A CONSTRUCTOR INITIALISER LIST
1272
 
1273
    This routine is called at the start of a ctor-initialiser list to
1274
    check that the current function is a constructor.  If so it returns
1275
    the corresponding class namespace.  Otherwise the null namespace
1276
    is returned.
1277
*/
1278
 
1279
NAMESPACE ctor_begin
1280
    PROTO_Z ()
1281
{
1282
    IDENTIFIER id = crt_func_id ;
1283
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1284
    if ( IS_hashid_constr ( nm ) ) {
1285
	TYPE t = DEREF_type ( hashid_constr_type ( nm ) ) ;
1286
	if ( IS_type_compound ( t ) ) {
1287
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1288
	    NAMESPACE cns = DEREF_nspace ( ctype_member ( ct ) ) ;
1289
	    return ( cns ) ;
1290
	}
1291
    }
1292
    report ( crt_loc, ERR_class_base_init_bad ( id ) ) ;
1293
    return ( NULL_nspace ) ;
1294
}
1295
 
1296
 
1297
/*
1298
    END A CONSTRUCTOR INITIALISER LIST
1299
 
1300
    This routine is called at the end of a ctor-initialiser list.  cns
1301
    gives the class namespace, e is the compound statement to which
1302
    the initialisers are to be added and elem is true if the list was
1303
    not empty.  The routine returns the resultant compound statement.
1304
*/
1305
 
1306
EXP ctor_end
1307
    PROTO_N ( ( cns, e, elem ) )
1308
    PROTO_T ( NAMESPACE cns X EXP e X int elem )
1309
{
1310
    if ( !IS_NULL_nspace ( cns ) ) {
1311
	/* Construct the constructor */
1312
	IDENTIFIER id = crt_func_id ;
1313
	if ( !elem ) report ( crt_loc, ERR_class_base_init_empty ( id ) ) ;
1314
	if ( IS_id_mem_func ( id ) ) {
1315
	    EXP c = make_constr ( cns, id, DEFAULT_USR, DEFAULT_CONSTR ) ;
1316
	    if ( !IS_NULL_exp ( c ) ) {
1317
		e = add_compound_stmt ( e, c ) ;
1318
	    }
1319
	}
1320
	destroy_ctor_lists () ;
1321
    }
1322
    return ( e ) ;
1323
}
1324
 
1325
 
1326
/*
1327
    HANDLE AN ABSENT CONSTRUCTOR INITIALISER LIST
1328
 
1329
    This routine is called for a function definition which contains no
1330
    ctor-initialiser.  e gives the compound statement giving the function
1331
    body.  The routine returns the value of e after any initialisers
1332
    required for constructors have been added.  Any destructors which need
1333
    to be called at the end of the function are returned via p.
1334
*/
1335
 
1336
EXP ctor_none
1337
    PROTO_N ( ( e, p ) )
1338
    PROTO_T ( EXP e X EXP *p )
1339
{
1340
    IDENTIFIER id = crt_func_id ;
1341
    if ( IS_id_mem_func ( id ) ) {
1342
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1343
	switch ( TAG_hashid ( nm ) ) {
1344
	    case hashid_constr_tag : {
1345
		/* Constructor declarations */
1346
		NAMESPACE cns = DEREF_nspace ( id_parent ( id ) ) ;
1347
		EXP c = make_constr ( cns, id, DEFAULT_USR, DEFAULT_CONSTR ) ;
1348
		if ( !IS_NULL_exp ( c ) ) {
1349
		    /* Add initialiser to function body */
1350
		    e = add_compound_stmt ( e, c ) ;
1351
		}
1352
		break ;
1353
	    }
1354
	    case hashid_destr_tag : {
1355
		/* Destructor declarations */
1356
		NAMESPACE cns = DEREF_nspace ( id_parent ( id ) ) ;
1357
		EXP c = make_destr_prelude ( cns ) ;
1358
		EXP d = make_constr ( cns, id, DEFAULT_USR, DEFAULT_DESTR ) ;
1359
		if ( !IS_NULL_exp ( c ) ) {
1360
		    /* Add destructor prelude to function body */
1361
		    e = add_compound_stmt ( e, c ) ;
1362
		}
1363
		if ( !IS_NULL_exp ( d ) ) {
1364
		    /* Set up destructor postlude */
1365
		    IDENTIFIER lab = postlude_label () ;
1366
		    EXP a = begin_label_stmt ( lab, lex_return ) ;
1367
		    *p = end_label_stmt ( a, d ) ;
1368
		}
1369
		break ;
1370
	    }
1371
	}
1372
    }
1373
    return ( e ) ;
1374
}
1375
 
1376
 
1377
/*
1378
    ADD A POSTLUDE TO A FUNCTION BODY
1379
 
1380
    This routine adds the postlude expression d to the end of the
1381
    function body e.  This is used in user declared destructors where the
1382
    default destructors need to be called after the main function body.
1383
    For convenience the code for falling out of a normal function body
1384
    is also handled by this routine.
1385
*/
1386
 
1387
EXP ctor_postlude
1388
    PROTO_N ( ( e, d ) )
1389
    PROTO_T ( EXP e X EXP d )
1390
{
1391
    if ( !IS_NULL_exp ( d ) ) {
1392
	/* Add postlude expression */
1393
	EXP r ;
1394
	unreached_code = 0 ;
1395
	e = add_compound_stmt ( e, d ) ;
1396
	unreached_code = 0 ;
1397
	MAKE_exp_return_stmt ( type_bottom, NULL_exp, r ) ;
1398
	e = add_compound_stmt ( e, r ) ;
1399
	unreached_code = 1 ;
1400
	unreached_last = 0 ;
1401
    } else {
1402
	/* Fall out of function */
1403
	if ( !unreached_code ) {
1404
	    EXP r = fall_return_stmt () ;
1405
	    e = add_compound_stmt ( e, r ) ;
1406
	}
1407
    }
1408
    return ( e ) ;
1409
}
1410
 
1411
 
1412
/*
1413
    CREATE AN EXCEPTION POSTLUDE EXPRESSION
1414
 
1415
    This routine creates an expression which will be called in the exception
1416
    specifier for the function id.  This is used in constructors to destroy
1417
    the partially complete object.
1418
*/
1419
 
1420
EXP except_postlude
1421
    PROTO_N ( ( id ) )
1422
    PROTO_T ( IDENTIFIER id )
1423
{
1424
    EXP e = NULL_exp ;
1425
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1426
    if ( IS_hashid_constr ( nm ) ) {
1427
	/* Have a constructor */
1428
	TYPE t = DEREF_type ( hashid_constr_type ( nm ) ) ;
1429
	if ( IS_type_compound ( t ) ) {
1430
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1431
	    NAMESPACE cns = DEREF_nspace ( ctype_member ( ct ) ) ;
1432
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1433
	    if ( !( ds & ( dspec_friend | dspec_trivial ) ) ) {
1434
		/* Constructor may throw an exception */
1435
		int ac = do_access_checks ;
1436
		EXP a = DEREF_exp ( id_function_etc_defn ( id ) ) ;
1437
		if ( !IS_NULL_exp ( a ) && IS_exp_initialiser ( a ) ) {
1438
		    LIST ( EXP ) p ;
1439
		    p = DEREF_list ( exp_initialiser_args ( a ) ) ;
1440
		    if ( LENGTH_list ( p ) == 1 ) {
1441
			/* Single initialiser in constructor */
1442
			a = DEREF_exp ( HEAD_list ( p ) ) ;
1443
			if ( !IS_NULL_exp ( a ) ) {
1444
			    TYPE s = DEREF_type ( exp_type ( a ) ) ;
1445
			    if ( !IS_type_array ( s ) ) return ( e ) ;
1446
			}
1447
		    }
1448
		}
1449
		do_access_checks = 0 ;
1450
		COPY_dspec ( id_storage ( id ), ( ds | dspec_trivial ) ) ;
1451
		start_try_check ( univ_type_set ) ;
1452
		e = make_constr ( cns, id, DEFAULT_DESTR, DEFAULT_DESTR ) ;
1453
		if ( !IS_NULL_exp ( e ) ) {
1454
		    /* Add destructor prelude */
1455
		    EXP d = make_destr_prelude ( cns ) ;
1456
		    e = join_exp ( d, e ) ;
1457
		}
1458
		e = end_try_check ( id, e ) ;
1459
		COPY_dspec ( id_storage ( id ), ds ) ;
1460
		do_access_checks = ac ;
1461
	    }
1462
	}
1463
    }
1464
    return ( e ) ;
1465
}
1466
 
1467
 
1468
/*
1469
    FIND INITIALISATION ORDER OF TWO BASE GRAPHS
1470
 
1471
    This routine compares the order of initialisation of the base graphs
1472
    gr and gs.  It returns 1 if gr is initialised before gs, -1 if gr is
1473
    initialised before gr and 0 if they are equal.  Note that virtual
1474
    bases are initialised before direct bases.
1475
*/
1476
 
1477
static int compare_base
1478
    PROTO_N ( ( gr, gs ) )
1479
    PROTO_T ( GRAPH gr X GRAPH gs )
1480
{
1481
    DECL_SPEC ar, as ;
1482
    LIST ( GRAPH ) br ;
1483
    if ( eq_graph ( gr, gs ) ) return ( 0 ) ;
1484
    ar = DEREF_dspec ( graph_access ( gr ) ) ;
1485
    as = DEREF_dspec ( graph_access ( gs ) ) ;
1486
    if ( ar & dspec_virtual ) {
1487
	if ( as & dspec_virtual ) {
1488
	    GRAPH gt = DEREF_graph ( graph_top ( gr ) ) ;
1489
	    CLASS_TYPE ct = DEREF_ctype ( graph_head ( gt ) ) ;
1490
	    br = DEREF_list ( ctype_vbase ( ct ) ) ;
1491
	} else {
1492
	    return ( 1 ) ;
1493
	}
1494
    } else {
1495
	if ( as & dspec_virtual ) {
1496
	    return ( -1 ) ;
1497
	} else {
1498
	    GRAPH gt = DEREF_graph ( graph_top ( gr ) ) ;
1499
	    br = DEREF_list ( graph_tails ( gt ) ) ;
1500
	}
1501
    }
1502
    while ( !IS_NULL_list ( br ) ) {
1503
	GRAPH gt = DEREF_graph ( HEAD_list ( br ) ) ;
1504
	if ( eq_graph ( gt, gr ) ) return ( 1 ) ;
1505
	if ( eq_graph ( gt, gs ) ) return ( -1 ) ;
1506
	br = TAIL_list ( br ) ;
1507
    }
1508
    return ( 0 ) ;
1509
}
1510
 
1511
 
1512
/*
1513
    FIND INITIALISATION ORDER OF TWO MEMBERS
1514
 
1515
    This routine compares the order of initialisation of the members pid
1516
    and mid of the class namespace ns.  It returns 1 if mid is initialised
1517
    before pid, -1 if pid is initialised before mid and 0 if they are equal.
1518
*/
1519
 
1520
static int compare_mem
1521
    PROTO_N ( ( ns, mid, pid ) )
1522
    PROTO_T ( NAMESPACE ns X IDENTIFIER mid X IDENTIFIER pid )
1523
{
1524
    MEMBER mem ;
1525
    if ( EQ_id ( mid, pid ) ) return ( 0 ) ;
1526
    mem = DEREF_member ( nspace_ctype_first ( ns ) ) ;
1527
    while ( !IS_NULL_member ( mem ) ) {
1528
	IDENTIFIER id = DEREF_id ( member_id ( mem ) ) ;
1529
	if ( EQ_id ( id, mid ) ) return ( 1 ) ;
1530
	if ( EQ_id ( id, pid ) ) return ( -1 ) ;
1531
	mem = DEREF_member ( member_next ( mem ) ) ;
1532
    }
1533
    return ( 0 ) ;
1534
}
1535
 
1536
 
1537
/*
1538
    NAME LOOK-UP FOR CONSTRUCTOR INITIALISER
1539
 
1540
    This routine looks up the name id as a ctor-initialiser for the class
1541
    cns.  If it denotes a base class then the corresponding graph is
1542
    returned via pgr.  If id is the null identifier then this is an
1543
    anachronistic way of initialising the unique direct base class of
1544
    cns, if this exists.  The null identifier is returned for illegal
1545
    initialisers.
1546
*/
1547
 
1548
static IDENTIFIER ctor_field
1549
    PROTO_N ( ( cns, id, pgr ) )
1550
    PROTO_T ( NAMESPACE cns X IDENTIFIER id X GRAPH *pgr )
1551
{
1552
    if ( !IS_NULL_nspace ( cns ) ) {
1553
	if ( IS_NULL_id ( id ) ) {
1554
	    /* Search for unique base class */
1555
	    ERROR err = ERR_class_base_init_old ( crt_func_id ) ;
1556
	    CLASS_TYPE ct = namespace_class ( cns ) ;
1557
	    GRAPH gr = uniq_base_class ( ct, &err ) ;
1558
	    if ( !IS_NULL_graph ( gr ) ) {
1559
		ct = DEREF_ctype ( graph_head ( gr ) ) ;
1560
		id = DEREF_id ( ctype_name ( ct ) ) ;
1561
		*pgr = gr ;
1562
	    } else {
1563
		id = NULL_id ;
1564
	    }
1565
	    report ( crt_loc, err ) ;
1566
 
1567
	} else {
1568
	    /* Look up name as class member */
1569
	    GRAPH gr ;
1570
	    int check_base = 1 ;
1571
	    IDENTIFIER fid = id ;
1572
	    NAMESPACE fns = DEREF_nspace ( id_parent ( fid ) ) ;
1573
	    if ( EQ_nspace ( fns, crt_namespace ) ) {
1574
		/* Rescan constructor parameters in enclosing scope */
1575
		HASHID nm = DEREF_hashid ( id_name ( fid ) ) ;
1576
		int c = cache_lookup ;
1577
		cache_lookup = 0 ;
1578
		remove_namespace () ;
1579
		fid = find_id ( nm ) ;
1580
		add_namespace ( fns ) ;
1581
		cache_lookup = c ;
1582
		id = fid ;
1583
	    }
1584
 
1585
	    /* Check for class members */
1586
	    gr = is_subfield ( cns, fid ) ;
1587
	    if ( !IS_NULL_graph ( gr ) ) {
1588
		check_base = 0 ;
1589
		fid = search_subfield ( cns, gr, fid ) ;
1590
		switch ( TAG_id ( fid ) ) {
1591
		    case id_member_tag : {
1592
			/* Non-static data members */
1593
			DECL_SPEC ds = DEREF_dspec ( id_storage ( fid ) ) ;
1594
			if ( ds & dspec_inherit ) {
1595
			    /* Can't denote an inherited member */
1596
			    ERROR err ;
1597
			    err = ERR_class_base_init_inherit ( fid ) ;
1598
			    report ( crt_loc, err ) ;
1599
			    id = NULL_id ;
1600
			} else {
1601
			    id = fid ;
1602
			}
1603
			break ;
1604
		    }
1605
		    case id_class_name_tag :
1606
		    case id_class_alias_tag : {
1607
			/* Check for base classes */
1608
			check_base = 1 ;
1609
			break ;
1610
		    }
1611
		    case id_undef_tag : {
1612
			/* Undeclared members */
1613
			HASHID nm = DEREF_hashid ( id_name ( fid ) ) ;
1614
			ERROR err = ERR_lookup_qual_undef ( nm, fns ) ;
1615
			report ( crt_loc, err ) ;
1616
			id = NULL_id ;
1617
			break ;
1618
		    }
1619
		    case id_ambig_tag : {
1620
			/* Ambiguous members */
1621
			id = report_ambiguous ( fid, 0, 1, 1 ) ;
1622
			break ;
1623
		    }
1624
		    default : {
1625
			/* Other members */
1626
			ERROR err = ERR_class_base_init_static ( fid ) ;
1627
			report ( crt_loc, err ) ;
1628
			id = NULL_id ;
1629
			break ;
1630
		    }
1631
		}
1632
	    }
1633
 
1634
	    /* Check for base classes */
1635
	    if ( check_base ) {
1636
		CLASS_TYPE cs = find_class ( fid ) ;
1637
		if ( IS_NULL_ctype ( cs ) && IS_id_undef ( fid ) ) {
1638
		    TYPE s = check_typename ( fns, fid, btype_class ) ;
1639
		    if ( !IS_NULL_type ( s ) && IS_type_compound ( s ) ) {
1640
			cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1641
		    }
1642
		}
1643
		if ( !IS_NULL_ctype ( cs ) ) {
1644
		    /* Class name found */
1645
		    ERROR err = NULL_err ;
1646
		    CLASS_TYPE ct = namespace_class ( cns ) ;
1647
		    gr = direct_base_class ( ct, cs, &err ) ;
1648
		    if ( !IS_NULL_err ( err ) ) {
1649
			/* Report invalid bases */
1650
			report ( crt_loc, err ) ;
1651
		    }
1652
		    if ( !IS_NULL_graph ( gr ) ) {
1653
			id = DEREF_id ( ctype_name ( cs ) ) ;
1654
			*pgr = gr ;
1655
		    } else {
1656
			id = NULL_id ;
1657
		    }
1658
		} else {
1659
		    /* Invalid ctor-initialiser */
1660
		    ERROR err = ERR_lookup_qual_bad ( id, cns ) ;
1661
		    report ( crt_loc, err ) ;
1662
		    id = NULL_id ;
1663
		}
1664
	    }
1665
	}
1666
    } else {
1667
	/* Invalid class namespace */
1668
	id = NULL_id ;
1669
    }
1670
    return ( id ) ;
1671
}
1672
 
1673
 
1674
/*
1675
    PROCESS A CONSTRUCTOR INITIALISER
1676
 
1677
    This routine processes a ctor-initialiser which sets the id component
1678
    of the current constructor function to the initialiser expression init.
1679
    cns gives the value returned by ctor_check.  id may be the null
1680
    identifier indicating the anachronistic single inheritance base class
1681
    initialisation.
1682
*/
1683
 
1684
void ctor_initialise
1685
    PROTO_N ( ( cns, id, init ) )
1686
    PROTO_T ( NAMESPACE cns X IDENTIFIER id X EXP init )
1687
{
1688
    TYPE t ;
1689
    int n = 0 ;
1690
    int ok = 1 ;
1691
    GRAPH gr = NULL_graph ;
1692
 
1693
    /* Check member identifier */
1694
    IDENTIFIER mid = ctor_field ( cns, id, &gr ) ;
1695
    if ( !IS_NULL_id ( mid ) ) {
1696
	EXP e ;
1697
	if ( IS_id_member ( mid ) ) {
1698
	    /* Member initialiser */
1699
	    e = find_mem_init ( mid ) ;
1700
	    t = DEREF_type ( id_member_type ( mid ) ) ;
1701
	    n = 1 ;
1702
	} else {
1703
	    /* Base class initialiser */
1704
	    e = find_base_init ( gr ) ;
1705
	    t = DEREF_type ( id_class_name_etc_defn ( mid ) ) ;
1706
	    if ( in_template_decl ) {
1707
		/* Allow for template parameters */
1708
		DECL_SPEC acc = DEREF_dspec ( graph_access ( gr ) ) ;
1709
		if ( acc & dspec_template ) {
1710
		    CLASS_TYPE ct = DEREF_ctype ( graph_head ( gr ) ) ;
1711
		    t = DEREF_type ( ctype_form ( ct ) ) ;
1712
		}
1713
	    }
1714
	    n = 2 ;
1715
	}
1716
	if ( !IS_NULL_exp ( e ) ) {
1717
	    /* Initialiser already given */
1718
	    report ( crt_loc, ERR_class_base_init_dup ( mid ) ) ;
1719
	}
1720
    } else {
1721
	/* Invalid initialiser */
1722
	HASHID nm = KEYWORD ( lex_zzzz ) ;
1723
	mid = DEREF_id ( hashid_id ( nm ) ) ;
1724
	t = type_error ;
1725
    }
1726
 
1727
    /* Check for order of initialisers */
1728
    if ( init_base_last ) {
1729
	/* Previous initialiser was a base */
1730
	if ( n == 2 ) {
1731
	    if ( !IS_NULL_list ( init_bases ) ) {
1732
		GRAPH gp = DEREF_graph ( HEAD_list ( init_bases ) ) ;
1733
		int cmp = compare_base ( gr, gp ) ;
1734
		if ( cmp > 0 ) ok = 0 ;
1735
	    }
1736
	}
1737
    } else {
1738
	/* Previous initialiser was a member */
1739
	if ( n == 1 ) {
1740
	    if ( !IS_NULL_list ( init_mems ) ) {
1741
		IDENTIFIER pid = DEREF_id ( HEAD_list ( init_mems ) ) ;
1742
		int cmp = compare_mem ( cns, mid, pid ) ;
1743
		if ( cmp > 0 ) ok = 0 ;
1744
	    }
1745
	} else if ( n == 2 ) {
1746
	    ok = 0 ;
1747
	}
1748
    }
1749
    if ( !ok ) {
1750
	/* Initialisers out of sequence */
1751
	report ( crt_loc, ERR_class_base_init_order ( mid ) ) ;
1752
    }
1753
 
1754
    /* Perform initialisation */
1755
    if ( IS_exp_initialiser ( init ) ) {
1756
	crt_access_list.inherit++ ;
1757
	decl_loc = crt_loc ;
1758
	if ( n == 2 ) in_ctor_base_init = 1 ;
1759
	init = init_general ( t, init, mid, 0 ) ;
1760
	in_ctor_base_init = 0 ;
1761
	crt_access_list.inherit-- ;
1762
    }
1763
    if ( n == 1 ) {
1764
	CONS_id ( mid, init_mems, init_mems ) ;
1765
	CONS_exp ( init, val_mems, val_mems ) ;
1766
	init_base_last = 0 ;
1767
    } else if ( n == 2 ) {
1768
	CONS_graph ( gr, init_bases, init_bases ) ;
1769
	CONS_exp ( init, val_bases, val_bases ) ;
1770
	init_base_last = 1 ;
1771
    }
1772
    no_ctor_init++ ;
1773
    return ;
1774
}
1775
 
1776
 
1777
/*
1778
    CONSTRUCT A PSEUDO-DESTRUCTOR
1779
 
1780
    This routine creates a pseudo-destructor with class name given by
1781
    id1 and b1 and destructor name given by id2 and b2.
1782
*/
1783
 
1784
IDENTIFIER make_pseudo_destr
1785
    PROTO_N ( ( id1, b1, id2, b2 ) )
1786
    PROTO_T ( IDENTIFIER id1 X BASE_TYPE b1 X IDENTIFIER id2 X BASE_TYPE b2 )
1787
{
1788
    HASHID nm ;
1789
    int create = 0 ;
1790
    int rescan = 0 ;
1791
    TYPE t1 = NULL_type ;
1792
    TYPE t2 = NULL_type ;
1793
    NAMESPACE ns = NULL_nspace ;
1794
 
1795
    /* Check class name */
1796
    if ( b1 == btype_none ) {
1797
	if ( !IS_NULL_id ( id1 ) ) {
1798
	    ns = find_namespace ( id1 ) ;
1799
	    if ( IS_id_class_name_etc ( id1 ) ) {
1800
		t1 = DEREF_type ( id_class_name_etc_defn ( id1 ) ) ;
1801
		t1 = copy_typedef ( id1, t1, cv_none ) ;
1802
		COPY_id ( type_name ( t1 ), id1 ) ;
1803
		if ( IS_NULL_nspace ( ns ) ) {
1804
		    ns = DEREF_nspace ( id_parent ( id1 ) ) ;
1805
		} else {
1806
		    /* Have class name */
1807
		    rescan = 1 ;
1808
		}
1809
		use_id ( id1, 0 ) ;
1810
	    } else {
1811
		if ( !IS_NULL_nspace ( ns ) && !IS_NULL_id ( id2 ) ) {
1812
		    /* Namespace qualifier allowed */
1813
		    create = 1 ;
1814
		} else {
1815
		    report ( crt_loc, ERR_dcl_type_simple_undef ( id1 ) ) ;
1816
		}
1817
	    }
1818
	}
1819
    } else {
1820
	t1 = make_base_type ( b1 ) ;
1821
    }
1822
 
1823
    /* Check destructor name */
1824
    if ( b2 == btype_none ) {
1825
	if ( !EQ_id ( id2, id1 ) ) {
1826
	    nm = DEREF_hashid ( id_name ( id2 ) ) ;
1827
	    if ( !IS_NULL_nspace ( ns ) ) {
1828
		/* Rescan id2 in the context of id1 */
1829
		IDENTIFIER id3 = find_qual_id ( ns, nm, create, 1 ) ;
1830
		if ( !IS_NULL_id ( id3 ) ) id2 = id3 ;
1831
	    }
1832
	    if ( IS_id_class_name_etc ( id2 ) ) {
1833
		t2 = DEREF_type ( id_class_name_etc_defn ( id2 ) ) ;
1834
		t2 = copy_typedef ( id2, t2, cv_none ) ;
1835
		COPY_id ( type_name ( t2 ), id2 ) ;
1836
		use_id ( id2, 0 ) ;
1837
	    } else {
1838
		report ( crt_loc, ERR_dcl_type_simple_undef ( id2 ) ) ;
1839
	    }
1840
	}
1841
    } else {
1842
	if ( b2 != b1 ) t2 = make_base_type ( b2 ) ;
1843
    }
1844
 
1845
    /* Form pseudo-destructor name */
1846
    if ( IS_NULL_type ( t2 ) ) {
1847
	t2 = t1 ;
1848
	if ( IS_NULL_type ( t2 ) ) t2 = type_error ;
1849
    } else {
1850
	if ( rescan ) {
1851
	    /* Error will be caught in search_field */
1852
	    /* EMPTY */
1853
	} else {
1854
	    if ( !IS_NULL_type ( t1 ) && !eq_type ( t1, t2 ) ) {
1855
		/* If both specified, types should match */
1856
		report ( crt_loc, ERR_expr_pseudo_type ( t1, t2 ) ) ;
1857
	    }
1858
	}
1859
    }
1860
    nm = lookup_destr ( t2, id2 ) ;
1861
    id2 = DEREF_id ( hashid_id ( nm ) ) ;
1862
    return ( id2 ) ;
1863
}
1864
 
1865
 
1866
/*
1867
    DECLARATION SPECIFIERS FOR IMPLICIT CONSTRUCTORS
1868
 
1869
    This value gives the declaration specifiers used for implicitly declared
1870
    constructors and destructors.
1871
*/
1872
 
1873
#define dspec_constr	( dspec_implicit | dspec_ignore | dspec_inline )
1874
 
1875
 
1876
/*
1877
    ADD THE EXCEPTIONS THROWN BY A CONSTRUCTOR TO A LIST
1878
 
1879
    This routine adds the list of exceptions thrown by the implicit
1880
    constructor or destructor (given by n) of the class ct to the list p.
1881
*/
1882
 
1883
static LIST ( TYPE ) add_constr_except
1884
    PROTO_N ( ( p, ct, n ) )
1885
    PROTO_T ( LIST ( TYPE ) p X CLASS_TYPE ct X int n )
1886
{
1887
    IDENTIFIER id = find_constr ( ct, n, KILL_err ) ;
1888
    if ( !IS_NULL_id ( id ) && IS_id_mem_func ( id ) ) {
1889
	TYPE fn = DEREF_type ( id_mem_func_type ( id ) ) ;
1890
	if ( IS_type_func ( fn ) ) {
1891
	    LIST ( TYPE ) q = DEREF_list ( type_func_except ( fn ) ) ;
1892
	    p = union_type_set ( p, q ) ;
1893
	}
1894
    }
1895
    return ( p ) ;
1896
}
1897
 
1898
 
1899
/*
1900
    FIND EXCEPTIONS THROWN BY AN IMPLICIT CONSTRUCTOR
1901
 
1902
    This routine finds the list of exceptions thrown by the implicit
1903
    constructor or destructor (given by n) of the class ct.
1904
*/
1905
 
1906
static LIST ( TYPE ) constr_except
1907
    PROTO_N ( ( ct, n ) )
1908
    PROTO_T ( CLASS_TYPE ct X int n )
1909
{
1910
    LIST ( TYPE ) res = NULL_list ( TYPE ) ;
1911
    GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
1912
    LIST ( GRAPH ) br = DEREF_list ( graph_tails ( gr ) ) ;
1913
    NAMESPACE cns = DEREF_nspace ( ctype_member ( ct ) ) ;
1914
    MEMBER mem = DEREF_member ( nspace_ctype_first ( cns ) ) ;
1915
 
1916
    /* Scan through virtual bases */
1917
    if ( n != DEFAULT_ASSIGN ) {
1918
	LIST ( GRAPH ) bv = DEREF_list ( ctype_vbase ( ct ) ) ;
1919
	while ( !IS_NULL_list ( bv ) ) {
1920
	    GRAPH gs = DEREF_graph ( HEAD_list ( bv ) ) ;
1921
	    CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
1922
	    res = add_constr_except ( res, cs, n ) ;
1923
	    if ( EQ_list ( res, univ_type_set ) ) return ( res ) ;
1924
	    bv = TAIL_list ( bv ) ;
1925
	}
1926
    }
1927
 
1928
    /* Scan through direct bases */
1929
    while ( !IS_NULL_list ( br ) ) {
1930
	GRAPH gs = DEREF_graph ( HEAD_list ( br ) ) ;
1931
	DECL_SPEC acc = DEREF_dspec ( graph_access ( gs ) ) ;
1932
	if ( !( acc & dspec_virtual ) || n == DEFAULT_ASSIGN ) {
1933
	    CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
1934
	    res = add_constr_except ( res, cs, n ) ;
1935
	    if ( EQ_list ( res, univ_type_set ) ) return ( res ) ;
1936
	}
1937
	br = TAIL_list ( br ) ;
1938
    }
1939
 
1940
    /* Scan through data members */
1941
    mem = next_data_member ( mem, 2 ) ;
1942
    while ( !IS_NULL_member ( mem ) ) {
1943
	IDENTIFIER id = DEREF_id ( member_id ( mem ) ) ;
1944
	TYPE s = DEREF_type ( id_member_type ( id ) ) ;
1945
	while ( IS_type_array ( s ) ) {
1946
	    s = DEREF_type ( type_array_sub ( s ) ) ;
1947
	}
1948
	if ( IS_type_compound ( s ) ) {
1949
	    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1950
	    res = add_constr_except ( res, cs, n ) ;
1951
	    if ( EQ_list ( res, univ_type_set ) ) return ( res ) ;
1952
	}
1953
	mem = DEREF_member ( member_next ( mem ) ) ;
1954
	mem = next_data_member ( mem, 2 ) ;
1955
    }
1956
    return ( res ) ;
1957
}
1958
 
1959
 
1960
/*
1961
    DECLARE IMPLICIT CONSTRUCTORS AND DESTRUCTORS
1962
 
1963
    This routine is called at the end of a class definition to implicitly
1964
    declare any necessary default constructors, copy constructors, assignment
1965
    operators or destructors.  Note that these are only actually defined
1966
    if they are used, and errors are only detected at this definition stage.
1967
    The routine returns information on the class being defined.
1968
*/
1969
 
1970
CLASS_INFO implicit_decl
1971
    PROTO_N ( ( ct, ci, cds ) )
1972
    PROTO_T ( CLASS_TYPE ct X CLASS_INFO ci X DECL_SPEC cds )
1973
{
1974
    TYPE t ;
1975
    HASHID nm ;
1976
    IDENTIFIER id ;
1977
    TYPE pars [2] ;
1978
    CLASS_INFO cj ;
1979
    LIST ( TYPE ) ex ;
1980
    IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
1981
    NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
1982
    LIST ( IDENTIFIER ) pals = DEREF_list ( ctype_pals ( ct ) ) ;
1983
 
1984
    /* Options being checked */
1985
    int need_copy_constr = 1 ;
1986
    int need_default_constr = 1 ;
1987
    int need_assignment_op = 1 ;
1988
    int need_destructor = 1 ;
1989
    int noncopy_constr = 0 ;
1990
    int access_constr = 0 ;
1991
    int access_destr = 0 ;
1992
    int three_rule = 0 ;
1993
 
1994
    /* Find accumulated information */
1995
    cj = ( ci & cinfo_implicit ) ;
1996
    if ( ci & ( cinfo_virtual_base | cinfo_polymorphic ) ) {
1997
	cj &= ~cinfo_trivial_make ;
1998
    }
1999
    ci &= ~cinfo_implicit ;
2000
 
2001
    /* Check constructors */
2002
    id = DEREF_id ( ctype_constr ( ct ) ) ;
2003
    if ( IS_id_mem_func ( id ) ) {
2004
	IDENTIFIER fid = id ;
2005
	while ( !IS_NULL_id ( fid ) ) {
2006
	    TYPE fn = DEREF_type ( id_mem_func_type ( fid ) ) ;
2007
	    int c = check_copy_constr ( fn, ct ) ;
2008
	    if ( c ) {
2009
		/* Copy constructor */
2010
		if ( c == 2 ) ci |= cinfo_const_copy ;
2011
		need_copy_constr = 0 ;
2012
		three_rule++ ;
2013
	    } else {
2014
		/* Check access for other constructors */
2015
		DECL_SPEC acc = DEREF_dspec ( id_storage ( fid ) ) ;
2016
		if ( ( acc & dspec_access ) != dspec_private ) {
2017
		    access_constr = 1 ;
2018
		}
2019
		noncopy_constr = 1 ;
2020
	    }
2021
	    fid = DEREF_id ( id_mem_func_over ( fid ) ) ;
2022
	}
2023
	ci |= cinfo_usr_constr ;
2024
	need_default_constr = 0 ;
2025
    } else {
2026
	/* Delete non-function meanings */
2027
	nm = DEREF_hashid ( id_name ( id ) ) ;
2028
	clear_member ( ns, nm ) ;
2029
    }
2030
 
2031
    /* Implicit declaration of default constructor */
2032
    if ( need_default_constr ) {
2033
	DECL_SPEC ds = ( dspec_constr | cds ) ;
2034
	if ( cj & cinfo_trivial_constr ) {
2035
	    ci |= cinfo_trivial_constr ;
2036
	    ds |= dspec_trivial ;
2037
	}
2038
	pars [0] = NULL_type ;
2039
	ex = constr_except ( ct, DEFAULT_CONSTR ) ;
2040
	IGNORE declare_func ( ds, id, NULL_type, pars, FUNC_NONE, ex ) ;
2041
	noncopy_constr = 1 ;
2042
	access_constr = 1 ;
2043
    }
2044
 
2045
    /* Implicit declaration of copy constructor */
2046
    if ( need_copy_constr ) {
2047
	CV_SPEC cv = cv_lvalue ;
2048
	DECL_SPEC ds = ( dspec_constr | cds ) ;
2049
	if ( cj & cinfo_trivial_copy ) {
2050
	    ci |= cinfo_trivial_copy ;
2051
	    ds |= dspec_trivial ;
2052
	}
2053
	if ( cj & cinfo_const_copy ) {
2054
	    ci |= cinfo_const_copy ;
2055
	    cv |= cv_const ;
2056
	}
2057
	MAKE_type_compound ( cv, ct, t ) ;
2058
	COPY_id ( type_name ( t ), cid ) ;
2059
	MAKE_type_ref ( cv_none, t, t ) ;
2060
	pars [0] = t ;
2061
	pars [1] = NULL_type ;
2062
	ex = constr_except ( ct, DEFAULT_COPY ) ;
2063
	IGNORE declare_func ( ds, id, NULL_type, pars, FUNC_NONE, ex ) ;
2064
    }
2065
 
2066
    /* Check assignment operators (ignore inheritance) */
2067
    nm = lookup_op ( lex_assign ) ;
2068
    id = search_id ( ns, nm, 0, 0 ) ;
2069
    if ( !IS_NULL_id ( id ) ) {
2070
        if ( IS_id_mem_func ( id ) ) {
2071
	    IDENTIFIER fid = id ;
2072
	    while ( !IS_NULL_id ( fid ) ) {
2073
		TYPE fn = DEREF_type ( id_mem_func_type ( fid ) ) ;
2074
		int c = check_copy_constr ( fn, ct ) ;
2075
		if ( c ) {
2076
		    if ( c == 2 || c == 3 ) ci |= cinfo_const_assign ;
2077
		    need_assignment_op = 0 ;
2078
		    three_rule++ ;
2079
		}
2080
		fid = DEREF_id ( id_mem_func_over ( fid ) ) ;
2081
	    }
2082
	} else {
2083
	    /* Delete non-function meanings */
2084
	    clear_member ( ns, nm ) ;
2085
	}
2086
    } else {
2087
	/* Create a dummy identifier */
2088
	id = DEREF_id ( hashid_id ( nm ) ) ;
2089
    }
2090
 
2091
    /* Implicit declaration of copy assignment operator */
2092
    if ( need_assignment_op ) {
2093
	CV_SPEC cv = cv_lvalue ;
2094
	DECL_SPEC ds = ( dspec_constr | cds ) ;
2095
	if ( cj & cinfo_trivial_assign ) {
2096
	    ci |= cinfo_trivial_assign ;
2097
	    ds |= dspec_trivial ;
2098
	}
2099
	if ( cj & cinfo_const_assign ) {
2100
	    ci |= cinfo_const_assign ;
2101
	    cv |= cv_const ;
2102
	}
2103
	MAKE_type_compound ( cv, ct, t ) ;
2104
	COPY_id ( type_name ( t ), cid ) ;
2105
	MAKE_type_ref ( cv_none, t, t ) ;
2106
	pars [0] = t ;
2107
	pars [1] = NULL_type ;
2108
	MAKE_type_compound ( cv_lvalue, ct, t ) ;
2109
	COPY_id ( type_name ( t ), cid ) ;
2110
	MAKE_type_ref ( cv_none, t, t ) ;
2111
	ex = constr_except ( ct, DEFAULT_ASSIGN ) ;
2112
	IGNORE declare_func ( ds, id, t, pars, FUNC_NONE, ex ) ;
2113
    }
2114
 
2115
    /* Check destructors */
2116
    id = DEREF_id ( ctype_destr ( ct ) ) ;
2117
    if ( IS_id_mem_func ( id ) ) {
2118
	DECL_SPEC acc = DEREF_dspec ( id_storage ( id ) ) ;
2119
	if ( ( acc & dspec_access ) != dspec_private ) {
2120
	    access_destr = 1 ;
2121
	}
2122
	need_destructor = 0 ;
2123
	three_rule++ ;
2124
    } else {
2125
	/* Delete non-function meanings */
2126
	nm = DEREF_hashid ( id_name ( id ) ) ;
2127
	clear_member ( ns, nm ) ;
2128
    }
2129
 
2130
    /* Implicit declaration of default destructor */
2131
    if ( need_destructor ) {
2132
	/* Check for trivial destructors */
2133
	DECL_SPEC ds = ( dspec_constr | cds ) ;
2134
	if ( cj & cinfo_trivial_destr ) {
2135
	    ci |= cinfo_trivial_destr ;
2136
	    ds |= dspec_trivial ;
2137
	}
2138
	pars [0] = NULL_type ;
2139
	ex = constr_except ( ct, DEFAULT_DESTR ) ;
2140
	IGNORE declare_func ( ds, id, NULL_type, pars, FUNC_NONE, ex ) ;
2141
	access_destr = 1 ;
2142
    }
2143
 
2144
    /* Report inaccessible constructors or destructors */
2145
    if ( three_rule != 0 && three_rule != 3 ) {
2146
	report ( crt_loc, ERR_class_dtor_three ( ct ) ) ;
2147
    }
2148
    if ( !noncopy_constr ) {
2149
	report ( crt_loc, ERR_class_ctor_make ( ct ) ) ;
2150
	access_constr = 1 ;
2151
    }
2152
    if ( IS_NULL_list ( pals ) ) {
2153
	if ( !access_constr ) {
2154
	    report ( crt_loc, ERR_class_ctor_private ( ct ) ) ;
2155
	}
2156
	if ( !access_destr ) {
2157
	    report ( crt_loc, ERR_class_dtor_private ( ct ) ) ;
2158
	}
2159
    }
2160
    return ( ci ) ;
2161
}
2162
 
2163
 
2164
/*
2165
    FIND A CONSTRUCTOR KIND
2166
 
2167
    This routine finds the kind of constructor for the function id of
2168
    type t.
2169
*/
2170
 
2171
int constr_kind
2172
    PROTO_N ( ( id, t ) )
2173
    PROTO_T ( IDENTIFIER id X TYPE t )
2174
{
2175
    int n = DEFAULT_USR ;
2176
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2177
    switch ( TAG_hashid ( nm ) ) {
2178
	case hashid_constr_tag : {
2179
	    if ( min_no_args ( t ) == 1 ) {
2180
		n = DEFAULT_CONSTR ;
2181
	    } else {
2182
		n = DEFAULT_COPY ;
2183
	    }
2184
	    break ;
2185
	}
2186
	case hashid_destr_tag : {
2187
	    /* Implicit default destructor */
2188
	    n = DEFAULT_DESTR ;
2189
	    break ;
2190
	}
2191
	case hashid_op_tag : {
2192
	    /* Implicit assignment operator */
2193
	    n = DEFAULT_ASSIGN ;
2194
	    break ;
2195
	}
2196
    }
2197
    return ( n ) ;
2198
}
2199
 
2200
 
2201
/*
2202
    DEFINE AN IMPLICIT CONSTRUCTOR OR DESTRUCTOR
2203
 
2204
    This routine defines the function id, which will be one of the
2205
    implicitly declared constructors or destructors declared in
2206
    implicit_decl.  n gives the constructor type or DEFAULT_USR if this
2207
    is not known.
2208
*/
2209
 
2210
void implicit_defn
2211
    PROTO_N ( ( id, n ) )
2212
    PROTO_T ( IDENTIFIER id X int n )
2213
{
2214
    TYPE t ;
2215
    EXP e, r ;
2216
    DECL_SPEC ds ;
2217
    IDENTIFIER fn ;
2218
    NAMESPACE cns ;
2219
    int in_func, in_decl ;
2220
 
2221
    /* Check for previous definition */
2222
    if ( !IS_id_mem_func ( id ) ) return ;
2223
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2224
    if ( ds & dspec_inherit ) {
2225
	/* Inherited functions */
2226
	id = DEREF_id ( id_alias ( id ) ) ;
2227
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2228
    }
2229
    if ( !( ds & dspec_implicit ) ) return ;
2230
    if ( ds & dspec_defn ) return ;
2231
    ds |= dspec_defn ;
2232
    COPY_dspec ( id_storage ( id ), ds ) ;
2233
 
2234
    /* Find constructor type */
2235
    t = DEREF_type ( id_mem_func_type ( id ) ) ;
2236
    if ( !IS_type_func ( t ) ) return ;
2237
    if ( n == DEFAULT_USR ) n = constr_kind ( id, t ) ;
2238
 
2239
    /* Force immediate access checks */
2240
    in_func = in_function_defn ;
2241
    in_decl = in_declaration ;
2242
    fn = crt_func_id ;
2243
    in_function_defn = 1 ;
2244
    in_declaration = 0 ;
2245
    crt_func_id = id ;
2246
 
2247
    /* Make initialiser list */
2248
    start_try_check ( univ_type_set ) ;
2249
    cns = DEREF_nspace ( type_func_pars ( t ) ) ;
2250
    push_namespace ( cns ) ;
2251
    r = make_this_decl ( id ) ;
2252
    cns = DEREF_nspace ( id_parent ( id ) ) ;
2253
    e = make_constr ( cns, id, n, n ) ;
2254
    if ( n == DEFAULT_DESTR ) {
2255
	/* Allow for destructor prelude */
2256
	if ( !( ds & dspec_trivial ) ) {
2257
	    EXP d = make_destr_prelude ( cns ) ;
2258
	    e = join_exp ( d, e ) ;
2259
	}
2260
    } else if ( n == DEFAULT_ASSIGN ) {
2261
	/* Allow for return value */
2262
	r = make_indir_exp ( r ) ;
2263
	MAKE_exp_return_stmt ( type_bottom, r, r ) ;
2264
	e = join_exp ( e, r ) ;
2265
    }
2266
    IGNORE pop_namespace () ;
2267
    e = end_try_check ( id, e ) ;
2268
    COPY_exp ( id_mem_func_defn ( id ), e ) ;
2269
    define_id ( id ) ;
2270
 
2271
    /* Compile the function definition */
2272
    if ( !( ds & dspec_trivial ) ) {
2273
	if ( do_dump ) {
2274
	    dump_implicit = 1 ;
2275
	    dump_declare ( id, &crt_loc, 1 ) ;
2276
	}
2277
	compile_function ( id, 0 ) ;
2278
    }
2279
 
2280
    /* Restore old values */
2281
    in_function_defn = in_func ;
2282
    in_declaration = in_decl ;
2283
    crt_func_id = fn ;
2284
    return ;
2285
}
2286
 
2287
 
2288
/*
2289
    EXPLICIT CONSTRUCTOR FLAGS
2290
 
2291
    These flags are used to indicate that an explicit constructor or
2292
    conversion function has been declared.
2293
*/
2294
 
2295
int have_conv_expl = 0 ;
2296
int have_constr_expl = 0 ;
2297
 
2298
 
2299
/*
2300
    REMOVE EXPLICIT CONSTRUCTORS
2301
 
2302
    This routine removes all explicit constructors from the candidate list
2303
    p beginning with the mth element.
2304
*/
2305
 
2306
static void remove_explicit
2307
    PROTO_N ( ( p, m ) )
2308
    PROTO_T ( CANDIDATE_LIST *p X unsigned m )
2309
{
2310
    unsigned i, n = p->size ;
2311
    CANDIDATE *q = p->elem + m ;
2312
    for ( i = m ; i < n ; i++ ) {
2313
	DECL_SPEC ds = DEREF_dspec ( id_storage ( q->base ) ) ;
2314
	if ( ds & dspec_explicit ) q->rank = RANK_IGNORE ;
2315
	q++ ;
2316
    }
2317
    return ;
2318
}
2319
 
2320
 
2321
/*
2322
    CONSTRUCT CONSTRUCTOR CANDIDATES LIST
2323
 
2324
    This routine constructs the candidate list consisting of the constructors
2325
    for the class type t (including the explicit constructors only if cast
2326
    indicates an explicit conversion).  The candidates are added to the
2327
    list p.  The routine returns the first constructor for t.
2328
*/
2329
 
2330
static IDENTIFIER constr_candidates
2331
    PROTO_N ( ( p, t, cast ) )
2332
    PROTO_T ( CANDIDATE_LIST *p X TYPE t X unsigned cast )
2333
{
2334
    /* Search for constructor candidates */
2335
    IDENTIFIER cid ;
2336
    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2337
    complete_class ( ct, 1 ) ;
2338
    cid = DEREF_id ( ctype_constr ( ct ) ) ;
2339
    if ( IS_id_mem_func ( cid ) ) {
2340
	unsigned i = p->size ;
2341
	add_candidates ( p, cid, 1, KIND_CONSTR ) ;
2342
	swap_candidates ( p, i ) ;
2343
	if ( cast == CAST_IMPLICIT && have_constr_expl ) {
2344
	    remove_explicit ( p, i ) ;
2345
	}
2346
    } else {
2347
	/* Can happen for incomplete types and in C */
2348
	cid = NULL_id ;
2349
    }
2350
    return ( cid ) ;
2351
}
2352
 
2353
 
2354
/*
2355
    CONSTRUCT CONVERSION CANDIDATES LIST
2356
 
2357
    This routine constructs the candidate list for the conversion functions
2358
    for the type s which can be implicitly converted to type t, plus the
2359
    constructors of t if t is a class type.  The candidates are added to
2360
    the end of p.  The routine returns the first constructor for t if t is
2361
    a class type, and the null identifier otherwise.
2362
*/
2363
 
2364
IDENTIFIER conv_candidates
2365
    PROTO_N ( ( p, t, s, cast ) )
2366
    PROTO_T ( CANDIDATE_LIST *p X TYPE t X TYPE s X unsigned cast )
2367
{
2368
    /* Add constructors */
2369
    IDENTIFIER cid = NULL_id ;
2370
    if ( IS_type_compound ( t ) ) {
2371
	cid = constr_candidates ( p, t, cast ) ;
2372
    }
2373
 
2374
    /* Add conversion functions */
2375
    if ( IS_type_compound ( s ) ) {
2376
	LIST ( IDENTIFIER ) conv ;
2377
	CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
2378
	complete_class ( cs, 1 ) ;
2379
	conv = DEREF_list ( ctype_conv ( cs ) ) ;
2380
	while ( !IS_NULL_list ( conv ) ) {
2381
	    IDENTIFIER id = DEREF_id ( HEAD_list ( conv ) ) ;
2382
	    TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
2383
	    if ( IS_type_templ ( fn ) ) {
2384
		/* Allow for template functions */
2385
		fn = deduce_conv ( fn, t ) ;
2386
		if ( !IS_NULL_type ( fn ) ) {
2387
		    int eq = 0 ;
2388
		    id = deduce_func ( id, fn, &eq ) ;
2389
		} else {
2390
		    id = NULL_id ;
2391
		}
2392
	    }
2393
	    if ( !IS_NULL_id ( id ) ) {
2394
		unsigned r ;
2395
		CONVERSION c ;
2396
		HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2397
		c.from = DEREF_type ( hashid_conv_type ( nm ) ) ;
2398
		c.to = t ;
2399
		r = std_convert_seq ( &c, NULL_exp, 0, 0 ) ;
2400
		if ( r != CONV_NONE ) {
2401
		    unsigned i = p->size ;
2402
		    add_candidates ( p, id, 1, KIND_CONV ) ;
2403
		    if ( cast == CAST_IMPLICIT && have_conv_expl ) {
2404
			remove_explicit ( p, i ) ;
2405
		    }
2406
		}
2407
	    }
2408
	    conv = TAIL_list ( conv ) ;
2409
	}
2410
    }
2411
    return ( cid ) ;
2412
}
2413
 
2414
 
2415
/*
2416
    CONSTRUCT GENERIC CONVERSION CANDIDATES LIST
2417
 
2418
    This routine adds all the conversion functions from the type t to
2419
    types matching the type category mask kind to the candidate list p.
2420
    Note that template conversion functions are excluded - they cannot
2421
    give a non-ambiguous resolution and are beaten by any non-template
2422
    conversion function.
2423
*/
2424
 
2425
static void gen_candidates
2426
    PROTO_N ( ( p, t, kind ) )
2427
    PROTO_T ( CANDIDATE_LIST *p X TYPE t X unsigned kind )
2428
{
2429
    if ( IS_type_compound ( t ) ) {
2430
	LIST ( IDENTIFIER ) conv ;
2431
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2432
	complete_class ( ct, 1 ) ;
2433
	conv = DEREF_list ( ctype_conv ( ct ) ) ;
2434
	while ( !IS_NULL_list ( conv ) ) {
2435
	    IDENTIFIER id = DEREF_id ( HEAD_list ( conv ) ) ;
2436
	    TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
2437
	    if ( IS_type_func ( fn ) ) {
2438
		TYPE r = DEREF_type ( type_func_ret ( fn ) ) ;
2439
		unsigned c = type_category ( &r ) ;
2440
		if ( c & kind ) {
2441
		    unsigned i = p->size ;
2442
		    add_candidates ( p, id, 1, KIND_CONV ) ;
2443
		    if ( have_conv_expl ) remove_explicit ( p, i ) ;
2444
		}
2445
	    }
2446
	    conv = TAIL_list ( conv ) ;
2447
	}
2448
    }
2449
    return ;
2450
}
2451
 
2452
 
2453
/*
2454
    APPLY A TRIVIAL DESTRUCTOR
2455
 
2456
    This routine applies a trivial destructor to the expression a.  It
2457
    is used in explicit destructor and pseudo-destructor calls.
2458
*/
2459
 
2460
EXP trivial_destr
2461
    PROTO_N ( ( a ) )
2462
    PROTO_T ( EXP a )
2463
{
2464
    EXP e = make_discard_exp ( a ) ;
2465
    MAKE_exp_cast ( type_void, CONV_ELLIPSIS, e, e ) ;
2466
    return ( e ) ;
2467
}
2468
 
2469
 
2470
/*
2471
    APPLY A TRIVIAL FUNCTION
2472
 
2473
    This routine applies the trivial constructor or destructor id to
2474
    the arguments args.  The null expression is returned for invalid
2475
    arguments.
2476
*/
2477
 
2478
EXP apply_trivial_func
2479
    PROTO_N ( ( id, args ) )
2480
    PROTO_T ( IDENTIFIER id X LIST ( EXP ) args )
2481
{
2482
    EXP e = NULL_exp ;
2483
    TYPE t = DEREF_type ( id_function_etc_type ( id ) ) ;
2484
    int n = constr_kind ( id, t ) ;
2485
    switch ( n ) {
2486
	case DEFAULT_CONSTR : {
2487
	    if ( LENGTH_list ( args ) == 1 ) {
2488
		/* Trivial constructor */
2489
		CLASS_TYPE ct = parent_class ( id ) ;
2490
		t = make_class_type ( ct ) ;
2491
		e = make_null_exp ( t ) ;
2492
	    }
2493
	    break ;
2494
	}
2495
	case DEFAULT_COPY : {
2496
	    if ( LENGTH_list ( args ) == 2 ) {
2497
		/* Trivial copy constructor */
2498
		ERROR err = NULL_err ;
2499
		LIST ( TYPE ) p = DEREF_list ( type_func_mtypes ( t ) ) ;
2500
		TYPE ta = DEREF_type ( HEAD_list ( p ) ) ;
2501
		ta = DEREF_type ( type_ref_sub ( ta ) ) ;
2502
		e = DEREF_exp ( HEAD_list ( TAIL_list ( args ) ) ) ;
2503
		e = convert_reference ( e, REF_NORMAL ) ;
2504
		e = convert_class ( ta, e, &err ) ;
2505
		if ( !IS_NULL_err ( err ) ) {
2506
		    err = concat_warning ( err, ERR_expr_ass_conv () ) ;
2507
		    report ( crt_loc, err ) ;
2508
		}
2509
	    }
2510
	    break ;
2511
	}
2512
	case DEFAULT_DESTR : {
2513
	    if ( LENGTH_list ( args ) == 1 ) {
2514
		/* Trivial destructor */
2515
		EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
2516
		e = trivial_destr ( a ) ;
2517
	    }
2518
	    break ;
2519
	}
2520
	case DEFAULT_ASSIGN : {
2521
	    if ( LENGTH_list ( args ) == 2 ) {
2522
		/* Trivial assignment */
2523
		EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
2524
		EXP b = DEREF_exp ( HEAD_list ( TAIL_list ( args ) ) ) ;
2525
		e = make_assign_exp ( a, b, 1 ) ;
2526
	    }
2527
	    break ;
2528
	}
2529
    }
2530
    return ( e ) ;
2531
}
2532
 
2533
 
2534
/*
2535
    APPLY A CONSTRUCTOR
2536
 
2537
    This routine applies the constructor id to the arguments args, using a
2538
    dummy expression for the object the constructor is applied to.  The
2539
    definition of this object, whether an existing object or a temporary,
2540
    is filled in later.
2541
*/
2542
 
2543
EXP apply_constr
2544
    PROTO_N ( ( id, args ) )
2545
    PROTO_T ( IDENTIFIER id X LIST ( EXP ) args )
2546
{
2547
    TYPE t ;
2548
    EXP e, a ;
2549
    DECL_SPEC ds ;
2550
    CLASS_TYPE ct ;
2551
    IDENTIFIER cid ;
2552
    CONS_exp ( NULL_exp, args, args ) ;
2553
 
2554
    /* Check for trivial constructors */
2555
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2556
    if ( ds & dspec_trivial ) {
2557
	e = apply_trivial_func ( id, args ) ;
2558
	if ( !IS_NULL_exp ( e ) ) {
2559
	    DESTROY_list ( args, SIZE_exp ) ;
2560
	    return ( e ) ;
2561
	}
2562
    }
2563
 
2564
    /* Create dummy argument */
2565
    ct = parent_class ( id ) ;
2566
    cid = DEREF_id ( ctype_name ( ct ) ) ;
2567
    MAKE_type_compound ( cv_lvalue, ct, t ) ;
2568
    COPY_id ( type_name ( t ), cid ) ;
2569
    MAKE_exp_dummy ( t, NULL_exp, LINK_NONE, NULL_off, 0, a ) ;
2570
    COPY_exp ( HEAD_list ( args ), a ) ;
2571
 
2572
    /* Apply the constructor */
2573
    e = apply_func_id ( id, qual_none, NULL_graph, args ) ;
2574
    if ( IS_exp_func_id ( e ) ) {
2575
	int n = DEFAULT_USR ;
2576
	TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
2577
	if ( check_copy_constr ( fn, ct ) ) {
2578
	    /* Mark copy constructor calls */
2579
	    n = DEFAULT_COPY ;
2580
	}
2581
	e = add_constr_args ( e, ct, EXTRA_CONSTR ) ;
2582
	t = make_class_type ( ct ) ;
2583
	MAKE_exp_constr ( t, e, a, a, n, e ) ;
2584
    }
2585
    return ( e ) ;
2586
}
2587
 
2588
 
2589
/*
2590
    INITIALISATION BY CONSTRUCTOR
2591
 
2592
    This routine converts the argument list args to the class type t by
2593
    constructor.  The cast parameter indicates if this is an explicit
2594
    conversion.  The routine is used, for example, in initialisations of
2595
    the form 't id ( args )'.
2596
*/
2597
 
2598
EXP convert_constr
2599
    PROTO_N ( ( t, args, err, cast ) )
2600
    PROTO_T ( TYPE t X LIST ( EXP ) args X ERROR *err X unsigned cast )
2601
{
2602
    EXP e ;
2603
    IDENTIFIER cid ;
2604
    CANDIDATE_LIST *p ;
2605
 
2606
    /* Check for template parameters */
2607
    if ( dependent_conv ( t, args ) ) {
2608
	MAKE_exp_opn ( t, lex_static_Hcast, args, e ) ;
2609
	return ( e ) ;
2610
    }
2611
 
2612
    /* Construct list of candidates */
2613
    p = &candidates ;
2614
    p->size = 0 ;
2615
    cid = constr_candidates ( p, t, cast ) ;
2616
 
2617
    /* Perform overload resolution */
2618
    if ( p->size ) {
2619
	CANDIDATE *q = resolve_overload ( p, args, NULL_type, 0 ) ;
2620
	IDENTIFIER qid = q->func ;
2621
	unsigned rank = q->rank ;
2622
	int kind = q->kind ;
2623
	if ( rank == RANK_BEST ) {
2624
	    /* Unambiguous resolution */
2625
	    if ( !IS_NULL_id ( cid ) ) {
2626
		swap_candidates ( p, ( unsigned ) 0 ) ;
2627
	    }
2628
	    if ( match_no_viable > 1 && overload_warn ) {
2629
		add_error ( err, ERR_over_match_ctor_ok ( qid ) ) ;
2630
	    }
2631
	} else if ( rank == RANK_VIABLE ) {
2632
	    /* Ambiguous resolution */
2633
	    q = resolve_ambiguous ( p, args, NULL_type, 0 ) ;
2634
	    qid = q->func ;
2635
	    rank = q->rank ;
2636
	    if ( !IS_NULL_id ( cid ) ) {
2637
		swap_candidates ( p, ( unsigned ) 0 ) ;
2638
	    }
2639
	    if ( rank == RANK_TARGET ) {
2640
		ERROR err2 = ERR_over_match_ctor_target ( qid ) ;
2641
		qid = make_ambig_func ( p, qid, args, qual_none, &err2 ) ;
2642
		kind = KIND_FUNC ;
2643
		add_error ( err, err2 ) ;
2644
	    } else if ( rank == RANK_VIABLE ) {
2645
		ERROR err2 = ERR_over_match_ctor_ambig ( qid ) ;
2646
		err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
2647
		add_error ( err, err2 ) ;
2648
	    }
2649
	} else {
2650
	    /* No resolution */
2651
	    if ( !IS_NULL_id ( cid ) ) {
2652
		swap_candidates ( p, ( unsigned ) 0 ) ;
2653
	    }
2654
	    goto error_lab ;
2655
	}
2656
	use_func_id ( qid, 0, suppress_usage ) ;
2657
	if ( kind == KIND_CONSTR ) {
2658
	    e = apply_constr ( qid, args ) ;
2659
	} else {
2660
	    /* Can only happen for target dependent resolutions */
2661
	    e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
2662
	    if ( IS_exp_constr ( e ) ) {
2663
		/* Introduce temporary variable if necessary */
2664
		TYPE s = DEREF_type ( exp_type ( e ) ) ;
2665
		e = make_temporary ( s, e, NULL_exp, 0, err ) ;
2666
		e = convert_lvalue ( e ) ;
2667
	    }
2668
	}
2669
	return ( e ) ;
2670
    }
2671
 
2672
    /* Deal with incomplete structures */
2673
    if ( IS_NULL_id ( cid ) ) {
2674
	ERROR err2 = check_incomplete ( t ) ;
2675
	if ( !IS_NULL_err ( err2 ) ) {
2676
	    add_error ( err, err2 ) ;
2677
	    add_error ( err, ERR_expr_type_conv_incompl () ) ;
2678
	    e = make_null_exp ( t ) ;
2679
	    return ( e ) ;
2680
	}
2681
#if LANGUAGE_C
2682
	if ( IS_NULL_list ( args ) ) {
2683
	    /* C default initialisation */
2684
	    e = make_null_exp ( t ) ;
2685
	    return ( e ) ;
2686
	}
2687
	if ( IS_NULL_list ( TAIL_list ( args ) ) ) {
2688
	    /* C copy initialisation */
2689
	    EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
2690
	    a = convert_none ( a ) ;
2691
	    a = convert_class ( t, a, err ) ;
2692
	    a = remove_temporary ( a, NULL_exp ) ;
2693
	    return ( a ) ;
2694
	}
2695
#endif
2696
    }
2697
 
2698
    /* No candidates */
2699
    error_lab : {
2700
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2701
	cid = DEREF_id ( ctype_constr ( ct ) ) ;
2702
	add_error ( err, ERR_over_match_ctor_none ( cid ) ) ;
2703
	e = make_null_exp ( t ) ;
2704
    }
2705
    return ( e ) ;
2706
}
2707
 
2708
 
2709
/*
2710
    CHECK FOR INITIALISATION BY USER-DEFINED CONVERSION
2711
 
2712
    This routine checks for user-defined conversions of a to type t.
2713
    It is identical to convert_conv except that it returns the null
2714
    expression is there are no viable conversion functions.
2715
*/
2716
 
2717
EXP convert_conv_aux
2718
    PROTO_N ( ( t, a, err, cast ) )
2719
    PROTO_T ( TYPE t X EXP a X ERROR *err X unsigned cast )
2720
{
2721
    IDENTIFIER cid ;
2722
    CANDIDATE_LIST *p ;
2723
    LIST ( EXP ) args ;
2724
    TYPE s = DEREF_type ( exp_type ( a ) ) ;
2725
    if ( IS_type_error ( s ) ) {
2726
	EXP e = make_error_exp ( 0 ) ;
2727
	return ( e ) ;
2728
    }
2729
 
2730
    /* Check for template parameters */
2731
    CONS_exp ( a, NULL_list ( EXP ), args ) ;
2732
    if ( dependent_conv ( t, args ) ) {
2733
	EXP e = cast_templ_type ( t, a, cast ) ;
2734
	DESTROY_list ( args, SIZE_exp ) ;
2735
	return ( e ) ;
2736
    }
2737
 
2738
    /* Construct list of candidates */
2739
    p = &candidates ;
2740
    p->size = 0 ;
2741
    cid = conv_candidates ( p, t, s, cast ) ;
2742
 
2743
    /* Perform overload resolution */
2744
    if ( p->size ) {
2745
	EXP e ;
2746
	int kind ;
2747
	DECL_SPEC ds ;
2748
	CANDIDATE *q ;
2749
	unsigned rank ;
2750
	IDENTIFIER qid ;
2751
	match_this++ ;
2752
	q = resolve_overload ( p, args, t, 0 ) ;
2753
	match_this-- ;
2754
	qid = q->func ;
2755
	rank = q->rank ;
2756
	kind = q->kind ;
2757
	if ( rank == RANK_BEST ) {
2758
	    /* Unambiguous resolution */
2759
	    if ( !IS_NULL_id ( cid ) ) {
2760
		swap_candidates ( p, ( unsigned ) 0 ) ;
2761
	    }
2762
	    if ( match_no_viable > 1 && overload_warn ) {
2763
		add_error ( err, ERR_over_match_conv_ok ( qid ) ) ;
2764
	    }
2765
	} else if ( rank == RANK_VIABLE ) {
2766
	    /* Ambiguous resolution */
2767
	    q = resolve_ambiguous ( p, args, t, 0 ) ;
2768
	    qid = q->func ;
2769
	    rank = q->rank ;
2770
	    if ( !IS_NULL_id ( cid ) ) {
2771
		swap_candidates ( p, ( unsigned ) 0 ) ;
2772
	    }
2773
	    if ( rank == RANK_TARGET ) {
2774
		ERROR err2 = ERR_over_match_conv_target ( s, t ) ;
2775
		qid = make_ambig_func ( p, qid, args, qual_none, &err2 ) ;
2776
		kind = KIND_FUNC ;
2777
		add_error ( err, err2 ) ;
2778
	    } else if ( rank == RANK_VIABLE ) {
2779
		ERROR err2 = ERR_over_match_conv_ambig ( s, t ) ;
2780
		err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
2781
		add_error ( err, err2 ) ;
2782
	    }
2783
	} else {
2784
	    /* No resolution */
2785
	    if ( !IS_NULL_id ( cid ) ) {
2786
		swap_candidates ( p, ( unsigned ) 0 ) ;
2787
	    }
2788
	    DESTROY_list ( args, SIZE_exp ) ;
2789
	    return ( NULL_exp ) ;
2790
	}
2791
 
2792
	/* Check conversion function */
2793
	ds = DEREF_dspec ( id_storage ( qid ) ) ;
2794
	if ( ds & dspec_trivial ) {
2795
	    /* Trivial copy constructor */
2796
	    if ( !( ds & dspec_defn ) ) {
2797
		implicit_defn ( qid, DEFAULT_COPY ) ;
2798
	    }
2799
	    CONS_exp ( NULL_exp, args, args ) ;
2800
	    a = apply_trivial_func ( qid, args ) ;
2801
	    DESTROY_list ( args, SIZE_exp ) ;
2802
	    return ( a ) ;
2803
	}
2804
 
2805
	/* Apply conversion function */
2806
	use_func_id ( qid, 0, suppress_usage ) ;
2807
	if ( kind == KIND_CONSTR ) {
2808
	    if ( eq_type_unqual ( t, s ) ) {
2809
		/* Force initialisation in copy constructor */
2810
		int depth = init_ref_force ;
2811
		if ( depth > 2 && IS_type_compound ( t ) ) {
2812
		    TYPE fn = DEREF_type ( id_function_etc_type ( qid ) ) ;
2813
		    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2814
		    int c = check_copy_constr ( fn, ct ) ;
2815
		    if ( c == 3 ) {
2816
			/* Bad copy constructor */
2817
			HASHID nm = DEREF_hashid ( id_name ( qid ) ) ;
2818
			add_error ( err, ERR_class_copy_bad ( nm ) ) ;
2819
			DESTROY_list ( args, SIZE_exp ) ;
2820
			return ( a ) ;
2821
		    }
2822
		}
2823
		init_ref_force = depth + 1 ;
2824
		e = apply_constr ( qid, args ) ;
2825
		init_ref_force = depth ;
2826
	    } else {
2827
		e = apply_constr ( qid, args ) ;
2828
	    }
2829
	} else {
2830
	    e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
2831
	    if ( IS_exp_constr ( e ) ) {
2832
		/* Introduce temporary variable if necessary */
2833
		s = DEREF_type ( exp_type ( e ) ) ;
2834
		e = make_temporary ( s, e, NULL_exp, 0, err ) ;
2835
	    }
2836
	}
2837
	return ( e ) ;
2838
    }
2839
 
2840
    /* No candidates */
2841
    DESTROY_list ( args, SIZE_exp ) ;
2842
    return ( NULL_exp ) ;
2843
}
2844
 
2845
 
2846
/*
2847
    INITIALISATION BY USER-DEFINED CONVERSION
2848
 
2849
    This routine converts the expression a to the type t by user-defined
2850
    conversions.  It is used, for example, in initialisations of the form
2851
    't id = a'.
2852
*/
2853
 
2854
EXP convert_conv
2855
    PROTO_N ( ( t, a, err, cast ) )
2856
    PROTO_T ( TYPE t X EXP a X ERROR *err X unsigned cast )
2857
{
2858
    EXP e = convert_conv_aux ( t, a, err, cast ) ;
2859
    if ( IS_NULL_exp ( e ) ) {
2860
	/* No viable conversion functions */
2861
	TYPE s ;
2862
	a = convert_lvalue ( a ) ;
2863
	s = DEREF_type ( exp_type ( a ) ) ;
2864
	if ( !IS_type_error ( s ) && !IS_type_error ( t ) ) {
2865
	    add_error ( err, check_incomplete ( t ) ) ;
2866
	    if ( check_int_type ( t, btype_bool ) ) {
2867
		e = convert_boolean ( a, exp_paren_tag, err ) ;
2868
		return ( e ) ;
2869
	    }
2870
	    add_error ( err, ERR_expr_cast_invalid ( s, t ) ) ;
2871
	}
2872
	e = make_null_exp ( t ) ;
2873
    } else {
2874
	/* Deal with further implicit conversions */
2875
	TYPE s = DEREF_type ( exp_type ( e ) ) ;
2876
	if ( IS_type_ref ( s ) ) {
2877
	    /* Reference conversion */
2878
	    s = DEREF_type ( type_ref_sub ( s ) ) ;
2879
	    MAKE_exp_indir ( s, e, e ) ;
2880
	}
2881
	if ( !IS_type_ref ( t ) ) {
2882
	    CV_SPEC cv = DEREF_cv ( type_qual ( s ) ) ;
2883
	    if ( cv & cv_lvalue ) {
2884
		/* lvalue conversion */
2885
		s = rvalue_type ( s ) ;
2886
		MAKE_exp_contents ( s, e, e ) ;
2887
	    }
2888
	}
2889
	if ( !eq_type ( t, s ) ) {
2890
	    cast = ( CAST_STANDARD | CAST_IMPLICIT ) ;
2891
	    e = cast_exp ( t, e, err, cast ) ;
2892
	}
2893
    }
2894
    return ( e ) ;
2895
}
2896
 
2897
 
2898
/*
2899
    GENERIC INITIALISATION BY USER-DEFINED CONVERSION
2900
 
2901
    This routine selects a user-defined conversion from the expression a
2902
    to a type with category matching kind.  It returns the null expression
2903
    if no such conversion exists.
2904
*/
2905
 
2906
EXP convert_gen
2907
    PROTO_N ( ( kind, a, err ) )
2908
    PROTO_T ( unsigned kind X EXP a X ERROR *err )
2909
{
2910
    EXP e = NULL_exp ;
2911
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
2912
 
2913
    /* Construct list of candidates */
2914
    CANDIDATE_LIST *p = &candidates ;
2915
    p->size = 0 ;
2916
    gen_candidates ( p, t, kind ) ;
2917
 
2918
    /* Perform overload resolution */
2919
    if ( p->size ) {
2920
	CANDIDATE *q ;
2921
	unsigned rank ;
2922
	IDENTIFIER qid ;
2923
	LIST ( EXP ) args ;
2924
	CONS_exp ( a, NULL_list ( EXP ), args ) ;
2925
	match_this++ ;
2926
	q = resolve_overload ( p, args, NULL_type, 0 ) ;
2927
	match_this-- ;
2928
	qid = q->func ;
2929
	rank = q->rank ;
2930
	if ( rank == RANK_BEST ) {
2931
	    /* Unambiguous resolution */
2932
	    if ( match_no_viable > 1 && overload_warn ) {
2933
		add_error ( err, ERR_over_match_conv_ok ( qid ) ) ;
2934
	    }
2935
	} else if ( rank == RANK_VIABLE ) {
2936
	    /* Ambiguous resolution */
2937
	    q = resolve_ambiguous ( p, args, NULL_type, 0 ) ;
2938
	    qid = q->func ;
2939
	    rank = q->rank ;
2940
	    if ( rank != RANK_BEST ) {
2941
		/* Can't be target dependent */
2942
		ERROR err2 = ERR_over_match_conv_dup ( t ) ;
2943
		err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
2944
		add_error ( err, err2 ) ;
2945
	    }
2946
	} else {
2947
	    /* No viable candidates */
2948
	    return ( e ) ;
2949
	}
2950
	use_func_id ( qid, 0, suppress_usage ) ;
2951
	e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
2952
	e = convert_reference ( e, REF_NORMAL ) ;
2953
	e = convert_lvalue ( e ) ;
2954
    }
2955
 
2956
    /* No candidates */
2957
    return ( e ) ;
2958
}