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 "hashid_ops.h"
35
#include "id_ops.h"
36
#include "loc_ext.h"
37
#include "member_ops.h"
38
#include "nspace_ops.h"
39
#include "off_ops.h"
40
#include "type_ops.h"
41
#include "error.h"
42
#include "catalog.h"
43
#include "option.h"
44
#include "access.h"
45
#include "allocate.h"
46
#include "basetype.h"
47
#include "capsule.h"
48
#include "chktype.h"
49
#include "class.h"
50
#include "compile.h"
51
#include "construct.h"
52
#include "declare.h"
53
#include "derive.h"
54
#include "dump.h"
55
#include "exception.h"
56
#include "file.h"
57
#include "function.h"
58
#include "hash.h"
59
#include "identifier.h"
60
#include "initialise.h"
61
#include "instance.h"
62
#include "inttype.h"
63
#include "namespace.h"
64
#include "operator.h"
65
#include "overload.h"
66
#include "predict.h"
67
#include "preproc.h"
68
#include "redeclare.h"
69
#include "syntax.h"
70
#include "template.h"
71
#include "tokdef.h"
72
#include "token.h"
73
#include "virtual.h"
74
 
75
 
76
/*
77
    DECLARATION LOCATION
78
 
79
    This variable is used to record the location of the current declaration.
80
    This is primarily so that the location of a declaration can be set to
81
    the location of the declarator rather than the end of the corresponding
82
    initialiser.
83
*/
84
 
85
LOCATION decl_loc = NULL_loc ;
86
int is_redeclared = 0 ;
87
 
88
 
89
/*
90
    DEFAULT LINKAGE FOR INLINE FUNCTIONS AND CONST OBJECTS
91
 
92
    These variables give the default linkages for inline functions and
93
    const objects and the default cv-qualifiers for external objects.
94
*/
95
 
96
DECL_SPEC inline_linkage = dspec_static ;
97
DECL_SPEC const_linkage = dspec_static ;
98
CV_SPEC cv_extern = cv_none ;
99
 
100
 
101
/*
102
    DUMMY EMPTY DECLARATION SPECIFIER
103
 
104
    The value dspec_empty is used during the processing of declarations
105
    to mark any declaration specifier which does not contain an explicit
106
    specifier.  The value dspec_nonempty gives those declaration
107
    specifiers which constitute an explicit specifier.
108
*/
109
 
110
#define dspec_empty		dspec_pure
111
#define dspec_nonempty		dspec_keyword
112
 
113
 
114
/*
115
    COMPLETE A DECLARATION SPECIFIER
116
 
117
    This routine completes the declaration specifier given by the
118
    specifiers ds, the type t and the cv-qualifier cv.  If these are
119
    all empty then the result is marked using dspec_empty.  The special
120
    case 'extern "LANG" typedef' is checked.
121
*/
122
 
123
DECL_SPEC complete_dspec
124
    PROTO_N ( ( ds, bt, t, cv ) )
125
    PROTO_T ( DECL_SPEC ds X BASE_TYPE bt X TYPE t X CV_SPEC cv )
126
{
127
    DECL_SPEC key = ( ds & dspec_nonempty ) ;
128
    if ( key || bt || !IS_NULL_type ( t ) || cv ) {
129
	/* Have a declaration specifier */
130
	if ( ds & dspec_c ) {
131
	    /* Have a linkage specification */
132
	    if ( ds & dspec_typedef ) ds &= ~dspec_extern ;
133
	    ds &= ~dspec_c ;
134
	}
135
    } else {
136
	ds |= dspec_empty ;
137
    }
138
    return ( ds ) ;
139
}
140
 
141
 
142
/*
143
    CHECK INFERRED OBJECT TYPES
144
 
145
    This routine checks the declaration specifiers ds and the type *p for
146
    inferred types and empty specifier lists.  It is used in object,
147
    parameter and class member declarations.
148
*/
149
 
150
static DECL_SPEC check_inferred_type
151
    PROTO_N ( ( ds, p, mem ) )
152
    PROTO_T ( DECL_SPEC ds X TYPE *p X int mem )
153
{
154
    int infer ;
155
    TYPE t = *p ;
156
    int empty = 0 ;
157
    ERROR err = NULL_err ;
158
 
159
    /* Report if there are no declaration specifiers */
160
    if ( ds & dspec_empty ) {
161
	if ( mem ) {
162
	    err = ERR_class_mem_ds_empty () ;
163
	} else {
164
	    err = ERR_dcl_dcl_ds_empty () ;
165
	}
166
	ds &= ~dspec_empty ;
167
	empty = 1 ;
168
    }
169
 
170
    /* Check on inferred types */
171
    infer = is_type_inferred ( t ) ;
172
    if ( infer != INFERRED_NOT ) {
173
	ERROR err2 ;
174
	t = clean_inferred_type ( t ) ;
175
	if ( empty ) {
176
	    err2 = ERR_dcl_type_infer ( t ) ;
177
	} else {
178
	    err2 = report_inferred_type ( t, infer ) ;
179
	}
180
	err = concat_error ( err, err2 ) ;
181
	*p = t ;
182
    }
183
    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
184
    return ( ds ) ;
185
}
186
 
187
 
188
/*
189
    CHECK INFERRED FUNCTION TYPES
190
 
191
    This routine checks the declaration specifiers ds and the function
192
    type t for inferred types and empty specifier lists.  It is used in
193
    function declarations and definitions (as indicated by def).
194
*/
195
 
196
static DECL_SPEC check_func_type
197
    PROTO_N ( ( ds, t, def, chk, mem ) )
198
    PROTO_T ( DECL_SPEC ds X TYPE t X int def X int chk X int mem )
199
{
200
    int empty = 0 ;
201
    ERROR err = NULL_err ;
202
 
203
    /* Report if there are no declaration specifiers */
204
    if ( ds & dspec_empty ) {
205
	if ( mem ) {
206
	    err = ERR_class_mem_ds_empty () ;
207
	} else if ( def ) {
208
	    err = ERR_dcl_dcl_ds_func () ;
209
	} else {
210
	    err = ERR_dcl_dcl_ds_empty () ;
211
	}
212
	ds &= ~dspec_empty ;
213
	empty = 1 ;
214
    }
215
 
216
    /* Check for template types */
217
    while ( IS_type_templ ( t ) ) {
218
	ds |= dspec_template ;
219
	t = DEREF_type ( type_templ_defn ( t ) ) ;
220
    }
221
 
222
    /* Check the return type */
223
    if ( chk ) {
224
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
225
	int infer = is_type_inferred ( r ) ;
226
	if ( infer != INFERRED_NOT ) {
227
	    ERROR err2 ;
228
	    r = clean_inferred_type ( r ) ;
229
	    if ( empty ) {
230
		err2 = ERR_dcl_type_infer ( r ) ;
231
	    } else {
232
		err2 = report_inferred_type ( r, infer ) ;
233
	    }
234
	    err = concat_error ( err, err2 ) ;
235
	    COPY_type ( type_func_ret ( t ), r ) ;
236
	}
237
    }
238
    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
239
    return ( ds ) ;
240
}
241
 
242
 
243
/*
244
    SHIFT A STORAGE CLASS
245
 
246
    This macro is used to shift a storage class specifier into a more
247
    sensible range.
248
*/
249
 
250
#define DSPEC_STORAGE( A )	( ( A ) >> 6 )
251
 
252
 
253
/*
254
    CHECK STORAGE CLASS SPECIFIERS
255
 
256
    This routine extracts the storage class specifiers from the declaration
257
    specifiers ds in the context given by loc.  It returns a valid storage
258
    class specifier.
259
*/
260
 
261
static DECL_SPEC check_storage
262
    PROTO_N ( ( ds, loc, id ) )
263
    PROTO_T ( DECL_SPEC ds X int loc X IDENTIFIER id )
264
{
265
    DECL_SPEC st = ( ds & dspec_storage ) ;
266
 
267
    /* Check on storage class */
268
    switch_label : {
269
	switch ( DSPEC_STORAGE ( st ) ) {
270
 
271
	    case DSPEC_STORAGE ( dspec_none ) : {
272
		/* No storage class given */
273
		break ;
274
	    }
275
 
276
	    case DSPEC_STORAGE ( dspec_auto ) :
277
	    case DSPEC_STORAGE ( dspec_register ) : {
278
		/* Deal with auto and register */
279
		switch ( loc ) {
280
		    case CONTEXT_OBJECT : {
281
			/* Objects declared in a block are alright */
282
			if ( !in_function_defn ) goto bad_auto_lab ;
283
			break ;
284
		    }
285
		    case CONTEXT_FUNCTION : {
286
			/* Functions can't be auto */
287
			if ( !in_function_defn ) goto bad_auto_lab ;
288
			report ( crt_loc, ERR_dcl_stc_auto_func ( st ) ) ;
289
			st = dspec_none ;
290
			break ;
291
		    }
292
		    case CONTEXT_PARAMETER :
293
		    case CONTEXT_WEAK_PARAM : {
294
			/* Function parameters are alright */
295
			if ( st == dspec_auto ) {
296
			    /* Can't have auto parameters in C */
297
			    report ( crt_loc, ERR_dcl_stc_auto_par () ) ;
298
			}
299
			break ;
300
		    }
301
		    case CONTEXT_TEMPL_PARAM : {
302
			/* Template parameters can't have storage class */
303
			report ( crt_loc, ERR_temp_param_dcl_stc ( st ) ) ;
304
			st = dspec_none ;
305
			break ;
306
		    }
307
		    default :
308
		    bad_auto_lab : {
309
			/* Anything outside a block can't be auto */
310
			report ( crt_loc, ERR_dcl_stc_auto_bad ( st ) ) ;
311
			st = dspec_none ;
312
			break ;
313
		    }
314
		}
315
		break ;
316
	    }
317
 
318
	    case DSPEC_STORAGE ( dspec_static ) : {
319
		/* Deal with static */
320
		switch ( loc ) {
321
		    case CONTEXT_PARAMETER :
322
		    case CONTEXT_WEAK_PARAM : {
323
			/* Function parameters can't be static */
324
			report ( crt_loc, ERR_dcl_stc_param ( st ) ) ;
325
			st = dspec_none ;
326
			break ;
327
		    }
328
		    case CONTEXT_TEMPL_PARAM : {
329
			/* Template parameters can't have storage class */
330
			report ( crt_loc, ERR_temp_param_dcl_stc ( st ) ) ;
331
			st = dspec_none ;
332
			break ;
333
		    }
334
		}
335
		break ;
336
	    }
337
 
338
	    case DSPEC_STORAGE ( dspec_extern ) : {
339
		/* Deal with extern */
340
		switch ( loc ) {
341
		    case CONTEXT_OBJECT :
342
		    case CONTEXT_FUNCTION : {
343
			/* Objects and functions can be extern */
344
			if ( !IS_NULL_id ( id ) ) {
345
			    switch ( TAG_id ( id ) ) {
346
				case id_member_tag :
347
				case id_mem_func_tag :
348
				case id_stat_member_tag :
349
				case id_stat_mem_func_tag : {
350
				    /* But not class members */
351
				    ERROR err = ERR_dcl_stc_ext_mem () ;
352
				    report ( crt_loc, err ) ;
353
				    break ;
354
				}
355
			    }
356
			}
357
			break ;
358
		    }
359
		    case CONTEXT_PARAMETER :
360
		    case CONTEXT_WEAK_PARAM : {
361
			/* Function parameters can't be extern */
362
			report ( crt_loc, ERR_dcl_stc_param ( st ) ) ;
363
			st = dspec_none ;
364
			break ;
365
		    }
366
		    case CONTEXT_TEMPL_PARAM : {
367
			/* Template parameters can't have storage class */
368
			report ( crt_loc, ERR_temp_param_dcl_stc ( st ) ) ;
369
			st = dspec_none ;
370
			break ;
371
		    }
372
		    default : {
373
			/* Class members can't be extern */
374
			report ( crt_loc, ERR_dcl_stc_ext_mem () ) ;
375
			st = dspec_none ;
376
			break ;
377
		    }
378
		}
379
		break ;
380
	    }
381
 
382
	    case DSPEC_STORAGE ( dspec_mutable ) : {
383
		/* Deal with mutable */
384
		if ( loc != CONTEXT_MEMBER ) {
385
		    /* Only data members can be mutable */
386
		    report ( crt_loc, ERR_dcl_stc_mut_bad () ) ;
387
		    st = dspec_none ;
388
		}
389
		break ;
390
	    }
391
 
392
	    default : {
393
		/* More than one storage class - select one */
394
		DECL_SPEC nst = dspec_static ;
395
		while ( !( st & nst ) ) {
396
		    ASSERT ( nst != dspec_none ) ;
397
		    nst <<= 1 ;
398
		}
399
		report ( crt_loc, ERR_dcl_stc_dup ( st, nst ) ) ;
400
		st = nst ;
401
		goto switch_label ;
402
	    }
403
	}
404
    }
405
    return ( st ) ;
406
}
407
 
408
 
409
/*
410
    CHECK STORAGE CLASS SPECIFIERS
411
 
412
    This routine extracts the function specifiers (plus friend specifiers)
413
    from the declaration specifiers ds in the context given by loc (as
414
    above).  It returns a valid combination of function and friend
415
    specifiers.
416
*/
417
 
418
static DECL_SPEC check_func_spec
419
    PROTO_N ( ( ds, loc ) )
420
    PROTO_T ( DECL_SPEC ds X int loc )
421
{
422
    DECL_SPEC fn = dspec_none ;
423
 
424
    /* Only functions can be inline */
425
    if ( ds & dspec_inline ) {
426
	if ( loc == CONTEXT_FUNCTION || loc == CONTEXT_FUNC_MEMBER ) {
427
	    fn |= dspec_inline ;
428
	} else {
429
	    report ( crt_loc, ERR_dcl_fct_spec_inline_bad () ) ;
430
	}
431
    }
432
 
433
    /* Only function members can be virtual */
434
    if ( ds & dspec_virtual ) {
435
	if ( loc == CONTEXT_FUNC_MEMBER ) {
436
	    fn |= dspec_virtual ;
437
	} else {
438
	    report ( crt_loc, ERR_dcl_fct_spec_virtual () ) ;
439
	}
440
    }
441
 
442
    /* Only function members can be explicit */
443
    if ( ds & dspec_explicit ) {
444
	if ( loc == CONTEXT_FUNC_MEMBER ) {
445
	    fn |= dspec_explicit ;
446
	} else {
447
	    report ( crt_loc, ERR_dcl_fct_spec_explicit () ) ;
448
	}
449
    }
450
 
451
    /* Only functions declared in a class can be friends */
452
    if ( ds & dspec_friend ) {
453
	if ( loc == CONTEXT_FUNCTION && in_class_defn ) {
454
	    /* Don't add to specifier list */
455
	    /* EMPTY */
456
	} else if ( in_class_defn ) {
457
	    report ( crt_loc, ERR_class_friend_decl () ) ;
458
	} else {
459
	    report ( crt_loc, ERR_dcl_friend_class () ) ;
460
	}
461
    }
462
 
463
    /* Allow for function discarding */
464
    if ( loc == CONTEXT_FUNCTION || loc == CONTEXT_FUNC_MEMBER ) {
465
	fn |= dspec_ignore ;
466
    }
467
    return ( fn ) ;
468
}
469
 
