1 /* Language-level data type conversion for GNU CHILL.
2 Copyright (C) 1992, 93, 1994, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file contains the functions for converting CHILL expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
36 extern tree bit_one_node
, bit_zero_node
;
37 extern tree string_one_type_node
;
38 extern tree bitstring_one_type_node
;
40 static tree convert_to_reference
PROTO ((tree
, tree
));
41 static tree convert_to_boolean
PROTO ((tree
, tree
));
42 static tree convert_to_char
PROTO ((tree
, tree
));
44 static tree base_type_size_in_bytes
PROTO ((tree
));
46 static tree remove_tree_element
PROTO ((tree
, tree
*));
47 static tree check_ps_range
PROTO ((tree
, tree
, tree
));
48 static tree digest_powerset_tuple
PROTO ((tree
, tree
));
49 static tree digest_structure_tuple
PROTO ((tree
, tree
));
50 static tree digest_array_tuple
PROTO ((tree
, tree
, int));
51 static tree convert1
PROTO ((tree
, tree
));
54 convert_to_reference (reftype
, expr
)
57 while (TREE_CODE (expr
) == NOP_EXPR
) /* RETYPE_EXPR */
58 expr
= TREE_OPERAND (expr
, 0);
60 if (! CH_LOCATION_P (expr
))
61 error("internal error: trying to make loc-identity with non-location");
64 mark_addressable (expr
);
65 return fold (build1 (ADDR_EXPR
, reftype
, expr
));
68 return error_mark_node
;
72 convert_from_reference (expr
)
75 tree e
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
76 TREE_READONLY (e
) = TREE_READONLY (expr
);
80 /* Convert EXPR to a boolean type. */
83 convert_to_boolean (type
, expr
)
86 register tree intype
= TREE_TYPE (expr
);
88 if (integer_zerop (expr
))
89 return boolean_false_node
;
90 if (integer_onep (expr
))
91 return boolean_true_node
;
93 /* Convert a singleton bitstring to a Boolean.
94 Needed if flag_old_strings. */
95 if (CH_BOOLS_ONE_P (intype
))
97 if (TREE_CODE (expr
) == CONSTRUCTOR
)
99 tree valuelist
= TREE_OPERAND (expr
, 1);
100 if (valuelist
== NULL_TREE
)
101 return boolean_false_node
;
102 if (TREE_CHAIN (valuelist
) == NULL_TREE
103 && TREE_PURPOSE (valuelist
) == NULL_TREE
104 && integer_zerop (TREE_VALUE (valuelist
)))
105 return boolean_true_node
;
107 return build_chill_bitref (expr
,
108 build_tree_list (NULL_TREE
,
112 if (INTEGRAL_TYPE_P (intype
))
113 return build1 (CONVERT_EXPR
, type
, expr
);
115 error ("cannot convert to a boolean mode");
116 return boolean_false_node
;
119 /* Convert EXPR to a char type. */
122 convert_to_char (type
, expr
)
125 register tree intype
= TREE_TYPE (expr
);
126 register enum chill_tree_code form
= TREE_CODE (intype
);
128 if (form
== CHAR_TYPE
)
129 return build1 (NOP_EXPR
, type
, expr
);
131 /* Convert a singleton string to a char.
132 Needed if flag_old_strings. */
133 if (CH_CHARS_ONE_P (intype
))
135 if (TREE_CODE (expr
) == STRING_CST
)
137 expr
= build_int_2 ((unsigned char)TREE_STRING_POINTER(expr
)[0], 0);
138 TREE_TYPE (expr
) = char_type_node
;
142 return build (ARRAY_REF
, char_type_node
, expr
, integer_zero_node
);
146 /* For now, assume it will always fit */
147 if (form
== INTEGER_TYPE
)
148 return build1 (CONVERT_EXPR
, type
, expr
);
150 error ("cannot convert to a char mode");
153 register tree tem
= build_int_2 (0, 0);
154 TREE_TYPE (tem
) = type
;
161 base_type_size_in_bytes (type
)
164 if (type
== NULL_TREE
165 || TREE_CODE (type
) == ERROR_MARK
166 || TREE_CODE (type
) != ARRAY_TYPE
)
167 return error_mark_node
;
168 return size_in_bytes (TREE_TYPE (type
));
173 * build a singleton array type, of TYPE objects.
176 build_array_type_for_scalar (type
)
180 if (type
== char_type_node
)
181 return build_string_type (type
, integer_one_node
);
183 if (type
== NULL_TREE
|| TREE_CODE (type
) == ERROR_MARK
)
184 return error_mark_node
;
186 return build_chill_array_type
188 tree_cons (NULL_TREE
,
189 build_chill_range_type (NULL_TREE
,
190 integer_zero_node
, integer_zero_node
),
198 unreferenced_type_of (type
)
201 if (type
== NULL_TREE
|| TREE_CODE (type
) == ERROR_MARK
)
202 return error_mark_node
;
203 while (TREE_CODE (type
) == REFERENCE_TYPE
)
204 type
= TREE_TYPE (type
);
210 /* Remove from *LISTP the first TREE_LIST node whose TREE_PURPOSE == KEY.
211 Return the TREE_LIST node, or NULL_TREE on failure. */
214 remove_tree_element (key
, listp
)
219 for ( ; node
; listp
= &TREE_CHAIN (node
), node
= *listp
)
221 if (TREE_PURPOSE (node
) == key
)
223 *listp
= TREE_CHAIN (node
);
224 TREE_CHAIN (node
) = NULL_TREE
;
231 /* This is quite the same as check_range in actions.c, but with
232 different error message. */
235 check_ps_range (value
, lo_limit
, hi_limit
)
240 tree check
= test_range (value
, lo_limit
, hi_limit
);
242 if (!integer_zerop (check
))
244 if (TREE_CODE (check
) == INTEGER_CST
)
246 error ("powerset tuple element out of range");
247 return error_mark_node
;
250 value
= check_expression (value
, check
,
251 ridpointers
[(int) RID_RANGEFAIL
]);
257 digest_powerset_tuple (type
, inits
)
263 tree domain
= TYPE_DOMAIN (type
);
265 int is_erroneous
= 0, is_constant
= 1, is_simple
= 1;
266 if (domain
== NULL_TREE
|| TREE_CODE (domain
) == ERROR_MARK
)
267 return error_mark_node
;
268 for (list
= TREE_OPERAND (inits
, 1); list
; list
= TREE_CHAIN (list
), i
++)
270 tree val
= TREE_VALUE (list
);
271 if (TREE_CODE (val
) == ERROR_MARK
)
276 if (!TREE_CONSTANT (val
))
278 else if (!initializer_constant_valid_p (val
, TREE_TYPE (val
)))
280 if (! CH_COMPATIBLE (val
, domain
))
282 error ("incompatible member of powerset tuple (at position #%d)", i
);
286 /* check range of value */
287 val
= check_ps_range (val
, TYPE_MIN_VALUE (domain
),
288 TYPE_MAX_VALUE (domain
));
289 if (TREE_CODE (val
) == ERROR_MARK
)
295 /* Updating the list in place is in principle questionable,
296 but I can't think how it could hurt. */
297 TREE_VALUE (list
) = convert (domain
, val
);
299 val
= TREE_PURPOSE (list
);
300 if (val
== NULL_TREE
)
303 if (TREE_CODE (val
) == ERROR_MARK
)
308 if (! CH_COMPATIBLE (val
, domain
))
310 error ("incompatible member of powerset tuple (at position #%d)", i
);
314 val
= check_ps_range (val
, TYPE_MIN_VALUE (domain
),
315 TYPE_MAX_VALUE (domain
));
316 if (TREE_CODE (val
) == ERROR_MARK
)
321 TREE_PURPOSE (list
) = convert (domain
, val
);
322 if (!TREE_CONSTANT (val
))
324 else if (!initializer_constant_valid_p (val
, TREE_TYPE (val
)))
327 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, TREE_OPERAND (inits
, 1));
329 return error_mark_node
;
331 TREE_CONSTANT (result
) = 1;
332 if (is_constant
&& is_simple
)
333 TREE_STATIC (result
) = 1;
338 digest_structure_tuple (type
, inits
)
342 tree elements
= CONSTRUCTOR_ELTS (inits
);
343 tree values
= NULL_TREE
;
346 int is_erroneous
= 0;
348 int labelled_elements
= 0;
349 int unlabelled_elements
= 0;
350 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
352 if (TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
)
353 { /* Regular fixed field. */
354 tree value
= remove_tree_element (DECL_NAME (field
), &elements
);
358 else if (elements
&& TREE_PURPOSE (elements
) == NULL_TREE
)
361 elements
= TREE_CHAIN (elements
);
362 unlabelled_elements
++;
369 sprintf (msg
, "initializer for field `%.80s'",
370 IDENTIFIER_POINTER (DECL_NAME (field
)));
371 val
= chill_convert_for_assignment (TREE_TYPE (field
),
372 TREE_VALUE (value
), msg
);
373 if (TREE_CODE (val
) == ERROR_MARK
)
377 TREE_VALUE (value
) = val
;
378 TREE_CHAIN (value
) = values
;
379 TREE_PURPOSE (value
) = field
;
381 if (TREE_CODE (val
) == ERROR_MARK
)
383 else if (!TREE_CONSTANT (val
))
385 else if (!initializer_constant_valid_p (val
,
392 pedwarn ("no initializer value for fixed field `%s'",
393 IDENTIFIER_POINTER (DECL_NAME (field
)));
399 tree selected_variant
= NULL_TREE
;
400 tree variant_values
= NULL_TREE
;
402 /* In a tagged variant structure mode, try to figure out
403 (from the fixed fields), which is the selected variant. */
404 if (TYPE_TAGFIELDS (TREE_TYPE (field
)))
406 for (variant
= TYPE_FIELDS (TREE_TYPE (field
));
407 variant
; variant
= TREE_CHAIN (variant
))
409 tree tag_labels
= TYPE_TAG_VALUES (TREE_TYPE (variant
));
410 tree tag_fields
= TYPE_TAGFIELDS (TREE_TYPE (field
));
411 if (DECL_NAME (variant
) == ELSE_VARIANT_NAME
)
413 selected_variant
= variant
;
416 for (; tag_labels
&& tag_fields
;
417 tag_labels
= TREE_CHAIN (tag_labels
),
418 tag_fields
= TREE_CHAIN (tag_fields
))
420 tree tag_value
= values
;
422 tree tag_decl
= TREE_VALUE (tag_fields
);
423 tree tag_value_set
= TREE_VALUE (tag_labels
);
424 for ( ; tag_value
; tag_value
= TREE_CHAIN (tag_value
))
426 if (TREE_PURPOSE (tag_value
) == tag_decl
)
428 tag_value
= TREE_VALUE (tag_value
);
432 if (!tag_value
|| TREE_CODE (tag_value
) != INTEGER_CST
)
434 pedwarn ("non-constant value for tag field `%s'",
435 IDENTIFIER_POINTER (DECL_NAME (tag_decl
)));
439 /* Check if the value of the tag (as given in a
440 previous field) matches the case label list. */
441 for (; tag_value_set
;
442 tag_value_set
= TREE_CHAIN (tag_value_set
))
444 if (tree_int_cst_equal (TREE_VALUE (tag_value_set
),
456 selected_variant
= variant
;
462 for (variant
= TYPE_FIELDS (TREE_TYPE (field
));
463 variant
; variant
= TREE_CHAIN (variant
))
465 tree vfield0
= TYPE_FIELDS (TREE_TYPE (variant
));
467 for (vfield
= vfield0
; vfield
; vfield
= TREE_CHAIN (vfield
))
469 tree value
= remove_tree_element (DECL_NAME (vfield
),
474 else if (variant
== selected_variant
475 && elements
&& TREE_PURPOSE (elements
) == NULL_TREE
)
478 elements
= TREE_CHAIN (elements
);
479 unlabelled_elements
++;
484 if (selected_variant
&& selected_variant
!= variant
)
486 error ("field `%s' in wrong variant",
487 IDENTIFIER_POINTER (DECL_NAME (vfield
)));
492 if (!selected_variant
&& vfield
!= vfield0
)
493 pedwarn ("missing variant fields (at least `%s')",
494 IDENTIFIER_POINTER (DECL_NAME (vfield0
)));
495 selected_variant
= variant
;
496 if (CH_COMPATIBLE (TREE_VALUE (value
),
499 tree val
= convert (TREE_TYPE (vfield
),
501 TREE_PURPOSE (value
) = vfield
;
502 TREE_VALUE (value
) = val
;
503 TREE_CHAIN (value
) = variant_values
;
504 variant_values
= value
;
505 if (TREE_CODE (val
) == ERROR_MARK
)
507 else if (!TREE_CONSTANT (val
))
509 else if (!initializer_constant_valid_p
510 (val
, TREE_TYPE (val
)))
516 error ("bad initializer for field `%s'",
517 IDENTIFIER_POINTER (DECL_NAME (vfield
)));
521 else if (variant
== selected_variant
)
523 pedwarn ("no initializer value for variant field `%s'",
524 IDENTIFIER_POINTER (DECL_NAME (field
)));
528 if (selected_variant
== NULL_TREE
)
529 pedwarn ("no selected variant");
532 variant_values
= build (CONSTRUCTOR
,
533 TREE_TYPE (selected_variant
),
534 NULL_TREE
, nreverse (variant_values
));
536 = build (CONSTRUCTOR
, TREE_TYPE (field
), NULL_TREE
,
537 build_tree_list (selected_variant
, variant_values
));
538 values
= tree_cons (field
, variant_values
, values
);
543 if (labelled_elements
&& unlabelled_elements
)
544 pedwarn ("mixture of labelled and unlabelled tuple elements");
546 /* Check for unused initializer elements. */
547 unlabelled_elements
= 0;
548 for ( ; elements
!= NULL_TREE
; elements
= TREE_CHAIN (elements
))
550 if (TREE_PURPOSE (elements
) == NULL_TREE
)
551 unlabelled_elements
++;
554 if (IDENTIFIER_POINTER (TREE_PURPOSE (elements
)) == 0)
555 error ("probably not a structure tuple");
557 error ("excess initializer for field `%s'",
558 IDENTIFIER_POINTER (TREE_PURPOSE (elements
)));
562 if (unlabelled_elements
)
564 error ("excess unnamed initializers");
568 CONSTRUCTOR_ELTS (inits
) = nreverse (values
);
569 TREE_TYPE (inits
) = type
;
571 return error_mark_node
;
573 TREE_CONSTANT (inits
) = 1;
574 if (is_constant
&& is_simple
)
575 TREE_STATIC (inits
) = 1;
579 /* Return a Chill representation of the INTEGER_CST VAL.
580 The result may be in a static buffer, */
583 display_int_cst (val
)
586 static char buffer
[50];
589 if (TREE_CODE (val
) != INTEGER_CST
)
590 return "<not a constant>";
592 x
= TREE_INT_CST_LOW (val
);
594 switch (TREE_CODE (TREE_TYPE (val
)))
604 strcpy (buffer
, "'^^'");
606 strcpy (buffer
, "'^J'");
607 else if (x
< ' ' || x
> '~')
608 sprintf (buffer
, "'^(%u)'", (unsigned int) x
);
610 sprintf (buffer
, "'%c'", (char) x
);
613 for (fields
= TYPE_VALUES (TREE_TYPE (val
)); fields
!= NULL_TREE
;
614 fields
= TREE_CHAIN (fields
))
616 if (tree_int_cst_equal (TREE_VALUE (fields
), val
))
617 return IDENTIFIER_POINTER (TREE_PURPOSE (fields
));
626 /* This code is derived from print-tree.c:print_code_brief. */
627 if (TREE_INT_CST_HIGH (val
) == 0)
629 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
635 else if (TREE_INT_CST_HIGH (val
) == -1 && TREE_INT_CST_LOW (val
) != 0)
637 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
645 #if HOST_BITS_PER_WIDE_INT == 64
646 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
652 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
658 TREE_INT_CST_HIGH (val
), TREE_INT_CST_LOW (val
));
664 digest_array_tuple (type
, init
, allow_missing_elements
)
667 int allow_missing_elements
;
669 tree element
= CONSTRUCTOR_ELTS (init
);
672 tree element_type
= TREE_TYPE (type
);
673 tree default_value
= NULL_TREE
;
674 tree element_list
= NULL_TREE
;
677 tree
*ptr
= &element_list
;
679 int labelled_elements
= 0;
680 int unlabelled_elements
= 0;
681 tree first
, last
= NULL_TREE
;
683 if (type
== NULL_TREE
|| TREE_CODE (type
) == ERROR_MARK
)
684 return error_mark_node
;
686 domain_min
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
687 domain_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
689 if (domain_min
== NULL
|| TREE_CODE (domain_min
) != INTEGER_CST
)
691 error ("non-constant start index for tuple");
692 return error_mark_node
;
694 if (TREE_CODE (domain_max
) != INTEGER_CST
)
697 if (TREE_CODE (type
) != ARRAY_TYPE
)
700 for ( ; element
!= NULL_TREE
; element
= TREE_CHAIN (element
))
702 tree purpose
= TREE_PURPOSE (element
);
703 tree value
= TREE_VALUE (element
);
705 if (purpose
== NULL_TREE
)
707 if (last
== NULL_TREE
)
711 HOST_WIDE_INT new_lo
, new_hi
;
712 add_double (TREE_INT_CST_LOW (last
), TREE_INT_CST_HIGH (last
),
715 first
= build_int_2 (new_lo
, new_hi
);
716 TREE_TYPE (first
) = TYPE_DOMAIN (type
);
719 unlabelled_elements
++;
724 if (TREE_CODE (purpose
) == INTEGER_CST
)
725 first
= last
= purpose
;
726 else if (TREE_CODE (purpose
) == TYPE_DECL
727 && discrete_type_p (TREE_TYPE (purpose
)))
729 first
= TYPE_MIN_VALUE (TREE_TYPE (purpose
));
730 last
= TYPE_MAX_VALUE (TREE_TYPE (purpose
));
732 else if (TREE_CODE (purpose
) != RANGE_EXPR
)
734 error ("invalid array tuple label");
738 else if (TREE_OPERAND (purpose
, 0) == NULL_TREE
)
739 first
= last
= NULL_TREE
; /* Default value. */
742 first
= TREE_OPERAND (purpose
, 0);
743 last
= TREE_OPERAND (purpose
, 1);
745 if ((first
!= NULL
&& TREE_CODE (first
) != INTEGER_CST
)
746 || (last
!= NULL
&& TREE_CODE (last
) != INTEGER_CST
))
748 error ("non-constant array tuple index range");
753 if (! CH_COMPATIBLE (value
, element_type
))
755 const char *err_val_name
=
756 first
? display_int_cst (first
) : "(default)";
757 error ("incompatible array tuple element %s", err_val_name
);
758 value
= error_mark_node
;
761 value
= convert (element_type
, value
);
762 if (TREE_CODE (value
) == ERROR_MARK
)
764 else if (!TREE_CONSTANT (value
))
766 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
769 if (first
== NULL_TREE
)
771 if (default_value
!= NULL
)
773 error ("multiple (*) or (ELSE) array tuple labels");
776 default_value
= value
;
780 if (first
!= last
&& tree_int_cst_lt (last
, first
))
782 error ("empty range in array tuple");
789 #define MAYBE_RANGE_OP(PURPOSE, OPNO) \
790 (TREE_CODE (PURPOSE) == RANGE_EXPR ? TREE_OPERAND (PURPOSE, OPNO): PURPOSE)
791 #define CONSTRUCTOR_ELT_LO(ELT) MAYBE_RANGE_OP (TREE_PURPOSE (ELT), 0)
792 #define CONSTRUCTOR_ELT_HI(ELT) MAYBE_RANGE_OP (TREE_PURPOSE (ELT), 1)
793 while (*ptr
&& tree_int_cst_lt (last
,
794 CONSTRUCTOR_ELT_LO (*ptr
)))
795 ptr
= &TREE_CHAIN (*ptr
);
796 if (*ptr
&& ! tree_int_cst_lt (CONSTRUCTOR_ELT_HI (*ptr
), first
))
798 const char *err_val_name
= display_int_cst (first
);
799 error ("array tuple has duplicate index %s", err_val_name
);
803 if ((ptr
== &element_list
&& tree_int_cst_lt (domain_max
, last
))
804 || (*ptr
== NULL_TREE
&& tree_int_cst_lt (first
, domain_min
)))
807 error ("array tuple index out of range");
808 else if (errors
== 0)
809 error ("too many array tuple values");
813 if (! tree_int_cst_lt (first
, last
))
815 else if (purpose
== NULL_TREE
|| TREE_CODE (purpose
) != RANGE_EXPR
)
816 purpose
= build_nt (RANGE_EXPR
, first
, last
);
817 *ptr
= tree_cons (purpose
, value
, *ptr
);
820 element_list
= nreverse (element_list
);
822 /* For each missing element, set it to the default value,
823 if there is one. Otherwise, emit an error. */
826 && (!allow_missing_elements
|| default_value
!= NULL_TREE
))
828 /* Iterate over each *gap* between specified elements/ranges. */
831 tree_int_cst_equal (CONSTRUCTOR_ELT_LO (element_list
), domain_min
))
833 ptr
= &TREE_CHAIN (element_list
);
834 prev_elt
= element_list
;
838 prev_elt
= NULL_TREE
;
844 /* Calculate the first element of the gap. */
845 if (prev_elt
== NULL_TREE
)
849 first
= CONSTRUCTOR_ELT_HI (prev_elt
);
850 if (tree_int_cst_equal (first
, domain_max
))
851 break; /* We're done. Avoid overflow below. */
852 first
= copy_node (first
);
853 add_double (TREE_INT_CST_LOW (first
), TREE_INT_CST_HIGH (first
),
855 &TREE_INT_CST_LOW (first
),
856 &TREE_INT_CST_HIGH (first
));
858 /* Calculate the last element of the gap. */
861 /* Actually end up with correct type. */
862 last
= size_binop (MINUS_EXPR
,
863 CONSTRUCTOR_ELT_LO (*ptr
),
868 if (TREE_CODE (last
) == INTEGER_CST
&& tree_int_cst_lt (last
, first
))
869 ; /* Empty "gap" - no missing elements. */
870 else if (default_value
)
873 if (tree_int_cst_equal (first
, last
))
876 purpose
= build_nt (RANGE_EXPR
, first
, last
);
877 *ptr
= tree_cons (purpose
, default_value
, *ptr
);
881 const char *err_val_name
= display_int_cst (first
);
882 if (TREE_CODE (last
) != INTEGER_CST
)
883 error ("dynamic array tuple without (*) or (ELSE)");
884 else if (tree_int_cst_equal (first
, last
))
885 error ("missing array tuple element %s", err_val_name
);
888 char *first_name
= (char *)
889 xmalloc (strlen (err_val_name
) + 1);
890 strcpy (first_name
, err_val_name
);
891 err_val_name
= display_int_cst (last
);
892 error ("missing array tuple elements %s : %s",
893 first_name
, err_val_name
);
898 if (*ptr
== NULL_TREE
)
901 ptr
= &TREE_CHAIN (*ptr
);
905 return error_mark_node
;
907 element
= build (CONSTRUCTOR
, type
, NULL_TREE
, element_list
);
908 TREE_CONSTANT (element
) = is_constant
;
909 if (is_constant
&& is_simple
)
910 TREE_STATIC (element
) = 1;
911 if (labelled_elements
&& unlabelled_elements
)
912 pedwarn ("mixture of labelled and unlabelled tuple elements");
916 /* This function is needed because no-op CHILL conversions are not fully
917 understood by the initialization machinery. This function should only
918 be called when a conversion truly is a no-op. */
921 convert1 (type
, expr
)
924 int was_constant
= TREE_CONSTANT (expr
);
926 was_constant
|= TREE_CONSTANT (expr
);
927 expr
= copy_node (expr
);
928 TREE_TYPE (expr
) = type
;
929 if (TREE_CONSTANT (expr
) != was_constant
) abort ();
930 TREE_CONSTANT (expr
) = was_constant
;
934 /* Create an expression whose value is that of EXPR,
935 converted to type TYPE. The TREE_TYPE of the value
936 is always TYPE. This function implements all reasonable
937 conversions; callers should filter out those that are
938 not permitted by the language being compiled.
940 In CHILL, we assume that the type is Compatible with the
941 Class of expr, and generally complain otherwise.
942 However, convert is more general (e.g. allows enum<->int
943 conversion), so there should probably be at least two routines.
944 Maybe add something like convert_for_assignment. FIXME. */
950 register tree e
= expr
;
951 register enum chill_tree_code code
;
954 if (e
== NULL_TREE
|| TREE_CODE (e
) == ERROR_MARK
)
955 return error_mark_node
;
957 if (type
== NULL_TREE
|| TREE_CODE (type
) == ERROR_MARK
)
958 return error_mark_node
;
960 code
= TREE_CODE (type
);
962 if (type
== TREE_TYPE (e
))
965 if (TREE_TYPE (e
) != NULL_TREE
966 && TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
967 e
= convert_from_reference (e
);
969 /* Support for converting *to* a reference type is limited;
970 it is only here as a convenience for loc-identity declarations,
971 and loc parameters. */
972 if (code
== REFERENCE_TYPE
)
973 return convert_to_reference (type
, e
);
975 /* if expression was untyped because of its context (an if_expr or case_expr
976 in a tuple, perhaps) just apply the type */
977 if (TREE_TYPE (e
) && TREE_CODE (TREE_TYPE (e
)) == ERROR_MARK
)
979 TREE_TYPE (e
) = type
;
983 /* Turn a NULL keyword into [0, 0] for an instance */
984 if (CH_IS_INSTANCE_MODE (type
) && expr
== null_pointer_node
)
986 tree field0
= TYPE_FIELDS (type
);
987 tree field1
= TREE_CHAIN (field0
);
988 e
= build (CONSTRUCTOR
, type
, NULL_TREE
,
989 tree_cons (field0
, integer_zero_node
,
990 tree_cons (field1
, integer_zero_node
,
992 TREE_CONSTANT (e
) = 1;
997 /* Turn a pointer into a function pointer for a procmode */
998 if (TREE_CODE (type
) == POINTER_TYPE
999 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
1000 && expr
== null_pointer_node
)
1001 return convert1 (type
, expr
);
1003 /* turn function_decl expression into a pointer to
1005 if (TREE_CODE (expr
) == FUNCTION_DECL
1006 && TREE_CODE (type
) == POINTER_TYPE
1007 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
1009 e
= build1 (ADDR_EXPR
, type
, expr
);
1010 TREE_CONSTANT (e
) = 1;
1014 if (TREE_TYPE (e
) && TREE_CODE (TREE_TYPE (e
)) == RECORD_TYPE
)
1015 e
= varying_to_slice (e
);
1016 type_varying
= chill_varying_type_p (type
);
1018 /* Convert a char to a singleton string.
1019 Needed for compatibility with 1984 version of Z.200. */
1020 if (TREE_TYPE (e
) && TREE_CODE (TREE_TYPE (e
)) == CHAR_TYPE
1021 && (CH_CHARS_ONE_P (type
) || type_varying
))
1023 if (TREE_CODE (e
) == INTEGER_CST
)
1025 char ch
= TREE_INT_CST_LOW (e
);
1026 e
= build_chill_string (1, &ch
);
1029 e
= build (CONSTRUCTOR
, string_one_type_node
, NULL_TREE
,
1030 tree_cons (NULL_TREE
, e
, NULL_TREE
));
1033 /* Convert a Boolean to a singleton bitstring.
1034 Needed for compatibility with 1984 version of Z.200. */
1035 if (TREE_TYPE (e
) && TREE_CODE (TREE_TYPE (e
)) == BOOLEAN_TYPE
1036 && (CH_BOOLS_ONE_P (type
) || type_varying
))
1038 if (TREE_CODE (e
) == INTEGER_CST
)
1039 e
= integer_zerop (e
) ? bit_zero_node
: bit_one_node
;
1041 e
= build (COND_EXPR
, bitstring_one_type_node
,
1042 e
, bit_one_node
, bit_zero_node
);
1048 tree field0
= TYPE_FIELDS (type
);
1049 tree field1
= TREE_CHAIN (field0
);
1051 tree target_array_type
= TREE_TYPE (field1
);
1052 tree needed_padding
;
1053 tree padding_max_size
= 0;
1054 int orig_e_constant
= TREE_CONSTANT (orig_e
);
1055 if (TREE_TYPE (e
) != NULL_TREE
1056 && TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
)
1058 /* Note that array_type_nelts returns 1 less than the size. */
1059 nentries
= array_type_nelts (TREE_TYPE (e
));
1060 needed_padding
= size_binop (MINUS_EXPR
,
1061 array_type_nelts (target_array_type
),
1063 if (TREE_CODE (needed_padding
) != INTEGER_CST
)
1065 padding_max_size
= size_in_bytes (TREE_TYPE (e
));
1066 if (TREE_CODE (padding_max_size
) != INTEGER_CST
)
1067 padding_max_size
= TYPE_ARRAY_MAX_SIZE (TREE_TYPE (e
));
1069 nentries
= size_binop (PLUS_EXPR
, nentries
, integer_one_node
);
1071 else if (TREE_CODE (e
) == CONSTRUCTOR
)
1073 HOST_WIDE_INT init_cnt
= 0;
1074 tree chaser
= CONSTRUCTOR_ELTS (e
);
1075 for ( ; chaser
; chaser
= TREE_CHAIN (chaser
))
1076 init_cnt
++; /* count initializer elements */
1077 nentries
= build_int_2 (init_cnt
, 0);
1078 needed_padding
= integer_zero_node
;
1079 if (TREE_TYPE (e
) == NULL_TREE
)
1080 e
= digest_array_tuple (TREE_TYPE (field1
), e
, 1);
1081 orig_e_constant
= TREE_CONSTANT (e
);
1085 error ("initializer is not an array or string mode");
1086 return error_mark_node
;
1089 FIXME check that nentries will fit in type
;
1091 if (!integer_zerop (needed_padding
))
1093 tree padding
, padding_type
, padding_range
;
1094 if (TREE_CODE (needed_padding
) == INTEGER_CST
1095 && (long)TREE_INT_CST_LOW (needed_padding
) < 0)
1097 error ("destination is too small");
1098 return error_mark_node
;
1100 padding_range
= build_chill_range_type (NULL_TREE
, integer_one_node
,
1103 = build_simple_array_type (TREE_TYPE (target_array_type
),
1104 padding_range
, NULL_TREE
);
1105 TYPE_ARRAY_MAX_SIZE (padding_type
) = padding_max_size
;
1106 if (CH_CHARS_TYPE_P (target_array_type
))
1107 MARK_AS_STRING_TYPE (padding_type
);
1108 padding
= build (UNDEFINED_EXPR
, padding_type
);
1109 if (TREE_CONSTANT (e
))
1110 e
= build_chill_binary_op (CONCAT_EXPR
, e
, padding
);
1112 e
= build (CONCAT_EXPR
, target_array_type
, e
, padding
);
1114 e
= convert (TREE_TYPE (field1
), e
);
1115 /* We build this constructor by hand (rather than going through
1116 digest_structure_tuple), to avoid some type-checking problem.
1117 E.g. type may have non-null novelty, but its field1 will
1118 have non-novelty. */
1119 e
= build (CONSTRUCTOR
, type
, NULL_TREE
,
1120 tree_cons (field0
, nentries
,
1121 build_tree_list (field1
, e
)));
1122 /* following was wrong, cause orig_e never will be TREE_CONSTANT. e
1123 may become constant after digest_array_tuple. */
1124 if (TREE_CONSTANT (nentries
) && orig_e_constant
) /* TREE_CONSTANT (orig_e)) */
1126 TREE_CONSTANT (e
) = 1;
1127 if (TREE_STATIC (nentries
) && TREE_STATIC (orig_e
))
1128 TREE_STATIC (e
) = 1;
1131 if (TREE_TYPE (e
) == NULL_TREE
)
1133 if (TREE_CODE (e
) == CONSTRUCTOR
)
1135 if (TREE_CODE (type
) == SET_TYPE
)
1136 return digest_powerset_tuple (type
, e
);
1137 if (TREE_CODE (type
) == RECORD_TYPE
)
1138 return digest_structure_tuple (type
, e
);
1139 if (TREE_CODE (type
) == ARRAY_TYPE
)
1140 return digest_array_tuple (type
, e
, 0);
1141 fatal ("internal error - bad CONSTRUCTOR passed to convert");
1143 else if (TREE_CODE (e
) == COND_EXPR
)
1144 e
= build (COND_EXPR
, type
,
1145 TREE_OPERAND (e
, 0),
1146 convert (type
, TREE_OPERAND (e
, 1)),
1147 convert (type
, TREE_OPERAND (e
, 2)));
1148 else if (TREE_CODE (e
) == CASE_EXPR
)
1149 TREE_TYPE (e
) = type
;
1152 error ("internal error: unknown type of expression");
1153 return error_mark_node
;
1157 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
))
1158 || (CH_NOVELTY (type
) != NULL_TREE
1159 && CH_NOVELTY (type
) == CH_NOVELTY (TREE_TYPE (e
))))
1160 return convert1 (type
, e
);
1162 if (TREE_CODE (TREE_TYPE (e
)) == VOID_TYPE
)
1164 error ("void value not ignored as it ought to be");
1165 return error_mark_node
;
1167 if (code
== VOID_TYPE
)
1168 return build1 (CONVERT_EXPR
, type
, e
);
1170 if (code
== SET_TYPE
)
1171 return convert1 (type
, e
);
1173 if (code
== INTEGER_TYPE
|| code
== ENUMERAL_TYPE
)
1175 if (flag_old_strings
)
1177 if (CH_CHARS_ONE_P (TREE_TYPE (e
)))
1178 e
= convert_to_char (char_type_node
, e
);
1179 else if (CH_BOOLS_ONE_P (TREE_TYPE (e
)))
1180 e
= convert_to_boolean (boolean_type_node
, e
);
1182 return fold (convert_to_integer (type
, e
));
1184 if (code
== POINTER_TYPE
)
1185 return fold (convert_to_pointer (type
, e
));
1186 if (code
== REAL_TYPE
)
1187 return fold (convert_to_real (type
, e
));
1188 if (code
== BOOLEAN_TYPE
)
1189 return fold (convert_to_boolean (type
, e
));
1190 if (code
== CHAR_TYPE
)
1191 return fold (convert_to_char (type
, e
));
1193 if (code
== ARRAY_TYPE
&& TYPE_MODE (type
) != TYPE_MODE (TREE_TYPE (e
)))
1195 /* The mode of the expression is different from that of the type.
1196 Earlier checks should have tested against different lengths.
1197 But even if the lengths are the same, it is possible that one
1198 type is a static type (and hence could be say SImode), while the
1199 other type is dynamic type (and hence is BLKmode).
1200 This causes problems when emitting instructions. */
1201 tree ee
= build1 (INDIRECT_REF
, type
,
1202 build1 (NOP_EXPR
, build_pointer_type (type
),
1204 build_pointer_type (TREE_TYPE (e
)),
1206 TREE_READONLY (ee
) = TYPE_READONLY (type
);
1211 return convert1 (type
, e
);
1214 /* Return an expression whose value is EXPR, but whose class is CLASS. */
1217 convert_to_class (class, expr
)
1218 struct ch_class
class;
1226 case CH_DERIVED_CLASS
:
1227 if (TREE_TYPE (expr
) != class.mode
)
1228 expr
= convert (class.mode
, expr
);
1229 if (!CH_DERIVED_FLAG (expr
))
1231 expr
= copy_node (expr
);
1232 CH_DERIVED_FLAG (expr
) = 1;
1235 case CH_VALUE_CLASS
:
1236 case CH_REFERENCE_CLASS
:
1237 if (TREE_TYPE (expr
) != class.mode
)
1238 expr
= convert (class.mode
, expr
);
1239 if (CH_DERIVED_FLAG (expr
))
1241 expr
= copy_node (expr
);
1242 CH_DERIVED_FLAG (expr
) = 0;