Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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 "version.h"
63
#include "system.h"
64
#include "c_types.h"
65
#include "ctype_ops.h"
66
#include "etype_ops.h"
67
#include "exp_ops.h"
68
#include "ftype_ops.h"
69
#include "graph_ops.h"
70
#include "hashid_ops.h"
71
#include "id_ops.h"
72
#include "itype_ops.h"
73
#include "member_ops.h"
74
#include "nat_ops.h"
75
#include "nspace_ops.h"
76
#include "off_ops.h"
77
#include "tok_ops.h"
78
#include "type_ops.h"
79
#include "error.h"
80
#include "catalog.h"
81
#include "basetype.h"
82
#include "bits.h"
83
#include "buffer.h"
84
#include "char.h"
85
#include "chktype.h"
86
#include "class.h"
87
#include "constant.h"
88
#include "convert.h"
89
#include "decode.h"
90
#include "derive.h"
91
#include "dump.h"
92
#include "encode.h"
93
#include "exception.h"
94
#include "file.h"
95
#include "function.h"
96
#include "hash.h"
97
#include "inttype.h"
98
#include "lex.h"
99
#include "literal.h"
100
#include "load.h"
101
#include "merge.h"
102
#include "namespace.h"
103
#include "redeclare.h"
104
#include "save.h"
105
#include "symbols.h"
106
#include "syntax.h"
107
#include "token.h"
108
#include "ustring.h"
109
#include "xalloc.h"
110
 
111
 
112
/*
113
    FORWARD DECLARATIONS
114
 
115
    The following forward declarations are required in the spec input
116
    routines.
117
*/
118
 
6 7u83 119
static EXP load_exp(BITSTREAM *, TYPE);
120
static TYPE load_type(BITSTREAM *, IDENTIFIER);
121
static TYPE load_ctype(BITSTREAM *, IDENTIFIER, CLASS_TYPE *);
122
static IDENTIFIER load_id(BITSTREAM *, NAMESPACE);
123
static LIST(TYPE)load_type_list(BITSTREAM *);
124
static TOKEN load_tok(BITSTREAM *, int);
125
static void load_nspace(BITSTREAM *, NAMESPACE, int);
2 7u83 126
 
127
 
128
/*
129
    SPEC INPUT ERROR FLAG
130
 
131
    This flag is set to true to indicate a spec input error.
132
*/
133
 
6 7u83 134
static int spec_error = 0;
2 7u83 135
 
136
 
137
/*
138
    SPEC ERROR ROUTINE
139
 
140
    This routine is called when a spec input error occurs.  code gives
141
    the reason for the error.
142
*/
143
 
6 7u83 144
static void
145
spec_fail(int code)
2 7u83 146
{
6 7u83 147
	if (!spec_error) {
148
		spec_error = code;
149
	}
150
	return;
2 7u83 151
}
152
 
153
 
154
/*
155
    SPEC ERROR CODES
156
 
157
    The spec error codes are given by the corresponding source line.
158
    This macro gives a convenient shorthand for calling spec_fail.
159
*/
160
 
161
#ifdef __LINE__
6 7u83 162
#define SPEC_ERROR()		spec_fail(__LINE__)
2 7u83 163
#else
6 7u83 164
#define SPEC_ERROR()		spec_fail(100)
2 7u83 165
#endif
166
 
167
 
168
/*
169
    SIMPLE READING ROUTINES
170
 
171
    These macros give the simple reading routines for the enumeration
172
    types etc.  Note that DE_BITS_2 is used for types which occupy more
173
    than 16 bits.
174
*/
175
 
6 7u83 176
#define load_btype(A)		(BASE_TYPE)DE_BITS_2((A), BITS_btype)
177
#define load_cinfo(A)		(CLASS_INFO)DE_BITS_2((A), BITS_cinfo)
178
#define load_cusage(A)	(CLASS_USAGE)DE_BITS((A), BITS_cusage)
179
#define load_cv(A)		(CV_SPEC)DE_BITS((A), BITS_cv_qual)
180
#define load_dspec(A)		(DECL_SPEC)DE_BITS_2((A), BITS_dspec)
181
#define load_mqual(A)		(CV_SPEC)DE_BITS((A), BITS_cv)
182
#define load_qual(A)		(QUALIFIER)DE_BITS((A), BITS_qual)
2 7u83 183
 
184
 
185
/*
186
    READ A LEXICAL TOKEN NUMBER
187
 
188
    This routine reads a lexical token number from the bitstream bs.
189
*/
190
 
6 7u83 191
static int
192
load_lex(BITSTREAM *bs)
2 7u83 193
{
6 7u83 194
	int t = (int)DE_BITS(bs, BITS_lex);
195
	if (t > LAST_TOKEN) {
196
		t = lex_unknown;
197
		SPEC_ERROR();
198
	}
199
	return (t);
2 7u83 200
}
201
 
202
 
203
/*
204
    READ A LIST OF PREPROCESSING TOKENS
205
 
206
    This routine reads a list of preprocessing tokens from the
207
    bitstream bs.
208
*/
209
 
6 7u83 210
static PPTOKEN *
211
load_pptoks(BITSTREAM *bs)
2 7u83 212
{
6 7u83 213
	/* NOT YET IMPLEMENTED */
214
	UNUSED(bs);
215
	return (NULL);
2 7u83 216
}
217
 
218
 
219
/*
220
    READ A LOCATION
221
 
222
    This routine reads the current location from the bitstream bs.
223
*/
224
 
6 7u83 225
static void
226
load_loc(BITSTREAM *bs)
2 7u83 227
{
6 7u83 228
	if (DE_BOOL(bs)) {
229
		/* Line number has changed */
230
		crt_loc.line = DE_INT(bs);
231
		crt_loc.column = 0;
232
		if (DE_BOOL(bs)) {
233
			/* File position has changed */
234
			string file;
235
			string input;
236
			ulong date, date2;
237
			STAT_TYPE *fs = NULL;
238
			PTR(POSITION)posn;
239
			PTR(LOCATION)from = NULL_ptr(LOCATION);
240
			unsigned long off = DE_INT(bs);
241
			if (DE_BOOL(bs)) {
242
				STAT_TYPE fstr;
243
				BUFFER *bf = clear_buffer(&token_buff,
244
							  NIL(FILE));
245
				de_tdfstring(bs, bf);
246
				file = xustrcpy(bf->start);
247
				if (DE_BOOL(bs)) {
248
					bf = clear_buffer(&token_buff,
249
							  NIL(FILE));
250
					de_tdfstring(bs, bf);
251
					input = xustrcpy(bf->start);
252
				} else {
253
					input = file;
254
				}
255
				date = DE_INT(bs);
256
				fs = stat_func(strlit(input), &fstr);
257
				date2 = stat_date(fs);
258
			} else {
259
				posn = crt_loc.posn;
260
				file = DEREF_string(posn_file(posn));
261
				input = DEREF_string(posn_input(posn));
262
				date = DEREF_ulong(posn_datestamp(posn));
263
				date2 = date;
264
			}
265
			posn = MAKE_ptr(SIZE_posn);
266
			MAKE_posn(file, input, input, NULL, off, from, date,
267
				  posn);
268
			crt_loc.posn = posn;
269
			if (date && date2 && date != date2) {
270
				/* File has changed on disk */
271
				if (!already_included(input, fs, 4)) {
272
					report(crt_loc, ERR_cpp_include_date());
273
				}
274
			}
2 7u83 275
		}
276
	}
6 7u83 277
	return;
2 7u83 278
}
279
 
280
 
281
/*
282
    READ AN IDENTIFIER NAME
283
 
284
    This routine reads an identifier name from the bitstream bs.  ns gives
285
    the current namespace.
286
*/
287
 
