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 – /branches/tendra5/src/producers/common/parse/literal.c – Rev 5 and 6

Subversion Repositories tendra.SVN

Rev

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

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2005 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, 1998
32
    		 Crown Copyright (c) 1997, 1998
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 84... Line 114...
84
    to be rewritten.  The valid digits are 0-9, A-Z (which evaluate to
114
    to be rewritten.  The valid digits are 0-9, A-Z (which evaluate to
85
    10-35) and a-z (which evaluate to 10-35).  Invalid digits are
115
    10-35) and a-z (which evaluate to 10-35).  Invalid digits are
86
    indicated by NONE.
116
    indicated by NONE.
87
*/
117
*/
88
 
118
 
89
unsigned char digit_values [ NO_CHAR + 1 ] = {
119
unsigned char digit_values[NO_CHAR + 1] = {
90
#define CHAR_DATA( A, B, C, D )	( B ),
120
#define CHAR_DATA(A, B, C, D)	(B),
91
#include "char.h"
121
#include "char.h"
92
#undef CHAR_DATA
122
#undef CHAR_DATA
93
    NONE		/* dummy */
123
	NONE		/* dummy */
94
} ;
124
};
95
 
125
 
96
 
126
 
97
/*
127
/*
98
    TABLE OF ESCAPE SEQUENCES
128
    TABLE OF ESCAPE SEQUENCES
99
 
129
 
Line 104... Line 134...
104
    indicated by OCTE, hexadecimal escape sequences by HEXE, universal
134
    indicated by OCTE, hexadecimal escape sequences by HEXE, universal
105
    character names by UNI4 or UNI8, and illegal escape sequences by
135
    character names by UNI4 or UNI8, and illegal escape sequences by
106
    NONE.
136
    NONE.
107
*/
137
*/
108
 
138
 
109
unsigned char escape_sequences [ NO_CHAR + 1 ] = {
139
unsigned char escape_sequences[NO_CHAR + 1] = {
110
#define CHAR_DATA( A, B, C, D )	( C ),
140
#define CHAR_DATA(A, B, C, D)	(C),
111
#include "char.h"
141
#include "char.h"
112
#undef CHAR_DATA
142
#undef CHAR_DATA
113
    NONE		/* dummy */
143
	NONE		/* dummy */
114
} ;
144
};
115
 
145
 
116
 
146
 
117
/*
147
/*
118
    SET AN ESCAPE SEQUENCE
148
    SET AN ESCAPE SEQUENCE
119
 
149
 
120
    This routine sets the character escape value for the character
150
    This routine sets the character escape value for the character
121
    literal expression a to be the character literal b, or an illegal
151
    literal expression a to be the character literal b, or an illegal
122
    escape if b is the null expression.
152
    escape if b is the null expression.
123
*/
153
*/
124
 
154
 
125
void set_escape
155
void
126
    PROTO_N ( ( a, b ) )
-
 
127
    PROTO_T ( EXP a X EXP b )
156
set_escape(EXP a, EXP b)
128
{
157
{
129
    int c = get_char_value ( a ) ;
158
	int c = get_char_value(a);
130
    int e = NONE ;
159
	int e = NONE;
131
    if ( !IS_NULL_exp ( b ) ) {
160
	if (!IS_NULL_exp(b)) {
132
	e = get_char_value ( b ) ;
161
		e = get_char_value(b);
133
	if ( e == char_illegal ) e = NONE ;
162
		if (e == char_illegal) {
-
 
163
			e = NONE;
134
    }
164
		}
-
 
165
	}
135
    if ( c >= 0 && c < NO_CHAR ) {
166
	if (c >= 0 && c < NO_CHAR) {
136
	escape_sequences [c] = ( unsigned char ) e ;
167
		escape_sequences[c] = (unsigned char)e;
137
    }
168
	}
138
    return ;
169
	return;
139
}
170
}
140
 
171
 
141
 
172
 
142
/*
173
/*
143
    CHECK A STRING OF DIGITS
174
    CHECK A STRING OF DIGITS
Line 145... Line 176...
145
    This routine scans the string s for valid digits for the given base.
176
    This routine scans the string s for valid digits for the given base.
146
    It returns a pointer to the first character which is not a valid
177
    It returns a pointer to the first character which is not a valid
147
    digit.
178
    digit.
148
*/
179
*/
149
 
180
 
150
static string check_digits
181
static string
151
    PROTO_N ( ( s, base ) )
-
 
152
    PROTO_T ( string s X unsigned base )
182
check_digits(string s, unsigned base)
153
{
183
{
154
    unsigned b ;
184
	unsigned b;
155
    character c ;
185
	character c;
156
    while ( c = *s, c != 0 ) {
186
	while (c = *s, c != 0) {
157
#if FS_EXTENDED_CHAR
187
#if FS_EXTENDED_CHAR
158
	if ( IS_EXTENDED ( c ) ) break ;
188
		if (IS_EXTENDED(c)) {
-
 
189
			break;
-
 
190
		}
159
#endif
191
#endif
160
	b = ( unsigned ) digit_values [c] ; ;
192
		b = (unsigned)digit_values[c];;
161
	if ( b >= base ) break ;
193
		if (b >= base) {
-
 
194
			break;
-
 
195
		}
162
	s++ ;
196
		s++;
163
    }
197
	}
164
    return ( s ) ;
198
	return(s);
165
}
199
}
166
 
200
 
167
 
201
 
168
/*
202
/*
169
    EVALUATE A STRING OF DIGITS
203
    EVALUATE A STRING OF DIGITS
170
 
204
 
171
    This routine evaluates the string of digits starting with s and
205
    This routine evaluates the string of digits starting with s and
172
    ending with t using the given base (which will be at most 16).  It
206
    ending with t using the given base (which will be at most 16).  It
173
    is assumed that all of these digits are in the correct range.
207
    is assumed that all of these digits are in the correct range.
174
*/
208
*/
175
 
209
 
176
static NAT eval_digits
210
static NAT
177
    PROTO_N ( ( s, t, base ) )
-
 
178
    PROTO_T ( string s X string t X unsigned base )
211
eval_digits(string s, string t, unsigned base)
179
{
212
{
180
    NAT n ;
213
	NAT n;
181
    int m = 0 ;
214
	int m = 0;
182
    string r = s ;
215
	string r = s;
183
    unsigned long v = 0 ;
216
	unsigned long v = 0;
184
    unsigned long b = ( unsigned long ) base ;
217
	unsigned long b = (unsigned long)base;
185
    while ( r != t && m < 8 ) {
218
	while (r != t && m < 8) {
186
	/* Evaluate first few digits */
219
		/* Evaluate first few digits */
187
	unsigned long d = ( unsigned long ) digit_values [ *r ] ;
220
		unsigned long d = (unsigned long)digit_values[*r];
188
	v = b * v + d ;
221
		v = b * v + d;
189
	m++ ;
222
		m++;
190
	r++ ;
223
		r++;
191
    }
224
	}
192
    n = make_nat_value ( v ) ;
225
	n = make_nat_value(v);
193
    while ( r != t ) {
226
	while (r != t) {
194
	/* Evaluate further digits */
227
		/* Evaluate further digits */
195
	unsigned d = ( unsigned ) digit_values [ *r ] ;
228
		unsigned d = (unsigned)digit_values[*r];
196
	n = make_nat_literal ( n, base, d ) ;
229
		n = make_nat_literal(n, base, d);
197
	r++ ;
230
		r++;
198
    }
231
	}
199
    return ( n ) ;
232
	return(n);
200
}
233
}
201
 
234
 
202
 
235
 
203
/*
236
/*
204
    EVALUATE A STRING OF DIGITS
237
    EVALUATE A STRING OF DIGITS
205
 
238
 
206
    This routine is the same as eval_digits except that it assumes that
239
    This routine is the same as eval_digits except that it assumes that
207
    the result fits inside an unsigned long, and reports an error
240
    the result fits inside an unsigned long, and reports an error
208
    otherwise.
241
    otherwise.
209
*/
242
*/
210
 
243
 
211
static unsigned long eval_char_digits
244
static unsigned long
212
    PROTO_N ( ( s, t, base ) )
-
 
213
    PROTO_T ( string s X string t X unsigned base )
245
eval_char_digits(string s, string t, unsigned base)
214
{
246
{
215
    string r ;
247
	string r;
216
    int overflow = 0 ;
248
	int overflow = 0;
217
    unsigned long n = 0 ;
249
	unsigned long n = 0;
218
    unsigned long b = ( unsigned long ) base ;
250
	unsigned long b = (unsigned long)base;
219
    for ( r = s ; r != t ; r++ ) {
251
	for (r = s; r != t; r++) {
220
	unsigned long m = n ;
252
		unsigned long m = n;
221
	n = b * n + ( unsigned long ) digit_values [ *r ] ;
253
		n = b * n + (unsigned long)digit_values[*r];
-
 
254
		if (n < m) {
222
	if ( n < m ) overflow = 1 ;
255
			overflow = 1;
223
    }
256
		}
-
 
257
	}
-
 
258
	if (overflow) {
224
    if ( overflow ) report ( crt_loc, ERR_lex_ccon_large () ) ;
259
		report(crt_loc, ERR_lex_ccon_large());
-
 
260
	}
225
    return ( n ) ;
261
	return(n);
226
}
262
}
227
 
263
 
228
 
264
 
229
/*
265
/*
230
    EVALUATE A LINE NUMBER
266
    EVALUATE A LINE NUMBER
Line 234... Line 270...
234
    arising are indicated using err.  This is a bit pattern consisting
270
    arising are indicated using err.  This is a bit pattern consisting
235
    of 2 if s is not a simple string of decimal digits, and 1 if its
271
    of 2 if s is not a simple string of decimal digits, and 1 if its
236
    value exceeds 32767.
272
    value exceeds 32767.
237
*/
273
*/
238
 
274
 
239
unsigned long eval_line_digits
275
unsigned long
240
    PROTO_N ( ( s, err ) )
-
 
241
    PROTO_T ( string s X unsigned *err )
276
eval_line_digits(string s, unsigned *err)
242
{
277
{
243
    string r ;
278
	string r;
244
    unsigned e = 0 ;
279
	unsigned e = 0;
245
    unsigned long n = 0 ;
280
	unsigned long n = 0;
246
    string t = check_digits ( s, ( unsigned ) 10 ) ;
281
	string t = check_digits(s, (unsigned)10);
247
    if ( *t ) e = 2 ;
282
	if (*t) {
-
 
283
		e = 2;
-
 
284
	}
248
    for ( r = s ; r != t ; r++ ) {
285
	for (r = s; r != t; r++) {
249
	n = 10 * n + ( unsigned long ) digit_values [ *r ] ;
286
		n = 10 * n + (unsigned long)digit_values[*r];
250
	if ( n > 0x7fff ) e |= 1 ;
287
		if (n > 0x7fff) {
-
 
288
			e |= 1;
251
    }
289
		}
-
 
290
	}
252
    *err = e ;
291
	*err = e;
253
    return ( n ) ;
292
	return(n);
254
}
293
}
255
 
294
 
256
 
295
 
257
/*
296
/*
258
    STRING HASH TABLE
297
    STRING HASH TABLE
259
 
298
 
260
    This variable gives the hash table used in shared string literals.
299
    This variable gives the hash table used in shared string literals.
261
*/
300
*/
262
 
301
 
263
static STRING *string_hash_table = NULL ;
302
static STRING *string_hash_table = NULL;
264
#define HASH_STRING_SIZE	( ( unsigned long ) 256 )
303
#define HASH_STRING_SIZE	((unsigned long)256)
265
 
304
 
266
 
305
 
267
/*
306
/*
268
    STRING AND CHARACTER LITERAL TYPES
307
    STRING AND CHARACTER LITERAL TYPES
269
 
308
 
270
    The type of a simple character literal is char in C++, but int in C.
309
    The type of a simple character literal is char in C++, but int in C.
271
    The variable type_char_lit is used to hold the appropriate result
310
    The variable type_char_lit is used to hold the appropriate result
272
    type.  Other string and character literals have fixed types, however
311
    type.  Other string and character literals have fixed types, however
273
    for convenience variables are used to identify them.
312
    for convenience variables are used to identify them.
274
*/
313
*/
275
 
314
 
276
static TYPE type_char_lit ;
315
static TYPE type_char_lit;
277
static TYPE type_mchar_lit ;
316
static TYPE type_mchar_lit;
278
static TYPE type_wchar_lit ;
317
static TYPE type_wchar_lit;
279
static TYPE type_string_lit ;
318
static TYPE type_string_lit;
280
static TYPE type_wstring_lit ;
319
static TYPE type_wstring_lit;
281
CV_SPEC cv_string = cv_none ;
320
CV_SPEC cv_string = cv_none;
282
 
321
 
283
 
322
 
284
/*
323
/*
285
    SET THE CHARACTER LITERAL TYPE
324
    SET THE CHARACTER LITERAL TYPE
286
 
325
 
287
    This routine sets the type of a character literal to be t.  t must be
326
    This routine sets the type of a character literal to be t.  t must be
288
    an integral type.  Note that only the representation type is set to t,
327
    an integral type.  Note that only the representation type is set to t,
289
    the semantic type is always char.
328
    the semantic type is always char.
290
*/
329
*/
291
 
330
 
292
void set_char_lit
331
void
293
    PROTO_N ( ( t ) )
-
 
294
    PROTO_T ( TYPE t )
332
set_char_lit(TYPE t)
295
{
333
{
296
    if ( IS_type_integer ( t ) ) {
334
	if (IS_type_integer(t)) {
297
	INT_TYPE r = DEREF_itype ( type_integer_rep ( t ) ) ;
335
		INT_TYPE r = DEREF_itype(type_integer_rep(t));
298
	INT_TYPE s = DEREF_itype ( type_integer_rep ( type_char ) ) ;
336
		INT_TYPE s = DEREF_itype(type_integer_rep(type_char));
299
	type_char_lit = make_itype ( r, s ) ;
337
		type_char_lit = make_itype(r, s);
300
    } else {
338
	} else {
301
	report ( preproc_loc, ERR_pragma_char_lit ( t ) ) ;
339
		report(preproc_loc, ERR_pragma_char_lit(t));
302
    }
340
	}
303
    return ;
341
	return;
304
}
342
}
305
 
343
 
306
 
344
 
307
/*
345
/*
308
    TABLE OF INTEGER LITERAL SPECIFICATIONS
346
    TABLE OF INTEGER LITERAL SPECIFICATIONS
Line 311... Line 349...
311
    literal type specification.  The table int_lit_spec holds the
349
    literal type specification.  The table int_lit_spec holds the
312
    specifications for the various combinations of base and suffix.
350
    specifications for the various combinations of base and suffix.
313
*/
351
*/
314
 
352
 
315
typedef struct lit_info_tag {
353
typedef struct lit_info_tag {
316
    int tag ;
354
	int tag;
317
    TYPE type ;
355
	TYPE type;
318
    NAT bound ;
356
	NAT bound;
319
    IDENTIFIER tok ;
357
	IDENTIFIER tok;
320
    int tok_no ;
358
	int tok_no;
321
    int opt ;
359
	int opt;
322
    struct lit_info_tag *next ;
360
	struct lit_info_tag *next;
323
} LITERAL_INFO ;
361
} LITERAL_INFO;
324
 
362
 
325
static LITERAL_INFO *int_lit_spec [ BASE_NO ] [ SUFFIX_NO ] = {
363
static LITERAL_INFO *int_lit_spec[BASE_NO][SUFFIX_NO] = {
326
    { NULL, NULL, NULL, NULL, NULL, NULL },
364
	{ NULL, NULL, NULL, NULL, NULL, NULL },
327
    { NULL, NULL, NULL, NULL, NULL, NULL },
365
	{ NULL, NULL, NULL, NULL, NULL, NULL },
328
    { NULL, NULL, NULL, NULL, NULL, NULL }
366
	{ NULL, NULL, NULL, NULL, NULL, NULL }
329
} ;
367
};
330
 
368
 
331
static LITERAL_INFO *crt_int_lit = NULL ;
369
static LITERAL_INFO *crt_int_lit = NULL;
332
static LITERAL_INFO **ptr_int_lit = NULL ;
370
static LITERAL_INFO **ptr_int_lit = NULL;
333
 
371
 
334
 
372
 
335
/*
373
/*
336
    TABLE OF BUILT-IN INTEGER LITERAL SPECIFICATIONS
374
    TABLE OF BUILT-IN INTEGER LITERAL SPECIFICATIONS
337
 
375
 
338
    This table gives the possible types and built-in tokens for the
376
    This table gives the possible types and built-in tokens for the
339
    various base and suffix combinations.
377
    various base and suffix combinations.
340
*/
378
*/
341
 
379
 
