]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
reload1.c (emit_reload_insns): Accept a new arg for the bb.
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
e5e809f4 2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 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"
670ee920 22#include "system.h"
b30f223b
RS
23#include "tree.h"
24#include "c-lex.h"
25#include "c-tree.h"
26#include "flags.h"
1bb8e5b1 27#include "obstack.h"
5f6da302 28#include "toplev.h"
d6f4ec51 29#include "output.h"
ccd043a9 30
c8724862
DB
31#if USE_CPPLIB
32#include "cpplib.h"
33cpp_reader parse_in;
34cpp_options parse_options;
35static enum cpp_token cpp_token;
36#endif
37
fef610be
JW
38#ifndef WCHAR_TYPE_SIZE
39#ifdef INT_TYPE_SIZE
40#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
41#else
42#define WCHAR_TYPE_SIZE BITS_PER_WORD
43#endif
44#endif
45
0b73773c
NH
46extern struct obstack permanent_obstack;
47
e78a3b42
RK
48/* Nonzero means the expression being parsed will never be evaluated.
49 This is a count, since unevaluated expressions can nest. */
50int skip_evaluation;
51
2786cbad 52enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
07417085 53 A_NO_INSTRUMENT_FUNCTION,
bbb1ae01 54 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
2ce3c6c6
JM
55 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS,
56 A_INIT_PRIORITY};
53065596 57
bb72a084
PE
58enum format_type { printf_format_type, scanf_format_type,
59 strftime_format_type };
60
53065596
RK
61static void declare_hidden_char_array PROTO((char *, char *));
62static void add_attribute PROTO((enum attrs, char *,
63 int, int, int));
64static void init_attributes PROTO((void));
bb72a084
PE
65static void record_function_format PROTO((tree, tree, enum format_type,
66 int, int));
0161e8da 67static void record_international_format PROTO((tree, tree, int));
4724b3de 68
0a7ed33c
BS
69/* Keep a stack of if statements. We record the number of compound
70 statements seen up to the if keyword, as well as the line number
71 and file of the if. If a potentially ambiguous else is seen, that
72 fact is recorded; the warning is issued when we can be sure that
73 the enclosing if statement does not have an else branch. */
74typedef struct
75{
76 int compstmt_count;
77 int line;
78 char *file;
79 int needs_warning;
80} if_elt;
81
82static if_elt *if_stack;
6d819282
MK
83
84/* Amount of space in the if statement stack. */
85static int if_stack_space = 0;
86
87/* Stack pointer. */
88static int if_stack_pointer = 0;
89
0a7ed33c
BS
90/* Generate RTL for the start of an if-then, and record the start of it
91 for ambiguous else detection. */
92
2ce3c6c6
JM
93/* A list of objects which have constructors or destructors which
94 reside in the global scope, and have an init_priority attribute
95 associated with them. The decl is stored in the TREE_VALUE slot
96 and the priority number is stored in the TREE_PURPOSE slot. */
97tree static_aggregates_initp;
98
6d819282
MK
99void
100c_expand_start_cond (cond, exitflag, compstmt_count)
101 tree cond;
102 int exitflag;
103 int compstmt_count;
104{
105 /* Make sure there is enough space on the stack. */
106 if (if_stack_space == 0)
107 {
108 if_stack_space = 10;
0a7ed33c 109 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
110 }
111 else if (if_stack_space == if_stack_pointer)
112 {
113 if_stack_space += 10;
0a7ed33c 114 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 115 }
0a7ed33c 116
6d819282 117 /* Record this if statement. */
0a7ed33c
BS
118 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
119 if_stack[if_stack_pointer].file = input_filename;
120 if_stack[if_stack_pointer].line = lineno;
121 if_stack[if_stack_pointer].needs_warning = 0;
122 if_stack_pointer++;
6d819282
MK
123
124 expand_start_cond (cond, exitflag);
125}
126
0a7ed33c
BS
127/* Generate RTL for the end of an if-then. Optionally warn if a nested
128 if statement had an ambiguous else clause. */
129
6d819282
MK
130void
131c_expand_end_cond ()
132{
133 if_stack_pointer--;
0a7ed33c
BS
134 if (if_stack[if_stack_pointer].needs_warning)
135 warning_with_file_and_line (if_stack[if_stack_pointer].file,
136 if_stack[if_stack_pointer].line,
137 "suggest explicit braces to avoid ambiguous `else'");
6d819282
MK
138 expand_end_cond ();
139}
140
0a7ed33c
BS
141/* Generate RTL between the then-clause and the else-clause
142 of an if-then-else. */
143
6d819282
MK
144void
145c_expand_start_else ()
146{
0a7ed33c
BS
147 /* An ambiguous else warning must be generated for the enclosing if
148 statement, unless we see an else branch for that one, too. */
6d819282
MK
149 if (warn_parentheses
150 && if_stack_pointer > 1
0a7ed33c
BS
151 && (if_stack[if_stack_pointer - 1].compstmt_count
152 == if_stack[if_stack_pointer - 2].compstmt_count))
153 if_stack[if_stack_pointer - 2].needs_warning = 1;
154
155 /* Even if a nested if statement had an else branch, it can't be
156 ambiguous if this one also has an else. So don't warn in that
157 case. Also don't warn for any if statements nested in this else. */
158 if_stack[if_stack_pointer - 1].needs_warning = 0;
159 if_stack[if_stack_pointer - 1].compstmt_count--;
6d819282
MK
160
161 expand_start_else ();
162}
163
b032c74c 164/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2
RS
165
166void
167declare_function_name ()
168{
7da551a2
RS
169 char *name, *printable_name;
170
171 if (current_function_decl == NULL)
172 {
173 name = "";
174 printable_name = "top level";
175 }
176 else
177 {
6152f876
RS
178 /* Allow functions to be nameless (such as artificial ones). */
179 if (DECL_NAME (current_function_decl))
180 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
181 else
182 name = "";
a1d7ffe3 183 printable_name = (*decl_printable_name) (current_function_decl, 2);
7da551a2
RS
184 }
185
4724b3de
RS
186 declare_hidden_char_array ("__FUNCTION__", name);
187 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
188}
97d17ac2 189
4724b3de
RS
190static void
191declare_hidden_char_array (name, value)
192 char *name, *value;
193{
194 tree decl, type, init;
195 int vlen;
7da551a2 196
4724b3de 197 /* If the default size of char arrays isn't big enough for the name,
700942a0 198 or if we want to give warnings for large objects, make a bigger one. */
4724b3de 199 vlen = strlen (value) + 1;
97d17ac2 200 type = char_array_type_node;
a62e870c 201 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
700942a0 202 || warn_larger_than)
97d17ac2 203 type = build_array_type (char_type_node,
4724b3de 204 build_index_type (build_int_2 (vlen, 0)));
7da551a2 205 push_obstacks_nochange ();
4724b3de 206 decl = build_decl (VAR_DECL, get_identifier (name), type);
7da551a2
RS
207 TREE_STATIC (decl) = 1;
208 TREE_READONLY (decl) = 1;
4724b3de 209 TREE_ASM_WRITTEN (decl) = 1;
b9a24ad4 210 DECL_SOURCE_LINE (decl) = 0;
d2eb0876 211 DECL_ARTIFICIAL (decl) = 1;
176e81eb 212 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 213 DECL_IGNORED_P (decl) = 1;
4724b3de 214 init = build_string (vlen, value);
97d17ac2 215 TREE_TYPE (init) = type;
7da551a2
RS
216 DECL_INITIAL (decl) = init;
217 finish_decl (pushdecl (decl), init, NULL_TREE);
218}
219
b30f223b
RS
220/* Given a chain of STRING_CST nodes,
221 concatenate them into one STRING_CST
222 and give it a suitable array-of-chars data type. */
223
224tree
225combine_strings (strings)
226 tree strings;
227{
228 register tree value, t;
229 register int length = 1;
230 int wide_length = 0;
231 int wide_flag = 0;
232 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
233 int nchars;
234
235 if (TREE_CHAIN (strings))
236 {
237 /* More than one in the chain, so concatenate. */
238 register char *p, *q;
239
240 /* Don't include the \0 at the end of each substring,
241 except for the last one.
242 Count wide strings and ordinary strings separately. */
243 for (t = strings; t; t = TREE_CHAIN (t))
244 {
245 if (TREE_TYPE (t) == wchar_array_type_node)
246 {
247 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
248 wide_flag = 1;
249 }
250 else
251 length += (TREE_STRING_LENGTH (t) - 1);
252 }
253
254 /* If anything is wide, the non-wides will be converted,
255 which makes them take more space. */
256 if (wide_flag)
257 length = length * wchar_bytes + wide_length;
258
259 p = savealloc (length);
260
261 /* Copy the individual strings into the new combined string.
262 If the combined string is wide, convert the chars to ints
263 for any individual strings that are not wide. */
264
265 q = p;
266 for (t = strings; t; t = TREE_CHAIN (t))
267 {
268 int len = (TREE_STRING_LENGTH (t)
269 - ((TREE_TYPE (t) == wchar_array_type_node)
270 ? wchar_bytes : 1));
271 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
272 {
273 bcopy (TREE_STRING_POINTER (t), q, len);
274 q += len;
275 }
276 else
277 {
278 int i;
279 for (i = 0; i < len; i++)
41bbd14e
JW
280 {
281 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
282 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
283 else
284 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
285 }
b30f223b
RS
286 q += len * wchar_bytes;
287 }
288 }
289 if (wide_flag)
290 {
291 int i;
292 for (i = 0; i < wchar_bytes; i++)
293 *q++ = 0;
294 }
295 else
296 *q = 0;
297
298 value = make_node (STRING_CST);
299 TREE_STRING_POINTER (value) = p;
300 TREE_STRING_LENGTH (value) = length;
b30f223b
RS
301 }
302 else
303 {
304 value = strings;
305 length = TREE_STRING_LENGTH (value);
306 if (TREE_TYPE (value) == wchar_array_type_node)
307 wide_flag = 1;
308 }
309
b57062ca 310 /* Compute the number of elements, for the array type. */
b30f223b
RS
311 nchars = wide_flag ? length / wchar_bytes : length;
312
313 /* Create the array type for the string constant.
314 -Wwrite-strings says make the string constant an array of const char
d9cf7c82
JM
315 so that copying it to a non-const pointer will get a warning.
316 For C++, this is the standard behavior. */
317 if (flag_const_strings
b30f223b
RS
318 && (! flag_traditional && ! flag_writable_strings))
319 {
320 tree elements
321 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
322 1, 0);
323 TREE_TYPE (value)
324 = build_array_type (elements,
325 build_index_type (build_int_2 (nchars - 1, 0)));
326 }
327 else
328 TREE_TYPE (value)
329 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
330 build_index_type (build_int_2 (nchars - 1, 0)));
d9cf7c82
JM
331
332 TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
b30f223b
RS
333 TREE_STATIC (value) = 1;
334 return value;
335}
336\f
53065596
RK
337/* To speed up processing of attributes, we maintain an array of
338 IDENTIFIER_NODES and the corresponding attribute types. */
339
340/* Array to hold attribute information. */
341
342static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
343
344static int attrtab_idx = 0;
345
346/* Add an entry to the attribute table above. */
347
348static void
349add_attribute (id, string, min_len, max_len, decl_req)
350 enum attrs id;
351 char *string;
352 int min_len, max_len;
353 int decl_req;
354{
355 char buf[100];
356
357 attrtab[attrtab_idx].id = id;
358 attrtab[attrtab_idx].name = get_identifier (string);
359 attrtab[attrtab_idx].min = min_len;
360 attrtab[attrtab_idx].max = max_len;
361 attrtab[attrtab_idx++].decl_req = decl_req;
362
363 sprintf (buf, "__%s__", string);
364
365 attrtab[attrtab_idx].id = id;
366 attrtab[attrtab_idx].name = get_identifier (buf);
367 attrtab[attrtab_idx].min = min_len;
368 attrtab[attrtab_idx].max = max_len;
369 attrtab[attrtab_idx++].decl_req = decl_req;
370}
371
372/* Initialize attribute table. */
373
374static void
375init_attributes ()
376{
a89ca5c6 377 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 378 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 379 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
380 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
381 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 382 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
383 add_attribute (A_CONST, "const", 0, 0, 1);
384 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
385 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
386 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
387 add_attribute (A_MODE, "mode", 1, 1, 1);
388 add_attribute (A_SECTION, "section", 1, 1, 1);
389 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 390 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 391 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
392 add_attribute (A_WEAK, "weak", 0, 0, 1);
393 add_attribute (A_ALIAS, "alias", 1, 1, 1);
2ce3c6c6 394 add_attribute (A_INIT_PRIORITY, "init_priority", 0, 1, 0);
07417085 395 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
53065596
RK
396}
397\f
1228e2a6 398/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
399 and install them in NODE, which is either a DECL (including a TYPE_DECL)
400 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
0f41302f 401 and declaration modifiers but before the declaration proper. */
b30f223b
RS
402
403void
53065596
RK
404decl_attributes (node, attributes, prefix_attributes)
405 tree node, attributes, prefix_attributes;
b30f223b 406{
a16b4c9c
JW
407 tree decl = 0, type = 0;
408 int is_type = 0;
53065596
RK
409 tree a;
410
411 if (attrtab_idx == 0)
412 init_attributes ();
413
414 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
415 {
416 decl = node;
417 type = TREE_TYPE (decl);
418 is_type = TREE_CODE (node) == TYPE_DECL;
419 }
420 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
421 type = node, is_type = 1;
a2cd7b45 422
f09db6e0
NC
423#ifdef INSERT_ATTRIBUTES
424 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
425#endif
426
eed32833 427 attributes = chainon (prefix_attributes, attributes);
5289b665 428
b30f223b 429 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
430 {
431 tree name = TREE_PURPOSE (a);
432 tree args = TREE_VALUE (a);
433 int i;
434 enum attrs id;
b57062ca 435
53065596
RK
436 for (i = 0; i < attrtab_idx; i++)
437 if (attrtab[i].name == name)
438 break;
439
6eaba4a7 440 if (i == attrtab_idx)
53065596 441 {
6eaba4a7
DE
442 if (! valid_machine_attribute (name, args, decl, type))
443 warning ("`%s' attribute directive ignored",
444 IDENTIFIER_POINTER (name));
17c1a44f
SC
445 else if (decl != 0)
446 type = TREE_TYPE (decl);
53065596
RK
447 continue;
448 }
449 else if (attrtab[i].decl_req && decl == 0)
450 {
451 warning ("`%s' attribute does not apply to types",
452 IDENTIFIER_POINTER (name));
453 continue;
454 }
455 else if (list_length (args) < attrtab[i].min
456 || list_length (args) > attrtab[i].max)
457 {
458 error ("wrong number of arguments specified for `%s' attribute",
459 IDENTIFIER_POINTER (name));
460 continue;
461 }
462
463 id = attrtab[i].id;
464 switch (id)
465 {
466 case A_PACKED:
1bcf5b08 467 if (is_type)
a89ca5c6
RK
468 TYPE_PACKED (type) = 1;
469 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
470 DECL_PACKED (decl) = 1;
471 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
472 used for DECL_REGISTER. It wouldn't mean anything anyway. */
473 else
474 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
475 break;
476
bbb1ae01
RK
477 case A_NOCOMMON:
478 if (TREE_CODE (decl) == VAR_DECL)
479 DECL_COMMON (decl) = 0;
480 else
481 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
482 break;
483
2786cbad
JM
484 case A_COMMON:
485 if (TREE_CODE (decl) == VAR_DECL)
486 DECL_COMMON (decl) = 1;
487 else
488 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
489 break;
490
53065596
RK
491 case A_NORETURN:
492 if (TREE_CODE (decl) == FUNCTION_DECL)
493 TREE_THIS_VOLATILE (decl) = 1;
494 else if (TREE_CODE (type) == POINTER_TYPE
495 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
b57062ca 496 TREE_TYPE (decl) = type
53065596
RK
497 = build_pointer_type
498 (build_type_variant (TREE_TYPE (type),
499 TREE_READONLY (TREE_TYPE (type)), 1));
500 else
501 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
502 break;
503
1c4fadec 504 case A_UNUSED:
6d819282
MK
505 if (is_type)
506 TREE_USED (type) = 1;
b57062ca 507 else if (TREE_CODE (decl) == PARM_DECL
6d819282
MK
508 || TREE_CODE (decl) == VAR_DECL
509 || TREE_CODE (decl) == FUNCTION_DECL)
1c4fadec
RK
510 TREE_USED (decl) = 1;
511 else
512 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
513 break;
514
53065596
RK
515 case A_CONST:
516 if (TREE_CODE (decl) == FUNCTION_DECL)
517 TREE_READONLY (decl) = 1;
518 else if (TREE_CODE (type) == POINTER_TYPE
519 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
520 TREE_TYPE (decl) = type
521 = build_pointer_type
522 (build_type_variant (TREE_TYPE (type), 1,
523 TREE_THIS_VOLATILE (TREE_TYPE (type))));
524 else
525 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
526 break;
527
528 case A_T_UNION:
1bcf5b08 529 if (is_type
53065596 530 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 531 && (decl == 0
62b1077c
RK
532 || (TYPE_FIELDS (type) != 0
533 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
534 TYPE_TRANSPARENT_UNION (type) = 1;
535 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
536 && TREE_CODE (type) == UNION_TYPE
537 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 538 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
539 else
540 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
541 break;
542
543 case A_CONSTRUCTOR:
544 if (TREE_CODE (decl) == FUNCTION_DECL
545 && TREE_CODE (type) == FUNCTION_TYPE
546 && decl_function_context (decl) == 0)
1a16a053
RK
547 {
548 DECL_STATIC_CONSTRUCTOR (decl) = 1;
549 TREE_USED (decl) = 1;
550 }
53065596
RK
551 else
552 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
553 break;
554
555 case A_DESTRUCTOR:
556 if (TREE_CODE (decl) == FUNCTION_DECL
557 && TREE_CODE (type) == FUNCTION_TYPE
558 && decl_function_context (decl) == 0)
1a16a053
RK
559 {
560 DECL_STATIC_DESTRUCTOR (decl) = 1;
561 TREE_USED (decl) = 1;
562 }
53065596
RK
563 else
564 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
565 break;
566
567 case A_MODE:
568 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
569 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
570 else
571 {
572 int j;
573 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
574 int len = strlen (p);
575 enum machine_mode mode = VOIDmode;
576 tree typefm;
577
578 if (len > 4 && p[0] == '_' && p[1] == '_'
579 && p[len - 1] == '_' && p[len - 2] == '_')
580 {
3438dff9 581 char *newp = (char *) alloca (len - 1);
53065596
RK
582
583 strcpy (newp, &p[2]);
584 newp[len - 4] = '\0';
585 p = newp;
586 }
587
588 /* Give this decl a type with the specified mode.
589 First check for the special modes. */
590 if (! strcmp (p, "byte"))
591 mode = byte_mode;
592 else if (!strcmp (p, "word"))
593 mode = word_mode;
594 else if (! strcmp (p, "pointer"))
595 mode = ptr_mode;
596 else
597 for (j = 0; j < NUM_MACHINE_MODES; j++)
598 if (!strcmp (p, GET_MODE_NAME (j)))
599 mode = (enum machine_mode) j;
600
601 if (mode == VOIDmode)
602 error ("unknown machine mode `%s'", p);
603 else if (0 == (typefm = type_for_mode (mode,
604 TREE_UNSIGNED (type))))
605 error ("no data type for mode `%s'", p);
606 else
607 {
608 TREE_TYPE (decl) = type = typefm;
609 DECL_SIZE (decl) = 0;
610 layout_decl (decl, 0);
611 }
612 }
613 break;
614
615 case A_SECTION:
5289b665 616#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
617 if ((TREE_CODE (decl) == FUNCTION_DECL
618 || TREE_CODE (decl) == VAR_DECL)
619 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
620 {
b57062ca 621 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
622 && current_function_decl != NULL_TREE
623 && ! TREE_STATIC (decl))
53065596
RK
624 error_with_decl (decl,
625 "section attribute cannot be specified for local variables");
626 /* The decl may have already been given a section attribute from
627 a previous declaration. Ensure they match. */
628 else if (DECL_SECTION_NAME (decl) != NULL_TREE
629 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
630 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
631 error_with_decl (node,
632 "section of `%s' conflicts with previous declaration");
633 else
634 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
635 }
636 else
637 error_with_decl (node,
5289b665
DE
638 "section attribute not allowed for `%s'");
639#else
53065596
RK
640 error_with_decl (node,
641 "section attributes are not supported for this target");
5289b665 642#endif
53065596
RK
643 break;
644
645 case A_ALIGNED:
6f38f669 646 {
53065596 647 tree align_expr
54630035
RK
648 = (args ? TREE_VALUE (args)
649 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
53065596 650 int align;
f09db6e0 651
53065596
RK
652 /* Strip any NOPs of any kind. */
653 while (TREE_CODE (align_expr) == NOP_EXPR
654 || TREE_CODE (align_expr) == CONVERT_EXPR
655 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
656 align_expr = TREE_OPERAND (align_expr, 0);
b57062ca 657
53065596
RK
658 if (TREE_CODE (align_expr) != INTEGER_CST)
659 {
660 error ("requested alignment is not a constant");
661 continue;
662 }
663
664 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
665
666 if (exact_log2 (align) == -1)
667 error ("requested alignment is not a power of 2");
668 else if (is_type)
06aa52de 669 TYPE_ALIGN (type) = align;
53065596
RK
670 else if (TREE_CODE (decl) != VAR_DECL
671 && TREE_CODE (decl) != FIELD_DECL)
672 error_with_decl (decl,
673 "alignment may not be specified for `%s'");
674 else
675 DECL_ALIGN (decl) = align;
6f38f669 676 }
53065596 677 break;
6f38f669 678
53065596 679 case A_FORMAT:
b30f223b 680 {
bb72a084 681 tree format_type_id = TREE_VALUE (args);
53065596
RK
682 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
683 tree first_arg_num_expr
684 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
685 int format_num;
686 int first_arg_num;
bb72a084 687 enum format_type format_type;
53065596
RK
688 tree argument;
689 int arg_num;
b57062ca 690
53065596
RK
691 if (TREE_CODE (decl) != FUNCTION_DECL)
692 {
693 error_with_decl (decl,
694 "argument format specified for non-function `%s'");
695 continue;
696 }
f5963e61 697
bb72a084 698 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
0161e8da 699 {
bb72a084 700 error ("unrecognized format specifier");
0161e8da
RK
701 continue;
702 }
53065596
RK
703 else
704 {
bb72a084 705 char *p = IDENTIFIER_POINTER (format_type_id);
f5963e61 706
bb72a084
PE
707 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
708 format_type = printf_format_type;
709 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
710 format_type = scanf_format_type;
711 else if (!strcmp (p, "strftime")
712 || !strcmp (p, "__strftime__"))
713 format_type = strftime_format_type;
714 else
715 {
716 error ("`%s' is an unrecognized format function type", p);
717 continue;
718 }
53065596 719 }
6f38f669 720
53065596
RK
721 /* Strip any conversions from the string index and first arg number
722 and verify they are constants. */
723 while (TREE_CODE (format_num_expr) == NOP_EXPR
724 || TREE_CODE (format_num_expr) == CONVERT_EXPR
725 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
726 format_num_expr = TREE_OPERAND (format_num_expr, 0);
677ff441 727
53065596
RK
728 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
729 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
730 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
731 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
732
733 if (TREE_CODE (format_num_expr) != INTEGER_CST
734 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
09e3dd72 735 {
53065596
RK
736 error ("format string has non-constant operand number");
737 continue;
09e3dd72 738 }
53065596
RK
739
740 format_num = TREE_INT_CST_LOW (format_num_expr);
741 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
742 if (first_arg_num != 0 && first_arg_num <= format_num)
68a91d8d 743 {
53065596 744 error ("format string arg follows the args to be formatted");
6f38f669 745 continue;
68a91d8d 746 }
53065596
RK
747
748 /* If a parameter list is specified, verify that the format_num
749 argument is actually a string, in case the format attribute
750 is in error. */
751 argument = TYPE_ARG_TYPES (type);
752 if (argument)
09e3dd72 753 {
53065596 754 for (arg_num = 1; ; ++arg_num)
09e3dd72 755 {
53065596
RK
756 if (argument == 0 || arg_num == format_num)
757 break;
758 argument = TREE_CHAIN (argument);
759 }
760 if (! argument
761 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
762 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
763 != char_type_node))
764 {
765 error ("format string arg not a string type");
09e3dd72
RK
766 continue;
767 }
53065596
RK
768 if (first_arg_num != 0)
769 {
770 /* Verify that first_arg_num points to the last arg,
0f41302f 771 the ... */
53065596
RK
772 while (argument)
773 arg_num++, argument = TREE_CHAIN (argument);
774 if (arg_num != first_arg_num)
775 {
776 error ("args to be formatted is not ...");
777 continue;
778 }
779 }
09e3dd72 780 }
a2cd7b45 781
53065596
RK
782 record_function_format (DECL_NAME (decl),
783 DECL_ASSEMBLER_NAME (decl),
bb72a084 784 format_type, format_num, first_arg_num);
53065596
RK
785 break;
786 }
4b8af8d9 787
0161e8da
RK
788 case A_FORMAT_ARG:
789 {
790 tree format_num_expr = TREE_VALUE (args);
791 int format_num, arg_num;
792 tree argument;
b57062ca 793
0161e8da
RK
794 if (TREE_CODE (decl) != FUNCTION_DECL)
795 {
796 error_with_decl (decl,
797 "argument format specified for non-function `%s'");
798 continue;
799 }
b57062ca 800
0161e8da
RK
801 /* Strip any conversions from the first arg number and verify it
802 is a constant. */
803 while (TREE_CODE (format_num_expr) == NOP_EXPR
804 || TREE_CODE (format_num_expr) == CONVERT_EXPR
805 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
806 format_num_expr = TREE_OPERAND (format_num_expr, 0);
807
808 if (TREE_CODE (format_num_expr) != INTEGER_CST)
809 {
810 error ("format string has non-constant operand number");
811 continue;
812 }
813
814 format_num = TREE_INT_CST_LOW (format_num_expr);
815
816 /* If a parameter list is specified, verify that the format_num
817 argument is actually a string, in case the format attribute
818 is in error. */
819 argument = TYPE_ARG_TYPES (type);
820 if (argument)
821 {
822 for (arg_num = 1; ; ++arg_num)
823 {
824 if (argument == 0 || arg_num == format_num)
825 break;
826 argument = TREE_CHAIN (argument);
827 }
828 if (! argument
829 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
830 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
831 != char_type_node))
832 {
833 error ("format string arg not a string type");
834 continue;
835 }
836 }
837
838 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
839 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
840 != char_type_node))
841 {
842 error ("function does not return string type");
843 continue;
844 }
845
846 record_international_format (DECL_NAME (decl),
847 DECL_ASSEMBLER_NAME (decl),
848 format_num);
849 break;
850 }
851
4b8af8d9
JM
852 case A_WEAK:
853 declare_weak (decl);
854 break;
855
856 case A_ALIAS:
857 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5d9dd5a5 858 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
859 error_with_decl (decl,
860 "`%s' defined both normally and as an alias");
861 else if (decl_function_context (decl) == 0)
862 {
20045452
RH
863 tree id;
864
865 id = TREE_VALUE (args);
866 if (TREE_CODE (id) != STRING_CST)
867 {
868 error ("alias arg not a string");
869 break;
870 }
871 id = get_identifier (TREE_STRING_POINTER (id));
872
4b8af8d9
JM
873 if (TREE_CODE (decl) == FUNCTION_DECL)
874 DECL_INITIAL (decl) = error_mark_node;
875 else
876 DECL_EXTERNAL (decl) = 0;
877 assemble_alias (decl, id);
878 }
879 else
880 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
881 break;
07417085 882
2ce3c6c6
JM
883 case A_INIT_PRIORITY:
884 {
885 tree initp_expr = (args ? TREE_VALUE (args): NULL_TREE);
886 int pri;
887
888 if (initp_expr)
889 STRIP_NOPS (initp_expr);
890
891 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
892 {
893 error ("requested init_priority is not an integer constant");
894 continue;
895 }
896
897 pri = TREE_INT_CST_LOW (initp_expr);
898
899 if (is_type || TREE_CODE (decl) != VAR_DECL
900 || ! TREE_STATIC (decl)
901 || DECL_EXTERNAL (decl)
902 || (TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
903 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
904 /* Static objects in functions are initialized the
905 first time control passes through that
906 function. This is not precise enough to pin down an
907 init_priority value, so don't allow it. */
908 || current_function_decl)
909 {
910 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
911 continue;
912 }
913
914 /* Check for init_priorities that are reserved for
915 implementation. Reserved for language and runtime
916 support implementations.*/
917 if ((10 <= pri && pri <= 99)
918 /* Reserved for standard library implementations. */
919 || (500 <= pri && pri <= 999)
920 /* Reserved for objects with no attributes. */
921 || pri > (MAX_INIT_PRIORITY - 50))
922 {
923 warning
924 ("requested init_priority is reserved for internal use");
925 continue;
926 }
927
928 if (pri > MAX_INIT_PRIORITY || pri <= 0)
929 {
930 error ("requested init_priority is out of range");
931 continue;
932 }
933
934 static_aggregates_initp
935 = perm_tree_cons (initp_expr, decl, static_aggregates_initp);
936 break;
937 }
938
07417085
KR
939 case A_NO_INSTRUMENT_FUNCTION:
940 if (TREE_CODE (decl) != FUNCTION_DECL)
941 {
942 error_with_decl (decl,
943 "`%s' attribute applies only to functions",
944 IDENTIFIER_POINTER (name));
945 }
946 else if (DECL_INITIAL (decl))
947 {
948 error_with_decl (decl,
949 "can't set `%s' attribute after definition",
950 IDENTIFIER_POINTER (name));
951 }
952 else
953 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
954 break;
53065596
RK
955 }
956 }
b30f223b 957}
800f4153
RK
958
959/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
960 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
961
962 The head of the declspec list is stored in DECLSPECS.
963 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
964
965 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
966 the list elements. We drop the containing TREE_LIST nodes and link the
967 resulting attributes together the way decl_attributes expects them. */
968
969void
970split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
971 tree specs_attrs;
972 tree *declspecs, *prefix_attributes;
973{
974 tree t, s, a, next, specs, attrs;
975
976 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
977 if (specs_attrs != NULL_TREE
978 && TREE_CODE (specs_attrs) != TREE_LIST)
979 {
980 *declspecs = specs_attrs;
981 *prefix_attributes = NULL_TREE;
982 return;
983 }
984
985 /* Remember to keep the lists in the same order, element-wise. */
986
987 specs = s = NULL_TREE;
988 attrs = a = NULL_TREE;
989 for (t = specs_attrs; t; t = next)
990 {
991 next = TREE_CHAIN (t);
992 /* Declspecs have a non-NULL TREE_VALUE. */
993 if (TREE_VALUE (t) != NULL_TREE)
994 {
995 if (specs == NULL_TREE)
996 specs = s = t;
997 else
998 {
999 TREE_CHAIN (s) = t;
1000 s = t;
1001 }
1002 }
1003 else
1004 {
1005 if (attrs == NULL_TREE)
1006 attrs = a = TREE_PURPOSE (t);
1007 else
1008 {
1009 TREE_CHAIN (a) = TREE_PURPOSE (t);
1010 a = TREE_PURPOSE (t);
1011 }
1012 /* More attrs can be linked here, move A to the end. */
1013 while (TREE_CHAIN (a) != NULL_TREE)
1014 a = TREE_CHAIN (a);
1015 }
1016 }
1017
1018 /* Terminate the lists. */
1019 if (s != NULL_TREE)
1020 TREE_CHAIN (s) = NULL_TREE;
1021 if (a != NULL_TREE)
1022 TREE_CHAIN (a) = NULL_TREE;
1023
1024 /* All done. */
1025 *declspecs = specs;
1026 *prefix_attributes = attrs;
1027}
d9525bec
BK
1028
1029/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1030 This function is used by the parser when a rule will accept attributes
1031 in a particular position, but we don't want to support that just yet.
1032
1033 A warning is issued for every ignored attribute. */
1034
1035tree
1036strip_attrs (specs_attrs)
1037 tree specs_attrs;
1038{
1039 tree specs, attrs;
1040
1041 split_specs_attrs (specs_attrs, &specs, &attrs);
1042
1043 while (attrs)
1044 {
1045 warning ("`%s' attribute ignored",
1046 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1047 attrs = TREE_CHAIN (attrs);
1048 }
1049
1050 return specs;
1051}
b30f223b 1052\f
1ccf251f
RK
1053/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1054 a parameter list. */
1055
1056#define T_I &integer_type_node
1057#define T_L &long_integer_type_node
2314fb70 1058#define T_LL &long_long_integer_type_node
1ccf251f
RK
1059#define T_S &short_integer_type_node
1060#define T_UI &unsigned_type_node
1061#define T_UL &long_unsigned_type_node
2314fb70 1062#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
1063#define T_US &short_unsigned_type_node
1064#define T_F &float_type_node
1065#define T_D &double_type_node
1066#define T_LD &long_double_type_node
1067#define T_C &char_type_node
b57062ca 1068#define T_UC &unsigned_char_type_node
1ccf251f
RK
1069#define T_V &void_type_node
1070#define T_W &wchar_type_node
df8a401a 1071#define T_ST &sizetype
1ccf251f
RK
1072
1073typedef struct {
1074 char *format_chars;
1075 int pointer_count;
1076 /* Type of argument if no length modifier is used. */
1077 tree *nolen;
bc516719 1078 /* Type of argument if length modifier for shortening to byte is used.
1ccf251f 1079 If NULL, then this modifier is not allowed. */
b57062ca 1080 tree *hhlen;
bc516719 1081 /* Type of argument if length modifier for shortening is used.
b57062ca 1082 If NULL, then this modifier is not allowed. */
1ccf251f
RK
1083 tree *hlen;
1084 /* Type of argument if length modifier `l' is used.
1085 If NULL, then this modifier is not allowed. */
1086 tree *llen;
2cedb812 1087 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
1088 If NULL, then this modifier is not allowed. */
1089 tree *qlen;
1ccf251f
RK
1090 /* Type of argument if length modifier `L' is used.
1091 If NULL, then this modifier is not allowed. */
1092 tree *bigllen;
e5e809f4
JL
1093 /* Type of argument if length modifier `Z' is used.
1094 If NULL, then this modifier is not allowed. */
1095 tree *zlen;
1ccf251f
RK
1096 /* List of other modifier characters allowed with these options. */
1097 char *flag_chars;
1098} format_char_info;
1099
1100static format_char_info print_char_table[] = {
b57062ca
UD
1101 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1102 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1103 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
e5e809f4 1104/* A GNU extension. */
b57062ca
UD
1105 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1106 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1107 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1108 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1109 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1110 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1111 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1112 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1ccf251f
RK
1113 { NULL }
1114};
1115
1116static format_char_info scan_char_table[] = {
b57062ca
UD
1117 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1118 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1119 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
c6a9dea8
JW
1120 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1121 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
b57062ca
UD
1122 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1123 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
c6a9dea8 1124 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
b57062ca
UD
1125 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1126 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1ccf251f
RK
1127 { NULL }
1128};
1129
bb72a084
PE
1130/* Handle format characters recognized by glibc's strftime.c.
1131 '2' - MUST do years as only two digits
1132 '3' - MAY do years as only two digits (depending on locale)
1133 'E' - E modifier is acceptable
1134 'O' - O modifier is acceptable to Standard C
1135 'o' - O modifier is acceptable as a GNU extension
1136 'G' - other GNU extensions */
1137
1138static format_char_info time_char_table[] = {
b57062ca
UD
1139 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1140 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1141 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1142 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1143 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1144 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1145 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1146 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1147 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1148 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1149 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1150 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1151 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
bb72a084
PE
1152 { NULL }
1153};
1154
0161e8da
RK
1155typedef struct function_format_info
1156{
1ccf251f
RK
1157 struct function_format_info *next; /* next structure on the list */
1158 tree name; /* identifier such as "printf" */
1159 tree assembler_name; /* optional mangled identifier (for C++) */
bb72a084 1160 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1ccf251f
RK
1161 int format_num; /* number of format argument */
1162 int first_arg_num; /* number of first arg (zero for varargs) */
1163} function_format_info;
1164
1165static function_format_info *function_format_list = NULL;
1166
0161e8da
RK
1167typedef struct international_format_info
1168{
1169 struct international_format_info *next; /* next structure on the list */
1170 tree name; /* identifier such as "gettext" */
1171 tree assembler_name; /* optional mangled identifier (for C++) */
1172 int format_num; /* number of format argument */
1173} international_format_info;
1174
1175static international_format_info *international_format_list = NULL;
1176
1177static void check_format_info PROTO((function_format_info *, tree));
1ccf251f
RK
1178
1179/* Initialize the table of functions to perform format checking on.
1180 The ANSI functions are always checked (whether <stdio.h> is
1181 included or not), since it is common to call printf without
1182 including <stdio.h>. There shouldn't be a problem with this,
1183 since ANSI reserves these function names whether you include the
b57062ca 1184 header file or not. In any case, the checking is harmless.
0161e8da
RK
1185
1186 Also initialize the name of function that modify the format string for
1187 internationalization purposes. */
1ccf251f
RK
1188
1189void
1190init_function_format_info ()
1191{
bb72a084
PE
1192 record_function_format (get_identifier ("printf"), NULL_TREE,
1193 printf_format_type, 1, 2);
1194 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1195 printf_format_type, 2, 3);
1196 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1197 printf_format_type, 2, 3);
1198 record_function_format (get_identifier ("scanf"), NULL_TREE,
1199 scanf_format_type, 1, 2);
1200 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1201 scanf_format_type, 2, 3);
1202 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1203 scanf_format_type, 2, 3);
1204 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1205 printf_format_type, 1, 0);
1206 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1207 printf_format_type, 2, 0);
1208 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1209 printf_format_type, 2, 0);
1210 record_function_format (get_identifier ("strftime"), NULL_TREE,
1211 strftime_format_type, 3, 0);
0161e8da
RK
1212
1213 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1214 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1215 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1ccf251f
RK
1216}
1217
1218/* Record information for argument format checking. FUNCTION_IDENT is
1219 the identifier node for the name of the function to check (its decl
bb72a084
PE
1220 need not exist yet).
1221 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1ccf251f
RK
1222 of the argument which is the format control string (starting from 1).
1223 FIRST_ARG_NUM is the number of the first actual argument to check
0161e8da 1224 against the format string, or zero if no checking is not be done
1ccf251f
RK
1225 (e.g. for varargs such as vfprintf). */
1226
bb72a084
PE
1227static void
1228record_function_format (name, assembler_name, format_type,
1ccf251f
RK
1229 format_num, first_arg_num)
1230 tree name;
1231 tree assembler_name;
bb72a084 1232 enum format_type format_type;
1ccf251f
RK
1233 int format_num;
1234 int first_arg_num;
1235{
1236 function_format_info *info;
1237
1238 /* Re-use existing structure if it's there. */
1239
1240 for (info = function_format_list; info; info = info->next)
1241 {
1242 if (info->name == name && info->assembler_name == assembler_name)
1243 break;
1244 }
1245 if (! info)
1246 {
1247 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1248 info->next = function_format_list;
1249 function_format_list = info;
1250
1251 info->name = name;
1252 info->assembler_name = assembler_name;
1253 }
1254
bb72a084 1255 info->format_type = format_type;
1ccf251f
RK
1256 info->format_num = format_num;
1257 info->first_arg_num = first_arg_num;
1258}
1259
0161e8da
RK
1260/* Record information for the names of function that modify the format
1261 argument to format functions. FUNCTION_IDENT is the identifier node for
1262 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1263 the number of the argument which is the format control string (starting
1264 from 1). */
1265
31837ce2 1266static void
0161e8da
RK
1267record_international_format (name, assembler_name, format_num)
1268 tree name;
1269 tree assembler_name;
1270 int format_num;
1271{
1272 international_format_info *info;
1273
1274 /* Re-use existing structure if it's there. */
1275
1276 for (info = international_format_list; info; info = info->next)
1277 {
1278 if (info->name == name && info->assembler_name == assembler_name)
1279 break;
1280 }
1281
1282 if (! info)
1283 {
1284 info
1285 = (international_format_info *)
1286 xmalloc (sizeof (international_format_info));
1287 info->next = international_format_list;
1288 international_format_list = info;
1289
1290 info->name = name;
1291 info->assembler_name = assembler_name;
1292 }
1293
1294 info->format_num = format_num;
1295}
1296
1ccf251f
RK
1297static char tfaff[] = "too few arguments for format";
1298\f
1299/* Check the argument list of a call to printf, scanf, etc.
1300 NAME is the function identifier.
1301 ASSEMBLER_NAME is the function's assembler identifier.
1302 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1303 PARAMS is the list of argument values. */
1304
1305void
1306check_function_format (name, assembler_name, params)
1307 tree name;
1308 tree assembler_name;
1309 tree params;
1310{
1311 function_format_info *info;
1312
1313 /* See if this function is a format function. */
1314 for (info = function_format_list; info; info = info->next)
1315 {
95216dec 1316 if (info->assembler_name
1ccf251f
RK
1317 ? (info->assembler_name == assembler_name)
1318 : (info->name == name))
1319 {
1320 /* Yup; check it. */
1321 check_format_info (info, params);
1322 break;
1323 }
1324 }
1325}
1326
1327/* Check the argument list of a call to printf, scanf, etc.
1328 INFO points to the function_format_info structure.
1329 PARAMS is the list of argument values. */
1330
1331static void
1332check_format_info (info, params)
1333 function_format_info *info;
1334 tree params;
1335{
1336 int i;
1337 int arg_num;
1338 int suppressed, wide, precise;
f67aab2c 1339 int length_char = 0;
1ccf251f
RK
1340 int format_char;
1341 int format_length;
1342 tree format_tree;
1343 tree cur_param;
1344 tree cur_type;
1345 tree wanted_type;
9b69f523 1346 tree first_fillin_param;
1ccf251f 1347 char *format_chars;
f67aab2c 1348 format_char_info *fci = NULL;
1ccf251f 1349 char flag_chars[8];
9b69f523 1350 int has_operand_number = 0;
1ccf251f
RK
1351
1352 /* Skip to format argument. If the argument isn't available, there's
1353 no work for us to do; prototype checking will catch the problem. */
1354 for (arg_num = 1; ; ++arg_num)
1355 {
1356 if (params == 0)
1357 return;
1358 if (arg_num == info->format_num)
1359 break;
1360 params = TREE_CHAIN (params);
1361 }
1362 format_tree = TREE_VALUE (params);
1363 params = TREE_CHAIN (params);
1364 if (format_tree == 0)
1365 return;
0161e8da 1366
1ccf251f
RK
1367 /* We can only check the format if it's a string constant. */
1368 while (TREE_CODE (format_tree) == NOP_EXPR)
1369 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
0161e8da
RK
1370
1371 if (TREE_CODE (format_tree) == CALL_EXPR
1372 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1373 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1374 == FUNCTION_DECL))
1375 {
1376 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1377
1378 /* See if this is a call to a known internationalization function
1379 that modifies the format arg. */
1380 international_format_info *info;
1381
1382 for (info = international_format_list; info; info = info->next)
1383 if (info->assembler_name
1384 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1385 : (info->name == DECL_NAME (function)))
1386 {
1387 tree inner_args;
1388 int i;
1389
1390 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1391 inner_args != 0;
1392 inner_args = TREE_CHAIN (inner_args), i++)
1393 if (i == info->format_num)
1394 {
1395 format_tree = TREE_VALUE (inner_args);
1396
1397 while (TREE_CODE (format_tree) == NOP_EXPR)
1398 format_tree = TREE_OPERAND (format_tree, 0);
1399 }
1400 }
1401 }
1402
39b751ce 1403 if (integer_zerop (format_tree))
1ccf251f
RK
1404 {
1405 warning ("null format string");
1406 return;
1407 }
1408 if (TREE_CODE (format_tree) != ADDR_EXPR)
1409 return;
1410 format_tree = TREE_OPERAND (format_tree, 0);
1411 if (TREE_CODE (format_tree) != STRING_CST)
1412 return;
1413 format_chars = TREE_STRING_POINTER (format_tree);
1414 format_length = TREE_STRING_LENGTH (format_tree);
1415 if (format_length <= 1)
1416 warning ("zero-length format string");
1417 if (format_chars[--format_length] != 0)
1418 {
1419 warning ("unterminated format string");
1420 return;
1421 }
1422 /* Skip to first argument to check. */
1423 while (arg_num + 1 < info->first_arg_num)
1424 {
1425 if (params == 0)
1426 return;
1427 params = TREE_CHAIN (params);
1428 ++arg_num;
1429 }
9b69f523
RK
1430
1431 first_fillin_param = params;
1ccf251f
RK
1432 while (1)
1433 {
af3c5588 1434 int aflag;
1ccf251f
RK
1435 if (*format_chars == 0)
1436 {
1437 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1438 warning ("embedded `\\0' in format");
9b69f523 1439 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
1440 warning ("too many arguments for format");
1441 return;
1442 }
1443 if (*format_chars++ != '%')
1444 continue;
1445 if (*format_chars == 0)
1446 {
1447 warning ("spurious trailing `%%' in format");
1448 continue;
1449 }
1450 if (*format_chars == '%')
1451 {
1452 ++format_chars;
1453 continue;
1454 }
1455 flag_chars[0] = 0;
1456 suppressed = wide = precise = FALSE;
bb72a084 1457 if (info->format_type == scanf_format_type)
1ccf251f
RK
1458 {
1459 suppressed = *format_chars == '*';
1460 if (suppressed)
1461 ++format_chars;
e9a780ec 1462 while (ISDIGIT (*format_chars))
1ccf251f
RK
1463 ++format_chars;
1464 }
bb72a084
PE
1465 else if (info->format_type == strftime_format_type)
1466 {
1467 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1468 {
1469 if (pedantic)
1470 warning ("ANSI C does not support the strftime `%c' flag",
1471 *format_chars);
1472 if (index (flag_chars, *format_chars) != 0)
1473 {
1474 warning ("repeated `%c' flag in format",
1475 *format_chars);
1476 ++format_chars;
1477 }
1478 else
1479 {
1480 i = strlen (flag_chars);
1481 flag_chars[i++] = *format_chars++;
1482 flag_chars[i] = 0;
1483 }
1484 }
e9a780ec 1485 while (ISDIGIT ((unsigned char) *format_chars))
bb72a084
PE
1486 {
1487 wide = TRUE;
1488 ++format_chars;
1489 }
1490 if (wide && pedantic)
1491 warning ("ANSI C does not support strftime format width");
1492 if (*format_chars == 'E' || *format_chars == 'O')
1493 {
1494 i = strlen (flag_chars);
1495 flag_chars[i++] = *format_chars++;
1496 flag_chars[i] = 0;
1497 if (*format_chars == 'E' || *format_chars == 'O')
1498 {
1499 warning ("multiple E/O modifiers in format");
1500 while (*format_chars == 'E' || *format_chars == 'O')
1501 ++format_chars;
1502 }
1503 }
1504 }
1505 else if (info->format_type == printf_format_type)
1ccf251f 1506 {
9b69f523
RK
1507 /* See if we have a number followed by a dollar sign. If we do,
1508 it is an operand number, so set PARAMS to that operand. */
1509 if (*format_chars >= '0' && *format_chars <= '9')
1510 {
1511 char *p = format_chars;
1512
1513 while (*p >= '0' && *p++ <= '9')
1514 ;
1515
1516 if (*p == '$')
1517 {
1518 int opnum = atoi (format_chars);
1519
1520 params = first_fillin_param;
1521 format_chars = p + 1;
1522 has_operand_number = 1;
1523
1524 for (i = 1; i < opnum && params != 0; i++)
1525 params = TREE_CHAIN (params);
1526
1527 if (opnum == 0 || params == 0)
1528 {
1529 warning ("operand number out of range in format");
1530 return;
1531 }
1532 }
1533 }
1534
1ccf251f
RK
1535 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1536 {
1537 if (index (flag_chars, *format_chars) != 0)
bb72a084 1538 warning ("repeated `%c' flag in format", *format_chars++);
6d819282
MK
1539 else
1540 {
1541 i = strlen (flag_chars);
1542 flag_chars[i++] = *format_chars++;
1543 flag_chars[i] = 0;
1544 }
1ccf251f 1545 }
b57062ca 1546 /* "If the space and + flags both appear,
1ccf251f
RK
1547 the space flag will be ignored." */
1548 if (index (flag_chars, ' ') != 0
1549 && index (flag_chars, '+') != 0)
1550 warning ("use of both ` ' and `+' flags in format");
1551 /* "If the 0 and - flags both appear,
1552 the 0 flag will be ignored." */
1553 if (index (flag_chars, '0') != 0
1554 && index (flag_chars, '-') != 0)
1555 warning ("use of both `0' and `-' flags in format");
1556 if (*format_chars == '*')
1557 {
1558 wide = TRUE;
1559 /* "...a field width...may be indicated by an asterisk.
1560 In this case, an int argument supplies the field width..." */
1561 ++format_chars;
1562 if (params == 0)
1563 {
1564 warning (tfaff);
1565 return;
1566 }
1567 if (info->first_arg_num != 0)
1568 {
1569 cur_param = TREE_VALUE (params);
1570 params = TREE_CHAIN (params);
1571 ++arg_num;
1572 /* size_t is generally not valid here.
1573 It will work on most machines, because size_t and int
1574 have the same mode. But might as well warn anyway,
1575 since it will fail on other machines. */
309ffab6
RS
1576 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1577 != integer_type_node)
1578 &&
1579 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1580 != unsigned_type_node))
bb72a084 1581 warning ("field width is not type int (arg %d)", arg_num);
1ccf251f
RK
1582 }
1583 }
1584 else
1585 {
e9a780ec 1586 while (ISDIGIT (*format_chars))
1ccf251f
RK
1587 {
1588 wide = TRUE;
1589 ++format_chars;
1590 }
1591 }
1592 if (*format_chars == '.')
1593 {
1594 precise = TRUE;
1595 ++format_chars;
e9a780ec 1596 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1ccf251f
RK
1597 warning ("`.' not followed by `*' or digit in format");
1598 /* "...a...precision...may be indicated by an asterisk.
1599 In this case, an int argument supplies the...precision." */
1600 if (*format_chars == '*')
1601 {
1602 if (info->first_arg_num != 0)
1603 {
1604 ++format_chars;
1605 if (params == 0)
1606 {
1607 warning (tfaff);
1608 return;
1609 }
1610 cur_param = TREE_VALUE (params);
1611 params = TREE_CHAIN (params);
1612 ++arg_num;
1613 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1614 != integer_type_node)
bb72a084
PE
1615 warning ("field width is not type int (arg %d)",
1616 arg_num);
1ccf251f
RK
1617 }
1618 }
1619 else
1620 {
e9a780ec 1621 while (ISDIGIT (*format_chars))
1ccf251f
RK
1622 ++format_chars;
1623 }
1624 }
1625 }
bb72a084 1626
af3c5588 1627 aflag = 0;
bb72a084
PE
1628
1629 if (info->format_type != strftime_format_type)
af3c5588 1630 {
bb72a084
PE
1631 if (*format_chars == 'h' || *format_chars == 'l')
1632 length_char = *format_chars++;
1633 else if (*format_chars == 'q' || *format_chars == 'L')
6d819282 1634 {
bb72a084 1635 length_char = *format_chars++;
f5963e61 1636 if (pedantic)
bb72a084
PE
1637 warning ("ANSI C does not support the `%c' length modifier",
1638 length_char);
6d819282 1639 }
bb72a084
PE
1640 else if (*format_chars == 'Z')
1641 {
1642 length_char = *format_chars++;
1643 if (pedantic)
1644 warning ("ANSI C does not support the `Z' length modifier");
1645 }
1646 else
1647 length_char = 0;
1648 if (length_char == 'l' && *format_chars == 'l')
1649 {
1650 length_char = 'q', format_chars++;
b57062ca 1651 /* FIXME: Is allowed in ISO C 9x. */
bb72a084
PE
1652 if (pedantic)
1653 warning ("ANSI C does not support the `ll' length modifier");
1654 }
bc516719
AS
1655 else if (length_char == 'h' && *format_chars == 'h')
1656 {
1657 length_char = 'H', format_chars++;
1658 /* FIXME: Is allowed in ISO C 9x. */
1659 if (pedantic)
1660 warning ("ANSI C does not support the `hh' length modifier");
1661 }
bb72a084
PE
1662 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1663 {
1664 if (format_chars[1] == 's' || format_chars[1] == 'S'
1665 || format_chars[1] == '[')
1666 {
1667 /* `a' is used as a flag. */
1668 aflag = 1;
1669 format_chars++;
1670 }
1671 }
1672 if (suppressed && length_char != 0)
1673 warning ("use of `*' and `%c' together in format", length_char);
1ccf251f
RK
1674 }
1675 format_char = *format_chars;
bb72a084
PE
1676 if (format_char == 0
1677 || (info->format_type != strftime_format_type && format_char == '%'))
1ccf251f
RK
1678 {
1679 warning ("conversion lacks type at end of format");
1680 continue;
1681 }
c6a9dea8
JW
1682 /* The m, C, and S formats are GNU extensions. */
1683 if (pedantic && info->format_type != strftime_format_type
1684 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1685 warning ("ANSI C does not support the `%c' format", format_char);
1686 /* ??? The a and A formats are C9X extensions, and should be allowed
1687 when a C9X option is added. */
1688 if (pedantic && info->format_type != strftime_format_type
1689 && (format_char == 'a' || format_char == 'A'))
1690 warning ("ANSI C does not support the `%c' format", format_char);
1ccf251f 1691 format_chars++;
bb72a084
PE
1692 switch (info->format_type)
1693 {
1694 case printf_format_type:
1695 fci = print_char_table;
1696 break;
1697 case scanf_format_type:
1698 fci = scan_char_table;
1699 break;
1700 case strftime_format_type:
1701 fci = time_char_table;
1702 break;
1703 default:
1704 abort ();
1705 }
1ccf251f
RK
1706 while (fci->format_chars != 0
1707 && index (fci->format_chars, format_char) == 0)
1708 ++fci;
1709 if (fci->format_chars == 0)
1710 {
1711 if (format_char >= 040 && format_char < 0177)
bb72a084 1712 warning ("unknown conversion type character `%c' in format",
1ccf251f
RK
1713 format_char);
1714 else
bb72a084 1715 warning ("unknown conversion type character 0x%x in format",
1ccf251f 1716 format_char);
1ccf251f
RK
1717 continue;
1718 }
bb72a084 1719 if (pedantic)
1ccf251f 1720 {
bb72a084
PE
1721 if (index (fci->flag_chars, 'G') != 0)
1722 warning ("ANSI C does not support `%%%c'", format_char);
1723 if (index (fci->flag_chars, 'o') != 0
1724 && index (flag_chars, 'O') != 0)
1725 warning ("ANSI C does not support `%%O%c'", format_char);
1ccf251f 1726 }
bb72a084
PE
1727 if (wide && index (fci->flag_chars, 'w') == 0)
1728 warning ("width used with `%c' format", format_char);
1729 if (index (fci->flag_chars, '2') != 0)
1730 warning ("`%%%c' yields only last 2 digits of year", format_char);
1731 else if (index (fci->flag_chars, '3') != 0)
1732 warning ("`%%%c' yields only last 2 digits of year in some locales",
1733 format_char);
1ccf251f 1734 if (precise && index (fci->flag_chars, 'p') == 0)
bb72a084 1735 warning ("precision used with `%c' format", format_char);
af3c5588
RK
1736 if (aflag && index (fci->flag_chars, 'a') == 0)
1737 {
bb72a084 1738 warning ("`a' flag used with `%c' format", format_char);
f9dcab52
RK
1739 /* To simplify the following code. */
1740 aflag = 0;
1ccf251f 1741 }
c6a9dea8
JW
1742 /* The a flag is a GNU extension. */
1743 else if (pedantic && aflag)
1744 warning ("ANSI C does not support the `a' flag");
bb72a084 1745 if (info->format_type == scanf_format_type && format_char == '[')
1ccf251f
RK
1746 {
1747 /* Skip over scan set, in case it happens to have '%' in it. */
1748 if (*format_chars == '^')
1749 ++format_chars;
1750 /* Find closing bracket; if one is hit immediately, then
1751 it's part of the scan set rather than a terminator. */
1752 if (*format_chars == ']')
1753 ++format_chars;
1754 while (*format_chars && *format_chars != ']')
1755 ++format_chars;
1756 if (*format_chars != ']')
bb72a084
PE
1757 /* The end of the format string was reached. */
1758 warning ("no closing `]' for `%%[' format");
1ccf251f
RK
1759 }
1760 if (suppressed)
1761 {
1762 if (index (fci->flag_chars, '*') == 0)
bb72a084 1763 warning ("suppression of `%c' conversion in format", format_char);
1ccf251f
RK
1764 continue;
1765 }
1766 for (i = 0; flag_chars[i] != 0; ++i)
1767 {
1768 if (index (fci->flag_chars, flag_chars[i]) == 0)
bb72a084
PE
1769 warning ("flag `%c' used with type `%c'",
1770 flag_chars[i], format_char);
1ccf251f 1771 }
bb72a084
PE
1772 if (info->format_type == strftime_format_type)
1773 continue;
f5963e61
JL
1774 if (precise && index (flag_chars, '0') != 0
1775 && (format_char == 'd' || format_char == 'i'
1776 || format_char == 'o' || format_char == 'u'
1777 || format_char == 'x' || format_char == 'x'))
bb72a084
PE
1778 warning ("`0' flag ignored with precision specifier and `%c' format",
1779 format_char);
1ccf251f
RK
1780 switch (length_char)
1781 {
1782 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
b57062ca 1783 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1ccf251f
RK
1784 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1785 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1786 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f 1787 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
e5e809f4 1788 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1ccf251f 1789 }
f5963e61 1790 if (wanted_type == 0)
72f54004
JM
1791 warning ("use of `%c' length character with `%c' type character",
1792 length_char, format_char);
1ccf251f 1793
1ccf251f
RK
1794 /* Finally. . .check type of argument against desired type! */
1795 if (info->first_arg_num == 0)
1796 continue;
2a13575e
RK
1797 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1798 /* This specifier takes no argument. */
1799 continue;
1ccf251f
RK
1800 if (params == 0)
1801 {
1802 warning (tfaff);
1803 return;
1804 }
1805 cur_param = TREE_VALUE (params);
1806 params = TREE_CHAIN (params);
1807 ++arg_num;
1808 cur_type = TREE_TYPE (cur_param);
1809
6d819282
MK
1810 STRIP_NOPS (cur_param);
1811
1ccf251f
RK
1812 /* Check the types of any additional pointer arguments
1813 that precede the "real" argument. */
f9dcab52 1814 for (i = 0; i < fci->pointer_count + aflag; ++i)
1ccf251f
RK
1815 {
1816 if (TREE_CODE (cur_type) == POINTER_TYPE)
1817 {
1818 cur_type = TREE_TYPE (cur_type);
6d819282 1819
f5963e61 1820 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
6d819282
MK
1821 cur_param = TREE_OPERAND (cur_param, 0);
1822 else
1823 cur_param = 0;
1824
1ccf251f
RK
1825 continue;
1826 }
87416640 1827 if (TREE_CODE (cur_type) != ERROR_MARK)
bb72a084
PE
1828 warning ("format argument is not a %s (arg %d)",
1829 ((fci->pointer_count + aflag == 1)
1830 ? "pointer" : "pointer to a pointer"),
1831 arg_num);
1ccf251f
RK
1832 break;
1833 }
1834
6d819282 1835 /* See if this is an attempt to write into a const type with
bc516719
AS
1836 scanf or with printf "%n". */
1837 if ((info->format_type == scanf_format_type
1838 || (info->format_type == printf_format_type
1839 && format_char == 'n'))
bb72a084 1840 && i == fci->pointer_count + aflag
6d819282
MK
1841 && wanted_type != 0
1842 && TREE_CODE (cur_type) != ERROR_MARK
1843 && (TYPE_READONLY (cur_type)
1844 || (cur_param != 0
1845 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1846 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1847 && TREE_READONLY (cur_param))))))
bb72a084 1848 warning ("writing into constant object (arg %d)", arg_num);
6d819282 1849
1ccf251f 1850 /* Check the type of the "real" argument, if there's a type we want. */
f9dcab52 1851 if (i == fci->pointer_count + aflag && wanted_type != 0
87416640 1852 && TREE_CODE (cur_type) != ERROR_MARK
1ccf251f
RK
1853 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1854 /* If we want `void *', allow any pointer type.
1855 (Anything else would already have got a warning.) */
1856 && ! (wanted_type == void_type_node
1857 && fci->pointer_count > 0)
1858 /* Don't warn about differences merely in signedness. */
1859 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
b7c9c707 1860 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1861 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1862 ? wanted_type == (cur_type = unsigned_type (cur_type))
1863 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
1864 /* Likewise, "signed char", "unsigned char" and "char" are
1865 equivalent but the above test won't consider them equivalent. */
1866 && ! (wanted_type == char_type_node
b7c9c707
RS
1867 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1868 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f
RK
1869 {
1870 register char *this;
1871 register char *that;
b57062ca 1872
1ccf251f
RK
1873 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1874 that = 0;
1875 if (TREE_CODE (cur_type) != ERROR_MARK
1876 && TYPE_NAME (cur_type) != 0
1877 && TREE_CODE (cur_type) != INTEGER_TYPE
1878 && !(TREE_CODE (cur_type) == POINTER_TYPE
1879 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1880 {
1881 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1882 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1883 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1884 else
1885 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1886 }
1887
1888 /* A nameless type can't possibly match what the format wants.
1889 So there will be a warning for it.
1890 Make up a string to describe vaguely what it is. */
1891 if (that == 0)
1892 {
1893 if (TREE_CODE (cur_type) == POINTER_TYPE)
1894 that = "pointer";
1895 else
1896 that = "different type";
1897 }
1898
309ffab6
RS
1899 /* Make the warning better in case of mismatch of int vs long. */
1900 if (TREE_CODE (cur_type) == INTEGER_TYPE
1901 && TREE_CODE (wanted_type) == INTEGER_TYPE
1902 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1903 && TYPE_NAME (cur_type) != 0
1904 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1905 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1906
1ccf251f 1907 if (strcmp (this, that) != 0)
bb72a084 1908 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1ccf251f
RK
1909 }
1910 }
1911}
1912\f
d74154d5
RS
1913/* Print a warning if a constant expression had overflow in folding.
1914 Invoke this function on every expression that the language
1915 requires to be a constant expression.
1916 Note the ANSI C standard says it is erroneous for a
1917 constant expression to overflow. */
96571883
BK
1918
1919void
1920constant_expression_warning (value)
1921 tree value;
1922{
c05f751c
RK
1923 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1924 || TREE_CODE (value) == COMPLEX_CST)
1925 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1926 pedwarn ("overflow in constant expression");
d74154d5
RS
1927}
1928
1929/* Print a warning if an expression had overflow in folding.
1930 Invoke this function on every expression that
1931 (1) appears in the source code, and
1932 (2) might be a constant expression that overflowed, and
1933 (3) is not already checked by convert_and_check;
1934 however, do not invoke this function on operands of explicit casts. */
1935
1936void
1937overflow_warning (value)
1938 tree value;
1939{
c05f751c
RK
1940 if ((TREE_CODE (value) == INTEGER_CST
1941 || (TREE_CODE (value) == COMPLEX_CST
1942 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1943 && TREE_OVERFLOW (value))
d74154d5 1944 {
7193bce2 1945 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1946 if (skip_evaluation == 0)
1947 warning ("integer overflow in expression");
d74154d5 1948 }
c05f751c
RK
1949 else if ((TREE_CODE (value) == REAL_CST
1950 || (TREE_CODE (value) == COMPLEX_CST
1951 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1952 && TREE_OVERFLOW (value))
1953 {
1954 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1955 if (skip_evaluation == 0)
1956 warning ("floating point overflow in expression");
c05f751c 1957 }
d74154d5
RS
1958}
1959
1960/* Print a warning if a large constant is truncated to unsigned,
1961 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1962 Invoke this function on every expression that might be implicitly
1963 converted to an unsigned type. */
1964
1965void
1966unsigned_conversion_warning (result, operand)
1967 tree result, operand;
1968{
1969 if (TREE_CODE (operand) == INTEGER_CST
1970 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1971 && TREE_UNSIGNED (TREE_TYPE (result))
e78a3b42 1972 && skip_evaluation == 0
d74154d5
RS
1973 && !int_fits_type_p (operand, TREE_TYPE (result)))
1974 {
1975 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1976 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 1977 warning ("large integer implicitly truncated to unsigned type");
d74154d5 1978 else if (warn_conversion)
90c939d4 1979 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
1980 }
1981}
1982
1983/* Convert EXPR to TYPE, warning about conversion problems with constants.
1984 Invoke this function on every expression that is converted implicitly,
1985 i.e. because of language rules and not because of an explicit cast. */
1986
1987tree
1988convert_and_check (type, expr)
1989 tree type, expr;
1990{
1991 tree t = convert (type, expr);
1992 if (TREE_CODE (t) == INTEGER_CST)
1993 {
7193bce2 1994 if (TREE_OVERFLOW (t))
d74154d5 1995 {
7193bce2
PE
1996 TREE_OVERFLOW (t) = 0;
1997
868fc750
RK
1998 /* Do not diagnose overflow in a constant expression merely
1999 because a conversion overflowed. */
2000 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2001
7193bce2
PE
2002 /* No warning for converting 0x80000000 to int. */
2003 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2004 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2005 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
2006 /* If EXPR fits in the unsigned version of TYPE,
2007 don't warn unless pedantic. */
e78a3b42
RK
2008 if ((pedantic
2009 || TREE_UNSIGNED (type)
2010 || ! int_fits_type_p (expr, unsigned_type (type)))
2011 && skip_evaluation == 0)
bb72a084 2012 warning ("overflow in implicit constant conversion");
d74154d5
RS
2013 }
2014 else
2015 unsigned_conversion_warning (t, expr);
2016 }
2017 return t;
96571883
BK
2018}
2019\f
b30f223b
RS
2020void
2021c_expand_expr_stmt (expr)
2022 tree expr;
2023{
2024 /* Do default conversion if safe and possibly important,
2025 in case within ({...}). */
2026 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2027 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2028 expr = default_conversion (expr);
2029
2030 if (TREE_TYPE (expr) != error_mark_node
2031 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2032 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2033 error ("expression statement has incomplete type");
2034
2035 expand_expr_stmt (expr);
2036}
2037\f
2038/* Validate the expression after `case' and apply default promotions. */
2039
2040tree
2041check_case_value (value)
2042 tree value;
2043{
2044 if (value == NULL_TREE)
2045 return value;
2046
2047 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 2048 STRIP_TYPE_NOPS (value);
b30f223b
RS
2049
2050 if (TREE_CODE (value) != INTEGER_CST
2051 && value != error_mark_node)
2052 {
2053 error ("case label does not reduce to an integer constant");
2054 value = error_mark_node;
2055 }
2056 else
2057 /* Promote char or short to int. */
2058 value = default_conversion (value);
2059
bc690db1
RS
2060 constant_expression_warning (value);
2061
b30f223b
RS
2062 return value;
2063}
2064\f
2065/* Return an integer type with BITS bits of precision,
2066 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2067
2068tree
2069type_for_size (bits, unsignedp)
2070 unsigned bits;
2071 int unsignedp;
2072{
a311b52c
JM
2073 if (bits == TYPE_PRECISION (integer_type_node))
2074 return unsignedp ? unsigned_type_node : integer_type_node;
2075
3fc7e390 2076 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
2077 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2078
3fc7e390 2079 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
2080 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2081
3fc7e390 2082 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
2083 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2084
3fc7e390 2085 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
2086 return (unsignedp ? long_long_unsigned_type_node
2087 : long_long_integer_type_node);
2088
3fc7e390
RS
2089 if (bits <= TYPE_PRECISION (intQI_type_node))
2090 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2091
2092 if (bits <= TYPE_PRECISION (intHI_type_node))
2093 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2094
2095 if (bits <= TYPE_PRECISION (intSI_type_node))
2096 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2097
2098 if (bits <= TYPE_PRECISION (intDI_type_node))
2099 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2100
b30f223b
RS
2101 return 0;
2102}
2103
2104/* Return a data type that has machine mode MODE.
2105 If the mode is an integer,
2106 then UNSIGNEDP selects between signed and unsigned types. */
2107
2108tree
2109type_for_mode (mode, unsignedp)
2110 enum machine_mode mode;
2111 int unsignedp;
2112{
a311b52c
JM
2113 if (mode == TYPE_MODE (integer_type_node))
2114 return unsignedp ? unsigned_type_node : integer_type_node;
2115
b30f223b
RS
2116 if (mode == TYPE_MODE (signed_char_type_node))
2117 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2118
2119 if (mode == TYPE_MODE (short_integer_type_node))
2120 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2121
b30f223b
RS
2122 if (mode == TYPE_MODE (long_integer_type_node))
2123 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2124
2125 if (mode == TYPE_MODE (long_long_integer_type_node))
2126 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2127
3fc7e390
RS
2128 if (mode == TYPE_MODE (intQI_type_node))
2129 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2130
2131 if (mode == TYPE_MODE (intHI_type_node))
2132 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2133
2134 if (mode == TYPE_MODE (intSI_type_node))
2135 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2136
2137 if (mode == TYPE_MODE (intDI_type_node))
2138 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2139
a6d7e156
JL
2140 if (mode == TYPE_MODE (intTI_type_node))
2141 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2142
b30f223b
RS
2143 if (mode == TYPE_MODE (float_type_node))
2144 return float_type_node;
2145
2146 if (mode == TYPE_MODE (double_type_node))
2147 return double_type_node;
2148
2149 if (mode == TYPE_MODE (long_double_type_node))
2150 return long_double_type_node;
2151
2152 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2153 return build_pointer_type (char_type_node);
2154
2155 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2156 return build_pointer_type (integer_type_node);
2157
2158 return 0;
2159}
2160\f
6acfe908
JM
2161/* Return the minimum number of bits needed to represent VALUE in a
2162 signed or unsigned type, UNSIGNEDP says which. */
2163
2164int
2165min_precision (value, unsignedp)
2166 tree value;
2167 int unsignedp;
2168{
2169 int log;
2170
2171 /* If the value is negative, compute its negative minus 1. The latter
2172 adjustment is because the absolute value of the largest negative value
2173 is one larger than the largest positive value. This is equivalent to
2174 a bit-wise negation, so use that operation instead. */
2175
2176 if (tree_int_cst_sgn (value) < 0)
2177 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2178
2179 /* Return the number of bits needed, taking into account the fact
2180 that we need one more bit for a signed than unsigned type. */
2181
2182 if (integer_zerop (value))
2183 log = 0;
2184 else if (TREE_INT_CST_HIGH (value) != 0)
2185 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2186 else
2187 log = floor_log2 (TREE_INT_CST_LOW (value));
2188
2189 return log + 1 + ! unsignedp;
2190}
2191\f
b30f223b
RS
2192/* Print an error message for invalid operands to arith operation CODE.
2193 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2194
2195void
2196binary_op_error (code)
2197 enum tree_code code;
2198{
6d819282 2199 register char *opname;
89c78d7d 2200
b30f223b
RS
2201 switch (code)
2202 {
2203 case NOP_EXPR:
2204 error ("invalid truth-value expression");
2205 return;
2206
2207 case PLUS_EXPR:
2208 opname = "+"; break;
2209 case MINUS_EXPR:
2210 opname = "-"; break;
2211 case MULT_EXPR:
2212 opname = "*"; break;
2213 case MAX_EXPR:
2214 opname = "max"; break;
2215 case MIN_EXPR:
2216 opname = "min"; break;
2217 case EQ_EXPR:
2218 opname = "=="; break;
2219 case NE_EXPR:
2220 opname = "!="; break;
2221 case LE_EXPR:
2222 opname = "<="; break;
2223 case GE_EXPR:
2224 opname = ">="; break;
2225 case LT_EXPR:
2226 opname = "<"; break;
2227 case GT_EXPR:
2228 opname = ">"; break;
2229 case LSHIFT_EXPR:
2230 opname = "<<"; break;
2231 case RSHIFT_EXPR:
2232 opname = ">>"; break;
2233 case TRUNC_MOD_EXPR:
047de90b 2234 case FLOOR_MOD_EXPR:
b30f223b
RS
2235 opname = "%"; break;
2236 case TRUNC_DIV_EXPR:
047de90b 2237 case FLOOR_DIV_EXPR:
b30f223b
RS
2238 opname = "/"; break;
2239 case BIT_AND_EXPR:
2240 opname = "&"; break;
2241 case BIT_IOR_EXPR:
2242 opname = "|"; break;
2243 case TRUTH_ANDIF_EXPR:
2244 opname = "&&"; break;
2245 case TRUTH_ORIF_EXPR:
2246 opname = "||"; break;
2247 case BIT_XOR_EXPR:
2248 opname = "^"; break;
047de90b
RS
2249 case LROTATE_EXPR:
2250 case RROTATE_EXPR:
2251 opname = "rotate"; break;
6d819282
MK
2252 default:
2253 opname = "unknown"; break;
b30f223b
RS
2254 }
2255 error ("invalid operands to binary %s", opname);
2256}
2257\f
2258/* Subroutine of build_binary_op, used for comparison operations.
2259 See if the operands have both been converted from subword integer types
2260 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
2261 This function is also responsible for converting the two operands
2262 to the proper common type for comparison.
b30f223b
RS
2263
2264 The arguments of this function are all pointers to local variables
2265 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2266 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2267
2268 If this function returns nonzero, it means that the comparison has
2269 a constant value. What this function returns is an expression for
2270 that value. */
2271
2272tree
2273shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2274 tree *op0_ptr, *op1_ptr;
2275 tree *restype_ptr;
2276 enum tree_code *rescode_ptr;
2277{
2278 register tree type;
2279 tree op0 = *op0_ptr;
2280 tree op1 = *op1_ptr;
2281 int unsignedp0, unsignedp1;
2282 int real1, real2;
2283 tree primop0, primop1;
2284 enum tree_code code = *rescode_ptr;
2285
2286 /* Throw away any conversions to wider types
2287 already present in the operands. */
2288
2289 primop0 = get_narrower (op0, &unsignedp0);
2290 primop1 = get_narrower (op1, &unsignedp1);
2291
2292 /* Handle the case that OP0 does not *contain* a conversion
2293 but it *requires* conversion to FINAL_TYPE. */
2294
2295 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2296 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2297 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2298 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2299
2300 /* If one of the operands must be floated, we cannot optimize. */
2301 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2302 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2303
2304 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
2305 so value is preserved), for canonicalization. Don't do this if
2306 the second arg is 0. */
b30f223b 2307
5af6001b
RK
2308 if (TREE_CONSTANT (primop0)
2309 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
2310 {
2311 register tree tem = primop0;
2312 register int temi = unsignedp0;
2313 primop0 = primop1;
2314 primop1 = tem;
2315 tem = op0;
2316 op0 = op1;
2317 op1 = tem;
2318 *op0_ptr = op0;
2319 *op1_ptr = op1;
2320 unsignedp0 = unsignedp1;
2321 unsignedp1 = temi;
2322 temi = real1;
2323 real1 = real2;
2324 real2 = temi;
2325
2326 switch (code)
2327 {
2328 case LT_EXPR:
2329 code = GT_EXPR;
2330 break;
2331 case GT_EXPR:
2332 code = LT_EXPR;
2333 break;
2334 case LE_EXPR:
2335 code = GE_EXPR;
2336 break;
2337 case GE_EXPR:
2338 code = LE_EXPR;
2339 break;
6d819282
MK
2340 default:
2341 break;
b30f223b
RS
2342 }
2343 *rescode_ptr = code;
2344 }
2345
2346 /* If comparing an integer against a constant more bits wide,
2347 maybe we can deduce a value of 1 or 0 independent of the data.
2348 Or else truncate the constant now
2349 rather than extend the variable at run time.
2350
2351 This is only interesting if the constant is the wider arg.
2352 Also, it is not safe if the constant is unsigned and the
2353 variable arg is signed, since in this case the variable
2354 would be sign-extended and then regarded as unsigned.
2355 Our technique fails in this case because the lowest/highest
2356 possible unsigned results don't follow naturally from the
2357 lowest/highest possible values of the variable operand.
2358 For just EQ_EXPR and NE_EXPR there is another technique that
2359 could be used: see if the constant can be faithfully represented
2360 in the other operand's type, by truncating it and reextending it
2361 and see if that preserves the constant's value. */
2362
2363 if (!real1 && !real2
2364 && TREE_CODE (primop1) == INTEGER_CST
2365 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2366 {
2367 int min_gt, max_gt, min_lt, max_lt;
2368 tree maxval, minval;
2369 /* 1 if comparison is nominally unsigned. */
2370 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2371 tree val;
2372
2373 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2374
2375 maxval = TYPE_MAX_VALUE (type);
2376 minval = TYPE_MIN_VALUE (type);
2377
2378 if (unsignedp && !unsignedp0)
2379 *restype_ptr = signed_type (*restype_ptr);
2380
2381 if (TREE_TYPE (primop1) != *restype_ptr)
2382 primop1 = convert (*restype_ptr, primop1);
2383 if (type != *restype_ptr)
2384 {
2385 minval = convert (*restype_ptr, minval);
2386 maxval = convert (*restype_ptr, maxval);
2387 }
2388
2389 if (unsignedp && unsignedp0)
2390 {
2391 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2392 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2393 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2394 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2395 }
2396 else
2397 {
2398 min_gt = INT_CST_LT (primop1, minval);
2399 max_gt = INT_CST_LT (primop1, maxval);
2400 min_lt = INT_CST_LT (minval, primop1);
2401 max_lt = INT_CST_LT (maxval, primop1);
2402 }
2403
2404 val = 0;
2405 /* This used to be a switch, but Genix compiler can't handle that. */
2406 if (code == NE_EXPR)
2407 {
2408 if (max_lt || min_gt)
a360da3a 2409 val = boolean_true_node;
b30f223b
RS
2410 }
2411 else if (code == EQ_EXPR)
2412 {
2413 if (max_lt || min_gt)
a360da3a 2414 val = boolean_false_node;
b30f223b
RS
2415 }
2416 else if (code == LT_EXPR)
2417 {
2418 if (max_lt)
a360da3a 2419 val = boolean_true_node;
b30f223b 2420 if (!min_lt)
a360da3a 2421 val = boolean_false_node;
b30f223b
RS
2422 }
2423 else if (code == GT_EXPR)
2424 {
2425 if (min_gt)
a360da3a 2426 val = boolean_true_node;
b30f223b 2427 if (!max_gt)
a360da3a 2428 val = boolean_false_node;
b30f223b
RS
2429 }
2430 else if (code == LE_EXPR)
2431 {
2432 if (!max_gt)
a360da3a 2433 val = boolean_true_node;
b30f223b 2434 if (min_gt)
a360da3a 2435 val = boolean_false_node;
b30f223b
RS
2436 }
2437 else if (code == GE_EXPR)
2438 {
2439 if (!min_lt)
a360da3a 2440 val = boolean_true_node;
b30f223b 2441 if (max_lt)
a360da3a 2442 val = boolean_false_node;
b30f223b
RS
2443 }
2444
2445 /* If primop0 was sign-extended and unsigned comparison specd,
2446 we did a signed comparison above using the signed type bounds.
2447 But the comparison we output must be unsigned.
2448
2449 Also, for inequalities, VAL is no good; but if the signed
2450 comparison had *any* fixed result, it follows that the
2451 unsigned comparison just tests the sign in reverse
2452 (positive values are LE, negative ones GE).
2453 So we can generate an unsigned comparison
2454 against an extreme value of the signed type. */
2455
2456 if (unsignedp && !unsignedp0)
2457 {
2458 if (val != 0)
2459 switch (code)
2460 {
2461 case LT_EXPR:
2462 case GE_EXPR:
2463 primop1 = TYPE_MIN_VALUE (type);
2464 val = 0;
2465 break;
2466
2467 case LE_EXPR:
2468 case GT_EXPR:
2469 primop1 = TYPE_MAX_VALUE (type);
2470 val = 0;
2471 break;
6d819282
MK
2472
2473 default:
2474 break;
b30f223b
RS
2475 }
2476 type = unsigned_type (type);
2477 }
2478
b7c9c707 2479 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
2480 {
2481 /* This is the case of (char)x >?< 0x80, which people used to use
2482 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 2483 if (val == boolean_false_node)
b30f223b 2484 warning ("comparison is always 0 due to limited range of data type");
a360da3a 2485 if (val == boolean_true_node)
b30f223b
RS
2486 warning ("comparison is always 1 due to limited range of data type");
2487 }
2488
b7c9c707 2489 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2490 {
1e276c4a 2491 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 2492 if (val == boolean_false_node)
b30f223b 2493 warning ("comparison is always 0 due to limited range of data type");
a360da3a 2494 if (val == boolean_true_node)
b30f223b
RS
2495 warning ("comparison is always 1 due to limited range of data type");
2496 }
2497
2498 if (val != 0)
2499 {
2500 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2501 if (TREE_SIDE_EFFECTS (primop0))
2502 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2503 return val;
2504 }
2505
2506 /* Value is not predetermined, but do the comparison
2507 in the type of the operand that is not constant.
2508 TYPE is already properly set. */
2509 }
2510 else if (real1 && real2
766f6c30
RS
2511 && (TYPE_PRECISION (TREE_TYPE (primop0))
2512 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2513 type = TREE_TYPE (primop0);
2514
2515 /* If args' natural types are both narrower than nominal type
2516 and both extend in the same manner, compare them
2517 in the type of the wider arg.
2518 Otherwise must actually extend both to the nominal
2519 common type lest different ways of extending
2520 alter the result.
2521 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2522
2523 else if (unsignedp0 == unsignedp1 && real1 == real2
2524 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2525 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2526 {
2527 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2528 type = signed_or_unsigned_type (unsignedp0
2529 || TREE_UNSIGNED (*restype_ptr),
2530 type);
2531 /* Make sure shorter operand is extended the right way
2532 to match the longer operand. */
2533 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2534 primop0);
2535 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2536 primop1);
2537 }
2538 else
2539 {
2540 /* Here we must do the comparison on the nominal type
2541 using the args exactly as we received them. */
2542 type = *restype_ptr;
2543 primop0 = op0;
2544 primop1 = op1;
2545
2546 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 2547 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2548 {
2549 tree value = 0;
2550 switch (code)
2551 {
2552 case GE_EXPR:
5af6001b
RK
2553 /* All unsigned values are >= 0, so we warn if extra warnings
2554 are requested. However, if OP0 is a constant that is
2555 >= 0, the signedness of the comparison isn't an issue,
2556 so suppress the warning. */
2557 if (extra_warnings
2558 && ! (TREE_CODE (primop0) == INTEGER_CST
2559 && ! TREE_OVERFLOW (convert (signed_type (type),
2560 primop0))))
b30f223b 2561 warning ("unsigned value >= 0 is always 1");
a360da3a 2562 value = boolean_true_node;
b30f223b
RS
2563 break;
2564
2565 case LT_EXPR:
5af6001b
RK
2566 if (extra_warnings
2567 && ! (TREE_CODE (primop0) == INTEGER_CST
2568 && ! TREE_OVERFLOW (convert (signed_type (type),
2569 primop0))))
b30f223b 2570 warning ("unsigned value < 0 is always 0");
a360da3a 2571 value = boolean_false_node;
6d819282
MK
2572 break;
2573
2574 default:
2575 break;
b30f223b
RS
2576 }
2577
2578 if (value != 0)
2579 {
2580 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2581 if (TREE_SIDE_EFFECTS (primop0))
2582 return build (COMPOUND_EXPR, TREE_TYPE (value),
2583 primop0, value);
2584 return value;
2585 }
2586 }
2587 }
2588
2589 *op0_ptr = convert (type, primop0);
2590 *op1_ptr = convert (type, primop1);
2591
a360da3a 2592 *restype_ptr = boolean_type_node;
b30f223b
RS
2593
2594 return 0;
2595}
2596\f
2597/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2598 or validate its data type for an `if' or `while' statement or ?..: exp.
2599
2600 This preparation consists of taking the ordinary
2601 representation of an expression expr and producing a valid tree
2602 boolean expression describing whether expr is nonzero. We could
a360da3a 2603 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2604 but we optimize comparisons, &&, ||, and !.
2605
a360da3a 2606 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2607
2608tree
2609truthvalue_conversion (expr)
2610 tree expr;
2611{
257e61ed
RS
2612 if (TREE_CODE (expr) == ERROR_MARK)
2613 return expr;
2614
d7c83727 2615#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2616 /* These really should return error_mark_node after 2.4 is stable.
2617 But not all callers handle ERROR_MARK properly. */
2618 switch (TREE_CODE (TREE_TYPE (expr)))
2619 {
2620 case RECORD_TYPE:
2621 error ("struct type value used where scalar is required");
a360da3a 2622 return boolean_false_node;
257e61ed
RS
2623
2624 case UNION_TYPE:
2625 error ("union type value used where scalar is required");
a360da3a 2626 return boolean_false_node;
257e61ed
RS
2627
2628 case ARRAY_TYPE:
2629 error ("array type value used where scalar is required");
a360da3a 2630 return boolean_false_node;
257e61ed
RS
2631
2632 default:
2633 break;
2634 }
d7c83727 2635#endif /* 0 */
257e61ed 2636
b30f223b
RS
2637 switch (TREE_CODE (expr))
2638 {
2639 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2640 or comparison expressions as truth values at this level. */
2641#if 0
2642 case COMPONENT_REF:
2643 /* A one-bit unsigned bit-field is already acceptable. */
2644 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2645 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2646 return expr;
2647 break;
2648#endif
2649
2650 case EQ_EXPR:
2651 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2652 or comparison expressions as truth values at this level. */
2653#if 0
2654 if (integer_zerop (TREE_OPERAND (expr, 1)))
2655 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2656#endif
2657 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2658 case TRUTH_ANDIF_EXPR:
2659 case TRUTH_ORIF_EXPR:
2660 case TRUTH_AND_EXPR:
2661 case TRUTH_OR_EXPR:
9379fac9 2662 case TRUTH_XOR_EXPR:
1180eb10 2663 case TRUTH_NOT_EXPR:
a360da3a
JM
2664 TREE_TYPE (expr) = boolean_type_node;
2665 return expr;
18c0f675 2666
b30f223b
RS
2667 case ERROR_MARK:
2668 return expr;
2669
2670 case INTEGER_CST:
a360da3a 2671 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2672
2673 case REAL_CST:
a360da3a 2674 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2675
2676 case ADDR_EXPR:
fc0c675f
RK
2677 /* If we are taking the address of a external decl, it might be zero
2678 if it is weak, so we cannot optimize. */
2679 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2680 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2681 break;
2682
b30f223b 2683 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2684 return build (COMPOUND_EXPR, boolean_type_node,
2685 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2686 else
a360da3a 2687 return boolean_true_node;
b30f223b 2688
766f6c30 2689 case COMPLEX_EXPR:
f0b996c5 2690 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2691 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2692 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2693 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2694 0);
2695
b30f223b
RS
2696 case NEGATE_EXPR:
2697 case ABS_EXPR:
2698 case FLOAT_EXPR:
2699 case FFS_EXPR:
2700 /* These don't change whether an object is non-zero or zero. */
2701 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2702
2703 case LROTATE_EXPR:
2704 case RROTATE_EXPR:
2705 /* These don't change whether an object is zero or non-zero, but
2706 we can't ignore them if their second arg has side-effects. */
2707 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2708 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2709 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2710 else
2711 return truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 2712
b30f223b
RS
2713 case COND_EXPR:
2714 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2715 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
2716 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2717 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2718
2719 case CONVERT_EXPR:
2720 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2721 since that affects how `default_conversion' will behave. */
2722 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2723 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2724 break;
0f41302f 2725 /* fall through... */
b30f223b
RS
2726 case NOP_EXPR:
2727 /* If this is widening the argument, we can ignore it. */
2728 if (TYPE_PRECISION (TREE_TYPE (expr))
2729 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2730 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2731 break;
2732
b30f223b 2733 case MINUS_EXPR:
f87550e0
JW
2734 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2735 this case. */
2736 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2737 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2738 break;
0f41302f 2739 /* fall through... */
f87550e0 2740 case BIT_XOR_EXPR:
d7c83727 2741 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2742 two objects. */
b30f223b
RS
2743 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2744 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2745 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2746 TREE_OPERAND (expr, 1), 1);
2747 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2748 fold (build1 (NOP_EXPR,
2749 TREE_TYPE (TREE_OPERAND (expr, 0)),
2750 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2751
fb48b1f0 2752 case BIT_AND_EXPR:
58cee643
RK
2753 if (integer_onep (TREE_OPERAND (expr, 1))
2754 && TREE_TYPE (expr) != boolean_type_node)
2755 /* Using convert here would cause infinite recursion. */
2756 return build1 (NOP_EXPR, boolean_type_node, expr);
2757 break;
fb48b1f0 2758
e2aab13d
RS
2759 case MODIFY_EXPR:
2760 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2761 warning ("suggest parentheses around assignment used as truth value");
2762 break;
b57062ca 2763
6d819282
MK
2764 default:
2765 break;
b30f223b
RS
2766 }
2767
f0b996c5 2768 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa
AS
2769 {
2770 tree tem = save_expr (expr);
2771 return (build_binary_op
2772 ((TREE_SIDE_EFFECTS (expr)
2773 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2774 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2775 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2776 0));
2777 }
f0b996c5 2778
b30f223b
RS
2779 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2780}
2781\f
c8724862
DB
2782#if USE_CPPLIB
2783/* Read the rest of a #-directive from input stream FINPUT.
2784 In normal use, the directive name and the white space after it
2785 have already been read, so they won't be included in the result.
2786 We allow for the fact that the directive line may contain
2787 a newline embedded within a character or string literal which forms
2788 a part of the directive.
2789
2790 The value is a string in a reusable buffer. It remains valid
2791 only until the next time this function is called. */
2792unsigned char *yy_cur, *yy_lim;
2793
2794#define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2795#define UNGETC(c) ((c), yy_cur--)
2796
2797int
2798yy_get_token ()
2799{
2800 for (;;)
2801 {
2802 parse_in.limit = parse_in.token_buffer;
2803 cpp_token = cpp_get_token (&parse_in);
2804 if (cpp_token == CPP_EOF)
2805 return -1;
2806 yy_lim = CPP_PWRITTEN (&parse_in);
2807 yy_cur = parse_in.token_buffer;
2808 if (yy_cur < yy_lim)
2809 return *yy_cur++;
2810 }
2811}
2812
2813char *
2814get_directive_line ()
2815{
2816 static char *directive_buffer = NULL;
2817 static unsigned buffer_length = 0;
2818 register char *p;
2819 register char *buffer_limit;
2820 register int looking_for = 0;
2821 register int char_escaped = 0;
2822
2823 if (buffer_length == 0)
2824 {
2825 directive_buffer = (char *)xmalloc (128);
2826 buffer_length = 128;
2827 }
2828
2829 buffer_limit = &directive_buffer[buffer_length];
2830
2831 for (p = directive_buffer; ; )
2832 {
2833 int c;
2834
2835 /* Make buffer bigger if it is full. */
2836 if (p >= buffer_limit)
2837 {
2838 register unsigned bytes_used = (p - directive_buffer);
2839
2840 buffer_length *= 2;
2841 directive_buffer
2842 = (char *)xrealloc (directive_buffer, buffer_length);
2843 p = &directive_buffer[bytes_used];
2844 buffer_limit = &directive_buffer[buffer_length];
2845 }
2846
2847 c = GETC ();
2848
2849 /* Discard initial whitespace. */
2850 if ((c == ' ' || c == '\t') && p == directive_buffer)
2851 continue;
2852
2853 /* Detect the end of the directive. */
2854 if (c == '\n' && looking_for == 0)
2855 {
2856 UNGETC (c);
2857 c = '\0';
2858 }
2859
2860 *p++ = c;
2861
2862 if (c == 0)
2863 return directive_buffer;
2864
2865 /* Handle string and character constant syntax. */
2866 if (looking_for)
2867 {
2868 if (looking_for == c && !char_escaped)
2869 looking_for = 0; /* Found terminator... stop looking. */
2870 }
2871 else
2872 if (c == '\'' || c == '"')
2873 looking_for = c; /* Don't stop buffering until we see another
2874 another one of these (or an EOF). */
2875
2876 /* Handle backslash. */
2877 char_escaped = (c == '\\' && ! char_escaped);
2878 }
2879}
2880#else
b30f223b
RS
2881/* Read the rest of a #-directive from input stream FINPUT.
2882 In normal use, the directive name and the white space after it
2883 have already been read, so they won't be included in the result.
2884 We allow for the fact that the directive line may contain
2885 a newline embedded within a character or string literal which forms
2886 a part of the directive.
2887
2888 The value is a string in a reusable buffer. It remains valid
05a81fe5
DE
2889 only until the next time this function is called.
2890
2891 The terminating character ('\n' or EOF) is left in FINPUT for the
2892 caller to re-read. */
b30f223b
RS
2893
2894char *
2895get_directive_line (finput)
2896 register FILE *finput;
2897{
2898 static char *directive_buffer = NULL;
2899 static unsigned buffer_length = 0;
2900 register char *p;
2901 register char *buffer_limit;
2902 register int looking_for = 0;
2903 register int char_escaped = 0;
2904
2905 if (buffer_length == 0)
2906 {
2907 directive_buffer = (char *)xmalloc (128);
2908 buffer_length = 128;
2909 }
2910
2911 buffer_limit = &directive_buffer[buffer_length];
2912
2913 for (p = directive_buffer; ; )
2914 {
2915 int c;
2916
2917 /* Make buffer bigger if it is full. */
2918 if (p >= buffer_limit)
2919 {
2920 register unsigned bytes_used = (p - directive_buffer);
2921
2922 buffer_length *= 2;
2923 directive_buffer
2924 = (char *)xrealloc (directive_buffer, buffer_length);
2925 p = &directive_buffer[bytes_used];
2926 buffer_limit = &directive_buffer[buffer_length];
2927 }
2928
2929 c = getc (finput);
2930
2931 /* Discard initial whitespace. */
2932 if ((c == ' ' || c == '\t') && p == directive_buffer)
2933 continue;
2934
2935 /* Detect the end of the directive. */
05a81fe5
DE
2936 if (looking_for == 0
2937 && (c == '\n' || c == EOF))
b30f223b
RS
2938 {
2939 ungetc (c, finput);
2940 c = '\0';
2941 }
2942
2943 *p++ = c;
2944
2945 if (c == 0)
2946 return directive_buffer;
2947
2948 /* Handle string and character constant syntax. */
2949 if (looking_for)
2950 {
2951 if (looking_for == c && !char_escaped)
2952 looking_for = 0; /* Found terminator... stop looking. */
2953 }
2954 else
2955 if (c == '\'' || c == '"')
2956 looking_for = c; /* Don't stop buffering until we see another
38e01259 2957 one of these (or an EOF). */
b30f223b
RS
2958
2959 /* Handle backslash. */
2960 char_escaped = (c == '\\' && ! char_escaped);
2961 }
2962}
c8724862 2963#endif /* !USE_CPPLIB */
0b73773c
NH
2964\f
2965/* Make a variant type in the proper way for C/C++, propagating qualifiers
2966 down to the element type of an array. */
2967
2968tree
2969c_build_type_variant (type, constp, volatilep)
2970 tree type;
2971 int constp, volatilep;
2972{
2973 if (TREE_CODE (type) == ARRAY_TYPE)
3ab1999b
RK
2974 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2975 constp, volatilep),
2976 TYPE_DOMAIN (type));
0b73773c
NH
2977 return build_type_variant (type, constp, volatilep);
2978}
41472af8
MM
2979
2980/* Return the typed-based alias set for T, which may be an expression
2981 or a type. */
2982
2983int
2984c_get_alias_set (t)
2985 tree t;
2986{
41472af8
MM
2987 tree type;
2988
2989 if (t == error_mark_node)
2990 return 0;
2991
2992 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2993 ? t : TREE_TYPE (t);
2994
2995 if (type == error_mark_node)
2996 return 0;
2997
2998 if (TYPE_ALIAS_SET_KNOWN_P (type))
2999 /* If we've already calculated the value, just return it. */
3000 return TYPE_ALIAS_SET (type);
3001
3002 if (TREE_CODE (t) == BIT_FIELD_REF)
3003 /* Perhaps reads and writes to this piece of data alias fields
3004 neighboring the bitfield. Perhaps that's impossible. For now,
3005 let's just assume that bitfields can alias everything, which is
3006 the conservative assumption. */
3007 return 0;
3008 if (TREE_CODE (t) == COMPONENT_REF
3009 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
3010 /* Permit type-punning when accessing a union, provided the
3011 access is directly through the union. For example, this code does
3012 not permit taking the address of a union member and then
3013 storing through it. Even the type-punning allowed here is a
3014 GCC extension, albeit a common and useful one; the C standard
3015 says that such accesses have implementation-defined behavior. */
3016 return 0;
3017 else if (TYPE_MAIN_VARIANT (type) != type)
3018 {
3019 /* The C standard specifically allows aliasing between
3020 cv-qualified variants of types. */
3021 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3022 return TYPE_ALIAS_SET (type);
3023 }
3024 else if (TREE_CODE (type) == INTEGER_TYPE)
3025 {
3026 tree signed_variant;
3027
3028 /* The C standard specifically allows aliasing between signed and
3029 unsigned variants of the same type. We treat the signed
3030 variant as canonical. */
3031 signed_variant = signed_type (type);
3032
3033 if (signed_variant != type)
3034 {
3035 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3036 return TYPE_ALIAS_SET (type);
3037 }
3038 else if (signed_variant == signed_char_type_node)
3039 /* The C standard guarantess that any object may be accessed
3040 via an lvalue that has character type. We don't have to
3041 check for unsigned_char_type_node or char_type_node because
3042 we are specifically looking at the signed variant. */
3043 {
3044 TYPE_ALIAS_SET (type) = 0;
3045 return TYPE_ALIAS_SET (type);
3046 }
3047 }
3048 else if (TREE_CODE (type) == RECORD_TYPE
3049 || TREE_CODE (type) == UNION_TYPE)
3050 {
3051 /* If TYPE is a struct or union type then we're reading or
3052 writing an entire struct. Thus, we don't know anything about
3053 aliasing. (In theory, such an access can only alias objects
3054 whose type is the same as one of the fields, recursively, but
3055 we don't yet make any use of that information.) */
3056 TYPE_ALIAS_SET (type) = 0;
3057 return TYPE_ALIAS_SET (type);
3058 }
3059
3060 /* TYPE is something we haven't seen before. Put it in a new alias
3061 set. */
2ba57343 3062 TYPE_ALIAS_SET (type) = new_alias_set ();
41472af8
MM
3063 return TYPE_ALIAS_SET (type);
3064}