6 7u83 288
static HASHID
289
load_hashid(BITSTREAM *bs, NAMESPACE ns)
2 7u83 290
{
6 7u83 291
	HASHID nm = NULL_hashid;
292
	unsigned n = DE_BITS(bs, BITS_hashid);
293
	if (n) {
294
		if (n <= ORDER_hashid) {
295
			unsigned tag = n - 1;
296
			ASSERT(ORDER_hashid == 7);
297
			switch (tag) {
298
			case hashid_name_tag:
299
			case hashid_ename_tag: {
300
				string s;
301
				int ext = 0;
302
				unsigned long h;
303
				BUFFER *bf = clear_buffer(&token_buff,
304
							  NIL(FILE));
305
				de_tdfstring(bs, bf);
306
				s = bf->start;
307
				h = hash(s);
308
				if (tag == hashid_ename_tag) {
309
					ext = 1;
310
				}
311
				nm = lookup_name(s, h, ext, lex_unknown);
312
				break;
2 7u83 313
			}
6 7u83 314
			case hashid_constr_tag: {
315
				if (IS_nspace_ctype(ns)) {
316
					IDENTIFIER cid =
317
					    DEREF_id(nspace_name(ns));
318
					if (IS_id_class_name(cid)) {
319
						TYPE t;
320
						t = DEREF_type(id_class_name_defn(cid));
321
						nm = lookup_constr(t, cid);
322
						break;
323
					}
324
				}
325
				SPEC_ERROR();
326
				break;
2 7u83 327
			}
6 7u83 328
			case hashid_destr_tag: {
329
				if (IS_nspace_ctype(ns)) {
330
					IDENTIFIER cid =
331
					    DEREF_id(nspace_name(ns));
332
					if (IS_id_class_name(cid)) {
333
						TYPE t;
334
						t = DEREF_type(id_class_name_defn(cid));
335
						nm = lookup_destr(t, cid);
336
						break;
337
					}
338
				}
339
				SPEC_ERROR();
340
				break;
341
			}
342
			case hashid_conv_tag: {
343
				TYPE t = load_type(bs, NULL_id);
344
				if (!IS_NULL_type(t)) {
345
					nm = lookup_conv(t);
346
					break;
347
				}
348
				SPEC_ERROR();
349
				break;
350
			}
351
			case hashid_op_tag: {
352
				int op = load_lex(bs);
353
				nm = lookup_op(op);
354
				break;
355
			}
356
			case hashid_anon_tag: {
357
				nm = lookup_anon();
358
				break;
359
			}
360
			}
361
		} else {
362
			SPEC_ERROR();
2 7u83 363
		}
364
	}
6 7u83 365
	return (nm);
2 7u83 366
}
367
 
368
 
369
/*
370
    READ A LIST OF IDENTIFIER NAMES
371
 
372
    This routine reads a list of identifier names from the bitstream bs.
373
*/
374
 
6 7u83 375
static LIST(HASHID)
376
load_hashid_list(BITSTREAM *bs, NAMESPACE ns)
2 7u83 377
{
6 7u83 378
	LIST(HASHID)p = NULL_list(HASHID);
379
	while (DE_BOOL(bs)) {
380
		HASHID nm = load_hashid(bs, ns);
381
		if (IS_NULL_hashid(nm)) {
382
			SPEC_ERROR();
383
			break;
384
		}
385
		CONS_hashid(nm, p, p);
2 7u83 386
	}
6 7u83 387
	p = REVERSE_list(p);
388
	return (p);
2 7u83 389
}
390
 
391
 
392
/*
393
    IDENTIFIER LOOK-UP TABLE
394
 
395
    These variables give the table which gives the mapping between identifier
396
    numbers and identifiers.
397
*/
398
 
6 7u83 399
static IDENTIFIER *id_table = NULL;
400
static ulong id_table_size = 0;
401
static ulong ids_pending = 0;
2 7u83 402
 
403
 
404
/*
405
    LOOK UP IDENTIFIER IN TABLE
406
 
407
    This routine returns a pointer to the dth element of the table above,
408
    extending it if necessary.
409
*/
410
 
6 7u83 411
static IDENTIFIER *
412
id_lookup(ulong d)
2 7u83 413
{
6 7u83 414
	ulong m = id_table_size;
415
	IDENTIFIER *p = id_table;
416
	if (d >= m) {
417
		ulong n = d + 100;
418
		p = xrealloc_nof(p, IDENTIFIER, n);
419
		while (m < n) {
420
			p[m] = NULL_id;
421
			m++;
422
		}
423
		id_table_size = n;
424
		id_table = p;
2 7u83 425
	}
6 7u83 426
	return (p + d);
2 7u83 427
}
428
 
429
 
430
/*
431
    READ AN IDENTIFIER NUMBER
432
 
433
    This routine reads an identifier number from the bitstream bs.  It is
434
    possible for an identifier to be used before it is declared using
435
    load_id.
436
*/
437
 
6 7u83 438
static IDENTIFIER
439
load_use(BITSTREAM *bs, unsigned tag)
2 7u83 440
{
6 7u83 441
	IDENTIFIER id;
442
	IDENTIFIER *pid;
443
	ulong d = DE_INT(bs);
444
	if (d == 0) {
445
		/* Null identifier */
446
		return (NULL_id);
447
	}
448
	pid = id_lookup(d);
449
	id = *pid;
450
	if (IS_NULL_id(id)) {
451
		/* Create dummy identifier */
452
		HASHID nm = KEYWORD(lex_zzzz);
453
		MAKE_id_pending(nm, dspec_none, NULL_nspace, crt_loc,
454
				tag, NULL_type, id);
455
		COPY_ulong(id_dump(id), d);
456
		ids_pending++;
457
		*pid = id;
458
	}
459
	return (id);
2 7u83 460
}
461
 
462
 
463
/*
464
    READ A LIST OF IDENTIFIER NUMBERS
465
 
466
    This routine reads a list of identifier numbers from the bitstream bs.
467
*/
468
 
6 7u83 469
static LIST(IDENTIFIER)
470
load_use_list(BITSTREAM *bs)
2 7u83 471
{
6 7u83 472
	LIST(IDENTIFIER)p = NULL_list(IDENTIFIER);
473
	while (DE_BOOL(bs)) {
474
		IDENTIFIER id = load_use(bs, null_tag);
475
		if (spec_error) {
476
			break;
477
		}
478
		CONS_id(id, p, p);
479
	}
480
	p = REVERSE_list(p);
481
	return (p);
2 7u83 482
}
483
 
484
 
485
/*
486
    READ A LIST OF TOKENS
487
 
488
    This routine reads a list of tokens from the bitstream bs.  def is
489
    as in load_tok.
490
*/
491
 
6 7u83 492
static LIST(TOKEN)
493
load_tok_list(BITSTREAM *bs, int def)
2 7u83 494
{
6 7u83 495
	LIST(TOKEN)p = NULL_list(TOKEN);
496
	while (DE_BOOL(bs)) {
497
		TOKEN tok = load_tok(bs, def);
498
		if (spec_error) {
499
			break;
500
		}
501
		CONS_tok(tok, p, p);
502
	}
503
	p = REVERSE_list(p);
504
	return (p);
2 7u83 505
}
506
 
507
 
508
/*
509
    READ AN INTEGER CONSTANT
510
 
511
    This routine reads an integer constant from the bitstream bs.
512
*/
513
 
6 7u83 514
static NAT
515
load_nat(BITSTREAM *bs)
2 7u83 516
{
6 7u83 517
	NAT m = NULL_nat;
518
	unsigned n = DE_BITS(bs, BITS_nat);
519
	if (n == 0) {
520
		/* Null constants */
521
		return (m);
2 7u83 522
	}
6 7u83 523
	if (n > ORDER_nat) {
524
		SPEC_ERROR();
525
		return (m);
526
	}
527
	ASSERT(ORDER_nat == 5);
528
	switch (n - 1) {
529
	case nat_small_tag: {
530
		unsigned long v = DE_INT(bs);
531
		m = make_nat_value(v);
532
		break;
533
	}
534
	case nat_large_tag: {
535
		int count = 0;
536
		LIST(unsigned)p = NULL_list(unsigned);
537
		while (DE_BOOL(bs)) {
538
			unsigned v = (unsigned)DE_INT(bs);
539
			CONS_unsigned(v, p, p);
540
			if (++count == 10) {
541
				/* Check for end of file */
542
				if (de_eof(bs)) {
543
					SPEC_ERROR();
544
					break;
545
				}
546
				count = 0;
547
			}
2 7u83 548
		}
6 7u83 549
		p = REVERSE_list(p);
550
		m = make_large_nat(p);
551
		break;
2 7u83 552
	}
6 7u83 553
	case nat_calc_tag: {
554
		EXP e = load_exp(bs, type_sint);
555
		if (!IS_NULL_exp(e)) {
556
			MAKE_nat_calc(e, m);
557
			break;
558
		}
559
		SPEC_ERROR();
560
		break;
2 7u83 561
	}
6 7u83 562
	case nat_neg_tag: {
563
		m = load_nat(bs);
564
		if (!IS_NULL_nat(m)) {
565
			m = negate_nat(m);
566
			break;
567
		}
568
		SPEC_ERROR();
569
		break;
2 7u83 570
	}
6 7u83 571
	case nat_token_tag: {
572
		IDENTIFIER tok = load_use(bs, null_tag);
573
		if (!IS_NULL_id(tok)) {
574
			LIST(TOKEN)args = load_tok_list(bs, 1);
575
			MAKE_nat_token(tok, args, m);
576
			break;
577
		}
578
		SPEC_ERROR();
579
		break;
2 7u83 580
	}
6 7u83 581
	}
582
	return (m);
2 7u83 583
}
584
 
585
 
586
/*
587
    READ AN OFFSET
588
 
589
    This routine reads an offset from the bitstream bs.
590
*/
591
 
