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 "etype_ops.h"
35
#include "exp_ops.h"
36
#include "graph_ops.h"
37
#include "hashid_ops.h"
38
#include "id_ops.h"
39
#include "inst_ops.h"
40
#include "nat_ops.h"
41
#include "member_ops.h"
42
#include "nspace_ops.h"
43
#include "off_ops.h"
44
#include "tok_ops.h"
45
#include "type_ops.h"
46
#include "error.h"
47
#include "catalog.h"
48
#include "option.h"
49
#include "access.h"
50
#include "allocate.h"
51
#include "basetype.h"
52
#include "cast.h"
53
#include "chktype.h"
54
#include "construct.h"
55
#include "constant.h"
56
#include "convert.h"
57
#include "copy.h"
58
#include "class.h"
59
#include "declare.h"
60
#include "derive.h"
61
#include "dump.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 "namespace.h"
69
#include "operator.h"
70
#include "parse.h"
71
#include "predict.h"
72
#include "redeclare.h"
73
#include "rewrite.h"
74
#include "statement.h"
75
#include "syntax.h"
76
#include "template.h"
77
#include "tokdef.h"
78
#include "token.h"
79
#include "virtual.h"
80
 
81
 
82
/*
83
    SET A CLASS KEY
84
 
85
    This routine adjusts the class information ci to be that representing
86
    the given class key.
87
*/
88
 
89
static CLASS_INFO set_class_key
90
    PROTO_N ( ( ci, key ) )
91
    PROTO_T ( CLASS_INFO ci X BASE_TYPE key )
92
{
93
    ci &= ~cinfo_key ;
94
    if ( key == btype_union ) {
95
	ci |= cinfo_union ;
96
    } else if ( key == btype_struct ) {
97
	ci |= cinfo_struct ;
98
    }
99
    return ( ci ) ;
100
}
101
 
102
 
103
/*
104
    IS A NAMESPACE CONTAINED IN A BLOCK?
105
 
106
    This routine checks whether the namespace ns is enclosed within a
107
    block namespace.
108
*/
109
 
110
int is_local_nspace
111
    PROTO_N ( ( ns ) )
112
    PROTO_T ( NAMESPACE ns )
113
{
114
    while ( !IS_NULL_nspace ( ns ) ) {
115
	IDENTIFIER id ;
116
	switch ( TAG_nspace ( ns ) ) {
117
	    case nspace_block_tag :
118
	    case nspace_dummy_tag : {
119
		return ( 1 ) ;
120
	    }
121
	    case nspace_param_tag :
122
	    case nspace_templ_tag : {
123
		return ( 2 ) ;
124
	    }
125
	}
126
	id = DEREF_id ( nspace_name ( ns ) ) ;
127
	if ( IS_NULL_id ( id ) ) break ;
128
	ns = DEREF_nspace ( id_parent ( id ) ) ;
129
    }
130
    return ( 0 ) ;
131
}
132
 
133
 
134
/*
135
    CREATE A CLASS OR ENUMERATION TYPE
136
 
137
    This routine creates a class or enumeration type (indicated by key)
138
    with type name nm and template qualifiers q in the namespace ns.
139
*/
140
 
141
IDENTIFIER make_class
142
    PROTO_N ( ( ns, nm, key, bds, q, prev ) )
143
    PROTO_T ( NAMESPACE ns X HASHID nm X BASE_TYPE key X
144
	      DECL_SPEC bds X TYPE q X TYPE prev )
145
{
146
    TYPE t ;
147
    IDENTIFIER id ;
148
    ENUM_TYPE et = NULL_etype ;
149
    CLASS_TYPE ct = NULL_ctype ;
150
    CLASS_INFO ci = cinfo_default ;
151
    DECL_SPEC ds = ( bds | dspec_lang ) ;
152
    if ( option ( OPT_complete_struct ) && crt_file_type == 0 ) {
153
	/* Allow incomplete types in start-up files */
154
	ci |= cinfo_incomplete ;
155
    }
156
 
157
    /* Check on class linkage and access */
158
    if ( !IS_NULL_nspace ( ns ) ) {
159
	if ( IS_nspace_ctype ( ns ) ) {
160
	    /* Only nested classes (not friend classes) have access */
161
	    if ( !( ds & dspec_access ) ) ds |= crt_access ;
162
	}
163
	if ( !really_in_function_defn || !is_local_nspace ( ns ) ) {
164
	    ds |= dspec_extern ;
165
	}
166
    }
167
 
168
    if ( key == btype_enum ) {
169
	/* Create the enumeration type */
170
	MAKE_id_enum_name ( nm, ds, ns, decl_loc, NULL_type, id ) ;
171
	COPY_btype ( id_enum_name_rep ( id ), key ) ;
172
	MAKE_etype_basic ( id, ci, type_sint, et ) ;
173
	MAKE_type_enumerate ( cv_none, et, t ) ;
174
	if ( !IS_NULL_type ( q ) ) {
175
	    report ( decl_loc, ERR_temp_decl_bad () ) ;
176
	}
177
	COPY_type ( id_enum_name_defn ( id ), t ) ;
178
 
179
    } else {
180
	TYPE s ;
181
	GRAPH gr ;
182
	MEMBER mem ;
183
	NAMESPACE mns ;
184
	DECL_SPEC acc ;
185
	IDENTIFIER cid ;
186
 
187
	/* Allow for template classes */
188
	if ( !IS_NULL_type ( q ) ) {
189
	    ds |= dspec_template ;
190
	    ci |= cinfo_template ;
191
	}
192
 
193
	/* Create the class member namespace */
194
	MAKE_id_class_name ( nm, ds, ns, decl_loc, NULL_type, id ) ;
195
	COPY_btype ( id_class_name_rep ( id ), key ) ;
196
	mns = make_namespace ( id, nspace_ctype_tag, 20 ) ;
197
 
198
	/* Construct the base class graph */
199
	acc = ( dspec_public | dspec_defn | dspec_done ) ;
200
	MAKE_graph_basic ( NULL_ctype, acc, gr ) ;
201
	COPY_graph ( graph_top ( gr ), gr ) ;
202
 
203
	/* Construct the class */
204
	ci = set_class_key ( ci, key ) ;
205
	MAKE_ctype_basic ( id, ci, cusage_none, mns, gr, 1, prev, ct ) ;
206
	COPY_ctype ( graph_head ( gr ), ct ) ;
207
 
208
	/* Construct the class type */
209
	MAKE_type_compound ( cv_none, ct, t ) ;
210
	COPY_type ( id_class_name_defn ( id ), t ) ;
211
	s = t ;
212
	if ( !IS_NULL_type ( q ) ) {
213
	    /* Allow for template qualifiers */
214
	    t = inject_pre_type ( q, t, 0 ) ;
215
	    COPY_type ( id_class_name_defn ( id ), t ) ;
216
	    IGNORE check_templ_params ( t, id ) ;
217
	}
218
 
219
	/* Construct the constructor and destructor names */
220
	IGNORE lookup_constr ( s, id ) ;
221
	IGNORE lookup_destr ( s, id ) ;
222
 
223
	/* Inject the class name into the class */
224
	ds &= ~dspec_access ;
225
	ds |= ( dspec_alias | dspec_implicit | dspec_public ) ;
226
	if ( bds & dspec_instance ) ds |= dspec_template ;
227
	mem = search_member ( mns, nm, 1 ) ;
228
	MAKE_id_class_name ( nm, ds, mns, decl_loc, t, cid ) ;
229
	COPY_btype ( id_class_name_rep ( cid ), key ) ;
230
	COPY_id ( id_alias ( cid ), id ) ;
231
	COPY_id ( member_alt ( mem ), cid ) ;
232
#if LANGUAGE_CPP
233
	COPY_id ( member_id ( mem ), cid ) ;
234
#endif
235
    }
236
 
237
    /* Deal with nested classes */
238
    if ( IS_nspace_ctype ( ns ) ) {
239
	CLASS_TYPE cr ;
240
	CLASS_INFO cj ;
241
	LIST ( IDENTIFIER ) fr ;
242
	IDENTIFIER rid = DEREF_id ( nspace_name ( ns ) ) ;
243
	TYPE r = DEREF_type ( id_class_name_defn ( rid ) ) ;
244
	while ( IS_type_templ ( r ) ) {
245
	    r = DEREF_type ( type_templ_defn ( r ) ) ;
246
	}
247
	cr = DEREF_ctype ( type_compound_defn ( r ) ) ;
248
	cj = DEREF_cinfo ( ctype_info ( cr ) ) ;
249
	if ( cj & cinfo_template ) {
250
	    /* Mark nested template classes */
251
	    ci |= ( cinfo_template | cinfo_rescan ) ;
252
	}
253
	ci |= cinfo_nested ;
254
	if ( !IS_NULL_ctype ( ct ) ) COPY_cinfo ( ctype_info ( ct ), ci ) ;
255
	if ( !IS_NULL_etype ( et ) ) COPY_cinfo ( etype_info ( et ), ci ) ;
256
	fr = DEREF_list ( ctype_nest ( cr ) ) ;
257
	CONS_id ( id, fr, fr ) ;
258
	COPY_list ( ctype_nest ( cr ), fr ) ;
259
    }
260
    return ( id ) ;
261
}
262
 
263
 
264
/*
265
    CHECK A CONSTRUCTOR NAME
266
 
267
    This routine is called for a declarator id which represents a type
268
    name in the namespace ns.  It checks whether id is actually a
269
    constructor name, and if so returns the correct identifier.
270
*/
271
 
272
IDENTIFIER constr_name
273
    PROTO_N ( ( ns, id ) )
274
    PROTO_T ( NAMESPACE ns X IDENTIFIER id )
275
{
276
    unsigned tag = TAG_id ( id ) ;
277
    if ( tag == id_class_name_tag || tag == id_class_alias_tag ) {
278
	CLASS_TYPE ct ;
279
	NAMESPACE cns ;
280
	TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
281
	while ( IS_type_templ ( t ) ) {
282
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
283
	}
284
	ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
285
	cns = DEREF_nspace ( ctype_member ( ct ) ) ;
286
	if ( IS_NULL_nspace ( ns ) ) ns = crt_namespace ;
287
	if ( EQ_nspace ( ns, cns ) ) {
288
	    /* This is a constructor */
289
	    IDENTIFIER cid = DEREF_id ( ctype_constr ( ct ) ) ;
290
	    if ( tag == id_class_alias_tag && is_constructor_next ) {
291
		/* Can't use type alias */
292
		HASHID cnm = DEREF_hashid ( id_name ( cid ) ) ;
293
		IDENTIFIER tid = DEREF_id ( hashid_constr_tid ( cnm ) ) ;
294
		if ( !EQ_id ( tid, id ) ) {
295
		    report ( crt_loc, ERR_dcl_typedef_constr ( id, cnm ) ) ;
296
		}
297
	    }
298
	    set_hashid_loc ( cid, id ) ;
299
	    id = cid ;
300
	}
301
    }
302
    is_constructor_next = 0 ;
303
    return ( id ) ;
304
}
305
 
306
 
307
/*
308
    HAS A TYPE BEEN DEFINED?
309
 
310
    This routine checks whether the type associated with the identifier id
311
    has been defined or just declared.  A value of 2 is returned for
312
    tokenised types.  The value associated with a defined type is returned
313
    via pt.  If force is true then any template classes are completed.
314
*/
315
 
316
int is_defined
317
    PROTO_N ( ( id, pt, force ) )
318
    PROTO_T ( IDENTIFIER id X TYPE *pt X int force )
319
{
320
    if ( IS_id_class_name_etc ( id ) ) {
321
	CLASS_INFO ci ;
322
	TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
323
	unsigned tag = TAG_type ( t ) ;
324
	*pt = t ;
325
	while ( tag == type_templ_tag ) {
326
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
327
	    tag = TAG_type ( t ) ;
328
	}
329
	if ( tag == type_compound_tag ) {
330
	    /* Class type */
331
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
332
	    if ( force ) complete_class ( ct, 0 ) ;
333
	    ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
334
	} else if ( tag == type_enumerate_tag ) {
335
	    /* Enumeration type */
336
	    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
337
	    ci = DEREF_cinfo ( etype_info ( et ) ) ;
338
	} else {
339
	    return ( 1 ) ;
340
	}
341
	if ( ci & cinfo_token ) {
342
	    /* Tokenised types */
343
	    IDENTIFIER tid = find_token ( id ) ;
344
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( tid ) ) ;
345
	    if ( ds & dspec_pure ) return ( 1 ) ;
346
	    return ( 2 ) ;
347
	}
348
	if ( ci & ( cinfo_complete | cinfo_defined ) ) {
349
	    /* Defined types */
350
	    return ( 1 ) ;
351
	}
352
    }
353
    return ( 0 ) ;
354
}
355
 
356
 
357
/*
358
    FIND THE KEY ASSOCIATED WITH A CLASS
359
 
360
    This routine returns the class key associated with the class type ct.
361
*/
362
 
363
BASE_TYPE find_class_key
364
    PROTO_N ( ( ct ) )
365
    PROTO_T ( CLASS_TYPE ct )
366
{
367
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
368
    if ( ci & cinfo_union ) return ( btype_union ) ;
369
    if ( ci & cinfo_struct ) return ( btype_struct ) ;
370
    return ( btype_class ) ;
371
}
372
 
373
 
374
/*
375
    FIND THE KEY ASSOCIATED WITH A TYPE
376
 
377
    This routine returns the class key associated with the identifier id.
378
    Note that typedef names are only expanded to their definitions if
379
    expand is true.  If id is a template type parameter which has not
380
    previously been qualified then its key is set to new_key.
381
*/
382
 
383
static BASE_TYPE find_key
384
    PROTO_N ( ( id, expand, new_key ) )
385
    PROTO_T ( IDENTIFIER id X int expand X BASE_TYPE new_key )
386
{
387
    switch ( TAG_id ( id ) ) {
388
	case id_class_name_tag :
389
	class_name_lab : {
390
	    /* Class names */
391
	    CLASS_TYPE ct ;
392
	    BASE_TYPE key ;
393
	    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
394
	    while ( IS_type_templ ( t ) ) {
395
		t = DEREF_type ( type_templ_defn ( t ) ) ;
396
	    }
397
	    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
398
	    key = find_class_key ( ct ) ;
399
	    return ( key ) ;
400
	}
401
	case id_enum_name_tag : {
402
	    /* Enumeration names */
403
	    return ( btype_enum ) ;
404
	}
405
	case id_class_alias_tag : {
406
	    /* Class aliases */
407
	    if ( expand ) goto class_name_lab ;
408
	    return ( btype_alias ) ;
409
	}
410
	case id_enum_alias_tag : {
411
	    /* Enumeration aliases */
412
	    if ( expand ) return ( btype_enum ) ;
413
	    return ( btype_alias ) ;
414
	}
415
	case id_type_alias_tag : {
416
	    /* Type aliases */
417
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
418
	    if ( ds & dspec_token ) {
419
		/* Check tokenised types */
420
		TYPE t = DEREF_type ( id_type_alias_defn ( id ) ) ;
421
		if ( IS_type_token ( t ) ) {
422
		    id = DEREF_id ( type_token_tok ( t ) ) ;
423
		    return ( find_key ( id, expand, new_key ) ) ;
424
		}
425
	    }
426
	    return ( btype_alias ) ;
427
	}
428
	case id_token_tag : {
429
	    /* Tokenised types */
430
	    TOKEN tok = DEREF_tok ( id_token_sort ( id ) ) ;
431
	    if ( IS_tok_type ( tok ) ) {
432
		BASE_TYPE bt = DEREF_btype ( tok_type_kind ( tok ) ) ;
433
		if ( bt & btype_template ) {
434
		    /* Template parameter */
435
		    BASE_TYPE key = ( bt & btype_named ) ;
436
		    if ( key == btype_none ) {
437
			key = new_key ;
438
			bt |= key ;
439
			COPY_btype ( tok_type_kind ( tok ), bt ) ;
440
		    }
441
		    return ( key ) ;
442
		}
443
		return ( btype_alias ) ;
444
	    }
445
	    break ;
446
	}
447
    }
448
    return ( btype_none ) ;
449
}
450
 
