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