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 "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 "nspace_ops.h"
75
#include "tok_ops.h"
76
#include "type_ops.h"
77
#include "virt_ops.h"
78
#include "error.h"
79
#include "tdf.h"
80
#include "access.h"
81
#include "basetype.h"
82
#include "buffer.h"
83
#include "capsule.h"
84
#include "check.h"
85
#include "class.h"
86
#include "compile.h"
87
#include "constant.h"
88
#include "convert.h"
89
#include "destroy.h"
90
#include "diag.h"
91
#include "diag2.h"
92
#include "encode.h"
93
#include "exception.h"
94
#include "exp.h"
95
#include "file.h"
96
#include "function.h"
97
#include "hash.h"
98
#include "mangle.h"
99
#include "namespace.h"
100
#include "print.h"
101
#include "shape.h"
102
#include "stmt.h"
103
#include "struct.h"
104
#include "syntax.h"
105
#include "throw.h"
106
#include "tok.h"
107
#include "typeid.h"
108
#include "ustring.h"
109
#include "virtual.h"
7 7u83 110
#if (TDF_OUTPUT && TDF_NEW_DIAG)
2 7u83 111
 
112
 
113
/*
114
    DWARF TAG VALUES
115
 
116
    Certain values used within the diagnostic format are actually DWARF
117
    tag values.  These macros give the values used.
118
*/
119
 
120
#define DWARF_LANG_TRAD_C		1
121
#define DWARF_LANG_ANSI_C		2
122
#define DWARF_LANG_CPP			4
123
 
124
#if LANGUAGE_C
125
#define DWARF_LANG			DWARF_LANG_ANSI_C
126
#define DWARF_LANG_NOT			DWARF_LANG_CPP
127
#else
128
#define DWARF_LANG			DWARF_LANG_CPP
129
#define DWARF_LANG_NOT			DWARF_LANG_ANSI_C
130
#endif
131
 
132
#define DWARF_EXPLICIT			1
133
#define DWARF_IMPLICIT			2
134
#define DWARF_VIRT_EXPL			3
135
#define DWARF_VIRT_IMPL			4
136
#define DWARF_CONSTR			5
137
#define DWARF_VIRT_CONSTR		6
138
#define DWARF_DESTR			7
139
#define DWARF_VIRT_DESTR		8
140
 
141
#define DWARF_USING_DIR			3
142
#define DWARF_USING_DECL		5
143
 
144
#define DWARF_ORDINARY			1
145
#define DWARF_NO_CALL			2
146
#define DWARF_RUN_TIME			3
147
 
148
#define DWARF_CASE_SENSITIVE		0
149
 
150
 
151
/*
152
    ENCODE A DIAGNOSTIC FILE NAME
153
 
154
    This routine adds the diagnostic file name given by posn to the
155
    bitstream bs.
156
*/
157
 
7 7u83 158
static BITSTREAM *
159
enc_dg_filename(BITSTREAM *bs, PTR(POSITION)posn)
2 7u83 160
{
7 7u83 161
	ulong n = DEREF_ulong(posn_tok(posn));
162
	if (n == LINK_NONE) {
163
		BITSTREAM *ts;
164
		string bn = DEREF_string(posn_base(posn));
165
		string mn = ustrlit(find_machine());
166
		ulong date = DEREF_ulong(posn_datestamp(posn));
167
		n = capsule_no(NULL_string, VAR_token);
168
		COPY_ulong(posn_tok(posn), n);
169
		if (!output_date) {
170
			date = 0;
171
		}
172
		ts = enc_tokdef_start(n, "Q", NIL(ulong), 0);
173
		ENC_make_dg_filename(ts);
174
		ENC_make_nat(ts);
175
		ENC_INT(ts, date);
176
		ENC_make_string(ts);
177
		ts = enc_ustring(ts, mn);
178
		ENC_make_string(ts);
179
		if (is_full_pathname(bn)) {
180
			ts = enc_ustring(ts, ustrlit(""));
181
		} else {
182
			string dn = DEREF_string(posn_dir(posn));
183
			if (dn == NULL) {
184
				string en = ustrlit(find_cwd());
185
				ts = enc_ustring(ts, en);
186
			} else if (is_full_pathname(dn)) {
187
				ts = enc_ustring(ts, dn);
188
			} else {
189
				string en = ustrlit(find_cwd());
190
				BUFFER *bf = clear_buffer(&incl_buff, NIL(FILE));
191
				bfprintf(bf, "%s/%s", en, dn);
192
				ts = enc_ustring(ts, bf->start);
193
			}
194
		}
195
		ENC_make_string(ts);
196
		ts = enc_ustring(ts, bn);
197
		enc_tokdef_end(n, ts);
2 7u83 198
	}
199
 
7 7u83 200
	/* Encode token application */
201
	ENC_dg_filename_apply_token(bs);
202
	n = link_no(bs, n, VAR_token);
203
	ENC_make_tok(bs, n);
204
	ENC_LEN_SMALL(bs, 0);
205
	return (bs);
2 7u83 206
}
207
 
208
 
209
/*
210
    ENCODE A DIAGNOSTIC SOURCE POSITION
211
 
212
    This routine adds the source position given by the span from p to q
213
    to the bitstream bs.
214
*/
215
 
7 7u83 216
static BITSTREAM *
217
enc_dg_loc(BITSTREAM *bs, PTR(LOCATION)p, PTR(LOCATION)q)
2 7u83 218
{
7 7u83 219
	if (IS_NULL_ptr(p) || IS_NULL_ptr(q)) {
220
		ENC_dg_null_sourcepos(bs);
2 7u83 221
	} else {
7 7u83 222
		ulong lp = DEREF_ulong(loc_line(p));
223
		ulong lq = DEREF_ulong(loc_line(q));
224
		ulong cp = DEREF_ulong(loc_column(p));
225
		ulong cq = DEREF_ulong(loc_column(q));
226
		PTR(POSITION)fp = DEREF_ptr(loc_posn(p));
227
		PTR(POSITION)fq = DEREF_ptr(loc_posn(q));
228
		if (EQ_ptr(p, q)) {
229
			ENC_dg_mark_sourcepos(bs);
230
		} else if (lp == lq && cp == cq && EQ_ptr(fp, fq)) {
231
			ENC_dg_mark_sourcepos(bs);
232
			q = p;
233
		} else {
234
			ENC_dg_span_sourcepos(bs);
235
		}
236
		bs = enc_dg_filename(bs, fp);
237
		ENC_make_nat(bs);
238
		ENC_INT(bs, lp);
239
		ENC_make_nat(bs);
240
		ENC_INT(bs, cp);
241
		if (!EQ_ptr(p, q)) {
242
			if (EQ_ptr(fp, fq)) {
243
				ENC_OFF(bs);
244
			} else {
245
				ENC_ON(bs);
246
				bs = enc_dg_filename(bs, fq);
247
			}
248
			ENC_make_nat(bs);
249
			ENC_INT(bs, lq);
250
			ENC_make_nat(bs);
251
			ENC_INT(bs, cq);
252
		}
2 7u83 253
	}
7 7u83 254
	return (bs);
2 7u83 255
}
256
 
257
 
258
/*
259
    FIND THE START OR END OF A BLOCK
260
 
261
    This routine returns the location of the start or end of the block e.
262
*/
263
 
7 7u83 264
static
265
PTR(LOCATION)block_loc(EXP e, int end)
2 7u83 266
{
7 7u83 267
	PTR(LOCATION)loc = NULL_ptr(LOCATION);
268
	if (!IS_NULL_exp(e)) {
269
		if (IS_exp_solve_stmt(e)) {
270
			e = DEREF_exp(exp_solve_stmt_body(e));
271
		}
272
		if (IS_exp_sequence(e)) {
273
			EXP a;
274
			LIST(EXP)p;
275
			if (end) {
276
				p = DEREF_list(exp_sequence_last(e));
277
			} else {
278
				p = DEREF_list(exp_sequence_first(e));
279
			}
280
			a = DEREF_exp(HEAD_list(p));
281
			if (!IS_NULL_exp(a) && IS_exp_location(a)) {
282
				loc = exp_location_end(a);
283
			}
284
		}
2 7u83 285
	}
7 7u83 286
	return (loc);
2 7u83 287
}
288
 
289
 
290
/*
291
    ENCODE THE START OF A NAMESPACE
292
 
293
    This routine adds the start of a list of names derived from the
294
    namespace ns to the bitstream bs.
295
*/
296
 
7 7u83 297
static BITSTREAM *
298
enc_dg_namelist(BITSTREAM *bs, NAMESPACE ns)
2 7u83 299
{
7 7u83 300
	ulong n = capsule_no(NULL_string, VAR_dgtag);
301
	IDENTIFIER id = DEREF_id(nspace_name(ns));
302
	if (!IS_NULL_id(id)) {
303
		COPY_ulong(id_no(id), n);
304
	}
305
	record_usage(n, VAR_dgtag, USAGE_DEFN);
306
	ENC_dg_tag_namelist(bs);
307
	n = link_no(bs, n, VAR_dgtag);
308
	ENC_make_dg_tag(bs, n);
309
	ENC_make_dg_namelist(bs);
310
	ENC_LIST_SMALL(bs, 0);
311
	return (bs);
2 7u83 312
}
313
 
314
 
315
/*
316
    ENCODE COMMAND-LINE OPTIONS
317
 
318
    This routine adds the revelant command-line options as a list of
319
    strings to the bitstream bs.
320
*/
321
 
7 7u83 322
static BITSTREAM *
323
enc_dg_options(BITSTREAM *bs)
2 7u83 324
{
7 7u83 325
	unsigned n = 0;
326
	INCL_DIR *d = dir_path;
327
	while (d != NULL) {
328
		n++;
329
		d = d->next;
330
	}
331
	ENC_LIST(bs, n);
332
	d = dir_path;
333
	while (d != NULL) {
334
		BUFFER *bf = clear_buffer(&incl_buff, NIL(FILE));
335
		ENC_make_string(bs);
336
		bfprintf(bf, "-I%s", d->path);
337
		bs = enc_ustring(bs, bf->start);
338
		d = d->next;
339
	}
340
	return (bs);
2 7u83 341
}
342
 
343
 
344
/*
345
    ENCODE A COMPILATION UNIT
346
 
347
    This routine adds the top-level compilation unit information to the
348
    bitstream bs.  The current location gives information about the
349
    primary source file.
350
*/
351
 