451
 
452
/*
453
    CHECK THAT TWO CLASS KEYS ARE CONSISTENT
454
 
455
    This routine checks whether the class keys key1 and key2 are consistent.
456
    Basically they are consistent if they are equal, but also 'class' is
457
    consistent with 'struct'.
458
*/
459
 
460
int equal_key
461
    PROTO_N ( ( key1, key2 ) )
462
    PROTO_T ( BASE_TYPE key1 X BASE_TYPE key2 )
463
{
464
    if ( key1 == key2 ) return ( 1 ) ;
465
    if ( key1 == btype_class && key2 == btype_struct ) return ( 1 ) ;
466
    if ( key1 == btype_struct && key2 == btype_class ) return ( 1 ) ;
467
    if ( key1 == btype_any || key2 == btype_any ) return ( 1 ) ;
468
    return ( 0 ) ;
469
}
470
 
471
 
472
/*
473
    CHECK A CLASS KEY
474
 
475
    This routine checks the given class key for the type id.  It returns
476
    true if either the new or the existing class key is 'enum'.
477
*/
478
 
479
static int check_key
480
    PROTO_N ( ( id, key ) )
481
    PROTO_T ( IDENTIFIER id X BASE_TYPE key )
482
{
483
    int is_enum = 0 ;
484
    BASE_TYPE prev = find_key ( id, 1, key ) ;
485
    if ( !equal_key ( prev, key ) ) {
486
	PTR ( LOCATION ) loc = id_loc ( id ) ;
487
	ERROR err = ERR_dcl_type_elab_bad ( key, prev, id, loc ) ;
488
	report ( crt_loc, err ) ;
489
	if ( prev == btype_enum ) is_enum = 1 ;
490
	if ( key == btype_enum ) is_enum = 1 ;
491
    }
492
    return ( is_enum ) ;
493
}
494
 
495
 
496
/*
497
    CREATE A DUMMY CLASS TYPE
498
 
499
    This routine creates a dummy class or union type (as indicated by key)
500
    representing the type 'id < args >' where id depends on a template
501
    parameter.
502
*/
503
 
504
TYPE make_dummy_class
505
    PROTO_N ( ( id, args, key ) )
506
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X BASE_TYPE key )
507
{
508
    TYPE t, s ;
509
    CLASS_TYPE ct ;
510
    CLASS_INFO ci ;
511
    IDENTIFIER tid ;
512
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
513
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
514
    decl_loc = crt_loc ;
515
    tid = make_class ( ns, nm, key, dspec_public, NULL_type, NULL_type ) ;
516
    t = DEREF_type ( id_class_name_defn ( tid ) ) ;
517
    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
518
    ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
519
    ci |= ( cinfo_defined | cinfo_complete ) ;
520
    COPY_cinfo ( ctype_info ( ct ), ci ) ;
521
    MAKE_type_token ( cv_none, id, args, s ) ;
522
    COPY_type ( ctype_form ( ct ), s ) ;
523
    return ( t ) ;
524
}
525
 
526
 
527
/*
528
    FIND THE CLASS ASSOCIATED WITH AN IDENTIFIER
529
 
530
    This routine returns the class type associated with the identifier id.
531
    The null class type is returned if id is not a class name.
532
*/
533
 
534
CLASS_TYPE find_class
535
    PROTO_N ( ( id ) )
536
    PROTO_T ( IDENTIFIER id )
537
{
538
    unsigned tag = TAG_id ( id ) ;
539
    if ( tag == id_class_name_tag || tag == id_class_alias_tag ) {
540
	/* Simple class names */
541
	TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
542
	if ( IS_type_compound ( t ) ) {
543
	    /* Simple classes */
544
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
545
	    return ( ct ) ;
546
	} else {
547
	    /* Template classes */
548
	    while ( IS_type_templ ( t ) ) {
549
		t = DEREF_type ( type_templ_defn ( t ) ) ;
550
	    }
551
	    if ( IS_type_compound ( t ) ) {
552
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
553
		if ( defining_class ( ct ) ) {
554
		    /* Only allow unqualified in definition */
555
		    return ( ct ) ;
556
		}
557
	    }
558
	}
559
 
560
    } else if ( tag == id_type_alias_tag ) {
561
	/* Check tokenised types */
562
	TYPE t = DEREF_type ( id_type_alias_defn ( id ) ) ;
563
	while ( IS_type_templ ( t ) ) {
564
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
565
	}
566
	if ( IS_type_token ( t ) ) {
567
	    id = DEREF_id ( type_token_tok ( t ) ) ;
568
	    return ( find_class ( id ) ) ;
569
	}
570
 
571
    } else if ( tag == id_token_tag && is_templ_param ( id ) ) {
572
	/* Check for template parameters */
573
	int simple = 0 ;
574
	BASE_TYPE key = btype_lang ;
575
	TOKEN sort = DEREF_tok ( id_token_sort ( id ) ) ;
576
	if ( IS_tok_type ( sort ) ) {
577
	    key = DEREF_btype ( tok_type_kind ( sort ) ) ;
578
	    key &= btype_named ;
579
	    simple = 1 ;
580
	}
581
	if ( key != btype_enum ) {
582
	    TYPE t ;
583
	    if ( simple ) {
584
		t = DEREF_type ( tok_type_alt ( sort ) ) ;
585
	    } else {
586
		t = DEREF_type ( tok_class_alt ( sort ) ) ;
587
	    }
588
	    if ( IS_NULL_type ( t ) ) {
589
		/* Create dummy class type */
590
		if ( key == btype_none ) key = btype_lang ;
591
		t = make_dummy_class ( id, NULL_list ( TOKEN ), key ) ;
592
		if ( simple ) {
593
		    COPY_type ( tok_type_alt ( sort ), t ) ;
594
		} else {
595
		    COPY_type ( tok_class_alt ( sort ), t ) ;
596
		}
597
	    }
598
	    if ( IS_type_compound ( t ) ) {
599
		/* Return previous dummy class type */
600
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
601
		return ( ct ) ;
602
	    }
603
	}
604
    }
605
    return ( NULL_ctype ) ;
606
}
607
 
608
 
609
/*
610
    FIND A CLASS TYPE
611
 
612
    This routine returns the compound type associated with the class ct.
613
*/
614
 
615
TYPE make_class_type
616
    PROTO_N ( ( ct ) )
617
    PROTO_T ( CLASS_TYPE ct )
618
{
619
    IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
620
    TYPE t = DEREF_type ( id_class_name_etc_defn ( cid ) ) ;
621
    while ( IS_type_templ ( t ) ) {
622
	t = DEREF_type ( type_templ_defn ( t ) ) ;
623
    }
624
    return ( t ) ;
625
}
626
 
627
 
628
/*
629
    REDECLARE A TEMPLATE CLASS
630
 
631
    This routine redeclares the template class t using the template type
632
    qualifiers q.
633
*/
634
 
635
static TYPE redecl_templ_class
636
    PROTO_N ( ( q, t, pid ) )
637
    PROTO_T ( TYPE q X TYPE t X IDENTIFIER *pid )
638
{
639
    TYPE s = t ;
640
    IDENTIFIER id ;
641
    while ( IS_type_templ ( s ) ) {
642
	s = DEREF_type ( type_templ_defn ( s ) ) ;
643
    }
644
    s = inject_pre_type ( q, s, 0 ) ;
645
    s = bind_specialise ( pid, s, dspec_none, 1, 0, 0 ) ;
646
    id = *pid ;
647
    if ( eq_type ( s, t ) == 1 ) {
648
	/* Consistent redeclaration */
649
	TYPE ps = s ;
650
	TYPE pt = t ;
651
	redecl_template ( &pt, &ps, id ) ;
652
    } else {
653
	/* Inconsistent redeclaration */
654
	ERROR err = ERR_basic_link_incompat ( s, t ) ;
655
	ERROR err2 = ERR_basic_link_decl_type ( id, id_loc ( id ) ) ;
656
	err = concat_error ( err, err2 ) ;
657
	report ( crt_loc, err ) ;
658
    }
659
    return ( s ) ;
660
}
661
 
662
 
663
/*
664
    EXTRACT THE TEMPLATE QUALIFIERS OF A TYPE
665
 
666
    This routine does the opposite of inject_pre_type by extracting the
667
    template qualifiers from the type t.
668
*/
669
 
670
static TYPE extract_templ_qual
671
    PROTO_N ( ( t ) )
672
    PROTO_T ( TYPE t )
673
{
674
    if ( !IS_NULL_type ( t ) && IS_type_templ ( t ) ) {
675
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
676
	s = extract_templ_qual ( s ) ;
677
	COPY_type ( type_templ_defn ( t ), s ) ;
678
	return ( t ) ;
679
    }
680
    return ( NULL_type ) ;
681
}
682
 
683
 
684
/*
685
    DECLARE A CLASS OR ENUMERATION NAME
686
 
687
    This routine declares a class or enumeration type with name nm,
688
    class key key (which can be btype_class, btype_struct, btype_union
689
    or btype_enum) and template specifiers q.  The argument def is 1 if
690
    the type is about to be defined or 2 if it is about to be defined
691
    as a token.  Note that it is possible for a class or enumeration
692
    to have the same name as an object in the same scope.  In this case
693
    the type name is hidden by the object name.
694
*/
695
 
696
static IDENTIFIER declare_type
697
    PROTO_N ( ( ns, nm, key, q, def, force ) )
698
    PROTO_T ( NAMESPACE ns X HASHID nm X BASE_TYPE key X TYPE q X
699
	      int def X int force )
700
{
701
    TYPE t = NULL_type ;
702
    MEMBER mem = search_member ( ns, nm, 1 ) ;
703
    IDENTIFIER pid = DEREF_id ( member_id ( mem ) ) ;
704
    IDENTIFIER id = type_member ( mem, 3 ) ;
705
    if ( !IS_NULL_id ( id ) ) {
706
	/* Check for reserved identifiers */
707
	IDENTIFIER qid = id ;
708
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
709
	if ( ds & dspec_reserve ) {
710
	    PTR ( LOCATION ) loc = id_loc ( id ) ;
711
	    report ( crt_loc, ERR_basic_odr_def_type ( id, loc ) ) ;
712
	    id = NULL_id ;
713
	} else {
714
	    id = redecl_inherit ( id, crt_id_qualifier, in_class_defn, 0 ) ;
715
	}
716
 
717
	/* Check previous type declaration */
718
	if ( !IS_NULL_id ( id ) ) {
719
	    switch ( TAG_id ( id ) ) {
720
 
721
		case id_class_name_tag :
722
		case id_enum_name_tag : {
723
		    /* Previously declared as class name */
724
		    int hide_prev = check_key ( id, key ) ;
725
		    int prev_def = is_defined ( id, &t, 1 ) ;
726
		    if ( !IS_NULL_type ( q ) || IS_type_templ ( t ) ) {
727
			/* Redeclaration of template type */
728
			q = redecl_templ_class ( q, t, &id ) ;
729
		    }
730
		    if ( def && prev_def ) {
731
			/* Multiple definitions */
732
			if ( def == 2 ) {
733
			    /* This is a token */
734
			    /* EMPTY */
735
			} else if ( prev_def == 2 ) {
736
			    /* Previous was a token */
737
			    /* EMPTY */
738
			} else {
739
			    /* Neither is a token */
740
			    PTR ( LOCATION ) loc = id_loc ( id ) ;
741
			    ERROR err = ERR_basic_odr_def_type ( id, loc ) ;
742
			    report ( crt_loc, err ) ;
743
			    q = extract_templ_qual ( q ) ;
744
			    t = NULL_type ;
745
			}
746
			break ;
747
		    }
748
		    if ( hide_prev ) {
749
			/* Hide previous if only one is an enumeration */
750
			break ;
751
		    }
752
		    /* This is a genuine type redeclaration */
753
		    found_elaborate_type = 1 ;
754
		    if ( in_class_defn ) {
755
			/* Adjust access specifiers */
756
			adjust_access ( id, crt_access, 0 ) ;
757
		    }
758
		    if ( def ) {
759
			/* Record location of definition */
760
			if ( !IS_NULL_type ( q ) ) {
761
			    /* Update template class */
762
			    COPY_type ( id_class_name_etc_defn ( id ), q ) ;
763
			    reset_primary_templ ( t, q ) ;
764
			}
765
			COPY_loc ( id_loc ( id ), decl_loc ) ;
766
		    }
767
		    return ( id ) ;
768
		}
769
 
770
		case id_class_alias_tag :
771
		case id_enum_alias_tag :
772
		case id_type_alias_tag : {
773
		    /* Previously declared as typedef name */
774
		    PTR ( LOCATION ) loc = id_loc ( id ) ;
775
		    report ( crt_loc, ERR_basic_odr_def_type ( id, loc ) ) ;
776
		    break ;
777
		}
778
 
779
		case id_nspace_name_tag :
780
		case id_nspace_alias_tag : {
781
		    /* Previously declared as namespace name */
782
		    PTR ( LOCATION ) loc = id_loc ( id ) ;
783
		    report ( crt_loc, ERR_basic_odr_decl ( id, loc ) ) ;
784
		    break ;
785
		}
786
	    }
787
 
788
	    /* Clobber previous definition */
789
	    if ( EQ_id ( pid, qid ) ) {
790
		COPY_id ( member_id ( mem ), NULL_id ) ;
791
		pid = NULL_id ;
792
	    }
793
	    COPY_id ( member_alt ( mem ), NULL_id ) ;
794
	}
795
    }
796
 
797
    /* Check for template declarations */
798
    if ( !IS_NULL_type ( q ) ) {
799
	if ( !IS_NULL_id ( pid ) ) {
800
	    /* Already declared as object */
801
	    PTR ( LOCATION ) loc = id_loc ( pid ) ;
802
	    report ( crt_loc, ERR_basic_odr_diff ( pid, loc ) ) ;
803
	}
804
	if ( crt_linkage == dspec_c ) {
805
	    report ( crt_loc, ERR_temp_decl_linkage () ) ;
806
	}
807
    }
808
 
809
    /* Construct the type declaration */
810
    if ( force ) {
811
	id = make_class ( ns, nm, key, dspec_none, q, t ) ;
812
	set_type_member ( mem, id ) ;
813
	if ( IS_nspace_param ( ns ) ) {
814
	    /* Warn about function parameter scope */
815
	    report ( crt_loc, ERR_basic_scope_pdecl_param ( id ) ) ;
816
	}
817
    }
818
    found_elaborate_type = 0 ;
819
    return ( id ) ;
820
}
821
 
822
 
823
/*
824
    FIND THE TYPE OF A CLASS MEMBER
825
 
826
    This routine returns the class type corresponding to the member
827
    identifier id.  The null type is returned for non-members.
828
*/
829
 
