Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251
WebSVN – tendra.SVN – Diff – /trunk/src/producers/common/construct/allocate.c – Rev 2 and 7

Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 80... Line 110...
80
 
110
 
81
    This routine calculates the simple arithmetic operation 'a op b'.  Any
111
    This routine calculates the simple arithmetic operation 'a op b'.  Any
82
    conversion errors are suppressed.
112
    conversion errors are suppressed.
83
*/
113
*/
84
 
114
 
85
static EXP make_dim_exp
115
static EXP
86
    PROTO_N ( ( op, a, b ) )
-
 
87
    PROTO_T ( int op X EXP a X EXP b )
116
make_dim_exp(int op, EXP a, EXP b)
88
{
117
{
89
    EXP e ;
118
	EXP e;
90
    int et ;
119
	int et;
91
    if ( IS_NULL_exp ( a ) ) return ( b ) ;
120
	if (IS_NULL_exp(a)) {
-
 
121
		return (b);
-
 
122
	}
92
    if ( IS_NULL_exp ( b ) ) return ( a ) ;
123
	if (IS_NULL_exp(b)) {
-
 
124
		return (a);
-
 
125
	}
93
    et = error_threshold ;
126
	et = error_threshold;
94
    error_threshold = ERROR_SERIOUS ;
127
	error_threshold = ERROR_SERIOUS;
95
    if ( op == lex_plus ) {
128
	if (op == lex_plus) {
96
	e = make_plus_exp ( a, b ) ;
129
		e = make_plus_exp(a, b);
97
    } else {
130
	} else {
98
	e = make_mult_exp ( op, a, b ) ;
131
		e = make_mult_exp(op, a, b);
99
    }
132
	}
100
    error_threshold = et ;
133
	error_threshold = et;
101
    return ( e ) ;
134
	return (e);
102
}
135
}
103
 
136
 
104
 
137
 
105
/*
138
/*
106
    ALLOCATION ROUTINES
139
    ALLOCATION ROUTINES
107
 
140
 
108
    The memory allocation and deallocation routines are only contained in
141
    The memory allocation and deallocation routines are only contained in
109
    the C++ producer.
142
    the C++ producer.
110
*/
143
*/
111
 
144
 
112
#if LANGUAGE_CPP
145
#if LANGUAGE_CPP
113
 
146
 
114
 
147
 
115
/*
148
/*
116
    BAD ALLOCATION EXCEPTION TYPE
149
    BAD ALLOCATION EXCEPTION TYPE
117
 
150
 
118
    The variable type_bad_alloc is used to represent the standard exception
151
    The variable type_bad_alloc is used to represent the standard exception
119
    type 'std::bad_alloc' thrown when an allocation function fails.  The
152
    type 'std::bad_alloc' thrown when an allocation function fails.  The
120
    list alloc_types is used to record all the function types for simple
153
    list alloc_types is used to record all the function types for simple
121
    allocation functions.
154
    allocation functions.
122
*/
155
*/
123
 
156
 
124
static TYPE type_bad_alloc = NULL_type ;
157
static TYPE type_bad_alloc = NULL_type;
125
static LIST ( TYPE ) alloc_types = NULL_list ( TYPE ) ;
158
static LIST(TYPE) alloc_types = NULL_list(TYPE);
126
 
159
 
127
 
160
 
128
/*
161
/*
129
    SET THE BAD ALLOCATION EXCEPTION TYPE
162
    SET THE BAD ALLOCATION EXCEPTION TYPE
130
 
163
 
131
    This routine sets type_bad_alloc to be t, updating the exception
164
    This routine sets type_bad_alloc to be t, updating the exception
132
    specifiers of any simple allocation functions previously declared.
165
    specifiers of any simple allocation functions previously declared.
133
*/
166
*/
134
 
167
 
135
static void set_bad_alloc
168
static void
136
    PROTO_N ( ( t ) )
-
 
137
    PROTO_T ( TYPE t )
169
set_bad_alloc(TYPE t)
138
{
170
{
139
    if ( !IS_NULL_type ( t ) ) {
171
	if (!IS_NULL_type(t)) {
140
	LIST ( TYPE ) p = alloc_types ;
172
		LIST(TYPE)p = alloc_types;
141
	while ( !IS_NULL_list ( p ) ) {
173
		while (!IS_NULL_list(p)) {
142
	    TYPE s = DEREF_type ( HEAD_list ( p ) ) ;
174
			TYPE s = DEREF_type(HEAD_list(p));
143
	    LIST ( TYPE ) e = DEREF_list ( type_func_except ( s ) ) ;
175
			LIST(TYPE)e = DEREF_list(type_func_except(s));
144
	    if ( !IS_NULL_list ( e ) && !EQ_list ( e, univ_type_set ) ) {
176
			if (!IS_NULL_list(e) && !EQ_list(e, univ_type_set)) {
-
 
177
				/* Change 'throw ( X )' to
145
		/* Change 'throw ( X )' to 'throw ( std::bad_alloc )' */
178
				 * 'throw ( std::bad_alloc )' */
146
		e = TAIL_list ( e ) ;
179
				e = TAIL_list(e);
147
		CONS_type ( t, e, e ) ;
180
				CONS_type(t, e, e);
148
		COPY_list ( type_func_except ( s ), e ) ;
181
				COPY_list(type_func_except(s), e);
149
	    }
182
			}
150
	    p = TAIL_list ( p ) ;
183
			p = TAIL_list(p);
-
 
184
		}
-
 
185
		type_bad_alloc = t;
151
	}
186
	}
152
	type_bad_alloc = t ;
-
 
153
    }
-
 
154
    return ;
187
	return;
155
}
188
}
156
 
189
 
157
 
190
 
158
/*
191
/*
159
    CHECK AN ALLOCATION FUNCTION
192
    CHECK AN ALLOCATION FUNCTION
Line 172... Line 205...
172
    functions.  Note that template functions are allowed (indicated by
205
    functions.  Note that template functions are allowed (indicated by
173
    templ), but they must have the form above and at least one further
206
    templ), but they must have the form above and at least one further
174
    parameter.
207
    parameter.
175
*/
208
*/
176
 
209
 
177
TYPE check_allocator
210
TYPE
178
    PROTO_N ( ( t, id, mem, templ ) )
-
 
179
    PROTO_T ( TYPE t X IDENTIFIER id X int mem X int templ )
