Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
7 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
7 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:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 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;
7 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;
7 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 "tok_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 "basetype.h"
77
#include "cast.h"
78
#include "check.h"
79
#include "chktype.h"
80
#include "class.h"
81
#include "compile.h"
82
#include "constant.h"
83
#include "construct.h"
84
#include "convert.h"
85
#include "copy.h"
86
#include "declare.h"
87
#include "derive.h"
88
#include "destroy.h"
89
#include "dump.h"
90
#include "exception.h"
91
#include "expression.h"
92
#include "file.h"
93
#include "function.h"
94
#include "hash.h"
95
#include "identifier.h"
96
#include "initialise.h"
97
#include "instance.h"
98
#include "label.h"
99
#include "member.h"
100
#include "namespace.h"
101
#include "operator.h"
102
#include "option.h"
103
#include "overload.h"
104
#include "predict.h"
105
#include "printf.h"
106
#include "redeclare.h"
107
#include "rewrite.h"
108
#include "statement.h"
109
#include "syntax.h"
110
#include "template.h"
111
#include "tokdef.h"
112
#include "token.h"
113
#include "variable.h"
114
 
115
 
116
/*
117
    CHECK A FUNCTION RETURN TYPE
118
 
119
    This routine checks whether the type t is a suitable function return
120
    type.  This can be void (unqualified) or a complete type.  def is
121
    true for a function definition or call, but false otherwise.
122
*/
123
 
7 7u83 124
TYPE
125
check_ret_type(TYPE t, ERROR *err, int def)
2 7u83 126
{
7 7u83 127
	CV_SPEC cv = DEREF_cv(type_qual(t));
128
	cv &= cv_qual;
129
	if (IS_type_top_etc(t)) {
130
		if (cv != cv_none && !def) {
131
			/* Can't return 'cv void' */
132
			add_error(err, ERR_dcl_fct_ret_void(t));
133
			if ((cv & cv_volatile) && IS_type_top(t)) {
134
				/* Map 'volatile void' to 'bottom' */
135
				t = type_bottom;
136
				cv &= ~cv_volatile;
137
				t = qualify_type(t, cv, 0);
138
			}
139
		}
140
	} else {
141
		if (def) {
142
			add_error(err, check_incomplete(t));
143
		}
2 7u83 144
	}
7 7u83 145
	if (cv != cv_none && !def) {
146
		/* Check for cv-qualified return types */
147
		add_error(err, ERR_dcl_fct_cv_ret(cv));
148
	}
149
	return (t);
2 7u83 150
}
151
 
152
 
153
/*
154
    APPLY REFERENCE CONVERSIONS TO A LIST OF EXPRESSIONS
155
 
156
    This routine applies reference conversions to each element of the
157
    argument list args.
158
*/
159
 
7 7u83 160
LIST(EXP)
161
convert_args(LIST(EXP) args)
2 7u83 162
{
7 7u83 163
	LIST(EXP)p = args;
164
	while (!IS_NULL_list(p)) {
165
		EXP e = DEREF_exp(HEAD_list(p));
166
		if (!IS_NULL_exp(e)) {
167
			e = convert_reference(e, REF_ASSIGN);
168
			COPY_exp(HEAD_list(p), e);
169
		}
170
		p = TAIL_list(p);
2 7u83 171
	}
7 7u83 172
	return (args);
2 7u83 173
}
174
 
175
 
176
/*
177
    FIND MINIMUM NUMBER OF ARGUMENTS FOR A FUNCTION
178
 
179
    This routine calculates the minimum number of arguments for the function
180
    type fn, taking default arguments and weak prototypes into account.
181
    The extra argument for member functions is included.
182
*/
183
 
7 7u83 184
unsigned
185
min_no_args(TYPE fn)
2 7u83 186
{
7 7u83 187
	unsigned n = 0;
188
	LIST(TYPE)ptypes;
189
	LIST(TYPE)mtypes;
190
	LIST(IDENTIFIER)p;
191
	while (IS_type_templ(fn)) {
192
		fn = DEREF_type(type_templ_defn(fn));
2 7u83 193
	}
7 7u83 194
	p = DEREF_list(type_func_pids(fn));
195
	ptypes = DEREF_list(type_func_ptypes(fn));
196
	mtypes = DEREF_list(type_func_mtypes(fn));
197
	if (!EQ_list(mtypes, ptypes)) {
198
		/* Add one for member functions */
199
		if (!IS_NULL_list(mtypes)) {
200
			mtypes = TAIL_list(mtypes);
201
			if (EQ_list(mtypes, ptypes)) {
202
				n = 1;
203
			}
204
		}
205
	}
206
	while (!IS_NULL_list(p)) {
207
		/* Scan for default arguments */
208
		IDENTIFIER id = DEREF_id(HEAD_list(p));
209
		EXP e = DEREF_exp(id_parameter_init(id));
210
		if (!IS_NULL_exp(e)) {
211
			break;
212
		}
213
		n++;
214
		p = TAIL_list(p);
215
	}
216
	return (n);
2 7u83 217
}
218
 
219
 
220
/*
221
    CHECK FUNCTION DEFAULT ARGUMENTS
222
 
223
    This routine checks the default arguments for the function type fn,
224
    returning true if there is a default argument.  Missing default
225
    arguments are checked if chk is true.
226
*/
227
 
7 7u83 228
int
229
check_func_dargs(TYPE fn, int chk, int clr)
2 7u83 230
{
7 7u83 231
	int started = 0;
232
	unsigned tag = TAG_type(fn);
233
	while (tag == type_templ_tag) {
234
		fn = DEREF_type(type_templ_defn(fn));
235
		tag = TAG_type(fn);
236
	}
237
	if (tag == type_func_tag) {
238
		LIST(IDENTIFIER)pids = DEREF_list(type_func_pids(fn));
239
		while (!IS_NULL_list(pids)) {
240
			IDENTIFIER pid = DEREF_id(HEAD_list(pids));
241
			EXP a = DEREF_exp(id_parameter_init(pid));
242
			if (IS_NULL_exp(a)) {
243
				if (started && !clr) {
244
					/* Missing default arguments */
245
					TYPE s;
246
					LOCATION loc;
247
					DEREF_loc(id_loc(pid), loc);
248
					report(loc, ERR_dcl_fct_default_missing(pid));
249
					s = DEREF_type(id_parameter_type(pid));
250
					MAKE_exp_value(s, a);
251
					COPY_exp(id_parameter_init(pid), a);
252
				}
253
			} else {
254
				started = 1;
255
				if (clr) {
256
					/* Clear default argument */
257
					COPY_exp(id_parameter_init(pid),
258
						 NULL_exp);
259
				} else {
260
					if (!chk) {
261
						break;
262
					}
263
				}
264
			}
265
			pids = TAIL_list(pids);
2 7u83 266
		}
267
	}
7 7u83 268
	return (started);
2 7u83 269
}
270
 
271
 
272
/*
273
    CHECK A WEAK PROTOTYPE ARGUMENT
274
 
275
    This routine checks the expression a passed as an argument to a weak
276
    function parameter of type t.  It returns the composite of t and the
277
    type of a, adding any errors to err.
278
*/
279
 
7 7u83 280
static TYPE
281
check_weak_arg(TYPE t, EXP a, ERROR *err)
2 7u83 282
{
7 7u83 283
	ERROR err2 = NULL_err;
284
	TYPE s = DEREF_type(exp_type(a));
285
	if (IS_exp_int_lit(a) && eq_type_offset(s, t)) {
286
		/* Allow for integer literals */
287
		NAT n = DEREF_nat(exp_int_lit_nat(a));
288
		if (check_nat_range(t, n) == 0) {
289
			return (t);
290
		}
291
	}
292
	t = check_compatible(t, s, 2, &err2, 1);
293
	if (!IS_NULL_err(err2)) {
294
		err2 = set_severity(err2, OPT_whatever, 0);
295
		add_error(err, err2);
296
	}
297
	return (t);
2 7u83 298
}
299
 
300
 
301
/*
302
    PERFORM WEAK PROTOTYPE ANALYSIS
303
 
304
    This routine performs weak prototype analysis for a call to the
305
    function id with the arguments args.  It is only called when the
306
    number of arguments matches the function type.
307
*/
308
 
7 7u83 309
static void
310
check_weak_args(IDENTIFIER id, LIST(EXP) args, unsigned n)
2 7u83 311
{
7 7u83 312
	TYPE fn = DEREF_type(id_function_etc_type(id));
313
	if (IS_type_func(fn)) {
314
		int changed = 0;
315
		int ell = DEREF_int(type_func_ellipsis(fn));
316
		LIST(TYPE) p = DEREF_list(type_func_ptypes(fn));
317
		LIST(TYPE) q = NULL_list(TYPE);
318
		LIST(IDENTIFIER) pids = NULL_list(IDENTIFIER);
319
		if (ell == FUNC_WEAK_ARGS) {
320
			changed = 1;
2 7u83 321
		}
7 7u83 322
		while (!IS_NULL_list(p)) {
323
			/* Check parameter types */
324
			TYPE s = DEREF_type(HEAD_list(p));
325
			if (!IS_NULL_list(args)) {
326
				ERROR err = NULL_err;
327
				EXP a = DEREF_exp(HEAD_list(args));
328
				TYPE t = check_weak_arg(s, a, &err);
329
				if (!IS_NULL_err(err)) {
330
					/* Report incompatible argument types */
331
					PTR(LOCATION)loc = id_loc(id);
332
					ERROR err2 =
333
					    ERR_expr_call_weak_arg(n, loc);
334
					err = concat_error(ERR_expr_call_func(id), err);
335
					err = concat_error(err, err2);
336
					report(crt_loc, err);
337
				}
338
				if (!EQ_type(t, s)) {
339
					if (changed) {
340
						changed = 1;
341
					}
342
					s = t;
343
				}
344
				args = TAIL_list(args);
345
				n++;
346
			}
347
			if (changed) {
348
				CONS_type(s, q, q);
349
			}
350
			p = TAIL_list(p);
2 7u83 351
		}
7 7u83 352
		if (ell == FUNC_NO_PARAMS) {
353
			/* Previous declaration was 'id()' */
354
			LOCATION loc;
355
			int par = CONTEXT_PARAMETER;
356
			NAMESPACE ns = DEREF_nspace(type_func_pars(fn));
357
			loc = decl_loc;
358
			decl_loc = crt_loc;
359
			push_namespace(ns);
360
			while (!IS_NULL_list(args)) {
361
				HASHID nm = lookup_anon();
362
				EXP a = DEREF_exp(HEAD_list(args));
363
				TYPE t = DEREF_type(exp_type(a));
364
				IDENTIFIER pid = DEREF_id(hashid_id(nm));
365
				pid = make_param_decl(dspec_none, t, pid, par);
366
				init_param(pid, NULL_exp);
367
				CONS_id(pid, pids, pids);
368
				CONS_type(t, q, q);
369
				args = TAIL_list(args);
370
			}
371
			IGNORE pop_namespace();
372
			ell = FUNC_WEAK_ARGS;
373
			decl_loc = loc;
374
			changed = 2;
375
		}
376
		if (changed == 2) {
377
			q = REVERSE_list(q);
378
			fn = copy_typedef(id, fn, cv_none);
379
			COPY_list(type_func_ptypes(fn), q);
380
			COPY_list(type_func_mtypes(fn), q);
381
			if (!IS_NULL_list(pids)) {
382
				pids = REVERSE_list(pids);
383
				COPY_list(type_func_pids(fn), pids);
384
			}
385
			COPY_int(type_func_ellipsis(fn), ell);
386
			COPY_type(id_function_etc_type(id), fn);
387
			COPY_loc(id_loc(id), crt_loc);
388
			decl_func_type(id, fn, 0);
389
		} else {
390
			DESTROY_list(q, SIZE_type);
391
		}
2 7u83 392
	}
7 7u83 393
	return;
2 7u83 394
}
395
 
396
 
397
/*
398
    CHECK PRINTF AND SCANF ARGUMENTS
399
 
400
    This routine performs weak prototype analysis for a call to the
401
    printf-like or scanf-like function id with the arguments args.  p gives
402
    the parameter types deduced from the format string.
403
*/
404
 
7 7u83 405
static void
406
check_printf_args(IDENTIFIER id, LIST(TYPE) p, LIST(EXP) args, unsigned n,
407
		  int pf)
2 7u83 408
{
7 7u83 409
	unsigned np = LENGTH_list(p) + (n - 1);
410
	unsigned na = LENGTH_list(args) + (n - 1);
411
	if (np != na) {
412
		/* Check number of parameters */
413
		ERROR err = ERR_expr_call_args_exact(na, na, np);
414
		err = set_severity(err, OPT_weak, -1);
415
		if (!IS_NULL_id(id)) {
416
			err = concat_error(ERR_expr_call_func(id), err);
417
		}
418
		report(crt_loc, err);
2 7u83 419
	}
7 7u83 420
	while (!IS_NULL_list(p) && !IS_NULL_list(args)) {
421
		/* Check parameter types */
422
		ERROR err = NULL_err;
423
		TYPE s = DEREF_type(HEAD_list(p));
424
		EXP a = DEREF_exp(HEAD_list(args));
425
		TYPE t = check_weak_arg(s, a, &err);
426
		if (!IS_NULL_err(err)) {
427
			/* Report incompatible argument types */
428
			PTR(LOCATION)loc = NULL_ptr(LOCATION);
429
			if (!IS_NULL_id(id)) {
430
				loc = id_loc(id);
431
			}
432
			err = concat_error(err, ERR_expr_call_weak_arg(n, loc));
433
		} else {
434
			/* Check that scanf arguments aren't const */
435
			if (!(pf & 1) && IS_type_ptr(t)) {
436
				CV_SPEC cv;
437
				t = DEREF_type(type_ptr_sub(t));
438
				cv = DEREF_cv(type_qual(t));
439
				if (cv & cv_const) {
440
					err = ERR_conv_qual_cast(cv_const);
441
					err = set_severity(err, OPT_weak, -1);
442
				}
443
			}
2 7u83 444
		}
7 7u83 445
		if (!IS_NULL_err(err)) {
446
			if (!IS_NULL_id(id)) {
447
				err = concat_error(ERR_expr_call_func(id), err);
448
			}
449
			report(crt_loc, err);
450
		}
451
		args = TAIL_list(args);
452
		p = TAIL_list(p);
453
		n++;
2 7u83 454
	}
7 7u83 455
	return;
2 7u83 456
}
457
 