830
CLASS_TYPE parent_class
831
    PROTO_N ( ( id ) )
832
    PROTO_T ( IDENTIFIER id )
833
{
834
    if ( !IS_NULL_id ( id ) ) {
835
	NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
836
	if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
837
	    IDENTIFIER cid = DEREF_id ( nspace_name ( ns ) ) ;
838
	    TYPE t = DEREF_type ( id_class_name_etc_defn ( cid ) ) ;
839
	    while ( IS_type_templ ( t ) ) {
840
		t = DEREF_type ( type_templ_defn ( t ) ) ;
841
	    }
842
	    if ( IS_type_compound ( t ) ) {
843
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
844
		return ( ct ) ;
845
	    }
846
	}
847
    }
848
    return ( NULL_ctype ) ;
849
}
850
 
851
 
852
/*
853
    FIND THE TYPE CORRESPONDING TO A NAMESPACE
854
 
855
    This routine returns the class type corresponding to the namespace
856
    ns.  The null type is returned if ns is not a class member namespace.
857
*/
858
 
859
CLASS_TYPE namespace_class
860
    PROTO_N ( ( ns ) )
861
    PROTO_T ( NAMESPACE ns )
862
{
863
    if ( IS_nspace_ctype ( ns ) ) {
864
	CLASS_TYPE ct ;
865
	IDENTIFIER cid = DEREF_id ( nspace_name ( ns ) ) ;
866
	TYPE t = DEREF_type ( id_class_name_etc_defn ( cid ) ) ;
867
	while ( IS_type_templ ( t ) ) {
868
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
869
	}
870
	ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
871
	return ( ct ) ;
872
    }
873
    return ( NULL_ctype ) ;
874
}
875
 
876
 
877
/*
878
    CLASS STACK
879
 
880
    The variable crt_class holds the class currently being defined.  The
881
    stack class_stack allows for nested class definitions.
882
*/
883
 
884
CLASS_TYPE crt_class = NULL_ctype ;
885
static STACK ( CLASS_TYPE ) class_stack = NULL_stack ( CLASS_TYPE ) ;
886
 
887
 
888
/*
889
    PUSH A CLASS ONTO THE CLASS STACK
890
 
891
    This routine sets the current class to ct pushing the previous class
892
    to the class stack.
893
*/
894
 
895
void push_class
896
    PROTO_N ( ( ct ) )
897
    PROTO_T ( CLASS_TYPE ct )
898
{
899
    PUSH_ctype ( crt_class, class_stack ) ;
900
    crt_class = ct ;
901
    return ;
902
}
903
 
904
 
905
/*
906
    POP A CLASS FROM THE CLASS STACK
907
 
908
    This routine sets the current class from the top of the class stack.
909
*/
910
 
911
void pop_class
912
    PROTO_Z ()
913
{
914
    POP_ctype ( crt_class, class_stack ) ;
915
    return ;
916
}
917
 
918
 
919
/*
920
    IS A CLASS BEING DEFINED?
921
 
922
    This routine checks whether the class ct is in the process of being
923
    defined by checking it against the current class and the various
924
    elements of the class stack.
925
*/
926
 
927
int defining_class
928
    PROTO_N ( ( ct ) )
929
    PROTO_T ( CLASS_TYPE ct )
930
{
931
    NAMESPACE nt ;
932
    LIST ( NAMESPACE ) q ;
933
    LIST ( CLASS_TYPE ) p ;
934
    if ( EQ_ctype ( ct, crt_class ) ) return ( 1 ) ;
935
    p = LIST_stack ( class_stack ) ;
936
    while ( !IS_NULL_list ( p ) ) {
937
	CLASS_TYPE cs = DEREF_ctype ( HEAD_list ( p ) ) ;
938
	if ( EQ_ctype ( cs, ct ) ) return ( 1 ) ;
939
	p = TAIL_list ( p ) ;
940
    }
941
    nt = DEREF_nspace ( ctype_member ( ct ) ) ;
942
    q = LIST_stack ( namespace_stack ) ;
943
    while ( !IS_NULL_list ( q ) ) {
944
	NAMESPACE ns = DEREF_nspace ( HEAD_list ( q ) ) ;
945
	if ( EQ_nspace ( ns, nt ) ) return ( 1 ) ;
946
	q = TAIL_list ( q ) ;
947
    }
948
    return ( 0 ) ;
949
}
950
 
951
 
952
/*
953
    BEGIN A CLASS DEFINITION
954
 
955
    This routine begins the definition of a class type (this includes
956
    structures and unions).  The class name is given by id and key is one
957
    of the values btype_class, btype_struct or btype_union indicating the
958
    class key.  ci gives any initial class information and q gives any
959
    template class qualifiers.
960
*/
961
 
962
IDENTIFIER begin_class_defn
963
    PROTO_N ( ( id, key, ci, q ) )
964
    PROTO_T ( IDENTIFIER id X BASE_TYPE key X CLASS_INFO ci X TYPE q )
965
{
966
    TYPE t ;
967
    DECL_SPEC ds ;
968
    CLASS_TYPE ct ;
969
    CLASS_INFO cj ;
970
    int nested = 0 ;
971
    IDENTIFIER cid = NULL_id ;
972
    QUALIFIER cq = crt_id_qualifier ;
973
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
974
    NAMESPACE ns = crt_namespace ;
975
    begin_declarator ( id, cq, qual_namespace, 1 ) ;
976
    IGNORE incr_value ( OPT_VAL_nested_class ) ;
977
 
978
    /* Check for template specialisations */
979
    if ( is_templ_decl ( id, NULL_type ) || is_templ_spec ( q ) ) {
980
	q = bind_specialise ( &id, q, dspec_none, 1, 1, 1 ) ;
981
	if ( IS_NULL_id ( id ) || check_key ( id, key ) ) {
982
	    nm = lookup_anon () ;
983
	    id = DEREF_id ( hashid_id ( nm ) ) ;
984
	    cq = qual_none ;
985
	} else {
986
	    cid = id ;
987
	    ns = DEREF_nspace ( id_parent ( cid ) ) ;
988
	    check_decl_nspace ( cid, ns, 1, crt_namespace ) ;
989
	    if ( is_defined ( cid, &t, 0 ) ) {
990
		/* Redefinition of template class */
991
		PTR ( LOCATION ) loc = id_loc ( cid ) ;
992
		ERROR err = ERR_basic_odr_def_type ( cid, loc ) ;
993
		report ( crt_loc, err ) ;
994
	    }
995
	}
996
    }
997
 
998
    /* Check for qualified identifiers */
999
    if ( IS_NULL_id ( cid ) ) {
1000
	/* Check on class name */
1001
	int def = 1 ;
1002
	ERROR err = check_id_name ( id, CONTEXT_CLASS ) ;
1003
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1004
	if ( IS_nspace_ctype ( ns ) ) nested = 1 ;
1005
	if ( cq == qual_none ) {
1006
	    /* Simple class name */
1007
	    if ( option ( OPT_class_scope ) ) {
1008
		ns = crt_namespace ;
1009
		if ( IS_nspace_param ( ns ) ) {
1010
		    ns = nonclass_namespace ;
1011
		    if ( nested ) nested = 2 ;
1012
		}
1013
	    } else {
1014
		ns = nonclass_namespace ;
1015
		if ( nested ) nested = 2 ;
1016
	    }
1017
	} else {
1018
	    /* Definition of nested class */
1019
	    ns = DEREF_nspace ( id_parent ( id ) ) ;
1020
	    check_decl_nspace ( id, ns, 1, crt_namespace ) ;
1021
	    if ( IS_id_undef ( id ) ) nm = lookup_anon () ;
1022
	    if ( nested ) nested = 2 ;
1023
	}
1024
 
1025
	/* Declare the type */
1026
	if ( ci & cinfo_token ) def = 2 ;
1027
	cid = declare_type ( ns, nm, key, q, def, 1 ) ;
1028
    }
1029
 
1030
    /* Find the class type */
1031
    t = DEREF_type ( id_class_name_defn ( cid ) ) ;
1032
    while ( IS_type_templ ( t ) ) {
1033
	t = DEREF_type ( type_templ_defn ( t ) ) ;
1034
    }
1035
    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1036
    ns = DEREF_nspace ( ctype_member ( ct ) ) ;
1037
 
1038
    /* Mark start of definition */
1039
    cj = DEREF_cinfo ( ctype_info ( ct ) ) ;
1040
    cj = set_class_key ( cj, key ) ;
1041
    cj |= ( ci | cinfo_defined ) ;
1042
    if ( nested == 1 ) cj |= cinfo_nested ;
1043
    cj &= ~cinfo_complete ;
1044
    COPY_cinfo ( ctype_info ( ct ), cj ) ;
1045
 
1046
    /* Mark tokenised types */
1047
    ds = DEREF_dspec ( id_storage ( cid ) ) ;
1048
    if ( cj & cinfo_token ) ds |= dspec_token ;
1049
    COPY_dspec ( id_storage ( cid ), ( ds | dspec_defn ) ) ;
1050
 
1051
#if LANGUAGE_CPP
1052
    /* Make sure that nested classes are nested */
1053
    if ( nested == 2 ) {
1054
	NAMESPACE pns = crt_namespace ;
1055
	HASHID pnm = lookup_anon () ;
1056
	IDENTIFIER pid = declare_type ( pns, pnm, key, q, 1, 1 ) ;
1057
	ds = DEREF_dspec ( id_storage ( pid ) ) ;
1058
	COPY_dspec ( id_storage ( pid ), ( ds | dspec_defn ) ) ;
1059
	t = DEREF_type ( id_class_name_defn ( cid ) ) ;
1060
	COPY_type ( id_class_name_defn ( pid ), t ) ;
1061
    }
1062
#endif
1063
 
1064
    /* Force definition information */
1065
    COPY_loc ( id_loc ( cid ), decl_loc ) ;
1066
    if ( do_dump ) dump_declare ( cid, &decl_loc, 1 ) ;
1067
 
1068
    /* Push current class and namespace */
1069
    push_namespace ( ns ) ;
1070
    push_class ( ct ) ;
1071
 
1072
    /* Set up default access specifier */
1073
    ds = ( key == btype_class ? dspec_private : dspec_public ) ;
1074
    crt_access = ds ;
1075
    prev_access = ds ;
1076
    return ( cid ) ;
1077
}
1078
 
1079
 
1080
/*
1081
    END A CLASS DEFINITION
1082
 
1083
    This routine completes the definition of the class type id.
1084
*/
1085
 
1086
IDENTIFIER end_class_defn
1087
    PROTO_N ( ( id ) )
1088
    PROTO_T ( IDENTIFIER id )
1089
{
1090
    TYPE p ;
1091
    CLASS_TYPE ct ;
1092
    CLASS_INFO ci ;
1093
    LIST ( IDENTIFIER ) ft ;
1094
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1095
    TYPE t = DEREF_type ( id_class_name_defn ( id ) ) ;
1096
    while ( IS_type_templ ( t ) ) {
1097
	t = DEREF_type ( type_templ_defn ( t ) ) ;
1098
    }
1099
    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1100
 
1101
    /* Deal with implicitly declared constructors */
1102
    ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1103
    if ( ci & cinfo_empty ) report ( crt_loc, ERR_class_none () ) ;
1104
#if LANGUAGE_CPP
1105
    ci = implicit_decl ( ct, ci, dspec_none ) ;
1106
#endif
1107
    if ( ci & cinfo_non_aggregate ) {
1108
	/* POD classes must be aggregate classes */
1109
	ci &= ~cinfo_pod ;
1110
    }
1111
    ci |= cinfo_complete ;
1112
    COPY_cinfo ( ctype_info ( ct ), ci ) ;
1113
 
1114
    /* Deal with any inheritance issues */
1115
    inherit_class () ;
1116
 
1117
    /* Check for previous definitions */
1118
    p = DEREF_type ( ctype_prev ( ct ) ) ;
1119
    if ( !IS_NULL_type ( p ) ) {
1120
	if ( ci & cinfo_token ) {
1121
	    /* Make p the tokenised type */
1122
	    TYPE q = p ;
1123
	    p = t ;
1124
	    t = q ;
1125
	}
1126
	force_tokdef++ ;
1127
	IGNORE unify_type ( p, t, cv_none, 0 ) ;
1128
	force_tokdef-- ;
1129
    }
1130
 
1131
    /* Compile any inline functions */
1132
    if ( is_exported ( id ) ) export_template ( id, 1 ) ;
1133
    ft = DEREF_list ( ctype_pals ( ct ) ) ;
1134
    if ( !IS_NULL_list ( ft ) ) {
1135
	unsigned nf ;
1136
	ft = REVERSE_list ( ft ) ;
1137
	COPY_list ( ctype_pals ( ct ), ft ) ;
1138
	nf = LENGTH_list ( ft ) ;
1139
	IGNORE check_value ( OPT_VAL_friends, ( ulong ) nf ) ;
1140
    }
1141
    ft = DEREF_list ( ctype_nest ( ct ) ) ;
1142
    if ( !IS_NULL_list ( ft ) ) {
1143
	ft = REVERSE_list ( ft ) ;
1144
	COPY_list ( ctype_nest ( ct ), ft ) ;
1145
	if ( !( ci & cinfo_nested ) || in_class_defn == 1 ) {
1146
	    rescan_functions () ;
1147
	}
1148
    }
1149
 
1150
    /* Pop current class and namespace */
1151
    if ( do_dump ) dump_undefine ( id, &crt_loc, 0 ) ;
1152
    IGNORE pop_namespace () ;
1153
    pop_class () ;
1154
    decr_value ( OPT_VAL_nested_class ) ;
1155
    end_declarator ( id, 1 ) ;
1156
 
1157
    /* Set type declaration flag */
1158
    if ( IS_hashid_anon ( nm ) ) {
1159
	have_type_declaration = TYPE_DECL_ANON ;
1160
    } else {
1161
	have_type_declaration = TYPE_DECL_NORMAL ;
1162
    }
1163
    return ( id ) ;
1164
}
1165
 
1166
 
1167
/*
1168
    CHECK CLASS INFORMATION
1169
 
1170
    This routine checks the effect of a non-static data member or base
1171
    class (as indicated by base) with class information cj on a class
1172
    with class information ci.  In a class definition trivial_constr,
1173
    for example, is true if all the base classes and members have
1174
    trivial constructors.  At the end of the definition, when the
1175
    implicit constructors are declared, it is used to indicate that the
1176
    class itself has a trivial constructor.
1177
*/
1178
 
1179
CLASS_INFO check_class_info
1180
    PROTO_N ( ( ci, cj, base, acc ) )
1181
    PROTO_T ( CLASS_INFO ci X CLASS_INFO cj X int base X DECL_SPEC acc )