211
check_allocator(TYPE t, IDENTIFIER id, int mem, int templ)
180
{
212
{
181
    if ( IS_type_templ ( t ) ) {
213
	if (IS_type_templ(t)) {
182
	/* Allow for template types */
214
		/* Allow for template types */
183
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
215
		TYPE s = DEREF_type(type_templ_defn(t));
184
	s = check_allocator ( s, id, mem, templ + 1 ) ;
216
		s = check_allocator(s, id, mem, templ + 1);
185
	COPY_type ( type_templ_defn ( t ), s ) ;
217
		COPY_type(type_templ_defn(t), s);
186
 
218
 
187
    } else {
-
 
188
	/* Find the operator */
-
 
189
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
-
 
190
	int op = DEREF_int ( hashid_op_lex ( nm ) ) ;
-
 
191
 
-
 
192
	/* Decompose function type */
-
 
193
	TYPE s ;
-
 
194
	TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
-
 
195
	LIST ( TYPE ) p = DEREF_list ( type_func_ptypes ( t ) ) ;
-
 
196
	LIST ( IDENTIFIER ) q = DEREF_list ( type_func_pids ( t ) ) ;
-
 
197
	int ell = DEREF_int ( type_func_ellipsis ( t ) ) ;
-
 
198
	if ( !IS_NULL_list ( p ) ) {
-
 
199
	    s = DEREF_type ( HEAD_list ( p ) ) ;
-
 
200
	    p = TAIL_list ( p ) ;
-
 
201
	} else {
219
	} else {
-
 
220
		/* Find the operator */
-
 
221
		HASHID nm = DEREF_hashid(id_name(id));
-
 
222
		int op = DEREF_int(hashid_op_lex(nm));
-
 
223
 
-
 
224
		/* Decompose function type */
-
 
225
		TYPE s;
-
 
226
		TYPE r = DEREF_type(type_func_ret(t));
-
 
227
		LIST(TYPE)p = DEREF_list(type_func_ptypes(t));
-
 
228
		LIST(IDENTIFIER)q = DEREF_list(type_func_pids(t));
-
 
229
		int ell = DEREF_int(type_func_ellipsis(t));
-
 
230
		if (!IS_NULL_list(p)) {
-
 
231
			s = DEREF_type(HEAD_list(p));
-
 
232
			p = TAIL_list(p);
-
 
233
		} else {
202
	    s = type_void ;
234
			s = type_void;
203
	}
235
		}
204
 
236
 
205
	if ( op == lex_new || op == lex_new_Harray ) {
237
		if (op == lex_new || op == lex_new_Harray) {
206
	    /* Allocator should return 'void *' */
238
			/* Allocator should return 'void *' */
207
	    TYPE u = type_void_star ;
239
			TYPE u = type_void_star;
208
	    if ( !eq_type ( r, u ) ) {
240
			if (!eq_type(r, u)) {
209
		report ( crt_loc, ERR_basic_stc_alloc_ret ( nm, u ) ) ;
241
				report(crt_loc, ERR_basic_stc_alloc_ret(nm, u));
210
	    }
242
			}
211
 
243
 
212
	    /* First parameter should be 'size_t' */
244
			/* First parameter should be 'size_t' */
213
	    u = type_size_t ;
245
			u = type_size_t;
214
	    if ( !eq_type ( s, u ) ) {
246
			if (!eq_type(s, u)) {
215
		report ( crt_loc, ERR_basic_stc_alloc_p1 ( nm, u ) ) ;
247
				report(crt_loc, ERR_basic_stc_alloc_p1(nm, u));
216
	    }
248
			}
217
 
249
 
218
	    /* First parameter can't have a default argument */
250
			/* First parameter can't have a default argument */
219
	    if ( !IS_NULL_list ( q ) ) {
251
			if (!IS_NULL_list(q)) {
220
		IDENTIFIER pid = DEREF_id ( HEAD_list ( q ) ) ;
252
				IDENTIFIER pid = DEREF_id(HEAD_list(q));
221
		EXP darg = DEREF_exp ( id_parameter_init ( pid ) ) ;
253
				EXP darg = DEREF_exp(id_parameter_init(pid));
222
		if ( !IS_NULL_exp ( darg ) ) {
254
				if (!IS_NULL_exp(darg)) {
-
 
255
					report(crt_loc,
223
		    report ( crt_loc, ERR_basic_stc_alloc_d1 ( nm ) ) ;
256
					       ERR_basic_stc_alloc_d1(nm));
224
		}
257
				}
225
	    }
258
			}
226
 
259
 
227
	    /* Template functions should have another parameter */
260
			/* Template functions should have another parameter */
228
	    if ( templ && IS_NULL_list ( p ) ) {
261
			if (templ && IS_NULL_list(p)) {
229
		report ( crt_loc, ERR_basic_stc_alloc_templ ( nm ) ) ;
262
				report(crt_loc, ERR_basic_stc_alloc_templ(nm));
230
	    }
263
			}
231
 
264
 
232
	} else {
265
		} else {
233
	    /* Deallocator should return 'void' */
266
			/* Deallocator should return 'void' */
234
	    TYPE u = type_void ;
267
			TYPE u = type_void;
235
	    if ( !eq_type ( r, u ) ) {
268
			if (!eq_type(r, u)) {
236
		report ( crt_loc, ERR_basic_stc_alloc_ret ( nm, u ) ) ;
269
				report(crt_loc, ERR_basic_stc_alloc_ret(nm, u));
237
	    }
270
			}
238
 
271
 
239
	    /* First argument should be 'void *' */
272
			/* First argument should be 'void *' */
240
	    u = type_void_star ;
273
			u = type_void_star;
241
	    if ( !eq_type ( s, u ) ) {
274
			if (!eq_type(s, u)) {
242
		report ( crt_loc, ERR_basic_stc_alloc_p1 ( nm, u ) ) ;
275
				report(crt_loc, ERR_basic_stc_alloc_p1(nm, u));
243
	    }
276
			}
244
 
277
 
245
	    /* Template functions should have another parameter */
278
			/* Template functions should have another parameter */
246
	    if ( templ && IS_NULL_list ( p ) ) {
279
			if (templ && IS_NULL_list(p)) {
247
		report ( crt_loc, ERR_basic_stc_alloc_templ ( nm ) ) ;
280
				report(crt_loc, ERR_basic_stc_alloc_templ(nm));
248
	    }
281
			}
249
 
282
 
250
	    /* Second argument may be 'size_t' (old form) */
283
			/* Second argument may be 'size_t' (old form) */
251
	    if ( mem && !IS_NULL_list ( p ) ) {
284
			if (mem && !IS_NULL_list(p)) {
252
		u = type_size_t ;
285
				u = type_size_t;
253
		s = DEREF_type ( HEAD_list ( p ) ) ;
286
				s = DEREF_type(HEAD_list(p));
254
		if ( !eq_type ( s, u ) ) {
287
				if (!eq_type(s, u)) {
-
 
288
					report(crt_loc,
255
		    report ( crt_loc, ERR_basic_stc_alloc_p2 ( nm, u ) ) ;
289
					       ERR_basic_stc_alloc_p2(nm, u));
256
		}
290
				}
257
		p = TAIL_list ( p ) ;
291
				p = TAIL_list(p);
258
	    }
292
			}
259
 
293
 
260
	    /* No further arguments allowed (old form) */
294
			/* No further arguments allowed (old form) */
261
	    if ( !IS_NULL_list ( p ) || ell ) {
295
			if (!IS_NULL_list(p) || ell) {
262
		report ( crt_loc, ERR_basic_stc_alloc_pn ( nm ) ) ;
296
				report(crt_loc, ERR_basic_stc_alloc_pn(nm));
263
	    }
297
			}
264
	}
298
		}
265
 
299
 
266
	/* Look up 'std::bad_alloc' */
300
		/* Look up 'std::bad_alloc' */
267
	s = type_bad_alloc ;
301
		s = type_bad_alloc;
268
	if ( IS_NULL_type ( s ) ) {
302
		if (IS_NULL_type(s)) {
269
	    s = find_std_type ( "bad_alloc", 1, 0 ) ;
303
			s = find_std_type("bad_alloc", 1, 0);
270
	    set_bad_alloc ( s ) ;
304
			set_bad_alloc(s);
271
	}
305
		}
272
    }
306
	}
273
    return ( t ) ;
307
	return (t);
274
}
308
}
275
 
309
 
276
 
310
 
277
/*
311
/*
278
    CHECK AN ALLOCATOR DECLARATION
312
    CHECK AN ALLOCATOR DECLARATION
279
 
313
 
280
    This routine checks the allocator declaration id.  This should either
314
    This routine checks the allocator declaration id.  This should either
281
    be a class member or a member of the global namespace with external
315
    be a class member or a member of the global namespace with external
282
    linkage.  alloc is 1 for allocator functions and 2 for deallocation
316
    linkage.  alloc is 1 for allocator functions and 2 for deallocation
283
    functions.
317
    functions.
284
*/
318
*/
285
 
319
 
286
void recheck_allocator
320
void
287
    PROTO_N ( ( id, alloc ) )
-
 
288
    PROTO_T ( IDENTIFIER id X int alloc )
321
recheck_allocator(IDENTIFIER id, int alloc)
289
{
322
{
290
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
323
	NAMESPACE ns = DEREF_nspace(id_parent(id));
291
    if ( alloc == 2 ) {
324
	if (alloc == 2) {
292
	IDENTIFIER over = DEREF_id ( id_function_etc_over ( id ) ) ;
325
		IDENTIFIER over = DEREF_id(id_function_etc_over(id));
293
	if ( !IS_NULL_id ( over ) ) {
326
		if (!IS_NULL_id(over)) {
294
	    /* Can't overload 'operator delete' (old form) */
327
			/* Can't overload 'operator delete' (old form) */
295
	    report ( crt_loc, ERR_basic_stc_dealloc_over ( over ) ) ;
328
			report(crt_loc, ERR_basic_stc_dealloc_over(over));
296
	}
329
		}
297
    }
330
	}
298
    if ( !IS_NULL_nspace ( ns ) ) {
331
	if (!IS_NULL_nspace(ns)) {
299
	switch ( TAG_nspace ( ns ) ) {
332
		switch (TAG_nspace(ns)) {
300
	    case nspace_global_tag : {
333
		case nspace_global_tag: {
301
		/* Declared in global namespace */
334
			/* Declared in global namespace */
302
		DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
335
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
303
		if ( ds & dspec_static ) {
336
			if (ds & dspec_static) {
304
		    report ( crt_loc, ERR_basic_stc_alloc_link ( id ) ) ;
337
				report(crt_loc, ERR_basic_stc_alloc_link(id));
305
		}
338
			}
306
		if ( alloc == 1 && crt_file_type == 1 ) {
339
			if (alloc == 1 && crt_file_type == 1) {
307
		    /* Check for built-in allocation functions */
340
				/* Check for built-in allocation functions */
308
		    TYPE t = DEREF_type ( id_function_type ( id ) ) ;
341
				TYPE t = DEREF_type(id_function_type(id));
309
		    if ( IS_type_func ( t ) ) {
342
				if (IS_type_func(t)) {
310
			LIST ( TYPE ) p ;
343
					LIST(TYPE)p;
311
			p = DEREF_list ( type_func_ptypes ( t ) ) ;
344
					p = DEREF_list(type_func_ptypes(t));
312
			if ( LENGTH_list ( p ) == 1 ) {
345
					if (LENGTH_list(p) == 1) {
313
			    CONS_type ( t, alloc_types, alloc_types ) ;
346
						CONS_type(t, alloc_types,
-
 
347
							  alloc_types);
-
 
348
					}
-
 
349
				}
314
			}
350
			}
-
 
351
			break;
-
 
352
		}
-
 
353
		case nspace_ctype_tag: {
-
 
354
			/* Declared in class namespace */
-
 
355
			break;
-
 
356
		}
-
 
357
		default: {
-
 
358
			/* Declared in other namespace */
-
 
359
			report(crt_loc, ERR_basic_stc_alloc_nspace(id));
-
 
360
			break;
315
		    }
361
		}
316
		}
362
		}
317
		break ;
-
 
318
	    }
-
 
319
	    case nspace_ctype_tag : {
-
 
320
		/* Declared in class namespace */
-
 
321
		break ;
-
 
322
	    }
-
 
323
	    default : {
-
 
324
		/* Declared in other namespace */
-
 
325
		report ( crt_loc, ERR_basic_stc_alloc_nspace ( id ) ) ;
-
 
326
		break ;
-
 
327
	    }
-
 
328
	}
363
	}
329
    }
-
 
330
    return ;
364
	return;
331
}
365
}
332
 
366
 
333
 
367
 