7 7u83 352
BITSTREAM *
353
enc_dg_compilation(BITSTREAM *bs)
2 7u83 354
{
7 7u83 355
	LOCATION loc;
356
	ulong date = crt_time;
357
	string vers = report_version(0);
358
	ENC_make_dg_compilation(bs);
359
	bs = enc_dg_filename(bs, crt_loc.posn);
360
	ENC_LIST_SMALL(bs, 0);
361
	ENC_LIST_SMALL(bs, 0);
362
	loc = crt_loc;
363
	IGNORE set_crt_loc(ustrlit(""), 0);
364
	bs = enc_dg_filename(bs, crt_loc.posn);
365
	crt_loc = loc;
366
	if (!output_date) {
367
		date = 0;
368
	}
369
	ENC_make_nat(bs);
370
	ENC_INT(bs, date);
371
	ENC_make_nat(bs);
372
	ENC_INT(bs, DWARF_LANG);
373
	ENC_make_nat(bs);
374
	ENC_INT(bs, DWARF_CASE_SENSITIVE);
375
	ENC_make_string(bs);
376
	bs = enc_ustring(bs, vers);
377
	bs = enc_dg_options(bs);
378
	bs = enc_dg_namelist(bs, global_namespace);
2 7u83 379
 
7 7u83 380
	/* Output basic type definitions */
381
	if (output_builtin) {
382
		CV_SPEC cv;
383
		BUILTIN_TYPE n;
384
		for (n = ntype_char; n < ntype_ellipsis; n++) {
385
			TYPE t = type_builtin[n];
386
			switch (TAG_type(t)) {
387
			case type_integer_tag:
388
			case type_floating_tag: {
389
				IGNORE enc_dg_basetype(t, 1);
390
				break;
391
			}
392
			}
2 7u83 393
		}
7 7u83 394
		for (cv = cv_none; cv <= cv_qual; cv++) {
395
			TYPE t = qualify_type(type_void, cv, 0);
396
			MAKE_type_ptr(cv_none, t, t);
397
			IGNORE enc_dg_basetype(t, 1);
398
		}
2 7u83 399
	}
7 7u83 400
	return (bs);
2 7u83 401
}
402
 
403
 
404
/*
405
    ENCODE A DIAGNOSTIC ACCESS SPECIFIER
406
 
407
    This routine adds the access specifier component of ds to the bitstream
408
    bs.  All access specifiers are optional, the default being public.
409
*/
410
 
7 7u83 411
static BITSTREAM *
412
enc_dg_access(BITSTREAM *bs, DECL_SPEC ds)
2 7u83 413
{
7 7u83 414
	DECL_SPEC acc = (ds & dspec_access);
415
	if (acc == dspec_private) {
416
		ENC_ON(bs);
417
		ENC_dg_private_accessibility(bs);
418
	} else if (acc == dspec_protected) {
419
		ENC_ON(bs);
420
		ENC_dg_protected_accessibility(bs);
421
	} else {
422
		ENC_OFF(bs);
423
	}
424
	return (bs);
2 7u83 425
}
426
 
427
 
428
/*
429
    ENCODE A DIAGNOSTIC VIRTUAL SPECIFIER
430
 
431
    This routine adds the virtual specifier component of ds to the bitstream
432
    bs.  All virtual specifiers are optional, the default being non-virtual.
433
*/
434
 
7 7u83 435
static BITSTREAM *
436
enc_dg_virtual(BITSTREAM *bs, DECL_SPEC ds)
2 7u83 437
{
7 7u83 438
	if (ds & dspec_virtual) {
439
		ENC_ON(bs);
440
		if (ds & dspec_pure) {
441
			ENC_dg_abstract_virtuality(bs);
442
		} else {
443
			ENC_dg_virtual_virtuality(bs);
444
		}
2 7u83 445
	} else {
7 7u83 446
		ENC_OFF(bs);
2 7u83 447
	}
7 7u83 448
	return (bs);
2 7u83 449
}
450
 
451
 
452
/*
453
    ENCODE A TEMPLATE ARGUMENT
454
 
455
    Template arguments are encoded as dummy object or type names.
456
*/
457
 
7 7u83 458
static BITSTREAM *
459
enc_dg_token_arg(BITSTREAM *bs, TOKEN tok)
2 7u83 460
{
7 7u83 461
	int is_type = 0;
462
	EXP e = NULL_exp;
463
	TYPE t = NULL_type;
464
	if (!IS_NULL_tok(tok)) {
465
		switch (TAG_tok(tok)) {
466
		case tok_exp_tag: {
467
			int c = DEREF_int(tok_exp_constant(tok));
468
			if (c) {
469
				e = DEREF_exp(tok_exp_value(tok));
470
			}
471
			break;
472
		}
473
		case tok_nat_tag:
474
		case tok_snat_tag: {
475
			NAT n = DEREF_nat(tok_nat_etc_value(tok));
476
			e = calc_nat_value(n, type_sint);
477
			break;
478
		}
479
		case tok_type_tag: {
480
			t = DEREF_type(tok_type_value(tok));
481
			is_type = 1;
482
			break;
483
		}
484
		case tok_class_tag: {
485
			is_type = 1;
486
			break;
487
		}
488
		}
2 7u83 489
	}
7 7u83 490
	if (is_type) {
491
		/* Type name */
492
		ENC_dg_type_name(bs);
493
		ENC_OFF(bs);
494
		ENC_dg_null_sourcepos(bs);
495
		ENC_OFF(bs);
496
		if (!IS_NULL_type(t)) {
497
			ENC_ON(bs);
498
			bs = enc_dg_type(bs, t, 0);
499
		} else {
500
			ENC_OFF(bs);
501
		}
502
		ENC_false(bs);
503
		ENC_OFFS(bs, 2);
2 7u83 504
	} else {
7 7u83 505
		/* Object name */
506
		ENC_dg_object_name(bs);
507
		bs = enc_dg_name(bs, NULL_id, NULL_type);
508
		ENC_dg_null_sourcepos(bs);
509
		if (!IS_NULL_exp(e)) {
510
			t = DEREF_type(exp_type(e));
511
			bs = enc_dg_type(bs, t, 0);
512
			ENC_ON(bs);
513
			bs = enc_exp(bs, e);
514
		} else {
515
			bs = enc_dg_type(bs, t, 0);
516
			ENC_OFF(bs);
517
		}
518
		ENC_OFF(bs);
2 7u83 519
	}
7 7u83 520
	return (bs);
2 7u83 521
}
522
 
523
 
524
/*
525
    ENCODE AN ARTIFICIAL IDENTIFIER NAME
526
 
527
    This routine adds the artificial identifier name s to the bitstream bs.
528
*/
529
 
7 7u83 530
static BITSTREAM *
531
enc_dg_artificial(BITSTREAM *bs, CONST char *s)
2 7u83 532
{
7 7u83 533
	ENC_dg_artificial_idname(bs);
534
	ENC_ON(bs);
535
	ENC_make_string(bs);
536
	bs = enc_ustring(bs, ustrlit(s));
537
	return (bs);
2 7u83 538
}
539
 
540
 
541
/*
542
    ENCODE A DIAGNOSTIC IDENTIFIER NAME
543
 
544
    This routine adds the name of the identifier id to the bitstream bs.
545
*/
546
 
7 7u83 547
BITSTREAM *
548
enc_dg_name(BITSTREAM *bs, IDENTIFIER id, TYPE form)
2 7u83 549
{
7 7u83 550
	if (!IS_NULL_type(form) && IS_type_token(form)) {
551
		/* Template instance name */
552
		IDENTIFIER tid = DEREF_id(type_token_tok(form));
553
		LIST(TOKEN)args = DEREF_list(type_token_args(form));
554
		if (!IS_id_token(tid)) {
555
			ENC_dg_instance_idname(bs);
556
			ENC_OFF(bs);
557
			bs = enc_dg_name(bs, tid, NULL_type);
558
			bs = enc_dg_loc(bs, id_loc(tid), id_loc(tid));
559
			ENC_LIST(bs, LENGTH_list(args));
560
			while (!IS_NULL_list(args)) {
561
				TOKEN arg = DEREF_tok(HEAD_list(args));
562
				bs = enc_dg_token_arg(bs, arg);
563
				args = TAIL_list(args);
564
			}
565
			return (bs);
566
		}
2 7u83 567
	}
7 7u83 568
	if (!IS_NULL_id(id)) {
569
		string s;
570
		HASHID nm = DEREF_hashid(id_name(id));
571
		switch (TAG_hashid(nm)) {
572
		case hashid_name_tag: {
573
			/* Simple case */
574
			s = DEREF_string(hashid_name_text(nm));
575
			break;
2 7u83 576
		}
7 7u83 577
		case hashid_ename_tag: {
578
			/* Extended case */
579
			if (EQ_KEYWORD(nm, lex_this_Hname)) {
580
				bs = enc_dg_artificial(bs, "this");
581
				return (bs);
582
			}
583
			s = DEREF_string(hashid_ename_text(nm));
584
			break;
585
		}
586
		case hashid_anon_tag: {
587
			/* Anonymous case */
588
			ENC_dg_anonymous_idname(bs);
589
			ENC_OFF(bs);
590
			return (bs);
591
		}
592
		default : {
593
			/* Other cases */
594
			BUFFER *bf;
595
			bf = clear_buffer(&mangle_buff, NIL(FILE));
596
			IGNORE print_hashid(nm, 0, 0, bf, 0);
597
			s = bf->start;
598
			break;
599
		}
600
		}
601
		if (has_linkage(id)) {
602
			ENC_dg_external_idname(bs);
603
		} else {
604
			ENC_dg_sourcestring_idname(bs);
605
		}
606
		ENC_make_string(bs);
607
		bs = enc_ustring(bs, s);
2 7u83 608
	} else {
7 7u83 609
		ENC_dg_anonymous_idname(bs);
610
		ENC_OFF(bs);
2 7u83 611
	}
7 7u83 612
	return (bs);
2 7u83 613
}
614
 
615
 
616
/*
617
    ENCODE A DIAGNOSTIC IDENTIFIER
618
 
619
    This routine adds the diagnostics for the identifier id to the
620
    bitstream bs.  use indicates whether a declaration or a definition
621
    should be output.
622
*/
623
 