1182
{
1183
    if ( !( cj & cinfo_pod ) ) ci &= ~cinfo_pod ;
1184
    if ( !( cj & cinfo_trivial_constr ) ) ci &= ~cinfo_trivial_constr ;
1185
    if ( !( cj & cinfo_trivial_copy ) ) ci &= ~cinfo_trivial_copy ;
1186
    if ( !( cj & cinfo_trivial_assign ) ) ci &= ~cinfo_trivial_assign ;
1187
    if ( !( cj & cinfo_trivial_destr ) ) ci &= ~cinfo_trivial_destr ;
1188
    if ( !( cj & cinfo_const_copy ) ) ci &= ~cinfo_const_copy ;
1189
    if ( !( cj & cinfo_const_assign ) ) ci &= ~cinfo_const_assign ;
1190
    if ( cj & cinfo_recursive ) ci |= cinfo_recursive ;
1191
    if ( cj & cinfo_params ) ci |= cinfo_params ;
1192
    if ( cj & cinfo_const ) ci |= cinfo_const ;
1193
    if ( base ) {
1194
	/* Base class checks */
1195
	if ( cj & cinfo_multiple_base ) ci |= cinfo_multiple_base ;
1196
	if ( cj & cinfo_templ_base ) ci |= cinfo_templ_base ;
1197
	if ( cj & cinfo_function ) ci |= cinfo_function ;
1198
	if ( cj & cinfo_static ) ci |= cinfo_static ;
1199
	ci &= ~cinfo_empty ;
1200
	ci |= cinfo_base ;
1201
	UNUSED ( acc ) ;
1202
    }
1203
    return ( ci ) ;
1204
}
1205
 
1206
 
1207
/*
1208
    CHECK FOR TRIVIAL CLASSES
1209
 
1210
    This routine checks whether the class ct has trivial default
1211
    constructors, destructors, copy constructors and copy assignment
1212
    operators.
1213
*/
1214
 
1215
ERROR check_trivial_class
1216
    PROTO_N ( ( ct ) )
1217
    PROTO_T ( CLASS_TYPE ct )
1218
{
1219
    ERROR err = NULL_err ;
1220
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1221
    CLASS_INFO cj = ( ci & cinfo_trivial ) ;
1222
    if ( cj != cinfo_trivial ) {
1223
	if ( !( cj & cinfo_trivial_constr ) ) {
1224
	    err = ERR_class_ctor_nontriv ( ct ) ;
1225
	} else if ( !( cj & cinfo_trivial_destr ) ) {
1226
	    err = ERR_class_dtor_nontriv ( ct ) ;
1227
	} else if ( !( cj & cinfo_trivial_copy ) ) {
1228
	    err = ERR_class_copy_nontriv_constr ( ct ) ;
1229
	} else if ( !( cj & cinfo_trivial_assign ) ) {
1230
	    err = ERR_class_copy_nontriv_assign ( ct ) ;
1231
	}
1232
    }
1233
    return ( err ) ;
1234
}
1235
 
1236
 
1237
/*
1238
    CHECK A CLASS MEMBER TYPE
1239
 
1240
    This routine checks the type t of a non-static data member of the
1241
    class ct.  This is recorded in the effect on the class information ci.
1242
    The criterion for spotting recursively defined classes is that it
1243
    contains a pointer to an incomplete type (or a base class or member
1244
    of such a type).  This certainly contains all recursive types, and
1245
    is about the best that can be done without any deep analysis.
1246
*/
1247
 
1248
CLASS_INFO check_member_type
1249
    PROTO_N ( ( ct, ci, t, ptr ) )
1250
    PROTO_T ( CLASS_TYPE ct X CLASS_INFO ci X TYPE t X int ptr )
1251
{
1252
    if ( !ptr ) {
1253
	/* Check for const members */
1254
	CV_SPEC cv = DEREF_cv ( type_qual ( t ) ) ;
1255
	if ( cv & cv_const ) ci |= cinfo_const ;
1256
    }
1257
    switch ( TAG_type ( t ) ) {
1258
	case type_compound_tag : {
1259
	    /* Compound types */
1260
	    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( t ) ) ;
1261
	    CLASS_INFO cj = DEREF_cinfo ( ctype_info ( cs ) ) ;
1262
	    if ( !( cj & cinfo_complete ) ) {
1263
		/* Could be a recursive type ... */
1264
		ci |= cinfo_recursive ;
1265
	    }
1266
	    if ( !ptr ) {
1267
		if ( ci & cinfo_union ) {
1268
		    /* Union component must be trivial */
1269
		    ERROR err = check_trivial_class ( cs ) ;
1270
		    if ( !IS_NULL_err ( err ) ) {
1271
			ERROR err2 = ERR_class_union_mem ( ct, t ) ;
1272
			err = concat_error ( err, err2 ) ;
1273
			report ( crt_loc, err ) ;
1274
		    }
1275
		}
1276
		ci = check_class_info ( ci, cj, 0, dspec_none ) ;
1277
	    }
1278
	    break ;
1279
	}
1280
	case type_ptr_tag : {
1281
	    /* Pointer types */
1282
	    if ( !ptr ) {
1283
		TYPE s = DEREF_type ( type_ptr_sub ( t ) ) ;
1284
		ci = check_member_type ( ct, ci, s, 1 ) ;
1285
	    }
1286
	    break ;
1287
	}
1288
	case type_ref_tag : {
1289
	    /* Reference types */
1290
	    if ( !ptr ) {
1291
		TYPE s = DEREF_type ( type_ref_sub ( t ) ) ;
1292
		if ( ci & cinfo_union ) {
1293
		    /* Unions can't have reference members */
1294
		    report ( crt_loc, ERR_class_union_ref ( ct, t ) ) ;
1295
		}
1296
		ci = check_member_type ( ct, ci, s, 1 ) ;
1297
		ci &= ~cinfo_pod ;
1298
	    }
1299
	    break ;
1300
	}
1301
	case type_ptr_mem_tag : {
1302
	    /* Pointer to member types */
1303
	    if ( !ptr ) ci &= ~cinfo_pod ;
1304
	    break ;
1305
	}
1306
	case type_array_tag : {
1307
	    /* Array types */
1308
	    TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
1309
	    ci = check_member_type ( ct, ci, s, ptr ) ;
1310
	    break ;
1311
	}
1312
    }
1313
    if ( is_templ_depend ( t ) ) ci |= cinfo_params ;
1314
    return ( ci ) ;
1315
}
1316
 
1317
 
1318
/*
1319
    REPORT INFORMATION ABOUT A CLASS
1320
 
1321
    This routine reports the information about the class ct indicated by
1322
    the class information mask cm.  It returns an error message summarising
1323
    those properties which hold for ct.  At most n properties are reported.
1324
*/
1325
 
1326
ERROR class_info
1327
    PROTO_N ( ( ct, cm, n ) )
1328
    PROTO_T ( CLASS_TYPE ct X CLASS_INFO cm X int n )
1329
{
1330
    /* Find class information */
1331
    ERROR err = NULL_err ;
1332
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1333
    ci &= cm ;
1334
    if ( ci == cinfo_none ) return ( err ) ;
1335
 
1336
    /* Check for tokenised types */
1337
    if ( ci & cinfo_token ) {
1338
	IDENTIFIER id = DEREF_id ( ctype_name ( ct ) ) ;
1339
	err = concat_error ( err, ERR_token_info ( id ) ) ;
1340
	if ( --n == 0 ) return ( err ) ;
1341
    }
1342
 
1343
    /* Check for user-defined constructors */
1344
    if ( ci & cinfo_usr_constr ) {
1345
	err = concat_error ( err, ERR_class_ctor_user ( ct ) ) ;
1346
	if ( --n == 0 ) return ( err ) ;
1347
    }
1348
 
1349
    /* Check for ambiguous base classes */
1350
    if ( ci & cinfo_ambiguous ) {
1351
	GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
1352
	gr = find_ambig_base ( gr ) ;
1353
	if ( !IS_NULL_graph ( gr ) ) {
1354
	    /* Ambiguous base class found */
1355
	    CLASS_TYPE cs = DEREF_ctype ( graph_head ( gr ) ) ;
1356
	    ERROR err2 = ERR_class_member_lookup_ambig ( cs, ct ) ;
1357
	    err = concat_error ( err, err2 ) ;
1358
	    if ( --n == 0 ) return ( err ) ;
1359
	}
1360
    }
1361
 
1362
    /* Check for base classes */
1363
    if ( ci & cinfo_base ) {
1364
	CLASS_TYPE cs ;
1365
	GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
1366
	LIST ( GRAPH ) tails = DEREF_list ( graph_tails ( gr ) ) ;
1367
	gr = DEREF_graph ( HEAD_list ( tails ) ) ;
1368
	cs = DEREF_ctype ( graph_head ( gr ) ) ;
1369
	err = concat_error ( err, ERR_class_derived_base ( ct, cs ) ) ;
1370
	if ( --n == 0 ) return ( err ) ;
1371
    }
1372
 
1373
    /* Check for private or protected members */
1374
    if ( ci & cinfo_private ) {
1375
	err = concat_error ( err, ERR_class_access_spec_priv ( ct ) ) ;
1376
	if ( --n == 0 ) return ( err ) ;
1377
    }
1378
 
1379
    /* Check for abstract classes */
1380
    if ( ci & cinfo_abstract ) {
1381
	IDENTIFIER id = find_pure_function ( ct ) ;
1382
	err = concat_error ( err, ERR_class_abstract_pure ( id ) ) ;
1383
	err = concat_error ( err, ERR_class_abstract_class ( ct ) ) ;
1384
	if ( --n == 0 ) return ( err ) ;
1385
    }
1386
 
1387
    /* Check for polymorphic classes */
1388
    if ( ci & cinfo_polymorphic ) {
1389
	err = concat_error ( err, ERR_class_virtual_poly ( ct ) ) ;
1390
	if ( --n == 0 ) return ( err ) ;
1391
    }
1392
 
1393
    /* Return the resultant error */
1394
    UNUSED ( n ) ;
1395
    return ( err ) ;
1396
}
1397
 
1398
 
1399
/*
1400
    DOES A CLASS TYPE HAVE A NON-TRIVIAL MEMBER?
1401
 
1402
    This routine checks whether the class type ct has a non-trivial member,
1403
    that is to say not all its base classes and members are empty classes.
1404
*/
1405
 
1406
static int is_empty_ctype
1407
    PROTO_N ( ( ct ) )
1408
    PROTO_T ( CLASS_TYPE ct )
1409
{
1410
    GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
1411
    LIST ( GRAPH ) br = DEREF_list ( graph_tails ( gr ) ) ;
1412
    NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
1413
    MEMBER mem = DEREF_member ( nspace_ctype_first ( ns ) ) ;
1414
    mem = next_data_member ( mem, 2 ) ;
1415
    if ( !IS_NULL_member ( mem ) ) return ( 0 ) ;
1416
    while ( !IS_NULL_list ( br ) ) {
1417
	GRAPH gs = DEREF_graph ( HEAD_list ( br ) ) ;
1418
	CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
1419
	if ( !is_empty_ctype ( cs ) ) return ( 0 ) ;
1420
	br = TAIL_list ( br ) ;
1421
    }
1422
    return ( 1 ) ;
1423
}
1424
 
1425
 
1426
/*
1427
    IS A TYPE A TRIVIAL CLASS TYPE?
1428
 
1429
    This routine checks whether the type t is an empty class type or an
1430
    array of such.
1431
*/
1432
 
1433
int is_empty_class
1434
    PROTO_N ( ( t ) )
1435
    PROTO_T ( TYPE t )
1436
{
1437
    while ( !IS_NULL_type ( t ) ) {
1438
	switch ( TAG_type ( t ) ) {
1439
	    case type_compound_tag : {
1440
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1441
		return ( is_empty_ctype ( ct ) ) ;
1442
	    }
1443
	    case type_array_tag : {
1444
		t = DEREF_type ( type_array_sub ( t ) ) ;
1445
		break ;
1446
	    }
1447
	    case type_templ_tag : {
1448
		t = DEREF_type ( type_templ_defn ( t ) ) ;
1449
		break ;
1450
	    }
1451
	    default : {
1452
		return ( 0 ) ;
1453
	    }
1454
	}
1455
    }
1456
    return ( 0 ) ;
1457
}
1458
 
1459
 
1460
/*
1461
    BEGIN AN ENUMERATION DEFINITION
1462
 
1463
    This routine begins the definition on an enumeration type.  The
1464
    enumeration name is given by id and q gives any template type qualifiers.
1465
    Note that an enumeration definition does not define a scope.
1466
*/
1467
 
1468
IDENTIFIER begin_enum_defn
1469
    PROTO_N ( ( id, q ) )
1470
    PROTO_T ( IDENTIFIER id X TYPE q )
1471
{
1472
    TYPE t ;
1473
    ERROR err ;
1474
    DECL_SPEC ds ;
1475
    NAMESPACE ns ;
1476
    ENUM_TYPE et ;
1477
    CLASS_INFO ei ;
1478
    IDENTIFIER eid = id ;
1479
    QUALIFIER cq = crt_id_qualifier ;
1480
    HASHID nm = DEREF_hashid ( id_name ( eid ) ) ;
1481
    begin_declarator ( id, cq, qual_namespace, 1 ) ;
1482
 
1483
    /* Check on enumeration name */
1484
    if ( is_templ_decl ( id, NULL_type ) || is_templ_spec ( q ) ) {
1485
	/* Shouldn't have a template application */
1486
	q = bind_specialise ( &eid, q, dspec_none, 1, 1, 1 ) ;
1487
	if ( IS_NULL_id ( eid ) || check_key ( eid, btype_enum ) ) {
1488
	    nm = lookup_anon () ;
1489
	    eid = DEREF_id ( hashid_id ( nm ) ) ;
1490
	    cq = qual_none ;
1491
	}
1492
    }
1493
    err = check_id_name ( eid, CONTEXT_ENUM ) ;
1494
    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1495
 
1496
    /* Check for qualified identifiers */
1497
    if ( cq == qual_none ) {
1498
	/* Simple enumeration name */
1499
	if ( option ( OPT_class_scope ) ) {
1500
	    ns = crt_namespace ;
1501
	    if ( IS_nspace_param ( ns ) ) ns = nonclass_namespace ;
1502
	} else {
1503
	    ns = nonclass_namespace ;
1504
	}
1505
    } else {
1506
	/* Definition of nested enumeration */
1507
	ns = DEREF_nspace ( id_parent ( eid ) ) ;
1508
	check_decl_nspace ( eid, ns, 1, crt_namespace ) ;
1509
	if ( IS_id_undef ( eid ) ) nm = lookup_anon () ;
1510
    }
1511
 
1512
    /* Declare the enumeration */
1513
    eid = declare_type ( ns, nm, btype_enum, q, 1, 1 ) ;
1514
    ds = DEREF_dspec ( id_storage ( eid ) ) ;
1515
    COPY_dspec ( id_storage ( eid ), ( ds | dspec_defn ) ) ;
1516
    t = DEREF_type ( id_enum_name_defn ( eid ) ) ;
1517
    et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
1518
 
1519
    /* Mark start of definition */
1520
    ei = DEREF_cinfo ( etype_info ( et ) ) ;
1521
    ei |= cinfo_defined ;
1522
    ei &= ~cinfo_complete ;
1523
    COPY_cinfo ( etype_info ( et ), ei ) ;
1524
    if ( do_dump ) dump_declare ( eid, &decl_loc, 1 ) ;
1525
 
1526
    /* Force definition information */
1527
    COPY_loc ( id_loc ( eid ), decl_loc ) ;
1528
    return ( eid ) ;
1529
}
1530
 
1531
 
1532
/*
1533
    END AN ENUMERATION DEFINITION
1534
 
1535
    This routine completes the definition of the enumeration type id.
1536
    Note that only at this stage do the enumerators acquire the type
1537
    of the enumeration in C++.
1538
*/
1539
 
1540
IDENTIFIER end_enum_defn
1541
    PROTO_N ( ( id ) )
1542
    PROTO_T ( IDENTIFIER id )
