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