6 7u83 592
static OFFSET
593
load_off(BITSTREAM *bs)
2 7u83 594
{
6 7u83 595
	/* NOT YET IMPLEMENTED */
596
	UNUSED(bs);
597
	return (NULL_off);
2 7u83 598
}
599
 
600
 
601
/*
602
    READ AN EXPRESSION
603
 
604
    This routine reads an expression from the bitstream bs.
605
*/
606
 
6 7u83 607
static EXP
608
load_exp(BITSTREAM *bs, TYPE t)
2 7u83 609
{
6 7u83 610
	unsigned tag;
611
	EXP e = NULL_exp;
612
	unsigned n = DE_BITS(bs, BITS_exp);
613
	if (n == 0) {
614
		/* Null expressions */
615
		return (e);
616
	}
617
	if (n > ORDER_exp) {
618
		SPEC_ERROR();
619
		return (e);
620
	}
621
	tag = n - 1;
622
	switch (tag) {
623
	case exp_int_lit_tag: {
624
		NAT m = load_nat(bs);
625
		if (!IS_NULL_nat(m)) {
626
			unsigned etag = DE_BITS(bs, BITS_exp);
627
			if (etag < ORDER_exp) {
628
				MAKE_exp_int_lit(t, m, etag, e);
629
				break;
630
			}
2 7u83 631
		}
6 7u83 632
		SPEC_ERROR();
633
		break;
2 7u83 634
	}
6 7u83 635
	case exp_token_tag: {
636
		IDENTIFIER tok = load_use(bs, null_tag);
637
		if (!IS_NULL_id(tok)) {
638
			LIST(TOKEN)args = load_tok_list(bs, 1);
639
			MAKE_exp_token(t, tok, args, e);
640
			break;
641
		}
642
		SPEC_ERROR();
643
		break;
2 7u83 644
	}
6 7u83 645
	default: {
646
		/* NOT YET IMPLEMENTED */
647
		MAKE_exp_value(t, e);
648
		break;
2 7u83 649
	}
6 7u83 650
	}
651
	return (e);
2 7u83 652
}
653
 
654
 
655
/*
656
    READ A BUILT-IN TYPE
657
 
658
    This routine reads a built-in type number from the bitstream bs.
659
*/
660
 
6 7u83 661
static BUILTIN_TYPE
662
load_ntype(BITSTREAM *bs)
2 7u83 663
{
6 7u83 664
	BUILTIN_TYPE nt = (BUILTIN_TYPE)DE_BITS(bs, BITS_ntype);
665
	if (nt >= ORDER_ntype) {
666
		nt = ntype_none;
667
		SPEC_ERROR();
668
	}
669
	return (nt);
2 7u83 670
}
671
 
672
 
673
/*
674
    READ AN INTEGRAL TYPE
675
 
676
    This routine reads a integral type from the bitstream bs.  Note that
677
    the result is returned as a type rather than an integral type.
678
*/
679
 
6 7u83 680
static TYPE
681
load_itype(BITSTREAM *bs, CV_SPEC cv, unsigned tag)
2 7u83 682
{
6 7u83 683
	TYPE t = NULL_type;
684
	unsigned n = DE_BITS(bs, BITS_itype);
685
	if (n == 0) {
686
		/* Null types */
687
		return (t);
2 7u83 688
	}
6 7u83 689
	if (n > ORDER_itype) {
690
		SPEC_ERROR();
691
		return (t);
692
	}
693
	switch (n - 1) {
694
	case itype_basic_tag: {
695
		BUILTIN_TYPE nt = load_ntype(bs);
696
		t = type_builtin[nt];
697
		break;
698
	}
699
	case itype_bitfield_tag: {
700
		TYPE s = load_type(bs, NULL_id);
701
		if (!IS_NULL_type(s)) {
702
			BASE_TYPE bt = load_btype(bs);
703
			NAT m = load_nat(bs);
704
			if (!IS_NULL_nat(m)) {
705
				t = check_bitfield_type(cv, s, bt, m, 1);
706
				cv = cv_none;
707
				break;
708
			}
2 7u83 709
		}
6 7u83 710
		SPEC_ERROR();
711
		break;
2 7u83 712
	}
6 7u83 713
	case itype_promote_tag: {
714
		TYPE s = load_itype(bs, cv_none, type_integer_tag);
715
		if (!IS_NULL_type(s)) {
716
			t = promote_type(s);
717
			break;
718
		}
719
		SPEC_ERROR();
720
		break;
2 7u83 721
	}
6 7u83 722
	case itype_arith_tag: {
723
		TYPE s = load_itype(bs, cv_none, type_integer_tag);
724
		if (!IS_NULL_type(s)) {
725
			TYPE r = load_itype(bs, cv_none, type_integer_tag);
726
			if (!IS_NULL_type(r)) {
727
				t = arith_itype(s, r, NULL_exp, NULL_exp);
728
				break;
729
			}
2 7u83 730
		}
6 7u83 731
		SPEC_ERROR();
732
		break;
2 7u83 733
	}
6 7u83 734
	case itype_literal_tag: {
735
		NAT m = load_nat(bs);
736
		if (!IS_NULL_nat(m)) {
737
			int form = (int)DE_BITS(bs, 2);
738
			if (form >= 0 && form < BASE_NO) {
739
				int suff = (int)DE_BITS(bs, 3);
740
				if (suff >= 0 && suff < SUFFIX_NO) {
741
					int fit = 0;
742
					string str = NULL_string;
743
					t = find_literal_type(m, form, suff,
744
							      str, &fit);
745
					break;
746
				}
747
			}
2 7u83 748
		}
6 7u83 749
		SPEC_ERROR();
750
		break;
2 7u83 751
	}
6 7u83 752
	case itype_token_tag: {
753
		IDENTIFIER tok = load_use(bs, null_tag);
754
		if (!IS_NULL_id(tok)) {
755
			LIST(TOKEN)args = load_tok_list(bs, 1);
756
			t = apply_itype_token(tok, args);
757
			break;
758
		}
759
		SPEC_ERROR();
760
		break;
2 7u83 761
	}
762
	}
6 7u83 763
	if (!IS_NULL_type(t)) {
764
		if (TAG_type(t) == tag) {
765
			if (cv) {
766
				t = qualify_type(t, cv, 0);
767
			}
768
		} else {
769
			SPEC_ERROR();
770
		}
771
	}
772
	return (t);
2 7u83 773
}
774
 
775
 
776
/*
777
    READ A FLOATING POINT TYPE
778
 
779
    This routine reads a floating point type from the bitstream bs.  Note
780
    that the result is returned as a type rather than a floating point type.
781
*/
782
 
6 7u83 783
static TYPE
784
load_ftype(BITSTREAM *bs, CV_SPEC cv)
2 7u83 785
{
6 7u83 786
	TYPE t = NULL_type;
787
	unsigned n = DE_BITS(bs, BITS_ftype);
788
	if (n == 0) {
789
		/* Null types */
790
		return (t);
2 7u83 791
	}
6 7u83 792
	if (n > ORDER_ftype) {
793
		SPEC_ERROR();
794
		return (t);
2 7u83 795
	}
6 7u83 796
	ASSERT(ORDER_ftype == 4);
797
	switch (n - 1) {
798
	case ftype_basic_tag: {
799
		BUILTIN_TYPE nt = load_ntype(bs);
800
		t = type_builtin[nt];
801
		break;
802
	}
803
	case ftype_arg_promote_tag: {
804
		TYPE s = load_ftype(bs, cv_none);
805
		if (!IS_NULL_type(s)) {
806
			t = arg_promote_type(s, KILL_err);
807
			break;
2 7u83 808
		}
6 7u83 809
		SPEC_ERROR();
810
		break;
2 7u83 811
	}
6 7u83 812
	case ftype_arith_tag: {
813
		TYPE s = load_ftype(bs, cv_none);
814
		if (!IS_NULL_type(s)) {
815
			TYPE r = load_ftype(bs, cv_none);
816
			if (!IS_NULL_type(r)) {
817
				t = arith_ftype(s, r);
818
				break;
819
			}
820
		}
821
		SPEC_ERROR();
822
		break;
2 7u83 823
	}
6 7u83 824
	case ftype_token_tag: {
825
		IDENTIFIER tok = load_use(bs, null_tag);
826
		if (!IS_NULL_id(tok)) {
827
			LIST(TOKEN)args = load_tok_list(bs, 1);
828
			t = apply_ftype_token(tok, args);
829
			break;
830
		}
831
		break;
2 7u83 832
	}
6 7u83 833
	}
834
	if (!IS_NULL_type(t)) {
835
		if (IS_type_floating(t)) {
836
			if (cv)t = qualify_type(t, cv, 0);
837
		} else {
838
			SPEC_ERROR();
839
			t = NULL_type;
840
		}
841
	}
842
	return (t);
2 7u83 843
}
844
 
845
 