342
static struct {
380
static struct {
343
    unsigned char type [6] ;
381
	unsigned char type[6];
344
    int tok ;
382
	int tok;
345
    LIST ( TYPE ) cases ;
383
	LIST(TYPE)cases;
346
} int_lit_tok [ BASE_NO ] [ SUFFIX_NO ] = {
384
} int_lit_tok[BASE_NO][SUFFIX_NO] = {
347
    {
385
	{
348
	{ { 2, 0, 2, 2, 1, 1 }, TOK_lit_int, NULL_list ( TYPE ) },
386
		{ { 2, 0, 2, 2, 1, 1 }, TOK_lit_int, NULL_list(TYPE)},
349
	{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list ( TYPE ) },
387
		{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list(TYPE)},
350
	{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list ( TYPE ) },
388
		{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list(TYPE)},
351
	{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list ( TYPE ) },
389
		{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list(TYPE)},
352
	{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list ( TYPE ) },
390
		{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list(TYPE)},
353
	{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list ( TYPE ) }
391
		{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list(TYPE)}
354
    },
392
	},
355
    {
393
	{
356
	{ { 2, 2, 2, 2, 1, 1 }, TOK_lit_hex, NULL_list ( TYPE ) },
394
		{ { 2, 2, 2, 2, 1, 1 }, TOK_lit_hex, NULL_list(TYPE)},
357
	{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list ( TYPE ) },
395
		{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list(TYPE)},
358
	{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list ( TYPE ) },
396
		{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list(TYPE)},
359
	{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list ( TYPE ) },
397
		{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list(TYPE)},
360
	{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list ( TYPE ) },
398
		{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list(TYPE)},
361
	{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list ( TYPE ) }
399
		{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list(TYPE)}
362
    },
400
	},
363
    {
401
	{
364
	{ { 2, 2, 2, 2, 1, 1 }, TOK_lit_hex, NULL_list ( TYPE ) },
402
		{ { 2, 2, 2, 2, 1, 1 }, TOK_lit_hex, NULL_list(TYPE)},
365
	{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list ( TYPE ) },
403
		{ { 0, 2, 0, 2, 0, 1 }, TOK_lit_unsigned, NULL_list(TYPE)},
366
	{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list ( TYPE ) },
404
		{ { 0, 0, 2, 2, 1, 1 }, TOK_lit_long, NULL_list(TYPE)},
367
	{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list ( TYPE ) },
405
		{ { 0, 0, 0, 2, 0, 1 }, TOK_lit_ulong, NULL_list(TYPE)},
368
	{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list ( TYPE ) },
406
		{ { 0, 0, 0, 0, 2, 2 }, TOK_lit_llong, NULL_list(TYPE)},
369
	{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list ( TYPE ) }
407
		{ { 0, 0, 0, 0, 0, 2 }, TOK_lit_ullong, NULL_list(TYPE)}
370
    }
408
	}
371
} ;
409
};
372
 
410
 
373
 
411
 
374
/*
412
/*
375
    INITIALISE TABLE OF INTEGER LITERAL TYPES
413
    INITIALISE TABLE OF INTEGER LITERAL TYPES
376
 
414
 
377
    This routine initialises the string and character literal types and
415
    This routine initialises the string and character literal types and
378
    the table int_lit_info.  The initial values for the table are given
416
    the table int_lit_info.  The initial values for the table are given
379
    by the following lists of types:
417
    by the following lists of types:
380
 
418
 
381
	decimal:	( int, long, unsigned long ),
419
	decimal:	( int, long, unsigned long ),
Line 404... Line 442...
404
 
442
 
405
    Variants are that characters have type int in C and that string
443
    Variants are that characters have type int in C and that string
406
    literals are not const in pre-ISO C++ and C.
444
    literals are not const in pre-ISO C++ and C.
407
*/
445
*/
408
 
446
 
409
void init_literal
447
void
410
    PROTO_Z ()
448
init_literal(void)
411
{
449
{
412
    int b, s ;
450
	int b, s;
413
    BUILTIN_TYPE n ;
451
	BUILTIN_TYPE n;
414
    OPTION opt = option ( OPT_int_overflow ) ;
452
	OPTION opt = option(OPT_int_overflow);
415
    ASSERT ( !IS_NULL_type ( type_char ) ) ;
453
	ASSERT(!IS_NULL_type(type_char));
416
 
454
 
417
    /* String and character literal types */
455
	/* String and character literal types */
418
    type_mchar_lit = type_sint ;
456
	type_mchar_lit = type_sint;
419
    type_wchar_lit = type_wchar_t ;
457
	type_wchar_lit = type_wchar_t;
420
    type_string_lit = type_char ;
458
	type_string_lit = type_char;
421
    type_wstring_lit = type_wchar_t ;
459
	type_wstring_lit = type_wchar_t;
422
#if LANGUAGE_CPP
460
#if LANGUAGE_CPP
423
    set_char_lit ( type_char ) ;
461
	set_char_lit(type_char);
424
    set_string_qual ( cv_const ) ;
462
	set_string_qual(cv_const);
425
#else
463
#else
426
    set_char_lit ( type_sint ) ;
464
	set_char_lit(type_sint);
427
    set_string_qual ( cv_none ) ;
465
	set_string_qual(cv_none);
428
#endif
466
#endif
429
 
467
 
430
    /* Set up type lists */
468
	/* Set up type lists */
431
    for ( b = 0 ; b < BASE_NO ; b++ ) {
469
	for (b = 0; b < BASE_NO; b++) {
432
	for ( s = 0 ; s < SUFFIX_NO ; s++ ) {
470
		for (s = 0; s < SUFFIX_NO; s++) {
433
	    LIST ( TYPE ) p = NULL_list ( TYPE ) ;
471
			LIST(TYPE)p = NULL_list(TYPE);
434
	    begin_literal ( b, s ) ;
472
			begin_literal(b, s);
435
	    for ( n = 0 ; n < 6 ; n++ ) {
473
			for (n = 0; n < 6; n++) {
436
		if ( int_lit_tok [b] [s].type [n] == 2 ) {
474
				if (int_lit_tok[b][s].type[n] == 2) {
437
		    TYPE t = type_builtin [ ntype_sint + n ] ;
475
					TYPE t = type_builtin[ntype_sint + n];
438
		    add_range_literal ( NULL_exp, 1 ) ;
476
					add_range_literal(NULL_exp, 1);
439
		    add_type_literal ( t ) ;
477
					add_type_literal(t);
440
		    CONS_type ( t, p, p ) ;
478
					CONS_type(t, p, p);
-
 
479
				}
-
 
480
			}
-
 
481
			add_range_literal(NULL_exp, 0);
-
 
482
			add_token_literal(NULL_id, (unsigned)opt);
-
 
483
			p = REVERSE_list(p);
-
 
484
			int_lit_tok[b][s].cases = uniq_type_set(p);
-
 
485
		}
-
 
486
	}
-
 
487
 
-
 
488
	/* Set up string hash table */
-
 
489
	if (string_hash_table == NULL) {
-
 
490
		unsigned long i;
-
 
491
		STRING *q = xmalloc_nof(STRING, HASH_STRING_SIZE);
-
 
492
		for (i = 0; i < HASH_STRING_SIZE; i++) {
-
 
493
			q[i] = NULL_str;
441
		}
494
		}
442
	    }
-
 
443
	    add_range_literal ( NULL_exp, 0 ) ;
-
 
444
	    add_token_literal ( NULL_id, ( unsigned ) opt ) ;
-
 
445
	    p = REVERSE_list ( p ) ;
495
		string_hash_table = q;
446
	    int_lit_tok [b] [s].cases = uniq_type_set ( p ) ;
-
 
447
	}
496
	}
448
    }
-
 
449
 
-
 
450
    /* Set up string hash table */
-
 
451
    if ( string_hash_table == NULL ) {
-
 
452
	unsigned long i ;
-
 
453
	STRING *q = xmalloc_nof ( STRING, HASH_STRING_SIZE ) ;
-
 
454
	for ( i = 0 ; i < HASH_STRING_SIZE ; i++ ) q [i] = NULL_str ;
-
 
455
	string_hash_table = q ;
-
 
456
    }
-
 
457
    return ;
497
	return;
458
}
498
}
459
 
499
 
460
 
500
 
461
/*
501
/*
462
    SET THE CV-QUALIFIERS FOR A STRING LITERAL
502
    SET THE CV-QUALIFIERS FOR A STRING LITERAL
463
 
503
 
464
    This routine sets the string and wide string literal types to be
504
    This routine sets the string and wide string literal types to be
465
    cv-qualified.
505
    cv-qualified.
466
*/
506
*/
467
 
507
 
468
void set_string_qual
508
void
469
    PROTO_N ( ( cv ) )
-
 
470
    PROTO_T ( CV_SPEC cv )
509
set_string_qual(CV_SPEC cv)
471
{
510
{
472
    type_string_lit = qualify_type ( type_string_lit, cv, 0 ) ;
511
	type_string_lit = qualify_type(type_string_lit, cv, 0);
473
    type_wstring_lit = qualify_type ( type_wstring_lit, cv, 0 ) ;
512
	type_wstring_lit = qualify_type(type_wstring_lit, cv, 0);
474
    cv_string = cv ;
513
	cv_string = cv;
475
    return ;
514
	return;
476
}
515
}
477
 
516
 
478
 
517
 
479
/*
518
/*
480
    BEGIN A LITERAL SPECIFICATION DEFINITION
519
    BEGIN A LITERAL SPECIFICATION DEFINITION
481
 
520
 
482
    This routine is called to begin the specification of the integer
521
    This routine is called to begin the specification of the integer
483
    literals of the given base and suffix.
522
    literals of the given base and suffix.
484
*/
523
*/
485
 
524
 
486
void begin_literal
525
void
487
    PROTO_N ( ( base, suff ) )
-
 
488
    PROTO_T ( int base X int suff )
526
begin_literal(int base, int suff)
489
{
527
{
490
    LITERAL_INFO **p = &( int_lit_spec [ base ] [ suff ] ) ;
528
	LITERAL_INFO **p = &(int_lit_spec[base][suff]);
491
    *p = NULL ;
529
	*p = NULL;
492
    ptr_int_lit = p ;
530
	ptr_int_lit = p;
493
    crt_int_lit = NULL ;
531
	crt_int_lit = NULL;
494
    return ;
532
	return;
495
}
533
}
496
 
534
 
497
 
535
 
498
/*
536
/*
499
    ADD A BOUND TO A LITERAL SPECIFICATION
537
    ADD A BOUND TO A LITERAL SPECIFICATION
Line 503... Line 541...
503
    is 1 then the bound matches all the values in the following type,
541
    is 1 then the bound matches all the values in the following type,
504
    and if it is 2 then the bound matches all values less than or equal
542
    and if it is 2 then the bound matches all values less than or equal
505
    to the integer literal expression e.
543
    to the integer literal expression e.
506
*/
544
*/
507
 
545
 
508
void add_range_literal
546
void
509
    PROTO_N ( ( e, n ) )
-
 
510
    PROTO_T ( EXP e X int n )
547
add_range_literal(EXP e, int n)
511
{
548
{
512
    LITERAL_INFO *p = xmalloc_one ( LITERAL_INFO ) ;
549
	LITERAL_INFO *p = xmalloc_one(LITERAL_INFO);
513
    p->tag = n ;
550
	p->tag = n;
514
    if ( !IS_NULL_exp ( e ) && IS_exp_int_lit ( e ) ) {
551
	if (!IS_NULL_exp(e) && IS_exp_int_lit(e)) {
515
	p->bound = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
552
		p->bound = DEREF_nat(exp_int_lit_nat(e));
516
    } else {
553
	} else {
517
	p->bound = small_nat [0] ;
554
		p->bound = small_nat[0];
518
    }
555
	}
519
    p->type = NULL_type ;
556
	p->type = NULL_type;
520
    p->tok = NULL_id ;
557
	p->tok = NULL_id;
521
    p->tok_no = -1 ;
558
	p->tok_no = -1;
522
    p->opt = OPT_none ;
559
	p->opt = OPT_none;
523
    p->next = NULL ;
560
	p->next = NULL;
524
    *ptr_int_lit = p ;
561
	*ptr_int_lit = p;
525
    crt_int_lit = p ;
562
	crt_int_lit = p;
526
    ptr_int_lit = &( p->next ) ;
563
	ptr_int_lit = & (p->next);
527
    return ;
564
	return;
528
}
565
}
529
 
566
 
530
 
567
 
531
/*
568
/*
532
    ADD A TYPE TO A LITERAL SPECIFICATION
569
    ADD A TYPE TO A LITERAL SPECIFICATION
533
 
570
 
534
    This routine specifies the type t for all values under the current
571
    This routine specifies the type t for all values under the current
535
    bound in the current literal specification.
572
    bound in the current literal specification.
536
*/
573
*/
537
 
574
 
538
void add_type_literal
575
void
539
    PROTO_N ( ( t ) )
-
 
540
    PROTO_T ( TYPE t )
576
add_type_literal(TYPE t)
541
{
577
{
542
    NAT n ;
578
	NAT n;
543
    LITERAL_INFO *p = crt_int_lit ;
579
	LITERAL_INFO *p = crt_int_lit;
544
    if ( IS_type_integer ( t ) ) {
580
	if (IS_type_integer(t)) {
545
	if ( !is_arg_promote ( t ) ) {
581
		if (!is_arg_promote(t)) {
546
	    /* Type should promote to itself */
582
			/* Type should promote to itself */
547
	    report ( preproc_loc, ERR_pragma_lit_type ( t ) ) ;
583
			report(preproc_loc, ERR_pragma_lit_type(t));
548
	    t = promote_type ( t ) ;
584
			t = promote_type(t);
549
	}
585
		}
550
    } else {
586
	} else {
551
	/* Type should be integral */
587
		/* Type should be integral */
552
	if ( !IS_type_error ( t ) ) {
588
		if (!IS_type_error(t)) {
553
	    report ( preproc_loc, ERR_pragma_lit_type ( t ) ) ;
589
			report(preproc_loc, ERR_pragma_lit_type(t));
554
	}
590
		}
555
	t = type_ulong ;
591
		t = type_ulong;
556
    }
592
	}
557
    p->type = qualify_type ( t, cv_none, 0 ) ;
593
	p->type = qualify_type(t, cv_none, 0);
558
    n = p->bound ;
594
	n = p->bound;
559
    if ( p->tag == 2 ) {
595
	if (p->tag == 2) {
560
	if ( check_nat_range ( t, n ) != 0 ) {
596
		if (check_nat_range(t, n)!= 0) {
561
	    /* Given bound should fit into type */
597
			/* Given bound should fit into type */
562
	    report ( preproc_loc, ERR_pragma_lit_range ( n, t ) ) ;
598
			report(preproc_loc, ERR_pragma_lit_range(n, t));
563
	    n = max_type_value ( t, 0 ) ;
599
			n = max_type_value(t, 0);
-
 
600
		}
-
 
601
	} else {
-
 
602
		n = max_type_value(t, 0);
564
	}
603
	}
565
    } else {
-
 
566
	n = max_type_value ( t, 0 ) ;
-
 
567
    }
-
 
568
    p->bound = n ;
604
	p->bound = n;
569
    return ;
605
	return;
570
}
606
}
571
 
607
 
572
 
608
 
573
/*
609
/*
574
    ADD A TOKEN TO A LITERAL SPECIFICATION
610
    ADD A TOKEN TO A LITERAL SPECIFICATION
575
 
611
 
576
    This routine specifies that the token id should be used to calculate
612
    This routine specifies that the token id should be used to calculate
577
    the type for all values under the current bound in the current
613
    the type for all values under the current bound in the current
578
    literal specification.  An error with severity sev is reported.
614
    literal specification.  An error with severity sev is reported.
579
*/
615
*/
580
 
616
 
581
void add_token_literal
617
void
582
    PROTO_N ( ( id, sev ) )
-
 
583
    PROTO_T ( IDENTIFIER id X unsigned sev )
618
add_token_literal(IDENTIFIER id, unsigned sev)
584
{
619
{
585
    int n = -1 ;
620
	int n = -1;
586
    LITERAL_INFO *p = crt_int_lit ;
621
	LITERAL_INFO *p = crt_int_lit;
587
    if ( !IS_NULL_id ( id ) ) {
622
	if (!IS_NULL_id(id)) {
588
	id = resolve_token ( id, "ZZ", 0 ) ;
623
		id = resolve_token(id, "ZZ", 0);
-
 
624
		if (!IS_NULL_id(id)) {
589
	if ( !IS_NULL_id ( id ) ) n = builtin_token ( id ) ;
625
			n = builtin_token(id);
590
    }
626
		}
-
 
627
	}
591
    if ( p->tag == 1 ) {
628
	if (p->tag == 1) {
592
	report ( preproc_loc, ERR_pragma_lit_question () ) ;
629
		report(preproc_loc, ERR_pragma_lit_question());
593
	p->tag = 3 ;
630
		p->tag = 3;
594
    }
631
	}
595
    p->tok = id ;
632
	p->tok = id;
596
    p->tok_no = n ;
633
	p->tok_no = n;
597
    switch ( sev ) {
634
	switch (sev) {
-
 
635
	case OPTION_ON:
598
	case OPTION_ON : p->opt = OPT_error ; break ;
636
		p->opt = OPT_error;
-
 
637
		break;
-
 
638
	case OPTION_WARN:
599
	case OPTION_WARN : p->opt = OPT_warning ; break ;
639
		p->opt = OPT_warning;
-
 
640
		break;
-
 
641
	default:
600
	default : p->opt = OPT_none ; break ;
642
		p->opt = OPT_none;
-
 
643
		break;
601
    }
644
	}
602
    return ;
645
	return;
603
}
646
}
604
 
647
 
605
 
648
 
606
/*
649
/*
607
    FIND AN INTEGER LITERAL TYPE
650
    FIND AN INTEGER LITERAL TYPE
Line 610... Line 653...
610
    with base base and suffix suff.  num gives the text used to specify
653
    with base base and suffix suff.  num gives the text used to specify
611
    the constant for the purposes of error reporting.  fit is set to
654
    the constant for the purposes of error reporting.  fit is set to
612
    true if lit definitely fits into the result.
655
    true if lit definitely fits into the result.
613
*/
656
*/
614
 
657
 
615
TYPE find_literal_type
658
TYPE
616
    PROTO_N ( ( lit, base, suff, num, fit ) )
-
 
617
    PROTO_T ( NAT lit X int base X int suff X string num X int *fit )