470
 
471
/*
472
    CONSTRUCT A TYPE DECLARATION
473
 
474
    This routine constructs the declaration of a type with declaration
475
    specifiers ds (which will include typedef), type t and name id in the
476
    namespace ns.  mem gives the member of ns corresponding to id or the
477
    null member for class member redeclarations.
478
*/
479
 
480
static IDENTIFIER make_type_decl
481
    PROTO_N ( ( ns, ds, t, mem, id ) )
482
    PROTO_T ( NAMESPACE ns X DECL_SPEC ds X TYPE t X
483
	      MEMBER mem X IDENTIFIER id )
484
{
485
    int reported = 0 ;
486
    IDENTIFIER old_id ;
487
    unsigned tag = TAG_type ( t ) ;
488
    QUALIFIER cq = crt_id_qualifier ;
489
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
490
 
491
    /* Can't have other declaration specifiers with typedef */
492
    DECL_SPEC st = ( ds & dspec_keyword ) ;
493
    if ( st != dspec_typedef ) {
494
	st &= ~dspec_typedef ;
495
	report ( crt_loc, ERR_dcl_typedef_dspec ( st ) ) ;
496
    }
497
 
498
    /* Check for function cv-qualifiers and exception specifiers */
499
    object_type ( t, id_type_alias_tag ) ;
500
 
501
    /* Check for previous declaration */
502
    if ( IS_NULL_member ( mem ) ) {
503
	mem = search_member ( ns, nm, 1 ) ;
504
	old_id = DEREF_id ( member_id ( mem ) ) ;
505
	old_id = redecl_inherit ( old_id, cq, in_class_defn, 2 ) ;
506
	if ( IS_NULL_id ( old_id ) ) {
507
	    report ( crt_loc, ERR_lookup_qual_undef ( nm, ns ) ) ;
508
	}
509
    } else {
510
	old_id = DEREF_id ( member_id ( mem ) ) ;
511
	old_id = redecl_inherit ( old_id, cq, in_class_defn, 2 ) ;
512
    }
513
 
514
    /* Allow for type redeclarations */
515
#if LANGUAGE_CPP
516
    id = type_member ( mem, 3 ) ;
517
    id = redecl_inherit ( id, cq, in_class_defn, 2 ) ;
518
#else
519
    id = old_id ;
520
#endif
521
    if ( !IS_NULL_id ( id ) && IS_id_class_name_etc ( id ) ) {
522
	/* Already declared as a type name */
523
	TYPE s ;
524
	ERROR err = NULL_err ;
525
	PTR ( LOCATION ) loc = id_loc ( id ) ;
526
	unsigned long tokdefs = no_token_defns ;
527
 
528
	/* Check for reserved identifiers */
529
	DECL_SPEC ods = DEREF_dspec ( id_storage ( id ) ) ;
530
	if ( ( ds | ods ) & dspec_reserve ) {
531
	    report ( crt_loc, ERR_basic_odr_decl ( id, loc ) ) ;
532
	    return ( id ) ;
533
	}
534
 
535
	/* Check type compatibility */
536
	s = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
537
	s = check_compatible ( s, t, 0, &err, 1 ) ;
538
	/* QUERY: or is type equality required? */
539
 
540
	/* Compatible redefinition */
541
	if ( IS_NULL_err ( err ) ) {
542
	    NAMESPACE cns = crt_namespace ;
543
	    if ( cq != qual_none ) {
544
		/* Check qualified definitions */
545
		check_decl_nspace ( id, ns, 1, cns ) ;
546
	    }
547
	    if ( in_class_defn && EQ_nspace ( ns, cns ) ) {
548
		if ( !is_tagged_type ( id ) ) {
549
		    /* Still not allowed in a class definition */
550
		    report ( crt_loc, ERR_class_mem_redecl ( id, loc ) ) ;
551
		}
552
	    } else if ( tokdefs == no_token_defns && !in_pragma_dir ) {
553
		/* Can't redeclare types in C */
554
		report ( crt_loc, ERR_basic_odr_typedef ( id, loc ) ) ;
555
	    }
556
	    if ( tag == type_func_tag ) {
557
		/* Check function types */
558
		s = redecl_func_type ( id, s, t, 0, 1 ) ;
559
	    }
560
	    COPY_type ( id_class_name_etc_defn ( id ), s ) ;
561
	    adjust_access ( id, crt_access, 0 ) ;
562
	    return ( id ) ;
563
	}
564
 
565
	/* Incompatible redefinition */
566
	err = concat_error ( err, ERR_basic_link_typedef ( id, loc ) ) ;
567
	report ( crt_loc, err ) ;
568
	reported = 1 ;
569
    }
570
 
571
    /* Declare the type */
572
    id = make_typedef ( ns, nm, t, dspec_none ) ;
573
    if ( is_tagged_type ( id ) ) {
574
	/* Class-like typedef-names */
575
	set_type_member ( mem, id ) ;
576
    } else {
577
	/* Object-like typedef-names */
578
	if ( !IS_NULL_id ( old_id ) && !reported ) {
579
	    /* Already declared as an object */
580
	    PTR ( LOCATION ) loc = id_loc ( old_id ) ;
581
	    report ( crt_loc, ERR_basic_odr_diff ( old_id, loc ) ) ;
582
	}
583
	set_member ( mem, id ) ;
584
    }
585
    if ( tag == type_func_tag ) {
586
	/* Check function type */
587
	decl_func_type ( id, t, 0 ) ;
588
    } else if ( tag == type_templ_tag ) {
589
	IGNORE check_templ_params ( t, id ) ;
590
	report ( crt_loc, ERR_temp_decl_bad () ) ;
591
    }
592
    return ( id ) ;
593
}
594
 
595
 
596
/*
597
    CREATE A SPECIAL TYPE DEFINITION
598
 
599
    This routine creates a typedef of the identifier id to the special
600
    type t.
601
*/
602
 
603
void typedef_special
604
    PROTO_N ( ( id, t ) )
605
    PROTO_T ( IDENTIFIER id X TYPE t )
606
{
607
    int pushed = 0 ;
608
    NAMESPACE ns = nonblock_namespace ;
609
    if ( !EQ_nspace ( ns, crt_namespace ) ) {
610
	push_namespace ( ns ) ;
611
	pushed = 1 ;
612
    }
613
    decl_loc = preproc_loc ;
614
    if ( in_class_defn ) {
615
	id = make_member_decl ( dspec_typedef, t, id, 0 ) ;
616
    } else {
617
	id = make_object_decl ( dspec_typedef, t, id, 0 ) ;
618
    }
619
    if ( do_dump ) dump_declare ( id, &decl_loc, 1 ) ;
620
    if ( pushed ) {
621
	IGNORE pop_namespace () ;
622
    }
623
    return ;
624
}
625
 
626
 
627
/*
628
    FIND THE LINKAGE OF AN IDENTIFIER
629
 
630
    This routine finds the linkage of the identifier id (including the
631
    language specifier), returning the default value st if id does not
632
    represent an object or function.
633
*/
634
 
635
static DECL_SPEC find_storage
636
    PROTO_N ( ( id, st, t ) )
637
    PROTO_T ( IDENTIFIER id X DECL_SPEC st X TYPE t )
638
{
639
    if ( !IS_NULL_id ( id ) ) {
640
	switch ( TAG_id ( id ) ) {
641
	    case id_variable_tag :
642
	    case id_parameter_tag :
643
	    case id_stat_member_tag :
644
	    case id_weak_param_tag : {
645
		/* Objects */
646
		DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
647
		st = ( ds & ( dspec_storage | dspec_language ) ) ;
648
		break ;
649
	    }
650
	    case id_function_tag :
651
	    case id_mem_func_tag :
652
	    case id_stat_mem_func_tag : {
653
		/* Functions */
654
		DECL_SPEC ds ;
655
#if LANGUAGE_CPP
656
		LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
657
		if ( !IS_NULL_type ( t ) ) {
658
		    int eq = 0 ;
659
		    id = resolve_func ( id, t, 0, 0, pids, &eq ) ;
660
		    if ( IS_NULL_id ( id ) || !IS_id_function_etc ( id ) ) {
661
			return ( st ) ;
662
		    }
663
		}
664
#else
665
		UNUSED ( t ) ;
666
#endif
667
		ds = DEREF_dspec ( id_storage ( id ) ) ;
668
		st = ( ds & ( dspec_storage | dspec_language ) ) ;
669
		break ;
670
	    }
671
	}
672
    }
673
    return ( st ) ;
674
}
675
 
676
 
677
/*
678
    CHECK THE LOCATION OF A DECLARATION
679
 
680
    This routine checks whether the namespace cns is a suitable location
681
    for redeclaring the object id, which is a member of the namespace ns.
682
    def is true for a definition.
683
*/
684
 
685
void check_decl_nspace
686
    PROTO_N ( ( id, ns, def, cns ) )
687
    PROTO_T ( IDENTIFIER id X NAMESPACE ns X int def X NAMESPACE cns )
688
{
689
    int func = 0 ;
690
    int local_def = really_in_function_defn ;
691
    switch ( TAG_id ( id ) ) {
692
	case id_class_name_tag :
693
	case id_enum_name_tag :
694
	case id_class_alias_tag :
695
	case id_enum_alias_tag :
696
	case id_type_alias_tag : {
697
	    /* Can define local types */
698
	    local_def = 0 ;
699
	    break ;
700
	}
701
	case id_mem_func_tag :
702
	case id_stat_mem_func_tag : {
703
	    /* Member function */
704
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
705
	    if ( ds & dspec_implicit ) {
706
		/* Ignore implicit constructors etc */
707
		return ;
708
	    }
709
	    if ( !def && !is_templ_nspace ( ns ) ) {
710
		/* Can't even redeclare in this case */
711
		report ( crt_loc, ERR_class_mfct_redecl ( id ) ) ;
712
		return ;
713
	    }
714
	    func = 1 ;
715
	    break ;
716
	}
717
	case id_undef_tag : {
718
	    /* Report undeclared members */
719
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
720
	    report ( crt_loc, ERR_lookup_qual_undef ( nm, ns ) ) ;
721
	    return ;
722
	}
723
    }
724
 
725
    if ( def ) {
726
	/* Check for enclosing namespace scope */
727
	if ( local_def || !is_subnspace ( cns, ns ) ) {
728
	    /* Report badly placed definition */
729
	    ERROR err ;
730
	    if ( IS_nspace_ctype ( ns ) ) {
731
		if ( func ) {
732
		    /* Member functions */
733
		    err = ERR_class_mfct_scope ( id ) ;
734
		} else {
735
		    /* Other class members */
736
		    err = ERR_class_static_data_scope ( id ) ;
737
		}
738
	    } else {
739
		/* Namespace members */
740
		err = ERR_dcl_nspace_memdef_scope ( id ) ;
741
	    }
742
	    report ( crt_loc, err ) ;
743
	}
744
    }
745
    return ;
746
}
747
 
748
 
749
/*
750
    CHECK AN OBJECT TYPE
751
 
752
    This routine checks the type t for the object id with declaration
753
    specifiers ds.
754
*/
755
 
756
void check_obj_decl
757
    PROTO_N ( ( ds, t, id, tentative ) )
758
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int tentative )
759
{
760
    unsigned tag = TAG_type ( t ) ;
761
    if ( tag == type_top_tag || tag == type_bottom_tag ) {
762
	/* Always report void declarations */
763
	report ( crt_loc, ERR_basic_fund_void_decl ( id, t ) ) ;
764
    } else if ( tag == type_templ_tag ) {
765
	/* Shouldn't have template type */
766
	report ( crt_loc, ERR_temp_decl_bad () ) ;
767
    } else if ( ds & dspec_defn ) {
768
	/* Only check otherwise if this is a definition */
769
	if ( tag == type_array_tag ) {
770
	    /* Arrays may be completed by the initialiser */
771
	    /* EMPTY */
772
	} else if ( tag == type_ref_tag ) {
773
	    /* References don't need checking */
774
	    /* EMPTY */
775
	} else {
776
	    ERROR err ;
777
	    if ( !tentative ) {
778
		err = check_complete ( t ) ;
779
		if ( !IS_NULL_err ( err ) ) {
780
		    /* Other definitions should have complete type */
781
		    ERROR err2 = ERR_basic_types_def_incompl ( id ) ;
782
		    err = concat_error ( err, err2 ) ;
783
		    report ( crt_loc, err ) ;
784
		}
785
	    }
786
	    err = check_abstract ( t ) ;
787
	    if ( !IS_NULL_err ( err ) ) {
788
		/* Objects can't have abstract type */
789
		ERROR err2 = ERR_class_abstract_decl ( id ) ;
790
		err = concat_error ( err, err2 ) ;
791
		report ( crt_loc, err ) ;
792
	    }
793
	}
794
    }
795
    return ;
796
}
797
 
798
 
799
/*
800
    DECLARE AN OBJECT
801
 
802
    This routine constructs the declaration of an object with declaration
803
    specifiers ds, type t and name id.
804
*/
805
 
806
IDENTIFIER make_object_decl
807
    PROTO_N ( ( ds, t, id, def ) )