458
 
459
/*
460
    APPLY ARGUMENT CONVERSIONS TO A LIST OF EXPRESSIONS
461
 
462
    This routine applies the argument conversions indicated by the function
463
    type fn to the argument list args.  gr gives information on how member
464
    functions are inherited.  The function return type is returned via pr.
465
    The identifier id is used in error reporting only, it is the function
466
    being called for named functions and the null identifier otherwise.
467
*/
468
 
7 7u83 469
static LIST(EXP)
470
cast_args(IDENTIFIER id, TYPE fn, GRAPH gr, LIST(EXP) args, TYPE *pr, int mem)
2 7u83 471
{
7 7u83 472
    int ell;
473
    TYPE ret;
474
    int extra = 0;
475
    unsigned n = 1;
476
    LIST(EXP)q;
477
    LIST(TYPE)p;
478
    LIST(TYPE)ptypes;
479
    LIST(TYPE)mtypes;
480
    ERROR err = NULL_err;
481
    int printf_function = 0;
482
    LIST(IDENTIFIER)pids;
483
    OPTION weak = OPTION_OFF;
484
    LIST(TYPE)ftypes = NULL_list(TYPE);
2 7u83 485
 
486
    /* Allow for template types */
7 7u83 487
    if (IS_type_templ(fn)) {
488
	int d;
489
	TOKEN sort = DEREF_tok(type_templ_sort(fn));
490
	pids = DEREF_list(tok_templ_pids(sort));
491
	force_template++;
492
	d = save_token_args(pids, NULL_list(TOKEN));
493
	fn = DEREF_type(type_templ_defn(fn));
494
	args = cast_args(id, fn, gr, args, &ret, mem);
495
	*pr = expand_type(ret, 2);
496
	restore_token_args(pids, d);
497
	force_template--;
498
	return (args);
2 7u83 499
    }
500
 
501
    /* Check function return type */
7 7u83 502
    ret = DEREF_type(type_func_ret(fn));
503
    ret = check_ret_type(ret, &err, 1);
504
    if (!IS_NULL_err(err)) {
505
	err = concat_error(err, ERR_expr_call_ret());
506
	if (!IS_NULL_id(id)) {
507
	    err = concat_error(ERR_expr_call_func(id), err);
2 7u83 508
	}
7 7u83 509
	report(crt_loc, err);
2 7u83 510
    }
7 7u83 511
    ret = convert_qual_type(ret);
512
    *pr = ret;
2 7u83 513
 
514
    /* Check number of arguments */
7 7u83 515
    ell = DEREF_int(type_func_ellipsis(fn));
516
    ptypes = DEREF_list(type_func_ptypes(fn));
517
    mtypes = DEREF_list(type_func_mtypes(fn));
518
    if (!EQ_list(mtypes, ptypes)) {
519
	if (mem) {
2 7u83 520
	    /* Member function */
7 7u83 521
	    extra = 1;
2 7u83 522
	} else {
7 7u83 523
	    fn = copy_typedef(NULL_id, fn, cv_none);
524
	    mtypes = ptypes;
525
	    COPY_list(type_func_mtypes(fn), mtypes);
2 7u83 526
	}
527
    }
7 7u83 528
    if (match_no_args == 0) {
2 7u83 529
	/* Weren't checked in overload resolution */
7 7u83 530
	unsigned na = LENGTH_list(args);
531
	unsigned npars = LENGTH_list(mtypes);
532
	if (na != npars) {
533
	    int more = 0;
534
	    unsigned margs = min_no_args(fn);
535
	    if (ell & FUNC_VAR_PARAMS) {
536
		    more = 1;
537
	    }
538
	    if (na < margs || (na > npars && !more)) {
2 7u83 539
		/* Illegal number of arguments */
7 7u83 540
		if (extra) {
2 7u83 541
		    /* Report actual numbers of passed arguments */
7 7u83 542
		    na--;
543
		    npars--;
544
		    margs--;
2 7u83 545
		}
7 7u83 546
		if (more) {
547
		    err = ERR_expr_call_args_min(na, na, margs);
548
		} else if (npars == margs) {
549
		    err = ERR_expr_call_args_exact(na, na, npars);
2 7u83 550
		} else {
7 7u83 551
		    err = ERR_expr_call_args_range(na, na, margs, npars);
2 7u83 552
		}
7 7u83 553
		if (ell & FUNC_NON_PROTO) {
2 7u83 554
		    /* Allowed for non-prototype functions */
7 7u83 555
		    err = set_severity(err, OPT_weak, -1);
2 7u83 556
		}
7 7u83 557
		if (!IS_NULL_err(err)) {
558
		    if (!IS_NULL_id(id)) {
559
			ERROR err2 = ERR_expr_call_func(id);
560
			err = concat_error(err2, err);
2 7u83 561
		    }
7 7u83 562
		    report(crt_loc, err);
2 7u83 563
		}
564
	    }
565
	}
566
    }
567
 
568
    /* Check function argument types */
7 7u83 569
    p = mtypes;
570
    q = args;
571
    pids = DEREF_list(type_func_pids(fn));
2 7u83 572
 
573
    /* Extra implicit argument */
7 7u83 574
    if (extra && !IS_NULL_list(p) && !IS_NULL_list(q)) {
575
	EXP b = DEREF_exp(HEAD_list(q));
576
	TYPE t = DEREF_type(HEAD_list(p));
577
	if (!IS_NULL_exp(b) && !IS_NULL_type(t)) {
2 7u83 578
	    /* Non-static member function called with object */
7 7u83 579
	    TYPE u = t;
580
	    int temp = 1;
581
	    CLASS_TYPE cs, ct;
582
	    TYPE s = DEREF_type(exp_type(b));
583
	    CV_SPEC qs = DEREF_cv(type_qual(s));
584
	    CV_SPEC qt = DEREF_cv(type_func_mqual(fn));
2 7u83 585
 
586
	    /* Check for temporary object */
7 7u83 587
	    while (IS_type_array(s)) {
588
		s = DEREF_type(type_array_sub(s));
589
		temp = 0;
2 7u83 590
	    }
7 7u83 591
	    while (IS_type_ptr_etc(s)) {
592
		s = DEREF_type(type_ptr_etc_sub(s));
593
		temp = 0;
2 7u83 594
	    }
7 7u83 595
	    if (temp) {
596
		TYPE ps;
597
		if (!(qs & cv_lvalue)) {
598
		    ERROR ferr = NULL_err;
599
		    b = make_temporary(s, b, NULL_exp, 0, &ferr);
600
		    if (!IS_NULL_err(ferr)) {
601
			    report(crt_loc, ferr);
602
		    }
2 7u83 603
		}
7 7u83 604
		MAKE_type_ptr(cv_none, s, ps);
605
		MAKE_exp_address(ps, b, b);
2 7u83 606
	    }
607
 
608
	    /* Check types for base class conversion */
7 7u83 609
	    while (IS_type_ptr_etc(t)) {
610
		t = DEREF_type(type_ptr_etc_sub(t));
2 7u83 611
	    }
7 7u83 612
	    cs = DEREF_ctype(type_compound_defn(s));
613
	    ct = DEREF_ctype(type_compound_defn(t));
614
	    if (!eq_ctype(cs, ct)) {
615
		GRAPH gs = find_base_class(cs, ct, 1);
616
		if (IS_NULL_graph(gs)) {
617
		    err = ERR_expr_ref_func_type(cs);
2 7u83 618
		} else {
7 7u83 619
		    OFFSET off;
620
		    err = check_ambig_base(gs);
621
		    if (!IS_NULL_graph(gr)) {
622
			CLASS_TYPE cr = DEREF_ctype(graph_head(gr));
623
			if (!eq_ctype(cs, cr) && !eq_ctype(cr, ct)) {
2 7u83 624
			    /* Inherited via intermediate base */
7 7u83 625
			    GRAPH g1 = find_base_class(cs, cr, 1);
626
			    if (!IS_NULL_graph(g1)) {
627
				GRAPH g2 = find_base_class(cr, ct, 1);
628
				if (!IS_NULL_graph(g2)) {
629
				    if (!IS_NULL_err(err)) {
630
					ERROR err2;
631
					destroy_error(err, 1);
632
					err = check_ambig_base(g1);
633
					err2 = check_ambig_base(g2);
634
					err = concat_error(err, err2);
2 7u83 635
				    }
7 7u83 636
				    gs = DEREF_graph(graph_top(g2));
637
				    gs = find_subgraph(g1, gs, g2);
2 7u83 638
				}
639
			    }
640
			}
641
		    }
7 7u83 642
		    if (!IS_NULL_err(err)) {
2 7u83 643
			/* Report ambiguous bases */
7 7u83 644
			ERROR err2 = ERR_class_member_lookup_func();
645
			err = concat_error(err, err2);
2 7u83 646
		    }
7 7u83 647
		    off = DEREF_off(graph_off(gs));
648
		    b = make_base_cast(u, b, off);
2 7u83 649
		    /* Access should have been checked */
650
		}
7 7u83 651
		if (!IS_NULL_err(err)) {
652
		    if (!IS_NULL_id(id)) {
653
			ERROR err2 = ERR_expr_call_func(id);
654
			err = concat_error(err2, err);
2 7u83 655
		    }
7 7u83 656
		    report(crt_loc, err);
2 7u83 657
		}
658
	    }
659
 
660
	    /* Check type qualifiers */
7 7u83 661
	    qs &= cv_qual;
662
	    qt &= cv_qual;
663
	    if (qs != qt) {
664
		    MAKE_exp_cast(u, CONV_QUAL, b, b);
665
	    }
666
	    qs = (qs & ~qt);
667
	    if (qs != cv_none) {
668
		if (IS_NULL_id(id)) {
669
		    err = ERR_class_this_qual(qs);
670
		    report(crt_loc, err);
2 7u83 671
		} else {
7 7u83 672
		    HASHID nm = DEREF_hashid(id_name(id));
673
		    if (!IS_hashid_destr(nm)) {
2 7u83 674
			/* Allowed for destructors */
7 7u83 675
			ERROR err2;
676
			err = ERR_class_this_qual(qs);
677
			err2 = ERR_expr_call_func(id);
678
			err = concat_error(err2, err);
679
			report(crt_loc, err);
2 7u83 680
		    }
681
		}
682
	    }
7 7u83 683
	    COPY_exp(HEAD_list(q), b);
2 7u83 684
	}
7 7u83 685
	p = TAIL_list(p);
686
	q = TAIL_list(q);
2 7u83 687
    }
688
 
689
    /* Normal arguments */
7 7u83 690
    if (ell & FUNC_NON_PROTO) {
691
	weak = option(OPT_weak);
692
	goto ellipsis_lab;
2 7u83 693
    }
7 7u83 694
    while (!IS_NULL_list(p) && !IS_NULL_list(q)) {
695
	EXP b = DEREF_exp(HEAD_list(q));
696
	TYPE t = DEREF_type(HEAD_list(p));
697
	OPTION opt = option(OPT_conv_int_int_impl);
698
	if (IS_NULL_exp(b)) {
699
		b = make_error_exp(0);
700
	}
701
	if (IS_exp_paren(b)) {
2 7u83 702
	    /* Parentheses suppress conversion warnings */
7 7u83 703
	    b = DEREF_exp(exp_paren_arg(b));
704
	    option(OPT_conv_int_int_impl) = OPTION_ALLOW;
2 7u83 705
	}
7 7u83 706
	if (IS_exp_string_lit(b)) {
707
	    if (option(OPT_printf_string)) {
2 7u83 708
		/* Check for printf and scanf strings */
7 7u83 709
		int pf = is_printf_type(t);
710
		if (pf) {
711
		    unsigned na = LENGTH_list(q) + 100;
712
		    STRING fmt = DEREF_str(exp_string_lit_str(b));
713
		    ftypes = find_printf_args(fmt, na, pf);
714
		    printf_function = pf;
2 7u83 715
		}
716
	    }
717
	}
7 7u83 718
	err = NULL_err;
719
	b = init_assign(t, cv_none, b, &err);
720
	if (pass_complex_type(t)) {
2 7u83 721
	    /* Create temporary variable */
7 7u83 722
	    b = remove_temporary(b, NULL_exp);
723
	    b = make_temporary(t, b, NULL_exp, 0, &err);
724
	    MAKE_type_ptr(cv_none, t, t);
725
	    MAKE_exp_address(t, b, b);
2 7u83 726
	}
7 7u83 727
	if (!IS_NULL_err(err)) {
728
	    err = init_error(err, 0);
729
	    err = concat_error(err, ERR_expr_call_arg(n));
730
	    if (!IS_NULL_id(id)) {
731
		err = concat_error(ERR_expr_call_func(id), err);
2 7u83 732
	    }
7 7u83 733
	    report(crt_loc, err);
2 7u83 734
	}
7 7u83 735
	option(OPT_conv_int_int_impl) = opt;
736
	n++;
737
	COPY_exp(HEAD_list(q), b);
738
	pids = TAIL_list(pids);
739
	p = TAIL_list(p);
740
	q = TAIL_list(q);
2 7u83 741
    }
742
 
743
    /* Default arguments */
7 7u83 744
    if (!IS_NULL_list(p)) {
745
	LIST(EXP)dargs = NULL_list(EXP);
746
	while (!IS_NULL_list(pids)) {
747
	    IDENTIFIER pid = DEREF_id(HEAD_list(pids));
748
	    EXP e = DEREF_exp(id_parameter_init(pid));
749
	    if (!IS_NULL_exp(e)) {
750
		TYPE t = DEREF_type(exp_type(e));
751
		if (pass_complex_type(t)) {
2 7u83 752
		    /* Create temporary variable */
7 7u83 753
		    EXP d = DEREF_exp(id_parameter_term(pid));
754
		    if (!IS_NULL_exp(d)) {
755
			d = copy_exp(d, NULL_type, NULL_type);
2 7u83 756
		    }
7 7u83 757
		    err = NULL_err;
758
		    e = remove_temporary(e, NULL_exp);
759
		    MAKE_exp_copy(t, e, e);
760
		    e = make_temporary(t, e, d, 0, &err);
761
		    if (!IS_NULL_err(err)) {
762
			err = concat_error(err, ERR_expr_call_arg(n));
763
			if (!IS_NULL_id(id)) {
764
			    ERROR err2 = ERR_expr_call_func(id);
765
			    err = concat_error(err2, err);
2 7u83 766
			}
7 7u83 767
			report(crt_loc, err);
2 7u83 768
		    }
7 7u83 769
		    MAKE_type_ptr(cv_none, t, t);
770
		    MAKE_exp_address(t, e, e);
2 7u83 771
		} else {
7 7u83 772
		    MAKE_exp_copy(t, e, e);
2 7u83 773
		}
7 7u83 774
		CONS_exp(e, dargs, dargs);
775
		n++;
2 7u83 776
	    }
7 7u83 777
	    pids = TAIL_list(pids);
2 7u83 778
	}
7 7u83 779
	if (!IS_NULL_list(dargs)) {
780
	    dargs = REVERSE_list(dargs);
781
	    args = APPEND_list(args, dargs);
2 7u83 782
	}
783
    }
784
 
785
    /* Ellipsis arguments */
7 7u83 786
    ellipsis_lab: {
787
	unsigned wn = n;
788
	LIST(EXP)wq = q;
789
	while (!IS_NULL_list(q)) {
790
	    TYPE t, s;
791
	    EXP b = DEREF_exp(HEAD_list(q));
792
	    if (IS_NULL_exp(b)) {
793
		    b = make_error_exp(0);
2 7u83 794
	    }
7 7u83 795
	    b = convert_lvalue(b);
796
	    b = convert_none(b);
797
	    t = DEREF_type(exp_type(b));
798
	    err = NULL_err;
799
	    s = arg_promote_type(t, &err);
800
	    if (!EQ_type(s, t)) {
801
		b = init_assign(s, cv_none, b, &err);
802
	    }
803
	    if (!IS_NULL_err(err)) {
804
		err = init_error(err, 0);
805
		err = concat_error(err, ERR_expr_call_ellipsis(n));
806
		if (!IS_NULL_id(id)) {
807
		    err = concat_error(ERR_expr_call_func(id), err);
2 7u83 808
		}
7 7u83 809
		report(crt_loc, err);
2 7u83 810
	    }
7 7u83 811
	    COPY_exp(HEAD_list(q), b);
812
	    n++;
813
	    q = TAIL_list(q);
2 7u83 814
	}
7 7u83 815
	if (weak && !IS_NULL_id(id)) {
2 7u83 816
	    /* Perform weak prototype analysis */
7 7u83 817
	    check_weak_args(id, wq, wn);
2 7u83 818
	}
7 7u83 819
	if (printf_function) {
2 7u83 820
	    /* Check printf arguments */
7 7u83 821
	    if (option(OPT_weak)) {
822
		check_printf_args(id, ftypes, wq, wn, printf_function);
2 7u83 823
	    }
7 7u83 824
	    DESTROY_list(ftypes, SIZE_type);
2 7u83 825
	}
826
    }
7 7u83 827
    IGNORE check_value(OPT_VAL_func_args,(unsigned long)(n - 1));
828
    return (args);
2 7u83 829
}
830
 