334
/*
368
/*
335
    FIND A DEALLOCATION FUNCTION
369
    FIND A DEALLOCATION FUNCTION
Line 338... Line 372...
338
    functions id.  If pid is not the null identifier then it is an
372
    functions id.  If pid is not the null identifier then it is an
339
    allocation function for which a matching placement delete is required.
373
    allocation function for which a matching placement delete is required.
340
    mem is true for member functions.
374
    mem is true for member functions.
341
*/
375
*/
342
 
376
 
343
static IDENTIFIER resolve_delete
377
static IDENTIFIER
344
    PROTO_N ( ( id, pid, mem ) )
-
 
345
    PROTO_T ( IDENTIFIER id X IDENTIFIER pid X int mem )
378
resolve_delete(IDENTIFIER id, IDENTIFIER pid, int mem)
346
{
379
{
347
    int eq = 0 ;
380
	int eq = 0;
348
    IDENTIFIER rid ;
381
	IDENTIFIER rid;
349
    LIST ( TYPE ) p ;
382
	LIST(TYPE)p;
350
    TYPE fn = type_temp_func ;
383
	TYPE fn = type_temp_func;
351
    LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
384
	LIST(IDENTIFIER)pids = NULL_list(IDENTIFIER);
352
    COPY_type ( type_func_ret ( fn ), type_void ) ;
385
	COPY_type(type_func_ret(fn), type_void);
353
    COPY_cv ( type_func_mqual ( fn ), cv_none ) ;
386
	COPY_cv(type_func_mqual(fn), cv_none);
354
 
387
 
355
    /* Try placement delete */
388
	/* Try placement delete */
356
    if ( !IS_NULL_id ( pid ) ) {
389
	if (!IS_NULL_id(pid)) {
357
	TYPE t = DEREF_type ( id_function_etc_type ( pid ) ) ;
390
		TYPE t = DEREF_type(id_function_etc_type(pid));
358
	if ( IS_type_func ( t ) ) {
391
		if (IS_type_func(t)) {
359
	    p = DEREF_list ( type_func_ptypes ( t ) ) ;
392
			p = DEREF_list(type_func_ptypes(t));
360
	    if ( !IS_NULL_list ( p ) ) p = TAIL_list ( p ) ;
393
			if (!IS_NULL_list(p)) {
-
 
394
				p = TAIL_list(p);
-
 
395
			}
361
	    CONS_type ( type_void_star, p, p ) ;
396
			CONS_type(type_void_star, p, p);
362
	    COPY_list ( type_func_ptypes ( fn ), p ) ;
397
			COPY_list(type_func_ptypes(fn), p);
363
	    COPY_list ( type_func_mtypes ( fn ), p ) ;
398
			COPY_list(type_func_mtypes(fn), p);
364
	    rid = resolve_func ( id, fn, 1, 1, pids, &eq ) ;
399
			rid = resolve_func(id, fn, 1, 1, pids, &eq);
365
	    COPY_list ( type_func_ptypes ( fn ), NULL_list ( TYPE ) ) ;
400
			COPY_list(type_func_ptypes(fn), NULL_list(TYPE));
366
	    COPY_list ( type_func_mtypes ( fn ), NULL_list ( TYPE ) ) ;
401
			COPY_list(type_func_mtypes(fn), NULL_list(TYPE));
367
	    DESTROY_CONS_type ( destroy, t, p, p ) ;
402
			DESTROY_CONS_type(destroy, t, p, p);
368
	    UNUSED ( p ) ;
403
			UNUSED(p);
369
	    UNUSED ( t ) ;
404
			UNUSED(t);
370
	    if ( !IS_NULL_id ( rid ) ) return ( rid ) ;
405
			if (!IS_NULL_id(rid)) {
-
 
406
				return (rid);
-
 
407
			}
371
	}
408
		}
372
	return ( NULL_id ) ;
409
		return (NULL_id);
373
    }
-
 
374
 
410
	}
375
    /* Try 'void ( void * )' */
-
 
376
    CONS_type ( type_void_star, NULL_list ( TYPE ), p ) ;
-
 
377
    COPY_list ( type_func_ptypes ( fn ), p ) ;
-
 
378
    COPY_list ( type_func_mtypes ( fn ), p ) ;
-
 
379
    rid = resolve_func ( id, fn, 0, 1, pids, &eq ) ;
-
 
380
    COPY_list ( type_func_ptypes ( fn ), NULL_list ( TYPE ) ) ;
-
 
381
    COPY_list ( type_func_mtypes ( fn ), NULL_list ( TYPE ) ) ;
-
 
382
    DESTROY_list ( p, SIZE_type ) ;
-
 
383
    if ( !IS_NULL_id ( rid ) ) return ( rid ) ;
-
 
384
 
411
 
-
 
412
	/* Try 'void ( void * )' */
-
 
413
	CONS_type(type_void_star, NULL_list(TYPE), p);
-
 
414
	COPY_list(type_func_ptypes(fn), p);
-
 
415
	COPY_list(type_func_mtypes(fn), p);
-
 
416
	rid = resolve_func(id, fn, 0, 1, pids, &eq);
-
 
417
	COPY_list(type_func_ptypes(fn), NULL_list(TYPE));
-
 
418
	COPY_list(type_func_mtypes(fn), NULL_list(TYPE));
-
 
419
	DESTROY_list(p, SIZE_type);
-
 
420
	if (!IS_NULL_id(rid)) {
-
 
421
		return (rid);
-
 
422
	}
-
 
423
 
385
    /* Try 'void ( void *, size_t )' */
424
	/* Try 'void ( void *, size_t )' */
386
    if ( mem ) {
425
	if (mem) {
387
	CONS_type ( type_size_t, NULL_list ( TYPE ), p ) ;
426
		CONS_type(type_size_t, NULL_list(TYPE), p);
388
	CONS_type ( type_void_star, p, p ) ;
427
		CONS_type(type_void_star, p, p);
389
	COPY_list ( type_func_ptypes ( fn ), p ) ;
428
		COPY_list(type_func_ptypes(fn), p);
390
	COPY_list ( type_func_mtypes ( fn ), p ) ;
429
		COPY_list(type_func_mtypes(fn), p);
391
	rid = resolve_func ( id, fn, 0, 1, pids, &eq ) ;
430
		rid = resolve_func(id, fn, 0, 1, pids, &eq);
392
	COPY_list ( type_func_ptypes ( fn ), NULL_list ( TYPE ) ) ;
431
		COPY_list(type_func_ptypes(fn), NULL_list(TYPE));
393
	COPY_list ( type_func_mtypes ( fn ), NULL_list ( TYPE ) ) ;
432
		COPY_list(type_func_mtypes(fn), NULL_list(TYPE));
394
	DESTROY_list ( p, SIZE_type ) ;
433
		DESTROY_list(p, SIZE_type);
395
	if ( !IS_NULL_id ( rid ) ) return ( rid ) ;
434
		if (!IS_NULL_id(rid)) {
-
 
435
			return (rid);
396
    }
436
		}
-
 
437
	}
397
    return ( NULL_id ) ;
438
	return (NULL_id);
398
}
439
}
399
 
440
 
400
 
441
 
401
/*
442
/*
402
    LOOK UP AN ALLOCATOR FUNCTION
443
    LOOK UP AN ALLOCATOR FUNCTION
Line 407... Line 448...
407
    currently in scope is checked.  If option new_array is false and op
448
    currently in scope is checked.  If option new_array is false and op
408
    is an array allocator, then the corresponding object allocator is
449
    is an array allocator, then the corresponding object allocator is
409
    returned, except if t is a class which has 'operator op' declared.
450
    returned, except if t is a class which has 'operator op' declared.
410
*/
451
*/
411
 
452
 
412
IDENTIFIER find_allocator
453
IDENTIFIER
413
    PROTO_N ( ( t, op, b, pid ) )
-
 
414
    PROTO_T ( TYPE t X int op X int b X IDENTIFIER pid )
