* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / ada / utils2.c
blob31b0c6db9fdeb3845f1f3bb5c8f86a410696dcef
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * U T I L S 2 *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2007, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 * *
25 ****************************************************************************/
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "rtl.h"
33 #include "ggc.h"
34 #include "flags.h"
35 #include "output.h"
36 #include "ada.h"
37 #include "types.h"
38 #include "atree.h"
39 #include "stringt.h"
40 #include "namet.h"
41 #include "uintp.h"
42 #include "fe.h"
43 #include "elists.h"
44 #include "nlists.h"
45 #include "sinfo.h"
46 #include "einfo.h"
47 #include "ada-tree.h"
48 #include "gigi.h"
50 static tree find_common_type (tree, tree);
51 static bool contains_save_expr_p (tree);
52 static tree contains_null_expr (tree);
53 static tree compare_arrays (tree, tree, tree);
54 static tree nonbinary_modular_operation (enum tree_code, tree, tree, tree);
55 static tree build_simple_component_ref (tree, tree, tree, bool);
57 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR or other logical
58 operation.
60 This preparation consists of taking the ordinary representation of
61 an expression expr and producing a valid tree boolean expression
62 describing whether expr is nonzero. We could simply always do
64 build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
66 but we optimize comparisons, &&, ||, and !.
68 The resulting type should always be the same as the input type.
69 This function is simpler than the corresponding C version since
70 the only possible operands will be things of Boolean type. */
72 tree
73 gnat_truthvalue_conversion (tree expr)
75 tree type = TREE_TYPE (expr);
77 switch (TREE_CODE (expr))
79 case EQ_EXPR: case NE_EXPR: case LE_EXPR: case GE_EXPR:
80 case LT_EXPR: case GT_EXPR:
81 case TRUTH_ANDIF_EXPR:
82 case TRUTH_ORIF_EXPR:
83 case TRUTH_AND_EXPR:
84 case TRUTH_OR_EXPR:
85 case TRUTH_XOR_EXPR:
86 case ERROR_MARK:
87 return expr;
89 case INTEGER_CST:
90 return (integer_zerop (expr) ? convert (type, integer_zero_node)
91 : convert (type, integer_one_node));
93 case REAL_CST:
94 return (real_zerop (expr) ? convert (type, integer_zero_node)
95 : convert (type, integer_one_node));
97 case COND_EXPR:
98 /* Distribute the conversion into the arms of a COND_EXPR. */
99 return fold
100 (build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
101 gnat_truthvalue_conversion (TREE_OPERAND (expr, 1)),
102 gnat_truthvalue_conversion (TREE_OPERAND (expr, 2))));
104 default:
105 return build_binary_op (NE_EXPR, type, expr,
106 convert (type, integer_zero_node));
110 /* Return the base type of TYPE. */
112 tree
113 get_base_type (tree type)
115 if (TREE_CODE (type) == RECORD_TYPE
116 && TYPE_JUSTIFIED_MODULAR_P (type))
117 type = TREE_TYPE (TYPE_FIELDS (type));
119 while (TREE_TYPE (type)
120 && (TREE_CODE (type) == INTEGER_TYPE
121 || TREE_CODE (type) == REAL_TYPE))
122 type = TREE_TYPE (type);
124 return type;
127 /* EXP is a GCC tree representing an address. See if we can find how
128 strictly the object at that address is aligned. Return that alignment
129 in bits. If we don't know anything about the alignment, return 0. */
131 unsigned int
132 known_alignment (tree exp)
134 unsigned int this_alignment;
135 unsigned int lhs, rhs;
136 unsigned int type_alignment;
138 /* For pointer expressions, we know that the designated object is always at
139 least as strictly aligned as the designated subtype, so we account for
140 both type and expression information in this case.
142 Beware that we can still get a dummy designated subtype here (e.g. Taft
143 Amendement types), in which the alignment information is meaningless and
144 should be ignored.
146 We always compute a type_alignment value and return the MAX of it
147 compared with what we get from the expression tree. Just set the
148 type_alignment value to 0 when the type information is to be ignored. */
149 type_alignment
150 = ((POINTER_TYPE_P (TREE_TYPE (exp))
151 && !TYPE_IS_DUMMY_P (TREE_TYPE (TREE_TYPE (exp))))
152 ? TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))) : 0);
154 switch (TREE_CODE (exp))
156 case CONVERT_EXPR:
157 case NOP_EXPR:
158 case NON_LVALUE_EXPR:
159 /* Conversions between pointers and integers don't change the alignment
160 of the underlying object. */
161 this_alignment = known_alignment (TREE_OPERAND (exp, 0));
162 break;
164 case PLUS_EXPR:
165 case MINUS_EXPR:
166 /* If two address are added, the alignment of the result is the
167 minimum of the two alignments. */
168 lhs = known_alignment (TREE_OPERAND (exp, 0));
169 rhs = known_alignment (TREE_OPERAND (exp, 1));
170 this_alignment = MIN (lhs, rhs);
171 break;
173 case INTEGER_CST:
174 /* The first part of this represents the lowest bit in the constant,
175 but is it in bytes, not bits. */
176 this_alignment
177 = MIN (BITS_PER_UNIT
178 * (TREE_INT_CST_LOW (exp) & - TREE_INT_CST_LOW (exp)),
179 BIGGEST_ALIGNMENT);
180 break;
182 case MULT_EXPR:
183 /* If we know the alignment of just one side, use it. Otherwise,
184 use the product of the alignments. */
185 lhs = known_alignment (TREE_OPERAND (exp, 0));
186 rhs = known_alignment (TREE_OPERAND (exp, 1));
188 if (lhs == 0 || rhs == 0)
189 this_alignment = MIN (BIGGEST_ALIGNMENT, MAX (lhs, rhs));
190 else
191 this_alignment = MIN (BIGGEST_ALIGNMENT, lhs * rhs);
192 break;
194 case ADDR_EXPR:
195 this_alignment = expr_align (TREE_OPERAND (exp, 0));
196 break;
198 default:
199 this_alignment = 0;
200 break;
203 return MAX (type_alignment, this_alignment);
206 /* We have a comparison or assignment operation on two types, T1 and T2,
207 which are both either array types or both record types.
208 Return the type that both operands should be converted to, if any.
209 Otherwise return zero. */
211 static tree
212 find_common_type (tree t1, tree t2)
214 /* If either type is non-BLKmode, use it. Note that we know that we will
215 not have any alignment problems since if we did the non-BLKmode
216 type could not have been used. */
217 if (TYPE_MODE (t1) != BLKmode)
218 return t1;
219 else if (TYPE_MODE (t2) != BLKmode)
220 return t2;
222 /* If both types have constant size, use the smaller one. Keep returning
223 T1 if we have a tie, to be consistent with the other cases. */
224 if (TREE_CONSTANT (TYPE_SIZE (t1)) && TREE_CONSTANT (TYPE_SIZE (t2)))
225 return tree_int_cst_lt (TYPE_SIZE (t2), TYPE_SIZE (t1)) ? t2 : t1;
227 /* Otherwise, if either type has a constant size, use it. */
228 else if (TREE_CONSTANT (TYPE_SIZE (t1)))
229 return t1;
230 else if (TREE_CONSTANT (TYPE_SIZE (t2)))
231 return t2;
233 /* In this case, both types have variable size. It's probably
234 best to leave the "type mismatch" because changing it could
235 case a bad self-referential reference. */
236 return 0;
239 /* See if EXP contains a SAVE_EXPR in a position where we would
240 normally put it.
242 ??? This is a real kludge, but is probably the best approach short
243 of some very general solution. */
245 static bool
246 contains_save_expr_p (tree exp)
248 switch (TREE_CODE (exp))
250 case SAVE_EXPR:
251 return true;
253 case ADDR_EXPR: case INDIRECT_REF:
254 case COMPONENT_REF:
255 case NOP_EXPR: case CONVERT_EXPR: case VIEW_CONVERT_EXPR:
256 return contains_save_expr_p (TREE_OPERAND (exp, 0));
258 case CONSTRUCTOR:
260 tree value;
261 unsigned HOST_WIDE_INT ix;
263 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), ix, value)
264 if (contains_save_expr_p (value))
265 return true;
266 return false;
269 default:
270 return false;
274 /* See if EXP contains a NULL_EXPR in an expression we use for sizes. Return
275 it if so. This is used to detect types whose sizes involve computations
276 that are known to raise Constraint_Error. */
278 static tree
279 contains_null_expr (tree exp)
281 tree tem;
283 if (TREE_CODE (exp) == NULL_EXPR)
284 return exp;
286 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
288 case tcc_unary:
289 return contains_null_expr (TREE_OPERAND (exp, 0));
291 case tcc_comparison:
292 case tcc_binary:
293 tem = contains_null_expr (TREE_OPERAND (exp, 0));
294 if (tem)
295 return tem;
297 return contains_null_expr (TREE_OPERAND (exp, 1));
299 case tcc_expression:
300 switch (TREE_CODE (exp))
302 case SAVE_EXPR:
303 return contains_null_expr (TREE_OPERAND (exp, 0));
305 case COND_EXPR:
306 tem = contains_null_expr (TREE_OPERAND (exp, 0));
307 if (tem)
308 return tem;
310 tem = contains_null_expr (TREE_OPERAND (exp, 1));
311 if (tem)
312 return tem;
314 return contains_null_expr (TREE_OPERAND (exp, 2));
316 default:
317 return 0;
320 default:
321 return 0;
325 /* Return an expression tree representing an equality comparison of
326 A1 and A2, two objects of ARRAY_TYPE. The returned expression should
327 be of type RESULT_TYPE
329 Two arrays are equal in one of two ways: (1) if both have zero length
330 in some dimension (not necessarily the same dimension) or (2) if the
331 lengths in each dimension are equal and the data is equal. We perform the
332 length tests in as efficient a manner as possible. */
334 static tree
335 compare_arrays (tree result_type, tree a1, tree a2)
337 tree t1 = TREE_TYPE (a1);
338 tree t2 = TREE_TYPE (a2);
339 tree result = convert (result_type, integer_one_node);
340 tree a1_is_null = convert (result_type, integer_zero_node);
341 tree a2_is_null = convert (result_type, integer_zero_node);
342 bool length_zero_p = false;
344 /* Process each dimension separately and compare the lengths. If any
345 dimension has a size known to be zero, set SIZE_ZERO_P to 1 to
346 suppress the comparison of the data. */
347 while (TREE_CODE (t1) == ARRAY_TYPE && TREE_CODE (t2) == ARRAY_TYPE)
349 tree lb1 = TYPE_MIN_VALUE (TYPE_DOMAIN (t1));
350 tree ub1 = TYPE_MAX_VALUE (TYPE_DOMAIN (t1));
351 tree lb2 = TYPE_MIN_VALUE (TYPE_DOMAIN (t2));
352 tree ub2 = TYPE_MAX_VALUE (TYPE_DOMAIN (t2));
353 tree bt = get_base_type (TREE_TYPE (lb1));
354 tree length1 = fold (build2 (MINUS_EXPR, bt, ub1, lb1));
355 tree length2 = fold (build2 (MINUS_EXPR, bt, ub2, lb2));
356 tree nbt;
357 tree tem;
358 tree comparison, this_a1_is_null, this_a2_is_null;
360 /* If the length of the first array is a constant, swap our operands
361 unless the length of the second array is the constant zero.
362 Note that we have set the `length' values to the length - 1. */
363 if (TREE_CODE (length1) == INTEGER_CST
364 && !integer_zerop (fold (build2 (PLUS_EXPR, bt, length2,
365 convert (bt, integer_one_node)))))
367 tem = a1, a1 = a2, a2 = tem;
368 tem = t1, t1 = t2, t2 = tem;
369 tem = lb1, lb1 = lb2, lb2 = tem;
370 tem = ub1, ub1 = ub2, ub2 = tem;
371 tem = length1, length1 = length2, length2 = tem;
372 tem = a1_is_null, a1_is_null = a2_is_null, a2_is_null = tem;
375 /* If the length of this dimension in the second array is the constant
376 zero, we can just go inside the original bounds for the first
377 array and see if last < first. */
378 if (integer_zerop (fold (build2 (PLUS_EXPR, bt, length2,
379 convert (bt, integer_one_node)))))
381 tree ub = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
382 tree lb = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
384 comparison = build_binary_op (LT_EXPR, result_type, ub, lb);
385 comparison = SUBSTITUTE_PLACEHOLDER_IN_EXPR (comparison, a1);
386 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
388 length_zero_p = true;
389 this_a1_is_null = comparison;
390 this_a2_is_null = convert (result_type, integer_one_node);
393 /* If the length is some other constant value, we know that the
394 this dimension in the first array cannot be superflat, so we
395 can just use its length from the actual stored bounds. */
396 else if (TREE_CODE (length2) == INTEGER_CST)
398 ub1 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
399 lb1 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
400 ub2 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t2)));
401 lb2 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t2)));
402 nbt = get_base_type (TREE_TYPE (ub1));
404 comparison
405 = build_binary_op (EQ_EXPR, result_type,
406 build_binary_op (MINUS_EXPR, nbt, ub1, lb1),
407 build_binary_op (MINUS_EXPR, nbt, ub2, lb2));
409 /* Note that we know that UB2 and LB2 are constant and hence
410 cannot contain a PLACEHOLDER_EXPR. */
412 comparison = SUBSTITUTE_PLACEHOLDER_IN_EXPR (comparison, a1);
413 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
415 this_a1_is_null = build_binary_op (LT_EXPR, result_type, ub1, lb1);
416 this_a2_is_null = convert (result_type, integer_zero_node);
419 /* Otherwise compare the computed lengths. */
420 else
422 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
423 length2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length2, a2);
425 comparison
426 = build_binary_op (EQ_EXPR, result_type, length1, length2);
428 this_a1_is_null
429 = build_binary_op (LT_EXPR, result_type, length1,
430 convert (bt, integer_zero_node));
431 this_a2_is_null
432 = build_binary_op (LT_EXPR, result_type, length2,
433 convert (bt, integer_zero_node));
436 result = build_binary_op (TRUTH_ANDIF_EXPR, result_type,
437 result, comparison);
439 a1_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
440 this_a1_is_null, a1_is_null);
441 a2_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
442 this_a2_is_null, a2_is_null);
444 t1 = TREE_TYPE (t1);
445 t2 = TREE_TYPE (t2);
448 /* Unless the size of some bound is known to be zero, compare the
449 data in the array. */
450 if (!length_zero_p)
452 tree type = find_common_type (TREE_TYPE (a1), TREE_TYPE (a2));
454 if (type)
455 a1 = convert (type, a1), a2 = convert (type, a2);
457 result = build_binary_op (TRUTH_ANDIF_EXPR, result_type, result,
458 fold (build2 (EQ_EXPR, result_type, a1, a2)));
462 /* The result is also true if both sizes are zero. */
463 result = build_binary_op (TRUTH_ORIF_EXPR, result_type,
464 build_binary_op (TRUTH_ANDIF_EXPR, result_type,
465 a1_is_null, a2_is_null),
466 result);
468 /* If either operand contains SAVE_EXPRs, they have to be evaluated before
469 starting the comparison above since the place it would be otherwise
470 evaluated would be wrong. */
472 if (contains_save_expr_p (a1))
473 result = build2 (COMPOUND_EXPR, result_type, a1, result);
475 if (contains_save_expr_p (a2))
476 result = build2 (COMPOUND_EXPR, result_type, a2, result);
478 return result;
481 /* Compute the result of applying OP_CODE to LHS and RHS, where both are of
482 type TYPE. We know that TYPE is a modular type with a nonbinary
483 modulus. */
485 static tree
486 nonbinary_modular_operation (enum tree_code op_code, tree type, tree lhs,
487 tree rhs)
489 tree modulus = TYPE_MODULUS (type);
490 unsigned int needed_precision = tree_floor_log2 (modulus) + 1;
491 unsigned int precision;
492 bool unsignedp = true;
493 tree op_type = type;
494 tree result;
496 /* If this is an addition of a constant, convert it to a subtraction
497 of a constant since we can do that faster. */
498 if (op_code == PLUS_EXPR && TREE_CODE (rhs) == INTEGER_CST)
499 rhs = fold (build2 (MINUS_EXPR, type, modulus, rhs)), op_code = MINUS_EXPR;
501 /* For the logical operations, we only need PRECISION bits. For
502 addition and subtraction, we need one more and for multiplication we
503 need twice as many. But we never want to make a size smaller than
504 our size. */
505 if (op_code == PLUS_EXPR || op_code == MINUS_EXPR)
506 needed_precision += 1;
507 else if (op_code == MULT_EXPR)
508 needed_precision *= 2;
510 precision = MAX (needed_precision, TYPE_PRECISION (op_type));
512 /* Unsigned will do for everything but subtraction. */
513 if (op_code == MINUS_EXPR)
514 unsignedp = false;
516 /* If our type is the wrong signedness or isn't wide enough, make a new
517 type and convert both our operands to it. */
518 if (TYPE_PRECISION (op_type) < precision
519 || TYPE_UNSIGNED (op_type) != unsignedp)
521 /* Copy the node so we ensure it can be modified to make it modular. */
522 op_type = copy_node (gnat_type_for_size (precision, unsignedp));
523 modulus = convert (op_type, modulus);
524 SET_TYPE_MODULUS (op_type, modulus);
525 TYPE_MODULAR_P (op_type) = 1;
526 lhs = convert (op_type, lhs);
527 rhs = convert (op_type, rhs);
530 /* Do the operation, then we'll fix it up. */
531 result = fold (build2 (op_code, op_type, lhs, rhs));
533 /* For multiplication, we have no choice but to do a full modulus
534 operation. However, we want to do this in the narrowest
535 possible size. */
536 if (op_code == MULT_EXPR)
538 tree div_type = copy_node (gnat_type_for_size (needed_precision, 1));
539 modulus = convert (div_type, modulus);
540 SET_TYPE_MODULUS (div_type, modulus);
541 TYPE_MODULAR_P (div_type) = 1;
542 result = convert (op_type,
543 fold (build2 (TRUNC_MOD_EXPR, div_type,
544 convert (div_type, result), modulus)));
547 /* For subtraction, add the modulus back if we are negative. */
548 else if (op_code == MINUS_EXPR)
550 result = save_expr (result);
551 result = fold (build3 (COND_EXPR, op_type,
552 build2 (LT_EXPR, integer_type_node, result,
553 convert (op_type, integer_zero_node)),
554 fold (build2 (PLUS_EXPR, op_type,
555 result, modulus)),
556 result));
559 /* For the other operations, subtract the modulus if we are >= it. */
560 else
562 result = save_expr (result);
563 result = fold (build3 (COND_EXPR, op_type,
564 build2 (GE_EXPR, integer_type_node,
565 result, modulus),
566 fold (build2 (MINUS_EXPR, op_type,
567 result, modulus)),
568 result));
571 return convert (type, result);
574 /* Make a binary operation of kind OP_CODE. RESULT_TYPE is the type
575 desired for the result. Usually the operation is to be performed
576 in that type. For MODIFY_EXPR and ARRAY_REF, RESULT_TYPE may be 0
577 in which case the type to be used will be derived from the operands.
579 This function is very much unlike the ones for C and C++ since we
580 have already done any type conversion and matching required. All we
581 have to do here is validate the work done by SEM and handle subtypes. */
583 tree
584 build_binary_op (enum tree_code op_code, tree result_type,
585 tree left_operand, tree right_operand)
587 tree left_type = TREE_TYPE (left_operand);
588 tree right_type = TREE_TYPE (right_operand);
589 tree left_base_type = get_base_type (left_type);
590 tree right_base_type = get_base_type (right_type);
591 tree operation_type = result_type;
592 tree best_type = NULL_TREE;
593 tree modulus;
594 tree result;
595 bool has_side_effects = false;
597 if (operation_type
598 && TREE_CODE (operation_type) == RECORD_TYPE
599 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
600 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
602 if (operation_type
603 && !AGGREGATE_TYPE_P (operation_type)
604 && TYPE_EXTRA_SUBTYPE_P (operation_type))
605 operation_type = get_base_type (operation_type);
607 modulus = (operation_type && TREE_CODE (operation_type) == INTEGER_TYPE
608 && TYPE_MODULAR_P (operation_type)
609 ? TYPE_MODULUS (operation_type) : 0);
611 switch (op_code)
613 case MODIFY_EXPR:
614 /* If there were any integral or pointer conversions on LHS, remove
615 them; we'll be putting them back below if needed. Likewise for
616 conversions between array and record types. But don't do this if
617 the right operand is not BLKmode (for packed arrays)
618 unless we are not changing the mode. */
619 while ((TREE_CODE (left_operand) == CONVERT_EXPR
620 || TREE_CODE (left_operand) == NOP_EXPR
621 || TREE_CODE (left_operand) == VIEW_CONVERT_EXPR)
622 && (((INTEGRAL_TYPE_P (left_type)
623 || POINTER_TYPE_P (left_type))
624 && (INTEGRAL_TYPE_P (TREE_TYPE
625 (TREE_OPERAND (left_operand, 0)))
626 || POINTER_TYPE_P (TREE_TYPE
627 (TREE_OPERAND (left_operand, 0)))))
628 || (((TREE_CODE (left_type) == RECORD_TYPE
629 /* Don't remove conversions to justified modular
630 types. */
631 && !TYPE_JUSTIFIED_MODULAR_P (left_type))
632 || TREE_CODE (left_type) == ARRAY_TYPE)
633 && ((TREE_CODE (TREE_TYPE
634 (TREE_OPERAND (left_operand, 0)))
635 == RECORD_TYPE)
636 || (TREE_CODE (TREE_TYPE
637 (TREE_OPERAND (left_operand, 0)))
638 == ARRAY_TYPE))
639 && (TYPE_MODE (right_type) == BLKmode
640 || (TYPE_MODE (left_type)
641 == TYPE_MODE (TREE_TYPE
642 (TREE_OPERAND
643 (left_operand, 0))))))))
645 left_operand = TREE_OPERAND (left_operand, 0);
646 left_type = TREE_TYPE (left_operand);
649 if (!operation_type)
650 operation_type = left_type;
652 /* If we are copying one array or record to another, find the best type
653 to use. */
654 if (((TREE_CODE (left_type) == ARRAY_TYPE
655 && TREE_CODE (right_type) == ARRAY_TYPE)
656 || (TREE_CODE (left_type) == RECORD_TYPE
657 && TREE_CODE (right_type) == RECORD_TYPE))
658 && (best_type = find_common_type (left_type, right_type)))
659 operation_type = best_type;
661 /* If a class-wide type may be involved, force use of the RHS type. */
662 if ((TREE_CODE (right_type) == RECORD_TYPE
663 || TREE_CODE (right_type) == UNION_TYPE)
664 && TYPE_ALIGN_OK (right_type))
665 operation_type = right_type;
667 /* Ensure everything on the LHS is valid. If we have a field reference,
668 strip anything that get_inner_reference can handle. Then remove any
669 conversions with type types having the same code and mode. Mark
670 VIEW_CONVERT_EXPRs with TREE_ADDRESSABLE. When done, we must have
671 either an INDIRECT_REF or a decl. */
672 result = left_operand;
673 while (1)
675 tree restype = TREE_TYPE (result);
677 if (TREE_CODE (result) == COMPONENT_REF
678 || TREE_CODE (result) == ARRAY_REF
679 || TREE_CODE (result) == ARRAY_RANGE_REF)
680 while (handled_component_p (result))
681 result = TREE_OPERAND (result, 0);
682 else if (TREE_CODE (result) == REALPART_EXPR
683 || TREE_CODE (result) == IMAGPART_EXPR
684 || ((TREE_CODE (result) == NOP_EXPR
685 || TREE_CODE (result) == CONVERT_EXPR)
686 && (((TREE_CODE (restype)
687 == TREE_CODE (TREE_TYPE
688 (TREE_OPERAND (result, 0))))
689 && (TYPE_MODE (TREE_TYPE
690 (TREE_OPERAND (result, 0)))
691 == TYPE_MODE (restype)))
692 || TYPE_ALIGN_OK (restype))))
693 result = TREE_OPERAND (result, 0);
694 else if (TREE_CODE (result) == VIEW_CONVERT_EXPR)
696 TREE_ADDRESSABLE (result) = 1;
697 result = TREE_OPERAND (result, 0);
699 else
700 break;
703 gcc_assert (TREE_CODE (result) == INDIRECT_REF
704 || TREE_CODE (result) == NULL_EXPR || DECL_P (result));
706 /* Convert the right operand to the operation type unless
707 it is either already of the correct type or if the type
708 involves a placeholder, since the RHS may not have the same
709 record type. */
710 if (operation_type != right_type
711 && (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (operation_type))))
713 right_operand = convert (operation_type, right_operand);
714 right_type = operation_type;
717 /* If the left operand is not the same type as the operation type,
718 surround it in a VIEW_CONVERT_EXPR. */
719 if (left_type != operation_type)
720 left_operand = unchecked_convert (operation_type, left_operand, false);
722 has_side_effects = true;
723 modulus = NULL_TREE;
724 break;
726 case ARRAY_REF:
727 if (!operation_type)
728 operation_type = TREE_TYPE (left_type);
730 /* ... fall through ... */
732 case ARRAY_RANGE_REF:
734 /* First convert the right operand to its base type. This will
735 prevent unneeded signedness conversions when sizetype is wider than
736 integer. */
737 right_operand = convert (right_base_type, right_operand);
738 right_operand = convert (TYPE_DOMAIN (left_type), right_operand);
740 if (!TREE_CONSTANT (right_operand)
741 || !TREE_CONSTANT (TYPE_MIN_VALUE (right_type)))
742 gnat_mark_addressable (left_operand);
744 modulus = NULL_TREE;
745 break;
747 case GE_EXPR:
748 case LE_EXPR:
749 case GT_EXPR:
750 case LT_EXPR:
751 gcc_assert (!POINTER_TYPE_P (left_type));
753 /* ... fall through ... */
755 case EQ_EXPR:
756 case NE_EXPR:
757 /* If either operand is a NULL_EXPR, just return a new one. */
758 if (TREE_CODE (left_operand) == NULL_EXPR)
759 return build2 (op_code, result_type,
760 build1 (NULL_EXPR, integer_type_node,
761 TREE_OPERAND (left_operand, 0)),
762 integer_zero_node);
764 else if (TREE_CODE (right_operand) == NULL_EXPR)
765 return build2 (op_code, result_type,
766 build1 (NULL_EXPR, integer_type_node,
767 TREE_OPERAND (right_operand, 0)),
768 integer_zero_node);
770 /* If either object is a justified modular types, get the
771 fields from within. */
772 if (TREE_CODE (left_type) == RECORD_TYPE
773 && TYPE_JUSTIFIED_MODULAR_P (left_type))
775 left_operand = convert (TREE_TYPE (TYPE_FIELDS (left_type)),
776 left_operand);
777 left_type = TREE_TYPE (left_operand);
778 left_base_type = get_base_type (left_type);
781 if (TREE_CODE (right_type) == RECORD_TYPE
782 && TYPE_JUSTIFIED_MODULAR_P (right_type))
784 right_operand = convert (TREE_TYPE (TYPE_FIELDS (right_type)),
785 right_operand);
786 right_type = TREE_TYPE (right_operand);
787 right_base_type = get_base_type (right_type);
790 /* If both objects are arrays, compare them specially. */
791 if ((TREE_CODE (left_type) == ARRAY_TYPE
792 || (TREE_CODE (left_type) == INTEGER_TYPE
793 && TYPE_HAS_ACTUAL_BOUNDS_P (left_type)))
794 && (TREE_CODE (right_type) == ARRAY_TYPE
795 || (TREE_CODE (right_type) == INTEGER_TYPE
796 && TYPE_HAS_ACTUAL_BOUNDS_P (right_type))))
798 result = compare_arrays (result_type, left_operand, right_operand);
800 if (op_code == NE_EXPR)
801 result = invert_truthvalue (result);
802 else
803 gcc_assert (op_code == EQ_EXPR);
805 return result;
808 /* Otherwise, the base types must be the same unless the objects are
809 records. If we have records, use the best type and convert both
810 operands to that type. */
811 if (left_base_type != right_base_type)
813 if (TREE_CODE (left_base_type) == RECORD_TYPE
814 && TREE_CODE (right_base_type) == RECORD_TYPE)
816 /* The only way these are permitted to be the same is if both
817 types have the same name. In that case, one of them must
818 not be self-referential. Use that one as the best type.
819 Even better is if one is of fixed size. */
820 best_type = NULL_TREE;
822 gcc_assert (TYPE_NAME (left_base_type)
823 && (TYPE_NAME (left_base_type)
824 == TYPE_NAME (right_base_type)));
826 if (TREE_CONSTANT (TYPE_SIZE (left_base_type)))
827 best_type = left_base_type;
828 else if (TREE_CONSTANT (TYPE_SIZE (right_base_type)))
829 best_type = right_base_type;
830 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (left_base_type)))
831 best_type = left_base_type;
832 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (right_base_type)))
833 best_type = right_base_type;
834 else
835 gcc_unreachable ();
837 left_operand = convert (best_type, left_operand);
838 right_operand = convert (best_type, right_operand);
840 else
841 gcc_unreachable ();
844 /* If we are comparing a fat pointer against zero, we need to
845 just compare the data pointer. */
846 else if (TYPE_FAT_POINTER_P (left_base_type)
847 && TREE_CODE (right_operand) == CONSTRUCTOR
848 && integer_zerop (VEC_index (constructor_elt,
849 CONSTRUCTOR_ELTS (right_operand),
851 ->value))
853 right_operand = build_component_ref (left_operand, NULL_TREE,
854 TYPE_FIELDS (left_base_type),
855 false);
856 left_operand = convert (TREE_TYPE (right_operand),
857 integer_zero_node);
859 else
861 left_operand = convert (left_base_type, left_operand);
862 right_operand = convert (right_base_type, right_operand);
865 modulus = NULL_TREE;
866 break;
868 case PREINCREMENT_EXPR:
869 case PREDECREMENT_EXPR:
870 case POSTINCREMENT_EXPR:
871 case POSTDECREMENT_EXPR:
872 /* In these, the result type and the left operand type should be the
873 same. Do the operation in the base type of those and convert the
874 right operand (which is an integer) to that type.
876 Note that these operations are only used in loop control where
877 we guarantee that no overflow can occur. So nothing special need
878 be done for modular types. */
880 gcc_assert (left_type == result_type);
881 operation_type = get_base_type (result_type);
882 left_operand = convert (operation_type, left_operand);
883 right_operand = convert (operation_type, right_operand);
884 has_side_effects = true;
885 modulus = NULL_TREE;
886 break;
888 case LSHIFT_EXPR:
889 case RSHIFT_EXPR:
890 case LROTATE_EXPR:
891 case RROTATE_EXPR:
892 /* The RHS of a shift can be any type. Also, ignore any modulus
893 (we used to abort, but this is needed for unchecked conversion
894 to modular types). Otherwise, processing is the same as normal. */
895 gcc_assert (operation_type == left_base_type);
896 modulus = NULL_TREE;
897 left_operand = convert (operation_type, left_operand);
898 break;
900 case TRUTH_ANDIF_EXPR:
901 case TRUTH_ORIF_EXPR:
902 case TRUTH_AND_EXPR:
903 case TRUTH_OR_EXPR:
904 case TRUTH_XOR_EXPR:
905 left_operand = gnat_truthvalue_conversion (left_operand);
906 right_operand = gnat_truthvalue_conversion (right_operand);
907 goto common;
909 case BIT_AND_EXPR:
910 case BIT_IOR_EXPR:
911 case BIT_XOR_EXPR:
912 /* For binary modulus, if the inputs are in range, so are the
913 outputs. */
914 if (modulus && integer_pow2p (modulus))
915 modulus = NULL_TREE;
917 goto common;
919 case COMPLEX_EXPR:
920 gcc_assert (TREE_TYPE (result_type) == left_base_type
921 && TREE_TYPE (result_type) == right_base_type);
922 left_operand = convert (left_base_type, left_operand);
923 right_operand = convert (right_base_type, right_operand);
924 break;
926 case TRUNC_DIV_EXPR: case TRUNC_MOD_EXPR:
927 case CEIL_DIV_EXPR: case CEIL_MOD_EXPR:
928 case FLOOR_DIV_EXPR: case FLOOR_MOD_EXPR:
929 case ROUND_DIV_EXPR: case ROUND_MOD_EXPR:
930 /* These always produce results lower than either operand. */
931 modulus = NULL_TREE;
932 goto common;
934 default:
935 common:
936 /* The result type should be the same as the base types of the
937 both operands (and they should be the same). Convert
938 everything to the result type. */
940 gcc_assert (operation_type == left_base_type
941 && left_base_type == right_base_type);
942 left_operand = convert (operation_type, left_operand);
943 right_operand = convert (operation_type, right_operand);
946 if (modulus && !integer_pow2p (modulus))
948 result = nonbinary_modular_operation (op_code, operation_type,
949 left_operand, right_operand);
950 modulus = NULL_TREE;
952 /* If either operand is a NULL_EXPR, just return a new one. */
953 else if (TREE_CODE (left_operand) == NULL_EXPR)
954 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (left_operand, 0));
955 else if (TREE_CODE (right_operand) == NULL_EXPR)
956 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (right_operand, 0));
957 else if (op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
958 result = fold (build4 (op_code, operation_type, left_operand,
959 right_operand, NULL_TREE, NULL_TREE));
960 else
961 result
962 = fold (build2 (op_code, operation_type, left_operand, right_operand));
964 TREE_SIDE_EFFECTS (result) |= has_side_effects;
965 TREE_CONSTANT (result)
966 |= (TREE_CONSTANT (left_operand) & TREE_CONSTANT (right_operand)
967 && op_code != ARRAY_REF && op_code != ARRAY_RANGE_REF);
969 if ((op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
970 && TYPE_VOLATILE (operation_type))
971 TREE_THIS_VOLATILE (result) = 1;
973 /* If we are working with modular types, perform the MOD operation
974 if something above hasn't eliminated the need for it. */
975 if (modulus)
976 result = fold (build2 (FLOOR_MOD_EXPR, operation_type, result,
977 convert (operation_type, modulus)));
979 if (result_type && result_type != operation_type)
980 result = convert (result_type, result);
982 return result;
985 /* Similar, but for unary operations. */
987 tree
988 build_unary_op (enum tree_code op_code, tree result_type, tree operand)
990 tree type = TREE_TYPE (operand);
991 tree base_type = get_base_type (type);
992 tree operation_type = result_type;
993 tree result;
994 bool side_effects = false;
996 if (operation_type
997 && TREE_CODE (operation_type) == RECORD_TYPE
998 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
999 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
1001 if (operation_type
1002 && !AGGREGATE_TYPE_P (operation_type)
1003 && TYPE_EXTRA_SUBTYPE_P (operation_type))
1004 operation_type = get_base_type (operation_type);
1006 switch (op_code)
1008 case REALPART_EXPR:
1009 case IMAGPART_EXPR:
1010 if (!operation_type)
1011 result_type = operation_type = TREE_TYPE (type);
1012 else
1013 gcc_assert (result_type == TREE_TYPE (type));
1015 result = fold (build1 (op_code, operation_type, operand));
1016 break;
1018 case TRUTH_NOT_EXPR:
1019 gcc_assert (result_type == base_type);
1020 result = invert_truthvalue (gnat_truthvalue_conversion (operand));
1021 break;
1023 case ATTR_ADDR_EXPR:
1024 case ADDR_EXPR:
1025 switch (TREE_CODE (operand))
1027 case INDIRECT_REF:
1028 case UNCONSTRAINED_ARRAY_REF:
1029 result = TREE_OPERAND (operand, 0);
1031 /* Make sure the type here is a pointer, not a reference.
1032 GCC wants pointer types for function addresses. */
1033 if (!result_type)
1034 result_type = build_pointer_type (type);
1035 break;
1037 case NULL_EXPR:
1038 result = operand;
1039 TREE_TYPE (result) = type = build_pointer_type (type);
1040 break;
1042 case ARRAY_REF:
1043 case ARRAY_RANGE_REF:
1044 case COMPONENT_REF:
1045 case BIT_FIELD_REF:
1046 /* If this is for 'Address, find the address of the prefix and
1047 add the offset to the field. Otherwise, do this the normal
1048 way. */
1049 if (op_code == ATTR_ADDR_EXPR)
1051 HOST_WIDE_INT bitsize;
1052 HOST_WIDE_INT bitpos;
1053 tree offset, inner;
1054 enum machine_mode mode;
1055 int unsignedp, volatilep;
1057 inner = get_inner_reference (operand, &bitsize, &bitpos, &offset,
1058 &mode, &unsignedp, &volatilep,
1059 false);
1061 /* If INNER is a padding type whose field has a self-referential
1062 size, convert to that inner type. We know the offset is zero
1063 and we need to have that type visible. */
1064 if (TREE_CODE (TREE_TYPE (inner)) == RECORD_TYPE
1065 && TYPE_IS_PADDING_P (TREE_TYPE (inner))
1066 && (CONTAINS_PLACEHOLDER_P
1067 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
1068 (TREE_TYPE (inner)))))))
1069 inner = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (inner))),
1070 inner);
1072 /* Compute the offset as a byte offset from INNER. */
1073 if (!offset)
1074 offset = size_zero_node;
1076 if (bitpos % BITS_PER_UNIT != 0)
1077 post_error
1078 ("taking address of object not aligned on storage unit?",
1079 error_gnat_node);
1081 offset = size_binop (PLUS_EXPR, offset,
1082 size_int (bitpos / BITS_PER_UNIT));
1084 /* Take the address of INNER, convert the offset to void *, and
1085 add then. It will later be converted to the desired result
1086 type, if any. */
1087 inner = build_unary_op (ADDR_EXPR, NULL_TREE, inner);
1088 inner = convert (ptr_void_type_node, inner);
1089 offset = convert (ptr_void_type_node, offset);
1090 result = build_binary_op (PLUS_EXPR, ptr_void_type_node,
1091 inner, offset);
1092 result = convert (build_pointer_type (TREE_TYPE (operand)),
1093 result);
1094 break;
1096 goto common;
1098 case CONSTRUCTOR:
1099 /* If this is just a constructor for a padded record, we can
1100 just take the address of the single field and convert it to
1101 a pointer to our type. */
1102 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
1104 result = (VEC_index (constructor_elt,
1105 CONSTRUCTOR_ELTS (operand),
1107 ->value);
1109 result = convert (build_pointer_type (TREE_TYPE (operand)),
1110 build_unary_op (ADDR_EXPR, NULL_TREE, result));
1111 break;
1114 goto common;
1116 case NOP_EXPR:
1117 if (AGGREGATE_TYPE_P (type)
1118 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (operand, 0))))
1119 return build_unary_op (ADDR_EXPR, result_type,
1120 TREE_OPERAND (operand, 0));
1122 /* ... fallthru ... */
1124 case VIEW_CONVERT_EXPR:
1125 /* If this just a variant conversion or if the conversion doesn't
1126 change the mode, get the result type from this type and go down.
1127 This is needed for conversions of CONST_DECLs, to eventually get
1128 to the address of their CORRESPONDING_VARs. */
1129 if ((TYPE_MAIN_VARIANT (type)
1130 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (operand, 0))))
1131 || (TYPE_MODE (type) != BLKmode
1132 && (TYPE_MODE (type)
1133 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (operand, 0))))))
1134 return build_unary_op (ADDR_EXPR,
1135 (result_type ? result_type
1136 : build_pointer_type (type)),
1137 TREE_OPERAND (operand, 0));
1138 goto common;
1140 case CONST_DECL:
1141 operand = DECL_CONST_CORRESPONDING_VAR (operand);
1143 /* ... fall through ... */
1145 default:
1146 common:
1148 /* If we are taking the address of a padded record whose field is
1149 contains a template, take the address of the template. */
1150 if (TREE_CODE (type) == RECORD_TYPE
1151 && TYPE_IS_PADDING_P (type)
1152 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
1153 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
1155 type = TREE_TYPE (TYPE_FIELDS (type));
1156 operand = convert (type, operand);
1159 if (type != error_mark_node)
1160 operation_type = build_pointer_type (type);
1162 gnat_mark_addressable (operand);
1163 result = fold (build1 (ADDR_EXPR, operation_type, operand));
1166 TREE_CONSTANT (result) = staticp (operand) || TREE_CONSTANT (operand);
1167 break;
1169 case INDIRECT_REF:
1170 /* If we want to refer to an entire unconstrained array,
1171 make up an expression to do so. This will never survive to
1172 the backend. If TYPE is a thin pointer, first convert the
1173 operand to a fat pointer. */
1174 if (TYPE_THIN_POINTER_P (type)
1175 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
1177 operand
1178 = convert (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))),
1179 operand);
1180 type = TREE_TYPE (operand);
1183 if (TYPE_FAT_POINTER_P (type))
1185 result = build1 (UNCONSTRAINED_ARRAY_REF,
1186 TYPE_UNCONSTRAINED_ARRAY (type), operand);
1187 TREE_READONLY (result) = TREE_STATIC (result)
1188 = TYPE_READONLY (TYPE_UNCONSTRAINED_ARRAY (type));
1190 else if (TREE_CODE (operand) == ADDR_EXPR)
1191 result = TREE_OPERAND (operand, 0);
1193 else
1195 result = fold (build1 (op_code, TREE_TYPE (type), operand));
1196 TREE_READONLY (result) = TYPE_READONLY (TREE_TYPE (type));
1199 side_effects
1200 = (!TYPE_FAT_POINTER_P (type) && TYPE_VOLATILE (TREE_TYPE (type)));
1201 break;
1203 case NEGATE_EXPR:
1204 case BIT_NOT_EXPR:
1206 tree modulus = ((operation_type
1207 && TREE_CODE (operation_type) == INTEGER_TYPE
1208 && TYPE_MODULAR_P (operation_type))
1209 ? TYPE_MODULUS (operation_type) : 0);
1210 int mod_pow2 = modulus && integer_pow2p (modulus);
1212 /* If this is a modular type, there are various possibilities
1213 depending on the operation and whether the modulus is a
1214 power of two or not. */
1216 if (modulus)
1218 gcc_assert (operation_type == base_type);
1219 operand = convert (operation_type, operand);
1221 /* The fastest in the negate case for binary modulus is
1222 the straightforward code; the TRUNC_MOD_EXPR below
1223 is an AND operation. */
1224 if (op_code == NEGATE_EXPR && mod_pow2)
1225 result = fold (build2 (TRUNC_MOD_EXPR, operation_type,
1226 fold (build1 (NEGATE_EXPR, operation_type,
1227 operand)),
1228 modulus));
1230 /* For nonbinary negate case, return zero for zero operand,
1231 else return the modulus minus the operand. If the modulus
1232 is a power of two minus one, we can do the subtraction
1233 as an XOR since it is equivalent and faster on most machines. */
1234 else if (op_code == NEGATE_EXPR && !mod_pow2)
1236 if (integer_pow2p (fold (build2 (PLUS_EXPR, operation_type,
1237 modulus,
1238 convert (operation_type,
1239 integer_one_node)))))
1240 result = fold (build2 (BIT_XOR_EXPR, operation_type,
1241 operand, modulus));
1242 else
1243 result = fold (build2 (MINUS_EXPR, operation_type,
1244 modulus, operand));
1246 result = fold (build3 (COND_EXPR, operation_type,
1247 fold (build2 (NE_EXPR,
1248 integer_type_node,
1249 operand,
1250 convert
1251 (operation_type,
1252 integer_zero_node))),
1253 result, operand));
1255 else
1257 /* For the NOT cases, we need a constant equal to
1258 the modulus minus one. For a binary modulus, we
1259 XOR against the constant and subtract the operand from
1260 that constant for nonbinary modulus. */
1262 tree cnst = fold (build2 (MINUS_EXPR, operation_type, modulus,
1263 convert (operation_type,
1264 integer_one_node)));
1266 if (mod_pow2)
1267 result = fold (build2 (BIT_XOR_EXPR, operation_type,
1268 operand, cnst));
1269 else
1270 result = fold (build2 (MINUS_EXPR, operation_type,
1271 cnst, operand));
1274 break;
1278 /* ... fall through ... */
1280 default:
1281 gcc_assert (operation_type == base_type);
1282 result = fold (build1 (op_code, operation_type, convert (operation_type,
1283 operand)));
1286 if (side_effects)
1288 TREE_SIDE_EFFECTS (result) = 1;
1289 if (TREE_CODE (result) == INDIRECT_REF)
1290 TREE_THIS_VOLATILE (result) = TYPE_VOLATILE (TREE_TYPE (result));
1293 if (result_type && TREE_TYPE (result) != result_type)
1294 result = convert (result_type, result);
1296 return result;
1299 /* Similar, but for COND_EXPR. */
1301 tree
1302 build_cond_expr (tree result_type, tree condition_operand,
1303 tree true_operand, tree false_operand)
1305 tree result;
1306 bool addr_p = false;
1308 /* The front-end verifies that result, true and false operands have same base
1309 type. Convert everything to the result type. */
1311 true_operand = convert (result_type, true_operand);
1312 false_operand = convert (result_type, false_operand);
1314 /* If the result type is unconstrained, take the address of
1315 the operands and then dereference our result. */
1316 if (TREE_CODE (result_type) == UNCONSTRAINED_ARRAY_TYPE
1317 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (result_type)))
1319 addr_p = true;
1320 result_type = build_pointer_type (result_type);
1321 true_operand = build_unary_op (ADDR_EXPR, result_type, true_operand);
1322 false_operand = build_unary_op (ADDR_EXPR, result_type, false_operand);
1325 result = fold (build3 (COND_EXPR, result_type, condition_operand,
1326 true_operand, false_operand));
1328 /* If either operand is a SAVE_EXPR (possibly surrounded by
1329 arithmetic, make sure it gets done. */
1330 true_operand = skip_simple_arithmetic (true_operand);
1331 false_operand = skip_simple_arithmetic (false_operand);
1333 if (TREE_CODE (true_operand) == SAVE_EXPR)
1334 result = build2 (COMPOUND_EXPR, result_type, true_operand, result);
1336 if (TREE_CODE (false_operand) == SAVE_EXPR)
1337 result = build2 (COMPOUND_EXPR, result_type, false_operand, result);
1339 /* ??? Seems the code above is wrong, as it may move ahead of the COND
1340 SAVE_EXPRs with side effects and not shared by both arms. */
1342 if (addr_p)
1343 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1345 return result;
1348 /* Similar, but for RETURN_EXPR. If RESULT_DECL is non-zero, build
1349 a RETURN_EXPR around the assignment of RET_VAL to RESULT_DECL.
1350 If RESULT_DECL is zero, build a bare RETURN_EXPR. */
1352 tree
1353 build_return_expr (tree result_decl, tree ret_val)
1355 tree result_expr;
1357 if (result_decl)
1359 /* The gimplifier explicitly enforces the following invariant:
1361 RETURN_EXPR
1363 MODIFY_EXPR
1366 RESULT_DECL ...
1368 As a consequence, type-homogeneity dictates that we use the type
1369 of the RESULT_DECL as the operation type. */
1371 tree operation_type = TREE_TYPE (result_decl);
1373 /* Convert the right operand to the operation type. Note that
1374 it's the same transformation as in the MODIFY_EXPR case of
1375 build_binary_op with the additional guarantee that the type
1376 cannot involve a placeholder, since otherwise the function
1377 would use the "target pointer" return mechanism. */
1379 if (operation_type != TREE_TYPE (ret_val))
1380 ret_val = convert (operation_type, ret_val);
1382 result_expr
1383 = build2 (MODIFY_EXPR, operation_type, result_decl, ret_val);
1385 else
1386 result_expr = NULL_TREE;
1388 return build1 (RETURN_EXPR, void_type_node, result_expr);
1391 /* Build a CALL_EXPR to call FUNDECL with one argument, ARG. Return
1392 the CALL_EXPR. */
1394 tree
1395 build_call_1_expr (tree fundecl, tree arg)
1397 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1398 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1399 1, arg);
1400 TREE_SIDE_EFFECTS (call) = 1;
1401 return call;
1404 /* Build a CALL_EXPR to call FUNDECL with two arguments, ARG1 & ARG2. Return
1405 the CALL_EXPR. */
1407 tree
1408 build_call_2_expr (tree fundecl, tree arg1, tree arg2)
1410 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1411 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1412 2, arg1, arg2);
1413 TREE_SIDE_EFFECTS (call) = 1;
1414 return call;
1417 /* Likewise to call FUNDECL with no arguments. */
1419 tree
1420 build_call_0_expr (tree fundecl)
1422 /* We rely on build_call_nary to compute TREE_SIDE_EFFECTS. This makes
1423 it possible to propagate DECL_IS_PURE on parameterless functions. */
1424 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1425 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1427 return call;
1430 /* Call a function that raises an exception and pass the line number and file
1431 name, if requested. MSG says which exception function to call.
1433 GNAT_NODE is the gnat node conveying the source location for which the
1434 error should be signaled, or Empty in which case the error is signaled on
1435 the current ref_file_name/input_line. */
1437 tree
1438 build_call_raise (int msg, Node_Id gnat_node)
1440 tree fndecl = gnat_raise_decls[msg];
1442 const char *str
1443 = (Debug_Flag_NN || Exception_Locations_Suppressed)
1444 ? ""
1445 : (gnat_node != Empty)
1446 ? IDENTIFIER_POINTER
1447 (get_identifier (Get_Name_String
1448 (Debug_Source_Name
1449 (Get_Source_File_Index (Sloc (gnat_node))))))
1450 : ref_filename;
1452 int len = strlen (str) + 1;
1453 tree filename = build_string (len, str);
1455 int line_number
1456 = (gnat_node != Empty)
1457 ? Get_Logical_Line_Number (Sloc(gnat_node)) : input_line;
1459 TREE_TYPE (filename)
1460 = build_array_type (char_type_node,
1461 build_index_type (build_int_cst (NULL_TREE, len)));
1463 return
1464 build_call_2_expr (fndecl,
1465 build1 (ADDR_EXPR, build_pointer_type (char_type_node),
1466 filename),
1467 build_int_cst (NULL_TREE, line_number));
1470 /* qsort comparer for the bit positions of two constructor elements
1471 for record components. */
1473 static int
1474 compare_elmt_bitpos (const PTR rt1, const PTR rt2)
1476 tree elmt1 = * (tree *) rt1;
1477 tree elmt2 = * (tree *) rt2;
1479 tree pos_field1 = bit_position (TREE_PURPOSE (elmt1));
1480 tree pos_field2 = bit_position (TREE_PURPOSE (elmt2));
1482 if (tree_int_cst_equal (pos_field1, pos_field2))
1483 return 0;
1484 else if (tree_int_cst_lt (pos_field1, pos_field2))
1485 return -1;
1486 else
1487 return 1;
1490 /* Return a CONSTRUCTOR of TYPE whose list is LIST. */
1492 tree
1493 gnat_build_constructor (tree type, tree list)
1495 tree elmt;
1496 int n_elmts;
1497 bool allconstant = (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST);
1498 bool side_effects = false;
1499 tree result;
1501 /* Scan the elements to see if they are all constant or if any has side
1502 effects, to let us set global flags on the resulting constructor. Count
1503 the elements along the way for possible sorting purposes below. */
1504 for (n_elmts = 0, elmt = list; elmt; elmt = TREE_CHAIN (elmt), n_elmts ++)
1506 if (!TREE_CONSTANT (TREE_VALUE (elmt))
1507 || (TREE_CODE (type) == RECORD_TYPE
1508 && DECL_BIT_FIELD (TREE_PURPOSE (elmt))
1509 && TREE_CODE (TREE_VALUE (elmt)) != INTEGER_CST)
1510 || !initializer_constant_valid_p (TREE_VALUE (elmt),
1511 TREE_TYPE (TREE_VALUE (elmt))))
1512 allconstant = false;
1514 if (TREE_SIDE_EFFECTS (TREE_VALUE (elmt)))
1515 side_effects = true;
1517 /* Propagate an NULL_EXPR from the size of the type. We won't ever
1518 be executing the code we generate here in that case, but handle it
1519 specially to avoid the compiler blowing up. */
1520 if (TREE_CODE (type) == RECORD_TYPE
1521 && (0 != (result
1522 = contains_null_expr (DECL_SIZE (TREE_PURPOSE (elmt))))))
1523 return build1 (NULL_EXPR, type, TREE_OPERAND (result, 0));
1526 /* For record types with constant components only, sort field list
1527 by increasing bit position. This is necessary to ensure the
1528 constructor can be output as static data, which the gimplifier
1529 might force in various circumstances. */
1530 if (allconstant && TREE_CODE (type) == RECORD_TYPE && n_elmts > 1)
1532 /* Fill an array with an element tree per index, and ask qsort to order
1533 them according to what a bitpos comparison function says. */
1535 tree *gnu_arr = (tree *) alloca (sizeof (tree) * n_elmts);
1536 int i;
1538 for (i = 0, elmt = list; elmt; elmt = TREE_CHAIN (elmt), i++)
1539 gnu_arr[i] = elmt;
1541 qsort (gnu_arr, n_elmts, sizeof (tree), compare_elmt_bitpos);
1543 /* Then reconstruct the list from the sorted array contents. */
1545 list = NULL_TREE;
1546 for (i = n_elmts - 1; i >= 0; i--)
1548 TREE_CHAIN (gnu_arr[i]) = list;
1549 list = gnu_arr[i];
1553 result = build_constructor_from_list (type, list);
1554 TREE_CONSTANT (result) = TREE_INVARIANT (result)
1555 = TREE_STATIC (result) = allconstant;
1556 TREE_SIDE_EFFECTS (result) = side_effects;
1557 TREE_READONLY (result) = TYPE_READONLY (type) || allconstant;
1558 return result;
1561 /* Return a COMPONENT_REF to access a field that is given by COMPONENT,
1562 an IDENTIFIER_NODE giving the name of the field, or FIELD, a FIELD_DECL,
1563 for the field. Don't fold the result if NO_FOLD_P is true.
1565 We also handle the fact that we might have been passed a pointer to the
1566 actual record and know how to look for fields in variant parts. */
1568 static tree
1569 build_simple_component_ref (tree record_variable, tree component,
1570 tree field, bool no_fold_p)
1572 tree record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_variable));
1573 tree ref;
1575 gcc_assert ((TREE_CODE (record_type) == RECORD_TYPE
1576 || TREE_CODE (record_type) == UNION_TYPE
1577 || TREE_CODE (record_type) == QUAL_UNION_TYPE)
1578 && TYPE_SIZE (record_type)
1579 && (component != 0) != (field != 0));
1581 /* If no field was specified, look for a field with the specified name
1582 in the current record only. */
1583 if (!field)
1584 for (field = TYPE_FIELDS (record_type); field;
1585 field = TREE_CHAIN (field))
1586 if (DECL_NAME (field) == component)
1587 break;
1589 if (!field)
1590 return NULL_TREE;
1592 /* If this field is not in the specified record, see if we can find
1593 something in the record whose original field is the same as this one. */
1594 if (DECL_CONTEXT (field) != record_type)
1595 /* Check if there is a field with name COMPONENT in the record. */
1597 tree new_field;
1599 /* First loop thru normal components. */
1601 for (new_field = TYPE_FIELDS (record_type); new_field;
1602 new_field = TREE_CHAIN (new_field))
1603 if (field == new_field
1604 || DECL_ORIGINAL_FIELD (new_field) == field
1605 || new_field == DECL_ORIGINAL_FIELD (field)
1606 || (DECL_ORIGINAL_FIELD (field)
1607 && (DECL_ORIGINAL_FIELD (field)
1608 == DECL_ORIGINAL_FIELD (new_field))))
1609 break;
1611 /* Next, loop thru DECL_INTERNAL_P components if we haven't found
1612 the component in the first search. Doing this search in 2 steps
1613 is required to avoiding hidden homonymous fields in the
1614 _Parent field. */
1616 if (!new_field)
1617 for (new_field = TYPE_FIELDS (record_type); new_field;
1618 new_field = TREE_CHAIN (new_field))
1619 if (DECL_INTERNAL_P (new_field))
1621 tree field_ref
1622 = build_simple_component_ref (record_variable,
1623 NULL_TREE, new_field, no_fold_p);
1624 ref = build_simple_component_ref (field_ref, NULL_TREE, field,
1625 no_fold_p);
1627 if (ref)
1628 return ref;
1631 field = new_field;
1634 if (!field)
1635 return NULL_TREE;
1637 /* If the field's offset has overflowed, do not attempt to access it
1638 as doing so may trigger sanity checks deeper in the back-end.
1639 Note that we don't need to warn since this will be done on trying
1640 to declare the object. */
1641 if (TREE_CODE (DECL_FIELD_OFFSET (field)) == INTEGER_CST
1642 && TREE_OVERFLOW (DECL_FIELD_OFFSET (field)))
1643 return NULL_TREE;
1645 /* It would be nice to call "fold" here, but that can lose a type
1646 we need to tag a PLACEHOLDER_EXPR with, so we can't do it. */
1647 ref = build3 (COMPONENT_REF, TREE_TYPE (field), record_variable, field,
1648 NULL_TREE);
1650 if (TREE_READONLY (record_variable) || TREE_READONLY (field))
1651 TREE_READONLY (ref) = 1;
1652 if (TREE_THIS_VOLATILE (record_variable) || TREE_THIS_VOLATILE (field)
1653 || TYPE_VOLATILE (record_type))
1654 TREE_THIS_VOLATILE (ref) = 1;
1656 return no_fold_p ? ref : fold (ref);
1659 /* Like build_simple_component_ref, except that we give an error if the
1660 reference could not be found. */
1662 tree
1663 build_component_ref (tree record_variable, tree component,
1664 tree field, bool no_fold_p)
1666 tree ref = build_simple_component_ref (record_variable, component, field,
1667 no_fold_p);
1669 if (ref)
1670 return ref;
1672 /* If FIELD was specified, assume this is an invalid user field so
1673 raise constraint error. Otherwise, we can't find the type to return, so
1674 abort. */
1675 gcc_assert (field);
1676 return build1 (NULL_EXPR, TREE_TYPE (field),
1677 build_call_raise (CE_Discriminant_Check_Failed, Empty));
1680 /* Build a GCC tree to call an allocation or deallocation function.
1681 If GNU_OBJ is nonzero, it is an object to deallocate. Otherwise,
1682 generate an allocator.
1684 GNU_SIZE is the size of the object in bytes and ALIGN is the alignment in
1685 bits. GNAT_PROC, if present, is a procedure to call and GNAT_POOL is the
1686 storage pool to use. If not preset, malloc and free will be used except
1687 if GNAT_PROC is the "fake" value of -1, in which case we allocate the
1688 object dynamically on the stack frame. */
1690 tree
1691 build_call_alloc_dealloc (tree gnu_obj, tree gnu_size, unsigned align,
1692 Entity_Id gnat_proc, Entity_Id gnat_pool,
1693 Node_Id gnat_node)
1695 tree gnu_align = size_int (align / BITS_PER_UNIT);
1697 gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_obj);
1699 if (Present (gnat_proc))
1701 /* The storage pools are obviously always tagged types, but the
1702 secondary stack uses the same mechanism and is not tagged */
1703 if (Is_Tagged_Type (Etype (gnat_pool)))
1705 /* The size is the third parameter; the alignment is the
1706 same type. */
1707 Entity_Id gnat_size_type
1708 = Etype (Next_Formal (Next_Formal (First_Formal (gnat_proc))));
1709 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
1710 tree gnu_proc = gnat_to_gnu (gnat_proc);
1711 tree gnu_proc_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_proc);
1712 tree gnu_pool = gnat_to_gnu (gnat_pool);
1713 tree gnu_pool_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_pool);
1714 tree gnu_call;
1716 gnu_size = convert (gnu_size_type, gnu_size);
1717 gnu_align = convert (gnu_size_type, gnu_align);
1719 /* The first arg is always the address of the storage pool; next
1720 comes the address of the object, for a deallocator, then the
1721 size and alignment. */
1722 if (gnu_obj)
1723 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1724 gnu_proc_addr, 4, gnu_pool_addr,
1725 gnu_obj, gnu_size, gnu_align);
1726 else
1727 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1728 gnu_proc_addr, 3, gnu_pool_addr,
1729 gnu_size, gnu_align);
1730 TREE_SIDE_EFFECTS (gnu_call) = 1;
1731 return gnu_call;
1734 /* Secondary stack case. */
1735 else
1737 /* The size is the second parameter */
1738 Entity_Id gnat_size_type
1739 = Etype (Next_Formal (First_Formal (gnat_proc)));
1740 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
1741 tree gnu_proc = gnat_to_gnu (gnat_proc);
1742 tree gnu_proc_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_proc);
1743 tree gnu_call;
1745 gnu_size = convert (gnu_size_type, gnu_size);
1747 /* The first arg is the address of the object, for a
1748 deallocator, then the size */
1749 if (gnu_obj)
1750 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1751 gnu_proc_addr, 2, gnu_obj, gnu_size);
1752 else
1753 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1754 gnu_proc_addr, 1, gnu_size);
1755 TREE_SIDE_EFFECTS (gnu_call) = 1;
1756 return gnu_call;
1760 else if (gnu_obj)
1761 return build_call_1_expr (free_decl, gnu_obj);
1763 /* ??? For now, disable variable-sized allocators in the stack since
1764 we can't yet gimplify an ALLOCATE_EXPR. */
1765 else if (gnat_pool == -1
1766 && TREE_CODE (gnu_size) == INTEGER_CST && !flag_stack_check)
1768 /* If the size is a constant, we can put it in the fixed portion of
1769 the stack frame to avoid the need to adjust the stack pointer. */
1770 if (TREE_CODE (gnu_size) == INTEGER_CST && !flag_stack_check)
1772 tree gnu_range
1773 = build_range_type (NULL_TREE, size_one_node, gnu_size);
1774 tree gnu_array_type = build_array_type (char_type_node, gnu_range);
1775 tree gnu_decl
1776 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
1777 gnu_array_type, NULL_TREE, false, false, false,
1778 false, NULL, gnat_node);
1780 return convert (ptr_void_type_node,
1781 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_decl));
1783 else
1784 gcc_unreachable ();
1785 #if 0
1786 return build2 (ALLOCATE_EXPR, ptr_void_type_node, gnu_size, gnu_align);
1787 #endif
1789 else
1791 if (Nkind (gnat_node) != N_Allocator || !Comes_From_Source (gnat_node))
1792 Check_No_Implicit_Heap_Alloc (gnat_node);
1793 return build_call_1_expr (malloc_decl, gnu_size);
1797 /* Build a GCC tree to correspond to allocating an object of TYPE whose
1798 initial value is INIT, if INIT is nonzero. Convert the expression to
1799 RESULT_TYPE, which must be some type of pointer. Return the tree.
1800 GNAT_PROC and GNAT_POOL optionally give the procedure to call and
1801 the storage pool to use. GNAT_NODE is used to provide an error
1802 location for restriction violations messages. If IGNORE_INIT_TYPE is
1803 true, ignore the type of INIT for the purpose of determining the size;
1804 this will cause the maximum size to be allocated if TYPE is of
1805 self-referential size. */
1807 tree
1808 build_allocator (tree type, tree init, tree result_type, Entity_Id gnat_proc,
1809 Entity_Id gnat_pool, Node_Id gnat_node, bool ignore_init_type)
1811 tree size = TYPE_SIZE_UNIT (type);
1812 tree result;
1814 /* If the initializer, if present, is a NULL_EXPR, just return a new one. */
1815 if (init && TREE_CODE (init) == NULL_EXPR)
1816 return build1 (NULL_EXPR, result_type, TREE_OPERAND (init, 0));
1818 /* If RESULT_TYPE is a fat or thin pointer, set SIZE to be the sum of the
1819 sizes of the object and its template. Allocate the whole thing and
1820 fill in the parts that are known. */
1821 else if (TYPE_FAT_OR_THIN_POINTER_P (result_type))
1823 tree storage_type
1824 = build_unc_object_type_from_ptr (result_type, type,
1825 get_identifier ("ALLOC"));
1826 tree template_type = TREE_TYPE (TYPE_FIELDS (storage_type));
1827 tree storage_ptr_type = build_pointer_type (storage_type);
1828 tree storage;
1829 tree template_cons = NULL_TREE;
1831 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (storage_type),
1832 init);
1834 /* If the size overflows, pass -1 so the allocator will raise
1835 storage error. */
1836 if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size))
1837 size = ssize_int (-1);
1839 storage = build_call_alloc_dealloc (NULL_TREE, size,
1840 TYPE_ALIGN (storage_type),
1841 gnat_proc, gnat_pool, gnat_node);
1842 storage = convert (storage_ptr_type, protect_multiple_eval (storage));
1844 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
1846 type = TREE_TYPE (TYPE_FIELDS (type));
1848 if (init)
1849 init = convert (type, init);
1852 /* If there is an initializing expression, make a constructor for
1853 the entire object including the bounds and copy it into the
1854 object. If there is no initializing expression, just set the
1855 bounds. */
1856 if (init)
1858 template_cons = tree_cons (TREE_CHAIN (TYPE_FIELDS (storage_type)),
1859 init, NULL_TREE);
1860 template_cons = tree_cons (TYPE_FIELDS (storage_type),
1861 build_template (template_type, type,
1862 init),
1863 template_cons);
1865 return convert
1866 (result_type,
1867 build2 (COMPOUND_EXPR, storage_ptr_type,
1868 build_binary_op
1869 (MODIFY_EXPR, storage_type,
1870 build_unary_op (INDIRECT_REF, NULL_TREE,
1871 convert (storage_ptr_type, storage)),
1872 gnat_build_constructor (storage_type, template_cons)),
1873 convert (storage_ptr_type, storage)));
1875 else
1876 return build2
1877 (COMPOUND_EXPR, result_type,
1878 build_binary_op
1879 (MODIFY_EXPR, template_type,
1880 build_component_ref
1881 (build_unary_op (INDIRECT_REF, NULL_TREE,
1882 convert (storage_ptr_type, storage)),
1883 NULL_TREE, TYPE_FIELDS (storage_type), 0),
1884 build_template (template_type, type, NULL_TREE)),
1885 convert (result_type, convert (storage_ptr_type, storage)));
1888 /* If we have an initializing expression, see if its size is simpler
1889 than the size from the type. */
1890 if (!ignore_init_type && init && TYPE_SIZE_UNIT (TREE_TYPE (init))
1891 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (init))) == INTEGER_CST
1892 || CONTAINS_PLACEHOLDER_P (size)))
1893 size = TYPE_SIZE_UNIT (TREE_TYPE (init));
1895 /* If the size is still self-referential, reference the initializing
1896 expression, if it is present. If not, this must have been a
1897 call to allocate a library-level object, in which case we use
1898 the maximum size. */
1899 if (CONTAINS_PLACEHOLDER_P (size))
1901 if (!ignore_init_type && init)
1902 size = substitute_placeholder_in_expr (size, init);
1903 else
1904 size = max_size (size, true);
1907 /* If the size overflows, pass -1 so the allocator will raise
1908 storage error. */
1909 if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size))
1910 size = ssize_int (-1);
1912 /* If this is a type whose alignment is larger than the
1913 biggest we support in normal alignment and this is in
1914 the default storage pool, make an "aligning type", allocate
1915 it, point to the field we need, and return that. */
1916 if (TYPE_ALIGN (type) > BIGGEST_ALIGNMENT
1917 && No (gnat_proc))
1919 tree new_type = make_aligning_type (type, TYPE_ALIGN (type), size);
1921 result = build_call_alloc_dealloc (NULL_TREE, TYPE_SIZE_UNIT (new_type),
1922 BIGGEST_ALIGNMENT, Empty,
1923 Empty, gnat_node);
1924 result = save_expr (result);
1925 result = convert (build_pointer_type (new_type), result);
1926 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1927 result = build_component_ref (result, NULL_TREE,
1928 TYPE_FIELDS (new_type), 0);
1929 result = convert (result_type,
1930 build_unary_op (ADDR_EXPR, NULL_TREE, result));
1932 else
1933 result = convert (result_type,
1934 build_call_alloc_dealloc (NULL_TREE, size,
1935 TYPE_ALIGN (type),
1936 gnat_proc,
1937 gnat_pool,
1938 gnat_node));
1940 /* If we have an initial value, put the new address into a SAVE_EXPR, assign
1941 the value, and return the address. Do this with a COMPOUND_EXPR. */
1943 if (init)
1945 result = save_expr (result);
1946 result
1947 = build2 (COMPOUND_EXPR, TREE_TYPE (result),
1948 build_binary_op
1949 (MODIFY_EXPR, NULL_TREE,
1950 build_unary_op (INDIRECT_REF,
1951 TREE_TYPE (TREE_TYPE (result)), result),
1952 init),
1953 result);
1956 return convert (result_type, result);
1959 /* Fill in a VMS descriptor for EXPR and return a constructor for it.
1960 GNAT_FORMAL is how we find the descriptor record. */
1962 tree
1963 fill_vms_descriptor (tree expr, Entity_Id gnat_formal)
1965 tree record_type = TREE_TYPE (TREE_TYPE (get_gnu_tree (gnat_formal)));
1966 tree field;
1967 tree const_list = NULL_TREE;
1969 expr = maybe_unconstrained_array (expr);
1970 gnat_mark_addressable (expr);
1972 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
1973 const_list
1974 = tree_cons (field,
1975 convert (TREE_TYPE (field),
1976 SUBSTITUTE_PLACEHOLDER_IN_EXPR
1977 (DECL_INITIAL (field), expr)),
1978 const_list);
1980 return gnat_build_constructor (record_type, nreverse (const_list));
1983 /* Indicate that we need to make the address of EXPR_NODE and it therefore
1984 should not be allocated in a register. Returns true if successful. */
1986 bool
1987 gnat_mark_addressable (tree expr_node)
1989 while (1)
1990 switch (TREE_CODE (expr_node))
1992 case ADDR_EXPR:
1993 case COMPONENT_REF:
1994 case ARRAY_REF:
1995 case ARRAY_RANGE_REF:
1996 case REALPART_EXPR:
1997 case IMAGPART_EXPR:
1998 case VIEW_CONVERT_EXPR:
1999 case CONVERT_EXPR:
2000 case NON_LVALUE_EXPR:
2001 case NOP_EXPR:
2002 expr_node = TREE_OPERAND (expr_node, 0);
2003 break;
2005 case CONSTRUCTOR:
2006 TREE_ADDRESSABLE (expr_node) = 1;
2007 return true;
2009 case VAR_DECL:
2010 case PARM_DECL:
2011 case RESULT_DECL:
2012 TREE_ADDRESSABLE (expr_node) = 1;
2013 return true;
2015 case FUNCTION_DECL:
2016 TREE_ADDRESSABLE (expr_node) = 1;
2017 return true;
2019 case CONST_DECL:
2020 return (DECL_CONST_CORRESPONDING_VAR (expr_node)
2021 && (gnat_mark_addressable
2022 (DECL_CONST_CORRESPONDING_VAR (expr_node))));
2023 default:
2024 return true;