OSDN Git Service

* reload.c (find_reloads_address): Make return value tri-state.
[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_type_copy (type);
3130       set_type_quals (t, type_quals);
3131     }
3132
3133   return t;
3134 }
3135
3136 /* Create a new variant of TYPE, equivalent but distinct.
3137    This is so the caller can modify it.  */
3138
3139 tree
3140 build_type_copy (tree type)
3141 {
3142   tree t, m = TYPE_MAIN_VARIANT (type);
3143
3144   t = copy_node (type);
3145   if (TYPE_CACHED_VALUES_P(t))
3146     {
3147       /* Do not copy the values cache.  */
3148       if (TREE_CODE (t) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t))
3149         abort ();
3150       TYPE_CACHED_VALUES_P (t) = 0;
3151       TYPE_CACHED_VALUES (t) = NULL_TREE;
3152     }
3153
3154   TYPE_POINTER_TO (t) = 0;
3155   TYPE_REFERENCE_TO (t) = 0;
3156
3157   /* Add this type to the chain of variants of TYPE.  */
3158   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3159   TYPE_NEXT_VARIANT (m) = t;
3160
3161   return t;
3162 }
3163 \f
3164 /* Hashing of types so that we don't make duplicates.
3165    The entry point is `type_hash_canon'.  */
3166
3167 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3168    with types in the TREE_VALUE slots), by adding the hash codes
3169    of the individual types.  */
3170
3171 unsigned int
3172 type_hash_list (tree list, hashval_t hashcode)
3173 {
3174   tree tail;
3175
3176   for (tail = list; tail; tail = TREE_CHAIN (tail))
3177     if (TREE_VALUE (tail) != error_mark_node)
3178       hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
3179                                         hashcode);
3180
3181   return hashcode;
3182 }
3183
3184 /* These are the Hashtable callback functions.  */
3185
3186 /* Returns true iff the types are equivalent.  */
3187
3188 static int
3189 type_hash_eq (const void *va, const void *vb)
3190 {
3191   const struct type_hash *a = va, *b = vb;
3192
3193   /* First test the things that are the same for all types.  */
3194   if (a->hash != b->hash
3195       || TREE_CODE (a->type) != TREE_CODE (b->type)
3196       || TREE_TYPE (a->type) != TREE_TYPE (b->type)
3197       || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3198                                  TYPE_ATTRIBUTES (b->type))
3199       || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
3200       || TYPE_MODE (a->type) != TYPE_MODE (b->type))
3201     return 0;
3202
3203   switch (TREE_CODE (a->type))
3204     {
3205     case VOID_TYPE:
3206     case COMPLEX_TYPE:
3207     case VECTOR_TYPE:
3208     case POINTER_TYPE:
3209     case REFERENCE_TYPE:
3210       return 1;
3211
3212     case ENUMERAL_TYPE:
3213       if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
3214           && !(TYPE_VALUES (a->type)
3215                && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
3216                && TYPE_VALUES (b->type)
3217                && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
3218                && type_list_equal (TYPE_VALUES (a->type),
3219                                    TYPE_VALUES (b->type))))
3220         return 0;
3221
3222       /* ... fall through ... */
3223
3224     case INTEGER_TYPE:
3225     case REAL_TYPE:
3226     case BOOLEAN_TYPE:
3227     case CHAR_TYPE:
3228       return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3229                || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3230                                       TYPE_MAX_VALUE (b->type)))
3231               && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3232                   || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3233                                          TYPE_MIN_VALUE (b->type))));
3234
3235     case OFFSET_TYPE:
3236       return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
3237
3238     case METHOD_TYPE:
3239       return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
3240               && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3241                   || (TYPE_ARG_TYPES (a->type)
3242                       && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3243                       && TYPE_ARG_TYPES (b->type)
3244                       && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3245                       && type_list_equal (TYPE_ARG_TYPES (a->type),
3246                                           TYPE_ARG_TYPES (b->type)))));
3247
3248     case ARRAY_TYPE:
3249     case SET_TYPE:
3250       return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
3251
3252     case RECORD_TYPE:
3253     case UNION_TYPE:
3254     case QUAL_UNION_TYPE:
3255       return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
3256               || (TYPE_FIELDS (a->type)
3257                   && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
3258                   && TYPE_FIELDS (b->type)
3259                   && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
3260                   && type_list_equal (TYPE_FIELDS (a->type),
3261                                       TYPE_FIELDS (b->type))));
3262
3263     case FUNCTION_TYPE:
3264       return (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3265               || (TYPE_ARG_TYPES (a->type)
3266                   && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3267                   && TYPE_ARG_TYPES (b->type)
3268                   && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3269                   && type_list_equal (TYPE_ARG_TYPES (a->type),
3270                                       TYPE_ARG_TYPES (b->type))));
3271
3272     default:
3273       return 0;
3274     }
3275 }
3276
3277 /* Return the cached hash value.  */
3278
3279 static hashval_t
3280 type_hash_hash (const void *item)
3281 {
3282   return ((const struct type_hash *) item)->hash;
3283 }
3284
3285 /* Look in the type hash table for a type isomorphic to TYPE.
3286    If one is found, return it.  Otherwise return 0.  */
3287
3288 tree
3289 type_hash_lookup (hashval_t hashcode, tree type)
3290 {
3291   struct type_hash *h, in;
3292
3293   /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3294      must call that routine before comparing TYPE_ALIGNs.  */
3295   layout_type (type);
3296
3297   in.hash = hashcode;
3298   in.type = type;
3299
3300   h = htab_find_with_hash (type_hash_table, &in, hashcode);
3301   if (h)
3302     return h->type;
3303   return NULL_TREE;
3304 }
3305
3306 /* Add an entry to the type-hash-table
3307    for a type TYPE whose hash code is HASHCODE.  */
3308
3309 void
3310 type_hash_add (hashval_t hashcode, tree type)
3311 {
3312   struct type_hash *h;
3313   void **loc;
3314
3315   h = ggc_alloc (sizeof (struct type_hash));
3316   h->hash = hashcode;
3317   h->type = type;
3318   loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3319   *(struct type_hash **) loc = h;
3320 }
3321
3322 /* Given TYPE, and HASHCODE its hash code, return the canonical
3323    object for an identical type if one already exists.
3324    Otherwise, return TYPE, and record it as the canonical object.
3325
3326    To use this function, first create a type of the sort you want.
3327    Then compute its hash code from the fields of the type that
3328    make it different from other similar types.
3329    Then call this function and use the value.  */
3330
3331 tree
3332 type_hash_canon (unsigned int hashcode, tree type)
3333 {
3334   tree t1;
3335
3336   /* The hash table only contains main variants, so ensure that's what we're
3337      being passed.  */
3338   if (TYPE_MAIN_VARIANT (type) != type)
3339     abort ();
3340
3341   if (!lang_hooks.types.hash_types)
3342     return type;
3343
3344   /* See if the type is in the hash table already.  If so, return it.
3345      Otherwise, add the type.  */
3346   t1 = type_hash_lookup (hashcode, type);
3347   if (t1 != 0)
3348     {
3349 #ifdef GATHER_STATISTICS
3350       tree_node_counts[(int) t_kind]--;
3351       tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3352 #endif
3353       return t1;
3354     }
3355   else
3356     {
3357       type_hash_add (hashcode, type);
3358       return type;
3359     }
3360 }
3361
3362 /* See if the data pointed to by the type hash table is marked.  We consider
3363    it marked if the type is marked or if a debug type number or symbol
3364    table entry has been made for the type.  This reduces the amount of
3365    debugging output and eliminates that dependency of the debug output on
3366    the number of garbage collections.  */
3367
3368 static int
3369 type_hash_marked_p (const void *p)
3370 {
3371   tree type = ((struct type_hash *) p)->type;
3372
3373   return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3374 }
3375
3376 static void
3377 print_type_hash_statistics (void)
3378 {
3379   fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3380            (long) htab_size (type_hash_table),
3381            (long) htab_elements (type_hash_table),
3382            htab_collisions (type_hash_table));
3383 }
3384
3385 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3386    with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3387    by adding the hash codes of the individual attributes.  */
3388
3389 unsigned int
3390 attribute_hash_list (tree list, hashval_t hashcode)
3391 {
3392   tree tail;
3393
3394   for (tail = list; tail; tail = TREE_CHAIN (tail))
3395     /* ??? Do we want to add in TREE_VALUE too? */
3396     hashcode = iterative_hash_object
3397       (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
3398   return hashcode;
3399 }
3400
3401 /* Given two lists of attributes, return true if list l2 is
3402    equivalent to l1.  */
3403
3404 int
3405 attribute_list_equal (tree l1, tree l2)
3406 {
3407   return attribute_list_contained (l1, l2)
3408          && attribute_list_contained (l2, l1);
3409 }
3410
3411 /* Given two lists of attributes, return true if list L2 is
3412    completely contained within L1.  */
3413 /* ??? This would be faster if attribute names were stored in a canonicalized
3414    form.  Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3415    must be used to show these elements are equivalent (which they are).  */
3416 /* ??? It's not clear that attributes with arguments will always be handled
3417    correctly.  */
3418
3419 int
3420 attribute_list_contained (tree l1, tree l2)
3421 {
3422   tree t1, t2;
3423
3424   /* First check the obvious, maybe the lists are identical.  */
3425   if (l1 == l2)
3426     return 1;
3427
3428   /* Maybe the lists are similar.  */
3429   for (t1 = l1, t2 = l2;
3430        t1 != 0 && t2 != 0
3431         && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3432         && TREE_VALUE (t1) == TREE_VALUE (t2);
3433        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3434
3435   /* Maybe the lists are equal.  */
3436   if (t1 == 0 && t2 == 0)
3437     return 1;
3438
3439   for (; t2 != 0; t2 = TREE_CHAIN (t2))
3440     {
3441       tree attr;
3442       for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3443            attr != NULL_TREE;
3444            attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3445                                     TREE_CHAIN (attr)))
3446         {
3447           if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3448             break;
3449         }
3450
3451       if (attr == 0)
3452         return 0;
3453
3454       if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3455         return 0;
3456     }
3457
3458   return 1;
3459 }
3460
3461 /* Given two lists of types
3462    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3463    return 1 if the lists contain the same types in the same order.
3464    Also, the TREE_PURPOSEs must match.  */
3465
3466 int
3467 type_list_equal (tree l1, tree l2)
3468 {
3469   tree t1, t2;
3470
3471   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3472     if (TREE_VALUE (t1) != TREE_VALUE (t2)
3473         || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3474             && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3475                   && (TREE_TYPE (TREE_PURPOSE (t1))
3476                       == TREE_TYPE (TREE_PURPOSE (t2))))))
3477       return 0;
3478
3479   return t1 == t2;
3480 }
3481
3482 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3483    given by TYPE.  If the argument list accepts variable arguments,
3484    then this function counts only the ordinary arguments.  */
3485
3486 int
3487 type_num_arguments (tree type)
3488 {
3489   int i = 0;
3490   tree t;
3491
3492   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3493     /* If the function does not take a variable number of arguments,
3494        the last element in the list will have type `void'.  */
3495     if (VOID_TYPE_P (TREE_VALUE (t)))
3496       break;
3497     else
3498       ++i;
3499
3500   return i;
3501 }
3502
3503 /* Nonzero if integer constants T1 and T2
3504    represent the same constant value.  */
3505
3506 int
3507 tree_int_cst_equal (tree t1, tree t2)
3508 {
3509   if (t1 == t2)
3510     return 1;
3511
3512   if (t1 == 0 || t2 == 0)
3513     return 0;
3514
3515   if (TREE_CODE (t1) == INTEGER_CST
3516       && TREE_CODE (t2) == INTEGER_CST
3517       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3518       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3519     return 1;
3520
3521   return 0;
3522 }
3523
3524 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3525    The precise way of comparison depends on their data type.  */
3526
3527 int
3528 tree_int_cst_lt (tree t1, tree t2)
3529 {
3530   if (t1 == t2)
3531     return 0;
3532
3533   if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
3534     {
3535       int t1_sgn = tree_int_cst_sgn (t1);
3536       int t2_sgn = tree_int_cst_sgn (t2);
3537
3538       if (t1_sgn < t2_sgn)
3539         return 1;
3540       else if (t1_sgn > t2_sgn)
3541         return 0;
3542       /* Otherwise, both are non-negative, so we compare them as
3543          unsigned just in case one of them would overflow a signed
3544          type.  */
3545     }
3546   else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
3547     return INT_CST_LT (t1, t2);
3548
3549   return INT_CST_LT_UNSIGNED (t1, t2);
3550 }
3551
3552 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2.  */
3553
3554 int
3555 tree_int_cst_compare (tree t1, tree t2)
3556 {
3557   if (tree_int_cst_lt (t1, t2))
3558     return -1;
3559   else if (tree_int_cst_lt (t2, t1))
3560     return 1;
3561   else
3562     return 0;
3563 }
3564
3565 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3566    the host.  If POS is zero, the value can be represented in a single
3567    HOST_WIDE_INT.  If POS is nonzero, the value must be positive and can
3568    be represented in a single unsigned HOST_WIDE_INT.  */
3569
3570 int
3571 host_integerp (tree t, int pos)
3572 {
3573   return (TREE_CODE (t) == INTEGER_CST
3574           && ! TREE_OVERFLOW (t)
3575           && ((TREE_INT_CST_HIGH (t) == 0
3576                && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3577               || (! pos && TREE_INT_CST_HIGH (t) == -1
3578                   && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3579                   && !TYPE_UNSIGNED (TREE_TYPE (t)))
3580               || (pos && TREE_INT_CST_HIGH (t) == 0)));
3581 }
3582
3583 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3584    INTEGER_CST and there is no overflow.  POS is nonzero if the result must
3585    be positive.  Abort if we cannot satisfy the above conditions.  */
3586
3587 HOST_WIDE_INT
3588 tree_low_cst (tree t, int pos)
3589 {
3590   if (host_integerp (t, pos))
3591     return TREE_INT_CST_LOW (t);
3592   else
3593     abort ();
3594 }
3595
3596 /* Return the most significant bit of the integer constant T.  */
3597
3598 int
3599 tree_int_cst_msb (tree t)
3600 {
3601   int prec;
3602   HOST_WIDE_INT h;
3603   unsigned HOST_WIDE_INT l;
3604
3605   /* Note that using TYPE_PRECISION here is wrong.  We care about the
3606      actual bits, not the (arbitrary) range of the type.  */
3607   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3608   rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3609                  2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3610   return (l & 1) == 1;
3611 }
3612
3613 /* Return an indication of the sign of the integer constant T.
3614    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3615    Note that -1 will never be returned it T's type is unsigned.  */
3616
3617 int
3618 tree_int_cst_sgn (tree t)
3619 {
3620   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3621     return 0;
3622   else if (TYPE_UNSIGNED (TREE_TYPE (t)))
3623     return 1;
3624   else if (TREE_INT_CST_HIGH (t) < 0)
3625     return -1;
3626   else
3627     return 1;
3628 }
3629
3630 /* Compare two constructor-element-type constants.  Return 1 if the lists
3631    are known to be equal; otherwise return 0.  */
3632
3633 int
3634 simple_cst_list_equal (tree l1, tree l2)
3635 {
3636   while (l1 != NULL_TREE && l2 != NULL_TREE)
3637     {
3638       if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3639         return 0;
3640
3641       l1 = TREE_CHAIN (l1);
3642       l2 = TREE_CHAIN (l2);
3643     }
3644
3645   return l1 == l2;
3646 }
3647
3648 /* Return truthvalue of whether T1 is the same tree structure as T2.
3649    Return 1 if they are the same.
3650    Return 0 if they are understandably different.
3651    Return -1 if either contains tree structure not understood by
3652    this function.  */
3653
3654 int
3655 simple_cst_equal (tree t1, tree t2)
3656 {
3657   enum tree_code code1, code2;
3658   int cmp;
3659   int i;
3660
3661   if (t1 == t2)
3662     return 1;
3663   if (t1 == 0 || t2 == 0)
3664     return 0;
3665
3666   code1 = TREE_CODE (t1);
3667   code2 = TREE_CODE (t2);
3668
3669   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3670     {
3671       if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3672           || code2 == NON_LVALUE_EXPR)
3673         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3674       else
3675         return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3676     }
3677
3678   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3679            || code2 == NON_LVALUE_EXPR)
3680     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3681
3682   if (code1 != code2)
3683     return 0;
3684
3685   switch (code1)
3686     {
3687     case INTEGER_CST:
3688       return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3689               && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3690
3691     case REAL_CST:
3692       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3693
3694     case STRING_CST:
3695       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3696               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3697                          TREE_STRING_LENGTH (t1)));
3698
3699     case CONSTRUCTOR:
3700       return simple_cst_list_equal (CONSTRUCTOR_ELTS (t1),
3701                                     CONSTRUCTOR_ELTS (t2));
3702
3703     case SAVE_EXPR:
3704       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3705
3706     case CALL_EXPR:
3707       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3708       if (cmp <= 0)
3709         return cmp;
3710       return
3711         simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3712
3713     case TARGET_EXPR:
3714       /* Special case: if either target is an unallocated VAR_DECL,
3715          it means that it's going to be unified with whatever the
3716          TARGET_EXPR is really supposed to initialize, so treat it
3717          as being equivalent to anything.  */
3718       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3719            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3720            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3721           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3722               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3723               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3724         cmp = 1;
3725       else
3726         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3727
3728       if (cmp <= 0)
3729         return cmp;
3730
3731       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3732
3733     case WITH_CLEANUP_EXPR:
3734       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3735       if (cmp <= 0)
3736         return cmp;
3737
3738       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3739
3740     case COMPONENT_REF:
3741       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3742         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3743
3744       return 0;
3745
3746     case VAR_DECL:
3747     case PARM_DECL:
3748     case CONST_DECL:
3749     case FUNCTION_DECL:
3750       return 0;
3751
3752     default:
3753       break;
3754     }
3755
3756   /* This general rule works for most tree codes.  All exceptions should be
3757      handled above.  If this is a language-specific tree code, we can't
3758      trust what might be in the operand, so say we don't know
3759      the situation.  */
3760   if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3761     return -1;
3762
3763   switch (TREE_CODE_CLASS (code1))
3764     {
3765     case '1':
3766     case '2':
3767     case '<':
3768     case 'e':
3769     case 'r':
3770     case 's':
3771       cmp = 1;
3772       for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3773         {
3774           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3775           if (cmp <= 0)
3776             return cmp;
3777         }
3778
3779       return cmp;
3780
3781     default:
3782       return -1;
3783     }
3784 }
3785
3786 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3787    Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3788    than U, respectively.  */
3789
3790 int
3791 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3792 {
3793   if (tree_int_cst_sgn (t) < 0)
3794     return -1;
3795   else if (TREE_INT_CST_HIGH (t) != 0)
3796     return 1;
3797   else if (TREE_INT_CST_LOW (t) == u)
3798     return 0;
3799   else if (TREE_INT_CST_LOW (t) < u)
3800     return -1;
3801   else
3802     return 1;
3803 }
3804
3805 /* Return true if CODE represents an associative tree code.  Otherwise
3806    return false.  */
3807 bool
3808 associative_tree_code (enum tree_code code)
3809 {
3810   switch (code)
3811     {
3812     case BIT_IOR_EXPR:
3813     case BIT_AND_EXPR:
3814     case BIT_XOR_EXPR:
3815     case PLUS_EXPR:
3816     case MULT_EXPR:
3817     case MIN_EXPR:
3818     case MAX_EXPR:
3819       return true;
3820
3821     default:
3822       break;
3823     }
3824   return false;
3825 }
3826
3827 /* Return true if CODE represents an commutative tree code.  Otherwise
3828    return false.  */
3829 bool
3830 commutative_tree_code (enum tree_code code)
3831 {
3832   switch (code)
3833     {
3834     case PLUS_EXPR:
3835     case MULT_EXPR:
3836     case MIN_EXPR:
3837     case MAX_EXPR:
3838     case BIT_IOR_EXPR:
3839     case BIT_XOR_EXPR:
3840     case BIT_AND_EXPR:
3841     case NE_EXPR:
3842     case EQ_EXPR:
3843     case UNORDERED_EXPR:
3844     case ORDERED_EXPR:
3845     case UNEQ_EXPR:
3846     case LTGT_EXPR:
3847     case TRUTH_AND_EXPR:
3848     case TRUTH_XOR_EXPR:
3849     case TRUTH_OR_EXPR:
3850       return true;
3851
3852     default:
3853       break;
3854     }
3855   return false;
3856 }
3857
3858 /* Generate a hash value for an expression.  This can be used iteratively
3859    by passing a previous result as the "val" argument.
3860
3861    This function is intended to produce the same hash for expressions which
3862    would compare equal using operand_equal_p.  */
3863
3864 hashval_t
3865 iterative_hash_expr (tree t, hashval_t val)
3866 {
3867   int i;
3868   enum tree_code code;
3869   char class;
3870
3871   if (t == NULL_TREE)
3872     return iterative_hash_object (t, val);
3873
3874   code = TREE_CODE (t);
3875   class = TREE_CODE_CLASS (code);
3876
3877   if (class == 'd'
3878       || TREE_CODE (t) == VALUE_HANDLE)
3879     {
3880       /* Decls we can just compare by pointer.  */
3881       val = iterative_hash_object (t, val);
3882     }
3883   else if (class == 'c')
3884     {
3885       /* Alas, constants aren't shared, so we can't rely on pointer
3886          identity.  */
3887       if (code == INTEGER_CST)
3888         {
3889           val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3890           val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3891         }
3892       else if (code == REAL_CST)
3893         {
3894           unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
3895
3896           val = iterative_hash (&val2, sizeof (unsigned int), val);
3897         }
3898       else if (code == STRING_CST)
3899         val = iterative_hash (TREE_STRING_POINTER (t),
3900                               TREE_STRING_LENGTH (t), val);
3901       else if (code == COMPLEX_CST)
3902         {
3903           val = iterative_hash_expr (TREE_REALPART (t), val);
3904           val = iterative_hash_expr (TREE_IMAGPART (t), val);
3905         }
3906       else if (code == VECTOR_CST)
3907         val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3908       else
3909         abort ();
3910     }
3911   else if (IS_EXPR_CODE_CLASS (class))
3912     {
3913       val = iterative_hash_object (code, val);
3914
3915       /* Don't hash the type, that can lead to having nodes which
3916          compare equal according to operand_equal_p, but which
3917          have different hash codes.  */
3918       if (code == NOP_EXPR
3919           || code == CONVERT_EXPR
3920           || code == NON_LVALUE_EXPR)
3921         {
3922           /* Make sure to include signness in the hash computation.  */
3923           val += TYPE_UNSIGNED (TREE_TYPE (t));
3924           val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
3925         }
3926
3927       if (commutative_tree_code (code))
3928         {
3929           /* It's a commutative expression.  We want to hash it the same
3930              however it appears.  We do this by first hashing both operands
3931              and then rehashing based on the order of their independent
3932              hashes.  */
3933           hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3934           hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3935           hashval_t t;
3936
3937           if (one > two)
3938             t = one, one = two, two = t;
3939
3940           val = iterative_hash_object (one, val);
3941           val = iterative_hash_object (two, val);
3942         }
3943       else
3944         for (i = first_rtl_op (code) - 1; i >= 0; --i)
3945           val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3946     }
3947   else if (code == TREE_LIST)
3948     {
3949       /* A list of expressions, for a CALL_EXPR or as the elements of a
3950          VECTOR_CST.  */
3951       for (; t; t = TREE_CHAIN (t))
3952         val = iterative_hash_expr (TREE_VALUE (t), val);
3953     }
3954   else if (code == SSA_NAME)
3955     {
3956       val = iterative_hash_object (SSA_NAME_VERSION (t), val);
3957       val = iterative_hash_expr (SSA_NAME_VAR (t), val);
3958     }
3959   else
3960     abort ();
3961
3962   return val;
3963 }
3964 \f
3965 /* Constructors for pointer, array and function types.
3966    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3967    constructed by language-dependent code, not here.)  */
3968
3969 /* Construct, lay out and return the type of pointers to TO_TYPE with
3970    mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
3971    reference all of memory. If such a type has already been
3972    constructed, reuse it.  */
3973
3974 tree
3975 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
3976                              bool can_alias_all)
3977 {
3978   tree t;
3979
3980   /* In some cases, languages will have things that aren't a POINTER_TYPE
3981      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
3982      In that case, return that type without regard to the rest of our
3983      operands.
3984
3985      ??? This is a kludge, but consistent with the way this function has
3986      always operated and there doesn't seem to be a good way to avoid this
3987      at the moment.  */
3988   if (TYPE_POINTER_TO (to_type) != 0
3989       && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
3990     return TYPE_POINTER_TO (to_type);
3991
3992   /* First, if we already have a type for pointers to TO_TYPE and it's
3993      the proper mode, use it.  */
3994   for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
3995     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
3996       return t;
3997
3998   t = make_node (POINTER_TYPE);
3999
4000   TREE_TYPE (t) = to_type;
4001   TYPE_MODE (t) = mode;
4002   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
4003   TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
4004   TYPE_POINTER_TO (to_type) = t;
4005
4006   /* Lay out the type.  This function has many callers that are concerned
4007      with expression-construction, and this simplifies them all.  */
4008   layout_type (t);
4009
4010   return t;
4011 }
4012
4013 /* By default build pointers in ptr_mode.  */
4014
4015 tree
4016 build_pointer_type (tree to_type)
4017 {
4018   return build_pointer_type_for_mode (to_type, ptr_mode, false);
4019 }
4020
4021 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
4022
4023 tree
4024 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
4025                                bool can_alias_all)
4026 {
4027   tree t;
4028
4029   /* In some cases, languages will have things that aren't a REFERENCE_TYPE
4030      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
4031      In that case, return that type without regard to the rest of our
4032      operands.
4033
4034      ??? This is a kludge, but consistent with the way this function has
4035      always operated and there doesn't seem to be a good way to avoid this
4036      at the moment.  */
4037   if (TYPE_REFERENCE_TO (to_type) != 0
4038       && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
4039     return TYPE_REFERENCE_TO (to_type);
4040
4041   /* First, if we already have a type for pointers to TO_TYPE and it's
4042      the proper mode, use it.  */
4043   for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
4044     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
4045       return t;
4046
4047   t = make_node (REFERENCE_TYPE);
4048
4049   TREE_TYPE (t) = to_type;
4050   TYPE_MODE (t) = mode;
4051   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
4052   TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
4053   TYPE_REFERENCE_TO (to_type) = t;
4054
4055   layout_type (t);
4056
4057   return t;
4058 }
4059
4060
4061 /* Build the node for the type of references-to-TO_TYPE by default
4062    in ptr_mode.  */
4063
4064 tree
4065 build_reference_type (tree to_type)
4066 {
4067   return build_reference_type_for_mode (to_type, ptr_mode, false);
4068 }
4069
4070 /* Build a type that is compatible with t but has no cv quals anywhere
4071    in its type, thus
4072
4073    const char *const *const *  ->  char ***.  */
4074
4075 tree
4076 build_type_no_quals (tree t)
4077 {
4078   switch (TREE_CODE (t))
4079     {
4080     case POINTER_TYPE:
4081       return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
4082                                           TYPE_MODE (t),
4083                                           TYPE_REF_CAN_ALIAS_ALL (t));
4084     case REFERENCE_TYPE:
4085       return
4086         build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
4087                                        TYPE_MODE (t),
4088                                        TYPE_REF_CAN_ALIAS_ALL (t));
4089     default:
4090       return TYPE_MAIN_VARIANT (t);
4091     }
4092 }
4093
4094 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
4095    MAXVAL should be the maximum value in the domain
4096    (one less than the length of the array).
4097
4098    The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
4099    We don't enforce this limit, that is up to caller (e.g. language front end).
4100    The limit exists because the result is a signed type and we don't handle
4101    sizes that use more than one HOST_WIDE_INT.  */
4102
4103 tree
4104 build_index_type (tree maxval)
4105 {
4106   tree itype = make_node (INTEGER_TYPE);
4107
4108   TREE_TYPE (itype) = sizetype;
4109   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
4110   TYPE_MIN_VALUE (itype) = size_zero_node;
4111   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
4112   TYPE_MODE (itype) = TYPE_MODE (sizetype);
4113   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
4114   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
4115   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
4116   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
4117
4118   if (host_integerp (maxval, 1))
4119     return type_hash_canon (tree_low_cst (maxval, 1), itype);
4120   else
4121     return itype;
4122 }
4123
4124 /* Builds a signed or unsigned integer type of precision PRECISION.
4125    Used for C bitfields whose precision does not match that of
4126    built-in target types.  */
4127 tree
4128 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
4129                                 int unsignedp)
4130 {
4131   tree itype = make_node (INTEGER_TYPE);
4132
4133   TYPE_PRECISION (itype) = precision;
4134
4135   if (unsignedp)
4136     fixup_unsigned_type (itype);
4137   else
4138     fixup_signed_type (itype);
4139
4140   if (host_integerp (TYPE_MAX_VALUE (itype), 1))
4141     return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
4142
4143   return itype;
4144 }
4145
4146 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
4147    ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
4148    low bound LOWVAL and high bound HIGHVAL.
4149    if TYPE==NULL_TREE, sizetype is used.  */
4150
4151 tree
4152 build_range_type (tree type, tree lowval, tree highval)
4153 {
4154   tree itype = make_node (INTEGER_TYPE);
4155
4156   TREE_TYPE (itype) = type;
4157   if (type == NULL_TREE)
4158     type = sizetype;
4159
4160   TYPE_MIN_VALUE (itype) = convert (type, lowval);
4161   TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
4162
4163   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
4164   TYPE_MODE (itype) = TYPE_MODE (type);
4165   TYPE_SIZE (itype) = TYPE_SIZE (type);
4166   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
4167   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
4168   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
4169
4170   if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
4171     return type_hash_canon (tree_low_cst (highval, 0)
4172                             - tree_low_cst (lowval, 0),
4173                             itype);
4174   else
4175     return itype;
4176 }
4177
4178 /* Just like build_index_type, but takes lowval and highval instead
4179    of just highval (maxval).  */
4180
4181 tree
4182 build_index_2_type (tree lowval, tree highval)
4183 {
4184   return build_range_type (sizetype, lowval, highval);
4185 }
4186
4187 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
4188    and number of elements specified by the range of values of INDEX_TYPE.
4189    If such a type has already been constructed, reuse it.  */
4190
4191 tree
4192 build_array_type (tree elt_type, tree index_type)
4193 {
4194   tree t;
4195   hashval_t hashcode = 0;
4196
4197   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
4198     {
4199       error ("arrays of functions are not meaningful");
4200       elt_type = integer_type_node;
4201     }
4202
4203   t = make_node (ARRAY_TYPE);
4204   TREE_TYPE (t) = elt_type;
4205   TYPE_DOMAIN (t) = index_type;
4206
4207   if (index_type == 0)
4208     return t;
4209
4210   hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
4211   hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
4212   t = type_hash_canon (hashcode, t);
4213
4214   if (!COMPLETE_TYPE_P (t))
4215     layout_type (t);
4216   return t;
4217 }
4218
4219 /* Return the TYPE of the elements comprising
4220    the innermost dimension of ARRAY.  */
4221
4222 tree
4223 get_inner_array_type (tree array)
4224 {
4225   tree type = TREE_TYPE (array);
4226
4227   while (TREE_CODE (type) == ARRAY_TYPE)
4228     type = TREE_TYPE (type);
4229
4230   return type;
4231 }
4232
4233 /* Construct, lay out and return
4234    the type of functions returning type VALUE_TYPE
4235    given arguments of types ARG_TYPES.
4236    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
4237    are data type nodes for the arguments of the function.
4238    If such a type has already been constructed, reuse it.  */
4239
4240 tree
4241 build_function_type (tree value_type, tree arg_types)
4242 {
4243   tree t;
4244   hashval_t hashcode = 0;
4245
4246   if (TREE_CODE (value_type) == FUNCTION_TYPE)
4247     {
4248       error ("function return type cannot be function");
4249       value_type = integer_type_node;
4250     }
4251
4252   /* Make a node of the sort we want.  */
4253   t = make_node (FUNCTION_TYPE);
4254   TREE_TYPE (t) = value_type;
4255   TYPE_ARG_TYPES (t) = arg_types;
4256
4257   /* If we already have such a type, use the old one.  */
4258   hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
4259   hashcode = type_hash_list (arg_types, hashcode);
4260   t = type_hash_canon (hashcode, t);
4261
4262   if (!COMPLETE_TYPE_P (t))
4263     layout_type (t);
4264   return t;
4265 }
4266
4267 /* Build a function type.  The RETURN_TYPE is the type returned by the
4268    function.  If additional arguments are provided, they are
4269    additional argument types.  The list of argument types must always
4270    be terminated by NULL_TREE.  */
4271
4272 tree
4273 build_function_type_list (tree return_type, ...)
4274 {
4275   tree t, args, last;
4276   va_list p;
4277
4278   va_start (p, return_type);
4279
4280   t = va_arg (p, tree);
4281   for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
4282     args = tree_cons (NULL_TREE, t, args);
4283
4284   last = args;
4285   args = nreverse (args);
4286   TREE_CHAIN (last) = void_list_node;
4287   args = build_function_type (return_type, args);
4288
4289   va_end (p);
4290   return args;
4291 }
4292
4293 /* Build a METHOD_TYPE for a member of BASETYPE.  The RETTYPE (a TYPE)
4294    and ARGTYPES (a TREE_LIST) are the return type and arguments types
4295    for the method.  An implicit additional parameter (of type
4296    pointer-to-BASETYPE) is added to the ARGTYPES.  */
4297
4298 tree
4299 build_method_type_directly (tree basetype,
4300                             tree rettype,
4301                             tree argtypes)
4302 {
4303   tree t;
4304   tree ptype;
4305   int hashcode = 0;
4306
4307   /* Make a node of the sort we want.  */
4308   t = make_node (METHOD_TYPE);
4309
4310   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4311   TREE_TYPE (t) = rettype;
4312   ptype = build_pointer_type (basetype);
4313
4314   /* The actual arglist for this function includes a "hidden" argument
4315      which is "this".  Put it into the list of argument types.  */
4316   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
4317   TYPE_ARG_TYPES (t) = argtypes;
4318
4319   /* If we already have such a type, use the old one.  */
4320   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
4321   hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
4322   hashcode = type_hash_list (argtypes, hashcode);
4323   t = type_hash_canon (hashcode, t);
4324
4325   if (!COMPLETE_TYPE_P (t))
4326     layout_type (t);
4327
4328   return t;
4329 }
4330
4331 /* Construct, lay out and return the type of methods belonging to class
4332    BASETYPE and whose arguments and values are described by TYPE.
4333    If that type exists already, reuse it.
4334    TYPE must be a FUNCTION_TYPE node.  */
4335
4336 tree
4337 build_method_type (tree basetype, tree type)
4338 {
4339   if (TREE_CODE (type) != FUNCTION_TYPE)
4340     abort ();
4341
4342   return build_method_type_directly (basetype,
4343                                      TREE_TYPE (type),
4344                                      TYPE_ARG_TYPES (type));
4345 }
4346
4347 /* Construct, lay out and return the type of offsets to a value
4348    of type TYPE, within an object of type BASETYPE.
4349    If a suitable offset type exists already, reuse it.  */
4350
4351 tree
4352 build_offset_type (tree basetype, tree type)
4353 {
4354   tree t;
4355   hashval_t hashcode = 0;
4356
4357   /* Make a node of the sort we want.  */
4358   t = make_node (OFFSET_TYPE);
4359
4360   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4361   TREE_TYPE (t) = type;
4362
4363   /* If we already have such a type, use the old one.  */
4364   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
4365   hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
4366   t = type_hash_canon (hashcode, t);
4367
4368   if (!COMPLETE_TYPE_P (t))
4369     layout_type (t);
4370
4371   return t;
4372 }
4373
4374 /* Create a complex type whose components are COMPONENT_TYPE.  */
4375
4376 tree
4377 build_complex_type (tree component_type)
4378 {
4379   tree t;
4380   hashval_t hashcode;
4381
4382   /* Make a node of the sort we want.  */
4383   t = make_node (COMPLEX_TYPE);
4384
4385   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4386
4387   /* If we already have such a type, use the old one.  */
4388   hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
4389   t = type_hash_canon (hashcode, t);
4390
4391   if (!COMPLETE_TYPE_P (t))
4392     layout_type (t);
4393
4394   /* If we are writing Dwarf2 output we need to create a name,
4395      since complex is a fundamental type.  */
4396   if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4397       && ! TYPE_NAME (t))
4398     {
4399       const char *name;
4400       if (component_type == char_type_node)
4401         name = "complex char";
4402       else if (component_type == signed_char_type_node)
4403         name = "complex signed char";
4404       else if (component_type == unsigned_char_type_node)
4405         name = "complex unsigned char";
4406       else if (component_type == short_integer_type_node)
4407         name = "complex short int";
4408       else if (component_type == short_unsigned_type_node)
4409         name = "complex short unsigned int";
4410       else if (component_type == integer_type_node)
4411         name = "complex int";
4412       else if (component_type == unsigned_type_node)
4413         name = "complex unsigned int";
4414       else if (component_type == long_integer_type_node)
4415         name = "complex long int";
4416       else if (component_type == long_unsigned_type_node)
4417         name = "complex long unsigned int";
4418       else if (component_type == long_long_integer_type_node)
4419         name = "complex long long int";
4420       else if (component_type == long_long_unsigned_type_node)
4421         name = "complex long long unsigned int";
4422       else
4423         name = 0;
4424
4425       if (name != 0)
4426         TYPE_NAME (t) = get_identifier (name);
4427     }
4428
4429   return build_qualified_type (t, TYPE_QUALS (component_type));
4430 }
4431 \f
4432 /* Return OP, stripped of any conversions to wider types as much as is safe.
4433    Converting the value back to OP's type makes a value equivalent to OP.
4434
4435    If FOR_TYPE is nonzero, we return a value which, if converted to
4436    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4437
4438    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4439    narrowest type that can hold the value, even if they don't exactly fit.
4440    Otherwise, bit-field references are changed to a narrower type
4441    only if they can be fetched directly from memory in that type.
4442
4443    OP must have integer, real or enumeral type.  Pointers are not allowed!
4444
4445    There are some cases where the obvious value we could return
4446    would regenerate to OP if converted to OP's type,
4447    but would not extend like OP to wider types.
4448    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4449    For example, if OP is (unsigned short)(signed char)-1,
4450    we avoid returning (signed char)-1 if FOR_TYPE is int,
4451    even though extending that to an unsigned short would regenerate OP,
4452    since the result of extending (signed char)-1 to (int)
4453    is different from (int) OP.  */
4454
4455 tree
4456 get_unwidened (tree op, tree for_type)
4457 {
4458   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
4459   tree type = TREE_TYPE (op);
4460   unsigned final_prec
4461     = TYPE_PRECISION (for_type != 0 ? for_type : type);
4462   int uns
4463     = (for_type != 0 && for_type != type
4464        && final_prec > TYPE_PRECISION (type)
4465        && TYPE_UNSIGNED (type));
4466   tree win = op;
4467
4468   while (TREE_CODE (op) == NOP_EXPR)
4469     {
4470       int bitschange
4471         = TYPE_PRECISION (TREE_TYPE (op))
4472           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4473
4474       /* Truncations are many-one so cannot be removed.
4475          Unless we are later going to truncate down even farther.  */
4476       if (bitschange < 0
4477           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4478         break;
4479
4480       /* See what's inside this conversion.  If we decide to strip it,
4481          we will set WIN.  */
4482       op = TREE_OPERAND (op, 0);
4483
4484       /* If we have not stripped any zero-extensions (uns is 0),
4485          we can strip any kind of extension.
4486          If we have previously stripped a zero-extension,
4487          only zero-extensions can safely be stripped.
4488          Any extension can be stripped if the bits it would produce
4489          are all going to be discarded later by truncating to FOR_TYPE.  */
4490
4491       if (bitschange > 0)
4492         {
4493           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4494             win = op;
4495           /* TYPE_UNSIGNED says whether this is a zero-extension.
4496              Let's avoid computing it if it does not affect WIN
4497              and if UNS will not be needed again.  */
4498           if ((uns || TREE_CODE (op) == NOP_EXPR)
4499               && TYPE_UNSIGNED (TREE_TYPE (op)))
4500             {
4501               uns = 1;
4502               win = op;
4503             }
4504         }
4505     }
4506
4507   if (TREE_CODE (op) == COMPONENT_REF
4508       /* Since type_for_size always gives an integer type.  */
4509       && TREE_CODE (type) != REAL_TYPE
4510       /* Don't crash if field not laid out yet.  */
4511       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4512       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4513     {
4514       unsigned int innerprec
4515         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4516       int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
4517                        || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4518       type = lang_hooks.types.type_for_size (innerprec, unsignedp);
4519
4520       /* We can get this structure field in the narrowest type it fits in.
4521          If FOR_TYPE is 0, do this only for a field that matches the
4522          narrower type exactly and is aligned for it
4523          The resulting extension to its nominal type (a fullword type)
4524          must fit the same conditions as for other extensions.  */
4525
4526       if (type != 0
4527           && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
4528           && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4529           && (! uns || final_prec <= innerprec || unsignedp))
4530         {
4531           win = build3 (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4532                         TREE_OPERAND (op, 1), NULL_TREE);
4533           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4534           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4535         }
4536     }
4537
4538   return win;
4539 }
4540 \f
4541 /* Return OP or a simpler expression for a narrower value
4542    which can be sign-extended or zero-extended to give back OP.
4543    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4544    or 0 if the value should be sign-extended.  */
4545
4546 tree
4547 get_narrower (tree op, int *unsignedp_ptr)
4548 {
4549   int uns = 0;
4550   int first = 1;
4551   tree win = op;
4552   bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
4553
4554   while (TREE_CODE (op) == NOP_EXPR)
4555     {
4556       int bitschange
4557         = (TYPE_PRECISION (TREE_TYPE (op))
4558            - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4559
4560       /* Truncations are many-one so cannot be removed.  */
4561       if (bitschange < 0)
4562         break;
4563
4564       /* See what's inside this conversion.  If we decide to strip it,
4565          we will set WIN.  */
4566
4567       if (bitschange > 0)
4568         {
4569           op = TREE_OPERAND (op, 0);
4570           /* An extension: the outermost one can be stripped,
4571              but remember whether it is zero or sign extension.  */
4572           if (first)
4573             uns = TYPE_UNSIGNED (TREE_TYPE (op));
4574           /* Otherwise, if a sign extension has been stripped,
4575              only sign extensions can now be stripped;
4576              if a zero extension has been stripped, only zero-extensions.  */
4577           else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
4578             break;
4579           first = 0;
4580         }
4581       else /* bitschange == 0 */
4582         {
4583           /* A change in nominal type can always be stripped, but we must
4584              preserve the unsignedness.  */
4585           if (first)
4586             uns = TYPE_UNSIGNED (TREE_TYPE (op));
4587           first = 0;
4588           op = TREE_OPERAND (op, 0);
4589           /* Keep trying to narrow, but don't assign op to win if it
4590              would turn an integral type into something else.  */
4591           if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
4592             continue;
4593         }
4594
4595       win = op;
4596     }
4597
4598   if (TREE_CODE (op) == COMPONENT_REF
4599       /* Since type_for_size always gives an integer type.  */
4600       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4601       /* Ensure field is laid out already.  */
4602       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4603       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4604     {
4605       unsigned HOST_WIDE_INT innerprec
4606         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4607       int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
4608                        || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4609       tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
4610
4611       /* We can get this structure field in a narrower type that fits it,
4612          but the resulting extension to its nominal type (a fullword type)
4613          must satisfy the same conditions as for other extensions.
4614
4615          Do this only for fields that are aligned (not bit-fields),
4616          because when bit-field insns will be used there is no
4617          advantage in doing this.  */
4618
4619       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4620           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4621           && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
4622           && type != 0)
4623         {
4624           if (first)
4625             uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
4626           win = build3 (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4627                         TREE_OPERAND (op, 1), NULL_TREE);
4628           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4629           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4630         }
4631     }
4632   *unsignedp_ptr = uns;
4633   return win;
4634 }
4635 \f
4636 /* Nonzero if integer constant C has a value that is permissible
4637    for type TYPE (an INTEGER_TYPE).  */
4638
4639 int
4640 int_fits_type_p (tree c, tree type)
4641 {
4642   tree type_low_bound = TYPE_MIN_VALUE (type);
4643   tree type_high_bound = TYPE_MAX_VALUE (type);
4644   int ok_for_low_bound, ok_for_high_bound;
4645
4646   /* Perform some generic filtering first, which may allow making a decision
4647      even if the bounds are not constant.  First, negative integers never fit
4648      in unsigned types, */
4649   if ((TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4650       /* Also, unsigned integers with top bit set never fit signed types.  */
4651       || (! TYPE_UNSIGNED (type)
4652           && TYPE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4653     return 0;
4654
4655   /* If at least one bound of the type is a constant integer, we can check
4656      ourselves and maybe make a decision. If no such decision is possible, but
4657      this type is a subtype, try checking against that.  Otherwise, use
4658      force_fit_type, which checks against the precision.
4659
4660      Compute the status for each possibly constant bound, and return if we see
4661      one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4662      for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4663      for "constant known to fit".  */
4664
4665   ok_for_low_bound = -1;
4666   ok_for_high_bound = -1;
4667
4668   /* Check if C >= type_low_bound.  */
4669   if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4670     {
4671       ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4672       if (! ok_for_low_bound)
4673         return 0;
4674     }
4675
4676   /* Check if c <= type_high_bound.  */
4677   if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4678     {
4679       ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4680       if (! ok_for_high_bound)
4681         return 0;
4682     }
4683
4684   /* If the constant fits both bounds, the result is known.  */
4685   if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4686     return 1;
4687
4688   /* If we haven't been able to decide at this point, there nothing more we
4689      can check ourselves here. Look at the base type if we have one.  */
4690   else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4691     return int_fits_type_p (c, TREE_TYPE (type));
4692
4693   /* Or to force_fit_type, if nothing else.  */
4694   else
4695     {
4696       c = copy_node (c);
4697       TREE_TYPE (c) = type;
4698       c = force_fit_type (c, -1, false, false);
4699       return !TREE_OVERFLOW (c);
4700     }
4701 }
4702
4703 /* Subprogram of following function.  Called by walk_tree.
4704
4705    Return *TP if it is an automatic variable or parameter of the
4706    function passed in as DATA.  */
4707
4708 static tree
4709 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
4710 {
4711   tree fn = (tree) data;
4712
4713   if (TYPE_P (*tp))
4714     *walk_subtrees = 0;
4715
4716   else if (DECL_P (*tp) && lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
4717     return *tp;
4718
4719   return NULL_TREE;
4720 }
4721
4722 /* Returns true if T is, contains, or refers to a type with variable
4723    size.  If FN is nonzero, only return true if a modifier of the type
4724    or position of FN is a variable or parameter inside FN.
4725
4726    This concept is more general than that of C99 'variably modified types':
4727    in C99, a struct type is never variably modified because a VLA may not
4728    appear as a structure member.  However, in GNU C code like:
4729
4730      struct S { int i[f()]; };
4731
4732    is valid, and other languages may define similar constructs.  */
4733
4734 bool
4735 variably_modified_type_p (tree type, tree fn)
4736 {
4737   tree t;
4738
4739 /* Test if T is either variable (if FN is zero) or an expression containing
4740    a variable in FN.  */
4741 #define RETURN_TRUE_IF_VAR(T)                                           \
4742   do { tree _t = (T);                                                   \
4743     if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST    \
4744         && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL)))        \
4745       return true;  } while (0)
4746
4747   if (type == error_mark_node)
4748     return false;
4749
4750   /* If TYPE itself has variable size, it is variably modified.
4751
4752      We do not yet have a representation of the C99 '[*]' syntax.
4753      When a representation is chosen, this function should be modified
4754      to test for that case as well.  */
4755   RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
4756   RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT(type));
4757
4758   switch (TREE_CODE (type))
4759     {
4760     case POINTER_TYPE:
4761     case REFERENCE_TYPE:
4762     case ARRAY_TYPE:
4763     case SET_TYPE:
4764     case VECTOR_TYPE:
4765       if (variably_modified_type_p (TREE_TYPE (type), fn))
4766         return true;
4767       break;
4768
4769     case FUNCTION_TYPE:
4770     case METHOD_TYPE:
4771       /* If TYPE is a function type, it is variably modified if any of the
4772          parameters or the return type are variably modified.  */
4773       if (variably_modified_type_p (TREE_TYPE (type), fn))
4774           return true;
4775
4776       for (t = TYPE_ARG_TYPES (type);
4777            t && t != void_list_node;
4778            t = TREE_CHAIN (t))
4779         if (variably_modified_type_p (TREE_VALUE (t), fn))
4780           return true;
4781       break;
4782
4783     case INTEGER_TYPE:
4784     case REAL_TYPE:
4785     case ENUMERAL_TYPE:
4786     case BOOLEAN_TYPE:
4787     case CHAR_TYPE:
4788       /* Scalar types are variably modified if their end points
4789          aren't constant.  */
4790       RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
4791       RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
4792       break;
4793
4794     case RECORD_TYPE:
4795     case UNION_TYPE:
4796     case QUAL_UNION_TYPE:
4797       /* We can't see if any of the field are variably-modified by the
4798          definition we normally use, since that would produce infinite
4799          recursion via pointers.  */
4800       /* This is variably modified if some field's type is.  */
4801       for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
4802         if (TREE_CODE (t) == FIELD_DECL)
4803           {
4804             RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
4805             RETURN_TRUE_IF_VAR (DECL_SIZE (t));
4806             RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
4807
4808             if (TREE_CODE (type) == QUAL_UNION_TYPE)
4809               RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
4810           }
4811         break;
4812
4813     default:
4814       break;
4815     }
4816
4817   /* The current language may have other cases to check, but in general,
4818      all other types are not variably modified.  */
4819   return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
4820
4821 #undef RETURN_TRUE_IF_VAR
4822 }
4823
4824 /* Given a DECL or TYPE, return the scope in which it was declared, or
4825    NULL_TREE if there is no containing scope.  */
4826
4827 tree
4828 get_containing_scope (tree t)
4829 {
4830   return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4831 }
4832
4833 /* Return the innermost context enclosing DECL that is
4834    a FUNCTION_DECL, or zero if none.  */
4835
4836 tree
4837 decl_function_context (tree decl)
4838 {
4839   tree context;
4840
4841   if (TREE_CODE (decl) == ERROR_MARK)
4842     return 0;
4843
4844   /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4845      where we look up the function at runtime.  Such functions always take
4846      a first argument of type 'pointer to real context'.
4847
4848      C++ should really be fixed to use DECL_CONTEXT for the real context,
4849      and use something else for the "virtual context".  */
4850   else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4851     context
4852       = TYPE_MAIN_VARIANT
4853         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4854   else
4855     context = DECL_CONTEXT (decl);
4856
4857   while (context && TREE_CODE (context) != FUNCTION_DECL)
4858     {
4859       if (TREE_CODE (context) == BLOCK)
4860         context = BLOCK_SUPERCONTEXT (context);
4861       else
4862         context = get_containing_scope (context);
4863     }
4864
4865   return context;
4866 }
4867
4868 /* Return the innermost context enclosing DECL that is
4869    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4870    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
4871
4872 tree
4873 decl_type_context (tree decl)
4874 {
4875   tree context = DECL_CONTEXT (decl);
4876
4877   while (context)
4878     switch (TREE_CODE (context))
4879       {
4880       case NAMESPACE_DECL:
4881       case TRANSLATION_UNIT_DECL:
4882         return NULL_TREE;
4883
4884       case RECORD_TYPE:
4885       case UNION_TYPE:
4886       case QUAL_UNION_TYPE:
4887         return context;
4888
4889       case TYPE_DECL:
4890       case FUNCTION_DECL:
4891         context = DECL_CONTEXT (context);
4892         break;
4893
4894       case BLOCK:
4895         context = BLOCK_SUPERCONTEXT (context);
4896         break;
4897
4898       default:
4899         abort ();
4900       }
4901
4902   return NULL_TREE;
4903 }
4904
4905 /* CALL is a CALL_EXPR.  Return the declaration for the function
4906    called, or NULL_TREE if the called function cannot be
4907    determined.  */
4908
4909 tree
4910 get_callee_fndecl (tree call)
4911 {
4912   tree addr;
4913
4914   /* It's invalid to call this function with anything but a
4915      CALL_EXPR.  */
4916   if (TREE_CODE (call) != CALL_EXPR)
4917     abort ();
4918
4919   /* The first operand to the CALL is the address of the function
4920      called.  */
4921   addr = TREE_OPERAND (call, 0);
4922
4923   STRIP_NOPS (addr);
4924
4925   /* If this is a readonly function pointer, extract its initial value.  */
4926   if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4927       && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4928       && DECL_INITIAL (addr))
4929     addr = DECL_INITIAL (addr);
4930
4931   /* If the address is just `&f' for some function `f', then we know
4932      that `f' is being called.  */
4933   if (TREE_CODE (addr) == ADDR_EXPR
4934       && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4935     return TREE_OPERAND (addr, 0);
4936
4937   /* We couldn't figure out what was being called.  Maybe the front
4938      end has some idea.  */
4939   return lang_hooks.lang_get_callee_fndecl (call);
4940 }
4941
4942 /* Print debugging information about tree nodes generated during the compile,
4943    and any language-specific information.  */
4944
4945 void
4946 dump_tree_statistics (void)
4947 {
4948 #ifdef GATHER_STATISTICS
4949   int i;
4950   int total_nodes, total_bytes;
4951 #endif
4952
4953   fprintf (stderr, "\n??? tree nodes created\n\n");
4954 #ifdef GATHER_STATISTICS
4955   fprintf (stderr, "Kind                   Nodes      Bytes\n");
4956   fprintf (stderr, "---------------------------------------\n");
4957   total_nodes = total_bytes = 0;
4958   for (i = 0; i < (int) all_kinds; i++)
4959     {
4960       fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4961                tree_node_counts[i], tree_node_sizes[i]);
4962       total_nodes += tree_node_counts[i];
4963       total_bytes += tree_node_sizes[i];
4964     }
4965   fprintf (stderr, "---------------------------------------\n");
4966   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4967   fprintf (stderr, "---------------------------------------\n");
4968   ssanames_print_statistics ();
4969   phinodes_print_statistics ();
4970 #else
4971   fprintf (stderr, "(No per-node statistics)\n");
4972 #endif
4973   print_type_hash_statistics ();
4974   lang_hooks.print_statistics ();
4975 }
4976 \f
4977 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4978
4979 /* Generate a crc32 of a string.  */
4980
4981 unsigned
4982 crc32_string (unsigned chksum, const char *string)
4983 {
4984   do
4985     {
4986       unsigned value = *string << 24;
4987       unsigned ix;
4988
4989       for (ix = 8; ix--; value <<= 1)
4990         {
4991           unsigned feedback;
4992
4993           feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4994           chksum <<= 1;
4995           chksum ^= feedback;
4996         }
4997     }
4998   while (*string++);
4999   return chksum;
5000 }
5001
5002 /* P is a string that will be used in a symbol.  Mask out any characters
5003    that are not valid in that context.  */
5004
5005 void
5006 clean_symbol_name (char *p)
5007 {
5008   for (; *p; p++)
5009     if (! (ISALNUM (*p)
5010 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
5011             || *p == '$'
5012 #endif
5013 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but...  */
5014             || *p == '.'
5015 #endif
5016            ))
5017       *p = '_';
5018 }
5019
5020 /* Generate a name for a function unique to this translation unit.
5021    TYPE is some string to identify the purpose of this function to the
5022    linker or collect2.  */
5023
5024 tree
5025 get_file_function_name_long (const char *type)
5026 {
5027   char *buf;
5028   const char *p;
5029   char *q;
5030
5031   if (first_global_object_name)
5032     p = first_global_object_name;
5033   else
5034     {
5035       /* We don't have anything that we know to be unique to this translation
5036          unit, so use what we do have and throw in some randomness.  */
5037       unsigned len;
5038       const char *name = weak_global_object_name;
5039       const char *file = main_input_filename;
5040
5041       if (! name)
5042         name = "";
5043       if (! file)
5044         file = input_filename;
5045
5046       len = strlen (file);
5047       q = alloca (9 * 2 + len + 1);
5048       memcpy (q, file, len + 1);
5049       clean_symbol_name (q);
5050
5051       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
5052                crc32_string (0, flag_random_seed));
5053
5054       p = q;
5055     }
5056
5057   buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
5058
5059   /* Set up the name of the file-level functions we may need.
5060      Use a global object (which is already required to be unique over
5061      the program) rather than the file name (which imposes extra
5062      constraints).  */
5063   sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
5064
5065   return get_identifier (buf);
5066 }
5067
5068 /* If KIND=='I', return a suitable global initializer (constructor) name.
5069    If KIND=='D', return a suitable global clean-up (destructor) name.  */
5070
5071 tree
5072 get_file_function_name (int kind)
5073 {
5074   char p[2];
5075
5076   p[0] = kind;
5077   p[1] = 0;
5078
5079   return get_file_function_name_long (p);
5080 }
5081 \f
5082 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
5083    The result is placed in BUFFER (which has length BIT_SIZE),
5084    with one bit in each char ('\000' or '\001').
5085
5086    If the constructor is constant, NULL_TREE is returned.
5087    Otherwise, a TREE_LIST of the non-constant elements is emitted.  */
5088
5089 tree
5090 get_set_constructor_bits (tree init, char *buffer, int bit_size)
5091 {
5092   int i;
5093   tree vals;
5094   HOST_WIDE_INT domain_min
5095     = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
5096   tree non_const_bits = NULL_TREE;
5097
5098   for (i = 0; i < bit_size; i++)
5099     buffer[i] = 0;
5100
5101   for (vals = TREE_OPERAND (init, 1);
5102        vals != NULL_TREE; vals = TREE_CHAIN (vals))
5103     {
5104       if (!host_integerp (TREE_VALUE (vals), 0)
5105           || (TREE_PURPOSE (vals) != NULL_TREE
5106               && !host_integerp (TREE_PURPOSE (vals), 0)))
5107         non_const_bits
5108           = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
5109       else if (TREE_PURPOSE (vals) != NULL_TREE)
5110         {
5111           /* Set a range of bits to ones.  */
5112           HOST_WIDE_INT lo_index
5113             = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
5114           HOST_WIDE_INT hi_index
5115             = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
5116
5117           if (lo_index < 0 || lo_index >= bit_size
5118               || hi_index < 0 || hi_index >= bit_size)
5119             abort ();
5120           for (; lo_index <= hi_index; lo_index++)
5121             buffer[lo_index] = 1;
5122         }
5123       else
5124         {
5125           /* Set a single bit to one.  */
5126           HOST_WIDE_INT index
5127             = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
5128           if (index < 0 || index >= bit_size)
5129             {
5130               error ("invalid initializer for bit string");
5131               return NULL_TREE;
5132             }
5133           buffer[index] = 1;
5134         }
5135     }
5136   return non_const_bits;
5137 }
5138
5139 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
5140    The result is placed in BUFFER (which is an array of bytes).
5141    If the constructor is constant, NULL_TREE is returned.
5142    Otherwise, a TREE_LIST of the non-constant elements is emitted.  */
5143
5144 tree
5145 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
5146 {
5147   int i;
5148   int set_word_size = BITS_PER_UNIT;
5149   int bit_size = wd_size * set_word_size;
5150   int bit_pos = 0;
5151   unsigned char *bytep = buffer;
5152   char *bit_buffer = alloca (bit_size);
5153   tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
5154
5155   for (i = 0; i < wd_size; i++)
5156     buffer[i] = 0;
5157
5158   for (i = 0; i < bit_size; i++)
5159     {
5160       if (bit_buffer[i])
5161         {
5162           if (BYTES_BIG_ENDIAN)
5163             *bytep |= (1 << (set_word_size - 1 - bit_pos));
5164           else
5165             *bytep |= 1 << bit_pos;
5166         }
5167       bit_pos++;
5168       if (bit_pos >= set_word_size)
5169         bit_pos = 0, bytep++;
5170     }
5171   return non_const_bits;
5172 }
5173 \f
5174 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5175
5176 /* Complain that the tree code of NODE does not match the expected 0
5177    terminated list of trailing codes. FILE, LINE, and FUNCTION are of
5178    the caller.  */
5179
5180 void
5181 tree_check_failed (const tree node, const char *file,
5182                    int line, const char *function, ...)
5183 {
5184   va_list args;
5185   char *buffer;
5186   unsigned length = 0;
5187   int code;
5188
5189   va_start (args, function);
5190   while ((code = va_arg (args, int)))
5191     length += 4 + strlen (tree_code_name[code]);
5192   va_end (args);
5193   va_start (args, function);
5194   buffer = alloca (length);
5195   length = 0;
5196   while ((code = va_arg (args, int)))
5197     {
5198       if (length)
5199         {
5200           strcpy (buffer + length, " or ");
5201           length += 4;
5202         }
5203       strcpy (buffer + length, tree_code_name[code]);
5204       length += strlen (tree_code_name[code]);
5205     }
5206   va_end (args);
5207
5208   internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
5209                   buffer, tree_code_name[TREE_CODE (node)],
5210                   function, trim_filename (file), line);
5211 }
5212
5213 /* Complain that the tree code of NODE does match the expected 0
5214    terminated list of trailing codes. FILE, LINE, and FUNCTION are of
5215    the caller.  */
5216
5217 void
5218 tree_not_check_failed (const tree node, const char *file,
5219                        int line, const char *function, ...)
5220 {
5221   va_list args;
5222   char *buffer;
5223   unsigned length = 0;
5224   int code;
5225
5226   va_start (args, function);
5227   while ((code = va_arg (args, int)))
5228     length += 4 + strlen (tree_code_name[code]);
5229   va_end (args);
5230   va_start (args, function);
5231   buffer = alloca (length);
5232   length = 0;
5233   while ((code = va_arg (args, int)))
5234     {
5235       if (length)
5236         {
5237           strcpy (buffer + length, " or ");
5238           length += 4;
5239         }
5240       strcpy (buffer + length, tree_code_name[code]);
5241       length += strlen (tree_code_name[code]);
5242     }
5243   va_end (args);
5244
5245   internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
5246                   buffer, tree_code_name[TREE_CODE (node)],
5247                   function, trim_filename (file), line);
5248 }
5249
5250 /* Similar to tree_check_failed, except that we check for a class of tree
5251    code, given in CL.  */
5252
5253 void
5254 tree_class_check_failed (const tree node, int cl, const char *file,
5255                          int line, const char *function)
5256 {
5257   internal_error
5258     ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
5259      cl, TREE_CODE_CLASS (TREE_CODE (node)),
5260      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
5261 }
5262
5263 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
5264    (dynamically sized) vector.  */
5265
5266 void
5267 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
5268                            const char *function)
5269 {
5270   internal_error
5271     ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
5272      idx + 1, len, function, trim_filename (file), line);
5273 }
5274
5275 /* Similar to above, except that the check is for the bounds of a PHI_NODE's
5276    (dynamically sized) vector.  */
5277
5278 void
5279 phi_node_elt_check_failed (int idx, int len, const char *file, int line,
5280                             const char *function)
5281 {
5282   internal_error
5283     ("tree check: accessed elt %d of phi_node with %d elts in %s, at %s:%d",
5284      idx + 1, len, function, trim_filename (file), line);
5285 }
5286
5287 /* Similar to above, except that the check is for the bounds of the operand
5288    vector of an expression node.  */
5289
5290 void
5291 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
5292                            int line, const char *function)
5293 {
5294   internal_error
5295     ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
5296      idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
5297      function, trim_filename (file), line);
5298 }
5299 #endif /* ENABLE_TREE_CHECKING */
5300 \f
5301 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
5302    and mapped to the machine mode MODE.  Initialize its fields and build
5303    the information necessary for debugging output.  */
5304
5305 static tree
5306 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
5307 {
5308   tree t = make_node (VECTOR_TYPE);
5309
5310   TREE_TYPE (t) = innertype;
5311   TYPE_VECTOR_SUBPARTS (t) = nunits;
5312   TYPE_MODE (t) = mode;
5313   layout_type (t);
5314
5315   {
5316     tree index = build_int_cst (NULL_TREE, nunits - 1, 0);
5317     tree array = build_array_type (innertype, build_index_type (index));
5318     tree rt = make_node (RECORD_TYPE);
5319
5320     TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
5321     DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
5322     layout_type (rt);
5323     TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
5324     /* In dwarfout.c, type lookup uses TYPE_UID numbers.  We want to output
5325        the representation type, and we want to find that die when looking up
5326        the vector type.  This is most easily achieved by making the TYPE_UID
5327        numbers equal.  */
5328     TYPE_UID (rt) = TYPE_UID (t);
5329   }
5330
5331   return t;
5332 }
5333
5334 static tree
5335 make_or_reuse_type (unsigned size, int unsignedp)
5336 {
5337   if (size == INT_TYPE_SIZE)
5338     return unsignedp ? unsigned_type_node : integer_type_node;
5339   if (size == CHAR_TYPE_SIZE)
5340     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
5341   if (size == SHORT_TYPE_SIZE)
5342     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
5343   if (size == LONG_TYPE_SIZE)
5344     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
5345   if (size == LONG_LONG_TYPE_SIZE)
5346     return (unsignedp ? long_long_unsigned_type_node
5347             : long_long_integer_type_node);
5348
5349   if (unsignedp)
5350     return make_unsigned_type (size);
5351   else
5352     return make_signed_type (size);
5353 }
5354
5355 /* Create nodes for all integer types (and error_mark_node) using the sizes
5356    of C datatypes.  The caller should call set_sizetype soon after calling
5357    this function to select one of the types as sizetype.  */
5358
5359 void
5360 build_common_tree_nodes (int signed_char)
5361 {
5362   error_mark_node = make_node (ERROR_MARK);
5363   TREE_TYPE (error_mark_node) = error_mark_node;
5364
5365   initialize_sizetypes ();
5366
5367   /* Define both `signed char' and `unsigned char'.  */
5368   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
5369   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
5370
5371   /* Define `char', which is like either `signed char' or `unsigned char'
5372      but not the same as either.  */
5373   char_type_node
5374     = (signed_char
5375        ? make_signed_type (CHAR_TYPE_SIZE)
5376        : make_unsigned_type (CHAR_TYPE_SIZE));
5377
5378   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
5379   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
5380   integer_type_node = make_signed_type (INT_TYPE_SIZE);
5381   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
5382   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
5383   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
5384   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
5385   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
5386
5387   /* Define a boolean type.  This type only represents boolean values but
5388      may be larger than char depending on the value of BOOL_TYPE_SIZE.
5389      Front ends which want to override this size (i.e. Java) can redefine
5390      boolean_type_node before calling build_common_tree_nodes_2.  */
5391   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
5392   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
5393   TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1, 0);
5394   TYPE_PRECISION (boolean_type_node) = 1;
5395
5396   /* Fill in the rest of the sized types.  Reuse existing type nodes
5397      when possible.  */
5398   intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
5399   intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
5400   intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
5401   intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
5402   intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
5403
5404   unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
5405   unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
5406   unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
5407   unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
5408   unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
5409
5410   access_public_node = get_identifier ("public");
5411   access_protected_node = get_identifier ("protected");
5412   access_private_node = get_identifier ("private");
5413 }
5414
5415 /* Call this function after calling build_common_tree_nodes and set_sizetype.
5416    It will create several other common tree nodes.  */
5417
5418 void
5419 build_common_tree_nodes_2 (int short_double)
5420 {
5421   /* Define these next since types below may used them.  */
5422   integer_zero_node = build_int_cst (NULL_TREE, 0, 0);
5423   integer_one_node = build_int_cst (NULL_TREE, 1, 0);
5424   integer_minus_one_node = build_int_cst (NULL_TREE, -1, -1);
5425
5426   size_zero_node = size_int (0);
5427   size_one_node = size_int (1);
5428   bitsize_zero_node = bitsize_int (0);
5429   bitsize_one_node = bitsize_int (1);
5430   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
5431
5432   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
5433   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
5434
5435   void_type_node = make_node (VOID_TYPE);
5436   layout_type (void_type_node);
5437
5438   /* We are not going to have real types in C with less than byte alignment,
5439      so we might as well not have any types that claim to have it.  */
5440   TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
5441   TYPE_USER_ALIGN (void_type_node) = 0;
5442
5443   null_pointer_node = build_int_cst (build_pointer_type (void_type_node),
5444                                      0, 0);
5445   layout_type (TREE_TYPE (null_pointer_node));
5446
5447   ptr_type_node = build_pointer_type (void_type_node);
5448   const_ptr_type_node
5449     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
5450   fileptr_type_node = ptr_type_node;
5451
5452   float_type_node = make_node (REAL_TYPE);
5453   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
5454   layout_type (float_type_node);
5455
5456   double_type_node = make_node (REAL_TYPE);
5457   if (short_double)
5458     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
5459   else
5460     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
5461   layout_type (double_type_node);
5462
5463   long_double_type_node = make_node (REAL_TYPE);
5464   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
5465   layout_type (long_double_type_node);
5466
5467   float_ptr_type_node = build_pointer_type (float_type_node);
5468   double_ptr_type_node = build_pointer_type (double_type_node);
5469   long_double_ptr_type_node = build_pointer_type (long_double_type_node);
5470   integer_ptr_type_node = build_pointer_type (integer_type_node);
5471
5472   complex_integer_type_node = make_node (COMPLEX_TYPE);
5473   TREE_TYPE (complex_integer_type_node) = integer_type_node;
5474   layout_type (complex_integer_type_node);
5475
5476   complex_float_type_node = make_node (COMPLEX_TYPE);
5477   TREE_TYPE (complex_float_type_node) = float_type_node;
5478   layout_type (complex_float_type_node);
5479
5480   complex_double_type_node = make_node (COMPLEX_TYPE);
5481   TREE_TYPE (complex_double_type_node) = double_type_node;
5482   layout_type (complex_double_type_node);
5483
5484   complex_long_double_type_node = make_node (COMPLEX_TYPE);
5485   TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
5486   layout_type (complex_long_double_type_node);
5487
5488   {
5489     tree t = targetm.build_builtin_va_list ();
5490
5491     /* Many back-ends define record types without setting TYPE_NAME.
5492        If we copied the record type here, we'd keep the original
5493        record type without a name.  This breaks name mangling.  So,
5494        don't copy record types and let c_common_nodes_and_builtins()
5495        declare the type to be __builtin_va_list.  */
5496     if (TREE_CODE (t) != RECORD_TYPE)
5497       t = build_type_copy (t);
5498
5499     va_list_type_node = t;
5500   }
5501 }
5502
5503 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
5504    better way.
5505
5506    If we requested a pointer to a vector, build up the pointers that
5507    we stripped off while looking for the inner type.  Similarly for
5508    return values from functions.
5509
5510    The argument TYPE is the top of the chain, and BOTTOM is the
5511    new type which we will point to.  */
5512
5513 tree
5514 reconstruct_complex_type (tree type, tree bottom)
5515 {
5516   tree inner, outer;
5517
5518   if (POINTER_TYPE_P (type))
5519     {
5520       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5521       outer = build_pointer_type (inner);
5522     }
5523   else if (TREE_CODE (type) == ARRAY_TYPE)
5524     {
5525       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5526       outer = build_array_type (inner, TYPE_DOMAIN (type));
5527     }
5528   else if (TREE_CODE (type) == FUNCTION_TYPE)
5529     {
5530       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5531       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
5532     }
5533   else if (TREE_CODE (type) == METHOD_TYPE)
5534     {
5535       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5536       outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
5537                                           inner,
5538                                           TYPE_ARG_TYPES (type));
5539     }
5540   else
5541     return bottom;
5542
5543   TYPE_READONLY (outer) = TYPE_READONLY (type);
5544   TYPE_VOLATILE (outer) = TYPE_VOLATILE (type);
5545
5546   return outer;
5547 }
5548
5549 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
5550    the inner type.  */
5551 tree
5552 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
5553 {
5554   int nunits;
5555
5556   if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
5557       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
5558     nunits = GET_MODE_NUNITS (mode);
5559
5560   else if (GET_MODE_CLASS (mode) == MODE_INT)
5561     {
5562       /* Check that there are no leftover bits.  */
5563       if (GET_MODE_BITSIZE (mode) % TREE_INT_CST_LOW (TYPE_SIZE (innertype)))
5564         abort ();
5565
5566       nunits = GET_MODE_BITSIZE (mode)
5567                / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
5568     }
5569   else
5570     abort ();
5571
5572   return make_vector_type (innertype, nunits, mode);
5573 }
5574
5575 /* Similarly, but takes the inner type and number of units, which must be
5576    a power of two.  */
5577
5578 tree
5579 build_vector_type (tree innertype, int nunits)
5580 {
5581   return make_vector_type (innertype, nunits, VOIDmode);
5582 }
5583
5584 /* Given an initializer INIT, return TRUE if INIT is zero or some
5585    aggregate of zeros.  Otherwise return FALSE.  */
5586 bool
5587 initializer_zerop (tree init)
5588 {
5589   tree elt;
5590
5591   STRIP_NOPS (init);
5592
5593   switch (TREE_CODE (init))
5594     {
5595     case INTEGER_CST:
5596       return integer_zerop (init);
5597
5598     case REAL_CST:
5599       /* ??? Note that this is not correct for C4X float formats.  There,
5600          a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
5601          negative exponent.  */
5602       return real_zerop (init)
5603         && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5604
5605     case COMPLEX_CST:
5606       return integer_zerop (init)
5607         || (real_zerop (init)
5608             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5609             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5610
5611     case VECTOR_CST:
5612       for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
5613         if (!initializer_zerop (TREE_VALUE (elt)))
5614           return false;
5615       return true;
5616
5617     case CONSTRUCTOR:
5618       elt = CONSTRUCTOR_ELTS (init);
5619       if (elt == NULL_TREE)
5620         return true;
5621
5622       /* A set is empty only if it has no elements.  */
5623       if (TREE_CODE (TREE_TYPE (init)) == SET_TYPE)
5624         return false;
5625
5626       for (; elt ; elt = TREE_CHAIN (elt))
5627         if (! initializer_zerop (TREE_VALUE (elt)))
5628           return false;
5629       return true;
5630
5631     default:
5632       return false;
5633     }
5634 }
5635
5636 void
5637 add_var_to_bind_expr (tree bind_expr, tree var)
5638 {
5639   BIND_EXPR_VARS (bind_expr)
5640     = chainon (BIND_EXPR_VARS (bind_expr), var);
5641   if (BIND_EXPR_BLOCK (bind_expr))
5642     BLOCK_VARS (BIND_EXPR_BLOCK (bind_expr))
5643       = BIND_EXPR_VARS (bind_expr);
5644 }
5645
5646 /* Build an empty statement.  */
5647
5648 tree
5649 build_empty_stmt (void)
5650 {
5651   return build1 (NOP_EXPR, void_type_node, size_zero_node);
5652 }
5653
5654
5655 /* Returns true if it is possible to prove that the index of
5656    an array access REF (an ARRAY_REF expression) falls into the
5657    array bounds.  */
5658
5659 bool
5660 in_array_bounds_p (tree ref)
5661 {
5662   tree idx = TREE_OPERAND (ref, 1);
5663   tree min, max;
5664
5665   if (TREE_CODE (idx) != INTEGER_CST)
5666     return false;
5667
5668   min = array_ref_low_bound (ref);
5669   max = array_ref_up_bound (ref);
5670   if (!min
5671       || !max
5672       || TREE_CODE (min) != INTEGER_CST
5673       || TREE_CODE (max) != INTEGER_CST)
5674     return false;
5675
5676   if (tree_int_cst_lt (idx, min)
5677       || tree_int_cst_lt (max, idx))
5678     return false;
5679
5680   return true;
5681 }
5682
5683 /* Return true if T (assumed to be a DECL) is a global variable.  */
5684
5685 bool
5686 is_global_var (tree t)
5687 {
5688   return (TREE_STATIC (t) || DECL_EXTERNAL (t));
5689 }
5690
5691 /* Return true if T (assumed to be a DECL) must be assigned a memory
5692    location.  */
5693
5694 bool
5695 needs_to_live_in_memory (tree t)
5696 {
5697   return (TREE_ADDRESSABLE (t)
5698           || is_global_var (t)
5699           || (TREE_CODE (t) == RESULT_DECL
5700               && aggregate_value_p (t, current_function_decl)));
5701 }
5702
5703 /* There are situations in which a language considers record types
5704    compatible which have different field lists.  Decide if two fields
5705    are compatible.  It is assumed that the parent records are compatible.  */
5706
5707 bool
5708 fields_compatible_p (tree f1, tree f2)
5709 {
5710   if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
5711                         DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
5712     return false;
5713
5714   if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
5715                         DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
5716     return false;
5717
5718   if (!lang_hooks.types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
5719     return false;
5720
5721   return true;
5722 }
5723
5724 /* Locate within RECORD a field that is compatible with ORIG_FIELD.  */
5725
5726 tree
5727 find_compatible_field (tree record, tree orig_field)
5728 {
5729   tree f;
5730
5731   for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
5732     if (TREE_CODE (f) == FIELD_DECL
5733         && fields_compatible_p (f, orig_field))
5734       return f;
5735
5736   /* ??? Why isn't this on the main fields list?  */
5737   f = TYPE_VFIELD (record);
5738   if (f && TREE_CODE (f) == FIELD_DECL
5739       && fields_compatible_p (f, orig_field))
5740     return f;
5741
5742   /* ??? We should abort here, but Java appears to do Bad Things
5743      with inherited fields.  */
5744   return orig_field;
5745 }
5746
5747 /* Return value of a constant X.  */
5748
5749 HOST_WIDE_INT
5750 int_cst_value (tree x)
5751 {
5752   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
5753   unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
5754   bool negative = ((val >> (bits - 1)) & 1) != 0;
5755
5756   if (bits > HOST_BITS_PER_WIDE_INT)
5757     abort ();
5758
5759   if (negative)
5760     val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
5761   else
5762     val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
5763
5764   return val;
5765 }
5766
5767 /* Returns the greatest common divisor of A and B, which must be
5768    INTEGER_CSTs.  */
5769
5770 tree
5771 tree_fold_gcd (tree a, tree b)
5772 {
5773   tree a_mod_b;
5774   tree type = TREE_TYPE (a);
5775
5776 #if defined ENABLE_CHECKING
5777   if (TREE_CODE (a) != INTEGER_CST
5778       || TREE_CODE (b) != INTEGER_CST)
5779     abort ();
5780 #endif
5781
5782   if (integer_zerop (a))
5783     return b;
5784
5785   if (integer_zerop (b))
5786     return a;
5787
5788   if (tree_int_cst_sgn (a) == -1)
5789     a = fold (build2 (MULT_EXPR, type, a,
5790                       convert (type, integer_minus_one_node)));
5791
5792   if (tree_int_cst_sgn (b) == -1)
5793     b = fold (build2 (MULT_EXPR, type, b,
5794                       convert (type, integer_minus_one_node)));
5795
5796   while (1)
5797     {
5798       a_mod_b = fold (build2 (CEIL_MOD_EXPR, type, a, b));
5799
5800       if (!TREE_INT_CST_LOW (a_mod_b)
5801           && !TREE_INT_CST_HIGH (a_mod_b))
5802         return b;
5803
5804       a = b;
5805       b = a_mod_b;
5806     }
5807 }
5808
5809 #include "gt-tree.h"