659
find_literal_type(NAT lit, int base, int suff, string num, int *fit)
618
{
660
{
619
    TYPE t ;
661
	TYPE t;
620
    int tok ;
662
	int tok;
621
    INT_TYPE it ;
663
	INT_TYPE it;
622
    int big = 0 ;
664
	int big = 0;
623
    int have_tok = 0 ;
665
	int have_tok = 0;
624
    int opt = OPT_error ;
666
	int opt = OPT_error;
625
    NAT n = small_nat [0] ;
667
	NAT n = small_nat[0];
626
    IDENTIFIER tid = NULL_id ;
668
	IDENTIFIER tid = NULL_id;
627
    LIST ( TYPE ) qt = NULL_list ( TYPE ) ;
669
	LIST(TYPE)qt = NULL_list(TYPE);
628
    LITERAL_INFO *pt = int_lit_spec [ base ] [ suff ] ;
670
	LITERAL_INFO *pt = int_lit_spec[base][suff];
629
 
671
 
630
    /* Deal with calculated literals */
672
	/* Deal with calculated literals */
631
    switch ( TAG_nat ( lit ) ) {
673
	switch (TAG_nat(lit)) {
632
	case nat_neg_tag : {
674
	case nat_neg_tag:
633
	    lit = DEREF_nat ( nat_neg_arg ( lit ) ) ;
675
		lit = DEREF_nat(nat_neg_arg(lit));
634
	    t = find_literal_type ( lit, base, suff, num, fit ) ;
676
		t = find_literal_type(lit, base, suff, num, fit);
-
 
677
		return(t);
-
 
678
	case nat_token_tag:
-
 
679
		t = type_sint;
-
 
680
		*fit = 1;
-
 
681
		return(t);
-
 
682
	case nat_calc_tag: {
-
 
683
		EXP e = DEREF_exp(nat_calc_value(lit));
-
 
684
		t = DEREF_type(exp_type(e));
-
 
685
		*fit = 1;
635
	    return ( t ) ;
686
		return(t);
636
	}
687
	}
637
	case nat_token_tag : {
-
 
638
	    t = type_sint ;
-
 
639
	    *fit = 1 ;
-
 
640
	    return ( t ) ;
-
 
641
	}
688
	}
642
	case nat_calc_tag : {
-
 
643
	    EXP e = DEREF_exp ( nat_calc_value ( lit ) ) ;
-
 
644
	    t = DEREF_type ( exp_type ( e ) ) ;
-
 
645
	    *fit = 1 ;
-
 
646
	    return ( t ) ;
-
 
647
	}
-
 
648
    }
-
 
649
 
689
 
650
    /* Deal with simple literals */
690
	/* Deal with simple literals */
651
    while ( pt != NULL ) {
691
	while (pt != NULL) {
652
	int ch = 4 ;
692
		int ch = 4;
653
	TYPE s = pt->type ;
693
		TYPE s = pt->type;
654
	switch ( pt->tag ) {
694
		switch (pt->tag) {
655
	    case 0 : {
695
		case 0: {
656
		TYPE r = s ;
696
			TYPE r = s;
657
		if ( IS_NULL_type ( r ) ) r = type_ulong ;
697
			if (IS_NULL_type(r)) {
-
 
698
				r = type_ulong;
-
 
699
			}
658
		ch = check_nat_range ( r, lit ) ;
700
			ch = check_nat_range(r, lit);
659
		if ( ch == 0 ) *fit = 1 ;
701
			if (ch == 0) {
-
 
702
				*fit = 1;
-
 
703
			}
660
		if ( ch > 4 ) big = ch ;
704
			if (ch > 4) {
-
 
705
				big = ch;
-
 
706
			}
-
 
707
			if (big) {
661
		if ( big ) n = max_type_value ( NULL_type, 0 ) ;
708
				n = max_type_value(NULL_type, 0);
-
 
709
			}
662
		ch = big ;
710
			ch = big;
663
		break ;
711
			break;
664
	    }
712
		}
665
	    case 1 : {
713
		case 1:
666
		n = pt->bound ;
714
			n = pt->bound;
667
		ch = check_nat_range ( s, lit ) ;
715
			ch = check_nat_range(s, lit);
668
		if ( ch == 0 ) *fit = 1 ;
716
			if (ch == 0) {
669
		break ;
717
				*fit = 1;
670
	    }
718
			}
-
 
719
			break;
671
	    case 2 : {
720
		case 2:
672
		n = pt->bound ;
721
			n = pt->bound;
673
		if ( compare_nat ( n, lit ) >= 0 ) {
722
			if (compare_nat(n, lit) >= 0) {
674
		    if ( !IS_NULL_type ( s ) ) *fit = 1 ;
723
				if (!IS_NULL_type(s))*fit = 1;
675
		    ch = 0 ;
724
				ch = 0;
-
 
725
			}
-
 
726
			break;
-
 
727
		}
-
 
728
 
-
 
729
		if (ch == 0) {
-
 
730
			/* lit definitely fits into bound */
-
 
731
			if (!IS_NULL_type(s) && IS_NULL_list(qt)) {
-
 
732
				/* No previous fit */
-
 
733
				return(s);
-
 
734
			}
-
 
735
		}
-
 
736
		if (ch <= 2) {
-
 
737
			/* lit may fit into bound */
-
 
738
			if (!IS_NULL_type(s)) {
-
 
739
				if (have_tok == 0) {
-
 
740
					INT_TYPE is =
-
 
741
					    DEREF_itype(type_integer_rep(s));
-
 
742
					LIST(TYPE)st =
-
 
743
					    DEREF_list(itype_cases(is));
-
 
744
					qt = union_type_set(qt, st);
-
 
745
					if (ch == 0) {
-
 
746
						have_tok = 1;
-
 
747
					}
-
 
748
				}
-
 
749
			} else {
-
 
750
				if (have_tok == 0 && !IS_NULL_id(pt->tok)) {
-
 
751
					DESTROY_list(qt, SIZE_type);
-
 
752
					tok = int_lit_tok[base][suff].tok;
-
 
753
					if (pt->tok_no == tok) {
-
 
754
						qt = int_lit_tok[base][suff].cases;
-
 
755
					} else if (suff < SUFFIX_LL) {
-
 
756
						qt = all_prom_types;
-
 
757
					} else {
-
 
758
						qt = all_llong_types;
-
 
759
					}
-
 
760
					have_tok = 2;
-
 
761
				}
-
 
762
				break;
-
 
763
			}
-
 
764
		}
-
 
765
		if (ch > 4) {
-
 
766
			big = ch;
-
 
767
		}
-
 
768
		pt = pt->next;
-
 
769
	}
-
 
770
 
-
 
771
	/* Tokenised result */
-
 
772
	if (have_tok != 2) {
-
 
773
		/* Find list of possible types */
-
 
774
		if (IS_NULL_list(qt)) {
-
 
775
			qt = int_lit_tok[base][suff].cases;
-
 
776
		} else {
-
 
777
			qt = REVERSE_list(qt);
-
 
778
			qt = uniq_type_set(qt);
-
 
779
		}
-
 
780
	}
-
 
781
	if (pt) {
-
 
782
		/* Get token information from table */
-
 
783
		tid = pt->tok;
-
 
784
		opt = pt->opt;
-
 
785
	}
-
 
786
	if (num && !(*fit)) {
-
 
787
		/* Report error if necessary */
-
 
788
		ERROR err = ERR_lex_icon_large(num, n);
-
 
789
		err = set_severity(err, opt, 0);
-
 
790
		if (!IS_NULL_err(err)) {
-
 
791
			report(crt_loc, err);
676
		}
792
		}
677
		break ;
-
 
678
	    }
-
 
679
	}
793
	}
680
	if ( ch == 0 ) {
794
	if (LENGTH_list(qt) == 1) {
681
	    /* lit definitely fits into bound */
-
 
682
	    if ( !IS_NULL_type ( s ) && IS_NULL_list ( qt ) ) {
-
 
683
		/* No previous fit */
795
		/* Only one possible case */
684
		return ( s ) ;
-
 
685
	    }
-
 
686
	}
-
 
687
	if ( ch <= 2 ) {
-
 
688
	    /* lit may fit into bound */
-
 
689
	    if ( !IS_NULL_type ( s ) ) {
-
 
690
		if ( have_tok == 0 ) {
-
 
691
		    INT_TYPE is = DEREF_itype ( type_integer_rep ( s ) ) ;
-
 
692
		    LIST ( TYPE ) st = DEREF_list ( itype_cases ( is ) ) ;
-
 
693
		    qt = union_type_set ( qt, st ) ;
796
		t = DEREF_type(HEAD_list(qt));
694
		    if ( ch == 0 ) have_tok = 1 ;
-
 
695
		}
-
 
696
	    } else {
-
 
697
		if ( have_tok == 0 && !IS_NULL_id ( pt->tok ) ) {
-
 
698
		    DESTROY_list ( qt, SIZE_type ) ;
797
		DESTROY_list(qt, SIZE_type);
699
		    tok = int_lit_tok [ base ] [ suff ].tok ;
-
 
700
		    if ( pt->tok_no == tok ) {
-
 
701
			qt = int_lit_tok [ base ] [ suff ].cases ;
-
 
702
		    } else if ( suff < SUFFIX_LL ) {
-
 
703
			qt = all_prom_types ;
-
 
704
		    } else {
-
 
705
			qt = all_llong_types ;
-
 
706
		    }
-
 
707
		    have_tok = 2 ;
-
 
708
		}
-
 
709
		break ;
798
		return(t);
710
	    }
-
 
711
	}
-
 
712
	if ( ch > 4 ) big = ch ;
-
 
713
	pt = pt->next ;
-
 
714
    }
-
 
715
 
-
 
716
    /* Tokenised result */
-
 
717
    if ( have_tok != 2 ) {
-
 
718
	/* Find list of possible types */
-
 
719
	if ( IS_NULL_list ( qt ) ) {
-
 
720
	    qt = int_lit_tok [ base ] [ suff ].cases ;
-
 
721
	} else {
-
 
722
	    qt = REVERSE_list ( qt ) ;
-
 
723
	    qt = uniq_type_set ( qt ) ;
-
 
724
	}
799
	}
725
    }
-
 
726
    if ( pt ) {
-
 
727
	/* Get token information from table */
-
 
728
	tid = pt->tok ;
-
 
729
	opt = pt->opt ;
-
 
730
    }
-
 
731
    if ( num && !( *fit ) ) {
-
 
732
	/* Report error if necessary */
-
 
733
	ERROR err = ERR_lex_icon_large ( num, n ) ;
-
 
734
	err = set_severity ( err, opt, 0 ) ;
-
 
735
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
-
 
736
    }
-
 
737
    if ( LENGTH_list ( qt ) == 1 ) {
-
 
738
	/* Only one possible case */
-
 
739
	t = DEREF_type ( HEAD_list ( qt ) ) ;
-
 
740
	DESTROY_list ( qt, SIZE_type ) ;
-
 
741
	return ( t ) ;
-
 
742
    }
-
 
743
    tok = int_lit_tok [ base ] [ suff ].tok ;
800
	tok = int_lit_tok[base][suff].tok;
744
    MAKE_itype_literal ( NULL_type, qt, lit, tok, base, suff, tid, it ) ;
801
	MAKE_itype_literal(NULL_type, qt, lit, tok, base, suff, tid, it);
745
    t = promote_itype ( it, it ) ;
802
	t = promote_itype(it, it);
746
    return ( t ) ;
803
	return(t);
747
}
804
}
748
 
805
 
749
 
806
 
750
/*
807
/*
751
    ANALYSE AN INTEGER OR FLOATING LITERAL
808
    ANALYSE AN INTEGER OR FLOATING LITERAL
Line 756... Line 813...
756
    depending on the form of the literal.  Note that str can be an area
813
    depending on the form of the literal.  Note that str can be an area
757
    of read-only memory for integer literals, but not for floating
814
    of read-only memory for integer literals, but not for floating
758
    literals.
815
    literals.
759
*/
816
*/
760
 
817
 
761
EXP make_literal_exp
818
EXP
762
    PROTO_N ( ( str, ptok, force ) )
-
 
763
    PROTO_T ( string str X int *ptok X int force )