808
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int def )
809
{
810
    ERROR err ;
811
    MEMBER mem ;
812
    NAMESPACE ns ;
813
    int redef = 0 ;
814
    int simple_id = 1 ;
815
    int tentative = 0 ;
816
    IDENTIFIER old_id ;
817
    IDENTIFIER alt_id ;
818
    DECL_SPEC st, df, rs ;
819
    IDENTIFIER prev_id = NULL_id ;
820
    unsigned tag = TAG_type ( t ) ;
821
    unsigned itag = id_variable_tag ;
822
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
823
 
824
    /* Check for template specialisations */
825
    bound_specialise = 0 ;
826
    if ( is_templ_decl ( id, NULL_type ) || is_templ_spec ( t ) ) {
827
	t = bind_specialise ( &id, t, ds, 0, 1, def ) ;
828
	if ( IS_NULL_id ( id ) ) {
829
	    /* Invalid specialisation */
830
	    crt_id_qualifier = qual_none ;
831
	    crt_templ_qualifier = 0 ;
832
	    id = DEREF_id ( hashid_id ( nm ) ) ;
833
	    while ( IS_type_templ ( t ) ) {
834
		t = DEREF_type ( type_templ_defn ( t ) ) ;
835
	    }
836
	    id = make_object_decl ( ds, t, id, def ) ;
837
	    return ( id ) ;
838
	}
839
	ns = DEREF_nspace ( id_parent ( id ) ) ;
840
	check_decl_nspace ( id, ns, 0, crt_namespace ) ;
841
	old_id = id ;
842
	mem = NULL_member ;
843
	simple_id = 0 ;
844
 
845
    } else {
846
	/* Check on identifier name */
847
	QUALIFIER cq = crt_id_qualifier ;
848
	err = check_id_name ( id, CONTEXT_OBJECT ) ;
849
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
850
 
851
	/* Check for qualified identifiers */
852
	if ( cq == qual_none ) {
853
	    /* Declaration of simple identifier */
854
	    ns = crt_namespace ;
855
	    mem = search_member ( ns, nm, 1 ) ;
856
	    old_id = DEREF_id ( member_id ( mem ) ) ;
857
	    alt_id = DEREF_id ( member_alt ( mem ) ) ;
858
	    if ( !IS_NULL_id ( old_id ) ) {
859
		if ( in_function_defn && ( ds & dspec_extern ) ) {
860
		    /* Redeclaration of block external */
861
		    prev_id = find_previous ( t, id ) ;
862
		    if ( EQ_id ( prev_id, old_id ) ) old_id = NULL_id ;
863
		} else {
864
		    old_id = redecl_inherit ( old_id, cq, 0, 2 ) ;
865
		}
866
		redef = 1 ;
867
	    } else if ( !IS_NULL_id ( alt_id ) ) {
868
		redef = 1 ;
869
	    }
870
	} else {
871
	    /* Redeclaration of class or namespace member */
872
	    ns = DEREF_nspace ( id_parent ( id ) ) ;
873
	    check_decl_nspace ( id, ns, 0, crt_namespace ) ;
874
	    if ( IS_id_undef ( id ) ) {
875
		if ( IS_nspace_ctype ( ns ) ) itag = id_stat_member_tag ;
876
		mem = search_member ( ns, nm, 1 ) ;
877
		old_id = NULL_id ;
878
	    } else {
879
		mem = NULL_member ;
880
		old_id = redecl_inherit ( id, cq, 0, 2 ) ;
881
		t = bind_specialise ( &old_id, t, ds, 0, 0, def ) ;
882
		if ( !IS_NULL_id ( old_id ) ) {
883
		    id = old_id ;
884
		    itag = TAG_id ( id ) ;
885
		    ns = DEREF_nspace ( id_parent ( id ) ) ;
886
		}
887
	    }
888
	    simple_id = 0 ;
889
	}
890
    }
891
 
892
    /* Deal with inferred types */
893
    ds = check_inferred_type ( ds, &t, 0 ) ;
894
 
895
    /* Check on storage class specifiers */
896
    st = check_storage ( ds, CONTEXT_OBJECT, old_id ) ;
897
 
898
    /* Deal with type definitions */
899
    if ( ds & dspec_typedef ) {
900
	id = make_type_decl ( ns, ds, t, mem, id ) ;
901
	return ( id ) ;
902
    }
903
 
904
    /* Check on function specifiers */
905
    IGNORE check_func_spec ( ds, CONTEXT_OBJECT ) ;
906
 
907
    /* Find the object linkage and whether it is a definition */
908
    if ( st == dspec_extern ) {
909
	/* Explicit extern indicates a declaration (probably) */
910
	df = dspec_none ;
911
	if ( in_function_defn && simple_id ) {
912
	    if ( IS_NULL_id ( prev_id ) ) {
913
		prev_id = find_previous ( t, id ) ;
914
	    }
915
	    st = find_storage ( prev_id, st, NULL_type ) ;
916
	} else {
917
	    prev_id = old_id ;
918
	    st = find_storage ( old_id, st, t ) ;
919
	}
920
    } else {
921
	if ( tag == type_templ_tag && bound_specialise ) {
922
	    /* A template specialisation is a declaration (probably) */
923
	    df = dspec_none ;
924
	} else {
925
	    /* Everything else is a definition */
926
	    df = dspec_defn ;
927
	}
928
	if ( in_function_defn ) {
929
	    /* Objects declared in a block have no linkage */
930
	    if ( IS_NULL_member ( mem ) ) {
931
		/* Use old linkage if explicitly qualified */
932
		st = find_storage ( old_id, dspec_none, t ) ;
933
	    } else if ( st == dspec_static ) {
934
		st = dspec_none ;
935
	    } else if ( st == dspec_register ) {
936
		st = ( dspec_auto | dspec_register ) ;
937
		used_register = 1 ;
938
	    } else {
939
		st = dspec_auto ;
940
	    }
941
	} else if ( st == dspec_static ) {
942
	    /* Check static declarations */
943
	    if ( !def ) tentative = LANGUAGE_C ;
944
	} else if ( st == dspec_none ) {
945
	    /* Objects declared const have internal linkage */
946
	    CV_SPEC qual = find_cv_qual ( t ) ;
947
	    if ( qual & cv_const ) {
948
		st = const_linkage ;
949
		if ( st & dspec_static ) {
950
		    st = find_storage ( old_id, st, t ) ;
951
		}
952
	    } else {
953
		st = dspec_extern ;
954
	    }
955
	    /* find_storage is not applied for objects */
956
	    if ( !def ) tentative = LANGUAGE_C ;
957
	}
958
    }
959
 
960
    /* Create the declaration */
961
    t = lvalue_type ( t ) ;
962
    rs = ( ds & dspec_other ) ;
963
    ds = ( st | df | rs ) ;
964
    if ( !IS_NULL_id ( old_id ) ) {
965
	/* Check redeclarations */
966
	old_id = redecl_id ( ds, t, old_id, 0, 0 ) ;
967
	if ( IS_NULL_id ( old_id ) && IS_NULL_member ( mem ) ) {
968
	    /* Bad redeclaration of class or namespace member */
969
	    nm = lookup_anon () ;
970
	    mem = search_member ( ns, nm, 1 ) ;
971
	    itag = id_stat_member_tag ;
972
	}
973
    }
974
    object_type ( t, itag ) ;
975
    if ( IS_NULL_id ( old_id ) ) {
976
	/* Construct the declaration */
977
	ds = adjust_linkage ( ds, 0 ) ;
978
	MAKE_id_variable_etc ( itag, nm, ds, ns, decl_loc, t, id ) ;
979
	if ( in_function_defn ) {
980
	    if ( ds & dspec_linkage ) {
981
		/* Block function declarations */
982
		id = unify_previous ( id, t, prev_id, 0 ) ;
983
	    } else if ( redef && ( ds & dspec_auto ) ) {
984
		/* Redeclarations of local variables */
985
		mem = update_member ( ns, mem ) ;
986
	    }
987
	} else {
988
	    id = unify_subsequent ( id, t, 0 ) ;
989
	}
990
	set_member ( mem, id ) ;
991
	if ( itag == id_variable_tag && option ( OPT_decl_hide ) ) {
992
	    check_hiding ( id ) ;
993
	}
994
	is_redeclared = 0 ;
995
    } else {
996
	/* Redeclare an existing object */
997
	id = old_id ;
998
	if ( IS_id_member ( id ) ) {
999
	    t = DEREF_type ( id_member_type ( id ) ) ;
1000
	} else {
1001
	    t = DEREF_type ( id_variable_etc_type ( id ) ) ;
1002
	}
1003
	ds = DEREF_dspec ( id_storage ( id ) ) ;
1004
	is_redeclared = 1 ;
1005
    }
1006
#if LANGUAGE_CPP
1007
    if ( ds & dspec_c ) c_linkage ( id, 0 ) ;
1008
#endif
1009
    check_obj_decl ( ds, t, id, tentative ) ;
1010
    return ( id ) ;
1011
}
1012
 
1013
 
1014
/*
1015
    DECLARE A FUNCTION
1016
 
1017
    This routine constructs a function declaration for a function with
1018
    declaration specifiers ds, type t and name id.  The argument def is
1019
    true to distinguish function definitions from function declarations.
1020
*/
1021
 
1022
IDENTIFIER make_func_decl
1023
    PROTO_N ( ( ds, t, id, def ) )