846
/*
847
    READ A BASE CLASS GRAPH
848
 
849
    This routine reads a base class from the bitstream bs.  gu gives the
850
    parent graph and gt gives the top of the graph.
851
*/
852
 
6 7u83 853
static GRAPH
854
load_graph(BITSTREAM *bs, GRAPH gu, GRAPH gt)
2 7u83 855
{
6 7u83 856
	GRAPH gr;
857
	LIST(GRAPH)br = NULL_list(GRAPH);
858
	if (IS_NULL_graph(gu)) {
859
		/* Top graph node */
860
		gr = gt;
2 7u83 861
	} else {
6 7u83 862
		/* Read graph node */
863
		CLASS_TYPE ct = NULL_ctype;
864
		IGNORE load_ctype(bs, NULL_id, &ct);
865
		if (!IS_NULL_ctype(ct)) {
866
			DECL_SPEC acc = load_dspec(bs);
867
			MAKE_graph_basic(ct, acc, gr);
868
		} else {
869
			SPEC_ERROR();
870
			return (NULL_graph);
871
		}
2 7u83 872
	}
6 7u83 873
	while (DE_BOOL(bs)) {
874
		/* Read base classes */
875
		GRAPH gs = load_graph(bs, gr, gt);
876
		if (IS_NULL_graph(gs)) {
877
			break;
878
		}
879
		CONS_graph(gs, br, br);
880
	}
881
	br = REVERSE_list(br);
882
	COPY_list(graph_tails(gr), br);
883
	COPY_graph(graph_top(gr), gt);
884
	COPY_graph(graph_up(gr), gu);
885
	return (gr);
2 7u83 886
}
887
 
888
 
889
/*
890
    READ A CLASS TYPE
891
 
892
    This routine reads a class type from the bitstream bs.  If def is not
893
    null then the full class definition is read, otherwise just a use is
894
    read.
895
*/
896
 
6 7u83 897
static TYPE
898
load_ctype(BITSTREAM *bs, IDENTIFIER def, CLASS_TYPE *pct)
2 7u83 899
{
6 7u83 900
	TYPE t = NULL_type;
901
	if (!IS_NULL_id(def)) {
902
		if (IS_id_class_name(def)) {
903
			/* Read class definition */
904
			GRAPH gr;
905
			CLASS_TYPE ct;
906
			DECL_SPEC acc;
907
			NAMESPACE mns;
2 7u83 908
 
6 7u83 909
			/* Read the class information */
910
			CLASS_INFO ci = load_cinfo(bs);
911
			CLASS_USAGE cu = load_cusage(bs);
2 7u83 912
 
6 7u83 913
			/* Create the class type */
914
			acc = (dspec_public | dspec_defn);
915
			MAKE_graph_basic(NULL_ctype, acc, gr);
916
			mns = make_namespace(def, nspace_ctype_tag, 20);
917
			MAKE_ctype_basic(def, ci, cu, mns, gr, 1, NULL_type,
918
					 ct);
919
			COPY_ctype(graph_head(gr), ct);
920
			MAKE_type_compound(cv_none, ct, t);
921
			COPY_type(id_class_name_defn(def), t);
922
			COPY_id(type_name(t), def);
923
			IGNORE lookup_constr(t, def);
924
			IGNORE lookup_destr(t, def);
925
			crt_class = ct;
926
			*pct = ct;
2 7u83 927
 
6 7u83 928
			/* Read the base class graph */
929
			IGNORE load_graph(bs, NULL_graph, gr);
930
			end_base_class(ct, 1);
2 7u83 931
 
6 7u83 932
			/* Read template form */
933
			if (DE_BOOL(bs)) {
934
				TYPE form = load_type(bs, NULL_id);
935
				COPY_type(ctype_form(ct), form);
936
			}
2 7u83 937
 
6 7u83 938
		} else {
939
			SPEC_ERROR();
940
		}
2 7u83 941
 
942
	} else {
6 7u83 943
		/* Read class name */
944
		IDENTIFIER cid = load_use(bs, id_class_name_tag);
945
		if (!IS_NULL_id(cid)) {
946
			if (IS_id_class_name(cid)) {
947
				t = DEREF_type(id_class_name_defn(cid));
948
			}
949
		}
950
		if (!IS_NULL_type(t)) {
951
			while (IS_type_templ(t)) {
952
				t = DEREF_type(type_templ_defn(t));
953
			}
954
			if (IS_type_compound(t)) {
955
				CLASS_TYPE ct =
956
				    DEREF_ctype(type_compound_defn(t));
957
				*pct = ct;
958
			} else {
959
				t = NULL_type;
960
				SPEC_ERROR();
961
			}
962
		} else {
963
			SPEC_ERROR();
964
		}
2 7u83 965
	}
6 7u83 966
	return (t);
2 7u83 967
}
968
 
969
 
970
/*
971
    READ AN ENUMERATION TYPE
972
 
973
    This routine reads an enumeration type from the bitstream bs.  If def
974
    is not null then the full enumeration definition is read, otherwise
975
    just a use is read.
976
*/
977
 
6 7u83 978
static TYPE
979
load_etype(BITSTREAM *bs, IDENTIFIER def, ENUM_TYPE *pet)
2 7u83 980
{
6 7u83 981
	TYPE t = NULL_type;
982
	if (!IS_NULL_id(def)) {
983
		if (IS_id_enum_name(def)) {
984
			/* Read enumeration definition */
985
			ENUM_TYPE et;
986
			CLASS_INFO ei = load_cinfo(bs);
987
			TYPE s = load_type(bs, NULL_id);
988
			if (!IS_NULL_type(s)) {
989
				MAKE_etype_basic(def, ei, s, et);
990
				if (DE_BOOL(bs)) {
991
					TYPE form = load_type(bs, NULL_id);
992
					COPY_type(etype_form(et), form);
993
				}
994
				MAKE_type_enumerate(cv_none, et, t);
995
				COPY_type(id_enum_name_defn(def), t);
996
				COPY_id(type_name(t), def);
997
				*pet = et;
998
			} else {
999
				SPEC_ERROR();
1000
			}
1001
		} else {
1002
			SPEC_ERROR();
2 7u83 1003
		}
1004
	} else {
6 7u83 1005
		/* Read enumeration name */
1006
		IDENTIFIER eid = load_use(bs, id_enum_name_tag);
1007
		if (!IS_NULL_id(eid)) {
1008
			if (IS_id_enum_name(eid)) {
1009
				t = DEREF_type(id_enum_name_defn(eid));
1010
			}
1011
		}
1012
		if (!IS_NULL_type(t) && IS_type_enumerate(t)) {
1013
			ENUM_TYPE et;
1014
			et = DEREF_etype(type_enumerate_defn(t));
1015
			*pet = et;
1016
		} else {
1017
			t = NULL_type;
1018
			SPEC_ERROR();
1019
		}
2 7u83 1020
	}
6 7u83 1021
	return (t);
2 7u83 1022
}
1023
 
1024
 
1025
/*
1026
    READ A FUNCTION PARAMETER LIST
1027
 
1028
    This routine reads a list of function or template parameters (as
1029
    indicated by tag) from the bitstream bs into the namespace ns.
1030
*/
1031
 
6 7u83 1032
static LIST(IDENTIFIER)
1033
load_param_list(BITSTREAM *bs, NAMESPACE ns, unsigned tag)
2 7u83 1034
{
6 7u83 1035
	LIST(IDENTIFIER)pids = NULL_list(IDENTIFIER);
1036
	while (DE_BOOL(bs)) {
1037
		HASHID nm;
1038
		MEMBER mem;
1039
		IDENTIFIER pid = load_id(bs, ns);
1040
		if (IS_NULL_id(pid) || TAG_id(pid) != tag) {
1041
			SPEC_ERROR();
1042
			break;
1043
		}
1044
		nm = DEREF_hashid(id_name(pid));
1045
		mem = search_member(ns, nm, 1);
1046
		COPY_id(member_id(mem), pid);
1047
		CONS_id(pid, pids, pids);
2 7u83 1048
	}
6 7u83 1049
	pids = REVERSE_list(pids);
1050
	return (pids);
2 7u83 1051
}
1052
 
1053
 
1054
/*
1055
    READ A TYPE
1056
 
1057
    This routine reads a type from the bitstream bs.  def is passed to
1058
    load_ctype and load_etype.
1059
*/
1060
 