819
make_literal_exp(string str, int *ptok, int force)
764
{
820
{
765
    EXP e ;
821
	EXP e;
766
    string r ;
822
	string r;
767
    int err = 0 ;
823
	int err = 0;
768
    int flt = 0 ;
824
	int flt = 0;
769
    string s = str ;
825
	string s = str;
770
    unsigned base = 10 ;
826
	unsigned base = 10;
771
    string dot_posn = NULL ;
827
	string dot_posn = NULL;
772
    string exp_posn = NULL ;
828
	string exp_posn = NULL;
773
    int form = BASE_DECIMAL ;
829
	int form = BASE_DECIMAL;
774
 
830
 
775
    /* Check small literals */
831
	/* Check small literals */
776
    character c1 = s [0] ;
832
	character c1 = s[0];
777
    character c2 = 0 ;
833
	character c2 = 0;
778
    if ( c1 ) c2 = s [1] ;
-
 
779
    if ( c2 == 0 && ( c1 >= char_zero && c1 <= char_nine ) ) {
-
 
780
	unsigned etag = exp_int_lit_tag ;
-
 
781
	int n = ( int ) digit_values [ c1 ] ;
-
 
782
	NAT lit = small_nat [n] ;
-
 
783
	if ( IS_NULL_nat ( lit ) ) lit = make_small_nat ( n ) ;
-
 
784
	if ( n == 0 ) etag = exp_null_tag ;
-
 
785
	MAKE_exp_int_lit ( type_sint, lit, etag, e ) ;
-
 
786
	*ptok = lex_integer_Hexp ;
-
 
787
	return ( e ) ;
-
 
788
    }
-
 
789
 
-
 
790
    if ( c1 == char_zero && ( c2 == char_x || c2 == char_X ) ) {
-
 
791
	/* Hexadecimal integer */
-
 
792
	base = 16 ;
-
 
793
	form = BASE_HEXADECIMAL ;
-
 
794
	r = s + 2 ;
-
 
795
	s = check_digits ( r, base ) ;
-
 
796
	if ( s == r ) err = 1 ;
-
 
797
    } else {
-
 
798
	if ( c1 == char_dot ) {
-
 
799
	    /* Fractional component of floating literal */
-
 
800
	    dot_posn = s ;
-
 
801
	    r = s + 1 ;
-
 
802
	    s = check_digits ( r, base ) ;
-
 
803
	    if ( s == r ) err = 1 ;
-
 
804
	    flt = 1 ;
-
 
805
	} else {
834
	if (c1) {
806
	    /* Sequence of decimal digits */
-
 
807
	    r = s ;
-
 
808
	    s = check_digits ( r, base ) ;
-
 
809
	    if ( s == r ) {
-
 
810
		if ( c1 == char_plus || c1 == char_minus ) {
-
 
811
		    /* Extension to handle signs */
-
 
812
		    e = make_literal_exp ( str + 1, ptok, force ) ;
-
 
813
		    if ( c1 == char_minus ) {
-
 
814
			e = make_uminus_exp ( lex_minus, e ) ;
-
 
815
		    }
-
 
816
		    return ( e ) ;
-
 
817
		}
-
 
818
		err = 1 ;
-
 
819
	    }
-
 
820
	    if ( s [0] == char_dot ) {
-
 
821
		/* Fractional component of floating literal */
-
 
822
		dot_posn = s ;
-
 
823
		s = check_digits ( s + 1, base ) ;
-
 
824
		flt = 1 ;
-
 
825
	    }
-
 
826
	}
-
 
827
	exp_posn = s ;
-
 
828
	c2 = s [0] ;
835
		c2 = s[1];
829
	if ( c2 == char_e || c2 == char_E ) {
-
 
830
	    /* Exponent component of floating literal */
-
 
831
	    c2 = s [1] ;
-
 
832
	    if ( c2 == char_plus || c2 == char_minus ) s++ ;
-
 
833
	    r = s + 1 ;
-
 
834
	    s = check_digits ( r, base ) ;
-
 
835
	    if ( s == r ) err = 1 ;
-
 
836
	    flt = 1 ;
-
 
837
	}
-
 
838
	if ( c1 == char_zero && !flt ) {
-
 
839
	    /* Octal integer */
-
 
840
	    base = 8 ;
-
 
841
	    form = BASE_OCTAL ;
-
 
842
	    r = check_digits ( str, base ) ;
-
 
843
	    if ( r != s ) {
-
 
844
		/* Digits contain 8 or 9 */
-
 
845
		report ( crt_loc, ERR_lex_icon_octal ( str ) ) ;
-
 
846
	    }
-
 
847
	}
-
 
848
    }
-
 
849
 
-
 
850
    if ( flt ) {
-
 
851
	/* Floating literals */
-
 
852
	int zero ;
-
 
853
	NAT expon ;
-
 
854
	character ep ;
-
 
855
	string frac_part ;
-
 
856
	string int_part = str ;
-
 
857
	string suff_posn = s ;
-
 
858
	unsigned trail_zero = 0 ;
-
 
859
	FLOAT lit = NULL_flt ;
-
 
860
	TYPE t = type_double ;
-
 
861
 
-
 
862
	/* Check float suffix */
-
 
863
	c1 = s [0] ;
-
 
864
	if ( c1 == char_f || c1 == char_F ) {
-
 
865
	    /* Suffix F */
-
 
866
	    t = type_float ;
-
 
867
	    s++ ;
-
 
868
	    c1 = s [0] ;
-
 
869
	} else if ( c1 == char_l || c1 == char_L ) {
-
 
870
	    /* Suffix L */
-
 
871
	    t = type_ldouble ;
-
 
872
	    s++ ;
-
 
873
	    c1 = s [0] ;
-
 
874
	}
-
 
875
 
-
 
876
	/* Check for end of number */
-
 
877
	if ( c1 || err ) report ( crt_loc, ERR_lex_literal_bad ( str ) ) ;
-
 
878
 
-
 
879
	/* Find number components (involves writing to s)  */
-
 
880
	while ( int_part [0] == char_zero ) {
-
 
881
	    /* Remove initial zeros */
-
 
882
	    int_part++ ;
-
 
883
	}
836
	}
884
	if ( dot_posn ) {
-
 
885
	    dot_posn [0] = 0 ;
-
 
886
	    if ( int_part == dot_posn ) int_part = small_number [0] ;
837
	if (c2 == 0 && (c1 >= char_zero && c1 <= char_nine)) {
887
	    frac_part = dot_posn + 1 ;
838
		unsigned etag = exp_int_lit_tag;
888
	    if ( frac_part == exp_posn ) {
839
		int n = (int)digit_values[c1];
889
		frac_part = small_number [0] ;
840
		NAT lit = small_nat[n];
890
	    } else {
-
 
891
		/* Remove trailing zeros */
-
 
892
		string frac_zero = exp_posn - 1 ;
-
 
893
		while ( frac_zero [0] == char_zero ) {
-
 
894
		    frac_zero [0] = 0 ;
841
		if (IS_NULL_nat(lit)) {
895
		    frac_zero-- ;
-
 
896
		    trail_zero++ ;
842
			lit = make_small_nat(n);
897
		}
843
		}
898
		if ( frac_zero == dot_posn ) frac_part = small_number [0] ;
-
 
899
	    }
-
 
900
	} else {
-
 
901
	    if ( int_part == exp_posn ) int_part = small_number [0] ;
-
 
902
	    frac_part = small_number [0] ;
-
 
903
	}
-
 
904
	ep = exp_posn [0] ;
-
 
905
	exp_posn [0] = 0 ;
-
 
906
	if ( ep == char_e || ep == char_E ) {
-
 
907
	    /* Evaluate exponent */
-
 
908
	    r = exp_posn + 1 ;
-
 
909
	    c2 = r [0] ;
844
		if (n == 0) {
910
	    if ( c2 == char_minus || c2 == char_plus ) r++ ;
-
 
911
	    expon = eval_digits ( r, suff_posn, base ) ;
-
 
912
	    if ( c2 == char_minus ) expon = negate_nat ( expon ) ;
-
 
913
	    zero = is_zero_nat ( expon ) ;
-
 
914
	} else {
-
 
915
	    expon = small_nat [0] ;
845
			etag = exp_null_tag;
916
	    zero = 1 ;
-
 
917
	}
-
 
918
	if ( zero && ustreq ( frac_part, small_number [0] ) ) {
-
 
919
	    int i ;
-
 
920
	    for ( i = 0 ; i < SMALL_FLT_SIZE ; i++ ) {
-
 
921
		if ( ustreq ( int_part, small_number [i] ) ) {
-
 
922
		    lit = get_float ( t, i ) ;
-
 
923
		    break ;
-
 
924
		}
846
		}
-
 
847
		MAKE_exp_int_lit(type_sint, lit, etag, e);
-
 
848
		*ptok = lex_integer_Hexp;
925
	    }
849
		return(e);
926
	}
850
	}
927
	if ( IS_NULL_flt ( lit ) ) {
-
 
928
	    int_part = xustrcpy ( int_part ) ;
-
 
929
	    frac_part = xustrcpy ( frac_part ) ;
-
 
930
	}
-
 
931
	if ( trail_zero ) {
-
 
932
	    /* Restore trailing zeros */
-
 
933
	    r = exp_posn - 1 ;
-
 
934
	    do {
-
 
935
		r [0] = char_zero ;
-
 
936
		r-- ;
-
 
937
		trail_zero-- ;
-
 
938
	    } while ( trail_zero ) ;
-
 
939
	}
-
 
940
	if ( dot_posn ) dot_posn [0] = char_dot ;
-
 
941
	exp_posn [0] = ep ;
-
 
942
 
851
 
-
 
852
	if (c1 == char_zero && (c2 == char_x || c2 == char_X)) {
-
 
853
		/* Hexadecimal integer */
-
 
854
		base = 16;
-
 
855
		form = BASE_HEXADECIMAL;
-
 
856
		r = s + 2;
-
 
857
		s = check_digits(r, base);
-
 
858
		if (s == r) {
-
 
859
			err = 1;
-
 
860
		}
-
 
861
	} else {
-
 
862
		if (c1 == char_dot) {
-
 
863
			/* Fractional component of floating literal */
-
 
864
			dot_posn = s;
-
 
865
			r = s + 1;
-
 
866
			s = check_digits(r, base);
-
 
867
			if (s == r) {
-
 
868
				err = 1;
-
 
869
			}
-
 
870
			flt = 1;
-
 
871
		} else {
-
 
872
			/* Sequence of decimal digits */
-
 
873
			r = s;
-
 
874
			s = check_digits(r, base);
-
 
875
			if (s == r) {
-
 
876
				if (c1 == char_plus || c1 == char_minus) {
-
 
877
					/* Extension to handle signs */
-
 
878
					e = make_literal_exp(str + 1, ptok,
-
 
879
							     force);
-
 
880
					if (c1 == char_minus) {
-
 
881
						e = make_uminus_exp(lex_minus,
-
 
882
								    e);
-
 
883
					}
-
 
884
					return(e);
-
 
885
				}
-
 
886
				err = 1;
-
 
887
			}
-
 
888
			if (s[0] == char_dot) {
-
 
889
				/* Fractional component of floating literal */
-
 
890
				dot_posn = s;
-
 
891
				s = check_digits(s + 1, base);
-
 
892
				flt = 1;
-
 
893
			}
-
 
894
		}
-
 
895
		exp_posn = s;
-
 
896
		c2 = s[0];
-
 
897
		if (c2 == char_e || c2 == char_E) {
943
	/* Construct result - type is as per suffix */
898
			/* Exponent component of floating literal */
-
 
899
			c2 = s[1];
-
 
900
			if (c2 == char_plus || c2 == char_minus) {
-
 
901
				s++;
-
 
902
			}
-
 
903
			r = s + 1;
-
 
904
			s = check_digits(r, base);
-
 
905
			if (s == r) {
-
 
906
				err = 1;
-
 
907
			}
-
 
908
			flt = 1;
-
 
909
		}
944
	if ( IS_NULL_flt ( lit ) ) {
910
		if (c1 == char_zero && !flt) {
-
 
911
			/* Octal integer */
-
 
912
			base = 8;
-
 
913
			form = BASE_OCTAL;
-
 
914
			r = check_digits(str, base);
-
 
915
			if (r != s) {
-
 
916
				/* Digits contain 8 or 9 */
945
	    MAKE_flt_simple ( int_part, frac_part, expon, lit ) ;
917
				report(crt_loc, ERR_lex_icon_octal(str));
-
 
918
			}
-
 
919
		}
946
	}
920
	}
947
	MAKE_exp_float_lit ( t, lit, e ) ;
-
 
948
	*ptok = lex_floating_Hexp ;
-
 
949
 
-
 
950
    } else {
-
 
951
	/* Integer literals */
-
 
952
	TYPE t ;
-
 
953
	NAT lit ;
-
 
954
	int ls = 0 ;
-
 
955
	int us = 0 ;
-
 
956
	int fit = 0 ;
-
 
957
 
921
 
-
 
922
	if (flt) {
958
	/* Find integer value */
923
		/* Floating literals */
-
 
924
		int zero;
-
 
925
		NAT expon;
-
 
926
		character ep;
-
 
927
		string frac_part;
-
 
928
		string int_part = str;
-
 
929
		string suff_posn = s;
-
 
930
		unsigned trail_zero = 0;
-
 
931
		FLOAT lit = NULL_flt;
-
 
932
		TYPE t = type_double;
-
 
933
 
-
 
934
		/* Check float suffix */
959
	r = str ;
935
		c1 = s[0];
-
 
936
		if (c1 == char_f || c1 == char_F) {
-
 
937
			/* Suffix F */
-
 
938
			t = type_float;
-
 
939
			s++;
-
 
940
			c1 = s[0];
-
 
941
		} else if (c1 == char_l || c1 == char_L) {
-
 
942
			/* Suffix L */
-
 
943
			t = type_ldouble;
-
 
944
			s++;
-
 
945
			c1 = s[0];
-
 
946
		}
-
 
947
 
-
 
948
		/* Check for end of number */
-
 
949
		if (c1 || err) {
-
 
950
			report(crt_loc, ERR_lex_literal_bad(str));
-
 
951
		}
-
 
952
 
-
 
953
		/* Find number components (involves writing to s)  */
-
 
954
		while (int_part[0] == char_zero) {
-
 
955
			/* Remove initial zeros */
-
 
956
			int_part++;
-
 
957
		}
-
 
958
		if (dot_posn) {
-
 
959
			dot_posn[0] = 0;
-
 
960
			if (int_part == dot_posn) {
-
 
961
				int_part = small_number[0];
-
 
962
			}
-
 
963
			frac_part = dot_posn + 1;
-
 
964
			if (frac_part == exp_posn) {
-
 
965
				frac_part = small_number[0];
-
 
966
			} else {
-
 
967
				/* Remove trailing zeros */
-
 
968
				string frac_zero = exp_posn - 1;
-
 
969
				while (frac_zero[0] == char_zero) {
-
 
970
					frac_zero[0] = 0;
-
 
971
					frac_zero--;
-
 
972
					trail_zero++;
-
 
973
				}
-
 
974
				if (frac_zero == dot_posn) {
-
 
975
					frac_part = small_number[0];
-
 
976
				}
-
 
977
			}
-
 
978
		} else {
-
 
979
			if (int_part == exp_posn) {
-
 
980
				int_part = small_number[0];
-
 
981
			}
-
 
982
			frac_part = small_number[0];
-
 
983
		}
-
 
984
		ep = exp_posn[0];
-
 
985
		exp_posn[0] = 0;
960
	if ( form == BASE_HEXADECIMAL ) r += 2 ;
986
		if (ep == char_e || ep == char_E) {
-
 
987
			/* Evaluate exponent */
-
 
988
			r = exp_posn + 1;
-
 
989
			c2 = r[0];
-
 
990
			if (c2 == char_minus || c2 == char_plus) {
-
 
991
				r++;
-
 
992
			}
961
	lit = eval_digits ( r, s, base ) ;
993
			expon = eval_digits(r, suff_posn, base);
-
 
994
			if (c2 == char_minus) {
-
 
995
				expon = negate_nat(expon);
-
 
996
			}
-
 
997
			zero = is_zero_nat(expon);
-
 
998
		} else {
-
 
999
			expon = small_nat[0];
-
 
1000
			zero = 1;
-
 
1001
		}
-
 
1002
		if (zero && ustreq(frac_part, small_number[0])) {
-
 
1003
			int i;
-
 
1004
			for (i = 0; i < SMALL_FLT_SIZE; i++) {
-
 
1005
				if (ustreq(int_part, small_number[i])) {
-
 
1006
					lit = get_float(t, i);
-
 
1007
					break;
-
 
1008
				}
-
 
1009
			}
-
 
1010
		}
-
 
1011
		if (IS_NULL_flt(lit)) {
-
 
1012
			int_part = xustrcpy(int_part);
-
 
1013
			frac_part = xustrcpy(frac_part);
-
 
1014
		}
-
 
1015
		if (trail_zero) {
-
 
1016
			/* Restore trailing zeros */
-
 
1017
			r = exp_posn - 1;
-
 
1018
			do {
-
 
1019
				r[0] = char_zero;
-
 
1020
				r--;
-
 
1021
				trail_zero--;
-
 
1022
			} while (trail_zero);
-
 
1023
		}
-
 
1024
		if (dot_posn) {
-
 
1025
			dot_posn[0] = char_dot;
-
 
1026
		}
-
 
1027
		exp_posn[0] = ep;
-
 
1028
 
-
 
1029
		/* Construct result - type is as per suffix */
-
 
1030
		if (IS_NULL_flt(lit)) {
-
 
1031
			MAKE_flt_simple(int_part, frac_part, expon, lit);
-
 
1032
		}
-
 
1033
		MAKE_exp_float_lit(t, lit, e);
-
 
1034
		*ptok = lex_floating_Hexp;
962
 
1035
 
963
	/* Check integer suffix */
-
 
964
	c1 = s [0] ;
-
 
965
	if ( c1 == char_u || c1 == char_U ) {
-
 
966
	    us = 1 ;
-
 
967
	    s++ ;
-
 
968
	    c1 = s [0] ;
-
 
969
	}
-
 
970
	if ( c1 == char_l || c1 == char_L ) {
-
 
971
	    ls = 1 ;
-
 
972
	    if ( s [1] == c1 && basetype_info [ ntype_sllong ].key ) {
-
 
973
		report ( crt_loc, ERR_lex_icon_llong ( str ) ) ;
-
 
974
		ls = 2 ;
-
 
975
		s++ ;
-
 
976
	    }
-
 
977
	    s++ ;
-
 
978
	    c1 = s [0] ;
-
 
979
	} else {
1036
	} else {
-
 
1037
		/* Integer literals */
-
 
1038
		TYPE t;
-
 
1039
		NAT lit;
-
 
1040
		int ls = 0;
-
 
1041
		int us = 0;
-
 
1042
		int fit = 0;
-
 
1043
 
-
 
1044
		/* Find integer value */
-
 
1045
		r = str;
-
 
1046
		if (form == BASE_HEXADECIMAL) {
-
 
1047
			r += 2;
-
 
1048
		}
-
 
1049
		lit = eval_digits(r, s, base);
-
 
1050
 
-
 
1051
		/* Check integer suffix */
-
 
1052
		c1 = s[0];
-
 
1053
		if (c1 == char_u || c1 == char_U) {
-
 
1054
			us = 1;
-
 
1055
			s++;
-
 
1056
			c1 = s[0];
-
 
1057
		}
-
 
1058
		if (c1 == char_l || c1 == char_L) {
-
 
1059
			ls = 1;
-
 
1060
			if (s[1] == c1 && basetype_info[ntype_sllong].key) {
-
 
1061
				report(crt_loc, ERR_lex_icon_llong(str));
-
 
1062
				ls = 2;
-
 
1063
				s++;
-
 
1064
			}
-
 
1065
			s++;
-
 
1066
			c1 = s[0];
-
 
1067
		} else {
980
	    /* Map 'int' to 'long' in '#if' expressions */
1068
			/* Map 'int' to 'long' in '#if' expressions */
981
	    if ( in_hash_if_exp ) ls = 1 ;
1069
			if (in_hash_if_exp) {
-
 
1070
				ls = 1;
-
 
1071
			}
982
	}
1072
		}
983
	if ( us == 0 && ( c1 == char_u || c1 == char_U ) ) {
1073
		if (us == 0 && (c1 == char_u || c1 == char_U)) {
984
	    us = 1 ;
1074
			us = 1;
985
	    s++ ;
1075
			s++;
986
	    c1 = s [0] ;
1076
			c1 = s[0];
987
	}
1077
		}
988
 
1078
 
989
	/* Check for end of number */
1079
		/* Check for end of number */
-
 
1080
		if (c1 || err) {
990
	if ( c1 || err ) report ( crt_loc, ERR_lex_literal_bad ( str ) ) ;
1081
			report(crt_loc, ERR_lex_literal_bad(str));
-
 
1082
		}
991
 
1083
 
992
	/* Find literal type */
1084
		/* Find literal type */
993
	if ( force ) {
1085
		if (force) {
994
	    t = type_ulong ;
1086
			t = type_ulong;
995
	    fit = 1 ;
1087
			fit = 1;
996
	} else {
1088
		} else {
997
	    int suff = SUFFIX ( us, ls ) ;
1089
			int suff = SUFFIX(us, ls);
998
	    t = find_literal_type ( lit, form, suff, str, &fit ) ;
1090
			t = find_literal_type(lit, form, suff, str, &fit);
-
 
1091
		}
-
 
1092
		MAKE_exp_int_lit(t, lit, exp_int_lit_tag, e);
-
 
1093
		if (!fit) {
-
 
1094
			/* Force result to be a calculated value */
-
 
1095
			MAKE_exp_cast(t, CONV_INT_INT, e, e);
-
 
1096
			MAKE_nat_calc(e, lit);
-
 
1097
			MAKE_exp_int_lit(t, lit, exp_int_lit_tag, e);
-
 
1098
		}
-
 
1099
		*ptok = lex_integer_Hexp;
999
	}
1100
	}
1000
	MAKE_exp_int_lit ( t, lit, exp_int_lit_tag, e ) ;
-
 
1001
	if ( !fit ) {
-
 
1002
	    /* Force result to be a calculated value */
-
 
1003
	    MAKE_exp_cast ( t, CONV_INT_INT, e, e ) ;
-
 
1004
	    MAKE_nat_calc ( e, lit ) ;
-
 
1005
	    MAKE_exp_int_lit ( t, lit, exp_int_lit_tag, e ) ;
-
 
1006
	}
-
 
1007
	*ptok = lex_integer_Hexp ;
-
 
1008
    }
-
 
1009
    return ( e ) ;
1101
	return(e);
1010
}
1102
}
1011
 
1103
 
1012
 
1104
 
1013
/*
1105
/*
1014
    IS A FLOATING LITERAL ZERO?
1106
    IS A FLOATING LITERAL ZERO?
1015
 
1107
 
1016
    This routine checks whether the floating point literal f is zero.
1108
    This routine checks whether the floating point literal f is zero.
1017
*/
1109
*/
1018
 
