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