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