Debian Codegraph @

gcc/m2/gm2-gcc/m2type.cc

raw
1 /* m2type.cc provides an interface to GCC type trees.
2
3 Copyright (C) 2012-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
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.
12
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.
17
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/>. */
21
22 #include "gcc-consolidation.h"
23
24 #include "../gm2-lang.h"
25 #include "../m2-tree.h"
26
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"
41
42 #define USE_BOOLEAN
43 static int broken_set_debugging_info = true;
44
45
46 struct GTY (()) struct_constructor
47 {
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;
61 };
62
63 static GTY (()) struct struct_constructor *top_constructor = NULL;
64
65 typedef struct GTY (()) array_desc
66 {
67 int type;
68 tree index;
69 tree array;
70 struct array_desc *next;
71 } array_desc;
72
73 static GTY (()) array_desc *list_of_arrays = NULL;
74 /* Used in BuildStartFunctionType. */
75 static GTY (()) tree param_type_list;
76
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;
123
124 /* gm2_canonicalize_array - returns a unique array node based on
125 index_type and type. */
126
127 static tree
128 gm2_canonicalize_array (tree index_type, int type)
129 {
130 array_desc *l = list_of_arrays;
131
132 while (l != NULL)
133 {
134 if (l->type == type && l->index == index_type)
135 return l->array;
136 else
137 l = l->next;
138 }
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;
148 }
149
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. */
154
155 tree
156 m2type_BuildStartArrayType (tree index_type, tree elt_type, int type)
157 {
158 tree t;
159
160 elt_type = m2tree_skip_type_decl (elt_type);
161 ASSERT_CONDITION (index_type != NULL_TREE);
162 if (elt_type == NULL_TREE)
163 {
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);
167 }
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);
173
174 return t;
175 }
176
177 /* PutArrayType assignes TREE_TYPE (array) to the skipped type. */
178
179 void
180 m2type_PutArrayType (tree array, tree type)
181 {
182 TREE_TYPE (array) = m2tree_skip_type_decl (type);
183 }
184
185 /* gccgm2_GetArrayNoOfElements returns the number of elements in
186 arraytype. */
187
188 tree
189 m2type_GetArrayNoOfElements (location_t location, tree arraytype)
190 {
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);
194
195 m2assert_AssertLocation (location);
196 return m2expr_FoldAndStrip (m2expr_BuildSub (location, max, min, false));
197 }
198
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. */
205
206 static tree
207 gm2_finish_build_array_type (tree arrayType, tree elt_type, tree index_type,
208 int type)
209 {
210 tree old = arrayType;
211
212 elt_type = m2tree_skip_type_decl (elt_type);
213 ASSERT_CONDITION (index_type != NULL_TREE);
214 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
215 {
216 error ("arrays of functions are not meaningful");
217 elt_type = integer_type_node;
218 }
219
220 TREE_TYPE (arrayType) = elt_type;
221 TYPE_DOMAIN (arrayType) = index_type;
222
223 arrayType = gm2_canonicalize_array (index_type, type);
224 if (arrayType != old)
225 internal_error ("array declaration canonicalization has failed");
226
227 if (!COMPLETE_TYPE_P (arrayType))
228 layout_type (arrayType);
229 return arrayType;
230 }
231
232 /* BuildEndArrayType returns a type which is an array indexed by
233 IndexType and which has ElementType elements. */
234
235 tree
236 m2type_BuildEndArrayType (tree arraytype, tree elementtype, tree indextype,
237 int type)
238 {
239 elementtype = m2tree_skip_type_decl (elementtype);
240 ASSERT (indextype == TYPE_DOMAIN (arraytype), indextype);
241
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);
248 }
249
250 /* gm2_build_array_type returns a type which is an array indexed by
251 IndexType and which has ElementType elements. */
252
253 static tree
254 gm2_build_array_type (tree elementtype, tree indextype, int fetype)
255 {
256 tree arrayType = m2type_BuildStartArrayType (indextype, elementtype, fetype);
257 return m2type_BuildEndArrayType (arrayType, elementtype, indextype, fetype);
258 }
259
260 /* ValueInTypeRange returns true if the constant, value, lies within
261 the range of type. */
262
263 bool
264 m2type_ValueInTypeRange (tree type, tree value)
265 {
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);
269
270 value = m2expr_FoldAndStrip (value);
271 return ((tree_int_cst_compare (min_value, value) <= 0)
272 && (tree_int_cst_compare (value, max_value) <= 0));
273 }
274
275 /* ValueOutOfTypeRange returns true if the constant, value, exceeds
276 the range of type. */
277
278 bool
279 m2type_ValueOutOfTypeRange (tree type, tree value)
280 {
281 return (!m2type_ValueInTypeRange (type, value));
282 }
283
284 /* ExceedsTypeRange return true if low or high exceed the range of
285 type. */
286
287 bool
288 m2type_ExceedsTypeRange (tree type, tree low, tree high)
289 {
290 return (m2type_ValueOutOfTypeRange (type, low)
291 || m2type_ValueOutOfTypeRange (type, high));
292 }
293
294 /* WithinTypeRange return true if low and high are within the range
295 of type. */
296
297 bool
298 m2type_WithinTypeRange (tree type, tree low, tree high)
299 {
300 return (m2type_ValueInTypeRange (type, low)
301 && m2type_ValueInTypeRange (type, high));
302 }
303
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. */
307
308 tree
309 m2type_BuildArrayIndexType (tree low, tree high)
310 {
311 tree sizelow = convert (m2type_GetIntegerType (), m2expr_FoldAndStrip (low));
312 tree sizehigh
313 = convert (m2type_GetIntegerType (), m2expr_FoldAndStrip (high));
314
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");
319
320 return build_range_type (m2type_GetIntegerType (),
321 m2expr_FoldAndStrip (sizelow),
322 m2expr_FoldAndStrip (sizehigh));
323 }
324
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. */
328
329 static tree
330 build_m2_type_node_by_array (tree arrayType, tree low, tree high, int fetype)
331 {
332 return gm2_build_array_type (arrayType,
333 m2type_BuildArrayIndexType (low, high), fetype);
334 }
335
336 /* build_m2_word16_type_node build an ISO 16 bit word as an ARRAY
337 [0..1] OF loc. */
338
339 static tree
340 build_m2_word16_type_node (location_t location, int loc)
341 {
342 return build_m2_type_node_by_array (m2type_GetISOLocType (),
343 m2expr_GetIntegerZero (location),
344 m2expr_GetIntegerOne (location), loc);
345 }
346
347 /* build_m2_word32_type_node build an ISO 32 bit word as an ARRAY
348 [0..3] OF loc. */
349
350 static tree
351 build_m2_word32_type_node (location_t location, int loc)
352 {
353 return build_m2_type_node_by_array (m2type_GetISOLocType (),
354 m2expr_GetIntegerZero (location),
355 m2decl_BuildIntegerConstant (3), loc);
356 }
357
358 /* build_m2_word64_type_node build an ISO 32 bit word as an ARRAY
359 [0..7] OF loc. */
360
361 static tree
362 build_m2_word64_type_node (location_t location, int loc)
363 {
364 return build_m2_type_node_by_array (m2type_GetISOLocType (),
365 m2expr_GetIntegerZero (location),
366 m2decl_BuildIntegerConstant (7), loc);
367 }
368
369
370 /* GetM2Complex32 return the fixed size complex type. */
371
372 tree
373 m2type_GetM2Complex32 (void)
374 {
375 return m2_complex32_type_node;
376 }
377
378 /* GetM2Complex64 return the fixed size complex type. */
379
380 tree
381 m2type_GetM2Complex64 (void)
382 {
383 return m2_complex64_type_node;
384 }
385
386 /* GetM2Complex96 return the fixed size complex type. */
387
388 tree
389 m2type_GetM2Complex96 (void)
390 {
391 return m2_complex96_type_node;
392 }
393
394 /* GetM2Complex128 return the fixed size complex type. */
395
396 tree
397 m2type_GetM2Complex128 (void)
398 {
399 return m2_complex128_type_node;
400 }
401
402 /* GetM2CType a test function. */
403
404 tree
405 m2type_GetM2CType (void)
406 {
407 return m2_c_type_node;
408 }
409
410 /* GetM2ShortComplexType return the short complex type. */
411
412 tree
413 m2type_GetM2ShortComplexType (void)
414 {
415 return m2_short_complex_type_node;
416 }
417
418 /* GetM2LongComplexType return the long complex type. */
419
420 tree
421 m2type_GetM2LongComplexType (void)
422 {
423 return m2_long_complex_type_node;
424 }
425
426 /* GetM2ComplexType return the complex type. */
427
428 tree
429 m2type_GetM2ComplexType (void)
430 {
431 return m2_complex_type_node;
432 }
433
434 /* GetM2Real128 return the real 128 bit type. */
435
436 tree
437 m2type_GetM2Real128 (void)
438 {
439 return m2_real128_type_node;
440 }
441
442 /* GetM2Real96 return the real 96 bit type. */
443
444 tree
445 m2type_GetM2Real96 (void)
446 {
447 return m2_real96_type_node;
448 }
449
450 /* GetM2Real64 return the real 64 bit type. */
451
452 tree
453 m2type_GetM2Real64 (void)
454 {
455 return m2_real64_type_node;
456 }
457
458 /* GetM2Real32 return the real 32 bit type. */
459
460 tree
461 m2type_GetM2Real32 (void)
462 {
463 return m2_real32_type_node;
464 }
465
466 /* GetM2Bitset32 return the bitset 32 bit type. */
467
468 tree
469 m2type_GetM2Bitset32 (void)
470 {
471 return m2_bitset32_type_node;
472 }
473
474 /* GetM2Bitset16 return the bitset 16 bit type. */
475
476 tree
477 m2type_GetM2Bitset16 (void)
478 {
479 return m2_bitset16_type_node;
480 }
481
482 /* GetM2Bitset8 return the bitset 8 bit type. */
483
484 tree
485 m2type_GetM2Bitset8 (void)
486 {
487 return m2_bitset8_type_node;
488 }
489
490 /* GetM2Word64 return the word 64 bit type. */
491
492 tree
493 m2type_GetM2Word64 (void)
494 {
495 return m2_word64_type_node;
496 }
497
498 /* GetM2Word32 return the word 32 bit type. */
499
500 tree
501 m2type_GetM2Word32 (void)
502 {
503 return m2_word32_type_node;
504 }
505
506 /* GetM2Word16 return the word 16 bit type. */
507
508 tree
509 m2type_GetM2Word16 (void)
510 {
511 return m2_word16_type_node;
512 }
513
514 /* GetM2Cardinal64 return the cardinal 64 bit type. */
515
516 tree
517 m2type_GetM2Cardinal64 (void)
518 {
519 return m2_cardinal64_type_node;
520 }
521
522 /* GetM2Cardinal32 return the cardinal 32 bit type. */
523
524 tree
525 m2type_GetM2Cardinal32 (void)
526 {
527 return m2_cardinal32_type_node;
528 }
529
530 /* GetM2Cardinal16 return the cardinal 16 bit type. */
531
532 tree
533 m2type_GetM2Cardinal16 (void)
534 {
535 return m2_cardinal16_type_node;
536 }
537
538 /* GetM2Cardinal8 return the cardinal 8 bit type. */
539
540 tree
541 m2type_GetM2Cardinal8 (void)
542 {
543 return m2_cardinal8_type_node;
544 }
545
546 /* GetM2Integer64 return the integer 64 bit type. */
547
548 tree
549 m2type_GetM2Integer64 (void)
550 {
551 return m2_integer64_type_node;
552 }
553
554 /* GetM2Integer32 return the integer 32 bit type. */
555
556 tree
557 m2type_GetM2Integer32 (void)
558 {
559 return m2_integer32_type_node;
560 }
561
562 /* GetM2Integer16 return the integer 16 bit type. */
563
564 tree
565 m2type_GetM2Integer16 (void)
566 {
567 return m2_integer16_type_node;
568 }
569
570 /* GetM2Integer8 return the integer 8 bit type. */
571
572 tree
573 m2type_GetM2Integer8 (void)
574 {
575 return m2_integer8_type_node;
576 }
577
578 /* GetM2RType return the ISO R data type, the longest real
579 datatype. */
580
581 tree
582 m2type_GetM2RType (void)
583 {
584 return long_double_type_node;
585 }
586
587 /* GetM2ZType return the ISO Z data type, the longest int datatype. */
588
589 tree
590 m2type_GetM2ZType (void)
591 {
592 return m2_z_type_node;
593 }
594
595 /* GetShortCardType return the C short unsigned data type. */
596
597 tree
598 m2type_GetShortCardType (void)
599 {
600 return short_unsigned_type_node;
601 }
602
603 /* GetM2ShortCardType return the m2 short cardinal data type. */
604
605 tree
606 m2type_GetM2ShortCardType (void)
607 {
608 return m2_short_card_type_node;
609 }
610
611 /* GetShortIntType return the C short int data type. */
612
613 tree
614 m2type_GetShortIntType (void)
615 {
616 return short_integer_type_node;
617 }
618
619 /* GetM2ShortIntType return the m2 short integer data type. */
620
621 tree
622 m2type_GetM2ShortIntType (void)
623 {
624 return m2_short_int_type_node;
625 }
626
627 /* GetM2LongCardType return the m2 long cardinal data type. */
628
629 tree
630 m2type_GetM2LongCardType (void)
631 {
632 return m2_long_card_type_node;
633 }
634
635 /* GetM2LongIntType return the m2 long integer data type. */
636
637 tree
638 m2type_GetM2LongIntType (void)
639 {
640 return m2_long_int_type_node;
641 }
642
643 /* GetM2LongRealType return the m2 long real data type. */
644
645 tree
646 m2type_GetM2LongRealType (void)
647 {
648 return m2_long_real_type_node;
649 }
650
651 /* GetM2RealType return the m2 real data type. */
652
653 tree
654 m2type_GetM2RealType (void)
655 {
656 return m2_real_type_node;
657 }
658
659 /* GetM2ShortRealType return the m2 short real data type. */
660
661 tree
662 m2type_GetM2ShortRealType (void)
663 {
664 return m2_short_real_type_node;
665 }
666
667 /* GetM2CardinalType return the m2 cardinal data type. */
668
669 tree
670 m2type_GetM2CardinalType (void)
671 {
672 return m2_cardinal_type_node;
673 }
674
675 /* GetM2IntegerType return the m2 integer data type. */
676
677 tree
678 m2type_GetM2IntegerType (void)
679 {
680 return m2_integer_type_node;
681 }
682
683 /* GetM2CharType return the m2 char data type. */
684
685 tree
686 m2type_GetM2CharType (void)
687 {
688 return m2_char_type_node;
689 }
690
691 /* GetProcType return the m2 proc data type. */
692
693 tree
694 m2type_GetProcType (void)
695 {
696 return proc_type_node;
697 }
698
699 /* GetISOWordType return the m2 iso word data type. */
700
701 tree
702 m2type_GetISOWordType (void)
703 {
704 return m2_iso_word_type_node;
705 }
706
707 /* GetISOByteType return the m2 iso byte data type. */
708
709 tree
710 m2type_GetISOByteType (void)
711 {
712 return m2_iso_byte_type_node;
713 }
714
715 /* GetISOLocType return the m2 loc word data type. */
716
717 tree
718 m2type_GetISOLocType (void)
719 {
720 return m2_iso_loc_type_node;
721 }
722
723 /* GetWordType return the C unsigned data type. */
724
725 tree
726 m2type_GetWordType (void)
727 {
728 return unsigned_type_node;
729 }
730
731 /* GetLongIntType return the C long int data type. */
732
733 tree
734 m2type_GetLongIntType (void)
735 {
736 return long_integer_type_node;
737 }
738
739 /* GetShortRealType return the C float data type. */
740
741 tree
742 m2type_GetShortRealType (void)
743 {
744 return float_type_node;
745 }
746
747 /* GetLongRealType return the C long double data type. */
748
749 tree
750 m2type_GetLongRealType (void)
751 {
752 return long_double_type_node;
753 }
754
755 /* GetRealType returns the C double_type_node. */
756
757 tree
758 m2type_GetRealType (void)
759 {
760 return double_type_node;
761 }
762
763 /* GetBitnumType return the ISO bitnum type. */
764
765 tree
766 m2type_GetBitnumType (void)
767 {
768 return bitnum_type_node;
769 }
770
771 /* GetBitsetType return the bitset type. */
772
773 tree
774 m2type_GetBitsetType (void)
775 {
776 return bitset_type_node;
777 }
778
779 /* GetCardinalType return the cardinal type. */
780
781 tree
782 m2type_GetCardinalType (void)
783 {
784 return unsigned_type_node;
785 }
786
787 /* GetPointerType return the GCC ptr type node. Equivalent to
788 (void *). */
789
790 tree
791 m2type_GetPointerType (void)
792 {
793 return ptr_type_node;
794 }
795
796 /* GetVoidType return the C void type. */
797
798 tree
799 m2type_GetVoidType (void)
800 {
801 return void_type_node;
802 }
803
804 /* GetByteType return the byte type node. */
805
806 tree
807 m2type_GetByteType (void)
808 {
809 return unsigned_char_type_node;
810 }
811
812 /* GetCharType return the char type node. */
813
814 tree
815 m2type_GetCharType (void)
816 {
817 return char_type_node;
818 }
819
820 /* GetIntegerType return the integer type node. */
821
822 tree
823 m2type_GetIntegerType (void)
824 {
825 return integer_type_node;
826 }
827
828 /* GetCSizeTType return a type representing size_t. */
829
830 tree
831 m2type_GetCSizeTType (void)
832 {
833 return sizetype;
834 }
835
836 /* GetCSSizeTType return a type representing size_t. */
837
838 tree
839 m2type_GetCSSizeTType (void)
840 {
841 return ssizetype;
842 }
843
844 /* GetCSSizeTType return a type representing off_t. */
845
846 tree
847 m2type_GetCOffTType (void)
848 {
849 return m2_offt_type_node;
850 }
851
852 /* GetPackedBooleanType return the packed boolean data type node. */
853
854 tree
855 m2type_GetPackedBooleanType (void)
856 {
857 return m2_packed_boolean_type_node;
858 }
859
860 /* GetBooleanTrue return modula-2 true. */
861
862 tree
863 m2type_GetBooleanTrue (void)
864 {
865 #if defined(USE_BOOLEAN)
866 return boolean_true_node;
867 #else /* !USE_BOOLEAN */
868 return m2expr_GetIntegerOne (m2linemap_BuiltinsLocation ());
869 #endif /* !USE_BOOLEAN */
870 }
871
872 /* GetBooleanFalse return modula-2 FALSE. */
873
874 tree
875 m2type_GetBooleanFalse (void)
876 {
877 #if defined(USE_BOOLEAN)
878 return boolean_false_node;
879 #else /* !USE_BOOLEAN */
880 return m2expr_GetIntegerZero (m2linemap_BuiltinsLocation ());
881 #endif /* !USE_BOOLEAN */
882 }
883
884 /* GetBooleanType return the modula-2 BOOLEAN type. */
885
886 tree
887 m2type_GetBooleanType (void)
888 {
889 #if defined(USE_BOOLEAN)
890 return boolean_type_node;
891 #else /* !USE_BOOLEAN */
892 return integer_type_node;
893 #endif /* !USE_BOOLEAN */
894 }
895
896 /* GetCardinalAddressType returns the internal data type for
897 computing binary arithmetic upon the ADDRESS datatype. */
898
899 tree
900 m2type_GetCardinalAddressType (void)
901 {
902 return m2_cardinal_address_type_node;
903 }
904
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. */
908
909 static tree
910 build_set_type (tree domain, tree range_type, int allow_void, int ispacked)
911 {
912 tree type;
913
914 if (!m2tree_IsOrdinal (domain)
915 && !(allow_void && TREE_CODE (domain) == VOID_TYPE))
916 {
917 error ("set base type must be an ordinal type");
918 return NULL;
919 }
920
921 if (TYPE_SIZE (range_type) == 0)
922 layout_type (range_type);
923
924 if (TYPE_SIZE (domain) == 0)
925 layout_type (domain);
926
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;
932 }
933
934
935 /* convert_type_to_range does the conversion and copies the range
936 type */
937
938 static tree
939 convert_type_to_range (tree type)
940 {
941 tree min, max;
942 tree itype;
943
944 if (!m2tree_IsOrdinal (type))
945 {
946 error ("ordinal type expected");
947 return error_mark_node;
948 }
949
950 min = TYPE_MIN_VALUE (type);
951 max = TYPE_MAX_VALUE (type);
952
953 if (TREE_TYPE (min) != TREE_TYPE (max))
954 {
955 error ("range limits are not of the same type");
956 return error_mark_node;
957 }
958
959 itype = build_range_type (TREE_TYPE (min), min, max);
960
961 if (TREE_TYPE (type) == NULL_TREE)
962 {
963 layout_type (type);
964 TREE_TYPE (itype) = type;
965 }
966 else
967 {
968 layout_type (TREE_TYPE (type));
969 TREE_TYPE (itype) = TREE_TYPE (type);
970 }
971
972 layout_type (itype);
973 return itype;
974 }
975 #endif
976
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). */
980
981 static tree
982 build_bitset_type (location_t location)
983 {
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);
990
991 #if 1
992 if (broken_set_debugging_info)
993 return unsigned_type_node;
994 #endif
995
996 ASSERT ((COMPLETE_TYPE_P (bitnum_type_node)), bitnum_type_node);
997
998 return m2type_BuildSetTypeFromSubrange (
999 location, NULL, bitnum_type_node, m2decl_BuildIntegerConstant (0),
1000 m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset () - 1), false);
1001 }
1002
1003 /* BuildSetTypeFromSubrange constructs a set type from a
1004 subrangeType. --fixme-- revisit once gdb/gcc supports dwarf-5 set type. */
1005
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)
1011 {
1012 m2assert_AssertLocation (location);
1013 lowval = m2expr_FoldAndStrip (lowval);
1014 highval = m2expr_FoldAndStrip (highval);
1015
1016 #if 0
1017 if (broken_set_debugging_info)
1018 return unsigned_type_node;
1019 else
1020 #endif
1021 if (ispacked)
1022 {
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);
1032 }
1033 else
1034 return unsigned_type_node;
1035 }
1036
1037 /* build_m2_size_set_type build and return a set type with
1038 precision bits. */
1039
1040 static tree
1041 build_m2_size_set_type (location_t location, int precision)
1042 {
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);
1049
1050 if (broken_set_debugging_info)
1051 return unsigned_type_node;
1052
1053 ASSERT ((COMPLETE_TYPE_P (bitnum_type_node)), bitnum_type_node);
1054
1055 return m2type_BuildSetTypeFromSubrange (
1056 location, NULL, bitnum_type_node, m2decl_BuildIntegerConstant (0),
1057 m2decl_BuildIntegerConstant (precision - 1), false);
1058 }
1059
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. */
1064
1065 static tree
1066 build_m2_specific_size_type (location_t location, enum tree_code base,
1067 int precision, int is_signed)
1068 {
1069 tree c;
1070
1071 m2assert_AssertLocation (location);
1072
1073 c = make_node (base);
1074 TYPE_PRECISION (c) = precision;
1075
1076 if (base == REAL_TYPE)
1077 {
1078 if (!float_mode_for_size (TYPE_PRECISION (c)).exists ())
1079 return NULL;
1080 }
1081 else if (base == SET_TYPE)
1082 return build_m2_size_set_type (location, precision);
1083 else
1084 {
1085 TYPE_SIZE (c) = 0;
1086
1087 if (is_signed)
1088 {
1089 fixup_signed_type (c);
1090 TYPE_UNSIGNED (c) = false;
1091 }
1092 else
1093 {
1094 fixup_unsigned_type (c);
1095 TYPE_UNSIGNED (c) = true;
1096 }
1097 }
1098 layout_type (c);
1099 return c;
1100 }
1101
1102 /* BuildSmallestTypeRange returns the smallest INTEGER_TYPE which
1103 is sufficient to contain values: low..high. */
1104
1105 tree
1106 m2type_BuildSmallestTypeRange (location_t location, tree low, tree high)
1107 {
1108 tree bits;
1109
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);
1117 }
1118
1119 /* GetTreeType returns TREE_TYPE (t). */
1120
1121 tree
1122 m2type_GetTreeType (tree t)
1123 {
1124 return TREE_TYPE (t);
1125 }
1126
1127 /* finish_build_pointer_type finish building a POINTER_TYPE node.
1128 necessary to solve self references in procedure types. */
1129
1130 /* Code taken from tree.cc:build_pointer_type_for_mode. */
1131
1132 static tree
1133 finish_build_pointer_type (tree t, tree to_type, enum machine_mode mode,
1134 bool can_alias_all)
1135 {
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;
1141
1142 /* Lay out the type. */
1143 /* layout_type (t); */
1144 layout_type (t);
1145 return t;
1146 }
1147
1148 /* BuildParameterDeclaration creates and returns one parameter
1149 from, name, and, type. It appends this parameter to the internal
1150 param_type_list. */
1151
1152 tree
1153 m2type_BuildProcTypeParameterDeclaration (location_t location, tree type,
1154 bool isreference)
1155 {
1156 m2assert_AssertLocation (location);
1157 ASSERT_BOOL (isreference);
1158 type = m2tree_skip_type_decl (type);
1159 if (isreference)
1160 type = build_reference_type (type);
1161
1162 param_type_list = tree_cons (NULL_TREE, type, param_type_list);
1163 return type;
1164 }
1165
1166 /* BuildEndFunctionType build a function type which would return a,
1167 value. The arguments have been created by
1168 BuildParameterDeclaration. */
1169
1170 tree
1171 m2type_BuildEndFunctionType (tree func, tree return_type, bool uses_varargs)
1172 {
1173 tree last;
1174
1175 if (return_type == NULL_TREE)
1176 return_type = void_type_node;
1177 else
1178 return_type = m2tree_skip_type_decl (return_type);
1179
1180 if (uses_varargs)
1181 {
1182 if (param_type_list != NULL_TREE)
1183 {
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);
1188 }
1189 }
1190 else if (param_type_list == NULL_TREE)
1191 param_type_list = void_list_node;
1192 else
1193 {
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;
1198 }
1199 param_type_list = build_function_type (return_type, param_type_list);
1200
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;
1205 }
1206
1207 /* BuildStartFunctionType creates a pointer type, necessary to
1208 create a function type. */
1209
1210 tree
1211 m2type_BuildStartFunctionType (location_t location ATTRIBUTE_UNUSED,
1212 char *name ATTRIBUTE_UNUSED)
1213 {
1214 tree n = make_node (POINTER_TYPE);
1215
1216 m2assert_AssertLocation (location);
1217 return n;
1218 }
1219
1220 /* InitFunctionTypeParameters resets the current function type
1221 parameter list. */
1222
1223 void
1224 m2type_InitFunctionTypeParameters (void)
1225 {
1226 param_type_list = NULL_TREE;
1227 }
1228
1229 /* gm2_finish_decl finishes VAR, TYPE and FUNCTION declarations. */
1230
1231 static void
1232 gm2_finish_decl (location_t location, tree decl)
1233 {
1234 tree type = TREE_TYPE (decl);
1235 int was_incomplete = (DECL_SIZE (decl) == 0);
1236
1237 m2assert_AssertLocation (location);
1238 if (VAR_P (decl))
1239 {
1240 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
1241 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
1242 layout_decl (decl, 0);
1243
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)
1247 {
1248 error_at (location, "storage size of %q+D isn%'t known", decl);
1249 TREE_TYPE (decl) = error_mark_node;
1250 }
1251
1252 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
1253 && DECL_SIZE (decl) != 0)
1254 {
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);
1259 }
1260
1261 if (TREE_USED (type))
1262 TREE_USED (decl) = 1;
1263 }
1264
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. */
1268
1269 if (VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 if (DECL_FILE_SCOPE_P (decl))
1272 {
1273 if (DECL_INITIAL (decl) == NULL_TREE
1274 || DECL_INITIAL (decl) == error_mark_node)
1275
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);
1280 }
1281
1282 if (!DECL_FILE_SCOPE_P (decl))
1283 {
1284
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))
1288 {
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;
1294 }
1295 }
1296 }
1297
1298 if (TREE_CODE (decl) == TYPE_DECL)
1299 {
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));
1303
1304 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
1305 }
1306 }
1307
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. */
1311
1312 tree
1313 m2type_BuildVariableArrayAndDeclare (location_t location, tree elementtype,
1314 tree high, char *name, tree scope)
1315 {
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;
1320
1321 m2assert_AssertLocation (location);
1322 decl = build_decl (location, VAR_DECL, id, arraytype);
1323
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;
1329
1330 m2block_pushDecl (decl);
1331
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;
1336 }
1337
1338 static tree
1339 build_m2_iso_word_node (location_t location, int loc)
1340 {
1341 tree c;
1342
1343 m2assert_AssertLocation (location);
1344 /* Define `WORD' as specified in ISO m2
1345
1346 WORD = ARRAY [0..SizeOfWord / SizeOfLoc] OF LOC ; */
1347
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;
1361 }
1362
1363 static tree
1364 build_m2_iso_byte_node (location_t location, int loc)
1365 {
1366 tree c;
1367
1368 /* Define `BYTE' as specified in ISO m2
1369
1370 BYTE = ARRAY [0..SizeOfByte / SizeOfLoc] OF LOC ; */
1371
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;
1382 }
1383
1384 static tree
1385 build_m2_offt_type_node (location_t location)
1386 {
1387 m2assert_AssertLocation (location);
1388 int offt_size = M2Options_GetFileOffsetBits ();
1389
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);
1394 }
1395
1396 /* m2type_InitSystemTypes initialise loc and word derivatives. */
1397
1398 void
1399 m2type_InitSystemTypes (location_t location, int loc)
1400 {
1401 m2assert_AssertLocation (location);
1402
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);
1405
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);
1410 }
1411
1412 static tree
1413 build_m2_integer_node (void)
1414 {
1415 return m2type_GetIntegerType ();
1416 }
1417
1418 static tree
1419 build_m2_cardinal_node (void)
1420 {
1421 return m2type_GetCardinalType ();
1422 }
1423
1424 static tree
1425 build_m2_char_node (void)
1426 {
1427 tree c;
1428
1429 /* Define `CHAR', to be an unsigned char. */
1430
1431 c = make_unsigned_type (CHAR_TYPE_SIZE);
1432 layout_type (c);
1433 return c;
1434 }
1435
1436 static tree
1437 build_m2_short_real_node (void)
1438 {
1439 /* Define `SHORTREAL'. */
1440 ASSERT_CONDITION (TYPE_SIZE (float_type_node));
1441 return float_type_node;
1442 }
1443
1444 static tree
1445 build_m2_real_node (void)
1446 {
1447 /* Define `REAL'. */
1448 ASSERT_CONDITION (TYPE_SIZE (double_type_node));
1449 return double_type_node;
1450 }
1451
1452 static tree
1453 build_m2_long_real_node (void)
1454 {
1455 tree longreal;
1456
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;
1464 }
1465
1466 static tree
1467 build_m2_ztype_node (void)
1468 {
1469 tree ztype_node;
1470
1471 /* Define `ZTYPE'. */
1472
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;
1479 }
1480
1481 static tree
1482 build_m2_long_int_node (void)
1483 {
1484 tree c;
1485
1486 /* Define `LONGINT'. */
1487
1488 c = make_signed_type (LONG_LONG_TYPE_SIZE);
1489 layout_type (c);
1490 return c;
1491 }
1492
1493 static tree
1494 build_m2_long_card_node (void)
1495 {
1496 tree c;
1497
1498 /* Define `LONGCARD'. */
1499
1500 c = make_unsigned_type (LONG_LONG_TYPE_SIZE);
1501 layout_type (c);
1502 return c;
1503 }
1504
1505 static tree
1506 build_m2_short_int_node (void)
1507 {
1508 tree c;
1509
1510 /* Define `SHORTINT'. */
1511
1512 c = make_signed_type (SHORT_TYPE_SIZE);
1513 layout_type (c);
1514 return c;
1515 }
1516
1517 static tree
1518 build_m2_short_card_node (void)
1519 {
1520 tree c;
1521
1522 /* Define `SHORTCARD'. */
1523
1524 c = make_unsigned_type (SHORT_TYPE_SIZE);
1525 layout_type (c);
1526 return c;
1527 }
1528
1529 static tree
1530 build_m2_iso_loc_node (void)
1531 {
1532 tree c;
1533
1534 /* Define `LOC' as specified in ISO m2. */
1535
1536 c = make_node (INTEGER_TYPE);
1537 TYPE_PRECISION (c) = BITS_PER_UNIT;
1538 TYPE_SIZE (c) = 0;
1539
1540 fixup_unsigned_type (c);
1541 TYPE_UNSIGNED (c) = 1;
1542 return c;
1543 }
1544
1545 static tree
1546 build_m2_integer8_type_node (location_t location)
1547 {
1548 m2assert_AssertLocation (location);
1549 return build_m2_specific_size_type (location, INTEGER_TYPE, 8, true);
1550 }
1551
1552 static tree
1553 build_m2_integer16_type_node (location_t location)
1554 {
1555 m2assert_AssertLocation (location);
1556 return build_m2_specific_size_type (location, INTEGER_TYPE, 16, true);
1557 }
1558
1559 static tree
1560 build_m2_integer32_type_node (location_t location)
1561 {
1562 m2assert_AssertLocation (location);
1563 return build_m2_specific_size_type (location, INTEGER_TYPE, 32, true);
1564 }
1565
1566 static tree
1567 build_m2_integer64_type_node (location_t location)
1568 {
1569 m2assert_AssertLocation (location);
1570 return build_m2_specific_size_type (location, INTEGER_TYPE, 64, true);
1571 }
1572
1573 static tree
1574 build_m2_cardinal8_type_node (location_t location)
1575 {
1576 m2assert_AssertLocation (location);
1577 return build_m2_specific_size_type (location, INTEGER_TYPE, 8, false);
1578 }
1579
1580 static tree
1581 build_m2_cardinal16_type_node (location_t location)
1582 {
1583 m2assert_AssertLocation (location);
1584 return build_m2_specific_size_type (location, INTEGER_TYPE, 16, false);
1585 }
1586
1587 static tree
1588 build_m2_cardinal32_type_node (location_t location)
1589 {
1590 m2assert_AssertLocation (location);
1591 return build_m2_specific_size_type (location, INTEGER_TYPE, 32, false);
1592 }
1593
1594 static tree
1595 build_m2_cardinal64_type_node (location_t location)
1596 {
1597 m2assert_AssertLocation (location);
1598 return build_m2_specific_size_type (location, INTEGER_TYPE, 64, false);
1599 }
1600
1601 static tree
1602 build_m2_bitset8_type_node (location_t location)
1603 {
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);
1609 }
1610
1611 static tree
1612 build_m2_bitset16_type_node (location_t location)
1613 {
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);
1619 }
1620
1621 static tree
1622 build_m2_bitset32_type_node (location_t location)
1623 {
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);
1629 }
1630
1631 static tree
1632 build_m2_real32_type_node (location_t location)
1633 {
1634 m2assert_AssertLocation (location);
1635 return build_m2_specific_size_type (location, REAL_TYPE, 32, true);
1636 }
1637
1638 static tree
1639 build_m2_real64_type_node (location_t location)
1640 {
1641 m2assert_AssertLocation (location);
1642 return build_m2_specific_size_type (location, REAL_TYPE, 64, true);
1643 }
1644
1645 static tree
1646 build_m2_real96_type_node (location_t location)
1647 {
1648 m2assert_AssertLocation (location);
1649 return build_m2_specific_size_type (location, REAL_TYPE, 96, true);
1650 }
1651
1652 static tree
1653 build_m2_real128_type_node (location_t location)
1654 {
1655 m2assert_AssertLocation (location);
1656 return build_m2_specific_size_type (location, REAL_TYPE, 128, true);
1657 }
1658
1659 static tree
1660 build_m2_complex_type_from (tree scalar_type)
1661 {
1662 tree new_type;
1663
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;
1672
1673 new_type = make_node (COMPLEX_TYPE);
1674 TREE_TYPE (new_type) = scalar_type;
1675 layout_type (new_type);
1676 return new_type;
1677 }
1678
1679 static tree
1680 build_m2_complex_type_node (void)
1681 {
1682 return build_m2_complex_type_from (m2_real_type_node);
1683 }
1684
1685 static tree
1686 build_m2_long_complex_type_node (void)
1687 {
1688 return build_m2_complex_type_from (m2_long_real_type_node);
1689 }
1690
1691 static tree
1692 build_m2_short_complex_type_node (void)
1693 {
1694 return build_m2_complex_type_from (m2_short_real_type_node);
1695 }
1696
1697 static tree
1698 build_m2_complex32_type_node (void)
1699 {
1700 return build_m2_complex_type_from (m2_real32_type_node);
1701 }
1702
1703 static tree
1704 build_m2_complex64_type_node (void)
1705 {
1706 return build_m2_complex_type_from (m2_real64_type_node);
1707 }
1708
1709 static tree
1710 build_m2_complex96_type_node (void)
1711 {
1712 return build_m2_complex_type_from (m2_real96_type_node);
1713 }
1714
1715 static tree
1716 build_m2_complex128_type_node (void)
1717 {
1718 return build_m2_complex_type_from (m2_real128_type_node);
1719 }
1720
1721 static tree
1722 build_m2_cardinal_address_type_node (location_t location)
1723 {
1724 tree size = size_in_bytes (ptr_type_node);
1725 int bits = TREE_INT_CST_LOW (size) * BITS_PER_UNIT;
1726
1727 return build_m2_specific_size_type (location, INTEGER_TYPE, bits, false);
1728 }
1729
1730 static void
1731 build_m2_boolean (location_t location)
1732 {
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;
1737 }
1738
1739
1740 /* Return true if real types a and b are the same. */
1741
1742 bool
1743 m2type_SameRealType (tree a, tree b)
1744 {
1745 return ((a == b)
1746 || (TYPE_PRECISION (a) == TYPE_PRECISION (b)));
1747 }
1748
1749 /* InitBaseTypes create the Modula-2 base types. */
1750
1751 void
1752 m2type_InitBaseTypes (location_t location)
1753 {
1754 m2assert_AssertLocation (location);
1755 m2block_init ();
1756
1757 ptr_type_node = build_pointer_type (void_type_node);
1758
1759 proc_type_node
1760 = build_pointer_type (build_function_type (void_type_node, NULL_TREE));
1761
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 ();
1798
1799 m2_cardinal_address_type_node
1800 = build_m2_cardinal_address_type_node (location);
1801
1802 m2_packed_boolean_type_node = build_nonstandard_integer_type (1, true);
1803 build_m2_boolean (location);
1804
1805 if (M2Options_GetPPOnly ())
1806 return;
1807
1808 m2builtins_init (location);
1809 m2except_InitExceptions (location);
1810 m2expr_init (location);
1811 }
1812
1813 /* BuildStartType given a, type, with a, name, return a GCC
1814 declaration of this type. TYPE name = foo ;
1815
1816 the type, foo, maybe a partially created type (which has
1817 yet to be 'gm2_finish_decl'ed). */
1818
1819 tree
1820 m2type_BuildStartType (location_t location, char *name, tree type)
1821 {
1822 tree id = get_identifier (name);
1823 tree decl, tem;
1824
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);
1829
1830 tem = m2block_pushDecl (decl);
1831 ASSERT (tem == decl, decl);
1832 ASSERT (m2tree_is_type (decl), decl);
1833
1834 return tem;
1835 }
1836
1837 /* BuildEndType finish declaring, type, and return, type. */
1838
1839 tree
1840 m2type_BuildEndType (location_t location, tree type)
1841 {
1842 m2assert_AssertLocation (location);
1843 layout_type (TREE_TYPE (type));
1844 gm2_finish_decl (location, type);
1845 return type;
1846 }
1847
1848 /* DeclareKnownType given a, type, with a, name, return a GCC
1849 declaration of this type. TYPE name = foo ; */
1850
1851 tree
1852 m2type_DeclareKnownType (location_t location, char *name, tree type)
1853 {
1854 m2assert_AssertLocation (location);
1855 return m2type_BuildEndType (location,
1856 m2type_BuildStartType (location, name, type));
1857 }
1858
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:
1864
1865 TYPE name = type ;
1866
1867 We need this function during gm2 initialization as it allows
1868 gm2 to access default types before creating Modula-2 types. */
1869
1870 tree
1871 m2type_GetDefaultType (location_t location, char *name, tree type)
1872 {
1873 tree id = maybe_get_identifier (name);
1874
1875 m2assert_AssertLocation (location);
1876 if (id == NULL)
1877 {
1878 tree prev = type;
1879 tree t;
1880
1881 while (prev != NULL)
1882 {
1883 if (TYPE_NAME (prev) == NULL)
1884 TYPE_NAME (prev) = get_identifier (name);
1885 prev = TREE_TYPE (prev);
1886 }
1887 t = m2type_DeclareKnownType (location, name, type);
1888 return t;
1889 }
1890 else
1891 return id;
1892 }
1893
1894 /* IsGccRealType return true if type is a GCC realtype. */
1895
1896 static
1897 bool
1898 IsGccRealType (tree type)
1899 {
1900 return (type == m2_real_type_node || type == m2type_GetRealType () ||
1901 type == m2_long_real_type_node || type == m2type_GetLongRealType () ||
1902 type == m2_short_real_type_node || type == m2type_GetShortRealType () ||
1903 type == m2type_GetM2Real32 () ||
1904 type == m2type_GetM2Real64 () ||
1905 type == m2type_GetM2Real96 () ||
1906 type == m2type_GetM2Real128 ());
1907 }
1908
1909 static
1910 tree
1911 do_min_real (tree type)
1912 {
1913 REAL_VALUE_TYPE r;
1914 char buf[128];
1915 enum machine_mode mode = TYPE_MODE (type);
1916
1917 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
1918 real_from_string (&r, buf);
1919 return build1 (NEGATE_EXPR, type, build_real (type, r));
1920 }
1921
1922 /* GetMinFrom given a, type, return a constant representing the
1923 minimum legal value. */
1924
1925 tree
1926 m2type_GetMinFrom (location_t location, tree type)
1927 {
1928 m2assert_AssertLocation (location);
1929
1930 if (IsGccRealType (type))
1931 return do_min_real (type);
1932 if (type == ptr_type_node)
1933 return m2expr_GetPointerZero (location);
1934
1935 return TYPE_MIN_VALUE (m2tree_skip_type_decl (type));
1936 }
1937
1938 static
1939 tree
1940 do_max_real (tree type)
1941 {
1942 REAL_VALUE_TYPE r;
1943 char buf[128];
1944 enum machine_mode mode = TYPE_MODE (type);
1945
1946 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
1947 real_from_string (&r, buf);
1948 return build_real (type, r);
1949 }
1950
1951 /* GetMaxFrom given a, type, return a constant representing the
1952 maximum legal value. */
1953
1954 tree
1955 m2type_GetMaxFrom (location_t location, tree type)
1956 {
1957 m2assert_AssertLocation (location);
1958
1959 if (IsGccRealType (type))
1960 return do_max_real (type);
1961 if (type == ptr_type_node)
1962 return fold (m2expr_BuildSub (location, m2expr_GetPointerZero (location),
1963 m2expr_GetPointerOne (location), false));
1964
1965 return TYPE_MAX_VALUE (m2tree_skip_type_decl (type));
1966 }
1967
1968 /* BuildTypeDeclaration adds the, type, to the current statement
1969 list. */
1970
1971 void
1972 m2type_BuildTypeDeclaration (location_t location, tree type)
1973 {
1974 enum tree_code code = TREE_CODE (type);
1975
1976 m2assert_AssertLocation (location);
1977 if (code == TYPE_DECL || code == RECORD_TYPE || code == POINTER_TYPE)
1978 {
1979 m2block_pushDecl (build_decl (location, TYPE_DECL, NULL, type));
1980 }
1981 else if (code == VAR_DECL)
1982 {
1983 m2type_BuildTypeDeclaration (location, TREE_TYPE (type));
1984 m2block_pushDecl (
1985 build_stmt (location, DECL_EXPR,
1986 type)); /* Is this safe? --fixme--. */
1987 }
1988 }
1989
1990 /* Begin compiling the definition of an enumeration type. NAME is
1991 its name (or null if anonymous). Returns the type object, as yet
1992 incomplete. Also records info about it so that build_enumerator may
1993 be used to declare the individual values as they are read. */
1994
1995 static tree
1996 gm2_start_enum (location_t location, tree name, int ispacked)
1997 {
1998 tree enumtype = make_node (ENUMERAL_TYPE);
1999
2000 m2assert_AssertLocation (location);
2001 if (TYPE_VALUES (enumtype) != 0)
2002 {
2003 /* This enum is a named one that has been declared already. */
2004 error_at (location, "redeclaration of enum %qs",
2005 IDENTIFIER_POINTER (name));
2006
2007 /* Completely replace its old definition. The old enumerators remain
2008 defined, however. */
2009 TYPE_VALUES (enumtype) = 0;
2010 }
2011
2012 TYPE_PACKED (enumtype) = ispacked;
2013 TREE_TYPE (enumtype) = m2type_GetIntegerType ();
2014
2015 /* This is required as rest_of_type_compilation will use this field
2016 when called from gm2_finish_enum.
2017
2018 Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
2019 tagged type we just added to the current scope. This fake NULL-named
2020 TYPE_DECL node helps dwarfout.cc to know when it needs to output a
2021 representation of a tagged type, and it also gives us a convenient
2022 place to record the "scope start" address for the tagged type. */
2023
2024 TYPE_STUB_DECL (enumtype) = m2block_pushDecl (
2025 build_decl (location, TYPE_DECL, NULL_TREE, enumtype));
2026
2027 return enumtype;
2028 }
2029
2030 /* After processing and defining all the values of an enumeration
2031 type, install their decls in the enumeration type and finish it off.
2032 ENUMTYPE is the type object, VALUES a list of decl-value pairs, and
2033 ATTRIBUTES are the specified attributes. Returns ENUMTYPE. */
2034
2035 static tree
2036 gm2_finish_enum (location_t location, tree enumtype, tree values)
2037 {
2038 tree pair, tem;
2039 tree minnode = 0, maxnode = 0;
2040 int precision;
2041 signop sign;
2042
2043 /* Calculate the maximum value of any enumerator in this type. */
2044
2045 if (values == error_mark_node)
2046 minnode = maxnode = integer_zero_node;
2047 else
2048 {
2049 minnode = maxnode = TREE_VALUE (values);
2050 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
2051 {
2052 tree value = TREE_VALUE (pair);
2053 if (tree_int_cst_lt (maxnode, value))
2054 maxnode = value;
2055 if (tree_int_cst_lt (value, minnode))
2056 minnode = value;
2057 }
2058 }
2059
2060 /* Construct the final type of this enumeration. It is the same as
2061 one of the integral types the narrowest one that fits, except that
2062 normally we only go as narrow as int and signed iff any of the
2063 values are negative. */
2064 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
2065 precision = MAX (tree_int_cst_min_precision (minnode, sign),
2066 tree_int_cst_min_precision (maxnode, sign));
2067
2068 if (precision > TYPE_PRECISION (integer_type_node))
2069 {
2070 warning (0, "enumeration values exceed range of integer");
2071 tem = long_long_integer_type_node;
2072 }
2073 else if (TYPE_PACKED (enumtype))
2074 tem = m2type_BuildSmallestTypeRange (location, minnode, maxnode);
2075 else
2076 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
2077
2078 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
2079 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
2080 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
2081 TYPE_SIZE (enumtype) = 0;
2082
2083 /* If the precision of the type was specific with an attribute and it
2084 was too small, give an error. Otherwise, use it. */
2085 if (TYPE_PRECISION (enumtype))
2086 {
2087 if (precision > TYPE_PRECISION (enumtype))
2088 error ("specified mode too small for enumerated values");
2089 }
2090 else
2091 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
2092
2093 layout_type (enumtype);
2094
2095 if (values != error_mark_node)
2096 {
2097
2098 /* Change the type of the enumerators to be the enum type. We need
2099 to do this irrespective of the size of the enum, for proper type
2100 checking. Replace the DECL_INITIALs of the enumerators, and the
2101 value slots of the list, with copies that have the enum type; they
2102 cannot be modified in place because they may be shared (e.g.
2103 integer_zero_node) Finally, change the purpose slots to point to the
2104 names of the decls. */
2105 for (pair = values; pair; pair = TREE_CHAIN (pair))
2106 {
2107 tree enu = TREE_PURPOSE (pair);
2108 tree ini = DECL_INITIAL (enu);
2109
2110 TREE_TYPE (enu) = enumtype;
2111
2112 if (TREE_TYPE (ini) != integer_type_node)
2113 ini = convert (enumtype, ini);
2114
2115 DECL_INITIAL (enu) = ini;
2116 TREE_PURPOSE (pair) = DECL_NAME (enu);
2117 TREE_VALUE (pair) = ini;
2118 }
2119
2120 TYPE_VALUES (enumtype) = values;
2121 }
2122
2123 /* Fix up all variant types of this enum type. */
2124 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
2125 {
2126 if (tem == enumtype)
2127 continue;
2128 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
2129 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
2130 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
2131 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
2132 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
2133 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
2134 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
2135 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
2136 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
2137 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
2138 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
2139 }
2140
2141 /* Finish debugging output for this type. */
2142 rest_of_type_compilation (enumtype, m2block_toplevel ());
2143 return enumtype;
2144 }
2145
2146 /* BuildStartEnumeration create an enumerated type in gcc. */
2147
2148 tree
2149 m2type_BuildStartEnumeration (location_t location, char *name, bool ispacked)
2150 {
2151 tree id;
2152
2153 m2assert_AssertLocation (location);
2154 if ((name == NULL) || (strcmp (name, "") == 0))
2155 id = NULL_TREE;
2156 else
2157 id = get_identifier (name);
2158
2159 return gm2_start_enum (location, id, ispacked);
2160 }
2161
2162 /* BuildEndEnumeration finish building the enumeration, it uses the
2163 enum list, enumvalues, and returns a enumeration type tree. */
2164
2165 tree
2166 m2type_BuildEndEnumeration (location_t location, tree enumtype,
2167 tree enumvalues)
2168 {
2169 tree finished ATTRIBUTE_UNUSED
2170 = gm2_finish_enum (location, enumtype, enumvalues);
2171 return enumtype;
2172 }
2173
2174 /* Build and install a CONST_DECL for one value of the current
2175 enumeration type (one that was begun with start_enum). Return a
2176 tree-list containing the CONST_DECL and its value. Assignment of
2177 sequential values by default is handled here. */
2178
2179 static tree
2180 gm2_build_enumerator (location_t location, tree name, tree value)
2181 {
2182 tree decl, type;
2183
2184 m2assert_AssertLocation (location);
2185 /* Remove no-op casts from the value. */
2186 if (value)
2187 STRIP_TYPE_NOPS (value);
2188
2189 /* Now create a declaration for the enum value name. */
2190
2191 type = TREE_TYPE (value);
2192
2193 decl = build_decl (location, CONST_DECL, name, type);
2194 DECL_INITIAL (decl) = convert (type, value);
2195 m2block_pushDecl (decl);
2196
2197 return tree_cons (decl, value, NULL_TREE);
2198 }
2199
2200 /* BuildEnumerator build an enumerator and add it to the,
2201 enumvalues, list. It returns a copy of the value. */
2202
2203 tree
2204 m2type_BuildEnumerator (location_t location, char *name, tree value,
2205 tree *enumvalues)
2206 {
2207 tree id = get_identifier (name);
2208 tree copy_of_value = copy_node (value);
2209 tree gccenum = gm2_build_enumerator (location, id, copy_of_value);
2210
2211 m2assert_AssertLocation (location);
2212 /* Choose copy_of_value for enum value. */
2213 *enumvalues = chainon (gccenum, *enumvalues);
2214 return copy_of_value;
2215 }
2216
2217 /* BuildPointerType returns a type which is a pointer to, totype. */
2218
2219 tree
2220 m2type_BuildPointerType (tree totype)
2221 {
2222 return build_pointer_type (m2tree_skip_type_decl (totype));
2223 }
2224
2225 /* BuildConstPointerType returns a type which is a const pointer
2226 to, totype. */
2227
2228 tree
2229 m2type_BuildConstPointerType (tree totype)
2230 {
2231 tree t = build_pointer_type (m2tree_skip_type_decl (totype));
2232 TYPE_READONLY (t) = true;
2233 return t;
2234 }
2235
2236 /* BuildSetType creates a SET OF [lowval..highval]. */
2237
2238 tree
2239 m2type_BuildSetType (location_t location, char *name, tree type, tree lowval,
2240 tree highval, bool ispacked)
2241 {
2242 tree range = build_range_type (m2tree_skip_type_decl (type),
2243 m2expr_FoldAndStrip (lowval),
2244 m2expr_FoldAndStrip (highval));
2245
2246 TYPE_PACKED (range) = ispacked;
2247 m2assert_AssertLocation (location);
2248 return m2type_BuildSetTypeFromSubrange (location, name, range,
2249 m2expr_FoldAndStrip (lowval),
2250 m2expr_FoldAndStrip (highval),
2251 ispacked);
2252 }
2253
2254 /* push_constructor returns a new compound constructor frame. */
2255
2256 static struct struct_constructor *
2257 push_constructor (void)
2258 {
2259 struct struct_constructor *p = ggc_alloc<struct_constructor> ();
2260
2261 p->level = top_constructor;
2262 top_constructor = p;
2263 return p;
2264 }
2265
2266 /* pop_constructor throws away the top constructor frame on the
2267 stack. */
2268
2269 static void
2270 pop_constructor (struct struct_constructor *p)
2271 {
2272 ASSERT_CONDITION (p
2273 == top_constructor); /* p should be the top_constructor. */
2274 top_constructor = top_constructor->level;
2275 }
2276
2277 /* BuildStartSetConstructor starts to create a set constant.
2278 Remember that type is really a record type. */
2279
2280 void *
2281 m2type_BuildStartSetConstructor (tree type)
2282 {
2283 struct struct_constructor *p = push_constructor ();
2284
2285 type = m2tree_skip_type_decl (type);
2286 layout_type (type);
2287 p->constructor_type = type;
2288 p->constructor_fields = TYPE_FIELDS (type);
2289 p->constructor_element_list = NULL_TREE;
2290 vec_alloc (p->constructor_elements, 1);
2291 return (void *)p;
2292 }
2293
2294 /* BuildSetConstructorElement adds, value, to the
2295 constructor_element_list. */
2296
2297 void
2298 m2type_BuildSetConstructorElement (void *p, tree value)
2299 {
2300 struct struct_constructor *c = (struct struct_constructor *)p;
2301
2302 if (value == NULL_TREE)
2303 {
2304 internal_error ("set type cannot be initialized with a %qs",
2305 "NULL_TREE");
2306 return;
2307 }
2308
2309 if (c->constructor_fields == NULL)
2310 {
2311 internal_error ("set type does not take another integer value");
2312 return;
2313 }
2314
2315 c->constructor_element_list
2316 = tree_cons (c->constructor_fields, value, c->constructor_element_list);
2317 c->constructor_fields = TREE_CHAIN (c->constructor_fields);
2318 }
2319
2320 /* BuildEndSetConstructor finishes building a set constant. */
2321
2322 tree
2323 m2type_BuildEndSetConstructor (void *p)
2324 {
2325 tree constructor;
2326 tree link;
2327 struct struct_constructor *c = (struct struct_constructor *)p;
2328
2329 for (link = c->constructor_element_list; link; link = TREE_CHAIN (link))
2330 {
2331 tree field = TREE_PURPOSE (link);
2332 DECL_SIZE (field) = bitsize_int (SET_WORD_SIZE);
2333 DECL_BIT_FIELD (field) = 1;
2334 }
2335
2336 constructor = build_constructor_from_list (
2337 c->constructor_type, nreverse (c->constructor_element_list));
2338 TREE_CONSTANT (constructor) = 1;
2339 TREE_STATIC (constructor) = 1;
2340
2341 pop_constructor (c);
2342
2343 return constructor;
2344 }
2345
2346 /* BuildStartRecordConstructor initializes a record compound
2347 constructor frame. */
2348
2349 void *
2350 m2type_BuildStartRecordConstructor (tree type)
2351 {
2352 struct struct_constructor *p = push_constructor ();
2353
2354 type = m2tree_skip_type_decl (type);
2355 layout_type (type);
2356 p->constructor_type = type;
2357 p->constructor_fields = TYPE_FIELDS (type);
2358 p->constructor_element_list = NULL_TREE;
2359 vec_alloc (p->constructor_elements, 1);
2360 return (void *)p;
2361 }
2362
2363 /* BuildEndRecordConstructor returns a tree containing the record
2364 compound literal. */
2365
2366 tree
2367 m2type_BuildEndRecordConstructor (void *p)
2368 {
2369 struct struct_constructor *c = (struct struct_constructor *)p;
2370 tree constructor = build_constructor_from_list (
2371 c->constructor_type, nreverse (c->constructor_element_list));
2372 TREE_CONSTANT (constructor) = 1;
2373 TREE_STATIC (constructor) = 1;
2374
2375 pop_constructor (c);
2376
2377 return constructor;
2378 }
2379
2380 /* BuildRecordConstructorElement adds, value, to the
2381 constructor_element_list. */
2382
2383 void
2384 m2type_BuildRecordConstructorElement (void *p, tree value)
2385 {
2386 m2type_BuildSetConstructorElement (p, value);
2387 }
2388
2389 /* BuildStartArrayConstructor initializes an array compound
2390 constructor frame. */
2391
2392 void *
2393 m2type_BuildStartArrayConstructor (tree type)
2394 {
2395 struct struct_constructor *p = push_constructor ();
2396
2397 type = m2tree_skip_type_decl (type);
2398 layout_type (type);
2399 p->constructor_type = type;
2400 p->constructor_fields = TREE_TYPE (type);
2401 p->constructor_element_list = NULL_TREE;
2402 vec_alloc (p->constructor_elements, 1);
2403 return (void *)p;
2404 }
2405
2406 /* BuildEndArrayConstructor returns a tree containing the array
2407 compound literal. */
2408
2409 tree
2410 m2type_BuildEndArrayConstructor (void *p)
2411 {
2412 struct struct_constructor *c = (struct struct_constructor *)p;
2413 tree constructor;
2414
2415 constructor
2416 = build_constructor (c->constructor_type, c->constructor_elements);
2417 TREE_CONSTANT (constructor) = true;
2418 TREE_STATIC (constructor) = true;
2419
2420 pop_constructor (c);
2421
2422 return constructor;
2423 }
2424
2425 /* BuildArrayConstructorElement adds, value, to the
2426 constructor_element_list. */
2427
2428 void
2429 m2type_BuildArrayConstructorElement (void *p, tree value, tree indice)
2430 {
2431 struct struct_constructor *c = (struct struct_constructor *)p;
2432 constructor_elt celt;
2433
2434 if (value == NULL_TREE)
2435 {
2436 internal_error ("array cannot be initialized with a %qs", "NULL_TREE");
2437 return;
2438 }
2439
2440 if (c->constructor_fields == NULL_TREE)
2441 {
2442 internal_error ("array type must be initialized");
2443 return;
2444 }
2445
2446 if (c->constructor_fields != TREE_TYPE (value))
2447 {
2448 internal_error (
2449 "array element value must be the same type as its declaration");
2450 return;
2451 }
2452
2453 celt.index = indice;
2454 celt.value = value;
2455 vec_safe_push (c->constructor_elements, celt);
2456 }
2457
2458 /* BuildArrayStringConstructor creates an array constructor for,
2459 arrayType, consisting of the character elements defined by, str,
2460 of, length, characters. */
2461
2462 tree
2463 m2type_BuildArrayStringConstructor (location_t location, tree arrayType,
2464 tree str, tree length)
2465 {
2466 tree n;
2467 tree val;
2468 int i = 0;
2469 const char *p = TREE_STRING_POINTER (str);
2470 tree type = m2tree_skip_type_decl (TREE_TYPE (arrayType));
2471 struct struct_constructor *c
2472 = (struct struct_constructor *)m2type_BuildStartArrayConstructor (
2473 arrayType);
2474 char nul[1];
2475 int len = strlen (p);
2476
2477 nul[0] = (char)0;
2478
2479 m2assert_AssertLocation (location);
2480 n = m2expr_GetIntegerZero (location);
2481 while (m2expr_CompareTrees (n, length) < 0)
2482 {
2483 if (i < len)
2484 val = m2convert_BuildConvert (
2485 location, type, m2type_BuildCharConstant (location, &p[i]), false);
2486 else
2487 val = m2type_BuildCharConstant (location, &nul[0]);
2488 m2type_BuildArrayConstructorElement (c, val, n);
2489 i += 1;
2490 n = m2expr_BuildAdd (location, n, m2expr_GetIntegerOne (location),
2491 false);
2492 }
2493 return m2type_BuildEndArrayConstructor (c);
2494 }
2495
2496 /* BuildSubrangeType creates a subrange of, type, with, lowval,
2497 highval. */
2498
2499 tree
2500 m2type_BuildSubrangeType (location_t location, char *name, tree type,
2501 tree lowval, tree highval)
2502 {
2503 tree range_type;
2504
2505 m2assert_AssertLocation (location);
2506 type = m2tree_skip_type_decl (type);
2507
2508 lowval = m2expr_FoldAndStrip (lowval);
2509 highval = m2expr_FoldAndStrip (highval);
2510
2511 if (m2expr_TreeOverflow (lowval))
2512 error ("low bound for the subrange has overflowed");
2513 if (m2expr_TreeOverflow (highval))
2514 error ("high bound for the subrange has overflowed");
2515
2516 /* First build a type with the base range. */
2517 range_type = build_range_type (type, lowval, highval);
2518
2519 TYPE_UNSIGNED (range_type) = TYPE_UNSIGNED (type);
2520 #if 0
2521 /* Then set the actual range. */
2522 SET_TYPE_RM_MIN_VALUE (range_type, lowval);
2523 SET_TYPE_RM_MAX_VALUE (range_type, highval);
2524 #endif
2525
2526 if ((name != NULL) && (strcmp (name, "") != 0))
2527 {
2528 /* Declared as TYPE foo = [x..y]; */
2529 range_type = m2type_DeclareKnownType (location, name, range_type);
2530 layout_type (m2tree_skip_type_decl (range_type));
2531 }
2532
2533 return range_type;
2534 }
2535
2536 /* BuildCharConstantChar creates a character constant given a character, ch. */
2537
2538 tree
2539 m2type_BuildCharConstantChar (location_t location, char ch)
2540 {
2541 tree id = build_int_cst (char_type_node, (int) ch);
2542 id = m2convert_BuildConvert (location, m2type_GetM2CharType (), id, false);
2543 return m2block_RememberConstant (id);
2544 }
2545
2546 /* BuildCharConstant creates a character constant given a, string. */
2547
2548 tree
2549 m2type_BuildCharConstant (location_t location, const char *string)
2550 {
2551 return m2type_BuildCharConstantChar (location, string[0]);
2552 }
2553
2554 /* RealToTree convert a real number into a Tree. */
2555
2556 tree
2557 m2type_RealToTree (char *name)
2558 {
2559 return build_real (
2560 m2type_GetLongRealType (),
2561 REAL_VALUE_ATOF (name, TYPE_MODE (m2type_GetLongRealType ())));
2562 }
2563
2564 /* gm2_start_struct start to create a struct. */
2565
2566 static tree
2567 gm2_start_struct (location_t location, enum tree_code code, char *name)
2568 {
2569 tree s = make_node (code);
2570 tree id;
2571
2572 m2assert_AssertLocation (location);
2573 if ((name == NULL) || (strcmp (name, "") == 0))
2574 id = NULL_TREE;
2575 else
2576 id = get_identifier (name);
2577
2578 /* This maybe set true later if necessary. */
2579 TYPE_PACKED (s) = false;
2580
2581 m2block_pushDecl (build_decl (location, TYPE_DECL, id, s));
2582 return s;
2583 }
2584
2585 /* BuildStartRecord return a RECORD tree. */
2586
2587 tree
2588 m2type_BuildStartRecord (location_t location, char *name)
2589 {
2590 m2assert_AssertLocation (location);
2591 return gm2_start_struct (location, RECORD_TYPE, name);
2592 }
2593
2594 /* BuildStartUnion return a union tree. */
2595
2596 tree
2597 m2type_BuildStartUnion (location_t location, char *name)
2598 {
2599 m2assert_AssertLocation (location);
2600 return gm2_start_struct (location, UNION_TYPE, name);
2601 }
2602
2603 /* m2type_BuildStartVarient builds a varient record. It creates a
2604 record field which has a, name, and whose type is a union. */
2605
2606 tree
2607 m2type_BuildStartVarient (location_t location, char *name)
2608 {
2609 tree varient = m2type_BuildStartUnion (location, name);
2610 tree field = m2type_BuildStartFieldRecord (location, name, varient);
2611 m2assert_AssertLocation (location);
2612 return field;
2613 }
2614
2615 /* m2type_BuildEndVarient finish the varientField by calling
2616 decl_finish and also finish the type of varientField (which is a
2617 union). */
2618
2619 tree
2620 m2type_BuildEndVarient (location_t location, tree varientField,
2621 tree varientList, bool isPacked)
2622 {
2623 tree varient = TREE_TYPE (varientField);
2624 m2assert_AssertLocation (location);
2625 varient = m2type_BuildEndRecord (location, varient, varientList, isPacked);
2626 gm2_finish_decl (location, varientField);
2627 return varientField;
2628 }
2629
2630 /* m2type_BuildStartFieldVarient builds a field varient record. It
2631 creates a record field which has a, name, and whose type is a
2632 record. */
2633
2634 tree
2635 m2type_BuildStartFieldVarient (location_t location, char *name)
2636 {
2637 tree record = m2type_BuildStartRecord (location, name);
2638 tree field = m2type_BuildStartFieldRecord (location, name, record);
2639 m2assert_AssertLocation (location);
2640 return field;
2641 }
2642
2643 /* BuildEndRecord a heavily pruned finish_struct from c-decl.cc. It
2644 sets the context for each field to, t, propagates isPacked
2645 throughout the fields in the structure. */
2646
2647 tree
2648 m2type_BuildEndRecord (location_t location, tree record, tree fieldlist,
2649 bool isPacked)
2650 {
2651 tree x, d;
2652
2653 m2assert_AssertLocation (location);
2654
2655 /* If this type was previously laid out as a forward reference, make
2656 sure we lay it out again. */
2657
2658 TYPE_SIZE (record) = 0;
2659
2660 /* Install struct as DECL_CONTEXT of each field decl. Also process
2661 specified field sizes, found in the DECL_INITIAL, storing 0 there
2662 after the type has been changed to precision equal to its width,
2663 rather than the precision of the specified standard type. (Correct
2664 layout requires the original type to have been preserved until now). */
2665
2666 for (x = fieldlist; x; x = TREE_CHAIN (x))
2667 {
2668 DECL_CONTEXT (x) = record;
2669
2670 if (TYPE_PACKED (record) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
2671 DECL_PACKED (x) = 1;
2672
2673 if (isPacked)
2674 {
2675 DECL_PACKED (x) = 1;
2676 DECL_BIT_FIELD (x) = 1;
2677 }
2678 }
2679
2680 /* Now we have the nearly final fieldlist. Record it, then lay out
2681 the structure or union (including the fields). */
2682
2683 TYPE_FIELDS (record) = fieldlist;
2684 layout_type (record);
2685
2686 /* Now we have the truly final field list. Store it in this type and
2687 in the variants. */
2688
2689 for (x = TYPE_MAIN_VARIANT (record); x; x = TYPE_NEXT_VARIANT (x))
2690 {
2691 TYPE_FIELDS (x) = TYPE_FIELDS (record);
2692 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (record);
2693 SET_TYPE_ALIGN (x, TYPE_ALIGN (record));
2694 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (record);
2695 }
2696
2697 d = build_decl (location, TYPE_DECL, NULL, record);
2698 TYPE_STUB_DECL (record) = d;
2699
2700 /* Finish debugging output for this type. This must be done after we have
2701 called build_decl. */
2702 rest_of_type_compilation (record, m2block_toplevel ());
2703
2704 return record;
2705 }
2706
2707 /* m2type_BuildEndFieldVarient finish the varientField by calling
2708 decl_finish and also finish the type of varientField (which is a
2709 record). */
2710
2711 tree
2712 m2type_BuildEndFieldVarient (location_t location, tree varientField,
2713 tree varientList, bool isPacked)
2714 {
2715 tree record = TREE_TYPE (varientField);
2716
2717 m2assert_AssertLocation (location);
2718 record = m2type_BuildEndRecord (location, record, varientList, isPacked);
2719 gm2_finish_decl (location, varientField);
2720 return varientField;
2721 }
2722
2723 /* m2type_BuildStartFieldRecord starts building a field record. It
2724 returns the field which must be completed by calling
2725 gm2_finish_decl. */
2726
2727 tree
2728 m2type_BuildStartFieldRecord (location_t location, char *name, tree type)
2729 {
2730 tree field, declarator;
2731
2732 m2assert_AssertLocation (location);
2733 if ((name == NULL) || (strcmp (name, "") == 0))
2734 declarator = NULL_TREE;
2735 else
2736 declarator = get_identifier (name);
2737
2738 field = build_decl (location, FIELD_DECL, declarator,
2739 m2tree_skip_type_decl (type));
2740 return field;
2741 }
2742
2743 /* Build a record field with name (name maybe NULL), returning the
2744 new field declaration, FIELD_DECL.
2745
2746 This is done during the parsing of the struct declaration. The
2747 FIELD_DECL nodes are chained together and the lot of them are
2748 ultimately passed to `build_struct' to make the RECORD_TYPE node. */
2749
2750 tree
2751 m2type_BuildFieldRecord (location_t location, char *name, tree type)
2752 {
2753 tree field = m2type_BuildStartFieldRecord (location, name, type);
2754
2755 m2assert_AssertLocation (location);
2756 gm2_finish_decl (location, field);
2757 return field;
2758 }
2759
2760 /* ChainOn interface so that Modula-2 can also create chains of
2761 declarations. */
2762
2763 tree
2764 m2type_ChainOn (tree t1, tree t2)
2765 {
2766 return chainon (t1, t2);
2767 }
2768
2769 /* ChainOnParamValue adds a list node {{name, str}, value} into the
2770 tree list. */
2771
2772 tree
2773 m2type_ChainOnParamValue (tree list, tree name, tree str, tree value)
2774 {
2775 return chainon (list, build_tree_list (build_tree_list (name, str), value));
2776 }
2777
2778 /* AddStringToTreeList adds, string, to list. */
2779
2780 tree
2781 m2type_AddStringToTreeList (tree list, tree string)
2782 {
2783 return tree_cons (NULL_TREE, string, list);
2784 }
2785
2786 /* SetAlignment sets the alignment of a, node, to, align. It
2787 duplicates the, node, and sets the alignment to prevent alignment
2788 effecting behaviour elsewhere. */
2789
2790 tree
2791 m2type_SetAlignment (tree node, tree align)
2792 {
2793 tree type = NULL_TREE;
2794 tree decl = NULL_TREE;
2795 bool is_type = false;
2796 int i;
2797
2798 if (DECL_P (node))
2799 {
2800 decl = node;
2801 is_type = (TREE_CODE (node) == TYPE_DECL);
2802 type = TREE_TYPE (decl);
2803 }
2804 else if (TYPE_P (node))
2805 {
2806 is_type = true;
2807 type = node;
2808 }
2809
2810 if (TREE_CODE (align) != INTEGER_CST)
2811 error ("requested alignment is not a constant");
2812 else if ((i = tree_log2 (align)) == -1)
2813 error ("requested alignment is not a power of 2");
2814 else if (i > HOST_BITS_PER_INT - 2)
2815 error ("requested alignment is too large");
2816 else if (is_type)
2817 {
2818 /* If we have a TYPE_DECL, then copy the type, so that we don't
2819 accidentally modify a builtin type. See pushdecl. */
2820 if (decl && TREE_TYPE (decl) != error_mark_node
2821 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
2822 {
2823 tree tt = TREE_TYPE (decl);
2824 type = build_variant_type_copy (type);
2825 DECL_ORIGINAL_TYPE (decl) = tt;
2826 TYPE_NAME (type) = decl;
2827 TREE_USED (type) = TREE_USED (decl);
2828 TREE_TYPE (decl) = type;
2829 }
2830
2831 SET_TYPE_ALIGN (type, (1 << i) * BITS_PER_UNIT);
2832 TYPE_USER_ALIGN (type) = 1;
2833
2834 if (decl)
2835 {
2836 SET_DECL_ALIGN (decl, (1 << i) * BITS_PER_UNIT);
2837 DECL_USER_ALIGN (decl) = 1;
2838 }
2839 }
2840 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FIELD_DECL)
2841 error ("alignment may not be specified for %qD", decl);
2842 else
2843 {
2844 SET_DECL_ALIGN (decl, (1 << i) * BITS_PER_UNIT);
2845 DECL_USER_ALIGN (decl) = 1;
2846 }
2847 return node;
2848 }
2849
2850 /* SetDeclPacked sets the packed bit in decl TREE, node. It
2851 returns the node. */
2852
2853 tree
2854 m2type_SetDeclPacked (tree node)
2855 {
2856 DECL_PACKED (node) = 1;
2857 return node;
2858 }
2859
2860 /* SetTypePacked sets the packed bit in type TREE, node. It
2861 returns the node. */
2862
2863 tree
2864 m2type_SetTypePacked (tree node)
2865 {
2866 TYPE_PACKED (node) = 1;
2867 return node;
2868 }
2869
2870 /* SetRecordFieldOffset returns field after the byteOffset and
2871 bitOffset has been applied to it. */
2872
2873 tree
2874 m2type_SetRecordFieldOffset (tree field, tree byteOffset, tree bitOffset,
2875 tree fieldtype, tree nbits)
2876 {
2877 DECL_FIELD_OFFSET (field) = byteOffset;
2878 DECL_FIELD_BIT_OFFSET (field) = bitOffset;
2879 TREE_TYPE (field) = m2tree_skip_type_decl (fieldtype);
2880 DECL_SIZE (field) = bitsize_int (TREE_INT_CST_LOW (nbits));
2881 return field;
2882 }
2883
2884 /* BuildPackedFieldRecord builds a packed field record of, name,
2885 and, fieldtype. */
2886
2887 tree
2888 m2type_BuildPackedFieldRecord (location_t location, char *name, tree fieldtype)
2889 {
2890 m2assert_AssertLocation (location);
2891 return m2type_BuildFieldRecord (location, name, fieldtype);
2892 }
2893
2894 /* BuildNumberOfArrayElements returns the number of elements in an
2895 arrayType. */
2896
2897 tree
2898 m2type_BuildNumberOfArrayElements (location_t location, tree arrayType)
2899 {
2900 tree index = TYPE_DOMAIN (arrayType);
2901 tree high = TYPE_MAX_VALUE (index);
2902 tree low = TYPE_MIN_VALUE (index);
2903 tree elements = m2expr_BuildAdd (
2904 location, m2expr_BuildSub (location, high, low, false),
2905 m2expr_GetIntegerOne (location), false);
2906 m2assert_AssertLocation (location);
2907 return elements;
2908 }
2909
2910 /* AddStatement maps onto add_stmt. */
2911
2912 void
2913 m2type_AddStatement (location_t location, tree t)
2914 {
2915 if (t != NULL_TREE)
2916 add_stmt (location, t);
2917 }
2918
2919 /* MarkFunctionReferenced marks a function as referenced. */
2920
2921 void
2922 m2type_MarkFunctionReferenced (tree f)
2923 {
2924 if (f != NULL_TREE)
2925 if (TREE_CODE (f) == FUNCTION_DECL)
2926 mark_decl_referenced (f);
2927 }
2928
2929 /* GarbageCollect force gcc to garbage collect. */
2930
2931 void
2932 m2type_GarbageCollect (void)
2933 {
2934 ggc_collect ();
2935 }
2936
2937 /* gm2_type_for_size return an integer type with BITS bits of
2938 precision, that is unsigned if UNSIGNEDP is nonzero, otherwise
2939 signed. */
2940
2941 tree
2942 m2type_gm2_type_for_size (unsigned int bits, bool unsignedp)
2943 {
2944 if (bits == TYPE_PRECISION (integer_type_node))
2945 return unsignedp ? unsigned_type_node : integer_type_node;
2946
2947 if (bits == TYPE_PRECISION (signed_char_type_node))
2948 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2949
2950 if (bits == TYPE_PRECISION (short_integer_type_node))
2951 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2952
2953 if (bits == TYPE_PRECISION (long_integer_type_node))
2954 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2955
2956 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2957 return (unsignedp ? long_long_unsigned_type_node
2958 : long_long_integer_type_node);
2959
2960 if (bits <= TYPE_PRECISION (intQI_type_node))
2961 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2962
2963 if (bits <= TYPE_PRECISION (intHI_type_node))
2964 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2965
2966 if (bits <= TYPE_PRECISION (intSI_type_node))
2967 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2968
2969 if (bits <= TYPE_PRECISION (intDI_type_node))
2970 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2971
2972 return 0;
2973 }
2974
2975 /* gm2_unsigned_type return an unsigned type the same as TYPE in
2976 other respects. */
2977
2978 tree
2979 m2type_gm2_unsigned_type (tree type)
2980 {
2981 tree type1 = TYPE_MAIN_VARIANT (type);
2982 if (type1 == signed_char_type_node || type1 == char_type_node)
2983 return unsigned_char_type_node;
2984 if (type1 == integer_type_node)
2985 return unsigned_type_node;
2986 if (type1 == short_integer_type_node)
2987 return short_unsigned_type_node;
2988 if (type1 == long_integer_type_node)
2989 return long_unsigned_type_node;
2990 if (type1 == long_long_integer_type_node)
2991 return long_long_unsigned_type_node;
2992
2993 #if HOST_BITS_PER_WIDE_INT >= 64
2994 if (type1 == intTI_type_node)
2995 return unsigned_intTI_type_node;
2996 #endif
2997 if (type1 == intDI_type_node)
2998 return unsigned_intDI_type_node;
2999 if (type1 == intSI_type_node)
3000 return unsigned_intSI_type_node;
3001 if (type1 == intHI_type_node)
3002 return unsigned_intHI_type_node;
3003 if (type1 == intQI_type_node)
3004 return unsigned_intQI_type_node;
3005
3006 return m2type_gm2_signed_or_unsigned_type (true, type);
3007 }
3008
3009 /* gm2_signed_type return a signed type the same as TYPE in other
3010 respects. */
3011
3012 tree
3013 m2type_gm2_signed_type (tree type)
3014 {
3015 tree type1 = TYPE_MAIN_VARIANT (type);
3016 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3017 return signed_char_type_node;
3018 if (type1 == unsigned_type_node)
3019 return integer_type_node;
3020 if (type1 == short_unsigned_type_node)
3021 return short_integer_type_node;
3022 if (type1 == long_unsigned_type_node)
3023 return long_integer_type_node;
3024 if (type1 == long_long_unsigned_type_node)
3025 return long_long_integer_type_node;
3026
3027 #if HOST_BITS_PER_WIDE_INT >= 64
3028 if (type1 == unsigned_intTI_type_node)
3029 return intTI_type_node;
3030 #endif
3031 if (type1 == unsigned_intDI_type_node)
3032 return intDI_type_node;
3033 if (type1 == unsigned_intSI_type_node)
3034 return intSI_type_node;
3035 if (type1 == unsigned_intHI_type_node)
3036 return intHI_type_node;
3037 if (type1 == unsigned_intQI_type_node)
3038 return intQI_type_node;
3039
3040 return m2type_gm2_signed_or_unsigned_type (false, type);
3041 }
3042
3043 /* check_type if the precision of baseType and type are the same
3044 then return true and set the signed or unsigned type in result
3045 else return false. */
3046
3047 static int
3048 check_type (tree baseType, tree type, int unsignedp, tree baseu, tree bases,
3049 tree *result)
3050 {
3051 if (TYPE_PRECISION (baseType) == TYPE_PRECISION (type))
3052 {
3053 if (unsignedp)
3054 *result = baseu;
3055 else
3056 *result = bases;
3057 return true;
3058 }
3059 return false;
3060 }
3061
3062 /* gm2_signed_or_unsigned_type return a type the same as TYPE
3063 except unsigned or signed according to UNSIGNEDP. */
3064
3065 tree
3066 m2type_gm2_signed_or_unsigned_type (int unsignedp, tree type)
3067 {
3068 tree result;
3069
3070 if (!INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
3071 return type;
3072
3073 /* For INTEGER_TYPEs we must check the precision as well, so as to
3074 yield correct results for bit-field types. */
3075
3076 if (check_type (signed_char_type_node, type, unsignedp,
3077 unsigned_char_type_node, signed_char_type_node, &result))
3078 return result;
3079 if (check_type (integer_type_node, type, unsignedp, unsigned_type_node,
3080 integer_type_node, &result))
3081 return result;
3082 if (check_type (short_integer_type_node, type, unsignedp,
3083 short_unsigned_type_node, short_integer_type_node, &result))
3084 return result;
3085 if (check_type (long_integer_type_node, type, unsignedp,
3086 long_unsigned_type_node, long_integer_type_node, &result))
3087 return result;
3088 if (check_type (long_long_integer_type_node, type, unsignedp,
3089 long_long_unsigned_type_node, long_long_integer_type_node,
3090 &result))
3091 return result;
3092
3093 #if HOST_BITS_PER_WIDE_INT >= 64
3094 if (check_type (intTI_type_node, type, unsignedp, unsigned_intTI_type_node,
3095 intTI_type_node, &result))
3096 return result;
3097 #endif
3098 if (check_type (intDI_type_node, type, unsignedp, unsigned_intDI_type_node,
3099 intDI_type_node, &result))
3100 return result;
3101 if (check_type (intSI_type_node, type, unsignedp, unsigned_intSI_type_node,
3102 intSI_type_node, &result))
3103 return result;
3104 if (check_type (intHI_type_node, type, unsignedp, unsigned_intHI_type_node,
3105 intHI_type_node, &result))
3106 return result;
3107 if (check_type (intQI_type_node, type, unsignedp, unsigned_intQI_type_node,
3108 intQI_type_node, &result))
3109 return result;
3110 #undef TYPE_OK
3111
3112 return type;
3113 }
3114
3115 /* IsAddress returns true if the type is an ADDRESS. */
3116
3117 bool
3118 m2type_IsAddress (tree type)
3119 {
3120 return type == ptr_type_node;
3121 }
3122
3123 /* check_record_fields return true if all the fields in left and right
3124 are GCC equivalent. */
3125
3126 static
3127 bool
3128 check_record_fields (tree left, tree right)
3129 {
3130 unsigned int i;
3131 tree right_value;
3132 vec<constructor_elt, va_gc> *values = CONSTRUCTOR_ELTS (right);
3133 FOR_EACH_CONSTRUCTOR_VALUE (values, i, right_value)
3134 {
3135 tree left_field = TREE_TYPE (m2treelib_get_field_no (left, NULL_TREE, false, i));
3136 if (! m2type_IsGccStrictTypeEquivalent (left_field, right_value))
3137 return false;
3138 }
3139 return true;
3140 }
3141
3142 /* check_array_types return true if left and right have the same type and right
3143 is not a CST_STRING. */
3144
3145 static
3146 bool
3147 check_array_types (tree right)
3148 {
3149 unsigned int i;
3150 tree value;
3151 vec<constructor_elt, va_gc> *values = CONSTRUCTOR_ELTS (right);
3152 FOR_EACH_CONSTRUCTOR_VALUE (values, i, value)
3153 {
3154 enum tree_code right_code = TREE_CODE (value);
3155 if (right_code == STRING_CST)
3156 return false;
3157 }
3158 return true;
3159 }
3160
3161 bool
3162 m2type_IsGccStrictTypeEquivalent (tree left, tree right)
3163 {
3164 enum tree_code right_code = TREE_CODE (right);
3165 enum tree_code left_code = TREE_CODE (left);
3166 if (left_code == VAR_DECL)
3167 return m2type_IsGccStrictTypeEquivalent (TREE_TYPE (left), right);
3168 if (right_code == VAR_DECL)
3169 return m2type_IsGccStrictTypeEquivalent (left, TREE_TYPE (right));
3170 if (left_code == RECORD_TYPE && right_code == CONSTRUCTOR)
3171 return check_record_fields (left, right);
3172 if (left_code == UNION_TYPE && right_code == CONSTRUCTOR)
3173 return false;
3174 if (left_code == ARRAY_TYPE && right_code == CONSTRUCTOR)
3175 return check_array_types (right);
3176 if (right_code == STRING_CST)
3177 return false;
3178 return true;
3179 }
3180
3181 #include "gt-m2-m2type.h"