1110
 
1019
int is_zero_float
1111
int
1020
    PROTO_N ( ( f ) )
-
 
1021
    PROTO_T ( FLOAT f )
1112
is_zero_float(FLOAT f)
1022
{
1113
{
1023
    string s ;
1114
	string s;
1024
    character c ;
1115
	character c;
1025
    s = DEREF_string ( flt_simple_int_part ( f ) ) ;
1116
	s = DEREF_string(flt_simple_int_part(f));
1026
    while ( c = *( s++ ), c != 0 ) {
1117
	while (c = *(s++), c != 0) {
1027
	if ( c != char_zero ) return ( 0 ) ;
1118
		if (c != char_zero) {
-
 
1119
			return(0);
1028
    }
1120
		}
-
 
1121
	}
1029
    s = DEREF_string ( flt_simple_frac_part ( f ) ) ;
1122
	s = DEREF_string(flt_simple_frac_part(f));
1030
    while ( c = *( s++ ), c != 0 ) {
1123
	while (c = *(s++), c != 0) {
1031
	if ( c != char_zero ) return ( 0 ) ;
1124
		if (c != char_zero) {
-
 
1125
			return(0);
1032
    }
1126
		}
-
 
1127
	}
1033
    return ( 1 ) ;
1128
	return(1);
1034
}
1129
}
1035
 
1130
 
1036
 
1131
 
1037
/*
1132
/*
1038
    ARE TWO FLOATING LITERALS EQUAL?
1133
    ARE TWO FLOATING LITERALS EQUAL?
Line 1040... Line 1135...
1040
    This routine checks whether the floating point literals f and g are
1135
    This routine checks whether the floating point literals f and g are
1041
    equal.  Note that this is equality of representation rather than
1136
    equal.  Note that this is equality of representation rather than
1042
    equality of the underlying numbers.
1137
    equality of the underlying numbers.
1043
*/
1138
*/
1044
 
1139
 
1045
int eq_float_lit
1140
int
1046
    PROTO_N ( ( f, g ) )
-
 
1047
    PROTO_T ( FLOAT f X FLOAT g )
1141
eq_float_lit(FLOAT f, FLOAT g)
1048
{
1142
{
1049
    NAT ef, eg ;
1143
	NAT ef, eg;
1050
    ulong nf, ng ;
1144
	ulong nf, ng;
1051
    string af, ag ;
1145
	string af, ag;
1052
    string bf, bg ;
1146
	string bf, bg;
1053
    if ( EQ_flt ( f, g ) ) return ( 1 ) ;
1147
	if (EQ_flt(f, g)) {
-
 
1148
		return(1);
-
 
1149
	}
1054
    DECONS_flt_simple ( nf, af, bf, ef, f ) ;
1150
	DECONS_flt_simple(nf, af, bf, ef, f);
1055
    DECONS_flt_simple ( ng, ag, bg, eg, g ) ;
1151
	DECONS_flt_simple(ng, ag, bg, eg, g);
1056
    if ( !ustreq ( af, ag ) ) return ( 0 ) ;
1152
	if (!ustreq(af, ag)) {
-
 
1153
		return(0);
-
 
1154
	}
1057
    if ( !ustreq ( bf, bg ) ) return ( 0 ) ;
1155
	if (!ustreq(bf, bg)) {
-
 
1156
		return(0);
-
 
1157
	}
1058
    if ( compare_nat ( ef, eg ) != 0 ) return ( 0 ) ;
1158
	if (compare_nat(ef, eg)!= 0) {
-
 
1159
		return(0);
-
 
1160
	}
-
 
1161
	if (nf == LINK_NONE) {
1059
    if ( nf == LINK_NONE ) COPY_ulong ( flt_tok ( f ), ng ) ;
1162
		COPY_ulong(flt_tok(f), ng);
-
 
1163
	}
-
 
1164
	if (ng == LINK_NONE) {
1060
    if ( ng == LINK_NONE ) COPY_ulong ( flt_tok ( g ), nf ) ;
1165
		COPY_ulong(flt_tok(g), nf);
-
 
1166
	}
1061
    return ( 1 ) ;
1167
	return(1);
1062
}
1168
}
1063
 
1169
 
1064
 
1170
 
1065
/*
1171
/*
1066
    DEFAULT ROUNDING MODE
1172
    DEFAULT ROUNDING MODE
1067
 
1173
 
1068
    This variable gives the default rounding mode used for converting
1174
    This variable gives the default rounding mode used for converting
1069
    floating point expressions to integers.
1175
    floating point expressions to integers.
1070
*/
1176
*/
1071
 
1177
 
1072
RMODE crt_round_mode = rmode_to_zero ;
1178
RMODE crt_round_mode = rmode_to_zero;
1073
 
1179
 
1074
 
1180
 
1075
/*
1181
/*
1076
    ROUND A FLOATING POINT LITERAL
1182
    ROUND A FLOATING POINT LITERAL
1077
 
1183
 
1078
    This routine rounds the floating point literal f to an integer
1184
    This routine rounds the floating point literal f to an integer
1079
    literal by the rounding mode corresponding to mode.  The null integer
1185
    literal by the rounding mode corresponding to mode.  The null integer
1080
    literal is returned to indicate a target dependent literal.  The
1186
    literal is returned to indicate a target dependent literal.  The
1081
    range of values in which the result is target independent is actually
1187
    range of values in which the result is target independent is actually
1082
    rather small - it is given by FLT_DIG.
1188
    rather small - it is given by FLT_DIG.
1182
 
1295
 
1183
/*
1296
/*
1184
    EVALUATE A UNICODE CHARACTER
1297
    EVALUATE A UNICODE CHARACTER
1185
 
1298
 
1186
    This routine evaluates the unicode character with prefix c, consisting
1299
    This routine evaluates the unicode character with prefix c, consisting
1187
    of n hex digits, given by ps.  ps is advanced to the position following
1300
    of n hex digits, given by ps.  ps is advanced to the position following
1188
    the hex digits.
1301
    the hex digits.
1189
*/
1302
*/
1190
 
1303
 
1191
unsigned long eval_unicode
1304
unsigned long
1192
    PROTO_N ( ( c, n, pc, ps, err ) )
-
 
1193
    PROTO_T ( int c X unsigned n X int *pc X string *ps X ERROR *err )
1305
eval_unicode(int c, unsigned n, int *pc, string *ps, ERROR *err)
1194
{
1306
{
1195
    string r = *ps ;
1307
	string r = *ps;
1196
    unsigned long u ;
1308
	unsigned long u;
1197
    unsigned base = 16 ;
1309
	unsigned base = 16;
1198
    string s = check_digits ( r, base ) ;
1310
	string s = check_digits(r, base);
1199
    unsigned m = ( unsigned ) ( s - r ) ;
1311
	unsigned m = (unsigned)(s - r);
1200
    if ( m < n ) {
1312
	if (m < n) {
1201
	add_error ( err, ERR_lex_charset_len ( c, n ) ) ;
1313
		add_error(err, ERR_lex_charset_len(c, n));
1202
    } else {
1314
	} else {
1203
	s = r + n ;
1315
		s = r + n;
1204
    }
1316
	}
1205
    *ps = s ;
1317
	*ps = s;
1206
    u = eval_char_digits ( r, s, base ) ;
1318
	u = eval_char_digits(r, s, base);
1207
    add_error ( err, ERR_lex_charset_replace ( u ) ) ;
1319
	add_error(err, ERR_lex_charset_replace(u));
1208
    if ( u < 0x20 || ( u >= 0x7f && u <= 0x9f ) || is_legal_char ( u ) ) {
1320
	if (u < 0x20 || (u >= 0x7f && u <= 0x9f) || is_legal_char(u)) {
1209
	add_error ( err, ERR_lex_charset_bad ( u ) ) ;
1321
		add_error(err, ERR_lex_charset_bad(u));
1210
	*pc = CHAR_SIMPLE ;
1322
		*pc = CHAR_SIMPLE;
1211
    } else {
1323
	} else {
1212
	if ( u <= ( unsigned long ) 0xffff ) *pc = CHAR_UNI4 ;
1324
		if (u <= (unsigned long)0xffff) {
-
 
1325
			*pc = CHAR_UNI4;
1213
    }
1326
		}
-
 
1327
	}
1214
    return ( u ) ;
1328
	return(u);
1215
}
1329
}
1216
 
1330
 
1217
 
1331
 
1218
/*
1332
/*
1219
    GET A MULTI-BYTE CHARACTER FROM A STRING
1333
    GET A MULTI-BYTE CHARACTER FROM A STRING
1220
 
1334
 
1221
    This routine returns the multi-byte character pointed to by the
1335
    This routine returns the multi-byte character pointed to by the
1222
    string s.  It assigns the character type to pc.
1336
    string s.  It assigns the character type to pc.
1223
*/
1337
*/
1224
 
1338
 
1225
unsigned long get_multi_char
1339
unsigned long
1226
    PROTO_N ( ( s, pc ) )
-
 
1227
    PROTO_T ( string s X int *pc )
1340
get_multi_char(string s, int *pc)
1228
{
1341
{
1229
    int i ;
1342
	int i;
1230
    unsigned long n = 0 ;
1343
	unsigned long n = 0;
1231
    for ( i = MULTI_WIDTH - 1 ; i >= 1 ; i-- ) {
1344
	for (i = MULTI_WIDTH - 1; i >= 1; i--) {
1232
	n = ( n << 8 ) + ( unsigned long ) s [i] ;
1345
		n = (n << 8) + (unsigned long)s[i];
1233
    }
1346
	}
1234
    *pc = ( int ) s [0] ;
1347
	*pc = (int)s[0];
1235
    return ( n ) ;
1348
	return(n);
1236
}
1349
}
1237
 
1350
 
1238
 
1351
 
1239
/*
1352
/*
1240
    ADD A MULTI-BYTE CHARACTER TO A STRING
1353
    ADD A MULTI-BYTE CHARACTER TO A STRING
1241
 
1354
 
1242
    This routine adds the multi-byte character n of type ch to the
1355
    This routine adds the multi-byte character n of type ch to the
1243
    string s.  A multi-byte character is represented by 5 characters.
1356
    string s.  A multi-byte character is represented by 5 characters.
1244
    The first is a key describing how the character was described (a
1357
    The first is a key describing how the character was described (a
1245
    simple character, a hex or octal escape sequence, a unicode
1358
    simple character, a hex or octal escape sequence, a unicode
1246
    character etc.).  The next four characters give the character value.
1359
    character etc.).  The next four characters give the character value.
1247
*/
1360
*/
1248
 
1361
 
1249
void add_multi_char
1362
void
1250
    PROTO_N ( ( s, n, ch ) )
-
 
1251
    PROTO_T ( string s X unsigned long n X int ch )
1363
add_multi_char(string s, unsigned long n, int ch)
1252
{
1364
{
1253
    int i ;
1365
	int i;
1254
    s [0] = ( character ) ch ;
1366
	s[0] = (character)ch;
1255
    for ( i = 1 ; i < MULTI_WIDTH ; i++ ) {
1367
	for (i = 1; i < MULTI_WIDTH; i++) {
1256
	s [i] = ( character ) ( n & 0xff ) ;
1368
		s[i] = (character)(n & 0xff);
1257
	n >>= 8 ;
1369
		n >>= 8;
1258
    }
1370
	}
-
 
1371
	if (n) {
1259
    if ( n ) report ( crt_loc, ERR_lex_ccon_large () ) ;
1372
		report(crt_loc, ERR_lex_ccon_large());
-
 
1373
	}
1260
    return ;
1374
	return;
1261
}
1375
}
1262
 
1376
 
1263
 
1377
 
1264
/*
1378
/*
1265
    CREATE A MULTI-BYTE STRING
1379
    CREATE A MULTI-BYTE STRING
1266
 
1380
 
1267
    This routine creates a multi-byte string of length n in s from the
1381
    This routine creates a multi-byte string of length n in s from the
1268
    string t of kind k.
1382
    string t of kind k.
1269
*/
1383
*/
1270
 
1384
 
1271
static void make_multi_string
1385
static void
1272
    PROTO_N ( ( s, t, n, k ) )
-
 
1273
    PROTO_T ( string s X string t X unsigned long n X unsigned k )
1386
make_multi_string(string s, string t, unsigned long n, unsigned k)
1274
{
1387
{
1275
    if ( k & STRING_MULTI ) {
1388
	if (k & STRING_MULTI) {
1276
	n *= MULTI_WIDTH ;
1389
		n *= MULTI_WIDTH;
1277
	xumemcpy ( s, t, ( gen_size ) n ) ;
1390
		xumemcpy(s, t, (gen_size)n);
1278
    } else {
1391
	} else {
1279
	unsigned long i ;
1392
		unsigned long i;
1280
	for ( i = 0 ; i < n ; i++ ) {
1393
		for (i = 0; i < n; i++) {
1281
	    add_multi_char ( s, ( unsigned long ) *t, CHAR_SIMPLE ) ;
1394
			add_multi_char(s, (unsigned long)*t, CHAR_SIMPLE);
1282
	    s += MULTI_WIDTH ;
1395
			s += MULTI_WIDTH;
1283
	    t++ ;
1396
			t++;
-
 
1397
		}
1284
	}
1398
	}
1285
    }
-
 
1286
    return ;
1399
	return;
1287
}
1400
}
1288
 
1401
 
1289
 
1402
 
1290
/*
1403
/*
1291
    GET A MULTIBYTE CHARACTER FROM A STRING
1404
    GET A MULTIBYTE CHARACTER FROM A STRING
Line 1298... Line 1411...
1298
    that single byte.
1411
    that single byte.
1299
*/
1412
*/
1300
 
1413
 
1301
#if FS_MULTIBYTE
1414
#if FS_MULTIBYTE
1302
 
1415
 
1303
static string get_multibyte
1416
static string
1304
    PROTO_N ( ( s, se, pc ) )
-
 
1305
    PROTO_T ( string s X string se X unsigned long *pc )
1417
get_multibyte(string s, string se, unsigned long *pc)
1306
{
1418
{
1307
    wchar_t c ;
1419
	wchar_t c;
1308
    int n = mbtowc ( &c, s, ( size_t ) ( se - s ) ) ;
1420
	int n = mbtowc(&c, s, (size_t)(se - s));
1309
    if ( n > 0 ) {
1421
	if (n > 0) {
1310
	/* Valid multibyte character */
1422
		/* Valid multibyte character */
1311
	*pc = ( unsigned long ) c ;
1423
		*pc = (unsigned long)c;
1312
	s += n ;
1424
		s += n;
1313
    } else if ( n == 0 ) {
1425
	} else if (n == 0) {
1314
	/* Null character */
1426
		/* Null character */
1315
	*pc = 0 ;
1427
		*pc = 0;
1316
	s++ ;
1428
		s++;
1317
    } else {
1429
	} else {
1318
	/* Invalid multibyte character */
1430
		/* Invalid multibyte character */
1319
	report ( crt_loc, ERR_lex_ccon_multibyte () ) ;
1431
		report(crt_loc, ERR_lex_ccon_multibyte());
1320
	*pc = ( unsigned long ) *( s++ ) ;
1432
		*pc = (unsigned long)*(s++);
1321
    }
1433
	}
1322
    return ( s ) ;
1434
	return(s);
1323
}
1435
}
1324
 
1436
 
1325
#endif
1437
#endif
1326
 
1438
 
1327
 
1439
 
1328
/*
1440
/*
1329
    ANALYSE A STRING OR CHARACTER LITERAL
1441
    ANALYSE A STRING OR CHARACTER LITERAL
1330
 
1442
 
1331
    This routine analyses the string or character literal given by the
1443
    This routine analyses the string or character literal given by the
1332
    string s (which ends at se).  Only characters in the range [0,0xff]
1444
    string s (which ends at se).  Only characters in the range [0, 0xff]
1333
    are assumed to be valid. Note that this is the routine which should
1445
    are assumed to be valid. Note that this is the routine which should
1334
    do the mapping from the source character set to the execution
1446
    do the mapping from the source character set to the execution
1335
    character set (translation phase 5), however this is deferred until
1447
    character set (translation phase 5), however this is deferred until
1336
    the string output routines.
1448
    the string output routines.
1337
*/
1449
*/
1338
 
1450
 
1339
STRING new_string_lit
1451
STRING
1340
    PROTO_N ( ( s, se, lex ) )
-
 
1341
    PROTO_T ( string s X string se X int lex )
