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