Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

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