831
 
832
/*
833
    APPLY A NAMED FUNCTION TO A LIST OF EXPRESSIONS
834
 
835
    This routine applies the function id to the list of arguments args.
836
    Any qualifiers used to express id are passed in as qual, since these
837
    may suppress the virtual call mechanism.  gr gives the base class
838
    from which a member function is inherited.
839
*/
840
 
7 7u83 841
EXP
842
apply_func_id(IDENTIFIER id, QUALIFIER qual, GRAPH gr, LIST(EXP)args)
2 7u83 843
{
7 7u83 844
	EXP e;
845
	TYPE fn;
846
	TYPE ret;
847
	DECL_SPEC ds;
848
	int throws = 1;
849
	EXP virt = NULL_exp;
850
	IDENTIFIER fid = id;
2 7u83 851
 
7 7u83 852
	/* Check for non-functions */
853
	if (!IS_id_function_etc(id)) {
854
		e = make_error_exp(0);
855
		return (e);
856
	}
2 7u83 857
 
7 7u83 858
	/* Check for inherited functions */
859
	ds = DEREF_dspec(id_storage(id));
860
	if (ds & dspec_inherit) {
861
		id = DEREF_id(id_alias(id));
862
		ds = DEREF_dspec(id_storage(id));
863
	}
2 7u83 864
 
7 7u83 865
	/* Check for trivial functions */
866
	if (ds & dspec_trivial) {
867
		e = apply_trivial_func(id, args);
868
		if (!IS_NULL_exp(e)) {
869
			DESTROY_list(args, SIZE_exp);
870
			return (e);
871
		}
2 7u83 872
	}
873
 
7 7u83 874
	/* Convert the arguments */
875
	fn = DEREF_type(id_function_etc_type(id));
876
	args = cast_args(id, fn, gr, args, &ret, 1);
2 7u83 877
 
7 7u83 878
	/* Check for virtual function calls */
879
	if (ds & dspec_virtual) {
880
		if (!(qual & qual_explicit)) {
881
			EXP a = DEREF_exp(HEAD_list(args));
882
			int know = know_type(a);
883
			if (know == 0) {
884
				if (!IS_NULL_exp(a)) {
885
					TYPE ta = DEREF_type(exp_type(a));
886
					MAKE_exp_dummy(ta, a, LINK_NONE,
887
						       NULL_off, 1, virt);
888
					COPY_exp(HEAD_list(args), virt);
889
				}
890
			} else if (know == 2) {
891
				/* Non-obvious case */
892
				report(crt_loc, ERR_class_virtual_not(id));
893
			}
2 7u83 894
		}
7 7u83 895
		if (IS_NULL_exp(virt)) {
896
			/* Explicit call of virtual function */
897
			if (ds & dspec_pure) {
898
				/* Call of pure virtual function */
899
				HASHID nm = DEREF_hashid(id_name(id));
900
				if (IS_hashid_destr(nm)) {
901
					/* Calling pure destructor is alright */
902
					/* EMPTY */
903
				} else {
904
					report(crt_loc,
905
					       ERR_class_abstract_call(id));
906
				}
907
			}
908
			if (!(ds & dspec_used)) {
909
				/* Mark explicit use */
910
				reuse_id(id, suppress_usage);
911
				ds = DEREF_dspec(id_storage(id));
912
			}
913
		}
2 7u83 914
	}
7 7u83 915
	if ((ds & dspec_friend) && IS_NULL_exp(virt)) {
916
		/* Call can't throw an exception */
917
		throws = 0;
918
	}
919
 
920
	/* Check for token applications */
921
	if (ds & dspec_token) {
922
		if ((qual & qual_mark) && !(ds & dspec_reserve)) {
923
			/* Parenthesised - use function proper */
924
			/* EMPTY */
2 7u83 925
		} else {
7 7u83 926
			TYPE form = DEREF_type(id_function_etc_form(id));
927
			if (!IS_NULL_type(form) && IS_type_token(form)) {
928
				IDENTIFIER ext = DEREF_id(type_token_tok(form));
929
				if (!IS_NULL_id(ext) && IS_id_token(ext)) {
930
					id = ext;
931
				}
932
			}
2 7u83 933
		}
934
	}
935
 
7 7u83 936
	/* A function call is a side effect */
937
	no_side_effects++;
938
 
939
	/* Form the result */
940
	MAKE_exp_func_id(ret, id, args, virt, e);
941
	if (pass_complex_type(ret)) {
942
		/* Allow for complex return types */
943
		EXP a;
944
		MAKE_exp_dummy(ret, NULL_exp, LINK_NONE, NULL_off, 0, a);
945
		if (IS_id_stat_mem_func(id)) {
946
			/* Insert as second parameter for static member functions */
947
			EXP b = DEREF_exp(HEAD_list(args));
948
			COPY_exp(HEAD_list(args), a);
949
			CONS_exp(b, args, args);
950
		} else {
951
			/* Insert as first parameter for other functions */
952
			CONS_exp(a, args, args);
2 7u83 953
		}
7 7u83 954
		COPY_list(exp_func_id_args(e), args);
955
		COPY_unsigned(exp_func_id_extra(e), 1);
956
		COPY_type(exp_type(e), type_void);
957
		MAKE_exp_constr(ret, e, a, a, DEFAULT_USR, e);
2 7u83 958
	}
959
 
7 7u83 960
	/* Check for exception violations */
961
	if (throws) {
962
		IGNORE check_func_throw(fn, fid);
2 7u83 963
	}
7 7u83 964
	return (e);
2 7u83 965
}
966
 
967
 
968
/*
969
    APPLY A FUNCTION TO A LIST OF EXPRESSIONS
970
 
971
    This routine applies the function expression a to the list of arguments
972
    args.
973
*/
974
 
7 7u83 975
static EXP
976
apply_func_exp(EXP a, LIST(EXP)args)
2 7u83 977
{
7 7u83 978
	/* Find function type */
979
	EXP e;
980
	TYPE ret;
981
	int mem = 0;
982
	TYPE fn = DEREF_type(exp_type(a));
983
	CV_SPEC cv = DEREF_cv(type_qual(fn));
984
	if (cv & cv_lvalue) {
985
		a = convert_lvalue(a);
986
		fn = DEREF_type(exp_type(a));
2 7u83 987
	}
7 7u83 988
	switch (TAG_type(fn)) {
989
	case type_ptr_tag:
990
		fn = DEREF_type(type_ptr_sub(fn));
991
		break;
992
	case type_ptr_mem_tag:
993
		fn = DEREF_type(type_ptr_mem_sub(fn));
994
		break;
2 7u83 995
	}
996
 
7 7u83 997
	/* Convert the arguments */
998
	match_no_args = 0;
999
	if (IS_exp_call(a)) {
1000
		mem = 1;
1001
	}
1002
	args = cast_args(NULL_id, fn, NULL_graph, args, &ret, mem);
1003
	if (mem) {
1004
		/* Record pointer to function member argument */
1005
		EXP c = DEREF_exp(HEAD_list(args));
1006
		TYPE t = DEREF_type(exp_type(c));
1007
		MAKE_exp_dummy(t, c, LINK_NONE, NULL_off, 1, c);
1008
		COPY_exp(exp_call_arg(a), c);
1009
		COPY_exp(HEAD_list(args), c);
1010
	}
2 7u83 1011
 
7 7u83 1012
	/* A function call is a side effect */
1013
	no_side_effects++;
2 7u83 1014
 
7 7u83 1015
	/* Form the result */
1016
	MAKE_exp_func(ret, a, args, e);
1017
	if (pass_complex_type(ret)) {
1018
		/* Allow for complex return types */
1019
		EXP b;
1020
		MAKE_exp_dummy(ret, NULL_exp, LINK_NONE, NULL_off, 0, b);
1021
		CONS_exp(b, args, args);
1022
		COPY_list(exp_func_args(e), args);
1023
		COPY_unsigned(exp_func_extra(e), 1);
1024
		COPY_type(exp_type(e), type_void);
1025
		MAKE_exp_constr(ret, e, b, b, DEFAULT_USR, e);
1026
	}
2 7u83 1027
 
7 7u83 1028
	/* Check for exception violations */
1029
	IGNORE check_func_throw(fn, NULL_id);
1030
	return (e);
2 7u83 1031
}
1032
 
1033
 
1034
/*
1035
    APPLY A TEMPLATE DEPENDENT FUNCTION
1036
 
1037
    This routine constructs the template dependent function call
1038
    'a ( args )', or '( a ) ( args )' if is_paren is true.
1039
*/
1040
 
7 7u83 1041
static EXP
1042
call_func_templ(EXP a, LIST(EXP) args, int is_paren)
2 7u83 1043
{
7 7u83 1044
	EXP e;
1045
	TYPE ret = type_templ_param;
1046
	if (is_paren) {
1047
		a = make_paren_exp(a);
1048
	}
1049
	CONS_exp(a, args, args);
1050
	MAKE_exp_opn(ret, lex_func_Hop, args, e);
1051
	return (e);
2 7u83 1052
}
1053
 
1054
 
1055
/*
1056
    CONSTRUCT A FUNCTION CALL EXPRESSION
1057
 
1058
    This routine constructs the function call expression 'a ( args )'.
1059
*/
1060
 
