OSDN Git Service

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