re PR c++/13975 (ICE on misplaced visibility specifier.)
[gcc.git] / gcc / tree.c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
28
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
31
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "real.h"
39 #include "tm_p.h"
40 #include "function.h"
41 #include "obstack.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "hashtab.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
48
49 /* obstack.[ch] explicitly declined to prototype this. */
50 extern int _obstack_allocated_p (struct obstack *h, void *obj);
51
52 #ifdef GATHER_STATISTICS
53 /* Statistics-gathering stuff. */
54
55 int tree_node_counts[(int) all_kinds];
56 int tree_node_sizes[(int) all_kinds];
57
58 /* Keep in sync with tree.h:enum tree_node_kind. */
59 static const char * const tree_node_kind_names[] = {
60 "decls",
61 "types",
62 "blocks",
63 "stmts",
64 "refs",
65 "exprs",
66 "constants",
67 "identifiers",
68 "perm_tree_lists",
69 "temp_tree_lists",
70 "vecs",
71 "random kinds",
72 "lang_decl kinds",
73 "lang_type kinds"
74 };
75 #endif /* GATHER_STATISTICS */
76
77 /* Unique id for next decl created. */
78 static GTY(()) int next_decl_uid;
79 /* Unique id for next type created. */
80 static GTY(()) int next_type_uid = 1;
81
82 /* Since we cannot rehash a type after it is in the table, we have to
83 keep the hash code. */
84
85 struct type_hash GTY(())
86 {
87 unsigned long hash;
88 tree type;
89 };
90
91 /* Initial size of the hash table (rounded to next prime). */
92 #define TYPE_HASH_INITIAL_SIZE 1000
93
94 /* Now here is the hash table. When recording a type, it is added to
95 the slot whose index is the hash code. Note that the hash table is
96 used for several kinds of types (function types, array types and
97 array index range types, for now). While all these live in the
98 same table, they are completely independent, and the hash code is
99 computed differently for each of these. */
100
101 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
102 htab_t type_hash_table;
103
104 static void set_type_quals (tree, int);
105 static int type_hash_eq (const void *, const void *);
106 static hashval_t type_hash_hash (const void *);
107 static void print_type_hash_statistics (void);
108 static void finish_vector_type (tree);
109 static tree make_vector (enum machine_mode, tree, int);
110 static int type_hash_marked_p (const void *);
111
112 tree global_trees[TI_MAX];
113 tree integer_types[itk_none];
114 \f
115 /* Init tree.c. */
116
117 void
118 init_ttree (void)
119 {
120 /* Initialize the hash table of types. */
121 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
122 type_hash_eq, 0);
123 }
124
125 \f
126 /* The name of the object as the assembler will see it (but before any
127 translations made by ASM_OUTPUT_LABELREF). Often this is the same
128 as DECL_NAME. It is an IDENTIFIER_NODE. */
129 tree
130 decl_assembler_name (tree decl)
131 {
132 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
133 (*lang_hooks.set_decl_assembler_name) (decl);
134 return DECL_CHECK (decl)->decl.assembler_name;
135 }
136
137 /* Compute the number of bytes occupied by 'node'. This routine only
138 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
139 size_t
140 tree_size (tree node)
141 {
142 enum tree_code code = TREE_CODE (node);
143
144 switch (TREE_CODE_CLASS (code))
145 {
146 case 'd': /* A decl node */
147 return sizeof (struct tree_decl);
148
149 case 't': /* a type node */
150 return sizeof (struct tree_type);
151
152 case 'b': /* a lexical block node */
153 return sizeof (struct tree_block);
154
155 case 'r': /* a reference */
156 case 'e': /* an expression */
157 case 's': /* an expression with side effects */
158 case '<': /* a comparison expression */
159 case '1': /* a unary arithmetic expression */
160 case '2': /* a binary arithmetic expression */
161 return (sizeof (struct tree_exp)
162 + TREE_CODE_LENGTH (code) * sizeof (char *) - sizeof (char *));
163
164 case 'c': /* a constant */
165 switch (code)
166 {
167 case INTEGER_CST: return sizeof (struct tree_int_cst);
168 case REAL_CST: return sizeof (struct tree_real_cst);
169 case COMPLEX_CST: return sizeof (struct tree_complex);
170 case VECTOR_CST: return sizeof (struct tree_vector);
171 case STRING_CST:
172 return sizeof (struct tree_string) + TREE_STRING_LENGTH (node);
173 default:
174 return (*lang_hooks.tree_size) (code);
175 }
176
177 case 'x': /* something random, like an identifier. */
178 switch (code)
179 {
180 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
181 case TREE_LIST: return sizeof (struct tree_list);
182 case TREE_VEC: return (sizeof (struct tree_vec)
183 + TREE_VEC_LENGTH(node) * sizeof(char *)
184 - sizeof (char *));
185
186 case ERROR_MARK:
187 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
188
189 default:
190 return (*lang_hooks.tree_size) (code);
191 }
192
193 default:
194 abort ();
195 }
196 }
197
198 /* Return a newly allocated node of code CODE.
199 For decl and type nodes, some other fields are initialized.
200 The rest of the node is initialized to zero.
201
202 Achoo! I got a code in the node. */
203
204 tree
205 make_node (enum tree_code code)
206 {
207 tree t;
208 int type = TREE_CODE_CLASS (code);
209 size_t length;
210 #ifdef GATHER_STATISTICS
211 tree_node_kind kind;
212 #endif
213 struct tree_common ttmp;
214
215 /* We can't allocate a TREE_VEC without knowing how many elements
216 it will have; likewise a STRING_CST without knowing the length. */
217 if (code == TREE_VEC || code == STRING_CST)
218 abort ();
219
220 TREE_SET_CODE ((tree)&ttmp, code);
221 length = tree_size ((tree)&ttmp);
222
223 #ifdef GATHER_STATISTICS
224 switch (type)
225 {
226 case 'd': /* A decl node */
227 kind = d_kind;
228 break;
229
230 case 't': /* a type node */
231 kind = t_kind;
232 break;
233
234 case 'b': /* a lexical block */
235 kind = b_kind;
236 break;
237
238 case 's': /* an expression with side effects */
239 kind = s_kind;
240 break;
241
242 case 'r': /* a reference */
243 kind = r_kind;
244 break;
245
246 case 'e': /* an expression */
247 case '<': /* a comparison expression */
248 case '1': /* a unary arithmetic expression */
249 case '2': /* a binary arithmetic expression */
250 kind = e_kind;
251 break;
252
253 case 'c': /* a constant */
254 kind = c_kind;
255 break;
256
257 case 'x': /* something random, like an identifier. */
258 if (code == IDENTIFIER_NODE)
259 kind = id_kind;
260 else if (code == TREE_VEC)
261 kind = vec_kind;
262 else
263 kind = x_kind;
264 break;
265
266 default:
267 abort ();
268 }
269
270 tree_node_counts[(int) kind]++;
271 tree_node_sizes[(int) kind] += length;
272 #endif
273
274 t = ggc_alloc_tree (length);
275
276 memset (t, 0, length);
277
278 TREE_SET_CODE (t, code);
279
280 switch (type)
281 {
282 case 's':
283 TREE_SIDE_EFFECTS (t) = 1;
284 break;
285
286 case 'd':
287 if (code != FUNCTION_DECL)
288 DECL_ALIGN (t) = 1;
289 DECL_USER_ALIGN (t) = 0;
290 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
291 DECL_SOURCE_LOCATION (t) = input_location;
292 DECL_UID (t) = next_decl_uid++;
293
294 /* We have not yet computed the alias set for this declaration. */
295 DECL_POINTER_ALIAS_SET (t) = -1;
296 break;
297
298 case 't':
299 TYPE_UID (t) = next_type_uid++;
300 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
301 TYPE_USER_ALIGN (t) = 0;
302 TYPE_MAIN_VARIANT (t) = t;
303
304 /* Default to no attributes for type, but let target change that. */
305 TYPE_ATTRIBUTES (t) = NULL_TREE;
306 (*targetm.set_default_type_attributes) (t);
307
308 /* We have not yet computed the alias set for this type. */
309 TYPE_ALIAS_SET (t) = -1;
310 break;
311
312 case 'c':
313 TREE_CONSTANT (t) = 1;
314 break;
315
316 case 'e':
317 switch (code)
318 {
319 case INIT_EXPR:
320 case MODIFY_EXPR:
321 case VA_ARG_EXPR:
322 case RTL_EXPR:
323 case PREDECREMENT_EXPR:
324 case PREINCREMENT_EXPR:
325 case POSTDECREMENT_EXPR:
326 case POSTINCREMENT_EXPR:
327 /* All of these have side-effects, no matter what their
328 operands are. */
329 TREE_SIDE_EFFECTS (t) = 1;
330 break;
331
332 default:
333 break;
334 }
335 break;
336 }
337
338 return t;
339 }
340 \f
341 /* Return a new node with the same contents as NODE except that its
342 TREE_CHAIN is zero and it has a fresh uid. */
343
344 tree
345 copy_node (tree node)
346 {
347 tree t;
348 enum tree_code code = TREE_CODE (node);
349 size_t length;
350
351 length = tree_size (node);
352 t = ggc_alloc_tree (length);
353 memcpy (t, node, length);
354
355 TREE_CHAIN (t) = 0;
356 TREE_ASM_WRITTEN (t) = 0;
357
358 if (TREE_CODE_CLASS (code) == 'd')
359 DECL_UID (t) = next_decl_uid++;
360 else if (TREE_CODE_CLASS (code) == 't')
361 {
362 TYPE_UID (t) = next_type_uid++;
363 /* The following is so that the debug code for
364 the copy is different from the original type.
365 The two statements usually duplicate each other
366 (because they clear fields of the same union),
367 but the optimizer should catch that. */
368 TYPE_SYMTAB_POINTER (t) = 0;
369 TYPE_SYMTAB_ADDRESS (t) = 0;
370 }
371
372 return t;
373 }
374
375 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
376 For example, this can copy a list made of TREE_LIST nodes. */
377
378 tree
379 copy_list (tree list)
380 {
381 tree head;
382 tree prev, next;
383
384 if (list == 0)
385 return 0;
386
387 head = prev = copy_node (list);
388 next = TREE_CHAIN (list);
389 while (next)
390 {
391 TREE_CHAIN (prev) = copy_node (next);
392 prev = TREE_CHAIN (prev);
393 next = TREE_CHAIN (next);
394 }
395 return head;
396 }
397
398 \f
399 /* Return a newly constructed INTEGER_CST node whose constant value
400 is specified by the two ints LOW and HI.
401 The TREE_TYPE is set to `int'.
402
403 This function should be used via the `build_int_2' macro. */
404
405 tree
406 build_int_2_wide (unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
407 {
408 tree t = make_node (INTEGER_CST);
409
410 TREE_INT_CST_LOW (t) = low;
411 TREE_INT_CST_HIGH (t) = hi;
412 TREE_TYPE (t) = integer_type_node;
413 return t;
414 }
415
416 /* Return a new VECTOR_CST node whose type is TYPE and whose values
417 are in a list pointed by VALS. */
418
419 tree
420 build_vector (tree type, tree vals)
421 {
422 tree v = make_node (VECTOR_CST);
423 int over1 = 0, over2 = 0;
424 tree link;
425
426 TREE_VECTOR_CST_ELTS (v) = vals;
427 TREE_TYPE (v) = type;
428
429 /* Iterate through elements and check for overflow. */
430 for (link = vals; link; link = TREE_CHAIN (link))
431 {
432 tree value = TREE_VALUE (link);
433
434 over1 |= TREE_OVERFLOW (value);
435 over2 |= TREE_CONSTANT_OVERFLOW (value);
436 }
437
438 TREE_OVERFLOW (v) = over1;
439 TREE_CONSTANT_OVERFLOW (v) = over2;
440
441 return v;
442 }
443
444 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
445 are in a list pointed to by VALS. */
446 tree
447 build_constructor (tree type, tree vals)
448 {
449 tree c = make_node (CONSTRUCTOR);
450 TREE_TYPE (c) = type;
451 CONSTRUCTOR_ELTS (c) = vals;
452
453 /* ??? May not be necessary. Mirrors what build does. */
454 if (vals)
455 {
456 TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (vals);
457 TREE_READONLY (c) = TREE_READONLY (vals);
458 TREE_CONSTANT (c) = TREE_CONSTANT (vals);
459 }
460 else
461 TREE_CONSTANT (c) = 0; /* safe side */
462
463 return c;
464 }
465
466 /* Return a new REAL_CST node whose type is TYPE and value is D. */
467
468 tree
469 build_real (tree type, REAL_VALUE_TYPE d)
470 {
471 tree v;
472 REAL_VALUE_TYPE *dp;
473 int overflow = 0;
474
475 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
476 Consider doing it via real_convert now. */
477
478 v = make_node (REAL_CST);
479 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
480 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
481
482 TREE_TYPE (v) = type;
483 TREE_REAL_CST_PTR (v) = dp;
484 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
485 return v;
486 }
487
488 /* Return a new REAL_CST node whose type is TYPE
489 and whose value is the integer value of the INTEGER_CST node I. */
490
491 REAL_VALUE_TYPE
492 real_value_from_int_cst (tree type, tree i)
493 {
494 REAL_VALUE_TYPE d;
495
496 /* Clear all bits of the real value type so that we can later do
497 bitwise comparisons to see if two values are the same. */
498 memset (&d, 0, sizeof d);
499
500 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
501 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
502 TREE_UNSIGNED (TREE_TYPE (i)));
503 return d;
504 }
505
506 /* Given a tree representing an integer constant I, return a tree
507 representing the same value as a floating-point constant of type TYPE. */
508
509 tree
510 build_real_from_int_cst (tree type, tree i)
511 {
512 tree v;
513 int overflow = TREE_OVERFLOW (i);
514
515 v = build_real (type, real_value_from_int_cst (type, i));
516
517 TREE_OVERFLOW (v) |= overflow;
518 TREE_CONSTANT_OVERFLOW (v) |= overflow;
519 return v;
520 }
521
522 /* Return a newly constructed STRING_CST node whose value is
523 the LEN characters at STR.
524 The TREE_TYPE is not initialized. */
525
526 tree
527 build_string (int len, const char *str)
528 {
529 tree s;
530 size_t length;
531
532 length = len + sizeof (struct tree_string);
533
534 #ifdef GATHER_STATISTICS
535 tree_node_counts[(int) c_kind]++;
536 tree_node_sizes[(int) c_kind] += length;
537 #endif
538
539 s = ggc_alloc_tree (length);
540
541 memset (s, 0, sizeof (struct tree_common));
542 TREE_SET_CODE (s, STRING_CST);
543 TREE_STRING_LENGTH (s) = len;
544 memcpy ((char *) TREE_STRING_POINTER (s), str, len);
545 ((char *) TREE_STRING_POINTER (s))[len] = '\0';
546
547 return s;
548 }
549
550 /* Return a newly constructed COMPLEX_CST node whose value is
551 specified by the real and imaginary parts REAL and IMAG.
552 Both REAL and IMAG should be constant nodes. TYPE, if specified,
553 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
554
555 tree
556 build_complex (tree type, tree real, tree imag)
557 {
558 tree t = make_node (COMPLEX_CST);
559
560 TREE_REALPART (t) = real;
561 TREE_IMAGPART (t) = imag;
562 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
563 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
564 TREE_CONSTANT_OVERFLOW (t)
565 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
566 return t;
567 }
568
569 /* Build a newly constructed TREE_VEC node of length LEN. */
570
571 tree
572 make_tree_vec (int len)
573 {
574 tree t;
575 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
576
577 #ifdef GATHER_STATISTICS
578 tree_node_counts[(int) vec_kind]++;
579 tree_node_sizes[(int) vec_kind] += length;
580 #endif
581
582 t = ggc_alloc_tree (length);
583
584 memset (t, 0, length);
585 TREE_SET_CODE (t, TREE_VEC);
586 TREE_VEC_LENGTH (t) = len;
587
588 return t;
589 }
590 \f
591 /* Return 1 if EXPR is the integer constant zero or a complex constant
592 of zero. */
593
594 int
595 integer_zerop (tree expr)
596 {
597 STRIP_NOPS (expr);
598
599 return ((TREE_CODE (expr) == INTEGER_CST
600 && ! TREE_CONSTANT_OVERFLOW (expr)
601 && TREE_INT_CST_LOW (expr) == 0
602 && TREE_INT_CST_HIGH (expr) == 0)
603 || (TREE_CODE (expr) == COMPLEX_CST
604 && integer_zerop (TREE_REALPART (expr))
605 && integer_zerop (TREE_IMAGPART (expr))));
606 }
607
608 /* Return 1 if EXPR is the integer constant one or the corresponding
609 complex constant. */
610
611 int
612 integer_onep (tree expr)
613 {
614 STRIP_NOPS (expr);
615
616 return ((TREE_CODE (expr) == INTEGER_CST
617 && ! TREE_CONSTANT_OVERFLOW (expr)
618 && TREE_INT_CST_LOW (expr) == 1
619 && TREE_INT_CST_HIGH (expr) == 0)
620 || (TREE_CODE (expr) == COMPLEX_CST
621 && integer_onep (TREE_REALPART (expr))
622 && integer_zerop (TREE_IMAGPART (expr))));
623 }
624
625 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
626 it contains. Likewise for the corresponding complex constant. */
627
628 int
629 integer_all_onesp (tree expr)
630 {
631 int prec;
632 int uns;
633
634 STRIP_NOPS (expr);
635
636 if (TREE_CODE (expr) == COMPLEX_CST
637 && integer_all_onesp (TREE_REALPART (expr))
638 && integer_zerop (TREE_IMAGPART (expr)))
639 return 1;
640
641 else if (TREE_CODE (expr) != INTEGER_CST
642 || TREE_CONSTANT_OVERFLOW (expr))
643 return 0;
644
645 uns = TREE_UNSIGNED (TREE_TYPE (expr));
646 if (!uns)
647 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
648 && TREE_INT_CST_HIGH (expr) == -1);
649
650 /* Note that using TYPE_PRECISION here is wrong. We care about the
651 actual bits, not the (arbitrary) range of the type. */
652 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
653 if (prec >= HOST_BITS_PER_WIDE_INT)
654 {
655 HOST_WIDE_INT high_value;
656 int shift_amount;
657
658 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
659
660 if (shift_amount > HOST_BITS_PER_WIDE_INT)
661 /* Can not handle precisions greater than twice the host int size. */
662 abort ();
663 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
664 /* Shifting by the host word size is undefined according to the ANSI
665 standard, so we must handle this as a special case. */
666 high_value = -1;
667 else
668 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
669
670 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
671 && TREE_INT_CST_HIGH (expr) == high_value);
672 }
673 else
674 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
675 }
676
677 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
678 one bit on). */
679
680 int
681 integer_pow2p (tree expr)
682 {
683 int prec;
684 HOST_WIDE_INT high, low;
685
686 STRIP_NOPS (expr);
687
688 if (TREE_CODE (expr) == COMPLEX_CST
689 && integer_pow2p (TREE_REALPART (expr))
690 && integer_zerop (TREE_IMAGPART (expr)))
691 return 1;
692
693 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
694 return 0;
695
696 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
697 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
698 high = TREE_INT_CST_HIGH (expr);
699 low = TREE_INT_CST_LOW (expr);
700
701 /* First clear all bits that are beyond the type's precision in case
702 we've been sign extended. */
703
704 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
705 ;
706 else if (prec > HOST_BITS_PER_WIDE_INT)
707 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
708 else
709 {
710 high = 0;
711 if (prec < HOST_BITS_PER_WIDE_INT)
712 low &= ~((HOST_WIDE_INT) (-1) << prec);
713 }
714
715 if (high == 0 && low == 0)
716 return 0;
717
718 return ((high == 0 && (low & (low - 1)) == 0)
719 || (low == 0 && (high & (high - 1)) == 0));
720 }
721
722 /* Return 1 if EXPR is an integer constant other than zero or a
723 complex constant other than zero. */
724
725 int
726 integer_nonzerop (tree expr)
727 {
728 STRIP_NOPS (expr);
729
730 return ((TREE_CODE (expr) == INTEGER_CST
731 && ! TREE_CONSTANT_OVERFLOW (expr)
732 && (TREE_INT_CST_LOW (expr) != 0
733 || TREE_INT_CST_HIGH (expr) != 0))
734 || (TREE_CODE (expr) == COMPLEX_CST
735 && (integer_nonzerop (TREE_REALPART (expr))
736 || integer_nonzerop (TREE_IMAGPART (expr)))));
737 }
738
739 /* Return the power of two represented by a tree node known to be a
740 power of two. */
741
742 int
743 tree_log2 (tree expr)
744 {
745 int prec;
746 HOST_WIDE_INT high, low;
747
748 STRIP_NOPS (expr);
749
750 if (TREE_CODE (expr) == COMPLEX_CST)
751 return tree_log2 (TREE_REALPART (expr));
752
753 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
754 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
755
756 high = TREE_INT_CST_HIGH (expr);
757 low = TREE_INT_CST_LOW (expr);
758
759 /* First clear all bits that are beyond the type's precision in case
760 we've been sign extended. */
761
762 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
763 ;
764 else if (prec > HOST_BITS_PER_WIDE_INT)
765 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
766 else
767 {
768 high = 0;
769 if (prec < HOST_BITS_PER_WIDE_INT)
770 low &= ~((HOST_WIDE_INT) (-1) << prec);
771 }
772
773 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
774 : exact_log2 (low));
775 }
776
777 /* Similar, but return the largest integer Y such that 2 ** Y is less
778 than or equal to EXPR. */
779
780 int
781 tree_floor_log2 (tree expr)
782 {
783 int prec;
784 HOST_WIDE_INT high, low;
785
786 STRIP_NOPS (expr);
787
788 if (TREE_CODE (expr) == COMPLEX_CST)
789 return tree_log2 (TREE_REALPART (expr));
790
791 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
792 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
793
794 high = TREE_INT_CST_HIGH (expr);
795 low = TREE_INT_CST_LOW (expr);
796
797 /* First clear all bits that are beyond the type's precision in case
798 we've been sign extended. Ignore if type's precision hasn't been set
799 since what we are doing is setting it. */
800
801 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
802 ;
803 else if (prec > HOST_BITS_PER_WIDE_INT)
804 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
805 else
806 {
807 high = 0;
808 if (prec < HOST_BITS_PER_WIDE_INT)
809 low &= ~((HOST_WIDE_INT) (-1) << prec);
810 }
811
812 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
813 : floor_log2 (low));
814 }
815
816 /* Return 1 if EXPR is the real constant zero. */
817
818 int
819 real_zerop (tree expr)
820 {
821 STRIP_NOPS (expr);
822
823 return ((TREE_CODE (expr) == REAL_CST
824 && ! TREE_CONSTANT_OVERFLOW (expr)
825 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
826 || (TREE_CODE (expr) == COMPLEX_CST
827 && real_zerop (TREE_REALPART (expr))
828 && real_zerop (TREE_IMAGPART (expr))));
829 }
830
831 /* Return 1 if EXPR is the real constant one in real or complex form. */
832
833 int
834 real_onep (tree expr)
835 {
836 STRIP_NOPS (expr);
837
838 return ((TREE_CODE (expr) == REAL_CST
839 && ! TREE_CONSTANT_OVERFLOW (expr)
840 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
841 || (TREE_CODE (expr) == COMPLEX_CST
842 && real_onep (TREE_REALPART (expr))
843 && real_zerop (TREE_IMAGPART (expr))));
844 }
845
846 /* Return 1 if EXPR is the real constant two. */
847
848 int
849 real_twop (tree expr)
850 {
851 STRIP_NOPS (expr);
852
853 return ((TREE_CODE (expr) == REAL_CST
854 && ! TREE_CONSTANT_OVERFLOW (expr)
855 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
856 || (TREE_CODE (expr) == COMPLEX_CST
857 && real_twop (TREE_REALPART (expr))
858 && real_zerop (TREE_IMAGPART (expr))));
859 }
860
861 /* Return 1 if EXPR is the real constant minus one. */
862
863 int
864 real_minus_onep (tree expr)
865 {
866 STRIP_NOPS (expr);
867
868 return ((TREE_CODE (expr) == REAL_CST
869 && ! TREE_CONSTANT_OVERFLOW (expr)
870 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
871 || (TREE_CODE (expr) == COMPLEX_CST
872 && real_minus_onep (TREE_REALPART (expr))
873 && real_zerop (TREE_IMAGPART (expr))));
874 }
875
876 /* Nonzero if EXP is a constant or a cast of a constant. */
877
878 int
879 really_constant_p (tree exp)
880 {
881 /* This is not quite the same as STRIP_NOPS. It does more. */
882 while (TREE_CODE (exp) == NOP_EXPR
883 || TREE_CODE (exp) == CONVERT_EXPR
884 || TREE_CODE (exp) == NON_LVALUE_EXPR)
885 exp = TREE_OPERAND (exp, 0);
886 return TREE_CONSTANT (exp);
887 }
888 \f
889 /* Return first list element whose TREE_VALUE is ELEM.
890 Return 0 if ELEM is not in LIST. */
891
892 tree
893 value_member (tree elem, tree list)
894 {
895 while (list)
896 {
897 if (elem == TREE_VALUE (list))
898 return list;
899 list = TREE_CHAIN (list);
900 }
901 return NULL_TREE;
902 }
903
904 /* Return first list element whose TREE_PURPOSE is ELEM.
905 Return 0 if ELEM is not in LIST. */
906
907 tree
908 purpose_member (tree elem, tree list)
909 {
910 while (list)
911 {
912 if (elem == TREE_PURPOSE (list))
913 return list;
914 list = TREE_CHAIN (list);
915 }
916 return NULL_TREE;
917 }
918
919 /* Return first list element whose BINFO_TYPE is ELEM.
920 Return 0 if ELEM is not in LIST. */
921
922 tree
923 binfo_member (tree elem, tree list)
924 {
925 while (list)
926 {
927 if (elem == BINFO_TYPE (list))
928 return list;
929 list = TREE_CHAIN (list);
930 }
931 return NULL_TREE;
932 }
933
934 /* Return nonzero if ELEM is part of the chain CHAIN. */
935
936 int
937 chain_member (tree elem, tree chain)
938 {
939 while (chain)
940 {
941 if (elem == chain)
942 return 1;
943 chain = TREE_CHAIN (chain);
944 }
945
946 return 0;
947 }
948
949 /* Return the length of a chain of nodes chained through TREE_CHAIN.
950 We expect a null pointer to mark the end of the chain.
951 This is the Lisp primitive `length'. */
952
953 int
954 list_length (tree t)
955 {
956 tree tail;
957 int len = 0;
958
959 for (tail = t; tail; tail = TREE_CHAIN (tail))
960 len++;
961
962 return len;
963 }
964
965 /* Returns the number of FIELD_DECLs in TYPE. */
966
967 int
968 fields_length (tree type)
969 {
970 tree t = TYPE_FIELDS (type);
971 int count = 0;
972
973 for (; t; t = TREE_CHAIN (t))
974 if (TREE_CODE (t) == FIELD_DECL)
975 ++count;
976
977 return count;
978 }
979
980 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
981 by modifying the last node in chain 1 to point to chain 2.
982 This is the Lisp primitive `nconc'. */
983
984 tree
985 chainon (tree op1, tree op2)
986 {
987 tree t1;
988
989 if (!op1)
990 return op2;
991 if (!op2)
992 return op1;
993
994 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
995 continue;
996 TREE_CHAIN (t1) = op2;
997
998 #ifdef ENABLE_TREE_CHECKING
999 {
1000 tree t2;
1001 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1002 if (t2 == t1)
1003 abort (); /* Circularity created. */
1004 }
1005 #endif
1006
1007 return op1;
1008 }
1009
1010 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1011
1012 tree
1013 tree_last (tree chain)
1014 {
1015 tree next;
1016 if (chain)
1017 while ((next = TREE_CHAIN (chain)))
1018 chain = next;
1019 return chain;
1020 }
1021
1022 /* Reverse the order of elements in the chain T,
1023 and return the new head of the chain (old last element). */
1024
1025 tree
1026 nreverse (tree t)
1027 {
1028 tree prev = 0, decl, next;
1029 for (decl = t; decl; decl = next)
1030 {
1031 next = TREE_CHAIN (decl);
1032 TREE_CHAIN (decl) = prev;
1033 prev = decl;
1034 }
1035 return prev;
1036 }
1037 \f
1038 /* Return a newly created TREE_LIST node whose
1039 purpose and value fields are PARM and VALUE. */
1040
1041 tree
1042 build_tree_list (tree parm, tree value)
1043 {
1044 tree t = make_node (TREE_LIST);
1045 TREE_PURPOSE (t) = parm;
1046 TREE_VALUE (t) = value;
1047 return t;
1048 }
1049
1050 /* Return a newly created TREE_LIST node whose
1051 purpose and value fields are PURPOSE and VALUE
1052 and whose TREE_CHAIN is CHAIN. */
1053
1054 tree
1055 tree_cons (tree purpose, tree value, tree chain)
1056 {
1057 tree node;
1058
1059 node = ggc_alloc_tree (sizeof (struct tree_list));
1060
1061 memset (node, 0, sizeof (struct tree_common));
1062
1063 #ifdef GATHER_STATISTICS
1064 tree_node_counts[(int) x_kind]++;
1065 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1066 #endif
1067
1068 TREE_SET_CODE (node, TREE_LIST);
1069 TREE_CHAIN (node) = chain;
1070 TREE_PURPOSE (node) = purpose;
1071 TREE_VALUE (node) = value;
1072 return node;
1073 }
1074
1075 /* Return the first expression in a sequence of COMPOUND_EXPRs. */
1076
1077 tree
1078 expr_first (tree expr)
1079 {
1080 if (expr == NULL_TREE)
1081 return expr;
1082 while (TREE_CODE (expr) == COMPOUND_EXPR)
1083 expr = TREE_OPERAND (expr, 0);
1084 return expr;
1085 }
1086
1087 /* Return the last expression in a sequence of COMPOUND_EXPRs. */
1088
1089 tree
1090 expr_last (tree expr)
1091 {
1092 if (expr == NULL_TREE)
1093 return expr;
1094 while (TREE_CODE (expr) == COMPOUND_EXPR)
1095 expr = TREE_OPERAND (expr, 1);
1096 return expr;
1097 }
1098
1099 /* Return the number of subexpressions in a sequence of COMPOUND_EXPRs. */
1100
1101 int
1102 expr_length (tree expr)
1103 {
1104 int len = 0;
1105
1106 if (expr == NULL_TREE)
1107 return 0;
1108 for (; TREE_CODE (expr) == COMPOUND_EXPR; expr = TREE_OPERAND (expr, 1))
1109 len += expr_length (TREE_OPERAND (expr, 0));
1110 ++len;
1111 return len;
1112 }
1113 \f
1114 /* Return the size nominally occupied by an object of type TYPE
1115 when it resides in memory. The value is measured in units of bytes,
1116 and its data type is that normally used for type sizes
1117 (which is the first type created by make_signed_type or
1118 make_unsigned_type). */
1119
1120 tree
1121 size_in_bytes (tree type)
1122 {
1123 tree t;
1124
1125 if (type == error_mark_node)
1126 return integer_zero_node;
1127
1128 type = TYPE_MAIN_VARIANT (type);
1129 t = TYPE_SIZE_UNIT (type);
1130
1131 if (t == 0)
1132 {
1133 (*lang_hooks.types.incomplete_type_error) (NULL_TREE, type);
1134 return size_zero_node;
1135 }
1136
1137 if (TREE_CODE (t) == INTEGER_CST)
1138 force_fit_type (t, 0);
1139
1140 return t;
1141 }
1142
1143 /* Return the size of TYPE (in bytes) as a wide integer
1144 or return -1 if the size can vary or is larger than an integer. */
1145
1146 HOST_WIDE_INT
1147 int_size_in_bytes (tree type)
1148 {
1149 tree t;
1150
1151 if (type == error_mark_node)
1152 return 0;
1153
1154 type = TYPE_MAIN_VARIANT (type);
1155 t = TYPE_SIZE_UNIT (type);
1156 if (t == 0
1157 || TREE_CODE (t) != INTEGER_CST
1158 || TREE_OVERFLOW (t)
1159 || TREE_INT_CST_HIGH (t) != 0
1160 /* If the result would appear negative, it's too big to represent. */
1161 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1162 return -1;
1163
1164 return TREE_INT_CST_LOW (t);
1165 }
1166 \f
1167 /* Return the bit position of FIELD, in bits from the start of the record.
1168 This is a tree of type bitsizetype. */
1169
1170 tree
1171 bit_position (tree field)
1172 {
1173 return bit_from_pos (DECL_FIELD_OFFSET (field),
1174 DECL_FIELD_BIT_OFFSET (field));
1175 }
1176
1177 /* Likewise, but return as an integer. Abort if it cannot be represented
1178 in that way (since it could be a signed value, we don't have the option
1179 of returning -1 like int_size_in_byte can. */
1180
1181 HOST_WIDE_INT
1182 int_bit_position (tree field)
1183 {
1184 return tree_low_cst (bit_position (field), 0);
1185 }
1186 \f
1187 /* Return the byte position of FIELD, in bytes from the start of the record.
1188 This is a tree of type sizetype. */
1189
1190 tree
1191 byte_position (tree field)
1192 {
1193 return byte_from_pos (DECL_FIELD_OFFSET (field),
1194 DECL_FIELD_BIT_OFFSET (field));
1195 }
1196
1197 /* Likewise, but return as an integer. Abort if it cannot be represented
1198 in that way (since it could be a signed value, we don't have the option
1199 of returning -1 like int_size_in_byte can. */
1200
1201 HOST_WIDE_INT
1202 int_byte_position (tree field)
1203 {
1204 return tree_low_cst (byte_position (field), 0);
1205 }
1206 \f
1207 /* Return the strictest alignment, in bits, that T is known to have. */
1208
1209 unsigned int
1210 expr_align (tree t)
1211 {
1212 unsigned int align0, align1;
1213
1214 switch (TREE_CODE (t))
1215 {
1216 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1217 /* If we have conversions, we know that the alignment of the
1218 object must meet each of the alignments of the types. */
1219 align0 = expr_align (TREE_OPERAND (t, 0));
1220 align1 = TYPE_ALIGN (TREE_TYPE (t));
1221 return MAX (align0, align1);
1222
1223 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1224 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1225 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1226 /* These don't change the alignment of an object. */
1227 return expr_align (TREE_OPERAND (t, 0));
1228
1229 case COND_EXPR:
1230 /* The best we can do is say that the alignment is the least aligned
1231 of the two arms. */
1232 align0 = expr_align (TREE_OPERAND (t, 1));
1233 align1 = expr_align (TREE_OPERAND (t, 2));
1234 return MIN (align0, align1);
1235
1236 case LABEL_DECL: case CONST_DECL:
1237 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1238 if (DECL_ALIGN (t) != 0)
1239 return DECL_ALIGN (t);
1240 break;
1241
1242 case FUNCTION_DECL:
1243 return FUNCTION_BOUNDARY;
1244
1245 default:
1246 break;
1247 }
1248
1249 /* Otherwise take the alignment from that of the type. */
1250 return TYPE_ALIGN (TREE_TYPE (t));
1251 }
1252 \f
1253 /* Return, as a tree node, the number of elements for TYPE (which is an
1254 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1255
1256 tree
1257 array_type_nelts (tree type)
1258 {
1259 tree index_type, min, max;
1260
1261 /* If they did it with unspecified bounds, then we should have already
1262 given an error about it before we got here. */
1263 if (! TYPE_DOMAIN (type))
1264 return error_mark_node;
1265
1266 index_type = TYPE_DOMAIN (type);
1267 min = TYPE_MIN_VALUE (index_type);
1268 max = TYPE_MAX_VALUE (index_type);
1269
1270 return (integer_zerop (min)
1271 ? max
1272 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1273 }
1274 \f
1275 /* Return nonzero if arg is static -- a reference to an object in
1276 static storage. This is not the same as the C meaning of `static'. */
1277
1278 int
1279 staticp (tree arg)
1280 {
1281 switch (TREE_CODE (arg))
1282 {
1283 case FUNCTION_DECL:
1284 /* Nested functions aren't static, since taking their address
1285 involves a trampoline. */
1286 return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1287 && ! DECL_NON_ADDR_CONST_P (arg));
1288
1289 case VAR_DECL:
1290 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1291 && ! DECL_THREAD_LOCAL (arg)
1292 && ! DECL_NON_ADDR_CONST_P (arg));
1293
1294 case CONSTRUCTOR:
1295 return TREE_STATIC (arg);
1296
1297 case LABEL_DECL:
1298 case STRING_CST:
1299 return 1;
1300
1301 /* If we are referencing a bitfield, we can't evaluate an
1302 ADDR_EXPR at compile time and so it isn't a constant. */
1303 case COMPONENT_REF:
1304 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1305 && staticp (TREE_OPERAND (arg, 0)));
1306
1307 case BIT_FIELD_REF:
1308 return 0;
1309
1310 #if 0
1311 /* This case is technically correct, but results in setting
1312 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1313 compile time. */
1314 case INDIRECT_REF:
1315 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1316 #endif
1317
1318 case ARRAY_REF:
1319 case ARRAY_RANGE_REF:
1320 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1321 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1322 return staticp (TREE_OPERAND (arg, 0));
1323
1324 default:
1325 if ((unsigned int) TREE_CODE (arg)
1326 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1327 return (*lang_hooks.staticp) (arg);
1328 else
1329 return 0;
1330 }
1331 }
1332 \f
1333 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1334 Do this to any expression which may be used in more than one place,
1335 but must be evaluated only once.
1336
1337 Normally, expand_expr would reevaluate the expression each time.
1338 Calling save_expr produces something that is evaluated and recorded
1339 the first time expand_expr is called on it. Subsequent calls to
1340 expand_expr just reuse the recorded value.
1341
1342 The call to expand_expr that generates code that actually computes
1343 the value is the first call *at compile time*. Subsequent calls
1344 *at compile time* generate code to use the saved value.
1345 This produces correct result provided that *at run time* control
1346 always flows through the insns made by the first expand_expr
1347 before reaching the other places where the save_expr was evaluated.
1348 You, the caller of save_expr, must make sure this is so.
1349
1350 Constants, and certain read-only nodes, are returned with no
1351 SAVE_EXPR because that is safe. Expressions containing placeholders
1352 are not touched; see tree.def for an explanation of what these
1353 are used for. */
1354
1355 tree
1356 save_expr (tree expr)
1357 {
1358 tree t = fold (expr);
1359 tree inner;
1360
1361 /* If the tree evaluates to a constant, then we don't want to hide that
1362 fact (i.e. this allows further folding, and direct checks for constants).
1363 However, a read-only object that has side effects cannot be bypassed.
1364 Since it is no problem to reevaluate literals, we just return the
1365 literal node. */
1366 inner = skip_simple_arithmetic (t);
1367 if (TREE_CONSTANT (inner)
1368 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1369 || TREE_CODE (inner) == SAVE_EXPR
1370 || TREE_CODE (inner) == ERROR_MARK)
1371 return t;
1372
1373 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1374 it means that the size or offset of some field of an object depends on
1375 the value within another field.
1376
1377 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1378 and some variable since it would then need to be both evaluated once and
1379 evaluated more than once. Front-ends must assure this case cannot
1380 happen by surrounding any such subexpressions in their own SAVE_EXPR
1381 and forcing evaluation at the proper time. */
1382 if (contains_placeholder_p (inner))
1383 return t;
1384
1385 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1386
1387 /* This expression might be placed ahead of a jump to ensure that the
1388 value was computed on both sides of the jump. So make sure it isn't
1389 eliminated as dead. */
1390 TREE_SIDE_EFFECTS (t) = 1;
1391 TREE_READONLY (t) = 1;
1392 return t;
1393 }
1394
1395 /* Look inside EXPR and into any simple arithmetic operations. Return
1396 the innermost non-arithmetic node. */
1397
1398 tree
1399 skip_simple_arithmetic (tree expr)
1400 {
1401 tree inner;
1402
1403 /* We don't care about whether this can be used as an lvalue in this
1404 context. */
1405 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1406 expr = TREE_OPERAND (expr, 0);
1407
1408 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1409 a constant, it will be more efficient to not make another SAVE_EXPR since
1410 it will allow better simplification and GCSE will be able to merge the
1411 computations if they actually occur. */
1412 inner = expr;
1413 while (1)
1414 {
1415 if (TREE_CODE_CLASS (TREE_CODE (inner)) == '1')
1416 inner = TREE_OPERAND (inner, 0);
1417 else if (TREE_CODE_CLASS (TREE_CODE (inner)) == '2')
1418 {
1419 if (TREE_CONSTANT (TREE_OPERAND (inner, 1)))
1420 inner = TREE_OPERAND (inner, 0);
1421 else if (TREE_CONSTANT (TREE_OPERAND (inner, 0)))
1422 inner = TREE_OPERAND (inner, 1);
1423 else
1424 break;
1425 }
1426 else
1427 break;
1428 }
1429
1430 return inner;
1431 }
1432
1433 /* Return TRUE if EXPR is a SAVE_EXPR or wraps simple arithmetic around a
1434 SAVE_EXPR. Return FALSE otherwise. */
1435
1436 bool
1437 saved_expr_p (tree expr)
1438 {
1439 return TREE_CODE (skip_simple_arithmetic (expr)) == SAVE_EXPR;
1440 }
1441
1442 /* Arrange for an expression to be expanded multiple independent
1443 times. This is useful for cleanup actions, as the backend can
1444 expand them multiple times in different places. */
1445
1446 tree
1447 unsave_expr (tree expr)
1448 {
1449 tree t;
1450
1451 /* If this is already protected, no sense in protecting it again. */
1452 if (TREE_CODE (expr) == UNSAVE_EXPR)
1453 return expr;
1454
1455 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1456 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1457 return t;
1458 }
1459
1460 /* Returns the index of the first non-tree operand for CODE, or the number
1461 of operands if all are trees. */
1462
1463 int
1464 first_rtl_op (enum tree_code code)
1465 {
1466 switch (code)
1467 {
1468 case SAVE_EXPR:
1469 return 2;
1470 case GOTO_SUBROUTINE_EXPR:
1471 case RTL_EXPR:
1472 return 0;
1473 case WITH_CLEANUP_EXPR:
1474 return 2;
1475 default:
1476 return TREE_CODE_LENGTH (code);
1477 }
1478 }
1479
1480 /* Return which tree structure is used by T. */
1481
1482 enum tree_node_structure_enum
1483 tree_node_structure (tree t)
1484 {
1485 enum tree_code code = TREE_CODE (t);
1486
1487 switch (TREE_CODE_CLASS (code))
1488 {
1489 case 'd': return TS_DECL;
1490 case 't': return TS_TYPE;
1491 case 'b': return TS_BLOCK;
1492 case 'r': case '<': case '1': case '2': case 'e': case 's':
1493 return TS_EXP;
1494 default: /* 'c' and 'x' */
1495 break;
1496 }
1497 switch (code)
1498 {
1499 /* 'c' cases. */
1500 case INTEGER_CST: return TS_INT_CST;
1501 case REAL_CST: return TS_REAL_CST;
1502 case COMPLEX_CST: return TS_COMPLEX;
1503 case VECTOR_CST: return TS_VECTOR;
1504 case STRING_CST: return TS_STRING;
1505 /* 'x' cases. */
1506 case ERROR_MARK: return TS_COMMON;
1507 case IDENTIFIER_NODE: return TS_IDENTIFIER;
1508 case TREE_LIST: return TS_LIST;
1509 case TREE_VEC: return TS_VEC;
1510 case PLACEHOLDER_EXPR: return TS_COMMON;
1511
1512 default:
1513 abort ();
1514 }
1515 }
1516
1517 /* Perform any modifications to EXPR required when it is unsaved. Does
1518 not recurse into EXPR's subtrees. */
1519
1520 void
1521 unsave_expr_1 (tree expr)
1522 {
1523 switch (TREE_CODE (expr))
1524 {
1525 case SAVE_EXPR:
1526 if (! SAVE_EXPR_PERSISTENT_P (expr))
1527 SAVE_EXPR_RTL (expr) = 0;
1528 break;
1529
1530 case TARGET_EXPR:
1531 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1532 It's OK for this to happen if it was part of a subtree that
1533 isn't immediately expanded, such as operand 2 of another
1534 TARGET_EXPR. */
1535 if (TREE_OPERAND (expr, 1))
1536 break;
1537
1538 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1539 TREE_OPERAND (expr, 3) = NULL_TREE;
1540 break;
1541
1542 case RTL_EXPR:
1543 /* I don't yet know how to emit a sequence multiple times. */
1544 if (RTL_EXPR_SEQUENCE (expr) != 0)
1545 abort ();
1546 break;
1547
1548 default:
1549 break;
1550 }
1551 }
1552
1553 /* Default lang hook for "unsave_expr_now". */
1554
1555 tree
1556 lhd_unsave_expr_now (tree expr)
1557 {
1558 enum tree_code code;
1559
1560 /* There's nothing to do for NULL_TREE. */
1561 if (expr == 0)
1562 return expr;
1563
1564 unsave_expr_1 (expr);
1565
1566 code = TREE_CODE (expr);
1567 switch (TREE_CODE_CLASS (code))
1568 {
1569 case 'c': /* a constant */
1570 case 't': /* a type node */
1571 case 'd': /* A decl node */
1572 case 'b': /* A block node */
1573 break;
1574
1575 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1576 if (code == TREE_LIST)
1577 {
1578 lhd_unsave_expr_now (TREE_VALUE (expr));
1579 lhd_unsave_expr_now (TREE_CHAIN (expr));
1580 }
1581 break;
1582
1583 case 'e': /* an expression */
1584 case 'r': /* a reference */
1585 case 's': /* an expression with side effects */
1586 case '<': /* a comparison expression */
1587 case '2': /* a binary arithmetic expression */
1588 case '1': /* a unary arithmetic expression */
1589 {
1590 int i;
1591
1592 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1593 lhd_unsave_expr_now (TREE_OPERAND (expr, i));
1594 }
1595 break;
1596
1597 default:
1598 abort ();
1599 }
1600
1601 return expr;
1602 }
1603
1604 /* Return 0 if it is safe to evaluate EXPR multiple times,
1605 return 1 if it is safe if EXPR is unsaved afterward, or
1606 return 2 if it is completely unsafe.
1607
1608 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1609 an expression tree, so that it safe to unsave them and the surrounding
1610 context will be correct.
1611
1612 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1613 occasionally across the whole of a function. It is therefore only
1614 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1615 below the UNSAVE_EXPR.
1616
1617 RTL_EXPRs consume their rtl during evaluation. It is therefore
1618 never possible to unsave them. */
1619
1620 int
1621 unsafe_for_reeval (tree expr)
1622 {
1623 int unsafeness = 0;
1624 enum tree_code code;
1625 int i, tmp, tmp2;
1626 tree exp;
1627 int first_rtl;
1628
1629 if (expr == NULL_TREE)
1630 return 1;
1631
1632 code = TREE_CODE (expr);
1633 first_rtl = first_rtl_op (code);
1634
1635 switch (code)
1636 {
1637 case SAVE_EXPR:
1638 case RTL_EXPR:
1639 return 2;
1640
1641 case TREE_LIST:
1642 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1643 {
1644 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1645 unsafeness = MAX (tmp, unsafeness);
1646 }
1647
1648 return unsafeness;
1649
1650 case CALL_EXPR:
1651 tmp2 = unsafe_for_reeval (TREE_OPERAND (expr, 0));
1652 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1653 return MAX (MAX (tmp, 1), tmp2);
1654
1655 case TARGET_EXPR:
1656 unsafeness = 1;
1657 break;
1658
1659 case EXIT_BLOCK_EXPR:
1660 /* EXIT_BLOCK_LABELED_BLOCK, a.k.a. TREE_OPERAND (expr, 0), holds
1661 a reference to an ancestor LABELED_BLOCK, so we need to avoid
1662 unbounded recursion in the 'e' traversal code below. */
1663 exp = EXIT_BLOCK_RETURN (expr);
1664 return exp ? unsafe_for_reeval (exp) : 0;
1665
1666 default:
1667 tmp = (*lang_hooks.unsafe_for_reeval) (expr);
1668 if (tmp >= 0)
1669 return tmp;
1670 break;
1671 }
1672
1673 switch (TREE_CODE_CLASS (code))
1674 {
1675 case 'c': /* a constant */
1676 case 't': /* a type node */
1677 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1678 case 'd': /* A decl node */
1679 case 'b': /* A block node */
1680 return 0;
1681
1682 case 'e': /* an expression */
1683 case 'r': /* a reference */
1684 case 's': /* an expression with side effects */
1685 case '<': /* a comparison expression */
1686 case '2': /* a binary arithmetic expression */
1687 case '1': /* a unary arithmetic expression */
1688 for (i = first_rtl - 1; i >= 0; i--)
1689 {
1690 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1691 unsafeness = MAX (tmp, unsafeness);
1692 }
1693
1694 return unsafeness;
1695
1696 default:
1697 return 2;
1698 }
1699 }
1700 \f
1701 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1702 or offset that depends on a field within a record. */
1703
1704 bool
1705 contains_placeholder_p (tree exp)
1706 {
1707 enum tree_code code;
1708 int result;
1709
1710 if (!exp)
1711 return 0;
1712
1713 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1714 in it since it is supplying a value for it. */
1715 code = TREE_CODE (exp);
1716 if (code == WITH_RECORD_EXPR)
1717 return 0;
1718 else if (code == PLACEHOLDER_EXPR)
1719 return 1;
1720
1721 switch (TREE_CODE_CLASS (code))
1722 {
1723 case 'r':
1724 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1725 position computations since they will be converted into a
1726 WITH_RECORD_EXPR involving the reference, which will assume
1727 here will be valid. */
1728 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1729
1730 case 'x':
1731 if (code == TREE_LIST)
1732 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
1733 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
1734 break;
1735
1736 case '1':
1737 case '2': case '<':
1738 case 'e':
1739 switch (code)
1740 {
1741 case COMPOUND_EXPR:
1742 /* Ignoring the first operand isn't quite right, but works best. */
1743 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1744
1745 case RTL_EXPR:
1746 case CONSTRUCTOR:
1747 return 0;
1748
1749 case COND_EXPR:
1750 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1751 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
1752 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
1753
1754 case SAVE_EXPR:
1755 /* If we already know this doesn't have a placeholder, don't
1756 check again. */
1757 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1758 return 0;
1759
1760 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1761 result = CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1762 if (result)
1763 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1764
1765 return result;
1766
1767 case CALL_EXPR:
1768 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1769
1770 default:
1771 break;
1772 }
1773
1774 switch (TREE_CODE_LENGTH (code))
1775 {
1776 case 1:
1777 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1778 case 2:
1779 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1780 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
1781 default:
1782 return 0;
1783 }
1784
1785 default:
1786 return 0;
1787 }
1788 return 0;
1789 }
1790
1791 /* Return 1 if any part of the computation of TYPE involves a PLACEHOLDER_EXPR.
1792 This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and field
1793 positions. */
1794
1795 bool
1796 type_contains_placeholder_p (tree type)
1797 {
1798 /* If the size contains a placeholder or the parent type (component type in
1799 the case of arrays) type involves a placeholder, this type does. */
1800 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1801 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
1802 || (TREE_TYPE (type) != 0
1803 && type_contains_placeholder_p (TREE_TYPE (type))))
1804 return 1;
1805
1806 /* Now do type-specific checks. Note that the last part of the check above
1807 greatly limits what we have to do below. */
1808 switch (TREE_CODE (type))
1809 {
1810 case VOID_TYPE:
1811 case COMPLEX_TYPE:
1812 case VECTOR_TYPE:
1813 case ENUMERAL_TYPE:
1814 case BOOLEAN_TYPE:
1815 case CHAR_TYPE:
1816 case POINTER_TYPE:
1817 case OFFSET_TYPE:
1818 case REFERENCE_TYPE:
1819 case METHOD_TYPE:
1820 case FILE_TYPE:
1821 case FUNCTION_TYPE:
1822 return 0;
1823
1824 case INTEGER_TYPE:
1825 case REAL_TYPE:
1826 /* Here we just check the bounds. */
1827 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
1828 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
1829
1830 case ARRAY_TYPE:
1831 case SET_TYPE:
1832 /* We're already checked the component type (TREE_TYPE), so just check
1833 the index type. */
1834 return type_contains_placeholder_p (TYPE_DOMAIN (type));
1835
1836 case RECORD_TYPE:
1837 case UNION_TYPE:
1838 case QUAL_UNION_TYPE:
1839 {
1840 static tree seen_types = 0;
1841 tree field;
1842 bool ret = 0;
1843
1844 /* We have to be careful here that we don't end up in infinite
1845 recursions due to a field of a type being a pointer to that type
1846 or to a mutually-recursive type. So we store a list of record
1847 types that we've seen and see if this type is in them. To save
1848 memory, we don't use a list for just one type. Here we check
1849 whether we've seen this type before and store it if not. */
1850 if (seen_types == 0)
1851 seen_types = type;
1852 else if (TREE_CODE (seen_types) != TREE_LIST)
1853 {
1854 if (seen_types == type)
1855 return 0;
1856
1857 seen_types = tree_cons (NULL_TREE, type,
1858 build_tree_list (NULL_TREE, seen_types));
1859 }
1860 else
1861 {
1862 if (value_member (type, seen_types) != 0)
1863 return 0;
1864
1865 seen_types = tree_cons (NULL_TREE, type, seen_types);
1866 }
1867
1868 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1869 if (TREE_CODE (field) == FIELD_DECL
1870 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
1871 || (TREE_CODE (type) == QUAL_UNION_TYPE
1872 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
1873 || type_contains_placeholder_p (TREE_TYPE (field))))
1874 {
1875 ret = true;
1876 break;
1877 }
1878
1879 /* Now remove us from seen_types and return the result. */
1880 if (seen_types == type)
1881 seen_types = 0;
1882 else
1883 seen_types = TREE_CHAIN (seen_types);
1884
1885 return ret;
1886 }
1887
1888 default:
1889 abort ();
1890 }
1891 }
1892
1893 /* Return 1 if EXP contains any expressions that produce cleanups for an
1894 outer scope to deal with. Used by fold. */
1895
1896 int
1897 has_cleanups (tree exp)
1898 {
1899 int i, nops, cmp;
1900
1901 if (! TREE_SIDE_EFFECTS (exp))
1902 return 0;
1903
1904 switch (TREE_CODE (exp))
1905 {
1906 case TARGET_EXPR:
1907 case GOTO_SUBROUTINE_EXPR:
1908 case WITH_CLEANUP_EXPR:
1909 return 1;
1910
1911 case CLEANUP_POINT_EXPR:
1912 return 0;
1913
1914 case CALL_EXPR:
1915 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1916 {
1917 cmp = has_cleanups (TREE_VALUE (exp));
1918 if (cmp)
1919 return cmp;
1920 }
1921 return 0;
1922
1923 default:
1924 break;
1925 }
1926
1927 /* This general rule works for most tree codes. All exceptions should be
1928 handled above. If this is a language-specific tree code, we can't
1929 trust what might be in the operand, so say we don't know
1930 the situation. */
1931 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1932 return -1;
1933
1934 nops = first_rtl_op (TREE_CODE (exp));
1935 for (i = 0; i < nops; i++)
1936 if (TREE_OPERAND (exp, i) != 0)
1937 {
1938 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1939 if (type == 'e' || type == '<' || type == '1' || type == '2'
1940 || type == 'r' || type == 's')
1941 {
1942 cmp = has_cleanups (TREE_OPERAND (exp, i));
1943 if (cmp)
1944 return cmp;
1945 }
1946 }
1947
1948 return 0;
1949 }
1950 \f
1951 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1952 return a tree with all occurrences of references to F in a
1953 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1954 contains only arithmetic expressions or a CALL_EXPR with a
1955 PLACEHOLDER_EXPR occurring only in its arglist. */
1956
1957 tree
1958 substitute_in_expr (tree exp, tree f, tree r)
1959 {
1960 enum tree_code code = TREE_CODE (exp);
1961 tree op0, op1, op2;
1962 tree new;
1963 tree inner;
1964
1965 switch (TREE_CODE_CLASS (code))
1966 {
1967 case 'c':
1968 case 'd':
1969 return exp;
1970
1971 case 'x':
1972 if (code == PLACEHOLDER_EXPR)
1973 return exp;
1974 else if (code == TREE_LIST)
1975 {
1976 op0 = (TREE_CHAIN (exp) == 0
1977 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
1978 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
1979 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1980 return exp;
1981
1982 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1983 }
1984
1985 abort ();
1986
1987 case '1':
1988 case '2':
1989 case '<':
1990 case 'e':
1991 switch (TREE_CODE_LENGTH (code))
1992 {
1993 case 1:
1994 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1995 if (op0 == TREE_OPERAND (exp, 0))
1996 return exp;
1997
1998 if (code == NON_LVALUE_EXPR)
1999 return op0;
2000
2001 new = fold (build1 (code, TREE_TYPE (exp), op0));
2002 break;
2003
2004 case 2:
2005 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2006 could, but we don't support it. */
2007 if (code == RTL_EXPR)
2008 return exp;
2009 else if (code == CONSTRUCTOR)
2010 abort ();
2011
2012 op0 = TREE_OPERAND (exp, 0);
2013 op1 = TREE_OPERAND (exp, 1);
2014 if (CONTAINS_PLACEHOLDER_P (op0))
2015 op0 = substitute_in_expr (op0, f, r);
2016 if (CONTAINS_PLACEHOLDER_P (op1))
2017 op1 = substitute_in_expr (op1, f, r);
2018
2019 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2020 return exp;
2021
2022 new = fold (build (code, TREE_TYPE (exp), op0, op1));
2023 break;
2024
2025 case 3:
2026 /* It cannot be that anything inside a SAVE_EXPR contains a
2027 PLACEHOLDER_EXPR. */
2028 if (code == SAVE_EXPR)
2029 return exp;
2030
2031 else if (code == CALL_EXPR)
2032 {
2033 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2034 if (op1 == TREE_OPERAND (exp, 1))
2035 return exp;
2036
2037 return build (code, TREE_TYPE (exp),
2038 TREE_OPERAND (exp, 0), op1, NULL_TREE);
2039 }
2040
2041 else if (code != COND_EXPR)
2042 abort ();
2043
2044 op0 = TREE_OPERAND (exp, 0);
2045 op1 = TREE_OPERAND (exp, 1);
2046 op2 = TREE_OPERAND (exp, 2);
2047
2048 if (CONTAINS_PLACEHOLDER_P (op0))
2049 op0 = substitute_in_expr (op0, f, r);
2050 if (CONTAINS_PLACEHOLDER_P (op1))
2051 op1 = substitute_in_expr (op1, f, r);
2052 if (CONTAINS_PLACEHOLDER_P (op2))
2053 op2 = substitute_in_expr (op2, f, r);
2054
2055 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2056 && op2 == TREE_OPERAND (exp, 2))
2057 return exp;
2058
2059 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2060 break;
2061
2062 default:
2063 abort ();
2064 }
2065
2066 break;
2067
2068 case 'r':
2069 switch (code)
2070 {
2071 case COMPONENT_REF:
2072 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2073 and it is the right field, replace it with R. */
2074 for (inner = TREE_OPERAND (exp, 0);
2075 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2076 inner = TREE_OPERAND (inner, 0))
2077 ;
2078 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2079 && TREE_OPERAND (exp, 1) == f)
2080 return r;
2081
2082 /* If this expression hasn't been completed let, leave it
2083 alone. */
2084 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2085 && TREE_TYPE (inner) == 0)
2086 return exp;
2087
2088 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2089 if (op0 == TREE_OPERAND (exp, 0))
2090 return exp;
2091
2092 new = fold (build (code, TREE_TYPE (exp), op0,
2093 TREE_OPERAND (exp, 1)));
2094 break;
2095
2096 case BIT_FIELD_REF:
2097 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2098 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2099 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2100 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2101 && op2 == TREE_OPERAND (exp, 2))
2102 return exp;
2103
2104 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2105 break;
2106
2107 case INDIRECT_REF:
2108 case BUFFER_REF:
2109 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2110 if (op0 == TREE_OPERAND (exp, 0))
2111 return exp;
2112
2113 new = fold (build1 (code, TREE_TYPE (exp), op0));
2114 break;
2115
2116 default:
2117 abort ();
2118 }
2119 break;
2120
2121 default:
2122 abort ();
2123 }
2124
2125 TREE_READONLY (new) = TREE_READONLY (exp);
2126 return new;
2127 }
2128 \f
2129 /* Stabilize a reference so that we can use it any number of times
2130 without causing its operands to be evaluated more than once.
2131 Returns the stabilized reference. This works by means of save_expr,
2132 so see the caveats in the comments about save_expr.
2133
2134 Also allows conversion expressions whose operands are references.
2135 Any other kind of expression is returned unchanged. */
2136
2137 tree
2138 stabilize_reference (tree ref)
2139 {
2140 tree result;
2141 enum tree_code code = TREE_CODE (ref);
2142
2143 switch (code)
2144 {
2145 case VAR_DECL:
2146 case PARM_DECL:
2147 case RESULT_DECL:
2148 /* No action is needed in this case. */
2149 return ref;
2150
2151 case NOP_EXPR:
2152 case CONVERT_EXPR:
2153 case FLOAT_EXPR:
2154 case FIX_TRUNC_EXPR:
2155 case FIX_FLOOR_EXPR:
2156 case FIX_ROUND_EXPR:
2157 case FIX_CEIL_EXPR:
2158 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2159 break;
2160
2161 case INDIRECT_REF:
2162 result = build_nt (INDIRECT_REF,
2163 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2164 break;
2165
2166 case COMPONENT_REF:
2167 result = build_nt (COMPONENT_REF,
2168 stabilize_reference (TREE_OPERAND (ref, 0)),
2169 TREE_OPERAND (ref, 1));
2170 break;
2171
2172 case BIT_FIELD_REF:
2173 result = build_nt (BIT_FIELD_REF,
2174 stabilize_reference (TREE_OPERAND (ref, 0)),
2175 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2176 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2177 break;
2178
2179 case ARRAY_REF:
2180 result = build_nt (ARRAY_REF,
2181 stabilize_reference (TREE_OPERAND (ref, 0)),
2182 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2183 break;
2184
2185 case ARRAY_RANGE_REF:
2186 result = build_nt (ARRAY_RANGE_REF,
2187 stabilize_reference (TREE_OPERAND (ref, 0)),
2188 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2189 break;
2190
2191 case COMPOUND_EXPR:
2192 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2193 it wouldn't be ignored. This matters when dealing with
2194 volatiles. */
2195 return stabilize_reference_1 (ref);
2196
2197 case RTL_EXPR:
2198 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2199 save_expr (build1 (ADDR_EXPR,
2200 build_pointer_type (TREE_TYPE (ref)),
2201 ref)));
2202 break;
2203
2204 /* If arg isn't a kind of lvalue we recognize, make no change.
2205 Caller should recognize the error for an invalid lvalue. */
2206 default:
2207 return ref;
2208
2209 case ERROR_MARK:
2210 return error_mark_node;
2211 }
2212
2213 TREE_TYPE (result) = TREE_TYPE (ref);
2214 TREE_READONLY (result) = TREE_READONLY (ref);
2215 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2216 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2217
2218 return result;
2219 }
2220
2221 /* Subroutine of stabilize_reference; this is called for subtrees of
2222 references. Any expression with side-effects must be put in a SAVE_EXPR
2223 to ensure that it is only evaluated once.
2224
2225 We don't put SAVE_EXPR nodes around everything, because assigning very
2226 simple expressions to temporaries causes us to miss good opportunities
2227 for optimizations. Among other things, the opportunity to fold in the
2228 addition of a constant into an addressing mode often gets lost, e.g.
2229 "y[i+1] += x;". In general, we take the approach that we should not make
2230 an assignment unless we are forced into it - i.e., that any non-side effect
2231 operator should be allowed, and that cse should take care of coalescing
2232 multiple utterances of the same expression should that prove fruitful. */
2233
2234 tree
2235 stabilize_reference_1 (tree e)
2236 {
2237 tree result;
2238 enum tree_code code = TREE_CODE (e);
2239
2240 /* We cannot ignore const expressions because it might be a reference
2241 to a const array but whose index contains side-effects. But we can
2242 ignore things that are actual constant or that already have been
2243 handled by this function. */
2244
2245 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2246 return e;
2247
2248 switch (TREE_CODE_CLASS (code))
2249 {
2250 case 'x':
2251 case 't':
2252 case 'd':
2253 case 'b':
2254 case '<':
2255 case 's':
2256 case 'e':
2257 case 'r':
2258 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2259 so that it will only be evaluated once. */
2260 /* The reference (r) and comparison (<) classes could be handled as
2261 below, but it is generally faster to only evaluate them once. */
2262 if (TREE_SIDE_EFFECTS (e))
2263 return save_expr (e);
2264 return e;
2265
2266 case 'c':
2267 /* Constants need no processing. In fact, we should never reach
2268 here. */
2269 return e;
2270
2271 case '2':
2272 /* Division is slow and tends to be compiled with jumps,
2273 especially the division by powers of 2 that is often
2274 found inside of an array reference. So do it just once. */
2275 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2276 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2277 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2278 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2279 return save_expr (e);
2280 /* Recursively stabilize each operand. */
2281 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2282 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2283 break;
2284
2285 case '1':
2286 /* Recursively stabilize each operand. */
2287 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2288 break;
2289
2290 default:
2291 abort ();
2292 }
2293
2294 TREE_TYPE (result) = TREE_TYPE (e);
2295 TREE_READONLY (result) = TREE_READONLY (e);
2296 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2297 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2298
2299 return result;
2300 }
2301 \f
2302 /* Low-level constructors for expressions. */
2303
2304 /* Build an expression of code CODE, data type TYPE,
2305 and operands as specified by the arguments ARG1 and following arguments.
2306 Expressions and reference nodes can be created this way.
2307 Constants, decls, types and misc nodes cannot be. */
2308
2309 tree
2310 build (enum tree_code code, tree tt, ...)
2311 {
2312 tree t;
2313 int length;
2314 int i;
2315 int fro;
2316 int constant;
2317 va_list p;
2318 tree node;
2319
2320 va_start (p, tt);
2321
2322 t = make_node (code);
2323 length = TREE_CODE_LENGTH (code);
2324 TREE_TYPE (t) = tt;
2325
2326 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2327 result based on those same flags for the arguments. But if the
2328 arguments aren't really even `tree' expressions, we shouldn't be trying
2329 to do this. */
2330 fro = first_rtl_op (code);
2331
2332 /* Expressions without side effects may be constant if their
2333 arguments are as well. */
2334 constant = (TREE_CODE_CLASS (code) == '<'
2335 || TREE_CODE_CLASS (code) == '1'
2336 || TREE_CODE_CLASS (code) == '2'
2337 || TREE_CODE_CLASS (code) == 'c');
2338
2339 if (length == 2)
2340 {
2341 /* This is equivalent to the loop below, but faster. */
2342 tree arg0 = va_arg (p, tree);
2343 tree arg1 = va_arg (p, tree);
2344
2345 TREE_OPERAND (t, 0) = arg0;
2346 TREE_OPERAND (t, 1) = arg1;
2347 TREE_READONLY (t) = 1;
2348 if (arg0 && fro > 0)
2349 {
2350 if (TREE_SIDE_EFFECTS (arg0))
2351 TREE_SIDE_EFFECTS (t) = 1;
2352 if (!TREE_READONLY (arg0))
2353 TREE_READONLY (t) = 0;
2354 if (!TREE_CONSTANT (arg0))
2355 constant = 0;
2356 }
2357
2358 if (arg1 && fro > 1)
2359 {
2360 if (TREE_SIDE_EFFECTS (arg1))
2361 TREE_SIDE_EFFECTS (t) = 1;
2362 if (!TREE_READONLY (arg1))
2363 TREE_READONLY (t) = 0;
2364 if (!TREE_CONSTANT (arg1))
2365 constant = 0;
2366 }
2367 }
2368 else if (length == 1)
2369 {
2370 tree arg0 = va_arg (p, tree);
2371
2372 /* The only one-operand cases we handle here are those with side-effects.
2373 Others are handled with build1. So don't bother checked if the
2374 arg has side-effects since we'll already have set it.
2375
2376 ??? This really should use build1 too. */
2377 if (TREE_CODE_CLASS (code) != 's')
2378 abort ();
2379 TREE_OPERAND (t, 0) = arg0;
2380 }
2381 else
2382 {
2383 for (i = 0; i < length; i++)
2384 {
2385 tree operand = va_arg (p, tree);
2386
2387 TREE_OPERAND (t, i) = operand;
2388 if (operand && fro > i)
2389 {
2390 if (TREE_SIDE_EFFECTS (operand))
2391 TREE_SIDE_EFFECTS (t) = 1;
2392 if (!TREE_CONSTANT (operand))
2393 constant = 0;
2394 }
2395 }
2396 }
2397 va_end (p);
2398
2399 TREE_CONSTANT (t) = constant;
2400
2401 if (code == CALL_EXPR && !TREE_SIDE_EFFECTS (t))
2402 {
2403 /* Calls have side-effects, except those to const or
2404 pure functions. */
2405 i = call_expr_flags (t);
2406 if (!(i & (ECF_CONST | ECF_PURE)))
2407 TREE_SIDE_EFFECTS (t) = 1;
2408
2409 /* And even those have side-effects if their arguments do. */
2410 else for (node = TREE_OPERAND (t, 1); node; node = TREE_CHAIN (node))
2411 if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
2412 {
2413 TREE_SIDE_EFFECTS (t) = 1;
2414 break;
2415 }
2416 }
2417
2418 return t;
2419 }
2420
2421 /* Same as above, but only builds for unary operators.
2422 Saves lions share of calls to `build'; cuts down use
2423 of varargs, which is expensive for RISC machines. */
2424
2425 tree
2426 build1 (enum tree_code code, tree type, tree node)
2427 {
2428 int length = sizeof (struct tree_exp);
2429 #ifdef GATHER_STATISTICS
2430 tree_node_kind kind;
2431 #endif
2432 tree t;
2433
2434 #ifdef GATHER_STATISTICS
2435 switch (TREE_CODE_CLASS (code))
2436 {
2437 case 's': /* an expression with side effects */
2438 kind = s_kind;
2439 break;
2440 case 'r': /* a reference */
2441 kind = r_kind;
2442 break;
2443 default:
2444 kind = e_kind;
2445 break;
2446 }
2447
2448 tree_node_counts[(int) kind]++;
2449 tree_node_sizes[(int) kind] += length;
2450 #endif
2451
2452 #ifdef ENABLE_CHECKING
2453 if (TREE_CODE_CLASS (code) == '2'
2454 || TREE_CODE_CLASS (code) == '<'
2455 || TREE_CODE_LENGTH (code) != 1)
2456 abort ();
2457 #endif /* ENABLE_CHECKING */
2458
2459 t = ggc_alloc_tree (length);
2460
2461 memset (t, 0, sizeof (struct tree_common));
2462
2463 TREE_SET_CODE (t, code);
2464
2465 TREE_TYPE (t) = type;
2466 TREE_COMPLEXITY (t) = 0;
2467 TREE_OPERAND (t, 0) = node;
2468 if (node && first_rtl_op (code) != 0)
2469 {
2470 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2471 TREE_READONLY (t) = TREE_READONLY (node);
2472 }
2473
2474 if (TREE_CODE_CLASS (code) == 's')
2475 TREE_SIDE_EFFECTS (t) = 1;
2476 else switch (code)
2477 {
2478 case INIT_EXPR:
2479 case MODIFY_EXPR:
2480 case VA_ARG_EXPR:
2481 case RTL_EXPR:
2482 case PREDECREMENT_EXPR:
2483 case PREINCREMENT_EXPR:
2484 case POSTDECREMENT_EXPR:
2485 case POSTINCREMENT_EXPR:
2486 /* All of these have side-effects, no matter what their
2487 operands are. */
2488 TREE_SIDE_EFFECTS (t) = 1;
2489 TREE_READONLY (t) = 0;
2490 break;
2491
2492 case INDIRECT_REF:
2493 /* Whether a dereference is readonly has nothing to do with whether
2494 its operand is readonly. */
2495 TREE_READONLY (t) = 0;
2496 break;
2497
2498 case ADDR_EXPR:
2499 if (node)
2500 {
2501 /* The address of a volatile decl or reference does not have
2502 side-effects. But be careful not to ignore side-effects from
2503 other sources deeper in the expression--if node is a _REF and
2504 one of its operands has side-effects, so do we. */
2505 if (TREE_THIS_VOLATILE (node))
2506 {
2507 TREE_SIDE_EFFECTS (t) = 0;
2508 if (!DECL_P (node))
2509 {
2510 int i = first_rtl_op (TREE_CODE (node)) - 1;
2511 for (; i >= 0; --i)
2512 {
2513 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, i)))
2514 TREE_SIDE_EFFECTS (t) = 1;
2515 }
2516 }
2517 }
2518 }
2519 break;
2520
2521 default:
2522 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2523 TREE_CONSTANT (t) = 1;
2524 break;
2525 }
2526
2527 return t;
2528 }
2529
2530 /* Similar except don't specify the TREE_TYPE
2531 and leave the TREE_SIDE_EFFECTS as 0.
2532 It is permissible for arguments to be null,
2533 or even garbage if their values do not matter. */
2534
2535 tree
2536 build_nt (enum tree_code code, ...)
2537 {
2538 tree t;
2539 int length;
2540 int i;
2541 va_list p;
2542
2543 va_start (p, code);
2544
2545 t = make_node (code);
2546 length = TREE_CODE_LENGTH (code);
2547
2548 for (i = 0; i < length; i++)
2549 TREE_OPERAND (t, i) = va_arg (p, tree);
2550
2551 va_end (p);
2552 return t;
2553 }
2554 \f
2555 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2556 We do NOT enter this node in any sort of symbol table.
2557
2558 layout_decl is used to set up the decl's storage layout.
2559 Other slots are initialized to 0 or null pointers. */
2560
2561 tree
2562 build_decl (enum tree_code code, tree name, tree type)
2563 {
2564 tree t;
2565
2566 t = make_node (code);
2567
2568 /* if (type == error_mark_node)
2569 type = integer_type_node; */
2570 /* That is not done, deliberately, so that having error_mark_node
2571 as the type can suppress useless errors in the use of this variable. */
2572
2573 DECL_NAME (t) = name;
2574 TREE_TYPE (t) = type;
2575
2576 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2577 layout_decl (t, 0);
2578 else if (code == FUNCTION_DECL)
2579 DECL_MODE (t) = FUNCTION_MODE;
2580
2581 return t;
2582 }
2583 \f
2584 /* BLOCK nodes are used to represent the structure of binding contours
2585 and declarations, once those contours have been exited and their contents
2586 compiled. This information is used for outputting debugging info. */
2587
2588 tree
2589 build_block (tree vars, tree tags ATTRIBUTE_UNUSED, tree subblocks,
2590 tree supercontext, tree chain)
2591 {
2592 tree block = make_node (BLOCK);
2593
2594 BLOCK_VARS (block) = vars;
2595 BLOCK_SUBBLOCKS (block) = subblocks;
2596 BLOCK_SUPERCONTEXT (block) = supercontext;
2597 BLOCK_CHAIN (block) = chain;
2598 return block;
2599 }
2600
2601 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2602 location where an expression or an identifier were encountered. It
2603 is necessary for languages where the frontend parser will handle
2604 recursively more than one file (Java is one of them). */
2605
2606 tree
2607 build_expr_wfl (tree node, const char *file, int line, int col)
2608 {
2609 static const char *last_file = 0;
2610 static tree last_filenode = NULL_TREE;
2611 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2612
2613 EXPR_WFL_NODE (wfl) = node;
2614 EXPR_WFL_SET_LINECOL (wfl, line, col);
2615 if (file != last_file)
2616 {
2617 last_file = file;
2618 last_filenode = file ? get_identifier (file) : NULL_TREE;
2619 }
2620
2621 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2622 if (node)
2623 {
2624 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2625 TREE_TYPE (wfl) = TREE_TYPE (node);
2626 }
2627
2628 return wfl;
2629 }
2630 \f
2631 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2632 is ATTRIBUTE. */
2633
2634 tree
2635 build_decl_attribute_variant (tree ddecl, tree attribute)
2636 {
2637 DECL_ATTRIBUTES (ddecl) = attribute;
2638 return ddecl;
2639 }
2640
2641 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2642 is ATTRIBUTE.
2643
2644 Record such modified types already made so we don't make duplicates. */
2645
2646 tree
2647 build_type_attribute_variant (tree ttype, tree attribute)
2648 {
2649 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2650 {
2651 unsigned int hashcode;
2652 tree ntype;
2653
2654 ntype = copy_node (ttype);
2655
2656 TYPE_POINTER_TO (ntype) = 0;
2657 TYPE_REFERENCE_TO (ntype) = 0;
2658 TYPE_ATTRIBUTES (ntype) = attribute;
2659
2660 /* Create a new main variant of TYPE. */
2661 TYPE_MAIN_VARIANT (ntype) = ntype;
2662 TYPE_NEXT_VARIANT (ntype) = 0;
2663 set_type_quals (ntype, TYPE_UNQUALIFIED);
2664
2665 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2666 + TYPE_HASH (TREE_TYPE (ntype))
2667 + attribute_hash_list (attribute));
2668
2669 switch (TREE_CODE (ntype))
2670 {
2671 case FUNCTION_TYPE:
2672 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2673 break;
2674 case ARRAY_TYPE:
2675 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2676 break;
2677 case INTEGER_TYPE:
2678 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2679 break;
2680 case REAL_TYPE:
2681 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2682 break;
2683 default:
2684 break;
2685 }
2686
2687 ntype = type_hash_canon (hashcode, ntype);
2688 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2689 }
2690
2691 return ttype;
2692 }
2693
2694 /* Return nonzero if IDENT is a valid name for attribute ATTR,
2695 or zero if not.
2696
2697 We try both `text' and `__text__', ATTR may be either one. */
2698 /* ??? It might be a reasonable simplification to require ATTR to be only
2699 `text'. One might then also require attribute lists to be stored in
2700 their canonicalized form. */
2701
2702 int
2703 is_attribute_p (const char *attr, tree ident)
2704 {
2705 int ident_len, attr_len;
2706 const char *p;
2707
2708 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2709 return 0;
2710
2711 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2712 return 1;
2713
2714 p = IDENTIFIER_POINTER (ident);
2715 ident_len = strlen (p);
2716 attr_len = strlen (attr);
2717
2718 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2719 if (attr[0] == '_')
2720 {
2721 if (attr[1] != '_'
2722 || attr[attr_len - 2] != '_'
2723 || attr[attr_len - 1] != '_')
2724 abort ();
2725 if (ident_len == attr_len - 4
2726 && strncmp (attr + 2, p, attr_len - 4) == 0)
2727 return 1;
2728 }
2729 else
2730 {
2731 if (ident_len == attr_len + 4
2732 && p[0] == '_' && p[1] == '_'
2733 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2734 && strncmp (attr, p + 2, attr_len) == 0)
2735 return 1;
2736 }
2737
2738 return 0;
2739 }
2740
2741 /* Given an attribute name and a list of attributes, return a pointer to the
2742 attribute's list element if the attribute is part of the list, or NULL_TREE
2743 if not found. If the attribute appears more than once, this only
2744 returns the first occurrence; the TREE_CHAIN of the return value should
2745 be passed back in if further occurrences are wanted. */
2746
2747 tree
2748 lookup_attribute (const char *attr_name, tree list)
2749 {
2750 tree l;
2751
2752 for (l = list; l; l = TREE_CHAIN (l))
2753 {
2754 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2755 abort ();
2756 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2757 return l;
2758 }
2759
2760 return NULL_TREE;
2761 }
2762
2763 /* Return an attribute list that is the union of a1 and a2. */
2764
2765 tree
2766 merge_attributes (tree a1, tree a2)
2767 {
2768 tree attributes;
2769
2770 /* Either one unset? Take the set one. */
2771
2772 if ((attributes = a1) == 0)
2773 attributes = a2;
2774
2775 /* One that completely contains the other? Take it. */
2776
2777 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2778 {
2779 if (attribute_list_contained (a2, a1))
2780 attributes = a2;
2781 else
2782 {
2783 /* Pick the longest list, and hang on the other list. */
2784
2785 if (list_length (a1) < list_length (a2))
2786 attributes = a2, a2 = a1;
2787
2788 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2789 {
2790 tree a;
2791 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2792 attributes);
2793 a != NULL_TREE;
2794 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2795 TREE_CHAIN (a)))
2796 {
2797 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2798 break;
2799 }
2800 if (a == NULL_TREE)
2801 {
2802 a1 = copy_node (a2);
2803 TREE_CHAIN (a1) = attributes;
2804 attributes = a1;
2805 }
2806 }
2807 }
2808 }
2809 return attributes;
2810 }
2811
2812 /* Given types T1 and T2, merge their attributes and return
2813 the result. */
2814
2815 tree
2816 merge_type_attributes (tree t1, tree t2)
2817 {
2818 return merge_attributes (TYPE_ATTRIBUTES (t1),
2819 TYPE_ATTRIBUTES (t2));
2820 }
2821
2822 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2823 the result. */
2824
2825 tree
2826 merge_decl_attributes (tree olddecl, tree newdecl)
2827 {
2828 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2829 DECL_ATTRIBUTES (newdecl));
2830 }
2831
2832 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2833
2834 /* Specialization of merge_decl_attributes for various Windows targets.
2835
2836 This handles the following situation:
2837
2838 __declspec (dllimport) int foo;
2839 int foo;
2840
2841 The second instance of `foo' nullifies the dllimport. */
2842
2843 tree
2844 merge_dllimport_decl_attributes (tree old, tree new)
2845 {
2846 tree a;
2847 int delete_dllimport_p;
2848
2849 old = DECL_ATTRIBUTES (old);
2850 new = DECL_ATTRIBUTES (new);
2851
2852 /* What we need to do here is remove from `old' dllimport if it doesn't
2853 appear in `new'. dllimport behaves like extern: if a declaration is
2854 marked dllimport and a definition appears later, then the object
2855 is not dllimport'd. */
2856 if (lookup_attribute ("dllimport", old) != NULL_TREE
2857 && lookup_attribute ("dllimport", new) == NULL_TREE)
2858 delete_dllimport_p = 1;
2859 else
2860 delete_dllimport_p = 0;
2861
2862 a = merge_attributes (old, new);
2863
2864 if (delete_dllimport_p)
2865 {
2866 tree prev, t;
2867
2868 /* Scan the list for dllimport and delete it. */
2869 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2870 {
2871 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2872 {
2873 if (prev == NULL_TREE)
2874 a = TREE_CHAIN (a);
2875 else
2876 TREE_CHAIN (prev) = TREE_CHAIN (t);
2877 break;
2878 }
2879 }
2880 }
2881
2882 return a;
2883 }
2884
2885 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2886 \f
2887 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2888 of the various TYPE_QUAL values. */
2889
2890 static void
2891 set_type_quals (tree type, int type_quals)
2892 {
2893 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2894 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2895 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2896 }
2897
2898 /* Return a version of the TYPE, qualified as indicated by the
2899 TYPE_QUALS, if one exists. If no qualified version exists yet,
2900 return NULL_TREE. */
2901
2902 tree
2903 get_qualified_type (tree type, int type_quals)
2904 {
2905 tree t;
2906
2907 /* Search the chain of variants to see if there is already one there just
2908 like the one we need to have. If so, use that existing one. We must
2909 preserve the TYPE_NAME, since there is code that depends on this. */
2910 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2911 if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type)
2912 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
2913 && attribute_list_equal (TYPE_ATTRIBUTES (t), TYPE_ATTRIBUTES (type)))
2914 return t;
2915
2916 return NULL_TREE;
2917 }
2918
2919 /* Like get_qualified_type, but creates the type if it does not
2920 exist. This function never returns NULL_TREE. */
2921
2922 tree
2923 build_qualified_type (tree type, int type_quals)
2924 {
2925 tree t;
2926
2927 /* See if we already have the appropriate qualified variant. */
2928 t = get_qualified_type (type, type_quals);
2929
2930 /* If not, build it. */
2931 if (!t)
2932 {
2933 t = build_type_copy (type);
2934 set_type_quals (t, type_quals);
2935 }
2936
2937 return t;
2938 }
2939
2940 /* Create a new variant of TYPE, equivalent but distinct.
2941 This is so the caller can modify it. */
2942
2943 tree
2944 build_type_copy (tree type)
2945 {
2946 tree t, m = TYPE_MAIN_VARIANT (type);
2947
2948 t = copy_node (type);
2949
2950 TYPE_POINTER_TO (t) = 0;
2951 TYPE_REFERENCE_TO (t) = 0;
2952
2953 /* Add this type to the chain of variants of TYPE. */
2954 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2955 TYPE_NEXT_VARIANT (m) = t;
2956
2957 return t;
2958 }
2959 \f
2960 /* Hashing of types so that we don't make duplicates.
2961 The entry point is `type_hash_canon'. */
2962
2963 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2964 with types in the TREE_VALUE slots), by adding the hash codes
2965 of the individual types. */
2966
2967 unsigned int
2968 type_hash_list (tree list)
2969 {
2970 unsigned int hashcode;
2971 tree tail;
2972
2973 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2974 hashcode += TYPE_HASH (TREE_VALUE (tail));
2975
2976 return hashcode;
2977 }
2978
2979 /* These are the Hashtable callback functions. */
2980
2981 /* Returns true if the types are equal. */
2982
2983 static int
2984 type_hash_eq (const void *va, const void *vb)
2985 {
2986 const struct type_hash *a = va, *b = vb;
2987 if (a->hash == b->hash
2988 && TREE_CODE (a->type) == TREE_CODE (b->type)
2989 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
2990 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
2991 TYPE_ATTRIBUTES (b->type))
2992 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
2993 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
2994 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
2995 TYPE_MAX_VALUE (b->type)))
2996 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
2997 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
2998 TYPE_MIN_VALUE (b->type)))
2999 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
3000 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
3001 || (TYPE_DOMAIN (a->type)
3002 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
3003 && TYPE_DOMAIN (b->type)
3004 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
3005 && type_list_equal (TYPE_DOMAIN (a->type),
3006 TYPE_DOMAIN (b->type)))))
3007 return 1;
3008 return 0;
3009 }
3010
3011 /* Return the cached hash value. */
3012
3013 static hashval_t
3014 type_hash_hash (const void *item)
3015 {
3016 return ((const struct type_hash *) item)->hash;
3017 }
3018
3019 /* Look in the type hash table for a type isomorphic to TYPE.
3020 If one is found, return it. Otherwise return 0. */
3021
3022 tree
3023 type_hash_lookup (unsigned int hashcode, tree type)
3024 {
3025 struct type_hash *h, in;
3026
3027 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3028 must call that routine before comparing TYPE_ALIGNs. */
3029 layout_type (type);
3030
3031 in.hash = hashcode;
3032 in.type = type;
3033
3034 h = htab_find_with_hash (type_hash_table, &in, hashcode);
3035 if (h)
3036 return h->type;
3037 return NULL_TREE;
3038 }
3039
3040 /* Add an entry to the type-hash-table
3041 for a type TYPE whose hash code is HASHCODE. */
3042
3043 void
3044 type_hash_add (unsigned int hashcode, tree type)
3045 {
3046 struct type_hash *h;
3047 void **loc;
3048
3049 h = ggc_alloc (sizeof (struct type_hash));
3050 h->hash = hashcode;
3051 h->type = type;
3052 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3053 *(struct type_hash **) loc = h;
3054 }
3055
3056 /* Given TYPE, and HASHCODE its hash code, return the canonical
3057 object for an identical type if one already exists.
3058 Otherwise, return TYPE, and record it as the canonical object
3059 if it is a permanent object.
3060
3061 To use this function, first create a type of the sort you want.
3062 Then compute its hash code from the fields of the type that
3063 make it different from other similar types.
3064 Then call this function and use the value.
3065 This function frees the type you pass in if it is a duplicate. */
3066
3067 /* Set to 1 to debug without canonicalization. Never set by program. */
3068 int debug_no_type_hash = 0;
3069
3070 tree
3071 type_hash_canon (unsigned int hashcode, tree type)
3072 {
3073 tree t1;
3074
3075 if (debug_no_type_hash)
3076 return type;
3077
3078 /* See if the type is in the hash table already. If so, return it.
3079 Otherwise, add the type. */
3080 t1 = type_hash_lookup (hashcode, type);
3081 if (t1 != 0)
3082 {
3083 #ifdef GATHER_STATISTICS
3084 tree_node_counts[(int) t_kind]--;
3085 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3086 #endif
3087 return t1;
3088 }
3089 else
3090 {
3091 type_hash_add (hashcode, type);
3092 return type;
3093 }
3094 }
3095
3096 /* See if the data pointed to by the type hash table is marked. We consider
3097 it marked if the type is marked or if a debug type number or symbol
3098 table entry has been made for the type. This reduces the amount of
3099 debugging output and eliminates that dependency of the debug output on
3100 the number of garbage collections. */
3101
3102 static int
3103 type_hash_marked_p (const void *p)
3104 {
3105 tree type = ((struct type_hash *) p)->type;
3106
3107 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3108 }
3109
3110 static void
3111 print_type_hash_statistics (void)
3112 {
3113 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3114 (long) htab_size (type_hash_table),
3115 (long) htab_elements (type_hash_table),
3116 htab_collisions (type_hash_table));
3117 }
3118
3119 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3120 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3121 by adding the hash codes of the individual attributes. */
3122
3123 unsigned int
3124 attribute_hash_list (tree list)
3125 {
3126 unsigned int hashcode;
3127 tree tail;
3128
3129 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3130 /* ??? Do we want to add in TREE_VALUE too? */
3131 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3132 return hashcode;
3133 }
3134
3135 /* Given two lists of attributes, return true if list l2 is
3136 equivalent to l1. */
3137
3138 int
3139 attribute_list_equal (tree l1, tree l2)
3140 {
3141 return attribute_list_contained (l1, l2)
3142 && attribute_list_contained (l2, l1);
3143 }
3144
3145 /* Given two lists of attributes, return true if list L2 is
3146 completely contained within L1. */
3147 /* ??? This would be faster if attribute names were stored in a canonicalized
3148 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3149 must be used to show these elements are equivalent (which they are). */
3150 /* ??? It's not clear that attributes with arguments will always be handled
3151 correctly. */
3152
3153 int
3154 attribute_list_contained (tree l1, tree l2)
3155 {
3156 tree t1, t2;
3157
3158 /* First check the obvious, maybe the lists are identical. */
3159 if (l1 == l2)
3160 return 1;
3161
3162 /* Maybe the lists are similar. */
3163 for (t1 = l1, t2 = l2;
3164 t1 != 0 && t2 != 0
3165 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3166 && TREE_VALUE (t1) == TREE_VALUE (t2);
3167 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3168
3169 /* Maybe the lists are equal. */
3170 if (t1 == 0 && t2 == 0)
3171 return 1;
3172
3173 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3174 {
3175 tree attr;
3176 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3177 attr != NULL_TREE;
3178 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3179 TREE_CHAIN (attr)))
3180 {
3181 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3182 break;
3183 }
3184
3185 if (attr == 0)
3186 return 0;
3187
3188 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3189 return 0;
3190 }
3191
3192 return 1;
3193 }
3194
3195 /* Given two lists of types
3196 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3197 return 1 if the lists contain the same types in the same order.
3198 Also, the TREE_PURPOSEs must match. */
3199
3200 int
3201 type_list_equal (tree l1, tree l2)
3202 {
3203 tree t1, t2;
3204
3205 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3206 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3207 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3208 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3209 && (TREE_TYPE (TREE_PURPOSE (t1))
3210 == TREE_TYPE (TREE_PURPOSE (t2))))))
3211 return 0;
3212
3213 return t1 == t2;
3214 }
3215
3216 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3217 given by TYPE. If the argument list accepts variable arguments,
3218 then this function counts only the ordinary arguments. */
3219
3220 int
3221 type_num_arguments (tree type)
3222 {
3223 int i = 0;
3224 tree t;
3225
3226 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3227 /* If the function does not take a variable number of arguments,
3228 the last element in the list will have type `void'. */
3229 if (VOID_TYPE_P (TREE_VALUE (t)))
3230 break;
3231 else
3232 ++i;
3233
3234 return i;
3235 }
3236
3237 /* Nonzero if integer constants T1 and T2
3238 represent the same constant value. */
3239
3240 int
3241 tree_int_cst_equal (tree t1, tree t2)
3242 {
3243 if (t1 == t2)
3244 return 1;
3245
3246 if (t1 == 0 || t2 == 0)
3247 return 0;
3248
3249 if (TREE_CODE (t1) == INTEGER_CST
3250 && TREE_CODE (t2) == INTEGER_CST
3251 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3252 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3253 return 1;
3254
3255 return 0;
3256 }
3257
3258 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3259 The precise way of comparison depends on their data type. */
3260
3261 int
3262 tree_int_cst_lt (tree t1, tree t2)
3263 {
3264 if (t1 == t2)
3265 return 0;
3266
3267 if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3268 {
3269 int t1_sgn = tree_int_cst_sgn (t1);
3270 int t2_sgn = tree_int_cst_sgn (t2);
3271
3272 if (t1_sgn < t2_sgn)
3273 return 1;
3274 else if (t1_sgn > t2_sgn)
3275 return 0;
3276 /* Otherwise, both are non-negative, so we compare them as
3277 unsigned just in case one of them would overflow a signed
3278 type. */
3279 }
3280 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3281 return INT_CST_LT (t1, t2);
3282
3283 return INT_CST_LT_UNSIGNED (t1, t2);
3284 }
3285
3286 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3287
3288 int
3289 tree_int_cst_compare (tree t1, tree t2)
3290 {
3291 if (tree_int_cst_lt (t1, t2))
3292 return -1;
3293 else if (tree_int_cst_lt (t2, t1))
3294 return 1;
3295 else
3296 return 0;
3297 }
3298
3299 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3300 the host. If POS is zero, the value can be represented in a single
3301 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3302 be represented in a single unsigned HOST_WIDE_INT. */
3303
3304 int
3305 host_integerp (tree t, int pos)
3306 {
3307 return (TREE_CODE (t) == INTEGER_CST
3308 && ! TREE_OVERFLOW (t)
3309 && ((TREE_INT_CST_HIGH (t) == 0
3310 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3311 || (! pos && TREE_INT_CST_HIGH (t) == -1
3312 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3313 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3314 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3315 }
3316
3317 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3318 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3319 be positive. Abort if we cannot satisfy the above conditions. */
3320
3321 HOST_WIDE_INT
3322 tree_low_cst (tree t, int pos)
3323 {
3324 if (host_integerp (t, pos))
3325 return TREE_INT_CST_LOW (t);
3326 else
3327 abort ();
3328 }
3329
3330 /* Return the most significant bit of the integer constant T. */
3331
3332 int
3333 tree_int_cst_msb (tree t)
3334 {
3335 int prec;
3336 HOST_WIDE_INT h;
3337 unsigned HOST_WIDE_INT l;
3338
3339 /* Note that using TYPE_PRECISION here is wrong. We care about the
3340 actual bits, not the (arbitrary) range of the type. */
3341 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3342 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3343 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3344 return (l & 1) == 1;
3345 }
3346
3347 /* Return an indication of the sign of the integer constant T.
3348 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3349 Note that -1 will never be returned it T's type is unsigned. */
3350
3351 int
3352 tree_int_cst_sgn (tree t)
3353 {
3354 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3355 return 0;
3356 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3357 return 1;
3358 else if (TREE_INT_CST_HIGH (t) < 0)
3359 return -1;
3360 else
3361 return 1;
3362 }
3363
3364 /* Compare two constructor-element-type constants. Return 1 if the lists
3365 are known to be equal; otherwise return 0. */
3366
3367 int
3368 simple_cst_list_equal (tree l1, tree l2)
3369 {
3370 while (l1 != NULL_TREE && l2 != NULL_TREE)
3371 {
3372 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3373 return 0;
3374
3375 l1 = TREE_CHAIN (l1);
3376 l2 = TREE_CHAIN (l2);
3377 }
3378
3379 return l1 == l2;
3380 }
3381
3382 /* Return truthvalue of whether T1 is the same tree structure as T2.
3383 Return 1 if they are the same.
3384 Return 0 if they are understandably different.
3385 Return -1 if either contains tree structure not understood by
3386 this function. */
3387
3388 int
3389 simple_cst_equal (tree t1, tree t2)
3390 {
3391 enum tree_code code1, code2;
3392 int cmp;
3393 int i;
3394
3395 if (t1 == t2)
3396 return 1;
3397 if (t1 == 0 || t2 == 0)
3398 return 0;
3399
3400 code1 = TREE_CODE (t1);
3401 code2 = TREE_CODE (t2);
3402
3403 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3404 {
3405 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3406 || code2 == NON_LVALUE_EXPR)
3407 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3408 else
3409 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3410 }
3411
3412 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3413 || code2 == NON_LVALUE_EXPR)
3414 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3415
3416 if (code1 != code2)
3417 return 0;
3418
3419 switch (code1)
3420 {
3421 case INTEGER_CST:
3422 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3423 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3424
3425 case REAL_CST:
3426 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3427
3428 case STRING_CST:
3429 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3430 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3431 TREE_STRING_LENGTH (t1)));
3432
3433 case CONSTRUCTOR:
3434 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3435 return 1;
3436 else
3437 abort ();
3438
3439 case SAVE_EXPR:
3440 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3441
3442 case CALL_EXPR:
3443 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3444 if (cmp <= 0)
3445 return cmp;
3446 return
3447 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3448
3449 case TARGET_EXPR:
3450 /* Special case: if either target is an unallocated VAR_DECL,
3451 it means that it's going to be unified with whatever the
3452 TARGET_EXPR is really supposed to initialize, so treat it
3453 as being equivalent to anything. */
3454 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3455 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3456 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3457 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3458 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3459 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3460 cmp = 1;
3461 else
3462 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3463
3464 if (cmp <= 0)
3465 return cmp;
3466
3467 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3468
3469 case WITH_CLEANUP_EXPR:
3470 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3471 if (cmp <= 0)
3472 return cmp;
3473
3474 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3475
3476 case COMPONENT_REF:
3477 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3478 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3479
3480 return 0;
3481
3482 case VAR_DECL:
3483 case PARM_DECL:
3484 case CONST_DECL:
3485 case FUNCTION_DECL:
3486 return 0;
3487
3488 default:
3489 break;
3490 }
3491
3492 /* This general rule works for most tree codes. All exceptions should be
3493 handled above. If this is a language-specific tree code, we can't
3494 trust what might be in the operand, so say we don't know
3495 the situation. */
3496 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3497 return -1;
3498
3499 switch (TREE_CODE_CLASS (code1))
3500 {
3501 case '1':
3502 case '2':
3503 case '<':
3504 case 'e':
3505 case 'r':
3506 case 's':
3507 cmp = 1;
3508 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3509 {
3510 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3511 if (cmp <= 0)
3512 return cmp;
3513 }
3514
3515 return cmp;
3516
3517 default:
3518 return -1;
3519 }
3520 }
3521
3522 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3523 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3524 than U, respectively. */
3525
3526 int
3527 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3528 {
3529 if (tree_int_cst_sgn (t) < 0)
3530 return -1;
3531 else if (TREE_INT_CST_HIGH (t) != 0)
3532 return 1;
3533 else if (TREE_INT_CST_LOW (t) == u)
3534 return 0;
3535 else if (TREE_INT_CST_LOW (t) < u)
3536 return -1;
3537 else
3538 return 1;
3539 }
3540
3541 /* Return true if CODE represents an associative tree code. Otherwise
3542 return false. */
3543 bool
3544 associative_tree_code (enum tree_code code)
3545 {
3546 switch (code)
3547 {
3548 case BIT_IOR_EXPR:
3549 case BIT_AND_EXPR:
3550 case BIT_XOR_EXPR:
3551 case PLUS_EXPR:
3552 case MINUS_EXPR:
3553 case MULT_EXPR:
3554 case LSHIFT_EXPR:
3555 case RSHIFT_EXPR:
3556 case MIN_EXPR:
3557 case MAX_EXPR:
3558 return true;
3559
3560 default:
3561 break;
3562 }
3563 return false;
3564 }
3565
3566 /* Return true if CODE represents an commutative tree code. Otherwise
3567 return false. */
3568 bool
3569 commutative_tree_code (enum tree_code code)
3570 {
3571 switch (code)
3572 {
3573 case PLUS_EXPR:
3574 case MULT_EXPR:
3575 case MIN_EXPR:
3576 case MAX_EXPR:
3577 case BIT_IOR_EXPR:
3578 case BIT_XOR_EXPR:
3579 case BIT_AND_EXPR:
3580 case NE_EXPR:
3581 case EQ_EXPR:
3582 return true;
3583
3584 default:
3585 break;
3586 }
3587 return false;
3588 }
3589
3590 /* Generate a hash value for an expression. This can be used iteratively
3591 by passing a previous result as the "val" argument.
3592
3593 This function is intended to produce the same hash for expressions which
3594 would compare equal using operand_equal_p. */
3595
3596 hashval_t
3597 iterative_hash_expr (tree t, hashval_t val)
3598 {
3599 int i;
3600 enum tree_code code;
3601 char class;
3602
3603 if (t == NULL_TREE)
3604 return iterative_hash_object (t, val);
3605
3606 code = TREE_CODE (t);
3607 class = TREE_CODE_CLASS (code);
3608
3609 if (class == 'd')
3610 {
3611 /* Decls we can just compare by pointer. */
3612 val = iterative_hash_object (t, val);
3613 }
3614 else if (class == 'c')
3615 {
3616 /* Alas, constants aren't shared, so we can't rely on pointer
3617 identity. */
3618 if (code == INTEGER_CST)
3619 {
3620 val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3621 val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3622 }
3623 else if (code == REAL_CST)
3624 val = iterative_hash (TREE_REAL_CST_PTR (t),
3625 sizeof (REAL_VALUE_TYPE), val);
3626 else if (code == STRING_CST)
3627 val = iterative_hash (TREE_STRING_POINTER (t),
3628 TREE_STRING_LENGTH (t), val);
3629 else if (code == COMPLEX_CST)
3630 {
3631 val = iterative_hash_expr (TREE_REALPART (t), val);
3632 val = iterative_hash_expr (TREE_IMAGPART (t), val);
3633 }
3634 else if (code == VECTOR_CST)
3635 val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3636 else
3637 abort ();
3638 }
3639 else if (IS_EXPR_CODE_CLASS (class))
3640 {
3641 val = iterative_hash_object (code, val);
3642
3643 if (code == NOP_EXPR || code == CONVERT_EXPR
3644 || code == NON_LVALUE_EXPR)
3645 val = iterative_hash_object (TREE_TYPE (t), val);
3646
3647 if (commutative_tree_code (code))
3648 {
3649 /* It's a commutative expression. We want to hash it the same
3650 however it appears. We do this by first hashing both operands
3651 and then rehashing based on the order of their independent
3652 hashes. */
3653 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3654 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3655 hashval_t t;
3656
3657 if (one > two)
3658 t = one, one = two, two = t;
3659
3660 val = iterative_hash_object (one, val);
3661 val = iterative_hash_object (two, val);
3662 }
3663 else
3664 for (i = first_rtl_op (code) - 1; i >= 0; --i)
3665 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3666 }
3667 else if (code == TREE_LIST)
3668 {
3669 /* A list of expressions, for a CALL_EXPR or as the elements of a
3670 VECTOR_CST. */
3671 for (; t; t = TREE_CHAIN (t))
3672 val = iterative_hash_expr (TREE_VALUE (t), val);
3673 }
3674 else
3675 abort ();
3676
3677 return val;
3678 }
3679 \f
3680 /* Constructors for pointer, array and function types.
3681 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3682 constructed by language-dependent code, not here.) */
3683
3684 /* Construct, lay out and return the type of pointers to TO_TYPE
3685 with mode MODE. If such a type has already been constructed,
3686 reuse it. */
3687
3688 tree
3689 build_pointer_type_for_mode (tree to_type, enum machine_mode mode)
3690 {
3691 tree t = TYPE_POINTER_TO (to_type);
3692
3693 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3694 if (t != 0 && mode == ptr_mode)
3695 return t;
3696
3697 t = make_node (POINTER_TYPE);
3698
3699 TREE_TYPE (t) = to_type;
3700 TYPE_MODE (t) = mode;
3701
3702 /* Record this type as the pointer to TO_TYPE. */
3703 if (mode == ptr_mode)
3704 TYPE_POINTER_TO (to_type) = t;
3705
3706 /* Lay out the type. This function has many callers that are concerned
3707 with expression-construction, and this simplifies them all.
3708 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3709 layout_type (t);
3710
3711 return t;
3712 }
3713
3714 /* By default build pointers in ptr_mode. */
3715
3716 tree
3717 build_pointer_type (tree to_type)
3718 {
3719 return build_pointer_type_for_mode (to_type, ptr_mode);
3720 }
3721
3722 /* Construct, lay out and return the type of references to TO_TYPE
3723 with mode MODE. If such a type has already been constructed,
3724 reuse it. */
3725
3726 tree
3727 build_reference_type_for_mode (tree to_type, enum machine_mode mode)
3728 {
3729 tree t = TYPE_REFERENCE_TO (to_type);
3730
3731 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3732 if (t != 0 && mode == ptr_mode)
3733 return t;
3734
3735 t = make_node (REFERENCE_TYPE);
3736
3737 TREE_TYPE (t) = to_type;
3738 TYPE_MODE (t) = mode;
3739
3740 /* Record this type as the pointer to TO_TYPE. */
3741 if (mode == ptr_mode)
3742 TYPE_REFERENCE_TO (to_type) = t;
3743
3744 layout_type (t);
3745
3746 return t;
3747 }
3748
3749
3750 /* Build the node for the type of references-to-TO_TYPE by default
3751 in ptr_mode. */
3752
3753 tree
3754 build_reference_type (tree to_type)
3755 {
3756 return build_reference_type_for_mode (to_type, ptr_mode);
3757 }
3758
3759 /* Build a type that is compatible with t but has no cv quals anywhere
3760 in its type, thus
3761
3762 const char *const *const * -> char ***. */
3763
3764 tree
3765 build_type_no_quals (tree t)
3766 {
3767 switch (TREE_CODE (t))
3768 {
3769 case POINTER_TYPE:
3770 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3771 case REFERENCE_TYPE:
3772 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3773 default:
3774 return TYPE_MAIN_VARIANT (t);
3775 }
3776 }
3777
3778 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3779 MAXVAL should be the maximum value in the domain
3780 (one less than the length of the array).
3781
3782 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3783 We don't enforce this limit, that is up to caller (e.g. language front end).
3784 The limit exists because the result is a signed type and we don't handle
3785 sizes that use more than one HOST_WIDE_INT. */
3786
3787 tree
3788 build_index_type (tree maxval)
3789 {
3790 tree itype = make_node (INTEGER_TYPE);
3791
3792 TREE_TYPE (itype) = sizetype;
3793 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3794 TYPE_MIN_VALUE (itype) = size_zero_node;
3795 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3796 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3797 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3798 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3799 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3800 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3801
3802 if (host_integerp (maxval, 1))
3803 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3804 else
3805 return itype;
3806 }
3807
3808 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3809 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3810 low bound LOWVAL and high bound HIGHVAL.
3811 if TYPE==NULL_TREE, sizetype is used. */
3812
3813 tree
3814 build_range_type (tree type, tree lowval, tree highval)
3815 {
3816 tree itype = make_node (INTEGER_TYPE);
3817
3818 TREE_TYPE (itype) = type;
3819 if (type == NULL_TREE)
3820 type = sizetype;
3821
3822 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3823 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3824
3825 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3826 TYPE_MODE (itype) = TYPE_MODE (type);
3827 TYPE_SIZE (itype) = TYPE_SIZE (type);
3828 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3829 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3830 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3831
3832 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3833 return type_hash_canon (tree_low_cst (highval, 0)
3834 - tree_low_cst (lowval, 0),
3835 itype);
3836 else
3837 return itype;
3838 }
3839
3840 /* Just like build_index_type, but takes lowval and highval instead
3841 of just highval (maxval). */
3842
3843 tree
3844 build_index_2_type (tree lowval, tree highval)
3845 {
3846 return build_range_type (sizetype, lowval, highval);
3847 }
3848
3849 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3850 and number of elements specified by the range of values of INDEX_TYPE.
3851 If such a type has already been constructed, reuse it. */
3852
3853 tree
3854 build_array_type (tree elt_type, tree index_type)
3855 {
3856 tree t;
3857 unsigned int hashcode;
3858
3859 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3860 {
3861 error ("arrays of functions are not meaningful");
3862 elt_type = integer_type_node;
3863 }
3864
3865 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3866 build_pointer_type (elt_type);
3867
3868 /* Allocate the array after the pointer type,
3869 in case we free it in type_hash_canon. */
3870 t = make_node (ARRAY_TYPE);
3871 TREE_TYPE (t) = elt_type;
3872 TYPE_DOMAIN (t) = index_type;
3873
3874 if (index_type == 0)
3875 {
3876 return t;
3877 }
3878
3879 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3880 t = type_hash_canon (hashcode, t);
3881
3882 if (!COMPLETE_TYPE_P (t))
3883 layout_type (t);
3884 return t;
3885 }
3886
3887 /* Return the TYPE of the elements comprising
3888 the innermost dimension of ARRAY. */
3889
3890 tree
3891 get_inner_array_type (tree array)
3892 {
3893 tree type = TREE_TYPE (array);
3894
3895 while (TREE_CODE (type) == ARRAY_TYPE)
3896 type = TREE_TYPE (type);
3897
3898 return type;
3899 }
3900
3901 /* Construct, lay out and return
3902 the type of functions returning type VALUE_TYPE
3903 given arguments of types ARG_TYPES.
3904 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3905 are data type nodes for the arguments of the function.
3906 If such a type has already been constructed, reuse it. */
3907
3908 tree
3909 build_function_type (tree value_type, tree arg_types)
3910 {
3911 tree t;
3912 unsigned int hashcode;
3913
3914 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3915 {
3916 error ("function return type cannot be function");
3917 value_type = integer_type_node;
3918 }
3919
3920 /* Make a node of the sort we want. */
3921 t = make_node (FUNCTION_TYPE);
3922 TREE_TYPE (t) = value_type;
3923 TYPE_ARG_TYPES (t) = arg_types;
3924
3925 /* If we already have such a type, use the old one and free this one. */
3926 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3927 t = type_hash_canon (hashcode, t);
3928
3929 if (!COMPLETE_TYPE_P (t))
3930 layout_type (t);
3931 return t;
3932 }
3933
3934 /* Build a function type. The RETURN_TYPE is the type returned by the
3935 function. If additional arguments are provided, they are
3936 additional argument types. The list of argument types must always
3937 be terminated by NULL_TREE. */
3938
3939 tree
3940 build_function_type_list (tree return_type, ...)
3941 {
3942 tree t, args, last;
3943 va_list p;
3944
3945 va_start (p, return_type);
3946
3947 t = va_arg (p, tree);
3948 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
3949 args = tree_cons (NULL_TREE, t, args);
3950
3951 last = args;
3952 args = nreverse (args);
3953 TREE_CHAIN (last) = void_list_node;
3954 args = build_function_type (return_type, args);
3955
3956 va_end (p);
3957 return args;
3958 }
3959
3960 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
3961 and ARGTYPES (a TREE_LIST) are the return type and arguments types
3962 for the method. An implicit additional parameter (of type
3963 pointer-to-BASETYPE) is added to the ARGTYPES. */
3964
3965 tree
3966 build_method_type_directly (tree basetype,
3967 tree rettype,
3968 tree argtypes)
3969 {
3970 tree t;
3971 tree ptype;
3972 int hashcode;
3973
3974 /* Make a node of the sort we want. */
3975 t = make_node (METHOD_TYPE);
3976
3977 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3978 TREE_TYPE (t) = rettype;
3979 ptype = build_pointer_type (basetype);
3980
3981 /* The actual arglist for this function includes a "hidden" argument
3982 which is "this". Put it into the list of argument types. */
3983 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
3984 TYPE_ARG_TYPES (t) = argtypes;
3985
3986 /* If we already have such a type, use the old one and free this one.
3987 Note that it also frees up the above cons cell if found. */
3988 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
3989 type_hash_list (argtypes);
3990
3991 t = type_hash_canon (hashcode, t);
3992
3993 if (!COMPLETE_TYPE_P (t))
3994 layout_type (t);
3995
3996 return t;
3997 }
3998
3999 /* Construct, lay out and return the type of methods belonging to class
4000 BASETYPE and whose arguments and values are described by TYPE.
4001 If that type exists already, reuse it.
4002 TYPE must be a FUNCTION_TYPE node. */
4003
4004 tree
4005 build_method_type (tree basetype, tree type)
4006 {
4007 if (TREE_CODE (type) != FUNCTION_TYPE)
4008 abort ();
4009
4010 return build_method_type_directly (basetype,
4011 TREE_TYPE (type),
4012 TYPE_ARG_TYPES (type));
4013 }
4014
4015 /* Construct, lay out and return the type of offsets to a value
4016 of type TYPE, within an object of type BASETYPE.
4017 If a suitable offset type exists already, reuse it. */
4018
4019 tree
4020 build_offset_type (tree basetype, tree type)
4021 {
4022 tree t;
4023 unsigned int hashcode;
4024
4025 /* Make a node of the sort we want. */
4026 t = make_node (OFFSET_TYPE);
4027
4028 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4029 TREE_TYPE (t) = type;
4030
4031 /* If we already have such a type, use the old one and free this one. */
4032 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4033 t = type_hash_canon (hashcode, t);
4034
4035 if (!COMPLETE_TYPE_P (t))
4036 layout_type (t);
4037
4038 return t;
4039 }
4040
4041 /* Create a complex type whose components are COMPONENT_TYPE. */
4042
4043 tree
4044 build_complex_type (tree component_type)
4045 {
4046 tree t;
4047 unsigned int hashcode;
4048
4049 /* Make a node of the sort we want. */
4050 t = make_node (COMPLEX_TYPE);
4051
4052 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4053 set_type_quals (t, TYPE_QUALS (component_type));
4054
4055 /* If we already have such a type, use the old one and free this one. */
4056 hashcode = TYPE_HASH (component_type);
4057 t = type_hash_canon (hashcode, t);
4058
4059 if (!COMPLETE_TYPE_P (t))
4060 layout_type (t);
4061
4062 /* If we are writing Dwarf2 output we need to create a name,
4063 since complex is a fundamental type. */
4064 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4065 && ! TYPE_NAME (t))
4066 {
4067 const char *name;
4068 if (component_type == char_type_node)
4069 name = "complex char";
4070 else if (component_type == signed_char_type_node)
4071 name = "complex signed char";
4072 else if (component_type == unsigned_char_type_node)
4073 name = "complex unsigned char";
4074 else if (component_type == short_integer_type_node)
4075 name = "complex short int";
4076 else if (component_type == short_unsigned_type_node)
4077 name = "complex short unsigned int";
4078 else if (component_type == integer_type_node)
4079 name = "complex int";
4080 else if (component_type == unsigned_type_node)
4081 name = "complex unsigned int";
4082 else if (component_type == long_integer_type_node)
4083 name = "complex long int";
4084 else if (component_type == long_unsigned_type_node)
4085 name = "complex long unsigned int";
4086 else if (component_type == long_long_integer_type_node)
4087 name = "complex long long int";
4088 else if (component_type == long_long_unsigned_type_node)
4089 name = "complex long long unsigned int";
4090 else
4091 name = 0;
4092
4093 if (name != 0)
4094 TYPE_NAME (t) = get_identifier (name);
4095 }
4096
4097 return t;
4098 }
4099 \f
4100 /* Return OP, stripped of any conversions to wider types as much as is safe.
4101 Converting the value back to OP's type makes a value equivalent to OP.
4102
4103 If FOR_TYPE is nonzero, we return a value which, if converted to
4104 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4105
4106 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4107 narrowest type that can hold the value, even if they don't exactly fit.
4108 Otherwise, bit-field references are changed to a narrower type
4109 only if they can be fetched directly from memory in that type.
4110
4111 OP must have integer, real or enumeral type. Pointers are not allowed!
4112
4113 There are some cases where the obvious value we could return
4114 would regenerate to OP if converted to OP's type,
4115 but would not extend like OP to wider types.
4116 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4117 For example, if OP is (unsigned short)(signed char)-1,
4118 we avoid returning (signed char)-1 if FOR_TYPE is int,
4119 even though extending that to an unsigned short would regenerate OP,
4120 since the result of extending (signed char)-1 to (int)
4121 is different from (int) OP. */
4122
4123 tree
4124 get_unwidened (tree op, tree for_type)
4125 {
4126 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4127 tree type = TREE_TYPE (op);
4128 unsigned final_prec
4129 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4130 int uns
4131 = (for_type != 0 && for_type != type
4132 && final_prec > TYPE_PRECISION (type)
4133 && TREE_UNSIGNED (type));
4134 tree win = op;
4135
4136 while (TREE_CODE (op) == NOP_EXPR)
4137 {
4138 int bitschange
4139 = TYPE_PRECISION (TREE_TYPE (op))
4140 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4141
4142 /* Truncations are many-one so cannot be removed.
4143 Unless we are later going to truncate down even farther. */
4144 if (bitschange < 0
4145 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4146 break;
4147
4148 /* See what's inside this conversion. If we decide to strip it,
4149 we will set WIN. */
4150 op = TREE_OPERAND (op, 0);
4151
4152 /* If we have not stripped any zero-extensions (uns is 0),
4153 we can strip any kind of extension.
4154 If we have previously stripped a zero-extension,
4155 only zero-extensions can safely be stripped.
4156 Any extension can be stripped if the bits it would produce
4157 are all going to be discarded later by truncating to FOR_TYPE. */
4158
4159 if (bitschange > 0)
4160 {
4161 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4162 win = op;
4163 /* TREE_UNSIGNED says whether this is a zero-extension.
4164 Let's avoid computing it if it does not affect WIN
4165 and if UNS will not be needed again. */
4166 if ((uns || TREE_CODE (op) == NOP_EXPR)
4167 && TREE_UNSIGNED (TREE_TYPE (op)))
4168 {
4169 uns = 1;
4170 win = op;
4171 }
4172 }
4173 }
4174
4175 if (TREE_CODE (op) == COMPONENT_REF
4176 /* Since type_for_size always gives an integer type. */
4177 && TREE_CODE (type) != REAL_TYPE
4178 /* Don't crash if field not laid out yet. */
4179 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4180 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4181 {
4182 unsigned int innerprec
4183 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4184 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4185 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4186 type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4187
4188 /* We can get this structure field in the narrowest type it fits in.
4189 If FOR_TYPE is 0, do this only for a field that matches the
4190 narrower type exactly and is aligned for it
4191 The resulting extension to its nominal type (a fullword type)
4192 must fit the same conditions as for other extensions. */
4193
4194 if (type != 0
4195 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
4196 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4197 && (! uns || final_prec <= innerprec || unsignedp))
4198 {
4199 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4200 TREE_OPERAND (op, 1));
4201 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4202 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4203 }
4204 }
4205
4206 return win;
4207 }
4208 \f
4209 /* Return OP or a simpler expression for a narrower value
4210 which can be sign-extended or zero-extended to give back OP.
4211 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4212 or 0 if the value should be sign-extended. */
4213
4214 tree
4215 get_narrower (tree op, int *unsignedp_ptr)
4216 {
4217 int uns = 0;
4218 int first = 1;
4219 tree win = op;
4220
4221 while (TREE_CODE (op) == NOP_EXPR)
4222 {
4223 int bitschange
4224 = (TYPE_PRECISION (TREE_TYPE (op))
4225 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4226
4227 /* Truncations are many-one so cannot be removed. */
4228 if (bitschange < 0)
4229 break;
4230
4231 /* See what's inside this conversion. If we decide to strip it,
4232 we will set WIN. */
4233
4234 if (bitschange > 0)
4235 {
4236 op = TREE_OPERAND (op, 0);
4237 /* An extension: the outermost one can be stripped,
4238 but remember whether it is zero or sign extension. */
4239 if (first)
4240 uns = TREE_UNSIGNED (TREE_TYPE (op));
4241 /* Otherwise, if a sign extension has been stripped,
4242 only sign extensions can now be stripped;
4243 if a zero extension has been stripped, only zero-extensions. */
4244 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4245 break;
4246 first = 0;
4247 }
4248 else /* bitschange == 0 */
4249 {
4250 /* A change in nominal type can always be stripped, but we must
4251 preserve the unsignedness. */
4252 if (first)
4253 uns = TREE_UNSIGNED (TREE_TYPE (op));
4254 first = 0;
4255 op = TREE_OPERAND (op, 0);
4256 }
4257
4258 win = op;
4259 }
4260
4261 if (TREE_CODE (op) == COMPONENT_REF
4262 /* Since type_for_size always gives an integer type. */
4263 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4264 /* Ensure field is laid out already. */
4265 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4266 {
4267 unsigned HOST_WIDE_INT innerprec
4268 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4269 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4270 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4271 tree type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4272
4273 /* We can get this structure field in a narrower type that fits it,
4274 but the resulting extension to its nominal type (a fullword type)
4275 must satisfy the same conditions as for other extensions.
4276
4277 Do this only for fields that are aligned (not bit-fields),
4278 because when bit-field insns will be used there is no
4279 advantage in doing this. */
4280
4281 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4282 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4283 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4284 && type != 0)
4285 {
4286 if (first)
4287 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4288 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4289 TREE_OPERAND (op, 1));
4290 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4291 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4292 }
4293 }
4294 *unsignedp_ptr = uns;
4295 return win;
4296 }
4297 \f
4298 /* Nonzero if integer constant C has a value that is permissible
4299 for type TYPE (an INTEGER_TYPE). */
4300
4301 int
4302 int_fits_type_p (tree c, tree type)
4303 {
4304 tree type_low_bound = TYPE_MIN_VALUE (type);
4305 tree type_high_bound = TYPE_MAX_VALUE (type);
4306 int ok_for_low_bound, ok_for_high_bound;
4307
4308 /* Perform some generic filtering first, which may allow making a decision
4309 even if the bounds are not constant. First, negative integers never fit
4310 in unsigned types, */
4311 if ((TREE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4312 /* Also, unsigned integers with top bit set never fit signed types. */
4313 || (! TREE_UNSIGNED (type)
4314 && TREE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4315 return 0;
4316
4317 /* If at least one bound of the type is a constant integer, we can check
4318 ourselves and maybe make a decision. If no such decision is possible, but
4319 this type is a subtype, try checking against that. Otherwise, use
4320 force_fit_type, which checks against the precision.
4321
4322 Compute the status for each possibly constant bound, and return if we see
4323 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4324 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4325 for "constant known to fit". */
4326
4327 ok_for_low_bound = -1;
4328 ok_for_high_bound = -1;
4329
4330 /* Check if C >= type_low_bound. */
4331 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4332 {
4333 ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4334 if (! ok_for_low_bound)
4335 return 0;
4336 }
4337
4338 /* Check if c <= type_high_bound. */
4339 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4340 {
4341 ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4342 if (! ok_for_high_bound)
4343 return 0;
4344 }
4345
4346 /* If the constant fits both bounds, the result is known. */
4347 if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4348 return 1;
4349
4350 /* If we haven't been able to decide at this point, there nothing more we
4351 can check ourselves here. Look at the base type if we have one. */
4352 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4353 return int_fits_type_p (c, TREE_TYPE (type));
4354
4355 /* Or to force_fit_type, if nothing else. */
4356 else
4357 {
4358 c = copy_node (c);
4359 TREE_TYPE (c) = type;
4360 return !force_fit_type (c, 0);
4361 }
4362 }
4363
4364 /* Returns true if T is, contains, or refers to a type with variable
4365 size. This concept is more general than that of C99 'variably
4366 modified types': in C99, a struct type is never variably modified
4367 because a VLA may not appear as a structure member. However, in
4368 GNU C code like:
4369
4370 struct S { int i[f()]; };
4371
4372 is valid, and other languages may define similar constructs. */
4373
4374 bool
4375 variably_modified_type_p (tree type)
4376 {
4377 tree t;
4378
4379 if (type == error_mark_node)
4380 return false;
4381
4382 /* If TYPE itself has variable size, it is variably modified.
4383
4384 We do not yet have a representation of the C99 '[*]' syntax.
4385 When a representation is chosen, this function should be modified
4386 to test for that case as well. */
4387 t = TYPE_SIZE (type);
4388 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4389 return true;
4390
4391 switch (TREE_CODE (type))
4392 {
4393 case POINTER_TYPE:
4394 case REFERENCE_TYPE:
4395 case ARRAY_TYPE:
4396 /* If TYPE is a pointer or reference, it is variably modified if
4397 the type pointed to is variably modified. Similarly for arrays;
4398 note that VLAs are handled by the TYPE_SIZE check above. */
4399 return variably_modified_type_p (TREE_TYPE (type));
4400
4401 case FUNCTION_TYPE:
4402 case METHOD_TYPE:
4403 /* If TYPE is a function type, it is variably modified if any of the
4404 parameters or the return type are variably modified. */
4405 {
4406 tree parm;
4407
4408 if (variably_modified_type_p (TREE_TYPE (type)))
4409 return true;
4410 for (parm = TYPE_ARG_TYPES (type);
4411 parm && parm != void_list_node;
4412 parm = TREE_CHAIN (parm))
4413 if (variably_modified_type_p (TREE_VALUE (parm)))
4414 return true;
4415 }
4416 break;
4417
4418 case INTEGER_TYPE:
4419 /* Scalar types are variably modified if their end points
4420 aren't constant. */
4421 t = TYPE_MIN_VALUE (type);
4422 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4423 return true;
4424 t = TYPE_MAX_VALUE (type);
4425 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4426 return true;
4427 return false;
4428
4429 default:
4430 break;
4431 }
4432
4433 /* The current language may have other cases to check, but in general,
4434 all other types are not variably modified. */
4435 return (*lang_hooks.tree_inlining.var_mod_type_p) (type);
4436 }
4437
4438 /* Given a DECL or TYPE, return the scope in which it was declared, or
4439 NULL_TREE if there is no containing scope. */
4440
4441 tree
4442 get_containing_scope (tree t)
4443 {
4444 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4445 }
4446
4447 /* Return the innermost context enclosing DECL that is
4448 a FUNCTION_DECL, or zero if none. */
4449
4450 tree
4451 decl_function_context (tree decl)
4452 {
4453 tree context;
4454
4455 if (TREE_CODE (decl) == ERROR_MARK)
4456 return 0;
4457
4458 if (TREE_CODE (decl) == SAVE_EXPR)
4459 context = SAVE_EXPR_CONTEXT (decl);
4460
4461 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4462 where we look up the function at runtime. Such functions always take
4463 a first argument of type 'pointer to real context'.
4464
4465 C++ should really be fixed to use DECL_CONTEXT for the real context,
4466 and use something else for the "virtual context". */
4467 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4468 context
4469 = TYPE_MAIN_VARIANT
4470 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4471 else
4472 context = DECL_CONTEXT (decl);
4473
4474 while (context && TREE_CODE (context) != FUNCTION_DECL)
4475 {
4476 if (TREE_CODE (context) == BLOCK)
4477 context = BLOCK_SUPERCONTEXT (context);
4478 else
4479 context = get_containing_scope (context);
4480 }
4481
4482 return context;
4483 }
4484
4485 /* Return the innermost context enclosing DECL that is
4486 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4487 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4488
4489 tree
4490 decl_type_context (tree decl)
4491 {
4492 tree context = DECL_CONTEXT (decl);
4493
4494 while (context)
4495 switch (TREE_CODE (context))
4496 {
4497 case NAMESPACE_DECL:
4498 case TRANSLATION_UNIT_DECL:
4499 return NULL_TREE;
4500
4501 case RECORD_TYPE:
4502 case UNION_TYPE:
4503 case QUAL_UNION_TYPE:
4504 return context;
4505
4506 case TYPE_DECL:
4507 case FUNCTION_DECL:
4508 context = DECL_CONTEXT (context);
4509 break;
4510
4511 case BLOCK:
4512 context = BLOCK_SUPERCONTEXT (context);
4513 break;
4514
4515 default:
4516 abort ();
4517 }
4518
4519 return NULL_TREE;
4520 }
4521
4522 /* CALL is a CALL_EXPR. Return the declaration for the function
4523 called, or NULL_TREE if the called function cannot be
4524 determined. */
4525
4526 tree
4527 get_callee_fndecl (tree call)
4528 {
4529 tree addr;
4530
4531 /* It's invalid to call this function with anything but a
4532 CALL_EXPR. */
4533 if (TREE_CODE (call) != CALL_EXPR)
4534 abort ();
4535
4536 /* The first operand to the CALL is the address of the function
4537 called. */
4538 addr = TREE_OPERAND (call, 0);
4539
4540 STRIP_NOPS (addr);
4541
4542 /* If this is a readonly function pointer, extract its initial value. */
4543 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4544 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4545 && DECL_INITIAL (addr))
4546 addr = DECL_INITIAL (addr);
4547
4548 /* If the address is just `&f' for some function `f', then we know
4549 that `f' is being called. */
4550 if (TREE_CODE (addr) == ADDR_EXPR
4551 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4552 return TREE_OPERAND (addr, 0);
4553
4554 /* We couldn't figure out what was being called. Maybe the front
4555 end has some idea. */
4556 return (*lang_hooks.lang_get_callee_fndecl) (call);
4557 }
4558
4559 /* Print debugging information about tree nodes generated during the compile,
4560 and any language-specific information. */
4561
4562 void
4563 dump_tree_statistics (void)
4564 {
4565 #ifdef GATHER_STATISTICS
4566 int i;
4567 int total_nodes, total_bytes;
4568 #endif
4569
4570 fprintf (stderr, "\n??? tree nodes created\n\n");
4571 #ifdef GATHER_STATISTICS
4572 fprintf (stderr, "Kind Nodes Bytes\n");
4573 fprintf (stderr, "---------------------------------------\n");
4574 total_nodes = total_bytes = 0;
4575 for (i = 0; i < (int) all_kinds; i++)
4576 {
4577 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4578 tree_node_counts[i], tree_node_sizes[i]);
4579 total_nodes += tree_node_counts[i];
4580 total_bytes += tree_node_sizes[i];
4581 }
4582 fprintf (stderr, "---------------------------------------\n");
4583 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4584 fprintf (stderr, "---------------------------------------\n");
4585 #else
4586 fprintf (stderr, "(No per-node statistics)\n");
4587 #endif
4588 print_type_hash_statistics ();
4589 (*lang_hooks.print_statistics) ();
4590 }
4591 \f
4592 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4593
4594 /* Generate a crc32 of a string. */
4595
4596 unsigned
4597 crc32_string (unsigned chksum, const char *string)
4598 {
4599 do
4600 {
4601 unsigned value = *string << 24;
4602 unsigned ix;
4603
4604 for (ix = 8; ix--; value <<= 1)
4605 {
4606 unsigned feedback;
4607
4608 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4609 chksum <<= 1;
4610 chksum ^= feedback;
4611 }
4612 }
4613 while (*string++);
4614 return chksum;
4615 }
4616
4617 /* P is a string that will be used in a symbol. Mask out any characters
4618 that are not valid in that context. */
4619
4620 void
4621 clean_symbol_name (char *p)
4622 {
4623 for (; *p; p++)
4624 if (! (ISALNUM (*p)
4625 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4626 || *p == '$'
4627 #endif
4628 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4629 || *p == '.'
4630 #endif
4631 ))
4632 *p = '_';
4633 }
4634
4635 /* Generate a name for a function unique to this translation unit.
4636 TYPE is some string to identify the purpose of this function to the
4637 linker or collect2. */
4638
4639 tree
4640 get_file_function_name_long (const char *type)
4641 {
4642 char *buf;
4643 const char *p;
4644 char *q;
4645
4646 if (first_global_object_name)
4647 p = first_global_object_name;
4648 else
4649 {
4650 /* We don't have anything that we know to be unique to this translation
4651 unit, so use what we do have and throw in some randomness. */
4652 unsigned len;
4653 const char *name = weak_global_object_name;
4654 const char *file = main_input_filename;
4655
4656 if (! name)
4657 name = "";
4658 if (! file)
4659 file = input_filename;
4660
4661 len = strlen (file);
4662 q = alloca (9 * 2 + len + 1);
4663 memcpy (q, file, len + 1);
4664 clean_symbol_name (q);
4665
4666 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
4667 crc32_string (0, flag_random_seed));
4668
4669 p = q;
4670 }
4671
4672 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
4673
4674 /* Set up the name of the file-level functions we may need.
4675 Use a global object (which is already required to be unique over
4676 the program) rather than the file name (which imposes extra
4677 constraints). */
4678 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4679
4680 return get_identifier (buf);
4681 }
4682
4683 /* If KIND=='I', return a suitable global initializer (constructor) name.
4684 If KIND=='D', return a suitable global clean-up (destructor) name. */
4685
4686 tree
4687 get_file_function_name (int kind)
4688 {
4689 char p[2];
4690
4691 p[0] = kind;
4692 p[1] = 0;
4693
4694 return get_file_function_name_long (p);
4695 }
4696 \f
4697 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4698 The result is placed in BUFFER (which has length BIT_SIZE),
4699 with one bit in each char ('\000' or '\001').
4700
4701 If the constructor is constant, NULL_TREE is returned.
4702 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4703
4704 tree
4705 get_set_constructor_bits (tree init, char *buffer, int bit_size)
4706 {
4707 int i;
4708 tree vals;
4709 HOST_WIDE_INT domain_min
4710 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4711 tree non_const_bits = NULL_TREE;
4712
4713 for (i = 0; i < bit_size; i++)
4714 buffer[i] = 0;
4715
4716 for (vals = TREE_OPERAND (init, 1);
4717 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4718 {
4719 if (!host_integerp (TREE_VALUE (vals), 0)
4720 || (TREE_PURPOSE (vals) != NULL_TREE
4721 && !host_integerp (TREE_PURPOSE (vals), 0)))
4722 non_const_bits
4723 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4724 else if (TREE_PURPOSE (vals) != NULL_TREE)
4725 {
4726 /* Set a range of bits to ones. */
4727 HOST_WIDE_INT lo_index
4728 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4729 HOST_WIDE_INT hi_index
4730 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4731
4732 if (lo_index < 0 || lo_index >= bit_size
4733 || hi_index < 0 || hi_index >= bit_size)
4734 abort ();
4735 for (; lo_index <= hi_index; lo_index++)
4736 buffer[lo_index] = 1;
4737 }
4738 else
4739 {
4740 /* Set a single bit to one. */
4741 HOST_WIDE_INT index
4742 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4743 if (index < 0 || index >= bit_size)
4744 {
4745 error ("invalid initializer for bit string");
4746 return NULL_TREE;
4747 }
4748 buffer[index] = 1;
4749 }
4750 }
4751 return non_const_bits;
4752 }
4753
4754 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4755 The result is placed in BUFFER (which is an array of bytes).
4756 If the constructor is constant, NULL_TREE is returned.
4757 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4758
4759 tree
4760 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
4761 {
4762 int i;
4763 int set_word_size = BITS_PER_UNIT;
4764 int bit_size = wd_size * set_word_size;
4765 int bit_pos = 0;
4766 unsigned char *bytep = buffer;
4767 char *bit_buffer = alloca (bit_size);
4768 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4769
4770 for (i = 0; i < wd_size; i++)
4771 buffer[i] = 0;
4772
4773 for (i = 0; i < bit_size; i++)
4774 {
4775 if (bit_buffer[i])
4776 {
4777 if (BYTES_BIG_ENDIAN)
4778 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4779 else
4780 *bytep |= 1 << bit_pos;
4781 }
4782 bit_pos++;
4783 if (bit_pos >= set_word_size)
4784 bit_pos = 0, bytep++;
4785 }
4786 return non_const_bits;
4787 }
4788 \f
4789 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4790 /* Complain that the tree code of NODE does not match the expected CODE.
4791 FILE, LINE, and FUNCTION are of the caller. */
4792
4793 void
4794 tree_check_failed (const tree node, enum tree_code code, const char *file,
4795 int line, const char *function)
4796 {
4797 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4798 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4799 function, trim_filename (file), line);
4800 }
4801
4802 /* Similar to above, except that we check for a class of tree
4803 code, given in CL. */
4804
4805 void
4806 tree_class_check_failed (const tree node, int cl, const char *file,
4807 int line, const char *function)
4808 {
4809 internal_error
4810 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4811 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4812 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4813 }
4814
4815 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
4816 (dynamically sized) vector. */
4817
4818 void
4819 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
4820 const char *function)
4821 {
4822 internal_error
4823 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
4824 idx + 1, len, function, trim_filename (file), line);
4825 }
4826
4827 /* Similar to above, except that the check is for the bounds of the operand
4828 vector of an expression node. */
4829
4830 void
4831 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
4832 int line, const char *function)
4833 {
4834 internal_error
4835 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
4836 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
4837 function, trim_filename (file), line);
4838 }
4839 #endif /* ENABLE_TREE_CHECKING */
4840 \f
4841 /* For a new vector type node T, build the information necessary for
4842 debugging output. */
4843
4844 static void
4845 finish_vector_type (tree t)
4846 {
4847 layout_type (t);
4848
4849 {
4850 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4851 tree array = build_array_type (TREE_TYPE (t),
4852 build_index_type (index));
4853 tree rt = make_node (RECORD_TYPE);
4854
4855 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4856 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4857 layout_type (rt);
4858 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4859 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4860 the representation type, and we want to find that die when looking up
4861 the vector type. This is most easily achieved by making the TYPE_UID
4862 numbers equal. */
4863 TYPE_UID (rt) = TYPE_UID (t);
4864 }
4865 }
4866
4867 /* Create nodes for all integer types (and error_mark_node) using the sizes
4868 of C datatypes. The caller should call set_sizetype soon after calling
4869 this function to select one of the types as sizetype. */
4870
4871 void
4872 build_common_tree_nodes (int signed_char)
4873 {
4874 error_mark_node = make_node (ERROR_MARK);
4875 TREE_TYPE (error_mark_node) = error_mark_node;
4876
4877 initialize_sizetypes ();
4878
4879 /* Define both `signed char' and `unsigned char'. */
4880 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4881 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4882
4883 /* Define `char', which is like either `signed char' or `unsigned char'
4884 but not the same as either. */
4885 char_type_node
4886 = (signed_char
4887 ? make_signed_type (CHAR_TYPE_SIZE)
4888 : make_unsigned_type (CHAR_TYPE_SIZE));
4889
4890 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4891 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4892 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4893 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4894 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4895 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4896 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4897 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4898
4899 /* Define a boolean type. This type only represents boolean values but
4900 may be larger than char depending on the value of BOOL_TYPE_SIZE.
4901 Front ends which want to override this size (i.e. Java) can redefine
4902 boolean_type_node before calling build_common_tree_nodes_2. */
4903 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
4904 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
4905 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
4906 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
4907 TYPE_PRECISION (boolean_type_node) = 1;
4908
4909 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4910 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4911 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4912 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4913 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4914
4915 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4916 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4917 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4918 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4919 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4920
4921 access_public_node = get_identifier ("public");
4922 access_protected_node = get_identifier ("protected");
4923 access_private_node = get_identifier ("private");
4924 }
4925
4926 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4927 It will create several other common tree nodes. */
4928
4929 void
4930 build_common_tree_nodes_2 (int short_double)
4931 {
4932 /* Define these next since types below may used them. */
4933 integer_zero_node = build_int_2 (0, 0);
4934 integer_one_node = build_int_2 (1, 0);
4935 integer_minus_one_node = build_int_2 (-1, -1);
4936
4937 size_zero_node = size_int (0);
4938 size_one_node = size_int (1);
4939 bitsize_zero_node = bitsize_int (0);
4940 bitsize_one_node = bitsize_int (1);
4941 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4942
4943 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
4944 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
4945
4946 void_type_node = make_node (VOID_TYPE);
4947 layout_type (void_type_node);
4948
4949 /* We are not going to have real types in C with less than byte alignment,
4950 so we might as well not have any types that claim to have it. */
4951 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4952 TYPE_USER_ALIGN (void_type_node) = 0;
4953
4954 null_pointer_node = build_int_2 (0, 0);
4955 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4956 layout_type (TREE_TYPE (null_pointer_node));
4957
4958 ptr_type_node = build_pointer_type (void_type_node);
4959 const_ptr_type_node
4960 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4961
4962 float_type_node = make_node (REAL_TYPE);
4963 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4964 layout_type (float_type_node);
4965
4966 double_type_node = make_node (REAL_TYPE);
4967 if (short_double)
4968 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4969 else
4970 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4971 layout_type (double_type_node);
4972
4973 long_double_type_node = make_node (REAL_TYPE);
4974 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4975 layout_type (long_double_type_node);
4976
4977 float_ptr_type_node = build_pointer_type (float_type_node);
4978 double_ptr_type_node = build_pointer_type (double_type_node);
4979 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
4980 integer_ptr_type_node = build_pointer_type (integer_type_node);
4981
4982 complex_integer_type_node = make_node (COMPLEX_TYPE);
4983 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4984 layout_type (complex_integer_type_node);
4985
4986 complex_float_type_node = make_node (COMPLEX_TYPE);
4987 TREE_TYPE (complex_float_type_node) = float_type_node;
4988 layout_type (complex_float_type_node);
4989
4990 complex_double_type_node = make_node (COMPLEX_TYPE);
4991 TREE_TYPE (complex_double_type_node) = double_type_node;
4992 layout_type (complex_double_type_node);
4993
4994 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4995 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4996 layout_type (complex_long_double_type_node);
4997
4998 {
4999 tree t = (*targetm.build_builtin_va_list) ();
5000
5001 /* Many back-ends define record types without setting TYPE_NAME.
5002 If we copied the record type here, we'd keep the original
5003 record type without a name. This breaks name mangling. So,
5004 don't copy record types and let c_common_nodes_and_builtins()
5005 declare the type to be __builtin_va_list. */
5006 if (TREE_CODE (t) != RECORD_TYPE)
5007 t = build_type_copy (t);
5008
5009 va_list_type_node = t;
5010 }
5011
5012 unsigned_V4SI_type_node
5013 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
5014 unsigned_V2HI_type_node
5015 = make_vector (V2HImode, unsigned_intHI_type_node, 1);
5016 unsigned_V2SI_type_node
5017 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
5018 unsigned_V2DI_type_node
5019 = make_vector (V2DImode, unsigned_intDI_type_node, 1);
5020 unsigned_V4HI_type_node
5021 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
5022 unsigned_V8QI_type_node
5023 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
5024 unsigned_V8HI_type_node
5025 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
5026 unsigned_V16QI_type_node
5027 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
5028 unsigned_V1DI_type_node
5029 = make_vector (V1DImode, unsigned_intDI_type_node, 1);
5030
5031 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
5032 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
5033 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
5034 V2HI_type_node = make_vector (V2HImode, intHI_type_node, 0);
5035 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
5036 V2DI_type_node = make_vector (V2DImode, intDI_type_node, 0);
5037 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
5038 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
5039 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
5040 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
5041 V2DF_type_node = make_vector (V2DFmode, double_type_node, 0);
5042 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
5043 V1DI_type_node = make_vector (V1DImode, intDI_type_node, 0);
5044 V4DF_type_node = make_vector (V4DFmode, double_type_node, 0);
5045 }
5046
5047 /* Returns a vector tree node given a vector mode, the inner type, and
5048 the signness. */
5049
5050 static tree
5051 make_vector (enum machine_mode mode, tree innertype, int unsignedp)
5052 {
5053 tree t;
5054
5055 t = make_node (VECTOR_TYPE);
5056 TREE_TYPE (t) = innertype;
5057 TYPE_MODE (t) = mode;
5058 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
5059 finish_vector_type (t);
5060
5061 return t;
5062 }
5063
5064 /* Given an initializer INIT, return TRUE if INIT is zero or some
5065 aggregate of zeros. Otherwise return FALSE. */
5066
5067 bool
5068 initializer_zerop (tree init)
5069 {
5070 STRIP_NOPS (init);
5071
5072 switch (TREE_CODE (init))
5073 {
5074 case INTEGER_CST:
5075 return integer_zerop (init);
5076 case REAL_CST:
5077 return real_zerop (init)
5078 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5079 case COMPLEX_CST:
5080 return integer_zerop (init)
5081 || (real_zerop (init)
5082 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5083 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5084 case CONSTRUCTOR:
5085 {
5086 /* Set is empty if it has no elements. */
5087 if ((TREE_CODE (TREE_TYPE (init)) == SET_TYPE)
5088 && CONSTRUCTOR_ELTS (init))
5089 return false;
5090
5091 if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
5092 {
5093 tree aggr_init = CONSTRUCTOR_ELTS (init);
5094
5095 while (aggr_init)
5096 {
5097 if (! initializer_zerop (TREE_VALUE (aggr_init)))
5098 return false;
5099 aggr_init = TREE_CHAIN (aggr_init);
5100 }
5101 return true;
5102 }
5103 return false;
5104 }
5105 default:
5106 return false;
5107 }
5108 }
5109
5110 #include "gt-tree.h"