1024
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int def )
1025
{
1026
    ERROR err ;
1027
    MEMBER mem ;
1028
    int ok = 1 ;
1029
    int chk = 1 ;
1030
    unsigned it ;
1031
    int simple_id = 1 ;
1032
    int main_func = 0 ;
1033
    IDENTIFIER old_id ;
1034
    NAMESPACE ns, ens ;
1035
    DECL_SPEC st, df, fn, rs ;
1036
    IDENTIFIER over_id = NULL_id ;
1037
    IDENTIFIER prev_id = NULL_id ;
1038
    unsigned itag = id_function_tag ;
1039
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1040
#if LANGUAGE_CPP
1041
    int allocator = 0 ;
1042
#endif
1043
 
1044
    /* Check for template specialisations */
1045
    if ( is_templ_decl ( id, t ) || is_templ_spec ( t ) ) {
1046
	t = bind_specialise ( &id, t, ds, 0, 1, def ) ;
1047
	if ( IS_NULL_id ( id ) ) {
1048
	    /* Invalid specialisation */
1049
	    crt_id_qualifier = qual_none ;
1050
	    crt_templ_qualifier = 0 ;
1051
	    id = DEREF_id ( hashid_id ( nm ) ) ;
1052
	    while ( IS_type_templ ( t ) ) {
1053
		t = DEREF_type ( type_templ_defn ( t ) ) ;
1054
	    }
1055
	    id = make_func_decl ( ds, t, id, def ) ;
1056
	    return ( id ) ;
1057
	}
1058
	ns = DEREF_nspace ( id_parent ( id ) ) ;
1059
	check_decl_nspace ( id, ns, def, crt_namespace ) ;
1060
	ens = ns ;
1061
	mem = NULL_member ;
1062
	old_id = id ;
1063
	simple_id = 0 ;
1064
 
1065
    } else {
1066
	/* Check on identifier name */
1067
	QUALIFIER cq = crt_id_qualifier ;
1068
	err = check_id_name ( id, CONTEXT_FUNCTION ) ;
1069
	if ( !IS_NULL_err ( err ) ) {
1070
	    report ( crt_loc, err ) ;
1071
	    ok = 0 ;
1072
	}
1073
 
1074
	/* Check for qualified identifiers */
1075
	if ( cq == qual_none ) {
1076
	    /* Declaration of simple identifier */
1077
	    ns = crt_namespace ;
1078
	    ens = nonblock_namespace ;
1079
	    mem = search_member ( ns, nm, 1 ) ;
1080
	    old_id = DEREF_id ( member_id ( mem ) ) ;
1081
	    if ( !IS_NULL_id ( old_id ) ) {
1082
		old_id = redecl_inherit ( old_id, cq, 0, 1 ) ;
1083
	    }
1084
	    if ( def && in_function_defn ) {
1085
		/* Check for nested function definitions */
1086
		report ( crt_loc, ERR_dcl_fct_def_scope () ) ;
1087
	    }
1088
	} else {
1089
	    /* Redeclaration of class or namespace member */
1090
	    ns = DEREF_nspace ( id_parent ( id ) ) ;
1091
	    check_decl_nspace ( id, ns, def, crt_namespace ) ;
1092
	    ens = ns ;
1093
	    if ( IS_id_undef ( id ) ) {
1094
		if ( IS_nspace_ctype ( ns ) ) itag = id_mem_func_tag ;
1095
		mem = search_member ( ns, nm, 1 ) ;
1096
		old_id = NULL_id ;
1097
	    } else {
1098
		mem = NULL_member ;
1099
		old_id = redecl_inherit ( id, cq, 0, 1 ) ;
1100
		t = bind_specialise ( &old_id, t, ds, 0, 0, def ) ;
1101
		if ( !IS_NULL_id ( old_id ) ) {
1102
		    id = old_id ;
1103
		    itag = TAG_id ( id ) ;
1104
		    ns = DEREF_nspace ( id_parent ( id ) ) ;
1105
		    ens = ns ;
1106
		}
1107
	    }
1108
	    simple_id = 0 ;
1109
	}
1110
    }
1111
 
1112
    /* Allow for special functions */
1113
    if ( EQ_KEYWORD ( nm, lex_main ) && IS_nspace_global ( ens ) ) {
1114
	if ( ds & dspec_typedef ) {
1115
	    /* Ignore type definition */
1116
	    /* EMPTY */
1117
	} else {
1118
	    t = check_main ( t, nm ) ;
1119
	    main_func = 1 ;
1120
	}
1121
    }
1122
    it = TAG_hashid ( nm ) ;
1123
#if LANGUAGE_CPP
1124
    switch ( it ) {
1125
	case hashid_constr_tag : {
1126
	    t = check_constr ( t, id, ns ) ;
1127
	    ds &= ~dspec_empty ;
1128
	    ds |= dspec_main ;
1129
	    break ;
1130
	}
1131
	case hashid_destr_tag : {
1132
	    t = check_destr ( t, id, ns ) ;
1133
	    ds &= ~dspec_empty ;
1134
	    ds |= dspec_main ;
1135
	    break ;
1136
	}
1137
	case hashid_op_tag : {
1138
	    int cl = IS_nspace_ctype ( ns ) ;
1139
	    t = check_operator ( t, id, cl, &allocator ) ;
1140
	    if ( !allocator ) ds |= dspec_ignore ;
1141
	    break ;
1142
	}
1143
	case hashid_conv_tag : {
1144
	    t = check_conv ( t, id ) ;
1145
	    ds &= ~dspec_empty ;
1146
	    chk = 0 ;
1147
	    break ;
1148
	}
1149
    }
1150
#endif
1151
 
1152
    /* Deal with inferred types */
1153
    ds = check_func_type ( ds, t, def, chk, 0 ) ;
1154
    if ( main_func ) ds |= dspec_main ;
1155
 
1156
    /* Handle function definitions */
1157
    df = ( def ? dspec_defn : dspec_none ) ;
1158
 
1159
    /* Check on storage class specifiers */
1160
    st = check_storage ( ds, CONTEXT_FUNCTION, old_id ) ;
1161
    if ( st == dspec_static ) {
1162
	/* Check on static functions */
1163
	if ( main_func ) {
1164
	    report ( crt_loc, ERR_basic_start_main_link ( nm, st ) ) ;
1165
	    st = dspec_extern ;
1166
	} else {
1167
	    /* Check static declarations */
1168
	    /* EMPTY */
1169
	}
1170
    } else if ( ds & dspec_inline ) {
1171
	/* Check on inline functions */
1172
	if ( main_func ) {
1173
	    fn = dspec_inline ;
1174
	    report ( crt_loc, ERR_basic_start_main_link ( nm, fn ) ) ;
1175
	    ds &= ~dspec_inline ;
1176
	} else {
1177
	    if ( st == dspec_extern && inline_linkage == dspec_static ) {
1178
		/* Inline functions can't be extern */
1179
		report ( crt_loc, ERR_dcl_stc_ext_inline () ) ;
1180
	    }
1181
	    if ( in_function_defn ) {
1182
		/* Can't declare inline functions in a block */
1183
		report ( crt_loc, ERR_dcl_fct_spec_block () ) ;
1184
		st = dspec_none ;
1185
	    }
1186
	}
1187
    }
1188
 
1189
    /* Deal with type definitions */
1190
    if ( ds & dspec_typedef ) {
1191
	/* Can only apply typedef to declarations, not definitions */
1192
	if ( !def ) {
1193
	    if ( ok ) {
1194
		/* Recheck identifier name */
1195
		err = check_id_name ( id, CONTEXT_OBJECT ) ;
1196
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1197
	    }
1198
	    id = make_type_decl ( ns, ds, t, mem, id ) ;
1199
	    return ( id ) ;
1200
	}
1201
	report ( crt_loc, ERR_dcl_typedef_func () ) ;
1202
    }
1203
 
1204
    /* Check on function specifiers */
1205
    fn = check_func_spec ( ds, CONTEXT_FUNCTION ) ;
1206
 
1207
    /* Find the function linkage */
1208
    if ( st == dspec_extern ) {
1209
	if ( in_function_defn && simple_id ) {
1210
	    prev_id = find_previous ( t, id ) ;
1211
	    st = find_storage ( prev_id, st, NULL_type ) ;
1212
	} else {
1213
	    prev_id = old_id ;
1214
	    st = find_storage ( old_id, st, t ) ;
1215
	}
1216
    } else if ( st == dspec_static ) {
1217
	if ( in_function_defn ) {
1218
	    /* Can't declare static functions in a block */
1219
	    report ( crt_loc, ERR_dcl_stc_stat_block () ) ;
1220
	    if ( simple_id ) {
1221
		prev_id = find_previous ( t, id ) ;
1222
	    } else {
1223
		prev_id = old_id ;
1224
	    }
1225
	}
1226
    } else if ( st == dspec_none ) {
1227
	/* Inline functions have internal linkage */
1228
	if ( fn & dspec_inline ) {
1229
	    st = inline_linkage ;
1230
	} else {
1231
	    st = dspec_extern ;
1232
	}
1233
	if ( in_function_defn ) {
1234
	    prev_id = find_previous ( t, id ) ;
1235
	    st = find_storage ( prev_id, st, NULL_type ) ;
1236
	} else {
1237
	    prev_id = old_id ;
1238
	    st = find_storage ( old_id, st, t ) ;
1239
	}
1240
    }
1241
 
1242
    /* Create the declaration */
1243
    t = lvalue_type ( t ) ;
1244
    rs = ( ds & dspec_other ) ;
1245
    ds = ( st | df | fn | rs ) ;
1246
    if ( !IS_NULL_id ( old_id ) ) {
1247
	/* Check redeclarations */
1248
	old_id = redecl_func ( ds, t, old_id, itag, &over_id, def ) ;
1249
	if ( IS_NULL_id ( old_id ) && IS_NULL_member ( mem ) ) {
1250
	    /* Bad redeclaration of class or namespace member */
1251
	    nm = lookup_anon () ;
1252
	    mem = search_member ( ns, nm, 1 ) ;
1253
	    if ( IS_nspace_ctype ( ns ) ) itag = id_mem_func_tag ;
1254
	    over_id = NULL_id ;
1255
	}
1256
    }
1257
    object_type ( t, itag ) ;
1258
    if ( IS_NULL_id ( old_id ) ) {
1259
	/* Declare the function */
1260
	ds = adjust_linkage ( ds, 0 ) ;
1261
	MAKE_id_function_etc ( itag, nm, ds, ns, decl_loc, t, over_id, id ) ;
1262
	if ( in_function_defn ) {
1263
	    id = unify_previous ( id, t, prev_id, def ) ;
1264
	} else {
1265
	    id = unify_subsequent ( id, t, def ) ;
1266
	}
1267
	set_member ( mem, id ) ;
1268
	decl_func_type ( id, t, def ) ;
1269
 
1270
	/* Check for conversion functions */
1271
	if ( it == hashid_conv_tag && itag != id_mem_func_tag ) {
1272
	    report ( crt_loc, ERR_class_conv_fct_mem () ) ;
1273
	}
1274
	if ( itag == id_function_tag && option ( OPT_decl_hide ) ) {
1275
	    check_hiding ( id ) ;
1276
	}
1277
	is_redeclared = 0 ;
1278
    } else {
1279
	/* Redeclare the function */
1280
	id = old_id ;
1281
	is_redeclared = 1 ;
1282
    }
1283
    ds = DEREF_dspec ( id_storage ( id ) ) ;
1284
#if LANGUAGE_CPP
1285
    if ( ds & dspec_c ) c_linkage ( id, def ) ;
1286
#endif
1287
 
1288
    /* Allow for discarded functions */
1289
    if ( !( rs & dspec_ignore ) && option ( OPT_discard_func ) ) {
1290
	ds &= ~dspec_ignore ;
1291
	COPY_dspec ( id_storage ( id ), ds ) ;
1292
    }
1293
#if LANGUAGE_CPP
1294
    if ( allocator ) recheck_allocator ( id, allocator ) ;
1295
#endif
1296
    if ( main_func ) recheck_main ( id ) ;
1297
    return ( id ) ;
1298
}
1299
 
1300
 
1301
/*
1302
    CHECK A PARAMETER DECLARATION
1303
 
1304
    This routine checks the type of the parameter id declared with type t.
1305
*/
1306
 
1307
void check_par_decl
1308
    PROTO_N ( ( t, id, loc ) )
1309
    PROTO_T ( TYPE t X IDENTIFIER id X int loc )
1310
{
1311
    unsigned tag = TAG_type ( t ) ;
1312
    if ( tag == type_compound_tag ) {
1313
	/* Parameters can't have abstract type */
1314
	ERROR err = check_abstract ( t ) ;
1315
	if ( !IS_NULL_err ( err ) ) {
1316
	    err = concat_error ( err, ERR_class_abstract_par () ) ;
1317
	    report ( crt_loc, err ) ;
1318
	}
1319
    } else if ( tag == type_templ_tag ) {
1320
	/* Shouldn't have template type */
1321
	report ( crt_loc, ERR_temp_decl_bad () ) ;
1322
    }
1323
    if ( loc == CONTEXT_WEAK_PARAM ) {
1324
	/* Check for 'void' and other types */
1325
	ERROR err = check_param_type ( id, t ) ;
1326
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1327
    }
1328
    return ;
1329
}
1330
 
1331
 
1332
/*
1333
    DECLARE A FUNCTION PARAMETER
1334
 
1335
    This routine constructs the declaration of a function parameter or
1336
    non-type template parameter (as indicated by loc) with declaration
1337
    specifiers ds, type t and name id.  Note that t is not checked - this
1338
    is only a declaration, and t may still legitimately be void, however
1339
    function and array parameters are adjusted to pointer parameters at
1340
    this stage.
1341
*/
1342
 
1343
IDENTIFIER make_param_decl
1344
    PROTO_N ( ( ds, t, id, loc ) )
1345
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int loc )
1346
{
1347
    ERROR err ;
1348
    DECL_SPEC st, rs ;
1349
    NAMESPACE ns = crt_namespace ;
1350
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1351
    MEMBER mem = search_member ( ns, nm, 1 ) ;
1352
    IDENTIFIER old_id = DEREF_id ( member_id ( mem ) ) ;
1353
 
1354
    /* Check on identifier name */
1355
    err = check_id_name ( id, loc ) ;
1356
    if ( !IS_NULL_err ( err ) ) report ( decl_loc, err ) ;
1357
 
1358
    /* Deal with inferred types */
1359
    ds = check_inferred_type ( ds, &t, 0 ) ;
1360
    func_type_defn ( 1 ) ;
1361
 
1362
    /* Check on storage class specifiers */
1363
    st = check_storage ( ds, loc, old_id ) ;
1364
    if ( st == dspec_register ) {
1365
	st = ( dspec_auto | dspec_register ) ;
1366
	used_register = 1 ;
1367
    } else {
1368
	st = dspec_auto ;
1369
    }
1370
 
1371
    /* Deal with type definitions */
1372
    if ( ds & dspec_typedef ) {
1373
	/* Can't have typedef in function parameters */
1374
	report ( decl_loc, ERR_dcl_typedef_par () ) ;
1375
    }
1376
 
1377
    /* Check on function specifiers */
1378
    IGNORE check_func_spec ( ds, loc ) ;
1379
 
1380
    /* Create the parameter declaration */
1381
    t = make_param_type ( t, loc ) ;
1382
    rs = ( ds & dspec_other ) ;
1383
    ds = ( st | rs | dspec_defn ) ;
1384
    if ( !IS_NULL_id ( old_id ) ) {
1385
	/* Check for redeclarations */
1386
	if ( loc == CONTEXT_TEMPL_PARAM ) {
1387
	    report ( decl_loc, ERR_temp_param_dup ( nm ) ) ;
1388
	    nm = lookup_anon () ;
1389
	    mem = search_member ( ns, nm, 1 ) ;
1390
	    id = DEREF_id ( hashid_id ( nm ) ) ;
1391
	} else {
1392
	    if ( loc == CONTEXT_WEAK_PARAM ) {
1393
		if ( IS_id_weak_param ( old_id ) ) {
1394
		    /* Check for order of declaration */
1395
		    MEMBER mem1 = DEREF_member ( nspace_last ( ns ) ) ;
1396
		    while ( !EQ_member ( mem1, mem ) ) {
1397
			IDENTIFIER mid = DEREF_id ( member_id ( mem1 ) ) ;
1398
			if ( !IS_NULL_id ( mid ) && IS_id_parameter ( mid ) ) {
1399
			    report ( decl_loc, ERR_dcl_fct_par_order () ) ;
1400
			    break ;
1401
			}
1402
			mem1 = DEREF_member ( member_next ( mem1 ) ) ;
1403
		    }
1404
		    old_id = NULL_id ;
1405
		} else if ( !IS_id_parameter ( old_id ) ) {
1406
		    report ( decl_loc, ERR_dcl_fct_par_undecl ( nm ) ) ;
1407
		}
1408
	    }
1409
	    if ( !IS_NULL_id ( old_id ) ) {
1410
		if ( IS_id_parameter ( old_id ) ) {
1411
		    /* Make up new name for parameter */
1412
		    nm = lookup_anon () ;
1413
		    mem = search_member ( ns, nm, 1 ) ;
1414
		}
1415
		IGNORE redecl_id ( ds, t, old_id, 0, 0 ) ;
1416
	    }
1417
	}
1418
    } else {
1419
	if ( loc == CONTEXT_WEAK_PARAM ) {
1420
	    report ( decl_loc, ERR_dcl_fct_par_undecl ( nm ) ) ;
1421
	}
1422
    }
1423
    ds = adjust_linkage ( ds, 0 ) ;
1424
    if ( loc == CONTEXT_TEMPL_PARAM ) {
1425
	IDENTIFIER pid ;
1426
	object_type ( t, id_token_tag ) ;
1427
	id = make_exp_param ( t, id ) ;
1428
	pid = DEREF_id ( id_token_alt ( id ) ) ;
1429
	set_member ( mem, pid ) ;
1430
    } else {
1431
	t = lvalue_type ( t ) ;
1432
	object_type ( t, id_parameter_tag ) ;
1433
	MAKE_id_parameter ( nm, ds, ns, decl_loc, t, id ) ;
1434
	set_member ( mem, id ) ;
1435
    }
1436
    check_par_decl ( t, id, loc ) ;
1437
    is_redeclared = 0 ;
1438
    return ( id ) ;
1439
}
1440
 
1441
 
1442
/*
1443
    DECLARE A NON-PROTOTYPE FUNCTION PARAMETER
1444
 
1445
    This routine is used to declare a non-prototype function parameter.
1446
*/
1447
 
1448
IDENTIFIER weak_param_decl
1449
    PROTO_N ( ( id ) )
1450
    PROTO_T ( IDENTIFIER id )
1451
{
1452
    NAMESPACE ns = crt_namespace ;
1453
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1454
    MEMBER mem = search_member ( ns, nm, 1 ) ;
1455
    IDENTIFIER old_id = DEREF_id ( member_id ( mem ) ) ;
1456
    if ( !IS_NULL_id ( old_id ) ) {
1457
	nm = lookup_anon () ;
1458
	mem = search_member ( ns, nm, 1 ) ;
1459
	IGNORE redecl_id ( dspec_none, NULL_type, old_id, 0, 0 ) ;
1460
    }
1461
    MAKE_id_weak_param ( nm, dspec_none, ns, decl_loc, id ) ;
1462
    set_member ( mem, id ) ;
1463
    is_redeclared = 0 ;
1464
    return ( id ) ;
1465
}
1466
 
1467
 
1468
/*
1469
    CHECK A MEMBER DECLARATION
1470
 
1471
    This routine checks the declaration of the class member id with type
1472
    t and declaration specifiers ds.
1473
*/
1474
 
1475
void check_mem_decl
1476
    PROTO_N ( ( ds, t, id ) )
