middle-end/118695 - missed misalign handling in MEM_REF expansion
[official-gcc.git] / gcc / m2 / gm2-gcc / m2type.cc
bloba946509d1c25904c0f4d1cdc9ee6a75f9e6edbdb
1 /* m2type.cc provides an interface to GCC type trees.
3 Copyright (C) 2012-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius@glam.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "gcc-consolidation.h"
24 #include "../gm2-lang.h"
25 #include "../m2-tree.h"
27 #define m2type_c
28 #include "m2assert.h"
29 #include "m2block.h"
30 #include "m2builtins.h"
31 #include "m2convert.h"
32 #include "m2decl.h"
33 #include "m2except.h"
34 #include "m2expr.h"
35 #include "m2linemap.h"
36 #include "m2tree.h"
37 #include "m2treelib.h"
38 #include "m2type.h"
39 #include "m2options.h"
40 #include "m2configure.h"
42 #define USE_BOOLEAN
43 static int broken_set_debugging_info = true;
46 struct GTY (()) struct_constructor
48 /* Constructor_type, the type that we are constructing. */
49 tree GTY ((skip (""))) constructor_type;
50 /* Constructor_fields, the list of fields belonging to
51 constructor_type. Used by SET and RECORD constructors. */
52 tree GTY ((skip (""))) constructor_fields;
53 /* Constructor_element_list, the list of constants used by SET and
54 RECORD constructors. */
55 tree GTY ((skip (""))) constructor_element_list;
56 /* Constructor_elements, used by an ARRAY initializer all elements
57 are held in reverse order. */
58 vec<constructor_elt, va_gc> *constructor_elements;
59 /* Level, the next level down in the constructor stack. */
60 struct struct_constructor *level;
63 static GTY (()) struct struct_constructor *top_constructor = NULL;
65 typedef struct GTY (()) array_desc
67 int type;
68 tree index;
69 tree array;
70 struct array_desc *next;
71 } array_desc;
73 static GTY (()) array_desc *list_of_arrays = NULL;
74 /* Used in BuildStartFunctionType. */
75 static GTY (()) tree param_type_list;
77 static GTY (()) tree proc_type_node;
78 static GTY (()) tree bitset_type_node;
79 static GTY (()) tree bitnum_type_node;
80 static GTY (()) tree m2_char_type_node;
81 static GTY (()) tree m2_integer_type_node;
82 static GTY (()) tree m2_cardinal_type_node;
83 static GTY (()) tree m2_short_real_type_node;
84 static GTY (()) tree m2_real_type_node;
85 static GTY (()) tree m2_long_real_type_node;
86 static GTY (()) tree m2_long_int_type_node;
87 static GTY (()) tree m2_long_card_type_node;
88 static GTY (()) tree m2_short_int_type_node;
89 static GTY (()) tree m2_short_card_type_node;
90 static GTY (()) tree m2_z_type_node;
91 static GTY (()) tree m2_iso_loc_type_node;
92 static GTY (()) tree m2_iso_byte_type_node;
93 static GTY (()) tree m2_iso_word_type_node;
94 static GTY (()) tree m2_integer8_type_node;
95 static GTY (()) tree m2_integer16_type_node;
96 static GTY (()) tree m2_integer32_type_node;
97 static GTY (()) tree m2_integer64_type_node;
98 static GTY (()) tree m2_cardinal8_type_node;
99 static GTY (()) tree m2_cardinal16_type_node;
100 static GTY (()) tree m2_cardinal32_type_node;
101 static GTY (()) tree m2_cardinal64_type_node;
102 static GTY (()) tree m2_word16_type_node;
103 static GTY (()) tree m2_word32_type_node;
104 static GTY (()) tree m2_word64_type_node;
105 static GTY (()) tree m2_bitset8_type_node;
106 static GTY (()) tree m2_bitset16_type_node;
107 static GTY (()) tree m2_bitset32_type_node;
108 static GTY (()) tree m2_real32_type_node;
109 static GTY (()) tree m2_real64_type_node;
110 static GTY (()) tree m2_real96_type_node;
111 static GTY (()) tree m2_real128_type_node;
112 static GTY (()) tree m2_complex_type_node;
113 static GTY (()) tree m2_long_complex_type_node;
114 static GTY (()) tree m2_short_complex_type_node;
115 static GTY (()) tree m2_c_type_node;
116 static GTY (()) tree m2_complex32_type_node;
117 static GTY (()) tree m2_complex64_type_node;
118 static GTY (()) tree m2_complex96_type_node;
119 static GTY (()) tree m2_complex128_type_node;
120 static GTY (()) tree m2_packed_boolean_type_node;
121 static GTY (()) tree m2_cardinal_address_type_node;
122 static GTY (()) tree m2_offt_type_node;
124 /* gm2_canonicalize_array - returns a unique array node based on
125 index_type and type. */
127 static tree
128 gm2_canonicalize_array (tree index_type, int type)
130 array_desc *l = list_of_arrays;
132 while (l != NULL)
134 if (l->type == type && l->index == index_type)
135 return l->array;
136 else
137 l = l->next;
139 l = ggc_alloc<array_desc> ();
140 l->next = list_of_arrays;
141 l->type = type;
142 l->index = index_type;
143 l->array = make_node (ARRAY_TYPE);
144 TREE_TYPE (l->array) = NULL_TREE;
145 TYPE_DOMAIN (l->array) = index_type;
146 list_of_arrays = l;
147 return l->array;
150 /* BuildStartArrayType - creates an array with an indextype and
151 elttype. The front end symbol type is also passed to allow the
152 gccgm2 to return the canonical edition of the array type even if
153 the GCC elttype is NULL_TREE. */
155 tree
156 m2type_BuildStartArrayType (tree index_type, tree elt_type, int type)
158 tree t;
160 elt_type = m2tree_skip_type_decl (elt_type);
161 ASSERT_CONDITION (index_type != NULL_TREE);
162 if (elt_type == NULL_TREE)
164 /* Cannot use GCC canonicalization routines yet, so we use our front
165 end version based on the front end type. */
166 return gm2_canonicalize_array (index_type, type);
168 t = gm2_canonicalize_array (index_type, type);
169 if (TREE_TYPE (t) == NULL_TREE)
170 TREE_TYPE (t) = elt_type;
171 else
172 ASSERT_CONDITION (TREE_TYPE (t) == elt_type);
174 return t;
177 /* PutArrayType assignes TREE_TYPE (array) to the skipped type. */
179 void
180 m2type_PutArrayType (tree array, tree type)
182 TREE_TYPE (array) = m2tree_skip_type_decl (type);
185 /* gccgm2_GetArrayNoOfElements returns the number of elements in
186 arraytype. */
188 tree
189 m2type_GetArrayNoOfElements (location_t location, tree arraytype)
191 tree index_type = TYPE_DOMAIN (m2tree_skip_type_decl (arraytype));
192 tree min = TYPE_MIN_VALUE (index_type);
193 tree max = TYPE_MAX_VALUE (index_type);
195 m2assert_AssertLocation (location);
196 return m2expr_FoldAndStrip (m2expr_BuildSub (location, max, min, false));
199 /* gm2_finish_build_array_type complete building the partially
200 created array type, arrayType. The arrayType is now known to be
201 declared as: ARRAY index_type OF elt_type. There will only ever
202 be one gcc tree type for this array definition. The third
203 parameter type is a front end type and this is necessary so that
204 the canonicalization creates unique array types for each type. */
206 static tree
207 gm2_finish_build_array_type (tree arrayType, tree elt_type, tree index_type,
208 int type)
210 tree old = arrayType;
212 elt_type = m2tree_skip_type_decl (elt_type);
213 ASSERT_CONDITION (index_type != NULL_TREE);
214 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
216 error ("arrays of functions are not meaningful");
217 elt_type = integer_type_node;
220 TREE_TYPE (arrayType) = elt_type;
221 TYPE_DOMAIN (arrayType) = index_type;
223 arrayType = gm2_canonicalize_array (index_type, type);
224 if (arrayType != old)
225 internal_error ("array declaration canonicalization has failed");
227 if (!COMPLETE_TYPE_P (arrayType))
228 layout_type (arrayType);
229 return arrayType;
232 /* BuildEndArrayType returns a type which is an array indexed by
233 IndexType and which has ElementType elements. */
235 tree
236 m2type_BuildEndArrayType (tree arraytype, tree elementtype, tree indextype,
237 int type)
239 elementtype = m2tree_skip_type_decl (elementtype);
240 ASSERT (indextype == TYPE_DOMAIN (arraytype), indextype);
242 if (TREE_CODE (elementtype) == FUNCTION_TYPE)
243 return gm2_finish_build_array_type (arraytype, ptr_type_node, indextype,
244 type);
245 else
246 return gm2_finish_build_array_type (
247 arraytype, m2tree_skip_type_decl (elementtype), indextype, type);
250 /* gm2_build_array_type returns a type which is an array indexed by
251 IndexType and which has ElementType elements. */
253 static tree
254 gm2_build_array_type (tree elementtype, tree indextype, int fetype)
256 tree arrayType = m2type_BuildStartArrayType (indextype, elementtype, fetype);
257 return m2type_BuildEndArrayType (arrayType, elementtype, indextype, fetype);
260 /* ValueInTypeRange returns true if the constant, value, lies within
261 the range of type. */
263 bool
264 m2type_ValueInTypeRange (tree type, tree value)
266 tree low_type = m2tree_skip_type_decl (type);
267 tree min_value = TYPE_MIN_VALUE (low_type);
268 tree max_value = TYPE_MAX_VALUE (low_type);
270 value = m2expr_FoldAndStrip (value);
271 return ((tree_int_cst_compare (min_value, value) <= 0)
272 && (tree_int_cst_compare (value, max_value) <= 0));
275 /* ValueOutOfTypeRange returns true if the constant, value, exceeds
276 the range of type. */
278 bool
279 m2type_ValueOutOfTypeRange (tree type, tree value)
281 return (!m2type_ValueInTypeRange (type, value));
284 /* ExceedsTypeRange return true if low or high exceed the range of
285 type. */
287 bool
288 m2type_ExceedsTypeRange (tree type, tree low, tree high)
290 return (m2type_ValueOutOfTypeRange (type, low)
291 || m2type_ValueOutOfTypeRange (type, high));
294 /* WithinTypeRange return true if low and high are within the range
295 of type. */
297 bool
298 m2type_WithinTypeRange (tree type, tree low, tree high)
300 return (m2type_ValueInTypeRange (type, low)
301 && m2type_ValueInTypeRange (type, high));
304 /* BuildArrayIndexType creates an integer index which accesses an
305 array. low and high are the min, max elements of the array. GCC
306 insists we access an array with an integer indice. */
308 tree
309 m2type_BuildArrayIndexType (tree low, tree high)
311 tree sizelow = convert (m2type_GetIntegerType (), m2expr_FoldAndStrip (low));
312 tree sizehigh
313 = convert (m2type_GetIntegerType (), m2expr_FoldAndStrip (high));
315 if (m2expr_TreeOverflow (sizelow))
316 error ("low bound for the array is outside the ztype limits");
317 if (m2expr_TreeOverflow (sizehigh))
318 error ("high bound for the array is outside the ztype limits");
320 return build_range_type (m2type_GetIntegerType (),
321 m2expr_FoldAndStrip (sizelow),
322 m2expr_FoldAndStrip (sizehigh));
325 /* build_m2_type_node_by_array builds a ISO Modula-2 word type from
326 ARRAY [low..high] OF arrayType. This matches the front end data
327 type fetype which is only used during canonicalization. */
329 static tree
330 build_m2_type_node_by_array (tree arrayType, tree low, tree high, int fetype)
332 return gm2_build_array_type (arrayType,
333 m2type_BuildArrayIndexType (low, high), fetype);
336 /* build_m2_word16_type_node build an ISO 16 bit word as an ARRAY
337 [0..1] OF loc. */
339 static tree
340 build_m2_word16_type_node (location_t location, int loc)
342 return build_m2_type_node_by_array (m2type_GetISOLocType (),
343 m2expr_GetIntegerZero (location),
344 m2expr_GetIntegerOne (location), loc);
347 /* build_m2_word32_type_node build an ISO 32 bit word as an ARRAY
348 [0..3] OF loc. */
350 static tree
351 build_m2_word32_type_node (location_t location, int loc)
353 return build_m2_type_node_by_array (m2type_GetISOLocType (),
354 m2expr_GetIntegerZero (location),
355 m2decl_BuildIntegerConstant (3), loc);
358 /* build_m2_word64_type_node build an ISO 32 bit word as an ARRAY
359 [0..7] OF loc. */
361 static tree
362 build_m2_word64_type_node (location_t location, int loc)
364 return build_m2_type_node_by_array (m2type_GetISOLocType (),
365 m2expr_GetIntegerZero (location),
366 m2decl_BuildIntegerConstant (7), loc);
370 /* GetM2Complex32 return the fixed size complex type. */
372 tree
373 m2type_GetM2Complex32 (void)
375 return m2_complex32_type_node;
378 /* GetM2Complex64 return the fixed size complex type. */
380 tree
381 m2type_GetM2Complex64 (void)
383 return m2_complex64_type_node;
386 /* GetM2Complex96 return the fixed size complex type. */
388 tree
389 m2type_GetM2Complex96 (void)
391 return m2_complex96_type_node;
394 /* GetM2Complex128 return the fixed size complex type. */
396 tree
397 m2type_GetM2Complex128 (void)
399 return m2_complex128_type_node;
402 /* GetM2CType a test function. */
404 tree
405 m2type_GetM2CType (void)
407 return m2_c_type_node;
410 /* GetM2ShortComplexType return the short complex type. */
412 tree
413 m2type_GetM2ShortComplexType (void)
415 return m2_short_complex_type_node;
418 /* GetM2LongComplexType return the long complex type. */
420 tree
421 m2type_GetM2LongComplexType (void)
423 return m2_long_complex_type_node;
426 /* GetM2ComplexType return the complex type. */
428 tree
429 m2type_GetM2ComplexType (void)
431 return m2_complex_type_node;
434 /* GetM2Real128 return the real 128 bit type. */
436 tree
437 m2type_GetM2Real128 (void)
439 return m2_real128_type_node;
442 /* GetM2Real96 return the real 96 bit type. */
444 tree
445 m2type_GetM2Real96 (void)
447 return m2_real96_type_node;
450 /* GetM2Real64 return the real 64 bit type. */
452 tree
453 m2type_GetM2Real64 (void)
455 return m2_real64_type_node;
458 /* GetM2Real32 return the real 32 bit type. */
460 tree
461 m2type_GetM2Real32 (void)
463 return m2_real32_type_node;
466 /* GetM2Bitset32 return the bitset 32 bit type. */
468 tree
469 m2type_GetM2Bitset32 (void)
471 return m2_bitset32_type_node;
474 /* GetM2Bitset16 return the bitset 16 bit type. */
476 tree
477 m2type_GetM2Bitset16 (void)
479 return m2_bitset16_type_node;
482 /* GetM2Bitset8 return the bitset 8 bit type. */
484 tree
485 m2type_GetM2Bitset8 (void)
487 return m2_bitset8_type_node;
490 /* GetM2Word64 return the word 64 bit type. */
492 tree
493 m2type_GetM2Word64 (void)
495 return m2_word64_type_node;
498 /* GetM2Word32 return the word 32 bit type. */
500 tree
501 m2type_GetM2Word32 (void)
503 return m2_word32_type_node;
506 /* GetM2Word16 return the word 16 bit type. */
508 tree
509 m2type_GetM2Word16 (void)
511 return m2_word16_type_node;
514 /* GetM2Cardinal64 return the cardinal 64 bit type. */
516 tree
517 m2type_GetM2Cardinal64 (void)
519 return m2_cardinal64_type_node;
522 /* GetM2Cardinal32 return the cardinal 32 bit type. */
524 tree
525 m2type_GetM2Cardinal32 (void)
527 return m2_cardinal32_type_node;
530 /* GetM2Cardinal16 return the cardinal 16 bit type. */
532 tree
533 m2type_GetM2Cardinal16 (void)
535 return m2_cardinal16_type_node;
538 /* GetM2Cardinal8 return the cardinal 8 bit type. */
540 tree
541 m2type_GetM2Cardinal8 (void)
543 return m2_cardinal8_type_node;
546 /* GetM2Integer64 return the integer 64 bit type. */
548 tree
549 m2type_GetM2Integer64 (void)
551 return m2_integer64_type_node;
554 /* GetM2Integer32 return the integer 32 bit type. */
556 tree
557 m2type_GetM2Integer32 (void)
559 return m2_integer32_type_node;
562 /* GetM2Integer16 return the integer 16 bit type. */
564 tree
565 m2type_GetM2Integer16 (void)
567 return m2_integer16_type_node;
570 /* GetM2Integer8 return the integer 8 bit type. */
572 tree
573 m2type_GetM2Integer8 (void)
575 return m2_integer8_type_node;
578 /* GetM2RType return the ISO R data type, the longest real
579 datatype. */
581 tree
582 m2type_GetM2RType (void)
584 return long_double_type_node;
587 /* GetM2ZType return the ISO Z data type, the longest int datatype. */
589 tree
590 m2type_GetM2ZType (void)
592 return m2_z_type_node;
595 /* GetShortCardType return the C short unsigned data type. */
597 tree
598 m2type_GetShortCardType (void)
600 return short_unsigned_type_node;
603 /* GetM2ShortCardType return the m2 short cardinal data type. */
605 tree
606 m2type_GetM2ShortCardType (void)
608 return m2_short_card_type_node;
611 /* GetShortIntType return the C short int data type. */
613 tree
614 m2type_GetShortIntType (void)
616 return short_integer_type_node;
619 /* GetM2ShortIntType return the m2 short integer data type. */
621 tree
622 m2type_GetM2ShortIntType (void)
624 return m2_short_int_type_node;
627 /* GetM2LongCardType return the m2 long cardinal data type. */
629 tree
630 m2type_GetM2LongCardType (void)
632 return m2_long_card_type_node;
635 /* GetM2LongIntType return the m2 long integer data type. */
637 tree
638 m2type_GetM2LongIntType (void)
640 return m2_long_int_type_node;
643 /* GetM2LongRealType return the m2 long real data type. */
645 tree
646 m2type_GetM2LongRealType (void)
648 return m2_long_real_type_node;
651 /* GetM2RealType return the m2 real data type. */
653 tree
654 m2type_GetM2RealType (void)
656 return m2_real_type_node;
659 /* GetM2ShortRealType return the m2 short real data type. */
661 tree
662 m2type_GetM2ShortRealType (void)
664 return m2_short_real_type_node;
667 /* GetM2CardinalType return the m2 cardinal data type. */
669 tree
670 m2type_GetM2CardinalType (void)
672 return m2_cardinal_type_node;
675 /* GetM2IntegerType return the m2 integer data type. */
677 tree
678 m2type_GetM2IntegerType (void)
680 return m2_integer_type_node;
683 /* GetM2CharType return the m2 char data type. */
685 tree
686 m2type_GetM2CharType (void)
688 return m2_char_type_node;
691 /* GetProcType return the m2 proc data type. */
693 tree
694 m2type_GetProcType (void)
696 return proc_type_node;
699 /* GetISOWordType return the m2 iso word data type. */
701 tree
702 m2type_GetISOWordType (void)
704 return m2_iso_word_type_node;
707 /* GetISOByteType return the m2 iso byte data type. */
709 tree
710 m2type_GetISOByteType (void)
712 return m2_iso_byte_type_node;
715 /* GetISOLocType return the m2 loc word data type. */
717 tree
718 m2type_GetISOLocType (void)
720 return m2_iso_loc_type_node;
723 /* GetWordType return the C unsigned data type. */
725 tree
726 m2type_GetWordType (void)
728 return unsigned_type_node;
731 /* GetLongIntType return the C long int data type. */
733 tree
734 m2type_GetLongIntType (void)
736 return long_integer_type_node;
739 /* GetShortRealType return the C float data type. */
741 tree
742 m2type_GetShortRealType (void)
744 return float_type_node;
747 /* GetLongRealType return the C long double data type. */
749 tree
750 m2type_GetLongRealType (void)
752 return long_double_type_node;
755 /* GetRealType returns the C double_type_node. */
757 tree
758 m2type_GetRealType (void)
760 return double_type_node;
763 /* GetBitnumType return the ISO bitnum type. */
765 tree
766 m2type_GetBitnumType (void)
768 return bitnum_type_node;
771 /* GetBitsetType return the bitset type. */
773 tree
774 m2type_GetBitsetType (void)
776 return bitset_type_node;
779 /* GetCardinalType return the cardinal type. */
781 tree
782 m2type_GetCardinalType (void)
784 return unsigned_type_node;
787 /* GetPointerType return the GCC ptr type node. Equivalent to
788 (void *). */
790 tree
791 m2type_GetPointerType (void)
793 return ptr_type_node;
796 /* GetVoidType return the C void type. */
798 tree
799 m2type_GetVoidType (void)
801 return void_type_node;
804 /* GetByteType return the byte type node. */
806 tree
807 m2type_GetByteType (void)
809 return unsigned_char_type_node;
812 /* GetCharType return the char type node. */
814 tree
815 m2type_GetCharType (void)
817 return char_type_node;
820 /* GetIntegerType return the integer type node. */
822 tree
823 m2type_GetIntegerType (void)
825 return integer_type_node;
828 /* GetCSizeTType return a type representing size_t. */
830 tree
831 m2type_GetCSizeTType (void)
833 return sizetype;
836 /* GetCSSizeTType return a type representing size_t. */
838 tree
839 m2type_GetCSSizeTType (void)
841 return ssizetype;
844 /* GetCSSizeTType return a type representing off_t. */
846 tree
847 m2type_GetCOffTType (void)
849 return m2_offt_type_node;
852 /* GetPackedBooleanType return the packed boolean data type node. */
854 tree
855 m2type_GetPackedBooleanType (void)
857 return m2_packed_boolean_type_node;
860 /* GetBooleanTrue return modula-2 true. */
862 tree
863 m2type_GetBooleanTrue (void)
865 #if defined(USE_BOOLEAN)
866 return boolean_true_node;
867 #else /* !USE_BOOLEAN */
868 return m2expr_GetIntegerOne (m2linemap_BuiltinsLocation ());
869 #endif /* !USE_BOOLEAN */
872 /* GetBooleanFalse return modula-2 FALSE. */
874 tree
875 m2type_GetBooleanFalse (void)
877 #if defined(USE_BOOLEAN)
878 return boolean_false_node;
879 #else /* !USE_BOOLEAN */
880 return m2expr_GetIntegerZero (m2linemap_BuiltinsLocation ());
881 #endif /* !USE_BOOLEAN */
884 /* GetBooleanType return the modula-2 BOOLEAN type. */
886 tree
887 m2type_GetBooleanType (void)
889 #if defined(USE_BOOLEAN)
890 return boolean_type_node;
891 #else /* !USE_BOOLEAN */
892 return integer_type_node;
893 #endif /* !USE_BOOLEAN */
896 /* GetCardinalAddressType returns the internal data type for
897 computing binary arithmetic upon the ADDRESS datatype. */
899 tree
900 m2type_GetCardinalAddressType (void)
902 return m2_cardinal_address_type_node;
905 #if 0
906 /* build_set_type creates a set type from the, domain, [low..high].
907 The values low..high all have type, range_type. */
909 static tree
910 build_set_type (tree domain, tree range_type, int allow_void, int ispacked)
912 tree type;
914 if (!m2tree_IsOrdinal (domain)
915 && !(allow_void && TREE_CODE (domain) == VOID_TYPE))
917 error ("set base type must be an ordinal type");
918 return NULL;
921 if (TYPE_SIZE (range_type) == 0)
922 layout_type (range_type);
924 if (TYPE_SIZE (domain) == 0)
925 layout_type (domain);
927 type = make_node (SET_TYPE);
928 TREE_TYPE (type) = range_type;
929 TYPE_DOMAIN (type) = domain;
930 TYPE_PACKED (type) = ispacked;
931 return type;
935 /* convert_type_to_range does the conversion and copies the range
936 type */
938 static tree
939 convert_type_to_range (tree type)
941 tree min, max;
942 tree itype;
944 if (!m2tree_IsOrdinal (type))
946 error ("ordinal type expected");
947 return error_mark_node;
950 min = TYPE_MIN_VALUE (type);
951 max = TYPE_MAX_VALUE (type);
953 if (TREE_TYPE (min) != TREE_TYPE (max))
955 error ("range limits are not of the same type");
956 return error_mark_node;
959 itype = build_range_type (TREE_TYPE (min), min, max);
961 if (TREE_TYPE (type) == NULL_TREE)
963 layout_type (type);
964 TREE_TYPE (itype) = type;
966 else
968 layout_type (TREE_TYPE (type));
969 TREE_TYPE (itype) = TREE_TYPE (type);
972 layout_type (itype);
973 return itype;
975 #endif
977 /* build_bitset_type builds the type BITSET which is exported from
978 SYSTEM. It also builds BITNUM (the subrange from which BITSET is
979 created). */
981 static tree
982 build_bitset_type (location_t location)
984 m2assert_AssertLocation (location);
985 bitnum_type_node = build_range_type (
986 m2tree_skip_type_decl (m2type_GetCardinalType ()),
987 m2decl_BuildIntegerConstant (0),
988 m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset () - 1));
989 layout_type (bitnum_type_node);
991 #if 1
992 if (broken_set_debugging_info)
993 return unsigned_type_node;
994 #endif
996 ASSERT ((COMPLETE_TYPE_P (bitnum_type_node)), bitnum_type_node);
998 return m2type_BuildSetTypeFromSubrange (
999 location, NULL, bitnum_type_node, m2decl_BuildIntegerConstant (0),
1000 m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset () - 1), false);
1003 /* BuildSetTypeFromSubrange constructs a set type from a
1004 subrangeType. --fixme-- revisit once gdb/gcc supports dwarf-5 set type. */
1006 tree
1007 m2type_BuildSetTypeFromSubrange (location_t location,
1008 char *name __attribute__ ((unused)),
1009 tree subrangeType __attribute__ ((unused)),
1010 tree lowval, tree highval, bool ispacked)
1012 m2assert_AssertLocation (location);
1013 lowval = m2expr_FoldAndStrip (lowval);
1014 highval = m2expr_FoldAndStrip (highval);
1016 #if 0
1017 if (broken_set_debugging_info)
1018 return unsigned_type_node;
1019 else
1020 #endif
1021 if (ispacked)
1023 tree noelements = m2expr_BuildAdd (
1024 location, m2expr_BuildSub (location, highval, lowval, false),
1025 integer_one_node, false);
1026 highval = m2expr_FoldAndStrip (m2expr_BuildSub (
1027 location, m2expr_BuildLSL (location, m2expr_GetWordOne (location),
1028 noelements, false),
1029 m2expr_GetIntegerOne (location), false));
1030 lowval = m2expr_GetIntegerZero (location);
1031 return m2type_BuildSmallestTypeRange (location, lowval, highval);
1033 else
1034 return unsigned_type_node;
1037 /* build_m2_size_set_type build and return a set type with
1038 precision bits. */
1040 static tree
1041 build_m2_size_set_type (location_t location, int precision)
1043 tree bitnum_type_node
1044 = build_range_type (m2tree_skip_type_decl (m2type_GetCardinalType ()),
1045 m2decl_BuildIntegerConstant (0),
1046 m2decl_BuildIntegerConstant (precision - 1));
1047 layout_type (bitnum_type_node);
1048 m2assert_AssertLocation (location);
1050 if (broken_set_debugging_info)
1051 return unsigned_type_node;
1053 ASSERT ((COMPLETE_TYPE_P (bitnum_type_node)), bitnum_type_node);
1055 return m2type_BuildSetTypeFromSubrange (
1056 location, NULL, bitnum_type_node, m2decl_BuildIntegerConstant (0),
1057 m2decl_BuildIntegerConstant (precision - 1), false);
1060 /* build_m2_specific_size_type build a specific data type matching
1061 number of bits precision whether it is_signed. It creates a
1062 set type if base == SET_TYPE or returns the already created real,
1063 if REAL_TYPE is specified. */
1065 static tree
1066 build_m2_specific_size_type (location_t location, enum tree_code base,
1067 int precision, int is_signed)
1069 tree c;
1071 m2assert_AssertLocation (location);
1073 c = make_node (base);
1074 TYPE_PRECISION (c) = precision;
1076 if (base == REAL_TYPE)
1078 if (!float_mode_for_size (TYPE_PRECISION (c)).exists ())
1079 return NULL;
1081 else if (base == SET_TYPE)
1082 return build_m2_size_set_type (location, precision);
1083 else
1085 TYPE_SIZE (c) = 0;
1087 if (is_signed)
1089 fixup_signed_type (c);
1090 TYPE_UNSIGNED (c) = false;
1092 else
1094 fixup_unsigned_type (c);
1095 TYPE_UNSIGNED (c) = true;
1098 layout_type (c);
1099 return c;
1102 /* BuildSmallestTypeRange returns the smallest INTEGER_TYPE which
1103 is sufficient to contain values: low..high. */
1105 tree
1106 m2type_BuildSmallestTypeRange (location_t location, tree low, tree high)
1108 tree bits;
1110 m2assert_AssertLocation (location);
1111 low = fold (low);
1112 high = fold (high);
1113 bits = fold (m2expr_calcNbits (location, low, high));
1114 return build_m2_specific_size_type (location, INTEGER_TYPE,
1115 TREE_INT_CST_LOW (bits),
1116 tree_int_cst_sgn (low) < 0);
1119 /* GetTreeType returns TREE_TYPE (t). */
1121 tree
1122 m2type_GetTreeType (tree t)
1124 return TREE_TYPE (t);
1127 /* finish_build_pointer_type finish building a POINTER_TYPE node.
1128 necessary to solve self references in procedure types. */
1130 /* Code taken from tree.cc:build_pointer_type_for_mode. */
1132 static tree
1133 finish_build_pointer_type (tree t, tree to_type, enum machine_mode mode,
1134 bool can_alias_all)
1136 TREE_TYPE (t) = to_type;
1137 SET_TYPE_MODE (t, mode);
1138 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
1139 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
1140 TYPE_POINTER_TO (to_type) = t;
1142 /* Lay out the type. */
1143 /* layout_type (t); */
1144 layout_type (t);
1145 return t;
1148 /* BuildParameterDeclaration creates and returns one parameter
1149 from, name, and, type. It appends this parameter to the internal
1150 param_type_list. */
1152 tree
1153 m2type_BuildProcTypeParameterDeclaration (location_t location, tree type,
1154 bool isreference)
1156 m2assert_AssertLocation (location);
1157 ASSERT_BOOL (isreference);
1158 type = m2tree_skip_type_decl (type);
1159 if (isreference)
1160 type = build_reference_type (type);
1162 param_type_list = tree_cons (NULL_TREE, type, param_type_list);
1163 return type;
1166 /* BuildEndFunctionType build a function type which would return a,
1167 value. The arguments have been created by
1168 BuildParameterDeclaration. */
1170 tree
1171 m2type_BuildEndFunctionType (tree func, tree return_type, bool uses_varargs)
1173 tree last;
1175 if (return_type == NULL_TREE)
1176 return_type = void_type_node;
1177 else
1178 return_type = m2tree_skip_type_decl (return_type);
1180 if (uses_varargs)
1182 if (param_type_list != NULL_TREE)
1184 param_type_list = nreverse (param_type_list);
1185 last = param_type_list;
1186 param_type_list = nreverse (param_type_list);
1187 gcc_assert (last != void_list_node);
1190 else if (param_type_list == NULL_TREE)
1191 param_type_list = void_list_node;
1192 else
1194 param_type_list = nreverse (param_type_list);
1195 last = param_type_list;
1196 param_type_list = nreverse (param_type_list);
1197 TREE_CHAIN (last) = void_list_node;
1199 param_type_list = build_function_type (return_type, param_type_list);
1201 func = finish_build_pointer_type (func, param_type_list, ptr_mode, false);
1202 TYPE_SIZE (func) = 0;
1203 layout_type (func);
1204 return func;
1207 /* BuildStartFunctionType creates a pointer type, necessary to
1208 create a function type. */
1210 tree
1211 m2type_BuildStartFunctionType (location_t location ATTRIBUTE_UNUSED,
1212 char *name ATTRIBUTE_UNUSED)
1214 tree n = make_node (POINTER_TYPE);
1216 m2assert_AssertLocation (location);
1217 return n;
1220 /* InitFunctionTypeParameters resets the current function type
1221 parameter list. */
1223 void
1224 m2type_InitFunctionTypeParameters (void)
1226 param_type_list = NULL_TREE;
1229 /* gm2_finish_decl finishes VAR, TYPE and FUNCTION declarations. */
1231 static void
1232 gm2_finish_decl (location_t location, tree decl)
1234 tree type = TREE_TYPE (decl);
1235 int was_incomplete = (DECL_SIZE (decl) == 0);
1237 m2assert_AssertLocation (location);
1238 if (VAR_P (decl))
1240 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
1241 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
1242 layout_decl (decl, 0);
1244 if (DECL_SIZE (decl) == 0
1245 /* Don't give an error if we already gave one earlier. */
1246 && TREE_TYPE (decl) != error_mark_node)
1248 error_at (location, "storage size of %q+D isn%'t known", decl);
1249 TREE_TYPE (decl) = error_mark_node;
1252 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
1253 && DECL_SIZE (decl) != 0)
1255 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
1256 m2expr_ConstantExpressionWarning (DECL_SIZE (decl));
1257 else
1258 error_at (location, "storage size of %q+D isn%'t constant", decl);
1261 if (TREE_USED (type))
1262 TREE_USED (decl) = 1;
1265 /* Output the assembler code and/or RTL code for variables and
1266 functions, unless the type is an undefined structure or union. If
1267 not, it will get done when the type is completed. */
1269 if (VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
1271 if (DECL_FILE_SCOPE_P (decl))
1273 if (DECL_INITIAL (decl) == NULL_TREE
1274 || DECL_INITIAL (decl) == error_mark_node)
1276 /* Don't output anything when a tentative file-scope definition is
1277 seen. But at end of compilation, do output code for them. */
1278 DECL_DEFER_OUTPUT (decl) = 1;
1279 rest_of_decl_compilation (decl, true, 0);
1282 if (!DECL_FILE_SCOPE_P (decl))
1285 /* Recompute the RTL of a local array now if it used to be an
1286 incomplete type. */
1287 if (was_incomplete && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
1289 /* If we used it already as memory, it must stay in memory. */
1290 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
1291 /* If it's still incomplete now, no init will save it. */
1292 if (DECL_SIZE (decl) == 0)
1293 DECL_INITIAL (decl) = 0;
1298 if (TREE_CODE (decl) == TYPE_DECL)
1300 if (!DECL_FILE_SCOPE_P (decl)
1301 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
1302 m2block_pushDecl (build_stmt (location, DECL_EXPR, decl));
1304 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
1308 /* BuildVariableArrayAndDeclare creates a variable length array.
1309 high is the maximum legal elements (which is a runtime variable).
1310 This creates and array index, array type and local variable. */
1312 tree
1313 m2type_BuildVariableArrayAndDeclare (location_t location, tree elementtype,
1314 tree high, char *name, tree scope)
1316 tree indextype = build_index_type (variable_size (high));
1317 tree arraytype = build_array_type (elementtype, indextype);
1318 tree id = get_identifier (name);
1319 tree decl;
1321 m2assert_AssertLocation (location);
1322 decl = build_decl (location, VAR_DECL, id, arraytype);
1324 DECL_EXTERNAL (decl) = false;
1325 TREE_PUBLIC (decl) = true;
1326 DECL_CONTEXT (decl) = scope;
1327 TREE_USED (arraytype) = true;
1328 TREE_USED (decl) = true;
1330 m2block_pushDecl (decl);
1332 gm2_finish_decl (location, indextype);
1333 gm2_finish_decl (location, arraytype);
1334 add_stmt (location, build_stmt (location, DECL_EXPR, decl));
1335 return decl;
1338 static tree
1339 build_m2_iso_word_node (location_t location, int loc)
1341 tree c;
1343 m2assert_AssertLocation (location);
1344 /* Define `WORD' as specified in ISO m2
1346 WORD = ARRAY [0..SizeOfWord / SizeOfLoc] OF LOC ; */
1348 if (m2decl_GetBitsPerInt () == BITS_PER_UNIT)
1349 c = m2type_GetISOLocType ();
1350 else
1351 c = gm2_build_array_type (
1352 m2type_GetISOLocType (),
1353 m2type_BuildArrayIndexType (
1354 m2expr_GetIntegerZero (location),
1355 (m2expr_BuildSub (location,
1356 m2decl_BuildIntegerConstant (
1357 m2decl_GetBitsPerInt () / BITS_PER_UNIT),
1358 m2expr_GetIntegerOne (location), false))),
1359 loc);
1360 return c;
1363 static tree
1364 build_m2_iso_byte_node (location_t location, int loc)
1366 tree c;
1368 /* Define `BYTE' as specified in ISO m2
1370 BYTE = ARRAY [0..SizeOfByte / SizeOfLoc] OF LOC ; */
1372 if (BITS_PER_UNIT == 8)
1373 c = m2type_GetISOLocType ();
1374 else
1375 c = gm2_build_array_type (
1376 m2type_GetISOLocType (),
1377 m2type_BuildArrayIndexType (
1378 m2expr_GetIntegerZero (location),
1379 m2decl_BuildIntegerConstant (BITS_PER_UNIT / 8)),
1380 loc);
1381 return c;
1384 static tree
1385 build_m2_offt_type_node (location_t location)
1387 m2assert_AssertLocation (location);
1388 int offt_size = M2Options_GetFileOffsetBits ();
1390 if (offt_size == 0)
1391 offt_size = TREE_INT_CST_LOW (TYPE_SIZE (ssizetype));
1392 return build_m2_specific_size_type (location, INTEGER_TYPE,
1393 offt_size, true);
1396 /* m2type_InitSystemTypes initialise loc and word derivatives. */
1398 void
1399 m2type_InitSystemTypes (location_t location, int loc)
1401 m2assert_AssertLocation (location);
1403 m2_iso_word_type_node = build_m2_iso_word_node (location, loc);
1404 m2_iso_byte_type_node = build_m2_iso_byte_node (location, loc);
1406 m2_word16_type_node = build_m2_word16_type_node (location, loc);
1407 m2_word32_type_node = build_m2_word32_type_node (location, loc);
1408 m2_word64_type_node = build_m2_word64_type_node (location, loc);
1409 m2_offt_type_node = build_m2_offt_type_node (location);
1412 static tree
1413 build_m2_integer_node (void)
1415 return m2type_GetIntegerType ();
1418 static tree
1419 build_m2_cardinal_node (void)
1421 return m2type_GetCardinalType ();
1424 static tree
1425 build_m2_char_node (void)
1427 tree c;
1429 /* Define `CHAR', to be an unsigned char. */
1431 c = make_unsigned_type (CHAR_TYPE_SIZE);
1432 layout_type (c);
1433 return c;
1436 static tree
1437 build_m2_short_real_node (void)
1439 /* Define `SHORTREAL'. */
1440 ASSERT_CONDITION (TYPE_SIZE (float_type_node));
1441 return float_type_node;
1444 static tree
1445 build_m2_real_node (void)
1447 /* Define `REAL'. */
1448 ASSERT_CONDITION (TYPE_SIZE (double_type_node));
1449 return double_type_node;
1452 static tree
1453 build_m2_long_real_node (void)
1455 tree longreal;
1457 /* Define `LONGREAL'. */
1458 if (M2Options_GetIEEELongDouble ())
1459 longreal = float128_type_node;
1460 else
1461 longreal = long_double_type_node;
1462 ASSERT_CONDITION (TYPE_SIZE (longreal));
1463 return longreal;
1466 static tree
1467 build_m2_ztype_node (void)
1469 tree ztype_node;
1471 /* Define `ZTYPE'. */
1473 if (targetm.scalar_mode_supported_p (TImode))
1474 ztype_node = gm2_type_for_size (128, 0);
1475 else
1476 ztype_node = gm2_type_for_size (64, 0);
1477 layout_type (ztype_node);
1478 return ztype_node;
1481 static tree
1482 build_m2_long_int_node (void)
1484 tree c;
1486 /* Define `LONGINT'. */
1488 c = make_signed_type (LONG_LONG_TYPE_SIZE);
1489 layout_type (c);
1490 return c;
1493 static tree
1494 build_m2_long_card_node (void)
1496 tree c;
1498 /* Define `LONGCARD'. */
1500 c = make_unsigned_type (LONG_LONG_TYPE_SIZE);
1501 layout_type (c);
1502 return c;
1505 static tree
1506 build_m2_short_int_node (void)
1508 tree c;
1510 /* Define `SHORTINT'. */
1512 c = make_signed_type (SHORT_TYPE_SIZE);
1513 layout_type (c);
1514 return c;
1517 static tree
1518 build_m2_short_card_node (void)
1520 tree c;
1522 /* Define `SHORTCARD'. */
1524 c = make_unsigned_type (SHORT_TYPE_SIZE);
1525 layout_type (c);
1526 return c;
1529 static tree
1530 build_m2_iso_loc_node (void)
1532 tree c;
1534 /* Define `LOC' as specified in ISO m2. */
1536 c = make_node (INTEGER_TYPE);
1537 TYPE_PRECISION (c) = BITS_PER_UNIT;
1538 TYPE_SIZE (c) = 0;
1540 fixup_unsigned_type (c);
1541 TYPE_UNSIGNED (c) = 1;
1542 return c;
1545 static tree
1546 build_m2_integer8_type_node (location_t location)
1548 m2assert_AssertLocation (location);
1549 return build_m2_specific_size_type (location, INTEGER_TYPE, 8, true);
1552 static tree
1553 build_m2_integer16_type_node (location_t location)
1555 m2assert_AssertLocation (location);
1556 return build_m2_specific_size_type (location, INTEGER_TYPE, 16, true);
1559 static tree
1560 build_m2_integer32_type_node (location_t location)
1562 m2assert_AssertLocation (location);
1563 return build_m2_specific_size_type (location, INTEGER_TYPE, 32, true);
1566 static tree
1567 build_m2_integer64_type_node (location_t location)
1569 m2assert_AssertLocation (location);
1570 return build_m2_specific_size_type (location, INTEGER_TYPE, 64, true);
1573 static tree
1574 build_m2_cardinal8_type_node (location_t location)
1576 m2assert_AssertLocation (location);
1577 return build_m2_specific_size_type (location, INTEGER_TYPE, 8, false);
1580 static tree
1581 build_m2_cardinal16_type_node (location_t location)
1583 m2assert_AssertLocation (location);
1584 return build_m2_specific_size_type (location, INTEGER_TYPE, 16, false);
1587 static tree
1588 build_m2_cardinal32_type_node (location_t location)
1590 m2assert_AssertLocation (location);
1591 return build_m2_specific_size_type (location, INTEGER_TYPE, 32, false);
1594 static tree
1595 build_m2_cardinal64_type_node (location_t location)
1597 m2assert_AssertLocation (location);
1598 return build_m2_specific_size_type (location, INTEGER_TYPE, 64, false);
1601 static tree
1602 build_m2_bitset8_type_node (location_t location)
1604 m2assert_AssertLocation (location);
1605 if (broken_set_debugging_info)
1606 return build_m2_specific_size_type (location, INTEGER_TYPE, 8, false);
1607 else
1608 return build_m2_specific_size_type (location, SET_TYPE, 8, false);
1611 static tree
1612 build_m2_bitset16_type_node (location_t location)
1614 m2assert_AssertLocation (location);
1615 if (broken_set_debugging_info)
1616 return build_m2_specific_size_type (location, INTEGER_TYPE, 16, false);
1617 else
1618 return build_m2_specific_size_type (location, SET_TYPE, 16, false);
1621 static tree
1622 build_m2_bitset32_type_node (location_t location)
1624 m2assert_AssertLocation (location);
1625 if (broken_set_debugging_info)
1626 return build_m2_specific_size_type (location, INTEGER_TYPE, 32, false);
1627 else
1628 return build_m2_specific_size_type (location, SET_TYPE, 32, false);
1631 static tree
1632 build_m2_real32_type_node (location_t location)
1634 m2assert_AssertLocation (location);
1635 return build_m2_specific_size_type (location, REAL_TYPE, 32, true);
1638 static tree
1639 build_m2_real64_type_node (location_t location)
1641 m2assert_AssertLocation (location);
1642 return build_m2_specific_size_type (location, REAL_TYPE, 64, true);
1645 static tree
1646 build_m2_real96_type_node (location_t location)
1648 m2assert_AssertLocation (location);
1649 return build_m2_specific_size_type (location, REAL_TYPE, 96, true);
1652 static tree
1653 build_m2_real128_type_node (location_t location)
1655 m2assert_AssertLocation (location);
1656 return build_m2_specific_size_type (location, REAL_TYPE, 128, true);
1659 static tree
1660 build_m2_complex_type_from (tree scalar_type)
1662 tree new_type;
1664 if (scalar_type == NULL)
1665 return NULL;
1666 if (scalar_type == float_type_node)
1667 return complex_float_type_node;
1668 if (scalar_type == double_type_node)
1669 return complex_double_type_node;
1670 if (scalar_type == long_double_type_node)
1671 return complex_long_double_type_node;
1673 new_type = make_node (COMPLEX_TYPE);
1674 TREE_TYPE (new_type) = scalar_type;
1675 layout_type (new_type);
1676 return new_type;
1679 static tree
1680 build_m2_complex_type_node (void)
1682 return build_m2_complex_type_from (m2_real_type_node);
1685 static tree
1686 build_m2_long_complex_type_node (void)
1688 return build_m2_complex_type_from (m2_long_real_type_node);
1691 static tree
1692 build_m2_short_complex_type_node (void)
1694 return build_m2_complex_type_from (m2_short_real_type_node);
1697 static tree
1698 build_m2_complex32_type_node (void)
1700 return build_m2_complex_type_from (m2_real32_type_node);
1703 static tree
1704 build_m2_complex64_type_node (void)
1706 return build_m2_complex_type_from (m2_real64_type_node);
1709 static tree
1710 build_m2_complex96_type_node (void)
1712 return build_m2_complex_type_from (m2_real96_type_node);
1715 static tree
1716 build_m2_complex128_type_node (void)
1718 return build_m2_complex_type_from (m2_real128_type_node);
1721 static tree
1722 build_m2_cardinal_address_type_node (location_t location)
1724 tree size = size_in_bytes (ptr_type_node);
1725 int bits = TREE_INT_CST_LOW (size) * BITS_PER_UNIT;
1727 return build_m2_specific_size_type (location, INTEGER_TYPE, bits, false);
1730 static void
1731 build_m2_boolean (location_t location)
1733 tree tname = get_identifier ("BOOLEAN");
1734 tree typedecl = build_decl (location, TYPE_DECL, tname, boolean_type_node);
1735 DECL_ARTIFICIAL (typedecl) = 1;
1736 TYPE_NAME (boolean_type_node) = typedecl;
1740 /* Return true if real types a and b are the same. */
1742 bool
1743 m2type_SameRealType (tree a, tree b)
1745 return ((a == b)
1746 || (TYPE_PRECISION (a) == TYPE_PRECISION (b)));
1749 /* InitBaseTypes create the Modula-2 base types. */
1751 void
1752 m2type_InitBaseTypes (location_t location)
1754 m2assert_AssertLocation (location);
1755 m2block_init ();
1757 ptr_type_node = build_pointer_type (void_type_node);
1759 proc_type_node
1760 = build_pointer_type (build_function_type (void_type_node, NULL_TREE));
1762 bitset_type_node = build_bitset_type (location);
1763 m2_char_type_node = build_m2_char_node ();
1764 m2_integer_type_node = build_m2_integer_node ();
1765 m2_cardinal_type_node = build_m2_cardinal_node ();
1766 m2_short_real_type_node = build_m2_short_real_node ();
1767 m2_real_type_node = build_m2_real_node ();
1768 m2_long_real_type_node = build_m2_long_real_node ();
1769 m2_long_int_type_node = build_m2_long_int_node ();
1770 m2_long_card_type_node = build_m2_long_card_node ();
1771 m2_short_int_type_node = build_m2_short_int_node ();
1772 m2_short_card_type_node = build_m2_short_card_node ();
1773 m2_z_type_node = build_m2_ztype_node ();
1774 m2_integer8_type_node = build_m2_integer8_type_node (location);
1775 m2_integer16_type_node = build_m2_integer16_type_node (location);
1776 m2_integer32_type_node = build_m2_integer32_type_node (location);
1777 m2_integer64_type_node = build_m2_integer64_type_node (location);
1778 m2_cardinal8_type_node = build_m2_cardinal8_type_node (location);
1779 m2_cardinal16_type_node = build_m2_cardinal16_type_node (location);
1780 m2_cardinal32_type_node = build_m2_cardinal32_type_node (location);
1781 m2_cardinal64_type_node = build_m2_cardinal64_type_node (location);
1782 m2_bitset8_type_node = build_m2_bitset8_type_node (location);
1783 m2_bitset16_type_node = build_m2_bitset16_type_node (location);
1784 m2_bitset32_type_node = build_m2_bitset32_type_node (location);
1785 m2_real32_type_node = build_m2_real32_type_node (location);
1786 m2_real64_type_node = build_m2_real64_type_node (location);
1787 m2_real96_type_node = build_m2_real96_type_node (location);
1788 m2_real128_type_node = build_m2_real128_type_node (location);
1789 m2_complex_type_node = build_m2_complex_type_node ();
1790 m2_long_complex_type_node = build_m2_long_complex_type_node ();
1791 m2_short_complex_type_node = build_m2_short_complex_type_node ();
1792 m2_c_type_node = m2_long_complex_type_node;
1793 m2_complex32_type_node = build_m2_complex32_type_node ();
1794 m2_complex64_type_node = build_m2_complex64_type_node ();
1795 m2_complex96_type_node = build_m2_complex96_type_node ();
1796 m2_complex128_type_node = build_m2_complex128_type_node ();
1797 m2_iso_loc_type_node = build_m2_iso_loc_node ();
1799 m2_cardinal_address_type_node
1800 = build_m2_cardinal_address_type_node (location);
1802 m2_packed_boolean_type_node = build_nonstandard_integer_type (1, true);
1803 build_m2_boolean (location);
1805 if (M2Options_GetPPOnly ())
1806 return;
1808 m2builtins_init (location);
1809 m2except_InitExceptions (location);
1810 m2expr_init (location);
1813 /* BuildStartType given a, type, with a, name, return a GCC
1814 declaration of this type. TYPE name = foo ;
1816 the type, foo, maybe a partially created type (which has
1817 yet to be 'gm2_finish_decl'ed). */
1819 tree
1820 m2type_BuildStartType (location_t location, char *name, tree type)
1822 tree id = get_identifier (name);
1823 tree decl, tem;
1825 m2assert_AssertLocation (location);
1826 ASSERT (m2tree_is_type (type), type);
1827 type = m2tree_skip_type_decl (type);
1828 decl = build_decl (location, TYPE_DECL, id, type);
1830 tem = m2block_pushDecl (decl);
1831 ASSERT (tem == decl, decl);
1832 ASSERT (m2tree_is_type (decl), decl);
1834 return tem;
1837 /* BuildEndType finish declaring, type, and return, type. */
1839 tree
1840 m2type_BuildEndType (location_t location, tree type)
1842 m2assert_AssertLocation (location);
1843 layout_type (TREE_TYPE (type));
1844 gm2_finish_decl (location, type);
1845 return type;
1848 /* DeclareKnownType given a, type, with a, name, return a GCC
1849 declaration of this type. TYPE name = foo ; */
1851 tree
1852 m2type_DeclareKnownType (location_t location, char *name, tree type)
1854 m2assert_AssertLocation (location);
1855 return m2type_BuildEndType (location,
1856 m2type_BuildStartType (location, name, type));
1859 /* GetDefaultType given a, type, with a, name, return a GCC
1860 declaration of this type. Checks to see whether the type name has
1861 already been declared as a default type and if so it returns this
1862 declaration. Otherwise it declares the type. In Modula-2 this is
1863 equivalent to:
1865 TYPE name = type ;
1867 We need this function during gm2 initialization as it allows
1868 gm2 to access default types before creating Modula-2 types. */
1870 tree
1871 m2type_GetDefaultType (location_t location, char *name, tree type)
1873 tree id = maybe_get_identifier (name);
1875 m2assert_AssertLocation (location);
1876 if (id == NULL)
1878 tree prev = type;
1879 tree t;
1881 while (prev != NULL)
1883 if (TYPE_NAME (prev) == NULL)
1884 TYPE_NAME (prev) = get_identifier (name);
1885 prev = TREE_TYPE (prev);
1887 t = m2type_DeclareKnownType (location, name, type);
1888 return t;
1890 else
1891 return id;
1894 tree
1895 do_min_real (tree type)
1897 REAL_VALUE_TYPE r;
1898 char buf[128];
1899 enum machine_mode mode = TYPE_MODE (type);
1901 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
1902 real_from_string (&r, buf);
1903 return build1 (NEGATE_EXPR, type, build_real (type, r));
1906 /* GetMinFrom given a, type, return a constant representing the
1907 minimum legal value. */
1909 tree
1910 m2type_GetMinFrom (location_t location, tree type)
1912 m2assert_AssertLocation (location);
1914 if (type == m2_real_type_node || type == m2type_GetRealType ())
1915 return do_min_real (type);
1916 if (type == m2_long_real_type_node || type == m2type_GetLongRealType ())
1917 return do_min_real (type);
1918 if (type == m2_short_real_type_node || type == m2type_GetShortRealType ())
1919 return do_min_real (type);
1920 if (type == ptr_type_node)
1921 return m2expr_GetPointerZero (location);
1923 return TYPE_MIN_VALUE (m2tree_skip_type_decl (type));
1926 tree
1927 do_max_real (tree type)
1929 REAL_VALUE_TYPE r;
1930 char buf[128];
1931 enum machine_mode mode = TYPE_MODE (type);
1933 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
1934 real_from_string (&r, buf);
1935 return build_real (type, r);
1938 /* GetMaxFrom given a, type, return a constant representing the
1939 maximum legal value. */
1941 tree
1942 m2type_GetMaxFrom (location_t location, tree type)
1944 m2assert_AssertLocation (location);
1946 if (type == m2_real_type_node || type == m2type_GetRealType ())
1947 return do_max_real (type);
1948 if (type == m2_long_real_type_node || type == m2type_GetLongRealType ())
1949 return do_max_real (type);
1950 if (type == m2_short_real_type_node || type == m2type_GetShortRealType ())
1951 return do_max_real (type);
1952 if (type == ptr_type_node)
1953 return fold (m2expr_BuildSub (location, m2expr_GetPointerZero (location),
1954 m2expr_GetPointerOne (location), false));
1956 return TYPE_MAX_VALUE (m2tree_skip_type_decl (type));
1959 /* BuildTypeDeclaration adds the, type, to the current statement
1960 list. */
1962 void
1963 m2type_BuildTypeDeclaration (location_t location, tree type)
1965 enum tree_code code = TREE_CODE (type);
1967 m2assert_AssertLocation (location);
1968 if (code == TYPE_DECL || code == RECORD_TYPE || code == POINTER_TYPE)
1970 m2block_pushDecl (build_decl (location, TYPE_DECL, NULL, type));
1972 else if (code == VAR_DECL)
1974 m2type_BuildTypeDeclaration (location, TREE_TYPE (type));
1975 m2block_pushDecl (
1976 build_stmt (location, DECL_EXPR,
1977 type)); /* Is this safe? --fixme--. */
1981 /* Begin compiling the definition of an enumeration type. NAME is
1982 its name (or null if anonymous). Returns the type object, as yet
1983 incomplete. Also records info about it so that build_enumerator may
1984 be used to declare the individual values as they are read. */
1986 static tree
1987 gm2_start_enum (location_t location, tree name, int ispacked)
1989 tree enumtype = make_node (ENUMERAL_TYPE);
1991 m2assert_AssertLocation (location);
1992 if (TYPE_VALUES (enumtype) != 0)
1994 /* This enum is a named one that has been declared already. */
1995 error_at (location, "redeclaration of enum %qs",
1996 IDENTIFIER_POINTER (name));
1998 /* Completely replace its old definition. The old enumerators remain
1999 defined, however. */
2000 TYPE_VALUES (enumtype) = 0;
2003 TYPE_PACKED (enumtype) = ispacked;
2004 TREE_TYPE (enumtype) = m2type_GetIntegerType ();
2006 /* This is required as rest_of_type_compilation will use this field
2007 when called from gm2_finish_enum.
2009 Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
2010 tagged type we just added to the current scope. This fake NULL-named
2011 TYPE_DECL node helps dwarfout.cc to know when it needs to output a
2012 representation of a tagged type, and it also gives us a convenient
2013 place to record the "scope start" address for the tagged type. */
2015 TYPE_STUB_DECL (enumtype) = m2block_pushDecl (
2016 build_decl (location, TYPE_DECL, NULL_TREE, enumtype));
2018 return enumtype;
2021 /* After processing and defining all the values of an enumeration
2022 type, install their decls in the enumeration type and finish it off.
2023 ENUMTYPE is the type object, VALUES a list of decl-value pairs, and
2024 ATTRIBUTES are the specified attributes. Returns ENUMTYPE. */
2026 static tree
2027 gm2_finish_enum (location_t location, tree enumtype, tree values)
2029 tree pair, tem;
2030 tree minnode = 0, maxnode = 0;
2031 int precision;
2032 signop sign;
2034 /* Calculate the maximum value of any enumerator in this type. */
2036 if (values == error_mark_node)
2037 minnode = maxnode = integer_zero_node;
2038 else
2040 minnode = maxnode = TREE_VALUE (values);
2041 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
2043 tree value = TREE_VALUE (pair);
2044 if (tree_int_cst_lt (maxnode, value))
2045 maxnode = value;
2046 if (tree_int_cst_lt (value, minnode))
2047 minnode = value;
2051 /* Construct the final type of this enumeration. It is the same as
2052 one of the integral types the narrowest one that fits, except that
2053 normally we only go as narrow as int and signed iff any of the
2054 values are negative. */
2055 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
2056 precision = MAX (tree_int_cst_min_precision (minnode, sign),
2057 tree_int_cst_min_precision (maxnode, sign));
2059 if (precision > TYPE_PRECISION (integer_type_node))
2061 warning (0, "enumeration values exceed range of integer");
2062 tem = long_long_integer_type_node;
2064 else if (TYPE_PACKED (enumtype))
2065 tem = m2type_BuildSmallestTypeRange (location, minnode, maxnode);
2066 else
2067 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
2069 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
2070 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
2071 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
2072 TYPE_SIZE (enumtype) = 0;
2074 /* If the precision of the type was specific with an attribute and it
2075 was too small, give an error. Otherwise, use it. */
2076 if (TYPE_PRECISION (enumtype))
2078 if (precision > TYPE_PRECISION (enumtype))
2079 error ("specified mode too small for enumerated values");
2081 else
2082 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
2084 layout_type (enumtype);
2086 if (values != error_mark_node)
2089 /* Change the type of the enumerators to be the enum type. We need
2090 to do this irrespective of the size of the enum, for proper type
2091 checking. Replace the DECL_INITIALs of the enumerators, and the
2092 value slots of the list, with copies that have the enum type; they
2093 cannot be modified in place because they may be shared (e.g.
2094 integer_zero_node) Finally, change the purpose slots to point to the
2095 names of the decls. */
2096 for (pair = values; pair; pair = TREE_CHAIN (pair))
2098 tree enu = TREE_PURPOSE (pair);
2099 tree ini = DECL_INITIAL (enu);
2101 TREE_TYPE (enu) = enumtype;
2103 if (TREE_TYPE (ini) != integer_type_node)
2104 ini = convert (enumtype, ini);
2106 DECL_INITIAL (enu) = ini;
2107 TREE_PURPOSE (pair) = DECL_NAME (enu);
2108 TREE_VALUE (pair) = ini;
2111 TYPE_VALUES (enumtype) = values;
2114 /* Fix up all variant types of this enum type. */
2115 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
2117 if (tem == enumtype)
2118 continue;
2119 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
2120 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
2121 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
2122 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
2123 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
2124 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
2125 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
2126 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
2127 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
2128 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
2129 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
2132 /* Finish debugging output for this type. */
2133 rest_of_type_compilation (enumtype, m2block_toplevel ());
2134 return enumtype;
2137 /* BuildStartEnumeration create an enumerated type in gcc. */
2139 tree
2140 m2type_BuildStartEnumeration (location_t location, char *name, bool ispacked)
2142 tree id;
2144 m2assert_AssertLocation (location);
2145 if ((name == NULL) || (strcmp (name, "") == 0))
2146 id = NULL_TREE;
2147 else
2148 id = get_identifier (name);
2150 return gm2_start_enum (location, id, ispacked);
2153 /* BuildEndEnumeration finish building the enumeration, it uses the
2154 enum list, enumvalues, and returns a enumeration type tree. */
2156 tree
2157 m2type_BuildEndEnumeration (location_t location, tree enumtype,
2158 tree enumvalues)
2160 tree finished ATTRIBUTE_UNUSED
2161 = gm2_finish_enum (location, enumtype, enumvalues);
2162 return enumtype;
2165 /* Build and install a CONST_DECL for one value of the current
2166 enumeration type (one that was begun with start_enum). Return a
2167 tree-list containing the CONST_DECL and its value. Assignment of
2168 sequential values by default is handled here. */
2170 static tree
2171 gm2_build_enumerator (location_t location, tree name, tree value)
2173 tree decl, type;
2175 m2assert_AssertLocation (location);
2176 /* Remove no-op casts from the value. */
2177 if (value)
2178 STRIP_TYPE_NOPS (value);
2180 /* Now create a declaration for the enum value name. */
2182 type = TREE_TYPE (value);
2184 decl = build_decl (location, CONST_DECL, name, type);
2185 DECL_INITIAL (decl) = convert (type, value);
2186 m2block_pushDecl (decl);
2188 return tree_cons (decl, value, NULL_TREE);
2191 /* BuildEnumerator build an enumerator and add it to the,
2192 enumvalues, list. It returns a copy of the value. */
2194 tree
2195 m2type_BuildEnumerator (location_t location, char *name, tree value,
2196 tree *enumvalues)
2198 tree id = get_identifier (name);
2199 tree copy_of_value = copy_node (value);
2200 tree gccenum = gm2_build_enumerator (location, id, copy_of_value);
2202 m2assert_AssertLocation (location);
2203 /* Choose copy_of_value for enum value. */
2204 *enumvalues = chainon (gccenum, *enumvalues);
2205 return copy_of_value;
2208 /* BuildPointerType returns a type which is a pointer to, totype. */
2210 tree
2211 m2type_BuildPointerType (tree totype)
2213 return build_pointer_type (m2tree_skip_type_decl (totype));
2216 /* BuildConstPointerType returns a type which is a const pointer
2217 to, totype. */
2219 tree
2220 m2type_BuildConstPointerType (tree totype)
2222 tree t = build_pointer_type (m2tree_skip_type_decl (totype));
2223 TYPE_READONLY (t) = true;
2224 return t;
2227 /* BuildSetType creates a SET OF [lowval..highval]. */
2229 tree
2230 m2type_BuildSetType (location_t location, char *name, tree type, tree lowval,
2231 tree highval, bool ispacked)
2233 tree range = build_range_type (m2tree_skip_type_decl (type),
2234 m2expr_FoldAndStrip (lowval),
2235 m2expr_FoldAndStrip (highval));
2237 TYPE_PACKED (range) = ispacked;
2238 m2assert_AssertLocation (location);
2239 return m2type_BuildSetTypeFromSubrange (location, name, range,
2240 m2expr_FoldAndStrip (lowval),
2241 m2expr_FoldAndStrip (highval),
2242 ispacked);
2245 /* push_constructor returns a new compound constructor frame. */
2247 static struct struct_constructor *
2248 push_constructor (void)
2250 struct struct_constructor *p = ggc_alloc<struct_constructor> ();
2252 p->level = top_constructor;
2253 top_constructor = p;
2254 return p;
2257 /* pop_constructor throws away the top constructor frame on the
2258 stack. */
2260 static void
2261 pop_constructor (struct struct_constructor *p)
2263 ASSERT_CONDITION (p
2264 == top_constructor); /* p should be the top_constructor. */
2265 top_constructor = top_constructor->level;
2268 /* BuildStartSetConstructor starts to create a set constant.
2269 Remember that type is really a record type. */
2271 void *
2272 m2type_BuildStartSetConstructor (tree type)
2274 struct struct_constructor *p = push_constructor ();
2276 type = m2tree_skip_type_decl (type);
2277 layout_type (type);
2278 p->constructor_type = type;
2279 p->constructor_fields = TYPE_FIELDS (type);
2280 p->constructor_element_list = NULL_TREE;
2281 vec_alloc (p->constructor_elements, 1);
2282 return (void *)p;
2285 /* BuildSetConstructorElement adds, value, to the
2286 constructor_element_list. */
2288 void
2289 m2type_BuildSetConstructorElement (void *p, tree value)
2291 struct struct_constructor *c = (struct struct_constructor *)p;
2293 if (value == NULL_TREE)
2295 internal_error ("set type cannot be initialized with a %qs",
2296 "NULL_TREE");
2297 return;
2300 if (c->constructor_fields == NULL)
2302 internal_error ("set type does not take another integer value");
2303 return;
2306 c->constructor_element_list
2307 = tree_cons (c->constructor_fields, value, c->constructor_element_list);
2308 c->constructor_fields = TREE_CHAIN (c->constructor_fields);
2311 /* BuildEndSetConstructor finishes building a set constant. */
2313 tree
2314 m2type_BuildEndSetConstructor (void *p)
2316 tree constructor;
2317 tree link;
2318 struct struct_constructor *c = (struct struct_constructor *)p;
2320 for (link = c->constructor_element_list; link; link = TREE_CHAIN (link))
2322 tree field = TREE_PURPOSE (link);
2323 DECL_SIZE (field) = bitsize_int (SET_WORD_SIZE);
2324 DECL_BIT_FIELD (field) = 1;
2327 constructor = build_constructor_from_list (
2328 c->constructor_type, nreverse (c->constructor_element_list));
2329 TREE_CONSTANT (constructor) = 1;
2330 TREE_STATIC (constructor) = 1;
2332 pop_constructor (c);
2334 return constructor;
2337 /* BuildStartRecordConstructor initializes a record compound
2338 constructor frame. */
2340 void *
2341 m2type_BuildStartRecordConstructor (tree type)
2343 struct struct_constructor *p = push_constructor ();
2345 type = m2tree_skip_type_decl (type);
2346 layout_type (type);
2347 p->constructor_type = type;
2348 p->constructor_fields = TYPE_FIELDS (type);
2349 p->constructor_element_list = NULL_TREE;
2350 vec_alloc (p->constructor_elements, 1);
2351 return (void *)p;
2354 /* BuildEndRecordConstructor returns a tree containing the record
2355 compound literal. */
2357 tree
2358 m2type_BuildEndRecordConstructor (void *p)
2360 struct struct_constructor *c = (struct struct_constructor *)p;
2361 tree constructor = build_constructor_from_list (
2362 c->constructor_type, nreverse (c->constructor_element_list));
2363 TREE_CONSTANT (constructor) = 1;
2364 TREE_STATIC (constructor) = 1;
2366 pop_constructor (c);
2368 return constructor;
2371 /* BuildRecordConstructorElement adds, value, to the
2372 constructor_element_list. */
2374 void
2375 m2type_BuildRecordConstructorElement (void *p, tree value)
2377 m2type_BuildSetConstructorElement (p, value);
2380 /* BuildStartArrayConstructor initializes an array compound
2381 constructor frame. */
2383 void *
2384 m2type_BuildStartArrayConstructor (tree type)
2386 struct struct_constructor *p = push_constructor ();
2388 type = m2tree_skip_type_decl (type);
2389 layout_type (type);
2390 p->constructor_type = type;
2391 p->constructor_fields = TREE_TYPE (type);
2392 p->constructor_element_list = NULL_TREE;
2393 vec_alloc (p->constructor_elements, 1);
2394 return (void *)p;
2397 /* BuildEndArrayConstructor returns a tree containing the array
2398 compound literal. */
2400 tree
2401 m2type_BuildEndArrayConstructor (void *p)
2403 struct struct_constructor *c = (struct struct_constructor *)p;
2404 tree constructor;
2406 constructor
2407 = build_constructor (c->constructor_type, c->constructor_elements);
2408 TREE_CONSTANT (constructor) = true;
2409 TREE_STATIC (constructor) = true;
2411 pop_constructor (c);
2413 return constructor;
2416 /* BuildArrayConstructorElement adds, value, to the
2417 constructor_element_list. */
2419 void
2420 m2type_BuildArrayConstructorElement (void *p, tree value, tree indice)
2422 struct struct_constructor *c = (struct struct_constructor *)p;
2423 constructor_elt celt;
2425 if (value == NULL_TREE)
2427 internal_error ("array cannot be initialized with a %qs", "NULL_TREE");
2428 return;
2431 if (c->constructor_fields == NULL_TREE)
2433 internal_error ("array type must be initialized");
2434 return;
2437 if (c->constructor_fields != TREE_TYPE (value))
2439 internal_error (
2440 "array element value must be the same type as its declaration");
2441 return;
2444 celt.index = indice;
2445 celt.value = value;
2446 vec_safe_push (c->constructor_elements, celt);
2449 /* BuildArrayStringConstructor creates an array constructor for,
2450 arrayType, consisting of the character elements defined by, str,
2451 of, length, characters. */
2453 tree
2454 m2type_BuildArrayStringConstructor (location_t location, tree arrayType,
2455 tree str, tree length)
2457 tree n;
2458 tree val;
2459 int i = 0;
2460 const char *p = TREE_STRING_POINTER (str);
2461 tree type = m2tree_skip_type_decl (TREE_TYPE (arrayType));
2462 struct struct_constructor *c
2463 = (struct struct_constructor *)m2type_BuildStartArrayConstructor (
2464 arrayType);
2465 char nul[1];
2466 int len = strlen (p);
2468 nul[0] = (char)0;
2470 m2assert_AssertLocation (location);
2471 n = m2expr_GetIntegerZero (location);
2472 while (m2expr_CompareTrees (n, length) < 0)
2474 if (i < len)
2475 val = m2convert_BuildConvert (
2476 location, type, m2type_BuildCharConstant (location, &p[i]), false);
2477 else
2478 val = m2type_BuildCharConstant (location, &nul[0]);
2479 m2type_BuildArrayConstructorElement (c, val, n);
2480 i += 1;
2481 n = m2expr_BuildAdd (location, n, m2expr_GetIntegerOne (location),
2482 false);
2484 return m2type_BuildEndArrayConstructor (c);
2487 /* BuildSubrangeType creates a subrange of, type, with, lowval,
2488 highval. */
2490 tree
2491 m2type_BuildSubrangeType (location_t location, char *name, tree type,
2492 tree lowval, tree highval)
2494 tree range_type;
2496 m2assert_AssertLocation (location);
2497 type = m2tree_skip_type_decl (type);
2499 lowval = m2expr_FoldAndStrip (lowval);
2500 highval = m2expr_FoldAndStrip (highval);
2502 if (m2expr_TreeOverflow (lowval))
2503 error ("low bound for the subrange has overflowed");
2504 if (m2expr_TreeOverflow (highval))
2505 error ("high bound for the subrange has overflowed");
2507 /* First build a type with the base range. */
2508 range_type = build_range_type (type, lowval, highval);
2510 TYPE_UNSIGNED (range_type) = TYPE_UNSIGNED (type);
2511 #if 0
2512 /* Then set the actual range. */
2513 SET_TYPE_RM_MIN_VALUE (range_type, lowval);
2514 SET_TYPE_RM_MAX_VALUE (range_type, highval);
2515 #endif
2517 if ((name != NULL) && (strcmp (name, "") != 0))
2519 /* Declared as TYPE foo = [x..y]; */
2520 range_type = m2type_DeclareKnownType (location, name, range_type);
2521 layout_type (m2tree_skip_type_decl (range_type));
2524 return range_type;
2527 /* BuildCharConstantChar creates a character constant given a character, ch. */
2529 tree
2530 m2type_BuildCharConstantChar (location_t location, char ch)
2532 tree id = build_int_cst (char_type_node, (int) ch);
2533 id = m2convert_BuildConvert (location, m2type_GetM2CharType (), id, false);
2534 return m2block_RememberConstant (id);
2537 /* BuildCharConstant creates a character constant given a, string. */
2539 tree
2540 m2type_BuildCharConstant (location_t location, const char *string)
2542 return m2type_BuildCharConstantChar (location, string[0]);
2545 /* RealToTree convert a real number into a Tree. */
2547 tree
2548 m2type_RealToTree (char *name)
2550 return build_real (
2551 m2type_GetLongRealType (),
2552 REAL_VALUE_ATOF (name, TYPE_MODE (m2type_GetLongRealType ())));
2555 /* gm2_start_struct start to create a struct. */
2557 static tree
2558 gm2_start_struct (location_t location, enum tree_code code, char *name)
2560 tree s = make_node (code);
2561 tree id;
2563 m2assert_AssertLocation (location);
2564 if ((name == NULL) || (strcmp (name, "") == 0))
2565 id = NULL_TREE;
2566 else
2567 id = get_identifier (name);
2569 /* This maybe set true later if necessary. */
2570 TYPE_PACKED (s) = false;
2572 m2block_pushDecl (build_decl (location, TYPE_DECL, id, s));
2573 return s;
2576 /* BuildStartRecord return a RECORD tree. */
2578 tree
2579 m2type_BuildStartRecord (location_t location, char *name)
2581 m2assert_AssertLocation (location);
2582 return gm2_start_struct (location, RECORD_TYPE, name);
2585 /* BuildStartUnion return a union tree. */
2587 tree
2588 m2type_BuildStartUnion (location_t location, char *name)
2590 m2assert_AssertLocation (location);
2591 return gm2_start_struct (location, UNION_TYPE, name);
2594 /* m2type_BuildStartVarient builds a varient record. It creates a
2595 record field which has a, name, and whose type is a union. */
2597 tree
2598 m2type_BuildStartVarient (location_t location, char *name)
2600 tree varient = m2type_BuildStartUnion (location, name);
2601 tree field = m2type_BuildStartFieldRecord (location, name, varient);
2602 m2assert_AssertLocation (location);
2603 return field;
2606 /* m2type_BuildEndVarient finish the varientField by calling
2607 decl_finish and also finish the type of varientField (which is a
2608 union). */
2610 tree
2611 m2type_BuildEndVarient (location_t location, tree varientField,
2612 tree varientList, bool isPacked)
2614 tree varient = TREE_TYPE (varientField);
2615 m2assert_AssertLocation (location);
2616 varient = m2type_BuildEndRecord (location, varient, varientList, isPacked);
2617 gm2_finish_decl (location, varientField);
2618 return varientField;
2621 /* m2type_BuildStartFieldVarient builds a field varient record. It
2622 creates a record field which has a, name, and whose type is a
2623 record. */
2625 tree
2626 m2type_BuildStartFieldVarient (location_t location, char *name)
2628 tree record = m2type_BuildStartRecord (location, name);
2629 tree field = m2type_BuildStartFieldRecord (location, name, record);
2630 m2assert_AssertLocation (location);
2631 return field;
2634 /* BuildEndRecord a heavily pruned finish_struct from c-decl.cc. It
2635 sets the context for each field to, t, propagates isPacked
2636 throughout the fields in the structure. */
2638 tree
2639 m2type_BuildEndRecord (location_t location, tree record, tree fieldlist,
2640 bool isPacked)
2642 tree x, d;
2644 m2assert_AssertLocation (location);
2646 /* If this type was previously laid out as a forward reference, make
2647 sure we lay it out again. */
2649 TYPE_SIZE (record) = 0;
2651 /* Install struct as DECL_CONTEXT of each field decl. Also process
2652 specified field sizes, found in the DECL_INITIAL, storing 0 there
2653 after the type has been changed to precision equal to its width,
2654 rather than the precision of the specified standard type. (Correct
2655 layout requires the original type to have been preserved until now). */
2657 for (x = fieldlist; x; x = TREE_CHAIN (x))
2659 DECL_CONTEXT (x) = record;
2661 if (TYPE_PACKED (record) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
2662 DECL_PACKED (x) = 1;
2664 if (isPacked)
2666 DECL_PACKED (x) = 1;
2667 DECL_BIT_FIELD (x) = 1;
2671 /* Now we have the nearly final fieldlist. Record it, then lay out
2672 the structure or union (including the fields). */
2674 TYPE_FIELDS (record) = fieldlist;
2675 layout_type (record);
2677 /* Now we have the truly final field list. Store it in this type and
2678 in the variants. */
2680 for (x = TYPE_MAIN_VARIANT (record); x; x = TYPE_NEXT_VARIANT (x))
2682 TYPE_FIELDS (x) = TYPE_FIELDS (record);
2683 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (record);
2684 SET_TYPE_ALIGN (x, TYPE_ALIGN (record));
2685 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (record);
2688 d = build_decl (location, TYPE_DECL, NULL, record);
2689 TYPE_STUB_DECL (record) = d;
2691 /* Finish debugging output for this type. This must be done after we have
2692 called build_decl. */
2693 rest_of_type_compilation (record, m2block_toplevel ());
2695 return record;
2698 /* m2type_BuildEndFieldVarient finish the varientField by calling
2699 decl_finish and also finish the type of varientField (which is a
2700 record). */
2702 tree
2703 m2type_BuildEndFieldVarient (location_t location, tree varientField,
2704 tree varientList, bool isPacked)
2706 tree record = TREE_TYPE (varientField);
2708 m2assert_AssertLocation (location);
2709 record = m2type_BuildEndRecord (location, record, varientList, isPacked);
2710 gm2_finish_decl (location, varientField);
2711 return varientField;
2714 /* m2type_BuildStartFieldRecord starts building a field record. It
2715 returns the field which must be completed by calling
2716 gm2_finish_decl. */
2718 tree
2719 m2type_BuildStartFieldRecord (location_t location, char *name, tree type)
2721 tree field, declarator;
2723 m2assert_AssertLocation (location);
2724 if ((name == NULL) || (strcmp (name, "") == 0))
2725 declarator = NULL_TREE;
2726 else
2727 declarator = get_identifier (name);
2729 field = build_decl (location, FIELD_DECL, declarator,
2730 m2tree_skip_type_decl (type));
2731 return field;
2734 /* Build a record field with name (name maybe NULL), returning the
2735 new field declaration, FIELD_DECL.
2737 This is done during the parsing of the struct declaration. The
2738 FIELD_DECL nodes are chained together and the lot of them are
2739 ultimately passed to `build_struct' to make the RECORD_TYPE node. */
2741 tree
2742 m2type_BuildFieldRecord (location_t location, char *name, tree type)
2744 tree field = m2type_BuildStartFieldRecord (location, name, type);
2746 m2assert_AssertLocation (location);
2747 gm2_finish_decl (location, field);
2748 return field;
2751 /* ChainOn interface so that Modula-2 can also create chains of
2752 declarations. */
2754 tree
2755 m2type_ChainOn (tree t1, tree t2)
2757 return chainon (t1, t2);
2760 /* ChainOnParamValue adds a list node {{name, str}, value} into the
2761 tree list. */
2763 tree
2764 m2type_ChainOnParamValue (tree list, tree name, tree str, tree value)
2766 return chainon (list, build_tree_list (build_tree_list (name, str), value));
2769 /* AddStringToTreeList adds, string, to list. */
2771 tree
2772 m2type_AddStringToTreeList (tree list, tree string)
2774 return tree_cons (NULL_TREE, string, list);
2777 /* SetAlignment sets the alignment of a, node, to, align. It
2778 duplicates the, node, and sets the alignment to prevent alignment
2779 effecting behaviour elsewhere. */
2781 tree
2782 m2type_SetAlignment (tree node, tree align)
2784 tree type = NULL_TREE;
2785 tree decl = NULL_TREE;
2786 bool is_type = false;
2787 int i;
2789 if (DECL_P (node))
2791 decl = node;
2792 is_type = (TREE_CODE (node) == TYPE_DECL);
2793 type = TREE_TYPE (decl);
2795 else if (TYPE_P (node))
2797 is_type = true;
2798 type = node;
2801 if (TREE_CODE (align) != INTEGER_CST)
2802 error ("requested alignment is not a constant");
2803 else if ((i = tree_log2 (align)) == -1)
2804 error ("requested alignment is not a power of 2");
2805 else if (i > HOST_BITS_PER_INT - 2)
2806 error ("requested alignment is too large");
2807 else if (is_type)
2809 /* If we have a TYPE_DECL, then copy the type, so that we don't
2810 accidentally modify a builtin type. See pushdecl. */
2811 if (decl && TREE_TYPE (decl) != error_mark_node
2812 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
2814 tree tt = TREE_TYPE (decl);
2815 type = build_variant_type_copy (type);
2816 DECL_ORIGINAL_TYPE (decl) = tt;
2817 TYPE_NAME (type) = decl;
2818 TREE_USED (type) = TREE_USED (decl);
2819 TREE_TYPE (decl) = type;
2822 SET_TYPE_ALIGN (type, (1 << i) * BITS_PER_UNIT);
2823 TYPE_USER_ALIGN (type) = 1;
2825 if (decl)
2827 SET_DECL_ALIGN (decl, (1 << i) * BITS_PER_UNIT);
2828 DECL_USER_ALIGN (decl) = 1;
2831 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FIELD_DECL)
2832 error ("alignment may not be specified for %qD", decl);
2833 else
2835 SET_DECL_ALIGN (decl, (1 << i) * BITS_PER_UNIT);
2836 DECL_USER_ALIGN (decl) = 1;
2838 return node;
2841 /* SetDeclPacked sets the packed bit in decl TREE, node. It
2842 returns the node. */
2844 tree
2845 m2type_SetDeclPacked (tree node)
2847 DECL_PACKED (node) = 1;
2848 return node;
2851 /* SetTypePacked sets the packed bit in type TREE, node. It
2852 returns the node. */
2854 tree
2855 m2type_SetTypePacked (tree node)
2857 TYPE_PACKED (node) = 1;
2858 return node;
2861 /* SetRecordFieldOffset returns field after the byteOffset and
2862 bitOffset has been applied to it. */
2864 tree
2865 m2type_SetRecordFieldOffset (tree field, tree byteOffset, tree bitOffset,
2866 tree fieldtype, tree nbits)
2868 DECL_FIELD_OFFSET (field) = byteOffset;
2869 DECL_FIELD_BIT_OFFSET (field) = bitOffset;
2870 TREE_TYPE (field) = m2tree_skip_type_decl (fieldtype);
2871 DECL_SIZE (field) = bitsize_int (TREE_INT_CST_LOW (nbits));
2872 return field;
2875 /* BuildPackedFieldRecord builds a packed field record of, name,
2876 and, fieldtype. */
2878 tree
2879 m2type_BuildPackedFieldRecord (location_t location, char *name, tree fieldtype)
2881 m2assert_AssertLocation (location);
2882 return m2type_BuildFieldRecord (location, name, fieldtype);
2885 /* BuildNumberOfArrayElements returns the number of elements in an
2886 arrayType. */
2888 tree
2889 m2type_BuildNumberOfArrayElements (location_t location, tree arrayType)
2891 tree index = TYPE_DOMAIN (arrayType);
2892 tree high = TYPE_MAX_VALUE (index);
2893 tree low = TYPE_MIN_VALUE (index);
2894 tree elements = m2expr_BuildAdd (
2895 location, m2expr_BuildSub (location, high, low, false),
2896 m2expr_GetIntegerOne (location), false);
2897 m2assert_AssertLocation (location);
2898 return elements;
2901 /* AddStatement maps onto add_stmt. */
2903 void
2904 m2type_AddStatement (location_t location, tree t)
2906 if (t != NULL_TREE)
2907 add_stmt (location, t);
2910 /* MarkFunctionReferenced marks a function as referenced. */
2912 void
2913 m2type_MarkFunctionReferenced (tree f)
2915 if (f != NULL_TREE)
2916 if (TREE_CODE (f) == FUNCTION_DECL)
2917 mark_decl_referenced (f);
2920 /* GarbageCollect force gcc to garbage collect. */
2922 void
2923 m2type_GarbageCollect (void)
2925 ggc_collect ();
2928 /* gm2_type_for_size return an integer type with BITS bits of
2929 precision, that is unsigned if UNSIGNEDP is nonzero, otherwise
2930 signed. */
2932 tree
2933 m2type_gm2_type_for_size (unsigned int bits, bool unsignedp)
2935 if (bits == TYPE_PRECISION (integer_type_node))
2936 return unsignedp ? unsigned_type_node : integer_type_node;
2938 if (bits == TYPE_PRECISION (signed_char_type_node))
2939 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2941 if (bits == TYPE_PRECISION (short_integer_type_node))
2942 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2944 if (bits == TYPE_PRECISION (long_integer_type_node))
2945 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2947 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2948 return (unsignedp ? long_long_unsigned_type_node
2949 : long_long_integer_type_node);
2951 if (bits <= TYPE_PRECISION (intQI_type_node))
2952 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2954 if (bits <= TYPE_PRECISION (intHI_type_node))
2955 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2957 if (bits <= TYPE_PRECISION (intSI_type_node))
2958 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2960 if (bits <= TYPE_PRECISION (intDI_type_node))
2961 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2963 return 0;
2966 /* gm2_unsigned_type return an unsigned type the same as TYPE in
2967 other respects. */
2969 tree
2970 m2type_gm2_unsigned_type (tree type)
2972 tree type1 = TYPE_MAIN_VARIANT (type);
2973 if (type1 == signed_char_type_node || type1 == char_type_node)
2974 return unsigned_char_type_node;
2975 if (type1 == integer_type_node)
2976 return unsigned_type_node;
2977 if (type1 == short_integer_type_node)
2978 return short_unsigned_type_node;
2979 if (type1 == long_integer_type_node)
2980 return long_unsigned_type_node;
2981 if (type1 == long_long_integer_type_node)
2982 return long_long_unsigned_type_node;
2984 #if HOST_BITS_PER_WIDE_INT >= 64
2985 if (type1 == intTI_type_node)
2986 return unsigned_intTI_type_node;
2987 #endif
2988 if (type1 == intDI_type_node)
2989 return unsigned_intDI_type_node;
2990 if (type1 == intSI_type_node)
2991 return unsigned_intSI_type_node;
2992 if (type1 == intHI_type_node)
2993 return unsigned_intHI_type_node;
2994 if (type1 == intQI_type_node)
2995 return unsigned_intQI_type_node;
2997 return m2type_gm2_signed_or_unsigned_type (true, type);
3000 /* gm2_signed_type return a signed type the same as TYPE in other
3001 respects. */
3003 tree
3004 m2type_gm2_signed_type (tree type)
3006 tree type1 = TYPE_MAIN_VARIANT (type);
3007 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3008 return signed_char_type_node;
3009 if (type1 == unsigned_type_node)
3010 return integer_type_node;
3011 if (type1 == short_unsigned_type_node)
3012 return short_integer_type_node;
3013 if (type1 == long_unsigned_type_node)
3014 return long_integer_type_node;
3015 if (type1 == long_long_unsigned_type_node)
3016 return long_long_integer_type_node;
3018 #if HOST_BITS_PER_WIDE_INT >= 64
3019 if (type1 == unsigned_intTI_type_node)
3020 return intTI_type_node;
3021 #endif
3022 if (type1 == unsigned_intDI_type_node)
3023 return intDI_type_node;
3024 if (type1 == unsigned_intSI_type_node)
3025 return intSI_type_node;
3026 if (type1 == unsigned_intHI_type_node)
3027 return intHI_type_node;
3028 if (type1 == unsigned_intQI_type_node)
3029 return intQI_type_node;
3031 return m2type_gm2_signed_or_unsigned_type (false, type);
3034 /* check_type if the precision of baseType and type are the same
3035 then return true and set the signed or unsigned type in result
3036 else return false. */
3038 static int
3039 check_type (tree baseType, tree type, int unsignedp, tree baseu, tree bases,
3040 tree *result)
3042 if (TYPE_PRECISION (baseType) == TYPE_PRECISION (type))
3044 if (unsignedp)
3045 *result = baseu;
3046 else
3047 *result = bases;
3048 return true;
3050 return false;
3053 /* gm2_signed_or_unsigned_type return a type the same as TYPE
3054 except unsigned or signed according to UNSIGNEDP. */
3056 tree
3057 m2type_gm2_signed_or_unsigned_type (int unsignedp, tree type)
3059 tree result;
3061 if (!INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
3062 return type;
3064 /* For INTEGER_TYPEs we must check the precision as well, so as to
3065 yield correct results for bit-field types. */
3067 if (check_type (signed_char_type_node, type, unsignedp,
3068 unsigned_char_type_node, signed_char_type_node, &result))
3069 return result;
3070 if (check_type (integer_type_node, type, unsignedp, unsigned_type_node,
3071 integer_type_node, &result))
3072 return result;
3073 if (check_type (short_integer_type_node, type, unsignedp,
3074 short_unsigned_type_node, short_integer_type_node, &result))
3075 return result;
3076 if (check_type (long_integer_type_node, type, unsignedp,
3077 long_unsigned_type_node, long_integer_type_node, &result))
3078 return result;
3079 if (check_type (long_long_integer_type_node, type, unsignedp,
3080 long_long_unsigned_type_node, long_long_integer_type_node,
3081 &result))
3082 return result;
3084 #if HOST_BITS_PER_WIDE_INT >= 64
3085 if (check_type (intTI_type_node, type, unsignedp, unsigned_intTI_type_node,
3086 intTI_type_node, &result))
3087 return result;
3088 #endif
3089 if (check_type (intDI_type_node, type, unsignedp, unsigned_intDI_type_node,
3090 intDI_type_node, &result))
3091 return result;
3092 if (check_type (intSI_type_node, type, unsignedp, unsigned_intSI_type_node,
3093 intSI_type_node, &result))
3094 return result;
3095 if (check_type (intHI_type_node, type, unsignedp, unsigned_intHI_type_node,
3096 intHI_type_node, &result))
3097 return result;
3098 if (check_type (intQI_type_node, type, unsignedp, unsigned_intQI_type_node,
3099 intQI_type_node, &result))
3100 return result;
3101 #undef TYPE_OK
3103 return type;
3106 /* IsAddress returns true if the type is an ADDRESS. */
3109 m2type_IsAddress (tree type)
3111 return type == ptr_type_node;
3114 #include "gt-m2-m2type.h"