6 7u83 1061
static TYPE
1062
load_type(BITSTREAM *bs, IDENTIFIER def)
2 7u83 1063
{
6 7u83 1064
	unsigned n;
1065
	CV_SPEC cv;
1066
	unsigned tag;
1067
	TYPE t = NULL_type;
1068
	if (DE_BOOL(bs)) {
1069
		/* Built-in types */
1070
		BUILTIN_TYPE nt;
1071
		cv = load_cv(bs);
1072
		nt = load_ntype(bs);
1073
		t = type_builtin[nt];
1074
		if (cv) {
1075
			t = qualify_type(t, cv, 0);
1076
		}
1077
		return (t);
1078
	}
2 7u83 1079
 
6 7u83 1080
	/* Read type independent fields */
1081
	n = DE_BITS(bs, BITS_type);
1082
	if (n == 0) {
1083
		/* Null types */
1084
		return (t);
1085
	}
1086
	if (n > ORDER_type) {
1087
		SPEC_ERROR();
1088
		return (NULL_type);
1089
	}
1090
	tag = n - 1;
1091
	cv = load_cv(bs);
2 7u83 1092
 
6 7u83 1093
	/* Read type dependent fields */
1094
	ASSERT(ORDER_type == 18);
1095
	switch (tag) {
2 7u83 1096
 
6 7u83 1097
	case type_pre_tag: {
1098
		IDENTIFIER tid = load_use(bs, null_tag);
1099
		QUALIFIER qual = load_qual(bs);
1100
		BASE_TYPE bt = load_btype(bs);
1101
		MAKE_type_pre(cv, bt, qual, t);
1102
		COPY_id(type_name(t), tid);
1103
		break;
2 7u83 1104
	}
1105
 
6 7u83 1106
	case type_integer_tag: {
1107
		t = load_itype(bs, cv, tag);
1108
		if (!IS_NULL_type(t)) {
1109
			if (DE_BOOL(bs)) {
1110
				TYPE s = load_itype(bs, cv_none, tag);
1111
				if (!IS_NULL_type(s)) {
1112
					INT_TYPE it, is;
1113
					it = DEREF_itype(type_integer_rep(t));
1114
					is = DEREF_itype(type_integer_rep(s));
1115
					t = make_itype(it, is);
1116
				}
1117
			}
1118
			break;
2 7u83 1119
		}
6 7u83 1120
		SPEC_ERROR();
1121
		break;
2 7u83 1122
	}
1123
 
6 7u83 1124
	case type_floating_tag: {
1125
		t = load_ftype(bs, cv);
1126
		break;
2 7u83 1127
	}
1128
 
6 7u83 1129
	case type_top_tag:
1130
	case type_bottom_tag: {
1131
		/* Should not happen */
1132
		MAKE_type_top_etc(tag, cv, t);
1133
		break;
2 7u83 1134
	}
1135
 
6 7u83 1136
	case type_ptr_tag:
1137
	case type_ref_tag: {
1138
		/* Pointer and reference types */
1139
		TYPE s = load_type(bs, NULL_id);
1140
		if (!IS_NULL_type(s)) {
1141
			MAKE_type_ptr_etc(tag, cv, s, t);
1142
			break;
1143
		}
1144
		SPEC_ERROR();
1145
		break;
2 7u83 1146
	}
1147
 
6 7u83 1148
	case type_ptr_mem_tag: {
1149
		/* Pointer to member types */
1150
		CLASS_TYPE cs = NULL_ctype;
1151
		IGNORE load_ctype(bs, NULL_id, &cs);
1152
		if (!IS_NULL_ctype(cs)) {
1153
			TYPE r = load_type(bs, NULL_id);
1154
			if (!IS_NULL_type(r)) {
1155
				MAKE_type_ptr_mem(cv, cs, r, t);
1156
				break;
1157
			}
2 7u83 1158
		}
6 7u83 1159
		SPEC_ERROR();
1160
		break;
2 7u83 1161
	}
1162
 
6 7u83 1163
	case type_func_tag: {
1164
		/* Function types */
1165
		TYPE r = load_type(bs, NULL_id);
1166
		if (!IS_NULL_type(r)) {
1167
			int ell;
1168
			NAMESPACE pns;
1169
			CV_SPEC mqual;
1170
			LIST(IDENTIFIER)pids;
1171
			CLASS_TYPE cs = NULL_ctype;
1172
			LIST(TYPE)ex = univ_type_set;
1173
			begin_param(NULL_id);
1174
			pns = crt_namespace;
1175
			pids = load_param_list(bs, pns, id_parameter_tag);
1176
			DESTROY_list(pids, SIZE_id);
1177
			ell = (int)DE_BITS(bs, BITS_ellipsis);
1178
			mqual = load_mqual(bs);
1179
			if (DE_BOOL(bs)) {
1180
				/* Read member function type */
1181
				IGNORE load_ctype(bs, NULL_id, &cs);
1182
			}
1183
			if (DE_BOOL(bs)) {
1184
				/* Read exception specifier */
1185
				ex = load_type_list(bs);
1186
			}
1187
			t = make_func_type(r, ell, mqual, ex);
1188
			end_param();
1189
			if (!IS_NULL_ctype(cs)) {
1190
				member_func_type(cs, id_mem_func_tag, t);
1191
			}
1192
			break;
2 7u83 1193
		}
6 7u83 1194
		SPEC_ERROR();
1195
		break;
2 7u83 1196
	}
1197
 
6 7u83 1198
	case type_array_tag: {
1199
		/* Array types */
1200
		TYPE s = load_type(bs, NULL_id);
1201
		if (!IS_NULL_type(s)) {
1202
			NAT m = load_nat(bs);
1203
			MAKE_type_array(cv, s, m, t);
1204
			break;
1205
		}
1206
		SPEC_ERROR();
1207
		break;
2 7u83 1208
	}
1209
 
6 7u83 1210
	case type_bitfield_tag: {
1211
		/* Bitfield types */
1212
		t = load_itype(bs, cv, tag);
1213
		break;
2 7u83 1214
	}
1215
 
6 7u83 1216
	case type_compound_tag: {
1217
		/* Class types */
1218
		CLASS_TYPE ct = NULL_ctype;
1219
		t = load_ctype(bs, def, &ct);
1220
		if (!IS_NULL_type(t)) {
1221
			if (cv)t = qualify_type(t, cv, 0);
1222
			break;
1223
		}
1224
		SPEC_ERROR();
1225
		break;
2 7u83 1226
	}
1227
 
6 7u83 1228
	case type_enumerate_tag: {
1229
		/* Enumeration types */
1230
		ENUM_TYPE et = NULL_etype;
1231
		t = load_etype(bs, def, &et);
1232
		if (!IS_NULL_type(t)) {
1233
			if (cv)t = qualify_type(t, cv, 0);
1234
			break;
1235
		}
1236
		SPEC_ERROR();
1237
		break;
2 7u83 1238
	}
1239
 
6 7u83 1240
	case type_token_tag: {
1241
		IDENTIFIER tok = load_use(bs, null_tag);
1242
		if (!IS_NULL_id(tok)) {
1243
			LIST(TOKEN)args = load_tok_list(bs, 1);
1244
			MAKE_type_token(cv, tok, args, t);
1245
			break;
1246
		}
1247
		SPEC_ERROR();
1248
		break;
2 7u83 1249
	}
1250
 
6 7u83 1251
	case type_templ_tag: {
1252
		TOKEN tok = load_tok(bs, 0);
1253
		if (!IS_NULL_tok(tok) && IS_tok_templ(tok)) {
1254
			TYPE s = load_type(bs, def);
1255
			if (!IS_NULL_type(s)) {
1256
				int fix = DE_BOOL(bs);
1257
				MAKE_type_templ(cv, tok, s, fix, t);
1258
				break;
1259
			}
2 7u83 1260
		}
6 7u83 1261
		SPEC_ERROR();
1262
		break;
2 7u83 1263
	}
1264
 
6 7u83 1265
	case type_instance_tag: {
1266
		IDENTIFIER tid = load_use(bs, null_tag);
1267
		if (!IS_NULL_id(tid)) {
1268
			IDENTIFIER id = load_use(bs, null_tag);
1269
			if (!IS_NULL_id(id)) {
1270
				DECL_SPEC acc = load_dspec(bs);
1271
				MAKE_type_instance(cv, id, acc, t);
1272
				COPY_id(type_name(t), tid);
1273
				break;
1274
			}
2 7u83 1275
		}
6 7u83 1276
		SPEC_ERROR();
1277
		break;
2 7u83 1278
	}
1279
 
6 7u83 1280
	case type_dummy_tag: {
1281
		int tok = (int)DE_INT(bs);
1282
		MAKE_type_dummy(cv, tok, t);
1283
		break;
2 7u83 1284
	}
1285
 
6 7u83 1286
	case type_error_tag: {
1287
		t = type_error;
1288
		if (cv)t = qualify_type(t, cv, 0);
1289
		break;
2 7u83 1290
	}
6 7u83 1291
	}
1292
	return (t);
2 7u83 1293
}
1294
 
1295
 
1296
/*
1297
    READ A LIST OF TYPES
1298
 
1299
    This routine reads a list of types from the bitstream bs.
1300
*/
1301
 