1543
{
1544
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1545
    TYPE t = DEREF_type ( id_enum_name_defn ( id ) ) ;
1546
    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
1547
    CLASS_INFO ei = DEREF_cinfo ( etype_info ( et ) ) ;
1548
 
1549
    /* Sort enumerators */
1550
    LIST ( IDENTIFIER ) ens = DEREF_list ( etype_values ( et ) ) ;
1551
    unsigned nenums = LENGTH_list ( ens ) ;
1552
    if ( nenums == 0 ) report ( crt_loc, ERR_dcl_enum_none () ) ;
1553
    IGNORE check_value ( OPT_VAL_enum_consts, ( ulong ) nenums ) ;
1554
    ens = REVERSE_list ( ens ) ;
1555
    COPY_list ( etype_values ( et ), ens ) ;
1556
 
1557
#if LANGUAGE_CPP
1558
    /* Convert enumerators to enumeration type */
1559
    while ( !IS_NULL_list ( ens ) ) {
1560
	IDENTIFIER eid = DEREF_id ( HEAD_list ( ens ) ) ;
1561
	EXP e = DEREF_exp ( id_enumerator_value ( eid ) ) ;
1562
	e = eval_exp ( e, 0 ) ;
1563
	e = cast_exp ( t, e, KILL_err, CAST_STATIC ) ;
1564
	if ( IS_exp_int_lit ( e ) ) {
1565
	    COPY_unsigned ( exp_int_lit_etag ( e ), exp_identifier_tag ) ;
1566
	    if ( is_zero_exp ( e ) ) ei |= cinfo_usr_constr ;
1567
	    COPY_exp ( id_enumerator_value ( eid ), e ) ;
1568
	}
1569
	ens = TAIL_list ( ens ) ;
1570
    }
1571
#endif
1572
 
1573
    /* Mark end of definition */
1574
    ei |= cinfo_complete ;
1575
    COPY_cinfo ( etype_info ( et ), ei ) ;
1576
    COPY_exp ( etype_value ( et ), NULL_exp ) ;
1577
    COPY_ulong ( etype_plus ( et ), 0 ) ;
1578
    if ( do_dump ) dump_undefine ( id, &crt_loc, 0 ) ;
1579
    end_declarator ( id, 1 ) ;
1580
 
1581
    /* Set type declaration flag */
1582
    if ( IS_hashid_anon ( nm ) && nenums == 0 ) {
1583
	have_type_declaration = TYPE_DECL_ANON ;
1584
    } else {
1585
	have_type_declaration = TYPE_DECL_NORMAL ;
1586
    }
1587
    return ( id ) ;
1588
}
1589
 
1590
 
1591
/*
1592
    LARGEST SIMPLE ENUMERATOR VALUE
1593
 
1594
    The underlying type of an enumeration can be int, unsigned int, long
1595
    or unsigned long, depending on the values of the enumerators.  This
1596
    macro gives the maximum value which is guaranteed to fit into all of
1597
    these types.  Because unsigned types are involved, the minimum value
1598
    is 0.
1599
*/
1600
 
1601
#define ENUM_MAX		( ( unsigned long ) 0x7fff )
1602
 
1603
 
1604
/*
1605
    DECLARE AN ENUMERATOR
1606
 
1607
    This routine declares an enumerator named id belonging to the enumeration
1608
    type indicated by eid.  The value of the enumerator is given by the
1609
    integer constant expression val, if this is present, or one more than
1610
    the previous enumerator otherwise.  In C the value is converted to
1611
    'int'.  In C++ the value is converted to the enumeration type, but
1612
    this is only done at the end of the enumeration definition.
1613
*/
1614
 
1615
IDENTIFIER make_enumerator
1616
    PROTO_N ( ( eid, id, val ) )
1617
    PROTO_T ( IDENTIFIER eid X IDENTIFIER id X EXP val )
1618
{
1619
    NAT n ;
1620
    EXP e ;
1621
    ERROR err ;
1622
    int record = 0 ;
1623
    unsigned long v ;
1624
    DECL_SPEC ds = ( crt_access | dspec_defn | dspec_lang ) ;
1625
 
1626
    /* Find the current enumeration type */
1627
    TYPE t = DEREF_type ( id_enum_name_defn ( eid ) ) ;
1628
    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
1629
    CLASS_INFO ei = DEREF_cinfo ( etype_info ( et ) ) ;
1630
    LIST ( IDENTIFIER ) ens = DEREF_list ( etype_values ( et ) ) ;
1631
    NAMESPACE ns = DEREF_nspace ( id_parent ( eid ) ) ;
1632
 
1633
    /* Look up the enumerator name */
1634
    IDENTIFIER nid = id ;
1635
    HASHID nm = DEREF_hashid ( id_name ( nid ) ) ;
1636
    MEMBER mem = search_member ( ns, nm, 1 ) ;
1637
 
1638
    /* Check on enumeration name */
1639
    err = check_id_name ( nid, CONTEXT_ENUMERATOR ) ;
1640
    if ( !IS_NULL_err ( err ) ) {
1641
	report ( decl_loc, err ) ;
1642
	err = NULL_err ;
1643
    }
1644
 
1645
    /* Check for redeclarations */
1646
    nid = DEREF_id ( member_id ( mem ) ) ;
1647
    if ( !IS_NULL_id ( nid ) ) {
1648
	nid = redecl_inherit ( nid, crt_id_qualifier, in_class_defn, 0 ) ;
1649
	if ( !IS_NULL_id ( nid ) ) {
1650
	    IGNORE redecl_id ( dspec_reserve, t, nid, 0, 0 ) ;
1651
	}
1652
    }
1653
 
1654
    /* Declare the enumerator */
1655
    MAKE_id_enumerator ( nm, ds, ns, decl_loc, t, val, nid ) ;
1656
    if ( do_dump ) dump_declare ( nid, &decl_loc, 1 ) ;
1657
 
1658
    /* Find the enumerator value */
1659
    t = DEREF_type ( etype_rep ( et ) ) ;
1660
    if ( !IS_NULL_exp ( val ) ) {
1661
	/* Check that any given val is a constant */
1662
	TYPE s = DEREF_type ( exp_type ( val ) ) ;
1663
#if LANGUAGE_CPP
1664
	s = promote_type ( s ) ;
1665
	val = convert_promote ( s, val ) ;
1666
#else
1667
	switch ( TAG_type ( s ) ) {
1668
	    case type_integer_tag :
1669
	    case type_bitfield_tag :
1670
	    case type_enumerate_tag : {
1671
		/* Convert integral values to 'int' */
1672
		s = t ;
1673
		val = cast_int_int ( s, val, &err, CAST_IMPLICIT, -1 ) ;
1674
		if ( !IS_NULL_err ( err ) ) {
1675
		    report ( crt_loc, err ) ;
1676
		    err = NULL_err ;
1677
		}
1678
		break ;
1679
	    }
1680
	}
1681
#endif
1682
	n = make_nat_exp ( val, &err ) ;
1683
	if ( !IS_NULL_err ( err ) ) {
1684
	    /* Value is not an integral constant */
1685
	    err = concat_error ( err, ERR_dcl_enum_const ( nid ) ) ;
1686
	    report ( crt_loc, err ) ;
1687
	    s = t ;
1688
	}
1689
	v = get_nat_value ( n ) ;
1690
	if ( v <= ENUM_MAX ) {
1691
	    /* Small values */
1692
	    if ( !EQ_type ( t, s ) ) {
1693
		/* Mark if enumerator has different type */
1694
		ei |= cinfo_polymorphic ;
1695
		t = s ;
1696
	    }
1697
	    if ( v == 0 ) ei |= cinfo_usr_constr ;
1698
	    COPY_exp ( etype_value ( et ), NULL_exp ) ;
1699
	} else {
1700
	    /* Large values */
1701
	    EXP cond = crt_hash_cond ;
1702
	    if ( !EQ_type ( t, s ) ) {
1703
		/* Extend type range if necessary */
1704
		t = arith_type ( t, s, NULL_exp, NULL_exp ) ;
1705
		COPY_type ( etype_rep ( et ), t ) ;
1706
		ei |= cinfo_polymorphic ;
1707
		t = s ;
1708
	    }
1709
	    if ( !IS_NULL_exp ( cond ) && is_calc_nat ( n ) ) {
1710
		/* Propagate target dependent conditionals */
1711
		EXP alt = make_null_exp ( t ) ;
1712
		MAKE_exp_hash_if ( t, cond, val, alt, val ) ;
1713
		MAKE_nat_calc ( val, n ) ;
1714
	    }
1715
	    record = 1 ;
1716
	    v = 0 ;
1717
	}
1718
    } else {
1719
	/* Other enumerators are one more than the previous one */
1720
	v = DEREF_ulong ( etype_plus ( et ) ) ;
1721
	n = make_nat_value ( v ) ;
1722
	e = DEREF_exp ( etype_value ( et ) ) ;
1723
	if ( IS_NULL_exp ( e ) ) {
1724
	    if ( ei & cinfo_polymorphic ) {
1725
		/* Use type of last enumerator, if different */
1726
		IDENTIFIER pid = DEREF_id ( HEAD_list ( ens ) ) ;
1727
		e = DEREF_exp ( id_enumerator_value ( pid ) ) ;
1728
		t = DEREF_type ( exp_type ( e ) ) ;
1729
	    }
1730
	    if ( v == 0 ) ei |= cinfo_usr_constr ;
1731
	} else {
1732
	    EXP c ;
1733
	    t = DEREF_type ( exp_type ( e ) ) ;
1734
	    MAKE_exp_int_lit ( t, n, exp_int_lit_tag, c ) ;
1735
	    MAKE_exp_plus ( t, e, c, e ) ;
1736
	    MAKE_nat_calc ( e, n ) ;
1737
	}
1738
    }
1739
    MAKE_exp_int_lit ( t, n, exp_identifier_tag, e ) ;
1740
    COPY_exp ( id_enumerator_value ( nid ), e ) ;
1741
 
1742
    /* Increment enumerator counter */
1743
    if ( v >= ENUM_MAX ) {
1744
	record = 1 ;
1745
	v = 0 ;
1746
    }
1747
    if ( record ) COPY_exp ( etype_value ( et ), e ) ;
1748
    COPY_ulong ( etype_plus ( et ), v + 1 ) ;
1749
 
1750
    /* Add enumerator to the enumerator list */
1751
    set_member ( mem, nid ) ;
1752
    CONS_id ( nid, ens, ens ) ;
1753
    COPY_list ( etype_values ( et ), ens ) ;
1754
    COPY_cinfo ( etype_info ( et ), ei ) ;
1755
    return ( nid ) ;
1756
}
1757
 
1758
 
1759
/*
1760
    DOES A VALUE APPEAR AS AN ENUMERATOR?
1761
 
1762
    This routine checks whether the integer constant n is a valid enumerator
1763
    for the enumeration et.  If so it returns the enumerator identifier.
1764
*/
1765
 
1766
IDENTIFIER find_enumerator
1767
    PROTO_N ( ( et, n ) )
1768
    PROTO_T ( ENUM_TYPE et X NAT n )
1769
{
1770
    LIST ( IDENTIFIER ) p = DEREF_list ( etype_values ( et ) ) ;
1771
    while ( !IS_NULL_list ( p ) ) {
1772
	IDENTIFIER eid = DEREF_id ( HEAD_list ( p ) ) ;
1773
	EXP e = DEREF_exp ( id_enumerator_value ( eid ) ) ;
1774
	NAT m = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
1775
	if ( EQ_nat ( n, m ) || eq_nat ( n, m ) ) return ( eid ) ;
1776
	p = TAIL_list ( p ) ;
1777
    }
1778
    return ( NULL_id ) ;
1779
}
1780
 
1781
 
1782
/*
1783
    ELABORATE TYPE SPECIFIER FLAG
1784
 
1785
    This flag is set by find_elaborate_type and declare_type to indicate
1786
    that the given type specifier referred to a pre-existing type.
1787
*/
1788
 
1789
int found_elaborate_type = 0 ;
1790
 
1791
 
1792
/*
1793
    FIND AN ELABORATED TYPE SPECIFIER
1794
 
1795
    This routine finds the type identifier corresponding to the elaborated
1796
    type specifier with identifier id, class key key (which can be
1797
    btype_class, btype_struct, btype_union or btype_enum) and template
1798
    type qualifiers q.  The argument mode gives information on the
1799
    context for the type specifier:
1800
 
1801
	dspec_defn	for explicit declarations,
1802
	dspec_friend	for friend declarations,
1803
	dspec_alias	for qualified identifiers,
1804
	dspec_auto	if type is to be declared in current namespace,
1805
	dspec_used	if type is to be searched for.
1806
*/
1807
 
1808
IDENTIFIER find_elaborate_type
1809
    PROTO_N ( ( id, key, q, mode ) )
1810
    PROTO_T ( IDENTIFIER id X BASE_TYPE key X TYPE q X DECL_SPEC mode )
