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