7 7u83 1061
EXP
1062
make_func_exp(EXP a, LIST(EXP)args, int rescan)
2 7u83 1063
{
7 7u83 1064
	EXP e;
1065
	TYPE fn;
1066
	unsigned tag;
1067
	unsigned ftag;
1068
	int is_ptr = 0;
1069
	int is_ptr_mem = 0;
1070
	int is_paren = IS_exp_paren(a);
2 7u83 1071
 
7 7u83 1072
	/* Do reference conversions */
1073
	a = convert_reference(a, REF_FUNCTION);
1074
	args = convert_args(args);
2 7u83 1075
 
7 7u83 1076
	/* Map '&f' to 'f' */
1077
	tag = TAG_exp(a);
1078
	if (tag == exp_address_tag) {
1079
		/* Non-member function */
1080
		EXP b = DEREF_exp(exp_address_arg(a));
1081
		if (IS_exp_identifier_etc(b)) {
1082
			a = b;
1083
			tag = TAG_exp(a);
1084
			is_paren = 1;
2 7u83 1085
		}
7 7u83 1086
	} else if (tag == exp_address_mem_tag) {
1087
		/* Member function (which better eventually be static) */
1088
		EXP b = DEREF_exp(exp_address_mem_arg(a));
1089
		IDENTIFIER id = DEREF_id(exp_member_id(b));
1090
		if (IS_id_function_etc(id)) {
1091
			while (!IS_NULL_id(id)) {
1092
				if (IS_id_mem_func(id)) {
1093
					report(crt_loc,
1094
					       ERR_over_match_call_mem(id));
1095
					break;
1096
				}
1097
				id = DEREF_id(id_function_etc_over(id));
1098
			}
1099
			a = b;
1100
			tag = TAG_exp(a);
1101
			is_paren = 1;
1102
			is_ptr_mem = 1;
1103
		}
2 7u83 1104
	}
1105
 
7 7u83 1106
	/* Check function type */
1107
	fn = DEREF_type(exp_type(a));
1108
	ftag = TAG_type(fn);
2 7u83 1109
#if LANGUAGE_CPP
7 7u83 1110
	if (ftag == type_compound_tag) {
1111
		/* Allow for overloading using 'operator ()' */
1112
		if (overload_depth == 0) {
1113
			e = function_overload(a, args);
1114
			return (e);
1115
		}
2 7u83 1116
	}
1117
#endif
7 7u83 1118
	if (ftag == type_ptr_tag) {
1119
		/* Allow for pointers to functions */
1120
		fn = DEREF_type(type_ptr_sub(fn));
1121
		ftag = TAG_type(fn);
1122
		is_ptr = 1;
2 7u83 1123
	}
7 7u83 1124
	while (ftag == type_templ_tag) {
1125
		fn = DEREF_type(type_templ_defn(fn));
1126
		ftag = TAG_type(fn);
2 7u83 1127
	}
7 7u83 1128
	if (ftag != type_func_tag) {
1129
		if (ftag == type_token_tag && is_templ_type(fn)) {
1130
			/* Allow for template types */
1131
			e = call_func_templ(a, args, is_paren);
1132
			return (e);
2 7u83 1133
		}
7 7u83 1134
		if (ftag != type_error_tag) {
1135
			/* Type should now be a function type */
1136
			ERROR err;
1137
			if (IS_exp_member(a)) {
1138
				IDENTIFIER id = DEREF_id(exp_member_id(a));
1139
				err = ERR_expr_prim_mem(id);
1140
			} else {
1141
				err = ERR_expr_call_op(fn);
1142
			}
1143
			if (ftag == type_ptr_mem_tag && in_ptr_mem_selector) {
1144
				err = concat_error(err,
1145
						   ERR_expr_mptr_oper_paren());
1146
			}
1147
			report(crt_loc, err);
2 7u83 1148
		}
7 7u83 1149
		e = make_error_exp(0);
1150
		return (e);
1151
	}
1152
 
1153
	/* Deal with named functions */
1154
	if (!is_ptr) {
1155
		switch (tag) {
1156
		case exp_identifier_tag:
1157
		case exp_member_tag:
1158
		case exp_ambiguous_tag:
1159
		case exp_undeclared_tag: {
1160
			/* Normal functions, 'f' */
1161
			int dep = 1;
1162
			DECL_SPEC ds;
1163
			IDENTIFIER id;
1164
			QUALIFIER qual;
1165
			EXP extra = NULL_exp;
1166
			NAMESPACE ns = NULL_nspace;
1167
			int acc = crt_access_list.inherit;
1168
			id = DEREF_id(exp_identifier_etc_id(a));
1169
			qual = DEREF_qual(exp_identifier_etc_qual(a));
1170
			if (rescan) {
1171
				id = rescan_func_id(id, qual);
2 7u83 1172
			}
7 7u83 1173
			if (dependent_call(id, args)) {
1174
				/* Call depends on template argument */
1175
				e = call_func_templ(a, args, is_paren);
1176
				return (e);
2 7u83 1177
			}
7 7u83 1178
			ds = DEREF_dspec(id_storage(id));
1179
			if (ds & dspec_instance) {
1180
				dep = 0;
2 7u83 1181
			}
7 7u83 1182
			if (qual & qual_explicit) {
1183
				dep = 0;
1184
			}
1185
			if (is_paren) {
1186
				qual |= qual_mark;
1187
			}
1188
			switch (TAG_id(id)) {
1189
			case id_mem_func_tag:
1190
			case id_stat_mem_func_tag:
1191
mem_func_label:
1192
				/* Add argument for member functions */
1193
				extra = make_this_ref(&ns);
1194
				CONS_exp(extra, args, args);
1195
				dep = 0;
1196
				break;
1197
			case id_ambig_tag: {
1198
				/* Check for ambiguous member functions */
1199
				CLASS_TYPE ct = parent_class(id);
1200
				if (!IS_NULL_ctype(ct)) {
1201
					goto mem_func_label;
1202
				}
1203
				break;
1204
			}
1205
			}
1206
 
1207
			id = resolve_call(id, args, qual, dep);
1208
			switch (TAG_id(id)) {
1209
			case id_function_tag:
1210
				/* Normal function call */
1211
				break;
1212
			case id_mem_func_tag:
1213
				/* Member function call */
1214
				if (is_ptr_mem) {
1215
					fn = DEREF_type(id_mem_func_type(id));
1216
					COPY_type(exp_type(a), fn);
1217
					a = make_ref_exp(a, 1);
1218
					fn = DEREF_type(exp_type(a));
1219
					report(crt_loc, ERR_expr_call_op(fn));
1220
				}
1221
				if (IS_NULL_exp(extra)) {
1222
					/* No associated object */
1223
					report(crt_loc, ERR_expr_call_mem(id));
1224
				} else {
1225
					/* Indicate inherited member function */
1226
					crt_access_list.inherit++;
1227
				}
1228
				break;
1229
			case id_stat_mem_func_tag:
1230
				/* Static member function call */
1231
				if (!IS_NULL_exp(extra)) {
1232
					COPY_exp(HEAD_list(args), NULL_exp);
1233
				}
1234
				break;
1235
			default:
1236
				/* Non-function called */
1237
				if (in_template_decl) {
1238
					e = call_func_templ(a, args, is_paren);
1239
				} else {
1240
					e = make_id_exp(id);
1241
					if (eq_exp(e, a, 0)) {
1242
						e = make_error_exp(0);
1243
					} else {
1244
						e = make_func_exp(e, args, 0);
1245
					}
1246
				}
1247
				return (e);
1248
			}
1249
			use_func_id(id, 1, suppress_usage);
1250
			crt_access_list.inherit = acc;
1251
			if (resolved_kind == KIND_CONSTR) {
1252
				e = apply_constr(id, args);
2 7u83 1253
			} else {
7 7u83 1254
				e = apply_func_id(id, qual, NULL_graph, args);
2 7u83 1255
			}
7 7u83 1256
			return (e);
2 7u83 1257
		}
1258
 
7 7u83 1259
		case exp_call_tag: {
1260
			/* Member function selectors */
1261
			EXP b = DEREF_exp(exp_call_ptr(a));
1262
			EXP c = DEREF_exp(exp_call_arg(a));
1263
			tag = TAG_exp(b);
1264
			if (tag == exp_identifier_tag ||
1265
			    tag == exp_member_tag) {
1266
				/* Named function selector, 'c->f' */
1267
				IDENTIFIER id;
1268
				QUALIFIER qual;
1269
				id = DEREF_id(exp_identifier_etc_id(b));
1270
				qual = DEREF_qual(exp_identifier_etc_qual(b));
1271
				if (rescan)id = rescan_func_id(id, qual_nested);
1272
				if (dependent_call(id, args)) {
1273
					e = call_func_templ(a, args, is_paren);
1274
				} else {
1275
					GRAPH gr = DEREF_graph(exp_call_base(a));
1276
					if (is_paren) {
1277
						qual |= qual_mark;
1278
					}
1279
					CONS_exp(c, args, args);
1280
					id = resolve_call(id, args, qual, 0);
1281
					use_func_id(id, 1, suppress_usage);
1282
					if (resolved_kind == KIND_CONSTR) {
1283
						e = apply_constr(id, args);
1284
					} else {
1285
						HASHID nm =
1286
						    DEREF_hashid(id_name(id));
1287
						e = apply_func_id(id, qual, gr,
1288
								  args);
1289
						if (!IS_hashid_name_etc(nm)) {
1290
							/* Check for extra
1291
							 * arguments */
1292
							int v = EXTRA_CONSTR;
1293
							CLASS_TYPE ct =
1294
							    parent_class(id);
1295
							if (IS_hashid_destr(nm)) {
1296
								v = EXTRA_DESTR;
1297
							}
1298
							e = add_constr_args(e, ct, v);
1299
						}
1300
					}
2 7u83 1301
				}
7 7u83 1302
				return (e);
2 7u83 1303
			}
7 7u83 1304
			if (tag == exp_undeclared_tag) {
1305
				/* Pseudo-destructor call, 'c->C::~C' */
1306
				IDENTIFIER id = DEREF_id(exp_undeclared_id(b));
1307
				HASHID nm = DEREF_hashid(id_name(id));
1308
				if (!IS_NULL_list(args)) {
1309
					report(crt_loc, ERR_expr_pseudo_args(nm));
1310
				}
1311
				e = trivial_destr(c);
1312
				return (e);
1313
			}
1314
			/* Pointer to member function selector, 'a.*f' */
1315
			CONS_exp(c, args, args);
1316
			break;
2 7u83 1317
		}
1318
		}
1319
	}
1320
 
7 7u83 1321
	/* Form the result for function expressions */
1322
	e = apply_func_exp(a, args);
1323
	return (e);
2 7u83 1324
}
1325
 
1326
 
1327
/*
1328
    SHOULD A FUNCTION RETURN VALUE BE IGNORED?
1329
 
1330
    This routine checks whether a function consisting of 'return e ;'
1331
    should be inlined.
1332
*/
1333
 
7 7u83 1334
static EXP
1335
check_inline_return(EXP e, TYPE ret)
2 7u83 1336
{
7 7u83 1337
	if (IS_NULL_exp(e)) {
1338
		/* No value returned */
1339
		MAKE_exp_value(ret, e);
1340
		return (e);
1341
	}
1342
	if (is_const_exp(e, -1)) {
1343
		/* Constant expression returned */
1344
		MAKE_exp_copy(ret, e, e);
1345
		return (e);
1346
	}
1347
	return (NULL_exp);
2 7u83 1348
}
1349
 
1350
 
1351
/*
1352
    SHOULD A FUNCTION CALL BE INLINED?
1353
 
1354
    Function inlining is generally left to the installers which have a
1355
    far better idea of whether a function should be inlined than the
1356
    producer.  This routine checks whether the function call 'id ( args )'
1357
    should be inlined by the producer.  This is generally done on the
1358
    basis of code size rather than efficiency.  If so it returns the result
1359
    expression.  Otherwise the null expression is returned.
1360
*/
1361
 
7 7u83 1362
EXP
1363
check_inline(IDENTIFIER id, LIST(EXP)args, TYPE ret)
2 7u83 1364
{
7 7u83 1365
	while (!IS_NULL_list(args)) {
1366
		/* Check argument list */
1367
		EXP a = DEREF_exp(HEAD_list(args));
1368
		if (!IS_NULL_exp(a)) {
1369
			return (NULL_exp);
2 7u83 1370
		}
7 7u83 1371
		args = TAIL_list(args);
1372
	}
1373
	if (IS_id_function_etc(id)) {
1374
		/* Check function definition */
1375
		EXP e = DEREF_exp(id_function_etc_defn(id));
1376
		if (!IS_NULL_exp(e) && IS_exp_sequence(e)) {
1377
			LIST(EXP)p = DEREF_list(exp_sequence_first(e));
1378
			p = TAIL_list(p);
1379
			if (IS_NULL_list(p)) {
1380
				/* Empty function definition */
1381
				e = check_inline_return(NULL_exp, ret);
1382
				return (e);
1383
			}
1384
			e = DEREF_exp(HEAD_list(p));
1385
			p = TAIL_list(p);
1386
			if (IS_NULL_list(p)) {
1387
				if (!IS_NULL_exp(e) && IS_exp_location(e)) {
1388
					/* Step over location marker */
1389
					e = DEREF_exp(exp_location_arg(e));
1390
				}
1391
				if (!IS_NULL_exp(e) && IS_exp_return_stmt(e)) {
1392
					/* Single return statement */
1393
					e = DEREF_exp(exp_return_stmt_value(e));
1394
					e = check_inline_return(e, ret);
1395
					return (e);
1396
				}
1397
			}
2 7u83 1398
		}
1399
	}
7 7u83 1400
	return (NULL_exp);
2 7u83 1401
}
1402
 
1403
 
1404
/*
1405
    CHECK FOR COMPLEX FUNCTION RETURN OR ARGUMENT TYPE
1406
 
1407
    This routine checks whether t is a complex function return or argument
1408
    type, that is to say a class type with a non-trivial constructor,
1409
    destructor, or assignment operator.  The calling convention implemented
1410
    is that such values are returned by means of a reference which is
1411
    passed to the function as the first argument, and that such values
1412
    are passed as arguments as a reference to a copy.
1413
*/
1414
 