454
find_allocator(TYPE t, int op, int b, IDENTIFIER pid)
415
{
455
{
416
    int dealloc = 0 ;
456
	int dealloc = 0;
417
    IDENTIFIER id = NULL_id ;
457
	IDENTIFIER id = NULL_id;
418
    HASHID nm = lookup_op ( op ) ;
458
	HASHID nm = lookup_op(op);
419
    HASHID nm_real = nm ;
459
	HASHID nm_real = nm;
420
 
460
 
421
    /* Allow for pre-ISO dialect */
461
	/* Allow for pre-ISO dialect */
422
    switch ( op ) {
462
	switch (op) {
423
	case lex_new : {
463
	case lex_new: {
424
	    break ;
464
		break;
425
	}
465
	}
426
	case lex_new_Harray : {
466
	case lex_new_Harray: {
427
	    if ( !option ( OPT_new_array ) ) {
467
		if (!option(OPT_new_array)) {
428
		nm = lookup_op ( lex_new ) ;
468
			nm = lookup_op(lex_new);
429
		t = type_error ;
469
			t = type_error;
430
	    }
470
		}
431
	    break ;
471
		break;
432
	}
472
	}
433
	case lex_delete : {
473
	case lex_delete: {
434
	    dealloc = 1 ;
474
		dealloc = 1;
435
	    break ;
475
		break;
436
	}
476
	}
437
	case lex_delete_Harray : {
477
	case lex_delete_Harray: {
438
	    if ( !option ( OPT_new_array ) ) {
478
		if (!option(OPT_new_array)) {
439
		nm = lookup_op ( lex_delete ) ;
479
			nm = lookup_op(lex_delete);
440
		t = type_error ;
480
			t = type_error;
441
	    }
481
		}
442
	    dealloc = 1 ;
482
		dealloc = 1;
443
	    break ;
483
		break;
444
	}
484
	}
445
    }
485
	}
446
 
486
 
447
    if ( b ) {
487
	if (b) {
448
	/* Try global scope ... */
488
		/* Try global scope ... */
449
	NAMESPACE ns = global_namespace ;
489
		NAMESPACE ns = global_namespace;
450
	MEMBER mem = search_member ( ns, nm, 0 ) ;
490
		MEMBER mem = search_member(ns, nm, 0);
451
	if ( !IS_NULL_member ( mem ) ) {
491
		if (!IS_NULL_member(mem)) {
452
	    id = DEREF_id ( member_id ( mem ) ) ;
492
			id = DEREF_id(member_id(mem));
453
	    if ( !IS_NULL_id ( id ) && dealloc ) {
493
			if (!IS_NULL_id(id) && dealloc) {
454
		id = resolve_delete ( id, pid, 0 ) ;
494
				id = resolve_delete(id, pid, 0);
455
	    }
495
			}
456
	}
496
		}
457
 
497
 
458
    } else {
498
	} else {
459
	/* Try class members ... */
499
		/* Try class members ... */
460
	if ( IS_type_compound ( t ) ) {
500
		if (IS_type_compound(t)) {
461
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
501
			CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
462
	    NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
502
			NAMESPACE ns = DEREF_nspace(ctype_member(ct));
463
	    id = search_field ( ns, nm_real, 0, 0 ) ;
503
			id = search_field(ns, nm_real, 0, 0);
464
	    if ( IS_NULL_id ( id ) && !EQ_hashid ( nm, nm_real ) ) {
504
			if (IS_NULL_id(id) && !EQ_hashid(nm, nm_real)) {
465
		id = search_field ( ns, nm, 0, 0 ) ;
505
				id = search_field(ns, nm, 0, 0);
466
	    }
506
			}
467
	    if ( !IS_NULL_id ( id ) && IS_id_ambig ( id ) ) {
507
			if (!IS_NULL_id(id) && IS_id_ambig(id)) {
468
		id = report_ambiguous ( id, 0, 1, 1 ) ;
508
				id = report_ambiguous(id, 0, 1, 1);
469
	    }
509
			}
470
	    if ( !IS_NULL_id ( id ) && dealloc ) {
510
			if (!IS_NULL_id(id) && dealloc) {
471
		id = resolve_delete ( id, pid, 1 ) ;
511
				id = resolve_delete(id, pid, 1);
472
	    }
512
			}
473
	}
513
		}
474
 
514
 
475
	/* Try current scope ... */
515
		/* Try current scope ... */
476
	if ( IS_NULL_id ( id ) ) {
516
		if (IS_NULL_id(id)) {
477
	    id = find_op_id ( nm ) ;
517
			id = find_op_id(nm);
478
	    if ( !IS_NULL_id ( id ) && dealloc ) {
518
			if (!IS_NULL_id(id) && dealloc) {
479
		id = resolve_delete ( id, pid, 0 ) ;
519
				id = resolve_delete(id, pid, 0);
480
	    }
520
			}
481
	}
521
		}
482
    }
522
	}
483
 
523
 
484
    /* Return function */
524
	/* Return function */
485
    if ( !IS_NULL_id ( id ) ) {
525
	if (!IS_NULL_id(id)) {
486
	if ( IS_id_function_etc ( id ) ) {
526
		if (IS_id_function_etc(id)) {
487
	    /* Function found */
527
			/* Function found */
488
	    return ( id ) ;
528
			return (id);
489
	}
529
		}
490
	if ( is_ambiguous_func ( id ) ) {
530
		if (is_ambiguous_func(id)) {
491
	    if ( dealloc ) {
531
			if (dealloc) {
492
		/* Can't do overload resolution on delete */
532
				/* Can't do overload resolution on delete */
493
		id = report_ambiguous ( id, 0, 1, 1 ) ;
533
				id = report_ambiguous(id, 0, 1, 1);
494
		return ( id ) ;
534
				return (id);
495
	    }
535
			}
496
	    return ( id ) ;
536
			return (id);
497
	}
537
		}
498
	if ( !IS_id_dummy ( id ) ) {
538
		if (!IS_id_dummy(id)) {
499
	    /* Result is not a function */
539
			/* Result is not a function */
500
	    report ( crt_loc, ERR_over_oper_func ( id ) ) ;
540
			report(crt_loc, ERR_over_oper_func(id));
501
	}
541
		}
502
    }
542
	}
503
    if ( IS_NULL_id ( pid ) ) {
543
	if (IS_NULL_id(pid)) {
504
	/* Allocation functions not declared */
544
		/* Allocation functions not declared */
505
	report ( crt_loc, ERR_lib_builtin ( NULL_string, nm ) ) ;
545
		report(crt_loc, ERR_lib_builtin(NULL_string, nm));
506
    }
546
	}
507
    return ( NULL_id ) ;
547
	return (NULL_id);
508
}
548
}
509
 
549
 
510
 
550
 
511
/*
551
/*
512
    CONSTRUCT A TEMPLATE DEPENDENT DELETE EXPRESSION
552
    CONSTRUCT A TEMPLATE DEPENDENT DELETE EXPRESSION
513
 
553
 
514
    This routine constructs a delete expression in the case where the
554
    This routine constructs a delete expression in the case where the
515
    expression type depends on a template parameter.
555
    expression type depends on a template parameter.
516
*/
556
*/
517
 
557
 
518
static EXP make_templ_delete
558
static EXP
519
    PROTO_N ( ( op, b, a ) )
-
 
520
    PROTO_T ( int op X int b X EXP a )
559
make_templ_delete(int op, int b, EXP a)
521
{
560
{
522
    EXP e ;
561
	EXP e;
523
    if ( b ) {
562
	if (b) {
524
	/* Allow for '::delete' */
563
		/* Allow for '::delete' */
525
	if ( op == lex_delete ) {
564
		if (op == lex_delete) {
526
	    op = lex_delete_Hfull ;
565
			op = lex_delete_Hfull;
527
	} else {
566
		} else {
528
	    op = lex_delete_Harray_Hfull ;
567
			op = lex_delete_Harray_Hfull;
-
 
568
		}
529
	}
569
	}
530
    }
-
 
531
    MAKE_exp_op ( type_void, op, a, NULL_exp, e ) ;
570
	MAKE_exp_op(type_void, op, a, NULL_exp, e);
532
    return ( e ) ;
571
	return (e);
533
}
572
}
534
 
573
 
535
 
574
 
536
/*
575
/*
537
    CONSTRUCT A PLACEMENT DELETE EXPRESSION
576
    CONSTRUCT A PLACEMENT DELETE EXPRESSION
Line 539... Line 578...
539
    This routine constructs the expressions 'delete a' and 'delete [] a'
578
    This routine constructs the expressions 'delete a' and 'delete [] a'
540
    (as indicated by op).  b indicates whether the expression was actually
579
    (as indicated by op).  b indicates whether the expression was actually
541
    '::delete'.  pid is used in placement delete expressions to give the
580
    '::delete'.  pid is used in placement delete expressions to give the
542
    corresponding allocation function (place then gives the extra
581
    corresponding allocation function (place then gives the extra
543
    arguments), otherwise it is the null identifier.
582
    arguments), otherwise it is the null identifier.
722
	}
755
	}
-
 
756
 
-
 
757
	/* Construct function call */
-
 
758
	if (!IS_NULL_id(id)) {
-
 
759
		if (need_cast) {
-
 
760
			MAKE_exp_cast(type_void_star, CONV_PTR_VOID, e, e);
-
 
761
		}
-
 
762
		CONS_exp(e, args, args);
-
 
763
		if (IS_id_stat_mem_func(id)) {
-
 
764
			/* Allow for static member functions */
-
 
765
			CONS_exp(NULL_exp, args, args);
-
 
766
		}
-
 
767
		use_func_id(id, 0, suppress_usage);
-
 
768
		e = apply_func_id(id, qual_none, NULL_graph, args);
-
 
769
		if (v == (EXTRA_DESTR | EXTRA_DELETE)) {
-
 
770
			/* 'operator delete' called via destructor */
-
 
771
			MAKE_exp_paren(type_void, e, e);
-
 
772
		}
723
    } else {
773
	} else {
724
	e = NULL_exp ;
774
		e = NULL_exp;
725
    }
775
	}
726
 
776
 
727
    /* Construct result */
777
	/* Construct result */
728
    MAKE_exp_dealloc ( type_void, d, e, a, c, e ) ;
778
	MAKE_exp_dealloc(type_void, d, e, a, c, e);
729
    return ( e ) ;
779
	return (e);
730
}
780
}
731
 
781
 
732
 
782
 
733
/*
783
/*
734
    CREATE A SIMPLE DELETE EXPRESSION
784
    CREATE A SIMPLE DELETE EXPRESSION
735
 
785
 
736
    This routine is a special case of placement_delete which handles the
786
    This routine is a special case of placement_delete which handles the
737
    explicit delete expressions.
787
    explicit delete expressions.
738
*/
788
*/
739
 
789
 
740
EXP make_delete_exp
790
EXP
741
    PROTO_N ( ( op, b, a ) )
-
 
742
    PROTO_T ( int op X int b X EXP a )
791
make_delete_exp(int op, int b, EXP a)
743
{
792
{
744
    EXP e = placement_delete ( op, b, a, NULL_id, NULL_list ( EXP ) ) ;
793
	EXP e = placement_delete(op, b, a, NULL_id, NULL_list(EXP));
745
    return ( e ) ;
794
	return (e);
746
}
795
}
747
 
796
 
748
 
797
 