7 7u83 624
static BITSTREAM *
625
enc_dg_decl(BITSTREAM *bs, IDENTIFIER id, ulong n, unsigned use)
2 7u83 626
{
7 7u83 627
	int spec = 0;
628
	int force = 1;
629
	int tagged = 0;
630
	unsigned tag = TAG_id(id);
631
	DECL_SPEC ds = DEREF_dspec(id_storage(id));
632
	switch (tag) {
633
	case id_class_name_tag:
634
	case id_enum_name_tag: {
635
		/* Class and enumeration names */
636
		tagged = LANGUAGE_C;
637
		force = 2;
638
		break;
2 7u83 639
	}
640
	}
7 7u83 641
	if (n != LINK_NONE && !tagged) {
642
		/* Declare diagnostic tag if required */
643
		if (use & USAGE_DECL) {
644
			ulong m = link_no(bs, n, VAR_dgtag);
645
			if (use & USAGE_USE) {
646
				ENC_dg_spec_ref_name(bs);
647
			} else {
648
				ENC_dg_tag_name(bs);
649
			}
650
			ENC_make_dg_tag(bs, m);
651
			if (use & USAGE_DEFN) {
652
				record_usage(n, VAR_dgtag, USAGE_DEFN);
653
			} else {
654
				record_usage(n, VAR_dgtag, USAGE_DECL);
655
				ENC_dg_is_spec_name(bs);
656
				spec = 1;
657
			}
658
		}
659
	}
660
	switch (tag) {
2 7u83 661
 
7 7u83 662
	case id_variable_tag:
663
	case id_parameter_tag:
664
	case id_stat_member_tag: {
665
		/* Object names */
666
		TYPE t = DEREF_type(id_variable_etc_type(id));
667
		ENC_dg_object_name(bs);
668
		bs = enc_dg_name(bs, id, NULL_type);
669
		bs = enc_dg_loc(bs, id_loc(id), id_loc(id));
670
		bs = enc_dg_type(bs, t, 0);
671
		if (use & USAGE_DEFN) {
672
			EXP e;
673
			int et = error_threshold;
674
			error_threshold = ERROR_SERIOUS;
675
			ENC_ON(bs);
676
			MAKE_exp_identifier(t, id, qual_none, e);
677
			e = convert_reference(e, REF_NORMAL);
678
			e = convert_lvalue(e);
679
			if (IS_type_array(t)) {
680
				/* Do really want contents of array */
681
				MAKE_exp_contents(t, e, e);
682
			}
683
			bs = enc_exp(bs, e);
684
			free_exp(e, 1);
685
			error_threshold = et;
686
		} else {
687
			ENC_OFF(bs);
2 7u83 688
		}
7 7u83 689
		bs = enc_dg_access(bs, ds);
690
		break;
2 7u83 691
	}
692
 
7 7u83 693
	case id_function_tag:
694
	case id_mem_func_tag:
695
	case id_stat_mem_func_tag: {
696
		/* Function names */
697
		PTR(LOCATION)end_loc = id_loc(id);
698
		TYPE t = DEREF_type(id_function_etc_type(id));
699
		TYPE form = DEREF_type(id_function_etc_form(id));
700
		if (use & USAGE_DEFN) {
701
			EXP e = DEREF_exp(id_function_etc_defn(id));
702
			end_loc = block_loc(e, 1);
703
			if (IS_NULL_ptr(end_loc)) {
704
				end_loc = id_loc(id);
705
			}
706
		}
707
		ENC_dg_proc_name(bs);
708
		bs = enc_dg_name(bs, id, form);
709
		if (ds & dspec_implicit) {
710
			/* Implicitly declared function */
711
			ENC_dg_null_sourcepos(bs);
2 7u83 712
		} else {
7 7u83 713
			bs = enc_dg_loc(bs, id_loc(id), end_loc);
2 7u83 714
		}
7 7u83 715
		bs = enc_dg_type(bs, t, 0);
716
		if (use & USAGE_DEFN) {
717
			ulong m = DEREF_ulong(id_no(id));
718
			ENC_ON(bs);
719
			if (m == LINK_NONE) {
720
				ENC_make_top(bs);
721
			} else {
722
				ENC_obtain_tag(bs);
723
				m = unit_no(bs, id, VAR_tag, 0);
724
				ENC_make_tag(bs, m);
725
			}
2 7u83 726
		} else {
7 7u83 727
			ENC_OFF(bs);
2 7u83 728
		}
7 7u83 729
		bs = enc_dg_access(bs, ds);
730
		bs = enc_dg_virtual(bs, ds);
731
		bs = enc_bool(bs,((ds & dspec_inline)? 1 : 0));
732
		if (IS_type_func(t)) {
733
			LIST(TYPE)ex = DEREF_list(type_func_except(t));
734
			if (EQ_list(ex, univ_type_set)) {
735
				ENC_OFF(bs);
736
			} else {
737
				ENC_ON(bs);
738
				ENC_LIST(bs, LENGTH_list(ex));
739
				while (!IS_NULL_list(ex)) {
740
					TYPE s = DEREF_type(HEAD_list(ex));
741
					bs = enc_dg_type(bs, s, 0);
742
					ex = TAIL_list(ex);
743
				}
744
			}
745
		} else {
746
			ENC_OFF(bs);
747
		}
748
		ENC_OFF(bs);
749
		break;
2 7u83 750
	}
751
 
7 7u83 752
	case id_class_name_tag:
753
	case id_enum_name_tag:
754
	case id_class_alias_tag:
755
	case id_enum_alias_tag:
756
	case id_type_alias_tag: {
757
		/* Type names */
758
		TYPE t = DEREF_type(id_class_name_etc_defn(id));
759
		ENC_dg_type_name(bs);
760
		if (tagged) {
761
			ENC_OFF(bs);
2 7u83 762
		} else {
7 7u83 763
			TYPE form = NULL_type;
764
			if (IS_type_compound(t)) {
765
				CLASS_TYPE ct;
766
				ct = DEREF_ctype(type_compound_defn(t));
767
				form = DEREF_type(ctype_form(ct));
768
			}
769
			ENC_ON(bs);
770
			bs = enc_dg_name(bs, id, form);
2 7u83 771
		}
7 7u83 772
		bs = enc_dg_loc(bs, id_loc(id), id_loc(id));
773
		bs = enc_dg_access(bs, ds);
774
		ENC_ON(bs);
775
		if (tagged) {
776
			ulong m = link_no(bs, n, VAR_dgtag);
777
			if (use & USAGE_USE) {
778
				ENC_dg_spec_ref_type(bs);
779
			} else {
780
				ENC_dg_tag_type(bs);
781
			}
782
			ENC_make_dg_tag(bs, m);
783
			if (use & USAGE_DEFN) {
784
				record_usage(n, VAR_dgtag, USAGE_DEFN);
785
			} else {
786
				record_usage(n, VAR_dgtag, USAGE_DECL);
787
			}
2 7u83 788
		}
7 7u83 789
		bs = enc_dg_type(bs, t, force);
790
		ENC_false(bs);
791
		ENC_OFFS(bs, 2);
792
		break;
2 7u83 793
	}
794
 
7 7u83 795
	case id_nspace_name_tag: {
796
		/* Namespace names */
797
		IDENTIFIER nid = id;
798
		NAMESPACE ns = DEREF_nspace(id_nspace_name_defn(id));
799
		if (IS_nspace_unnamed(ns)) {
800
			nid = NULL_id;
801
		}
802
		ENC_dg_namespace_name(bs);
803
		bs = enc_dg_name(bs, nid, NULL_type);
804
		bs = enc_dg_loc(bs, id_loc(id), id_loc(id));
805
		bs = enc_dg_namelist(bs, ns);
806
		break;
2 7u83 807
	}
7 7u83 808
	}
809
	if (spec) {
810
		ENC_OFF(bs);
811
	}
812
	return (bs);
2 7u83 813
}
814
 
815
 
816
/*
817
    ENCODE DIAGNOSTICS FOR A TOKEN DEFINITION
818
 
819
    This routine outputs any diagnostic information for the token id
820
    to the appropriate diagnostic units.  It is only called if id is
821
    defined.  The type t may be used to override the type of id.
822
*/
823
 
7 7u83 824
void
825
enc_dg_token(IDENTIFIER id, TYPE t)
2 7u83 826
{
7 7u83 827
	UNUSED(id);
828
	UNUSED(t);
829
	return;
2 7u83 830
}
831
 
832
 
833
/*
834
    LIST OF INCOMPLETE CLASSES
835
 
836
    This list is used to hold all the classes which are used while they
837
    are incomplete.  A diagnostic tag is introduced for each such class
838
    which may be defined later if the class is completed.
839
*/
840
 
7 7u83 841
static LIST(IDENTIFIER)dg_classes = NULL_list(IDENTIFIER);
2 7u83 842
 
843
 
844
/*
845
    DEFINE INCOMPLETE CLASSES
846
 
847
    This routine defines the diagnostic tags for the incomplete classes
848
    in the list above.
849
*/
850
 
7 7u83 851
int
852
enc_dg_pending(void)
2 7u83 853
{
7 7u83 854
	int changed = 0;
855
	LIST(IDENTIFIER)p;
856
	while (p = dg_classes, !IS_NULL_list(p)) {
857
		dg_classes = NULL_list(IDENTIFIER);
858
		while (!IS_NULL_list(p)) {
859
			IDENTIFIER id;
860
			DESTROY_CONS_id(destroy, id, p, p);
861
			enc_dg_id(id, 2);
862
			changed = 1;
863
		}
2 7u83 864
	}
7 7u83 865
	return (changed);
2 7u83 866
}
867
 
868
 
869
/*
870
    FIND A DIAGNOSTIC TAG USAGE
871
 
872
    This routine determines the usage for the diagnostic tag associated
873
    with the identifier id in the context given by def (true for
874
    definitions, false for declarations).  The value returned indicates
875
    whether the diagnostic tag should be declared or defined.
876
*/
877
 