7 7u83 1415
int
1416
pass_complex_type(TYPE t)
2 7u83 1417
{
7 7u83 1418
	if (!IS_NULL_type(t) && IS_type_compound(t)) {
1419
		CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1420
		CLASS_INFO ci = DEREF_cinfo(ctype_info(ct));
1421
		CLASS_INFO cj = (ci & cinfo_trivial);
1422
		if (cj != cinfo_trivial) {
1423
			return (1);
1424
		}
1425
	}
1426
	return (0);
2 7u83 1427
}
1428
 
1429
 
1430
/*
1431
    CHECK A FUNCTION RETURN OR PARAMETER TYPE
1432
 
1433
    This routine checks whether the type just constructed by the parser
1434
    contains type definitions and so is not suitable for use as a function
1435
    return or parameter type (as indicated by par).  The necessary
1436
    information is stored in the global variable have_type_declaration.
1437
*/
1438
 
7 7u83 1439
void
1440
func_type_defn(int par)
2 7u83 1441
{
7 7u83 1442
	int td = have_type_declaration;
1443
	if (td == TYPE_DECL_NORMAL || td == TYPE_DECL_ANON) {
1444
		if (par) {
1445
			report(crt_loc, ERR_dcl_fct_typedef_par());
1446
		} else {
1447
			report(crt_loc, ERR_dcl_fct_typedef_ret());
1448
		}
2 7u83 1449
	}
7 7u83 1450
	return;
2 7u83 1451
}
1452
 
1453
 
1454
/*
1455
    CREATE A PARAMETER TYPE
1456
 
1457
    This routine creates the parameter type for a function or template
1458
    parameter (as indicated by loc) declared with type t.  Thus functions
1459
    and, for function parameters, arrays are converted to pointers.
1460
*/
1461
 
7 7u83 1462
TYPE
1463
make_param_type(TYPE t, int loc)
2 7u83 1464
{
7 7u83 1465
	switch (TAG_type(t)) {
1466
	case type_func_tag: {
1467
		/* Function parameters are adjusted to pointers */
1468
		member_func_type(NULL_ctype, id_parameter_tag, t);
1469
		check_weak_func(t, 0);
1470
		MAKE_type_ptr(cv_none, t, t);
1471
		break;
2 7u83 1472
	}
7 7u83 1473
	case type_array_tag: {
1474
		/* Array parameters are adjusted to pointers */
1475
		if (loc == CONTEXT_PARAMETER || loc == CONTEXT_WEAK_PARAM) {
1476
			t = DEREF_type(type_array_sub(t));
1477
			MAKE_type_ptr(cv_none, t, t);
1478
		}
1479
		break;
2 7u83 1480
	}
7 7u83 1481
	}
1482
	return (t);
2 7u83 1483
}
1484
 
1485
 
1486
/*
1487
    CHECK A PARAMETER TYPE
1488
 
1489
    A parameter cannot include a type of the form pointer to unbound array
1490
    or reference to unbound array.  This routine returns an error if the type
1491
    t includes a type of this form.  Strictly the parameters of any function
1492
    components should also be checked, but these will have already been
1493
    handled by a previous check.
1494
*/
1495
 
7 7u83 1496
ERROR
1497
check_param_type(IDENTIFIER id, TYPE t)
2 7u83 1498
{
7 7u83 1499
	TYPE s = t;
1500
	int state = 0;
1501
	while (!IS_NULL_type(s)) {
1502
		switch (TAG_type(s)) {
1503
		case type_top_tag:
1504
		case type_bottom_tag: {
1505
			/* Void types */
1506
			if (state) {
1507
				return (NULL_err);
1508
			}
1509
			return (ERR_dcl_fct_par_void(id, t));
2 7u83 1510
		}
7 7u83 1511
		case type_ptr_tag:
1512
		case type_ref_tag: {
1513
			/* Pointer and reference types */
1514
			s = DEREF_type(type_ptr_etc_sub(s));
1515
			state = 1;
1516
			break;
1517
		}
1518
		case type_array_tag: {
1519
			/* Array types */
1520
			if (state) {
1521
				NAT n = DEREF_nat(type_array_size(s));
1522
				if (IS_NULL_nat(n)) {
1523
					return (ERR_dcl_fct_par_array(id, t));
1524
				}
1525
			}
1526
			s = DEREF_type(type_array_sub(s));
1527
			state = 0;
1528
			break;
1529
		}
1530
		default:
1531
			/* Other types */
1532
			return (NULL_err);
1533
		}
2 7u83 1534
	}
7 7u83 1535
	return (NULL_err);
2 7u83 1536
}
1537
 
1538
 
1539
/*
1540
    ADJUST A FUNCTION LINKAGE
1541
 
1542
    This routine adjusts the function linkage specifier cv to include
1543
    the current linkage specifier.
1544
*/
1545
 
7 7u83 1546
CV_SPEC
1547
func_linkage(CV_SPEC cv)
2 7u83 1548
{
7 7u83 1549
	DECL_SPEC ln = crt_linkage;
1550
	if (ln) {
1551
		if (ln & dspec_c) {
1552
			cv |= cv_c;
1553
		}
1554
		if (ln & dspec_cpp) {
1555
			cv |= cv_cpp;
1556
		}
1557
	}
1558
	return (cv);
2 7u83 1559
}
1560
 
1561
 
1562
/*
1563
    CREATE A LIST OF FUNCTION PARAMETER TYPES
1564
 
1565
    This routine constructs the function type with parameter declarations
1566
    given by the current namespace and const-volatile qualifiers cv.  The
1567
    kind of function is given by ell, which is a combination of the
1568
    FUNC_* values defined in function.h.  The function return type is
1569
    usually filled in later (by inject_pre_type) but may be specified
1570
    by r.  Note that function and array parameters have already been
1571
    adjusted to pointers by make_param_decl.
1572
*/
1573
 
7 7u83 1574
TYPE
1575
make_func_type(TYPE r, int ell, CV_SPEC cv, LIST(TYPE) ex)
2 7u83 1576
{
7 7u83 1577
	int no_param = 0;
1578
	TYPE t = NULL_type;
1579
	LIST(IDENTIFIER) p1;
1580
	IDENTIFIER id = NULL_id;
1581
	LIST(TYPE) q = NULL_list(TYPE);
1582
	LIST(IDENTIFIER) p = NULL_list(IDENTIFIER);
2 7u83 1583
 
7 7u83 1584
	/* Get parameter declarations */
1585
	NAMESPACE ns = crt_namespace;
1586
	MEMBER mem = DEREF_member(nspace_last(ns));
2 7u83 1587
 
7 7u83 1588
	/* Build up parameter type list (deleting cv-qualifiers) */
1589
	while (!IS_NULL_member(mem)) {
1590
		id = DEREF_id(member_id(mem));
1591
		if (!IS_NULL_id(id) && IS_id_parameter(id)) {
1592
			t = DEREF_type(id_parameter_type(id));
1593
			if ((ell & FUNC_WEAK) && !is_arg_promote(t)) {
1594
				DECL_SPEC ds;
1595
				t = arg_promote_type(t, KILL_err);
1596
				ds = DEREF_dspec(id_storage(id));
1597
				ds |= dspec_virtual;
1598
				COPY_dspec(id_storage(id), ds);
1599
			}
1600
			t = qualify_type(t, cv_none, 0);
1601
			CONS_type(t, q, q);
1602
			CONS_id(id, p, p);
1603
			no_param++;
1604
		}
1605
		mem = DEREF_member(member_next(mem));
2 7u83 1606
	}
1607
 
7 7u83 1608
	/* Check for non-prototype functions */
1609
	if (IS_NULL_type(r)) {
1610
		if (ell & FUNC_NON_PROTO) {
1611
			report(crt_loc, ERR_dcl_fct_nonproto());
1612
			if (ell == FUNC_WEAK && no_param == 0) {
1613
				/* Can't have 't weak ()' */
1614
				report(crt_loc, ERR_dcl_fct_par_weak());
1615
			}
2 7u83 1616
#if LANGUAGE_CPP
7 7u83 1617
			ell &= FUNC_ELLIPSIS;
2 7u83 1618
#endif
7 7u83 1619
		} else {
1620
			report(crt_loc, ERR_dcl_fct_proto());
1621
		}
2 7u83 1622
	}
1623
 
7 7u83 1624
	/* Check for '( ... )' */
1625
	if (no_param == 0 && (ell & FUNC_ELLIPSIS)) {
1626
		report(crt_loc, ERR_dcl_fct_par_ellipsis());
1627
	}
2 7u83 1628
 
7 7u83 1629
	/* Check for '( void )' */
1630
	if (no_param == 1 && !(ell & FUNC_ELLIPSIS)) {
1631
		/* Precisely one parameter (in t and id) */
1632
		if (IS_type_top(t)) {
1633
			HASHID nm = DEREF_hashid(id_name(id));
1634
			if (!IS_hashid_anon(nm)) {
1635
				report(crt_loc, ERR_dcl_fct_par_empty());
1636
			}
1637
			DESTROY_list(q, SIZE_type);
1638
			DESTROY_list(p, SIZE_id);
1639
			q = NULL_list(TYPE);
1640
			p = NULL_list(IDENTIFIER);
1641
		}
2 7u83 1642
	}
1643
 
7 7u83 1644
	/* Check for other void parameters */
1645
	p1 = p;
1646
	while (!IS_NULL_list(p1)) {
1647
		ERROR err;
1648
		id = DEREF_id(HEAD_list(p1));
1649
		t = DEREF_type(id_parameter_type(id));
1650
		err = check_param_type(id, t);
1651
		if (!IS_NULL_err(err)) {
1652
			/* Void or other illegal parameter type */
1653
			report(crt_loc, err);
1654
		}
1655
		p1 = TAIL_list(p1);
2 7u83 1656
	}
1657
 
7 7u83 1658
	/* Construct the function type */
1659
	MAKE_type_func(cv_none, r, q, ell, cv, q, ns, p, ex, t);
1660
	return (t);
2 7u83 1661
}
1662
 
1663
 
1664
/*
1665
    ADJUST MEMBER FUNCTION TYPES
1666
 
1667
    Each function type has two lists of parameter types, ptypes, which
1668
    gives the actual declared parameters, and mtypes, which has an extra
1669
    reference to class parameter for non-static member functions, or an
1670
    extra null parameter for static member functions.  This routine sets
1671
    the mtypes field of t from the ptypes field according to whether or not
1672
    ct is the null class and the tag of an associated declaration, itag.
1673
    Note that member function types always have C++ linkage.
1674
*/
1675
 
7 7u83 1676
void
1677
member_func_type(CLASS_TYPE ct, unsigned itag, TYPE t)
2 7u83 1678
{
7 7u83 1679
	unsigned tag = TAG_type(t);
1680
	while (tag == type_templ_tag) {
1681
		t = DEREF_type(type_templ_defn(t));
1682
		tag = TAG_type(t);
1683
	}
1684
	if (tag == type_func_tag) {
1685
		CV_SPEC cv = DEREF_cv(type_func_mqual(t));
1686
		CV_SPEC qual = (cv & cv_qual);
1687
		LIST(TYPE) ptypes = DEREF_list(type_func_ptypes(t));
1688
		LIST(TYPE) mtypes = DEREF_list(type_func_mtypes(t));
1689
		if (IS_NULL_ctype(ct)) {
1690
			/* Non-member functions */
1691
			if (qual && itag != id_type_alias_tag) {
1692
				/* Can't have cv-qualifiers */
1693
				report(crt_loc, ERR_dcl_fct_cv(qual));
1694
				cv &= ~cv_qual;
1695
				COPY_cv(type_func_mqual(t), cv);
1696
			}
1697
			if (!EQ_list(ptypes, mtypes)) {
1698
				/* Set mtypes equal to ptypes */
1699
				COPY_list(type_func_mtypes(t), ptypes);
1700
			}
2 7u83 1701
		} else {
7 7u83 1702
			/* Member functions */
1703
			if (EQ_list(ptypes, mtypes)) {
1704
				/* Add extra argument to mtypes */
1705
				TYPE r;
1706
				if (itag == id_mem_func_tag) {
1707
					IDENTIFIER cid =
1708
					    DEREF_id(ctype_name(ct));
1709
					cv = (qual | cv_cpp);
1710
					qual |= cv_lvalue;
1711
					MAKE_type_compound(qual, ct, r);
1712
					COPY_id(type_name(r), cid);
1713
					MAKE_type_ref(cv_none, r, r);
1714
				} else {
1715
					if (qual) {
1716
						/* Can't have cv-qualifiers */
1717
						report(crt_loc,
1718
						       ERR_dcl_fct_cv(qual));
1719
					}
1720
					r = NULL_type;
1721
					cv = cv_cpp;
1722
				}
1723
				CONS_type(r, ptypes, mtypes);
1724
				COPY_list(type_func_mtypes(t), mtypes);
1725
				COPY_cv(type_func_mqual(t), cv);
1726
			} else {
1727
				/* Don't check existing extra argument */
1728
				/* EMPTY */
1729
			}
2 7u83 1730
		}
1731
	}
7 7u83 1732
	return;
2 7u83 1733
}
1734
 
1735
 
1736
/*
1737
    CHECK A NON-PROTOTYPE FUNCTION TYPE
1738
 
1739
    A parameter list can only be used in a function definition.  This
1740
    routine checks the function type t with def being true for a function
1741
    definition.
1742
*/
1743
 