1811
{
1812
    ERROR err ;
1813
    LOCATION loc ;
1814
    NAMESPACE ns ;
1815
    IDENTIFIER tid ;
1816
    int templ = is_templ_spec ( q ) ;
1817
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1818
 
1819
    /* Check for template applications */
1820
    if ( templ || is_templ_decl ( id, NULL_type ) ) {
1821
	tid = id ;
1822
	if ( mode & dspec_defn ) {
1823
	    /* Bind template specialisations */
1824
	    DECL_SPEC ds = ( mode & dspec_friend ) ;
1825
	    q = bind_specialise ( &tid, q, ds, 1, 1, 0 ) ;
1826
	}
1827
	if ( !IS_NULL_id ( tid ) && IS_id_class_name_etc ( tid ) ) {
1828
	    IGNORE check_key ( tid, key ) ;
1829
	    if ( templ ) {
1830
		/* Check namespace for explicit declaration */
1831
		ns = DEREF_nspace ( id_parent ( tid ) ) ;
1832
		check_decl_nspace ( tid, ns, 0, crt_namespace ) ;
1833
	    }
1834
	    found_elaborate_type = 1 ;
1835
	    return ( tid ) ;
1836
	}
1837
    } else {
1838
	if ( mode & dspec_alias ) {
1839
	    /* Qualified identifier */
1840
	    NAMESPACE tns = DEREF_nspace ( id_parent ( id ) ) ;
1841
	    tid = find_qual_id ( tns, nm, 0, 1 ) ;
1842
	    if ( IS_NULL_id ( tid ) || !IS_id_class_name_etc ( tid ) ) {
1843
		if ( !IS_NULL_id ( tid ) && IS_id_ambig ( tid ) ) {
1844
		    tid = report_ambiguous ( tid, 1, 1, 1 ) ;
1845
		} else {
1846
		    report ( crt_loc, ERR_dcl_type_simple_undef ( id ) ) ;
1847
		    tid = NULL_id ;
1848
		    q = NULL_type ;
1849
		}
1850
	    }
1851
	} else if ( mode & dspec_register ) {
1852
	    /* Search enclosing namespace */
1853
	    tid = find_extern_id ( nm, nonclass_namespace, 1 ) ;
1854
	} else if ( mode & dspec_used ) {
1855
	    /* Search for enclosing declaration */
1856
	    tid = find_type_id ( nm, 1 ) ;
1857
	} else {
1858
	    /* Force a new declaration */
1859
	    tid = NULL_id ;
1860
	}
1861
    }
1862
 
1863
    /* Check previous declaration */
1864
    while ( !IS_NULL_id ( tid ) ) {
1865
	if ( IS_id_class_name_etc ( tid ) ) {
1866
	    /* Previous declaration was a type name */
1867
	    BASE_TYPE prev = find_key ( tid, 0, key ) ;
1868
	    if ( prev == btype_alias ) {
1869
		/* This was a typedef name */
1870
		err = ERR_lookup_elab_alias ( key, tid ) ;
1871
		report ( crt_loc, err ) ;
1872
	    } else if ( !equal_key ( prev, key ) ) {
1873
		/* The class keys don't match */
1874
		PTR ( LOCATION ) ploc = id_loc ( tid ) ;
1875
		err = ERR_dcl_type_elab_bad ( key, prev, tid, ploc ) ;
1876
		report ( crt_loc, err ) ;
1877
	    }
1878
	    found_elaborate_type = 1 ;
1879
	    return ( tid ) ;
1880
	}
1881
	if ( IS_id_ambig ( tid ) ) {
1882
	    tid = report_ambiguous ( tid, 1, 1, 1 ) ;
1883
	} else {
1884
	    report ( crt_loc, ERR_lookup_elab_invalid ( key, tid ) ) ;
1885
	    tid = NULL_id ;
1886
	}
1887
    }
1888
 
1889
    /* Find namespace for declared type */
1890
    if ( mode & dspec_alias ) {
1891
	/* Declare in parent namespace */
1892
	ns = DEREF_nspace ( id_parent ( id ) ) ;
1893
    } else if ( ( mode & dspec_auto ) && option ( OPT_class_scope ) ) {
1894
	/* Declare in the current namespace */
1895
	ns = crt_namespace ;
1896
    } else if ( mode & dspec_template ) {
1897
	/* Declare in template namespace */
1898
	ns = templ_namespace ;
1899
    } else {
1900
	/* Declare in enclosing namespace */
1901
	ns = nonclass_namespace ;
1902
    }
1903
 
1904
    /* Declare new type */
1905
    loc = decl_loc ;
1906
    err = check_id_name ( id, CONTEXT_CLASS ) ;
1907
    id = underlying_id ( id ) ;
1908
    DEREF_loc ( id_loc ( id ), decl_loc ) ;
1909
    if ( !IS_NULL_err ( err ) ) report ( decl_loc, err ) ;
1910
    tid = declare_type ( ns, nm, key, q, 0, 1 ) ;
1911
    if ( do_dump ) dump_declare ( tid, &decl_loc, 0 ) ;
1912
    if ( key == btype_enum ) {
1913
	/* Can't declare enumerations (but have done) */
1914
	report ( decl_loc, ERR_lookup_elab_enum ( tid ) ) ;
1915
    }
1916
    if ( mode & dspec_friend ) {
1917
	/* Warn about potential friend problems */
1918
	report ( decl_loc, ERR_class_friend_pre ( tid ) ) ;
1919
    }
1920
    decl_loc = loc ;
1921
    return ( tid ) ;
1922
}
1923
 
1924
 
1925
/*
1926
    CREATE A TYPE ALIAS
1927
 
1928
    This routine creates a typedef with name nm and definition t
1929
    in the namespace ns (but doesn't bring it into scope).  Note that
1930
    typedefs are split into class aliases, enumeration aliases and other
1931
    type aliases.  In the former two cases the class identifier is
1932
    associated with t as the type name, and the member namespace name for
1933
    classes, if this has not yet been defined.
1934
*/
1935
 
1936
IDENTIFIER make_typedef
1937
    PROTO_N ( ( ns, nm, t, ds ) )
1938
    PROTO_T ( NAMESPACE ns X HASHID nm X TYPE t X DECL_SPEC ds )
1939
{
1940
    TYPE s ;
1941
    unsigned tag ;
1942
    IDENTIFIER id ;
1943
 
1944
    /* Force copy of type */
1945
    CV_SPEC cv = DEREF_cv ( type_qual ( t ) ) ;
1946
    t = qualify_type ( t, cv, 1 ) ;
1947
    s = t ;
1948
    tag = TAG_type ( s ) ;
1949
 
1950
    /* Check for template types */
1951
    if ( ds == dspec_none ) ds = ( crt_access | dspec_defn ) ;
1952
    while ( tag == type_templ_tag ) {
1953
	s = DEREF_type ( type_templ_defn ( s ) ) ;
1954
	tag = TAG_type ( s ) ;
1955
	ds |= dspec_template ;
1956
    }
1957
 
1958
    /* Check for class aliases */
1959
    if ( tag == type_compound_tag ) {
1960
	CV_SPEC qual = DEREF_cv ( type_qual ( s ) ) ;
1961
	if ( qual == cv_none ) {
1962
	    /* Find the class name */
1963
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( s ) ) ;
1964
	    IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
1965
	    HASHID cnm = DEREF_hashid ( id_name ( cid ) ) ;
1966
 
1967
	    /* Set up the class alias */
1968
	    MAKE_id_class_alias ( nm, ds, ns, decl_loc, t, id ) ;
1969
 
1970
	    if ( IS_hashid_anon ( cnm ) ) {
1971
		/* Set the class name if necessary */
1972
		NAMESPACE mns ;
1973
		if ( do_dump ) {
1974
		    dump_declare ( id, &decl_loc, 1 ) ;
1975
		    dump_anon_class = 1 ;
1976
		}
1977
		COPY_dspec ( id_storage ( id ), ( ds | dspec_lang ) ) ;
1978
		mns = DEREF_nspace ( ctype_member ( ct ) ) ;
1979
		COPY_id ( nspace_name ( mns ), id ) ;
1980
		COPY_id ( ctype_name ( ct ), id ) ;
1981
 
1982
		/* Also set constructor and destructor names */
1983
		cid = DEREF_id ( ctype_constr ( ct ) ) ;
1984
		cnm = DEREF_hashid ( id_name ( cid ) ) ;
1985
		COPY_id ( hashid_constr_tid ( cnm ), id ) ;
1986
		cid = DEREF_id ( ctype_destr ( ct ) ) ;
1987
		cnm = DEREF_hashid ( id_name ( cid ) ) ;
1988
		COPY_id ( hashid_destr_tid ( cnm ), id ) ;
1989
	    }
1990
	    return ( id ) ;
1991
	}
1992
    }
1993
 
1994
    /* Check for enumeration aliases */
1995
    if ( tag == type_enumerate_tag ) {
1996
	CV_SPEC qual = DEREF_cv ( type_qual ( s ) ) ;
1997
	if ( qual == cv_none ) {
1998
	    /* Find the enumeration name */
1999
	    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( s ) ) ;
2000
	    IDENTIFIER eid = DEREF_id ( etype_name ( et ) ) ;
2001
	    HASHID enm = DEREF_hashid ( id_name ( eid ) ) ;
2002
 
2003
	    /* Set up the enumeration alias */
2004
	    MAKE_id_enum_alias ( nm, ds, ns, decl_loc, t, id ) ;
2005
 
2006
	    /* Set the enumeration name if necessary */
2007
	    if ( IS_hashid_anon ( enm ) ) {
2008
		if ( do_dump ) {
2009
		    dump_declare ( id, &decl_loc, 1 ) ;
2010
		    dump_anon_class = 1 ;
2011
		}
2012
		COPY_dspec ( id_storage ( id ), ( ds | dspec_lang ) ) ;
2013
		COPY_id ( etype_name ( et ), id ) ;
2014
	    }
2015
	    return ( id ) ;
2016
	}
2017
    }
2018
 
2019
    /* Other type aliases */
2020
    MAKE_id_type_alias ( nm, ds, ns, decl_loc, t, id ) ;
2021
    return ( id ) ;
2022
}
2023
 
2024
 
2025
/*
2026
    FIND THE COPIED VERSION OF A CLASS MEMBER
2027
 
2028
    This routine finds the member of the class cid corresponding to the
2029
    class member id.  cid will be a copy of the class containing id.
2030
    Note that if res is false a set of overloaded functions is mapped to
2031
    a set of overloaded functions, further resolutions based on type may be
2032
    performed later.  If res is true overload resolution is performed
2033
    based on whether the result is an instantiation of id.
2034
*/
2035
 
2036
IDENTIFIER find_copied
2037
    PROTO_N ( ( cid, id, res ) )
2038
    PROTO_T ( IDENTIFIER cid X IDENTIFIER id X int res )
2039
{
2040
    HASHID nm ;
2041
    MEMBER mem ;
2042
    NAMESPACE ns ;
2043
    CLASS_TYPE ct ;
2044
    IDENTIFIER mid ;
2045
 
2046
    /* Find class namespace */
2047
    TYPE t = DEREF_type ( id_class_name_etc_defn ( cid ) ) ;
2048
    while ( IS_type_templ ( t ) ) {
2049
	t = DEREF_type ( type_templ_defn ( t ) ) ;
2050
    }
2051
    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2052
    complete_class ( ct, 1 ) ;
2053
    ns = DEREF_nspace ( ctype_member ( ct ) ) ;
2054
 
2055
    /* Look up name in namespace */
2056
    nm = DEREF_hashid ( id_name ( id ) ) ;
2057
    nm = expand_name ( nm, ct ) ;
2058
    do {
2059
	mem = search_member ( ns, nm, 0 ) ;
2060
	if ( !IS_NULL_member ( mem ) ) break ;
2061
	nm = next_expand_name ( nm ) ;
2062
    } while ( !IS_NULL_hashid ( nm ) ) ;
2063
 
2064
    /* Check for corresponding identifier */
2065
    if ( !IS_NULL_member ( mem ) ) {
2066
	mid = DEREF_id ( member_id ( mem ) ) ;
2067
	if ( !IS_NULL_id ( mid ) ) {
2068
	    /* Identifier matches member */
2069
	    if ( IS_id_function_etc ( mid ) && IS_id_function_etc ( id ) ) {
2070
		/* Matching functions */
2071
		if ( res ) {
2072
		    IDENTIFIER fid = NULL_id ;
2073
		    TYPE f = DEREF_type ( id_function_etc_form ( id ) ) ;
2074
		    if ( !IS_NULL_type ( f ) && IS_type_instance ( f ) ) {
2075
			fid = DEREF_id ( type_instance_id ( f ) ) ;
2076
		    }
2077
		    while ( !IS_NULL_id ( mid ) ) {
2078
			/* Perform overload resolution */
2079
			if ( EQ_id ( mid, id ) ) return ( mid ) ;
2080
			if ( EQ_id ( mid, fid ) ) return ( mid ) ;
2081
			f = DEREF_type ( id_function_etc_form ( mid ) ) ;
2082
			if ( !IS_NULL_type ( f ) && IS_type_instance ( f ) ) {
2083
			    IDENTIFIER nid ;
2084
			    nid = DEREF_id ( type_instance_id ( f ) ) ;
2085
			    if ( EQ_id ( nid, id ) ) return ( mid ) ;
2086
			    if ( EQ_id ( nid, fid ) ) return ( mid ) ;
2087
			}
2088
			mid = DEREF_id ( id_function_etc_over ( mid ) ) ;
2089
		    }
2090
		    mid = id ;
2091
		}
2092
		return ( mid ) ;
2093
	    }
2094
	    if ( TAG_id ( mid ) == TAG_id ( id ) ) return ( mid ) ;
2095
	}
2096
	mid = DEREF_id ( member_alt ( mem ) ) ;
2097
	if ( !IS_NULL_id ( mid ) && TAG_id ( mid ) == TAG_id ( id ) ) {
2098
	    /* Identifier matches type member */
2099
	    return ( mid ) ;
2100
	}
2101
    }
2102
    return ( id ) ;
2103
}
2104
 
2105
 
2106
/*
2107
    COPY A CLASS TYPE
2108
 
2109
    This routine creates a new class which is a copy of t.  This is used
2110
    in the instantiation of template classes.  Note that the members of
2111
    the class are copied later by copy_members.
2112
*/
2113
 
2114
TYPE copy_class
2115
    PROTO_N ( ( t, ds ) )
2116
    PROTO_T ( TYPE t X DECL_SPEC ds )
2117
{
2118
    LOCATION loc ;
2119
    IDENTIFIER tid ;
2120
    int r = really_in_class_defn ;
2121
    int f = really_in_function_defn ;
2122
    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2123
    BASE_TYPE key = find_class_key ( ct ) ;
2124
    IDENTIFIER id = DEREF_id ( ctype_name ( ct ) ) ;
2125
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2126
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2127
    DECL_SPEC acc = DEREF_dspec ( id_storage ( id ) ) ;
2128
    ds |= ( acc & dspec_access ) ;
2129
    loc = crt_loc ;
2130
    bad_crt_loc++ ;
2131
    really_in_class_defn = 0 ;
2132
    really_in_function_defn = 0 ;
2133
    DEREF_loc ( id_loc ( id ), crt_loc ) ;
2134
    decl_loc = crt_loc ;
2135
    tid = make_class ( ns, nm, key, ds, NULL_type, NULL_type ) ;
2136
    t = DEREF_type ( id_class_name_defn ( tid ) ) ;
2137
    really_in_function_defn = f ;
2138
    really_in_class_defn = r ;
2139
    bad_crt_loc-- ;
2140
    decl_loc = loc ;
2141
    crt_loc = loc ;
2142
    return ( t ) ;
2143
}
2144
 
2145
 
2146
/*
2147
    COPY A LIST OF TEMPLATE SPECIALISATIONS
2148
 
2149
    This routine copies any partial or explicit specialisations of the
2150
    template member tid of a template class to the corresponding member
2151
    sid of an instance of that template class.
2152
*/
2153
 
2154
static void copy_specs
2155
    PROTO_N ( ( sid, tid, type ) )
2156
    PROTO_T ( IDENTIFIER sid X IDENTIFIER tid X int type )
2157
{
2158
    TYPE s, t ;
2159
    if ( type ) {
2160
	/* Template classes */
2161
	s = DEREF_type ( id_class_name_etc_defn ( sid ) ) ;
2162
	t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
2163
    } else {
2164
	/* Template functions */
2165
	s = DEREF_type ( id_function_etc_type ( sid ) ) ;
2166
	t = DEREF_type ( id_function_etc_type ( tid ) ) ;
2167
    }
2168
    if ( IS_type_templ ( s ) && IS_type_templ ( t ) ) {
2169
	TOKEN ps = DEREF_tok ( type_templ_sort ( s ) ) ;
2170
	TOKEN pt = DEREF_tok ( type_templ_sort ( t ) ) ;
2171
	INSTANCE as = DEREF_inst ( tok_templ_apps ( ps ) ) ;
2172
	INSTANCE at = DEREF_inst ( tok_templ_apps ( pt ) ) ;
2173
	while ( !IS_NULL_inst ( at ) ) {
2174
	    DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( at ) ) ;
2175
	    if ( !( acc & ( dspec_alias | dspec_main ) ) ) {
2176
		/* NOT YET IMPLEMENTED */
2177
	    }
2178
	    at = DEREF_inst ( inst_next ( at ) ) ;
2179
	}
2180
	COPY_inst ( tok_templ_apps ( ps ), as ) ;
2181
    }
2182
    return ;
2183
}
2184
 
2185
 
2186
/*
2187
    COPY A NESTED CLASS
2188
 
2189
    This routine copies the nested class or enumeration type tid of a
2190
    template class.
2191
*/
2192
 
2193
static IDENTIFIER copy_nested
2194
    PROTO_N ( ( tid, t, q, ploc ) )
2195
    PROTO_T ( IDENTIFIER tid X TYPE t X TYPE q X LOCATION *ploc )