1477
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id )
1478
{
1479
    unsigned tag = TAG_type ( t ) ;
1480
    if ( ds & dspec_mutable ) {
1481
	/* Can't apply mutable to a const member */
1482
	CV_SPEC qual = find_cv_qual ( t ) ;
1483
	if ( qual & cv_const ) {
1484
	    report ( crt_loc, ERR_dcl_stc_mut_const ( id ) ) ;
1485
	    if ( IS_id_member ( id ) ) {
1486
		qual &= ~cv_const ;
1487
		t = qualify_type ( t, qual, 0 ) ;
1488
		COPY_type ( id_member_type ( id ), t ) ;
1489
	    }
1490
	}
1491
    }
1492
    if ( tag == type_top_tag || tag == type_bottom_tag ) {
1493
	/* Always report void members */
1494
	report ( crt_loc, ERR_basic_fund_void_mem ( id, t ) ) ;
1495
    } else if ( tag == type_templ_tag ) {
1496
	/* Shouldn't have template type */
1497
	report ( crt_loc, ERR_temp_decl_bad () ) ;
1498
    } else if ( ds & dspec_defn ) {
1499
	/* Only check otherwise for defined (non-static) members */
1500
	if ( tag == type_ref_tag ) {
1501
	    /* References don't need checking */
1502
	    /* EMPTY */
1503
	} else {
1504
	    /* Other members should have complete type */
1505
	    ERROR err = check_complete ( t ) ;
1506
	    if ( !IS_NULL_err ( err ) ) {
1507
		ERROR err2 = ERR_class_mem_incompl_mem ( id ) ;
1508
		err = concat_error ( err, err2 ) ;
1509
		report ( crt_loc, err ) ;
1510
	    } else {
1511
		/* Members can't have abstract type */
1512
		err = check_abstract ( t ) ;
1513
		if ( !IS_NULL_err ( err ) ) {
1514
		    ERROR err2 = ERR_class_abstract_mem ( id ) ;
1515
		    err = concat_error ( err, err2 ) ;
1516
		    report ( crt_loc, err ) ;
1517
		}
1518
	    }
1519
	}
1520
    }
1521
    return ;
1522
}
1523
 
1524
 
1525
/*
1526
    DECLARE A CLASS MEMBER
1527
 
1528
    This routine constructs the declaration of a class member with
1529
    declaration specifiers ds, type t and name id.  Note that the access
1530
    declarations (i.e. just a qualified-id and a semicolon) can only be
1531
    spotted at this stage.  The argument sm is true if this is the first
1532
    declarator in a list and the next token is a semicolon.
1533
*/
1534
 
1535
IDENTIFIER make_member_decl
1536
    PROTO_N ( ( ds, t, id, sm ) )
1537
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int sm )
1538
{
1539
    ERROR err ;
1540
    HASHID nm ;
1541
    MEMBER mem ;
1542
    int redef = 0 ;
1543
    int tokenised = 0 ;
1544
    IDENTIFIER old_id ;
1545
    IDENTIFIER alt_id ;
1546
    DECL_SPEC st, df, rs ;
1547
    OFFSET off = NULL_off ;
1548
    CLASS_TYPE ct = crt_class ;
1549
    IDENTIFIER tok_id = NULL_id ;
1550
    NAMESPACE ns = crt_namespace ;
1551
    unsigned tag = TAG_type ( t ) ;
1552
    QUALIFIER cq = crt_id_qualifier ;
1553
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1554
 
1555
    /* Check for template specialisations */
1556
    if ( is_templ_decl ( id, NULL_type ) ) {
1557
	IGNORE bind_specialise ( &id, t, dspec_extern, 0, 1, 0 ) ;
1558
	if ( !IS_NULL_id ( id ) ) {
1559
	    report ( crt_loc, ERR_temp_spec_member ( id ) ) ;
1560
	}
1561
	return ( id ) ;
1562
    }
1563
 
1564
    /* Find previous declaration */
1565
    nm = DEREF_hashid ( id_name ( id ) ) ;
1566
    if ( IS_hashid_constr ( nm ) && cq == qual_none ) {
1567
	DECL_SPEC sds = ( dspec_static | dspec_typedef | dspec_reserve ) ;
1568
	if ( !( ds & sds ) ) {
1569
	    /* Can use class name for non-static member */
1570
	    report ( crt_loc, ERR_class_mem_ctor_data ( id ) ) ;
1571
	    id = DEREF_id ( hashid_constr_tid ( nm ) ) ;
1572
	    nm = DEREF_hashid ( id_name ( id ) ) ;
1573
	}
1574
    }
1575
    mem = search_member ( ns, nm, 1 ) ;
1576
    old_id = DEREF_id ( member_id ( mem ) ) ;
1577
    alt_id = DEREF_id ( member_alt ( mem ) ) ;
1578
    if ( !IS_NULL_id ( old_id ) ) {
1579
	old_id = redecl_inherit ( old_id, cq, 1, 2 ) ;
1580
    }
1581
 
1582
    /* Check on member qualifications */
1583
    if ( cq != qual_none ) {
1584
	if ( ( ds & dspec_empty ) && sm ) {
1585
	    /* Spot access declarations */
1586
	    id = access_decl ( id ) ;
1587
	    return ( id ) ;
1588
	}
1589
	ns = DEREF_nspace ( id_parent ( id ) ) ;
1590
	if ( EQ_nspace ( ns, crt_namespace ) ) {
1591
	    /* Qualifier indicates the current class */
1592
	    report ( crt_loc, ERR_dcl_meaning_mem ( cq, id ) ) ;
1593
	    crt_id_qualifier = qual_none ;
1594
	} else {
1595
	    /* Qualifier indicates some other namespace */
1596
	    id = make_object_decl ( ds, t, id, 0 ) ;
1597
	    return ( id ) ;
1598
	}
1599
    }
1600
 
1601
    /* Check on identifier name */
1602
    err = check_id_name ( id, CONTEXT_MEMBER ) ;
1603
    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1604
 
1605
    /* Deal with inferred types */
1606
    ds = check_inferred_type ( ds, &t, 1 ) ;
1607
 
1608
    /* Check on storage class specifiers */
1609
    st = check_storage ( ds, CONTEXT_MEMBER, old_id ) ;
1610
    if ( st == dspec_static ) {
1611
	if ( tag == type_bitfield_tag ) {
1612
	    /* Bitfield members can't be static */
1613
	    report ( crt_loc, ERR_class_bit_static () ) ;
1614
	    st = dspec_none ;
1615
	    df = dspec_defn ;
1616
	} else {
1617
	    if ( ci & cinfo_union ) {
1618
		/* Unions can't have static data members */
1619
		report ( crt_loc, ERR_class_union_static ( ct ) ) ;
1620
		st = dspec_none ;
1621
		df = dspec_defn ;
1622
	    } else {
1623
		/* Static members are only declared */
1624
		df = dspec_none ;
1625
	    }
1626
	}
1627
    } else {
1628
	/* Other members are defined */
1629
	df = dspec_defn ;
1630
    }
1631
 
1632
    /* Deal with type definitions */
1633
    if ( ds & dspec_typedef ) {
1634
	LIST ( IDENTIFIER ) ft = DEREF_list ( ctype_nest ( ct ) ) ;
1635
	if ( tag == type_bitfield_tag ) {
1636
	    report ( crt_loc, ERR_class_bit_typedef () ) ;
1637
	    t = find_bitfield_type ( t ) ;
1638
	}
1639
	id = make_type_decl ( ns, ds, t, mem, id ) ;
1640
	CONS_id ( id, ft, ft ) ;
1641
	COPY_list ( ctype_nest ( ct ), ft ) ;
1642
	return ( id ) ;
1643
    }
1644
 
1645
    /* Check on function specifiers */
1646
    IGNORE check_func_spec ( ds, CONTEXT_MEMBER ) ;
1647
 
1648
    /* Record class properties */
1649
    rs = ( ds & dspec_other ) ;
1650
    if ( rs & dspec_token ) tokenised = 1 ;
1651
    if ( st == dspec_static ) {
1652
	ds = ( df | rs | crt_access ) ;
1653
    } else {
1654
	ds = ( st | df | rs | crt_access ) ;
1655
    }
1656
 
1657
    /* Check for redeclarations */
1658
    t = lvalue_type ( t ) ;
1659
    if ( !IS_NULL_id ( old_id ) ) {
1660
	if ( st != dspec_static && IS_id_member ( old_id ) ) {
1661
	    /* Allow for token definitions */
1662
	    if ( tokenised ) {
1663
		/* Token with previous definition */
1664
		off = DEREF_off ( id_member_off ( old_id ) ) ;
1665
		t = DEREF_type ( id_member_type ( old_id ) ) ;
1666
		tokenised = 3 ;
1667
	    } else {
1668
		tok_id = find_token ( old_id ) ;
1669
		if ( IS_id_token ( tok_id ) ) {
1670
		    /* Member was previously tokenised */
1671
		    DECL_SPEC tds = DEREF_dspec ( id_storage ( tok_id ) ) ;
1672
		    if ( !( tds & ( dspec_pure | dspec_auto ) ) ) {
1673
			/* Token can't be defined */
1674
			tokenised = 2 ;
1675
		    }
1676
		}
1677
	    }
1678
	}
1679
	if ( tokenised == 0 ) {
1680
	    /* Redeclare if neither is a token */
1681
	    old_id = redecl_id ( ds, t, old_id, 0, 0 ) ;
1682
	    if ( IS_NULL_id ( old_id ) ) redef = 1 ;
1683
	}
1684
    } else if ( !IS_NULL_id ( alt_id ) ) {
1685
	redef = 1 ;
1686
    }
1687
 
1688
    /* Create the declaration */
1689
    ds = adjust_linkage ( ds, 1 ) ;
1690
    if ( !really_in_function_defn ) ds |= dspec_extern ;
1691
    if ( st == dspec_static ) {
1692
	object_type ( t, id_stat_member_tag ) ;
1693
	MAKE_id_stat_member ( nm, ds, ns, decl_loc, t, id ) ;
1694
    } else {
1695
	object_type ( t, id_member_tag ) ;
1696
	MAKE_id_member ( nm, ds, ns, decl_loc, t, id ) ;
1697
	if ( tokenised == 0 || tokenised == 2 ) {
1698
	    /* Construct member offset if not tokenised */
1699
	    MAKE_off_member ( id, off ) ;
1700
	}
1701
	COPY_off ( id_member_off ( id ), off ) ;
1702
	if ( redef ) mem = update_member ( ns, mem ) ;
1703
    }
1704
 
1705
    /* Set the namespace member */
1706
    if ( tokenised >= 2 ) {
1707
	/* Create dummy member */
1708
	MEMBER mem_old = DEREF_member ( nspace_last ( ns ) ) ;
1709
	if ( tokenised == 2 ) {
1710
	    /* Define existing token */
1711
	    IGNORE define_mem_token ( tok_id, off, t, 1 ) ;
1712
	} else {
1713
	    /* Token is defined later ... */
1714
	    /* EMPTY */
1715
	}
1716
	MAKE_member_large ( NULL_member, NULL_member, mem ) ;
1717
	COPY_id ( member_id ( mem ), id ) ;
1718
	COPY_member ( nspace_last ( ns ), mem ) ;
1719
	COPY_member ( member_next ( mem_old ), mem ) ;
1720
    }
1721
    set_member ( mem, id ) ;
1722
    check_mem_decl ( ds, t, id ) ;
1723
    is_redeclared = 0 ;
1724
 
1725
    /* Adjust class information */
1726
    if ( st == dspec_static ) {
1727
	if ( really_in_function_defn ) {
1728
	    /* Can't have static members in local classes */
1729
	    report ( crt_loc, ERR_class_local_static ( id ) ) ;
1730
	}
1731
	ci |= cinfo_static ;
1732
    } else {
1733
	/* Check member types */
1734
	if ( crt_access != dspec_public ) ci |= cinfo_private ;
1735
	ci = check_member_type ( ct, ci, t, 0 ) ;
1736
	ci &= ~cinfo_empty ;
1737
    }
1738
    COPY_cinfo ( ctype_info ( ct ), ci ) ;
1739
    return ( id ) ;
1740
}
1741
 
1742
 
1743
/*
1744
    MAINTAIN SPECIAL MEMBER FUNCTIONS FOR A CLASS
1745
 
1746
    This routine updates the special member function information for
1747
    the class ct using the function id.  tag gives the identifier name tag.
1748
*/
1749
 
1750
void special_func_mem
1751
    PROTO_N ( ( ct, id, tag, prev ) )
1752
    PROTO_T ( CLASS_TYPE ct X IDENTIFIER id X unsigned tag X IDENTIFIER prev )
1753
{
1754
    switch ( tag ) {
1755
	case hashid_constr_tag : {
1756
	    /* Set constructor */
1757
	    COPY_id ( ctype_constr ( ct ), id ) ;
1758
	    break ;
1759
	}
1760
	case hashid_destr_tag : {
1761
	    /* Set destructor */
1762
	    COPY_id ( ctype_destr ( ct ), id ) ;
1763
	    break ;
1764
	}
1765
	case hashid_conv_tag : {
1766
	    /* Maintain list of conversion functions */
1767
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1768
	    LIST ( IDENTIFIER ) conv = DEREF_list ( ctype_conv ( ct ) ) ;
1769
	    LIST ( IDENTIFIER ) p = conv ;
1770
	    while ( !IS_NULL_list ( p ) ) {
1771
		IDENTIFIER pid = DEREF_id ( HEAD_list ( p ) ) ;
1772
		HASHID pnm = DEREF_hashid ( id_name ( pid ) ) ;
1773
		if ( EQ_hashid ( nm, pnm ) ) {
1774
		    /* Already member of list */
1775
		    IDENTIFIER over ;
1776
		    over = DEREF_id ( id_function_etc_over ( id ) ) ;
1777
		    if ( !EQ_id ( pid, over ) && !EQ_id ( pid, prev ) ) {
1778
			/* Template function has produced duplicate */
1779
			PTR ( LOCATION ) ploc = id_loc ( pid ) ;
1780
			report ( crt_loc, ERR_class_mem_redecl ( id, ploc ) ) ;
1781
		    }
1782
		    COPY_id ( HEAD_list ( p ), id ) ;
1783
		    return ;
1784
		}
1785
		p = TAIL_list ( p ) ;
1786
	    }
1787
	    CONS_id ( id, conv, conv ) ;
1788
	    COPY_list ( ctype_conv ( ct ), conv ) ;
1789
	    break ;
1790
	}
1791
    }
1792
    return ;
1793
}
1794
 
1795
 
1796
/*
1797
    DECLARE A CLASS FUNCTION MEMBER
1798
 
1799
    This routine constructs a function declaration for a class member
1800
    function with declaration specifiers ds, type t and name id.  The
1801
    argument def is true to distinguish function definitions from function
1802
    declarations.
1803
*/
1804
 
1805
#if LANGUAGE_CPP
1806
 
1807
IDENTIFIER make_func_mem_decl
1808
    PROTO_N ( ( ds, t, id, def ) )