7 7u83 1744
void
1745
check_weak_func(TYPE t, int def)
2 7u83 1746
{
7 7u83 1747
	int ell = DEREF_int(type_func_ellipsis(t));
1748
	if (ell == FUNC_PARAMS) {
1749
		/* Have parameter list '( a1, a2, ..., an )' */
1750
		if (def) {
1751
			LOCATION loc;
1752
			LIST(TYPE) q = NULL_list(TYPE);
1753
			LIST(IDENTIFIER) p = DEREF_list(type_func_pids(t));
1754
			NAMESPACE ns = DEREF_nspace(type_func_pars(t));
1755
			MEMBER mem = DEREF_member(nspace_last(ns));
1756
			loc = decl_loc;
1757
			bad_crt_loc++;
1758
			while (!IS_NULL_member(mem)) {
1759
				IDENTIFIER id = DEREF_id(member_id(mem));
1760
				if (!IS_NULL_id(id)) {
1761
					if (IS_id_weak_param(id)) {
1762
						/* Implicitly declare
1763
						 * parameter */
1764
						ERROR err;
1765
						TYPE s = type_sint;
1766
						int par = CONTEXT_WEAK_PARAM;
1767
						DEREF_loc(id_loc(id), decl_loc);
1768
						id = make_param_decl(dspec_none, s, id, par);
1769
						init_param(id, NULL_exp);
1770
						err = ERR_dcl_fct_par_impl(id);
1771
						if (!IS_NULL_err(err)) {
1772
							ERROR err2 = ERR_dcl_type_infer(s);
1773
							err = concat_error(err, err2);
1774
							report(decl_loc, err);
1775
						}
1776
						CONS_id(id, p, p);
1777
						CONS_type(s, q, q);
1778
					} else if (IS_id_parameter(id)) {
1779
						/* Add parameter to list */
1780
						TYPE s = DEREF_type(id_parameter_type(id));
1781
						if (!is_arg_promote(s)) {
1782
							DECL_SPEC ds;
1783
							s = arg_promote_type(s, KILL_err);
1784
							ds = DEREF_dspec(id_storage(id));
1785
							ds |= dspec_virtual;
1786
							COPY_dspec(id_storage(id), ds);
1787
						}
1788
						s = qualify_type(s, cv_none, 0);
1789
						CONS_id(id, p, p);
1790
						CONS_type(s, q, q);
1791
					}
1792
				}
1793
				mem = DEREF_member(member_next(mem));
2 7u83 1794
			}
7 7u83 1795
			COPY_list(type_func_ptypes(t), q);
1796
			COPY_list(type_func_mtypes(t), q);
1797
			COPY_list(type_func_pids(t), p);
1798
			bad_crt_loc--;
1799
			decl_loc = loc;
1800
			ell = FUNC_WEAK_PARAMS;
1801
		} else {
1802
			report(crt_loc, ERR_dcl_fct_weak());
1803
			ell = FUNC_NO_PARAMS;
2 7u83 1804
		}
7 7u83 1805
	} else if (ell == FUNC_NO_PARAMS) {
1806
		/* Have empty parameter list '()' */
1807
		if (def) {
1808
			ell = FUNC_WEAK_PARAMS;
1809
		}
2 7u83 1810
	}
7 7u83 1811
	COPY_int(type_func_ellipsis(t), ell);
1812
	return;
2 7u83 1813
}
1814
 
1815
 
1816
/*
1817
    CHECK THE FUNCTION COMPONENTS OF AN OBJECT TYPE
1818
 
1819
    This routine checks the function components of an object with the
1820
    given identifier tag and type t.
1821
*/
1822
 
7 7u83 1823
void
1824
object_type(TYPE t, unsigned tag)
2 7u83 1825
{
7 7u83 1826
	int depth = 0;
1827
	if (tag == id_type_alias_tag || tag == null_tag) {
1828
		/* Force errors in these cases */
1829
		depth = 2;
1830
	}
1831
	while (!IS_NULL_type(t)) {
1832
		ASSERT(ORDER_type == 18);
1833
		switch (TAG_type(t)) {
1834
		case type_func_tag: {
1835
			/* Function types */
1836
			LIST(TYPE) ex;
1837
			CV_SPEC cv = DEREF_cv(type_func_mqual(t));
2 7u83 1838
 
7 7u83 1839
			/* Check function qualifiers */
1840
			CV_SPEC mq = (cv & cv_qual);
1841
			CV_SPEC lq = (cv & cv_language);
1842
			if (depth == 0) {
1843
				if (tag == id_mem_func_tag) {
1844
					/* Member functions have C++ linkage */
1845
					lq = cv_cpp;
1846
				} else {
1847
					if (mq) {
1848
						/* Can't have cv-qualifiers */
1849
						report(crt_loc,
1850
						       ERR_dcl_fct_cv_decl(mq));
1851
						mq = cv_none;
1852
					}
1853
					if (tag == id_stat_mem_func_tag) {
1854
						/* Member functions have C++
1855
						 * linkage */
1856
						lq = cv_cpp;
1857
					}
1858
				}
2 7u83 1859
			}
7 7u83 1860
			if (lq == cv_none) {
1861
				lq = cv_lang;
2 7u83 1862
			}
7 7u83 1863
			COPY_cv(type_func_mqual(t), (mq | lq));
2 7u83 1864
 
7 7u83 1865
			/* Check default arguments */
1866
			if (depth && check_func_dargs(t, 0, 0)) {
1867
				report(crt_loc, ERR_dcl_fct_default_bad());
1868
			}
2 7u83 1869
 
7 7u83 1870
			/* Check exception specifiers */
1871
			ex = DEREF_list(type_func_except(t));
1872
			if (!EQ_list(ex, empty_type_set)) {
1873
				if (!EQ_list(ex, univ_type_set)) {
1874
					ulong n = (ulong)LENGTH_list(ex);
1875
					IGNORE check_value(OPT_VAL_exception_specs, n);
1876
				}
1877
				if (depth > 1) {
1878
					report(crt_loc, ERR_except_spec_bad());
1879
				}
1880
			}
2 7u83 1881
 
7 7u83 1882
			/* Recheck parameter types */
1883
			if (depth > 1) {
1884
				LIST(TYPE)p;
1885
				p = DEREF_list(type_func_ptypes(t));
1886
				while (!IS_NULL_list(p)) {
1887
					TYPE s = DEREF_type(HEAD_list(p));
1888
					s = find_func_type(s);
1889
					if (!IS_NULL_type(s)) {
1890
						ex = DEREF_list(type_func_except(s));
1891
						if (!EQ_list(ex, empty_type_set)) {
1892
							report(crt_loc, ERR_except_spec_bad());
1893
						}
1894
					}
1895
					p = TAIL_list(p);
1896
				}
2 7u83 1897
			}
7 7u83 1898
			return;
2 7u83 1899
		}
7 7u83 1900
		case type_ptr_tag: {
1901
			t = DEREF_type(type_ptr_sub(t));
1902
			depth++;
1903
			break;
1904
		}
1905
		case type_ref_tag: {
1906
			t = DEREF_type(type_ref_sub(t));
1907
			break;
1908
		}
1909
		case type_ptr_mem_tag: {
1910
			t = DEREF_type(type_ptr_mem_sub(t));
1911
			depth++;
1912
			break;
1913
		}
1914
		case type_array_tag: {
1915
			t = DEREF_type(type_array_sub(t));
1916
			depth++;
1917
			break;
1918
		}
1919
		case type_templ_tag: {
1920
			t = DEREF_type(type_templ_defn(t));
1921
			break;
1922
		}
1923
		default:
1924
			/* Other types */
1925
			return;
1926
		}
2 7u83 1927
	}
7 7u83 1928
	return;
2 7u83 1929
}
1930
 
1931
 
1932
/*
1933
    DECLARE A FUNCTION TYPE
1934
 
1935
    This routine checks the function or function typedef id of type t,
1936
    including checking that if a default argument is given for a
1937
    particular parameter one is also given for all subsequent parameters.
1938
    The redeclaration case, where default arguments can be inherited
1939
    from the previous declaration is dealt with by redecl_func_type.
1940
*/
1941
 
7 7u83 1942
void
1943
decl_func_type(IDENTIFIER id, TYPE t, int def)
2 7u83 1944
{
7 7u83 1945
	NAMESPACE pns;
1946
	int is_templ = 0;
1947
	unsigned tag = TAG_id(id);
2 7u83 1948
 
7 7u83 1949
	/* Step over template components */
1950
	if (IS_type_templ(t)) {
1951
		t = check_templ_params(t, id);
1952
		is_templ = 1;
1953
	}
1954
	pns = DEREF_nspace(type_func_pars(t));
1955
	COPY_id(nspace_name(pns), id);
2 7u83 1956
 
7 7u83 1957
	/* Check default arguments */
1958
	IGNORE check_func_dargs(t, 1, 0);
2 7u83 1959
 
7 7u83 1960
	/* Check cv-qualifiers */
1961
	switch (tag) {
1962
	case id_function_tag:
1963
	case id_mem_func_tag:
1964
	case id_stat_mem_func_tag: {
1965
		/* Allow for member functions */
1966
		CLASS_TYPE ct = parent_class(id);
1967
		member_func_type(ct, tag, t);
2 7u83 1968
 
7 7u83 1969
		/* Allow for overloading of template functions */
1970
		if (is_templ) {
1971
			templ_func_decl(id);
1972
		} else {
1973
			IDENTIFIER over = DEREF_id(id_function_etc_over(id));
1974
			if (!IS_NULL_id(over)) {
1975
				/* Overloads a template function */
1976
				DECL_SPEC ds = DEREF_dspec(id_storage(over));
1977
				if (ds & dspec_template) {
1978
					templ_func_decl(id);
1979
				}
1980
			}
2 7u83 1981
		}
7 7u83 1982
		break;
2 7u83 1983
	}
7 7u83 1984
	default:
1985
		/* Allow for function typedefs */
1986
		member_func_type(NULL_ctype, tag, t);
1987
		break;
2 7u83 1988
	}
7 7u83 1989
	UNUSED(def);
1990
	return;
2 7u83 1991
}
1992
 
1993
 
1994
/*
1995
    REDECLARE DEFAULT ARGUMENTS
1996
 
1997
    This routine checks and unifies the default arguments for the equal
1998
    function types s and t.  It returns the number of default arguments
1999
    added by this unification.
2000
*/
2001
 
7 7u83 2002
static unsigned
2003
redecl_func_dargs(TYPE s, TYPE t)
2 7u83 2004
{
7 7u83 2005
    unsigned nargs = 0;
2006
    if (IS_type_func(s) && IS_type_func(t)) {
2007
	int started = 0;
2008
	LIST(IDENTIFIER) ps = DEREF_list(type_func_pids(s));
2009
	LIST(IDENTIFIER) pt = DEREF_list(type_func_pids(t));
2010
	while (!IS_NULL_list(ps) && !IS_NULL_list(pt)) {
2011
	    ERROR err = NULL_err;
2012
	    IDENTIFIER as = DEREF_id(HEAD_list(ps));
2013
	    IDENTIFIER at = DEREF_id(HEAD_list(pt));
2014
	    EXP ds = DEREF_exp(id_parameter_init(as));
2015
	    EXP dt = DEREF_exp(id_parameter_init(at));
2016
	    if (IS_NULL_exp(ds)) {
2 7u83 2017
		/* No existing default argument */
7 7u83 2018
		if (IS_NULL_exp(dt)) {
2019
		    if (started) {
2 7u83 2020
			/* Missing default arguments */
7 7u83 2021
			TYPE r = DEREF_type(id_parameter_type(at));
2022
			err = ERR_dcl_fct_default_missing(at);
2023
			MAKE_exp_value(r, dt);
2024
			COPY_exp(id_parameter_init(at), dt);
2025
			COPY_exp(id_parameter_init(as), dt);
2 7u83 2026
		    }
2027
		} else {
2028
		    /* New default argument */
7 7u83 2029
		    COPY_exp(id_parameter_init(as), dt);
2030
		    dt = DEREF_exp(id_parameter_term(at));
2031
		    COPY_exp(id_parameter_term(as), dt);
2032
		    nargs++;
2033
		    started = 1;
2 7u83 2034
		}
2035
	    } else {
2036
		/* Existing default argument */
7 7u83 2037
		if (IS_NULL_exp(dt)) {
2 7u83 2038
		    /* Inherited default argument */
7 7u83 2039
		    COPY_exp(id_parameter_init(at), ds);
2040
		    ds = DEREF_exp(id_parameter_term(as));
2041
		    COPY_exp(id_parameter_term(at), ds);
2 7u83 2042
		} else {
2043
		    /* Redefined default argument */
7 7u83 2044
		    PTR(LOCATION)sloc = id_loc(as);
2045
		    if (eq_exp(ds, dt, 0)) {
2046
			err = ERR_dcl_fct_default_dup(at, sloc);
2 7u83 2047
		    } else {
2048
			/* NOT YET IMPLEMENTED: dt uncompiled */
7 7u83 2049
			err = ERR_dcl_fct_default_redef(at, sloc);
2 7u83 2050
		    }
7 7u83 2051
		    COPY_exp(id_parameter_init(as), dt);
2052
		    dt = DEREF_exp(id_parameter_term(at));
2053
		    COPY_exp(id_parameter_term(as), dt);
2 7u83 2054
		}
7 7u83 2055
		started = 1;
2 7u83 2056
	    }
7 7u83 2057
	    if (!IS_NULL_err(err)) {
2 7u83 2058
		/* Report any errors */
7 7u83 2059
		LOCATION loc;
2060
		DEREF_loc(id_loc(at), loc);
2061
		report(loc, err);
2 7u83 2062
	    }
7 7u83 2063
	    ps = TAIL_list(ps);
2064
	    pt = TAIL_list(pt);
2 7u83 2065
	}
2066
    }
7 7u83 2067
    return (nargs);
2 7u83 2068
}
2069
 
