Subversion Repositories tendra.SVN

Rev

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

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