6 7u83 1302
static LIST(TYPE)
1303
load_type_list(BITSTREAM *bs)
2 7u83 1304
{
6 7u83 1305
	LIST(TYPE)p = NULL_list(TYPE);
1306
	while (DE_BOOL(bs)) {
1307
		TYPE t = load_type(bs, NULL_id);
1308
		if (spec_error) {
1309
			break;
1310
		}
1311
		CONS_type(t, p, p);
1312
	}
1313
	p = REVERSE_list(p);
1314
	return (p);
2 7u83 1315
}
1316
 
1317
 
1318
/*
1319
    READ A TOKEN
1320
 
1321
    This routine reads a token from the bitstream bs.  If def is true
1322
    then the token value is also read.
1323
*/
1324
 
6 7u83 1325
static TOKEN
1326
load_tok(BITSTREAM *bs, int def)
2 7u83 1327
{
6 7u83 1328
	unsigned tag;
1329
	TOKEN tok = NULL_tok;
1330
	unsigned n = DE_BITS(bs, BITS_tok);
1331
	if (n == 0) {
1332
		return (tok);
2 7u83 1333
	}
6 7u83 1334
	if (n > ORDER_tok) {
1335
		SPEC_ERROR();
1336
		return (tok);
2 7u83 1337
	}
6 7u83 1338
	ASSERT(ORDER_tok == 10);
1339
	tag = n - 1;
1340
	switch (tag) {
1341
	case tok_exp_tag: {
1342
		TYPE t = load_type(bs, NULL_id);
1343
		if (!IS_NULL_type(t)) {
1344
			EXP e = NULL_exp;
1345
			int c = DE_BOOL(bs);
1346
			if (def) {
1347
				e = load_exp(bs, t);
1348
			}
1349
			MAKE_tok_exp(t, c, e, tok);
1350
			break;
1351
		}
1352
		SPEC_ERROR();
1353
		break;
2 7u83 1354
	}
6 7u83 1355
	case tok_stmt_tag: {
1356
		EXP e = NULL_exp;
1357
		if (def) {
1358
			e = load_exp(bs, type_void);
1359
		}
1360
		MAKE_tok_stmt(e, tok);
1361
		break;
2 7u83 1362
	}
6 7u83 1363
	case tok_nat_tag:
1364
	case tok_snat_tag: {
1365
		NAT m = NULL_nat;
1366
		if (def) {
1367
			m = load_nat(bs);
1368
		}
1369
		MAKE_tok_nat_etc(tag, m, tok);
1370
		break;
2 7u83 1371
	}
6 7u83 1372
	case tok_type_tag: {
1373
		BASE_TYPE kind = load_btype(bs);
1374
		TYPE s = load_type(bs, NULL_id);
1375
		TYPE t = NULL_type;
1376
		if (def) {
1377
			t = load_type(bs, NULL_id);
2 7u83 1378
		}
6 7u83 1379
		MAKE_tok_type(kind, t, tok);
1380
		COPY_type(tok_type_alt(tok), s);
1381
		break;
2 7u83 1382
	}
6 7u83 1383
	case tok_func_tag: {
1384
		TYPE t = load_type(bs, NULL_id);
1385
		if (!IS_NULL_type(t) && IS_type_func(t)) {
1386
			TOKEN proc = load_tok(bs, 0);
1387
			IDENTIFIER id = NULL_id;
1388
			if (def) {
1389
				id = load_use(bs, null_tag);
1390
			}
1391
			MAKE_tok_func(t, tok);
1392
			COPY_tok(tok_func_proc(tok), proc);
1393
			COPY_id(tok_func_defn(tok), id);
1394
			break;
1395
		}
1396
		SPEC_ERROR();
1397
		break;
2 7u83 1398
	}
6 7u83 1399
	case tok_member_tag: {
1400
		TYPE s = load_type(bs, NULL_id);
1401
		if (!IS_NULL_type(s)) {
1402
			TYPE t = load_type(bs, NULL_id);
1403
			if (!IS_NULL_type(t)) {
1404
				OFFSET off = NULL_off;
1405
				if (def) {
1406
					off = load_off(bs);
1407
				}
1408
				MAKE_tok_member(s, t, off, tok);
1409
				break;
1410
			}
1411
		}
1412
		SPEC_ERROR();
1413
		break;
2 7u83 1414
	}
6 7u83 1415
	case tok_class_tag: {
1416
		TYPE t = load_type(bs, NULL_id);
1417
		if (!IS_NULL_type(t)) {
1418
			TYPE s = load_type(bs, NULL_id);
1419
			IDENTIFIER id = NULL_id;
1420
			if (def) {
1421
				id = load_use(bs, null_tag);
1422
			}
1423
			MAKE_tok_class(t, id, tok);
1424
			COPY_type(tok_class_alt(tok), s);
1425
			break;
1426
		}
1427
		SPEC_ERROR();
1428
		break;
2 7u83 1429
	}
6 7u83 1430
	case tok_proc_tag: {
1431
		TOKEN res;
1432
		NAMESPACE pns;
1433
		int key = lex_identifier;
1434
		LIST(IDENTIFIER)bids;
1435
		LIST(IDENTIFIER)pids;
1436
		begin_param(NULL_id);
1437
		pns = crt_namespace;
1438
		bids = load_param_list(bs, pns, id_token_tag);
1439
		if (DE_BOOL(bs)) {
1440
			pids = bids;
1441
		} else {
1442
			pids = load_use_list(bs);
1443
		}
1444
		end_param();
1445
		res = load_tok(bs, def);
1446
		if (DE_BOOL(bs)) {
1447
			key = load_lex(bs);
1448
		}
1449
		if (!IS_NULL_tok(res)) {
1450
			MAKE_tok_proc(res, pns, key, tok);
1451
			tok = cont_proc_token(tok, bids, pids);
1452
		} else {
1453
			SPEC_ERROR();
1454
		}
1455
		break;
1456
	}
1457
	case tok_templ_tag: {
1458
		DECL_SPEC ex = load_dspec(bs);
1459
		if (DE_BOOL(bs)) {
1460
			NAMESPACE pns;
1461
			LIST(TOKEN)dargs;
1462
			LIST(IDENTIFIER)pids;
1463
			pns = make_namespace(NULL_id, nspace_templ_tag, 0);
1464
			pids = load_param_list(bs, pns, id_token_tag);
1465
			set_proc_token(pids);
1466
			dargs = load_tok_list(bs, 1);
1467
			MAKE_tok_templ(ex, pns, tok);
1468
			COPY_list(tok_templ_pids(tok), pids);
1469
			COPY_list(tok_templ_dargs(tok), dargs);
1470
		} else {
1471
			MAKE_tok_templ(ex, NULL_nspace, tok);
1472
		}
1473
		break;
1474
	}
1475
	}
1476
	return (tok);
2 7u83 1477
}
1478
 
1479
 
1480
/*
1481
    READ AN IDENTIFIER
1482
 
1483
    This routine reads a member of the namespace ns from the bitstream bs.
1484
*/
1485
 