7 7u83 878
static unsigned
879
find_dg_usage(IDENTIFIER id, ulong *pn, int def)
2 7u83 880
{
7 7u83 881
	unsigned use = USAGE_NONE;
882
	switch (TAG_id(id)) {
883
	case id_class_name_tag:
884
	case id_enum_name_tag:
885
	case id_class_alias_tag:
886
	case id_enum_alias_tag:
887
	case id_type_alias_tag: {
888
		/* Type names */
889
		TYPE t;
890
		ulong n = DEREF_ulong(id_no(id));
891
		if (n == LINK_NONE) {
892
			/* Introduce diagnostic tag for type */
893
			NAMESPACE ns;
894
			string s = NULL;
895
			if (def == 0) {
896
				return (USAGE_NONE);
897
			}
898
			t = DEREF_type(id_class_name_etc_defn(id));
899
			if (IS_type_top_etc(t)) {
900
				/* Ignore 'void' types */
901
				return (USAGE_NONE);
902
			}
903
			ns = DEREF_nspace(id_parent(id));
904
			if (is_local_nspace(ns) == 2) {
905
				return (USAGE_NONE);
906
			}
907
			if (output_all) {
908
				s = mangle_tname("__dg__", t);
909
			}
910
			n = capsule_no(s, VAR_dgtag);
911
			COPY_ulong(id_no(id), n);
2 7u83 912
		} else {
7 7u83 913
			use = USAGE_USE;
2 7u83 914
		}
7 7u83 915
		if (is_defined(id, &t, 0)) {
916
			/* Only define each type once */
917
			unsigned prev = find_usage(n, VAR_dgtag);
918
			if (prev & USAGE_DEFN) {
919
				use = USAGE_NONE;
920
			} else {
921
				use |= (USAGE_DECL | USAGE_DEFN);
922
			}
2 7u83 923
		} else {
7 7u83 924
			/* Only declare each type once */
925
			if (use == USAGE_USE) {
926
				use = USAGE_NONE;
927
			} else {
928
				CONS_id(id, dg_classes, dg_classes);
929
				use = USAGE_DECL;
930
			}
2 7u83 931
		}
7 7u83 932
		*pn = n;
933
		break;
2 7u83 934
	}
7 7u83 935
	case id_variable_tag:
936
	case id_stat_member_tag:
937
	case id_function_tag:
938
	case id_mem_func_tag:
939
	case id_stat_mem_func_tag: {
940
		/* Tag names */
941
		ulong n = get_diag_tag(id, VAR_tag);
942
		if (n == LINK_NONE) {
943
			/* Introduce diagnostic tag for tag */
944
			string s = NULL;
945
			if (output_all) {
946
				s = mangle_name(id, VAR_dgtag, 0);
947
			}
948
			n = capsule_no(s, VAR_dgtag);
949
			set_diag_tag(id, VAR_tag, n);
950
			use = USAGE_DECL;
951
			if (def) {
952
				use |= USAGE_DEFN;
953
			}
954
		} else {
955
			/* Only declare each tag once */
956
			if (def) {
957
				unsigned prev = find_usage(n, VAR_dgtag);
958
				if (prev & USAGE_DEFN) {
959
					use = USAGE_NONE;
960
				} else {
961
					use = (USAGE_USE | USAGE_DECL | USAGE_DEFN);
962
				}
963
			}
2 7u83 964
		}
7 7u83 965
		*pn = n;
966
		break;
2 7u83 967
	}
7 7u83 968
	case id_nspace_name_tag: {
969
		/* Namespace names */
970
		use = USAGE_DEFN;
971
		break;
2 7u83 972
	}
7 7u83 973
	}
974
	return (use);
2 7u83 975
}
976
 
977
 
978
/*
979
    ENCODE A GLOBAL DIAGNOSTIC IDENTIFIER
980
 
981
    This routine adds the diagnostic information for the global identifier
982
    id to the diagnostic definition unit.  def is true for a definition.
983
*/
984
 
7 7u83 985
void
986
enc_dg_id(IDENTIFIER id, int def)
2 7u83 987
{
7 7u83 988
	ulong n;
989
	NAMESPACE ns;
990
	BITSTREAM *bs;
991
	IDENTIFIER pid;
2 7u83 992
 
7 7u83 993
	/* Check the identifier */
994
	ulong m = LINK_NONE;
995
	unsigned use = find_dg_usage(id, &m, def);
996
	if (use == USAGE_NONE) {
997
		return;
998
	}
2 7u83 999
 
7 7u83 1000
	/* Find the parent namespace */
1001
	ns = DEREF_nspace(id_parent(id));
1002
	while (!IS_NULL_nspace(ns) && IS_nspace_ctype(ns)) {
1003
		pid = DEREF_id(nspace_name(ns));
1004
		enc_dg_id(pid, 1);
1005
		if (m != LINK_NONE) {
1006
			/* Check for inline definitions */
1007
			unsigned prev = find_usage(m, VAR_dgtag);
1008
			if (prev & USAGE_DEFN) {
1009
				return;
1010
			}
1011
		}
1012
		ns = DEREF_nspace(id_parent(pid));
2 7u83 1013
	}
7 7u83 1014
	if (IS_NULL_nspace(ns) || !IS_nspace_named_etc(ns)) {
1015
		/* Ignore local identifiers */
1016
		return;
1017
	}
1018
	pid = DEREF_id(nspace_name(ns));
1019
	if (IS_NULL_id(pid)) {
1020
		return;
1021
	}
1022
	n = DEREF_ulong(id_no(pid));
1023
	if (n == LINK_NONE) {
1024
		/* Allocate namespace list number */
1025
		enc_dg_id(pid, 1);
1026
		n = DEREF_ulong(id_no(pid));
1027
		if (n == LINK_NONE) {
1028
			return;
1029
		}
1030
	}
2 7u83 1031
 
7 7u83 1032
	/* Add the identifier to the namespace */
1033
	bs = start_bitstream(NIL(FILE), diagcomp_unit->link);
1034
	ENC_dg_name_append(bs);
1035
	n = link_no(bs, n, VAR_dgtag);
1036
	ENC_make_dg_tag(bs, n);
1037
	bs = enc_dg_decl(bs, id, m, use);
1038
	count_item(bs);
1039
	diagcomp_unit = join_bitstreams(diagcomp_unit, bs);
1040
	return;
2 7u83 1041
}
1042
 
1043
 
1044
/*
1045
    DIAGNOSTICS FOR POINTER TO VOID
1046
 
1047
    This table gives the diagnostic tag numbers used to represent the types
1048
    'cv void *'  for the various cv-qualifiers, cv.
1049
*/
1050
 
7 7u83 1051
static ulong diag_ptr_void[4] = {
1052
	LINK_NONE, LINK_NONE, LINK_NONE, LINK_NONE
1053
};
2 7u83 1054
 
1055
 
1056
/*
1057
    ENCODE A BUILT-IN DIAGNOSTIC TYPE
1058
 
1059
    This routine adds the diagnostics for the definition of the built-in
1060
    type t to the diagnostic  bs.  This includes 'void *' as well as the
1061
    more obvious integer and floating point types.
1062
*/
1063
 
7 7u83 1064
ulong
1065
enc_dg_basetype(TYPE t, int def)
2 7u83 1066
{
7 7u83 1067
	BITSTREAM *bs = NULL;
1068
	CV_SPEC cv = cv_none;
1069
	BASE_TYPE bt = btype_none;
1070
	unsigned tag = TAG_type(t);
1071
	string nm = mangle_tname("__dg__", t);
1072
	ulong n = capsule_no(nm, VAR_dgtag);
2 7u83 1073
 
7 7u83 1074
	/* Introduce dummy type name */
1075
	if (def) {
1076
		IDENTIFIER gid = DEREF_id(nspace_name(global_namespace));
1077
		ulong m = DEREF_ulong(id_no(gid));
1078
		record_usage(n, VAR_dgtag, USAGE_DEFN);
1079
		bs = start_bitstream(NIL(FILE), diagcomp_unit->link);
1080
		ENC_dg_name_append(bs);
1081
		m = link_no(bs, m, VAR_dgtag);
1082
		ENC_make_dg_tag(bs, m);
1083
		ENC_dg_type_name(bs);
1084
		ENC_OFF(bs);
1085
		ENC_dg_global_sourcepos(bs);
1086
		ENC_OFF(bs);
1087
		ENC_ON(bs);
1088
		ENC_dg_tag_type(bs);
1089
		m = link_no(bs, n, VAR_dgtag);
1090
		ENC_make_dg_tag(bs, m);
1091
	}
2 7u83 1092
 
7 7u83 1093
	/* Encode type definition */
1094
	switch (tag) {
1095
	case type_integer_tag: {
1096
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1097
		COPY_ulong(itype_diag(it), n);
1098
		if (IS_itype_basic(it)) {
1099
			bt = DEREF_btype(itype_basic_rep(it));
2 7u83 1100
		}
7 7u83 1101
		if (def) {
1102
			if (bt & (btype_char | btype_wchar_t)) {
1103
				ENC_dg_char_type(bs);
1104
			} else if (bt & btype_bool) {
1105
				ENC_dg_boolean_type(bs);
1106
			} else {
1107
				ENC_dg_integer_type(bs);
1108
			}
1109
		}
1110
		break;
2 7u83 1111
	}
7 7u83 1112
	case type_floating_tag: {
1113
		FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
1114
		COPY_ulong(ftype_diag(ft), n);
1115
		if (IS_ftype_basic(ft)) {
1116
			bt = DEREF_btype(ftype_basic_rep(ft));
1117
		}
1118
		if (def) {
1119
			ENC_dg_float_type(bs);
1120
		}
1121
		break;
2 7u83 1122
	}
7 7u83 1123
	case type_ptr_tag: {
1124
		TYPE s = DEREF_type(type_ptr_sub(t));
1125
		cv = DEREF_cv(type_qual(s));
1126
		cv &= cv_qual;
1127
		diag_ptr_void[cv] = n;
1128
		bt = (btype_void | btype_star);
1129
		if (def) {
1130
			ENC_dg_address_type(bs);
1131
		}
1132
		break;
2 7u83 1133
	}
1134
	}
7 7u83 1135
	if (def) {
1136
		if (bt == btype_none) {
1137
			bs = enc_dg_name(bs, NULL_id, NULL_type);
1138
		} else {
1139
			int sp = 0;
1140
			BUFFER *bf = clear_buffer(&mangle_buff, NIL(FILE));
1141
			ENC_dg_external_idname(bs);
1142
			ENC_make_string(bs);
1143
			if (cv) {
1144
				sp = print_cv(cv, bf, sp);
1145
			}
1146
			IGNORE print_btype(bt, bf, sp);
1147
			bs = enc_ustring(bs, bf->start);
1148
		}
1149
		switch (tag) {
1150
		case type_integer_tag: {
1151
			bs = enc_variety(bs, t);
1152
			break;
1153
		}
1154
		case type_floating_tag: {
1155
			bs = enc_flvar(bs, t);
1156
			break;
1157
		}
1158
		case type_ptr_tag: {
1159
			bs = enc_shape(bs, t);
1160
			break;
1161
		}
1162
		}
1163
		ENC_true(bs);
1164
		ENC_OFFS(bs, 2);
1165
		count_item(bs);
1166
		diagcomp_unit = join_bitstreams(diagcomp_unit, bs);
2 7u83 1167
	}
7 7u83 1168
	return (n);
2 7u83 1169
}
1170
 
1171
 
1172
/*
1173
    ENCODE A DIAGNOSTIC CLASS TAG
1174
 
1175
    This routine adds the diagnostic tag corresponding to the class ct
1176
    to the bitstream bs.
1177
*/
1178
 
7 7u83 1179
static BITSTREAM *
1180
enc_dg_ctype(BITSTREAM *bs, CLASS_TYPE ct)
2 7u83 1181
{
7 7u83 1182
	ulong n;
1183
	IDENTIFIER cid = DEREF_id(ctype_name(ct));
1184
	cid = DEREF_id(id_alias(cid));
1185
	n = DEREF_ulong(id_no(cid));
1186
	if (n == LINK_NONE) {
1187
		enc_dg_id(cid, 1);
1188
		n = DEREF_ulong(id_no(cid));
1189
	}
1190
	n = link_no(bs, n, VAR_dgtag);
1191
	ENC_make_dg_tag(bs, n);
1192
	return (bs);
2 7u83 1193
}
1194
 
