]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
88a3dbc1 | 2 | Copyright (C) 1987, 88, 91, 92, 93, 1994 Free Software Foundation, Inc. |
400fbf9f JW |
3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | /* This file is part of the C front end. | |
22 | It contains routines to build C expressions given their operands, | |
23 | including computing the types of the result, C-specific error checks, | |
24 | and some optimization. | |
25 | ||
26 | There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, | |
27 | and to process initializations in declarations (since they work | |
28 | like a strange sort of assignment). */ | |
29 | ||
30 | #include "config.h" | |
31 | #include <stdio.h> | |
32 | #include "tree.h" | |
33 | #include "c-tree.h" | |
34 | #include "flags.h" | |
35 | ||
b71c7f8a | 36 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 37 | message within this initializer. */ |
b71c7f8a | 38 | static int missing_braces_mentioned; |
103b7b17 | 39 | |
3845b542 MS |
40 | extern char *index (); |
41 | extern char *rindex (); | |
42 | ||
75ddf8b0 RK |
43 | static tree quality_type PROTO((tree, tree)); |
44 | static int comp_target_types PROTO((tree, tree)); | |
45 | static int function_types_compatible_p PROTO((tree, tree)); | |
46 | static int type_lists_compatible_p PROTO((tree, tree)); | |
47 | static int self_promoting_type_p PROTO((tree)); | |
48 | static tree decl_constant_value PROTO((tree)); | |
49 | static tree lookup_field PROTO((tree, tree, tree *)); | |
50 | static tree convert_arguments PROTO((tree, tree, tree, tree)); | |
51 | static tree pointer_int_sum PROTO((enum tree_code, tree, tree)); | |
52 | static tree pointer_diff PROTO((tree, tree)); | |
53 | static tree unary_complex_lvalue PROTO((enum tree_code, tree)); | |
54 | static void pedantic_lvalue_warning PROTO((enum tree_code)); | |
55 | static tree internal_build_compound_expr PROTO((tree, int)); | |
56 | static tree convert_for_assignment PROTO((tree, tree, char *, tree, | |
57 | tree, int)); | |
58 | static void warn_for_assignment PROTO((char *, char *, tree, int)); | |
59 | static tree valid_compound_expr_initializer PROTO((tree, tree)); | |
60 | static void push_string PROTO((char *)); | |
61 | static void push_member_name PROTO((tree)); | |
62 | static void push_array_bounds PROTO((int)); | |
63 | static int spelling_length PROTO((void)); | |
64 | static char *print_spelling PROTO((char *)); | |
65 | static char *get_spelling PROTO((char *)); | |
66 | static void warning_init PROTO((char *, char *, | |
67 | char *)); | |
68 | static tree digest_init PROTO((tree, tree, int, int)); | |
69 | static void check_init_type_bitfields PROTO((tree)); | |
70 | static void output_init_element PROTO((tree, tree, tree, int)); | |
71 | static void output_pending_init_elements PROTO((int)); | |
400fbf9f JW |
72 | \f |
73 | /* Do `exp = require_complete_type (exp);' to make sure exp | |
74 | does not have an incomplete type. (That includes void types.) */ | |
75 | ||
76 | tree | |
77 | require_complete_type (value) | |
78 | tree value; | |
79 | { | |
80 | tree type = TREE_TYPE (value); | |
81 | ||
82 | /* First, detect a valid value with a complete type. */ | |
83 | if (TYPE_SIZE (type) != 0 | |
84 | && type != void_type_node) | |
85 | return value; | |
86 | ||
87 | incomplete_type_error (value, type); | |
88 | return error_mark_node; | |
89 | } | |
90 | ||
91 | /* Print an error message for invalid use of an incomplete type. | |
92 | VALUE is the expression that was used (or 0 if that isn't known) | |
93 | and TYPE is the type that was invalid. */ | |
94 | ||
95 | void | |
96 | incomplete_type_error (value, type) | |
97 | tree value; | |
98 | tree type; | |
99 | { | |
100 | char *errmsg; | |
101 | ||
102 | /* Avoid duplicate error message. */ | |
103 | if (TREE_CODE (type) == ERROR_MARK) | |
104 | return; | |
105 | ||
106 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
107 | || TREE_CODE (value) == PARM_DECL)) | |
108 | error ("`%s' has an incomplete type", | |
109 | IDENTIFIER_POINTER (DECL_NAME (value))); | |
110 | else | |
111 | { | |
112 | retry: | |
113 | /* We must print an error message. Be clever about what it says. */ | |
114 | ||
115 | switch (TREE_CODE (type)) | |
116 | { | |
117 | case RECORD_TYPE: | |
118 | errmsg = "invalid use of undefined type `struct %s'"; | |
119 | break; | |
120 | ||
121 | case UNION_TYPE: | |
122 | errmsg = "invalid use of undefined type `union %s'"; | |
123 | break; | |
124 | ||
125 | case ENUMERAL_TYPE: | |
126 | errmsg = "invalid use of undefined type `enum %s'"; | |
127 | break; | |
128 | ||
129 | case VOID_TYPE: | |
130 | error ("invalid use of void expression"); | |
131 | return; | |
132 | ||
133 | case ARRAY_TYPE: | |
134 | if (TYPE_DOMAIN (type)) | |
135 | { | |
136 | type = TREE_TYPE (type); | |
137 | goto retry; | |
138 | } | |
139 | error ("invalid use of array with unspecified bounds"); | |
140 | return; | |
141 | ||
142 | default: | |
143 | abort (); | |
144 | } | |
145 | ||
146 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
147 | error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type))); | |
148 | else | |
149 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
150 | error ("invalid use of incomplete typedef `%s'", | |
151 | IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); | |
152 | } | |
153 | } | |
154 | ||
155 | /* Return a variant of TYPE which has all the type qualifiers of LIKE | |
156 | as well as those of TYPE. */ | |
157 | ||
158 | static tree | |
159 | qualify_type (type, like) | |
160 | tree type, like; | |
161 | { | |
162 | int constflag = TYPE_READONLY (type) || TYPE_READONLY (like); | |
163 | int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like); | |
164 | return c_build_type_variant (type, constflag, volflag); | |
165 | } | |
166 | \f | |
167 | /* Return the common type of two types. | |
168 | We assume that comptypes has already been done and returned 1; | |
6cb72a7d RS |
169 | if that isn't so, this may crash. In particular, we assume that qualifiers |
170 | match. | |
400fbf9f JW |
171 | |
172 | This is the type for the result of most arithmetic operations | |
6cb72a7d | 173 | if the operands have the given two types. */ |
400fbf9f JW |
174 | |
175 | tree | |
176 | common_type (t1, t2) | |
177 | tree t1, t2; | |
178 | { | |
179 | register enum tree_code code1; | |
180 | register enum tree_code code2; | |
181 | ||
182 | /* Save time if the two types are the same. */ | |
183 | ||
184 | if (t1 == t2) return t1; | |
185 | ||
186 | /* If one type is nonsense, use the other. */ | |
187 | if (t1 == error_mark_node) | |
188 | return t2; | |
189 | if (t2 == error_mark_node) | |
190 | return t1; | |
191 | ||
192 | /* Treat an enum type as the unsigned integer type of the same width. */ | |
193 | ||
194 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
195 | t1 = type_for_size (TYPE_PRECISION (t1), 1); | |
196 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
197 | t2 = type_for_size (TYPE_PRECISION (t2), 1); | |
198 | ||
199 | code1 = TREE_CODE (t1); | |
200 | code2 = TREE_CODE (t2); | |
201 | ||
75326e8c RK |
202 | /* If one type is complex, form the common type of the non-complex |
203 | components, then make that complex. Use T1 or T2 if it is the | |
204 | required type. */ | |
b6a10c9f RS |
205 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) |
206 | { | |
75326e8c RK |
207 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; |
208 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
209 | tree subtype = common_type (subtype1, subtype2); | |
210 | ||
211 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
212 | return t1; | |
213 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
214 | return t2; | |
b6a10c9f | 215 | else |
75326e8c | 216 | return build_complex_type (subtype); |
b6a10c9f RS |
217 | } |
218 | ||
400fbf9f JW |
219 | switch (code1) |
220 | { | |
221 | case INTEGER_TYPE: | |
222 | case REAL_TYPE: | |
223 | /* If only one is real, use it as the result. */ | |
224 | ||
225 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
226 | return t1; | |
227 | ||
228 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
229 | return t2; | |
230 | ||
231 | /* Both real or both integers; use the one with greater precision. */ | |
232 | ||
233 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
234 | return t1; | |
235 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
236 | return t2; | |
237 | ||
238 | /* Same precision. Prefer longs to ints even when same size. */ | |
239 | ||
36618528 RS |
240 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node |
241 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
400fbf9f JW |
242 | return long_unsigned_type_node; |
243 | ||
36618528 RS |
244 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node |
245 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
400fbf9f JW |
246 | { |
247 | /* But preserve unsignedness from the other type, | |
248 | since long cannot hold all the values of an unsigned int. */ | |
249 | if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) | |
250 | return long_unsigned_type_node; | |
251 | return long_integer_type_node; | |
252 | } | |
253 | ||
254 | /* Otherwise prefer the unsigned one. */ | |
255 | ||
256 | if (TREE_UNSIGNED (t1)) | |
257 | return t1; | |
258 | else return t2; | |
259 | ||
260 | case POINTER_TYPE: | |
400fbf9f JW |
261 | /* For two pointers, do this recursively on the target type, |
262 | and combine the qualifiers of the two types' targets. */ | |
8706edbc RS |
263 | /* This code was turned off; I don't know why. |
264 | But ANSI C specifies doing this with the qualifiers. | |
265 | So I turned it on again. */ | |
400fbf9f JW |
266 | { |
267 | tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)), | |
268 | TYPE_MAIN_VARIANT (TREE_TYPE (t2))); | |
269 | int constp | |
270 | = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2)); | |
271 | int volatilep | |
272 | = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2)); | |
273 | return build_pointer_type (c_build_type_variant (target, constp, volatilep)); | |
274 | } | |
8706edbc | 275 | #if 0 |
400fbf9f | 276 | return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2))); |
8706edbc | 277 | #endif |
400fbf9f JW |
278 | |
279 | case ARRAY_TYPE: | |
280 | { | |
281 | tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
282 | /* Save space: see if the result is identical to one of the args. */ | |
283 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) | |
284 | return t1; | |
285 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) | |
286 | return t2; | |
287 | /* Merge the element types, and have a size if either arg has one. */ | |
288 | return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); | |
289 | } | |
290 | ||
291 | case FUNCTION_TYPE: | |
292 | /* Function types: prefer the one that specified arg types. | |
293 | If both do, merge the arg types. Also merge the return types. */ | |
294 | { | |
295 | tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
296 | tree p1 = TYPE_ARG_TYPES (t1); | |
297 | tree p2 = TYPE_ARG_TYPES (t2); | |
298 | int len; | |
299 | tree newargs, n; | |
300 | int i; | |
301 | ||
302 | /* Save space: see if the result is identical to one of the args. */ | |
303 | if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2)) | |
304 | return t1; | |
305 | if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1)) | |
306 | return t2; | |
307 | ||
308 | /* Simple way if one arg fails to specify argument types. */ | |
309 | if (TYPE_ARG_TYPES (t1) == 0) | |
310 | return build_function_type (valtype, TYPE_ARG_TYPES (t2)); | |
311 | if (TYPE_ARG_TYPES (t2) == 0) | |
312 | return build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
313 | ||
314 | /* If both args specify argument types, we must merge the two | |
315 | lists, argument by argument. */ | |
316 | ||
317 | len = list_length (p1); | |
318 | newargs = 0; | |
319 | ||
320 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 321 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
322 | |
323 | n = newargs; | |
324 | ||
325 | for (; p1; | |
326 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
327 | { | |
328 | /* A null type means arg type is not specified. | |
329 | Take whatever the other function type has. */ | |
330 | if (TREE_VALUE (p1) == 0) | |
331 | { | |
332 | TREE_VALUE (n) = TREE_VALUE (p2); | |
333 | goto parm_done; | |
334 | } | |
335 | if (TREE_VALUE (p2) == 0) | |
336 | { | |
337 | TREE_VALUE (n) = TREE_VALUE (p1); | |
338 | goto parm_done; | |
339 | } | |
340 | ||
341 | /* Given wait (union {union wait *u; int *i} *) | |
342 | and wait (union wait *), | |
343 | prefer union wait * as type of parm. */ | |
344 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
345 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
346 | { | |
347 | tree memb; | |
348 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); | |
349 | memb; memb = TREE_CHAIN (memb)) | |
350 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2))) | |
351 | { | |
352 | TREE_VALUE (n) = TREE_VALUE (p2); | |
353 | if (pedantic) | |
354 | pedwarn ("function types not truly compatible in ANSI C"); | |
355 | goto parm_done; | |
356 | } | |
357 | } | |
358 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
359 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
360 | { | |
361 | tree memb; | |
362 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); | |
363 | memb; memb = TREE_CHAIN (memb)) | |
364 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1))) | |
365 | { | |
366 | TREE_VALUE (n) = TREE_VALUE (p1); | |
367 | if (pedantic) | |
368 | pedwarn ("function types not truly compatible in ANSI C"); | |
369 | goto parm_done; | |
370 | } | |
371 | } | |
372 | TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2)); | |
373 | parm_done: ; | |
374 | } | |
375 | ||
376 | return build_function_type (valtype, newargs); | |
377 | } | |
378 | ||
379 | default: | |
380 | return t1; | |
381 | } | |
382 | ||
383 | } | |
384 | \f | |
385 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment | |
386 | or various other operations. Return 2 if they are compatible | |
387 | but a warning may be needed if you use them together. */ | |
388 | ||
389 | int | |
390 | comptypes (type1, type2) | |
391 | tree type1, type2; | |
392 | { | |
393 | register tree t1 = type1; | |
394 | register tree t2 = type2; | |
395 | ||
396 | /* Suppress errors caused by previously reported errors. */ | |
397 | ||
398 | if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
399 | return 1; | |
400 | ||
401 | /* Treat an enum type as the unsigned integer type of the same width. */ | |
402 | ||
403 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
404 | t1 = type_for_size (TYPE_PRECISION (t1), 1); | |
405 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
406 | t2 = type_for_size (TYPE_PRECISION (t2), 1); | |
407 | ||
408 | if (t1 == t2) | |
409 | return 1; | |
410 | ||
411 | /* Different classes of types can't be compatible. */ | |
412 | ||
413 | if (TREE_CODE (t1) != TREE_CODE (t2)) return 0; | |
414 | ||
415 | /* Qualifiers must match. */ | |
416 | ||
417 | if (TYPE_READONLY (t1) != TYPE_READONLY (t2)) | |
418 | return 0; | |
419 | if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2)) | |
420 | return 0; | |
421 | ||
08632da2 RS |
422 | /* Allow for two different type nodes which have essentially the same |
423 | definition. Note that we already checked for equality of the type | |
424 | type qualifiers (just above). */ | |
400fbf9f JW |
425 | |
426 | if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
427 | return 1; | |
428 | ||
429 | switch (TREE_CODE (t1)) | |
430 | { | |
431 | case POINTER_TYPE: | |
432 | return (TREE_TYPE (t1) == TREE_TYPE (t2) | |
433 | ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2))); | |
434 | ||
435 | case FUNCTION_TYPE: | |
436 | return function_types_compatible_p (t1, t2); | |
437 | ||
438 | case ARRAY_TYPE: | |
439 | { | |
440 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
441 | int val = 1; | |
442 | tree d1 = TYPE_DOMAIN (t1); | |
443 | tree d2 = TYPE_DOMAIN (t2); | |
444 | ||
445 | /* Target types must match incl. qualifiers. */ | |
446 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
447 | && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))) | |
448 | return 0; | |
449 | ||
450 | /* Sizes must match unless one is missing or variable. */ | |
451 | if (d1 == 0 || d2 == 0 || d1 == d2 | |
452 | || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
453 | || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
454 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST | |
455 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST) | |
456 | return val; | |
457 | ||
458 | return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1)) | |
459 | == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2))) | |
460 | && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1)) | |
461 | == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2))) | |
462 | && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1)) | |
463 | == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2))) | |
464 | && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1)) | |
465 | == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))) | |
466 | ? val : 0); | |
467 | } | |
468 | ||
469 | case RECORD_TYPE: | |
392202b0 TW |
470 | if (maybe_objc_comptypes (t1, t2, 0) == 1) |
471 | return 1; | |
400fbf9f JW |
472 | } |
473 | return 0; | |
474 | } | |
475 | ||
476 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, | |
477 | ignoring their qualifiers. */ | |
478 | ||
479 | static int | |
480 | comp_target_types (ttl, ttr) | |
481 | tree ttl, ttr; | |
482 | { | |
392202b0 | 483 | int val; |
8b40563c | 484 | |
392202b0 TW |
485 | /* Give maybe_objc_comptypes a crack at letting these types through. */ |
486 | if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0) | |
487 | return val; | |
8b40563c | 488 | |
392202b0 TW |
489 | val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)), |
490 | TYPE_MAIN_VARIANT (TREE_TYPE (ttr))); | |
8b40563c | 491 | |
400fbf9f JW |
492 | if (val == 2 && pedantic) |
493 | pedwarn ("types are not quite compatible"); | |
494 | return val; | |
495 | } | |
496 | \f | |
497 | /* Subroutines of `comptypes'. */ | |
498 | ||
499 | /* Return 1 if two function types F1 and F2 are compatible. | |
500 | If either type specifies no argument types, | |
501 | the other must specify a fixed number of self-promoting arg types. | |
502 | Otherwise, if one type specifies only the number of arguments, | |
503 | the other must specify that number of self-promoting arg types. | |
504 | Otherwise, the argument types must match. */ | |
505 | ||
506 | static int | |
507 | function_types_compatible_p (f1, f2) | |
508 | tree f1, f2; | |
509 | { | |
510 | tree args1, args2; | |
511 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
512 | int val = 1; | |
513 | int val1; | |
514 | ||
515 | if (!(TREE_TYPE (f1) == TREE_TYPE (f2) | |
516 | || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2))))) | |
517 | return 0; | |
518 | ||
519 | args1 = TYPE_ARG_TYPES (f1); | |
520 | args2 = TYPE_ARG_TYPES (f2); | |
521 | ||
522 | /* An unspecified parmlist matches any specified parmlist | |
523 | whose argument types don't need default promotions. */ | |
524 | ||
525 | if (args1 == 0) | |
526 | { | |
527 | if (!self_promoting_args_p (args2)) | |
528 | return 0; | |
529 | /* If one of these types comes from a non-prototype fn definition, | |
530 | compare that with the other type's arglist. | |
531 | If they don't match, ask for a warning (but no error). */ | |
532 | if (TYPE_ACTUAL_ARG_TYPES (f1) | |
533 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1))) | |
534 | val = 2; | |
535 | return val; | |
536 | } | |
537 | if (args2 == 0) | |
538 | { | |
539 | if (!self_promoting_args_p (args1)) | |
540 | return 0; | |
541 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
542 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2))) | |
543 | val = 2; | |
544 | return val; | |
545 | } | |
546 | ||
547 | /* Both types have argument lists: compare them and propagate results. */ | |
548 | val1 = type_lists_compatible_p (args1, args2); | |
549 | return val1 != 1 ? val1 : val; | |
550 | } | |
551 | ||
552 | /* Check two lists of types for compatibility, | |
553 | returning 0 for incompatible, 1 for compatible, | |
554 | or 2 for compatible with warning. */ | |
555 | ||
556 | static int | |
557 | type_lists_compatible_p (args1, args2) | |
558 | tree args1, args2; | |
559 | { | |
560 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
561 | int val = 1; | |
9d5f3e49 | 562 | int newval = 0; |
400fbf9f JW |
563 | |
564 | while (1) | |
565 | { | |
566 | if (args1 == 0 && args2 == 0) | |
567 | return val; | |
568 | /* If one list is shorter than the other, | |
569 | they fail to match. */ | |
570 | if (args1 == 0 || args2 == 0) | |
571 | return 0; | |
572 | /* A null pointer instead of a type | |
573 | means there is supposed to be an argument | |
574 | but nothing is specified about what type it has. | |
575 | So match anything that self-promotes. */ | |
576 | if (TREE_VALUE (args1) == 0) | |
577 | { | |
578 | if (! self_promoting_type_p (TREE_VALUE (args2))) | |
579 | return 0; | |
580 | } | |
581 | else if (TREE_VALUE (args2) == 0) | |
582 | { | |
583 | if (! self_promoting_type_p (TREE_VALUE (args1))) | |
584 | return 0; | |
585 | } | |
586 | else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2)))) | |
587 | { | |
588 | /* Allow wait (union {union wait *u; int *i} *) | |
589 | and wait (union wait *) to be compatible. */ | |
590 | if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE | |
591 | && TYPE_NAME (TREE_VALUE (args1)) == 0 | |
592 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST | |
593 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)), | |
594 | TYPE_SIZE (TREE_VALUE (args2)))) | |
595 | { | |
596 | tree memb; | |
597 | for (memb = TYPE_FIELDS (TREE_VALUE (args1)); | |
598 | memb; memb = TREE_CHAIN (memb)) | |
599 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2))) | |
600 | break; | |
601 | if (memb == 0) | |
602 | return 0; | |
603 | } | |
604 | else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE | |
605 | && TYPE_NAME (TREE_VALUE (args2)) == 0 | |
606 | && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST | |
607 | && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)), | |
608 | TYPE_SIZE (TREE_VALUE (args1)))) | |
609 | { | |
610 | tree memb; | |
611 | for (memb = TYPE_FIELDS (TREE_VALUE (args2)); | |
612 | memb; memb = TREE_CHAIN (memb)) | |
613 | if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1))) | |
614 | break; | |
615 | if (memb == 0) | |
616 | return 0; | |
617 | } | |
618 | else | |
619 | return 0; | |
620 | } | |
621 | ||
622 | /* comptypes said ok, but record if it said to warn. */ | |
623 | if (newval > val) | |
624 | val = newval; | |
625 | ||
626 | args1 = TREE_CHAIN (args1); | |
627 | args2 = TREE_CHAIN (args2); | |
628 | } | |
629 | } | |
630 | ||
631 | /* Return 1 if PARMS specifies a fixed number of parameters | |
632 | and none of their types is affected by default promotions. */ | |
633 | ||
805f961c | 634 | int |
400fbf9f JW |
635 | self_promoting_args_p (parms) |
636 | tree parms; | |
637 | { | |
638 | register tree t; | |
639 | for (t = parms; t; t = TREE_CHAIN (t)) | |
640 | { | |
641 | register tree type = TREE_VALUE (t); | |
642 | ||
643 | if (TREE_CHAIN (t) == 0 && type != void_type_node) | |
644 | return 0; | |
645 | ||
d627ed1b RS |
646 | if (type == 0) |
647 | return 0; | |
648 | ||
6cb72a7d | 649 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
400fbf9f JW |
650 | return 0; |
651 | ||
d627ed1b | 652 | if (C_PROMOTING_INTEGER_TYPE_P (type)) |
400fbf9f JW |
653 | return 0; |
654 | } | |
655 | return 1; | |
656 | } | |
657 | ||
658 | /* Return 1 if TYPE is not affected by default promotions. */ | |
659 | ||
660 | static int | |
661 | self_promoting_type_p (type) | |
662 | tree type; | |
663 | { | |
6cb72a7d | 664 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
400fbf9f JW |
665 | return 0; |
666 | ||
d627ed1b | 667 | if (C_PROMOTING_INTEGER_TYPE_P (type)) |
400fbf9f JW |
668 | return 0; |
669 | ||
670 | return 1; | |
671 | } | |
672 | \f | |
673 | /* Return an unsigned type the same as TYPE in other respects. */ | |
674 | ||
675 | tree | |
676 | unsigned_type (type) | |
677 | tree type; | |
678 | { | |
6cb72a7d RS |
679 | tree type1 = TYPE_MAIN_VARIANT (type); |
680 | if (type1 == signed_char_type_node || type1 == char_type_node) | |
400fbf9f | 681 | return unsigned_char_type_node; |
6cb72a7d | 682 | if (type1 == integer_type_node) |
400fbf9f | 683 | return unsigned_type_node; |
6cb72a7d | 684 | if (type1 == short_integer_type_node) |
400fbf9f | 685 | return short_unsigned_type_node; |
6cb72a7d | 686 | if (type1 == long_integer_type_node) |
400fbf9f | 687 | return long_unsigned_type_node; |
6cb72a7d | 688 | if (type1 == long_long_integer_type_node) |
400fbf9f JW |
689 | return long_long_unsigned_type_node; |
690 | return type; | |
691 | } | |
692 | ||
693 | /* Return a signed type the same as TYPE in other respects. */ | |
694 | ||
695 | tree | |
696 | signed_type (type) | |
697 | tree type; | |
698 | { | |
6cb72a7d RS |
699 | tree type1 = TYPE_MAIN_VARIANT (type); |
700 | if (type1 == unsigned_char_type_node || type1 == char_type_node) | |
400fbf9f | 701 | return signed_char_type_node; |
6cb72a7d | 702 | if (type1 == unsigned_type_node) |
400fbf9f | 703 | return integer_type_node; |
6cb72a7d | 704 | if (type1 == short_unsigned_type_node) |
400fbf9f | 705 | return short_integer_type_node; |
6cb72a7d | 706 | if (type1 == long_unsigned_type_node) |
400fbf9f | 707 | return long_integer_type_node; |
6cb72a7d | 708 | if (type1 == long_long_unsigned_type_node) |
400fbf9f JW |
709 | return long_long_integer_type_node; |
710 | return type; | |
711 | } | |
712 | ||
713 | /* Return a type the same as TYPE except unsigned or | |
714 | signed according to UNSIGNEDP. */ | |
715 | ||
716 | tree | |
717 | signed_or_unsigned_type (unsignedp, type) | |
718 | int unsignedp; | |
719 | tree type; | |
720 | { | |
400f8e7d | 721 | if (! INTEGRAL_TYPE_P (type)) |
400fbf9f JW |
722 | return type; |
723 | if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) | |
724 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
725 | if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) | |
726 | return unsignedp ? unsigned_type_node : integer_type_node; | |
727 | if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) | |
728 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
729 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) | |
730 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
731 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) | |
732 | return (unsignedp ? long_long_unsigned_type_node | |
733 | : long_long_integer_type_node); | |
734 | return type; | |
735 | } | |
736 | ||
737 | /* Compute the value of the `sizeof' operator. */ | |
738 | ||
739 | tree | |
740 | c_sizeof (type) | |
741 | tree type; | |
742 | { | |
743 | enum tree_code code = TREE_CODE (type); | |
f7c8fb3f | 744 | tree t; |
400fbf9f JW |
745 | |
746 | if (code == FUNCTION_TYPE) | |
747 | { | |
748 | if (pedantic || warn_pointer_arith) | |
749 | pedwarn ("sizeof applied to a function type"); | |
750 | return size_int (1); | |
751 | } | |
752 | if (code == VOID_TYPE) | |
753 | { | |
754 | if (pedantic || warn_pointer_arith) | |
755 | pedwarn ("sizeof applied to a void type"); | |
756 | return size_int (1); | |
757 | } | |
758 | if (code == ERROR_MARK) | |
759 | return size_int (1); | |
760 | if (TYPE_SIZE (type) == 0) | |
761 | { | |
762 | error ("sizeof applied to an incomplete type"); | |
763 | return size_int (0); | |
764 | } | |
765 | ||
766 | /* Convert in case a char is more than one unit. */ | |
f7c8fb3f RS |
767 | t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), |
768 | size_int (TYPE_PRECISION (char_type_node))); | |
fa427131 | 769 | /* size_binop does not put the constant in range, so do it now. */ |
10d5caec PE |
770 | if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0)) |
771 | TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1; | |
f7c8fb3f | 772 | return t; |
400fbf9f JW |
773 | } |
774 | ||
775 | tree | |
776 | c_sizeof_nowarn (type) | |
777 | tree type; | |
778 | { | |
779 | enum tree_code code = TREE_CODE (type); | |
f7c8fb3f | 780 | tree t; |
400fbf9f JW |
781 | |
782 | if (code == FUNCTION_TYPE | |
783 | || code == VOID_TYPE | |
784 | || code == ERROR_MARK) | |
785 | return size_int (1); | |
786 | if (TYPE_SIZE (type) == 0) | |
787 | return size_int (0); | |
788 | ||
789 | /* Convert in case a char is more than one unit. */ | |
f7c8fb3f RS |
790 | t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), |
791 | size_int (TYPE_PRECISION (char_type_node))); | |
e58cd767 | 792 | force_fit_type (t, 0); |
f7c8fb3f | 793 | return t; |
400fbf9f JW |
794 | } |
795 | ||
796 | /* Compute the size to increment a pointer by. */ | |
797 | ||
798 | tree | |
799 | c_size_in_bytes (type) | |
800 | tree type; | |
801 | { | |
802 | enum tree_code code = TREE_CODE (type); | |
f7c8fb3f | 803 | tree t; |
400fbf9f JW |
804 | |
805 | if (code == FUNCTION_TYPE) | |
806 | return size_int (1); | |
807 | if (code == VOID_TYPE) | |
808 | return size_int (1); | |
809 | if (code == ERROR_MARK) | |
810 | return size_int (1); | |
811 | if (TYPE_SIZE (type) == 0) | |
812 | { | |
813 | error ("arithmetic on pointer to an incomplete type"); | |
814 | return size_int (1); | |
815 | } | |
816 | ||
817 | /* Convert in case a char is more than one unit. */ | |
f7c8fb3f | 818 | t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), |
400fbf9f | 819 | size_int (BITS_PER_UNIT)); |
e58cd767 | 820 | force_fit_type (t, 0); |
f7c8fb3f | 821 | return t; |
400fbf9f JW |
822 | } |
823 | ||
824 | /* Implement the __alignof keyword: Return the minimum required | |
825 | alignment of TYPE, measured in bytes. */ | |
826 | ||
827 | tree | |
828 | c_alignof (type) | |
829 | tree type; | |
830 | { | |
831 | enum tree_code code = TREE_CODE (type); | |
832 | ||
833 | if (code == FUNCTION_TYPE) | |
834 | return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
835 | ||
836 | if (code == VOID_TYPE || code == ERROR_MARK) | |
837 | return size_int (1); | |
838 | ||
839 | return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); | |
840 | } | |
841 | \f | |
842 | /* Implement the __alignof keyword: Return the minimum required | |
843 | alignment of EXPR, measured in bytes. For VAR_DECL's and | |
844 | FIELD_DECL's return DECL_ALIGN (which can be set from an | |
845 | "aligned" __attribute__ specification). */ | |
9e9bd45d | 846 | |
400fbf9f JW |
847 | tree |
848 | c_alignof_expr (expr) | |
849 | tree expr; | |
850 | { | |
851 | if (TREE_CODE (expr) == VAR_DECL) | |
852 | return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT); | |
853 | ||
854 | if (TREE_CODE (expr) == COMPONENT_REF | |
855 | && DECL_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
856 | { | |
857 | error ("`__alignof' applied to a bit-field"); | |
858 | return size_int (1); | |
859 | } | |
860 | else if (TREE_CODE (expr) == COMPONENT_REF | |
861 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) | |
862 | return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT); | |
863 | ||
864 | if (TREE_CODE (expr) == INDIRECT_REF) | |
865 | { | |
866 | tree t = TREE_OPERAND (expr, 0); | |
867 | tree best = t; | |
868 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
869 | ||
870 | while (TREE_CODE (t) == NOP_EXPR | |
871 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) | |
872 | { | |
873 | int thisalign; | |
874 | ||
875 | t = TREE_OPERAND (t, 0); | |
876 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
877 | if (thisalign > bestalign) | |
878 | best = t, bestalign = thisalign; | |
879 | } | |
880 | return c_alignof (TREE_TYPE (TREE_TYPE (best))); | |
881 | } | |
882 | else | |
883 | return c_alignof (TREE_TYPE (expr)); | |
884 | } | |
885 | /* Return either DECL or its known constant value (if it has one). */ | |
886 | ||
887 | static tree | |
888 | decl_constant_value (decl) | |
889 | tree decl; | |
890 | { | |
891 | if (! TREE_PUBLIC (decl) | |
892 | /* Don't change a variable array bound or initial value to a constant | |
893 | in a place where a variable is invalid. */ | |
894 | && current_function_decl != 0 | |
895 | && ! pedantic | |
896 | && ! TREE_THIS_VOLATILE (decl) | |
897 | && DECL_INITIAL (decl) != 0 | |
898 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
899 | /* This is invalid if initial value is not constant. | |
900 | If it has either a function call, a memory reference, | |
901 | or a variable, then re-evaluating it could give different results. */ | |
902 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
903 | /* Check for cases where this is sub-optimal, even though valid. */ | |
904 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR | |
905 | && DECL_MODE (decl) != BLKmode) | |
906 | return DECL_INITIAL (decl); | |
907 | return decl; | |
908 | } | |
909 | ||
910 | /* Perform default promotions for C data used in expressions. | |
911 | Arrays and functions are converted to pointers; | |
912 | enumeral types or short or char, to int. | |
913 | In addition, manifest constants symbols are replaced by their values. */ | |
914 | ||
915 | tree | |
916 | default_conversion (exp) | |
917 | tree exp; | |
918 | { | |
919 | register tree type = TREE_TYPE (exp); | |
920 | register enum tree_code code = TREE_CODE (type); | |
921 | ||
922 | /* Constants can be used directly unless they're not loadable. */ | |
923 | if (TREE_CODE (exp) == CONST_DECL) | |
924 | exp = DECL_INITIAL (exp); | |
925 | /* Replace a nonvolatile const static variable with its value. */ | |
926 | else if (optimize | |
927 | && TREE_CODE (exp) == VAR_DECL | |
928 | && TREE_READONLY (exp) | |
ec193993 RS |
929 | /* But not for iterators! */ |
930 | && !ITERATOR_P (exp) | |
400fbf9f JW |
931 | && DECL_MODE (exp) != BLKmode) |
932 | { | |
933 | exp = decl_constant_value (exp); | |
934 | type = TREE_TYPE (exp); | |
935 | } | |
936 | ||
a7d53fce RS |
937 | /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as |
938 | an lvalue. */ | |
939 | /* Do not use STRIP_NOPS here! It will remove conversions from pointer | |
940 | to integer and cause infinite recursion. */ | |
941 | while (TREE_CODE (exp) == NON_LVALUE_EXPR | |
942 | || (TREE_CODE (exp) == NOP_EXPR | |
943 | && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) | |
944 | exp = TREE_OPERAND (exp, 0); | |
400fbf9f JW |
945 | |
946 | /* Normally convert enums to int, | |
947 | but convert wide enums to something wider. */ | |
948 | if (code == ENUMERAL_TYPE) | |
949 | { | |
950 | type = type_for_size (MAX (TYPE_PRECISION (type), | |
951 | TYPE_PRECISION (integer_type_node)), | |
86463d5d RS |
952 | ((flag_traditional |
953 | || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)) | |
954 | && TREE_UNSIGNED (type))); | |
400fbf9f JW |
955 | return convert (type, exp); |
956 | } | |
957 | ||
d627ed1b | 958 | if (C_PROMOTING_INTEGER_TYPE_P (type)) |
400fbf9f | 959 | { |
e83d45c4 RS |
960 | /* Traditionally, unsignedness is preserved in default promotions. |
961 | Also preserve unsignedness if not really getting any wider. */ | |
962 | if (TREE_UNSIGNED (type) | |
963 | && (flag_traditional | |
964 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
400fbf9f JW |
965 | return convert (unsigned_type_node, exp); |
966 | return convert (integer_type_node, exp); | |
967 | } | |
19d76e60 RK |
968 | if (flag_traditional && !flag_allow_single_precision |
969 | && TYPE_MAIN_VARIANT (type) == float_type_node) | |
400fbf9f JW |
970 | return convert (double_type_node, exp); |
971 | if (code == VOID_TYPE) | |
972 | { | |
973 | error ("void value not ignored as it ought to be"); | |
974 | return error_mark_node; | |
975 | } | |
976 | if (code == FUNCTION_TYPE) | |
977 | { | |
978 | return build_unary_op (ADDR_EXPR, exp, 0); | |
979 | } | |
980 | if (code == ARRAY_TYPE) | |
981 | { | |
982 | register tree adr; | |
983 | tree restype = TREE_TYPE (type); | |
984 | tree ptrtype; | |
d11fdb45 RS |
985 | int constp = 0; |
986 | int volatilep = 0; | |
987 | ||
988 | if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' | |
989 | || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd') | |
990 | { | |
991 | constp = TREE_READONLY (exp); | |
992 | volatilep = TREE_THIS_VOLATILE (exp); | |
993 | } | |
994 | ||
995 | if (TYPE_READONLY (type) || TYPE_VOLATILE (type) | |
996 | || constp || volatilep) | |
997 | restype = c_build_type_variant (restype, | |
998 | TYPE_READONLY (type) || constp, | |
999 | TYPE_VOLATILE (type) || volatilep); | |
400fbf9f JW |
1000 | |
1001 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1002 | return convert (TYPE_POINTER_TO (restype), | |
1003 | TREE_OPERAND (exp, 0)); | |
1004 | ||
1005 | if (TREE_CODE (exp) == COMPOUND_EXPR) | |
1006 | { | |
1007 | tree op1 = default_conversion (TREE_OPERAND (exp, 1)); | |
1008 | return build (COMPOUND_EXPR, TREE_TYPE (op1), | |
1009 | TREE_OPERAND (exp, 0), op1); | |
1010 | } | |
1011 | ||
1012 | if (!lvalue_p (exp) | |
1013 | && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) | |
1014 | { | |
8efabd13 RS |
1015 | error ("invalid use of non-lvalue array"); |
1016 | return error_mark_node; | |
400fbf9f JW |
1017 | } |
1018 | ||
400fbf9f JW |
1019 | ptrtype = build_pointer_type (restype); |
1020 | ||
1021 | if (TREE_CODE (exp) == VAR_DECL) | |
1022 | { | |
1023 | /* ??? This is not really quite correct | |
1024 | in that the type of the operand of ADDR_EXPR | |
1025 | is not the target type of the type of the ADDR_EXPR itself. | |
1026 | Question is, can this lossage be avoided? */ | |
1027 | adr = build1 (ADDR_EXPR, ptrtype, exp); | |
1028 | if (mark_addressable (exp) == 0) | |
1029 | return error_mark_node; | |
1030 | TREE_CONSTANT (adr) = staticp (exp); | |
1031 | TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ | |
1032 | return adr; | |
1033 | } | |
1034 | /* This way is better for a COMPONENT_REF since it can | |
1035 | simplify the offset for a component. */ | |
1036 | adr = build_unary_op (ADDR_EXPR, exp, 1); | |
1037 | return convert (ptrtype, adr); | |
1038 | } | |
1039 | return exp; | |
1040 | } | |
1041 | \f | |
19d76e60 RK |
1042 | /* Look up component name in the structure type definition. |
1043 | ||
1044 | If this component name is found indirectly within an anonymous union, | |
1045 | store in *INDIRECT the component which directly contains | |
1046 | that anonymous union. Otherwise, set *INDIRECT to 0. */ | |
2f2d13da DE |
1047 | |
1048 | static tree | |
19d76e60 | 1049 | lookup_field (type, component, indirect) |
2f2d13da | 1050 | tree type, component; |
19d76e60 | 1051 | tree *indirect; |
2f2d13da DE |
1052 | { |
1053 | tree field; | |
1054 | ||
1055 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
1056 | to the field elements. Use a binary search on this array to quickly | |
1057 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
1058 | will always be set for structures which have many elements. */ | |
1059 | ||
1060 | if (TYPE_LANG_SPECIFIC (type)) | |
1061 | { | |
1062 | int bot, top, half; | |
1063 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0]; | |
1064 | ||
1065 | field = TYPE_FIELDS (type); | |
1066 | bot = 0; | |
1067 | top = TYPE_LANG_SPECIFIC (type)->len; | |
1068 | while (top - bot > 1) | |
1069 | { | |
c1719013 | 1070 | HOST_WIDE_INT cmp; |
2f2d13da DE |
1071 | |
1072 | half = (top - bot + 1) >> 1; | |
1073 | field = field_array[bot+half]; | |
1074 | ||
1075 | if (DECL_NAME (field) == NULL_TREE) | |
1076 | { | |
1077 | /* Step through all anon unions in linear fashion. */ | |
1078 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
1079 | { | |
19d76e60 RK |
1080 | tree anon, junk; |
1081 | ||
2f2d13da | 1082 | field = field_array[bot++]; |
19d76e60 | 1083 | anon = lookup_field (TREE_TYPE (field), component, &junk); |
2f2d13da | 1084 | if (anon != NULL_TREE) |
19d76e60 RK |
1085 | { |
1086 | *indirect = field; | |
1087 | return anon; | |
1088 | } | |
2f2d13da DE |
1089 | } |
1090 | ||
1091 | /* Entire record is only anon unions. */ | |
1092 | if (bot > top) | |
1093 | return NULL_TREE; | |
1094 | ||
1095 | /* Restart the binary search, with new lower bound. */ | |
1096 | continue; | |
1097 | } | |
1098 | ||
c1719013 | 1099 | cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component; |
2f2d13da DE |
1100 | if (cmp == 0) |
1101 | break; | |
1102 | if (cmp < 0) | |
1103 | bot += half; | |
1104 | else | |
1105 | top = bot + half; | |
1106 | } | |
1107 | ||
1108 | if (DECL_NAME (field_array[bot]) == component) | |
1109 | field = field_array[bot]; | |
1110 | else if (DECL_NAME (field) != component) | |
1111 | field = 0; | |
1112 | } | |
1113 | else | |
1114 | { | |
1115 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1116 | { | |
1117 | if (DECL_NAME (field) == NULL_TREE) | |
1118 | { | |
19d76e60 RK |
1119 | tree junk; |
1120 | tree anon = lookup_field (TREE_TYPE (field), component, &junk); | |
2f2d13da | 1121 | if (anon != NULL_TREE) |
19d76e60 RK |
1122 | { |
1123 | *indirect = field; | |
1124 | return anon; | |
1125 | } | |
2f2d13da DE |
1126 | } |
1127 | ||
1128 | if (DECL_NAME (field) == component) | |
1129 | break; | |
1130 | } | |
1131 | } | |
1132 | ||
19d76e60 | 1133 | *indirect = NULL_TREE; |
2f2d13da DE |
1134 | return field; |
1135 | } | |
1136 | ||
400fbf9f JW |
1137 | /* Make an expression to refer to the COMPONENT field of |
1138 | structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */ | |
1139 | ||
1140 | tree | |
1141 | build_component_ref (datum, component) | |
1142 | tree datum, component; | |
1143 | { | |
1144 | register tree type = TREE_TYPE (datum); | |
1145 | register enum tree_code code = TREE_CODE (type); | |
1146 | register tree field = NULL; | |
1147 | register tree ref; | |
1148 | ||
1149 | /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it | |
1150 | unless we are not to support things not strictly ANSI. */ | |
1151 | switch (TREE_CODE (datum)) | |
1152 | { | |
1153 | case COMPOUND_EXPR: | |
1154 | { | |
1155 | tree value = build_component_ref (TREE_OPERAND (datum, 1), component); | |
400fbf9f JW |
1156 | return build (COMPOUND_EXPR, TREE_TYPE (value), |
1157 | TREE_OPERAND (datum, 0), value); | |
1158 | } | |
1159 | case COND_EXPR: | |
400fbf9f JW |
1160 | return build_conditional_expr |
1161 | (TREE_OPERAND (datum, 0), | |
1162 | build_component_ref (TREE_OPERAND (datum, 1), component), | |
1163 | build_component_ref (TREE_OPERAND (datum, 2), component)); | |
1164 | } | |
1165 | ||
1166 | /* See if there is a field or component with name COMPONENT. */ | |
1167 | ||
1168 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
1169 | { | |
19d76e60 RK |
1170 | tree indirect = 0; |
1171 | ||
400fbf9f JW |
1172 | if (TYPE_SIZE (type) == 0) |
1173 | { | |
8d9bfdc5 | 1174 | incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
1175 | return error_mark_node; |
1176 | } | |
1177 | ||
19d76e60 | 1178 | field = lookup_field (type, component, &indirect); |
400fbf9f JW |
1179 | |
1180 | if (!field) | |
1181 | { | |
1182 | error (code == RECORD_TYPE | |
1183 | ? "structure has no member named `%s'" | |
1184 | : "union has no member named `%s'", | |
1185 | IDENTIFIER_POINTER (component)); | |
1186 | return error_mark_node; | |
1187 | } | |
1188 | if (TREE_TYPE (field) == error_mark_node) | |
1189 | return error_mark_node; | |
1190 | ||
19d76e60 RK |
1191 | /* If FIELD was found buried within an anonymous union, |
1192 | make one COMPONENT_REF to get that anonymous union, | |
1193 | then fall thru to make a second COMPONENT_REF to get FIELD. */ | |
1194 | if (indirect != 0) | |
1195 | { | |
1196 | ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect); | |
1197 | if (TREE_READONLY (datum) || TREE_READONLY (indirect)) | |
1198 | TREE_READONLY (ref) = 1; | |
1199 | if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect)) | |
1200 | TREE_THIS_VOLATILE (ref) = 1; | |
1201 | datum = ref; | |
1202 | } | |
1203 | ||
400fbf9f JW |
1204 | ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field); |
1205 | ||
1206 | if (TREE_READONLY (datum) || TREE_READONLY (field)) | |
1207 | TREE_READONLY (ref) = 1; | |
1208 | if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field)) | |
1209 | TREE_THIS_VOLATILE (ref) = 1; | |
1210 | ||
1211 | return ref; | |
1212 | } | |
1213 | else if (code != ERROR_MARK) | |
1214 | error ("request for member `%s' in something not a structure or union", | |
1215 | IDENTIFIER_POINTER (component)); | |
1216 | ||
1217 | return error_mark_node; | |
1218 | } | |
1219 | \f | |
1220 | /* Given an expression PTR for a pointer, return an expression | |
1221 | for the value pointed to. | |
1222 | ERRORSTRING is the name of the operator to appear in error messages. */ | |
1223 | ||
1224 | tree | |
1225 | build_indirect_ref (ptr, errorstring) | |
1226 | tree ptr; | |
1227 | char *errorstring; | |
1228 | { | |
1229 | register tree pointer = default_conversion (ptr); | |
1230 | register tree type = TREE_TYPE (pointer); | |
1231 | ||
1232 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b RS |
1233 | { |
1234 | if (TREE_CODE (pointer) == ADDR_EXPR | |
1235 | && !flag_volatile | |
1236 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) | |
1237 | == TREE_TYPE (type))) | |
1238 | return TREE_OPERAND (pointer, 0); | |
1239 | else | |
1240 | { | |
1241 | tree t = TREE_TYPE (type); | |
1242 | register tree ref = build1 (INDIRECT_REF, | |
1243 | TYPE_MAIN_VARIANT (t), pointer); | |
400fbf9f | 1244 | |
870cc33b RS |
1245 | if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE) |
1246 | { | |
1247 | error ("dereferencing pointer to incomplete type"); | |
1248 | return error_mark_node; | |
1249 | } | |
1250 | if (TREE_CODE (t) == VOID_TYPE) | |
1251 | warning ("dereferencing `void *' pointer"); | |
1252 | ||
1253 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
1254 | so that we get the proper error message if the result is used | |
1255 | to assign to. Also, &* is supposed to be a no-op. | |
1256 | And ANSI C seems to specify that the type of the result | |
1257 | should be the const type. */ | |
1258 | /* A de-reference of a pointer to const is not a const. It is valid | |
1259 | to change it via some other pointer. */ | |
1260 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
1261 | TREE_SIDE_EFFECTS (ref) | |
1262 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile; | |
493692cd | 1263 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
870cc33b RS |
1264 | return ref; |
1265 | } | |
1266 | } | |
400fbf9f JW |
1267 | else if (TREE_CODE (pointer) != ERROR_MARK) |
1268 | error ("invalid type argument of `%s'", errorstring); | |
1269 | return error_mark_node; | |
1270 | } | |
1271 | ||
1272 | /* This handles expressions of the form "a[i]", which denotes | |
1273 | an array reference. | |
1274 | ||
1275 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
1276 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
1277 | This avoids forcing the array out of registers, and can work on | |
1278 | arrays that are not lvalues (for example, members of structures returned | |
1279 | by functions). */ | |
1280 | ||
1281 | tree | |
1282 | build_array_ref (array, index) | |
1283 | tree array, index; | |
1284 | { | |
1285 | if (index == 0) | |
1286 | { | |
1287 | error ("subscript missing in array reference"); | |
1288 | return error_mark_node; | |
1289 | } | |
1290 | ||
1291 | if (TREE_TYPE (array) == error_mark_node | |
1292 | || TREE_TYPE (index) == error_mark_node) | |
1293 | return error_mark_node; | |
1294 | ||
1295 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE | |
1296 | && TREE_CODE (array) != INDIRECT_REF) | |
1297 | { | |
1298 | tree rval, type; | |
1299 | ||
400fbf9f JW |
1300 | /* Subscripting with type char is likely to lose |
1301 | on a machine where chars are signed. | |
1302 | So warn on any machine, but optionally. | |
1303 | Don't warn for unsigned char since that type is safe. | |
1304 | Don't warn for signed char because anyone who uses that | |
1305 | must have done so deliberately. */ | |
1306 | if (warn_char_subscripts | |
1307 | && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) | |
1308 | warning ("array subscript has type `char'"); | |
1309 | ||
0e51ef9b RS |
1310 | /* Apply default promotions *after* noticing character types. */ |
1311 | index = default_conversion (index); | |
1312 | ||
fdeefd49 RS |
1313 | /* Require integer *after* promotion, for sake of enums. */ |
1314 | if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE) | |
1315 | { | |
1316 | error ("array subscript is not an integer"); | |
1317 | return error_mark_node; | |
1318 | } | |
1319 | ||
400fbf9f JW |
1320 | /* An array that is indexed by a non-constant |
1321 | cannot be stored in a register; we must be able to do | |
1322 | address arithmetic on its address. | |
1323 | Likewise an array of elements of variable size. */ | |
1324 | if (TREE_CODE (index) != INTEGER_CST | |
1325 | || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0 | |
1326 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) | |
1327 | { | |
1328 | if (mark_addressable (array) == 0) | |
1329 | return error_mark_node; | |
1330 | } | |
e6d52559 JW |
1331 | /* An array that is indexed by a constant value which is not within |
1332 | the array bounds cannot be stored in a register either; because we | |
1333 | would get a crash in store_bit_field/extract_bit_field when trying | |
1334 | to access a non-existent part of the register. */ | |
1335 | if (TREE_CODE (index) == INTEGER_CST | |
1336 | && TYPE_VALUES (TREE_TYPE (array)) | |
1337 | && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array)))) | |
1338 | { | |
1339 | if (mark_addressable (array) == 0) | |
1340 | return error_mark_node; | |
1341 | } | |
400fbf9f JW |
1342 | |
1343 | if (pedantic && !lvalue_p (array)) | |
1344 | { | |
1394aabd | 1345 | if (DECL_REGISTER (array)) |
400fbf9f JW |
1346 | pedwarn ("ANSI C forbids subscripting `register' array"); |
1347 | else | |
1348 | pedwarn ("ANSI C forbids subscripting non-lvalue array"); | |
1349 | } | |
1350 | ||
1351 | if (pedantic) | |
1352 | { | |
1353 | tree foo = array; | |
1354 | while (TREE_CODE (foo) == COMPONENT_REF) | |
1355 | foo = TREE_OPERAND (foo, 0); | |
1394aabd | 1356 | if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) |
400fbf9f JW |
1357 | pedwarn ("ANSI C forbids subscripting non-lvalue array"); |
1358 | } | |
1359 | ||
1360 | type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array))); | |
1361 | rval = build (ARRAY_REF, type, array, index); | |
1362 | /* Array ref is const/volatile if the array elements are | |
1363 | or if the array is. */ | |
1364 | TREE_READONLY (rval) | |
1365 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
1366 | | TREE_READONLY (array)); | |
1367 | TREE_SIDE_EFFECTS (rval) | |
1368 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1369 | | TREE_SIDE_EFFECTS (array)); | |
1370 | TREE_THIS_VOLATILE (rval) | |
1371 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
1372 | /* This was added by rms on 16 Nov 91. | |
1373 | It fixes vol struct foo *a; a->elts[1] | |
1374 | in an inline function. | |
1375 | Hope it doesn't break something else. */ | |
1376 | | TREE_THIS_VOLATILE (array)); | |
1377 | return require_complete_type (fold (rval)); | |
1378 | } | |
1379 | ||
1380 | { | |
1381 | tree ar = default_conversion (array); | |
1382 | tree ind = default_conversion (index); | |
1383 | ||
1384 | /* Put the integer in IND to simplify error checking. */ | |
1385 | if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) | |
1386 | { | |
1387 | tree temp = ar; | |
1388 | ar = ind; | |
1389 | ind = temp; | |
1390 | } | |
1391 | ||
1392 | if (ar == error_mark_node) | |
1393 | return ar; | |
1394 | ||
1395 | if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE) | |
1396 | { | |
1397 | error ("subscripted value is neither array nor pointer"); | |
1398 | return error_mark_node; | |
1399 | } | |
1400 | if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) | |
1401 | { | |
1402 | error ("array subscript is not an integer"); | |
1403 | return error_mark_node; | |
1404 | } | |
1405 | ||
1406 | return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0), | |
1407 | "array indexing"); | |
1408 | } | |
1409 | } | |
1410 | \f | |
400fbf9f JW |
1411 | /* Build a function call to function FUNCTION with parameters PARAMS. |
1412 | PARAMS is a list--a chain of TREE_LIST nodes--in which the | |
1413 | TREE_VALUE of each node is a parameter-expression. | |
1414 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
1415 | ||
1416 | tree | |
1417 | build_function_call (function, params) | |
1418 | tree function, params; | |
1419 | { | |
346d29dc | 1420 | register tree fntype, fundecl = 0; |
400fbf9f | 1421 | register tree coerced_params; |
19d76e60 | 1422 | tree name = NULL_TREE, assembler_name = NULL_TREE; |
400fbf9f | 1423 | |
fc76e425 | 1424 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 1425 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
1426 | |
1427 | /* Convert anything with function type to a pointer-to-function. */ | |
1428 | if (TREE_CODE (function) == FUNCTION_DECL) | |
1429 | { | |
1430 | name = DECL_NAME (function); | |
19d76e60 RK |
1431 | assembler_name = DECL_ASSEMBLER_NAME (function); |
1432 | ||
400fbf9f JW |
1433 | /* Differs from default_conversion by not setting TREE_ADDRESSABLE |
1434 | (because calling an inline function does not mean the function | |
1435 | needs to be separately compiled). */ | |
1436 | fntype = build_type_variant (TREE_TYPE (function), | |
1437 | TREE_READONLY (function), | |
1438 | TREE_THIS_VOLATILE (function)); | |
9b7267b8 | 1439 | fundecl = function; |
400fbf9f JW |
1440 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); |
1441 | } | |
1442 | else | |
1443 | function = default_conversion (function); | |
1444 | ||
1445 | fntype = TREE_TYPE (function); | |
1446 | ||
1447 | if (TREE_CODE (fntype) == ERROR_MARK) | |
1448 | return error_mark_node; | |
1449 | ||
1450 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
1451 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
1452 | { | |
1453 | error ("called object is not a function"); | |
1454 | return error_mark_node; | |
1455 | } | |
1456 | ||
1457 | /* fntype now gets the type of function pointed to. */ | |
1458 | fntype = TREE_TYPE (fntype); | |
1459 | ||
1460 | /* Convert the parameters to the types declared in the | |
1461 | function prototype, or apply default promotions. */ | |
1462 | ||
1463 | coerced_params | |
9b7267b8 | 1464 | = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl); |
400fbf9f JW |
1465 | |
1466 | /* Check for errors in format strings. */ | |
400fbf9f | 1467 | |
19d76e60 RK |
1468 | if (warn_format && (name || assembler_name)) |
1469 | check_function_format (name, assembler_name, coerced_params); | |
400fbf9f JW |
1470 | |
1471 | /* Recognize certain built-in functions so we can make tree-codes | |
1472 | other than CALL_EXPR. We do this when it enables fold-const.c | |
1473 | to do something useful. */ | |
1474 | ||
1475 | if (TREE_CODE (function) == ADDR_EXPR | |
1476 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL | |
1477 | && DECL_BUILT_IN (TREE_OPERAND (function, 0))) | |
1478 | switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0))) | |
1479 | { | |
1480 | case BUILT_IN_ABS: | |
1481 | case BUILT_IN_LABS: | |
1482 | case BUILT_IN_FABS: | |
1483 | if (coerced_params == 0) | |
1484 | return integer_zero_node; | |
1485 | return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); | |
1486 | } | |
1487 | ||
1488 | { | |
1489 | register tree result | |
1490 | = build (CALL_EXPR, TREE_TYPE (fntype), | |
1491 | function, coerced_params, NULL_TREE); | |
1492 | ||
1493 | TREE_SIDE_EFFECTS (result) = 1; | |
1494 | if (TREE_TYPE (result) == void_type_node) | |
1495 | return result; | |
1496 | return require_complete_type (result); | |
1497 | } | |
1498 | } | |
1499 | \f | |
1500 | /* Convert the argument expressions in the list VALUES | |
1501 | to the types in the list TYPELIST. The result is a list of converted | |
1502 | argument expressions. | |
1503 | ||
1504 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
1505 | perform the default conversions. | |
1506 | ||
1507 | PARMLIST is the chain of parm decls for the function being called. | |
1508 | It may be 0, if that info is not available. | |
1509 | It is used only for generating error messages. | |
1510 | ||
1511 | NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. | |
1512 | ||
1513 | This is also where warnings about wrong number of args are generated. | |
1514 | ||
1515 | Both VALUES and the returned value are chains of TREE_LIST nodes | |
1516 | with the elements of the list in the TREE_VALUE slots of those nodes. */ | |
1517 | ||
1518 | static tree | |
9b7267b8 RS |
1519 | convert_arguments (typelist, values, name, fundecl) |
1520 | tree typelist, values, name, fundecl; | |
400fbf9f JW |
1521 | { |
1522 | register tree typetail, valtail; | |
1523 | register tree result = NULL; | |
1524 | int parmnum; | |
1525 | ||
1526 | /* Scan the given expressions and types, producing individual | |
1527 | converted arguments and pushing them on RESULT in reverse order. */ | |
1528 | ||
1529 | for (valtail = values, typetail = typelist, parmnum = 0; | |
1530 | valtail; | |
1531 | valtail = TREE_CHAIN (valtail), parmnum++) | |
1532 | { | |
1533 | register tree type = typetail ? TREE_VALUE (typetail) : 0; | |
1534 | register tree val = TREE_VALUE (valtail); | |
1535 | ||
1536 | if (type == void_type_node) | |
1537 | { | |
1538 | if (name) | |
1539 | error ("too many arguments to function `%s'", | |
1540 | IDENTIFIER_POINTER (name)); | |
1541 | else | |
1542 | error ("too many arguments to function"); | |
1543 | break; | |
1544 | } | |
1545 | ||
1546 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
1547 | /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0 |
1548 | to convert automatically to a pointer. */ | |
400fbf9f JW |
1549 | if (TREE_CODE (val) == NON_LVALUE_EXPR) |
1550 | val = TREE_OPERAND (val, 0); | |
1551 | ||
1552 | if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE | |
1553 | || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE) | |
1554 | val = default_conversion (val); | |
1555 | ||
1556 | val = require_complete_type (val); | |
1557 | ||
1558 | if (type != 0) | |
1559 | { | |
1560 | /* Formal parm type is specified by a function prototype. */ | |
1561 | tree parmval; | |
1562 | ||
1563 | if (TYPE_SIZE (type) == 0) | |
1564 | { | |
1565 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
1566 | parmval = val; | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | tree parmname; | |
36618528 | 1571 | tree type0 = type; |
400fbf9f JW |
1572 | #ifdef PROMOTE_PROTOTYPES |
1573 | /* Rather than truncating and then reextending, | |
1574 | convert directly to int, if that's the type we will want. */ | |
1575 | if (! flag_traditional | |
7fb90b98 RK |
1576 | && (TREE_CODE (type) == INTEGER_TYPE |
1577 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
400fbf9f JW |
1578 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
1579 | type = integer_type_node; | |
1580 | #endif | |
1581 | ||
1582 | #if 0 /* This turns out not to win--there's no way to write a prototype | |
1583 | for a function whose arg type is a union with no tag. */ | |
1584 | /* Nameless union automatically casts the types it contains. */ | |
1585 | if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0) | |
1586 | { | |
1587 | tree field; | |
1588 | ||
1589 | for (field = TYPE_FIELDS (type); field; | |
1590 | field = TREE_CHAIN (field)) | |
1591 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
1592 | TYPE_MAIN_VARIANT (TREE_TYPE (val)))) | |
1593 | break; | |
1594 | ||
1595 | if (field) | |
1596 | val = build1 (CONVERT_EXPR, type, val); | |
1597 | } | |
1598 | #endif | |
1599 | ||
d45cf215 RS |
1600 | /* Optionally warn about conversions that |
1601 | differ from the default conversions. */ | |
400fbf9f JW |
1602 | if (warn_conversion) |
1603 | { | |
1604 | int formal_prec = TYPE_PRECISION (type); | |
400fbf9f JW |
1605 | |
1606 | if (TREE_CODE (type) != REAL_TYPE | |
1607 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
754a4d82 | 1608 | warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1); |
400fbf9f JW |
1609 | else if (TREE_CODE (type) == REAL_TYPE |
1610 | && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE) | |
754a4d82 | 1611 | warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1); |
d45cf215 RS |
1612 | else if (TREE_CODE (type) == REAL_TYPE |
1613 | && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) | |
1614 | { | |
1615 | /* Warn if any argument is passed as `float', | |
047de90b | 1616 | since without a prototype it would be `double'. */ |
d45cf215 | 1617 | if (formal_prec == TYPE_PRECISION (float_type_node)) |
754a4d82 | 1618 | warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1); |
d45cf215 | 1619 | } |
400fbf9f JW |
1620 | /* Detect integer changing in width or signedness. */ |
1621 | else if ((TREE_CODE (type) == INTEGER_TYPE | |
1622 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
1623 | && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE | |
d45cf215 | 1624 | || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE)) |
400fbf9f | 1625 | { |
d45cf215 RS |
1626 | tree would_have_been = default_conversion (val); |
1627 | tree type1 = TREE_TYPE (would_have_been); | |
1628 | ||
754a4d82 RS |
1629 | if (TREE_CODE (type) == ENUMERAL_TYPE |
1630 | && type == TREE_TYPE (val)) | |
1631 | /* No warning if function asks for enum | |
1632 | and the actual arg is that enum type. */ | |
1633 | ; | |
1634 | else if (formal_prec != TYPE_PRECISION (type1)) | |
1635 | warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1); | |
d45cf215 RS |
1636 | else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1)) |
1637 | ; | |
800cd3b9 RS |
1638 | /* Don't complain if the formal parameter type |
1639 | is an enum, because we can't tell now whether | |
1640 | the value was an enum--even the same enum. */ | |
1641 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1642 | ; | |
400fbf9f JW |
1643 | else if (TREE_CODE (val) == INTEGER_CST |
1644 | && int_fits_type_p (val, type)) | |
1645 | /* Change in signedness doesn't matter | |
1646 | if a constant value is unaffected. */ | |
1647 | ; | |
4bbbc5d9 RS |
1648 | /* Likewise for a constant in a NOP_EXPR. */ |
1649 | else if (TREE_CODE (val) == NOP_EXPR | |
1650 | && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST | |
1651 | && int_fits_type_p (TREE_OPERAND (val, 0), type)) | |
1652 | ; | |
1653 | #if 0 /* We never get such tree structure here. */ | |
047de90b RS |
1654 | else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE |
1655 | && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type) | |
1656 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type)) | |
1657 | /* Change in signedness doesn't matter | |
1658 | if an enum value is unaffected. */ | |
1659 | ; | |
4bbbc5d9 | 1660 | #endif |
ce9895ae RS |
1661 | /* If the value is extended from a narrower |
1662 | unsigned type, it doesn't matter whether we | |
1663 | pass it as signed or unsigned; the value | |
1664 | certainly is the same either way. */ | |
1665 | else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type) | |
1666 | && TREE_UNSIGNED (TREE_TYPE (val))) | |
1667 | ; | |
400fbf9f | 1668 | else if (TREE_UNSIGNED (type)) |
754a4d82 | 1669 | warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1); |
400fbf9f | 1670 | else |
754a4d82 | 1671 | warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1); |
400fbf9f JW |
1672 | } |
1673 | } | |
1674 | ||
1675 | parmval = convert_for_assignment (type, val, | |
1676 | (char *)0, /* arg passing */ | |
9b7267b8 | 1677 | fundecl, name, parmnum + 1); |
400fbf9f JW |
1678 | |
1679 | #ifdef PROMOTE_PROTOTYPES | |
7fb90b98 RK |
1680 | if ((TREE_CODE (type) == INTEGER_TYPE |
1681 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
400fbf9f JW |
1682 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
1683 | parmval = default_conversion (parmval); | |
1684 | #endif | |
1685 | } | |
8d9bfdc5 | 1686 | result = tree_cons (NULL_TREE, parmval, result); |
400fbf9f JW |
1687 | } |
1688 | else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE | |
1689 | && (TYPE_PRECISION (TREE_TYPE (val)) | |
1690 | < TYPE_PRECISION (double_type_node))) | |
1691 | /* Convert `float' to `double'. */ | |
1692 | result = tree_cons (NULL_TREE, convert (double_type_node, val), result); | |
1693 | else | |
1694 | /* Convert `short' and `char' to full-size `int'. */ | |
1695 | result = tree_cons (NULL_TREE, default_conversion (val), result); | |
1696 | ||
1697 | if (typetail) | |
1698 | typetail = TREE_CHAIN (typetail); | |
1699 | } | |
1700 | ||
1701 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) | |
1702 | { | |
1703 | if (name) | |
1704 | error ("too few arguments to function `%s'", | |
1705 | IDENTIFIER_POINTER (name)); | |
1706 | else | |
1707 | error ("too few arguments to function"); | |
1708 | } | |
1709 | ||
1710 | return nreverse (result); | |
1711 | } | |
1712 | \f | |
1713 | /* This is the entry point used by the parser | |
1714 | for binary operators in the input. | |
1715 | In addition to constructing the expression, | |
1716 | we check for operands that were written with other binary operators | |
1717 | in a way that is likely to confuse the user. */ | |
edc7c4ec | 1718 | |
400fbf9f JW |
1719 | tree |
1720 | parser_build_binary_op (code, arg1, arg2) | |
1721 | enum tree_code code; | |
1722 | tree arg1, arg2; | |
1723 | { | |
1724 | tree result = build_binary_op (code, arg1, arg2, 1); | |
1725 | ||
1726 | char class; | |
1727 | char class1 = TREE_CODE_CLASS (TREE_CODE (arg1)); | |
1728 | char class2 = TREE_CODE_CLASS (TREE_CODE (arg2)); | |
1729 | enum tree_code code1 = ERROR_MARK; | |
1730 | enum tree_code code2 = ERROR_MARK; | |
1731 | ||
1732 | if (class1 == 'e' || class1 == '1' | |
1733 | || class1 == '2' || class1 == '<') | |
1734 | code1 = C_EXP_ORIGINAL_CODE (arg1); | |
1735 | if (class2 == 'e' || class2 == '1' | |
1736 | || class2 == '2' || class2 == '<') | |
1737 | code2 = C_EXP_ORIGINAL_CODE (arg2); | |
1738 | ||
1739 | /* Check for cases such as x+y<<z which users are likely | |
1740 | to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE | |
1741 | is cleared to prevent these warnings. */ | |
1742 | if (warn_parentheses) | |
1743 | { | |
1744 | if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
1745 | { | |
1746 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1747 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1748 | warning ("suggest parentheses around + or - inside shift"); | |
1749 | } | |
1750 | ||
1751 | if (code == TRUTH_ORIF_EXPR) | |
1752 | { | |
1753 | if (code1 == TRUTH_ANDIF_EXPR | |
1754 | || code2 == TRUTH_ANDIF_EXPR) | |
1755 | warning ("suggest parentheses around && within ||"); | |
1756 | } | |
1757 | ||
1758 | if (code == BIT_IOR_EXPR) | |
1759 | { | |
1760 | if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR | |
1761 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1762 | || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR | |
1763 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1764 | warning ("suggest parentheses around arithmetic in operand of |"); | |
1765 | } | |
1766 | ||
1767 | if (code == BIT_XOR_EXPR) | |
1768 | { | |
1769 | if (code1 == BIT_AND_EXPR | |
1770 | || code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1771 | || code2 == BIT_AND_EXPR | |
1772 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1773 | warning ("suggest parentheses around arithmetic in operand of ^"); | |
1774 | } | |
1775 | ||
1776 | if (code == BIT_AND_EXPR) | |
1777 | { | |
1778 | if (code1 == PLUS_EXPR || code1 == MINUS_EXPR | |
1779 | || code2 == PLUS_EXPR || code2 == MINUS_EXPR) | |
1780 | warning ("suggest parentheses around + or - in operand of &"); | |
1781 | } | |
1782 | } | |
1783 | ||
001af587 | 1784 | /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ |
edc7c4ec | 1785 | if (TREE_CODE_CLASS (code) == '<' && extra_warnings |
001af587 RS |
1786 | && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')) |
1787 | warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); | |
1788 | ||
e58cd767 RS |
1789 | unsigned_conversion_warning (result, arg1); |
1790 | unsigned_conversion_warning (result, arg2); | |
1791 | overflow_warning (result); | |
1792 | ||
edc7c4ec RS |
1793 | class = TREE_CODE_CLASS (TREE_CODE (result)); |
1794 | ||
400fbf9f JW |
1795 | /* Record the code that was specified in the source, |
1796 | for the sake of warnings about confusing nesting. */ | |
1797 | if (class == 'e' || class == '1' | |
1798 | || class == '2' || class == '<') | |
1799 | C_SET_EXP_ORIGINAL_CODE (result, code); | |
1800 | else | |
1801 | { | |
1802 | int flag = TREE_CONSTANT (result); | |
d11fdb45 RS |
1803 | /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR |
1804 | so that convert_for_assignment wouldn't strip it. | |
1805 | That way, we got warnings for things like p = (1 - 1). | |
1806 | But it turns out we should not get those warnings. */ | |
1807 | result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result); | |
400fbf9f JW |
1808 | C_SET_EXP_ORIGINAL_CODE (result, code); |
1809 | TREE_CONSTANT (result) = flag; | |
1810 | } | |
1811 | ||
1812 | return result; | |
1813 | } | |
1814 | ||
1815 | /* Build a binary-operation expression without default conversions. | |
1816 | CODE is the kind of expression to build. | |
1817 | This function differs from `build' in several ways: | |
1818 | the data type of the result is computed and recorded in it, | |
1819 | warnings are generated if arg data types are invalid, | |
1820 | special handling for addition and subtraction of pointers is known, | |
1821 | and some optimization is done (operations on narrow ints | |
1822 | are done in the narrower type when that gives the same result). | |
1823 | Constant folding is also done before the result is returned. | |
1824 | ||
1825 | Note that the operands will never have enumeral types, or function | |
1826 | or array types, because either they will have the default conversions | |
1827 | performed or they have both just been converted to some other type in which | |
1828 | the arithmetic is to be done. */ | |
1829 | ||
1830 | tree | |
1831 | build_binary_op (code, orig_op0, orig_op1, convert_p) | |
1832 | enum tree_code code; | |
1833 | tree orig_op0, orig_op1; | |
1834 | int convert_p; | |
1835 | { | |
1836 | tree type0, type1; | |
1837 | register enum tree_code code0, code1; | |
1838 | tree op0, op1; | |
1839 | ||
1840 | /* Expression code to give to the expression when it is built. | |
1841 | Normally this is CODE, which is what the caller asked for, | |
1842 | but in some special cases we change it. */ | |
1843 | register enum tree_code resultcode = code; | |
1844 | ||
1845 | /* Data type in which the computation is to be performed. | |
1846 | In the simplest cases this is the common type of the arguments. */ | |
1847 | register tree result_type = NULL; | |
1848 | ||
1849 | /* Nonzero means operands have already been type-converted | |
1850 | in whatever way is necessary. | |
1851 | Zero means they need to be converted to RESULT_TYPE. */ | |
1852 | int converted = 0; | |
1853 | ||
1854 | /* Nonzero means after finally constructing the expression | |
1855 | give it this type. Otherwise, give it type RESULT_TYPE. */ | |
1856 | tree final_type = 0; | |
1857 | ||
1858 | /* Nonzero if this is an operation like MIN or MAX which can | |
1859 | safely be computed in short if both args are promoted shorts. | |
1860 | Also implies COMMON. | |
1861 | -1 indicates a bitwise operation; this makes a difference | |
1862 | in the exact conditions for when it is safe to do the operation | |
1863 | in a narrower mode. */ | |
1864 | int shorten = 0; | |
1865 | ||
1866 | /* Nonzero if this is a comparison operation; | |
1867 | if both args are promoted shorts, compare the original shorts. | |
1868 | Also implies COMMON. */ | |
1869 | int short_compare = 0; | |
1870 | ||
1871 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
1872 | original short and then promoted if the operand is a promoted short. */ | |
1873 | int short_shift = 0; | |
1874 | ||
1875 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
1876 | int common = 0; | |
1877 | ||
1878 | if (convert_p) | |
1879 | { | |
1880 | op0 = default_conversion (orig_op0); | |
1881 | op1 = default_conversion (orig_op1); | |
1882 | } | |
1883 | else | |
1884 | { | |
1885 | op0 = orig_op0; | |
1886 | op1 = orig_op1; | |
1887 | } | |
1888 | ||
1889 | type0 = TREE_TYPE (op0); | |
1890 | type1 = TREE_TYPE (op1); | |
1891 | ||
1892 | /* The expression codes of the data types of the arguments tell us | |
1893 | whether the arguments are integers, floating, pointers, etc. */ | |
1894 | code0 = TREE_CODE (type0); | |
1895 | code1 = TREE_CODE (type1); | |
1896 | ||
fc76e425 | 1897 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce RS |
1898 | STRIP_TYPE_NOPS (op0); |
1899 | STRIP_TYPE_NOPS (op1); | |
400fbf9f JW |
1900 | |
1901 | /* If an error was already reported for one of the arguments, | |
1902 | avoid reporting another error. */ | |
1903 | ||
1904 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
1905 | return error_mark_node; | |
1906 | ||
1907 | switch (code) | |
1908 | { | |
1909 | case PLUS_EXPR: | |
1910 | /* Handle the pointer + int case. */ | |
1911 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
1912 | return pointer_int_sum (PLUS_EXPR, op0, op1); | |
1913 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) | |
1914 | return pointer_int_sum (PLUS_EXPR, op1, op0); | |
1915 | else | |
1916 | common = 1; | |
1917 | break; | |
1918 | ||
1919 | case MINUS_EXPR: | |
1920 | /* Subtraction of two similar pointers. | |
1921 | We must subtract them as integers, then divide by object size. */ | |
1922 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
1923 | && comp_target_types (type0, type1)) | |
1924 | return pointer_diff (op0, op1); | |
1925 | /* Handle pointer minus int. Just like pointer plus int. */ | |
1926 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
1927 | return pointer_int_sum (MINUS_EXPR, op0, op1); | |
1928 | else | |
1929 | common = 1; | |
1930 | break; | |
1931 | ||
1932 | case MULT_EXPR: | |
1933 | common = 1; | |
1934 | break; | |
1935 | ||
1936 | case TRUNC_DIV_EXPR: | |
1937 | case CEIL_DIV_EXPR: | |
1938 | case FLOOR_DIV_EXPR: | |
1939 | case ROUND_DIV_EXPR: | |
1940 | case EXACT_DIV_EXPR: | |
b6a10c9f RS |
1941 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
1942 | || code0 == COMPLEX_TYPE) | |
1943 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
1944 | || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
1945 | { |
1946 | if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) | |
1947 | resultcode = RDIV_EXPR; | |
1948 | else | |
8b39ed65 TG |
1949 | { |
1950 | /* Although it would be tempting to shorten always here, that | |
1951 | loses on some targets, since the modulo instruction is | |
1952 | undefined if the quotient can't be represented in the | |
1953 | computation mode. We shorten only if unsigned or if | |
1954 | dividing by something we know != -1. */ | |
1955 | shorten = (TREE_UNSIGNED (orig_op0) | |
1956 | || (TREE_CODE (op1) == INTEGER_CST | |
1957 | && (TREE_INT_CST_LOW (op1) != -1 | |
1958 | || TREE_INT_CST_HIGH (op1) != -1))); | |
1959 | } | |
400fbf9f JW |
1960 | common = 1; |
1961 | } | |
1962 | break; | |
1963 | ||
1964 | case BIT_AND_EXPR: | |
1965 | case BIT_ANDTC_EXPR: | |
1966 | case BIT_IOR_EXPR: | |
1967 | case BIT_XOR_EXPR: | |
1968 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
1969 | shorten = -1; | |
1970 | /* If one operand is a constant, and the other is a short type | |
1971 | that has been converted to an int, | |
1972 | really do the work in the short type and then convert the | |
1973 | result to int. If we are lucky, the constant will be 0 or 1 | |
1974 | in the short type, making the entire operation go away. */ | |
1975 | if (TREE_CODE (op0) == INTEGER_CST | |
1976 | && TREE_CODE (op1) == NOP_EXPR | |
1977 | && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))) | |
1978 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0)))) | |
1979 | { | |
1980 | final_type = result_type; | |
1981 | op1 = TREE_OPERAND (op1, 0); | |
1982 | result_type = TREE_TYPE (op1); | |
1983 | } | |
1984 | if (TREE_CODE (op1) == INTEGER_CST | |
1985 | && TREE_CODE (op0) == NOP_EXPR | |
1986 | && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))) | |
1987 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
1988 | { | |
1989 | final_type = result_type; | |
1990 | op0 = TREE_OPERAND (op0, 0); | |
1991 | result_type = TREE_TYPE (op0); | |
1992 | } | |
1993 | break; | |
1994 | ||
1995 | case TRUNC_MOD_EXPR: | |
047de90b | 1996 | case FLOOR_MOD_EXPR: |
400fbf9f | 1997 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) |
03d5b1f5 RS |
1998 | { |
1999 | /* Although it would be tempting to shorten always here, that loses | |
2000 | on some targets, since the modulo instruction is undefined if the | |
2001 | quotient can't be represented in the computation mode. We shorten | |
2002 | only if unsigned or if dividing by something we know != -1. */ | |
2003 | shorten = (TREE_UNSIGNED (orig_op0) | |
2004 | || (TREE_CODE (op1) == INTEGER_CST | |
2005 | && (TREE_INT_CST_LOW (op1) != -1 | |
2006 | || TREE_INT_CST_HIGH (op1) != -1))); | |
2007 | common = 1; | |
2008 | } | |
400fbf9f JW |
2009 | break; |
2010 | ||
2011 | case TRUTH_ANDIF_EXPR: | |
2012 | case TRUTH_ORIF_EXPR: | |
2013 | case TRUTH_AND_EXPR: | |
2014 | case TRUTH_OR_EXPR: | |
1eca8b1e | 2015 | case TRUTH_XOR_EXPR: |
b6a10c9f RS |
2016 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE |
2017 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) | |
2018 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE | |
2019 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
2020 | { |
2021 | /* Result of these operations is always an int, | |
2022 | but that does not mean the operands should be | |
2023 | converted to ints! */ | |
2024 | result_type = integer_type_node; | |
2025 | op0 = truthvalue_conversion (op0); | |
2026 | op1 = truthvalue_conversion (op1); | |
2027 | converted = 1; | |
2028 | } | |
2029 | break; | |
2030 | ||
2031 | /* Shift operations: result has same type as first operand; | |
2032 | always convert second operand to int. | |
2033 | Also set SHORT_SHIFT if shifting rightward. */ | |
2034 | ||
2035 | case RSHIFT_EXPR: | |
2036 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2037 | { | |
2038 | if (TREE_CODE (op1) == INTEGER_CST) | |
2039 | { | |
17651386 | 2040 | if (tree_int_cst_lt (op1, integer_zero_node)) |
315da535 | 2041 | warning ("right shift count is negative"); |
17651386 RS |
2042 | else |
2043 | { | |
2044 | if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1)) | |
2045 | short_shift = 1; | |
2046 | if (TREE_INT_CST_HIGH (op1) != 0 | |
2047 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2048 | >= TYPE_PRECISION (type0))) | |
315da535 | 2049 | warning ("right shift count >= width of type"); |
17651386 | 2050 | } |
400fbf9f | 2051 | } |
d45cf215 RS |
2052 | /* Use the type of the value to be shifted. |
2053 | This is what most traditional C compilers do. */ | |
2054 | result_type = type0; | |
400fbf9f JW |
2055 | /* Unless traditional, convert the shift-count to an integer, |
2056 | regardless of size of value being shifted. */ | |
2057 | if (! flag_traditional) | |
2058 | { | |
6cb72a7d | 2059 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2060 | op1 = convert (integer_type_node, op1); |
2061 | /* Avoid converting op1 to result_type later. */ | |
2062 | converted = 1; | |
2063 | } | |
400fbf9f JW |
2064 | } |
2065 | break; | |
2066 | ||
2067 | case LSHIFT_EXPR: | |
2068 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2069 | { | |
17651386 RS |
2070 | if (TREE_CODE (op1) == INTEGER_CST) |
2071 | { | |
2072 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
315da535 | 2073 | warning ("left shift count is negative"); |
17651386 RS |
2074 | else if (TREE_INT_CST_HIGH (op1) != 0 |
2075 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2076 | >= TYPE_PRECISION (type0))) | |
315da535 | 2077 | warning ("left shift count >= width of type"); |
17651386 | 2078 | } |
d45cf215 RS |
2079 | /* Use the type of the value to be shifted. |
2080 | This is what most traditional C compilers do. */ | |
2081 | result_type = type0; | |
400fbf9f JW |
2082 | /* Unless traditional, convert the shift-count to an integer, |
2083 | regardless of size of value being shifted. */ | |
2084 | if (! flag_traditional) | |
2085 | { | |
6cb72a7d | 2086 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2087 | op1 = convert (integer_type_node, op1); |
2088 | /* Avoid converting op1 to result_type later. */ | |
2089 | converted = 1; | |
2090 | } | |
400fbf9f JW |
2091 | } |
2092 | break; | |
2093 | ||
2094 | case RROTATE_EXPR: | |
2095 | case LROTATE_EXPR: | |
2096 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
2097 | { | |
17651386 RS |
2098 | if (TREE_CODE (op1) == INTEGER_CST) |
2099 | { | |
2100 | if (tree_int_cst_lt (op1, integer_zero_node)) | |
2101 | warning ("shift count is negative"); | |
2102 | else if (TREE_INT_CST_HIGH (op1) != 0 | |
2103 | || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) | |
2104 | >= TYPE_PRECISION (type0))) | |
2105 | warning ("shift count >= width of type"); | |
2106 | } | |
d45cf215 RS |
2107 | /* Use the type of the value to be shifted. |
2108 | This is what most traditional C compilers do. */ | |
2109 | result_type = type0; | |
400fbf9f JW |
2110 | /* Unless traditional, convert the shift-count to an integer, |
2111 | regardless of size of value being shifted. */ | |
2112 | if (! flag_traditional) | |
2113 | { | |
6cb72a7d | 2114 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) |
400fbf9f JW |
2115 | op1 = convert (integer_type_node, op1); |
2116 | /* Avoid converting op1 to result_type later. */ | |
2117 | converted = 1; | |
2118 | } | |
400fbf9f JW |
2119 | } |
2120 | break; | |
2121 | ||
2122 | case EQ_EXPR: | |
2123 | case NE_EXPR: | |
2124 | /* Result of comparison is always int, | |
2125 | but don't convert the args to int! */ | |
2126 | result_type = integer_type_node; | |
2127 | converted = 1; | |
b6a10c9f RS |
2128 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
2129 | || code0 == COMPLEX_TYPE) | |
2130 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
2131 | || code1 == COMPLEX_TYPE)) | |
400fbf9f JW |
2132 | short_compare = 1; |
2133 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2134 | { | |
2135 | register tree tt0 = TREE_TYPE (type0); | |
2136 | register tree tt1 = TREE_TYPE (type1); | |
2137 | /* Anything compares with void *. void * compares with anything. | |
d11fdb45 RS |
2138 | Otherwise, the targets must be compatible |
2139 | and both must be object or both incomplete. */ | |
400fbf9f JW |
2140 | if (comp_target_types (type0, type1)) |
2141 | ; | |
2142 | else if (TYPE_MAIN_VARIANT (tt0) == void_type_node) | |
2143 | { | |
fd5d5b94 RS |
2144 | /* op0 != orig_op0 detects the case of something |
2145 | whose value is 0 but which isn't a valid null ptr const. */ | |
2146 | if (pedantic && (!integer_zerop (op0) || op0 != orig_op0) | |
400fbf9f JW |
2147 | && TREE_CODE (tt1) == FUNCTION_TYPE) |
2148 | pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); | |
2149 | } | |
2150 | else if (TYPE_MAIN_VARIANT (tt1) == void_type_node) | |
2151 | { | |
fd5d5b94 | 2152 | if (pedantic && (!integer_zerop (op1) || op1 != orig_op1) |
400fbf9f JW |
2153 | && TREE_CODE (tt0) == FUNCTION_TYPE) |
2154 | pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); | |
2155 | } | |
2156 | else | |
2157 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
2158 | } | |
2159 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST | |
2160 | && integer_zerop (op1)) | |
2161 | op1 = null_pointer_node; | |
2162 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST | |
2163 | && integer_zerop (op0)) | |
2164 | op0 = null_pointer_node; | |
2165 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
2166 | { | |
2167 | if (! flag_traditional) | |
2168 | pedwarn ("comparison between pointer and integer"); | |
2169 | op1 = convert (TREE_TYPE (op0), op1); | |
2170 | } | |
2171 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
2172 | { | |
2173 | if (! flag_traditional) | |
2174 | pedwarn ("comparison between pointer and integer"); | |
2175 | op0 = convert (TREE_TYPE (op1), op0); | |
2176 | } | |
2177 | else | |
2178 | /* If args are not valid, clear out RESULT_TYPE | |
2179 | to cause an error message later. */ | |
2180 | result_type = 0; | |
2181 | break; | |
2182 | ||
2183 | case MAX_EXPR: | |
2184 | case MIN_EXPR: | |
9db931af RS |
2185 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
2186 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
400fbf9f JW |
2187 | shorten = 1; |
2188 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2189 | { | |
2190 | if (! comp_target_types (type0, type1)) | |
2191 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
2192 | else if (pedantic | |
2193 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
2194 | pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); | |
2195 | result_type = common_type (type0, type1); | |
2196 | } | |
2197 | break; | |
2198 | ||
2199 | case LE_EXPR: | |
2200 | case GE_EXPR: | |
2201 | case LT_EXPR: | |
2202 | case GT_EXPR: | |
9db931af RS |
2203 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) |
2204 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) | |
400fbf9f JW |
2205 | short_compare = 1; |
2206 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
2207 | { | |
2208 | if (! comp_target_types (type0, type1)) | |
2209 | pedwarn ("comparison of distinct pointer types lacks a cast"); | |
d11fdb45 RS |
2210 | else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0) |
2211 | != (TYPE_SIZE (TREE_TYPE (type1)) != 0)) | |
2212 | pedwarn ("comparison of complete and incomplete pointers"); | |
400fbf9f JW |
2213 | else if (pedantic |
2214 | && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
2215 | pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); | |
2216 | result_type = integer_type_node; | |
2217 | } | |
2218 | else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST | |
2219 | && integer_zerop (op1)) | |
2220 | { | |
2221 | result_type = integer_type_node; | |
2222 | op1 = null_pointer_node; | |
840f571b | 2223 | if (pedantic) |
400fbf9f JW |
2224 | pedwarn ("ordered comparison of pointer with integer zero"); |
2225 | } | |
2226 | else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST | |
2227 | && integer_zerop (op0)) | |
2228 | { | |
2229 | result_type = integer_type_node; | |
2230 | op0 = null_pointer_node; | |
2231 | if (pedantic) | |
2232 | pedwarn ("ordered comparison of pointer with integer zero"); | |
2233 | } | |
2234 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
2235 | { | |
2236 | result_type = integer_type_node; | |
2237 | if (! flag_traditional) | |
2238 | pedwarn ("comparison between pointer and integer"); | |
2239 | op1 = convert (TREE_TYPE (op0), op1); | |
2240 | } | |
2241 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
2242 | { | |
2243 | result_type = integer_type_node; | |
2244 | if (! flag_traditional) | |
2245 | pedwarn ("comparison between pointer and integer"); | |
2246 | op0 = convert (TREE_TYPE (op1), op0); | |
2247 | } | |
2248 | converted = 1; | |
2249 | break; | |
2250 | } | |
2251 | ||
b6a10c9f RS |
2252 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) |
2253 | && | |
2254 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) | |
400fbf9f | 2255 | { |
b6a10c9f RS |
2256 | int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); |
2257 | ||
400fbf9f JW |
2258 | if (shorten || common || short_compare) |
2259 | result_type = common_type (type0, type1); | |
2260 | ||
2261 | /* For certain operations (which identify themselves by shorten != 0) | |
2262 | if both args were extended from the same smaller type, | |
2263 | do the arithmetic in that type and then extend. | |
2264 | ||
2265 | shorten !=0 and !=1 indicates a bitwise operation. | |
2266 | For them, this optimization is safe only if | |
2267 | both args are zero-extended or both are sign-extended. | |
2268 | Otherwise, we might change the result. | |
2269 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
2270 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
2271 | ||
b6a10c9f | 2272 | if (shorten && none_complex) |
400fbf9f JW |
2273 | { |
2274 | int unsigned0, unsigned1; | |
2275 | tree arg0 = get_narrower (op0, &unsigned0); | |
2276 | tree arg1 = get_narrower (op1, &unsigned1); | |
2277 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2278 | int uns = TREE_UNSIGNED (result_type); | |
2279 | tree type; | |
2280 | ||
2281 | final_type = result_type; | |
2282 | ||
e7951b3f | 2283 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion |
400fbf9f JW |
2284 | but it *requires* conversion to FINAL_TYPE. */ |
2285 | ||
e7951b3f RS |
2286 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2287 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2288 | && TREE_TYPE (op0) != final_type) | |
400fbf9f | 2289 | unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0)); |
e7951b3f RS |
2290 | if ((TYPE_PRECISION (TREE_TYPE (op1)) |
2291 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2292 | && TREE_TYPE (op1) != final_type) | |
400fbf9f JW |
2293 | unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1)); |
2294 | ||
2295 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ | |
2296 | ||
2297 | /* For bitwise operations, signedness of nominal type | |
2298 | does not matter. Consider only how operands were extended. */ | |
2299 | if (shorten == -1) | |
2300 | uns = unsigned0; | |
2301 | ||
2302 | /* Note that in all three cases below we refrain from optimizing | |
2303 | an unsigned operation on sign-extended args. | |
2304 | That would not be valid. */ | |
2305 | ||
2306 | /* Both args variable: if both extended in same way | |
2307 | from same width, do it in that width. | |
2308 | Do it unsigned if args were zero-extended. */ | |
2309 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2310 | < TYPE_PRECISION (result_type)) | |
2311 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2312 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2313 | && unsigned0 == unsigned1 | |
2314 | && (unsigned0 || !uns)) | |
2315 | result_type | |
2316 | = signed_or_unsigned_type (unsigned0, | |
2317 | common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2318 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2319 | && (unsigned1 || !uns) | |
2320 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2321 | < TYPE_PRECISION (result_type)) | |
2322 | && (type = signed_or_unsigned_type (unsigned1, | |
2323 | TREE_TYPE (arg1)), | |
2324 | int_fits_type_p (arg0, type))) | |
2325 | result_type = type; | |
2326 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2327 | && (unsigned0 || !uns) | |
2328 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2329 | < TYPE_PRECISION (result_type)) | |
2330 | && (type = signed_or_unsigned_type (unsigned0, | |
2331 | TREE_TYPE (arg0)), | |
2332 | int_fits_type_p (arg1, type))) | |
2333 | result_type = type; | |
2334 | } | |
2335 | ||
2336 | /* Shifts can be shortened if shifting right. */ | |
2337 | ||
2338 | if (short_shift) | |
2339 | { | |
2340 | int unsigned_arg; | |
2341 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
2342 | ||
2343 | final_type = result_type; | |
2344 | ||
2345 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) | |
2346 | unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0)); | |
2347 | ||
2348 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) | |
2349 | /* If arg is sign-extended and then unsigned-shifted, | |
2350 | we can simulate this with a signed shift in arg's type | |
2351 | only if the extended result is at least twice as wide | |
2352 | as the arg. Otherwise, the shift could use up all the | |
2353 | ones made by sign-extension and bring in zeros. | |
2354 | We can't optimize that case at all, but in most machines | |
2355 | it never happens because available widths are 2**N. */ | |
2356 | && (!TREE_UNSIGNED (final_type) | |
2357 | || unsigned_arg | |
2358 | || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type))) | |
2359 | { | |
2360 | /* Do an unsigned shift if the operand was zero-extended. */ | |
2361 | result_type | |
2362 | = signed_or_unsigned_type (unsigned_arg, | |
2363 | TREE_TYPE (arg0)); | |
2364 | /* Convert value-to-be-shifted to that type. */ | |
2365 | if (TREE_TYPE (op0) != result_type) | |
2366 | op0 = convert (result_type, op0); | |
2367 | converted = 1; | |
2368 | } | |
2369 | } | |
2370 | ||
2371 | /* Comparison operations are shortened too but differently. | |
2372 | They identify themselves by setting short_compare = 1. */ | |
2373 | ||
75326e8c | 2374 | if (short_compare) |
400fbf9f JW |
2375 | { |
2376 | /* Don't write &op0, etc., because that would prevent op0 | |
2377 | from being kept in a register. | |
2378 | Instead, make copies of the our local variables and | |
2379 | pass the copies by reference, then copy them back afterward. */ | |
2380 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
2381 | enum tree_code xresultcode = resultcode; | |
2382 | tree val | |
2383 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
2384 | if (val != 0) | |
2385 | return val; | |
2386 | op0 = xop0, op1 = xop1, result_type = xresult_type; | |
2387 | resultcode = xresultcode; | |
2388 | ||
2389 | if (extra_warnings) | |
2390 | { | |
2391 | tree op0_type = TREE_TYPE (orig_op0); | |
2392 | tree op1_type = TREE_TYPE (orig_op1); | |
2393 | int op0_unsigned = TREE_UNSIGNED (op0_type); | |
2394 | int op1_unsigned = TREE_UNSIGNED (op1_type); | |
2395 | ||
2396 | /* Give warnings for comparisons between signed and unsigned | |
2397 | quantities that will fail. Do not warn if the signed quantity | |
2398 | is an unsuffixed integer literal (or some static constant | |
2399 | expression involving such literals) and it is positive. | |
2400 | Do not warn if the width of the unsigned quantity is less | |
2401 | than that of the signed quantity, since in this case all | |
2402 | values of the unsigned quantity fit in the signed quantity. | |
2403 | Do not warn if the signed type is the same size as the | |
2404 | result_type since sign extension does not cause trouble in | |
2405 | this case. */ | |
2406 | /* Do the checking based on the original operand trees, so that | |
2407 | casts will be considered, but default promotions won't be. */ | |
2408 | if (op0_unsigned != op1_unsigned | |
2409 | && ((op0_unsigned | |
2410 | && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type) | |
2411 | && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type) | |
2412 | && (TREE_CODE (op1) != INTEGER_CST | |
2413 | || (TREE_CODE (op1) == INTEGER_CST | |
2414 | && INT_CST_LT (op1, integer_zero_node)))) | |
2415 | || | |
2416 | (op1_unsigned | |
2417 | && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type) | |
2418 | && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type) | |
2419 | && (TREE_CODE (op0) != INTEGER_CST | |
2420 | || (TREE_CODE (op0) == INTEGER_CST | |
2421 | && INT_CST_LT (op0, integer_zero_node)))))) | |
2422 | warning ("comparison between signed and unsigned"); | |
2423 | } | |
2424 | } | |
2425 | } | |
2426 | ||
2427 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. | |
2428 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
2429 | Then the expression will be built. | |
2430 | It will be given type FINAL_TYPE if that is nonzero; | |
2431 | otherwise, it will be given type RESULT_TYPE. */ | |
2432 | ||
2433 | if (!result_type) | |
2434 | { | |
2435 | binary_op_error (code); | |
2436 | return error_mark_node; | |
2437 | } | |
2438 | ||
2439 | if (! converted) | |
2440 | { | |
2441 | if (TREE_TYPE (op0) != result_type) | |
2442 | op0 = convert (result_type, op0); | |
2443 | if (TREE_TYPE (op1) != result_type) | |
2444 | op1 = convert (result_type, op1); | |
2445 | } | |
2446 | ||
2447 | { | |
2448 | register tree result = build (resultcode, result_type, op0, op1); | |
2449 | register tree folded; | |
2450 | ||
2451 | folded = fold (result); | |
2452 | if (folded == result) | |
2453 | TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); | |
2454 | if (final_type != 0) | |
2455 | return convert (final_type, folded); | |
2456 | return folded; | |
2457 | } | |
2458 | } | |
2459 | \f | |
2460 | /* Return a tree for the sum or difference (RESULTCODE says which) | |
2461 | of pointer PTROP and integer INTOP. */ | |
2462 | ||
2463 | static tree | |
2464 | pointer_int_sum (resultcode, ptrop, intop) | |
2465 | enum tree_code resultcode; | |
2466 | register tree ptrop, intop; | |
2467 | { | |
2468 | tree size_exp; | |
2469 | ||
2470 | register tree result; | |
2471 | register tree folded; | |
2472 | ||
2473 | /* The result is a pointer of the same type that is being added. */ | |
2474 | ||
2475 | register tree result_type = TREE_TYPE (ptrop); | |
2476 | ||
2477 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
2478 | { | |
2479 | if (pedantic || warn_pointer_arith) | |
2480 | pedwarn ("pointer of type `void *' used in arithmetic"); | |
2481 | size_exp = integer_one_node; | |
2482 | } | |
2483 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
2484 | { | |
2485 | if (pedantic || warn_pointer_arith) | |
2486 | pedwarn ("pointer to a function used in arithmetic"); | |
2487 | size_exp = integer_one_node; | |
2488 | } | |
2489 | else | |
2490 | size_exp = c_size_in_bytes (TREE_TYPE (result_type)); | |
2491 | ||
2492 | /* If what we are about to multiply by the size of the elements | |
2493 | contains a constant term, apply distributive law | |
2494 | and multiply that constant term separately. | |
2495 | This helps produce common subexpressions. */ | |
2496 | ||
2497 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) | |
2498 | && ! TREE_CONSTANT (intop) | |
2499 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) | |
2500 | && TREE_CONSTANT (size_exp) | |
2501 | /* If the constant comes from pointer subtraction, | |
2502 | skip this optimization--it would cause an error. */ | |
2503 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE) | |
2504 | { | |
2505 | enum tree_code subcode = resultcode; | |
d45cf215 | 2506 | tree int_type = TREE_TYPE (intop); |
400fbf9f JW |
2507 | if (TREE_CODE (intop) == MINUS_EXPR) |
2508 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
d45cf215 RS |
2509 | /* Convert both subexpression types to the type of intop, |
2510 | because weird cases involving pointer arithmetic | |
2511 | can result in a sum or difference with different type args. */ | |
2512 | ptrop = build_binary_op (subcode, ptrop, | |
2513 | convert (int_type, TREE_OPERAND (intop, 1)), 1); | |
2514 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
400fbf9f JW |
2515 | } |
2516 | ||
2517 | /* Convert the integer argument to a type the same size as a pointer | |
2518 | so the multiply won't overflow spuriously. */ | |
2519 | ||
2520 | if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE) | |
2521 | intop = convert (type_for_size (POINTER_SIZE, 0), intop); | |
2522 | ||
2523 | /* Replace the integer argument | |
2524 | with a suitable product by the object size. */ | |
2525 | ||
2526 | intop = build_binary_op (MULT_EXPR, intop, size_exp, 1); | |
2527 | ||
2528 | /* Create the sum or difference. */ | |
2529 | ||
2530 | result = build (resultcode, result_type, ptrop, intop); | |
2531 | ||
2532 | folded = fold (result); | |
2533 | if (folded == result) | |
2534 | TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop); | |
2535 | return folded; | |
2536 | } | |
2537 | ||
2538 | /* Return a tree for the difference of pointers OP0 and OP1. | |
2539 | The resulting tree has type int. */ | |
2540 | ||
2541 | static tree | |
2542 | pointer_diff (op0, op1) | |
2543 | register tree op0, op1; | |
2544 | { | |
2545 | register tree result, folded; | |
2546 | tree restype = ptrdiff_type_node; | |
2547 | ||
2548 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); | |
2549 | ||
2550 | if (pedantic || warn_pointer_arith) | |
2551 | { | |
2552 | if (TREE_CODE (target_type) == VOID_TYPE) | |
2553 | pedwarn ("pointer of type `void *' used in subtraction"); | |
2554 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
2555 | pedwarn ("pointer to a function used in subtraction"); | |
2556 | } | |
2557 | ||
2558 | /* First do the subtraction as integers; | |
2559 | then drop through to build the divide operator. */ | |
2560 | ||
2561 | op0 = build_binary_op (MINUS_EXPR, convert (restype, op0), | |
2562 | convert (restype, op1), 1); | |
ea8dd784 JW |
2563 | /* This generates an error if op1 is pointer to incomplete type. */ |
2564 | if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0) | |
2565 | error ("arithmetic on pointer to an incomplete type"); | |
2566 | /* This generates an error if op0 is pointer to incomplete type. */ | |
400fbf9f JW |
2567 | op1 = c_size_in_bytes (target_type); |
2568 | ||
2569 | /* Divide by the size, in easiest possible way. */ | |
2570 | ||
2571 | result = build (EXACT_DIV_EXPR, restype, op0, op1); | |
2572 | ||
2573 | folded = fold (result); | |
2574 | if (folded == result) | |
2575 | TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); | |
2576 | return folded; | |
2577 | } | |
2578 | \f | |
2579 | /* Construct and perhaps optimize a tree representation | |
2580 | for a unary operation. CODE, a tree_code, specifies the operation | |
2581 | and XARG is the operand. NOCONVERT nonzero suppresses | |
2582 | the default promotions (such as from short to int). */ | |
2583 | ||
2584 | tree | |
2585 | build_unary_op (code, xarg, noconvert) | |
2586 | enum tree_code code; | |
2587 | tree xarg; | |
2588 | int noconvert; | |
2589 | { | |
2590 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
2591 | register tree arg = xarg; | |
2592 | register tree argtype = 0; | |
2593 | register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg)); | |
2594 | char *errstring = NULL; | |
2595 | tree val; | |
2596 | ||
2597 | if (typecode == ERROR_MARK) | |
2598 | return error_mark_node; | |
2599 | if (typecode == ENUMERAL_TYPE) | |
2600 | typecode = INTEGER_TYPE; | |
2601 | ||
2602 | switch (code) | |
2603 | { | |
2604 | case CONVERT_EXPR: | |
2605 | /* This is used for unary plus, because a CONVERT_EXPR | |
2606 | is enough to prevent anybody from looking inside for | |
2607 | associativity, but won't generate any code. */ | |
b6a10c9f RS |
2608 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2609 | || typecode == COMPLEX_TYPE)) | |
400fbf9f JW |
2610 | errstring = "wrong type argument to unary plus"; |
2611 | else if (!noconvert) | |
2612 | arg = default_conversion (arg); | |
2613 | break; | |
2614 | ||
2615 | case NEGATE_EXPR: | |
b6a10c9f RS |
2616 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2617 | || typecode == COMPLEX_TYPE)) | |
400fbf9f JW |
2618 | errstring = "wrong type argument to unary minus"; |
2619 | else if (!noconvert) | |
2620 | arg = default_conversion (arg); | |
2621 | break; | |
2622 | ||
2623 | case BIT_NOT_EXPR: | |
1c2a9b35 RS |
2624 | if (typecode == COMPLEX_TYPE) |
2625 | { | |
2626 | code = CONJ_EXPR; | |
2627 | if (!noconvert) | |
2628 | arg = default_conversion (arg); | |
2629 | } | |
2630 | else if (typecode != INTEGER_TYPE) | |
400fbf9f JW |
2631 | errstring = "wrong type argument to bit-complement"; |
2632 | else if (!noconvert) | |
2633 | arg = default_conversion (arg); | |
2634 | break; | |
2635 | ||
2636 | case ABS_EXPR: | |
b6a10c9f RS |
2637 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE |
2638 | || typecode == COMPLEX_TYPE)) | |
400fbf9f JW |
2639 | errstring = "wrong type argument to abs"; |
2640 | else if (!noconvert) | |
2641 | arg = default_conversion (arg); | |
2642 | break; | |
2643 | ||
1c2a9b35 RS |
2644 | case CONJ_EXPR: |
2645 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
2646 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
2647 | || typecode == COMPLEX_TYPE)) | |
2648 | errstring = "wrong type argument to conjugation"; | |
2649 | else if (!noconvert) | |
2650 | arg = default_conversion (arg); | |
2651 | break; | |
2652 | ||
400fbf9f JW |
2653 | case TRUTH_NOT_EXPR: |
2654 | if (typecode != INTEGER_TYPE | |
2655 | && typecode != REAL_TYPE && typecode != POINTER_TYPE | |
b6a10c9f | 2656 | && typecode != COMPLEX_TYPE |
400fbf9f JW |
2657 | /* These will convert to a pointer. */ |
2658 | && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE) | |
2659 | { | |
2660 | errstring = "wrong type argument to unary exclamation mark"; | |
2661 | break; | |
2662 | } | |
2663 | arg = truthvalue_conversion (arg); | |
2664 | return invert_truthvalue (arg); | |
2665 | ||
2666 | case NOP_EXPR: | |
2667 | break; | |
b6a10c9f RS |
2668 | |
2669 | case REALPART_EXPR: | |
2670 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2671 | return TREE_REALPART (arg); | |
2672 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2673 | return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2674 | else | |
2675 | return arg; | |
2676 | ||
2677 | case IMAGPART_EXPR: | |
2678 | if (TREE_CODE (arg) == COMPLEX_CST) | |
2679 | return TREE_IMAGPART (arg); | |
2680 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) | |
2681 | return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); | |
2682 | else | |
2683 | return convert (TREE_TYPE (arg), integer_zero_node); | |
400fbf9f JW |
2684 | |
2685 | case PREINCREMENT_EXPR: | |
2686 | case POSTINCREMENT_EXPR: | |
2687 | case PREDECREMENT_EXPR: | |
2688 | case POSTDECREMENT_EXPR: | |
2689 | /* Handle complex lvalues (when permitted) | |
2690 | by reduction to simpler cases. */ | |
2691 | ||
2692 | val = unary_complex_lvalue (code, arg); | |
2693 | if (val != 0) | |
2694 | return val; | |
2695 | ||
b6a10c9f RS |
2696 | /* Increment or decrement the real part of the value, |
2697 | and don't change the imaginary part. */ | |
2698 | if (typecode == COMPLEX_TYPE) | |
2699 | { | |
2700 | tree real, imag; | |
2701 | ||
2702 | arg = stabilize_reference (arg); | |
2703 | real = build_unary_op (REALPART_EXPR, arg, 1); | |
2704 | imag = build_unary_op (IMAGPART_EXPR, arg, 1); | |
2705 | return build (COMPLEX_EXPR, TREE_TYPE (arg), | |
2706 | build_unary_op (code, real, 1), imag); | |
2707 | } | |
2708 | ||
400fbf9f JW |
2709 | /* Report invalid types. */ |
2710 | ||
2711 | if (typecode != POINTER_TYPE | |
2712 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) | |
2713 | { | |
2714 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
2715 | errstring ="wrong type argument to increment"; | |
2716 | else | |
2717 | errstring ="wrong type argument to decrement"; | |
2718 | break; | |
2719 | } | |
2720 | ||
2721 | { | |
2722 | register tree inc; | |
2723 | tree result_type = TREE_TYPE (arg); | |
2724 | ||
2725 | arg = get_unwidened (arg, 0); | |
2726 | argtype = TREE_TYPE (arg); | |
2727 | ||
2728 | /* Compute the increment. */ | |
2729 | ||
2730 | if (typecode == POINTER_TYPE) | |
2731 | { | |
6bc4e3d0 RS |
2732 | /* If pointer target is an undefined struct, |
2733 | we just cannot know how to do the arithmetic. */ | |
2734 | if (TYPE_SIZE (TREE_TYPE (result_type)) == 0) | |
2735 | error ("%s of pointer to unknown structure", | |
2736 | ((code == PREINCREMENT_EXPR | |
2737 | || code == POSTINCREMENT_EXPR) | |
2738 | ? "increment" : "decrement")); | |
2739 | else if ((pedantic || warn_pointer_arith) | |
2740 | && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE | |
2741 | || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)) | |
400fbf9f JW |
2742 | pedwarn ("wrong type argument to %s", |
2743 | ((code == PREINCREMENT_EXPR | |
2744 | || code == POSTINCREMENT_EXPR) | |
2745 | ? "increment" : "decrement")); | |
2746 | inc = c_sizeof_nowarn (TREE_TYPE (result_type)); | |
2747 | } | |
2748 | else | |
2749 | inc = integer_one_node; | |
2750 | ||
2751 | inc = convert (argtype, inc); | |
2752 | ||
2753 | /* Handle incrementing a cast-expression. */ | |
2754 | ||
2755 | while (1) | |
2756 | switch (TREE_CODE (arg)) | |
2757 | { | |
2758 | case NOP_EXPR: | |
2759 | case CONVERT_EXPR: | |
2760 | case FLOAT_EXPR: | |
2761 | case FIX_TRUNC_EXPR: | |
2762 | case FIX_FLOOR_EXPR: | |
2763 | case FIX_ROUND_EXPR: | |
2764 | case FIX_CEIL_EXPR: | |
ee71df46 | 2765 | pedantic_lvalue_warning (CONVERT_EXPR); |
400fbf9f JW |
2766 | /* If the real type has the same machine representation |
2767 | as the type it is cast to, we can make better output | |
2768 | by adding directly to the inside of the cast. */ | |
2769 | if ((TREE_CODE (TREE_TYPE (arg)) | |
2770 | == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
2771 | && (TYPE_MODE (TREE_TYPE (arg)) | |
2772 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0))))) | |
2773 | arg = TREE_OPERAND (arg, 0); | |
2774 | else | |
2775 | { | |
2776 | tree incremented, modify, value; | |
400fbf9f JW |
2777 | arg = stabilize_reference (arg); |
2778 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
2779 | value = arg; | |
2780 | else | |
2781 | value = save_expr (arg); | |
2782 | incremented = build (((code == PREINCREMENT_EXPR | |
2783 | || code == POSTINCREMENT_EXPR) | |
2784 | ? PLUS_EXPR : MINUS_EXPR), | |
2785 | argtype, value, inc); | |
2786 | TREE_SIDE_EFFECTS (incremented) = 1; | |
2787 | modify = build_modify_expr (arg, NOP_EXPR, incremented); | |
2788 | value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value); | |
2789 | TREE_USED (value) = 1; | |
2790 | return value; | |
2791 | } | |
2792 | break; | |
2793 | ||
2794 | default: | |
2795 | goto give_up; | |
2796 | } | |
2797 | give_up: | |
2798 | ||
2799 | /* Complain about anything else that is not a true lvalue. */ | |
2800 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
2801 | || code == POSTINCREMENT_EXPR) | |
2802 | ? "increment" : "decrement"))) | |
2803 | return error_mark_node; | |
2804 | ||
2805 | /* Report a read-only lvalue. */ | |
26b3c423 | 2806 | if (TREE_READONLY (arg)) |
400fbf9f JW |
2807 | readonly_warning (arg, |
2808 | ((code == PREINCREMENT_EXPR | |
2809 | || code == POSTINCREMENT_EXPR) | |
2810 | ? "increment" : "decrement")); | |
2811 | ||
2812 | val = build (code, TREE_TYPE (arg), arg, inc); | |
2813 | TREE_SIDE_EFFECTS (val) = 1; | |
2814 | val = convert (result_type, val); | |
2815 | if (TREE_CODE (val) != code) | |
2816 | TREE_NO_UNUSED_WARNING (val) = 1; | |
2817 | return val; | |
2818 | } | |
2819 | ||
2820 | case ADDR_EXPR: | |
2821 | /* Note that this operation never does default_conversion | |
2822 | regardless of NOCONVERT. */ | |
2823 | ||
2824 | /* Let &* cancel out to simplify resulting code. */ | |
2825 | if (TREE_CODE (arg) == INDIRECT_REF) | |
2826 | { | |
2827 | /* Don't let this be an lvalue. */ | |
2828 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
2829 | return non_lvalue (TREE_OPERAND (arg, 0)); | |
2830 | return TREE_OPERAND (arg, 0); | |
2831 | } | |
2832 | ||
2833 | /* For &x[y], return x+y */ | |
2834 | if (TREE_CODE (arg) == ARRAY_REF) | |
2835 | { | |
2836 | if (mark_addressable (TREE_OPERAND (arg, 0)) == 0) | |
2837 | return error_mark_node; | |
2838 | return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), | |
2839 | TREE_OPERAND (arg, 1), 1); | |
2840 | } | |
2841 | ||
2842 | /* Handle complex lvalues (when permitted) | |
2843 | by reduction to simpler cases. */ | |
2844 | val = unary_complex_lvalue (code, arg); | |
2845 | if (val != 0) | |
2846 | return val; | |
2847 | ||
2848 | #if 0 /* Turned off because inconsistent; | |
2849 | float f; *&(int)f = 3.4 stores in int format | |
2850 | whereas (int)f = 3.4 stores in float format. */ | |
2851 | /* Address of a cast is just a cast of the address | |
2852 | of the operand of the cast. */ | |
2853 | switch (TREE_CODE (arg)) | |
2854 | { | |
2855 | case NOP_EXPR: | |
2856 | case CONVERT_EXPR: | |
2857 | case FLOAT_EXPR: | |
2858 | case FIX_TRUNC_EXPR: | |
2859 | case FIX_FLOOR_EXPR: | |
2860 | case FIX_ROUND_EXPR: | |
2861 | case FIX_CEIL_EXPR: | |
2862 | if (pedantic) | |
2863 | pedwarn ("ANSI C forbids the address of a cast expression"); | |
2864 | return convert (build_pointer_type (TREE_TYPE (arg)), | |
2865 | build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), | |
2866 | 0)); | |
2867 | } | |
2868 | #endif | |
2869 | ||
2870 | /* Allow the address of a constructor if all the elements | |
2871 | are constant. */ | |
2872 | if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg)) | |
2873 | ; | |
2874 | /* Anything not already handled and not a true memory reference | |
2875 | is an error. */ | |
2876 | else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'")) | |
2877 | return error_mark_node; | |
2878 | ||
2879 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ | |
2880 | argtype = TREE_TYPE (arg); | |
2881 | /* If the lvalue is const or volatile, | |
2882 | merge that into the type that the address will point to. */ | |
2883 | if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd' | |
2884 | || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r') | |
2885 | { | |
2886 | if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) | |
2887 | argtype = c_build_type_variant (argtype, | |
2888 | TREE_READONLY (arg), | |
2889 | TREE_THIS_VOLATILE (arg)); | |
2890 | } | |
2891 | ||
2892 | argtype = build_pointer_type (argtype); | |
2893 | ||
2894 | if (mark_addressable (arg) == 0) | |
2895 | return error_mark_node; | |
2896 | ||
2897 | { | |
2898 | tree addr; | |
2899 | ||
2900 | if (TREE_CODE (arg) == COMPONENT_REF) | |
2901 | { | |
2902 | tree field = TREE_OPERAND (arg, 1); | |
2903 | ||
2904 | addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0); | |
2905 | ||
2906 | if (DECL_BIT_FIELD (field)) | |
2907 | { | |
2908 | error ("attempt to take address of bit-field structure member `%s'", | |
2909 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
2910 | return error_mark_node; | |
2911 | } | |
2912 | ||
2913 | addr = convert (argtype, addr); | |
2914 | ||
2915 | if (! integer_zerop (DECL_FIELD_BITPOS (field))) | |
2916 | { | |
2917 | tree offset | |
2918 | = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field), | |
2919 | size_int (BITS_PER_UNIT)); | |
2920 | int flag = TREE_CONSTANT (addr); | |
2921 | addr = fold (build (PLUS_EXPR, argtype, | |
2922 | addr, convert (argtype, offset))); | |
2923 | TREE_CONSTANT (addr) = flag; | |
2924 | } | |
2925 | } | |
2926 | else | |
2927 | addr = build1 (code, argtype, arg); | |
2928 | ||
2929 | /* Address of a static or external variable or | |
8706edbc RS |
2930 | file-scope function counts as a constant. */ |
2931 | if (staticp (arg) | |
2932 | && ! (TREE_CODE (arg) == FUNCTION_DECL | |
2933 | && DECL_CONTEXT (arg) != 0)) | |
7d2d49af | 2934 | TREE_CONSTANT (addr) = 1; |
400fbf9f JW |
2935 | return addr; |
2936 | } | |
2937 | } | |
2938 | ||
2939 | if (!errstring) | |
2940 | { | |
2941 | if (argtype == 0) | |
2942 | argtype = TREE_TYPE (arg); | |
2943 | return fold (build1 (code, argtype, arg)); | |
2944 | } | |
2945 | ||
2946 | error (errstring); | |
2947 | return error_mark_node; | |
2948 | } | |
2949 | ||
2950 | #if 0 | |
2951 | /* If CONVERSIONS is a conversion expression or a nested sequence of such, | |
2952 | convert ARG with the same conversions in the same order | |
2953 | and return the result. */ | |
2954 | ||
2955 | static tree | |
2956 | convert_sequence (conversions, arg) | |
2957 | tree conversions; | |
2958 | tree arg; | |
2959 | { | |
2960 | switch (TREE_CODE (conversions)) | |
2961 | { | |
2962 | case NOP_EXPR: | |
2963 | case CONVERT_EXPR: | |
2964 | case FLOAT_EXPR: | |
2965 | case FIX_TRUNC_EXPR: | |
2966 | case FIX_FLOOR_EXPR: | |
2967 | case FIX_ROUND_EXPR: | |
2968 | case FIX_CEIL_EXPR: | |
2969 | return convert (TREE_TYPE (conversions), | |
2970 | convert_sequence (TREE_OPERAND (conversions, 0), | |
2971 | arg)); | |
2972 | ||
2973 | default: | |
2974 | return arg; | |
2975 | } | |
2976 | } | |
2977 | #endif /* 0 */ | |
2978 | ||
2979 | /* Return nonzero if REF is an lvalue valid for this language. | |
2980 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
1394aabd | 2981 | Lvalues can have their address taken, unless they have DECL_REGISTER. */ |
400fbf9f JW |
2982 | |
2983 | int | |
2984 | lvalue_p (ref) | |
2985 | tree ref; | |
2986 | { | |
2987 | register enum tree_code code = TREE_CODE (ref); | |
2988 | ||
2989 | switch (code) | |
2990 | { | |
b6a10c9f RS |
2991 | case REALPART_EXPR: |
2992 | case IMAGPART_EXPR: | |
400fbf9f JW |
2993 | case COMPONENT_REF: |
2994 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
2995 | ||
2996 | case STRING_CST: | |
2997 | return 1; | |
2998 | ||
2999 | case INDIRECT_REF: | |
3000 | case ARRAY_REF: | |
3001 | case VAR_DECL: | |
3002 | case PARM_DECL: | |
3003 | case RESULT_DECL: | |
3004 | case ERROR_MARK: | |
3005 | if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3006 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE) | |
3007 | return 1; | |
3008 | break; | |
3009 | } | |
3010 | return 0; | |
3011 | } | |
3012 | ||
3013 | /* Return nonzero if REF is an lvalue valid for this language; | |
3014 | otherwise, print an error message and return zero. */ | |
3015 | ||
3016 | int | |
3017 | lvalue_or_else (ref, string) | |
3018 | tree ref; | |
3019 | char *string; | |
3020 | { | |
3021 | int win = lvalue_p (ref); | |
3022 | if (! win) | |
3023 | error ("invalid lvalue in %s", string); | |
3024 | return win; | |
3025 | } | |
3026 | ||
3027 | /* Apply unary lvalue-demanding operator CODE to the expression ARG | |
3028 | for certain kinds of expressions which are not really lvalues | |
3029 | but which we can accept as lvalues. | |
3030 | ||
3031 | If ARG is not a kind of expression we can handle, return zero. */ | |
3032 | ||
3033 | static tree | |
3034 | unary_complex_lvalue (code, arg) | |
3035 | enum tree_code code; | |
3036 | tree arg; | |
3037 | { | |
3038 | /* Handle (a, b) used as an "lvalue". */ | |
3039 | if (TREE_CODE (arg) == COMPOUND_EXPR) | |
3040 | { | |
3041 | tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0); | |
3042 | pedantic_lvalue_warning (COMPOUND_EXPR); | |
3043 | return build (COMPOUND_EXPR, TREE_TYPE (real_result), | |
3044 | TREE_OPERAND (arg, 0), real_result); | |
3045 | } | |
3046 | ||
3047 | /* Handle (a ? b : c) used as an "lvalue". */ | |
3048 | if (TREE_CODE (arg) == COND_EXPR) | |
3049 | { | |
3050 | pedantic_lvalue_warning (COND_EXPR); | |
3051 | return (build_conditional_expr | |
3052 | (TREE_OPERAND (arg, 0), | |
3053 | build_unary_op (code, TREE_OPERAND (arg, 1), 0), | |
3054 | build_unary_op (code, TREE_OPERAND (arg, 2), 0))); | |
3055 | } | |
3056 | ||
3057 | return 0; | |
3058 | } | |
3059 | ||
3060 | /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR | |
3061 | COMPOUND_EXPR, or CONVERT_EXPR (for casts). */ | |
3062 | ||
3063 | static void | |
3064 | pedantic_lvalue_warning (code) | |
3065 | enum tree_code code; | |
3066 | { | |
3067 | if (pedantic) | |
3068 | pedwarn ("ANSI C forbids use of %s expressions as lvalues", | |
3069 | code == COND_EXPR ? "conditional" | |
3070 | : code == COMPOUND_EXPR ? "compound" : "cast"); | |
3071 | } | |
3072 | \f | |
3073 | /* Warn about storing in something that is `const'. */ | |
3074 | ||
3075 | void | |
3076 | readonly_warning (arg, string) | |
3077 | tree arg; | |
3078 | char *string; | |
3079 | { | |
3080 | char buf[80]; | |
3081 | strcpy (buf, string); | |
3082 | ||
3791970d | 3083 | /* Forbid assignments to iterators. */ |
550707f7 | 3084 | if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg)) |
3791970d RS |
3085 | { |
3086 | strcat (buf, " of iterator `%s'"); | |
550707f7 | 3087 | pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); |
3791970d RS |
3088 | } |
3089 | ||
400fbf9f JW |
3090 | if (TREE_CODE (arg) == COMPONENT_REF) |
3091 | { | |
3092 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
3093 | readonly_warning (TREE_OPERAND (arg, 0), string); | |
3094 | else | |
3095 | { | |
3096 | strcat (buf, " of read-only member `%s'"); | |
3097 | pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1)))); | |
3098 | } | |
3099 | } | |
3100 | else if (TREE_CODE (arg) == VAR_DECL) | |
3101 | { | |
3102 | strcat (buf, " of read-only variable `%s'"); | |
3103 | pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3104 | } | |
3105 | else | |
3106 | { | |
3107 | pedwarn ("%s of read-only location", buf); | |
3108 | } | |
3109 | } | |
3110 | \f | |
3111 | /* Mark EXP saying that we need to be able to take the | |
3112 | address of it; it should not be allocated in a register. | |
3113 | Value is 1 if successful. */ | |
3114 | ||
3115 | int | |
3116 | mark_addressable (exp) | |
3117 | tree exp; | |
3118 | { | |
3119 | register tree x = exp; | |
3120 | while (1) | |
3121 | switch (TREE_CODE (x)) | |
3122 | { | |
3123 | case ADDR_EXPR: | |
3124 | case COMPONENT_REF: | |
3125 | case ARRAY_REF: | |
ce95080d RS |
3126 | case REALPART_EXPR: |
3127 | case IMAGPART_EXPR: | |
400fbf9f JW |
3128 | x = TREE_OPERAND (x, 0); |
3129 | break; | |
3130 | ||
3131 | case CONSTRUCTOR: | |
3132 | TREE_ADDRESSABLE (x) = 1; | |
3133 | return 1; | |
3134 | ||
3135 | case VAR_DECL: | |
3136 | case CONST_DECL: | |
3137 | case PARM_DECL: | |
3138 | case RESULT_DECL: | |
1394aabd RS |
3139 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) |
3140 | && DECL_NONLOCAL (x)) | |
4bb6d2f8 RS |
3141 | { |
3142 | if (TREE_PUBLIC (x)) | |
3143 | { | |
3144 | error ("global register variable `%s' used in nested function", | |
3145 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3146 | return 0; | |
3147 | } | |
3148 | pedwarn ("register variable `%s' used in nested function", | |
3149 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3150 | } | |
1394aabd | 3151 | else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)) |
400fbf9f JW |
3152 | { |
3153 | if (TREE_PUBLIC (x)) | |
3154 | { | |
3155 | error ("address of global register variable `%s' requested", | |
3156 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3157 | return 0; | |
3158 | } | |
3159 | pedwarn ("address of register variable `%s' requested", | |
3160 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
3161 | } | |
3162 | put_var_into_stack (x); | |
3163 | ||
3164 | /* drops in */ | |
3165 | case FUNCTION_DECL: | |
3166 | TREE_ADDRESSABLE (x) = 1; | |
3167 | #if 0 /* poplevel deals with this now. */ | |
3168 | if (DECL_CONTEXT (x) == 0) | |
3169 | TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1; | |
3170 | #endif | |
3171 | ||
3172 | default: | |
3173 | return 1; | |
3174 | } | |
3175 | } | |
3176 | \f | |
3177 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. */ | |
3178 | ||
3179 | tree | |
3180 | build_conditional_expr (ifexp, op1, op2) | |
3181 | tree ifexp, op1, op2; | |
3182 | { | |
3183 | register tree type1; | |
3184 | register tree type2; | |
3185 | register enum tree_code code1; | |
3186 | register enum tree_code code2; | |
3187 | register tree result_type = NULL; | |
fd5d5b94 | 3188 | tree orig_op1 = op1, orig_op2 = op2; |
400fbf9f JW |
3189 | |
3190 | /* If second operand is omitted, it is the same as the first one; | |
3191 | make sure it is calculated only once. */ | |
3192 | if (op1 == 0) | |
3193 | { | |
3194 | if (pedantic) | |
3195 | pedwarn ("ANSI C forbids omitting the middle term of a ?: expression"); | |
3196 | ifexp = op1 = save_expr (ifexp); | |
3197 | } | |
3198 | ||
3199 | ifexp = truthvalue_conversion (default_conversion (ifexp)); | |
3200 | ||
400fbf9f JW |
3201 | #if 0 /* Produces wrong result if within sizeof. */ |
3202 | /* Don't promote the operands separately if they promote | |
3203 | the same way. Return the unpromoted type and let the combined | |
3204 | value get promoted if necessary. */ | |
3205 | ||
3206 | if (TREE_TYPE (op1) == TREE_TYPE (op2) | |
3207 | && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE | |
3208 | && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE | |
3209 | && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE) | |
3210 | { | |
3211 | if (TREE_CODE (ifexp) == INTEGER_CST) | |
a29f2ec1 | 3212 | return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
400fbf9f JW |
3213 | |
3214 | return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2)); | |
3215 | } | |
3216 | #endif | |
3217 | ||
e855c5ce | 3218 | /* Promote both alternatives. */ |
400fbf9f JW |
3219 | |
3220 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
3221 | op1 = default_conversion (op1); | |
3222 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
3223 | op2 = default_conversion (op2); | |
3224 | ||
e855c5ce RS |
3225 | if (TREE_CODE (ifexp) == ERROR_MARK |
3226 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
3227 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
3228 | return error_mark_node; | |
3229 | ||
400fbf9f JW |
3230 | type1 = TREE_TYPE (op1); |
3231 | code1 = TREE_CODE (type1); | |
3232 | type2 = TREE_TYPE (op2); | |
3233 | code2 = TREE_CODE (type2); | |
3234 | ||
3235 | /* Quickly detect the usual case where op1 and op2 have the same type | |
3236 | after promotion. */ | |
1ad409d2 RS |
3237 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) |
3238 | { | |
3239 | if (type1 == type2) | |
3240 | result_type = type1; | |
3241 | else | |
3242 | result_type = TYPE_MAIN_VARIANT (type1); | |
3243 | } | |
400fbf9f JW |
3244 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE) |
3245 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE)) | |
3246 | { | |
3247 | result_type = common_type (type1, type2); | |
3248 | } | |
3249 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
3250 | { | |
3251 | if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE)) | |
3252 | pedwarn ("ANSI C forbids conditional expr with only one void side"); | |
3253 | result_type = void_type_node; | |
3254 | } | |
3255 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
3256 | { | |
3257 | if (comp_target_types (type1, type2)) | |
3258 | result_type = common_type (type1, type2); | |
fd5d5b94 RS |
3259 | else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node |
3260 | && TREE_CODE (orig_op1) != NOP_EXPR) | |
400fbf9f | 3261 | result_type = qualify_type (type2, type1); |
fd5d5b94 RS |
3262 | else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node |
3263 | && TREE_CODE (orig_op2) != NOP_EXPR) | |
400fbf9f JW |
3264 | result_type = qualify_type (type1, type2); |
3265 | else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node) | |
3266 | { | |
3267 | if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) | |
3268 | pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); | |
3269 | result_type = qualify_type (type1, type2); | |
3270 | } | |
3271 | else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node) | |
3272 | { | |
3273 | if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) | |
3274 | pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); | |
3275 | result_type = qualify_type (type2, type1); | |
3276 | } | |
3277 | else | |
3278 | { | |
3279 | pedwarn ("pointer type mismatch in conditional expression"); | |
3280 | result_type = build_pointer_type (void_type_node); | |
3281 | } | |
3282 | } | |
3283 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
3284 | { | |
3285 | if (! integer_zerop (op2)) | |
3286 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
3287 | else | |
3288 | { | |
3289 | op2 = null_pointer_node; | |
3290 | #if 0 /* The spec seems to say this is permitted. */ | |
3291 | if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE) | |
3292 | pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); | |
3293 | #endif | |
3294 | } | |
3295 | result_type = type1; | |
3296 | } | |
3297 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
3298 | { | |
3299 | if (!integer_zerop (op1)) | |
3300 | pedwarn ("pointer/integer type mismatch in conditional expression"); | |
3301 | else | |
3302 | { | |
3303 | op1 = null_pointer_node; | |
3304 | #if 0 /* The spec seems to say this is permitted. */ | |
3305 | if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE) | |
3306 | pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); | |
3307 | #endif | |
3308 | } | |
3309 | result_type = type2; | |
3310 | } | |
3311 | ||
3312 | if (!result_type) | |
3313 | { | |
3314 | if (flag_cond_mismatch) | |
3315 | result_type = void_type_node; | |
3316 | else | |
3317 | { | |
3318 | error ("type mismatch in conditional expression"); | |
3319 | return error_mark_node; | |
3320 | } | |
3321 | } | |
3322 | ||
1dfdf85d RS |
3323 | /* Merge const and volatile flags of the incoming types. */ |
3324 | result_type | |
3325 | = build_type_variant (result_type, | |
48c73063 RS |
3326 | TREE_READONLY (op1) || TREE_READONLY (op2), |
3327 | TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2)); | |
e58cd767 | 3328 | |
400fbf9f | 3329 | if (result_type != TREE_TYPE (op1)) |
e58cd767 | 3330 | op1 = convert_and_check (result_type, op1); |
400fbf9f | 3331 | if (result_type != TREE_TYPE (op2)) |
e58cd767 | 3332 | op2 = convert_and_check (result_type, op2); |
400fbf9f JW |
3333 | |
3334 | #if 0 | |
3335 | if (code1 == RECORD_TYPE || code1 == UNION_TYPE) | |
3336 | { | |
3337 | result_type = TREE_TYPE (op1); | |
3338 | if (TREE_CONSTANT (ifexp)) | |
a29f2ec1 | 3339 | return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
400fbf9f JW |
3340 | |
3341 | if (TYPE_MODE (result_type) == BLKmode) | |
3342 | { | |
3343 | register tree tempvar | |
3344 | = build_decl (VAR_DECL, NULL_TREE, result_type); | |
3345 | register tree xop1 = build_modify_expr (tempvar, op1); | |
3346 | register tree xop2 = build_modify_expr (tempvar, op2); | |
3347 | register tree result = fold (build (COND_EXPR, result_type, | |
3348 | ifexp, xop1, xop2)); | |
3349 | ||
3350 | layout_decl (tempvar, TYPE_ALIGN (result_type)); | |
3351 | /* No way to handle variable-sized objects here. | |
3352 | I fear that the entire handling of BLKmode conditional exprs | |
3353 | needs to be redone. */ | |
3354 | if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST) | |
3355 | abort (); | |
3356 | DECL_RTL (tempvar) | |
3357 | = assign_stack_local (DECL_MODE (tempvar), | |
3358 | (TREE_INT_CST_LOW (DECL_SIZE (tempvar)) | |
3359 | + BITS_PER_UNIT - 1) | |
3360 | / BITS_PER_UNIT, | |
3361 | 0); | |
3362 | ||
3363 | TREE_SIDE_EFFECTS (result) | |
3364 | = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1) | |
3365 | | TREE_SIDE_EFFECTS (op2); | |
3366 | return build (COMPOUND_EXPR, result_type, result, tempvar); | |
3367 | } | |
3368 | } | |
3369 | #endif /* 0 */ | |
5abb45f2 RS |
3370 | |
3371 | if (TREE_CODE (ifexp) == INTEGER_CST) | |
a29f2ec1 | 3372 | return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); |
400fbf9f | 3373 | |
400fbf9f JW |
3374 | return fold (build (COND_EXPR, result_type, ifexp, op1, op2)); |
3375 | } | |
3376 | \f | |
3377 | /* Given a list of expressions, return a compound expression | |
3378 | that performs them all and returns the value of the last of them. */ | |
3379 | ||
3380 | tree | |
3381 | build_compound_expr (list) | |
3382 | tree list; | |
82bde854 | 3383 | { |
43a5a542 | 3384 | return internal_build_compound_expr (list, TRUE); |
82bde854 MM |
3385 | } |
3386 | ||
3387 | static tree | |
3388 | internal_build_compound_expr (list, first_p) | |
3389 | tree list; | |
3390 | int first_p; | |
400fbf9f JW |
3391 | { |
3392 | register tree rest; | |
3393 | ||
3394 | if (TREE_CHAIN (list) == 0) | |
3395 | { | |
6dc42e49 | 3396 | #if 0 /* If something inside inhibited lvalueness, we should not override. */ |
400fbf9f JW |
3397 | /* Consider (x, y+0), which is not an lvalue since y+0 is not. */ |
3398 | ||
3399 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
3400 | if (TREE_CODE (list) == NON_LVALUE_EXPR) | |
3401 | list = TREE_OPERAND (list, 0); | |
3402 | #endif | |
3403 | ||
439f6027 | 3404 | /* Don't let (0, 0) be null pointer constant. */ |
82bde854 | 3405 | if (!first_p && integer_zerop (TREE_VALUE (list))) |
439f6027 RS |
3406 | return non_lvalue (TREE_VALUE (list)); |
3407 | return TREE_VALUE (list); | |
400fbf9f JW |
3408 | } |
3409 | ||
3410 | if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0) | |
3411 | { | |
3412 | /* Convert arrays to pointers when there really is a comma operator. */ | |
3413 | if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE) | |
3414 | TREE_VALUE (TREE_CHAIN (list)) | |
3415 | = default_conversion (TREE_VALUE (TREE_CHAIN (list))); | |
3416 | } | |
3417 | ||
82bde854 | 3418 | rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE); |
400fbf9f | 3419 | |
29df70e6 JW |
3420 | /* When pedantic, a compound expression can be neither an lvalue |
3421 | nor an integer constant expression. */ | |
3422 | if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic) | |
400fbf9f JW |
3423 | return rest; |
3424 | ||
3425 | return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest); | |
3426 | } | |
3427 | ||
3428 | /* Build an expression representing a cast to type TYPE of expression EXPR. */ | |
3429 | ||
3430 | tree | |
3431 | build_c_cast (type, expr) | |
3432 | register tree type; | |
3433 | tree expr; | |
3434 | { | |
3435 | register tree value = expr; | |
3436 | ||
3437 | if (type == error_mark_node || expr == error_mark_node) | |
3438 | return error_mark_node; | |
3439 | type = TYPE_MAIN_VARIANT (type); | |
3440 | ||
3441 | #if 0 | |
3442 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
3443 | if (TREE_CODE (value) == NON_LVALUE_EXPR) | |
3444 | value = TREE_OPERAND (value, 0); | |
3445 | #endif | |
3446 | ||
3447 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3448 | { | |
3449 | error ("cast specifies array type"); | |
3450 | return error_mark_node; | |
3451 | } | |
3452 | ||
3453 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
3454 | { | |
3455 | error ("cast specifies function type"); | |
3456 | return error_mark_node; | |
3457 | } | |
3458 | ||
3459 | if (type == TREE_TYPE (value)) | |
3460 | { | |
3461 | if (pedantic) | |
3462 | { | |
3463 | if (TREE_CODE (type) == RECORD_TYPE | |
3464 | || TREE_CODE (type) == UNION_TYPE) | |
3465 | pedwarn ("ANSI C forbids casting nonscalar to the same type"); | |
3466 | } | |
3467 | } | |
3468 | else if (TREE_CODE (type) == UNION_TYPE) | |
3469 | { | |
3470 | tree field; | |
0c16ddf7 RS |
3471 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
3472 | || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE) | |
3473 | value = default_conversion (value); | |
3474 | ||
400fbf9f JW |
3475 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
3476 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
3477 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3478 | break; | |
3479 | ||
3480 | if (field) | |
3481 | { | |
805f961c | 3482 | char *name; |
281ec92f | 3483 | tree t; |
805f961c | 3484 | |
400fbf9f JW |
3485 | if (pedantic) |
3486 | pedwarn ("ANSI C forbids casts to union type"); | |
805f961c RS |
3487 | if (TYPE_NAME (type) != 0) |
3488 | { | |
3489 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
3490 | name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
3491 | else | |
3492 | name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); | |
3493 | } | |
3494 | else | |
3495 | name = ""; | |
281ec92f RS |
3496 | t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE, |
3497 | build_tree_list (field, value)), | |
3498 | 0, 0); | |
3499 | TREE_CONSTANT (t) = TREE_CONSTANT (value); | |
3500 | return t; | |
400fbf9f JW |
3501 | } |
3502 | error ("cast to union type from type not present in union"); | |
3503 | return error_mark_node; | |
3504 | } | |
3505 | else | |
3506 | { | |
10d5caec | 3507 | tree otype, ovalue; |
53b01f59 RS |
3508 | |
3509 | /* If casting to void, avoid the error that would come | |
3510 | from default_conversion in the case of a non-lvalue array. */ | |
3511 | if (type == void_type_node) | |
3512 | return build1 (CONVERT_EXPR, type, value); | |
3513 | ||
400fbf9f JW |
3514 | /* Convert functions and arrays to pointers, |
3515 | but don't convert any other types. */ | |
3516 | if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE | |
3517 | || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE) | |
3518 | value = default_conversion (value); | |
3519 | otype = TREE_TYPE (value); | |
3520 | ||
d45cf215 | 3521 | /* Optionally warn about potentially worrisome casts. */ |
400fbf9f JW |
3522 | |
3523 | if (warn_cast_qual | |
3524 | && TREE_CODE (type) == POINTER_TYPE | |
3525 | && TREE_CODE (otype) == POINTER_TYPE) | |
3526 | { | |
3527 | if (TYPE_VOLATILE (TREE_TYPE (otype)) | |
3528 | && ! TYPE_VOLATILE (TREE_TYPE (type))) | |
3529 | pedwarn ("cast discards `volatile' from pointer target type"); | |
3530 | if (TYPE_READONLY (TREE_TYPE (otype)) | |
3531 | && ! TYPE_READONLY (TREE_TYPE (type))) | |
3532 | pedwarn ("cast discards `const' from pointer target type"); | |
3533 | } | |
3534 | ||
3535 | /* Warn about possible alignment problems. */ | |
d45cf215 | 3536 | if (STRICT_ALIGNMENT && warn_cast_align |
400fbf9f JW |
3537 | && TREE_CODE (type) == POINTER_TYPE |
3538 | && TREE_CODE (otype) == POINTER_TYPE | |
3539 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
3540 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
3541 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
3542 | warning ("cast increases required alignment of target type"); | |
400fbf9f JW |
3543 | |
3544 | if (TREE_CODE (type) == INTEGER_TYPE | |
3545 | && TREE_CODE (otype) == POINTER_TYPE | |
c9b7f31c RS |
3546 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) |
3547 | && !TREE_CONSTANT (value)) | |
400fbf9f JW |
3548 | warning ("cast from pointer to integer of different size"); |
3549 | ||
3550 | if (TREE_CODE (type) == POINTER_TYPE | |
3551 | && TREE_CODE (otype) == INTEGER_TYPE | |
2918ed3c | 3552 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) |
c9b7f31c | 3553 | #if 0 |
2918ed3c RS |
3554 | /* Don't warn about converting 0 to pointer, |
3555 | provided the 0 was explicit--not cast or made by folding. */ | |
c9b7f31c RS |
3556 | && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)) |
3557 | #endif | |
3558 | /* Don't warn about converting any constant. */ | |
3559 | && !TREE_CONSTANT (value)) | |
400fbf9f JW |
3560 | warning ("cast to pointer from integer of different size"); |
3561 | ||
10d5caec | 3562 | ovalue = value; |
400fbf9f | 3563 | value = convert (type, value); |
e58cd767 RS |
3564 | |
3565 | /* Ignore any integer overflow caused by the cast. */ | |
3566 | if (TREE_CODE (value) == INTEGER_CST) | |
10d5caec PE |
3567 | { |
3568 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
3569 | TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); | |
3570 | } | |
400fbf9f JW |
3571 | } |
3572 | ||
fd5d5b94 RS |
3573 | /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */ |
3574 | if (pedantic && TREE_CODE (value) == INTEGER_CST | |
3575 | && TREE_CODE (expr) == INTEGER_CST | |
3576 | && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE) | |
3577 | value = non_lvalue (value); | |
3578 | ||
3579 | /* If pedantic, don't let a cast be an lvalue. */ | |
400fbf9f | 3580 | if (value == expr && pedantic) |
fd5d5b94 RS |
3581 | value = non_lvalue (value); |
3582 | ||
400fbf9f JW |
3583 | return value; |
3584 | } | |
3585 | \f | |
3586 | /* Build an assignment expression of lvalue LHS from value RHS. | |
3587 | MODIFYCODE is the code for a binary operator that we use | |
3588 | to combine the old value of LHS with RHS to get the new value. | |
3589 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */ | |
3590 | ||
3591 | tree | |
3592 | build_modify_expr (lhs, modifycode, rhs) | |
3593 | tree lhs, rhs; | |
3594 | enum tree_code modifycode; | |
3595 | { | |
3596 | register tree result; | |
3597 | tree newrhs; | |
3598 | tree lhstype = TREE_TYPE (lhs); | |
3599 | tree olhstype = lhstype; | |
3600 | ||
3601 | /* Types that aren't fully specified cannot be used in assignments. */ | |
3602 | lhs = require_complete_type (lhs); | |
3603 | ||
3604 | /* Avoid duplicate error messages from operands that had errors. */ | |
3605 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
3606 | return error_mark_node; | |
3607 | ||
3608 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
3609 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
3610 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f JW |
3611 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) |
3612 | rhs = TREE_OPERAND (rhs, 0); | |
3613 | ||
3614 | newrhs = rhs; | |
3615 | ||
3616 | /* Handle control structure constructs used as "lvalues". */ | |
3617 | ||
3618 | switch (TREE_CODE (lhs)) | |
3619 | { | |
3620 | /* Handle (a, b) used as an "lvalue". */ | |
3621 | case COMPOUND_EXPR: | |
3622 | pedantic_lvalue_warning (COMPOUND_EXPR); | |
19d76e60 RK |
3623 | newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), |
3624 | modifycode, rhs); | |
3625 | if (TREE_CODE (newrhs) == ERROR_MARK) | |
3626 | return error_mark_node; | |
400fbf9f | 3627 | return build (COMPOUND_EXPR, lhstype, |
19d76e60 RK |
3628 | TREE_OPERAND (lhs, 0), newrhs); |
3629 | ||
400fbf9f JW |
3630 | /* Handle (a ? b : c) used as an "lvalue". */ |
3631 | case COND_EXPR: | |
3632 | pedantic_lvalue_warning (COND_EXPR); | |
3633 | rhs = save_expr (rhs); | |
3634 | { | |
3635 | /* Produce (a ? (b = rhs) : (c = rhs)) | |
3636 | except that the RHS goes through a save-expr | |
3637 | so the code to compute it is only emitted once. */ | |
3638 | tree cond | |
3639 | = build_conditional_expr (TREE_OPERAND (lhs, 0), | |
3640 | build_modify_expr (TREE_OPERAND (lhs, 1), | |
3641 | modifycode, rhs), | |
3642 | build_modify_expr (TREE_OPERAND (lhs, 2), | |
3643 | modifycode, rhs)); | |
19d76e60 RK |
3644 | if (TREE_CODE (cond) == ERROR_MARK) |
3645 | return cond; | |
400fbf9f JW |
3646 | /* Make sure the code to compute the rhs comes out |
3647 | before the split. */ | |
3648 | return build (COMPOUND_EXPR, TREE_TYPE (lhs), | |
3649 | /* But cast it to void to avoid an "unused" error. */ | |
3650 | convert (void_type_node, rhs), cond); | |
3651 | } | |
3652 | } | |
3653 | ||
3654 | /* If a binary op has been requested, combine the old LHS value with the RHS | |
3655 | producing the value we should actually store into the LHS. */ | |
3656 | ||
3657 | if (modifycode != NOP_EXPR) | |
3658 | { | |
3659 | lhs = stabilize_reference (lhs); | |
3660 | newrhs = build_binary_op (modifycode, lhs, rhs, 1); | |
3661 | } | |
3662 | ||
3663 | /* Handle a cast used as an "lvalue". | |
3664 | We have already performed any binary operator using the value as cast. | |
3665 | Now convert the result to the cast type of the lhs, | |
3666 | and then true type of the lhs and store it there; | |
3667 | then convert result back to the cast type to be the value | |
3668 | of the assignment. */ | |
3669 | ||
3670 | switch (TREE_CODE (lhs)) | |
3671 | { | |
3672 | case NOP_EXPR: | |
3673 | case CONVERT_EXPR: | |
3674 | case FLOAT_EXPR: | |
3675 | case FIX_TRUNC_EXPR: | |
3676 | case FIX_FLOOR_EXPR: | |
3677 | case FIX_ROUND_EXPR: | |
3678 | case FIX_CEIL_EXPR: | |
3679 | if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE | |
3680 | || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE) | |
3681 | newrhs = default_conversion (newrhs); | |
3682 | { | |
3683 | tree inner_lhs = TREE_OPERAND (lhs, 0); | |
3684 | tree result; | |
3685 | result = build_modify_expr (inner_lhs, NOP_EXPR, | |
3686 | convert (TREE_TYPE (inner_lhs), | |
3687 | convert (lhstype, newrhs))); | |
19d76e60 RK |
3688 | if (TREE_CODE (result) == ERROR_MARK) |
3689 | return result; | |
400fbf9f JW |
3690 | pedantic_lvalue_warning (CONVERT_EXPR); |
3691 | return convert (TREE_TYPE (lhs), result); | |
3692 | } | |
3693 | } | |
3694 | ||
3695 | /* Now we have handled acceptable kinds of LHS that are not truly lvalues. | |
3696 | Reject anything strange now. */ | |
3697 | ||
3698 | if (!lvalue_or_else (lhs, "assignment")) | |
3699 | return error_mark_node; | |
3700 | ||
3701 | /* Warn about storing in something that is `const'. */ | |
3702 | ||
3703 | if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype) | |
3704 | || ((TREE_CODE (lhstype) == RECORD_TYPE | |
3705 | || TREE_CODE (lhstype) == UNION_TYPE) | |
3706 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
3707 | readonly_warning (lhs, "assignment"); | |
3708 | ||
3709 | /* If storing into a structure or union member, | |
3710 | it has probably been given type `int'. | |
3711 | Compute the type that would go with | |
3712 | the actual amount of storage the member occupies. */ | |
3713 | ||
3714 | if (TREE_CODE (lhs) == COMPONENT_REF | |
3715 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
3716 | || TREE_CODE (lhstype) == REAL_TYPE | |
3717 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
3718 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
3719 | ||
3720 | /* If storing in a field that is in actuality a short or narrower than one, | |
3721 | we must store in the field in its actual type. */ | |
3722 | ||
3723 | if (lhstype != TREE_TYPE (lhs)) | |
3724 | { | |
3725 | lhs = copy_node (lhs); | |
3726 | TREE_TYPE (lhs) = lhstype; | |
3727 | } | |
3728 | ||
3729 | /* Convert new value to destination type. */ | |
3730 | ||
3731 | newrhs = convert_for_assignment (lhstype, newrhs, "assignment", | |
9b7267b8 | 3732 | NULL_TREE, NULL_TREE, 0); |
400fbf9f JW |
3733 | if (TREE_CODE (newrhs) == ERROR_MARK) |
3734 | return error_mark_node; | |
3735 | ||
3736 | result = build (MODIFY_EXPR, lhstype, lhs, newrhs); | |
3737 | TREE_SIDE_EFFECTS (result) = 1; | |
3738 | ||
3739 | /* If we got the LHS in a different type for storing in, | |
3740 | convert the result back to the nominal type of LHS | |
3741 | so that the value we return always has the same type | |
3742 | as the LHS argument. */ | |
3743 | ||
3744 | if (olhstype == TREE_TYPE (result)) | |
3745 | return result; | |
9b7267b8 RS |
3746 | return convert_for_assignment (olhstype, result, "assignment", |
3747 | NULL_TREE, NULL_TREE, 0); | |
400fbf9f JW |
3748 | } |
3749 | \f | |
3750 | /* Convert value RHS to type TYPE as preparation for an assignment | |
3751 | to an lvalue of type TYPE. | |
3752 | The real work of conversion is done by `convert'. | |
3753 | The purpose of this function is to generate error messages | |
3754 | for assignments that are not allowed in C. | |
3755 | ERRTYPE is a string to use in error messages: | |
3756 | "assignment", "return", etc. If it is null, this is parameter passing | |
d45cf215 RS |
3757 | for a function call (and different error messages are output). Otherwise, |
3758 | it may be a name stored in the spelling stack and interpreted by | |
3759 | get_spelling. | |
400fbf9f JW |
3760 | |
3761 | FUNNAME is the name of the function being called, | |
3762 | as an IDENTIFIER_NODE, or null. | |
3763 | PARMNUM is the number of the argument, for printing in error messages. */ | |
3764 | ||
3765 | static tree | |
9b7267b8 | 3766 | convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum) |
400fbf9f JW |
3767 | tree type, rhs; |
3768 | char *errtype; | |
9b7267b8 | 3769 | tree fundecl, funname; |
400fbf9f JW |
3770 | int parmnum; |
3771 | { | |
3772 | register enum tree_code codel = TREE_CODE (type); | |
3773 | register tree rhstype; | |
3774 | register enum tree_code coder; | |
3775 | ||
3776 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
3777 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
3778 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f JW |
3779 | if (TREE_CODE (rhs) == NON_LVALUE_EXPR) |
3780 | rhs = TREE_OPERAND (rhs, 0); | |
3781 | ||
3782 | if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE | |
3783 | || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE) | |
3784 | rhs = default_conversion (rhs); | |
3785 | ||
3786 | rhstype = TREE_TYPE (rhs); | |
3787 | coder = TREE_CODE (rhstype); | |
3788 | ||
3789 | if (coder == ERROR_MARK) | |
3790 | return error_mark_node; | |
3791 | ||
3792 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) | |
e58cd767 RS |
3793 | { |
3794 | overflow_warning (rhs); | |
8b40563c TW |
3795 | /* Check for Objective-C protocols. This will issue a warning if |
3796 | there are protocol violations. No need to use the return value. */ | |
3797 | maybe_objc_comptypes (type, rhstype, 0); | |
e58cd767 RS |
3798 | return rhs; |
3799 | } | |
400fbf9f JW |
3800 | |
3801 | if (coder == VOID_TYPE) | |
3802 | { | |
3803 | error ("void value not ignored as it ought to be"); | |
3804 | return error_mark_node; | |
3805 | } | |
3806 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
b6a10c9f RS |
3807 | if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE |
3808 | || codel == COMPLEX_TYPE) | |
400fbf9f | 3809 | && |
b6a10c9f | 3810 | (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE |
b037a230 | 3811 | || coder == COMPLEX_TYPE)) |
da3c6115 | 3812 | return convert_and_check (type, rhs); |
9b7267b8 | 3813 | /* Conversion to a union from its member types. */ |
db9e5545 | 3814 | else if (codel == UNION_TYPE) |
9b7267b8 RS |
3815 | { |
3816 | tree memb_types; | |
3817 | for (memb_types = TYPE_FIELDS (type); memb_types; | |
3818 | memb_types = TREE_CHAIN (memb_types)) | |
3819 | { | |
3820 | if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs))) | |
3821 | { | |
3822 | if (pedantic | |
3823 | && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl))) | |
3824 | pedwarn ("ANSI C prohibits argument conversion to union type"); | |
3825 | return build1 (NOP_EXPR, type, rhs); | |
3826 | } | |
2df34974 RS |
3827 | else if (coder == POINTER_TYPE |
3828 | && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE) | |
3829 | { | |
3830 | tree memb_type = TREE_TYPE (memb_types); | |
3831 | register tree ttl = TREE_TYPE (memb_type); | |
3832 | register tree ttr = TREE_TYPE (rhstype); | |
3833 | ||
3834 | /* Any non-function converts to a [const][volatile] void * | |
3835 | and vice versa; otherwise, targets must be the same. | |
3836 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
3837 | if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
3838 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
3839 | || comp_target_types (memb_type, rhstype)) | |
3840 | { | |
3841 | /* Const and volatile mean something different for function types, | |
3842 | so the usual warnings are not appropriate. */ | |
3843 | if (TREE_CODE (ttr) != FUNCTION_TYPE | |
3844 | || TREE_CODE (ttl) != FUNCTION_TYPE) | |
3845 | { | |
3846 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) | |
3847 | warn_for_assignment ("%s discards `const' from pointer target type", | |
3848 | get_spelling (errtype), funname, parmnum); | |
3849 | if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) | |
3850 | warn_for_assignment ("%s discards `volatile' from pointer target type", | |
3851 | get_spelling (errtype), funname, parmnum); | |
3852 | } | |
3853 | else | |
3854 | { | |
3855 | /* Because const and volatile on functions are restrictions | |
3856 | that say the function will not do certain things, | |
3857 | it is okay to use a const or volatile function | |
3858 | where an ordinary one is wanted, but not vice-versa. */ | |
3859 | if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) | |
3860 | warn_for_assignment ("%s makes `const *' function pointer from non-const", | |
3861 | get_spelling (errtype), funname, parmnum); | |
3862 | if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) | |
3863 | warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", | |
3864 | get_spelling (errtype), funname, parmnum); | |
3865 | } | |
3866 | if (pedantic | |
3867 | && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl))) | |
3868 | pedwarn ("ANSI C prohibits argument conversion to union type"); | |
3869 | return build1 (NOP_EXPR, type, rhs); | |
3870 | } | |
3871 | } | |
9b7267b8 RS |
3872 | } |
3873 | } | |
400fbf9f JW |
3874 | /* Conversions among pointers */ |
3875 | else if (codel == POINTER_TYPE && coder == POINTER_TYPE) | |
3876 | { | |
3877 | register tree ttl = TREE_TYPE (type); | |
3878 | register tree ttr = TREE_TYPE (rhstype); | |
3879 | ||
3880 | /* Any non-function converts to a [const][volatile] void * | |
3881 | and vice versa; otherwise, targets must be the same. | |
3882 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
3883 | if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
3884 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
790e9490 RS |
3885 | || comp_target_types (type, rhstype) |
3886 | || (unsigned_type (TYPE_MAIN_VARIANT (ttl)) | |
3887 | == unsigned_type (TYPE_MAIN_VARIANT (ttr)))) | |
400fbf9f JW |
3888 | { |
3889 | if (pedantic | |
3890 | && ((TYPE_MAIN_VARIANT (ttl) == void_type_node | |
3891 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
3892 | || | |
3893 | (TYPE_MAIN_VARIANT (ttr) == void_type_node | |
fd5d5b94 RS |
3894 | /* Check TREE_CODE to catch cases like (void *) (char *) 0 |
3895 | which are not ANSI null ptr constants. */ | |
3896 | && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR) | |
400fbf9f JW |
3897 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
3898 | warn_for_assignment ("ANSI forbids %s between function pointer and `void *'", | |
d45cf215 | 3899 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3900 | /* Const and volatile mean something different for function types, |
3901 | so the usual warnings are not appropriate. */ | |
3902 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
3903 | || TREE_CODE (ttl) != FUNCTION_TYPE) | |
3904 | { | |
3905 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) | |
3906 | warn_for_assignment ("%s discards `const' from pointer target type", | |
d45cf215 | 3907 | get_spelling (errtype), funname, parmnum); |
790e9490 | 3908 | else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) |
400fbf9f | 3909 | warn_for_assignment ("%s discards `volatile' from pointer target type", |
d45cf215 | 3910 | get_spelling (errtype), funname, parmnum); |
790e9490 RS |
3911 | /* If this is not a case of ignoring a mismatch in signedness, |
3912 | no warning. */ | |
3913 | else if (TYPE_MAIN_VARIANT (ttl) == void_type_node | |
3914 | || TYPE_MAIN_VARIANT (ttr) == void_type_node | |
3915 | || comp_target_types (type, rhstype)) | |
3916 | ; | |
3917 | /* If there is a mismatch, do warn. */ | |
3918 | else if (pedantic) | |
3919 | warn_for_assignment ("pointer targets in %s differ in signedness", | |
3920 | get_spelling (errtype), funname, parmnum); | |
400fbf9f JW |
3921 | } |
3922 | else | |
3923 | { | |
3924 | /* Because const and volatile on functions are restrictions | |
3925 | that say the function will not do certain things, | |
3926 | it is okay to use a const or volatile function | |
3927 | where an ordinary one is wanted, but not vice-versa. */ | |
3928 | if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) | |
3929 | warn_for_assignment ("%s makes `const *' function pointer from non-const", | |
d45cf215 | 3930 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3931 | if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) |
3932 | warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", | |
d45cf215 | 3933 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3934 | } |
3935 | } | |
400fbf9f JW |
3936 | else |
3937 | warn_for_assignment ("%s from incompatible pointer type", | |
d45cf215 | 3938 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3939 | return convert (type, rhs); |
3940 | } | |
3941 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) | |
3942 | { | |
2918ed3c | 3943 | /* An explicit constant 0 can convert to a pointer, |
f1a2b955 RS |
3944 | or one that results from arithmetic, even including |
3945 | a cast to integer type. */ | |
3946 | if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)) | |
3947 | && | |
3948 | ! (TREE_CODE (rhs) == NOP_EXPR | |
3949 | && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE | |
3950 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST | |
3951 | && integer_zerop (TREE_OPERAND (rhs, 0)))) | |
400fbf9f JW |
3952 | { |
3953 | warn_for_assignment ("%s makes pointer from integer without a cast", | |
d45cf215 | 3954 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3955 | return convert (type, rhs); |
3956 | } | |
3957 | return null_pointer_node; | |
3958 | } | |
3959 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) | |
3960 | { | |
3961 | warn_for_assignment ("%s makes integer from pointer without a cast", | |
d45cf215 | 3962 | get_spelling (errtype), funname, parmnum); |
400fbf9f JW |
3963 | return convert (type, rhs); |
3964 | } | |
3965 | ||
3966 | if (!errtype) | |
3967 | { | |
3968 | if (funname) | |
8b40563c TW |
3969 | { |
3970 | tree selector = maybe_building_objc_message_expr (); | |
3971 | ||
3972 | if (selector && parmnum > 2) | |
3973 | error ("incompatible type for argument %d of `%s'", | |
3974 | parmnum - 2, IDENTIFIER_POINTER (selector)); | |
3975 | else | |
3976 | error ("incompatible type for argument %d of `%s'", | |
3977 | parmnum, IDENTIFIER_POINTER (funname)); | |
3978 | } | |
400fbf9f JW |
3979 | else |
3980 | error ("incompatible type for argument %d of indirect function call", | |
3981 | parmnum); | |
3982 | } | |
3983 | else | |
d45cf215 | 3984 | error ("incompatible types in %s", get_spelling (errtype)); |
400fbf9f JW |
3985 | |
3986 | return error_mark_node; | |
3987 | } | |
3988 | ||
3989 | /* Print a warning using MSG. | |
3990 | It gets OPNAME as its one parameter. | |
3991 | If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'". | |
3992 | FUNCTION and ARGNUM are handled specially if we are building an | |
3993 | Objective-C selector. */ | |
3994 | ||
3995 | static void | |
3996 | warn_for_assignment (msg, opname, function, argnum) | |
3997 | char *msg; | |
3998 | char *opname; | |
3999 | tree function; | |
4000 | int argnum; | |
4001 | { | |
4002 | static char argstring[] = "passing arg %d of `%s'"; | |
4003 | static char argnofun[] = "passing arg %d"; | |
4004 | ||
4005 | if (opname == 0) | |
4006 | { | |
4007 | tree selector = maybe_building_objc_message_expr (); | |
4008 | ||
4009 | if (selector && argnum > 2) | |
4010 | { | |
4011 | function = selector; | |
4012 | argnum -= 2; | |
4013 | } | |
4014 | if (function) | |
4015 | { | |
4016 | /* Function name is known; supply it. */ | |
4017 | opname = (char *) alloca (IDENTIFIER_LENGTH (function) | |
4018 | + sizeof (argstring) + 25 /*%d*/ + 1); | |
4019 | sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function)); | |
4020 | } | |
4021 | else | |
4022 | { | |
4023 | /* Function name unknown (call through ptr); just give arg number. */ | |
4024 | opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1); | |
4025 | sprintf (opname, argnofun, argnum); | |
4026 | } | |
4027 | } | |
4028 | pedwarn (msg, opname); | |
4029 | } | |
4030 | \f | |
4031 | /* Return nonzero if VALUE is a valid constant-valued expression | |
4032 | for use in initializing a static variable; one that can be an | |
4033 | element of a "constant" initializer. | |
4034 | ||
4035 | Return null_pointer_node if the value is absolute; | |
4036 | if it is relocatable, return the variable that determines the relocation. | |
4037 | We assume that VALUE has been folded as much as possible; | |
4038 | therefore, we do not need to check for such things as | |
4039 | arithmetic-combinations of integers. */ | |
4040 | ||
ca1f6b57 | 4041 | tree |
f0c70ef0 | 4042 | initializer_constant_valid_p (value, endtype) |
400fbf9f | 4043 | tree value; |
f0c70ef0 | 4044 | tree endtype; |
400fbf9f JW |
4045 | { |
4046 | switch (TREE_CODE (value)) | |
4047 | { | |
4048 | case CONSTRUCTOR: | |
281ec92f RS |
4049 | if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE |
4050 | && TREE_CONSTANT (value)) | |
75ddf8b0 RK |
4051 | return |
4052 | initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)), | |
4053 | endtype); | |
281ec92f | 4054 | |
400fbf9f JW |
4055 | return TREE_STATIC (value) ? null_pointer_node : 0; |
4056 | ||
4057 | case INTEGER_CST: | |
4058 | case REAL_CST: | |
4059 | case STRING_CST: | |
466e9220 | 4060 | case COMPLEX_CST: |
400fbf9f JW |
4061 | return null_pointer_node; |
4062 | ||
4063 | case ADDR_EXPR: | |
4064 | return TREE_OPERAND (value, 0); | |
4065 | ||
4066 | case NON_LVALUE_EXPR: | |
f0c70ef0 | 4067 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); |
400fbf9f JW |
4068 | |
4069 | case CONVERT_EXPR: | |
4070 | case NOP_EXPR: | |
4071 | /* Allow conversions between pointer types. */ | |
4072 | if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE | |
4073 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE) | |
f0c70ef0 | 4074 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); |
400fbf9f JW |
4075 | /* Allow conversions between real types. */ |
4076 | if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE | |
4077 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE) | |
f0c70ef0 | 4078 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); |
400fbf9f JW |
4079 | /* Allow length-preserving conversions between integer types. */ |
4080 | if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE | |
4081 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE | |
4082 | && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)), | |
4083 | TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0))))) | |
f0c70ef0 | 4084 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); |
400fbf9f JW |
4085 | /* Allow conversions between integer types only if explicit value. */ |
4086 | if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE | |
4087 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE) | |
4088 | { | |
f0c70ef0 RS |
4089 | tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0), |
4090 | endtype); | |
400fbf9f JW |
4091 | if (inner == null_pointer_node) |
4092 | return null_pointer_node; | |
4093 | return 0; | |
4094 | } | |
9b7267b8 | 4095 | /* Allow (int) &foo provided int is as wide as a pointer. */ |
400fbf9f JW |
4096 | if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE |
4097 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE | |
9b7267b8 RS |
4098 | && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)), |
4099 | TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0))))) | |
f0c70ef0 RS |
4100 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), |
4101 | endtype); | |
805f961c RS |
4102 | /* Allow conversions to union types if the value inside is okay. */ |
4103 | if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE) | |
f0c70ef0 RS |
4104 | return initializer_constant_valid_p (TREE_OPERAND (value, 0), |
4105 | endtype); | |
400fbf9f JW |
4106 | return 0; |
4107 | ||
4108 | case PLUS_EXPR: | |
1bbe9280 RS |
4109 | if (TREE_CODE (endtype) == INTEGER_TYPE |
4110 | && TYPE_PRECISION (endtype) < POINTER_SIZE) | |
f0c70ef0 | 4111 | return 0; |
400fbf9f | 4112 | { |
f0c70ef0 RS |
4113 | tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), |
4114 | endtype); | |
4115 | tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), | |
4116 | endtype); | |
400fbf9f JW |
4117 | /* If either term is absolute, use the other terms relocation. */ |
4118 | if (valid0 == null_pointer_node) | |
4119 | return valid1; | |
4120 | if (valid1 == null_pointer_node) | |
4121 | return valid0; | |
4122 | return 0; | |
4123 | } | |
4124 | ||
4125 | case MINUS_EXPR: | |
1bbe9280 RS |
4126 | if (TREE_CODE (endtype) == INTEGER_TYPE |
4127 | && TYPE_PRECISION (endtype) < POINTER_SIZE) | |
f0c70ef0 | 4128 | return 0; |
400fbf9f | 4129 | { |
f0c70ef0 RS |
4130 | tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), |
4131 | endtype); | |
4132 | tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), | |
4133 | endtype); | |
400fbf9f JW |
4134 | /* Win if second argument is absolute. */ |
4135 | if (valid1 == null_pointer_node) | |
4136 | return valid0; | |
4137 | /* Win if both arguments have the same relocation. | |
4138 | Then the value is absolute. */ | |
4139 | if (valid0 == valid1) | |
4140 | return null_pointer_node; | |
4141 | return 0; | |
4142 | } | |
4143 | } | |
4144 | ||
4145 | return 0; | |
4146 | } | |
d9fc6069 JW |
4147 | |
4148 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
4149 | return its value. Otherwise, return error_mark_node. | |
4150 | ||
4151 | This is for handling COMPOUND_EXPRs as initializer elements | |
4152 | which is allowed with a warning when -pedantic is specified. */ | |
4153 | ||
4154 | static tree | |
4155 | valid_compound_expr_initializer (value, endtype) | |
4156 | tree value; | |
4157 | tree endtype; | |
4158 | { | |
4159 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
4160 | { | |
4161 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
4162 | == error_mark_node) | |
4163 | return error_mark_node; | |
4164 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
4165 | endtype); | |
4166 | } | |
4167 | else if (! TREE_CONSTANT (value) | |
4168 | && ! initializer_constant_valid_p (value, endtype)) | |
4169 | return error_mark_node; | |
4170 | else | |
4171 | return value; | |
4172 | } | |
400fbf9f JW |
4173 | \f |
4174 | /* Perform appropriate conversions on the initial value of a variable, | |
4175 | store it in the declaration DECL, | |
4176 | and print any error messages that are appropriate. | |
4177 | If the init is invalid, store an ERROR_MARK. */ | |
4178 | ||
4179 | void | |
4180 | store_init_value (decl, init) | |
4181 | tree decl, init; | |
4182 | { | |
4183 | register tree value, type; | |
4184 | ||
4185 | /* If variable's type was invalidly declared, just ignore it. */ | |
4186 | ||
4187 | type = TREE_TYPE (decl); | |
4188 | if (TREE_CODE (type) == ERROR_MARK) | |
4189 | return; | |
4190 | ||
4191 | /* Digest the specified initializer into an expression. */ | |
4192 | ||
790e9490 RS |
4193 | value = digest_init (type, init, TREE_STATIC (decl), |
4194 | TREE_STATIC (decl) || pedantic); | |
400fbf9f JW |
4195 | |
4196 | /* Store the expression if valid; else report error. */ | |
4197 | ||
4198 | #if 0 | |
4199 | /* Note that this is the only place we can detect the error | |
4200 | in a case such as struct foo bar = (struct foo) { x, y }; | |
d45cf215 | 4201 | where there is one initial value which is a constructor expression. */ |
400fbf9f JW |
4202 | if (value == error_mark_node) |
4203 | ; | |
4204 | else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value)) | |
4205 | { | |
4206 | error ("initializer for static variable is not constant"); | |
4207 | value = error_mark_node; | |
4208 | } | |
4209 | else if (TREE_STATIC (decl) | |
f0c70ef0 | 4210 | && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) |
400fbf9f JW |
4211 | { |
4212 | error ("initializer for static variable uses complicated arithmetic"); | |
4213 | value = error_mark_node; | |
4214 | } | |
4215 | else | |
4216 | { | |
4217 | if (pedantic && TREE_CODE (value) == CONSTRUCTOR) | |
4218 | { | |
4219 | if (! TREE_CONSTANT (value)) | |
4220 | pedwarn ("aggregate initializer is not constant"); | |
4221 | else if (! TREE_STATIC (value)) | |
4222 | pedwarn ("aggregate initializer uses complicated arithmetic"); | |
4223 | } | |
4224 | } | |
4225 | #endif | |
4226 | ||
10d5caec PE |
4227 | DECL_INITIAL (decl) = value; |
4228 | ||
26b3c423 | 4229 | /* ANSI wants warnings about out-of-range constant initializers. */ |
10d5caec | 4230 | STRIP_TYPE_NOPS (value); |
26b3c423 | 4231 | constant_expression_warning (value); |
400fbf9f JW |
4232 | } |
4233 | \f | |
075fc632 | 4234 | /* Methods for storing and printing names for error messages. */ |
d45cf215 RS |
4235 | |
4236 | /* Implement a spelling stack that allows components of a name to be pushed | |
4237 | and popped. Each element on the stack is this structure. */ | |
4238 | ||
4239 | struct spelling | |
4240 | { | |
4241 | int kind; | |
4242 | union | |
4243 | { | |
4244 | int i; | |
4245 | char *s; | |
4246 | } u; | |
4247 | }; | |
4248 | ||
4249 | #define SPELLING_STRING 1 | |
4250 | #define SPELLING_MEMBER 2 | |
4251 | #define SPELLING_BOUNDS 3 | |
4252 | ||
4253 | static struct spelling *spelling; /* Next stack element (unused). */ | |
4254 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
4255 | static int spelling_size; /* Size of the spelling stack. */ | |
4256 | ||
4257 | /* Macros to save and restore the spelling stack around push_... functions. | |
4258 | Alternative to SAVE_SPELLING_STACK. */ | |
4259 | ||
4260 | #define SPELLING_DEPTH() (spelling - spelling_base) | |
4261 | #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth) | |
4262 | ||
4263 | /* Save and restore the spelling stack around arbitrary C code. */ | |
4264 | ||
4265 | #define SAVE_SPELLING_DEPTH(code) \ | |
4266 | { \ | |
4267 | int __depth = SPELLING_DEPTH (); \ | |
4268 | code; \ | |
4269 | RESTORE_SPELLING_DEPTH (__depth); \ | |
4270 | } | |
4271 | ||
4272 | /* Push an element on the spelling stack with type KIND and assign VALUE | |
4273 | to MEMBER. */ | |
4274 | ||
4275 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ | |
4276 | { \ | |
4277 | int depth = SPELLING_DEPTH (); \ | |
4278 | \ | |
4279 | if (depth >= spelling_size) \ | |
4280 | { \ | |
4281 | spelling_size += 10; \ | |
4282 | if (spelling_base == 0) \ | |
4283 | spelling_base \ | |
4284 | = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \ | |
4285 | else \ | |
4286 | spelling_base \ | |
4287 | = (struct spelling *) xrealloc (spelling_base, \ | |
4288 | spelling_size * sizeof (struct spelling)); \ | |
4289 | RESTORE_SPELLING_DEPTH (depth); \ | |
4290 | } \ | |
4291 | \ | |
4292 | spelling->kind = (KIND); \ | |
4293 | spelling->MEMBER = (VALUE); \ | |
4294 | spelling++; \ | |
4295 | } | |
4296 | ||
4297 | /* Push STRING on the stack. Printed literally. */ | |
4298 | ||
4299 | static void | |
4300 | push_string (string) | |
4301 | char *string; | |
4302 | { | |
4303 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
4304 | } | |
4305 | ||
4306 | /* Push a member name on the stack. Printed as '.' STRING. */ | |
4307 | ||
4308 | static void | |
19d76e60 RK |
4309 | push_member_name (decl) |
4310 | tree decl; | |
4311 | ||
d45cf215 | 4312 | { |
19d76e60 RK |
4313 | char *string |
4314 | = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>"; | |
d45cf215 RS |
4315 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
4316 | } | |
4317 | ||
4318 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ | |
4319 | ||
4320 | static void | |
4321 | push_array_bounds (bounds) | |
4322 | int bounds; | |
4323 | { | |
4324 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
4325 | } | |
4326 | ||
4327 | /* Compute the maximum size in bytes of the printed spelling. */ | |
4328 | ||
4329 | static int | |
4330 | spelling_length () | |
4331 | { | |
4332 | register int size = 0; | |
4333 | register struct spelling *p; | |
4334 | ||
4335 | for (p = spelling_base; p < spelling; p++) | |
4336 | { | |
4337 | if (p->kind == SPELLING_BOUNDS) | |
4338 | size += 25; | |
4339 | else | |
4340 | size += strlen (p->u.s) + 1; | |
4341 | } | |
4342 | ||
4343 | return size; | |
4344 | } | |
4345 | ||
4346 | /* Print the spelling to BUFFER and return it. */ | |
4347 | ||
4348 | static char * | |
4349 | print_spelling (buffer) | |
4350 | register char *buffer; | |
4351 | { | |
4352 | register char *d = buffer; | |
4353 | register char *s; | |
4354 | register struct spelling *p; | |
4355 | ||
4356 | for (p = spelling_base; p < spelling; p++) | |
4357 | if (p->kind == SPELLING_BOUNDS) | |
4358 | { | |
4359 | sprintf (d, "[%d]", p->u.i); | |
4360 | d += strlen (d); | |
4361 | } | |
4362 | else | |
4363 | { | |
4364 | if (p->kind == SPELLING_MEMBER) | |
4365 | *d++ = '.'; | |
4366 | for (s = p->u.s; *d = *s++; d++) | |
4367 | ; | |
4368 | } | |
4369 | *d++ = '\0'; | |
4370 | return buffer; | |
4371 | } | |
4372 | ||
4373 | /* Provide a means to pass component names derived from the spelling stack. */ | |
4374 | ||
4375 | char initialization_message; | |
4376 | ||
4377 | /* Interpret the spelling of the given ERRTYPE message. */ | |
4378 | ||
4379 | static char * | |
4380 | get_spelling (errtype) | |
4381 | char *errtype; | |
4382 | { | |
4383 | static char *buffer; | |
4384 | static int size = -1; | |
4385 | ||
4386 | if (errtype == &initialization_message) | |
4387 | { | |
4388 | /* Avoid counting chars */ | |
4389 | static char message[] = "initialization of `%s'"; | |
4390 | register int needed = sizeof (message) + spelling_length () + 1; | |
047de90b | 4391 | char *temp; |
d45cf215 RS |
4392 | |
4393 | if (size < 0) | |
4394 | buffer = (char *) xmalloc (size = needed); | |
4395 | if (needed > size) | |
4396 | buffer = (char *) xrealloc (buffer, size = needed); | |
4397 | ||
047de90b RS |
4398 | temp = (char *) alloca (needed); |
4399 | sprintf (buffer, message, print_spelling (temp)); | |
d45cf215 RS |
4400 | return buffer; |
4401 | } | |
4402 | ||
4403 | return errtype; | |
4404 | } | |
4405 | ||
400fbf9f JW |
4406 | /* Issue an error message for a bad initializer component. |
4407 | FORMAT describes the message. OFWHAT is the name for the component. | |
4408 | LOCAL is a format string for formatting the insertion of the name | |
4409 | into the message. | |
4410 | ||
d45cf215 | 4411 | If OFWHAT is null, the component name is stored on the spelling stack. |
6dc42e49 | 4412 | If the component name is a null string, then LOCAL is omitted entirely. */ |
400fbf9f JW |
4413 | |
4414 | void | |
4415 | error_init (format, local, ofwhat) | |
4416 | char *format, *local, *ofwhat; | |
4417 | { | |
d45cf215 RS |
4418 | char *buffer; |
4419 | ||
4420 | if (ofwhat == 0) | |
73a424d3 | 4421 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d45cf215 | 4422 | buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); |
400fbf9f JW |
4423 | |
4424 | if (*ofwhat) | |
4425 | sprintf (buffer, local, ofwhat); | |
4426 | else | |
4427 | buffer[0] = 0; | |
4428 | ||
4429 | error (format, buffer); | |
4430 | } | |
4431 | ||
4432 | /* Issue a pedantic warning for a bad initializer component. | |
4433 | FORMAT describes the message. OFWHAT is the name for the component. | |
4434 | LOCAL is a format string for formatting the insertion of the name | |
4435 | into the message. | |
4436 | ||
d45cf215 | 4437 | If OFWHAT is null, the component name is stored on the spelling stack. |
6dc42e49 | 4438 | If the component name is a null string, then LOCAL is omitted entirely. */ |
400fbf9f JW |
4439 | |
4440 | void | |
4441 | pedwarn_init (format, local, ofwhat) | |
4442 | char *format, *local, *ofwhat; | |
4443 | { | |
d45cf215 RS |
4444 | char *buffer; |
4445 | ||
4446 | if (ofwhat == 0) | |
73a424d3 | 4447 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d45cf215 | 4448 | buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); |
400fbf9f JW |
4449 | |
4450 | if (*ofwhat) | |
4451 | sprintf (buffer, local, ofwhat); | |
4452 | else | |
4453 | buffer[0] = 0; | |
4454 | ||
4455 | pedwarn (format, buffer); | |
4456 | } | |
b71c7f8a RK |
4457 | |
4458 | /* Issue a warning for a bad initializer component. | |
4459 | FORMAT describes the message. OFWHAT is the name for the component. | |
4460 | LOCAL is a format string for formatting the insertion of the name | |
4461 | into the message. | |
4462 | ||
4463 | If OFWHAT is null, the component name is stored on the spelling stack. | |
4464 | If the component name is a null string, then LOCAL is omitted entirely. */ | |
4465 | ||
4466 | static void | |
4467 | warning_init (format, local, ofwhat) | |
4468 | char *format, *local, *ofwhat; | |
4469 | { | |
4470 | char *buffer; | |
4471 | ||
4472 | if (ofwhat == 0) | |
4473 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); | |
4474 | buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); | |
4475 | ||
4476 | if (*ofwhat) | |
4477 | sprintf (buffer, local, ofwhat); | |
4478 | else | |
4479 | buffer[0] = 0; | |
4480 | ||
4481 | warning (format, buffer); | |
4482 | } | |
400fbf9f JW |
4483 | \f |
4484 | /* Digest the parser output INIT as an initializer for type TYPE. | |
4485 | Return a C expression of type TYPE to represent the initial value. | |
4486 | ||
400fbf9f JW |
4487 | The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors |
4488 | if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT | |
59b22f64 | 4489 | applies only to elements of constructors. */ |
400fbf9f | 4490 | |
b62acd60 | 4491 | static tree |
790e9490 RS |
4492 | digest_init (type, init, require_constant, constructor_constant) |
4493 | tree type, init; | |
400fbf9f | 4494 | int require_constant, constructor_constant; |
400fbf9f JW |
4495 | { |
4496 | enum tree_code code = TREE_CODE (type); | |
047de90b | 4497 | tree inside_init = init; |
400fbf9f | 4498 | |
400fbf9f JW |
4499 | if (init == error_mark_node) |
4500 | return init; | |
4501 | ||
4502 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
fc76e425 RS |
4503 | /* Do not use STRIP_NOPS here. We do not want an enumerator |
4504 | whose value is 0 to count as a null pointer constant. */ | |
400fbf9f | 4505 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
047de90b | 4506 | inside_init = TREE_OPERAND (init, 0); |
400fbf9f | 4507 | |
400fbf9f JW |
4508 | /* Initialization of an array of chars from a string constant |
4509 | optionally enclosed in braces. */ | |
4510 | ||
4511 | if (code == ARRAY_TYPE) | |
4512 | { | |
4513 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
4514 | if ((typ1 == char_type_node | |
4515 | || typ1 == signed_char_type_node | |
4516 | || typ1 == unsigned_char_type_node | |
4517 | || typ1 == unsigned_wchar_type_node | |
4518 | || typ1 == signed_wchar_type_node) | |
fd5d5b94 | 4519 | && ((inside_init && TREE_CODE (inside_init) == STRING_CST))) |
400fbf9f | 4520 | { |
4d65300e RS |
4521 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
4522 | TYPE_MAIN_VARIANT (type))) | |
fd5d5b94 | 4523 | return inside_init; |
d11fdb45 | 4524 | |
fd5d5b94 | 4525 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) |
400fbf9f JW |
4526 | != char_type_node) |
4527 | && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node)) | |
4528 | { | |
4529 | error_init ("char-array%s initialized from wide string", | |
de520661 | 4530 | " `%s'", NULL); |
400fbf9f JW |
4531 | return error_mark_node; |
4532 | } | |
fd5d5b94 | 4533 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) |
400fbf9f JW |
4534 | == char_type_node) |
4535 | && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)) | |
4536 | { | |
4537 | error_init ("int-array%s initialized from non-wide string", | |
de520661 | 4538 | " `%s'", NULL); |
400fbf9f JW |
4539 | return error_mark_node; |
4540 | } | |
4541 | ||
fd5d5b94 | 4542 | TREE_TYPE (inside_init) = type; |
400fbf9f JW |
4543 | if (TYPE_DOMAIN (type) != 0 |
4544 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) | |
4545 | { | |
4546 | register int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); | |
4547 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; | |
fe9ef5d7 RS |
4548 | /* Subtract 1 (or sizeof (wchar_t)) |
4549 | because it's ok to ignore the terminating null char | |
400fbf9f | 4550 | that is counted in the length of the constant. */ |
fd5d5b94 | 4551 | if (size < TREE_STRING_LENGTH (inside_init) |
fe9ef5d7 RS |
4552 | - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node) |
4553 | ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT | |
4554 | : 1)) | |
400fbf9f JW |
4555 | pedwarn_init ( |
4556 | "initializer-string for array of chars%s is too long", | |
de520661 | 4557 | " `%s'", NULL); |
400fbf9f | 4558 | } |
fd5d5b94 | 4559 | return inside_init; |
400fbf9f JW |
4560 | } |
4561 | } | |
4562 | ||
de520661 RS |
4563 | /* Any type can be initialized |
4564 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 4565 | |
2726966d | 4566 | if (inside_init && TREE_TYPE (inside_init) != 0 |
5522c047 PB |
4567 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
4568 | TYPE_MAIN_VARIANT (type)) | |
2726966d | 4569 | || (code == ARRAY_TYPE |
3c3fa147 RS |
4570 | && comptypes (TREE_TYPE (inside_init), type)) |
4571 | || (code == POINTER_TYPE | |
3c3fa147 RS |
4572 | && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
4573 | || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE) | |
4574 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), | |
4575 | TREE_TYPE (type))))) | |
400fbf9f JW |
4576 | { |
4577 | if (code == POINTER_TYPE | |
047de90b RS |
4578 | && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
4579 | || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)) | |
4580 | inside_init = default_conversion (inside_init); | |
de520661 RS |
4581 | else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST |
4582 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f JW |
4583 | { |
4584 | error_init ("array%s initialized from non-constant array expression", | |
de520661 | 4585 | " `%s'", NULL); |
400fbf9f JW |
4586 | return error_mark_node; |
4587 | } | |
4588 | ||
047de90b RS |
4589 | if (optimize && TREE_READONLY (inside_init) |
4590 | && TREE_CODE (inside_init) == VAR_DECL) | |
4591 | inside_init = decl_constant_value (inside_init); | |
400fbf9f | 4592 | |
d9fc6069 JW |
4593 | /* Compound expressions can only occur here if -pedantic or |
4594 | -pedantic-errors is specified. In the later case, we always want | |
4595 | an error. In the former case, we simply want a warning. */ | |
4596 | if (require_constant && pedantic | |
4597 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
4598 | { | |
4599 | inside_init | |
4600 | = valid_compound_expr_initializer (inside_init, | |
4601 | TREE_TYPE (inside_init)); | |
4602 | if (inside_init == error_mark_node) | |
4603 | error_init ("initializer element%s is not constant", | |
4604 | " for `%s'", NULL); | |
4605 | else | |
4606 | pedwarn_init ("initializer element%s is not constant", | |
4607 | " for `%s'", NULL); | |
4608 | if (flag_pedantic_errors) | |
4609 | inside_init = error_mark_node; | |
4610 | } | |
4611 | else if (require_constant && ! TREE_CONSTANT (inside_init)) | |
400fbf9f JW |
4612 | { |
4613 | error_init ("initializer element%s is not constant", | |
de520661 | 4614 | " for `%s'", NULL); |
047de90b | 4615 | inside_init = error_mark_node; |
400fbf9f | 4616 | } |
f0c70ef0 RS |
4617 | else if (require_constant |
4618 | && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) | |
400fbf9f JW |
4619 | { |
4620 | error_init ("initializer element%s is not computable at load time", | |
de520661 | 4621 | " for `%s'", NULL); |
047de90b | 4622 | inside_init = error_mark_node; |
400fbf9f JW |
4623 | } |
4624 | ||
047de90b | 4625 | return inside_init; |
400fbf9f JW |
4626 | } |
4627 | ||
400fbf9f JW |
4628 | /* Handle scalar types, including conversions. */ |
4629 | ||
4630 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE | |
337633f9 | 4631 | || code == ENUMERAL_TYPE || code == COMPLEX_TYPE) |
400fbf9f | 4632 | { |
e3a12f0c RS |
4633 | /* Note that convert_for_assignment calls default_conversion |
4634 | for arrays and functions. We must not call it in the | |
4635 | case where inside_init is a null pointer constant. */ | |
4636 | inside_init | |
4637 | = convert_for_assignment (type, init, "initialization", | |
4638 | NULL_TREE, NULL_TREE, 0); | |
400fbf9f | 4639 | |
047de90b | 4640 | if (require_constant && ! TREE_CONSTANT (inside_init)) |
400fbf9f JW |
4641 | { |
4642 | error_init ("initializer element%s is not constant", | |
de520661 | 4643 | " for `%s'", NULL); |
047de90b | 4644 | inside_init = error_mark_node; |
400fbf9f | 4645 | } |
f0c70ef0 RS |
4646 | else if (require_constant |
4647 | && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) | |
400fbf9f JW |
4648 | { |
4649 | error_init ("initializer element%s is not computable at load time", | |
de520661 | 4650 | " for `%s'", NULL); |
047de90b | 4651 | inside_init = error_mark_node; |
400fbf9f JW |
4652 | } |
4653 | ||
047de90b | 4654 | return inside_init; |
400fbf9f JW |
4655 | } |
4656 | ||
4657 | /* Come here only for records and arrays. */ | |
4658 | ||
4659 | if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) | |
4660 | { | |
4661 | error_init ("variable-sized object%s may not be initialized", | |
de520661 | 4662 | " `%s'", NULL); |
400fbf9f JW |
4663 | return error_mark_node; |
4664 | } | |
4665 | ||
81a55c6c RS |
4666 | /* Traditionally, you can write struct foo x = 0; |
4667 | and it initializes the first element of x to 0. */ | |
4668 | if (flag_traditional) | |
4669 | { | |
4670 | tree top = 0, prev = 0; | |
4671 | while (TREE_CODE (type) == RECORD_TYPE | |
4672 | || TREE_CODE (type) == ARRAY_TYPE | |
4673 | || TREE_CODE (type) == QUAL_UNION_TYPE | |
4674 | || TREE_CODE (type) == UNION_TYPE) | |
4675 | { | |
4676 | tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); | |
4677 | if (prev == 0) | |
4678 | top = temp; | |
4679 | else | |
4680 | TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp); | |
4681 | prev = temp; | |
4682 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4683 | type = TREE_TYPE (type); | |
4684 | else if (TYPE_FIELDS (type)) | |
4685 | type = TREE_TYPE (TYPE_FIELDS (type)); | |
4686 | else | |
4687 | { | |
4688 | error_init ("invalid initializer%s", " for `%s'", NULL); | |
4689 | return error_mark_node; | |
4690 | } | |
4691 | } | |
4692 | TREE_OPERAND (prev, 1) | |
4693 | = build_tree_list (NULL_TREE, | |
4694 | digest_init (type, init, require_constant, | |
4695 | constructor_constant)); | |
4696 | return top; | |
4697 | } | |
de520661 | 4698 | error_init ("invalid initializer%s", " for `%s'", NULL); |
400fbf9f JW |
4699 | return error_mark_node; |
4700 | } | |
4701 | \f | |
de520661 | 4702 | /* Handle initializers that use braces. */ |
400fbf9f | 4703 | |
de520661 RS |
4704 | /* Type of object we are accumulating a constructor for. |
4705 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
4706 | static tree constructor_type; | |
400fbf9f | 4707 | |
de520661 RS |
4708 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
4709 | left to fill. */ | |
4710 | static tree constructor_fields; | |
400fbf9f | 4711 | |
de520661 RS |
4712 | /* For an ARRAY_TYPE, this is the specified index |
4713 | at which to store the next element we get. | |
4714 | This is a special INTEGER_CST node that we modify in place. */ | |
4715 | static tree constructor_index; | |
400fbf9f | 4716 | |
de520661 RS |
4717 | /* For an ARRAY_TYPE, this is the end index of the range |
4718 | to intitialize with the next element, or NULL in the ordinary case | |
4719 | where the element is used just once. */ | |
4720 | static tree constructor_range_end; | |
400fbf9f | 4721 | |
de520661 RS |
4722 | /* For an ARRAY_TYPE, this is the maximum index. */ |
4723 | static tree constructor_max_index; | |
103b7b17 | 4724 | |
de520661 RS |
4725 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
4726 | static tree constructor_unfilled_fields; | |
400fbf9f | 4727 | |
de520661 RS |
4728 | /* For an ARRAY_TYPE, this is the index of the first element |
4729 | not yet written out. | |
4730 | This is a special INTEGER_CST node that we modify in place. */ | |
4731 | static tree constructor_unfilled_index; | |
4732 | ||
b62acd60 RS |
4733 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
4734 | This is so we can generate gaps between fields, when appropriate. | |
4735 | This is a special INTEGER_CST node that we modify in place. */ | |
4736 | static tree constructor_bit_index; | |
4737 | ||
de520661 RS |
4738 | /* If we are saving up the elements rather than allocating them, |
4739 | this is the list of elements so far (in reverse order, | |
4740 | most recent first). */ | |
4741 | static tree constructor_elements; | |
4742 | ||
4743 | /* 1 if so far this constructor's elements are all compile-time constants. */ | |
4744 | static int constructor_constant; | |
4745 | ||
4746 | /* 1 if so far this constructor's elements are all valid address constants. */ | |
4747 | static int constructor_simple; | |
4748 | ||
4749 | /* 1 if this constructor is erroneous so far. */ | |
4750 | static int constructor_erroneous; | |
4751 | ||
4752 | /* 1 if have called defer_addressed_constants. */ | |
4753 | static int constructor_subconstants_deferred; | |
4754 | ||
4755 | /* List of pending elements at this constructor level. | |
4756 | These are elements encountered out of order | |
4757 | which belong at places we haven't reached yet in actually | |
4758 | writing the output. */ | |
4759 | static tree constructor_pending_elts; | |
4760 | ||
4761 | /* The SPELLING_DEPTH of this constructor. */ | |
4762 | static int constructor_depth; | |
4763 | ||
cc77d4d5 PB |
4764 | /* 0 if implicitly pushing constructor levels is allowed. */ |
4765 | int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */ | |
4766 | ||
de520661 RS |
4767 | /* 1 if this constructor level was entered implicitly. */ |
4768 | static int constructor_implicit; | |
4769 | ||
4770 | static int require_constant_value; | |
4771 | static int require_constant_elements; | |
4772 | ||
4773 | /* 1 if it is ok to output this constructor as we read it. | |
4774 | 0 means must accumulate a CONSTRUCTOR expression. */ | |
4775 | static int constructor_incremental; | |
4776 | ||
4777 | /* DECL node for which an initializer is being read. | |
4778 | 0 means we are reading a constructor expression | |
4779 | such as (struct foo) {...}. */ | |
4780 | static tree constructor_decl; | |
4781 | ||
4782 | /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */ | |
4783 | static char *constructor_asmspec; | |
4784 | ||
4785 | /* Nonzero if this is an initializer for a top-level decl. */ | |
4786 | static int constructor_top_level; | |
4787 | ||
4788 | /* When we finish reading a constructor expression | |
4789 | (constructor_decl is 0), the CONSTRUCTOR goes here. */ | |
4790 | static tree constructor_result; | |
b62acd60 RS |
4791 | \f |
4792 | /* This stack has a level for each implicit or explicit level of | |
4793 | structuring in the initializer, including the outermost one. It | |
4794 | saves the values of most of the variables above. */ | |
de520661 RS |
4795 | |
4796 | struct constructor_stack | |
400fbf9f | 4797 | { |
de520661 RS |
4798 | struct constructor_stack *next; |
4799 | tree type; | |
4800 | tree fields; | |
4801 | tree index; | |
4802 | tree range_end; | |
4803 | tree max_index; | |
4804 | tree unfilled_index; | |
4805 | tree unfilled_fields; | |
b62acd60 | 4806 | tree bit_index; |
de520661 RS |
4807 | tree elements; |
4808 | int offset; | |
4809 | tree pending_elts; | |
4810 | int depth; | |
790e9490 RS |
4811 | /* If nonzero, this value should replace the entire |
4812 | constructor at this level. */ | |
4813 | tree replacement_value; | |
de520661 RS |
4814 | char constant; |
4815 | char simple; | |
4816 | char implicit; | |
4817 | char incremental; | |
4818 | char erroneous; | |
4819 | char outer; | |
4820 | }; | |
d45cf215 | 4821 | |
de520661 | 4822 | struct constructor_stack *constructor_stack; |
400fbf9f | 4823 | |
de520661 RS |
4824 | /* This stack records separate initializers that are nested. |
4825 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
4826 | in cases like { ... (struct foo) { ... } ... }. */ | |
400fbf9f | 4827 | |
de520661 RS |
4828 | struct initializer_stack |
4829 | { | |
4830 | struct initializer_stack *next; | |
4831 | tree decl; | |
4832 | char *asmspec; | |
4833 | struct constructor_stack *constructor_stack; | |
dea273df | 4834 | tree elements; |
de520661 RS |
4835 | struct spelling *spelling; |
4836 | struct spelling *spelling_base; | |
4837 | int spelling_size; | |
4838 | char top_level; | |
4839 | char incremental; | |
4840 | char require_constant_value; | |
4841 | char require_constant_elements; | |
4842 | char deferred; | |
4843 | }; | |
4844 | ||
4845 | struct initializer_stack *initializer_stack; | |
4846 | \f | |
4847 | /* Prepare to parse and output the initializer for variable DECL. */ | |
4848 | ||
4849 | void | |
e28cae4f | 4850 | start_init (decl, asmspec_tree, top_level) |
de520661 | 4851 | tree decl; |
e28cae4f | 4852 | tree asmspec_tree; |
de520661 RS |
4853 | int top_level; |
4854 | { | |
4855 | char *locus; | |
4856 | struct initializer_stack *p | |
4857 | = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack)); | |
e28cae4f RS |
4858 | char *asmspec = 0; |
4859 | ||
4860 | if (asmspec_tree) | |
4861 | asmspec = TREE_STRING_POINTER (asmspec_tree); | |
de520661 RS |
4862 | |
4863 | p->decl = constructor_decl; | |
4864 | p->asmspec = constructor_asmspec; | |
4865 | p->incremental = constructor_incremental; | |
4866 | p->require_constant_value = require_constant_value; | |
4867 | p->require_constant_elements = require_constant_elements; | |
4868 | p->constructor_stack = constructor_stack; | |
dea273df | 4869 | p->elements = constructor_elements; |
de520661 RS |
4870 | p->spelling = spelling; |
4871 | p->spelling_base = spelling_base; | |
4872 | p->spelling_size = spelling_size; | |
4873 | p->deferred = constructor_subconstants_deferred; | |
4874 | p->top_level = constructor_top_level; | |
b62acd60 | 4875 | p->next = initializer_stack; |
de520661 RS |
4876 | initializer_stack = p; |
4877 | ||
4878 | constructor_decl = decl; | |
4879 | constructor_incremental = top_level; | |
4880 | constructor_asmspec = asmspec; | |
4881 | constructor_subconstants_deferred = 0; | |
4882 | constructor_top_level = top_level; | |
4883 | ||
4884 | if (decl != 0) | |
3c3fa147 | 4885 | { |
de520661 | 4886 | require_constant_value = TREE_STATIC (decl); |
f1a2b955 RS |
4887 | require_constant_elements |
4888 | = ((TREE_STATIC (decl) || pedantic) | |
4889 | /* For a scalar, you can always use any value to initialize, | |
4890 | even within braces. */ | |
4891 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
4892 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
4893 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
4894 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
de520661 RS |
4895 | locus = IDENTIFIER_POINTER (DECL_NAME (decl)); |
4896 | constructor_incremental |= TREE_STATIC (decl); | |
3c3fa147 | 4897 | } |
400fbf9f | 4898 | else |
de520661 RS |
4899 | { |
4900 | require_constant_value = 0; | |
4901 | require_constant_elements = 0; | |
4902 | locus = "(anonymous)"; | |
4903 | } | |
400fbf9f | 4904 | |
de520661 | 4905 | constructor_stack = 0; |
400fbf9f | 4906 | |
b71c7f8a RK |
4907 | missing_braces_mentioned = 0; |
4908 | ||
de520661 RS |
4909 | spelling_base = 0; |
4910 | spelling_size = 0; | |
4911 | RESTORE_SPELLING_DEPTH (0); | |
d45cf215 | 4912 | |
de520661 RS |
4913 | if (locus) |
4914 | push_string (locus); | |
4915 | } | |
400fbf9f | 4916 | |
de520661 RS |
4917 | void |
4918 | finish_init () | |
4919 | { | |
4920 | struct initializer_stack *p = initializer_stack; | |
400fbf9f | 4921 | |
de520661 RS |
4922 | /* Output subconstants (string constants, usually) |
4923 | that were referenced within this initializer and saved up. | |
4924 | Must do this if and only if we called defer_addressed_constants. */ | |
4925 | if (constructor_subconstants_deferred) | |
4926 | output_deferred_addressed_constants (); | |
4f77a31b | 4927 | |
de520661 RS |
4928 | /* Free the whole constructor stack of this initializer. */ |
4929 | while (constructor_stack) | |
4930 | { | |
4931 | struct constructor_stack *q = constructor_stack; | |
4932 | constructor_stack = q->next; | |
4933 | free (q); | |
4934 | } | |
400fbf9f | 4935 | |
de520661 RS |
4936 | /* Pop back to the data of the outer initializer (if any). */ |
4937 | constructor_decl = p->decl; | |
4938 | constructor_asmspec = p->asmspec; | |
4939 | constructor_incremental = p->incremental; | |
4940 | require_constant_value = p->require_constant_value; | |
4941 | require_constant_elements = p->require_constant_elements; | |
4942 | constructor_stack = p->constructor_stack; | |
dea273df | 4943 | constructor_elements = p->elements; |
de520661 RS |
4944 | spelling = p->spelling; |
4945 | spelling_base = p->spelling_base; | |
4946 | spelling_size = p->spelling_size; | |
4947 | constructor_subconstants_deferred = p->deferred; | |
4948 | constructor_top_level = p->top_level; | |
4949 | initializer_stack = p->next; | |
4950 | free (p); | |
4951 | } | |
4952 | \f | |
4953 | /* Call here when we see the initializer is surrounded by braces. | |
4954 | This is instead of a call to push_init_level; | |
4955 | it is matched by a call to pop_init_level. | |
400fbf9f | 4956 | |
de520661 RS |
4957 | TYPE is the type to initialize, for a constructor expression. |
4958 | For an initializer for a decl, TYPE is zero. */ | |
5a7ec9d9 | 4959 | |
de520661 RS |
4960 | void |
4961 | really_start_incremental_init (type) | |
4962 | tree type; | |
4963 | { | |
4964 | struct constructor_stack *p | |
4965 | = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); | |
4966 | ||
4967 | if (type == 0) | |
4968 | type = TREE_TYPE (constructor_decl); | |
4969 | ||
4970 | /* Turn off constructor_incremental if type is a struct with bitfields. | |
4971 | Do this before the first push, so that the corrected value | |
4972 | is available in finish_init. */ | |
4973 | check_init_type_bitfields (type); | |
4974 | ||
4975 | p->type = constructor_type; | |
4976 | p->fields = constructor_fields; | |
4977 | p->index = constructor_index; | |
4978 | p->range_end = constructor_range_end; | |
4979 | p->max_index = constructor_max_index; | |
4980 | p->unfilled_index = constructor_unfilled_index; | |
4981 | p->unfilled_fields = constructor_unfilled_fields; | |
b62acd60 | 4982 | p->bit_index = constructor_bit_index; |
5cb7368c | 4983 | p->elements = constructor_elements; |
de520661 RS |
4984 | p->constant = constructor_constant; |
4985 | p->simple = constructor_simple; | |
4986 | p->erroneous = constructor_erroneous; | |
4987 | p->pending_elts = constructor_pending_elts; | |
4988 | p->depth = constructor_depth; | |
790e9490 | 4989 | p->replacement_value = 0; |
de520661 RS |
4990 | p->implicit = 0; |
4991 | p->incremental = constructor_incremental; | |
4992 | p->outer = 0; | |
4993 | p->next = 0; | |
4994 | constructor_stack = p; | |
4995 | ||
4996 | constructor_constant = 1; | |
4997 | constructor_simple = 1; | |
4998 | constructor_depth = SPELLING_DEPTH (); | |
4999 | constructor_elements = 0; | |
5000 | constructor_pending_elts = 0; | |
5001 | constructor_type = type; | |
5002 | ||
5003 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5004 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5005 | { | |
5006 | constructor_fields = TYPE_FIELDS (constructor_type); | |
fc623854 RS |
5007 | /* Skip any nameless bit fields atthe beginning. */ |
5008 | while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) | |
5009 | && DECL_NAME (constructor_fields) == 0) | |
5010 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 5011 | constructor_unfilled_fields = constructor_fields; |
b62acd60 | 5012 | constructor_bit_index = copy_node (integer_zero_node); |
de520661 RS |
5013 | } |
5014 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5015 | { | |
de520661 | 5016 | constructor_range_end = 0; |
de520661 | 5017 | if (TYPE_DOMAIN (constructor_type)) |
2bede729 PB |
5018 | { |
5019 | constructor_max_index | |
5020 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
5021 | constructor_index | |
5022 | = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
5023 | } | |
5024 | else | |
5025 | constructor_index = copy_node (integer_zero_node); | |
5026 | constructor_unfilled_index = copy_node (constructor_index); | |
de520661 RS |
5027 | } |
5028 | else | |
5029 | { | |
5030 | /* Handle the case of int x = {5}; */ | |
5031 | constructor_fields = constructor_type; | |
5032 | constructor_unfilled_fields = constructor_type; | |
5033 | } | |
400fbf9f | 5034 | |
de520661 RS |
5035 | if (constructor_incremental) |
5036 | { | |
5037 | int momentary = suspend_momentary (); | |
5038 | push_obstacks_nochange (); | |
5039 | if (TREE_PERMANENT (constructor_decl)) | |
5040 | end_temporary_allocation (); | |
5041 | make_decl_rtl (constructor_decl, constructor_asmspec, | |
5042 | constructor_top_level); | |
5043 | assemble_variable (constructor_decl, constructor_top_level, 0, 1); | |
5044 | pop_obstacks (); | |
5045 | resume_momentary (momentary); | |
5046 | } | |
400fbf9f | 5047 | |
de520661 RS |
5048 | if (constructor_incremental) |
5049 | { | |
5050 | defer_addressed_constants (); | |
5051 | constructor_subconstants_deferred = 1; | |
5052 | } | |
5053 | } | |
5054 | \f | |
5055 | /* Push down into a subobject, for initialization. | |
5056 | If this is for an explicit set of braces, IMPLICIT is 0. | |
5057 | If it is because the next element belongs at a lower level, | |
5058 | IMPLICIT is 1. */ | |
400fbf9f | 5059 | |
de520661 RS |
5060 | void |
5061 | push_init_level (implicit) | |
5062 | int implicit; | |
5063 | { | |
94ba5069 RS |
5064 | struct constructor_stack *p; |
5065 | ||
5066 | /* If we've exhausted any levels that didn't have braces, | |
5067 | pop them now. */ | |
5068 | while (constructor_stack->implicit) | |
5069 | { | |
5070 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5071 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5072 | && constructor_fields == 0) | |
5073 | process_init_element (pop_init_level (1)); | |
5074 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5075 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
5076 | process_init_element (pop_init_level (1)); | |
5077 | else | |
5078 | break; | |
5079 | } | |
5080 | ||
e700c8ec RS |
5081 | /* Structure elements may require alignment. Do this now |
5082 | if necessary for the subaggregate. */ | |
9d3a34d4 RS |
5083 | if (constructor_incremental && TREE_CODE (constructor_type) == RECORD_TYPE |
5084 | && constructor_fields) | |
e700c8ec RS |
5085 | { |
5086 | /* Advance to offset of this element. */ | |
5087 | if (! tree_int_cst_equal (constructor_bit_index, | |
5088 | DECL_FIELD_BITPOS (constructor_fields))) | |
5089 | { | |
5090 | int next = (TREE_INT_CST_LOW | |
5091 | (DECL_FIELD_BITPOS (constructor_fields)) | |
5092 | / BITS_PER_UNIT); | |
5093 | int here = (TREE_INT_CST_LOW (constructor_bit_index) | |
5094 | / BITS_PER_UNIT); | |
5095 | ||
5096 | assemble_zeros (next - here); | |
5097 | } | |
5098 | } | |
5099 | ||
94ba5069 | 5100 | p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); |
de520661 RS |
5101 | p->type = constructor_type; |
5102 | p->fields = constructor_fields; | |
5103 | p->index = constructor_index; | |
5104 | p->range_end = constructor_range_end; | |
5105 | p->max_index = constructor_max_index; | |
5106 | p->unfilled_index = constructor_unfilled_index; | |
5107 | p->unfilled_fields = constructor_unfilled_fields; | |
b62acd60 | 5108 | p->bit_index = constructor_bit_index; |
de520661 RS |
5109 | p->elements = constructor_elements; |
5110 | p->constant = constructor_constant; | |
5111 | p->simple = constructor_simple; | |
5112 | p->erroneous = constructor_erroneous; | |
5113 | p->pending_elts = constructor_pending_elts; | |
5114 | p->depth = constructor_depth; | |
790e9490 | 5115 | p->replacement_value = 0; |
de520661 RS |
5116 | p->implicit = implicit; |
5117 | p->incremental = constructor_incremental; | |
5118 | p->outer = 0; | |
5119 | p->next = constructor_stack; | |
5120 | constructor_stack = p; | |
5121 | ||
5122 | constructor_constant = 1; | |
5123 | constructor_simple = 1; | |
5124 | constructor_depth = SPELLING_DEPTH (); | |
5125 | constructor_elements = 0; | |
5126 | constructor_pending_elts = 0; | |
5127 | ||
94ba5069 RS |
5128 | /* Don't die if an entire brace-pair level is superfluous |
5129 | in the containing level. */ | |
5130 | if (constructor_type == 0) | |
5131 | ; | |
5132 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5133 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 5134 | { |
91fa3c30 RS |
5135 | /* Don't die if there are extra init elts at the end. */ |
5136 | if (constructor_fields == 0) | |
5137 | constructor_type = 0; | |
5138 | else | |
5139 | { | |
5140 | constructor_type = TREE_TYPE (constructor_fields); | |
19d76e60 | 5141 | push_member_name (constructor_fields); |
91fa3c30 | 5142 | } |
de520661 RS |
5143 | } |
5144 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5145 | { | |
5146 | constructor_type = TREE_TYPE (constructor_type); | |
5147 | push_array_bounds (TREE_INT_CST_LOW (constructor_index)); | |
5148 | } | |
400fbf9f | 5149 | |
91fa3c30 RS |
5150 | if (constructor_type == 0) |
5151 | { | |
5152 | error_init ("extra brace group at end of initializer%s", | |
5153 | " for `%s'", NULL); | |
5154 | constructor_fields = 0; | |
5155 | constructor_unfilled_fields = 0; | |
b71c7f8a | 5156 | return; |
91fa3c30 | 5157 | } |
b71c7f8a RK |
5158 | |
5159 | /* Turn off constructor_incremental if type is a struct with bitfields. */ | |
5160 | check_init_type_bitfields (constructor_type); | |
5161 | ||
5162 | if (implicit && warn_missing_braces && !missing_braces_mentioned) | |
5163 | { | |
5164 | missing_braces_mentioned = 1; | |
5165 | warning_init ("missing braces around initializer%s", " for `%s'", NULL); | |
5166 | } | |
5167 | ||
5168 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
91fa3c30 | 5169 | || TREE_CODE (constructor_type) == UNION_TYPE) |
de520661 RS |
5170 | { |
5171 | constructor_fields = TYPE_FIELDS (constructor_type); | |
fc623854 RS |
5172 | /* Skip any nameless bit fields atthe beginning. */ |
5173 | while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) | |
5174 | && DECL_NAME (constructor_fields) == 0) | |
5175 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 5176 | constructor_unfilled_fields = constructor_fields; |
b62acd60 | 5177 | constructor_bit_index = copy_node (integer_zero_node); |
de520661 RS |
5178 | } |
5179 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5180 | { | |
de520661 | 5181 | constructor_range_end = 0; |
de520661 | 5182 | if (TYPE_DOMAIN (constructor_type)) |
2bede729 PB |
5183 | { |
5184 | constructor_max_index | |
5185 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
5186 | constructor_index | |
5187 | = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
5188 | } | |
5189 | else | |
5190 | constructor_index = copy_node (integer_zero_node); | |
5191 | constructor_unfilled_index = copy_node (constructor_index); | |
de520661 RS |
5192 | } |
5193 | else | |
5194 | { | |
b71c7f8a | 5195 | warning_init ("braces around scalar initializer%s", " for `%s'", NULL); |
de520661 RS |
5196 | constructor_fields = constructor_type; |
5197 | constructor_unfilled_fields = constructor_type; | |
5198 | } | |
5199 | } | |
400fbf9f | 5200 | |
de520661 RS |
5201 | /* Don't read a struct incrementally if it has any bitfields, |
5202 | because the incremental reading code doesn't know how to | |
5203 | handle bitfields yet. */ | |
d45cf215 | 5204 | |
de520661 RS |
5205 | static void |
5206 | check_init_type_bitfields (type) | |
5207 | tree type; | |
5208 | { | |
5209 | if (TREE_CODE (type) == RECORD_TYPE) | |
5210 | { | |
5211 | tree tail; | |
5212 | for (tail = TYPE_FIELDS (type); tail; | |
5213 | tail = TREE_CHAIN (tail)) | |
3c9d8baf RK |
5214 | { |
5215 | if (DECL_BIT_FIELD (tail) | |
5216 | /* This catches cases like `int foo : 8;'. */ | |
5217 | || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail))) | |
5218 | { | |
5219 | constructor_incremental = 0; | |
5220 | break; | |
5221 | } | |
5222 | ||
5223 | check_init_type_bitfields (TREE_TYPE (tail)); | |
5224 | } | |
400fbf9f | 5225 | } |
3c9d8baf RK |
5226 | |
5227 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
5228 | check_init_type_bitfields (TREE_TYPE (type)); | |
de520661 RS |
5229 | } |
5230 | ||
5231 | /* At the end of an implicit or explicit brace level, | |
5232 | finish up that level of constructor. | |
5233 | If we were outputting the elements as they are read, return 0 | |
5234 | from inner levels (process_init_element ignores that), | |
5235 | but return error_mark_node from the outermost level | |
5236 | (that's what we want to put in DECL_INITIAL). | |
5237 | Otherwise, return a CONSTRUCTOR expression. */ | |
5238 | ||
5239 | tree | |
5240 | pop_init_level (implicit) | |
5241 | int implicit; | |
5242 | { | |
5243 | struct constructor_stack *p; | |
9d5f3e49 | 5244 | int size = 0; |
de520661 RS |
5245 | tree constructor = 0; |
5246 | ||
5247 | if (implicit == 0) | |
400fbf9f | 5248 | { |
de520661 RS |
5249 | /* When we come to an explicit close brace, |
5250 | pop any inner levels that didn't have explicit braces. */ | |
5251 | while (constructor_stack->implicit) | |
5252 | process_init_element (pop_init_level (1)); | |
5253 | } | |
400fbf9f | 5254 | |
de520661 | 5255 | p = constructor_stack; |
91fa3c30 RS |
5256 | |
5257 | if (constructor_type != 0) | |
5258 | size = int_size_in_bytes (constructor_type); | |
400fbf9f | 5259 | |
de520661 RS |
5260 | /* Now output all pending elements. */ |
5261 | output_pending_init_elements (1); | |
5262 | ||
b62acd60 RS |
5263 | #if 0 /* c-parse.in warns about {}. */ |
5264 | /* In ANSI, each brace level must have at least one element. */ | |
5265 | if (! implicit && pedantic | |
5266 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5267 | ? integer_zerop (constructor_unfilled_index) | |
5268 | : constructor_unfilled_fields == TYPE_FIELDS (constructor_type))) | |
5269 | pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL); | |
5270 | #endif | |
5271 | ||
de520661 RS |
5272 | /* Pad out the end of the structure. */ |
5273 | ||
790e9490 RS |
5274 | if (p->replacement_value) |
5275 | { | |
5276 | /* If this closes a superfluous brace pair, | |
5277 | just pass out the element between them. */ | |
5278 | constructor = p->replacement_value; | |
5279 | /* If this is the top level thing within the initializer, | |
d11fdb45 | 5280 | and it's for a variable, then since we already called |
790e9490 RS |
5281 | assemble_variable, we must output the value now. */ |
5282 | if (p->next == 0 && constructor_decl != 0 | |
5283 | && constructor_incremental) | |
5284 | { | |
5285 | constructor = digest_init (constructor_type, constructor, | |
5286 | 0, 0); | |
5287 | ||
5288 | /* If initializing an array of unknown size, | |
5289 | determine the size now. */ | |
5290 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5291 | && TYPE_DOMAIN (constructor_type) == 0) | |
5292 | { | |
5293 | int failure; | |
5ded5b76 | 5294 | int momentary_p; |
790e9490 RS |
5295 | |
5296 | push_obstacks_nochange (); | |
5297 | if (TREE_PERMANENT (constructor_type)) | |
5298 | end_temporary_allocation (); | |
5299 | ||
5ded5b76 RK |
5300 | momentary_p = suspend_momentary (); |
5301 | ||
790e9490 RS |
5302 | /* We shouldn't have an incomplete array type within |
5303 | some other type. */ | |
5304 | if (constructor_stack->next) | |
5305 | abort (); | |
5306 | ||
5307 | failure | |
5308 | = complete_array_type (constructor_type, | |
5309 | constructor, 0); | |
5310 | if (failure) | |
5311 | abort (); | |
5312 | ||
5313 | size = int_size_in_bytes (constructor_type); | |
5ded5b76 | 5314 | resume_momentary (momentary_p); |
790e9490 RS |
5315 | pop_obstacks (); |
5316 | } | |
5317 | ||
5318 | output_constant (constructor, size); | |
5319 | } | |
5320 | } | |
91fa3c30 RS |
5321 | else if (constructor_type == 0) |
5322 | ; | |
19d76e60 RK |
5323 | else if (TREE_CODE (constructor_type) != RECORD_TYPE |
5324 | && TREE_CODE (constructor_type) != UNION_TYPE | |
5325 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
5326 | && ! constructor_incremental) | |
5327 | { | |
5328 | /* A nonincremental scalar initializer--just return | |
5329 | the element, after verifying there is just one. */ | |
5330 | if (constructor_elements == 0) | |
5331 | { | |
5332 | error_init ("empty scalar initializer%s", | |
5333 | " for `%s'", NULL); | |
5334 | constructor = error_mark_node; | |
5335 | } | |
5336 | else if (TREE_CHAIN (constructor_elements) != 0) | |
5337 | { | |
5338 | error_init ("extra elements in scalar initializer%s", | |
5339 | " for `%s'", NULL); | |
5340 | constructor = TREE_VALUE (constructor_elements); | |
5341 | } | |
5342 | else | |
5343 | constructor = TREE_VALUE (constructor_elements); | |
5344 | } | |
790e9490 | 5345 | else if (! constructor_incremental) |
de520661 RS |
5346 | { |
5347 | if (constructor_erroneous) | |
5348 | constructor = error_mark_node; | |
5349 | else | |
400fbf9f | 5350 | { |
de520661 RS |
5351 | int momentary = suspend_momentary (); |
5352 | ||
5353 | constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE, | |
5354 | nreverse (constructor_elements)); | |
5355 | if (constructor_constant) | |
5356 | TREE_CONSTANT (constructor) = 1; | |
5357 | if (constructor_constant && constructor_simple) | |
5358 | TREE_STATIC (constructor) = 1; | |
19d76e60 | 5359 | |
de520661 RS |
5360 | resume_momentary (momentary); |
5361 | } | |
5362 | } | |
5363 | else | |
5364 | { | |
5365 | tree filled; | |
5366 | int momentary = suspend_momentary (); | |
400fbf9f | 5367 | |
de520661 RS |
5368 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
5369 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5370 | { | |
de520661 RS |
5371 | /* Find the offset of the end of that field. */ |
5372 | filled = size_binop (CEIL_DIV_EXPR, | |
b62acd60 | 5373 | constructor_bit_index, |
de520661 RS |
5374 | size_int (BITS_PER_UNIT)); |
5375 | } | |
5376 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5377 | { | |
5378 | /* If initializing an array of unknown size, | |
5379 | determine the size now. */ | |
5380 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5381 | && TYPE_DOMAIN (constructor_type) == 0) | |
400fbf9f | 5382 | { |
de520661 RS |
5383 | tree maxindex |
5384 | = size_binop (MINUS_EXPR, | |
5385 | constructor_unfilled_index, | |
5386 | integer_one_node); | |
5387 | ||
5388 | push_obstacks_nochange (); | |
5389 | if (TREE_PERMANENT (constructor_type)) | |
5390 | end_temporary_allocation (); | |
5391 | maxindex = copy_node (maxindex); | |
5392 | TYPE_DOMAIN (constructor_type) = build_index_type (maxindex); | |
5393 | TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type); | |
5394 | ||
de520661 RS |
5395 | if (pedantic |
5396 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)), | |
5397 | integer_zero_node)) | |
5398 | error_with_decl (constructor_decl, "zero-size array `%s'"); | |
5399 | layout_type (constructor_type); | |
5400 | size = int_size_in_bytes (constructor_type); | |
5401 | pop_obstacks (); | |
400fbf9f JW |
5402 | } |
5403 | ||
de520661 RS |
5404 | filled = size_binop (MULT_EXPR, constructor_unfilled_index, |
5405 | size_in_bytes (TREE_TYPE (constructor_type))); | |
5406 | } | |
5407 | else | |
5408 | filled = 0; | |
400fbf9f | 5409 | |
de520661 RS |
5410 | if (filled != 0) |
5411 | assemble_zeros (size - TREE_INT_CST_LOW (filled)); | |
5412 | ||
5413 | resume_momentary (momentary); | |
5414 | } | |
5415 | ||
5416 | ||
5417 | constructor_type = p->type; | |
5418 | constructor_fields = p->fields; | |
5419 | constructor_index = p->index; | |
5420 | constructor_range_end = p->range_end; | |
5421 | constructor_max_index = p->max_index; | |
5422 | constructor_unfilled_index = p->unfilled_index; | |
5423 | constructor_unfilled_fields = p->unfilled_fields; | |
b62acd60 | 5424 | constructor_bit_index = p->bit_index; |
de520661 RS |
5425 | constructor_elements = p->elements; |
5426 | constructor_constant = p->constant; | |
5427 | constructor_simple = p->simple; | |
5428 | constructor_erroneous = p->erroneous; | |
5429 | constructor_pending_elts = p->pending_elts; | |
5430 | constructor_depth = p->depth; | |
5431 | constructor_incremental = p->incremental; | |
5432 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
5433 | ||
5434 | constructor_stack = p->next; | |
5435 | free (p); | |
5436 | ||
5437 | if (constructor == 0) | |
5438 | { | |
5439 | if (constructor_stack == 0) | |
5440 | return error_mark_node; | |
5441 | return NULL_TREE; | |
5442 | } | |
5443 | return constructor; | |
5444 | } | |
5445 | ||
5446 | /* Within an array initializer, specify the next index to be initialized. | |
5447 | FIRST is that index. If LAST is nonzero, then initialize a range | |
5448 | of indices, running from FIRST through LAST. */ | |
5449 | ||
5450 | void | |
5451 | set_init_index (first, last) | |
5452 | tree first, last; | |
5453 | { | |
19d76e60 RK |
5454 | while ((TREE_CODE (first) == NOP_EXPR |
5455 | || TREE_CODE (first) == CONVERT_EXPR | |
5456 | || TREE_CODE (first) == NON_LVALUE_EXPR) | |
5457 | && (TYPE_MODE (TREE_TYPE (first)) | |
5458 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0))))) | |
5459 | (first) = TREE_OPERAND (first, 0); | |
5460 | if (last) | |
5461 | while ((TREE_CODE (last) == NOP_EXPR | |
5462 | || TREE_CODE (last) == CONVERT_EXPR | |
5463 | || TREE_CODE (last) == NON_LVALUE_EXPR) | |
5464 | && (TYPE_MODE (TREE_TYPE (last)) | |
5465 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0))))) | |
5466 | (last) = TREE_OPERAND (last, 0); | |
5467 | ||
94ba5069 RS |
5468 | if (TREE_CODE (first) != INTEGER_CST) |
5469 | error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); | |
5470 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
5471 | error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); | |
5472 | else if (tree_int_cst_lt (first, constructor_unfilled_index)) | |
de520661 RS |
5473 | error_init ("duplicate array index in initializer%s", " for `%s'", NULL); |
5474 | else | |
5475 | { | |
5476 | TREE_INT_CST_LOW (constructor_index) | |
5477 | = TREE_INT_CST_LOW (first); | |
5478 | TREE_INT_CST_HIGH (constructor_index) | |
5479 | = TREE_INT_CST_HIGH (first); | |
5480 | ||
5481 | if (last != 0 && tree_int_cst_lt (last, first)) | |
5482 | error_init ("empty index range in initializer%s", " for `%s'", NULL); | |
5483 | else | |
b62acd60 RS |
5484 | { |
5485 | if (pedantic) | |
5486 | pedwarn ("ANSI C forbids specifying element to initialize"); | |
5487 | constructor_range_end = last; | |
5488 | } | |
de520661 RS |
5489 | } |
5490 | } | |
5491 | ||
5492 | /* Within a struct initializer, specify the next field to be initialized. */ | |
5493 | ||
94ba5069 | 5494 | void |
de520661 RS |
5495 | set_init_label (fieldname) |
5496 | tree fieldname; | |
5497 | { | |
5498 | tree tail; | |
5499 | int passed = 0; | |
5500 | ||
5501 | for (tail = TYPE_FIELDS (constructor_type); tail; | |
5502 | tail = TREE_CHAIN (tail)) | |
5503 | { | |
5504 | if (tail == constructor_unfilled_fields) | |
5505 | passed = 1; | |
5506 | if (DECL_NAME (tail) == fieldname) | |
5507 | break; | |
5508 | } | |
5509 | ||
5510 | if (tail == 0) | |
5511 | error ("unknown field `%s' specified in initializer", | |
5512 | IDENTIFIER_POINTER (fieldname)); | |
5513 | else if (!passed) | |
5514 | error ("field `%s' already initialized", | |
5515 | IDENTIFIER_POINTER (fieldname)); | |
5516 | else | |
b62acd60 RS |
5517 | { |
5518 | constructor_fields = tail; | |
5519 | if (pedantic) | |
5520 | pedwarn ("ANSI C forbids specifying structure member to initialize"); | |
5521 | } | |
de520661 RS |
5522 | } |
5523 | \f | |
5524 | /* "Output" the next constructor element. | |
5525 | At top level, really output it to assembler code now. | |
5526 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
5527 | TYPE is the data type that the containing data type wants here. | |
5528 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
5529 | ||
5530 | PENDING if non-nil means output pending elements that belong | |
5531 | right after this element. (PENDING is normally 1; | |
5532 | it is 0 while outputting pending elements, to avoid recursion.) */ | |
5533 | ||
34403047 | 5534 | static void |
de520661 RS |
5535 | output_init_element (value, type, field, pending) |
5536 | tree value, type, field; | |
5537 | int pending; | |
5538 | { | |
5539 | int duplicate = 0; | |
5540 | ||
d3ab9753 RS |
5541 | if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE |
5542 | || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE | |
fd5d5b94 RS |
5543 | && !(TREE_CODE (value) == STRING_CST |
5544 | && TREE_CODE (type) == ARRAY_TYPE | |
5545 | && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE) | |
1e40eab8 RS |
5546 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), |
5547 | TYPE_MAIN_VARIANT (type)))) | |
d3ab9753 RS |
5548 | value = default_conversion (value); |
5549 | ||
5550 | if (value == error_mark_node) | |
5551 | constructor_erroneous = 1; | |
5552 | else if (!TREE_CONSTANT (value)) | |
5553 | constructor_constant = 0; | |
5554 | else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) | |
5555 | constructor_simple = 0; | |
5556 | ||
de520661 RS |
5557 | if (require_constant_value && ! TREE_CONSTANT (value)) |
5558 | { | |
5559 | error_init ("initializer element%s is not constant", | |
5560 | " for `%s'", NULL); | |
5561 | value = error_mark_node; | |
5562 | } | |
5563 | else if (require_constant_elements | |
5564 | && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) | |
5565 | { | |
5566 | error_init ("initializer element%s is not computable at load time", | |
5567 | " for `%s'", NULL); | |
5568 | value = error_mark_node; | |
5569 | } | |
5570 | ||
5571 | /* If this element duplicates one on constructor_pending_elts, | |
5572 | print a message and ignore it. Don't do this when we're | |
5573 | processing elements taken off constructor_pending_elts, | |
5574 | because we'd always get spurious errors. */ | |
5575 | if (pending) | |
5576 | { | |
5577 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5578 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5579 | { | |
5580 | if (purpose_member (field, constructor_pending_elts)) | |
400fbf9f | 5581 | { |
de520661 RS |
5582 | error_init ("duplicate initializer%s", " for `%s'", NULL); |
5583 | duplicate = 1; | |
400fbf9f | 5584 | } |
de520661 RS |
5585 | } |
5586 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5587 | { | |
5588 | tree tail; | |
5589 | for (tail = constructor_pending_elts; tail; | |
5590 | tail = TREE_CHAIN (tail)) | |
5591 | if (TREE_PURPOSE (tail) != 0 | |
5592 | && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST | |
5593 | && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index)) | |
5594 | break; | |
400fbf9f | 5595 | |
de520661 | 5596 | if (tail != 0) |
400fbf9f | 5597 | { |
de520661 RS |
5598 | error_init ("duplicate initializer%s", " for `%s'", NULL); |
5599 | duplicate = 1; | |
400fbf9f | 5600 | } |
400fbf9f JW |
5601 | } |
5602 | } | |
400fbf9f | 5603 | |
de520661 RS |
5604 | /* If this element doesn't come next in sequence, |
5605 | put it on constructor_pending_elts. */ | |
5606 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5607 | && !tree_int_cst_equal (field, constructor_unfilled_index)) | |
5608 | { | |
5609 | if (! duplicate) | |
8348547a RS |
5610 | /* The copy_node is needed in case field is actually |
5611 | constructor_index, which is modified in place. */ | |
de520661 | 5612 | constructor_pending_elts |
8348547a | 5613 | = tree_cons (copy_node (field), |
d11fdb45 | 5614 | digest_init (type, value, 0, 0), |
de520661 RS |
5615 | constructor_pending_elts); |
5616 | } | |
76aaaae2 | 5617 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
de520661 RS |
5618 | && field != constructor_unfilled_fields) |
5619 | { | |
76aaaae2 RS |
5620 | /* We do this for records but not for unions. In a union, |
5621 | no matter which field is specified, it can be initialized | |
5622 | right away since it starts at the beginning of the union. */ | |
de520661 RS |
5623 | if (!duplicate) |
5624 | constructor_pending_elts | |
5625 | = tree_cons (field, | |
d11fdb45 | 5626 | digest_init (type, value, 0, 0), |
de520661 RS |
5627 | constructor_pending_elts); |
5628 | } | |
5629 | else | |
5630 | { | |
5631 | /* Otherwise, output this element either to | |
5632 | constructor_elements or to the assembler file. */ | |
400fbf9f | 5633 | |
de520661 | 5634 | if (!duplicate) |
c2f4acb7 | 5635 | { |
de520661 | 5636 | if (! constructor_incremental) |
94ba5069 | 5637 | { |
19d76e60 | 5638 | if (field && TREE_CODE (field) == INTEGER_CST) |
94ba5069 RS |
5639 | field = copy_node (field); |
5640 | constructor_elements | |
5641 | = tree_cons (field, digest_init (type, value, 0, 0), | |
5642 | constructor_elements); | |
5643 | } | |
de520661 | 5644 | else |
b62acd60 RS |
5645 | { |
5646 | /* Structure elements may require alignment. | |
5647 | Do this, if necessary. */ | |
5648 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
5649 | { | |
5650 | /* Advance to offset of this element. */ | |
5651 | if (! tree_int_cst_equal (constructor_bit_index, | |
b5ff0f70 | 5652 | DECL_FIELD_BITPOS (field))) |
b62acd60 RS |
5653 | { |
5654 | int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)) | |
5655 | / BITS_PER_UNIT); | |
5656 | int here = (TREE_INT_CST_LOW (constructor_bit_index) | |
5657 | / BITS_PER_UNIT); | |
5658 | ||
5659 | assemble_zeros (next - here); | |
5660 | } | |
5661 | } | |
d11fdb45 RS |
5662 | output_constant (digest_init (type, value, 0, 0), |
5663 | int_size_in_bytes (type)); | |
b62acd60 | 5664 | |
925d5bbf RS |
5665 | /* For a record or union, |
5666 | keep track of end position of last field. */ | |
5667 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5668 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
b62acd60 | 5669 | { |
b5ff0f70 RK |
5670 | tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field), |
5671 | DECL_SIZE (field)); | |
b62acd60 RS |
5672 | TREE_INT_CST_LOW (constructor_bit_index) |
5673 | = TREE_INT_CST_LOW (temp); | |
5674 | TREE_INT_CST_HIGH (constructor_bit_index) | |
5675 | = TREE_INT_CST_HIGH (temp); | |
5676 | } | |
5677 | } | |
c2f4acb7 RS |
5678 | } |
5679 | ||
de520661 RS |
5680 | /* Advance the variable that indicates sequential elements output. */ |
5681 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
400fbf9f | 5682 | { |
de520661 RS |
5683 | tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index, |
5684 | integer_one_node); | |
5685 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
5686 | = TREE_INT_CST_LOW (tem); | |
5687 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
5688 | = TREE_INT_CST_HIGH (tem); | |
5689 | } | |
5690 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
5691 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
5692 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
5693 | constructor_unfilled_fields = 0; | |
5694 | ||
5695 | /* Now output any pending elements which have become next. */ | |
5696 | if (pending) | |
5697 | output_pending_init_elements (0); | |
5698 | } | |
5699 | } | |
400fbf9f | 5700 | |
de520661 RS |
5701 | /* Output any pending elements which have become next. |
5702 | As we output elements, constructor_unfilled_{fields,index} | |
5703 | advances, which may cause other elements to become next; | |
5704 | if so, they too are output. | |
5705 | ||
5706 | If ALL is 0, we return when there are | |
5707 | no more pending elements to output now. | |
5708 | ||
5709 | If ALL is 1, we output space as necessary so that | |
5710 | we can output all the pending elements. */ | |
5711 | ||
5712 | static void | |
5713 | output_pending_init_elements (all) | |
5714 | int all; | |
5715 | { | |
5716 | tree tail; | |
5717 | tree next; | |
5718 | ||
5719 | retry: | |
5720 | ||
5721 | /* Look thru the whole pending list. | |
5722 | If we find an element that should be output now, | |
5723 | output it. Otherwise, set NEXT to the element | |
5724 | that comes first among those still pending. */ | |
5725 | ||
5726 | next = 0; | |
5727 | for (tail = constructor_pending_elts; tail; | |
5728 | tail = TREE_CHAIN (tail)) | |
5729 | { | |
5730 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5731 | { | |
5732 | if (tree_int_cst_equal (TREE_PURPOSE (tail), | |
5733 | constructor_unfilled_index)) | |
400fbf9f | 5734 | { |
de520661 RS |
5735 | output_init_element (TREE_VALUE (tail), TREE_TYPE (constructor_type), |
5736 | constructor_unfilled_index, 0); | |
5737 | goto retry; | |
5738 | } | |
5739 | else if (tree_int_cst_lt (TREE_PURPOSE (tail), | |
5740 | constructor_unfilled_index)) | |
5741 | ; | |
5742 | else if (next == 0 | |
5743 | || tree_int_cst_lt (TREE_PURPOSE (tail), | |
5744 | next)) | |
5745 | next = TREE_PURPOSE (tail); | |
5746 | } | |
5747 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5748 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5749 | { | |
5750 | if (TREE_PURPOSE (tail) == constructor_unfilled_fields) | |
5751 | { | |
5752 | output_init_element (TREE_VALUE (tail), | |
5753 | TREE_TYPE (constructor_unfilled_fields), | |
5754 | constructor_unfilled_fields, | |
5755 | 0); | |
5756 | goto retry; | |
400fbf9f | 5757 | } |
281ec92f RS |
5758 | else if (constructor_unfilled_fields == 0 |
5759 | || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)), | |
5760 | DECL_FIELD_BITPOS (constructor_unfilled_fields))) | |
de520661 RS |
5761 | ; |
5762 | else if (next == 0 | |
5763 | || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)), | |
5764 | DECL_FIELD_BITPOS (next))) | |
5765 | next = TREE_PURPOSE (tail); | |
400fbf9f | 5766 | } |
de520661 RS |
5767 | } |
5768 | ||
5769 | /* Ordinarily return, but not if we want to output all | |
5770 | and there are elements left. */ | |
5771 | if (! (all && next != 0)) | |
5772 | return; | |
5773 | ||
5774 | /* Generate space up to the position of NEXT. */ | |
5775 | if (constructor_incremental) | |
5776 | { | |
5777 | tree filled; | |
9d5f3e49 | 5778 | tree nextpos_tree = size_int (0); |
400fbf9f | 5779 | |
de520661 RS |
5780 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
5781 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 5782 | { |
b5ff0f70 | 5783 | /* Find the last field written out, if any. */ |
de520661 RS |
5784 | for (tail = TYPE_FIELDS (constructor_type); tail; |
5785 | tail = TREE_CHAIN (tail)) | |
5786 | if (TREE_CHAIN (tail) == constructor_unfilled_fields) | |
5787 | break; | |
b5ff0f70 RK |
5788 | |
5789 | if (tail) | |
5790 | /* Find the offset of the end of that field. */ | |
5791 | filled = size_binop (CEIL_DIV_EXPR, | |
5792 | size_binop (PLUS_EXPR, | |
5793 | DECL_FIELD_BITPOS (tail), | |
5794 | DECL_SIZE (tail)), | |
5795 | size_int (BITS_PER_UNIT)); | |
5796 | else | |
5797 | filled = size_int (0); | |
5798 | ||
de520661 RS |
5799 | nextpos_tree = size_binop (CEIL_DIV_EXPR, |
5800 | DECL_FIELD_BITPOS (next), | |
5801 | size_int (BITS_PER_UNIT)); | |
b5ff0f70 RK |
5802 | |
5803 | TREE_INT_CST_HIGH (constructor_bit_index) | |
5804 | = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next)); | |
5805 | TREE_INT_CST_LOW (constructor_bit_index) | |
5806 | = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next)); | |
de520661 | 5807 | constructor_unfilled_fields = next; |
400fbf9f | 5808 | } |
de520661 | 5809 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
400fbf9f | 5810 | { |
de520661 RS |
5811 | filled = size_binop (MULT_EXPR, constructor_unfilled_index, |
5812 | size_in_bytes (TREE_TYPE (constructor_type))); | |
5813 | nextpos_tree | |
5814 | = size_binop (MULT_EXPR, next, | |
5815 | size_in_bytes (TREE_TYPE (constructor_type))); | |
5816 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
5817 | = TREE_INT_CST_LOW (next); | |
5818 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
5819 | = TREE_INT_CST_HIGH (next); | |
400fbf9f | 5820 | } |
de520661 RS |
5821 | else |
5822 | filled = 0; | |
400fbf9f | 5823 | |
de520661 | 5824 | if (filled) |
fe67cf58 | 5825 | { |
de520661 RS |
5826 | int nextpos = TREE_INT_CST_LOW (nextpos_tree); |
5827 | ||
5828 | assemble_zeros (nextpos - TREE_INT_CST_LOW (filled)); | |
fe67cf58 | 5829 | } |
de520661 | 5830 | } |
94ba5069 RS |
5831 | else |
5832 | { | |
5833 | /* If it's not incremental, just skip over the gap, | |
5834 | so that after jumping to retry we will output the next | |
5835 | successive element. */ | |
5836 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
5837 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5838 | constructor_unfilled_fields = next; | |
5839 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
5840 | { | |
5841 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
5842 | = TREE_INT_CST_LOW (next); | |
5843 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
5844 | = TREE_INT_CST_HIGH (next); | |
5845 | } | |
5846 | } | |
de520661 RS |
5847 | |
5848 | goto retry; | |
5849 | } | |
5850 | \f | |
5851 | /* Add one non-braced element to the current constructor level. | |
5852 | This adjusts the current position within the constructor's type. | |
5853 | This may also start or terminate implicit levels | |
5854 | to handle a partly-braced initializer. | |
5855 | ||
5856 | Once this has found the correct level for the new element, | |
5857 | it calls output_init_element. | |
5858 | ||
5859 | Note: if we are incrementally outputting this constructor, | |
5860 | this function may be called with a null argument | |
5861 | representing a sub-constructor that was already incrementally output. | |
5862 | When that happens, we output nothing, but we do the bookkeeping | |
5863 | to skip past that element of the current constructor. */ | |
5864 | ||
5865 | void | |
5866 | process_init_element (value) | |
5867 | tree value; | |
5868 | { | |
b62acd60 RS |
5869 | tree orig_value = value; |
5870 | int string_flag = value != 0 && TREE_CODE (value) == STRING_CST; | |
5871 | ||
790e9490 RS |
5872 | /* Handle superfluous braces around string cst as in |
5873 | char x[] = {"foo"}; */ | |
5874 | if (string_flag | |
d27c148b | 5875 | && constructor_type |
790e9490 | 5876 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
61e215dd | 5877 | && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE |
790e9490 RS |
5878 | && integer_zerop (constructor_unfilled_index)) |
5879 | { | |
5880 | constructor_stack->replacement_value = value; | |
5881 | return; | |
5882 | } | |
5883 | ||
790e9490 RS |
5884 | if (constructor_stack->replacement_value != 0) |
5885 | { | |
5886 | error_init ("excess elements in struct initializer%s", | |
5887 | " after `%s'", NULL_PTR); | |
5888 | return; | |
5889 | } | |
5890 | ||
91fa3c30 RS |
5891 | /* Ignore elements of a brace group if it is entirely superfluous |
5892 | and has already been diagnosed. */ | |
5893 | if (constructor_type == 0) | |
5894 | return; | |
5895 | ||
de520661 RS |
5896 | /* If we've exhausted any levels that didn't have braces, |
5897 | pop them now. */ | |
5898 | while (constructor_stack->implicit) | |
5899 | { | |
5900 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
5901 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
5902 | && constructor_fields == 0) | |
5903 | process_init_element (pop_init_level (1)); | |
5904 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
5905 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
5906 | process_init_element (pop_init_level (1)); | |
fe67cf58 | 5907 | else |
de520661 | 5908 | break; |
400fbf9f JW |
5909 | } |
5910 | ||
de520661 | 5911 | while (1) |
400fbf9f | 5912 | { |
de520661 | 5913 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
400fbf9f | 5914 | { |
de520661 RS |
5915 | tree fieldtype; |
5916 | enum tree_code fieldcode; | |
5917 | ||
5918 | if (constructor_fields == 0) | |
5919 | { | |
5920 | pedwarn_init ("excess elements in struct initializer%s", | |
5921 | " after `%s'", NULL_PTR); | |
5922 | break; | |
5923 | } | |
5924 | ||
5925 | fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields)); | |
5926 | fieldcode = TREE_CODE (fieldtype); | |
5927 | ||
b62acd60 RS |
5928 | /* Accept a string constant to initialize a subarray. */ |
5929 | if (value != 0 | |
5930 | && fieldcode == ARRAY_TYPE | |
5931 | && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE | |
5932 | && string_flag) | |
5933 | value = orig_value; | |
5934 | /* Otherwise, if we have come to a subaggregate, | |
5935 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 5936 | else if (value != 0 && !constructor_no_implicit |
b62acd60 RS |
5937 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype |
5938 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
5939 | || fieldcode == UNION_TYPE)) | |
de520661 RS |
5940 | { |
5941 | push_init_level (1); | |
5942 | continue; | |
5943 | } | |
5944 | ||
5945 | if (value) | |
5946 | { | |
19d76e60 | 5947 | push_member_name (constructor_fields); |
de520661 RS |
5948 | output_init_element (value, fieldtype, constructor_fields, 1); |
5949 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
5950 | } | |
5951 | else | |
b62acd60 RS |
5952 | /* Do the bookkeeping for an element that was |
5953 | directly output as a constructor. */ | |
5954 | { | |
5955 | /* For a record, keep track of end position of last field. */ | |
5956 | tree temp = size_binop (PLUS_EXPR, | |
5957 | DECL_FIELD_BITPOS (constructor_fields), | |
5958 | DECL_SIZE (constructor_fields)); | |
5959 | TREE_INT_CST_LOW (constructor_bit_index) | |
5960 | = TREE_INT_CST_LOW (temp); | |
5961 | TREE_INT_CST_HIGH (constructor_bit_index) | |
5962 | = TREE_INT_CST_HIGH (temp); | |
5963 | ||
5964 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
5965 | } | |
de520661 RS |
5966 | |
5967 | constructor_fields = TREE_CHAIN (constructor_fields); | |
fc623854 RS |
5968 | /* Skip any nameless bit fields atthe beginning. */ |
5969 | while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) | |
5970 | && DECL_NAME (constructor_fields) == 0) | |
5971 | constructor_fields = TREE_CHAIN (constructor_fields); | |
de520661 | 5972 | break; |
400fbf9f | 5973 | } |
de520661 | 5974 | if (TREE_CODE (constructor_type) == UNION_TYPE) |
400fbf9f | 5975 | { |
de520661 RS |
5976 | tree fieldtype; |
5977 | enum tree_code fieldcode; | |
5978 | ||
5979 | if (constructor_fields == 0) | |
5980 | { | |
5981 | pedwarn_init ("excess elements in union initializer%s", | |
5982 | " after `%s'", NULL_PTR); | |
5983 | break; | |
5984 | } | |
5985 | ||
5986 | fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields)); | |
5987 | fieldcode = TREE_CODE (fieldtype); | |
5988 | ||
b62acd60 RS |
5989 | /* Accept a string constant to initialize a subarray. */ |
5990 | if (value != 0 | |
5991 | && fieldcode == ARRAY_TYPE | |
5992 | && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE | |
5993 | && string_flag) | |
5994 | value = orig_value; | |
5995 | /* Otherwise, if we have come to a subaggregate, | |
5996 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 5997 | else if (value != 0 && !constructor_no_implicit |
b62acd60 RS |
5998 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype |
5999 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
6000 | || fieldcode == UNION_TYPE)) | |
de520661 RS |
6001 | { |
6002 | push_init_level (1); | |
6003 | continue; | |
6004 | } | |
6005 | ||
6006 | if (value) | |
6007 | { | |
19d76e60 | 6008 | push_member_name (constructor_fields); |
de520661 RS |
6009 | output_init_element (value, fieldtype, constructor_fields, 1); |
6010 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
6011 | } | |
6012 | else | |
94ba5069 RS |
6013 | /* Do the bookkeeping for an element that was |
6014 | directly output as a constructor. */ | |
6015 | { | |
6016 | TREE_INT_CST_LOW (constructor_bit_index) | |
6017 | = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields)); | |
6018 | TREE_INT_CST_HIGH (constructor_bit_index) | |
6019 | = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields)); | |
6020 | ||
6021 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
6022 | } | |
de520661 RS |
6023 | |
6024 | constructor_fields = 0; | |
6025 | break; | |
400fbf9f | 6026 | } |
de520661 RS |
6027 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6028 | { | |
6029 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
6030 | enum tree_code eltcode = TREE_CODE (elttype); | |
6031 | ||
b62acd60 RS |
6032 | /* Accept a string constant to initialize a subarray. */ |
6033 | if (value != 0 | |
6034 | && eltcode == ARRAY_TYPE | |
6035 | && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE | |
6036 | && string_flag) | |
6037 | value = orig_value; | |
6038 | /* Otherwise, if we have come to a subaggregate, | |
6039 | and we don't have an element of its type, push into it. */ | |
cc77d4d5 | 6040 | else if (value != 0 && !constructor_no_implicit |
b62acd60 RS |
6041 | && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype |
6042 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE | |
6043 | || eltcode == UNION_TYPE)) | |
de520661 RS |
6044 | { |
6045 | push_init_level (1); | |
6046 | continue; | |
6047 | } | |
6048 | ||
6049 | if (constructor_max_index != 0 | |
6050 | && tree_int_cst_lt (constructor_max_index, constructor_index)) | |
6051 | { | |
6052 | pedwarn_init ("excess elements in array initializer%s", | |
6053 | " after `%s'", NULL_PTR); | |
6054 | break; | |
6055 | } | |
400fbf9f | 6056 | |
de520661 RS |
6057 | /* Now output the actual element. |
6058 | Ordinarily, output once. | |
6059 | If there is a range, repeat it till we advance past the range. */ | |
6060 | do | |
6061 | { | |
6062 | tree tem; | |
d45cf215 | 6063 | |
de520661 RS |
6064 | if (value) |
6065 | { | |
6066 | push_array_bounds (TREE_INT_CST_LOW (constructor_index)); | |
6067 | output_init_element (value, elttype, constructor_index, 1); | |
6068 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
6069 | } | |
d45cf215 | 6070 | |
de520661 RS |
6071 | tem = size_binop (PLUS_EXPR, constructor_index, |
6072 | integer_one_node); | |
6073 | TREE_INT_CST_LOW (constructor_index) | |
6074 | = TREE_INT_CST_LOW (tem); | |
6075 | TREE_INT_CST_HIGH (constructor_index) | |
6076 | = TREE_INT_CST_HIGH (tem); | |
6077 | ||
6078 | if (!value) | |
6079 | /* If we are doing the bookkeeping for an element that was | |
6080 | directly output as a constructor, | |
6081 | we must update constructor_unfilled_index. */ | |
6082 | { | |
6083 | TREE_INT_CST_LOW (constructor_unfilled_index) | |
6084 | = TREE_INT_CST_LOW (constructor_index); | |
6085 | TREE_INT_CST_HIGH (constructor_unfilled_index) | |
6086 | = TREE_INT_CST_HIGH (constructor_index); | |
6087 | } | |
6088 | } | |
6089 | while (! (constructor_range_end == 0 | |
6090 | || tree_int_cst_lt (constructor_range_end, | |
6091 | constructor_index))); | |
400fbf9f | 6092 | |
de520661 RS |
6093 | break; |
6094 | } | |
6095 | ||
6096 | /* Handle the sole element allowed in a braced initializer | |
6097 | for a scalar variable. */ | |
6098 | if (constructor_fields == 0) | |
6099 | { | |
6100 | pedwarn_init ("excess elements in scalar initializer%s", | |
6101 | " after `%s'", NULL_PTR); | |
6102 | break; | |
6103 | } | |
6104 | ||
6105 | if (value) | |
6106 | output_init_element (value, constructor_type, NULL_TREE, 1); | |
6107 | constructor_fields = 0; | |
6108 | break; | |
fe67cf58 | 6109 | } |
de520661 RS |
6110 | |
6111 | /* If the (lexically) previous elments are not now saved, | |
6112 | we can discard the storage for them. */ | |
fe23d5ad | 6113 | if (constructor_incremental && constructor_pending_elts == 0 && value != 0) |
de520661 | 6114 | clear_momentary (); |
400fbf9f JW |
6115 | } |
6116 | \f | |
6117 | /* Expand an ASM statement with operands, handling output operands | |
6118 | that are not variables or INDIRECT_REFS by transforming such | |
6119 | cases into cases that expand_asm_operands can handle. | |
6120 | ||
6121 | Arguments are same as for expand_asm_operands. */ | |
6122 | ||
6123 | void | |
6124 | c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line) | |
6125 | tree string, outputs, inputs, clobbers; | |
6126 | int vol; | |
6127 | char *filename; | |
6128 | int line; | |
6129 | { | |
6130 | int noutputs = list_length (outputs); | |
6131 | register int i; | |
6132 | /* o[I] is the place that output number I should be written. */ | |
6133 | register tree *o = (tree *) alloca (noutputs * sizeof (tree)); | |
6134 | register tree tail; | |
6135 | ||
6136 | if (TREE_CODE (string) == ADDR_EXPR) | |
6137 | string = TREE_OPERAND (string, 0); | |
6138 | if (TREE_CODE (string) != STRING_CST) | |
6139 | { | |
6140 | error ("asm template is not a string constant"); | |
6141 | return; | |
6142 | } | |
6143 | ||
7b6327ae | 6144 | /* Record the contents of OUTPUTS before it is modified. */ |
400fbf9f JW |
6145 | for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) |
6146 | o[i] = TREE_VALUE (tail); | |
6147 | ||
6148 | /* Perform default conversions on array and function inputs. */ | |
6149 | /* Don't do this for other types-- | |
6150 | it would screw up operands expected to be in memory. */ | |
6151 | for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++) | |
6152 | if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE | |
6153 | || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE) | |
6154 | TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail)); | |
6155 | ||
6156 | /* Generate the ASM_OPERANDS insn; | |
6157 | store into the TREE_VALUEs of OUTPUTS some trees for | |
6158 | where the values were actually stored. */ | |
6159 | expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line); | |
6160 | ||
6161 | /* Copy all the intermediate outputs into the specified outputs. */ | |
6162 | for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) | |
6163 | { | |
6164 | if (o[i] != TREE_VALUE (tail)) | |
6165 | { | |
6166 | expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)), | |
6167 | 0, VOIDmode, 0); | |
6168 | free_temp_slots (); | |
6169 | } | |
6170 | /* Detect modification of read-only values. | |
6171 | (Otherwise done by build_modify_expr.) */ | |
6172 | else | |
6173 | { | |
6174 | tree type = TREE_TYPE (o[i]); | |
6175 | if (TYPE_READONLY (type) | |
6176 | || ((TREE_CODE (type) == RECORD_TYPE | |
6177 | || TREE_CODE (type) == UNION_TYPE) | |
6178 | && C_TYPE_FIELDS_READONLY (type))) | |
6179 | readonly_warning (o[i], "modification by `asm'"); | |
6180 | } | |
6181 | } | |
6182 | ||
6183 | /* Those MODIFY_EXPRs could do autoincrements. */ | |
6184 | emit_queue (); | |
6185 | } | |
6186 | \f | |
6187 | /* Expand a C `return' statement. | |
6188 | RETVAL is the expression for what to return, | |
6189 | or a null pointer for `return;' with no value. */ | |
6190 | ||
6191 | void | |
6192 | c_expand_return (retval) | |
6193 | tree retval; | |
6194 | { | |
6195 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)); | |
6196 | ||
6197 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
08bf538e | 6198 | warning ("function declared `noreturn' has a `return' statement"); |
400fbf9f JW |
6199 | |
6200 | if (!retval) | |
6201 | { | |
6202 | current_function_returns_null = 1; | |
6203 | if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
6204 | warning ("`return' with no value, in function returning non-void"); | |
6205 | expand_null_return (); | |
6206 | } | |
6207 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) | |
6208 | { | |
6209 | current_function_returns_null = 1; | |
6210 | if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) | |
6211 | pedwarn ("`return' with a value, in function returning void"); | |
6212 | expand_return (retval); | |
6213 | } | |
6214 | else | |
6215 | { | |
6216 | tree t = convert_for_assignment (valtype, retval, "return", | |
9b7267b8 | 6217 | NULL_TREE, NULL_TREE, 0); |
400fbf9f | 6218 | tree res = DECL_RESULT (current_function_decl); |
88a3dbc1 | 6219 | tree inner; |
70768eda RK |
6220 | |
6221 | if (t == error_mark_node) | |
6222 | return; | |
6223 | ||
88a3dbc1 RK |
6224 | inner = t = convert (TREE_TYPE (res), t); |
6225 | ||
6226 | /* Strip any conversions, additions, and subtractions, and see if | |
6227 | we are returning the address of a local variable. Warn if so. */ | |
6228 | while (TREE_CODE (inner) == NOP_EXPR | |
6229 | || TREE_CODE (inner) == NON_LVALUE_EXPR | |
6230 | || TREE_CODE (inner) == CONVERT_EXPR | |
6231 | || TREE_CODE (inner) == PLUS_EXPR | |
6232 | || TREE_CODE (inner) == MINUS_EXPR) | |
6233 | inner = TREE_OPERAND (inner, 0); | |
6234 | ||
6235 | if (TREE_CODE (inner) == ADDR_EXPR) | |
6236 | { | |
6237 | inner = TREE_OPERAND (inner, 0); | |
6238 | ||
6239 | while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r') | |
6240 | inner = TREE_OPERAND (inner, 0); | |
6241 | ||
6242 | if (TREE_CODE (inner) == VAR_DECL | |
e7c692b2 | 6243 | && ! DECL_EXTERNAL (inner) |
88a3dbc1 RK |
6244 | && ! TREE_STATIC (inner) |
6245 | && DECL_CONTEXT (inner) == current_function_decl) | |
6246 | warning ("function returns address of local variable"); | |
6247 | } | |
6248 | ||
6249 | t = build (MODIFY_EXPR, TREE_TYPE (res), res, t); | |
1c2a9b35 | 6250 | TREE_SIDE_EFFECTS (t) = 1; |
400fbf9f JW |
6251 | expand_return (t); |
6252 | current_function_returns_value = 1; | |
6253 | } | |
6254 | } | |
6255 | \f | |
6256 | /* Start a C switch statement, testing expression EXP. | |
6257 | Return EXP if it is valid, an error node otherwise. */ | |
6258 | ||
6259 | tree | |
6260 | c_expand_start_case (exp) | |
6261 | tree exp; | |
6262 | { | |
6263 | register enum tree_code code = TREE_CODE (TREE_TYPE (exp)); | |
6264 | tree type = TREE_TYPE (exp); | |
6265 | ||
6266 | if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK) | |
6267 | { | |
6268 | error ("switch quantity not an integer"); | |
6269 | exp = error_mark_node; | |
6270 | } | |
6271 | else | |
6272 | { | |
6273 | tree index; | |
6cb72a7d | 6274 | type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); |
400fbf9f JW |
6275 | |
6276 | if (warn_traditional | |
6cb72a7d RS |
6277 | && (type == long_integer_type_node |
6278 | || type == long_unsigned_type_node)) | |
400fbf9f JW |
6279 | pedwarn ("`long' switch expression not converted to `int' in ANSI C"); |
6280 | ||
6281 | exp = default_conversion (exp); | |
6282 | type = TREE_TYPE (exp); | |
8d9bfdc5 | 6283 | index = get_unwidened (exp, NULL_TREE); |
400fbf9f JW |
6284 | /* We can't strip a conversion from a signed type to an unsigned, |
6285 | because if we did, int_fits_type_p would do the wrong thing | |
6286 | when checking case values for being in range, | |
6287 | and it's too hard to do the right thing. */ | |
6288 | if (TREE_UNSIGNED (TREE_TYPE (exp)) | |
6289 | == TREE_UNSIGNED (TREE_TYPE (index))) | |
6290 | exp = index; | |
6291 | } | |
6292 | ||
6293 | expand_start_case (1, exp, type, "switch statement"); | |
6294 | ||
6295 | return exp; | |
6296 | } |