749
/*
798
/*
750
    DELETE ARRAY ANACHRONISM
799
    DELETE ARRAY ANACHRONISM
751
 
800
 
752
    It used to be necessary to include the size of the array being deleted
801
    It used to be necessary to include the size of the array being deleted
753
    in 'delete []'.  This routine deals with this anachronism.
802
    in 'delete []'.  This routine deals with this anachronism.
754
*/
803
*/
755
 
804
 
756
void old_delete_array
805
void
757
    PROTO_N ( ( e ) )
-
 
758
    PROTO_T ( EXP e )
806
old_delete_array(EXP e)
759
{
807
{
760
    /* Check that e is a suitable array bound */
808
	/* Check that e is a suitable array bound */
761
    int op = lex_delete_Harray ;
809
	int op = lex_delete_Harray;
762
    IGNORE make_new_array_dim ( e ) ;
810
	IGNORE make_new_array_dim(e);
763
 
811
 
764
    /* But complain just the same */
812
	/* But complain just the same */
765
    report ( crt_loc, ERR_expr_delete_array ( op ) ) ;
813
	report(crt_loc, ERR_expr_delete_array(op));
766
    return ;
814
	return;
767
}
815
}
768
 
816
 
769
 
817
 
770
/*
818
/*
771
    CONSTRUCT A NEW ARRAY BOUND
819
    CONSTRUCT A NEW ARRAY BOUND
772
 
820
 
773
    In a new-declarator the first array bound can be a variable expression,
821
    In a new-declarator the first array bound can be a variable expression,
774
    whereas all subsequent array bounds must be constant expressions as
822
    whereas all subsequent array bounds must be constant expressions as
775
    normal.  This routine is a version of make_array_dim designed exclusively
823
    normal.  This routine is a version of make_array_dim designed exclusively
776
    to deal with this first bound.  Note that the result is not strictly
824
    to deal with this first bound.  Note that the result is not strictly
777
    a legal NAT and is only used to pass the bound information to
825
    a legal NAT and is only used to pass the bound information to
778
    make_new_exp, where it is prompty destroyed.
826
    make_new_exp, where it is prompty destroyed.
779
*/
827
*/
780
 
828
 
-
 
829
NAT
781
NAT make_new_array_dim
830
make_new_array_dim(EXP e)
782
    PROTO_N ( ( e ) )
-
 
783
    PROTO_T ( EXP e )
-
 
784
{
831
{
785
    NAT n ;
832
	NAT n;
786
    if ( IS_exp_int_lit ( e ) ) {
833
	if (IS_exp_int_lit(e)) {
787
	/* Get the value if e is constant */
834
		/* Get the value if e is constant */
788
	n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
835
		n = DEREF_nat(exp_int_lit_nat(e));
789
    } else {
836
	} else {
790
	/* Make dummy literal */
837
		/* Make dummy literal */
791
	MAKE_nat_calc ( e, n ) ;
838
		MAKE_nat_calc(e, n);
792
    }
839
	}
793
    return ( n ) ;
840
	return (n);
794
}
841
}
795
 
842
 
796
 
843
 
797
/*
844
/*
798
    CONSTRUCT A TEMPLATE DEPENDENT NEW EXPRESSION
845
    CONSTRUCT A TEMPLATE DEPENDENT NEW EXPRESSION
799
 
846
 
800
    This routine constructs a new expression in the case where the object
847
    This routine constructs a new expression in the case where the object
801
    type is a template parameter.  t gives the given type with array
848
    type is a template parameter.  t gives the given type with array
802
    dimension d, while p is the pointer type.
849
    dimension d, while p is the pointer type.
803
*/
850
*/
804
 
851
 
805
static EXP make_templ_new
852
static EXP
806
    PROTO_N ( ( t, d, p, b, place, init ) )
-
 
807
    PROTO_T ( TYPE t X EXP d X TYPE p X int b X LIST ( EXP ) place X EXP init )
853
make_templ_new(TYPE t, EXP d, TYPE p, int b, LIST(EXP)place, EXP init)
808
{
854
{
809
    EXP e ;
855
	EXP e;
810
    int op = ( b ? lex_new_Hfull : lex_new ) ;
856
	int op = (b ? lex_new_Hfull : lex_new);
811
    CONS_exp ( init, place, place ) ;
857
	CONS_exp(init, place, place);
812
    CONS_exp ( d, place, place ) ;
858
	CONS_exp(d, place, place);
813
    MAKE_exp_value ( t, e ) ;
859
	MAKE_exp_value(t, e);
814
    CONS_exp ( e, place, place ) ;
860
	CONS_exp(e, place, place);
815
    MAKE_exp_opn ( p, op, place, e ) ;
861
	MAKE_exp_opn(p, op, place, e);
816
    return ( e ) ;
862
	return (e);
817
}
863
}
818
 
864
 
819
 
865
 
820
/*
866
/*
821
    CONSTRUCT A NEW EXPRESSION
867
    CONSTRUCT A NEW EXPRESSION
822
 
868
 
823
    This routine constructs the expression 'new ( place ) ( t ) ( init )',
869
    This routine constructs the expression 'new ( place ) ( t ) ( init )',
824
    where place is a possibly empty list of expressions and init is
870
    where place is a possibly empty list of expressions and init is
825
    a new-initialiser expression.  n gives the number of types defined
871
    a new-initialiser expression.  n gives the number of types defined
826
    in t and b indicates whether the expression was actually '::new'.
872
    in t and b indicates whether the expression was actually '::new'.
987
 
1042
 
988
    /* Return the result */
1043
	/* Return the result */
989
    MAKE_exp_alloc ( ret, e, init, gc, arr, e ) ;
1044
	MAKE_exp_alloc(ret, e, init, gc, arr, e);
990
    return ( e ) ;
1045
	return (e);
991
}
1046
}
992
 
1047
 
993
 
1048
 
994
/*
1049
/*
995
    CREATE A NEW-INITIALISER
1050
    CREATE A NEW-INITIALISER
996
 
1051
 
997
    This routine creates a new-initialiser expression of type t from the
1052
    This routine creates a new-initialiser expression of type t from the
998
    expression list p.
1053
    expression list p.
999
*/
1054
*/
1000
 
1055
 
1001
EXP make_new_init
1056
EXP
1002
    PROTO_N ( ( t, p, init ) )
-
 
1003
    PROTO_T ( TYPE t X LIST ( EXP ) p X int init )
1057
make_new_init(TYPE t, LIST(EXP)p, int init)
1004
{
1058
{
1005
    EXP e ;
1059
	EXP e;
1006
    int op = lex_new ;
1060
	int op = lex_new;
1007
    ERROR err = check_complete ( t ) ;
1061
	ERROR err = check_complete(t);
1008
    if ( !IS_NULL_err ( err ) ) {
1062
	if (!IS_NULL_err(err)) {
1009
	/* Type should be complete */
1063
		/* Type should be complete */
1010
	err = concat_error ( err, ERR_expr_new_incompl () ) ;
1064
		err = concat_error(err, ERR_expr_new_incompl());
1011
	report ( crt_loc, err ) ;
-
 
1012
    }
-
 
1013
    err = check_abstract ( t ) ;
-
 
1014
    if ( !IS_NULL_err ( err ) ) {
-
 
1015
	/* Type can't be abstract */
-
 
1016
	err = concat_error ( err, ERR_expr_new_abstract () ) ;
-
 
1017
	report ( crt_loc, err ) ;
1065
		report(crt_loc, err);
1018
	err = NULL_err ;
-
 
1019
    }
-
 
1020
    while ( IS_type_array ( t ) ) {
-
 
1021
	/* Step over array components */
-
 
1022
	op = lex_new_Harray ;
-
 
1023
	if ( init ) {
-
 
1024
	    report ( crt_loc, ERR_expr_new_array_init ( op ) ) ;
-
 
1025
	    init = 0 ;
-
 
1026
	}
1066
	}
1027
	t = DEREF_type ( type_array_sub ( t ) ) ;
-
 
1028
    }
-
 
1029
    p = convert_args ( p ) ;
1067
	err = check_abstract(t);
1030
    if ( is_templ_type ( t ) ) {
1068
	if (!IS_NULL_err(err)) {
1031
	if ( op == lex_new_Harray ) {
1069
		/* Type can't be abstract */
1032
	    /* Create dummy array type */
1070
		err = concat_error(err, ERR_expr_new_abstract());
1033
	    NAT n = small_nat [1] ;
1071
		report(crt_loc, err);
1034
	    MAKE_type_array ( cv_none, t, n, t ) ;
1072
		err = NULL_err;
1035
	}
1073
	}
-
 
1074
	while (IS_type_array(t)) {
-
 
1075
		/* Step over array components */
-
 
1076
		op = lex_new_Harray;
1036
	if ( init ) {
1077
		if (init) {
1037
	    MAKE_exp_opn ( t, lex_compute, p, e ) ;
1078
			report(crt_loc, ERR_expr_new_array_init(op));
1038
	} else {
1079
			init = 0;
-
 
1080
		}
1039
	    MAKE_exp_op ( t, lex_compute, NULL_exp, NULL_exp, e ) ;
1081
		t = DEREF_type(type_array_sub(t));
1040
	}
1082
	}
-
 
1083
	p = convert_args(p);
-
 
1084
	if (is_templ_type(t)) {
-
 
1085
		if (op == lex_new_Harray) {
-
 
1086
			/* Create dummy array type */
-
 
1087
			NAT n = small_nat[1];
-
 
1088
			MAKE_type_array(cv_none, t, n, t);
-
 
1089
		}
-
 
1090
		if (init) {
-
 
1091
			MAKE_exp_opn(t, lex_compute, p, e);
1041
    } else {
1092
		} else {
-
 
1093
			MAKE_exp_op(t, lex_compute, NULL_exp, NULL_exp, e);
-
 
1094
		}
-
 
1095
	} else {
1042
	if ( init ) {
1096
		if (init) {
1043
	    e = init_constr ( t, p, &err ) ;
1097
			e = init_constr(t, p, &err);
1044
	} else {
1098
		} else {
1045
	    e = init_empty ( t, cv_none, 0, &err ) ;
1099
			e = init_empty(t, cv_none, 0, &err);
1046
	}
1100
		}
1047
	if ( !IS_NULL_err ( err ) ) {
1101
		if (!IS_NULL_err(err)) {
1048
	    /* Report conversion errors */
1102
			/* Report conversion errors */
1049
	    err = concat_error ( ERR_expr_new_init ( op ), err ) ;
1103
			err = concat_error(ERR_expr_new_init(op), err);
1050
	    report ( crt_loc, err ) ;
1104
			report(crt_loc, err);
1051
	}
1105
		}
1052
	if ( !IS_NULL_exp ( e ) ) {
1106
		if (!IS_NULL_exp(e)) {
1053
	    /* Assign value to dummy expression */
1107
			/* Assign value to dummy expression */
1054
	    EXP a ;
1108
			EXP a;
1055
	    MAKE_exp_dummy ( t, NULL_exp, LINK_NONE, NULL_off, 1, a ) ;
1109
			MAKE_exp_dummy(t, NULL_exp, LINK_NONE, NULL_off, 1, a);
1056
	    MAKE_exp_assign ( t, a, e, e ) ;
1110
			MAKE_exp_assign(t, a, e, e);
-
 
1111
		}
1057
	}
1112
	}
1058
    }
-
 
1059
    return ( e ) ;
1113
	return (e);
1060
}
1114
}
1061
 