1195
 
1196
/*
1197
    ENCODE A DIAGNOSTIC OFFSET TOKEN
1198
 
1199
    This routine adds the offset off plus the offset token tok to the
1200
    bitstream bs in the form of a token with one expression parameter
1201
    which returns the parameter plus the offset (allowing for virtual
1202
    indirections etc.).
1203
*/
1204
 
7 7u83 1205
static BITSTREAM *
1206
enc_dg_offset(BITSTREAM *bs, OFFSET off, ulong tok, int spec)
2 7u83 1207
{
7 7u83 1208
	BITSTREAM *ts, *us;
1209
	ulong m = LINK_NONE;
1210
	ulong n = capsule_no(NULL_string, VAR_token);
1211
	ts = enc_tokdef_start(n, "EE", &m, 1);
1212
	if (spec == -1) {
1213
		us = ts;
1214
	} else {
1215
		ts = enc_special(ts, spec);
1216
		us = start_bitstream(NIL(FILE), ts->link);
1217
	}
1218
	if (tok == LINK_NONE) {
1219
		us = enc_add_ptr(us, NULL_exp, m, off, 1);
1220
	} else {
1221
		ENC_add_to_ptr(us);
1222
		us = enc_add_ptr(us, NULL_exp, m, off, 1);
1223
		tok = link_no(us, tok, VAR_token);
1224
		ENC_exp_apply_token(us);
1225
		ENC_make_tok(us, tok);
1226
		ENC_LEN_SMALL(us, 0);
1227
	}
1228
	if (spec == -1) {
1229
		ts = us;
1230
	} else {
1231
		ts = enc_bitstream(ts, us);
1232
	}
1233
	enc_tokdef_end(n, ts);
1234
	n = link_no(bs, n, VAR_token);
1235
	ENC_make_tok(bs, n);
1236
	return (bs);
2 7u83 1237
}
1238
 
1239
 
1240
/*
1241
    ENCODE A LIST OF DIAGNOSTIC BASE CLASSES
1242
 
1243
    This routine adds the list of diagnostic base classes given by br
1244
    to the bitstream bs.
1245
*/
1246
 
7 7u83 1247
static BITSTREAM *
1248
enc_dg_bases(BITSTREAM *bs, LIST(GRAPH)br)
2 7u83 1249
{
7 7u83 1250
	ENC_LIST(bs, LENGTH_list(br));
1251
	while (!IS_NULL_list(br)) {
1252
		GRAPH gs = DEREF_graph(HEAD_list(br));
1253
		OFFSET off = DEREF_off(graph_off(gs));
1254
		CLASS_TYPE cs = DEREF_ctype(graph_head(gs));
1255
		DECL_SPEC acc = DEREF_dspec(graph_access(gs));
1256
		ENC_make_dg_class_base(bs);
1257
		bs = enc_dg_ctype(bs, cs);
1258
		ENC_OFF(bs);
1259
		if (is_zero_offset(off)) {
1260
			ENC_OFF(bs);
1261
		} else {
1262
			/* Base class offset */
1263
			ENC_ON(bs);
1264
			bs = enc_dg_offset(bs, off, LINK_NONE, -1);
1265
		}
1266
		bs = enc_dg_access(bs, acc);
1267
		bs = enc_dg_virtual(bs, acc);
1268
		br = TAIL_list(br);
2 7u83 1269
	}
7 7u83 1270
	return (bs);
2 7u83 1271
}
1272
 
1273
 
1274
/*
1275
    ENCODE A DIAGNOSTIC CLASS MEMBER
1276
 
1277
    This routine adds the class member id to the bitstream bs as a
1278
    DG_CLASSMEM if ct is not null, or as a DG_FIELD otherwise.  The
1279
    number of members is recorded in pm.  The routine is also used to
1280
    handle type members of blocks when blk is true.
1281
*/
1282
 
7 7u83 1283
static BITSTREAM *
1284
enc_dg_member(BITSTREAM *bs, IDENTIFIER id, unsigned *pm, CLASS_TYPE ct,
1285
	      int blk)
2 7u83 1286
{
7 7u83 1287
	int def = 0;
1288
	unsigned done = 0;
1289
	DECL_SPEC ds = DEREF_dspec(id_storage(id));
1290
	if (IS_id_function_etc(id)) {
1291
		/* Allow for overloaded functions */
1292
		IDENTIFIER fid = DEREF_id(id_function_etc_over(id));
1293
		if (!IS_NULL_id(fid)) {
1294
			bs = enc_dg_member(bs, fid, pm, ct, blk);
1295
		}
2 7u83 1296
	}
7 7u83 1297
	if (ds & (dspec_alias | dspec_inherit | dspec_token)) {
1298
		/* Ignore inherited members */
1299
		return (bs);
2 7u83 1300
	}
7 7u83 1301
	if (ds & dspec_defn) {
1302
		def = 1;
2 7u83 1303
	}
7 7u83 1304
	switch (TAG_id(id)) {
1305
	case id_member_tag: {
1306
		/* Data members */
1307
		HASHID nm = DEREF_hashid(id_name(id));
1308
		if (!IS_hashid_anon(nm)) {
1309
			TYPE t = DEREF_type(id_member_type(id));
1310
			OFFSET off = DEREF_off(id_member_off(id));
1311
			ENC_dg_field_classmem(bs);
1312
			bs = enc_dg_name(bs, id, NULL_type);
1313
			bs = enc_dg_loc(bs, id_loc(id), id_loc(id));
1314
			bs = enc_offset(bs, off);
1315
			bs = enc_dg_type(bs, t, 0);
1316
			bs = enc_dg_access(bs, ds);
1317
			ENC_OFFS(bs, 2);
1318
			done = 1;
2 7u83 1319
		}
7 7u83 1320
		break;
2 7u83 1321
	}
7 7u83 1322
	case id_stat_member_tag: {
1323
		/* Static data members */
1324
		if (!IS_NULL_ctype(ct)) {
1325
			ulong m = LINK_NONE;
1326
			unsigned use = find_dg_usage(id, &m, def);
1327
			ENC_dg_name_classmem(bs);
1328
			bs = enc_dg_decl(bs, id, m,(use | USAGE_DECL));
1329
			done = 1;
1330
		}
1331
		break;
2 7u83 1332
	}
7 7u83 1333
	case id_mem_func_tag: {
1334
		/* Member functions */
1335
		if (!IS_NULL_ctype(ct) && !(ds & dspec_trivial)) {
1336
			ulong m = LINK_NONE;
1337
			unsigned use = find_dg_usage(id, &m, def);
1338
			ENC_dg_function_classmem(bs);
1339
			bs = enc_dg_decl(bs, id, m,(use | USAGE_DECL));
1340
			if (ds & dspec_virtual) {
1341
				/* Offset from start of virtual function table */
1342
				BITSTREAM *ts;
1343
				VIRTUAL vt = DEREF_virt(ctype_virt(ct));
1344
				ulong n = virtual_no(id, vt);
1345
				ENC_ON(bs);
1346
				ts = start_bitstream(NIL(FILE), bs->link);
1347
				bs = enc_special(bs, TOK_vtab_off);
1348
				ts = enc_make_snat(ts,(int)n);
1349
				bs = enc_bitstream(bs, ts);
1350
			} else {
1351
				ENC_OFF(bs);
1352
			}
1353
			done = 1;
2 7u83 1354
		}
7 7u83 1355
		break;
2 7u83 1356
	}
7 7u83 1357
	case id_stat_mem_func_tag: {
1358
		/* Static member functions */
1359
		if (!IS_NULL_ctype(ct)) {
1360
			ulong m = LINK_NONE;
1361
			unsigned use = find_dg_usage(id, &m, def);
1362
			ENC_dg_name_classmem(bs);
1363
			bs = enc_dg_decl(bs, id, m,(use | USAGE_DECL));
1364
			done = 1;
1365
		}
1366
		break;
1367
	}
1368
	case id_class_name_tag:
1369
	case id_enum_name_tag:
1370
	case id_class_alias_tag:
1371
	case id_enum_alias_tag:
1372
	case id_type_alias_tag: {
1373
		/* Nested types */
1374
		if (!IS_NULL_ctype(ct) || blk) {
1375
			ulong m = LINK_NONE;
1376
			unsigned use = find_dg_usage(id, &m, 1);
1377
			if (blk) {
1378
				use = ((use & USAGE_DEFN) | USAGE_DECL);
1379
				ENC_name_decl_dg(bs);
1380
			} else {
1381
				ENC_dg_name_classmem(bs);
1382
			}
1383
			bs = enc_dg_decl(bs, id, m, use);
1384
			done = 1;
1385
		}
1386
		break;
1387
	}
1388
	}
1389
	*pm += done;
1390
	return (bs);
2 7u83 1391
}
1392
 
1393
 
1394
/*
1395
    ENCODE A LIST OF DIAGNOSTIC MEMBERS
1396
 
1397
    This routine calls enc_dg_member for all the members of the
1398
    namespace ns.
1399
*/
1400
 
7 7u83 1401
static BITSTREAM *
1402
enc_dg_namespace(BITSTREAM *bs, NAMESPACE ns, unsigned *pm, CLASS_TYPE ct)
2 7u83 1403
{
7 7u83 1404
	MEMBER mem;
1405
	int blk = 0;
1406
	if (IS_nspace_ctype(ns)) {
1407
		mem = DEREF_member(nspace_ctype_first(ns));
1408
	} else {
1409
		mem = DEREF_member(nspace_last(ns));
1410
		blk = 1;
2 7u83 1411
	}
7 7u83 1412
	while (!IS_NULL_member(mem)) {
1413
		/* Class members */
1414
		IDENTIFIER mid = DEREF_id(member_id(mem));
1415
		IDENTIFIER nid = DEREF_id(member_alt(mem));
1416
		if (!IS_NULL_id(mid)) {
1417
			bs = enc_dg_member(bs, mid, pm, ct, blk);
1418
		}
1419
		if (!IS_NULL_id(nid) && !EQ_id(nid, mid)) {
1420
			bs = enc_dg_member(bs, nid, pm, ct, blk);
1421
		}
1422
		mem = DEREF_member(member_next(mem));
2 7u83 1423
	}
7 7u83 1424
	return (bs);
2 7u83 1425
}
1426
 
1427
 
1428
/*
1429
    ENCODE VIRTUAL FUNCTION TABLE INFORMATION
1430
 
1431
    This routine adds the information for the virtual function table
1432
    and run-time type information associated with the polymorphic class
1433
    t to the bitstream bs in the form of a number of dummy class members.
1434
    Information on these members is returned via vtags.
1435
*/
1436
 
1437
#if LANGUAGE_CPP
1438
 
