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