1115
 
1062
 
1116
 
1063
/*
1117
/*
1064
    BEGIN A NEW-INITIALISER TRY BLOCK
1118
    BEGIN A NEW-INITIALISER TRY BLOCK
1065
 
1119
 
1066
    Each new-initialiser is enclosed in a dummy try block.  This is because
1120
    Each new-initialiser is enclosed in a dummy try block.  This is because
1067
    if the initialiser throws an exception it is necessary to catch it,
1121
    if the initialiser throws an exception it is necessary to catch it,
1068
    delete the memory just allocated, and then re-throw the exception to
1122
    delete the memory just allocated, and then re-throw the exception to
1069
    the enclosing real handler.
1123
    the enclosing real handler.
1070
*/
1124
*/
1071
 
1125
 
1072
EXP begin_new_try
1126
EXP
1073
    PROTO_Z ()
1127
begin_new_try(void)
1074
{
1128
{
1075
    EXP a = begin_try_stmt ( 0 ) ;
1129
	EXP a = begin_try_stmt(0);
1076
    EXP b = begin_compound_stmt ( 2 ) ;
1130
	EXP b = begin_compound_stmt(2);
1077
    COPY_exp ( exp_try_block_body ( a ), b ) ;
1131
	COPY_exp(exp_try_block_body(a), b);
1078
    return ( a ) ;
1132
	return (a);
1079
}
1133
}
1080
 
1134
 
1081
 
1135
 
1082
/*
1136
/*
1083
    END A NEW-INITIALISER TRY BLOCK
1137
    END A NEW-INITIALISER TRY BLOCK
1084
 
1138
 
1085
    This routine adds the new-initialiser expression b to the try block a.
1139
    This routine adds the new-initialiser expression b to the try block a.
1086
*/
1140
*/
1087
 
1141
 
1088
EXP end_new_try
1142
EXP
1089
    PROTO_N ( ( a, b ) )
-
 
1090
    PROTO_T ( EXP a X EXP b )
1143
end_new_try(EXP a, EXP b)
1091
{
1144
{
1092
    EXP c = DEREF_exp ( exp_try_block_body ( a ) ) ;
1145
	EXP c = DEREF_exp(exp_try_block_body(a));
1093
    c = add_compound_stmt ( c, b ) ;
1146
	c = add_compound_stmt(c, b);
1094
    c = end_compound_stmt ( c ) ;
1147
	c = end_compound_stmt(c);
1095
    a = cont_try_stmt ( a, c ) ;
1148
	a = cont_try_stmt(a, c);
1096
    a = end_try_stmt ( a, 1 ) ;
1149
	a = end_try_stmt(a, 1);
1097
    if ( IS_NULL_exp ( b ) ) {
1150
	if (IS_NULL_exp(b)) {
1098
	free_exp ( a, 1 ) ;
1151
		free_exp(a, 1);
1099
	a = NULL_exp ;
1152
		a = NULL_exp;
1100
    }
1153
	}
1101
    return ( a ) ;
1154
	return (a);
1102
}
1155
}
1103
 
1156
 
1104
 
1157
 
1105
/*
1158
/*
1106
    FIND THE BODY OF A NEW-INITIALISER TRY BLOCK
1159
    FIND THE BODY OF A NEW-INITIALISER TRY BLOCK
1107
 
1160
 
1108
    This routine returns the initialiser component of the new-initialiser
1161
    This routine returns the initialiser component of the new-initialiser
1109
    try block a.
1162
    try block a.
1110
*/
1163
*/
1111
 
1164
 
1112
EXP new_try_body
1165
EXP
1113
    PROTO_N ( ( a ) )
-
 
1114
    PROTO_T ( EXP a )
1166
new_try_body(EXP a)
1115
{
1167
{
1116
    while ( !IS_NULL_exp ( a ) ) {
1168
	while (!IS_NULL_exp(a)) {
1117
	switch ( TAG_exp ( a ) ) {
1169
		switch (TAG_exp(a)) {
1118
	    case exp_try_block_tag : {
1170
		case exp_try_block_tag: {
1119
		a = DEREF_exp ( exp_try_block_body ( a ) ) ;
1171
			a = DEREF_exp(exp_try_block_body(a));
1120
		break ;
1172
			break;
1121
	    }
1173
		}
1122
	    case exp_decl_stmt_tag : {
1174
		case exp_decl_stmt_tag: {
1123
		a = DEREF_exp ( exp_decl_stmt_body ( a ) ) ;
1175
			a = DEREF_exp(exp_decl_stmt_body(a));
1124
		break ;
1176
			break;
1125
	    }
1177
		}
1126
	    case exp_sequence_tag : {
1178
		case exp_sequence_tag: {
1127
		LIST ( EXP ) p = DEREF_list ( exp_sequence_first ( a ) ) ;
1179
			LIST(EXP)p = DEREF_list(exp_sequence_first(a));
1128
		p = TAIL_list ( p ) ;
1180
			p = TAIL_list(p);
1129
		if ( IS_NULL_list ( p ) ) {
1181
			if (IS_NULL_list(p)) {
1130
		    a = NULL_exp ;
1182
				a = NULL_exp;
1131
		} else {
1183
			} else {
1132
		    a = DEREF_exp ( HEAD_list ( p ) ) ;
1184
				a = DEREF_exp(HEAD_list(p));
1133
		}
1185
			}
1134
		break ;
1186
			break;
1135
	    }
1187
		}
1136
	    case exp_location_tag : {
1188
		case exp_location_tag: {
1137
		a = DEREF_exp ( exp_location_arg ( a ) ) ;
1189
			a = DEREF_exp(exp_location_arg(a));
1138
		break ;
1190
			break;
1139
	    }
1191
		}
1140
	    default : {
1192
		default: {
1141
		return ( a ) ;
1193
			return (a);
1142
	    }
1194
		}
1143
	}
1195
		}
1144
    }
1196
	}
1145
    return ( NULL_exp ) ;
1197
	return (NULL_exp);
1146
}
1198
}
1147
 
1199
 
1148
 
1200
 
1149
/*
1201
/*
1150
    END OF ALLOCATION ROUTINES
1202
    END OF ALLOCATION ROUTINES
1151
 
1203
 
1152
    The remaining routines are common to both producers.
1204
    The remaining routines are common to both producers.
1153
*/
1205
*/
1154
 
1206
 
1155
#endif /* LANGUAGE_CPP */
1207
#endif /* LANGUAGE_CPP */
1156
 
1208
 
1157
 
1209
 
1158
/*
1210
/*
Line 1161... Line 1213...
1161
    This routine multiplies the dimensions of any array components in the
1213
    This routine multiplies the dimensions of any array components in the
1162
    type pointed to by pt returning it as an expression of type s.  It
1214
    type pointed to by pt returning it as an expression of type s.  It
1163
    assigns the non-array components back to pt.
1215
    assigns the non-array components back to pt.
1164
*/
1216
*/
1165
 
1217
 
1166
EXP sizeof_array
1218
EXP
1167
    PROTO_N ( ( pt, s ) )
-
 
1168
    PROTO_T ( TYPE *pt X TYPE s )
1219
sizeof_array(TYPE *pt, TYPE s)
1169
{
1220
{
1170
    TYPE t = *pt ;
1221
	TYPE t = *pt;
1171
    EXP a = NULL_exp ;
1222
	EXP a = NULL_exp;
1172
    while ( IS_type_array ( t ) ) {
1223
	while (IS_type_array(t)) {
1173
	EXP b ;
1224
		EXP b;
1174
	NAT n = DEREF_nat ( type_array_size ( t ) ) ;
1225
		NAT n = DEREF_nat(type_array_size(t));
1175
	if ( IS_NULL_nat ( n ) ) n = small_nat [0] ;
1226
		if (IS_NULL_nat(n)) {
-
 
1227
			n = small_nat[0];
-
 
1228
		}
1176
	b = calc_nat_value ( n, s ) ;
1229
		b = calc_nat_value(n, s);
1177
	a = make_dim_exp ( lex_star, a, b ) ;
1230
		a = make_dim_exp(lex_star, a, b);
1178
	t = DEREF_type ( type_array_sub ( t ) ) ;
1231
		t = DEREF_type(type_array_sub(t));
1179
    }
1232
	}
1180
    *pt = t ;
1233
	*pt = t;
1181
    return ( a ) ;
1234
	return (a);
1182
}
1235
}
1183
 
