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