2070
 
2071
/*
2072
    REDECLARE A FUNCTION TYPE
2073
 
2074
    This routine deals with the compatible redeclaration of the function
2075
    id of type s to have type t.  It unifies the parameter types, checking
2076
    default arguments etc.  It also checks any exception specifications
2077
    for the two declarations.  The parameter names in the returned type
2078
    are those from t if def is true, and those from s otherwise.
2079
*/
2080
 
7 7u83 2081
TYPE
2082
redecl_func_type(IDENTIFIER id, TYPE s, TYPE t, int def, int dargs)
2 7u83 2083
{
7 7u83 2084
	TYPE fs = s;
2085
	TYPE ft = t;
2086
	CV_SPEC qs, qt;
2087
	NAMESPACE ns, nt;
2088
	unsigned tag = TAG_id(id);
2 7u83 2089
 
7 7u83 2090
	/* Check template components */
2091
	redecl_template(&fs, &ft, id);
2092
	ns = DEREF_nspace(type_func_pars(fs));
2093
	nt = DEREF_nspace(type_func_pars(ft));
2094
	COPY_id(nspace_name(ns), id);
2095
	COPY_id(nspace_name(nt), id);
2 7u83 2096
 
2097
#if LANGUAGE_C
7 7u83 2098
	/* Copy composite type information */
2099
	if (def) {
2100
		TYPE ret = DEREF_type(type_func_ret(fs));
2101
		int ell = DEREF_int(type_func_ellipsis(fs));
2102
		LIST(TYPE)ptypes = DEREF_list(type_func_ptypes(fs));
2103
		COPY_list(type_func_ptypes(ft), ptypes);
2104
		COPY_int(type_func_ellipsis(ft), ell);
2105
		COPY_type(type_func_ret(ft), ret);
2106
	}
2 7u83 2107
#endif
2108
 
7 7u83 2109
	/* Check default arguments */
2110
	if (dargs && redecl_func_dargs(fs, ft)) {
2111
		if (IS_type_templ(t)) {
2112
			report(decl_loc, ERR_dcl_fct_default_templ());
2113
		}
2 7u83 2114
	}
2115
 
7 7u83 2116
	/* Check exception specifications */
2117
	if (eq_except(s, t) != 2) {
2118
		PTR(LOCATION)loc = id_loc(id);
2119
		report(decl_loc, ERR_except_spec_wrong(id, loc));
2120
	}
2 7u83 2121
 
7 7u83 2122
	/* Check cv-qualifiers */
2123
	switch (tag) {
2124
	case id_mem_func_tag:
2125
	case id_stat_mem_func_tag: {
2126
		/* Allow for member functions */
2127
		CLASS_TYPE ct = parent_class(id);
2128
		member_func_type(ct, tag, ft);
2129
		break;
2 7u83 2130
	}
7 7u83 2131
	default:
2132
		/* Allow for function typedefs */
2133
		member_func_type(NULL_ctype, tag, ft);
2134
		break;
2 7u83 2135
	}
2136
 
7 7u83 2137
	/* Check function qualifiers */
2138
	qs = DEREF_cv(type_func_mqual(fs));
2139
	qt = DEREF_cv(type_func_mqual(ft));
2140
	if (qs != qt) {
2141
		qs &= cv_language;
2142
		qt &= cv_language;
2143
		if (qs != qt) {
2144
			/* Language specifiers don't match */
2145
			string ln = linkage_string(dspec_none, qs);
2146
			report(decl_loc, ERR_dcl_link_func(ln));
2147
		}
2 7u83 2148
	}
2149
 
7 7u83 2150
	/* Select type to continue with */
2151
	if (def) {
2152
		LIST(IDENTIFIER) sids = DEREF_list(type_func_pids(fs));
2153
		LIST(IDENTIFIER) tids = DEREF_list(type_func_pids(ft));
2154
		if (!EQ_list(sids, tids)) {
2155
			while (!IS_NULL_list(sids) && !IS_NULL_list(tids)) {
2156
				/* Identify parameters */
2157
				IDENTIFIER sid = DEREF_id(HEAD_list(sids));
2158
				IDENTIFIER tid = DEREF_id(HEAD_list(tids));
2159
				tid = chase_alias(tid);
2160
				sid = chase_alias(sid);
2161
				COPY_id(id_alias(sid), tid);
2162
				tids = TAIL_list(tids);
2163
				sids = TAIL_list(sids);
2164
			}
2165
		}
2166
		if (IS_type_templ(t)) {
2167
			/* Reset primary function template */
2168
			reset_primary_templ(s, t);
2169
		}
2170
		s = t;
2 7u83 2171
	}
7 7u83 2172
	return (s);
2 7u83 2173
}
2174
 
2175
 
2176
/*
2177
    FIND THE FUNCTION COMPONENT OF A TYPE
2178
 
2179
    This routine returns the function component of a function, pointer to
2180
    function, or pointer to member function type.
2181
*/
2182
 
7 7u83 2183
TYPE
2184
find_func_type(TYPE t)
2 7u83 2185
{
7 7u83 2186
	if (!IS_NULL_type(t)) {
2187
		unsigned tag = TAG_type(t);
2188
		if (tag == type_ref_tag) {
2189
			t = DEREF_type(type_ref_sub(t));
2190
			tag = TAG_type(t);
2191
		}
2192
		if (tag == type_ptr_tag) {
2193
			t = DEREF_type(type_ptr_sub(t));
2194
			tag = TAG_type(t);
2195
		} else if (tag == type_ptr_mem_tag) {
2196
			t = DEREF_type(type_ptr_mem_sub(t));
2197
			tag = TAG_type(t);
2198
		}
2199
		if (tag == type_func_tag) {
2200
			return (t);
2201
		}
2 7u83 2202
	}
7 7u83 2203
	return (NULL_type);
2 7u83 2204
}
2205
 
2206
 
2207
/*
2208
    STACK OF DECLARATOR LOCATIONS
2209
 
2210
    The location decl_loc refers to the current declarator.  This value
2211
    needs to be stored during parameter declarations and this stack is
2212
    used for this purpose.
2213
*/
2214
 
7 7u83 2215
static STACK(LOCATION) decl_locs = NULL_stack(LOCATION);
2 7u83 2216
 
2217
 
2218
/*
2219
    BEGIN FUNCTION PARAMETER NAMESPACE
2220
 
2221
    This routine is called at the start of a list of function parameters to
2222
    initialise the parameter namespace.  id gives the associated function
2223
    name (or the null identifier).
2224
*/
2225
 
7 7u83 2226
void
2227
begin_param(IDENTIFIER id)
2 7u83 2228
{
7 7u83 2229
	NAMESPACE ns = make_namespace(id, nspace_param_tag, 0);
2230
	push_namespace(ns);
2231
	PUSH_loc(decl_loc, decl_locs);
2232
	return;
2 7u83 2233
}
2234
 
2235
 
2236
/*
2237
    END FUNCTION PARAMETER NAMESPACE
2238
 
2239
    This routine is called at the end of a list of function parameters.
2240
*/
2241
 
7 7u83 2242
void
2243
end_param(void)
2 7u83 2244
{
7 7u83 2245
	IGNORE pop_namespace();
2246
	POP_loc(decl_loc, decl_locs);
2247
	return;
2 7u83 2248
}
2249
 
2250
 
2251
/*
2252
    ADJUST A FUNCTION PARAMETER NAMESPACE
2253
 
2254
    This routine adjusts the parameter namespace of the dfunction type
2255
    t used in a function definition.
2256
*/
2257
 
7 7u83 2258
void
2259
adjust_param(TYPE t)
2 7u83 2260
{
7 7u83 2261
	NAMESPACE ns;
2262
	while (IS_type_templ(t)) {
2263
		t = DEREF_type(type_templ_defn(t));
2264
	}
2265
	ns = DEREF_nspace(type_func_pars(t));
2266
	MODIFY_nspace_block_etc(nspace_block_tag, ns);
2267
	report(crt_loc, ERR_dcl_fct_typedef());
2268
	return;
2 7u83 2269
}
2270
 
2271
 
2272
/*
2273
    READ A LIST OF NON-PROTOTYPE FUNCTION PARAMETERS
2274
 
2275
    This routine reads a list of function parameters, returning true for
2276
    a function definition.
2277
*/
2278
 
7 7u83 2279
int
2280
function_params(TYPE t)
2 7u83 2281
{
7 7u83 2282
	int def = 0;
2283
	if (IS_type_func(t)) {
2284
		int func = have_func_declarator;
2285
		int ell = DEREF_int(type_func_ellipsis(t));
2286
		NAMESPACE ns = DEREF_nspace(type_func_pars(t));
2287
		in_weak_param++;
2288
		push_namespace(ns);
2289
		while (predict_dspec(0)) {
2290
			if (!func) {
2291
				adjust_param(t);
2292
				func = 1;
2293
			}
2294
			if (ell != FUNC_NO_PARAMS && ell != FUNC_PARAMS) {
2295
				/* Can't have parameters with prototype */
2296
				report(crt_loc, ERR_dcl_fct_par_proto());
2297
				ell = FUNC_PARAMS;
2298
			}
2299
			parse_decl(NULL_type, dspec_none);
2300
			def = 1;
2301
		}
2302
		if (predict_func_defn()) {
2303
			if (!func)adjust_param(t);
2304
			def = 1;
2305
		}
2306
		check_weak_func(t, def);
2307
		IGNORE pop_namespace();
2308
		in_weak_param--;
2 7u83 2309
	}
7 7u83 2310
	return (def);
2 7u83 2311
}
2312
 
2313
 
2314
/*
2315
    MAIN FUNCTION
2316
 
2317
    This variable is used to hold the main function.
2318
*/
2319
 
7 7u83 2320
IDENTIFIER main_function = NULL_id;
2 7u83 2321
 
2322
 
2323
/*
2324
    CHECK THE TYPE OF THE MAIN FUNCTION
2325
 
2326
    This routine checks the type t of the main function nm.  The return
2327
    type must always be 'int', but the parameter types are implementation
2328
    dependent.  Basically, anything other than:
2329
 
2330
	    int main ( void ) ;
2331
	    int main ( int, char **, [extra parameters] ) ;
2332
 
2333
    is deemed to be unorthodox.
2334
*/
2335
 
7 7u83 2336
TYPE
2337
check_main(TYPE t, HASHID nm)
2 7u83 2338
{
7 7u83 2339
    int ok = 0;
2340
    if (IS_type_func(t)) {
2341
	TYPE r = DEREF_type(type_func_ret(t));
2342
	LIST(TYPE) p = DEREF_list(type_func_ptypes(t));
2343
	int ell = DEREF_int(type_func_ellipsis(t));
2344
	if (!eq_type_unqual(r, type_sint)) {
2 7u83 2345
	    /* Return type is wrong */
7 7u83 2346
	    ERROR err = ERR_basic_start_main_ret(r, nm);
2347
	    report(crt_loc, err);
2 7u83 2348
	}
7 7u83 2349
	if (IS_NULL_list(p)) {
2 7u83 2350
	    /* Check for no parameter case */
7 7u83 2351
	    if (!(ell & FUNC_ELLIPSIS)) {
2352
		    ok = 2;
2353
	    }
2 7u83 2354
	} else {
2355
	    /* Check for two parameter case */
7 7u83 2356
	    TYPE s = DEREF_type(HEAD_list(p));
2357
	    if (eq_type_unqual(s, type_sint)) {
2358
		p = TAIL_list(p);
2359
		if (!IS_NULL_list(p)) {
2360
		    s = DEREF_type(HEAD_list(p));
2361
		    if (IS_type_ptr(s)) {
2362
			s = DEREF_type(type_ptr_sub(s));
2363
			if (IS_type_ptr(s)) {
2364
			    s = DEREF_type(type_ptr_sub(s));
2365
			    if (eq_type_unqual(s, type_char)) {
2366
				p = TAIL_list(p);
2367
				if (IS_NULL_list(p)) {
2368
				    if (!(ell & FUNC_ELLIPSIS)) {
2369
					ok = 2;
2 7u83 2370
				    } else {
7 7u83 2371
					ok = 1;
2 7u83 2372
				    }
2373
				} else {
7 7u83 2374
				    ok = 1;
2 7u83 2375
				}
2376
			    }
2377
			}
2378
		    }
2379
		}
2380
	    }
2381
	}
2382
    }
7 7u83 2383
    if (!ok) {
2 7u83 2384
	/* Warn about unorthodox parameter types */
7 7u83 2385
	ERROR err = ERR_basic_start_main_proto(t, nm);
2386
	report(crt_loc, err);
2 7u83 2387
    }
7 7u83 2388
    return (t);
2 7u83 2389
}
2390
 
2391
 
2392
/*
2393
    RECHECK THE MAIN FUNCTION
2394
 
2395
    This routine rechecks the main function id.  In particular 'main' can't
2396
    be overloaded.
2397
*/
2398
 
7 7u83 2399
void
2400
recheck_main(IDENTIFIER id)
2 7u83 2401
{
7 7u83 2402
	TYPE fn = DEREF_type(id_function_etc_type(id));
2403
	IDENTIFIER over = DEREF_id(id_function_etc_over(id));
2404
	if (!IS_NULL_id(over) || IS_type_templ(fn)) {
2405
		/* Can't overload 'main' */
2406
		report(decl_loc, ERR_basic_start_main_over(id));
2407
	}
2408
	main_function = id;
2409
	return;
2 7u83 2410
}
2411
 
2412
 
2413
/*
2414
    CURRENT FUNCTION INFORMATION
2415
 
2416
    These variables are used to hold information concerning the current
2417
    function, including its name, return type and exception specification.
2418
*/
2419
 
