Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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