1809
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int def )
1810
{
1811
    ERROR err ;
1812
    MEMBER mem ;
1813
    int ok = 1 ;
1814
    int chk = 1 ;
1815
    unsigned it ;
1816
    unsigned itag ;
1817
    int allocator = 0 ;
1818
    IDENTIFIER old_id ;
1819
    LIST ( VIRTUAL ) vt ;
1820
    DECL_SPEC st, df, fn, rs ;
1821
    IDENTIFIER over_id = NULL_id ;
1822
    IDENTIFIER hide_id = NULL_id ;
1823
    NAMESPACE ns = crt_namespace ;
1824
    QUALIFIER cq = crt_id_qualifier ;
1825
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1826
    CLASS_TYPE ct = crt_class ;
1827
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1828
 
1829
    /* Check for template specialisations */
1830
    if ( is_templ_decl ( id, t ) || is_templ_spec ( t ) ) {
1831
	st = ( dspec_extern | ( ds & dspec_inline ) ) ;
1832
	IGNORE bind_specialise ( &id, t, st, 0, 1, def ) ;
1833
	if ( !IS_NULL_id ( id ) ) {
1834
	    report ( crt_loc, ERR_temp_spec_member ( id ) ) ;
1835
	}
1836
	return ( id ) ;
1837
    }
1838
 
1839
    /* Find previous declaration */
1840
    mem = search_member ( ns, nm, 1 ) ;
1841
    old_id = DEREF_id ( member_id ( mem ) ) ;
1842
    if ( !IS_NULL_id ( old_id ) ) {
1843
	old_id = redecl_inherit ( old_id, cq, 1, 1 ) ;
1844
    }
1845
 
1846
    /* Check on member qualifications */
1847
    if ( cq != qual_none ) {
1848
	ns = DEREF_nspace ( id_parent ( id ) ) ;
1849
	if ( EQ_nspace ( ns, crt_namespace ) ) {
1850
	    /* Qualifier indicates the current class */
1851
	    report ( crt_loc, ERR_dcl_meaning_mem ( cq, id ) ) ;
1852
	    crt_id_qualifier = qual_none ;
1853
	} else {
1854
	    /* Qualifier indicates some other namespace */
1855
	    id = make_func_decl ( ds, t, id, def ) ;
1856
	    return ( id ) ;
1857
	}
1858
    }
1859
 
1860
    /* Check on identifier name */
1861
    err = check_id_name ( id, CONTEXT_FUNC_MEMBER ) ;
1862
    if ( !IS_NULL_err ( err ) ) {
1863
	report ( crt_loc, err ) ;
1864
	ok = 0 ;
1865
    }
1866
 
1867
    /* Allow for special functions */
1868
    it = TAG_hashid ( nm ) ;
1869
    switch ( it ) {
1870
	case hashid_constr_tag : {
1871
	    t = check_constr ( t, id, ns ) ;
1872
	    ds &= ~dspec_empty ;
1873
	    ds |= dspec_main ;
1874
	    break ;
1875
	}
1876
	case hashid_destr_tag : {
1877
	    t = check_destr ( t, id, ns ) ;
1878
	    ds &= ~dspec_empty ;
1879
	    ds |= dspec_main ;
1880
	    break ;
1881
	}
1882
	case hashid_op_tag : {
1883
	    t = check_operator ( t, id, 1, &allocator ) ;
1884
	    if ( !allocator ) ds |= dspec_ignore ;
1885
	    break ;
1886
	}
1887
	case hashid_conv_tag : {
1888
	    t = check_conv ( t, id ) ;
1889
	    ds &= ~dspec_empty ;
1890
	    chk = 0 ;
1891
	    break ;
1892
	}
1893
    }
1894
 
1895
    /* Deal with inferred types */
1896
    ds = check_func_type ( ds, t, def, chk, 1 ) ;
1897
 
1898
    /* Handle function definitions */
1899
    if ( def ) {
1900
	/* Functions defined in a class are inline */
1901
	df = dspec_defn ;
1902
	ds |= dspec_inline ;
1903
    } else {
1904
	df = dspec_none ;
1905
    }
1906
 
1907
    /* Check on storage class specifiers */
1908
    st = check_storage ( ds, CONTEXT_FUNC_MEMBER, old_id ) ;
1909
 
1910
    /* Deal with type definitions */
1911
    if ( ds & dspec_typedef ) {
1912
	/* Can only apply typedef to declarations, not definitions */
1913
	if ( !def ) {
1914
	    if ( ok ) {
1915
		/* Recheck identifier name */
1916
		err = check_id_name ( id, CONTEXT_MEMBER ) ;
1917
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1918
	    }
1919
	    id = make_type_decl ( ns, ds, t, mem, id ) ;
1920
	    return ( id ) ;
1921
	}
1922
	report ( crt_loc, ERR_dcl_typedef_func () ) ;
1923
    }
1924
 
1925
    /* Check special functions */
1926
    if ( allocator ) {
1927
	/* Allocator functions are implicitly static */
1928
	if ( st != dspec_static ) {
1929
	    st = dspec_static ;
1930
	    allocator |= 0x4 ;
1931
	}
1932
    } else if ( st == dspec_static ) {
1933
	switch ( it ) {
1934
	    case hashid_constr_tag : {
1935
		/* Constructors can't be static */
1936
		report ( crt_loc, ERR_class_ctor_static () ) ;
1937
		st = dspec_none ;
1938
		break ;
1939
	    }
1940
	    case hashid_destr_tag : {
1941
		/* Destructors can't be static */
1942
		report ( crt_loc, ERR_class_dtor_static () ) ;
1943
		st = dspec_none ;
1944
		break ;
1945
	    }
1946
	    case hashid_op_tag : {
1947
		/* Overloaded operators can't be static */
1948
		report ( crt_loc, ERR_over_oper_static () ) ;
1949
		st = dspec_none ;
1950
		break ;
1951
	    }
1952
	    case hashid_conv_tag : {
1953
		/* Conversion functions can't be static */
1954
		report ( crt_loc, ERR_class_conv_fct_mem () ) ;
1955
		st = dspec_none ;
1956
		break ;
1957
	    }
1958
	}
1959
    }
1960
 
1961
    /* Check on function specifiers */
1962
    fn = check_func_spec ( ds, CONTEXT_FUNC_MEMBER ) ;
1963
    vt = overrides_virtual ( ct, nm, t, &hide_id ) ;
1964
    if ( !IS_NULL_list ( vt ) ) {
1965
	/* Check for overriding of virtual functions */
1966
	if ( !( fn & dspec_virtual ) ) {
1967
	    if ( !( ds & dspec_implicit ) ) {
1968
		err = ERR_class_virtual_override ( nm ) ;
1969
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1970
	    }
1971
	    fn |= dspec_virtual ;
1972
	}
1973
    } else if ( !IS_NULL_id ( hide_id ) ) {
1974
	err = ERR_class_virtual_hide ( nm, hide_id ) ;
1975
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1976
    }
1977
    if ( fn & dspec_virtual ) {
1978
	if ( st == dspec_static ) {
1979
	    /* Static members can't be virtual */
1980
	    err = ERR_class_static_mfct_virt () ;
1981
	    if ( allocator & 0x4 ) {
1982
		/* Allocator functions are implicitly static */
1983
		err = concat_error ( ERR_class_free_static ( nm ), err ) ;
1984
	    }
1985
	    report ( crt_loc, err ) ;
1986
	    fn &= ~dspec_virtual ;
1987
	} else if ( it == hashid_constr_tag ) {
1988
	    /* Constructors can't be virtual */
1989
	    report ( crt_loc, ERR_class_ctor_virtual () ) ;
1990
	    fn &= ~dspec_virtual ;
1991
	} else {
1992
	    if ( ci & cinfo_union ) {
1993
		/* Unions can't have virtual functions */
1994
		report ( crt_loc, ERR_class_union_virtual ( ct ) ) ;
1995
		fn &= ~dspec_virtual ;
1996
	    } else if ( ds & dspec_template ) {
1997
		/* Can't have a virtual template function */
1998
		report ( crt_loc, ERR_temp_mem_virtual () ) ;
1999
		fn &= ~dspec_virtual ;
2000
	    } else {
2001
		/* The current class is polymorphic */
2002
		ci |= cinfo_polymorphic ;
2003
		ci &= ~cinfo_empty ;
2004
	    }
2005
	}
2006
    }
2007
    if ( fn & dspec_explicit ) {
2008
	/* Only constructors can be explicit */
2009
	if ( it == hashid_constr_tag ) {
2010
	    have_constr_expl = 1 ;
2011
	} else {
2012
	    if ( it == hashid_conv_tag ) {
2013
		report ( crt_loc, ERR_dcl_fct_spec_expl_conv () ) ;
2014
		have_conv_expl = 1 ;
2015
	    } else {
2016
		report ( crt_loc, ERR_dcl_fct_spec_expl_constr () ) ;
2017
		fn &= ~dspec_explicit ;
2018
	    }
2019
	}
2020
    }
2021
    if ( ds & dspec_template ) {
2022
	/* Check for template function members */
2023
	if ( it == hashid_destr_tag ) {
2024
	    report ( crt_loc, ERR_temp_mem_destr () ) ;
2025
	}
2026
    }
2027
 
2028
    /* Record class properties */
2029
    rs = ( ds & dspec_other ) ;
2030
    if ( st == dspec_static ) {
2031
	itag = id_stat_mem_func_tag ;
2032
	ds = ( df | fn | rs | crt_access ) ;
2033
    } else {
2034
	itag = id_mem_func_tag ;
2035
	ds = ( st | df | fn | rs | crt_access ) ;
2036
    }
2037
    if ( !( ds & dspec_implicit ) ) ci |= cinfo_function ;
2038
    COPY_cinfo ( ctype_info ( ct ), ci ) ;
2039
 
2040
    /* Create the function declaration */
2041
    t = lvalue_type ( t ) ;
2042
    if ( !IS_NULL_id ( old_id ) ) {
2043
	/* Check for redeclarations */
2044
	IGNORE redecl_func ( ds, t, old_id, itag, &over_id, def ) ;
2045
    }
2046
    object_type ( t, itag ) ;
2047
    ds = adjust_linkage ( ds, 1 ) ;
2048
    if ( !really_in_function_defn ) ds |= dspec_extern ;
2049
    MAKE_id_function_etc ( itag, nm, ds, ns, decl_loc, t, over_id, id ) ;
2050
    if ( !IS_NULL_id ( over_id ) ) {
2051
	id = hide_functions ( id, over_id, 1 ) ;
2052
    }
2053
    set_member ( mem, id ) ;
2054
    decl_func_type ( id, t, def ) ;
2055
    is_redeclared = 0 ;
2056
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2057
 
2058
    /* Maintain lists of functions */
2059
    special_func_mem ( ct, id, it, old_id ) ;
2060
    if ( def ) {
2061
	LIST ( IDENTIFIER ) ft = DEREF_list ( ctype_nest ( ct ) ) ;
2062
	CONS_id ( id, ft, ft ) ;
2063
	COPY_list ( ctype_nest ( ct ), ft ) ;
2064
    }
2065
    if ( ds & dspec_virtual ) {
2066
	add_virtual ( ct, id, vt ) ;
2067
    }
2068
    if ( allocator ) {
2069
	allocator &= 0x3 ;
2070
	recheck_allocator ( id, allocator ) ;
2071
    }
2072
 
2073
    /* Allow for discarded functions */
2074
    if ( !( rs & dspec_ignore ) && option ( OPT_discard_func ) ) {
2075
	ds &= ~dspec_ignore ;
2076
	COPY_dspec ( id_storage ( id ), ds ) ;
2077
    }
2078
    return ( id ) ;
2079
}
2080
 
2081
#endif
2082
 
2083
 
2084
/*
2085
    DECLARE A FRIEND FUNCTION
2086
 
2087
    This routine is used to handle the declaration of a friend function
2088
    within a class.  The parameters are identical to those in the previous
2089
    routine.
2090
*/
2091
 
2092
#if LANGUAGE_CPP
2093
 
2094
IDENTIFIER make_friend_decl
2095
    PROTO_N ( ( ds, t, id, def, chum ) )
2096
    PROTO_T ( DECL_SPEC ds X TYPE t X IDENTIFIER id X int def X int chum )