7 7u83 1439
static BITSTREAM *
1440
enc_dg_vtable(BITSTREAM *bs, TYPE t, ulong *vtags)
2 7u83 1441
{
7 7u83 1442
	ulong n, m;
1443
	IDENTIFIER tid = dummy_type_name;
1444
	TYPE ti = get_type_info(lex_typeid, t, 0);
1445
	TYPE tv  = get_type_info(lex_vtable, t, 0);
1446
	TYPE pv = DEREF_type(type_array_sub(tv));
1447
	CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1448
	VIRTUAL vt = DEREF_virt(ctype_virt(ct));
1449
	ulong tok = DEREF_ulong(virt_table_tok(vt));
1450
	OFFSET off = DEREF_off(virt_table_off(vt));
1451
	ulong tbl = DEREF_ulong(virt_table_tbl(vt));
2 7u83 1452
 
7 7u83 1453
	/* Make tag for slot type */
1454
	m = capsule_no(NULL_string, VAR_dgtag);
1455
	COPY_ulong(id_no(tid), m);
1456
	COPY_id(type_name(pv), tid);
1457
	record_usage(m, VAR_dgtag, USAGE_DEFN);
2 7u83 1458
 
7 7u83 1459
	/* Virtual function table pointer */
1460
	n = capsule_no(NULL_string, VAR_dgtag);
1461
	record_usage(n, VAR_dgtag, USAGE_DEFN);
1462
	vtags[0] = n;
1463
	ENC_dg_tag_classmem(bs);
1464
	n = link_no(bs, n, VAR_dgtag);
1465
	ENC_make_dg_tag(bs, n);
1466
	ENC_dg_indirect_classmem(bs);
1467
	bs = enc_dg_artificial(bs, "__vptr");
1468
	ENC_dg_null_sourcepos(bs);
1469
	bs = enc_dg_offset(bs, off, tok, -1);
1470
	ENC_dg_pointer_type(bs);
1471
	ENC_dg_tag_type(bs);
1472
	m = link_no(bs, m, VAR_dgtag);
1473
	ENC_make_dg_tag(bs, m);
1474
	bs = enc_dg_type(bs, pv, 1);
1475
	ENC_OFF(bs);
2 7u83 1476
 
7 7u83 1477
	/* Run-time type information pointer */
1478
	if (output_rtti) {
1479
		n = capsule_no(NULL_string, VAR_dgtag);
1480
		record_usage(n, VAR_dgtag, USAGE_DEFN);
1481
		vtags[1] = n;
1482
		ENC_dg_tag_classmem(bs);
1483
		n = link_no(bs, n, VAR_dgtag);
1484
		ENC_make_dg_tag(bs, n);
1485
		ENC_dg_indirect_classmem(bs);
1486
		bs = enc_dg_artificial(bs, "__tptr");
1487
		ENC_dg_null_sourcepos(bs);
1488
		bs = enc_dg_offset(bs, off, tok, TOK_typeid_ref);
1489
		ENC_dg_pointer_type(bs);
1490
		bs = enc_dg_type(bs, ti, 0);
1491
		ENC_OFF(bs);
1492
	}
2 7u83 1493
 
7 7u83 1494
	/* Virtual function table */
1495
	n = capsule_no(NULL_string, VAR_dgtag);
1496
	record_usage(n, VAR_dgtag, USAGE_DEFN);
1497
	vtags[2] = n;
1498
	record_usage(tbl, VAR_tag, USAGE_USE);
1499
	ENC_dg_name_classmem(bs);
1500
	ENC_dg_tag_name(bs);
1501
	n = link_no(bs, n, VAR_dgtag);
1502
	ENC_make_dg_tag(bs, n);
1503
	ENC_dg_object_name(bs);
1504
	bs = enc_dg_artificial(bs, "__vtbl");
1505
	ENC_dg_null_sourcepos(bs);
1506
	bs = enc_dg_type(bs, tv, 0);
1507
	ENC_ON(bs);
1508
	ENC_contents(bs);
1509
	bs = enc_shape(bs, tv);
1510
	ENC_obtain_tag(bs);
1511
	tbl = link_no(bs, tbl, VAR_tag);
1512
	ENC_make_tag(bs, tbl);
1513
	ENC_OFF(bs);
2 7u83 1514
 
7 7u83 1515
	/* Run-time type information structure */
1516
	if (output_rtti) {
1517
		n = capsule_no(NULL_string, VAR_dgtag);
1518
		record_usage(n, VAR_dgtag, USAGE_DEFN);
1519
		vtags[3] = n;
1520
		ENC_dg_name_classmem(bs);
1521
		ENC_dg_tag_name(bs);
1522
		n = link_no(bs, n, VAR_dgtag);
1523
		ENC_make_dg_tag(bs, n);
1524
		ENC_dg_object_name(bs);
1525
		bs = enc_dg_artificial(bs, "__typeid");
1526
		ENC_dg_null_sourcepos(bs);
1527
		bs = enc_dg_type(bs, ti, 0);
1528
		ENC_ON(bs);
1529
		ENC_contents(bs);
1530
		bs = enc_shape(bs, ti);
1531
		bs = enc_rtti_type(bs, t, lex_typeid);
1532
		ENC_OFF(bs);
1533
	}
2 7u83 1534
 
7 7u83 1535
	/* Clear slot type */
1536
	COPY_ulong(id_no(tid), LINK_NONE);
1537
	COPY_id(type_name(pv), NULL_id);
1538
	return (bs);
2 7u83 1539
}
1540
 
1541
#endif
1542
 
1543
 
1544
/*
1545
    ENCODE A DIAGNOSTIC CLASS
1546
 
1547
    This routine adds the definition of the class type t to the bitstream
1548
    bs.  force is 2 to indicate that a tag name should be output for this
1549
    class.
1550
*/
1551
 
7 7u83 1552
static BITSTREAM *
1553
enc_dg_class(BITSTREAM *bs, TYPE t, int force)
2 7u83 1554
{
7 7u83 1555
	BITSTREAM *ts;
1556
	unsigned m = 0;
1557
	ulong vtags[4];
1558
	CLASS_TYPE cs = NULL_ctype;
1559
	CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1560
	CLASS_INFO ci = DEREF_cinfo(ctype_info(ct));
1561
	NAMESPACE ns = DEREF_nspace(ctype_member(ct));
1562
	if (force == 0) {
1563
		ENC_dg_unknown_type(bs);
1564
		bs = enc_shape(bs, t);
1565
		return (bs);
1566
	}
1567
	if ((ci & cinfo_complete) && (ci & cinfo_defined)) {
1568
		/* Complete class */
1569
		IGNORE compile_class(ct);
1570
		if (ci & (cinfo_static | cinfo_function | cinfo_base)) {
1571
			cs = ct;
1572
		} else if ((ci & cinfo_trivial)!= cinfo_trivial) {
1573
			cs = ct;
1574
		} else {
1575
			LIST(IDENTIFIER)ft;
1576
			ft = DEREF_list(ctype_nest(ct));
1577
			if (!IS_NULL_list(ft)) {
1578
				cs = ct;
1579
			}
1580
			ft = DEREF_list(ctype_pals(ct));
1581
			if (!IS_NULL_list(ft)) {
1582
				cs = ct;
1583
			}
1584
		}
2 7u83 1585
	} else {
7 7u83 1586
		/* Incomplete class */
1587
		ENC_dg_is_spec_type(bs);
1588
		t = NULL_type;
2 7u83 1589
	}
7 7u83 1590
	if (!IS_NULL_ctype(cs)) {
1591
		/* Class type */
1592
		GRAPH gr = DEREF_graph(ctype_base(ct));
1593
		LIST(GRAPH)br = DEREF_list(graph_tails(gr));
1594
		ENC_dg_class_type(bs);
1595
		bs = enc_dg_bases(bs, br);
1596
	} else {
1597
		/* Structure type */
1598
		ENC_dg_struct_type(bs);
1599
	}
1600
	vtags[0] = LINK_NONE;
1601
	vtags[1] = LINK_NONE;
1602
	vtags[2] = LINK_NONE;
1603
	vtags[3] = LINK_NONE;
1604
	ts = start_bitstream(NIL(FILE), bs->link);
1605
	ts = enc_dg_namespace(ts, ns, &m, cs);
2 7u83 1606
#if LANGUAGE_CPP
7 7u83 1607
	if (!IS_NULL_ctype(cs)) {
1608
		if (ci & cinfo_polymorphic) {
1609
			ts = enc_dg_vtable(ts, t, vtags);
1610
			if (output_rtti) {
1611
				m += 2;
1612
			}
1613
			m += 2;
1614
		}
2 7u83 1615
	}
1616
#endif
7 7u83 1617
	ENC_LIST(bs, m);
1618
	bs = join_bitstreams(bs, ts);
1619
	if (!IS_NULL_ctype(cs)) {
1620
		/* Friends */
1621
		ENC_OFF(bs);
1622
		ENC_LIST_SMALL(bs, 0);
1623
	}
1624
	if (!IS_NULL_type(t)) {
1625
		/* Class shape */
1626
		ENC_ON(bs);
1627
		bs = enc_shape(bs, t);
2 7u83 1628
	} else {
7 7u83 1629
		ENC_OFF(bs);
2 7u83 1630
	}
7 7u83 1631
	if (!IS_NULL_ctype(cs)) {
1632
		/* Virtual function table information */
1633
		if (vtags[0] == LINK_NONE) {
1634
			ENC_OFFS(bs, 2);
1635
		} else {
1636
			ENC_ON(bs);
1637
			vtags[2] = link_no(bs, vtags[2], VAR_dgtag);
1638
			ENC_make_dg_tag(bs, vtags[2]);
1639
			ENC_ON(bs);
1640
			vtags[0] = link_no(bs, vtags[0], VAR_dgtag);
1641
			ENC_make_dg_tag(bs, vtags[0]);
1642
		}
1643
	}
1644
	if (force == 2) {
1645
		/* Output tag name */
1646
		IDENTIFIER cid = DEREF_id(ctype_name(ct));
1647
		TYPE form = DEREF_type(ctype_form(ct));
1648
		ENC_ON(bs);
1649
		bs = enc_dg_name(bs, cid, form);
1650
		ENC_ON(bs);
1651
		bs = enc_dg_loc(bs, id_loc(cid), id_loc(cid));
2 7u83 1652
	} else {
7 7u83 1653
		ENC_OFFS(bs, 2);
2 7u83 1654
	}
7 7u83 1655
	if (IS_NULL_ctype(cs)) {
1656
		ENC_OFF(bs);
1657
	}
1658
	bs = enc_bool(bs,((ci & cinfo_union)? 1 : 0));
1659
	if (!IS_NULL_ctype(cs)) {
1660
		/* Run-time type information */
1661
		if (vtags[1] == LINK_NONE) {
1662
			ENC_OFFS(bs, 2);
1663
		} else {
1664
			ENC_ON(bs);
1665
			vtags[3] = link_no(bs, vtags[3], VAR_dgtag);
1666
			ENC_make_dg_tag(bs, vtags[3]);
1667
			ENC_ON(bs);
1668
			vtags[1] = link_no(bs, vtags[1], VAR_dgtag);
1669
			ENC_make_dg_tag(bs, vtags[1]);
1670
		}
1671
	}
1672
	bs = enc_bool(bs, LANGUAGE_CPP);
1673
	if (!IS_NULL_ctype(cs)) {
1674
		ENC_OFF(bs);
1675
	}
1676
	return (bs);
2 7u83 1677
}
1678
 