1236
 
1184
 
1237
 
1185
/*
1238
/*
1186
    FIND THE SIZE OF A TYPE
1239
    FIND THE SIZE OF A TYPE
1187
 
1240
 
1188
    This routine calculates the size of the type t when this can be precisely
1241
    This routine calculates the size of the type t when this can be precisely
1189
    evaluated, returning the null literal if this is not possible.
1242
    evaluated, returning the null literal if this is not possible.
1190
*/
1243
*/
1191
 
1244
 
1192
static NAT sizeof_type
1245
static NAT
1193
    PROTO_N ( ( t ) )
-
 
1194
    PROTO_T ( TYPE t )
1246
sizeof_type(TYPE t)
1195
{
1247
{
1196
    switch ( TAG_type ( t ) ) {
1248
	switch (TAG_type(t)) {
1197
	case type_integer_tag : {
1249
	case type_integer_tag: {
1198
	    /* Allow for integral types */
1250
		/* Allow for integral types */
1199
	    INT_TYPE it = DEREF_itype ( type_integer_rep ( t ) ) ;
1251
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1200
	    if ( IS_itype_basic ( it ) ) {
1252
		if (IS_itype_basic(it)) {
1201
		BASE_TYPE bt = DEREF_btype ( itype_basic_rep ( it ) ) ;
1253
			BASE_TYPE bt = DEREF_btype(itype_basic_rep(it));
1202
		if ( bt & btype_char ) {
1254
			if (bt & btype_char) {
1203
		    /* char has size one */
1255
				/* char has size one */
1204
		    NAT n = small_nat [1] ;
1256
				NAT n = small_nat[1];
1205
		    return ( n ) ;
1257
				return (n);
1206
		}
1258
			}
1207
	    }
1259
		}
1208
	    break ;
1260
		break;
1209
	}
1261
	}
1210
	case type_top_tag :
1262
	case type_top_tag:
1211
	case type_bottom_tag : {
1263
	case type_bottom_tag: {
1212
	    /* void has size one */
1264
		/* void has size one */
1213
	    NAT n = small_nat [1] ;
1265
		NAT n = small_nat[1];
1214
	    return ( n ) ;
1266
		return (n);
1215
	}
1267
	}
1216
	case type_array_tag : {
1268
	case type_array_tag: {
1217
	    /* Allow for array types */
1269
		/* Allow for array types */
1218
	    TYPE s = type_size_t ;
1270
		TYPE s = type_size_t;
1219
	    EXP a = sizeof_array ( &t, s ) ;
1271
		EXP a = sizeof_array(&t, s);
1220
	    NAT n = sizeof_type ( t ) ;
1272
		NAT n = sizeof_type(t);
1221
	    if ( !IS_NULL_nat ( n ) ) {
1273
		if (!IS_NULL_nat(n)) {
1222
		EXP b = calc_nat_value ( n, s ) ;
1274
			EXP b = calc_nat_value(n, s);
1223
		a = make_dim_exp ( lex_star, a, b ) ;
1275
			a = make_dim_exp(lex_star, a, b);
1224
		if ( IS_exp_int_lit ( a ) ) {
1276
			if (IS_exp_int_lit(a)) {
1225
		    n = DEREF_nat ( exp_int_lit_nat ( a ) ) ;
1277
				n = DEREF_nat(exp_int_lit_nat(a));
1226
		    return ( n ) ;
1278
				return (n);
1227
		}
1279
			}
1228
	    }
1280
		}
1229
	    break ;
1281
		break;
1230
	}
1282
	}
1231
	case type_enumerate_tag : {
1283
	case type_enumerate_tag: {
1232
	    /* An enumeration maps to its underlying type */
1284
		/* An enumeration maps to its underlying type */
1233
	    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
1285
		ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
1234
	    TYPE s = DEREF_type ( etype_rep ( et ) ) ;
1286
		TYPE s = DEREF_type(etype_rep(et));
1235
	    return ( sizeof_type ( s ) ) ;
1287
		return (sizeof_type(s));
1236
	}
1288
	}
1237
    }
1289
	}
1238
    return ( NULL_nat ) ;
1290
	return (NULL_nat);
1239
}
1291
}
1240
 
1292
 
1241
 
1293
 
1242
/*
1294
/*
1243
    CREATE A SIZEOF EXPRESSION
1295
    CREATE A SIZEOF EXPRESSION
1244
 
1296
 
1245
    This routine constructs the expression 'sizeof ( t )' without applying
1297
    This routine constructs the expression 'sizeof ( t )' without applying
1246
    any checks to t.
1298
    any checks to t.
1247
*/
1299
*/
1248
 
1300
 
1249
EXP sizeof_exp
1301
EXP
1250
    PROTO_N ( ( t ) )
-
 
1251
    PROTO_T ( TYPE t )
1302
sizeof_exp(TYPE t)
1252
{
1303
{
1253
    EXP e ;
1304
	EXP e;
1254
    NAT sz = sizeof_type ( t ) ;
1305
	NAT sz = sizeof_type(t);
1255
    if ( IS_NULL_nat ( sz ) ) {
1306
	if (IS_NULL_nat(sz)) {
1256
	/* Calculate size if it is not obvious */
1307
		/* Calculate size if it is not obvious */
1257
	OFFSET off ;
1308
		OFFSET off;
1258
	MAKE_off_type ( t, off ) ;
1309
		MAKE_off_type(t, off);
1259
	MAKE_exp_offset_size ( type_size_t, off, type_char, 1, e ) ;
1310
		MAKE_exp_offset_size(type_size_t, off, type_char, 1, e);
1260
	MAKE_nat_calc ( e, sz ) ;
1311
		MAKE_nat_calc(e, sz);
1261
    }
1312
	}
1262
    MAKE_exp_int_lit ( type_size_t, sz, exp_offset_size_tag, e ) ;
1313
	MAKE_exp_int_lit(type_size_t, sz, exp_offset_size_tag, e);
1263
    return ( e ) ;
1314
	return (e);
1264
}
1315
}
1265
 
1316
 
1266
 
1317
 
1267
/*
1318
/*
1268
    CONSTRUCT A SIZEOF EXPRESSION
1319
    CONSTRUCT A SIZEOF EXPRESSION
Line 1272... Line 1323...
1272
    except in the case where the result depends on a template parameter.
1323
    except in the case where the result depends on a template parameter.
1273
    The argument n gives the number of types defined in t.  Note that the
1324
    The argument n gives the number of types defined in t.  Note that the
1274
    result is a constant integer expression.
1325
    result is a constant integer expression.
1275
*/
1326
*/
1276
 
1327
 
1277
EXP make_sizeof_exp
1328
EXP
1278
    PROTO_N ( ( t, a, n, op ) )
-
 
1279
    PROTO_T ( TYPE t X EXP a X int n X int op )
1329
make_sizeof_exp(TYPE t, EXP a, int n, int op)
1280
{
1330
{
1281
    /* Deal with argument dependent case */
1331
	/* Deal with argument dependent case */
1282
#if LANGUAGE_CPP
1332
#if LANGUAGE_CPP
1283
    if ( !IS_NULL_exp ( a ) ) {
1333
	if (!IS_NULL_exp(a)) {
1284
	EXP e ;
1334
		EXP e;
1285
	NAT sz ;
1335
		NAT sz;
1286
	TYPE s = type_size_t ;
1336
		TYPE s = type_size_t;
1287
	MAKE_exp_op ( s, op, a, NULL_exp, e ) ;
1337
		MAKE_exp_op(s, op, a, NULL_exp, e);
1288
	MAKE_nat_calc ( e, sz ) ;
1338
		MAKE_nat_calc(e, sz);
1289
	MAKE_exp_int_lit ( s, sz, exp_op_tag, e ) ;
1339
		MAKE_exp_int_lit(s, sz, exp_op_tag, e);
1290
	return ( e ) ;
1340
		return (e);
1291
    }
1341
	}
1292
#else
1342
#else
1293
    UNUSED ( a ) ;
1343
	UNUSED(a);
1294
#endif
1344
#endif
1295
 
1345
 
1296
    /* Check on type */
1346
	/* Check on type */
1297
    switch ( TAG_type ( t ) ) {
1347
	switch (TAG_type(t)) {
1298
	case type_func_tag : {
1348
	case type_func_tag: {
1299
	    /* Can't have sizeof ( function ) */
1349
		/* Can't have sizeof (function) */
1300
	    report ( crt_loc, ERR_expr_sizeof_func ( op ) ) ;
1350
		report(crt_loc, ERR_expr_sizeof_func(op));
1301
	    MAKE_type_ptr ( cv_none, t, t ) ;
1351
		MAKE_type_ptr(cv_none, t, t);
1302
	    break ;
-
 
1303
	}
-
 
1304
	case type_bitfield_tag : {
-
 
1305
	    /* Can't have sizeof ( bitfield ) */
-
 
1306
	    report ( crt_loc, ERR_expr_sizeof_bitf ( op ) ) ;
-
 
1307
	    t = find_bitfield_type ( t ) ;
-
 
1308
	    break ;
1352
		break;
1309
	}
1353
	}
-
 
1354
	case type_bitfield_tag: {
-
 
1355
		/* Can't have sizeof (bitfield) */
-
 
1356
		report(crt_loc, ERR_expr_sizeof_bitf(op));
-
 
1357
		t = find_bitfield_type(t);
-
 
1358
		break;
-
 
1359
	}
1310
	case type_ref_tag : {
1360
	case type_ref_tag: {
1311
	    /* sizeof ( T & ) equals sizeof ( T ) */
1361
		/* sizeof (T &) equals sizeof (T) */
1312
	    t = DEREF_type ( type_ref_sub ( t ) ) ;
1362
		t = DEREF_type(type_ref_sub(t));
1313
	    break ;
1363
		break;
1314
	}
1364
	}
1315
	default : {
1365
	default : {
1316
	    /* Can't have sizeof ( incomplete ) */
1366
		/* Can't have sizeof (incomplete) */
1317
	    ERROR err = check_incomplete ( t ) ;
1367
		ERROR err = check_incomplete(t);
1318
	    if ( !IS_NULL_err ( err ) ) {
1368
		if (!IS_NULL_err(err)) {
1319
		err = concat_error ( err, ERR_expr_sizeof_incompl ( op ) ) ;
1369
			err = concat_error(err, ERR_expr_sizeof_incompl(op));
1320
		report ( crt_loc, err ) ;
1370
			report(crt_loc, err);
1321
	    }
1371
		}
1322
	    break ;
1372
		break;
1323
	}
1373
	}
1324
    }
1374
	}
1325
 
1375
 
1326
    /* Report on type definitions */
1376
	/* Report on type definitions */
-
 
1377
	if (n) {
1327
    if ( n ) report ( crt_loc, ERR_expr_sizeof_typedef ( op ) ) ;
1378
		report(crt_loc, ERR_expr_sizeof_typedef(op));
-
 
1379
	}
1328
 
1380
 
1329
    /* Calculate result */
1381
	/* Calculate result */
1330
    return ( sizeof_exp ( t ) ) ;
1382
	return (sizeof_exp(t));
1331
}
1383
}
1332
 