7 7u83 2420
IDENTIFIER crt_func_id = NULL_id;
2421
TYPE crt_func_return = NULL_type;
2422
int crt_func_complex = 0;
2 7u83 2423
 
2424
 
2425
/*
2426
    NESTED FUNCTION STACK
2427
 
2428
    Note that it is indirectly possible to have nested function definitions
2429
    (a function definition can include a class definition which can include
2430
    a member function definition).  These stacks are used to preserve
2431
    information across nested functions.
2432
*/
2433
 
2434
static struct {
7 7u83 2435
	STACK(IDENTIFIER)id;
2436
	STACK(TYPE)ret;
2437
	STACK(int)unreached;
2438
	STACK(int)declaration;
2439
	STACK(int)destructor;
2440
	STACK(int)handler;
2441
	STACK(STACK(EXP))loops;
2442
	STACK(NAMESPACE)labels;
2443
	STACK(LIST(EXP))solves;
2444
	STACK(LIST(EXP))tries;
2445
	STACK(unsigned long)opts;
2 7u83 2446
} func_stack = {
7 7u83 2447
	NULL_stack(IDENTIFIER),
2448
	NULL_stack(TYPE),
2449
	NULL_stack(int),
2450
	NULL_stack(int),
2451
	NULL_stack(int),
2452
	NULL_stack(int),
2453
	NULL_stack(STACK(EXP)),
2454
	NULL_stack(NAMESPACE),
2455
	NULL_stack(LIST(EXP)),
2456
	NULL_stack(LIST(EXP)),
2457
	NULL_stack(unsigned long)
2458
};
2 7u83 2459
 
2460
 
2461
/*
2462
    BEGIN A FUNCTION DEFINITION
2463
 
2464
    This routine is called at the start of a function definition.  t gives
2465
    the function type.
2466
*/
2467
 
7 7u83 2468
void
2469
begin_function(IDENTIFIER id)
2 7u83 2470
{
7 7u83 2471
	TYPE t;
2472
	int ell;
2473
	TYPE ret;
2474
	NAMESPACE ns;
2475
	NAMESPACE bns;
2476
	unsigned long n;
2477
	LIST(TYPE) ex;
2478
	ERROR err = NULL_err;
2479
	LIST(IDENTIFIER) p;
2480
	unsigned long npars = 0;
2 7u83 2481
 
7 7u83 2482
	/* Check for previous definition */
2483
	if (IS_id_function_etc(id)) {
2484
		EXP e = DEREF_exp(id_function_etc_defn(id));
2485
		DECL_SPEC ds = DEREF_dspec(id_storage(id));
2486
		t = DEREF_type(id_function_etc_type(id));
2487
		if (IS_NULL_exp(e)) {
2488
			/* Mark with dummy definition */
2489
			MAKE_exp_value(t, e);
2490
			COPY_exp(id_function_etc_defn(id), e);
2491
		} else {
2492
			/* Already defined */
2493
			PTR(LOCATION) loc = id_loc(id);
2494
			report(decl_loc, ERR_basic_odr_def_func(id, loc));
2495
		}
2496
		if ((ds & dspec_inline) && (ds & dspec_called)) {
2497
			report(decl_loc, ERR_dcl_fct_spec_inline_call(id));
2498
		}
2 7u83 2499
	} else {
7 7u83 2500
		t = type_func_void;
2 7u83 2501
	}
7 7u83 2502
	COPY_loc(id_loc(id), decl_loc);
2503
	stmt_loc = crt_loc;
2504
 
2505
	/* Decompose function type */
2506
	while (IS_type_templ(t)) {
2507
		t = DEREF_type(type_templ_defn(t));
2 7u83 2508
	}
7 7u83 2509
	ell = DEREF_int(type_func_ellipsis(t));
2510
	p = DEREF_list(type_func_pids(t));
2511
	ex = DEREF_list(type_func_except(t));
2512
	ns = DEREF_nspace(type_func_pars(t));
2513
	if (IS_NULL_nspace(ns)) {
2514
		ns = make_namespace(id, nspace_param_tag, 0);
2515
	}
2516
	bns = ns;
2 7u83 2517
 
7 7u83 2518
	/* Check return type */
2519
	ret = DEREF_type(type_func_ret(t));
2520
	ret = check_ret_type(ret, &err, 1);
2521
	if (!IS_NULL_err(err)) {
2522
		err = concat_error(err, ERR_dcl_fct_ret());
2523
		report(decl_loc, err);
2524
	}
2 7u83 2525
 
7 7u83 2526
	/* Save old function data */
2527
	PUSH_id(crt_func_id, func_stack.id);
2528
	PUSH_type(crt_func_return, func_stack.ret);
2529
	PUSH_int(unreached_code, func_stack.unreached);
2530
	PUSH_int(in_declaration, func_stack.declaration);
2531
	PUSH_int(have_destructor, func_stack.destructor);
2532
	PUSH_int(in_func_handler, func_stack.handler);
2533
	PUSH_stack(crt_loop_stack, func_stack.loops);
2534
	PUSH_nspace(label_namespace, func_stack.labels);
2535
	PUSH_list(all_solve_stmts, func_stack.solves);
2536
	PUSH_list(all_try_blocks, func_stack.tries);
2537
	n = crt_option_value(OPT_VAL_statement_depth);
2538
	PUSH_ulong(n, func_stack.opts);
2539
	n = crt_option_value(OPT_VAL_nested_class);
2540
	PUSH_ulong(n, func_stack.opts);
2541
	n = number_errors;
2542
	PUSH_ulong(n, func_stack.opts);
2 7u83 2543
 
7 7u83 2544
	/* Set up new function data */
2545
	commentary(id);
2546
	crt_func_id = id;
2547
	crt_func_return = ret;
2548
	crt_func_complex = pass_complex_type(ret);
2549
	unreached_code = 0;
2550
	unreached_last = 0;
2551
	unreached_fall = 1;
2552
	in_declaration = 0;
2553
	have_destructor = 0;
2554
	in_func_handler = 0;
2555
	crt_loop_stack = NULL_stack(EXP);
2556
	all_solve_stmts = NULL_list(EXP);
2557
	all_try_blocks = NULL_list(EXP);
2558
	crt_option_value(OPT_VAL_statement_depth) = 0;
2559
	crt_option_value(OPT_VAL_nested_class) = 0;
2560
	if (option(OPT_variable)) {
2561
		record_location++;
2 7u83 2562
	}
7 7u83 2563
	label_namespace = make_namespace(id, nspace_label_tag, 0);
2564
	if (do_dump) {
2565
		dump_declare(id, &decl_loc, 1);
2 7u83 2566
	}
7 7u83 2567
 
2568
	/* Modify function parameter namespace */
2569
	if (IS_nspace_block(bns)) {
2570
		bns = NULL_nspace;
2 7u83 2571
	}
7 7u83 2572
	MODIFY_nspace_block_etc(nspace_block_tag, ns);
2573
	COPY_id(nspace_name(ns), id);
2574
 
2575
	/* Check function parameters */
2576
	crt_id_qualifier = qual_none;
2577
	while (!IS_NULL_list(p)) {
2578
		IDENTIFIER par = DEREF_id(HEAD_list(p));
2579
		IDENTIFIER par2 = DEREF_id(id_alias(par));
2580
		HASHID pnm = DEREF_hashid(id_name(par));
2581
		TYPE pt = DEREF_type(id_parameter_type(par));
2582
		unsigned ptag = TAG_type(pt);
2583
		if (!EQ_id(par2, par)) {
2584
			/* Rename parameter if necessary */
2585
			par2 = chase_alias(par2);
2586
			pnm = DEREF_hashid(id_name(par2));
2587
			COPY_hashid(id_name(par), pnm);
2588
			COPY_id(id_alias(par), par2);
2 7u83 2589
		}
7 7u83 2590
		if (do_local) {
2591
			LOCATION loc;
2592
			DEREF_loc(id_loc(par), loc);
2593
			dump_declare(par, &loc, 1);
2594
		}
2595
		if (IS_hashid_anon(pnm)) {
2596
			/* Report anonymous parameters */
2597
			LOCATION loc;
2598
			DEREF_loc(id_loc(par), loc);
2599
			report(loc, ERR_dcl_fct_par_anon());
2600
		}
2601
		switch (ptag) {
2602
		case type_top_tag:
2603
		case type_bottom_tag:
2604
			/* Void parameters have already been reported */
2605
			break;
2606
		case type_ref_tag:
2607
			/* References don't need checking */
2608
			break;
2609
		default:
2610
			/* Check for incomplete types */
2611
			err = check_complete(pt);
2612
			if (!IS_NULL_err(err)) {
2613
				LOCATION loc;
2614
				ERROR err2 = ERR_basic_types_par_incompl(par);
2615
				err = concat_error(err, err2);
2616
				DEREF_loc(id_loc(par), loc);
2617
				report(loc, err);
2618
			}
2619
			break;
2620
		}
2621
		if (option(OPT_decl_hide)) {
2622
			check_hiding(par);
2623
		}
2624
		npars++;
2625
		p = TAIL_list(p);
2 7u83 2626
	}
2627
 
7 7u83 2628
	/* Declare parameters */
2629
	push_namespace(ns);
2630
	block_namespace = bns;
2631
	IGNORE check_value(OPT_VAL_func_pars, npars);
2 7u83 2632
 
7 7u83 2633
	/* Deal with the this pointer */
2634
	if (IS_id_mem_func(id)) {
2635
		IGNORE make_this_decl(id);
2636
	}
2 7u83 2637
 
7 7u83 2638
	/* Deal with the ellipsis parameter */
2639
	if (ell & FUNC_ELLIPSIS) {
2640
		make_ellipsis_decl();
2641
	}
2 7u83 2642
 
7 7u83 2643
	/* Create dummy try block */
2644
	start_try_check(ex);
2645
	return;
2 7u83 2646
}
2647
 
2648
 
2649
/*
2650
    END A FUNCTION DEFINITION
2651
 
2652
    This routine is called at the end of a function definition.  The
2653
    expression body gives the compound statement comprising the definition.
2654
    Note that the parameters are in the outermost scope of body and so
2655
    have already been taken out of scope.
2656
*/
2657
 
7 7u83 2658
EXP
2659
end_function(IDENTIFIER id, EXP body)
2 7u83 2660
{
7 7u83 2661
	/* Check for errors in function */
2662
	TYPE ret;
2663
	int flow = 0;
2664
	unsigned long n;
2665
	POP_ulong(n, func_stack.opts);
2666
	if (option(OPT_variable)) {
2667
		if (n == number_errors) {
2668
			flow = 1;
2669
		}
2670
		record_location--;
2671
	}
2 7u83 2672
 
7 7u83 2673
	/* Check function body */
2674
	if (!IS_NULL_exp(body)) {
2675
		if (!unreached_code) {
2676
			/* Check for falling out of function */
2677
			EXP e = fall_return_stmt();
2678
			body = add_compound_stmt(body, e);
2679
		}
2680
		unreached_code = 0;
2681
		set_parent_stmt(body, NULL_exp);
2682
		if (IS_exp_solve_stmt(body)) {
2683
			/* Analyse enclosing solve statement */
2684
			body = solve_labels(body);
2685
		} else if (check_labels()) {
2686
			/* Construct enclosing solve statement */
2687
			EXP a;
2688
			MAKE_exp_solve_stmt(type_void, body, a);
2689
			CONS_exp(a, all_solve_stmts, all_solve_stmts);
2690
			set_parent_stmt(body, a);
2691
			body = solve_labels(a);
2692
		}
2693
		end_solve_stmts();
2 7u83 2694
#if LANGUAGE_CPP
7 7u83 2695
		if (!IS_NULL_list(all_try_blocks)) {
2696
			/* Check all try blocks */
2697
			end_try_blocks(id);
2698
		}
2 7u83 2699
#endif
7 7u83 2700
		body = end_try_check(id, body);
2701
		if (flow || (have_destructor && do_usage)) {
2702
			/* Variable flow analysis */
2703
			check_flow(id, body, flow);
2704
		}
2 7u83 2705
	}
2706
 
7 7u83 2707
	/* Define the function */
2708
	if (IS_id_function_etc(id)) {
2709
		COPY_exp(id_function_etc_defn(id), body);
2710
		define_id(id);
2711
		if (!in_template_decl) {
2712
			compile_function(id, 0);
2713
			free_nspace(label_namespace);
2714
		}
2715
		if (do_dump) {
2716
			dump_undefine(id, &crt_loc, 0);
2717
		}
2 7u83 2718
	}
2719
 
7 7u83 2720
	/* Restore old function data */
2721
	POP_id(crt_func_id, func_stack.id);
2722
	POP_type(ret, func_stack.ret);
2723
	POP_int(unreached_code, func_stack.unreached);
2724
	POP_int(in_declaration, func_stack.declaration);
2725
	POP_int(have_destructor, func_stack.destructor);
2726
	POP_int(in_func_handler, func_stack.handler);
2727
	POP_stack(crt_loop_stack, func_stack.loops);
2728
	POP_nspace(label_namespace, func_stack.labels);
2729
	POP_list(all_solve_stmts, func_stack.solves);
2730
	POP_list(all_try_blocks, func_stack.tries);
2731
	POP_ulong(n, func_stack.opts);
2732
	crt_option_value(OPT_VAL_nested_class) = n;
2733
	POP_ulong(n, func_stack.opts);
2734
	crt_option_value(OPT_VAL_statement_depth) = n;
2735
	crt_func_complex = pass_complex_type(ret);
2736
	crt_func_return = ret;
2737
	block_namespace = NULL_nspace;
2738
	suppress_variable = 0;
2739
	unreached_last = 0;
2740
	return (body);
2 7u83 2741
}