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