1452
new_string_lit(string s, string se, int lex)
1342
{
1453
{
1343
    STRING res ;
1454
	STRING res;
1344
    STRING prev ;
1455
	STRING prev;
1345
    int multi = 0 ;
1456
	int multi = 0;
1346
    int overflow = 0 ;
1457
	int overflow = 0;
1347
    unsigned long len = 0 ;
1458
	unsigned long len = 0;
1348
    unsigned kind = STRING_NONE ;
1459
	unsigned kind = STRING_NONE;
1349
#if FS_MULTIBYTE
1460
#if FS_MULTIBYTE
1350
    int multibyte = allow_multibyte ;
1461
	int multibyte = allow_multibyte;
1351
#endif
1462
#endif
1352
    gen_size sz = ( gen_size ) ( se - s ) + 1 ;
1463
	gen_size sz = (gen_size)(se - s) + 1;
1353
    string str = xustr ( sz ) ;
1464
	string str = xustr(sz);
1354
 
1465
 
1355
    /* Find string type */
1466
	/* Find string type */
1356
    switch ( lex ) {
1467
	switch (lex) {
1357
	case lex_char_Hlit :
1468
	case lex_char_Hlit:
1358
	case lex_char_Hexp : {
1469
	case lex_char_Hexp:
1359
	    kind = STRING_CHAR ;
1470
		kind = STRING_CHAR;
1360
	    break ;
1471
		break;
1361
	}
-
 
1362
	case lex_wchar_Hlit :
1472
	case lex_wchar_Hlit:
1363
	case lex_wchar_Hexp : {
1473
	case lex_wchar_Hexp:
1364
	    kind = ( STRING_WIDE | STRING_CHAR ) ;
1474
		kind = (STRING_WIDE | STRING_CHAR);
1365
	    break ;
1475
		break;
-
 
1476
	case lex_string_Hlit:
-
 
1477
	case lex_string_Hexp:
-
 
1478
		kind = STRING_NONE;
1366
	}
1479
		break;
1367
	case lex_string_Hlit :
1480
	case lex_wstring_Hlit:
1368
	case lex_string_Hexp : {
1481
	case lex_wstring_Hexp:
1369
	    kind = STRING_NONE ;
1482
		kind = STRING_WIDE;
1370
	    break ;
1483
		break;
-
 
1484
	}
-
 
1485
	if (do_string) {
-
 
1486
		dump_string_lit(s, se, kind);
1371
	}
1487
	}
1372
	case lex_wstring_Hlit :
-
 
1373
	case lex_wstring_Hexp : {
-
 
1374
	    kind = STRING_WIDE ;
-
 
1375
	    break ;
-
 
1376
	}
-
 
1377
    }
-
 
1378
    if ( do_string ) dump_string_lit ( s, se, kind ) ;
-
 
1379
 
1488
 
1380
    /* Scan string replacing escape sequences */
1489
	/* Scan string replacing escape sequences */
1381
    while ( s != se ) {
1490
	while (s != se) {
1382
	unsigned long c ;
1491
		unsigned long c;
1383
	int ch = CHAR_SIMPLE ;
1492
		int ch = CHAR_SIMPLE;
1384
#if FS_MULTIBYTE
1493
#if FS_MULTIBYTE
1385
	if ( multibyte ) {
-
 
1386
	    s = get_multibyte ( s, se, &c ) ;
-
 
1387
	} else {
-
 
1388
	    c = ( unsigned long ) *( s++ ) ;
-
 
1389
	}
-
 
1390
#else
-
 
1391
	c = ( unsigned long ) *( s++ ) ;
-
 
1392
#endif
-
 
1393
	if ( c == char_backslash ) {
-
 
1394
	    if ( s != se ) {
-
 
1395
		/* Unterminated string literals already reported */
-
 
1396
		character e = NONE ;
-
 
1397
#if FS_MULTIBYTE
-
 
1398
		if ( multibyte ) {
1494
		if (multibyte) {
1399
		    s = get_multibyte ( s, se, &c ) ;
1495
			s = get_multibyte(s, se, &c);
1400
		} else {
1496
		} else {
1401
		    c = ( unsigned long ) *( s++ ) ;
1497
			c = (unsigned long)*(s++);
1402
		}
1498
		}
1403
#else
1499
#else
1404
		c = ( unsigned long ) *( s++ ) ;
1500
		c = (unsigned long)*(s++);
1405
#endif
1501
#endif
-
 
1502
		if (c == char_backslash) {
-
 
1503
			if (s != se) {
-
 
1504
				/* Unterminated string literals already
-
 
1505
				 * reported */
-
 
1506
				character e = NONE;
-
 
1507
#if FS_MULTIBYTE
-
 
1508
				if (multibyte) {
-
 
1509
					s = get_multibyte(s, se, &c);
-
 
1510
				} else {
-
 
1511
					c = (unsigned long)*(s++);
-
 
1512
				}
-
 
1513
#else
-
 
1514
				c = (unsigned long)*(s++);
-
 
1515
#endif
-
 
1516
				if (c < NO_CHAR) {
1406
		if ( c < NO_CHAR ) e = escape_sequences [c] ;
1517
					e = escape_sequences[c];
-
 
1518
				}
1407
		switch ( e ) {
1519
				switch (e) {
1408
 
1520
 
1409
		    case OCTE : {
1521
				case OCTE: {
1410
			/* Octal escape sequences */
1522
					/* Octal escape sequences */
1411
			unsigned base = 8 ;
1523
					unsigned base = 8;
1412
			string r = s - 1 ;
1524
					string r = s - 1;
1413
			s = check_digits ( r, base ) ;
1525
					s = check_digits(r, base);
1414
			if ( s > r + 3 ) s = r + 3 ;
1526
					if (s > r + 3) {
-
 
1527
						s = r + 3;
-
 
1528
					}
1415
			c = eval_char_digits ( r, s, base ) ;
1529
					c = eval_char_digits(r, s, base);
1416
			ch = CHAR_OCTAL ;
1530
					ch = CHAR_OCTAL;
1417
			break ;
1531
					break;
1418
		    }
1532
				}
1419
 
1533
 
1420
		    case HEXE : {
1534
				case HEXE: {
1421
			/* Hexadecimal escape sequences */
1535
					/* Hexadecimal escape sequences */
1422
			unsigned base = 16 ;
1536
					unsigned base = 16;
1423
			string r = s ;
1537
					string r = s;
1424
			s = check_digits ( r, base ) ;
1538
					s = check_digits(r, base);
1425
			if ( s == r ) {
1539
					if (s == r) {
1426
			    int i = ( int ) c ;
1540
						int i = (int)c;
-
 
1541
						report(crt_loc,
1427
			    report ( crt_loc, ERR_lex_ccon_hex ( i ) ) ;
1542
						       ERR_lex_ccon_hex(i));
1428
			} else {
1543
					} else {
1429
			    c = eval_char_digits ( r, s, base ) ;
1544
						c = eval_char_digits(r, s,
-
 
1545
								     base);
1430
			}
1546
					}
1431
			ch = CHAR_HEX ;
1547
					ch = CHAR_HEX;
1432
			break ;
1548
					break;
1433
		    }
1549
				}
1434
 
1550
 
1435
		    case UNI4 : {
1551
				case UNI4: {
1436
			/* Short unicode escape sequences */
1552
					/* Short unicode escape sequences */
1437
			if ( allow_unicodes ) {
1553
					if (allow_unicodes) {
1438
			    string r = s ;
1554
						string r = s;
1439
			    unsigned d = 4 ;
1555
						unsigned d = 4;
1440
			    ERROR err = NULL_err ;
1556
						ERROR err = NULL_err;
1441
			    c = eval_unicode ( char_u, d, &ch, &r, &err ) ;
1557
						c = eval_unicode(char_u, d, &ch,
-
 
1558
								 &r, &err);
1442
			    if ( !IS_NULL_err ( err ) ) {
1559
						if (!IS_NULL_err(err)) {
1443
				report ( crt_loc, err ) ;
1560
							report(crt_loc, err);
1444
			    }
1561
						}
1445
			    ch = CHAR_UNI4 ;
1562
						ch = CHAR_UNI4;
1446
			    s = r ;
1563
						s = r;
1447
			    break ;
1564
						break;
1448
			}
1565
					}
1449
			goto illegal_lab ;
1566
					goto illegal_lab;
1450
		    }
1567
				}
1451
 
1568
 
1452
		    case UNI8 : {
1569
				case UNI8: {
1453
			/* Long unicode escape sequences */
1570
					/* Long unicode escape sequences */
1454
			if ( allow_unicodes ) {
1571
					if (allow_unicodes) {
1455
			    string r = s ;
1572
						string r = s;
1456
			    unsigned d = 8 ;
1573
						unsigned d = 8;
1457
			    ERROR err = NULL_err ;
1574
						ERROR err = NULL_err;
1458
			    c = eval_unicode ( char_U, d, &ch, &r, &err ) ;
1575
						c = eval_unicode(char_U, d, &ch,
-
 
1576
								 &r, &err);
1459
			    if ( !IS_NULL_err ( err ) ) {
1577
						if (!IS_NULL_err(err)) {
1460
				report ( crt_loc, err ) ;
1578
							report(crt_loc, err);
1461
			    }
1579
						}
1462
			    ch = CHAR_UNI8 ;
1580
						ch = CHAR_UNI8;
1463
			    s = r ;
1581
						s = r;
1464
			    break ;
1582
						break;
1465
			}
1583
					}
1466
			goto illegal_lab ;
1584
					goto illegal_lab;
1467
		    }
1585
				}
1468
 
1586
 
1469
		    case NONE :
1587
				case NONE:
1470
		    illegal_lab : {
1588
illegal_lab: {
1471
			/* Illegal escape sequences */
1589
					/* Illegal escape sequences */
1472
			int i = ( int ) c ;
1590
		     			int i = (int)c;
1473
			report ( crt_loc, ERR_lex_ccon_escape ( i ) ) ;
1591
					report(crt_loc, ERR_lex_ccon_escape(i));
1474
			break ;
1592
					break;
1475
		    }
1593
	     }
1476
 
1594
 
1477
		    default : {
1595
				default:
1478
			/* Simple escape sequences */
1596
					/* Simple escape sequences */
1479
			c = ( unsigned long ) e ;
1597
					c = (unsigned long)e;
1480
			break ;
1598
					break;
-
 
1599
				}
-
 
1600
			}
-
 
1601
		}
-
 
1602
		if ((ch != CHAR_SIMPLE || c >= 256) && !multi) {
-
 
1603
			/* Convert to multi-character format */
-
 
1604
			string a;
-
 
1605
			sz *= MULTI_WIDTH;
-
 
1606
			a = xustr(sz);
-
 
1607
			make_multi_string(a, str, len, kind);
-
 
1608
			if (len) {
-
 
1609
				len *= MULTI_WIDTH;
-
 
1610
				if (len == 0) {
-
 
1611
					overflow = 1;
-
 
1612
				}
-
 
1613
			}
-
 
1614
			if (c >= 256) {
-
 
1615
				/* Mark fat strings */
-
 
1616
				if (!(kind & STRING_WIDE)) {
-
 
1617
					if (ch == CHAR_UNI4 ||
-
 
1618
					    ch == CHAR_UNI8) {
-
 
1619
						/* EMPTY */
-
 
1620
					} else {
-
 
1621
						report(crt_loc,
-
 
1622
						       ERR_lex_ccon_large());
1481
		    }
1623
					}
-
 
1624
				}
-
 
1625
				kind |= STRING_FAT;
-
 
1626
			}
-
 
1627
			kind |= STRING_MULTI;
-
 
1628
			multi = 1;
-
 
1629
			str = a;
-
 
1630
		}
-
 
1631
		if (multi) {
-
 
1632
			add_multi_char(str + len, c, ch);
-
 
1633
			len += MULTI_WIDTH;
-
 
1634
		} else {
-
 
1635
			str[len++] = (character)c;
-
 
1636
		}
-
 
1637
		if (len == 0) {
-
 
1638
			overflow = 1;
1482
		}
1639
		}
1483
	    }
-
 
1484
	}
-
 
1485
	if ( ( ch != CHAR_SIMPLE || c >= 256 ) && !multi ) {
-
 
1486
	    /* Convert to multi-character format */
-
 
1487
	    string a ;
-
 
1488
	    sz *= MULTI_WIDTH ;
-
 
1489
	    a = xustr ( sz ) ;
-
 
1490
	    make_multi_string ( a, str, len, kind ) ;
-
 
1491
	    if ( len ) {
-
 
1492
		len *= MULTI_WIDTH ;
-
 
1493
		if ( len == 0 ) overflow = 1 ;
-
 
1494
	    }
-
 
1495
	    if ( c >= 256 ) {
-
 
1496
		/* Mark fat strings */
-
 
1497
		if ( !( kind & STRING_WIDE ) ) {
-
 
1498
		    if ( ch == CHAR_UNI4 || ch == CHAR_UNI8 ) {
-
 
1499
			/* EMPTY */
-
 
1500
		    } else {
-
 
1501
			report ( crt_loc, ERR_lex_ccon_large () ) ;
-
 
1502
		    }
-
 
1503
		}
-
 
1504
		kind |= STRING_FAT ;
-
 
1505
	    }
-
 
1506
	    kind |= STRING_MULTI ;
-
 
1507
	    multi = 1 ;
-
 
1508
	    str = a ;
-
 
1509
	}
-
 
1510
	if ( multi ) {
-
 
1511
	    add_multi_char ( str + len, c, ch ) ;
-
 
1512
	    len += MULTI_WIDTH ;
-
 
1513
	} else {
-
 
1514
	    str [ len++ ] = ( character ) c ;
-
 
1515
	}
1640
	}
1516
	if ( len == 0 ) overflow = 1 ;
-
 
1517
    }
-
 
1518
    if ( multi ) {
1641
	if (multi) {
1519
	add_multi_char ( str + len, ( unsigned long ) 0, CHAR_OCTAL ) ;
1642
		add_multi_char(str + len, (unsigned long)0, CHAR_OCTAL);
1520
	len /= MULTI_WIDTH ;
1643
		len /= MULTI_WIDTH;
1521
    } else {
-
 
1522
	str [ len ] = 0 ;
-
 
1523
    }
-
 
1524
    if ( overflow ) len = ULONG_MAX ;
-
 
1525
    if ( !check_value ( OPT_VAL_string_length, len ) ) {
-
 
1526
	len = option_value ( OPT_VAL_string_length ) ;
-
 
1527
	if ( multi ) {
-
 
1528
	    unsigned long n = MULTI_WIDTH * len ;
-
 
1529
	    add_multi_char ( str + n, ( unsigned long ) 0, CHAR_OCTAL ) ;
-
 
1530
	} else {
1644
	} else {
1531
	    str [ len ] = 0 ;
1645
		str[len] = 0;
1532
	}
1646
	}
-
 
1647
	if (overflow) {
-
 
1648
		len = ULONG_MAX;
-
 
1649
	}
-
 
1650
	if (!check_value(OPT_VAL_string_length, len)) {
-
 
1651
		len = option_value(OPT_VAL_string_length);
-
 
1652
		if (multi) {
-
 
1653
			unsigned long n = MULTI_WIDTH * len;
-
 
1654
			add_multi_char(str + n, (unsigned long)0, CHAR_OCTAL);
-
 
1655
		} else {
-
 
1656
			str[len] = 0;
1533
    }
1657
		}
-
 
1658
	}
1534
    MAKE_str_simple ( len, str, kind, res ) ;
1659
	MAKE_str_simple(len, str, kind, res);
1535
    prev = share_string_lit ( res ) ;
1660
	prev = share_string_lit(res);
1536
    if ( !EQ_str ( prev, res ) ) {
1661
	if (!EQ_str(prev, res)) {
1537
	/* Share string literals */
1662
		/* Share string literals */
1538
	unsigned long v ;
1663
		unsigned long v;
1539
	DESTROY_str_simple ( destroy, res, len, str, kind, v, res ) ;
1664
		DESTROY_str_simple(destroy, res, len, str, kind, v, res);
1540
	xufree ( str, sz ) ;
1665
		xufree(str, sz);
1541
	UNUSED ( res ) ;
1666
		UNUSED(res);
1542
	UNUSED ( len ) ;
1667
		UNUSED(len);
1543
	UNUSED ( kind ) ;
1668
		UNUSED(kind);
1544
	UNUSED ( v ) ;
1669
		UNUSED(v);
1545
	res = prev ;
1670
		res = prev;
1546
    }
1671
	}
1547
    return ( res ) ;
1672
	return(res);
1548
}
1673
}
1549
 
1674
 
1550
 
1675
 
1551
/*
1676
/*
1552
    ARE TWO STRINGS EQUAL?
1677
    ARE TWO STRINGS EQUAL?
1553
 
1678
 
1554
    This routine checks whether the string literals s and t are equal.
1679
    This routine checks whether the string literals s and t are equal.
1555
*/
1680
*/
1556
 
1681
 
1557
int eq_string_lit
1682
int
1558
    PROTO_N ( ( s, t ) )
-
 
1559
    PROTO_T ( STRING s X STRING t )
1683
eq_string_lit(STRING s, STRING t)
1560
{
1684
{
1561
    string as, at ;
1685
	string as, at;
1562
    unsigned ks, kt ;
1686
	unsigned ks, kt;
1563
    unsigned long ns, nt ;
1687
	unsigned long ns, nt;
1564
    if ( EQ_str ( s, t ) ) return ( 1 ) ;
1688
	if (EQ_str(s, t)) {
-
 
1689
		return(1);
-
 
1690
	}
1565
    ks = DEREF_unsigned ( str_simple_kind ( s ) ) ;
1691
	ks = DEREF_unsigned(str_simple_kind(s));
1566
    kt = DEREF_unsigned ( str_simple_kind ( t ) ) ;
1692
	kt = DEREF_unsigned(str_simple_kind(t));
1567
    ns = DEREF_ulong ( str_simple_len ( s ) ) ;
1693
	ns = DEREF_ulong(str_simple_len(s));
1568
    nt = DEREF_ulong ( str_simple_len ( t ) ) ;
1694
	nt = DEREF_ulong(str_simple_len(t));
1569
    if ( ks == kt && ns == nt ) {
1695
	if (ks == kt && ns == nt) {
1570
	as = DEREF_string ( str_simple_text ( s ) ) ;
1696
		as = DEREF_string(str_simple_text(s));
1571
	at = DEREF_string ( str_simple_text ( t ) ) ;
1697
		at = DEREF_string(str_simple_text(t));
1572
	if ( as == at ) return ( 1 ) ;
1698
		if (as == at) {
-
 
1699
			return(1);
-
 
1700
		}
1573
	if ( ks & STRING_MULTI ) ns *= MULTI_WIDTH ;
1701
		if (ks & STRING_MULTI) {
-
 
1702
			ns *= MULTI_WIDTH;
-
 
1703
		}
1574
	if ( xumemcmp ( as, at, ( gen_size ) ns ) == 0 ) return ( 1 ) ;
1704
		if (xumemcmp(as, at, (gen_size)ns) == 0) {
-
 
1705
			return(1);
1575
    }
1706
		}
-
 
1707
	}
1576
    return ( 0 ) ;
1708
	return(0);
1577
}
1709
}
1578
 