6 7u83 1486
static IDENTIFIER
1487
load_id(BITSTREAM *bs, NAMESPACE ns)
2 7u83 1488
{
6 7u83 1489
	ulong d;
1490
	HASHID nm;
1491
	unsigned n;
1492
	unsigned tag;
1493
	DECL_SPEC ds;
1494
	IDENTIFIER qid;
1495
	IDENTIFIER *pid;
1496
	IDENTIFIER id = NULL_id;
1497
	IDENTIFIER lid = NULL_id;
2 7u83 1498
 
6 7u83 1499
	/* Read identifier number */
1500
	d = DE_INT(bs);
1501
	if (d == 0) {
1502
		/* Null identifiers */
1503
		return (id);
1504
	}
2 7u83 1505
 
6 7u83 1506
	/* Read identifier tag */
1507
	n = DE_BITS(bs, BITS_id);
1508
	if (n == 0) {
1509
		/* Null identifiers */
1510
		return (id);
1511
	}
1512
	if (n > ORDER_id) {
1513
		SPEC_ERROR();
1514
		return (id);
1515
	}
2 7u83 1516
 
6 7u83 1517
	/* Check previous look-up */
1518
	pid = id_lookup(d);
1519
	qid = *pid;
1520
	if (!IS_NULL_id(qid)) {
1521
		if (!IS_id_pending(qid)) {
1522
			SPEC_ERROR();
1523
			qid = NULL_id;
1524
		}
2 7u83 1525
	}
1526
 
6 7u83 1527
	/* Read identifier independent information */
1528
	nm = load_hashid(bs, ns);
1529
	if (IS_NULL_hashid(nm)) {
1530
		SPEC_ERROR();
1531
		return (id);
1532
	}
1533
	tag = n - 1;
1534
	ds = load_dspec(bs);
1535
	load_loc(bs);
1536
	if (DE_BOOL(bs)) {
1537
		/* Read alias */
1538
		lid = load_use(bs, tag);
1539
	}
2 7u83 1540
 
6 7u83 1541
	/* Read identifier dependent information */
1542
	ASSERT(ORDER_id == 28);
1543
	switch (tag) {
2 7u83 1544
 
6 7u83 1545
	case id_dummy_tag: {
1546
		id = DEREF_id(hashid_id(nm));
1547
		id = underlying_id(id);
1548
		break;
2 7u83 1549
	}
1550
 
6 7u83 1551
	case id_keyword_tag:
1552
	case id_iso_keyword_tag:
1553
	case id_reserved_tag: {
1554
		int key = load_lex(bs);
1555
		id = make_keyword(nm, key, NULL_id);
1556
		break;
2 7u83 1557
	}
1558
 
6 7u83 1559
	case id_builtin_tag: {
1560
		TYPE r = load_type(bs, NULL_id);
1561
		if (!IS_NULL_type(r)) {
1562
			LIST(TYPE)p = load_type_list(bs);
1563
			MAKE_id_builtin(nm, ds, ns, crt_loc, r, p, id);
1564
			break;
1565
		}
1566
		SPEC_ERROR();
1567
		break;
2 7u83 1568
	}
1569
 
6 7u83 1570
	case id_obj_macro_tag: {
1571
		PPTOKEN *def = load_pptoks(bs);
1572
		MAKE_id_obj_macro(nm, ds, ns, crt_loc, def, id);
1573
		break;
2 7u83 1574
	}
1575
 
6 7u83 1576
	case id_func_macro_tag: {
1577
		PPTOKEN *def = load_pptoks(bs);
1578
		LIST(HASHID)pars = load_hashid_list(bs, NULL_nspace);
1579
		unsigned npars = LENGTH_list(pars);
1580
		MAKE_id_func_macro(nm, ds, ns, crt_loc, def, pars, npars, id);
1581
		break;
2 7u83 1582
	}
1583
 
6 7u83 1584
	case id_predicate_tag: {
1585
		/* NOT YET IMPLEMENTED */
1586
		MAKE_id_undef(nm, ds, ns, crt_loc, id);
1587
		break;
2 7u83 1588
	}
1589
 
6 7u83 1590
	case id_class_name_tag: {
1591
		/* Class names */
1592
		TYPE t = type_error;
1593
		MAKE_id_class_name(nm, ds, ns, crt_loc, t, id);
1594
		if (ds & dspec_implicit) {
1595
			if (!IS_NULL_id(lid) && IS_id_class_name(lid)) {
1596
				t = DEREF_type(id_class_name_defn(lid));
1597
			}
1598
		} else {
1599
			*pid = id;
1600
			t = load_type(bs, id);
2 7u83 1601
		}
6 7u83 1602
		if (type_tag(t) == type_compound_tag) {
1603
			COPY_type(id_class_name_defn(id), t);
1604
			break;
1605
		}
1606
		SPEC_ERROR();
1607
		break;
2 7u83 1608
	}
1609
 
6 7u83 1610
	case id_enum_name_tag: {
1611
		/* Enumeration names */
1612
		TYPE t = type_error;
1613
		MAKE_id_enum_name(nm, ds, ns, crt_loc, t, id);
1614
		t = load_type(bs, id);
1615
		if (type_tag(t) == type_enumerate_tag) {
1616
			COPY_type(id_enum_name_defn(id), t);
1617
			break;
1618
		}
1619
		SPEC_ERROR();
1620
		break;
2 7u83 1621
	}
1622
 
6 7u83 1623
	case id_class_alias_tag:
1624
	case id_enum_alias_tag:
1625
	case id_type_alias_tag: {
1626
		/* Type aliases */
1627
		TYPE t = load_type(bs, NULL_id);
1628
		if (!IS_NULL_type(t)) {
1629
			id = make_typedef(ns, nm, t, ds);
1630
			if (TAG_id(id) != tag) {
1631
				SPEC_ERROR();
1632
			}
1633
			break;
1634
		}
1635
		SPEC_ERROR();
1636
		break;
2 7u83 1637
	}
1638
 
6 7u83 1639
	case id_nspace_name_tag: {
1640
		/* Namespace names */
1641
		NAMESPACE pns = NULL_nspace;
1642
		unsigned ntag = nspace_named_tag;
1643
		if (IS_hashid_anon(nm)) {
1644
			ntag = nspace_unnamed_tag;
1645
		}
1646
		MAKE_id_nspace_name(nm, ds, ns, crt_loc, pns, id);
1647
		pns = make_namespace(id, ntag, 50);
1648
		COPY_nspace(id_nspace_name_defn(id), pns);
1649
		break;
2 7u83 1650
	}
1651
 
6 7u83 1652
	case id_nspace_alias_tag: {
1653
		/* Namespace aliases */
1654
		IDENTIFIER nid = load_use(bs, id_nspace_name_tag);
1655
		if (!IS_NULL_id(nid)) {
1656
			NAMESPACE pns = find_namespace(nid);
1657
			if (!IS_NULL_nspace(pns)) {
1658
				MAKE_id_nspace_alias(nm, ds, ns, crt_loc, pns,
1659
						     id);
1660
				break;
1661
			}
2 7u83 1662
		}
6 7u83 1663
		SPEC_ERROR();
1664
		break;
2 7u83 1665
	}
1666
 
6 7u83 1667
	case id_variable_tag:
1668
	case id_parameter_tag:
1669
	case id_stat_member_tag: {
1670
		TYPE t = load_type(bs, NULL_id);
1671
		if (!IS_NULL_type(t)) {
1672
			MAKE_id_variable_etc(tag, nm, ds, ns, crt_loc, t, id);
1673
			break;
1674
		}
1675
		SPEC_ERROR();
1676
		break;
2 7u83 1677
	}
1678
 
6 7u83 1679
	case id_function_tag:
1680
	case id_mem_func_tag:
1681
	case id_stat_mem_func_tag: {
1682
		TYPE t = load_type(bs, NULL_id);
1683
		if (!IS_NULL_type(t)) {
1684
			MAKE_id_function_etc(tag, nm, ds, ns, crt_loc, t,
1685
					     NULL_id, id);
1686
			*pid = id;
1687
			if (DE_BOOL(bs)) {
1688
				/* Read function template form */
1689
				TYPE form = load_type(bs, NULL_id);
1690
				COPY_type(id_function_etc_form(id), form);
1691
			}
1692
			break;
2 7u83 1693
		}
6 7u83 1694
		SPEC_ERROR();
1695
		break;
2 7u83 1696
	}
1697
 
6 7u83 1698
	case id_member_tag: {
1699
		TYPE t = load_type(bs, NULL_id);
1700
		if (!IS_NULL_type(t)) {
1701
			MAKE_id_member(nm, ds, ns, crt_loc, t, id);
1702
			break;
1703
		}
1704
		SPEC_ERROR();
1705
		break;
2 7u83 1706
	}
1707
 
6 7u83 1708
	case id_enumerator_tag: {
1709
		TYPE t = load_type(bs, NULL_id);
1710
		if (!IS_NULL_type(t)) {
1711
			EXP e = load_exp(bs, t);
1712
			if (!IS_NULL_exp(e)) {
1713
				MAKE_id_enumerator(nm, ds, ns, crt_loc, t, e,
1714
						   id);
1715
				break;
1716
			}
2 7u83 1717
		}
6 7u83 1718
		SPEC_ERROR();
1719
		break;
2 7u83 1720
	}
1721
 
6 7u83 1722
	case id_label_tag:
1723
	case id_weak_param_tag: {
1724
		/* NOT YET IMPLEMENTED */
1725
		MAKE_id_undef(nm, ds, ns, crt_loc, id);
1726
		break;
2 7u83 1727
	}
1728
 
6 7u83 1729
	case id_token_tag: {
1730
		TOKEN tok = load_tok(bs, 0);
1731
		if (!IS_NULL_tok(tok)) {
1732
			MAKE_id_token(nm, ds, ns, crt_loc, tok, NULL_id, id);
1733
			COPY_id(id_token_alt(id), id);
1734
			break;
1735
		}
1736
		break;
2 7u83 1737
	}
1738
 
6 7u83 1739
	case id_ambig_tag: {
1740
		LIST(IDENTIFIER)ids = load_use_list(bs);
1741
		int over = DE_BOOL(bs);
1742
		MAKE_id_ambig(nm, ds, ns, crt_loc, ids, over, id);
1743
		break;
2 7u83 1744
	}
1745
 
6 7u83 1746
	case id_undef_tag: {
1747
		MAKE_id_undef(nm, ds, ns, crt_loc, id);
1748
		if (DE_BOOL(bs)) {
1749
			TYPE form = load_type(bs, NULL_id);
1750
			COPY_type(id_undef_form(id), form);
1751
		}
1752
		break;
2 7u83 1753
	}
1754
 
6 7u83 1755
	case id_pending_tag: {
1756
		/* This shouldn't happen */
1757
		SPEC_ERROR();
1758
		break;
2 7u83 1759
	}
6 7u83 1760
	}
2 7u83 1761
 
6 7u83 1762
	/* Set identifier look up */
1763
	if (!IS_NULL_id(id)) {
1764
		if (!IS_NULL_id(lid)) {
1765
			COPY_id(id_alias(id), lid);
1766
		}
1767
		if (!IS_NULL_id(qid)) {
1768
			COPY_id(id_alias(qid), id);
1769
			ids_pending--;
1770
		}
1771
		*pid = id;
2 7u83 1772
	}
6 7u83 1773
	return (id);
2 7u83 1774
}
1775
 
