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