1710
 
1579
 
1711
 
1580
/*
1712
/*
1581
    CONCATENATE TWO STRING LITERALS
1713
    CONCATENATE TWO STRING LITERALS
1582
 
1714
 
1583
    This routine concatenates the string literals s and t.
1715
    This routine concatenates the string literals s and t.
1584
*/
1716
*/
1585
 
-
 
1586
STRING concat_string_lit
-
 
1587
    PROTO_N ( ( s, t ) )
-
 
1588
    PROTO_T ( STRING s X STRING t )
-
 
1589
{
-
 
1590
    string c ;
-
 
1591
    STRING res ;
-
 
1592
    STRING prev ;
-
 
1593
    unsigned kc ;
-
 
1594
    gen_size sz ;
-
 
1595
    unsigned long nc ;
-
 
1596
    string a = DEREF_string ( str_simple_text ( s ) ) ;
-
 
1597
    string b = DEREF_string ( str_simple_text ( t ) ) ;
-
 
1598
    unsigned ka = DEREF_unsigned ( str_simple_kind ( s ) ) ;
-
 
1599
    unsigned kb = DEREF_unsigned ( str_simple_kind ( t ) ) ;
-
 
1600
    unsigned long na = DEREF_ulong ( str_simple_len ( s ) ) ;
-
 
1601
    unsigned long nb = DEREF_ulong ( str_simple_len ( t ) ) ;
-
 
1602
 
1717
 
-
 
1718
STRING
-
 
1719
concat_string_lit(STRING s, STRING t)
-
 
1720
{
-
 
1721
	string c;
-
 
1722
	STRING res;
-
 
1723
	STRING prev;
-
 
1724
	unsigned kc;
-
 
1725
	gen_size sz;
-
 
1726
	unsigned long nc;
-
 
1727
	string a = DEREF_string(str_simple_text(s));
-
 
1728
	string b = DEREF_string(str_simple_text(t));
-
 
1729
	unsigned ka = DEREF_unsigned(str_simple_kind(s));
-
 
1730
	unsigned kb = DEREF_unsigned(str_simple_kind(t));
-
 
1731
	unsigned long na = DEREF_ulong(str_simple_len(s));
-
 
1732
	unsigned long nb = DEREF_ulong(str_simple_len(t));
-
 
1733
 
1603
    /* Form the result literal */
1734
	/* Form the result literal */
1604
    if ( na == 0 ) return ( t ) ;
1735
	if (na == 0) {
-
 
1736
		return(t);
-
 
1737
	}
1605
    if ( nb == 0 ) return ( s ) ;
1738
	if (nb == 0) {
-
 
1739
		return(s);
-
 
1740
	}
1606
    nc = na + nb ;
1741
	nc = na + nb;
1607
    if ( nc < na || nc < nb ) nc = ULONG_MAX ;
1742
	if (nc < na || nc < nb) {
-
 
1743
		nc = ULONG_MAX;
-
 
1744
	}
1608
    if ( !check_value ( OPT_VAL_string_length, nc ) ) {
1745
	if (!check_value(OPT_VAL_string_length, nc)) {
1609
	nc = option_value ( OPT_VAL_string_length ) ;
1746
		nc = option_value(OPT_VAL_string_length);
1610
	nb = nc - na ;
1747
		nb = nc - na;
1611
    }
1748
	}
1612
    kc = ( ka | kb ) ;
1749
	kc = (ka | kb);
1613
    if ( kc & STRING_MULTI ) {
1750
	if (kc & STRING_MULTI) {
1614
	/* Multi-byte strings */
1751
		/* Multi-byte strings */
1615
	unsigned long sa = MULTI_WIDTH * na ;
1752
		unsigned long sa = MULTI_WIDTH * na;
1616
	unsigned long sc = MULTI_WIDTH * nc ;
1753
		unsigned long sc = MULTI_WIDTH * nc;
1617
	sz = ( gen_size ) ( sc + MULTI_WIDTH ) ;
1754
		sz = (gen_size)(sc + MULTI_WIDTH);
1618
	c = xustr ( sz ) ;
1755
		c = xustr(sz);
1619
	make_multi_string ( c, a, na, ka ) ;
1756
		make_multi_string(c, a, na, ka);
1620
	make_multi_string ( c + sa, b, nb, kb ) ;
1757
		make_multi_string(c + sa, b, nb, kb);
1621
	add_multi_char ( c + sc, ( unsigned long ) 0, CHAR_OCTAL ) ;
1758
		add_multi_char(c + sc, (unsigned long)0, CHAR_OCTAL);
1622
    } else {
1759
	} else {
1623
	/* Simple strings */
1760
		/* Simple strings */
1624
	sz = ( gen_size ) ( nc + 1 ) ;
1761
		sz = (gen_size)(nc + 1);
1625
	c = xustr ( sz ) ;
1762
		c = xustr(sz);
1626
	xumemcpy ( c, a, ( gen_size ) na ) ;
1763
		xumemcpy(c, a, (gen_size)na);
1627
	xumemcpy ( c + na, b, ( gen_size ) nb ) ;
1764
		xumemcpy(c + na, b, (gen_size)nb);
1628
	c [ nc ] = 0 ;
1765
		c[nc] = 0;
1629
    }
1766
	}
1630
    MAKE_str_simple ( nc, c, kc, res ) ;
1767
	MAKE_str_simple(nc, c, kc, res);
1631
    prev = share_string_lit ( res ) ;
1768
	prev = share_string_lit(res);
1632
    if ( !EQ_str ( prev, res ) ) {
1769
	if (!EQ_str(prev, res)) {
1633
	/* Share string literals */
1770
		/* Share string literals */
1634
	unsigned long v ;
1771
		unsigned long v;
1635
	DESTROY_str_simple ( destroy, res, nc, c, kc, v, res ) ;
1772
		DESTROY_str_simple(destroy, res, nc, c, kc, v, res);
1636
	xufree ( c, sz ) ;
1773
		xufree(c, sz);
1637
	UNUSED ( res ) ;
1774
		UNUSED(res);
1638
	UNUSED ( nc ) ;
1775
		UNUSED(nc);
1639
	UNUSED ( kc ) ;
1776
		UNUSED(kc);
1640
	UNUSED ( v ) ;
1777
		UNUSED(v);
1641
	res = prev ;
1778
		res = prev;
1642
    }
1779
	}
1643
    return ( res ) ;
1780
	return(res);
1644
}
1781
}
1645
 
1782
 
1646
 
1783
 
1647
/*
1784
/*
1648
    FIND THE SHARED COPY OF A STRING LITERAL
1785
    FIND THE SHARED COPY OF A STRING LITERAL
1649
 
1786
 
1650
    This routine is used to implement shared string literals.  It returns
1787
    This routine is used to implement shared string literals.  It returns
1651
    the canonical copy of s (i.e. the first string equal to s for which
1788
    the canonical copy of s (i.e. the first string equal to s for which
1652
    the routine was called).
1789
    the routine was called).
1653
*/
1790
*/
1654
 
1791
 
1655
STRING share_string_lit
1792
STRING
1656
    PROTO_N ( ( s ) )
-
 
1657
    PROTO_T ( STRING s )
1793
share_string_lit(STRING s)
1658
{
1794
{
1659
    string a = DEREF_string ( str_simple_text ( s ) ) ;
1795
	string a = DEREF_string(str_simple_text(s));
1660
    unsigned long h = ( hash ( a ) % HASH_STRING_SIZE ) ;
1796
	unsigned long h = (hash(a)% HASH_STRING_SIZE);
1661
    STRING p = string_hash_table [h] ;
1797
	STRING p = string_hash_table[h];
1662
    STRING t = p ;
1798
	STRING t = p;
1663
    while ( !IS_NULL_str ( t ) ) {
1799
	while (!IS_NULL_str(t)) {
1664
	if ( eq_string_lit ( t, s ) ) return ( t ) ;
1800
		if (eq_string_lit(t, s)) {
-
 
1801
			return(t);
-
 
1802
		}
1665
	t = DEREF_str ( str_next ( t ) ) ;
1803
		t = DEREF_str(str_next(t));
1666
    }
1804
	}
1667
    COPY_str ( str_next ( s ), p ) ;
1805
	COPY_str(str_next(s), p);
1668
    string_hash_table [h] = s ;
1806
	string_hash_table[h] = s;
1669
    return ( s ) ;
1807
	return(s);
1670
}
1808
}
1671
 
1809
 
1672
 
1810
 
1673
/*
1811
/*
1674
    GET THE NEXT CHARACTER FROM A STRING
1812
    GET THE NEXT CHARACTER FROM A STRING
Line 1676... Line 1814...
1676
    This routine returns the next character from the string s, using
1814
    This routine returns the next character from the string s, using
1677
    the tok field as a counter.  The character type is assigned to pc,
1815
    the tok field as a counter.  The character type is assigned to pc,
1678
    including CHAR_NONE to indicate the end of the string.
1816
    including CHAR_NONE to indicate the end of the string.
1679
*/
1817
*/
1680
 
1818
 
1681
unsigned long get_string_char
1819
unsigned long
1682
    PROTO_N ( ( s, pc ) )
-
 
1683
    PROTO_T ( STRING s X int *pc )
1820
get_string_char(STRING s, int *pc)
1684
{
1821
{
1685
    unsigned long c ;
1822
	unsigned long c;
1686
    unsigned long i = DEREF_ulong ( str_simple_tok ( s ) ) ;
1823
	unsigned long i = DEREF_ulong(str_simple_tok(s));
1687
    unsigned long n = DEREF_ulong ( str_simple_len ( s ) ) ;
1824
	unsigned long n = DEREF_ulong(str_simple_len(s));
1688
    if ( i < n ) {
1825
	if (i < n) {
1689
	string text = DEREF_string ( str_simple_text ( s ) ) ;
1826
		string text = DEREF_string(str_simple_text(s));
1690
	unsigned kind = DEREF_unsigned ( str_simple_kind ( s ) ) ;
1827
		unsigned kind = DEREF_unsigned(str_simple_kind(s));
1691
	if ( kind & STRING_MULTI ) {
1828
		if (kind & STRING_MULTI) {
1692
	    c = get_multi_char ( text + MULTI_WIDTH * i, pc ) ;
1829
			c = get_multi_char(text + MULTI_WIDTH * i, pc);
-
 
1830
		} else {
-
 
1831
			c = (unsigned long)text[i];
-
 
1832
			*pc = CHAR_SIMPLE;
-
 
1833
		}
1693
	} else {
1834
	} else {
1694
	    c = ( unsigned long ) text [i] ;
1835
		c = 0;
1695
	    *pc = CHAR_SIMPLE ;
1836
		*pc = CHAR_NONE;
1696
	}
1837
	}
1697
    } else {
-
 
1698
	c = 0 ;
-
 
1699
	*pc = CHAR_NONE ;
-
 
1700
    }
-
 
1701
    COPY_ulong ( str_simple_tok ( s ), i + 1 ) ;
1838
	COPY_ulong(str_simple_tok(s), i + 1);
1702
    return ( c ) ;
1839
	return(c);
1703
}
1840
}
1704
 
1841
 
1705
 
1842
 
1706
/*
1843
/*
1707
    FIND A CHARACTER LITERAL
1844
    FIND A CHARACTER LITERAL
1708
 
1845
 
1709
    This routine returns the character value corresponding to the character
1846
    This routine returns the character value corresponding to the character
1710
    literal expression e.
1847
    literal expression e.
1711
*/
1848
*/
1712
 
1849
 
1713
int get_char_value
1850
int
1714
    PROTO_N ( ( e ) )
-
 
1715
    PROTO_T ( EXP e )
1851
get_char_value(EXP e)
1716
{
1852
{
1717
    int c = char_illegal ;
1853
	int c = char_illegal;
1718
    if ( !IS_NULL_exp ( e ) ) {
1854
	if (!IS_NULL_exp(e)) {
1719
	if ( IS_exp_int_lit ( e ) ) {
1855
		if (IS_exp_int_lit(e)) {
1720
	    NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
1856
			NAT n = DEREF_nat(exp_int_lit_nat(e));
1721
	    if ( IS_nat_calc ( n ) ) {
1857
			if (IS_nat_calc(n)) {
1722
		e = DEREF_exp ( nat_calc_value ( n ) ) ;
1858
				e = DEREF_exp(nat_calc_value(n));
1723
	    }
1859
			}
1724
	}
1860
		}
1725
	if ( IS_exp_cast ( e ) ) {
1861
		if (IS_exp_cast(e)) {
1726
	    e = DEREF_exp ( exp_cast_arg ( e ) ) ;
1862
			e = DEREF_exp(exp_cast_arg(e));
1727
	    if ( IS_exp_int_lit ( e ) ) {
1863
			if (IS_exp_int_lit(e)) {
1728
		NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
1864
				NAT n = DEREF_nat(exp_int_lit_nat(e));
1729
		if ( IS_nat_calc ( n ) ) {
1865
				if (IS_nat_calc(n)) {
1730
		    e = DEREF_exp ( nat_calc_value ( n ) ) ;
1866
					e = DEREF_exp(nat_calc_value(n));
1731
		}
1867
				}
1732
	    }
1868
			}
1733
	}
1869
		}
1734
	if ( IS_exp_char_lit ( e ) ) {
1870
		if (IS_exp_char_lit(e)) {
1735
	    STRING s = DEREF_str ( exp_char_lit_str ( e ) ) ;
1871
			STRING s = DEREF_str(exp_char_lit_str(e));
1736
	    unsigned kind = DEREF_unsigned ( str_simple_kind ( s ) ) ;
1872
			unsigned kind = DEREF_unsigned(str_simple_kind(s));
1737
	    if ( !( kind & STRING_MULTI ) ) {
1873
			if (!(kind & STRING_MULTI)) {
-
 
1874
				unsigned long len =
1738
		unsigned long len = DEREF_ulong ( str_simple_len ( s ) ) ;
1875
				    DEREF_ulong(str_simple_len(s));
1739
		if ( len == 1 ) {
1876
				if (len == 1) {
-
 
1877
					string t =
1740
		    string t = DEREF_string ( str_simple_text ( s ) ) ;
1878
					    DEREF_string(str_simple_text(s));
1741
		    c = ( int ) *t ;
1879
					c = (int)*t;
1742
		}
1880
				}
1743
	    }
1881
			}
1744
	}
1882
		}
1745
    }
1883
	}
1746
    return ( c ) ;
1884
	return(c);
1747
}
1885
}
1748
 
1886
 
1749
 
1887
 
1750
/*
1888
/*
1751
    EVALUATE A CHARACTER LITERAL
1889
    EVALUATE A CHARACTER LITERAL
1752
 
1890
 
1753
    This routine evaluates the character literal str by mapping it to
1891
    This routine evaluates the character literal str by mapping it to
1754
    its ASCII representation.  The value is stored in the tok field
1892
    its ASCII representation.  The value is stored in the tok field
1755
    (the fact that LINK_NONE equals EXTENDED_MAX is convenient, but not
1893
    (the fact that LINK_NONE equals EXTENDED_MAX is convenient, but not
1756
    essential).
1894
    essential).
1757
*/
1895
*/
1758
 
1896
 
1759
NAT eval_char_lit
1897
NAT
1760
    PROTO_N ( ( str ) )
-
 
1761
    PROTO_T ( STRING str )
