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