]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
Change output to be a sequence of short strings
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
54630035 2 Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
b30f223b
RS
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
b30f223b
RS
20
21#include "config.h"
22#include "tree.h"
23#include "c-lex.h"
24#include "c-tree.h"
25#include "flags.h"
1bb8e5b1 26#include "obstack.h"
b30f223b 27#include <stdio.h>
1ccf251f 28#include <ctype.h>
b30f223b 29
fef610be
JW
30#ifndef WCHAR_TYPE_SIZE
31#ifdef INT_TYPE_SIZE
32#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
33#else
34#define WCHAR_TYPE_SIZE BITS_PER_WORD
35#endif
36#endif
37
0b73773c
NH
38extern struct obstack permanent_obstack;
39
2786cbad 40enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
bbb1ae01 41 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
1c4fadec 42 A_UNUSED, A_FORMAT, A_WEAK, A_ALIAS};
53065596
RK
43
44static void declare_hidden_char_array PROTO((char *, char *));
45static void add_attribute PROTO((enum attrs, char *,
46 int, int, int));
47static void init_attributes PROTO((void));
4724b3de 48
b032c74c 49/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2
RS
50
51void
52declare_function_name ()
53{
7da551a2
RS
54 char *name, *printable_name;
55
56 if (current_function_decl == NULL)
57 {
58 name = "";
59 printable_name = "top level";
60 }
61 else
62 {
63 char *kind = "function";
64 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
65 kind = "method";
6152f876
RS
66 /* Allow functions to be nameless (such as artificial ones). */
67 if (DECL_NAME (current_function_decl))
68 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
69 else
70 name = "";
7da551a2
RS
71 printable_name = (*decl_printable_name) (current_function_decl, &kind);
72 }
73
4724b3de
RS
74 declare_hidden_char_array ("__FUNCTION__", name);
75 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
76}
97d17ac2 77
4724b3de
RS
78static void
79declare_hidden_char_array (name, value)
80 char *name, *value;
81{
82 tree decl, type, init;
83 int vlen;
7da551a2 84
4724b3de 85 /* If the default size of char arrays isn't big enough for the name,
700942a0 86 or if we want to give warnings for large objects, make a bigger one. */
4724b3de 87 vlen = strlen (value) + 1;
97d17ac2 88 type = char_array_type_node;
700942a0
RK
89 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
90 || warn_larger_than)
97d17ac2 91 type = build_array_type (char_type_node,
4724b3de 92 build_index_type (build_int_2 (vlen, 0)));
7da551a2 93 push_obstacks_nochange ();
4724b3de 94 decl = build_decl (VAR_DECL, get_identifier (name), type);
7da551a2
RS
95 TREE_STATIC (decl) = 1;
96 TREE_READONLY (decl) = 1;
4724b3de 97 TREE_ASM_WRITTEN (decl) = 1;
b9a24ad4 98 DECL_SOURCE_LINE (decl) = 0;
d2eb0876 99 DECL_ARTIFICIAL (decl) = 1;
176e81eb 100 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 101 DECL_IGNORED_P (decl) = 1;
4724b3de 102 init = build_string (vlen, value);
97d17ac2 103 TREE_TYPE (init) = type;
7da551a2
RS
104 DECL_INITIAL (decl) = init;
105 finish_decl (pushdecl (decl), init, NULL_TREE);
106}
107
b30f223b
RS
108/* Given a chain of STRING_CST nodes,
109 concatenate them into one STRING_CST
110 and give it a suitable array-of-chars data type. */
111
112tree
113combine_strings (strings)
114 tree strings;
115{
116 register tree value, t;
117 register int length = 1;
118 int wide_length = 0;
119 int wide_flag = 0;
120 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
121 int nchars;
122
123 if (TREE_CHAIN (strings))
124 {
125 /* More than one in the chain, so concatenate. */
126 register char *p, *q;
127
128 /* Don't include the \0 at the end of each substring,
129 except for the last one.
130 Count wide strings and ordinary strings separately. */
131 for (t = strings; t; t = TREE_CHAIN (t))
132 {
133 if (TREE_TYPE (t) == wchar_array_type_node)
134 {
135 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
136 wide_flag = 1;
137 }
138 else
139 length += (TREE_STRING_LENGTH (t) - 1);
140 }
141
142 /* If anything is wide, the non-wides will be converted,
143 which makes them take more space. */
144 if (wide_flag)
145 length = length * wchar_bytes + wide_length;
146
147 p = savealloc (length);
148
149 /* Copy the individual strings into the new combined string.
150 If the combined string is wide, convert the chars to ints
151 for any individual strings that are not wide. */
152
153 q = p;
154 for (t = strings; t; t = TREE_CHAIN (t))
155 {
156 int len = (TREE_STRING_LENGTH (t)
157 - ((TREE_TYPE (t) == wchar_array_type_node)
158 ? wchar_bytes : 1));
159 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
160 {
161 bcopy (TREE_STRING_POINTER (t), q, len);
162 q += len;
163 }
164 else
165 {
166 int i;
167 for (i = 0; i < len; i++)
41bbd14e
JW
168 {
169 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
170 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
171 else
172 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
173 }
b30f223b
RS
174 q += len * wchar_bytes;
175 }
176 }
177 if (wide_flag)
178 {
179 int i;
180 for (i = 0; i < wchar_bytes; i++)
181 *q++ = 0;
182 }
183 else
184 *q = 0;
185
186 value = make_node (STRING_CST);
187 TREE_STRING_POINTER (value) = p;
188 TREE_STRING_LENGTH (value) = length;
189 TREE_CONSTANT (value) = 1;
190 }
191 else
192 {
193 value = strings;
194 length = TREE_STRING_LENGTH (value);
195 if (TREE_TYPE (value) == wchar_array_type_node)
196 wide_flag = 1;
197 }
198
199 /* Compute the number of elements, for the array type. */
200 nchars = wide_flag ? length / wchar_bytes : length;
201
202 /* Create the array type for the string constant.
203 -Wwrite-strings says make the string constant an array of const char
204 so that copying it to a non-const pointer will get a warning. */
205 if (warn_write_strings
206 && (! flag_traditional && ! flag_writable_strings))
207 {
208 tree elements
209 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
210 1, 0);
211 TREE_TYPE (value)
212 = build_array_type (elements,
213 build_index_type (build_int_2 (nchars - 1, 0)));
214 }
215 else
216 TREE_TYPE (value)
217 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
218 build_index_type (build_int_2 (nchars - 1, 0)));
219 TREE_CONSTANT (value) = 1;
220 TREE_STATIC (value) = 1;
221 return value;
222}
223\f
53065596
RK
224/* To speed up processing of attributes, we maintain an array of
225 IDENTIFIER_NODES and the corresponding attribute types. */
226
227/* Array to hold attribute information. */
228
229static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
230
231static int attrtab_idx = 0;
232
233/* Add an entry to the attribute table above. */
234
235static void
236add_attribute (id, string, min_len, max_len, decl_req)
237 enum attrs id;
238 char *string;
239 int min_len, max_len;
240 int decl_req;
241{
242 char buf[100];
243
244 attrtab[attrtab_idx].id = id;
245 attrtab[attrtab_idx].name = get_identifier (string);
246 attrtab[attrtab_idx].min = min_len;
247 attrtab[attrtab_idx].max = max_len;
248 attrtab[attrtab_idx++].decl_req = decl_req;
249
250 sprintf (buf, "__%s__", string);
251
252 attrtab[attrtab_idx].id = id;
253 attrtab[attrtab_idx].name = get_identifier (buf);
254 attrtab[attrtab_idx].min = min_len;
255 attrtab[attrtab_idx].max = max_len;
256 attrtab[attrtab_idx++].decl_req = decl_req;
257}
258
259/* Initialize attribute table. */
260
261static void
262init_attributes ()
263{
a89ca5c6 264 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 265 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 266 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
267 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
268 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
1c4fadec 269 add_attribute (A_UNUSED, "unused", 0, 0, 1);
53065596
RK
270 add_attribute (A_CONST, "const", 0, 0, 1);
271 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
272 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
273 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
274 add_attribute (A_MODE, "mode", 1, 1, 1);
275 add_attribute (A_SECTION, "section", 1, 1, 1);
276 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 277 add_attribute (A_FORMAT, "format", 3, 3, 1);
4b8af8d9
JM
278 add_attribute (A_WEAK, "weak", 0, 0, 1);
279 add_attribute (A_ALIAS, "alias", 1, 1, 1);
53065596
RK
280}
281\f
1228e2a6 282/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
283 and install them in NODE, which is either a DECL (including a TYPE_DECL)
284 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
285 and declaration modifiers but before the declaration proper. */
b30f223b
RS
286
287void
53065596
RK
288decl_attributes (node, attributes, prefix_attributes)
289 tree node, attributes, prefix_attributes;
b30f223b 290{
53065596
RK
291 tree decl = 0, type;
292 int is_type;
293 tree a;
294
295 if (attrtab_idx == 0)
296 init_attributes ();
297
298 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
299 {
300 decl = node;
301 type = TREE_TYPE (decl);
302 is_type = TREE_CODE (node) == TYPE_DECL;
303 }
304 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
305 type = node, is_type = 1;
a2cd7b45 306
eed32833 307 attributes = chainon (prefix_attributes, attributes);
5289b665 308
b30f223b 309 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
310 {
311 tree name = TREE_PURPOSE (a);
312 tree args = TREE_VALUE (a);
313 int i;
314 enum attrs id;
315
316 for (i = 0; i < attrtab_idx; i++)
317 if (attrtab[i].name == name)
318 break;
319
6eaba4a7 320 if (i == attrtab_idx)
53065596 321 {
6eaba4a7
DE
322 if (! valid_machine_attribute (name, args, decl, type))
323 warning ("`%s' attribute directive ignored",
324 IDENTIFIER_POINTER (name));
17c1a44f
SC
325 else if (decl != 0)
326 type = TREE_TYPE (decl);
53065596
RK
327 continue;
328 }
329 else if (attrtab[i].decl_req && decl == 0)
330 {
331 warning ("`%s' attribute does not apply to types",
332 IDENTIFIER_POINTER (name));
333 continue;
334 }
335 else if (list_length (args) < attrtab[i].min
336 || list_length (args) > attrtab[i].max)
337 {
338 error ("wrong number of arguments specified for `%s' attribute",
339 IDENTIFIER_POINTER (name));
340 continue;
341 }
342
343 id = attrtab[i].id;
344 switch (id)
345 {
346 case A_PACKED:
1bcf5b08 347 if (is_type)
a89ca5c6
RK
348 TYPE_PACKED (type) = 1;
349 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
350 DECL_PACKED (decl) = 1;
351 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
352 used for DECL_REGISTER. It wouldn't mean anything anyway. */
353 else
354 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
355 break;
356
bbb1ae01
RK
357 case A_NOCOMMON:
358 if (TREE_CODE (decl) == VAR_DECL)
359 DECL_COMMON (decl) = 0;
360 else
361 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
362 break;
363
2786cbad
JM
364 case A_COMMON:
365 if (TREE_CODE (decl) == VAR_DECL)
366 DECL_COMMON (decl) = 1;
367 else
368 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
369 break;
370
53065596
RK
371 case A_NORETURN:
372 if (TREE_CODE (decl) == FUNCTION_DECL)
373 TREE_THIS_VOLATILE (decl) = 1;
374 else if (TREE_CODE (type) == POINTER_TYPE
375 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
376 TREE_TYPE (decl) = type
377 = build_pointer_type
378 (build_type_variant (TREE_TYPE (type),
379 TREE_READONLY (TREE_TYPE (type)), 1));
380 else
381 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
382 break;
383
1c4fadec
RK
384 case A_UNUSED:
385 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL
386 || TREE_CODE (decl) == FUNCTION_DECL)
387 TREE_USED (decl) = 1;
388 else
389 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
390 break;
391
53065596
RK
392 case A_CONST:
393 if (TREE_CODE (decl) == FUNCTION_DECL)
394 TREE_READONLY (decl) = 1;
395 else if (TREE_CODE (type) == POINTER_TYPE
396 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
397 TREE_TYPE (decl) = type
398 = build_pointer_type
399 (build_type_variant (TREE_TYPE (type), 1,
400 TREE_THIS_VOLATILE (TREE_TYPE (type))));
401 else
402 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
403 break;
404
405 case A_T_UNION:
1bcf5b08 406 if (is_type
53065596 407 && TREE_CODE (type) == UNION_TYPE
1bcf5b08
RK
408 && (decl == 0
409 || TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))))
410 TYPE_TRANSPARENT_UNION (type) = 1;
411 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
412 && TREE_CODE (type) == UNION_TYPE
413 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 414 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
415 else
416 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
417 break;
418
419 case A_CONSTRUCTOR:
420 if (TREE_CODE (decl) == FUNCTION_DECL
421 && TREE_CODE (type) == FUNCTION_TYPE
422 && decl_function_context (decl) == 0)
1a16a053
RK
423 {
424 DECL_STATIC_CONSTRUCTOR (decl) = 1;
425 TREE_USED (decl) = 1;
426 }
53065596
RK
427 else
428 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
429 break;
430
431 case A_DESTRUCTOR:
432 if (TREE_CODE (decl) == FUNCTION_DECL
433 && TREE_CODE (type) == FUNCTION_TYPE
434 && decl_function_context (decl) == 0)
1a16a053
RK
435 {
436 DECL_STATIC_DESTRUCTOR (decl) = 1;
437 TREE_USED (decl) = 1;
438 }
53065596
RK
439 else
440 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
441 break;
442
443 case A_MODE:
444 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
445 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
446 else
447 {
448 int j;
449 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
450 int len = strlen (p);
451 enum machine_mode mode = VOIDmode;
452 tree typefm;
453
454 if (len > 4 && p[0] == '_' && p[1] == '_'
455 && p[len - 1] == '_' && p[len - 2] == '_')
456 {
3438dff9 457 char *newp = (char *) alloca (len - 1);
53065596
RK
458
459 strcpy (newp, &p[2]);
460 newp[len - 4] = '\0';
461 p = newp;
462 }
463
464 /* Give this decl a type with the specified mode.
465 First check for the special modes. */
466 if (! strcmp (p, "byte"))
467 mode = byte_mode;
468 else if (!strcmp (p, "word"))
469 mode = word_mode;
470 else if (! strcmp (p, "pointer"))
471 mode = ptr_mode;
472 else
473 for (j = 0; j < NUM_MACHINE_MODES; j++)
474 if (!strcmp (p, GET_MODE_NAME (j)))
475 mode = (enum machine_mode) j;
476
477 if (mode == VOIDmode)
478 error ("unknown machine mode `%s'", p);
479 else if (0 == (typefm = type_for_mode (mode,
480 TREE_UNSIGNED (type))))
481 error ("no data type for mode `%s'", p);
482 else
483 {
484 TREE_TYPE (decl) = type = typefm;
485 DECL_SIZE (decl) = 0;
486 layout_decl (decl, 0);
487 }
488 }
489 break;
490
491 case A_SECTION:
5289b665 492#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
493 if ((TREE_CODE (decl) == FUNCTION_DECL
494 || TREE_CODE (decl) == VAR_DECL)
495 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
496 {
497 if (TREE_CODE (decl) == VAR_DECL
498 && current_function_decl != NULL_TREE)
499 error_with_decl (decl,
500 "section attribute cannot be specified for local variables");
501 /* The decl may have already been given a section attribute from
502 a previous declaration. Ensure they match. */
503 else if (DECL_SECTION_NAME (decl) != NULL_TREE
504 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
505 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
506 error_with_decl (node,
507 "section of `%s' conflicts with previous declaration");
508 else
509 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
510 }
511 else
512 error_with_decl (node,
5289b665
DE
513 "section attribute not allowed for `%s'");
514#else
53065596
RK
515 error_with_decl (node,
516 "section attributes are not supported for this target");
5289b665 517#endif
53065596
RK
518 break;
519
520 case A_ALIGNED:
6f38f669 521 {
53065596 522 tree align_expr
54630035
RK
523 = (args ? TREE_VALUE (args)
524 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
53065596
RK
525 int align;
526
527 /* Strip any NOPs of any kind. */
528 while (TREE_CODE (align_expr) == NOP_EXPR
529 || TREE_CODE (align_expr) == CONVERT_EXPR
530 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
531 align_expr = TREE_OPERAND (align_expr, 0);
532
533 if (TREE_CODE (align_expr) != INTEGER_CST)
534 {
535 error ("requested alignment is not a constant");
536 continue;
537 }
538
539 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
540
541 if (exact_log2 (align) == -1)
542 error ("requested alignment is not a power of 2");
543 else if (is_type)
06aa52de 544 TYPE_ALIGN (type) = align;
53065596
RK
545 else if (TREE_CODE (decl) != VAR_DECL
546 && TREE_CODE (decl) != FIELD_DECL)
547 error_with_decl (decl,
548 "alignment may not be specified for `%s'");
549 else
550 DECL_ALIGN (decl) = align;
6f38f669 551 }
53065596 552 break;
6f38f669 553
53065596 554 case A_FORMAT:
b30f223b 555 {
53065596
RK
556 tree format_type = TREE_VALUE (args);
557 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
558 tree first_arg_num_expr
559 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
560 int format_num;
561 int first_arg_num;
562 int is_scan;
563 tree argument;
564 int arg_num;
b30f223b 565
53065596
RK
566 if (TREE_CODE (decl) != FUNCTION_DECL)
567 {
568 error_with_decl (decl,
569 "argument format specified for non-function `%s'");
570 continue;
571 }
b30f223b 572
53065596
RK
573 if (TREE_CODE (format_type) == IDENTIFIER_NODE
574 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
575 || !strcmp (IDENTIFIER_POINTER (format_type),
576 "__printf__")))
577 is_scan = 0;
578 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
579 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
580 || !strcmp (IDENTIFIER_POINTER (format_type),
581 "__scanf__")))
582 is_scan = 1;
583 else
584 {
585 error ("unrecognized format specifier for `%s'");
586 continue;
587 }
6f38f669 588
53065596
RK
589 /* Strip any conversions from the string index and first arg number
590 and verify they are constants. */
591 while (TREE_CODE (format_num_expr) == NOP_EXPR
592 || TREE_CODE (format_num_expr) == CONVERT_EXPR
593 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
594 format_num_expr = TREE_OPERAND (format_num_expr, 0);
677ff441 595
53065596
RK
596 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
597 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
598 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
599 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
600
601 if (TREE_CODE (format_num_expr) != INTEGER_CST
602 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
09e3dd72 603 {
53065596
RK
604 error ("format string has non-constant operand number");
605 continue;
09e3dd72 606 }
53065596
RK
607
608 format_num = TREE_INT_CST_LOW (format_num_expr);
609 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
610 if (first_arg_num != 0 && first_arg_num <= format_num)
68a91d8d 611 {
53065596 612 error ("format string arg follows the args to be formatted");
6f38f669 613 continue;
68a91d8d 614 }
53065596
RK
615
616 /* If a parameter list is specified, verify that the format_num
617 argument is actually a string, in case the format attribute
618 is in error. */
619 argument = TYPE_ARG_TYPES (type);
620 if (argument)
09e3dd72 621 {
53065596 622 for (arg_num = 1; ; ++arg_num)
09e3dd72 623 {
53065596
RK
624 if (argument == 0 || arg_num == format_num)
625 break;
626 argument = TREE_CHAIN (argument);
627 }
628 if (! argument
629 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
630 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
631 != char_type_node))
632 {
633 error ("format string arg not a string type");
09e3dd72
RK
634 continue;
635 }
53065596
RK
636 if (first_arg_num != 0)
637 {
638 /* Verify that first_arg_num points to the last arg,
639 the ... */
640 while (argument)
641 arg_num++, argument = TREE_CHAIN (argument);
642 if (arg_num != first_arg_num)
643 {
644 error ("args to be formatted is not ...");
645 continue;
646 }
647 }
09e3dd72 648 }
a2cd7b45 649
53065596
RK
650 record_function_format (DECL_NAME (decl),
651 DECL_ASSEMBLER_NAME (decl),
652 is_scan, format_num, first_arg_num);
653 break;
654 }
4b8af8d9
JM
655
656 case A_WEAK:
657 declare_weak (decl);
658 break;
659
660 case A_ALIAS:
661 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
662 || TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))
663 error_with_decl (decl,
664 "`%s' defined both normally and as an alias");
665 else if (decl_function_context (decl) == 0)
666 {
667 tree id = get_identifier (TREE_STRING_POINTER
668 (TREE_VALUE (args)));
669 if (TREE_CODE (decl) == FUNCTION_DECL)
670 DECL_INITIAL (decl) = error_mark_node;
671 else
672 DECL_EXTERNAL (decl) = 0;
673 assemble_alias (decl, id);
674 }
675 else
676 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
677 break;
53065596
RK
678 }
679 }
b30f223b
RS
680}
681\f
1ccf251f
RK
682/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
683 a parameter list. */
684
685#define T_I &integer_type_node
686#define T_L &long_integer_type_node
2314fb70 687#define T_LL &long_long_integer_type_node
1ccf251f
RK
688#define T_S &short_integer_type_node
689#define T_UI &unsigned_type_node
690#define T_UL &long_unsigned_type_node
2314fb70 691#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
692#define T_US &short_unsigned_type_node
693#define T_F &float_type_node
694#define T_D &double_type_node
695#define T_LD &long_double_type_node
696#define T_C &char_type_node
697#define T_V &void_type_node
698#define T_W &wchar_type_node
df8a401a 699#define T_ST &sizetype
1ccf251f
RK
700
701typedef struct {
702 char *format_chars;
703 int pointer_count;
704 /* Type of argument if no length modifier is used. */
705 tree *nolen;
706 /* Type of argument if length modifier for shortening is used.
707 If NULL, then this modifier is not allowed. */
708 tree *hlen;
709 /* Type of argument if length modifier `l' is used.
710 If NULL, then this modifier is not allowed. */
711 tree *llen;
2cedb812 712 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
713 If NULL, then this modifier is not allowed. */
714 tree *qlen;
1ccf251f
RK
715 /* Type of argument if length modifier `L' is used.
716 If NULL, then this modifier is not allowed. */
717 tree *bigllen;
718 /* List of other modifier characters allowed with these options. */
719 char *flag_chars;
720} format_char_info;
721
722static format_char_info print_char_table[] = {
2cedb812
RK
723 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
724 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
4cc00c5a 725 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
af3c5588 726/* Two GNU extensions. */
2314fb70 727 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
2a13575e 728 { "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
2314fb70
CH
729 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
730 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
731 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
732 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
733 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
734 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
735 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
736 { NULL }
737};
738
739static format_char_info scan_char_table[] = {
2cedb812
RK
740 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
741 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
2314fb70
CH
742 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
743 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
744 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
745 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
746 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
747 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
748 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
749 { NULL }
750};
751
752typedef struct function_format_info {
753 struct function_format_info *next; /* next structure on the list */
754 tree name; /* identifier such as "printf" */
755 tree assembler_name; /* optional mangled identifier (for C++) */
756 int is_scan; /* TRUE if *scanf */
757 int format_num; /* number of format argument */
758 int first_arg_num; /* number of first arg (zero for varargs) */
759} function_format_info;
760
761static function_format_info *function_format_list = NULL;
762
763static void check_format_info PROTO((function_format_info *, tree));
764
765/* Initialize the table of functions to perform format checking on.
766 The ANSI functions are always checked (whether <stdio.h> is
767 included or not), since it is common to call printf without
768 including <stdio.h>. There shouldn't be a problem with this,
769 since ANSI reserves these function names whether you include the
770 header file or not. In any case, the checking is harmless. */
771
772void
773init_function_format_info ()
774{
775 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
776 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
777 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
778 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
779 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
780 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
781 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
782 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
783 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
784}
785
786/* Record information for argument format checking. FUNCTION_IDENT is
787 the identifier node for the name of the function to check (its decl
788 need not exist yet). IS_SCAN is true for scanf-type format checking;
789 false indicates printf-style format checking. FORMAT_NUM is the number
790 of the argument which is the format control string (starting from 1).
791 FIRST_ARG_NUM is the number of the first actual argument to check
792 against teh format string, or zero if no checking is not be done
793 (e.g. for varargs such as vfprintf). */
794
795void
796record_function_format (name, assembler_name, is_scan,
797 format_num, first_arg_num)
798 tree name;
799 tree assembler_name;
800 int is_scan;
801 int format_num;
802 int first_arg_num;
803{
804 function_format_info *info;
805
806 /* Re-use existing structure if it's there. */
807
808 for (info = function_format_list; info; info = info->next)
809 {
810 if (info->name == name && info->assembler_name == assembler_name)
811 break;
812 }
813 if (! info)
814 {
815 info = (function_format_info *) xmalloc (sizeof (function_format_info));
816 info->next = function_format_list;
817 function_format_list = info;
818
819 info->name = name;
820 info->assembler_name = assembler_name;
821 }
822
823 info->is_scan = is_scan;
824 info->format_num = format_num;
825 info->first_arg_num = first_arg_num;
826}
827
828static char tfaff[] = "too few arguments for format";
829\f
830/* Check the argument list of a call to printf, scanf, etc.
831 NAME is the function identifier.
832 ASSEMBLER_NAME is the function's assembler identifier.
833 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
834 PARAMS is the list of argument values. */
835
836void
837check_function_format (name, assembler_name, params)
838 tree name;
839 tree assembler_name;
840 tree params;
841{
842 function_format_info *info;
843
844 /* See if this function is a format function. */
845 for (info = function_format_list; info; info = info->next)
846 {
95216dec 847 if (info->assembler_name
1ccf251f
RK
848 ? (info->assembler_name == assembler_name)
849 : (info->name == name))
850 {
851 /* Yup; check it. */
852 check_format_info (info, params);
853 break;
854 }
855 }
856}
857
858/* Check the argument list of a call to printf, scanf, etc.
859 INFO points to the function_format_info structure.
860 PARAMS is the list of argument values. */
861
862static void
863check_format_info (info, params)
864 function_format_info *info;
865 tree params;
866{
867 int i;
868 int arg_num;
869 int suppressed, wide, precise;
870 int length_char;
871 int format_char;
872 int format_length;
873 tree format_tree;
874 tree cur_param;
875 tree cur_type;
876 tree wanted_type;
9b69f523 877 tree first_fillin_param;
1ccf251f
RK
878 char *format_chars;
879 format_char_info *fci;
880 static char message[132];
881 char flag_chars[8];
9b69f523 882 int has_operand_number = 0;
1ccf251f
RK
883
884 /* Skip to format argument. If the argument isn't available, there's
885 no work for us to do; prototype checking will catch the problem. */
886 for (arg_num = 1; ; ++arg_num)
887 {
888 if (params == 0)
889 return;
890 if (arg_num == info->format_num)
891 break;
892 params = TREE_CHAIN (params);
893 }
894 format_tree = TREE_VALUE (params);
895 params = TREE_CHAIN (params);
896 if (format_tree == 0)
897 return;
898 /* We can only check the format if it's a string constant. */
899 while (TREE_CODE (format_tree) == NOP_EXPR)
900 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
39b751ce 901 if (integer_zerop (format_tree))
1ccf251f
RK
902 {
903 warning ("null format string");
904 return;
905 }
906 if (TREE_CODE (format_tree) != ADDR_EXPR)
907 return;
908 format_tree = TREE_OPERAND (format_tree, 0);
909 if (TREE_CODE (format_tree) != STRING_CST)
910 return;
911 format_chars = TREE_STRING_POINTER (format_tree);
912 format_length = TREE_STRING_LENGTH (format_tree);
913 if (format_length <= 1)
914 warning ("zero-length format string");
915 if (format_chars[--format_length] != 0)
916 {
917 warning ("unterminated format string");
918 return;
919 }
920 /* Skip to first argument to check. */
921 while (arg_num + 1 < info->first_arg_num)
922 {
923 if (params == 0)
924 return;
925 params = TREE_CHAIN (params);
926 ++arg_num;
927 }
9b69f523
RK
928
929 first_fillin_param = params;
1ccf251f
RK
930 while (1)
931 {
af3c5588 932 int aflag;
1ccf251f
RK
933 if (*format_chars == 0)
934 {
935 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
936 warning ("embedded `\\0' in format");
9b69f523 937 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
938 warning ("too many arguments for format");
939 return;
940 }
941 if (*format_chars++ != '%')
942 continue;
943 if (*format_chars == 0)
944 {
945 warning ("spurious trailing `%%' in format");
946 continue;
947 }
948 if (*format_chars == '%')
949 {
950 ++format_chars;
951 continue;
952 }
953 flag_chars[0] = 0;
954 suppressed = wide = precise = FALSE;
955 if (info->is_scan)
956 {
957 suppressed = *format_chars == '*';
958 if (suppressed)
959 ++format_chars;
960 while (isdigit (*format_chars))
961 ++format_chars;
962 }
963 else
964 {
9b69f523
RK
965 /* See if we have a number followed by a dollar sign. If we do,
966 it is an operand number, so set PARAMS to that operand. */
967 if (*format_chars >= '0' && *format_chars <= '9')
968 {
969 char *p = format_chars;
970
971 while (*p >= '0' && *p++ <= '9')
972 ;
973
974 if (*p == '$')
975 {
976 int opnum = atoi (format_chars);
977
978 params = first_fillin_param;
979 format_chars = p + 1;
980 has_operand_number = 1;
981
982 for (i = 1; i < opnum && params != 0; i++)
983 params = TREE_CHAIN (params);
984
985 if (opnum == 0 || params == 0)
986 {
987 warning ("operand number out of range in format");
988 return;
989 }
990 }
991 }
992
1ccf251f
RK
993 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
994 {
995 if (index (flag_chars, *format_chars) != 0)
996 {
997 sprintf (message, "repeated `%c' flag in format",
998 *format_chars);
999 warning (message);
1000 }
1001 i = strlen (flag_chars);
1002 flag_chars[i++] = *format_chars++;
1003 flag_chars[i] = 0;
1004 }
1005 /* "If the space and + flags both appear,
1006 the space flag will be ignored." */
1007 if (index (flag_chars, ' ') != 0
1008 && index (flag_chars, '+') != 0)
1009 warning ("use of both ` ' and `+' flags in format");
1010 /* "If the 0 and - flags both appear,
1011 the 0 flag will be ignored." */
1012 if (index (flag_chars, '0') != 0
1013 && index (flag_chars, '-') != 0)
1014 warning ("use of both `0' and `-' flags in format");
1015 if (*format_chars == '*')
1016 {
1017 wide = TRUE;
1018 /* "...a field width...may be indicated by an asterisk.
1019 In this case, an int argument supplies the field width..." */
1020 ++format_chars;
1021 if (params == 0)
1022 {
1023 warning (tfaff);
1024 return;
1025 }
1026 if (info->first_arg_num != 0)
1027 {
1028 cur_param = TREE_VALUE (params);
1029 params = TREE_CHAIN (params);
1030 ++arg_num;
1031 /* size_t is generally not valid here.
1032 It will work on most machines, because size_t and int
1033 have the same mode. But might as well warn anyway,
1034 since it will fail on other machines. */
309ffab6
RS
1035 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1036 != integer_type_node)
1037 &&
1038 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1039 != unsigned_type_node))
1ccf251f
RK
1040 {
1041 sprintf (message,
1042 "field width is not type int (arg %d)",
1043 arg_num);
1044 warning (message);
1045 }
1046 }
1047 }
1048 else
1049 {
1050 while (isdigit (*format_chars))
1051 {
1052 wide = TRUE;
1053 ++format_chars;
1054 }
1055 }
1056 if (*format_chars == '.')
1057 {
1058 precise = TRUE;
1059 ++format_chars;
1060 if (*format_chars != '*' && !isdigit (*format_chars))
1061 warning ("`.' not followed by `*' or digit in format");
1062 /* "...a...precision...may be indicated by an asterisk.
1063 In this case, an int argument supplies the...precision." */
1064 if (*format_chars == '*')
1065 {
1066 if (info->first_arg_num != 0)
1067 {
1068 ++format_chars;
1069 if (params == 0)
1070 {
1071 warning (tfaff);
1072 return;
1073 }
1074 cur_param = TREE_VALUE (params);
1075 params = TREE_CHAIN (params);
1076 ++arg_num;
1077 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1078 != integer_type_node)
1079 {
1080 sprintf (message,
1081 "field width is not type int (arg %d)",
1082 arg_num);
1083 warning (message);
1084 }
1085 }
1086 }
1087 else
1088 {
1089 while (isdigit (*format_chars))
1090 ++format_chars;
1091 }
1092 }
1093 }
80d390a2
RK
1094 if (*format_chars == 'h' || *format_chars == 'l')
1095 length_char = *format_chars++;
1096 else if (*format_chars == 'q' || *format_chars == 'L')
15cf7c83
RK
1097 {
1098 length_char = *format_chars++;
1099 if (pedantic)
80d390a2
RK
1100 pedwarn ("ANSI C does not support the `%c' length modifier",
1101 length_char);
15cf7c83 1102 }
1ccf251f
RK
1103 else
1104 length_char = 0;
2cedb812 1105 if (length_char == 'l' && *format_chars == 'l')
15cf7c83
RK
1106 {
1107 length_char = 'q', format_chars++;
1108 if (pedantic)
1109 pedwarn ("ANSI C does not support the `ll' length modifier");
1110 }
af3c5588
RK
1111 aflag = 0;
1112 if (*format_chars == 'a')
1113 {
1114 aflag = 1;
1115 format_chars++;
1116 }
1ccf251f
RK
1117 if (suppressed && length_char != 0)
1118 {
1119 sprintf (message,
1120 "use of `*' and `%c' together in format",
1121 length_char);
1122 warning (message);
1123 }
1124 format_char = *format_chars;
69c3716d 1125 if (format_char == 0 || format_char == '%')
1ccf251f
RK
1126 {
1127 warning ("conversion lacks type at end of format");
1128 continue;
1129 }
1130 format_chars++;
1131 fci = info->is_scan ? scan_char_table : print_char_table;
1132 while (fci->format_chars != 0
1133 && index (fci->format_chars, format_char) == 0)
1134 ++fci;
1135 if (fci->format_chars == 0)
1136 {
1137 if (format_char >= 040 && format_char < 0177)
1138 sprintf (message,
1139 "unknown conversion type character `%c' in format",
1140 format_char);
1141 else
1142 sprintf (message,
1143 "unknown conversion type character 0x%x in format",
1144 format_char);
1145 warning (message);
1146 continue;
1147 }
1148 if (wide && index (fci->flag_chars, 'w') == 0)
1149 {
1150 sprintf (message, "width used with `%c' format",
1151 format_char);
1152 warning (message);
1153 }
1154 if (precise && index (fci->flag_chars, 'p') == 0)
1155 {
1156 sprintf (message, "precision used with `%c' format",
1157 format_char);
1158 warning (message);
af3c5588
RK
1159 }
1160 if (aflag && index (fci->flag_chars, 'a') == 0)
1161 {
1162 sprintf (message, "`a' flag used with `%c' format",
1163 format_char);
1164 warning (message);
1ccf251f
RK
1165 }
1166 if (info->is_scan && format_char == '[')
1167 {
1168 /* Skip over scan set, in case it happens to have '%' in it. */
1169 if (*format_chars == '^')
1170 ++format_chars;
1171 /* Find closing bracket; if one is hit immediately, then
1172 it's part of the scan set rather than a terminator. */
1173 if (*format_chars == ']')
1174 ++format_chars;
1175 while (*format_chars && *format_chars != ']')
1176 ++format_chars;
1177 if (*format_chars != ']')
1178 /* The end of the format string was reached. */
1179 warning ("no closing `]' for `%%[' format");
1180 }
1181 if (suppressed)
1182 {
1183 if (index (fci->flag_chars, '*') == 0)
1184 {
1185 sprintf (message,
1186 "suppression of `%c' conversion in format",
1187 format_char);
1188 warning (message);
1189 }
1190 continue;
1191 }
1192 for (i = 0; flag_chars[i] != 0; ++i)
1193 {
1194 if (index (fci->flag_chars, flag_chars[i]) == 0)
1195 {
1196 sprintf (message, "flag `%c' used with type `%c'",
1197 flag_chars[i], format_char);
1198 warning (message);
1199 }
1200 }
1201 if (precise && index (flag_chars, '0') != 0
1202 && (format_char == 'd' || format_char == 'i'
1203 || format_char == 'o' || format_char == 'u'
1204 || format_char == 'x' || format_char == 'x'))
1205 {
1206 sprintf (message,
1207 "precision and `0' flag not both allowed with `%c' format",
1208 format_char);
1209 warning (message);
1210 }
1211 switch (length_char)
1212 {
1213 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1214 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1215 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1216 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f
RK
1217 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1218 }
1219 if (wanted_type == 0)
1220 {
1221 sprintf (message,
1222 "use of `%c' length character with `%c' type character",
1223 length_char, format_char);
1224 warning (message);
1225 }
1226
1227 /*
1228 ** XXX -- should kvetch about stuff such as
1229 ** {
1230 ** const int i;
1231 **
1232 ** scanf ("%d", &i);
1233 ** }
1234 */
1235
1236 /* Finally. . .check type of argument against desired type! */
1237 if (info->first_arg_num == 0)
1238 continue;
2a13575e
RK
1239 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1240 /* This specifier takes no argument. */
1241 continue;
1ccf251f
RK
1242 if (params == 0)
1243 {
1244 warning (tfaff);
1245 return;
1246 }
1247 cur_param = TREE_VALUE (params);
1248 params = TREE_CHAIN (params);
1249 ++arg_num;
1250 cur_type = TREE_TYPE (cur_param);
1251
1252 /* Check the types of any additional pointer arguments
1253 that precede the "real" argument. */
1254 for (i = 0; i < fci->pointer_count; ++i)
1255 {
1256 if (TREE_CODE (cur_type) == POINTER_TYPE)
1257 {
1258 cur_type = TREE_TYPE (cur_type);
1259 continue;
1260 }
87416640
RK
1261 if (TREE_CODE (cur_type) != ERROR_MARK)
1262 {
1263 sprintf (message,
1264 "format argument is not a %s (arg %d)",
1265 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1266 arg_num);
1267 warning (message);
1268 }
1ccf251f
RK
1269 break;
1270 }
1271
1272 /* Check the type of the "real" argument, if there's a type we want. */
1273 if (i == fci->pointer_count && wanted_type != 0
87416640 1274 && TREE_CODE (cur_type) != ERROR_MARK
1ccf251f
RK
1275 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1276 /* If we want `void *', allow any pointer type.
1277 (Anything else would already have got a warning.) */
1278 && ! (wanted_type == void_type_node
1279 && fci->pointer_count > 0)
1280 /* Don't warn about differences merely in signedness. */
1281 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
b7c9c707 1282 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1283 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1284 ? wanted_type == (cur_type = unsigned_type (cur_type))
1285 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
1286 /* Likewise, "signed char", "unsigned char" and "char" are
1287 equivalent but the above test won't consider them equivalent. */
1288 && ! (wanted_type == char_type_node
b7c9c707
RS
1289 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1290 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f
RK
1291 {
1292 register char *this;
1293 register char *that;
1294
1295 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1296 that = 0;
1297 if (TREE_CODE (cur_type) != ERROR_MARK
1298 && TYPE_NAME (cur_type) != 0
1299 && TREE_CODE (cur_type) != INTEGER_TYPE
1300 && !(TREE_CODE (cur_type) == POINTER_TYPE
1301 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1302 {
1303 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1304 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1305 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1306 else
1307 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1308 }
1309
1310 /* A nameless type can't possibly match what the format wants.
1311 So there will be a warning for it.
1312 Make up a string to describe vaguely what it is. */
1313 if (that == 0)
1314 {
1315 if (TREE_CODE (cur_type) == POINTER_TYPE)
1316 that = "pointer";
1317 else
1318 that = "different type";
1319 }
1320
309ffab6
RS
1321 /* Make the warning better in case of mismatch of int vs long. */
1322 if (TREE_CODE (cur_type) == INTEGER_TYPE
1323 && TREE_CODE (wanted_type) == INTEGER_TYPE
1324 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1325 && TYPE_NAME (cur_type) != 0
1326 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1327 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1328
1ccf251f
RK
1329 if (strcmp (this, that) != 0)
1330 {
1331 sprintf (message, "%s format, %s arg (arg %d)",
1332 this, that, arg_num);
1333 warning (message);
1334 }
1335 }
1336 }
1337}
1338\f
d74154d5
RS
1339/* Print a warning if a constant expression had overflow in folding.
1340 Invoke this function on every expression that the language
1341 requires to be a constant expression.
1342 Note the ANSI C standard says it is erroneous for a
1343 constant expression to overflow. */
96571883
BK
1344
1345void
1346constant_expression_warning (value)
1347 tree value;
1348{
c05f751c
RK
1349 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1350 || TREE_CODE (value) == COMPLEX_CST)
1351 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1352 pedwarn ("overflow in constant expression");
d74154d5
RS
1353}
1354
1355/* Print a warning if an expression had overflow in folding.
1356 Invoke this function on every expression that
1357 (1) appears in the source code, and
1358 (2) might be a constant expression that overflowed, and
1359 (3) is not already checked by convert_and_check;
1360 however, do not invoke this function on operands of explicit casts. */
1361
1362void
1363overflow_warning (value)
1364 tree value;
1365{
c05f751c
RK
1366 if ((TREE_CODE (value) == INTEGER_CST
1367 || (TREE_CODE (value) == COMPLEX_CST
1368 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1369 && TREE_OVERFLOW (value))
d74154d5 1370 {
7193bce2 1371 TREE_OVERFLOW (value) = 0;
14aadfe8 1372 warning ("integer overflow in expression");
d74154d5 1373 }
c05f751c
RK
1374 else if ((TREE_CODE (value) == REAL_CST
1375 || (TREE_CODE (value) == COMPLEX_CST
1376 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1377 && TREE_OVERFLOW (value))
1378 {
1379 TREE_OVERFLOW (value) = 0;
f33f79c0 1380 warning ("floating point overflow in expression");
c05f751c 1381 }
d74154d5
RS
1382}
1383
1384/* Print a warning if a large constant is truncated to unsigned,
1385 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1386 Invoke this function on every expression that might be implicitly
1387 converted to an unsigned type. */
1388
1389void
1390unsigned_conversion_warning (result, operand)
1391 tree result, operand;
1392{
1393 if (TREE_CODE (operand) == INTEGER_CST
1394 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1395 && TREE_UNSIGNED (TREE_TYPE (result))
1396 && !int_fits_type_p (operand, TREE_TYPE (result)))
1397 {
1398 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1399 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 1400 warning ("large integer implicitly truncated to unsigned type");
d74154d5 1401 else if (warn_conversion)
90c939d4 1402 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
1403 }
1404}
1405
1406/* Convert EXPR to TYPE, warning about conversion problems with constants.
1407 Invoke this function on every expression that is converted implicitly,
1408 i.e. because of language rules and not because of an explicit cast. */
1409
1410tree
1411convert_and_check (type, expr)
1412 tree type, expr;
1413{
1414 tree t = convert (type, expr);
1415 if (TREE_CODE (t) == INTEGER_CST)
1416 {
7193bce2 1417 if (TREE_OVERFLOW (t))
d74154d5 1418 {
7193bce2
PE
1419 TREE_OVERFLOW (t) = 0;
1420
868fc750
RK
1421 /* Do not diagnose overflow in a constant expression merely
1422 because a conversion overflowed. */
1423 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1424
7193bce2
PE
1425 /* No warning for converting 0x80000000 to int. */
1426 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1427 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1428 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
1429 /* If EXPR fits in the unsigned version of TYPE,
1430 don't warn unless pedantic. */
1431 if (pedantic
1432 || TREE_UNSIGNED (type)
1433 || ! int_fits_type_p (expr, unsigned_type (type)))
1434 warning ("overflow in implicit constant conversion");
d74154d5
RS
1435 }
1436 else
1437 unsigned_conversion_warning (t, expr);
1438 }
1439 return t;
96571883
BK
1440}
1441\f
b30f223b
RS
1442void
1443c_expand_expr_stmt (expr)
1444 tree expr;
1445{
1446 /* Do default conversion if safe and possibly important,
1447 in case within ({...}). */
1448 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1449 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1450 expr = default_conversion (expr);
1451
1452 if (TREE_TYPE (expr) != error_mark_node
1453 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1454 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1455 error ("expression statement has incomplete type");
1456
1457 expand_expr_stmt (expr);
1458}
1459\f
1460/* Validate the expression after `case' and apply default promotions. */
1461
1462tree
1463check_case_value (value)
1464 tree value;
1465{
1466 if (value == NULL_TREE)
1467 return value;
1468
1469 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 1470 STRIP_TYPE_NOPS (value);
b30f223b
RS
1471
1472 if (TREE_CODE (value) != INTEGER_CST
1473 && value != error_mark_node)
1474 {
1475 error ("case label does not reduce to an integer constant");
1476 value = error_mark_node;
1477 }
1478 else
1479 /* Promote char or short to int. */
1480 value = default_conversion (value);
1481
bc690db1
RS
1482 constant_expression_warning (value);
1483
b30f223b
RS
1484 return value;
1485}
1486\f
1487/* Return an integer type with BITS bits of precision,
1488 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1489
1490tree
1491type_for_size (bits, unsignedp)
1492 unsigned bits;
1493 int unsignedp;
1494{
a311b52c
JM
1495 if (bits == TYPE_PRECISION (integer_type_node))
1496 return unsignedp ? unsigned_type_node : integer_type_node;
1497
3fc7e390 1498 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
1499 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1500
3fc7e390 1501 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
1502 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1503
3fc7e390 1504 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
1505 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1506
3fc7e390 1507 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
1508 return (unsignedp ? long_long_unsigned_type_node
1509 : long_long_integer_type_node);
1510
3fc7e390
RS
1511 if (bits <= TYPE_PRECISION (intQI_type_node))
1512 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1513
1514 if (bits <= TYPE_PRECISION (intHI_type_node))
1515 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1516
1517 if (bits <= TYPE_PRECISION (intSI_type_node))
1518 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1519
1520 if (bits <= TYPE_PRECISION (intDI_type_node))
1521 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1522
b30f223b
RS
1523 return 0;
1524}
1525
1526/* Return a data type that has machine mode MODE.
1527 If the mode is an integer,
1528 then UNSIGNEDP selects between signed and unsigned types. */
1529
1530tree
1531type_for_mode (mode, unsignedp)
1532 enum machine_mode mode;
1533 int unsignedp;
1534{
a311b52c
JM
1535 if (mode == TYPE_MODE (integer_type_node))
1536 return unsignedp ? unsigned_type_node : integer_type_node;
1537
b30f223b
RS
1538 if (mode == TYPE_MODE (signed_char_type_node))
1539 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1540
1541 if (mode == TYPE_MODE (short_integer_type_node))
1542 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1543
b30f223b
RS
1544 if (mode == TYPE_MODE (long_integer_type_node))
1545 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1546
1547 if (mode == TYPE_MODE (long_long_integer_type_node))
1548 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1549
3fc7e390
RS
1550 if (mode == TYPE_MODE (intQI_type_node))
1551 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1552
1553 if (mode == TYPE_MODE (intHI_type_node))
1554 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1555
1556 if (mode == TYPE_MODE (intSI_type_node))
1557 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1558
1559 if (mode == TYPE_MODE (intDI_type_node))
1560 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1561
b30f223b
RS
1562 if (mode == TYPE_MODE (float_type_node))
1563 return float_type_node;
1564
1565 if (mode == TYPE_MODE (double_type_node))
1566 return double_type_node;
1567
1568 if (mode == TYPE_MODE (long_double_type_node))
1569 return long_double_type_node;
1570
1571 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1572 return build_pointer_type (char_type_node);
1573
1574 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1575 return build_pointer_type (integer_type_node);
1576
1577 return 0;
1578}
1579\f
6acfe908
JM
1580/* Return the minimum number of bits needed to represent VALUE in a
1581 signed or unsigned type, UNSIGNEDP says which. */
1582
1583int
1584min_precision (value, unsignedp)
1585 tree value;
1586 int unsignedp;
1587{
1588 int log;
1589
1590 /* If the value is negative, compute its negative minus 1. The latter
1591 adjustment is because the absolute value of the largest negative value
1592 is one larger than the largest positive value. This is equivalent to
1593 a bit-wise negation, so use that operation instead. */
1594
1595 if (tree_int_cst_sgn (value) < 0)
1596 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1597
1598 /* Return the number of bits needed, taking into account the fact
1599 that we need one more bit for a signed than unsigned type. */
1600
1601 if (integer_zerop (value))
1602 log = 0;
1603 else if (TREE_INT_CST_HIGH (value) != 0)
1604 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1605 else
1606 log = floor_log2 (TREE_INT_CST_LOW (value));
1607
1608 return log + 1 + ! unsignedp;
1609}
1610\f
b30f223b
RS
1611/* Print an error message for invalid operands to arith operation CODE.
1612 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1613
1614void
1615binary_op_error (code)
1616 enum tree_code code;
1617{
89c78d7d
RK
1618 register char *opname = "unknown";
1619
b30f223b
RS
1620 switch (code)
1621 {
1622 case NOP_EXPR:
1623 error ("invalid truth-value expression");
1624 return;
1625
1626 case PLUS_EXPR:
1627 opname = "+"; break;
1628 case MINUS_EXPR:
1629 opname = "-"; break;
1630 case MULT_EXPR:
1631 opname = "*"; break;
1632 case MAX_EXPR:
1633 opname = "max"; break;
1634 case MIN_EXPR:
1635 opname = "min"; break;
1636 case EQ_EXPR:
1637 opname = "=="; break;
1638 case NE_EXPR:
1639 opname = "!="; break;
1640 case LE_EXPR:
1641 opname = "<="; break;
1642 case GE_EXPR:
1643 opname = ">="; break;
1644 case LT_EXPR:
1645 opname = "<"; break;
1646 case GT_EXPR:
1647 opname = ">"; break;
1648 case LSHIFT_EXPR:
1649 opname = "<<"; break;
1650 case RSHIFT_EXPR:
1651 opname = ">>"; break;
1652 case TRUNC_MOD_EXPR:
047de90b 1653 case FLOOR_MOD_EXPR:
b30f223b
RS
1654 opname = "%"; break;
1655 case TRUNC_DIV_EXPR:
047de90b 1656 case FLOOR_DIV_EXPR:
b30f223b
RS
1657 opname = "/"; break;
1658 case BIT_AND_EXPR:
1659 opname = "&"; break;
1660 case BIT_IOR_EXPR:
1661 opname = "|"; break;
1662 case TRUTH_ANDIF_EXPR:
1663 opname = "&&"; break;
1664 case TRUTH_ORIF_EXPR:
1665 opname = "||"; break;
1666 case BIT_XOR_EXPR:
1667 opname = "^"; break;
047de90b
RS
1668 case LROTATE_EXPR:
1669 case RROTATE_EXPR:
1670 opname = "rotate"; break;
b30f223b
RS
1671 }
1672 error ("invalid operands to binary %s", opname);
1673}
1674\f
1675/* Subroutine of build_binary_op, used for comparison operations.
1676 See if the operands have both been converted from subword integer types
1677 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
1678 This function is also responsible for converting the two operands
1679 to the proper common type for comparison.
b30f223b
RS
1680
1681 The arguments of this function are all pointers to local variables
1682 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1683 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1684
1685 If this function returns nonzero, it means that the comparison has
1686 a constant value. What this function returns is an expression for
1687 that value. */
1688
1689tree
1690shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1691 tree *op0_ptr, *op1_ptr;
1692 tree *restype_ptr;
1693 enum tree_code *rescode_ptr;
1694{
1695 register tree type;
1696 tree op0 = *op0_ptr;
1697 tree op1 = *op1_ptr;
1698 int unsignedp0, unsignedp1;
1699 int real1, real2;
1700 tree primop0, primop1;
1701 enum tree_code code = *rescode_ptr;
1702
1703 /* Throw away any conversions to wider types
1704 already present in the operands. */
1705
1706 primop0 = get_narrower (op0, &unsignedp0);
1707 primop1 = get_narrower (op1, &unsignedp1);
1708
1709 /* Handle the case that OP0 does not *contain* a conversion
1710 but it *requires* conversion to FINAL_TYPE. */
1711
1712 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1713 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1714 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1715 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1716
1717 /* If one of the operands must be floated, we cannot optimize. */
1718 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1719 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1720
1721 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
1722 so value is preserved), for canonicalization. Don't do this if
1723 the second arg is 0. */
b30f223b 1724
5af6001b
RK
1725 if (TREE_CONSTANT (primop0)
1726 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
1727 {
1728 register tree tem = primop0;
1729 register int temi = unsignedp0;
1730 primop0 = primop1;
1731 primop1 = tem;
1732 tem = op0;
1733 op0 = op1;
1734 op1 = tem;
1735 *op0_ptr = op0;
1736 *op1_ptr = op1;
1737 unsignedp0 = unsignedp1;
1738 unsignedp1 = temi;
1739 temi = real1;
1740 real1 = real2;
1741 real2 = temi;
1742
1743 switch (code)
1744 {
1745 case LT_EXPR:
1746 code = GT_EXPR;
1747 break;
1748 case GT_EXPR:
1749 code = LT_EXPR;
1750 break;
1751 case LE_EXPR:
1752 code = GE_EXPR;
1753 break;
1754 case GE_EXPR:
1755 code = LE_EXPR;
1756 break;
1757 }
1758 *rescode_ptr = code;
1759 }
1760
1761 /* If comparing an integer against a constant more bits wide,
1762 maybe we can deduce a value of 1 or 0 independent of the data.
1763 Or else truncate the constant now
1764 rather than extend the variable at run time.
1765
1766 This is only interesting if the constant is the wider arg.
1767 Also, it is not safe if the constant is unsigned and the
1768 variable arg is signed, since in this case the variable
1769 would be sign-extended and then regarded as unsigned.
1770 Our technique fails in this case because the lowest/highest
1771 possible unsigned results don't follow naturally from the
1772 lowest/highest possible values of the variable operand.
1773 For just EQ_EXPR and NE_EXPR there is another technique that
1774 could be used: see if the constant can be faithfully represented
1775 in the other operand's type, by truncating it and reextending it
1776 and see if that preserves the constant's value. */
1777
1778 if (!real1 && !real2
1779 && TREE_CODE (primop1) == INTEGER_CST
1780 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1781 {
1782 int min_gt, max_gt, min_lt, max_lt;
1783 tree maxval, minval;
1784 /* 1 if comparison is nominally unsigned. */
1785 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1786 tree val;
1787
1788 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1789
1790 maxval = TYPE_MAX_VALUE (type);
1791 minval = TYPE_MIN_VALUE (type);
1792
1793 if (unsignedp && !unsignedp0)
1794 *restype_ptr = signed_type (*restype_ptr);
1795
1796 if (TREE_TYPE (primop1) != *restype_ptr)
1797 primop1 = convert (*restype_ptr, primop1);
1798 if (type != *restype_ptr)
1799 {
1800 minval = convert (*restype_ptr, minval);
1801 maxval = convert (*restype_ptr, maxval);
1802 }
1803
1804 if (unsignedp && unsignedp0)
1805 {
1806 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1807 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1808 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1809 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1810 }
1811 else
1812 {
1813 min_gt = INT_CST_LT (primop1, minval);
1814 max_gt = INT_CST_LT (primop1, maxval);
1815 min_lt = INT_CST_LT (minval, primop1);
1816 max_lt = INT_CST_LT (maxval, primop1);
1817 }
1818
1819 val = 0;
1820 /* This used to be a switch, but Genix compiler can't handle that. */
1821 if (code == NE_EXPR)
1822 {
1823 if (max_lt || min_gt)
a360da3a 1824 val = boolean_true_node;
b30f223b
RS
1825 }
1826 else if (code == EQ_EXPR)
1827 {
1828 if (max_lt || min_gt)
a360da3a 1829 val = boolean_false_node;
b30f223b
RS
1830 }
1831 else if (code == LT_EXPR)
1832 {
1833 if (max_lt)
a360da3a 1834 val = boolean_true_node;
b30f223b 1835 if (!min_lt)
a360da3a 1836 val = boolean_false_node;
b30f223b
RS
1837 }
1838 else if (code == GT_EXPR)
1839 {
1840 if (min_gt)
a360da3a 1841 val = boolean_true_node;
b30f223b 1842 if (!max_gt)
a360da3a 1843 val = boolean_false_node;
b30f223b
RS
1844 }
1845 else if (code == LE_EXPR)
1846 {
1847 if (!max_gt)
a360da3a 1848 val = boolean_true_node;
b30f223b 1849 if (min_gt)
a360da3a 1850 val = boolean_false_node;
b30f223b
RS
1851 }
1852 else if (code == GE_EXPR)
1853 {
1854 if (!min_lt)
a360da3a 1855 val = boolean_true_node;
b30f223b 1856 if (max_lt)
a360da3a 1857 val = boolean_false_node;
b30f223b
RS
1858 }
1859
1860 /* If primop0 was sign-extended and unsigned comparison specd,
1861 we did a signed comparison above using the signed type bounds.
1862 But the comparison we output must be unsigned.
1863
1864 Also, for inequalities, VAL is no good; but if the signed
1865 comparison had *any* fixed result, it follows that the
1866 unsigned comparison just tests the sign in reverse
1867 (positive values are LE, negative ones GE).
1868 So we can generate an unsigned comparison
1869 against an extreme value of the signed type. */
1870
1871 if (unsignedp && !unsignedp0)
1872 {
1873 if (val != 0)
1874 switch (code)
1875 {
1876 case LT_EXPR:
1877 case GE_EXPR:
1878 primop1 = TYPE_MIN_VALUE (type);
1879 val = 0;
1880 break;
1881
1882 case LE_EXPR:
1883 case GT_EXPR:
1884 primop1 = TYPE_MAX_VALUE (type);
1885 val = 0;
1886 break;
1887 }
1888 type = unsigned_type (type);
1889 }
1890
b7c9c707 1891 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
1892 {
1893 /* This is the case of (char)x >?< 0x80, which people used to use
1894 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 1895 if (val == boolean_false_node)
b30f223b 1896 warning ("comparison is always 0 due to limited range of data type");
a360da3a 1897 if (val == boolean_true_node)
b30f223b
RS
1898 warning ("comparison is always 1 due to limited range of data type");
1899 }
1900
b7c9c707 1901 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 1902 {
1e276c4a 1903 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 1904 if (val == boolean_false_node)
b30f223b 1905 warning ("comparison is always 0 due to limited range of data type");
a360da3a 1906 if (val == boolean_true_node)
b30f223b
RS
1907 warning ("comparison is always 1 due to limited range of data type");
1908 }
1909
1910 if (val != 0)
1911 {
1912 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1913 if (TREE_SIDE_EFFECTS (primop0))
1914 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1915 return val;
1916 }
1917
1918 /* Value is not predetermined, but do the comparison
1919 in the type of the operand that is not constant.
1920 TYPE is already properly set. */
1921 }
1922 else if (real1 && real2
766f6c30
RS
1923 && (TYPE_PRECISION (TREE_TYPE (primop0))
1924 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
1925 type = TREE_TYPE (primop0);
1926
1927 /* If args' natural types are both narrower than nominal type
1928 and both extend in the same manner, compare them
1929 in the type of the wider arg.
1930 Otherwise must actually extend both to the nominal
1931 common type lest different ways of extending
1932 alter the result.
1933 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1934
1935 else if (unsignedp0 == unsignedp1 && real1 == real2
1936 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1937 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1938 {
1939 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1940 type = signed_or_unsigned_type (unsignedp0
1941 || TREE_UNSIGNED (*restype_ptr),
1942 type);
1943 /* Make sure shorter operand is extended the right way
1944 to match the longer operand. */
1945 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1946 primop0);
1947 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1948 primop1);
1949 }
1950 else
1951 {
1952 /* Here we must do the comparison on the nominal type
1953 using the args exactly as we received them. */
1954 type = *restype_ptr;
1955 primop0 = op0;
1956 primop1 = op1;
1957
1958 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 1959 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
1960 {
1961 tree value = 0;
1962 switch (code)
1963 {
1964 case GE_EXPR:
5af6001b
RK
1965 /* All unsigned values are >= 0, so we warn if extra warnings
1966 are requested. However, if OP0 is a constant that is
1967 >= 0, the signedness of the comparison isn't an issue,
1968 so suppress the warning. */
1969 if (extra_warnings
1970 && ! (TREE_CODE (primop0) == INTEGER_CST
1971 && ! TREE_OVERFLOW (convert (signed_type (type),
1972 primop0))))
b30f223b 1973 warning ("unsigned value >= 0 is always 1");
a360da3a 1974 value = boolean_true_node;
b30f223b
RS
1975 break;
1976
1977 case LT_EXPR:
5af6001b
RK
1978 if (extra_warnings
1979 && ! (TREE_CODE (primop0) == INTEGER_CST
1980 && ! TREE_OVERFLOW (convert (signed_type (type),
1981 primop0))))
b30f223b 1982 warning ("unsigned value < 0 is always 0");
a360da3a 1983 value = boolean_false_node;
b30f223b
RS
1984 }
1985
1986 if (value != 0)
1987 {
1988 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1989 if (TREE_SIDE_EFFECTS (primop0))
1990 return build (COMPOUND_EXPR, TREE_TYPE (value),
1991 primop0, value);
1992 return value;
1993 }
1994 }
1995 }
1996
1997 *op0_ptr = convert (type, primop0);
1998 *op1_ptr = convert (type, primop1);
1999
a360da3a 2000 *restype_ptr = boolean_type_node;
b30f223b
RS
2001
2002 return 0;
2003}
2004\f
2005/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2006 or validate its data type for an `if' or `while' statement or ?..: exp.
2007
2008 This preparation consists of taking the ordinary
2009 representation of an expression expr and producing a valid tree
2010 boolean expression describing whether expr is nonzero. We could
a360da3a 2011 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2012 but we optimize comparisons, &&, ||, and !.
2013
a360da3a 2014 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2015
2016tree
2017truthvalue_conversion (expr)
2018 tree expr;
2019{
257e61ed
RS
2020 if (TREE_CODE (expr) == ERROR_MARK)
2021 return expr;
2022
d7c83727 2023#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2024 /* These really should return error_mark_node after 2.4 is stable.
2025 But not all callers handle ERROR_MARK properly. */
2026 switch (TREE_CODE (TREE_TYPE (expr)))
2027 {
2028 case RECORD_TYPE:
2029 error ("struct type value used where scalar is required");
a360da3a 2030 return boolean_false_node;
257e61ed
RS
2031
2032 case UNION_TYPE:
2033 error ("union type value used where scalar is required");
a360da3a 2034 return boolean_false_node;
257e61ed
RS
2035
2036 case ARRAY_TYPE:
2037 error ("array type value used where scalar is required");
a360da3a 2038 return boolean_false_node;
257e61ed
RS
2039
2040 default:
2041 break;
2042 }
d7c83727 2043#endif /* 0 */
257e61ed 2044
b30f223b
RS
2045 switch (TREE_CODE (expr))
2046 {
2047 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2048 or comparison expressions as truth values at this level. */
2049#if 0
2050 case COMPONENT_REF:
2051 /* A one-bit unsigned bit-field is already acceptable. */
2052 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2053 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2054 return expr;
2055 break;
2056#endif
2057
2058 case EQ_EXPR:
2059 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2060 or comparison expressions as truth values at this level. */
2061#if 0
2062 if (integer_zerop (TREE_OPERAND (expr, 1)))
2063 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2064#endif
2065 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2066 case TRUTH_ANDIF_EXPR:
2067 case TRUTH_ORIF_EXPR:
2068 case TRUTH_AND_EXPR:
2069 case TRUTH_OR_EXPR:
9379fac9 2070 case TRUTH_XOR_EXPR:
1180eb10 2071 case TRUTH_NOT_EXPR:
a360da3a
JM
2072 TREE_TYPE (expr) = boolean_type_node;
2073 return expr;
18c0f675 2074
b30f223b
RS
2075 case ERROR_MARK:
2076 return expr;
2077
2078 case INTEGER_CST:
a360da3a 2079 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2080
2081 case REAL_CST:
a360da3a 2082 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2083
2084 case ADDR_EXPR:
fc0c675f
RK
2085 /* If we are taking the address of a external decl, it might be zero
2086 if it is weak, so we cannot optimize. */
2087 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2088 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2089 break;
2090
b30f223b 2091 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2092 return build (COMPOUND_EXPR, boolean_type_node,
2093 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2094 else
a360da3a 2095 return boolean_true_node;
b30f223b 2096
766f6c30 2097 case COMPLEX_EXPR:
f0b996c5 2098 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2099 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2100 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2101 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2102 0);
2103
b30f223b
RS
2104 case NEGATE_EXPR:
2105 case ABS_EXPR:
2106 case FLOAT_EXPR:
2107 case FFS_EXPR:
2108 /* These don't change whether an object is non-zero or zero. */
2109 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2110
2111 case LROTATE_EXPR:
2112 case RROTATE_EXPR:
2113 /* These don't change whether an object is zero or non-zero, but
2114 we can't ignore them if their second arg has side-effects. */
2115 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2116 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2117 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2118 else
2119 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2120
2121 case COND_EXPR:
2122 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2123 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
2124 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2125 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2126
2127 case CONVERT_EXPR:
2128 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2129 since that affects how `default_conversion' will behave. */
2130 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2131 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2132 break;
2133 /* fall through... */
2134 case NOP_EXPR:
2135 /* If this is widening the argument, we can ignore it. */
2136 if (TYPE_PRECISION (TREE_TYPE (expr))
2137 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2138 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2139 break;
2140
b30f223b 2141 case MINUS_EXPR:
f87550e0
JW
2142 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2143 this case. */
2144 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2145 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2146 break;
2147 /* fall through... */
2148 case BIT_XOR_EXPR:
d7c83727 2149 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2150 two objects. */
b30f223b
RS
2151 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2152 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2153 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2154 TREE_OPERAND (expr, 1), 1);
2155 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2156 fold (build1 (NOP_EXPR,
2157 TREE_TYPE (TREE_OPERAND (expr, 0)),
2158 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2159
fb48b1f0 2160 case BIT_AND_EXPR:
58cee643
RK
2161 if (integer_onep (TREE_OPERAND (expr, 1))
2162 && TREE_TYPE (expr) != boolean_type_node)
2163 /* Using convert here would cause infinite recursion. */
2164 return build1 (NOP_EXPR, boolean_type_node, expr);
2165 break;
fb48b1f0 2166
e2aab13d
RS
2167 case MODIFY_EXPR:
2168 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2169 warning ("suggest parentheses around assignment used as truth value");
2170 break;
b30f223b
RS
2171 }
2172
f0b996c5
RS
2173 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2174 return (build_binary_op
2175 ((TREE_SIDE_EFFECTS (expr)
b839fb3f 2176 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2177 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2178 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2179 0));
2180
b30f223b
RS
2181 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2182}
2183\f
2184/* Read the rest of a #-directive from input stream FINPUT.
2185 In normal use, the directive name and the white space after it
2186 have already been read, so they won't be included in the result.
2187 We allow for the fact that the directive line may contain
2188 a newline embedded within a character or string literal which forms
2189 a part of the directive.
2190
2191 The value is a string in a reusable buffer. It remains valid
05a81fe5
DE
2192 only until the next time this function is called.
2193
2194 The terminating character ('\n' or EOF) is left in FINPUT for the
2195 caller to re-read. */
b30f223b
RS
2196
2197char *
2198get_directive_line (finput)
2199 register FILE *finput;
2200{
2201 static char *directive_buffer = NULL;
2202 static unsigned buffer_length = 0;
2203 register char *p;
2204 register char *buffer_limit;
2205 register int looking_for = 0;
2206 register int char_escaped = 0;
2207
2208 if (buffer_length == 0)
2209 {
2210 directive_buffer = (char *)xmalloc (128);
2211 buffer_length = 128;
2212 }
2213
2214 buffer_limit = &directive_buffer[buffer_length];
2215
2216 for (p = directive_buffer; ; )
2217 {
2218 int c;
2219
2220 /* Make buffer bigger if it is full. */
2221 if (p >= buffer_limit)
2222 {
2223 register unsigned bytes_used = (p - directive_buffer);
2224
2225 buffer_length *= 2;
2226 directive_buffer
2227 = (char *)xrealloc (directive_buffer, buffer_length);
2228 p = &directive_buffer[bytes_used];
2229 buffer_limit = &directive_buffer[buffer_length];
2230 }
2231
2232 c = getc (finput);
2233
2234 /* Discard initial whitespace. */
2235 if ((c == ' ' || c == '\t') && p == directive_buffer)
2236 continue;
2237
2238 /* Detect the end of the directive. */
05a81fe5
DE
2239 if (looking_for == 0
2240 && (c == '\n' || c == EOF))
b30f223b
RS
2241 {
2242 ungetc (c, finput);
2243 c = '\0';
2244 }
2245
2246 *p++ = c;
2247
2248 if (c == 0)
2249 return directive_buffer;
2250
2251 /* Handle string and character constant syntax. */
2252 if (looking_for)
2253 {
2254 if (looking_for == c && !char_escaped)
2255 looking_for = 0; /* Found terminator... stop looking. */
2256 }
2257 else
2258 if (c == '\'' || c == '"')
2259 looking_for = c; /* Don't stop buffering until we see another
2260 another one of these (or an EOF). */
2261
2262 /* Handle backslash. */
2263 char_escaped = (c == '\\' && ! char_escaped);
2264 }
2265}
0b73773c
NH
2266\f
2267/* Make a variant type in the proper way for C/C++, propagating qualifiers
2268 down to the element type of an array. */
2269
2270tree
2271c_build_type_variant (type, constp, volatilep)
2272 tree type;
2273 int constp, volatilep;
2274{
2275 if (TREE_CODE (type) == ARRAY_TYPE)
2276 {
084b6d7b
RK
2277 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2278
2279 push_obstacks (TYPE_OBSTACK (real_main_variant),
2280 TYPE_OBSTACK (real_main_variant));
0b73773c
NH
2281 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2282 constp, volatilep),
2283 TYPE_DOMAIN (type));
ba5ce70d 2284
084b6d7b
RK
2285 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2286 make a copy. (TYPE might have come from the hash table and
2287 REAL_MAIN_VARIANT might be in some function's obstack.) */
2288
2289 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2290 {
2291 type = copy_node (type);
2292 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2293 }
2294
2295 TYPE_MAIN_VARIANT (type) = real_main_variant;
2296 pop_obstacks ();
0b73773c
NH
2297 }
2298 return build_type_variant (type, constp, volatilep);
2299}