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