1679
 
1680
/*
1681
    ENCODE A DIAGNOSTIC ENUMERATION TYPE
1682
 
1683
    This routine adds the definition of the enumeration type t to the
1684
    bitstream bs.  force is 2 to indicate that a tag name should be
1685
    output for this enumeration type.
1686
*/
1687
 
7 7u83 1688
static BITSTREAM *
1689
enc_dg_etype(BITSTREAM *bs, TYPE t, int force)
2 7u83 1690
{
7 7u83 1691
	ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
1692
	LIST(IDENTIFIER)p = DEREF_list(etype_values(et));
1693
	CLASS_INFO ei = DEREF_cinfo(etype_info(et));
1694
	if (!(ei & cinfo_complete) || force == 0) {
1695
		t = DEREF_type(etype_rep(et));
1696
		bs = enc_dg_type(bs, t, 0);
1697
		return (bs);
1698
	}
1699
	ENC_dg_enum_type(bs);
1700
	ENC_LIST(bs, LENGTH_list(p));
1701
	while (!IS_NULL_list(p)) {
1702
		IDENTIFIER pid = DEREF_id(HEAD_list(p));
1703
		EXP e = DEREF_exp(id_enumerator_value(pid));
1704
		ENC_make_dg_enum(bs);
1705
		bs = enc_exp(bs, e);
1706
		bs = enc_dg_name(bs, pid, NULL_type);
1707
		bs = enc_dg_loc(bs, id_loc(pid), id_loc(pid));
1708
		p = TAIL_list(p);
1709
	}
1710
	if (force == 2) {
1711
		/* Output tag name */
1712
		IDENTIFIER eid = DEREF_id(etype_name(et));
1713
		ENC_ON(bs);
1714
		bs = enc_dg_name(bs, eid, NULL_type);
1715
		ENC_ON(bs);
1716
		bs = enc_dg_loc(bs, id_loc(eid), id_loc(eid));
1717
	} else {
1718
		ENC_OFFS(bs, 2);
1719
	}
1720
	bs = enc_shape(bs, t);
1721
	bs = enc_bool(bs, LANGUAGE_CPP);
1722
	return (bs);
2 7u83 1723
}
1724
 
1725
 
1726
/*
1727
    ENCODE A DIAGNOSTIC TYPE
1728
 
1729
    This routine adds the diagnostic information for the type t to the
1730
    bitstream bs.
1731
*/
1732
 
7 7u83 1733
BITSTREAM *
1734
enc_dg_type(BITSTREAM *bs, TYPE t, int force)
2 7u83 1735
{
7 7u83 1736
	ulong n;
1737
	CV_SPEC cv;
1738
	if (IS_NULL_type(t)) {
1739
		ENC_dg_void_type(bs);
1740
		return (bs);
1741
	}
1742
	cv = DEREF_cv(type_qual(t));
1743
	if (cv & cv_const) {
1744
		/* Allow for const qualifier */
1745
		ENC_dg_qualified_type(bs);
1746
		ENC_dg_const_qualifier(bs);
1747
	}
1748
	if (cv & cv_volatile) {
1749
		/* Allow for volatile qualifier */
1750
		ENC_dg_qualified_type(bs);
1751
		ENC_dg_volatile_qualifier(bs);
1752
	}
2 7u83 1753
 
7 7u83 1754
	/* Check type name */
1755
	if (!force) {
1756
		IDENTIFIER tid = DEREF_id(type_name(t));
1757
		if (!IS_NULL_id(tid)) {
1758
			tid = DEREF_id(id_alias(tid));
1759
			n = DEREF_ulong(id_no(tid));
1760
			if (n == LINK_NONE) {
1761
				enc_dg_id(tid, 1);
1762
				n = DEREF_ulong(id_no(tid));
1763
				if (n == LINK_NONE) {
1764
					goto type_label;
1765
				}
1766
			}
1767
			ENC_dg_named_type(bs);
1768
			n = link_no(bs, n, VAR_dgtag);
1769
			ENC_make_dg_tag(bs, n);
1770
			return (bs);
1771
		}
2 7u83 1772
	}
1773
 
7 7u83 1774
	/* Encode type definition */
1775
type_label: {
1776
		    ASSERT(ORDER_type == 18);
1777
		    switch (TAG_type(t)) {
1778
		    case type_integer_tag: {
1779
			    /* Integral types */
1780
			    INT_TYPE it = DEREF_itype(type_integer_rep(t));
1781
			    n = DEREF_ulong(itype_diag(it));
1782
			    goto basetype_lab;
2 7u83 1783
		    }
7 7u83 1784
basetype_lab: {
1785
		      if (n == LINK_NONE) {
1786
			      if (cv & cv_qual) {
1787
				      t = qualify_type(t, cv_none, 0);
1788
			      }
1789
			      n = enc_dg_basetype(t, 1);
1790
		      }
1791
		      ENC_dg_named_type(bs);
1792
		      n = link_no(bs, n, VAR_dgtag);
1793
		      ENC_make_dg_tag(bs, n);
1794
		      break;
1795
	      }
1796
		    case type_floating_tag: {
1797
			    /* Floating point types */
1798
			    FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
1799
			    n = DEREF_ulong(ftype_diag(ft));
1800
			    goto basetype_lab;
1801
		    }
1802
		    case type_top_tag:
1803
		    case type_bottom_tag: {
1804
			    /* Void types */
1805
			    ENC_dg_void_type(bs);
1806
			    break;
1807
		    }
1808
		    case type_ptr_tag: {
1809
			    /* Pointer types */
1810
			    TYPE s = DEREF_type(type_ptr_sub(t));
1811
			    if (IS_type_top_etc(s)) {
1812
				    /* Pointer void types */
1813
				    cv = DEREF_cv(type_qual(s));
1814
				    cv &= cv_qual;
1815
				    n = diag_ptr_void[cv];
1816
				    goto basetype_lab;
1817
			    }
1818
			    ENC_dg_pointer_type(bs);
1819
			    bs = enc_dg_type(bs, s, 0);
1820
			    ENC_OFF(bs);
1821
			    break;
1822
		    }
1823
		    case type_ref_tag: {
1824
			    /* Reference types */
1825
			    TYPE s = DEREF_type(type_ref_sub(t));
1826
			    ENC_dg_reference_type(bs);
1827
			    bs = enc_dg_type(bs, s, 0);
1828
			    break;
1829
		    }
1830
		    case type_ptr_mem_tag: {
1831
			    /* Pointer to member types */
1832
			    CLASS_TYPE cs = DEREF_ctype(type_ptr_mem_of(t));
1833
			    TYPE s = DEREF_type(type_ptr_mem_sub(t));
1834
			    if (IS_type_func(s)) {
1835
				    ENC_dg_ptr_memfn_type(bs);
1836
			    } else {
1837
				    ENC_dg_ptr_memdata_type(bs);
1838
			    }
1839
			    bs = enc_dg_ctype(bs, cs);
1840
			    bs = enc_dg_type(bs, s, 0);
1841
			    bs = enc_shape(bs, t);
1842
			    ENC_OFF(bs);
1843
			    break;
1844
		    }
1845
		    case type_func_tag: {
1846
			    /* Function types */
1847
			    TYPE r = DEREF_type(type_func_ret(t));
1848
			    int ell = DEREF_int(type_func_ellipsis(t));
1849
			    CV_SPEC mqual = DEREF_cv(type_func_mqual(t));
1850
			    LIST(TYPE)p = DEREF_list(type_func_ptypes(t));
1851
			    LIST(TYPE)q = DEREF_list(type_func_mtypes(t));
1852
			    unsigned nq = LENGTH_list(q);
1853
			    ENC_dg_proc_type(bs);
1854
			    ENC_LIST(bs, nq);
1855
			    if (!EQ_list(p, q) && !IS_NULL_list(q)) {
1856
				    /* Allow for 'this' parameter */
1857
				    TYPE s = DEREF_type(HEAD_list(q));
1858
				    ENC_dg_object_param(bs);
1859
				    ENC_ON(bs);
1860
				    bs = enc_dg_artificial(bs, "this");
1861
				    ENC_OFFS(bs, 2);
1862
				    bs = enc_dg_type(bs, s, 0);
1863
				    ENC_OFF(bs);
1864
				    q = TAIL_list(q);
1865
			    }
1866
			    while (!IS_NULL_list(q)) {
1867
				    /* Real function parameters */
1868
				    TYPE s = DEREF_type(HEAD_list(q));
1869
				    if (ell & FUNC_PARAMS) {
1870
					    s = unpromote_type(s);
1871
				    }
1872
				    ENC_dg_object_param(bs);
1873
				    ENC_OFFS(bs, 3);
1874
				    bs = enc_dg_type(bs, s, 0);
1875
				    ENC_OFF(bs);
1876
				    q = TAIL_list(q);
1877
			    }
1878
			    bs = enc_dg_type(bs, r, 0);
1879
			    if (ell & FUNC_NON_PROTO) {
1880
				    /* Non-prototype function */
1881
				    ENC_ON(bs);
1882
				    ENC_false(bs);
1883
			    } else {
1884
				    /* Prototype function */
1885
				    ENC_OFF(bs);
1886
			    }
1887
			    ENC_OFF(bs);
1888
			    if (mqual & cv_lang) {
1889
				    /* Default language linkage */
1890
				    ENC_OFF(bs);
1891
			    } else {
1892
				    /* Non-default language linkage */
1893
				    ENC_ON(bs);
1894
				    ENC_make_nat(bs);
1895
				    ENC_INT(bs, DWARF_LANG_NOT);
1896
			    }
1897
			    if (ell & FUNC_ELLIPSIS) {
1898
				    /* Ellipsis function */
1899
				    ENC_ON(bs);
1900
				    ENC_var_callers(bs);
1901
			    } else {
1902
				    /* Non-ellipsis function */
1903
				    ENC_OFF(bs);
1904
			    }
1905
			    break;
1906
		    }
1907
		    case type_array_tag: {
1908
			    /* Array types */
1909
			    TYPE s = DEREF_type(type_array_sub(t));
1910
			    NAT m = DEREF_nat(type_array_size(t));
1911
			    ENC_dg_array_type(bs);
1912
			    bs = enc_dg_type(bs, s, 0);
1913
			    bs = enc_shape_offset(bs, s);
1914
			    ENC_OFF(bs);
1915
			    ENC_LIST_SMALL(bs, 1);
1916
			    if (IS_NULL_nat(m)) {
1917
				    ENC_dg_unspecified_dim(bs);
1918
			    } else {
1919
				    TYPE i = type_sint;
1920
				    unsigned long v = get_nat_value(m);
1921
				    ENC_dg_bounds_dim(bs);
1922
				    ENC_dg_static_bound(bs);
1923
				    bs = enc_make_int(bs, i, 0);
1924
				    ENC_dg_static_bound(bs);
1925
				    if (v < SMALL_ARRAY_BOUND) {
1926
					    /* Small value */
1927
					    if (v) {
1928
						    v--;
1929
					    }
1930
					    bs = enc_make_int(bs, i, (int)v);
1931
				    } else {
1932
					    ENC_minus(bs);
1933
					    bs = enc_error_treatment(bs, i);
1934
					    ENC_make_int(bs);
1935
					    bs = enc_variety(bs, i);
1936
					    bs = enc_snat(bs, m, 0, 1);
1937
					    bs = enc_make_int(bs, i, 1);
1938
				    }
1939
				    bs = enc_dg_type(bs, i, 0);
1940
			    }
1941
			    break;
1942
		    }
1943
		    case type_bitfield_tag: {
1944
			    /* Bitfield types */
1945
			    INT_TYPE bf = DEREF_itype(type_bitfield_defn(t));
1946
			    TYPE s = DEREF_type(itype_bitfield_sub(bf));
1947
			    ENC_dg_bitfield_type(bs);
1948
			    bs = enc_dg_type(bs, s, 0);
1949
			    bs = enc_bfvar(bs, t);
1950
			    bs = enc_shape(bs, s);
1951
			    break;
1952
		    }
1953
		    case type_compound_tag: {
1954
			    /* Class types */
1955
			    bs = enc_dg_class(bs, t, force);
1956
			    break;
1957
		    }
1958
		    case type_enumerate_tag: {
1959
			    /* Enumeration types */
1960
			    bs = enc_dg_etype(bs, t, force);
1961
			    break;
1962
		    }
1963
		    default: {
1964
			    /* Other types */
1965
			    ENC_dg_unknown_type(bs);
1966
			    bs = enc_shape(bs, t);
1967
			    break;
1968
		    }
1969
		    }
2 7u83 1970
	    }
7 7u83 1971
	    return (bs);
2 7u83 1972
}
1973
 