2196
{
2197
    IDENTIFIER id = tid ;
2198
    switch ( TAG_type ( t ) ) {
2199
 
2200
	case type_compound_tag : {
2201
	    /* Non-template classes */
2202
	    TYPE s ;
2203
	    TYPE form ;
2204
	    int def = 0 ;
2205
	    DECL_SPEC ds ;
2206
	    CLASS_TYPE cs ;
2207
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2208
	    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
2209
	    BASE_TYPE key = find_class_key ( ct ) ;
2210
	    IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
2211
	    HASHID nm = DEREF_hashid ( id_name ( cid ) ) ;
2212
	    NAMESPACE ns = crt_namespace ;
2213
 
2214
	    /* Declare class */
2215
	    if ( !IS_id_class_name ( tid ) ) break ;
2216
	    if ( ci & cinfo_complete ) def = 1 ;
2217
	    id = declare_type ( ns, nm, key, q, def, 1 ) ;
2218
	    s = DEREF_type ( id_class_name_defn ( id ) ) ;
2219
	    while ( IS_type_templ ( s ) ) {
2220
		s = DEREF_type ( type_templ_defn ( s ) ) ;
2221
	    }
2222
	    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
2223
	    ds = DEREF_dspec ( id_storage ( id ) ) ;
2224
	    ds |= dspec_instance ;
2225
	    COPY_dspec ( id_storage ( id ), ds ) ;
2226
 
2227
	    /* Set up instance type */
2228
	    MAKE_type_instance ( cv_none, tid, dspec_none, form ) ;
2229
	    COPY_id ( type_name ( form ), id ) ;
2230
	    COPY_type ( ctype_form ( cs ), form ) ;
2231
	    if ( do_dump ) dump_declare ( id, ploc, 0 ) ;
2232
	    /* complete_class ( cs, 1 ) ; */
2233
	    break ;
2234
	}
2235
 
2236
	case type_enumerate_tag : {
2237
	    /* Enumeration type */
2238
	    TYPE form ;
2239
	    DECL_SPEC ds ;
2240
	    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
2241
	    CLASS_INFO ei = DEREF_cinfo ( etype_info ( et ) ) ;
2242
	    TYPE s = DEREF_type ( etype_rep ( et ) ) ;
2243
	    IDENTIFIER eid = DEREF_id ( etype_name ( et ) ) ;
2244
	    HASHID nm = DEREF_hashid ( id_name ( eid ) ) ;
2245
	    NAMESPACE ns = crt_namespace ;
2246
 
2247
	    /* Declare enumeration */
2248
	    if ( !IS_id_enum_name ( tid ) ) break ;
2249
	    id = declare_type ( ns, nm, btype_enum, NULL_type, 1, 1 ) ;
2250
	    ds = DEREF_dspec ( id_storage ( id ) ) ;
2251
	    ds |= dspec_instance ;
2252
	    COPY_dspec ( id_storage ( id ), ds ) ;
2253
	    t = DEREF_type ( id_enum_name_defn ( id ) ) ;
2254
	    et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
2255
	    COPY_cinfo ( etype_info ( et ), ei ) ;
2256
	    s = expand_type ( s, 1 ) ;
2257
	    COPY_type ( etype_rep ( et ), s ) ;
2258
 
2259
	    /* Set up instance type */
2260
	    MAKE_type_instance ( cv_none, tid, dspec_none, form ) ;
2261
	    COPY_id ( type_name ( form ), id ) ;
2262
	    COPY_type ( etype_form ( et ), form ) ;
2263
	    if ( do_dump ) dump_declare ( id, ploc, 0 ) ;
2264
	    break ;
2265
	}
2266
 
2267
	case type_templ_tag : {
2268
	    /* Template classes */
2269
	    TYPE s ;
2270
	    TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
2271
	    sort = expand_templ_sort ( sort, 1 ) ;
2272
	    MAKE_type_templ ( cv_none, sort, NULL_type, 0, s ) ;
2273
	    q = inject_pre_type ( q, s, 0 ) ;
2274
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
2275
	    id = copy_nested ( tid, t, q, ploc ) ;
2276
	    reset_templ_sort ( sort ) ;
2277
	    copy_specs ( id, tid, 1 ) ;
2278
	    break ;
2279
	}
2280
    }
2281
    return ( id ) ;
2282
}
2283
 
2284
 
2285
/*
2286
    COPY A MEMBER OF A CLASS
2287
 
2288
    This routine copies the class member id to the namespace ns, renaming
2289
    it to nm.
2290
*/
2291
 
2292
static IDENTIFIER copy_member
2293
    PROTO_N ( ( id, nm, ns, ct, ploc ) )
2294
    PROTO_T ( IDENTIFIER id X HASHID nm X NAMESPACE ns X CLASS_TYPE ct X
2295
	      LOCATION *ploc )
2296
{
2297
    TYPE form ;
2298
    IDENTIFIER tid = NULL_id ;
2299
    unsigned tag = TAG_id ( id ) ;
2300
 
2301
    /* Check for implicit and inherited members */
2302
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2303
    if ( ds & ( dspec_inherit | dspec_implicit ) ) {
2304
	/* Ignore implicit or inherited members */
2305
	if ( ( ds & dspec_alias ) && ( ds & dspec_inherit ) ) {
2306
	    GRAPH gr ;
2307
	    tid = DEREF_id ( id_alias ( id ) ) ;
2308
	    tid = rescan_member ( tid ) ;
2309
	    gr = is_subfield ( ns, tid ) ;
2310
	    if ( !IS_NULL_graph ( gr ) ) {
2311
		DECL_SPEC acc = crt_access ;
2312
		crt_access = ( ds & dspec_access ) ;
2313
		tid = search_subfield ( ns, gr, tid ) ;
2314
		tid = alias_id ( tid, ns, NULL_id, 0 ) ;
2315
		if ( TAG_id ( tid ) != tag ) tid = NULL_id ;
2316
		crt_access = acc ;
2317
	    } else {
2318
		report ( crt_loc, ERR_dcl_nspace_udecl_base ( tid, ct ) ) ;
2319
		tid = NULL_id ;
2320
	    }
2321
	}
2322
	if ( tag == id_mem_func_tag || tag == id_stat_mem_func_tag ) {
2323
	    IDENTIFIER fid = DEREF_id ( id_function_etc_over ( id ) ) ;
2324
	    if ( !IS_NULL_id ( fid ) ) {
2325
		/* Deal with overloaded functions */
2326
		DEREF_loc ( id_loc ( fid ), crt_loc ) ;
2327
		fid = copy_member ( fid, nm, ns, ct, ploc ) ;
2328
		if ( !IS_NULL_id ( tid ) ) {
2329
		    COPY_id ( id_function_etc_over ( tid ), fid ) ;
2330
		    if ( !IS_NULL_id ( fid ) ) {
2331
			tid = hide_functions ( tid, fid, 1 ) ;
2332
		    }
2333
		} else {
2334
		    tid = fid ;
2335
		}
2336
	    }
2337
	}
2338
	return ( tid ) ;
2339
    }
2340
 
2341
    /* Nested classes and enumerations already copied */
2342
    if ( tag == id_class_name_tag || tag == id_enum_name_tag ) {
2343
	return ( NULL_id ) ;
2344
    }
2345
 
2346
    /* Copy member */
2347
    tid = copy_id ( id, 2 ) ;
2348
    if ( !EQ_id ( tid, id ) ) {
2349
	int def = 0 ;
2350
	int virt = 0 ;
2351
	int templ = 0 ;
2352
	IDENTIFIER fid = NULL_id ;
2353
	CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
2354
	ds = DEREF_dspec ( id_storage ( tid ) ) ;
2355
	COPY_nspace ( id_parent ( tid ), ns ) ;
2356
	COPY_hashid ( id_name ( tid ), nm ) ;
2357
	switch ( tag ) {
2358
 
2359
	    case id_member_tag : {
2360
		/* Non-static data members */
2361
		OFFSET off ;
2362
		DECL_SPEC acc = ( ds & dspec_access ) ;
2363
		TYPE t = DEREF_type ( id_member_type ( tid ) ) ;
2364
		MAKE_off_member ( tid, off ) ;
2365
		COPY_off ( id_member_off ( tid ), off ) ;
2366
		check_mem_decl ( ds, t, tid ) ;
2367
		ci = check_member_type ( ct, ci, t, 0 ) ;
2368
		if ( acc != dspec_public ) ci |= cinfo_private ;
2369
		def = 1 ;
2370
		break ;
2371
	    }
2372
 
2373
	    case id_stat_member_tag : {
2374
		/* Static data members */
2375
		EXP dummy ;
2376
		TYPE t = DEREF_type ( id_stat_member_type ( tid ) ) ;
2377
		check_mem_decl ( ds, t, tid ) ;
2378
		MAKE_type_instance ( cv_none, id, dspec_none, form ) ;
2379
		COPY_id ( type_name ( form ), tid ) ;
2380
		MAKE_exp_paren ( form, NULL_exp, dummy ) ;
2381
		COPY_exp ( id_stat_member_init ( tid ), NULL_exp ) ;
2382
		COPY_exp ( id_stat_member_term ( tid ), dummy ) ;
2383
		ci |= cinfo_static ;
2384
		ds &= ~dspec_defn ;
2385
		break ;
2386
	    }
2387
 
2388
#if LANGUAGE_CPP
2389
	    case id_mem_func_tag :
2390
	    case id_stat_mem_func_tag : {
2391
		/* Member functions */
2392
		LIST ( VIRTUAL ) vt ;
2393
		IDENTIFIER hide_id = NULL_id ;
2394
		unsigned ntag = TAG_hashid ( nm ) ;
2395
		TYPE t = DEREF_type ( id_function_etc_type ( tid ) ) ;
2396
		fid = DEREF_id ( id_function_etc_over ( id ) ) ;
2397
		if ( !IS_NULL_id ( fid ) ) {
2398
		    /* Deal with overloaded functions */
2399
		    DEREF_loc ( id_loc ( fid ), crt_loc ) ;
2400
		    fid = copy_member ( fid, nm, ns, ct, ploc ) ;
2401
		    COPY_id ( id_function_etc_over ( tid ), fid ) ;
2402
		    DEREF_loc ( id_loc ( tid ), crt_loc ) ;
2403
		}
2404
		if ( ntag == hashid_op_tag ) {
2405
		    /* Check operator type */
2406
		    int alloc = 0 ;
2407
		    t = check_operator ( t, tid, 1, &alloc ) ;
2408
		    if ( alloc ) recheck_allocator ( tid, alloc ) ;
2409
		}
2410
		decl_func_type ( tid, t, 0 ) ;
2411
		special_func_mem ( ct, tid, ntag, NULL_id ) ;
2412
		vt = overrides_virtual ( ct, nm, t, &hide_id ) ;
2413
		if ( !IS_NULL_list ( vt ) ) {
2414
		    /* Check for overriding virtual functions */
2415
		    if ( !( ds & dspec_virtual ) ) {
2416
			ERROR err = ERR_class_virtual_override ( nm ) ;
2417
			if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
2418
		    }
2419
		    ds |= dspec_virtual ;
2420
		}
2421
		if ( ds & dspec_virtual ) {
2422
		    /* Deal with virtual functions */
2423
		    add_virtual ( ct, tid, vt ) ;
2424
		    ci |= cinfo_polymorphic ;
2425
		    if ( !( ds & dspec_pure ) ) virt = 1 ;
2426
		}
2427
		MAKE_type_instance ( cv_none, id, dspec_none, form ) ;
2428
		COPY_id ( type_name ( form ), tid ) ;
2429
		COPY_type ( id_function_etc_form ( tid ), form ) ;
2430
		COPY_exp ( id_function_etc_defn ( tid ), NULL_exp ) ;
2431
		if ( IS_type_templ ( t ) ) templ = 1 ;
2432
		ci |= cinfo_function ;
2433
		ds &= ~dspec_defn ;
2434
		break ;
2435
	    }
2436
#endif
2437
 
2438
	    case id_enumerator_tag : {
2439
		/* Enumerators */
2440
		TYPE t = DEREF_type ( id_enumerator_etype ( tid ) ) ;
2441
		if ( IS_type_enumerate ( t ) ) {
2442
		    /* Maintain list of enumerators */
2443
		    LIST ( IDENTIFIER ) p, q ;
2444
		    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
2445
		    p = DEREF_list ( etype_values ( et ) ) ;
2446
		    CONS_id ( tid, NULL_list ( IDENTIFIER ), q ) ;
2447
		    p = APPEND_list ( p, q ) ;
2448
		    COPY_list ( etype_values ( et ), p ) ;
2449
		}
2450
		break ;
2451
	    }
2452
 
2453
	    case id_class_alias_tag :
2454
	    case id_enum_alias_tag :
2455
	    case id_type_alias_tag : {
2456
		/* Typedefs */
2457
		LIST ( IDENTIFIER ) ft = DEREF_list ( ctype_nest ( ct ) ) ;
2458
		CONS_id ( tid, ft, ft ) ;
2459
		COPY_list ( ctype_nest ( ct ), ft ) ;
2460
		break ;
2461
	    }
2462
	}
2463
	ds &= ~( dspec_used | dspec_called | dspec_done ) ;
2464
	ds |= dspec_instance ;
2465
	COPY_dspec ( id_storage ( tid ), ds ) ;
2466
	COPY_cinfo ( ctype_info ( ct ), ci ) ;
2467
	if ( do_dump ) dump_declare ( tid, ploc, def ) ;
2468
	if ( templ ) copy_specs ( tid, id, 0 ) ;
2469
	if ( virt ) define_template ( tid, 0 ) ;
2470
	if ( !IS_NULL_id ( fid ) ) {
2471
	    /* Check overloaded functions */
2472
	    tid = hide_functions ( tid, fid, 1 ) ;
2473
	}
2474
    }
2475
    return ( tid ) ;
2476
}
2477
 
2478
 
2479
/*
2480
    COPY A FRIEND CLASS
2481
 
2482
    This routine copies the friend class id of a template class.  This may
2483
    involve name injection.
2484
*/
2485
 
2486
static IDENTIFIER copy_friend_class
2487
    PROTO_N ( ( id ) )
2488
    PROTO_T ( IDENTIFIER id )
2489
{
2490
    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
2491
    TYPE s = expand_type ( t, 1 ) ;
2492
    t = s ;
2493
    while ( IS_type_templ ( s ) ) {
2494
	s = DEREF_type ( type_templ_defn ( s ) ) ;
2495
    }
2496
    if ( IS_type_compound ( s ) ) {
2497
	CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
2498
	TYPE form = DEREF_type ( ctype_form ( cs ) ) ;
2499
	id = DEREF_id ( ctype_name ( cs ) ) ;
2500
	if ( IS_NULL_type ( form ) ) {
2501
	    /* Allow for name injection */
2502
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2503
	    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2504
	    MEMBER mem = search_member ( ns, nm, 1 ) ;
2505
	    TYPE q = extract_templ_qual ( t ) ;
2506
	    BASE_TYPE key = find_key ( id, 1, btype_none ) ;
2507
	    IDENTIFIER tid = declare_type ( ns, nm, key, q, 0, 0 ) ;
2508
	    IGNORE inject_pre_type ( q, t, 0 ) ;
2509
	    if ( !IS_NULL_id ( tid ) && IS_id_class_name ( tid ) ) {
2510
		/* Allow for redeclarations */
2511
		if ( !EQ_id ( tid, id ) ) {
2512
		    IDENTIFIER pid = DEREF_id ( id_alias ( tid ) ) ;
2513
		    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2514
		    ds |= dspec_alias ;
2515
		    COPY_dspec ( id_storage ( id ), ds ) ;
2516
		    COPY_id ( id_alias ( id ), pid ) ;
2517
		    t = DEREF_type ( id_class_name_defn ( tid ) ) ;
2518
		    COPY_type ( ctype_form ( cs ), t ) ;
2519
		    id = tid ;
2520
		}
2521
	    }
2522
	    if ( !in_template_decl ) set_type_member ( mem, id ) ;
2523
	}
2524
    }
2525
    return ( id ) ;
2526
}
2527
 