2097
{
2098
    ERROR err ;
2099
    MEMBER mem ;
2100
    int chk = 1 ;
2101
    unsigned it ;
2102
    int allocator = 0 ;
2103
    int main_func = 0 ;
2104
    IDENTIFIER old_id ;
2105
    NAMESPACE ns, ens ;
2106
    DECL_SPEC st, df, fn, rs ;
2107
    IDENTIFIER over_id = NULL_id ;
2108
    unsigned itag = id_function_tag ;
2109
    QUALIFIER cq = crt_id_qualifier ;
2110
    int td = crt_templ_qualifier ;
2111
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2112
 
2113
    /* Check for template specialisations */
2114
    if ( in_template_decl && cq == qual_none && td == 0 && chum ) {
2115
	TYPE s = injected_type ( t, 1 ) ;
2116
	if ( !EQ_type ( s, t ) ) {
2117
	    /* Friend declaration is implicitly a template */
2118
	    int eq = 0 ;
2119
	    IDENTIFIER tid = make_friend_decl ( ds, s, id, 0, 0 ) ;
2120
	    if ( do_dump ) dump_declare ( tid, &decl_loc, 0 ) ;
2121
	    id = deduce_func ( tid, t, &eq ) ;
2122
	    if ( IS_NULL_id ( id ) ) return ( tid ) ;
2123
	    crt_templ_qualifier = 1 ;
2124
	}
2125
    }
2126
    if ( is_templ_decl ( id, t ) || is_templ_spec ( t ) ) {
2127
	t = bind_specialise ( &id, t, ds, 0, 1, def ) ;
2128
	if ( IS_NULL_id ( id ) ) {
2129
	    /* Invalid specialisation */
2130
	    crt_id_qualifier = qual_none ;
2131
	    crt_templ_qualifier = 0 ;
2132
	    id = DEREF_id ( hashid_id ( nm ) ) ;
2133
	    while ( IS_type_templ ( t ) ) {
2134
		t = DEREF_type ( type_templ_defn ( t ) ) ;
2135
	    }
2136
	    id = make_friend_decl ( ds, t, id, def, chum ) ;
2137
	    return ( id ) ;
2138
	}
2139
	ns = DEREF_nspace ( id_parent ( id ) ) ;
2140
	ens = ns ;
2141
	mem = NULL_member ;
2142
	old_id = id ;
2143
	if ( def ) {
2144
	    /* Check enclosing namespace scope */
2145
	    check_decl_nspace ( id, ns, def, nonclass_namespace ) ;
2146
	}
2147
 
2148
    } else {
2149
	/* Deal with typedef immediately */
2150
	if ( ( ds & dspec_typedef ) && !def ) {
2151
	    id = make_func_mem_decl ( ds, t, id, def ) ;
2152
	    return ( id ) ;
2153
	}
2154
 
2155
	/* Check on identifier name */
2156
	err = check_id_name ( id, CONTEXT_FUNCTION ) ;
2157
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
2158
 
2159
	/* Check on member qualification */
2160
	if ( cq == qual_none ) {
2161
	    /* Declaration of simple identifier */
2162
	    ns = nonclass_namespace ;
2163
	    ens = nonblock_namespace ;
2164
	    mem = search_member ( ns, nm, 1 ) ;
2165
	    old_id = DEREF_id ( member_id ( mem ) ) ;
2166
	    if ( !IS_NULL_id ( old_id ) ) {
2167
		old_id = redecl_inherit ( old_id, cq, 0, 1 ) ;
2168
	    }
2169
	    if ( in_template_decl && is_templ_nspace ( crt_namespace ) ) {
2170
		/* Friend of template class */
2171
		NAMESPACE tns = templ_namespace ;
2172
		mem = search_member ( tns, nm, 1 ) ;
2173
	    }
2174
	} else {
2175
	    /* Redeclaration of class or namespace member */
2176
	    ns = DEREF_nspace ( id_parent ( id ) ) ;
2177
	    ens = ns ;
2178
	    /* QUERY: Any other restrictions? */
2179
	    if ( IS_id_undef ( id ) ) {
2180
		report ( crt_loc, ERR_lookup_qual_undef ( nm, ns ) ) ;
2181
		if ( IS_nspace_ctype ( ns ) ) itag = id_mem_func_tag ;
2182
		mem = search_member ( ns, nm, 1 ) ;
2183
		old_id = NULL_id ;
2184
	    } else {
2185
		mem = NULL_member ;
2186
		old_id = redecl_inherit ( id, cq, 0, 1 ) ;
2187
		t = bind_specialise ( &old_id, t, ds, 0, 0, def ) ;
2188
		if ( !IS_NULL_id ( old_id ) ) {
2189
		    id = old_id ;
2190
		    itag = TAG_id ( id ) ;
2191
		    ns = DEREF_nspace ( id_parent ( id ) ) ;
2192
		    ens = ns ;
2193
		    if ( def ) {
2194
			/* Check enclosing namespace scope */
2195
			check_decl_nspace ( id, ns, def, nonclass_namespace ) ;
2196
		    }
2197
		}
2198
	    }
2199
	}
2200
    }
2201
 
2202
    /* Can't define function in local class */
2203
    if ( def && really_in_function_defn ) {
2204
	report ( crt_loc, ERR_class_friend_local () ) ;
2205
    }
2206
 
2207
    /* Allow for special functions */
2208
    if ( EQ_KEYWORD ( nm, lex_main ) && IS_nspace_global ( ens ) ) {
2209
	/* Declare main as a friend - it could happen */
2210
	t = check_main ( t, nm ) ;
2211
	main_func = 1 ;
2212
    }
2213
    it = TAG_hashid ( nm ) ;
2214
    switch ( it ) {
2215
	case hashid_constr_tag : {
2216
	    t = check_constr ( t, id, ns ) ;
2217
	    ds |= dspec_main ;
2218
	    break ;
2219
	}
2220
	case hashid_destr_tag : {
2221
	    t = check_destr ( t, id, ns ) ;
2222
	    ds |= dspec_main ;
2223
	    break ;
2224
	}
2225
	case hashid_op_tag : {
2226
	    int cl = IS_nspace_ctype ( ns ) ;
2227
	    t = check_operator ( t, id, cl, &allocator ) ;
2228
	    if ( !allocator ) ds |= dspec_ignore ;
2229
	    break ;
2230
	}
2231
	case hashid_conv_tag : {
2232
	    t = check_conv ( t, id ) ;
2233
	    chk = 0 ;
2234
	    break ;
2235
	}
2236
    }
2237
 
2238
    /* Deal with inferred types */
2239
    ds = check_func_type ( ds, t, def, chk, 1 ) ;
2240
    if ( main_func ) ds |= dspec_main ;
2241
 
2242
    /* Check on storage class specifiers */
2243
    st = check_storage ( ds, CONTEXT_FUNCTION, old_id ) ;
2244
    if ( st != dspec_none ) {
2245
	/* Can't have storage class with friend */
2246
	report ( crt_loc, ERR_class_friend_storage ( st ) ) ;
2247
    }
2248
    if ( def ) {
2249
	/* Functions defined in a class are inline */
2250
	df = dspec_defn ;
2251
	ds |= dspec_inline ;
2252
	st = find_storage ( old_id, inline_linkage, t ) ;
2253
    } else if ( ds & dspec_inline ) {
2254
	df = dspec_none ;
2255
	st = find_storage ( old_id, inline_linkage, t ) ;
2256
    } else {
2257
	df = dspec_none ;
2258
	st = find_storage ( old_id, dspec_extern, t ) ;
2259
    }
2260
    if ( ( ds & dspec_inline ) && main_func ) {
2261
	report ( crt_loc, ERR_basic_start_main_link ( nm, dspec_inline ) ) ;
2262
	ds &= ~dspec_inline ;
2263
	st = dspec_extern ;
2264
    }
2265
 
2266
    /* Check on function specifiers */
2267
    fn = check_func_spec ( ds, CONTEXT_FUNCTION ) ;
2268
 
2269
    /* Create the declaration */
2270
    t = lvalue_type ( t ) ;
2271
    rs = ( ds & dspec_other ) ;
2272
    ds = ( st | df | fn | rs ) ;
2273
    if ( !IS_NULL_id ( old_id ) ) {
2274
	/* Check redeclarations */
2275
	old_id = redecl_func ( ds, t, old_id, itag, &over_id, def ) ;
2276
	if ( IS_NULL_id ( old_id ) && IS_NULL_member ( mem ) ) {
2277
	    /* Bad redeclaration of class or namespace member */
2278
	    nm = lookup_anon () ;
2279
	    mem = search_member ( ns, nm, 1 ) ;
2280
	    if ( IS_nspace_ctype ( ns ) ) itag = id_mem_func_tag ;
2281
	    over_id = NULL_id ;
2282
	}
2283
    }
2284
    object_type ( t, itag ) ;
2285
    if ( IS_NULL_id ( old_id ) ) {
2286
	/* Declare the function */
2287
	ds = adjust_linkage ( ds, 0 ) ;
2288
	MAKE_id_function_etc ( itag, nm, ds, ns, decl_loc, t, over_id, id ) ;
2289
	id = unify_subsequent ( id, t, def ) ;
2290
	set_member ( mem, id ) ;
2291
	decl_func_type ( id, t, def ) ;
2292
	report ( decl_loc, ERR_class_friend_pre ( id ) ) ;
2293
 
2294
	/* Check for conversion functions */
2295
	if ( it == hashid_conv_tag && itag != id_mem_func_tag ) {
2296
	    report ( crt_loc, ERR_class_conv_fct_mem () ) ;
2297
	}
2298
	if ( itag == id_function_tag && option ( OPT_decl_hide ) ) {
2299
	    check_hiding ( id ) ;
2300
	}
2301
	is_redeclared = 0 ;
2302
    } else {
2303
	/* Redeclare the function */
2304
	id = old_id ;
2305
	is_redeclared = 1 ;
2306
    }
2307
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2308
#if LANGUAGE_CPP
2309
    if ( ds & dspec_c ) c_linkage ( id, def ) ;
2310
#endif
2311
 
2312
    /* Maintain list of inline functions */
2313
    if ( def ) {
2314
	CLASS_TYPE ct = crt_class ;
2315
	LIST ( IDENTIFIER ) ft = DEREF_list ( ctype_nest ( ct ) ) ;
2316
	CONS_id ( id, ft, ft ) ;
2317
	COPY_list ( ctype_nest ( ct ), ft ) ;
2318
    }
2319
 
2320
    /* Allow for discarded functions */
2321
    if ( !( rs & dspec_ignore ) && option ( OPT_discard_func ) ) {
2322
	ds &= ~dspec_ignore ;
2323
	COPY_dspec ( id_storage ( id ), ds ) ;
2324
    }
2325
 
2326
    /* Make the function a friend */
2327
    if ( chum ) friend_function ( crt_class, id, 1 ) ;
2328
    if ( main_func ) recheck_main ( id ) ;
2329
    if ( allocator ) recheck_allocator ( id, allocator ) ;
2330
    return ( id ) ;
2331
}
2332
 
2333
#endif
2334
 
2335
 
2336
/*
2337
    CHECK ON AN ANONYMOUS UNION
2338
 
2339
    An anonymous union cannot have private or protected members or member
2340
    functions (in addition, no union can have static data members).  This
2341
    information is readily accessible from the class information field.
2342
    The routine class_info is not used in this case because it includes
2343
    the name of ct in its errors, which is not particularly useful when
2344
    ct is anonymous.
2345
*/
2346
 
2347
#if LANGUAGE_CPP
2348
 
2349
static void check_anon_union
2350
    PROTO_N ( ( ct ) )
2351
    PROTO_T ( CLASS_TYPE ct )
2352
{
2353
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
2354
    if ( ci & cinfo_private ) {
2355
	report ( crt_loc, ERR_class_union_anon_private () ) ;
2356
    }
2357
    if ( ci & cinfo_function ) {
2358
	report ( crt_loc, ERR_class_union_anon_func () ) ;
2359
    }
2360
    return ;
2361
}
2362
 
2363
#endif
2364
 
2365
 
2366
/*
2367
    CHECK AN EMPTY OBJECT DECLARATION
2368
 
2369
    This routine handles a declaration with no declarators, comprising the
2370
    declaration specifiers ds, the template type qualifiers q and the type
2371
    specifiers t (which may be an elaborate pre-type).  Whether this is
2372
    legal depends on the value of the flag have_type_specifier described
2373
    in predict.c, which indicates whether t contained a type declaration
2374
    or definition.  Note that anonymous unions come under this heading
2375
    (I hate anonymous unions).
2376
*/
2377
 
2378
static IDENTIFIER empty_object_decl
2379
    PROTO_N ( ( ds, q, t ) )
2380
    PROTO_T ( DECL_SPEC ds X TYPE q X TYPE t )
2381
{
2382
    CV_SPEC qual ;
2383
    DECL_SPEC st ;
2384
    IDENTIFIER tid ;
2385
    BASE_TYPE key = btype_class ;
2386
    int td = have_type_declaration ;
2387
 
2388
    /* Check for type declarations */
2389
    if ( IS_type_pre ( t ) ) {
2390
	QUALIFIER it ;
2391
	DESTROY_type_pre ( destroy, qual, tid, key, it, t ) ;
2392
	if ( it == qual_none ) {
2393
	    /* Unqualified identifier */
2394
	    DECL_SPEC mode = ( dspec_defn | dspec_auto ) ;
2395
	    td = TYPE_DECL_ELABORATE ;
2396
	    tid = find_elaborate_type ( tid, key, q, mode ) ;
2397
	} else {
2398
	    /* Qualified identifier */
2399
	    DECL_SPEC mode = ( dspec_defn | dspec_alias ) ;
2400
	    td = TYPE_DECL_OVER_ELAB ;
2401
	    if ( !IS_NULL_type ( q ) ) td = TYPE_DECL_ELABORATE ;
2402
	    tid = find_elaborate_type ( tid, key, q, mode ) ;
2403
	}
2404
#if LANGUAGE_CPP
2405
	t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
2406
	t = qualify_type ( t, qual, 0 ) ;
2407
#endif
2408
    } else {
2409
	tid = DEREF_id ( type_name ( t ) ) ;
2410
	qual = DEREF_cv ( type_qual ( t ) ) ;
2411
    }
2412
    qual &= cv_qual ;
2413
 
2414
#if LANGUAGE_CPP
2415
    /* Check for anonymous unions */
2416
    if ( td == TYPE_DECL_ANON && !( ds & dspec_typedef ) ) {
2417
	TYPE s = t ;
2418
	while ( IS_type_templ ( s ) ) {
2419
	    s = DEREF_type ( type_templ_defn ( s ) ) ;
2420
	}
2421
	if ( IS_type_compound ( s ) ) {
2422
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( s ) ) ;
2423
	    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
2424
	    if ( ci & cinfo_union ) {
2425
		/* Anonymous union found */
2426
		int def ;
2427
		HASHID nm ;
2428
		IDENTIFIER id ;
2429
		check_anon_union ( ct ) ;
2430
 
2431
		/* Create the union object */
2432
		nm = lookup_anon () ;
2433
		id = DEREF_id ( hashid_id ( nm ) ) ;
2434
		crt_id_qualifier = qual_none ;
2435
		crt_templ_qualifier = 0 ;
2436
		id = make_object_decl ( ds, t, id, 0 ) ;
2437
		ds = DEREF_dspec ( id_storage ( id ) ) ;
2438
		if ( ds & dspec_extern ) {
2439
		    /* Anonymous unions can't have external linkage */
2440
		    report ( crt_loc, ERR_class_union_anon_static () ) ;
2441
		}
2442
		def = init_object ( id, NULL_exp ) ;
2443
		if ( do_dump ) dump_declare ( id, &crt_loc, def ) ;
2444
		if ( output_diag && !in_function_defn ) {
2445
		    compile_variable ( id, 1 ) ;
2446
		}
2447
 
2448
		/* Bring fields into scope */
2449
		if ( !redecl_anon_union ( ct, ds, id ) ) {
2450
		    report ( crt_loc, ERR_dcl_dcl_empty () ) ;
2451
		}
2452
		return ( id ) ;
2453
	    }
2454
	}
2455
    }
2456
#endif
2457
 
2458
    /* Check declaration specifiers */
2459
    if ( ds != dspec_none ) {
2460
	st = check_storage ( ds, CONTEXT_OBJECT, NULL_id ) ;
2461
	if ( st != dspec_none ) {
2462
	    /* Can't have a storage class specifier */
2463
	    report ( crt_loc, ERR_dcl_stc_bad ( st ) ) ;
2464
	}
2465
	if ( ds & dspec_typedef ) {
2466
	    report ( crt_loc, ERR_dcl_typedef_dcl () ) ;
2467
	}
2468
	IGNORE check_func_spec ( ds, CONTEXT_OBJECT ) ;
2469
    }
2470
 
2471
    /* Check type qualifiers */
2472
    if ( qual != cv_none && td != TYPE_DECL_NONE ) {
2473
	report ( crt_loc, ERR_dcl_type_cv_unused ( qual ) ) ;
2474
    }