1974
 
1975
/*
1976
    ENCODE A LOCAL DIAGNOSTIC IDENTIFIER
1977
 
1978
    This routine adds the diagnostic information for the local identifier
1979
    id to the bitstream bs.  ts gives the encoding of the scope of id.
1980
*/
1981
 
7 7u83 1982
BITSTREAM *
1983
enc_dg_local(BITSTREAM *bs, IDENTIFIER id, BITSTREAM *ts)
2 7u83 1984
{
7 7u83 1985
	bs = enc_diag_start(bs);
1986
	ENC_name_decl_dg(ts);
1987
	ts = enc_dg_decl(ts, id, LINK_NONE, USAGE_DEFN);
1988
	bs = enc_bitstream(bs, ts);
1989
	return (bs);
2 7u83 1990
}
1991
 
1992
 
1993
/*
1994
    ENCODE A LIST OF DIAGNOSTIC PARAMETERS
1995
 
1996
    This routine adds the diagnostic information for the list of function
1997
    parameters p to the bitstream bs.  ts and e give the function body.
1998
*/
1999
 
7 7u83 2000
BITSTREAM *
2001
enc_dg_params(BITSTREAM *bs, LIST(IDENTIFIER)p, BITSTREAM *ts, EXP e)
2 7u83 2002
{
7 7u83 2003
	bs = enc_diag_start(bs);
2004
	ENC_list_dg(ts);
2005
	ENC_LIST_SMALL(ts, 2);
2006
	ENC_params_dg(ts);
2007
	ENC_LIST(ts, LENGTH_list(p));
2008
	while (!IS_NULL_list(p)) {
2009
		IDENTIFIER pid = DEREF_id(HEAD_list(p));
2010
		ts = enc_dg_decl(ts, pid, LINK_NONE, USAGE_DEFN);
2011
		p = TAIL_list(p);
2012
	}
2013
	ENC_OFF(ts);
2014
	ts = enc_dg_stmt(ts, e, 1);
2015
	bs = enc_bitstream(bs, ts);
2016
	return (bs);
2 7u83 2017
}
2018
 
2019
 
2020
/*
2021
    ENCODE A DIAGNOSTIC STATEMENT
2022
 
2023
    This routine adds the diagnostic information associated with the
2024
    statement e to the bitstream bs.
2025
*/
2026
 
7 7u83 2027
BITSTREAM *
2028
enc_dg_stmt(BITSTREAM *bs, EXP e, int stmt)
2 7u83 2029
{
7 7u83 2030
	PTR(LOCATION)loc = crt_enc_loc;
2031
	if (!IS_NULL_exp(e)) {
2032
		switch (TAG_exp(e)) {
2033
		case exp_sequence_tag: {
2034
			/* Lexical block */
2035
			int blk = DEREF_int(exp_sequence_block(e));
2036
			NAMESPACE ns = DEREF_nspace(exp_sequence_decl(e));
2037
			if (blk) {
2038
				BITSTREAM *ts;
2039
				unsigned m = 0;
2040
				PTR(LOCATION)start_loc = block_loc(e, 0);
2041
				ts = start_bitstream(NIL(FILE), bs->link);
2042
				ts = enc_dg_namespace(ts, ns, &m, NULL_ctype);
2043
				if (blk > 1) {
2044
					m++;
2045
				}
2046
				if (m != 1) {
2047
					ENC_list_dg(bs);
2048
					ENC_LIST(bs, m);
2049
				}
2050
				if (blk > 1) {
2051
					ENC_lexical_block_dg(bs);
2052
					ENC_OFF(bs);
2053
					bs = enc_dg_loc(bs, start_loc, loc);
2054
				}
2055
				bs = join_bitstreams(bs, ts);
2056
				return (bs);
2057
			}
2058
			break;
2 7u83 2059
		}
7 7u83 2060
		case exp_solve_stmt_tag: {
2061
			EXP a = DEREF_exp(exp_solve_stmt_body(e));
2062
			bs = enc_dg_stmt(bs, a, stmt);
2063
			return (bs);
2 7u83 2064
		}
7 7u83 2065
		case exp_decl_stmt_tag: {
2066
			IDENTIFIER id = DEREF_id(exp_decl_stmt_id(e));
2067
			loc = id_loc(id);
2068
			break;
2069
		}
2070
		case exp_label_stmt_tag: {
2071
			IDENTIFIER id = DEREF_id(exp_label_stmt_label(e));
2072
			HASHID nm = DEREF_hashid(id_name(id));
2073
			loc = id_loc(id);
2074
			if (!IS_hashid_anon(nm)) {
2075
				/* Named label */
2076
				ENC_label_dg(bs);
2077
				bs = enc_dg_name(bs, id, NULL_type);
2078
				bs = enc_dg_loc(bs, loc, loc);
2079
				return (bs);
2080
			}
2081
			break;
2082
		}
2 7u83 2083
#if LANGUAGE_CPP
7 7u83 2084
		case exp_try_block_tag: {
2085
			/* Try block */
2086
			EXP a;
2087
			unsigned np;
2088
			LIST(EXP)p;
2089
			a = DEREF_exp(exp_try_block_ellipsis(e));
2090
			p = DEREF_list(exp_try_block_handlers(e));
2091
			np = LENGTH_list(p);
2092
			if (IS_exp_handler(a)) {
2093
				np++;
2094
			}
2095
			ENC_list_dg(bs);
2096
			ENC_LIST_SMALL(bs, 2);
2097
			ENC_exception_scope_dg(bs);
2098
			ENC_LIST(bs, np);
2099
			while (!IS_NULL_list(p)) {
2100
				EXP b = DEREF_exp(HEAD_list(p));
2101
				ulong m = capsule_no(NULL_string, VAR_dgtag);
2102
				COPY_ulong(exp_handler_diag(b), m);
2103
				m = link_no(bs, m, VAR_dgtag);
2104
				ENC_make_dg_tag(bs, m);
2105
				p = TAIL_list(p);
2106
			}
2107
			if (IS_exp_handler(a)) {
2108
				ulong m = capsule_no(NULL_string, VAR_dgtag);
2109
				COPY_ulong(exp_handler_diag(a), m);
2110
				m = link_no(bs, m, VAR_dgtag);
2111
				ENC_make_dg_tag(bs, m);
2112
			}
2113
			e = DEREF_exp(exp_try_block_body(e));
2114
			bs = enc_dg_stmt(bs, e, stmt);
2115
			return (bs);
2 7u83 2116
		}
7 7u83 2117
		case exp_handler_tag: {
2118
			/* Exception handler */
2119
			ulong m = DEREF_ulong(exp_handler_diag(e));
2120
			IDENTIFIER id = DEREF_id(exp_handler_except(e));
2121
			record_usage(m, VAR_dgtag, USAGE_DEFN);
2122
			ENC_list_dg(bs);
2123
			ENC_LIST_SMALL(bs, 2);
2124
			ENC_make_tag_dg(bs);
2125
			m = link_no(bs, m, VAR_dgtag);
2126
			ENC_make_dg_tag(bs, m);
2127
			ENC_exception_handler_dg(bs);
2128
			if (!IS_NULL_id(id)) {
2129
				ENC_ON(bs);
2130
				bs = enc_dg_decl(bs, id, LINK_NONE, USAGE_DEFN);
2131
			} else {
2132
				ENC_OFF(bs);
2133
			}
2134
			e = DEREF_exp(exp_handler_body(e));
2135
			bs = enc_dg_stmt(bs, e, stmt);
2136
			return (bs);
2 7u83 2137
		}
7 7u83 2138
#endif
2 7u83 2139
		}
2140
	}
7 7u83 2141
	if (stmt == 2) {
2142
		/* Used to mark conditionals */
2143
		ENC_list_dg(bs);
2144
		ENC_LIST_SMALL(bs, 2);
2145
		ENC_branch_dg(bs);
2146
		bs = enc_dg_loc(bs, loc, loc);
2147
	}
2148
	ENC_singlestep_dg(bs);
2149
	bs = enc_dg_loc(bs, loc, loc);
2150
	return (bs);
2 7u83 2151
}
2152
 
2153
 
2154
#endif /* TDF_OUTPUT && TDF_NEW_DIAG */