1898
eval_char_lit(STRING str)
1762
{
1899
{
1763
    NAT n ;
1900
	NAT n;
1764
    unsigned long v = DEREF_ulong ( str_simple_tok ( str ) ) ;
1901
	unsigned long v = DEREF_ulong(str_simple_tok(str));
1765
    if ( v == LINK_NONE ) {
1902
	if (v == LINK_NONE) {
1766
	unsigned long i ;
1903
		unsigned long i;
1767
	string s = DEREF_string ( str_simple_text ( str ) ) ;
1904
		string s = DEREF_string(str_simple_text(str));
1768
	unsigned long len = DEREF_ulong ( str_simple_len ( str ) ) ;
1905
		unsigned long len = DEREF_ulong(str_simple_len(str));
1769
	unsigned kind = DEREF_unsigned ( str_simple_kind ( str ) ) ;
1906
		unsigned kind = DEREF_unsigned(str_simple_kind(str));
1770
	if ( kind & STRING_MULTI ) {
1907
		if (kind & STRING_MULTI) {
1771
	    NAT b = make_small_nat ( 256 ) ;
1908
			NAT b = make_small_nat(256);
1772
	    n = small_nat [0] ;
1909
			n = small_nat[0];
1773
	    for ( i = 0 ; i < len ; i++ ) {
1910
			for (i = 0; i < len; i++) {
1774
		NAT d ;
1911
				NAT d;
1775
		int ch = CHAR_SIMPLE ;
1912
				int ch = CHAR_SIMPLE;
1776
		unsigned long c = get_multi_char ( s, &ch ) ;
1913
				unsigned long c = get_multi_char(s, &ch);
1777
		if ( ch == CHAR_SIMPLE ) c = to_ascii ( c, &ch ) ;
1914
				if (ch == CHAR_SIMPLE) {
-
 
1915
					c = to_ascii(c, &ch);
-
 
1916
				}
1778
		d = make_nat_value ( c ) ;
1917
				d = make_nat_value(c);
1779
		n = binary_nat_op ( exp_mult_tag, n, b ) ;
1918
				n = binary_nat_op(exp_mult_tag, n, b);
1780
		n = binary_nat_op ( exp_plus_tag, n, d ) ;
1919
				n = binary_nat_op(exp_plus_tag, n, d);
1781
		s += MULTI_WIDTH ;
1920
				s += MULTI_WIDTH;
-
 
1921
			}
-
 
1922
		} else {
-
 
1923
			n = small_nat[0];
-
 
1924
			for (i = 0; i < len; i++) {
-
 
1925
				int ch = CHAR_SIMPLE;
-
 
1926
				unsigned long c = (unsigned long)*s;
-
 
1927
				c = to_ascii(c, &ch);
-
 
1928
				n = make_nat_literal(n, (unsigned)256,
-
 
1929
						     (unsigned)c);
-
 
1930
				s++;
1782
	    }
1931
			}
-
 
1932
		}
-
 
1933
		v = get_nat_value(n);
-
 
1934
		if (v != EXTENDED_MAX) {
-
 
1935
			/* Store calculated value */
-
 
1936
			COPY_ulong(str_simple_tok(str), v);
-
 
1937
		}
1783
	} else {
1938
	} else {
1784
	    n = small_nat [0] ;
-
 
1785
	    for ( i = 0 ; i < len ; i++ ) {
-
 
1786
		int ch = CHAR_SIMPLE ;
-
 
1787
		unsigned long c = ( unsigned long ) *s ;
-
 
1788
		c = to_ascii ( c, &ch ) ;
1939
		/* Use stored value */
1789
		n = make_nat_literal ( n, ( unsigned ) 256, ( unsigned ) c ) ;
-
 
1790
		s++ ;
-
 
1791
	    }
-
 
1792
	}
-
 
1793
	v = get_nat_value ( n ) ;
1940
		n = make_nat_value(v);
1794
	if ( v != EXTENDED_MAX ) {
-
 
1795
	    /* Store calculated value */
-
 
1796
	    COPY_ulong ( str_simple_tok ( str ), v ) ;
-
 
1797
	}
1941
	}
1798
    } else {
-
 
1799
	/* Use stored value */
-
 
1800
	n = make_nat_value ( v ) ;
-
 
1801
    }
-
 
1802
    return ( n ) ;
1942
	return(n);
1803
}
1943
}
1804
 
1944
 
1805
 
1945
 
1806
/*
1946
/*
1807
    FIND A CHARACTER REPRESENTATION TYPE
1947
    FIND A CHARACTER REPRESENTATION TYPE
Line 1809... Line 1949...
1809
    In the case where a character literal type doesn't fit into its type
1949
    In the case where a character literal type doesn't fit into its type
1810
    then this routine gives a type in which the literal value can be
1950
    then this routine gives a type in which the literal value can be
1811
    constructed and then converted into its underlying type.
1951
    constructed and then converted into its underlying type.
1812
*/
1952
*/
1813
 
1953
 
1814
TYPE find_char_type
1954
TYPE
1815
    PROTO_N ( ( n ) )
-
 
1816
    PROTO_T ( NAT n )
1955
find_char_type(NAT n)
1817
{
1956
{
1818
    TYPE t ;
1957
	TYPE t;
1819
    int fit = 0 ;
1958
	int fit = 0;
1820
    string str = NULL_string ;
1959
	string str = NULL_string;
1821
    t = find_literal_type ( n, BASE_OCTAL, SUFFIX_NONE, str, &fit ) ;
1960
	t = find_literal_type(n, BASE_OCTAL, SUFFIX_NONE, str, &fit);
1822
    return ( t ) ;
1961
	return(t);
1823
}
1962
}
1824
 
1963
 
1825
 
1964
 
1826
/*
1965
/*
1827
    CREATE A STRING OR CHARACTER LITERAL EXPRESSION
1966
    CREATE A STRING OR CHARACTER LITERAL EXPRESSION
Line 1831... Line 1970...
1831
    (where it is a char cast to an int) and C++ (where it stays as a
1970
    (where it is a char cast to an int) and C++ (where it stays as a
1832
    char), and also that a string, or wide string, literal is an lvalue
1971
    char), and also that a string, or wide string, literal is an lvalue
1833
    of array type.
1972
    of array type.
1834
*/
1973
*/
1835
 
1974
 
1836
EXP make_string_exp
1975
EXP
1837
    PROTO_N ( ( s ) )
-
 
1838
    PROTO_T ( STRING s )
1976
make_string_exp(STRING s)
1839
{
1977
{
1840
    EXP e ;
1978
	EXP e;
1841
    string text = DEREF_string ( str_simple_text ( s ) ) ;
1979
	string text = DEREF_string(str_simple_text(s));
1842
    unsigned long len = DEREF_ulong ( str_simple_len ( s ) ) ;
1980
	unsigned long len = DEREF_ulong(str_simple_len(s));
1843
    unsigned kind = DEREF_unsigned ( str_simple_kind ( s ) ) ;
1981
	unsigned kind = DEREF_unsigned(str_simple_kind(s));
1844
 
1982
 
1845
    if ( kind & STRING_CHAR ) {
1983
	if (kind & STRING_CHAR) {
1846
	int fits = 0 ;
1984
		int fits = 0;
1847
	int digit = -1 ;
1985
		int digit = -1;
1848
	TYPE t0, t1, t2 ;
1986
		TYPE t0, t1, t2;
1849
	NAT n = NULL_nat ;
1987
		NAT n = NULL_nat;
1850
	ERROR err = NULL_err ;
1988
		ERROR err = NULL_err;
1851
	if ( kind & STRING_WIDE ) {
1989
		if (kind & STRING_WIDE) {
1852
	    t0 = type_wchar_lit ;
1990
			t0 = type_wchar_lit;
1853
	    t1 = t0 ;
1991
			t1 = t0;
1854
	    t2 = t0 ;
1992
			t2 = t0;
1855
	} else if ( len <= 1 ) {
1993
		} else if (len <= 1) {
1856
	    t0 = type_char ;
1994
			t0 = type_char;
1857
	    t1 = t0 ;
1995
			t1 = t0;
1858
	    t2 = type_char_lit ;
1996
			t2 = type_char_lit;
1859
	} else {
1997
		} else {
1860
	    report ( crt_loc, ERR_lex_ccon_multi ( s ) ) ;
1998
			report(crt_loc, ERR_lex_ccon_multi(s));
1861
	    t0 = type_mchar_lit ;
1999
			t0 = type_mchar_lit;
1862
	    t1 = t0 ;
2000
			t1 = t0;
1863
	    t2 = t0 ;
2001
			t2 = t0;
1864
	}
2002
		}
1865
	if ( len == 0 ) {
2003
		if (len == 0) {
1866
	    fits = 1 ;
2004
			fits = 1;
1867
	    n = small_nat [0] ;
2005
			n = small_nat[0];
1868
	    COPY_ulong ( str_simple_tok ( s ), 0 ) ;
2006
			COPY_ulong(str_simple_tok(s), 0);
1869
	} else if ( len == 1 ) {
2007
		} else if (len == 1) {
1870
	    if ( kind & STRING_MULTI ) {
2008
			if (kind & STRING_MULTI) {
1871
		if ( !( kind & STRING_FAT ) ) {
2009
				if (!(kind & STRING_FAT)) {
1872
		    /* Simple octal or hex escape sequence */
2010
					/* Simple octal or hex escape
-
 
2011
					 * sequence */
-
 
2012
					unsigned long v =
1873
		    unsigned long v = DEREF_ulong ( str_simple_tok ( s ) ) ;
2013
					    DEREF_ulong(str_simple_tok(s));
1874
		    if ( v == LINK_NONE ) {
2014
					if (v == LINK_NONE) {
1875
			int ch = CHAR_SIMPLE ;
2015
						int ch = CHAR_SIMPLE;
1876
			v = get_multi_char ( text, &ch ) ;
2016
						v = get_multi_char(text, &ch);
-
 
2017
						if (ch == CHAR_OCTAL ||
1877
			if ( ch == CHAR_OCTAL || ch == CHAR_HEX ) {
2018
						    ch == CHAR_HEX) {
1878
			    if ( v < 128 ) fits = 1 ;
2019
							if (v < 128)fits = 1;
1879
			    n = make_nat_value ( v ) ;
2020
							n = make_nat_value(v);
1880
			    COPY_ulong ( str_simple_tok ( s ), v ) ;
2021
							COPY_ulong(str_simple_tok(s), v);
1881
			}
2022
						}
1882
		    } else {
2023
					} else {
1883
			if ( v < 128 ) fits = 1 ;
2024
						if (v < 128)fits = 1;
-
 
2025
						n = make_nat_value(v);
-
 
2026
					}
-
 
2027
				}
-
 
2028
			} else {
-
 
2029
				/* Single character */
-
 
2030
				character c = text[0];
-
 
2031
				if (in_hash_if_exp) {
-
 
2032
					/* Evaluate character value
-
 
2033
					 * immediately */
-
 
2034
					unsigned long v =
-
 
2035
					    DEREF_ulong(str_simple_tok(s));
-
 
2036
					if (v == LINK_NONE) {
-
 
2037
						int ch = CHAR_SIMPLE;
-
 
2038
						v = (unsigned long)c;
-
 
2039
						v = to_ascii(v, &ch);
-
 
2040
						COPY_ulong(str_simple_tok(s),
-
 
2041
							   v);
-
 
2042
					}
-
 
2043
					if (v < 128) {
-
 
2044
						fits = 1;
-
 
2045
					}
1884
			n = make_nat_value ( v ) ;
2046
					n = make_nat_value(v);
-
 
2047
				} else {
-
 
2048
					if (c >= char_zero && c <= char_nine) {
-
 
2049
						/* Allow for digits */
-
 
2050
						digit = (int)(c - char_zero);
1885
		    }
2051
					}
-
 
2052
				}
-
 
2053
			}
1886
		}
2054
		}
-
 
2055
		if (IS_NULL_nat(n)) {
-
 
2056
			/* Make character literal expression */
-
 
2057
			MAKE_exp_char_lit(t0, s, digit, e);
-
 
2058
			MAKE_nat_calc(e, n);
1887
	    } else {
2059
		} else {
-
 
2060
			if (!fits && check_nat_range(t0, n)!= 0) {
1888
		/* Single character */
2061
				/* Value doesn't fit into t0 */
1889
		character c = text [0] ;
2062
				t0 = find_char_type(n);
-
 
2063
			}
-
 
2064
		}
-
 
2065
		MAKE_exp_int_lit(t0, n, exp_char_lit_tag, e);
1890
		if ( in_hash_if_exp ) {
2066
		if (!EQ_type(t0, t1)) {
1891
		    /* Evaluate character value immediately */
2067
			/* Convert from t0 to t1 */
1892
		    unsigned long v = DEREF_ulong ( str_simple_tok ( s ) ) ;
2068
			e = make_cast_nat(t1, e, &err, CAST_STATIC);
-
 
2069
		}
1893
		    if ( v == LINK_NONE ) {
2070
		if (!EQ_type(t1, t2)) {
1894
			int ch = CHAR_SIMPLE ;
2071
			/* Convert from t1 to t2 */
1895
			v = ( unsigned long ) c ;
2072
			e = make_cast_nat(t2, e, &err, CAST_IMPLICIT);
-
 
2073
		}
1896
			v = to_ascii ( v, &ch ) ;
2074
		if (!IS_NULL_err(err)) {
1897
			COPY_ulong ( str_simple_tok ( s ), v ) ;
2075
			report(crt_loc, err);
1898
		    }
2076
		}
-
 
2077
	} else {
1899
		    if ( v < 128 ) fits = 1 ;
2078
		/* String literals */
-
 
2079
		TYPE t;
1900
		    n = make_nat_value ( v ) ;
2080
		NAT n = make_nat_value(len + 1);
-
 
2081
		if (kind & STRING_WIDE) {
-
 
2082
			t = type_wstring_lit;
1901
		} else {
2083
		} else {
1902
		    if ( c >= char_zero && c <= char_nine ) {
-
 
1903
			/* Allow for digits */
2084
			t = type_string_lit;
1904
			digit = ( int ) ( c - char_zero ) ;
-
 
1905
		    }
-
 
1906
		}
2085
		}
-
 
2086
		MAKE_type_array(cv_lvalue, t, n, t);
1907
	    }
2087
		MAKE_exp_string_lit(t, s, e);
1908
	}
2088
	}
1909
	if ( IS_NULL_nat ( n ) ) {
-
 
1910
	    /* Make character literal expression */
-
 
1911
	    MAKE_exp_char_lit ( t0, s, digit, e ) ;
-
 
1912
	    MAKE_nat_calc ( e, n ) ;
-
 
1913
	} else {
-
 
1914
	    if ( !fits && check_nat_range ( t0, n ) != 0 ) {
-
 
1915
		/* Value doesn't fit into t0 */
-
 
1916
		t0 = find_char_type ( n ) ;
-
 
1917
	    }
-
 
1918
	}
-
 
1919
	MAKE_exp_int_lit ( t0, n, exp_char_lit_tag, e ) ;
-
 
1920
	if ( !EQ_type ( t0, t1 ) ) {
-
 
1921
	    /* Convert from t0 to t1 */
-
 
1922
	    e = make_cast_nat ( t1, e, &err, CAST_STATIC ) ;
-
 
1923
	}
-
 
1924
	if ( !EQ_type ( t1, t2 ) ) {
-
 
1925
	    /* Convert from t1 to t2 */
-
 
1926
	    e = make_cast_nat ( t2, e, &err, CAST_IMPLICIT ) ;
-
 
1927
	}
-
 
1928
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
-
 
1929
    } else {
-
 
1930
	/* String literals */
-
 
1931
	TYPE t ;
-
 
1932
	NAT n = make_nat_value ( len + 1 ) ;
-
 
1933
	if ( kind & STRING_WIDE ) {
-
 
1934
	    t = type_wstring_lit ;
-
 
1935
	} else {
-
 
1936
	    t = type_string_lit ;
-
 
1937
	}
-
 
1938
	MAKE_type_array ( cv_lvalue, t, n, t ) ;
-
 
1939
	MAKE_exp_string_lit ( t, s, e ) ;
-
 
1940
    }
-
 
1941
    return ( e ) ;
2089
	return(e);
1942
}
2090
}
1943
 
2091
 
1944
 
2092
 
1945
/*
2093
/*
1946
    CREATE A BOOLEAN LITERAL EXPRESSION
2094
    CREATE A BOOLEAN LITERAL EXPRESSION
1947
 
2095
 
1948
    This routine creates a boolean literal expression given by the boolean
2096
    This routine creates a boolean literal expression given by the boolean
1949
    value b (which should be one of the values BOOL_FALSE and BOOL_TRUE
2097
    value b (which should be one of the values BOOL_FALSE and BOOL_TRUE
1950
    defined in literal.h).
2098
    defined in literal.h).
1951
*/
2099
*/
1952
 
2100
 
1953
EXP make_bool_exp
2101
EXP
1954
    PROTO_N ( ( b, tag ) )
-
 
1955
    PROTO_T ( unsigned b X unsigned tag )
2102
make_bool_exp(unsigned b, unsigned tag)
1956
{
2103
{
1957
    EXP e ;
2104
	EXP e;
1958
    NAT n = small_nat [b] ;
2105
	NAT n = small_nat[b];
1959
    MAKE_exp_int_lit ( type_bool, n, tag, e ) ;
2106
	MAKE_exp_int_lit(type_bool, n, tag, e);
1960
    return ( e ) ;
2107
	return(e);
1961
}
2108
}
1962
 
2109
 
1963
 
2110
 
1964
/*
2111
/*
1965
    TEST A BOOLEAN LITERAL EXPRESSION
2112
    TEST A BOOLEAN LITERAL EXPRESSION
1966
 
2113
 
1967
    This routine is the reverse of the one above.  It returns the boolean
2114
    This routine is the reverse of the one above.  It returns the boolean
1968
    value (BOOL_FALSE, BOOL_TRUE or BOOL_UNKNOWN) corresponding to the
2115
    value (BOOL_FALSE, BOOL_TRUE or BOOL_UNKNOWN) corresponding to the
1969
    expression e.
2116
    expression e.
1970
*/
2117
*/
1971
 
2118
 
1972
unsigned test_bool_exp
2119
unsigned
1973
    PROTO_N ( ( e ) )
-
 
1974
    PROTO_T ( EXP e )
2120
test_bool_exp(EXP e)
1975
{
2121
{
1976
    NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
2122
	NAT n = DEREF_nat(exp_int_lit_nat(e));
1977
    if ( IS_nat_small ( n ) ) {
2123
	if (IS_nat_small(n)) {
1978
	unsigned b = DEREF_unsigned ( nat_small_value ( n ) ) ;
2124
		unsigned b = DEREF_unsigned(nat_small_value(n));
1979
	if ( b == BOOL_FALSE ) return ( BOOL_FALSE ) ;
2125
		if (b == BOOL_FALSE) {
-
 
2126
			return(BOOL_FALSE);
-
 
2127
		}
1980
	if ( b == BOOL_TRUE ) return ( BOOL_TRUE ) ;
2128
		if (b == BOOL_TRUE) {
-
 
2129
			return(BOOL_TRUE);
1981
    }
2130
		}
-
 
2131
	}
1982
    return ( BOOL_UNKNOWN ) ;
2132
	return(BOOL_UNKNOWN);
1983
}
2133
}