1384
 
1333
 
1385
 
1334
/*
1386
/*
1335
    FIND THE TYPE OF AN EXPRESSION
1387
    FIND THE TYPE OF AN EXPRESSION
1336
 
1388
 
1337
    This routine returns the type of the expression pointed to by pa after
1389
    This routine returns the type of the expression pointed to by pa after
1338
    apply reference conversions to it.  It is used, for example, to
1390
    apply reference conversions to it.  It is used, for example, to
1339
    transform 'sizeof ( a )' into 'sizeof ( t )'.  n gives the number of
1391
    transform 'sizeof ( a )' into 'sizeof ( t )'.  n gives the number of
1340
    side effects in pa.
1392
    side effects in pa.
1341
*/
1393
*/
1342
 
1394
 
1343
TYPE typeof_exp
1395
TYPE
1344
    PROTO_N ( ( pa, n, op ) )
-
 
1345
    PROTO_T ( EXP *pa X int n X int op )
1396
typeof_exp(EXP *pa, int n, int op)
1346
{
1397
{
1347
    TYPE t ;
1398
	TYPE t;
1348
    EXP a = *pa ;
1399
	EXP a = *pa;
-
 
1400
	if (n) {
1349
    if ( n ) report ( crt_loc, ERR_expr_sizeof_side ( op ) ) ;
1401
		report(crt_loc, ERR_expr_sizeof_side(op));
-
 
1402
	}
1350
    a = convert_reference ( a, REF_NORMAL ) ;
1403
	a = convert_reference(a, REF_NORMAL);
1351
    a = convert_none ( a ) ;
1404
	a = convert_none(a);
1352
    t = DEREF_type ( exp_type ( a ) ) ;
1405
	t = DEREF_type(exp_type(a));
1353
    if ( !is_templ_type ( t ) ) {
1406
	if (!is_templ_type(t)) {
1354
	/* Free operand in simple case */
1407
		/* Free operand in simple case */
1355
	free_exp ( a, 2 ) ;
1408
		free_exp(a, 2);
1356
	a = NULL_exp ;
1409
		a = NULL_exp;
1357
    }
1410
	}
1358
    *pa = a ;
1411
	*pa = a;
1359
    return ( t ) ;
1412
	return (t);
1360
}
1413
}
1361
 
1414
 
1362
 
1415
 
1363
/*
1416
/*
1364
    FIND THE NUMBER OF ITEMS IN AN INITIALISER EXPRESSION
1417
    FIND THE NUMBER OF ITEMS IN AN INITIALISER EXPRESSION
1365
 
1418
 
1366
    This routine returns the number of initialisers in the expression e
1419
    This routine returns the number of initialisers in the expression e
1367
    counting each array element separately.
1420
    counting each array element separately.
1368
*/
1421
*/
1369
 
1422
 
1370
EXP sizeof_init
1423
EXP
1371
    PROTO_N ( ( e, s ) )
-
 
1372
    PROTO_T ( EXP e X TYPE s )
1424
sizeof_init(EXP e, TYPE s)
1373
{
1425
{
1374
    EXP a = NULL_exp ;
1426
	EXP a = NULL_exp;
1375
    unsigned long v = 0 ;
1427
	unsigned long v = 0;
1376
    if ( !IS_NULL_exp ( e ) ) {
1428
	if (!IS_NULL_exp(e)) {
1377
	LIST ( EXP ) p, q ;
1429
		LIST(EXP)p, q;
1378
	if ( IS_exp_comma ( e ) ) {
1430
		if (IS_exp_comma(e)) {
1379
	    p = DEREF_list ( exp_comma_args ( e ) ) ;
1431
			p = DEREF_list(exp_comma_args(e));
1380
	    p = END_list ( p ) ;
1432
			p = END_list(p);
1381
	    e = DEREF_exp ( HEAD_list ( p ) ) ;
1433
			e = DEREF_exp(HEAD_list(p));
-
 
1434
		}
-
 
1435
		if (IS_exp_initialiser(e)) {
-
 
1436
			p = DEREF_list(exp_initialiser_args(e));
-
 
1437
			q = NULL_list(EXP);
-
 
1438
		} else {
-
 
1439
			CONS_exp(e, NULL_list(EXP), p);
-
 
1440
			q = p;
-
 
1441
		}
-
 
1442
		while (!IS_NULL_list(p)) {
-
 
1443
			EXP b = DEREF_exp(HEAD_list(p));
-
 
1444
			if (!IS_NULL_exp(b)) {
-
 
1445
				TYPE t = DEREF_type(exp_type(b));
-
 
1446
				if (IS_type_array(t)) {
-
 
1447
					/* Multiply up array bounds */
-
 
1448
					EXP c = sizeof_array(&t, s);
-
 
1449
					a = make_dim_exp(lex_plus, a, c);
-
 
1450
				} else {
-
 
1451
					/* Other types count once */
-
 
1452
					v++;
-
 
1453
				}
-
 
1454
			}
-
 
1455
			p = TAIL_list(p);
-
 
1456
		}
-
 
1457
		if (!IS_NULL_list(q)) {
-
 
1458
			DESTROY_list(q, SIZE_exp);
-
 
1459
		}
1382
	}
1460
	}
1383
	if ( IS_exp_initialiser ( e ) ) {
1461
	if (IS_NULL_exp(a)) {
1384
	    p = DEREF_list ( exp_initialiser_args ( e ) ) ;
1462
		NAT n = make_nat_value(v);
1385
	    q = NULL_list ( EXP ) ;
1463
		a = calc_nat_value(n, s);
1386
	} else {
1464
	} else {
1387
	    CONS_exp ( e, NULL_list ( EXP ), p ) ;
-
 
1388
	    q = p ;
1465
		if (v) {
1389
	}
-
 
1390
	while ( !IS_NULL_list ( p ) ) {
-
 
1391
	    EXP b = DEREF_exp ( HEAD_list ( p ) ) ;
-
 
1392
	    if ( !IS_NULL_exp ( b ) ) {
-
 
1393
		TYPE t = DEREF_type ( exp_type ( b ) ) ;
-
 
1394
		if ( IS_type_array ( t ) ) {
1466
			NAT n = make_nat_value(v);
1395
		    /* Multiply up array bounds */
-
 
1396
		    EXP c = sizeof_array ( &t, s ) ;
1467
			EXP c = calc_nat_value(n, s);
1397
		    a = make_dim_exp ( lex_plus, a, c ) ;
1468
			a = make_dim_exp(lex_plus, a, c);
1398
		} else {
-
 
1399
		    /* Other types count once */
-
 
1400
		    v++ ;
-
 
1401
		}
1469
		}
1402
	    }
-
 
1403
	    p = TAIL_list ( p ) ;
-
 
1404
	}
-
 
1405
	if ( !IS_NULL_list ( q ) ) DESTROY_list ( q, SIZE_exp ) ;
-
 
1406
    }
-
 
1407
    if ( IS_NULL_exp ( a ) ) {
-
 
1408
	NAT n = make_nat_value ( v ) ;
-
 
1409
	a = calc_nat_value ( n, s ) ;
-
 
1410
    } else {
-
 
1411
	if ( v ) {
-
 
1412
	    NAT n = make_nat_value ( v ) ;
-
 
1413
	    EXP c = calc_nat_value ( n, s ) ;
-
 
1414
	    a = make_dim_exp ( lex_plus, a, c ) ;
-
 
1415
	}
1470
	}
1416
    }
-
 
1417
    return ( a ) ;
1471
	return (a);
1418
}
1472
}