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