1776
 
1777
/*
1778
    READ NAMESPACE MEMBERS
1779
 
1780
    This routine reads the members of a class or namespace and the instances
1781
    of a template class or template function.
1782
*/
1783
 
6 7u83 1784
static void
1785
load_members(BITSTREAM *bs, IDENTIFIER id)
2 7u83 1786
{
6 7u83 1787
	switch (TAG_id(id)) {
1788
	case id_class_name_tag: {
1789
		/* Read class members */
1790
		DECL_SPEC ds = DEREF_dspec(id_storage(id));
1791
		if (!(ds & dspec_implicit)) {
1792
			int templ = 0;
1793
			TYPE t = DEREF_type(id_class_name_defn(id));
1794
			while (IS_type_templ(t)) {
1795
				templ = 1;
1796
				t = DEREF_type(type_templ_defn(t));
1797
			}
1798
			if (IS_type_compound(t)) {
1799
				CLASS_TYPE ct =
1800
				    DEREF_ctype(type_compound_defn(t));
1801
				NAMESPACE cns = DEREF_nspace(ctype_member(ct));
1802
				load_nspace(bs, cns, 0);
1803
				if (templ) {
1804
					/* Read template class instances */
1805
					NAMESPACE ns =
1806
					    DEREF_nspace(id_parent(id));
1807
					load_nspace(bs, ns, 1);
1808
				}
1809
			}
2 7u83 1810
		}
6 7u83 1811
		break;
1812
	}
1813
	case id_nspace_name_tag: {
1814
		/* Read namespace members */
1815
		NAMESPACE cns = DEREF_nspace(id_nspace_name_defn(id));
1816
		load_nspace(bs, cns, 0);
1817
		break;
1818
	}
1819
	case id_function_tag:
1820
	case id_mem_func_tag:
1821
	case id_stat_mem_func_tag: {
1822
		TYPE t = DEREF_type(id_function_etc_type(id));
1823
		if (IS_type_templ(t)) {
1824
			/* Read template function instances */
1825
			NAMESPACE ns = DEREF_nspace(id_parent(id));
1826
			load_nspace(bs, ns, 1);
2 7u83 1827
		}
6 7u83 1828
		break;
2 7u83 1829
	}
1830
	}
6 7u83 1831
	return;
2 7u83 1832
}
1833
 
1834
 
1835
/*
1836
    READ A LIST OF NAMESPACE MEMBERS
1837
 
1838
    This routine reads the members of the namespace ns from the bitstream
1839
    bs.  The list is terminated by a null identifier.  The routine is
1840
    also used with inst true to read a list of template instances.
1841
*/
1842
 
6 7u83 1843
static void
1844
load_nspace(BITSTREAM *bs, NAMESPACE ns, int inst)
2 7u83 1845
{
6 7u83 1846
	int v = verbose;
1847
	for (;;) {
1848
		/* Read identifier */
1849
		IDENTIFIER id = load_id(bs, ns);
1850
		if (IS_NULL_id(id) || spec_error) {
1851
			break;
1852
		}
1853
		if (v) {
1854
			commentary(id);
1855
		}
2 7u83 1856
 
6 7u83 1857
		/* Set namespace member */
1858
		if (!IS_NULL_nspace(ns) && !inst) {
1859
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
1860
			if ((ds & dspec_extern) || IS_nspace_block_etc(ns)) {
1861
				/* External linkage */
1862
				HASHID nm = DEREF_hashid(id_name(id));
1863
				MEMBER mem = search_member(ns, nm, 1);
1864
				IDENTIFIER pid = DEREF_id(member_id(mem));
1865
				if (is_tagged_type(id)) {
1866
					/* Type members */
1867
					COPY_id(member_alt(mem), id);
1868
					if (IS_NULL_id(pid)) {
1869
						COPY_id(member_id(mem), id);
1870
					}
1871
				} else {
1872
					/* Non-type members */
1873
					if (!IS_NULL_id(pid) &&
1874
					    IS_id_function_etc(pid)) {
1875
						if (IS_id_function_etc(id)) {
1876
							/* Allow for overloaded
1877
							 * functions */
1878
							COPY_id(id_function_etc_over(id), pid);
1879
						}
1880
					}
1881
					COPY_id(member_id(mem), id);
1882
				}
1883
			} else {
1884
				/* Internal linkage */
1885
				LIST(IDENTIFIER)ids;
1886
				ids = DEREF_list(nspace_named_etc_extra(ns));
1887
				CONS_id(id, ids, ids);
1888
				COPY_list(nspace_named_etc_extra(ns), ids);
2 7u83 1889
			}
1890
		}
6 7u83 1891
 
1892
		/* Check for classes, namespaces and templates */
1893
		load_members(bs, id);
2 7u83 1894
	}
6 7u83 1895
	return;
2 7u83 1896
}
1897
 
1898
 
1899
/*
1900
    READ A SPEC FILE
1901
 
1902
    This routine reads a spec file from the input file.  It returns zero
1903
    for a legal spec file.  The identifiers are initially read into a
1904
    dummy namespace which is then merged with the existing namespace.
1905
*/
1906
 
6 7u83 1907
int
1908
read_spec(void)
2 7u83 1909
{
6 7u83 1910
	/* Read file identifier */
1911
	int e = 0;
1912
	char buff[20];
1913
	CONST char *msg = NULL;
1914
	NAMESPACE gns = NULL_nspace;
1915
	BITSTREAM *bs = start_bitstream(input_file, NULL_gen_ptr);
1916
	unsigned c1 = DE_BITS(bs, BYTE_SIZE);
1917
	unsigned c2 = DE_BITS(bs, BYTE_SIZE);
1918
	unsigned c3 = DE_BITS(bs, BYTE_SIZE);
1919
	unsigned c4 = DE_BITS(bs, BYTE_SIZE);
1920
	if (c1 == ascii_T && c2 == ascii_D && c3 == ascii_F && c4 == ascii_K) {
1921
		unsigned long n1 = DE_INT(bs);
1922
		unsigned long n2 = DE_INT(bs);
1923
		unsigned long n3 = DE_INT(bs);
1924
		DE_ALIGN(bs);
1925
		if (n1 == SPEC_major && n2 <= SPEC_minor) {
1926
			/* Read main file body */
1927
			if (n3 > LANGUAGE_CPP) {
1928
				msg = "bad source language";
1929
				e = 1;
1930
			}
1931
			if (e == 0) {
1932
				int d = do_dump;
1933
				do_dump = 0;
1934
				spec_error = 0;
1935
				gns = make_global_nspace("<global>", 50);
1936
				load_nspace(bs, gns, 0);
1937
				if (ids_pending) {
1938
					SPEC_ERROR();
1939
				}
1940
				crt_class = NULL_ctype;
1941
				e = spec_error;
1942
				if (e == 0) {
1943
					DE_ALIGN(bs);
1944
					if (!de_eof(bs)) {
1945
						msg = "end of file expected";
1946
						e = 1;
1947
					}
1948
				} else {
1949
					sprintf_v(buff, "code %d", e);
1950
					msg = buff;
1951
				}
1952
				do_dump = d;
1953
			}
2 7u83 1954
		} else {
6 7u83 1955
			msg = "bad version number";
1956
			e = 1;
2 7u83 1957
		}
1958
	} else {
6 7u83 1959
		msg = "bad magic number";
1960
		e = 1;
2 7u83 1961
	}
1962
 
6 7u83 1963
	/* Check for errors */
1964
	if (msg) {
1965
		fail(ERR_fail_spec_bad(input_name, ustrlit(msg)));
1966
	}
1967
	if (!output_spec) {
1968
		e = 1;
1969
	}
1970
	xfree_nof(id_table);
1971
	id_table_size = 0;
1972
	id_table = NULL;
1973
	spec_error = 0;
2 7u83 1974
 
6 7u83 1975
	/* Merge read identifiers */
1976
	if (!IS_NULL_nspace(gns)) {
1977
		merge_namespaces(global_namespace, gns);
1978
	}
1979
	return (e);
2 7u83 1980
}