2475
 
2476
    /* Check for type definitions */
2477
    switch ( td ) {
2478
	case TYPE_DECL_NONE : {
2479
	    report ( crt_loc, ERR_dcl_dcl_empty () ) ;
2480
	    tid = NULL_id ;
2481
	    break ;
2482
	}
2483
	case TYPE_DECL_ANON : {
2484
	    report ( crt_loc, ERR_dcl_dcl_anon () ) ;
2485
	    break ;
2486
	}
2487
	case TYPE_DECL_OVER_ELAB : {
2488
	    report ( crt_loc, ERR_dcl_type_elab_qual ( key ) ) ;
2489
	    break ;
2490
	}
2491
    }
2492
    return ( tid ) ;
2493
}
2494
 
2495
 
2496
/*
2497
    CHECK AN EMPTY MEMBER DECLARATION
2498
 
2499
    This routine handles a class member declaration with no declarators,
2500
    comprising the declaration specifiers ds, the template type qualifiers
2501
    q and the type specifiers t.  This is similar to empty_object_decl,
2502
    except that provision needs to be made for friend declarations of the
2503
    form 'friend class C'.
2504
*/
2505
 
2506
static IDENTIFIER empty_member_decl
2507
    PROTO_N ( ( ds, q, t ) )
2508
    PROTO_T ( DECL_SPEC ds X TYPE q X TYPE t )
2509
{
2510
    CV_SPEC qual ;
2511
    IDENTIFIER tid ;
2512
    BASE_TYPE key = btype_class ;
2513
    int td = have_type_declaration ;
2514
 
2515
    /* Check for type declarations */
2516
    if ( IS_type_pre ( t ) ) {
2517
	QUALIFIER it ;
2518
	DESTROY_type_pre ( destroy, qual, tid, key, it, t ) ;
2519
	if ( it == qual_none ) {
2520
	    /* Unqualified identifier */
2521
	    DECL_SPEC mode = dspec_defn ;
2522
	    if ( option ( OPT_class_scope ) ) mode |= dspec_auto ;
2523
	    if ( ds & dspec_friend ) {
2524
		mode = ( dspec_defn | dspec_used | dspec_friend ) ;
2525
		if ( in_template_decl && is_templ_nspace ( crt_namespace ) ) {
2526
		    /* Friend of template class */
2527
		    mode |= dspec_template ;
2528
		}
2529
		mode |= dspec_register ;
2530
	    }
2531
	    tid = find_elaborate_type ( tid, key, q, mode ) ;
2532
	    if ( mode & dspec_template ) {
2533
		NAMESPACE ns = DEREF_nspace ( id_parent ( tid ) ) ;
2534
		if ( EQ_nspace ( ns, templ_namespace ) ) {
2535
		    ns = nonclass_namespace ;
2536
		    COPY_nspace ( id_parent ( tid ), ns ) ;
2537
		}
2538
	    }
2539
	    td = TYPE_DECL_ELABORATE ;
2540
	} else {
2541
	    /* Qualified identifier */
2542
	    DECL_SPEC mode = ( dspec_defn | dspec_alias ) ;
2543
	    if ( ds & dspec_friend ) mode |= dspec_friend ;
2544
	    tid = find_elaborate_type ( tid, key, q, mode ) ;
2545
	    td = TYPE_DECL_OVER_ELAB ;
2546
	}
2547
	t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
2548
	t = qualify_type ( t, qual, 0 ) ;
2549
    } else {
2550
	tid = DEREF_id ( type_name ( t ) ) ;
2551
	qual = DEREF_cv ( type_qual ( t ) ) ;
2552
    }
2553
    qual &= cv_qual ;
2554
 
2555
#if LANGUAGE_CPP
2556
    /* Check for anonymous unions */
2557
    if ( td == TYPE_DECL_ANON && !( ds & dspec_typedef ) ) {
2558
	TYPE s = t ;
2559
	while ( IS_type_templ ( s ) ) {
2560
	    s = DEREF_type ( type_templ_defn ( s ) ) ;
2561
	}
2562
	if ( IS_type_compound ( s ) ) {
2563
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( s ) ) ;
2564
	    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
2565
	    if ( ci & cinfo_union ) {
2566
		/* Anonymous union found */
2567
		int def ;
2568
		HASHID nm ;
2569
		IDENTIFIER id ;
2570
		check_anon_union ( ct ) ;
2571
 
2572
		/* Create union member */
2573
		nm = lookup_anon () ;
2574
		id = DEREF_id ( hashid_id ( nm ) ) ;
2575
		crt_id_qualifier = qual_none ;
2576
		crt_templ_qualifier = 0 ;
2577
		if ( ds & dspec_static ) {
2578
		    /* Can't be a static data member */
2579
		    report ( crt_loc, ERR_class_union_anon_mem () ) ;
2580
		    ds &= ~dspec_static ;
2581
		}
2582
		id = make_member_decl ( ds, t, id, 0 ) ;
2583
		def = init_member ( id, NULL_exp ) ;
2584
		if ( do_dump ) dump_declare ( id, &crt_loc, def ) ;
2585
 
2586
		/* Bring fields into scope */
2587
		if ( !redecl_anon_union ( ct, dspec_none, id ) ) {
2588
		    report ( crt_loc, ERR_class_mem_empty () ) ;
2589
		}
2590
		return ( id ) ;
2591
	    }
2592
	}
2593
    }
2594
#endif
2595
 
2596
    /* Check declaration specifiers */
2597
    if ( ds != dspec_none ) {
2598
	CLASS_TYPE cs = crt_class ;
2599
	CLASS_TYPE ct = NULL_ctype ;
2600
	DECL_SPEC st = check_storage ( ds, CONTEXT_MEMBER, NULL_id ) ;
2601
	if ( st != dspec_none ) {
2602
	    /* Can't have a storage class specifier */
2603
	    report ( crt_loc, ERR_dcl_stc_bad ( st ) ) ;
2604
	}
2605
	if ( ds & dspec_typedef ) {
2606
	    report ( crt_loc, ERR_dcl_typedef_dcl () ) ;
2607
	}
2608
	if ( ds & dspec_friend ) {
2609
	    /* Allow for friend declarations */
2610
	    while ( IS_type_templ ( t ) ) {
2611
		t = DEREF_type ( type_templ_defn ( t ) ) ;
2612
	    }
2613
	    if ( IS_type_compound ( t ) ) {
2614
		ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2615
	    } else if ( is_templ_type ( t ) ) {
2616
		IDENTIFIER cid = DEREF_id ( type_token_tok ( t ) ) ;
2617
		ct = find_class ( cid ) ;
2618
	    }
2619
	    if ( !IS_NULL_ctype ( ct ) ) {
2620
		switch ( td ) {
2621
		    case TYPE_DECL_ELABORATE :
2622
		    case TYPE_DECL_OVER_ELAB : {
2623
			/* Have 'friend class A ;' */
2624
			break ;
2625
		    }
2626
		    case TYPE_DECL_NORMAL : {
2627
			/* Have 'friend class A { ... } ;' */
2628
			report ( crt_loc, ERR_class_friend_def () ) ;
2629
			break ;
2630
		    }
2631
		    case TYPE_DECL_ANON : {
2632
			/* Have 'friend class { ... } ;' */
2633
			report ( crt_loc, ERR_class_mem_anon () ) ;
2634
			report ( crt_loc, ERR_class_friend_def () ) ;
2635
			break ;
2636
		    }
2637
		    default : {
2638
			/* Have 'friend A ;' */
2639
			report ( crt_loc, ERR_class_friend_elab () ) ;
2640
			break ;
2641
		    }
2642
		}
2643
		ds &= ~dspec_friend ;
2644
	    }
2645
	}
2646
	IGNORE check_func_spec ( ds, CONTEXT_MEMBER ) ;
2647
	if ( !IS_NULL_ctype ( ct ) ) {
2648
	    /* Declare a class as a friend */
2649
	    tid = DEREF_id ( ctype_name ( ct ) ) ;
2650
	    if ( qual != cv_none ) {
2651
		report ( crt_loc, ERR_dcl_type_cv_unused ( qual ) ) ;
2652
	    }
2653
	    friend_class ( cs, tid, 1 ) ;
2654
	    return ( tid ) ;
2655
	}
2656
    }
2657
 
2658
    /* Check type qualifiers */
2659
    if ( qual != cv_none && td != TYPE_DECL_NONE ) {
2660
	report ( crt_loc, ERR_dcl_type_cv_unused ( qual ) ) ;
2661
    }
2662
 
2663
    /* Check for type definitions */
2664
    switch ( td ) {
2665
	case TYPE_DECL_NONE : {
2666
	    report ( crt_loc, ERR_class_mem_empty () ) ;
2667
	    tid = NULL_id ;
2668
	    break ;
2669
	}
2670
	case TYPE_DECL_ANON : {
2671
	    report ( crt_loc, ERR_class_mem_anon () ) ;
2672
	    break ;
2673
	}
2674
	case TYPE_DECL_OVER_ELAB : {
2675
	    report ( crt_loc, ERR_dcl_type_elab_qual ( key ) ) ;
2676
	    break ;
2677
	}
2678
    }
2679
    return ( tid ) ;
2680
}
2681
 
2682
 
2683
/*
2684
    CHECK AN EMPTY DECLARATION
2685
 
2686
    This routine checks the empty declaration declared with the declaration
2687
    specifiers ds, the template type qualifiers q, the pre-type bt and t
2688
    and the cv-qualifiers cv (not all of which will be empty).  The tok
2689
    parameter is used to pass in the number of the last lexical token read,
2690
    this is used for a backwards compatibility hack involving types such
2691
    as wchar_t which used to be defined by typedefs, but are now keywords.
2692
    mem is true for member declarations.
2693
*/
2694
 
2695
IDENTIFIER empty_decl
2696
    PROTO_N ( ( ds, q, bt, t, cv, tok, mem ) )
2697
    PROTO_T ( DECL_SPEC ds X TYPE q X BASE_TYPE bt X TYPE t X
2698
	      CV_SPEC cv X int tok X int mem )
2699
{
2700
    /* Check for empty declarations */
2701
    IDENTIFIER id ;
2702
    int have_specifier = 1 ;
2703
    decl_loc = crt_loc ;
2704
    if ( ds == dspec_none && bt == btype_none && cv == cv_none ) {
2705
	if ( IS_NULL_type ( q ) ) {
2706
	    if ( IS_NULL_type ( t ) ) {
2707
		/* Only semicolon in declaration */
2708
		if ( mem ) {
2709
		    if ( tok != lex_func_Hop ) {
2710
			/* Allowed after function definition */
2711
			report ( crt_loc, ERR_class_mem_semicolon () ) ;
2712
		    }
2713
		} else {
2714
		    report ( crt_loc, ERR_dcl_dcl_semicolon () ) ;
2715
		}
2716
		return ( NULL_id ) ;
2717
	    }
2718
	    if ( have_type_declaration == TYPE_DECL_NONE ) {
2719
		/* No type specifier in declaration */
2720
		have_specifier = 0 ;
2721
	    }
2722
	}
2723
    }
2724
 
2725
    /* Check for definitions of built-in types */
2726
    if ( ( ds & dspec_typedef ) && !mem ) {
2727
	BASE_TYPE bs = key_type ( tok ) ;
2728
	if ( bs ) {
2729
	    /* Type is now a keyword */
2730
	    HASHID nm = KEYWORD ( tok ) ;
2731
	    id = DEREF_id ( hashid_id ( nm ) ) ;
2732
	    bt &= ~bs ;
2733
	    t = complete_pre_type ( bt, t, cv, 0 ) ;
2734
	    report ( crt_loc, ERR_lex_key_word ( tok ) ) ;
2735
	    crt_id_qualifier = qual_none ;
2736
	    crt_templ_qualifier = 0 ;
2737
	    id = make_object_decl ( ds, t, id, 0 ) ;
2738
	    return ( id ) ;
2739
	}
2740
    }
2741
 
2742
    /* Check for type access declarations */
2743
    if ( !have_specifier && mem ) {
2744
	if ( !IS_NULL_type ( t ) && IS_type_pre ( t ) ) {
2745
	    BASE_TYPE key = DEREF_btype ( type_pre_rep ( t ) ) ;
2746
	    if ( key == btype_alias ) {
2747
		IDENTIFIER tid = DEREF_id ( type_name ( t ) ) ;
2748
		QUALIFIER qual = DEREF_qual ( type_pre_nqual ( t ) ) ;
2749
		if ( qual && !IS_NULL_id ( tid ) ) {
2750
		    id = access_decl ( tid ) ;
2751
		    return ( id ) ;
2752
		}
2753
	    }
2754
	}
2755
    }
2756
 
2757
    /* Perform the declaration */
2758
    t = empty_complete_pre_type ( bt, t, cv, 0 ) ;
2759
    if ( mem ) {
2760
	report ( crt_loc, ERR_class_mem_declarator () ) ;
2761
	id = empty_member_decl ( ds, q, t ) ;
2762
    } else {
2763
	id = empty_object_decl ( ds, q, t ) ;
2764
    }
2765
    if ( in_weak_param ) {
2766
	/* Shouldn't happen in parameter declaration lists */
2767
	report ( crt_loc, ERR_dcl_fct_par_none () ) ;
2768
    }
2769
    return ( id ) ;
2770
}
2771
 
2772
 
2773
/*
2774
    UPDATE EXTERNAL DECLARATION INFORMATION
2775
 
2776
    This routine is called after each external declaration.  d gives
2777
    the number of objects declared and e gives the associated expression
2778
    for an asm definition.  Note that this is the point of instantiation
2779
    for any pending templates.
2780
*/
2781
 
2782
void external_declaration
2783
    PROTO_N ( ( e, d ) )
2784
    PROTO_T ( EXP e X int d )
2785
{
2786
    if ( crt_file_type == 0 ) {
2787
	no_declarations += ( unsigned long ) d ;
2788
    }
2789
    if ( !IS_NULL_exp ( e ) ) {
2790
	/* Compile any asm definitions */
2791
	compile_asm ( e ) ;
2792
    }
2793
    if ( !in_preproc_dir ) {
2794
	if ( crt_access_list.pending ) {
2795
	    /* Clear any outstanding access checks */
2796
	    IGNORE report_access ( NULL_id ) ;
2797
	}
2798
	if ( !IS_NULL_list ( pending_templates ) ) {
2799
	    /* Instantiate any pending templates */
2800
	    clear_templates ( 0 ) ;
2801
	}
2802
    }
2803
    return ;
2804
}