2528
 
2529
/*
2530
    COPY A FRIEND FUNCTION
2531
 
2532
    This routine copies the friend function id of a template class.  This
2533
    may involve name injection.
2534
*/
2535
 
2536
static IDENTIFIER copy_friend_func
2537
    PROTO_N ( ( id ) )
2538
    PROTO_T ( IDENTIFIER id )
2539
{
2540
    TYPE form ;
2541
    MEMBER mem ;
2542
    DECL_SPEC ds ;
2543
    IDENTIFIER fid ;
2544
    int changed = 0 ;
2545
    IDENTIFIER over = NULL_id ;
2546
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2547
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2548
    HASHID nm1 = expand_name ( nm, NULL_ctype ) ;
2549
    NAMESPACE ns1 = rescan_nspace ( ns ) ;
2550
    TYPE t = DEREF_type ( id_function_etc_type ( id ) ) ;
2551
    if ( !IS_type_templ ( t ) ) {
2552
	TYPE t1 = expand_type ( t, 1 ) ;
2553
	if ( !EQ_type ( t1, t ) ) {
2554
	    changed = 1 ;
2555
	    t = t1 ;
2556
	}
2557
    }
2558
    if ( !EQ_hashid ( nm1, nm ) ) {
2559
	changed = 1 ;
2560
	nm = nm1 ;
2561
    }
2562
    if ( !EQ_nspace ( ns1, ns ) ) {
2563
	changed = 1 ;
2564
	ns = ns1 ;
2565
    }
2566
    if ( changed ) {
2567
	/* Copy identifier if necessary */
2568
	id = copy_id ( id, 2 ) ;
2569
	COPY_nspace ( id_parent ( id ), ns ) ;
2570
	COPY_hashid ( id_name ( id ), nm ) ;
2571
    }
2572
 
2573
    /* Check for template functions */
2574
    form = DEREF_type ( id_function_etc_form ( id ) ) ;
2575
    if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
2576
	IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
2577
	if ( IS_id_function_etc ( tid ) ) {
2578
	    /* Template function instance */
2579
	    TYPE t1 = DEREF_type ( id_function_etc_type ( tid ) ) ;
2580
	    t = injected_type ( t, 1 ) ;
2581
	    if ( eq_type ( t1, t ) == 1 ) {
2582
		/* Allow for redeclarations */
2583
		id = tid ;
2584
	    } else {
2585
		LIST ( TOKEN ) args ;
2586
		args = DEREF_list ( type_token_args ( form ) ) ;
2587
		tid = copy_friend_func ( tid ) ;
2588
		if ( do_dump ) dump_declare ( tid, &crt_loc, 0 ) ;
2589
		args = expand_args ( args, 1, 1 ) ;
2590
		id = instance_func ( tid, args, 0, 0 ) ;
2591
		return ( id ) ;
2592
	    }
2593
	}
2594
    }
2595
 
2596
    /* Look up matching declaration */
2597
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2598
    mem = search_member ( ns, nm, 1 ) ;
2599
    fid = DEREF_id ( member_id ( mem ) ) ;
2600
    if ( !IS_NULL_id ( fid ) ) {
2601
	unsigned tag = TAG_id ( id ) ;
2602
	DECL_SPEC cl = crt_linkage ;
2603
	QUALIFIER cq = crt_id_qualifier ;
2604
	crt_id_qualifier = qual_nested ;
2605
	crt_linkage = ( ds & dspec_language ) ;
2606
	ds &= ~dspec_alias ;
2607
	fid = redecl_func ( ds, t, fid, tag, &over, -2 ) ;
2608
	crt_id_qualifier = cq ;
2609
	crt_linkage = cl ;
2610
    }
2611
    if ( IS_NULL_id ( fid ) ) {
2612
	/* Allow for name injection */
2613
	if ( IS_nspace_ctype ( ns ) ) {
2614
	    report ( crt_loc, ERR_basic_link_unmatch ( t, id ) ) ;
2615
	} else {
2616
	    COPY_id ( id_function_etc_over ( id ), over ) ;
2617
	    COPY_exp ( id_function_etc_defn ( id ), NULL_exp ) ;
2618
	    if ( !in_template_decl ) set_member ( mem, id ) ;
2619
	}
2620
    } else {
2621
	/* Set up function alias */
2622
	if ( !EQ_id ( fid, id ) ) {
2623
	    IDENTIFIER pid = DEREF_id ( id_alias ( fid ) ) ;
2624
	    ds |= dspec_alias ;
2625
	    COPY_dspec ( id_storage ( id ), ds ) ;
2626
	    COPY_id ( id_alias ( id ), pid ) ;
2627
	    id = fid ;
2628
	}
2629
    }
2630
    return ( id ) ;
2631
}
2632
 
2633
 
2634
/*
2635
    COPY THE MEMBERS OF A CLASS
2636
 
2637
    This routine copies the members of the class cs to the class ct.
2638
    Note that this is done only if cs has been completely defined,
2639
    partially defined classes are not copied.
2640
*/
2641
 
2642
void copy_members
2643
    PROTO_N ( ( ct, cs, ci, def ) )
2644
    PROTO_T ( CLASS_TYPE ct X CLASS_TYPE cs X CLASS_INFO ci X int def )
2645
{
2646
    CLASS_INFO ck = DEREF_cinfo ( ctype_info ( cs ) ) ;
2647
    CLASS_INFO cj = DEREF_cinfo ( ctype_info ( ct ) ) ;
2648
    if ( !( ck & cinfo_complete ) ) def = 0 ;
2649
    if ( ck & cinfo_recursive ) cj |= cinfo_recursive ;
2650
    cj |= ci ;
2651
    COPY_cinfo ( ctype_info ( ct ), cj ) ;
2652
    if ( def && !( cj & cinfo_defined ) ) {
2653
	int ic ;
2654
	GRAPH gr ;
2655
	MEMBER mem ;
2656
	DECL_SPEC ds ;
2657
	LOCATION loc ;
2658
	IDENTIFIER cid ;
2659
	DECL_SPEC pacc ;
2660
	ACCESS_LIST accs ;
2661
	LIST ( GRAPH ) br ;
2662
	LIST ( IDENTIFIER ) fr ;
2663
	LIST ( CLASS_TYPE ) fc ;
2664
	int lex = crt_lex_token ;
2665
	int fn = in_function_defn ;
2666
	int rfn = really_in_function_defn ;
2667
	NAMESPACE nt = DEREF_nspace ( ctype_member ( ct ) ) ;
2668
	NAMESPACE ns = DEREF_nspace ( ctype_member ( cs ) ) ;
2669
 
2670
	/* Save class information */
2671
	loc = crt_loc ;
2672
	bad_crt_loc++ ;
2673
	ic = in_class_defn ;
2674
	push_class ( ct ) ;
2675
	push_namespace ( nt ) ;
2676
	in_class_defn = 1 ;
2677
	in_function_defn = 0 ;
2678
	really_in_class_defn++ ;
2679
	really_in_function_defn = 0 ;
2680
	crt_lex_token = lex_ignore_token ;
2681
	save_access ( &accs ) ;
2682
	cid = DEREF_id ( ctype_name ( ct ) ) ;
2683
	DEREF_loc ( id_loc ( cid ), crt_loc ) ;
2684
	ds = DEREF_dspec ( id_storage ( cid ) ) ;
2685
	ds |= dspec_defn ;
2686
	COPY_dspec ( id_storage ( cid ), ds ) ;
2687
	cj |= ( cinfo_complete | cinfo_defined ) ;
2688
	COPY_cinfo ( ctype_info ( ct ), cj ) ;
2689
	if ( do_dump ) dump_declare ( cid, &loc, 1 ) ;
2690
 
2691
	/* Copy base classes */
2692
	pacc = prev_access ;
2693
	gr = DEREF_graph ( ctype_base ( cs ) ) ;
2694
	br = DEREF_list ( graph_tails ( gr ) ) ;
2695
	while ( !IS_NULL_list ( br ) ) {
2696
	    int virt = 0 ;
2697
	    GRAPH gu = DEREF_graph ( HEAD_list ( br ) ) ;
2698
	    CLASS_TYPE cu = DEREF_ctype ( graph_head ( gu ) ) ;
2699
	    DECL_SPEC acc = DEREF_dspec ( graph_access ( gu ) ) ;
2700
	    cid = DEREF_id ( ctype_name ( cu ) ) ;
2701
	    if ( acc & dspec_virtual ) virt = 1 ;
2702
	    acc &= dspec_access ;
2703
	    add_base_class ( cid, acc, virt ) ;
2704
	    br = TAIL_list ( br ) ;
2705
	}
2706
	end_base_class ( ct, 1 ) ;
2707
	prev_access = pacc ;
2708
 
2709
	/* Copy nested classes */
2710
	fr = DEREF_list ( ctype_nest ( cs ) ) ;
2711
	while ( !IS_NULL_list ( fr ) ) {
2712
	    IDENTIFIER fid = DEREF_id ( HEAD_list ( fr ) ) ;
2713
	    if ( IS_id_class_name_etc ( fid ) ) {
2714
		DECL_SPEC fds = DEREF_dspec ( id_storage ( fid ) ) ;
2715
		if ( !( fds & dspec_instance ) ) {
2716
		    TYPE f ;
2717
		    DEREF_loc ( id_loc ( fid ), crt_loc ) ;
2718
		    pacc = crt_access ;
2719
		    crt_access = ( fds & dspec_access ) ;
2720
		    f = DEREF_type ( id_class_name_etc_defn ( fid ) ) ;
2721
		    IGNORE copy_nested ( fid, f, NULL_type, &loc ) ;
2722
		    crt_access = pacc ;
2723
		}
2724
	    }
2725
	    fr = TAIL_list ( fr ) ;
2726
	}
2727
 
2728
	/* Copy class members */
2729
	mem = DEREF_member ( nspace_ctype_first ( ns ) ) ;
2730
	while ( !IS_NULL_member ( mem ) ) {
2731
	    HASHID tnm = NULL_hashid ;
2732
	    MEMBER tmem = NULL_member ;
2733
	    IDENTIFIER id = DEREF_id ( member_id ( mem ) ) ;
2734
	    IDENTIFIER alt = DEREF_id ( member_alt ( mem ) ) ;
2735
	    if ( !IS_NULL_id ( id ) ) {
2736
		IDENTIFIER tid ;
2737
		HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2738
		DEREF_loc ( id_loc ( id ), crt_loc ) ;
2739
		tnm = expand_name ( nm, ct ) ;
2740
		tmem = search_member ( nt, tnm, 1 ) ;
2741
		tid = copy_member ( id, tnm, nt, ct, &loc ) ;
2742
		if ( !IS_NULL_id ( tid ) ) {
2743
		    set_member ( tmem, tid ) ;
2744
		}
2745
	    }
2746
	    if ( !IS_NULL_id ( alt ) && !EQ_id ( id, alt ) ) {
2747
		IDENTIFIER talt ;
2748
		DEREF_loc ( id_loc ( alt ), crt_loc ) ;
2749
		if ( IS_NULL_member ( tmem ) ) {
2750
		    HASHID nm = DEREF_hashid ( id_name ( alt ) ) ;
2751
		    tnm = expand_name ( nm, ct ) ;
2752
		    tmem = search_member ( nt, tnm, 1 ) ;
2753
		}
2754
		talt = copy_member ( alt, tnm, nt, ct, &loc ) ;
2755
		if ( !IS_NULL_id ( talt ) ) {
2756
		    set_type_member ( tmem, talt ) ;
2757
		}
2758
	    }
2759
	    mem = DEREF_member ( member_next ( mem ) ) ;
2760
	}
2761
 
2762
	/* Copy chums */
2763
	fc = DEREF_list ( ctype_chums ( cs ) ) ;
2764
	while ( !IS_NULL_list ( fc ) ) {
2765
	    TYPE r = NULL_type ;
2766
	    CLASS_TYPE cr = DEREF_ctype ( HEAD_list ( fc ) ) ;
2767
	    cr = expand_ctype ( cr, 2, &r ) ;
2768
	    friend_class ( cr, cid, 0 ) ;
2769
	    fc = TAIL_list ( fc ) ;
2770
	}
2771
 
2772
	/* Copy pals */
2773
	fr = DEREF_list ( ctype_pals ( cs ) ) ;
2774
	if ( !IS_NULL_list ( fr ) ) {
2775
	    while ( !IS_NULL_list ( fr ) ) {
2776
		IDENTIFIER fid = DEREF_id ( HEAD_list ( fr ) ) ;
2777
		DEREF_loc ( id_loc ( fid ), decl_loc ) ;
2778
		DEREF_loc ( id_loc ( fid ), crt_loc ) ;
2779
		if ( IS_id_class_name ( fid ) ) {
2780
		    fid = copy_friend_class ( fid ) ;
2781
		    friend_class ( ct, fid, 0 ) ;
2782
		} else {
2783
		    EXP e = DEREF_exp ( id_function_etc_defn ( fid ) ) ;
2784
		    fid = copy_friend_func ( fid ) ;
2785
		    friend_function ( ct, fid, 0 ) ;
2786
		    if ( !IS_NULL_exp ( e ) && !IS_exp_value ( e ) ) {
2787
			copy_object ( fid, e ) ;
2788
		    }
2789
		}
2790
		if ( do_dump ) dump_declare ( fid, &crt_loc, 0 ) ;
2791
		fr = TAIL_list ( fr ) ;
2792
	    }
2793
	    fr = DEREF_list ( ctype_pals ( ct ) ) ;
2794
	    fr = REVERSE_list ( fr ) ;
2795
	    COPY_list ( ctype_pals ( ct ), fr ) ;
2796
	}
2797
 
2798
	/* Update class information */
2799
	cj = DEREF_cinfo ( ctype_info ( ct ) ) ;
2800
#if LANGUAGE_CPP
2801
	cj = implicit_decl ( ct, cj, dspec_instance ) ;
2802
#endif
2803
	if ( cj & cinfo_non_aggregate ) {
2804
	    /* POD classes must be aggregate classes */
2805
	    cj &= ~cinfo_pod ;
2806
	}
2807
	cj |= cinfo_complete ;
2808
	COPY_cinfo ( ctype_info ( ct ), cj ) ;
2809
	inherit_class () ;
2810
	IGNORE restore_access ( cid, &accs ) ;
2811
	in_class_defn = ic ;
2812
	really_in_class_defn-- ;
2813
	really_in_function_defn = rfn ;
2814
	in_function_defn = fn ;
2815
	crt_lex_token = lex ;
2816
	IGNORE pop_namespace () ;
2817
	pop_class () ;
2818
	decl_loc = loc ;
2819
	crt_loc = loc ;
2820
	bad_crt_loc-- ;
2821
    }
2822
    return ;
2823
}