c-common.c (c_expand_expr, c_staticp): Remove.
[platform/upstream/gcc.git] / gcc / tree.c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 #include "pointer-set.h"
53 #include "fixed-value.h"
54
55 /* Tree code classes.  */
56
57 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
58 #define END_OF_BASE_TREE_CODES tcc_exceptional,
59
60 const enum tree_code_class tree_code_type[] = {
61 #include "all-tree.def"
62 };
63
64 #undef DEFTREECODE
65 #undef END_OF_BASE_TREE_CODES
66
67 /* Table indexed by tree code giving number of expression
68    operands beyond the fixed part of the node structure.
69    Not used for types or decls.  */
70
71 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
72 #define END_OF_BASE_TREE_CODES 0,
73
74 const unsigned char tree_code_length[] = {
75 #include "all-tree.def"
76 };
77
78 #undef DEFTREECODE
79 #undef END_OF_BASE_TREE_CODES
80
81 /* Names of tree components.
82    Used for printing out the tree and error messages.  */
83 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
84 #define END_OF_BASE_TREE_CODES "@dummy",
85
86 const char *const tree_code_name[] = {
87 #include "all-tree.def"
88 };
89
90 #undef DEFTREECODE
91 #undef END_OF_BASE_TREE_CODES
92
93 /* Each tree code class has an associated string representation.
94    These must correspond to the tree_code_class entries.  */
95
96 const char *const tree_code_class_strings[] =
97 {
98   "exceptional",
99   "constant",
100   "type",
101   "declaration",
102   "reference",
103   "comparison",
104   "unary",
105   "binary",
106   "statement",
107   "vl_exp",
108   "expression"
109 };
110
111 /* obstack.[ch] explicitly declined to prototype this.  */
112 extern int _obstack_allocated_p (struct obstack *h, void *obj);
113
114 #ifdef GATHER_STATISTICS
115 /* Statistics-gathering stuff.  */
116
117 int tree_node_counts[(int) all_kinds];
118 int tree_node_sizes[(int) all_kinds];
119
120 /* Keep in sync with tree.h:enum tree_node_kind.  */
121 static const char * const tree_node_kind_names[] = {
122   "decls",
123   "types",
124   "blocks",
125   "stmts",
126   "refs",
127   "exprs",
128   "constants",
129   "identifiers",
130   "perm_tree_lists",
131   "temp_tree_lists",
132   "vecs",
133   "binfos",
134   "ssa names",
135   "constructors",
136   "random kinds",
137   "lang_decl kinds",
138   "lang_type kinds",
139   "omp clauses",
140 };
141 #endif /* GATHER_STATISTICS */
142
143 /* Unique id for next decl created.  */
144 static GTY(()) int next_decl_uid;
145 /* Unique id for next type created.  */
146 static GTY(()) int next_type_uid = 1;
147
148 /* Since we cannot rehash a type after it is in the table, we have to
149    keep the hash code.  */
150
151 struct type_hash GTY(())
152 {
153   unsigned long hash;
154   tree type;
155 };
156
157 /* Initial size of the hash table (rounded to next prime).  */
158 #define TYPE_HASH_INITIAL_SIZE 1000
159
160 /* Now here is the hash table.  When recording a type, it is added to
161    the slot whose index is the hash code.  Note that the hash table is
162    used for several kinds of types (function types, array types and
163    array index range types, for now).  While all these live in the
164    same table, they are completely independent, and the hash code is
165    computed differently for each of these.  */
166
167 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
168      htab_t type_hash_table;
169
170 /* Hash table and temporary node for larger integer const values.  */
171 static GTY (()) tree int_cst_node;
172 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
173      htab_t int_cst_hash_table;
174
175 /* Hash table for optimization flags and target option flags.  Use the same
176    hash table for both sets of options.  Nodes for building the current
177    optimization and target option nodes.  The assumption is most of the time
178    the options created will already be in the hash table, so we avoid
179    allocating and freeing up a node repeatably.  */
180 static GTY (()) tree cl_optimization_node;
181 static GTY (()) tree cl_target_option_node;
182 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
183      htab_t cl_option_hash_table;
184
185 /* General tree->tree mapping  structure for use in hash tables.  */
186
187
188 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map))) 
189      htab_t debug_expr_for_decl;
190
191 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map))) 
192      htab_t value_expr_for_decl;
193
194 static GTY ((if_marked ("tree_priority_map_marked_p"), 
195              param_is (struct tree_priority_map)))
196   htab_t init_priority_for_decl;
197
198 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
199   htab_t restrict_base_for_decl;
200
201 static void set_type_quals (tree, int);
202 static int type_hash_eq (const void *, const void *);
203 static hashval_t type_hash_hash (const void *);
204 static hashval_t int_cst_hash_hash (const void *);
205 static int int_cst_hash_eq (const void *, const void *);
206 static hashval_t cl_option_hash_hash (const void *);
207 static int cl_option_hash_eq (const void *, const void *);
208 static void print_type_hash_statistics (void);
209 static void print_debug_expr_statistics (void);
210 static void print_value_expr_statistics (void);
211 static int type_hash_marked_p (const void *);
212 static unsigned int type_hash_list (const_tree, hashval_t);
213 static unsigned int attribute_hash_list (const_tree, hashval_t);
214
215 tree global_trees[TI_MAX];
216 tree integer_types[itk_none];
217
218 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
219
220 /* Number of operands for each OpenMP clause.  */
221 unsigned const char omp_clause_num_ops[] =
222 {
223   0, /* OMP_CLAUSE_ERROR  */
224   1, /* OMP_CLAUSE_PRIVATE  */
225   1, /* OMP_CLAUSE_SHARED  */
226   1, /* OMP_CLAUSE_FIRSTPRIVATE  */
227   2, /* OMP_CLAUSE_LASTPRIVATE  */
228   4, /* OMP_CLAUSE_REDUCTION  */
229   1, /* OMP_CLAUSE_COPYIN  */
230   1, /* OMP_CLAUSE_COPYPRIVATE  */
231   1, /* OMP_CLAUSE_IF  */
232   1, /* OMP_CLAUSE_NUM_THREADS  */
233   1, /* OMP_CLAUSE_SCHEDULE  */
234   0, /* OMP_CLAUSE_NOWAIT  */
235   0, /* OMP_CLAUSE_ORDERED  */
236   0, /* OMP_CLAUSE_DEFAULT  */
237   3, /* OMP_CLAUSE_COLLAPSE  */
238   0  /* OMP_CLAUSE_UNTIED   */
239 };
240
241 const char * const omp_clause_code_name[] =
242 {
243   "error_clause",
244   "private",
245   "shared",
246   "firstprivate",
247   "lastprivate",
248   "reduction",
249   "copyin",
250   "copyprivate",
251   "if",
252   "num_threads",
253   "schedule",
254   "nowait",
255   "ordered",
256   "default",
257   "collapse",
258   "untied"
259 };
260 \f
261 /* Init tree.c.  */
262
263 void
264 init_ttree (void)
265 {
266   /* Initialize the hash table of types.  */
267   type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
268                                      type_hash_eq, 0);
269
270   debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
271                                          tree_map_eq, 0);
272
273   value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
274                                          tree_map_eq, 0);
275   init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
276                                             tree_priority_map_eq, 0);
277   restrict_base_for_decl = htab_create_ggc (256, tree_map_hash,
278                                             tree_map_eq, 0);
279
280   int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
281                                         int_cst_hash_eq, NULL);
282   
283   int_cst_node = make_node (INTEGER_CST);
284
285   cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
286                                           cl_option_hash_eq, NULL);
287
288   cl_optimization_node = make_node (OPTIMIZATION_NODE);
289   cl_target_option_node = make_node (TARGET_OPTION_NODE);
290
291   tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
292   tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
293   tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
294   
295
296   tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
297   tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
298   tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
299   tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
300   tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
301   tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
302   tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
303   tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
304   tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
305
306
307   tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
308   tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
309   tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
310   tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
311   tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
312   tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1; 
313
314   tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
315   tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
316   tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
317   tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
318   tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
319   tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
320   tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
321   tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
322   tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
323   tree_contains_struct[NAME_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
324   tree_contains_struct[SYMBOL_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
325   tree_contains_struct[MEMORY_PARTITION_TAG][TS_DECL_MINIMAL] = 1;
326
327   tree_contains_struct[NAME_MEMORY_TAG][TS_MEMORY_TAG] = 1;
328   tree_contains_struct[SYMBOL_MEMORY_TAG][TS_MEMORY_TAG] = 1;
329   tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_TAG] = 1;
330
331   tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_PARTITION_TAG] = 1;
332
333   tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
334   tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
335   tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
336   tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
337   
338   tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
339   tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
340   tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
341   tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
342   tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
343   tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
344   tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
345   tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
346   tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL] = 1;
347   tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON] = 1;
348
349   lang_hooks.init_ts ();
350 }
351
352 \f
353 /* The name of the object as the assembler will see it (but before any
354    translations made by ASM_OUTPUT_LABELREF).  Often this is the same
355    as DECL_NAME.  It is an IDENTIFIER_NODE.  */
356 tree
357 decl_assembler_name (tree decl)
358 {
359   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
360     lang_hooks.set_decl_assembler_name (decl);
361   return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
362 }
363
364 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL.  */
365
366 bool
367 decl_assembler_name_equal (tree decl, const_tree asmname)
368 {
369   tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
370   const char *decl_str;
371   const char *asmname_str;
372   bool test = false;
373
374   if (decl_asmname == asmname)
375     return true;
376
377   decl_str = IDENTIFIER_POINTER (decl_asmname);
378   asmname_str = IDENTIFIER_POINTER (asmname);
379   
380
381   /* If the target assembler name was set by the user, things are trickier.
382      We have a leading '*' to begin with.  After that, it's arguable what
383      is the correct thing to do with -fleading-underscore.  Arguably, we've
384      historically been doing the wrong thing in assemble_alias by always
385      printing the leading underscore.  Since we're not changing that, make
386      sure user_label_prefix follows the '*' before matching.  */
387   if (decl_str[0] == '*')
388     {
389       size_t ulp_len = strlen (user_label_prefix);
390
391       decl_str ++;
392
393       if (ulp_len == 0)
394         test = true;
395       else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
396         decl_str += ulp_len, test=true;
397       else
398         decl_str --;
399     }
400   if (asmname_str[0] == '*')
401     {
402       size_t ulp_len = strlen (user_label_prefix);
403
404       asmname_str ++;
405
406       if (ulp_len == 0)
407         test = true;
408       else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
409         asmname_str += ulp_len, test=true;
410       else
411         asmname_str --;
412     }
413
414   if (!test)
415     return false;
416   return strcmp (decl_str, asmname_str) == 0;
417 }
418
419 /* Hash asmnames ignoring the user specified marks.  */
420
421 hashval_t
422 decl_assembler_name_hash (const_tree asmname)
423 {
424   if (IDENTIFIER_POINTER (asmname)[0] == '*')
425     {
426       const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
427       size_t ulp_len = strlen (user_label_prefix);
428
429       if (ulp_len == 0)
430         ;
431       else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
432         decl_str += ulp_len;
433
434       return htab_hash_string (decl_str);
435     }
436
437   return htab_hash_string (IDENTIFIER_POINTER (asmname));
438 }
439
440 /* Compute the number of bytes occupied by a tree with code CODE.
441    This function cannot be used for nodes that have variable sizes,
442    including TREE_VEC, STRING_CST, and CALL_EXPR.  */
443 size_t
444 tree_code_size (enum tree_code code)
445 {
446   switch (TREE_CODE_CLASS (code))
447     {
448     case tcc_declaration:  /* A decl node */
449       {
450         switch (code)
451           {
452           case FIELD_DECL:
453             return sizeof (struct tree_field_decl);
454           case PARM_DECL:
455             return sizeof (struct tree_parm_decl);
456           case VAR_DECL:
457             return sizeof (struct tree_var_decl);
458           case LABEL_DECL:
459             return sizeof (struct tree_label_decl);
460           case RESULT_DECL:
461             return sizeof (struct tree_result_decl);
462           case CONST_DECL:
463             return sizeof (struct tree_const_decl);
464           case TYPE_DECL:
465             return sizeof (struct tree_type_decl);
466           case FUNCTION_DECL:
467             return sizeof (struct tree_function_decl);
468           case NAME_MEMORY_TAG:
469           case SYMBOL_MEMORY_TAG:
470             return sizeof (struct tree_memory_tag);
471           case MEMORY_PARTITION_TAG:
472             return sizeof (struct tree_memory_partition_tag);
473           default:
474             return sizeof (struct tree_decl_non_common);
475           }
476       }
477
478     case tcc_type:  /* a type node */
479       return sizeof (struct tree_type);
480
481     case tcc_reference:   /* a reference */
482     case tcc_expression:  /* an expression */
483     case tcc_statement:   /* an expression with side effects */
484     case tcc_comparison:  /* a comparison expression */
485     case tcc_unary:       /* a unary arithmetic expression */
486     case tcc_binary:      /* a binary arithmetic expression */
487       return (sizeof (struct tree_exp)
488               + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
489
490     case tcc_constant:  /* a constant */
491       switch (code)
492         {
493         case INTEGER_CST:       return sizeof (struct tree_int_cst);
494         case REAL_CST:          return sizeof (struct tree_real_cst);
495         case FIXED_CST:         return sizeof (struct tree_fixed_cst);
496         case COMPLEX_CST:       return sizeof (struct tree_complex);
497         case VECTOR_CST:        return sizeof (struct tree_vector);
498         case STRING_CST:        gcc_unreachable ();
499         default:
500           return lang_hooks.tree_size (code);
501         }
502
503     case tcc_exceptional:  /* something random, like an identifier.  */
504       switch (code)
505         {
506         case IDENTIFIER_NODE:   return lang_hooks.identifier_size;
507         case TREE_LIST:         return sizeof (struct tree_list);
508
509         case ERROR_MARK:
510         case PLACEHOLDER_EXPR:  return sizeof (struct tree_common);
511
512         case TREE_VEC:
513         case OMP_CLAUSE:        gcc_unreachable ();
514
515         case SSA_NAME:          return sizeof (struct tree_ssa_name);
516
517         case STATEMENT_LIST:    return sizeof (struct tree_statement_list);
518         case BLOCK:             return sizeof (struct tree_block);
519         case CONSTRUCTOR:       return sizeof (struct tree_constructor);
520         case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
521         case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
522
523         default:
524           return lang_hooks.tree_size (code);
525         }
526
527     default:
528       gcc_unreachable ();
529     }
530 }
531
532 /* Compute the number of bytes occupied by NODE.  This routine only
533    looks at TREE_CODE, except for those nodes that have variable sizes.  */
534 size_t
535 tree_size (const_tree node)
536 {
537   const enum tree_code code = TREE_CODE (node);
538   switch (code)
539     {
540     case TREE_BINFO:
541       return (offsetof (struct tree_binfo, base_binfos)
542               + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
543
544     case TREE_VEC:
545       return (sizeof (struct tree_vec)
546               + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
547
548     case STRING_CST:
549       return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
550
551     case OMP_CLAUSE:
552       return (sizeof (struct tree_omp_clause)
553               + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
554                 * sizeof (tree));
555
556     default:
557       if (TREE_CODE_CLASS (code) == tcc_vl_exp)
558         return (sizeof (struct tree_exp)
559                 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
560       else
561         return tree_code_size (code);
562     }
563 }
564
565 /* Return a newly allocated node of code CODE.  For decl and type
566    nodes, some other fields are initialized.  The rest of the node is
567    initialized to zero.  This function cannot be used for TREE_VEC or
568    OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
569
570    Achoo!  I got a code in the node.  */
571
572 tree
573 make_node_stat (enum tree_code code MEM_STAT_DECL)
574 {
575   tree t;
576   enum tree_code_class type = TREE_CODE_CLASS (code);
577   size_t length = tree_code_size (code);
578 #ifdef GATHER_STATISTICS
579   tree_node_kind kind;
580
581   switch (type)
582     {
583     case tcc_declaration:  /* A decl node */
584       kind = d_kind;
585       break;
586
587     case tcc_type:  /* a type node */
588       kind = t_kind;
589       break;
590
591     case tcc_statement:  /* an expression with side effects */
592       kind = s_kind;
593       break;
594
595     case tcc_reference:  /* a reference */
596       kind = r_kind;
597       break;
598
599     case tcc_expression:  /* an expression */
600     case tcc_comparison:  /* a comparison expression */
601     case tcc_unary:  /* a unary arithmetic expression */
602     case tcc_binary:  /* a binary arithmetic expression */
603       kind = e_kind;
604       break;
605
606     case tcc_constant:  /* a constant */
607       kind = c_kind;
608       break;
609
610     case tcc_exceptional:  /* something random, like an identifier.  */
611       switch (code)
612         {
613         case IDENTIFIER_NODE:
614           kind = id_kind;
615           break;
616
617         case TREE_VEC:
618           kind = vec_kind;
619           break;
620
621         case TREE_BINFO:
622           kind = binfo_kind;
623           break;
624
625         case SSA_NAME:
626           kind = ssa_name_kind;
627           break;
628
629         case BLOCK:
630           kind = b_kind;
631           break;
632
633         case CONSTRUCTOR:
634           kind = constr_kind;
635           break;
636
637         default:
638           kind = x_kind;
639           break;
640         }
641       break;
642       
643     default:
644       gcc_unreachable ();
645     }
646
647   tree_node_counts[(int) kind]++;
648   tree_node_sizes[(int) kind] += length;
649 #endif
650
651   if (code == IDENTIFIER_NODE)
652     t = (tree) ggc_alloc_zone_pass_stat (length, &tree_id_zone);
653   else
654     t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
655
656   memset (t, 0, length);
657
658   TREE_SET_CODE (t, code);
659
660   switch (type)
661     {
662     case tcc_statement:
663       TREE_SIDE_EFFECTS (t) = 1;
664       break;
665
666     case tcc_declaration:
667       if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
668         {
669           if (code == FUNCTION_DECL)
670             {
671               DECL_ALIGN (t) = FUNCTION_BOUNDARY;
672               DECL_MODE (t) = FUNCTION_MODE;
673             }
674           else
675             DECL_ALIGN (t) = 1;
676           /* We have not yet computed the alias set for this declaration.  */
677           DECL_POINTER_ALIAS_SET (t) = -1;
678         }
679       DECL_SOURCE_LOCATION (t) = input_location;
680       DECL_UID (t) = next_decl_uid++;
681
682       break;
683
684     case tcc_type:
685       TYPE_UID (t) = next_type_uid++;
686       TYPE_ALIGN (t) = BITS_PER_UNIT;
687       TYPE_USER_ALIGN (t) = 0;
688       TYPE_MAIN_VARIANT (t) = t;
689       TYPE_CANONICAL (t) = t;
690
691       /* Default to no attributes for type, but let target change that.  */
692       TYPE_ATTRIBUTES (t) = NULL_TREE;
693       targetm.set_default_type_attributes (t);
694
695       /* We have not yet computed the alias set for this type.  */
696       TYPE_ALIAS_SET (t) = -1;
697       break;
698
699     case tcc_constant:
700       TREE_CONSTANT (t) = 1;
701       break;
702
703     case tcc_expression:
704       switch (code)
705         {
706         case INIT_EXPR:
707         case MODIFY_EXPR:
708         case VA_ARG_EXPR:
709         case PREDECREMENT_EXPR:
710         case PREINCREMENT_EXPR:
711         case POSTDECREMENT_EXPR:
712         case POSTINCREMENT_EXPR:
713           /* All of these have side-effects, no matter what their
714              operands are.  */
715           TREE_SIDE_EFFECTS (t) = 1;
716           break;
717
718         default:
719           break;
720         }
721       break;
722
723     default:
724       /* Other classes need no special treatment.  */
725       break;
726     }
727
728   return t;
729 }
730 \f
731 /* Return a new node with the same contents as NODE except that its
732    TREE_CHAIN is zero and it has a fresh uid.  */
733
734 tree
735 copy_node_stat (tree node MEM_STAT_DECL)
736 {
737   tree t;
738   enum tree_code code = TREE_CODE (node);
739   size_t length;
740
741   gcc_assert (code != STATEMENT_LIST);
742
743   length = tree_size (node);
744   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
745   memcpy (t, node, length);
746
747   TREE_CHAIN (t) = 0;
748   TREE_ASM_WRITTEN (t) = 0;
749   TREE_VISITED (t) = 0;
750   t->base.ann = 0;
751
752   if (TREE_CODE_CLASS (code) == tcc_declaration)
753     {
754       DECL_UID (t) = next_decl_uid++;
755       if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
756           && DECL_HAS_VALUE_EXPR_P (node))
757         {
758           SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
759           DECL_HAS_VALUE_EXPR_P (t) = 1;
760         }
761       if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
762         {
763           SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
764           DECL_HAS_INIT_PRIORITY_P (t) = 1;
765         }
766       if (TREE_CODE (node) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (node))
767         {
768           SET_DECL_RESTRICT_BASE (t, DECL_GET_RESTRICT_BASE (node));
769           DECL_BASED_ON_RESTRICT_P (t) = 1;
770         }
771     }
772   else if (TREE_CODE_CLASS (code) == tcc_type)
773     {
774       TYPE_UID (t) = next_type_uid++;
775       /* The following is so that the debug code for
776          the copy is different from the original type.
777          The two statements usually duplicate each other
778          (because they clear fields of the same union),
779          but the optimizer should catch that.  */
780       TYPE_SYMTAB_POINTER (t) = 0;
781       TYPE_SYMTAB_ADDRESS (t) = 0;
782       
783       /* Do not copy the values cache.  */
784       if (TYPE_CACHED_VALUES_P(t))
785         {
786           TYPE_CACHED_VALUES_P (t) = 0;
787           TYPE_CACHED_VALUES (t) = NULL_TREE;
788         }
789     }
790
791   return t;
792 }
793
794 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
795    For example, this can copy a list made of TREE_LIST nodes.  */
796
797 tree
798 copy_list (tree list)
799 {
800   tree head;
801   tree prev, next;
802
803   if (list == 0)
804     return 0;
805
806   head = prev = copy_node (list);
807   next = TREE_CHAIN (list);
808   while (next)
809     {
810       TREE_CHAIN (prev) = copy_node (next);
811       prev = TREE_CHAIN (prev);
812       next = TREE_CHAIN (next);
813     }
814   return head;
815 }
816
817 \f
818 /* Create an INT_CST node with a LOW value sign extended.  */
819
820 tree
821 build_int_cst (tree type, HOST_WIDE_INT low)
822 {
823   /* Support legacy code.  */
824   if (!type)
825     type = integer_type_node;
826
827   return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
828 }
829
830 /* Create an INT_CST node with a LOW value zero extended.  */
831
832 tree
833 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
834 {
835   return build_int_cst_wide (type, low, 0);
836 }
837
838 /* Create an INT_CST node with a LOW value in TYPE.  The value is sign extended
839    if it is negative.  This function is similar to build_int_cst, but
840    the extra bits outside of the type precision are cleared.  Constants
841    with these extra bits may confuse the fold so that it detects overflows
842    even in cases when they do not occur, and in general should be avoided.
843    We cannot however make this a default behavior of build_int_cst without
844    more intrusive changes, since there are parts of gcc that rely on the extra
845    precision of the integer constants.  */
846
847 tree
848 build_int_cst_type (tree type, HOST_WIDE_INT low)
849 {
850   unsigned HOST_WIDE_INT low1;
851   HOST_WIDE_INT hi;
852
853   gcc_assert (type);
854
855   fit_double_type (low, low < 0 ? -1 : 0, &low1, &hi, type);
856
857   return build_int_cst_wide (type, low1, hi);
858 }
859
860 /* Create an INT_CST node of TYPE and value HI:LOW.  The value is truncated
861    and sign extended according to the value range of TYPE.  */
862
863 tree
864 build_int_cst_wide_type (tree type,
865                          unsigned HOST_WIDE_INT low, HOST_WIDE_INT high)
866 {
867   fit_double_type (low, high, &low, &high, type);
868   return build_int_cst_wide (type, low, high);
869 }
870
871 /* These are the hash table functions for the hash table of INTEGER_CST
872    nodes of a sizetype.  */
873
874 /* Return the hash code code X, an INTEGER_CST.  */
875
876 static hashval_t
877 int_cst_hash_hash (const void *x)
878 {
879   const_tree const t = (const_tree) x;
880
881   return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
882           ^ htab_hash_pointer (TREE_TYPE (t)));
883 }
884
885 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
886    is the same as that given by *Y, which is the same.  */
887
888 static int
889 int_cst_hash_eq (const void *x, const void *y)
890 {
891   const_tree const xt = (const_tree) x;
892   const_tree const yt = (const_tree) y;
893
894   return (TREE_TYPE (xt) == TREE_TYPE (yt)
895           && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
896           && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
897 }
898
899 /* Create an INT_CST node of TYPE and value HI:LOW.
900    The returned node is always shared.  For small integers we use a
901    per-type vector cache, for larger ones we use a single hash table.  */
902
903 tree
904 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
905 {
906   tree t;
907   int ix = -1;
908   int limit = 0;
909
910   gcc_assert (type);
911
912   switch (TREE_CODE (type))
913     {
914     case POINTER_TYPE:
915     case REFERENCE_TYPE:
916       /* Cache NULL pointer.  */
917       if (!hi && !low)
918         {
919           limit = 1;
920           ix = 0;
921         }
922       break;
923
924     case BOOLEAN_TYPE:
925       /* Cache false or true.  */
926       limit = 2;
927       if (!hi && low < 2)
928         ix = low;
929       break;
930
931     case INTEGER_TYPE:
932     case OFFSET_TYPE:
933       if (TYPE_UNSIGNED (type))
934         {
935           /* Cache 0..N */
936           limit = INTEGER_SHARE_LIMIT;
937           if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
938             ix = low;
939         }
940       else
941         {
942           /* Cache -1..N */
943           limit = INTEGER_SHARE_LIMIT + 1;
944           if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
945             ix = low + 1;
946           else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
947             ix = 0;
948         }
949       break;
950
951     case ENUMERAL_TYPE:
952       break;
953
954     default:
955       gcc_unreachable ();
956     }
957
958   if (ix >= 0)
959     {
960       /* Look for it in the type's vector of small shared ints.  */
961       if (!TYPE_CACHED_VALUES_P (type))
962         {
963           TYPE_CACHED_VALUES_P (type) = 1;
964           TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
965         }
966
967       t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
968       if (t)
969         {
970           /* Make sure no one is clobbering the shared constant.  */
971           gcc_assert (TREE_TYPE (t) == type);
972           gcc_assert (TREE_INT_CST_LOW (t) == low);
973           gcc_assert (TREE_INT_CST_HIGH (t) == hi);
974         }
975       else
976         {
977           /* Create a new shared int.  */
978           t = make_node (INTEGER_CST);
979
980           TREE_INT_CST_LOW (t) = low;
981           TREE_INT_CST_HIGH (t) = hi;
982           TREE_TYPE (t) = type;
983           
984           TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
985         }
986     }
987   else
988     {
989       /* Use the cache of larger shared ints.  */
990       void **slot;
991
992       TREE_INT_CST_LOW (int_cst_node) = low;
993       TREE_INT_CST_HIGH (int_cst_node) = hi;
994       TREE_TYPE (int_cst_node) = type;
995
996       slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
997       t = (tree) *slot;
998       if (!t)
999         {
1000           /* Insert this one into the hash table.  */
1001           t = int_cst_node;
1002           *slot = t;
1003           /* Make a new node for next time round.  */
1004           int_cst_node = make_node (INTEGER_CST);
1005         }
1006     }
1007
1008   return t;
1009 }
1010
1011 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1012    and the rest are zeros.  */
1013
1014 tree
1015 build_low_bits_mask (tree type, unsigned bits)
1016 {
1017   unsigned HOST_WIDE_INT low;
1018   HOST_WIDE_INT high;
1019   unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
1020
1021   gcc_assert (bits <= TYPE_PRECISION (type));
1022
1023   if (bits == TYPE_PRECISION (type)
1024       && !TYPE_UNSIGNED (type))
1025     {
1026       /* Sign extended all-ones mask.  */
1027       low = all_ones;
1028       high = -1;
1029     }
1030   else if (bits <= HOST_BITS_PER_WIDE_INT)
1031     {
1032       low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1033       high = 0;
1034     }
1035   else
1036     {
1037       bits -= HOST_BITS_PER_WIDE_INT;
1038       low = all_ones;
1039       high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1040     }
1041
1042   return build_int_cst_wide (type, low, high);
1043 }
1044
1045 /* Checks that X is integer constant that can be expressed in (unsigned)
1046    HOST_WIDE_INT without loss of precision.  */
1047
1048 bool
1049 cst_and_fits_in_hwi (const_tree x)
1050 {
1051   if (TREE_CODE (x) != INTEGER_CST)
1052     return false;
1053
1054   if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1055     return false;
1056
1057   return (TREE_INT_CST_HIGH (x) == 0
1058           || TREE_INT_CST_HIGH (x) == -1);
1059 }
1060
1061 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1062    are in a list pointed to by VALS.  */
1063
1064 tree
1065 build_vector (tree type, tree vals)
1066 {
1067   tree v = make_node (VECTOR_CST);
1068   int over = 0;
1069   tree link;
1070
1071   TREE_VECTOR_CST_ELTS (v) = vals;
1072   TREE_TYPE (v) = type;
1073
1074   /* Iterate through elements and check for overflow.  */
1075   for (link = vals; link; link = TREE_CHAIN (link))
1076     {
1077       tree value = TREE_VALUE (link);
1078
1079       /* Don't crash if we get an address constant.  */
1080       if (!CONSTANT_CLASS_P (value))
1081         continue;
1082
1083       over |= TREE_OVERFLOW (value);
1084     }
1085
1086   TREE_OVERFLOW (v) = over;
1087   return v;
1088 }
1089
1090 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1091    are extracted from V, a vector of CONSTRUCTOR_ELT.  */
1092
1093 tree
1094 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1095 {
1096   tree list = NULL_TREE;
1097   unsigned HOST_WIDE_INT idx;
1098   tree value;
1099
1100   FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1101     list = tree_cons (NULL_TREE, value, list);
1102   return build_vector (type, nreverse (list));
1103 }
1104
1105 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1106    are in the VEC pointed to by VALS.  */
1107 tree
1108 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1109 {
1110   tree c = make_node (CONSTRUCTOR);
1111   TREE_TYPE (c) = type;
1112   CONSTRUCTOR_ELTS (c) = vals;
1113   return c;
1114 }
1115
1116 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1117    INDEX and VALUE.  */
1118 tree
1119 build_constructor_single (tree type, tree index, tree value)
1120 {
1121   VEC(constructor_elt,gc) *v;
1122   constructor_elt *elt;
1123   tree t;
1124
1125   v = VEC_alloc (constructor_elt, gc, 1);
1126   elt = VEC_quick_push (constructor_elt, v, NULL);
1127   elt->index = index;
1128   elt->value = value;
1129
1130   t = build_constructor (type, v);
1131   TREE_CONSTANT (t) = TREE_CONSTANT (value);
1132   return t;
1133 }
1134
1135
1136 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1137    are in a list pointed to by VALS.  */
1138 tree
1139 build_constructor_from_list (tree type, tree vals)
1140 {
1141   tree t, val;
1142   VEC(constructor_elt,gc) *v = NULL;
1143   bool constant_p = true;
1144
1145   if (vals)
1146     {
1147       v = VEC_alloc (constructor_elt, gc, list_length (vals));
1148       for (t = vals; t; t = TREE_CHAIN (t))
1149         {
1150           constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
1151           val = TREE_VALUE (t);
1152           elt->index = TREE_PURPOSE (t);
1153           elt->value = val;
1154           if (!TREE_CONSTANT (val))
1155             constant_p = false;
1156         }
1157     }
1158
1159   t = build_constructor (type, v);
1160   TREE_CONSTANT (t) = constant_p;
1161   return t;
1162 }
1163
1164 /* Return a new FIXED_CST node whose type is TYPE and value is F.  */
1165
1166 tree
1167 build_fixed (tree type, FIXED_VALUE_TYPE f)
1168 {
1169   tree v;
1170   FIXED_VALUE_TYPE *fp;
1171
1172   v = make_node (FIXED_CST);
1173   fp = GGC_NEW (FIXED_VALUE_TYPE);
1174   memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1175
1176   TREE_TYPE (v) = type;
1177   TREE_FIXED_CST_PTR (v) = fp;
1178   return v;
1179 }
1180
1181 /* Return a new REAL_CST node whose type is TYPE and value is D.  */
1182
1183 tree
1184 build_real (tree type, REAL_VALUE_TYPE d)
1185 {
1186   tree v;
1187   REAL_VALUE_TYPE *dp;
1188   int overflow = 0;
1189
1190   /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1191      Consider doing it via real_convert now.  */
1192
1193   v = make_node (REAL_CST);
1194   dp = GGC_NEW (REAL_VALUE_TYPE);
1195   memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1196
1197   TREE_TYPE (v) = type;
1198   TREE_REAL_CST_PTR (v) = dp;
1199   TREE_OVERFLOW (v) = overflow;
1200   return v;
1201 }
1202
1203 /* Return a new REAL_CST node whose type is TYPE
1204    and whose value is the integer value of the INTEGER_CST node I.  */
1205
1206 REAL_VALUE_TYPE
1207 real_value_from_int_cst (const_tree type, const_tree i)
1208 {
1209   REAL_VALUE_TYPE d;
1210
1211   /* Clear all bits of the real value type so that we can later do
1212      bitwise comparisons to see if two values are the same.  */
1213   memset (&d, 0, sizeof d);
1214
1215   real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1216                      TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1217                      TYPE_UNSIGNED (TREE_TYPE (i)));
1218   return d;
1219 }
1220
1221 /* Given a tree representing an integer constant I, return a tree
1222    representing the same value as a floating-point constant of type TYPE.  */
1223
1224 tree
1225 build_real_from_int_cst (tree type, const_tree i)
1226 {
1227   tree v;
1228   int overflow = TREE_OVERFLOW (i);
1229
1230   v = build_real (type, real_value_from_int_cst (type, i));
1231
1232   TREE_OVERFLOW (v) |= overflow;
1233   return v;
1234 }
1235
1236 /* Return a newly constructed STRING_CST node whose value is
1237    the LEN characters at STR.
1238    The TREE_TYPE is not initialized.  */
1239
1240 tree
1241 build_string (int len, const char *str)
1242 {
1243   tree s;
1244   size_t length;
1245
1246   /* Do not waste bytes provided by padding of struct tree_string.  */
1247   length = len + offsetof (struct tree_string, str) + 1;
1248
1249 #ifdef GATHER_STATISTICS
1250   tree_node_counts[(int) c_kind]++;
1251   tree_node_sizes[(int) c_kind] += length;
1252 #endif  
1253
1254   s = ggc_alloc_tree (length);
1255
1256   memset (s, 0, sizeof (struct tree_common));
1257   TREE_SET_CODE (s, STRING_CST);
1258   TREE_CONSTANT (s) = 1;
1259   TREE_STRING_LENGTH (s) = len;
1260   memcpy (s->string.str, str, len);
1261   s->string.str[len] = '\0';
1262
1263   return s;
1264 }
1265
1266 /* Return a newly constructed COMPLEX_CST node whose value is
1267    specified by the real and imaginary parts REAL and IMAG.
1268    Both REAL and IMAG should be constant nodes.  TYPE, if specified,
1269    will be the type of the COMPLEX_CST; otherwise a new type will be made.  */
1270
1271 tree
1272 build_complex (tree type, tree real, tree imag)
1273 {
1274   tree t = make_node (COMPLEX_CST);
1275
1276   TREE_REALPART (t) = real;
1277   TREE_IMAGPART (t) = imag;
1278   TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1279   TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1280   return t;
1281 }
1282
1283 /* Return a constant of arithmetic type TYPE which is the
1284    multiplicative identity of the set TYPE.  */
1285
1286 tree
1287 build_one_cst (tree type)
1288 {
1289   switch (TREE_CODE (type))
1290     {
1291     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1292     case POINTER_TYPE: case REFERENCE_TYPE:
1293     case OFFSET_TYPE:
1294       return build_int_cst (type, 1);
1295
1296     case REAL_TYPE:
1297       return build_real (type, dconst1);
1298
1299     case FIXED_POINT_TYPE:
1300       /* We can only generate 1 for accum types.  */
1301       gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1302       return build_fixed (type, FCONST1(TYPE_MODE (type)));
1303
1304     case VECTOR_TYPE:
1305       {
1306         tree scalar, cst;
1307         int i;
1308
1309         scalar = build_one_cst (TREE_TYPE (type));
1310
1311         /* Create 'vect_cst_ = {cst,cst,...,cst}'  */
1312         cst = NULL_TREE;
1313         for (i = TYPE_VECTOR_SUBPARTS (type); --i >= 0; )
1314           cst = tree_cons (NULL_TREE, scalar, cst);
1315
1316         return build_vector (type, cst);
1317       }
1318
1319     case COMPLEX_TYPE:
1320       return build_complex (type,
1321                             build_one_cst (TREE_TYPE (type)),
1322                             fold_convert (TREE_TYPE (type), integer_zero_node));
1323
1324     default:
1325       gcc_unreachable ();
1326     }
1327 }
1328
1329 /* Build a BINFO with LEN language slots.  */
1330
1331 tree
1332 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1333 {
1334   tree t;
1335   size_t length = (offsetof (struct tree_binfo, base_binfos)
1336                    + VEC_embedded_size (tree, base_binfos));
1337
1338 #ifdef GATHER_STATISTICS
1339   tree_node_counts[(int) binfo_kind]++;
1340   tree_node_sizes[(int) binfo_kind] += length;
1341 #endif
1342
1343   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1344
1345   memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1346
1347   TREE_SET_CODE (t, TREE_BINFO);
1348
1349   VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1350
1351   return t;
1352 }
1353
1354
1355 /* Build a newly constructed TREE_VEC node of length LEN.  */
1356
1357 tree
1358 make_tree_vec_stat (int len MEM_STAT_DECL)
1359 {
1360   tree t;
1361   int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1362
1363 #ifdef GATHER_STATISTICS
1364   tree_node_counts[(int) vec_kind]++;
1365   tree_node_sizes[(int) vec_kind] += length;
1366 #endif
1367
1368   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1369
1370   memset (t, 0, length);
1371
1372   TREE_SET_CODE (t, TREE_VEC);
1373   TREE_VEC_LENGTH (t) = len;
1374
1375   return t;
1376 }
1377 \f
1378 /* Return 1 if EXPR is the integer constant zero or a complex constant
1379    of zero.  */
1380
1381 int
1382 integer_zerop (const_tree expr)
1383 {
1384   STRIP_NOPS (expr);
1385
1386   return ((TREE_CODE (expr) == INTEGER_CST
1387            && TREE_INT_CST_LOW (expr) == 0
1388            && TREE_INT_CST_HIGH (expr) == 0)
1389           || (TREE_CODE (expr) == COMPLEX_CST
1390               && integer_zerop (TREE_REALPART (expr))
1391               && integer_zerop (TREE_IMAGPART (expr))));
1392 }
1393
1394 /* Return 1 if EXPR is the integer constant one or the corresponding
1395    complex constant.  */
1396
1397 int
1398 integer_onep (const_tree expr)
1399 {
1400   STRIP_NOPS (expr);
1401
1402   return ((TREE_CODE (expr) == INTEGER_CST
1403            && TREE_INT_CST_LOW (expr) == 1
1404            && TREE_INT_CST_HIGH (expr) == 0)
1405           || (TREE_CODE (expr) == COMPLEX_CST
1406               && integer_onep (TREE_REALPART (expr))
1407               && integer_zerop (TREE_IMAGPART (expr))));
1408 }
1409
1410 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1411    it contains.  Likewise for the corresponding complex constant.  */
1412
1413 int
1414 integer_all_onesp (const_tree expr)
1415 {
1416   int prec;
1417   int uns;
1418
1419   STRIP_NOPS (expr);
1420
1421   if (TREE_CODE (expr) == COMPLEX_CST
1422       && integer_all_onesp (TREE_REALPART (expr))
1423       && integer_zerop (TREE_IMAGPART (expr)))
1424     return 1;
1425
1426   else if (TREE_CODE (expr) != INTEGER_CST)
1427     return 0;
1428
1429   uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1430   if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1431       && TREE_INT_CST_HIGH (expr) == -1)
1432     return 1;
1433   if (!uns)
1434     return 0;
1435
1436   /* Note that using TYPE_PRECISION here is wrong.  We care about the
1437      actual bits, not the (arbitrary) range of the type.  */
1438   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1439   if (prec >= HOST_BITS_PER_WIDE_INT)
1440     {
1441       HOST_WIDE_INT high_value;
1442       int shift_amount;
1443
1444       shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1445
1446       /* Can not handle precisions greater than twice the host int size.  */
1447       gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1448       if (shift_amount == HOST_BITS_PER_WIDE_INT)
1449         /* Shifting by the host word size is undefined according to the ANSI
1450            standard, so we must handle this as a special case.  */
1451         high_value = -1;
1452       else
1453         high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1454
1455       return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1456               && TREE_INT_CST_HIGH (expr) == high_value);
1457     }
1458   else
1459     return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1460 }
1461
1462 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1463    one bit on).  */
1464
1465 int
1466 integer_pow2p (const_tree expr)
1467 {
1468   int prec;
1469   HOST_WIDE_INT high, low;
1470
1471   STRIP_NOPS (expr);
1472
1473   if (TREE_CODE (expr) == COMPLEX_CST
1474       && integer_pow2p (TREE_REALPART (expr))
1475       && integer_zerop (TREE_IMAGPART (expr)))
1476     return 1;
1477
1478   if (TREE_CODE (expr) != INTEGER_CST)
1479     return 0;
1480
1481   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1482           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1483   high = TREE_INT_CST_HIGH (expr);
1484   low = TREE_INT_CST_LOW (expr);
1485
1486   /* First clear all bits that are beyond the type's precision in case
1487      we've been sign extended.  */
1488
1489   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1490     ;
1491   else if (prec > HOST_BITS_PER_WIDE_INT)
1492     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1493   else
1494     {
1495       high = 0;
1496       if (prec < HOST_BITS_PER_WIDE_INT)
1497         low &= ~((HOST_WIDE_INT) (-1) << prec);
1498     }
1499
1500   if (high == 0 && low == 0)
1501     return 0;
1502
1503   return ((high == 0 && (low & (low - 1)) == 0)
1504           || (low == 0 && (high & (high - 1)) == 0));
1505 }
1506
1507 /* Return 1 if EXPR is an integer constant other than zero or a
1508    complex constant other than zero.  */
1509
1510 int
1511 integer_nonzerop (const_tree expr)
1512 {
1513   STRIP_NOPS (expr);
1514
1515   return ((TREE_CODE (expr) == INTEGER_CST
1516            && (TREE_INT_CST_LOW (expr) != 0
1517                || TREE_INT_CST_HIGH (expr) != 0))
1518           || (TREE_CODE (expr) == COMPLEX_CST
1519               && (integer_nonzerop (TREE_REALPART (expr))
1520                   || integer_nonzerop (TREE_IMAGPART (expr)))));
1521 }
1522
1523 /* Return 1 if EXPR is the fixed-point constant zero.  */
1524
1525 int
1526 fixed_zerop (const_tree expr)
1527 {
1528   return (TREE_CODE (expr) == FIXED_CST
1529           && double_int_zero_p (TREE_FIXED_CST (expr).data));
1530 }
1531
1532 /* Return the power of two represented by a tree node known to be a
1533    power of two.  */
1534
1535 int
1536 tree_log2 (const_tree expr)
1537 {
1538   int prec;
1539   HOST_WIDE_INT high, low;
1540
1541   STRIP_NOPS (expr);
1542
1543   if (TREE_CODE (expr) == COMPLEX_CST)
1544     return tree_log2 (TREE_REALPART (expr));
1545
1546   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1547           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1548
1549   high = TREE_INT_CST_HIGH (expr);
1550   low = TREE_INT_CST_LOW (expr);
1551
1552   /* First clear all bits that are beyond the type's precision in case
1553      we've been sign extended.  */
1554
1555   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1556     ;
1557   else if (prec > HOST_BITS_PER_WIDE_INT)
1558     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1559   else
1560     {
1561       high = 0;
1562       if (prec < HOST_BITS_PER_WIDE_INT)
1563         low &= ~((HOST_WIDE_INT) (-1) << prec);
1564     }
1565
1566   return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1567           : exact_log2 (low));
1568 }
1569
1570 /* Similar, but return the largest integer Y such that 2 ** Y is less
1571    than or equal to EXPR.  */
1572
1573 int
1574 tree_floor_log2 (const_tree expr)
1575 {
1576   int prec;
1577   HOST_WIDE_INT high, low;
1578
1579   STRIP_NOPS (expr);
1580
1581   if (TREE_CODE (expr) == COMPLEX_CST)
1582     return tree_log2 (TREE_REALPART (expr));
1583
1584   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1585           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1586
1587   high = TREE_INT_CST_HIGH (expr);
1588   low = TREE_INT_CST_LOW (expr);
1589
1590   /* First clear all bits that are beyond the type's precision in case
1591      we've been sign extended.  Ignore if type's precision hasn't been set
1592      since what we are doing is setting it.  */
1593
1594   if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1595     ;
1596   else if (prec > HOST_BITS_PER_WIDE_INT)
1597     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1598   else
1599     {
1600       high = 0;
1601       if (prec < HOST_BITS_PER_WIDE_INT)
1602         low &= ~((HOST_WIDE_INT) (-1) << prec);
1603     }
1604
1605   return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1606           : floor_log2 (low));
1607 }
1608
1609 /* Return 1 if EXPR is the real constant zero.  */
1610
1611 int
1612 real_zerop (const_tree expr)
1613 {
1614   STRIP_NOPS (expr);
1615
1616   return ((TREE_CODE (expr) == REAL_CST
1617            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1618           || (TREE_CODE (expr) == COMPLEX_CST
1619               && real_zerop (TREE_REALPART (expr))
1620               && real_zerop (TREE_IMAGPART (expr))));
1621 }
1622
1623 /* Return 1 if EXPR is the real constant one in real or complex form.  */
1624
1625 int
1626 real_onep (const_tree expr)
1627 {
1628   STRIP_NOPS (expr);
1629
1630   return ((TREE_CODE (expr) == REAL_CST
1631            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1632           || (TREE_CODE (expr) == COMPLEX_CST
1633               && real_onep (TREE_REALPART (expr))
1634               && real_zerop (TREE_IMAGPART (expr))));
1635 }
1636
1637 /* Return 1 if EXPR is the real constant two.  */
1638
1639 int
1640 real_twop (const_tree expr)
1641 {
1642   STRIP_NOPS (expr);
1643
1644   return ((TREE_CODE (expr) == REAL_CST
1645            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1646           || (TREE_CODE (expr) == COMPLEX_CST
1647               && real_twop (TREE_REALPART (expr))
1648               && real_zerop (TREE_IMAGPART (expr))));
1649 }
1650
1651 /* Return 1 if EXPR is the real constant minus one.  */
1652
1653 int
1654 real_minus_onep (const_tree expr)
1655 {
1656   STRIP_NOPS (expr);
1657
1658   return ((TREE_CODE (expr) == REAL_CST
1659            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1660           || (TREE_CODE (expr) == COMPLEX_CST
1661               && real_minus_onep (TREE_REALPART (expr))
1662               && real_zerop (TREE_IMAGPART (expr))));
1663 }
1664
1665 /* Nonzero if EXP is a constant or a cast of a constant.  */
1666
1667 int
1668 really_constant_p (const_tree exp)
1669 {
1670   /* This is not quite the same as STRIP_NOPS.  It does more.  */
1671   while (CONVERT_EXPR_P (exp)
1672          || TREE_CODE (exp) == NON_LVALUE_EXPR)
1673     exp = TREE_OPERAND (exp, 0);
1674   return TREE_CONSTANT (exp);
1675 }
1676 \f
1677 /* Return first list element whose TREE_VALUE is ELEM.
1678    Return 0 if ELEM is not in LIST.  */
1679
1680 tree
1681 value_member (tree elem, tree list)
1682 {
1683   while (list)
1684     {
1685       if (elem == TREE_VALUE (list))
1686         return list;
1687       list = TREE_CHAIN (list);
1688     }
1689   return NULL_TREE;
1690 }
1691
1692 /* Return first list element whose TREE_PURPOSE is ELEM.
1693    Return 0 if ELEM is not in LIST.  */
1694
1695 tree
1696 purpose_member (const_tree elem, tree list)
1697 {
1698   while (list)
1699     {
1700       if (elem == TREE_PURPOSE (list))
1701         return list;
1702       list = TREE_CHAIN (list);
1703     }
1704   return NULL_TREE;
1705 }
1706
1707 /* Return nonzero if ELEM is part of the chain CHAIN.  */
1708
1709 int
1710 chain_member (const_tree elem, const_tree chain)
1711 {
1712   while (chain)
1713     {
1714       if (elem == chain)
1715         return 1;
1716       chain = TREE_CHAIN (chain);
1717     }
1718
1719   return 0;
1720 }
1721
1722 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1723    We expect a null pointer to mark the end of the chain.
1724    This is the Lisp primitive `length'.  */
1725
1726 int
1727 list_length (const_tree t)
1728 {
1729   const_tree p = t;
1730 #ifdef ENABLE_TREE_CHECKING
1731   const_tree q = t;
1732 #endif
1733   int len = 0;
1734
1735   while (p)
1736     {
1737       p = TREE_CHAIN (p);
1738 #ifdef ENABLE_TREE_CHECKING
1739       if (len % 2)
1740         q = TREE_CHAIN (q);
1741       gcc_assert (p != q);
1742 #endif
1743       len++;
1744     }
1745
1746   return len;
1747 }
1748
1749 /* Returns the number of FIELD_DECLs in TYPE.  */
1750
1751 int
1752 fields_length (const_tree type)
1753 {
1754   tree t = TYPE_FIELDS (type);
1755   int count = 0;
1756
1757   for (; t; t = TREE_CHAIN (t))
1758     if (TREE_CODE (t) == FIELD_DECL)
1759       ++count;
1760
1761   return count;
1762 }
1763
1764 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1765    by modifying the last node in chain 1 to point to chain 2.
1766    This is the Lisp primitive `nconc'.  */
1767
1768 tree
1769 chainon (tree op1, tree op2)
1770 {
1771   tree t1;
1772
1773   if (!op1)
1774     return op2;
1775   if (!op2)
1776     return op1;
1777
1778   for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1779     continue;
1780   TREE_CHAIN (t1) = op2;
1781
1782 #ifdef ENABLE_TREE_CHECKING
1783   {
1784     tree t2;
1785     for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1786       gcc_assert (t2 != t1);
1787   }
1788 #endif
1789
1790   return op1;
1791 }
1792
1793 /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
1794
1795 tree
1796 tree_last (tree chain)
1797 {
1798   tree next;
1799   if (chain)
1800     while ((next = TREE_CHAIN (chain)))
1801       chain = next;
1802   return chain;
1803 }
1804
1805 /* Reverse the order of elements in the chain T,
1806    and return the new head of the chain (old last element).  */
1807
1808 tree
1809 nreverse (tree t)
1810 {
1811   tree prev = 0, decl, next;
1812   for (decl = t; decl; decl = next)
1813     {
1814       next = TREE_CHAIN (decl);
1815       TREE_CHAIN (decl) = prev;
1816       prev = decl;
1817     }
1818   return prev;
1819 }
1820 \f
1821 /* Return a newly created TREE_LIST node whose
1822    purpose and value fields are PARM and VALUE.  */
1823
1824 tree
1825 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1826 {
1827   tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1828   TREE_PURPOSE (t) = parm;
1829   TREE_VALUE (t) = value;
1830   return t;
1831 }
1832
1833 /* Return a newly created TREE_LIST node whose
1834    purpose and value fields are PURPOSE and VALUE
1835    and whose TREE_CHAIN is CHAIN.  */
1836
1837 tree
1838 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1839 {
1840   tree node;
1841
1842   node = (tree) ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1843
1844   memset (node, 0, sizeof (struct tree_common));
1845
1846 #ifdef GATHER_STATISTICS
1847   tree_node_counts[(int) x_kind]++;
1848   tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1849 #endif
1850
1851   TREE_SET_CODE (node, TREE_LIST);
1852   TREE_CHAIN (node) = chain;
1853   TREE_PURPOSE (node) = purpose;
1854   TREE_VALUE (node) = value;
1855   return node;
1856 }
1857
1858 /* Return the elements of a CONSTRUCTOR as a TREE_LIST.  */
1859
1860 tree
1861 ctor_to_list (tree ctor)
1862 {
1863   tree list = NULL_TREE;
1864   tree *p = &list;
1865   unsigned ix;
1866   tree purpose, val;
1867
1868   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), ix, purpose, val)
1869     {
1870       *p = build_tree_list (purpose, val);
1871       p = &TREE_CHAIN (*p);
1872     }
1873
1874   return list;
1875 }
1876 \f
1877 /* Return the size nominally occupied by an object of type TYPE
1878    when it resides in memory.  The value is measured in units of bytes,
1879    and its data type is that normally used for type sizes
1880    (which is the first type created by make_signed_type or
1881    make_unsigned_type).  */
1882
1883 tree
1884 size_in_bytes (const_tree type)
1885 {
1886   tree t;
1887
1888   if (type == error_mark_node)
1889     return integer_zero_node;
1890
1891   type = TYPE_MAIN_VARIANT (type);
1892   t = TYPE_SIZE_UNIT (type);
1893
1894   if (t == 0)
1895     {
1896       lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1897       return size_zero_node;
1898     }
1899
1900   return t;
1901 }
1902
1903 /* Return the size of TYPE (in bytes) as a wide integer
1904    or return -1 if the size can vary or is larger than an integer.  */
1905
1906 HOST_WIDE_INT
1907 int_size_in_bytes (const_tree type)
1908 {
1909   tree t;
1910
1911   if (type == error_mark_node)
1912     return 0;
1913
1914   type = TYPE_MAIN_VARIANT (type);
1915   t = TYPE_SIZE_UNIT (type);
1916   if (t == 0
1917       || TREE_CODE (t) != INTEGER_CST
1918       || TREE_INT_CST_HIGH (t) != 0
1919       /* If the result would appear negative, it's too big to represent.  */
1920       || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1921     return -1;
1922
1923   return TREE_INT_CST_LOW (t);
1924 }
1925
1926 /* Return the maximum size of TYPE (in bytes) as a wide integer
1927    or return -1 if the size can vary or is larger than an integer.  */
1928
1929 HOST_WIDE_INT
1930 max_int_size_in_bytes (const_tree type)
1931 {
1932   HOST_WIDE_INT size = -1;
1933   tree size_tree;
1934
1935   /* If this is an array type, check for a possible MAX_SIZE attached.  */
1936
1937   if (TREE_CODE (type) == ARRAY_TYPE)
1938     {
1939       size_tree = TYPE_ARRAY_MAX_SIZE (type);
1940
1941       if (size_tree && host_integerp (size_tree, 1))
1942         size = tree_low_cst (size_tree, 1);
1943     }
1944
1945   /* If we still haven't been able to get a size, see if the language
1946      can compute a maximum size.  */
1947
1948   if (size == -1)
1949     {
1950       size_tree = lang_hooks.types.max_size (type);
1951
1952       if (size_tree && host_integerp (size_tree, 1))
1953         size = tree_low_cst (size_tree, 1);
1954     }
1955
1956   return size;
1957 }
1958 \f
1959 /* Return the bit position of FIELD, in bits from the start of the record.
1960    This is a tree of type bitsizetype.  */
1961
1962 tree
1963 bit_position (const_tree field)
1964 {
1965   return bit_from_pos (DECL_FIELD_OFFSET (field),
1966                        DECL_FIELD_BIT_OFFSET (field));
1967 }
1968
1969 /* Likewise, but return as an integer.  It must be representable in
1970    that way (since it could be a signed value, we don't have the
1971    option of returning -1 like int_size_in_byte can.  */
1972
1973 HOST_WIDE_INT
1974 int_bit_position (const_tree field)
1975 {
1976   return tree_low_cst (bit_position (field), 0);
1977 }
1978 \f
1979 /* Return the byte position of FIELD, in bytes from the start of the record.
1980    This is a tree of type sizetype.  */
1981
1982 tree
1983 byte_position (const_tree field)
1984 {
1985   return byte_from_pos (DECL_FIELD_OFFSET (field),
1986                         DECL_FIELD_BIT_OFFSET (field));
1987 }
1988
1989 /* Likewise, but return as an integer.  It must be representable in
1990    that way (since it could be a signed value, we don't have the
1991    option of returning -1 like int_size_in_byte can.  */
1992
1993 HOST_WIDE_INT
1994 int_byte_position (const_tree field)
1995 {
1996   return tree_low_cst (byte_position (field), 0);
1997 }
1998 \f
1999 /* Return the strictest alignment, in bits, that T is known to have.  */
2000
2001 unsigned int
2002 expr_align (const_tree t)
2003 {
2004   unsigned int align0, align1;
2005
2006   switch (TREE_CODE (t))
2007     {
2008     CASE_CONVERT:  case NON_LVALUE_EXPR:
2009       /* If we have conversions, we know that the alignment of the
2010          object must meet each of the alignments of the types.  */
2011       align0 = expr_align (TREE_OPERAND (t, 0));
2012       align1 = TYPE_ALIGN (TREE_TYPE (t));
2013       return MAX (align0, align1);
2014
2015     case SAVE_EXPR:         case COMPOUND_EXPR:       case MODIFY_EXPR:
2016     case INIT_EXPR:         case TARGET_EXPR:         case WITH_CLEANUP_EXPR:
2017     case CLEANUP_POINT_EXPR:
2018       /* These don't change the alignment of an object.  */
2019       return expr_align (TREE_OPERAND (t, 0));
2020
2021     case COND_EXPR:
2022       /* The best we can do is say that the alignment is the least aligned
2023          of the two arms.  */
2024       align0 = expr_align (TREE_OPERAND (t, 1));
2025       align1 = expr_align (TREE_OPERAND (t, 2));
2026       return MIN (align0, align1);
2027
2028       /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2029          meaningfully, it's always 1.  */
2030     case LABEL_DECL:     case CONST_DECL:
2031     case VAR_DECL:       case PARM_DECL:   case RESULT_DECL:
2032     case FUNCTION_DECL:
2033       gcc_assert (DECL_ALIGN (t) != 0);
2034       return DECL_ALIGN (t);
2035
2036     default:
2037       break;
2038     }
2039
2040   /* Otherwise take the alignment from that of the type.  */
2041   return TYPE_ALIGN (TREE_TYPE (t));
2042 }
2043 \f
2044 /* Return, as a tree node, the number of elements for TYPE (which is an
2045    ARRAY_TYPE) minus one. This counts only elements of the top array.  */
2046
2047 tree
2048 array_type_nelts (const_tree type)
2049 {
2050   tree index_type, min, max;
2051
2052   /* If they did it with unspecified bounds, then we should have already
2053      given an error about it before we got here.  */
2054   if (! TYPE_DOMAIN (type))
2055     return error_mark_node;
2056
2057   index_type = TYPE_DOMAIN (type);
2058   min = TYPE_MIN_VALUE (index_type);
2059   max = TYPE_MAX_VALUE (index_type);
2060
2061   return (integer_zerop (min)
2062           ? max
2063           : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2064 }
2065 \f
2066 /* If arg is static -- a reference to an object in static storage -- then
2067    return the object.  This is not the same as the C meaning of `static'.
2068    If arg isn't static, return NULL.  */
2069
2070 tree
2071 staticp (tree arg)
2072 {
2073   switch (TREE_CODE (arg))
2074     {
2075     case FUNCTION_DECL:
2076       /* Nested functions are static, even though taking their address will
2077          involve a trampoline as we unnest the nested function and create
2078          the trampoline on the tree level.  */
2079       return arg;
2080
2081     case VAR_DECL:
2082       return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2083               && ! DECL_THREAD_LOCAL_P (arg)
2084               && ! DECL_DLLIMPORT_P (arg)
2085               ? arg : NULL);
2086
2087     case CONST_DECL:
2088       return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2089               ? arg : NULL);
2090
2091     case CONSTRUCTOR:
2092       return TREE_STATIC (arg) ? arg : NULL;
2093
2094     case LABEL_DECL:
2095     case STRING_CST:
2096       return arg;
2097
2098     case COMPONENT_REF:
2099       /* If the thing being referenced is not a field, then it is
2100          something language specific.  */
2101       gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2102
2103       /* If we are referencing a bitfield, we can't evaluate an
2104          ADDR_EXPR at compile time and so it isn't a constant.  */
2105       if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2106         return NULL;
2107
2108       return staticp (TREE_OPERAND (arg, 0));
2109
2110     case BIT_FIELD_REF:
2111       return NULL;
2112
2113     case MISALIGNED_INDIRECT_REF:
2114     case ALIGN_INDIRECT_REF:
2115     case INDIRECT_REF:
2116       return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2117
2118     case ARRAY_REF:
2119     case ARRAY_RANGE_REF:
2120       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2121           && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2122         return staticp (TREE_OPERAND (arg, 0));
2123       else
2124         return NULL;
2125
2126     case COMPOUND_LITERAL_EXPR:
2127       return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2128
2129     default:
2130       return NULL;
2131     }
2132 }
2133
2134 \f
2135
2136
2137 /* Return whether OP is a DECL whose address is function-invariant.  */
2138
2139 bool
2140 decl_address_invariant_p (const_tree op)
2141 {
2142   /* The conditions below are slightly less strict than the one in
2143      staticp.  */
2144
2145   switch (TREE_CODE (op))
2146     {
2147     case PARM_DECL:
2148     case RESULT_DECL:
2149     case LABEL_DECL:
2150     case FUNCTION_DECL:
2151       return true;
2152
2153     case VAR_DECL:
2154       if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2155            && !DECL_DLLIMPORT_P (op))
2156           || DECL_THREAD_LOCAL_P (op)
2157           || DECL_CONTEXT (op) == current_function_decl
2158           || decl_function_context (op) == current_function_decl)
2159         return true;
2160       break;
2161
2162     case CONST_DECL:
2163       if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2164           || decl_function_context (op) == current_function_decl)
2165         return true;
2166       break;
2167
2168     default:
2169       break;
2170     }
2171
2172   return false;
2173 }
2174
2175 /* Return whether OP is a DECL whose address is interprocedural-invariant.  */
2176
2177 bool
2178 decl_address_ip_invariant_p (const_tree op)
2179 {
2180   /* The conditions below are slightly less strict than the one in
2181      staticp.  */
2182
2183   switch (TREE_CODE (op))
2184     {
2185     case LABEL_DECL:
2186     case FUNCTION_DECL:
2187     case STRING_CST:
2188       return true;
2189
2190     case VAR_DECL:
2191       if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2192            && !DECL_DLLIMPORT_P (op))
2193           || DECL_THREAD_LOCAL_P (op))
2194         return true;
2195       break;
2196
2197     case CONST_DECL:
2198       if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2199         return true;
2200       break;
2201
2202     default:
2203       break;
2204     }
2205
2206   return false;
2207 }
2208
2209
2210 /* Return true if T is function-invariant (internal function, does
2211    not handle arithmetic; that's handled in skip_simple_arithmetic and
2212    tree_invariant_p).  */
2213
2214 static bool tree_invariant_p (tree t);
2215
2216 static bool
2217 tree_invariant_p_1 (tree t)
2218 {
2219   tree op;
2220
2221   if (TREE_CONSTANT (t)
2222       || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2223     return true;
2224
2225   switch (TREE_CODE (t))
2226     {
2227     case SAVE_EXPR:
2228       return true;
2229
2230     case ADDR_EXPR:
2231       op = TREE_OPERAND (t, 0);
2232       while (handled_component_p (op))
2233         {
2234           switch (TREE_CODE (op))
2235             {
2236             case ARRAY_REF:
2237             case ARRAY_RANGE_REF:
2238               if (!tree_invariant_p (TREE_OPERAND (op, 1))
2239                   || TREE_OPERAND (op, 2) != NULL_TREE
2240                   || TREE_OPERAND (op, 3) != NULL_TREE)
2241                 return false;
2242               break;
2243
2244             case COMPONENT_REF:
2245               if (TREE_OPERAND (op, 2) != NULL_TREE)
2246                 return false;
2247               break;
2248
2249             default:;
2250             }
2251           op = TREE_OPERAND (op, 0);
2252         }
2253
2254       return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2255
2256     default:
2257       break;
2258     }
2259
2260   return false;
2261 }
2262
2263 /* Return true if T is function-invariant.  */
2264
2265 static bool
2266 tree_invariant_p (tree t)
2267 {
2268   tree inner = skip_simple_arithmetic (t);
2269   return tree_invariant_p_1 (inner);
2270 }
2271
2272 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2273    Do this to any expression which may be used in more than one place,
2274    but must be evaluated only once.
2275
2276    Normally, expand_expr would reevaluate the expression each time.
2277    Calling save_expr produces something that is evaluated and recorded
2278    the first time expand_expr is called on it.  Subsequent calls to
2279    expand_expr just reuse the recorded value.
2280
2281    The call to expand_expr that generates code that actually computes
2282    the value is the first call *at compile time*.  Subsequent calls
2283    *at compile time* generate code to use the saved value.
2284    This produces correct result provided that *at run time* control
2285    always flows through the insns made by the first expand_expr
2286    before reaching the other places where the save_expr was evaluated.
2287    You, the caller of save_expr, must make sure this is so.
2288
2289    Constants, and certain read-only nodes, are returned with no
2290    SAVE_EXPR because that is safe.  Expressions containing placeholders
2291    are not touched; see tree.def for an explanation of what these
2292    are used for.  */
2293
2294 tree
2295 save_expr (tree expr)
2296 {
2297   tree t = fold (expr);
2298   tree inner;
2299
2300   /* If the tree evaluates to a constant, then we don't want to hide that
2301      fact (i.e. this allows further folding, and direct checks for constants).
2302      However, a read-only object that has side effects cannot be bypassed.
2303      Since it is no problem to reevaluate literals, we just return the
2304      literal node.  */
2305   inner = skip_simple_arithmetic (t);
2306   if (TREE_CODE (inner) == ERROR_MARK)
2307     return inner;
2308
2309   if (tree_invariant_p_1 (inner))
2310     return t;
2311
2312   /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2313      it means that the size or offset of some field of an object depends on
2314      the value within another field.
2315
2316      Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2317      and some variable since it would then need to be both evaluated once and
2318      evaluated more than once.  Front-ends must assure this case cannot
2319      happen by surrounding any such subexpressions in their own SAVE_EXPR
2320      and forcing evaluation at the proper time.  */
2321   if (contains_placeholder_p (inner))
2322     return t;
2323
2324   t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2325
2326   /* This expression might be placed ahead of a jump to ensure that the
2327      value was computed on both sides of the jump.  So make sure it isn't
2328      eliminated as dead.  */
2329   TREE_SIDE_EFFECTS (t) = 1;
2330   return t;
2331 }
2332
2333 /* Look inside EXPR and into any simple arithmetic operations.  Return
2334    the innermost non-arithmetic node.  */
2335
2336 tree
2337 skip_simple_arithmetic (tree expr)
2338 {
2339   tree inner;
2340
2341   /* We don't care about whether this can be used as an lvalue in this
2342      context.  */
2343   while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2344     expr = TREE_OPERAND (expr, 0);
2345
2346   /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2347      a constant, it will be more efficient to not make another SAVE_EXPR since
2348      it will allow better simplification and GCSE will be able to merge the
2349      computations if they actually occur.  */
2350   inner = expr;
2351   while (1)
2352     {
2353       if (UNARY_CLASS_P (inner))
2354         inner = TREE_OPERAND (inner, 0);
2355       else if (BINARY_CLASS_P (inner))
2356         {
2357           if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2358             inner = TREE_OPERAND (inner, 0);
2359           else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2360             inner = TREE_OPERAND (inner, 1);
2361           else
2362             break;
2363         }
2364       else
2365         break;
2366     }
2367
2368   return inner;
2369 }
2370
2371 /* Return which tree structure is used by T.  */
2372
2373 enum tree_node_structure_enum
2374 tree_node_structure (const_tree t)
2375 {
2376   const enum tree_code code = TREE_CODE (t);
2377
2378   switch (TREE_CODE_CLASS (code))
2379     {      
2380     case tcc_declaration:
2381       {
2382         switch (code)
2383           {
2384           case FIELD_DECL:
2385             return TS_FIELD_DECL;
2386           case PARM_DECL:
2387             return TS_PARM_DECL;
2388           case VAR_DECL:
2389             return TS_VAR_DECL;
2390           case LABEL_DECL:
2391             return TS_LABEL_DECL;
2392           case RESULT_DECL:
2393             return TS_RESULT_DECL;
2394           case CONST_DECL:
2395             return TS_CONST_DECL;
2396           case TYPE_DECL:
2397             return TS_TYPE_DECL;
2398           case FUNCTION_DECL:
2399             return TS_FUNCTION_DECL;
2400           case SYMBOL_MEMORY_TAG:
2401           case NAME_MEMORY_TAG:
2402           case MEMORY_PARTITION_TAG:
2403             return TS_MEMORY_TAG;
2404           default:
2405             return TS_DECL_NON_COMMON;
2406           }
2407       }
2408     case tcc_type:
2409       return TS_TYPE;
2410     case tcc_reference:
2411     case tcc_comparison:
2412     case tcc_unary:
2413     case tcc_binary:
2414     case tcc_expression:
2415     case tcc_statement:
2416     case tcc_vl_exp:
2417       return TS_EXP;
2418     default:  /* tcc_constant and tcc_exceptional */
2419       break;
2420     }
2421   switch (code)
2422     {
2423       /* tcc_constant cases.  */
2424     case INTEGER_CST:           return TS_INT_CST;
2425     case REAL_CST:              return TS_REAL_CST;
2426     case FIXED_CST:             return TS_FIXED_CST;
2427     case COMPLEX_CST:           return TS_COMPLEX;
2428     case VECTOR_CST:            return TS_VECTOR;
2429     case STRING_CST:            return TS_STRING;
2430       /* tcc_exceptional cases.  */
2431     case ERROR_MARK:            return TS_COMMON;
2432     case IDENTIFIER_NODE:       return TS_IDENTIFIER;
2433     case TREE_LIST:             return TS_LIST;
2434     case TREE_VEC:              return TS_VEC;
2435     case SSA_NAME:              return TS_SSA_NAME;
2436     case PLACEHOLDER_EXPR:      return TS_COMMON;
2437     case STATEMENT_LIST:        return TS_STATEMENT_LIST;
2438     case BLOCK:                 return TS_BLOCK;
2439     case CONSTRUCTOR:           return TS_CONSTRUCTOR;
2440     case TREE_BINFO:            return TS_BINFO;
2441     case OMP_CLAUSE:            return TS_OMP_CLAUSE;
2442     case OPTIMIZATION_NODE:     return TS_OPTIMIZATION;
2443     case TARGET_OPTION_NODE:    return TS_TARGET_OPTION;
2444
2445     default:
2446       gcc_unreachable ();
2447     }
2448 }
2449 \f
2450 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2451    or offset that depends on a field within a record.  */
2452
2453 bool
2454 contains_placeholder_p (const_tree exp)
2455 {
2456   enum tree_code code;
2457
2458   if (!exp)
2459     return 0;
2460
2461   code = TREE_CODE (exp);
2462   if (code == PLACEHOLDER_EXPR)
2463     return 1;
2464
2465   switch (TREE_CODE_CLASS (code))
2466     {
2467     case tcc_reference:
2468       /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2469          position computations since they will be converted into a
2470          WITH_RECORD_EXPR involving the reference, which will assume
2471          here will be valid.  */
2472       return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2473
2474     case tcc_exceptional:
2475       if (code == TREE_LIST)
2476         return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2477                 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2478       break;
2479
2480     case tcc_unary:
2481     case tcc_binary:
2482     case tcc_comparison:
2483     case tcc_expression:
2484       switch (code)
2485         {
2486         case COMPOUND_EXPR:
2487           /* Ignoring the first operand isn't quite right, but works best.  */
2488           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2489
2490         case COND_EXPR:
2491           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2492                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2493                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2494
2495         case SAVE_EXPR:
2496           /* The save_expr function never wraps anything containing
2497              a PLACEHOLDER_EXPR. */
2498           return 0;
2499
2500         default:
2501           break;
2502         }
2503
2504       switch (TREE_CODE_LENGTH (code))
2505         {
2506         case 1:
2507           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2508         case 2:
2509           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2510                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2511         default:
2512           return 0;
2513         }
2514
2515     case tcc_vl_exp:
2516       switch (code)
2517         {
2518         case CALL_EXPR:
2519           {
2520             const_tree arg;
2521             const_call_expr_arg_iterator iter;
2522             FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2523               if (CONTAINS_PLACEHOLDER_P (arg))
2524                 return 1;
2525             return 0;
2526           }
2527         default:
2528           return 0;
2529         }
2530
2531     default:
2532       return 0;
2533     }
2534   return 0;
2535 }
2536
2537 /* Return true if any part of the computation of TYPE involves a
2538    PLACEHOLDER_EXPR.  This includes size, bounds, qualifiers
2539    (for QUAL_UNION_TYPE) and field positions.  */
2540
2541 static bool
2542 type_contains_placeholder_1 (const_tree type)
2543 {
2544   /* If the size contains a placeholder or the parent type (component type in
2545      the case of arrays) type involves a placeholder, this type does.  */
2546   if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2547       || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2548       || (TREE_TYPE (type) != 0
2549           && type_contains_placeholder_p (TREE_TYPE (type))))
2550     return true;
2551
2552   /* Now do type-specific checks.  Note that the last part of the check above
2553      greatly limits what we have to do below.  */
2554   switch (TREE_CODE (type))
2555     {
2556     case VOID_TYPE:
2557     case COMPLEX_TYPE:
2558     case ENUMERAL_TYPE:
2559     case BOOLEAN_TYPE:
2560     case POINTER_TYPE:
2561     case OFFSET_TYPE:
2562     case REFERENCE_TYPE:
2563     case METHOD_TYPE:
2564     case FUNCTION_TYPE:
2565     case VECTOR_TYPE:
2566       return false;
2567
2568     case INTEGER_TYPE:
2569     case REAL_TYPE:
2570     case FIXED_POINT_TYPE:
2571       /* Here we just check the bounds.  */
2572       return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2573               || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2574
2575     case ARRAY_TYPE:
2576       /* We're already checked the component type (TREE_TYPE), so just check
2577          the index type.  */
2578       return type_contains_placeholder_p (TYPE_DOMAIN (type));
2579
2580     case RECORD_TYPE:
2581     case UNION_TYPE:
2582     case QUAL_UNION_TYPE:
2583       {
2584         tree field;
2585
2586         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2587           if (TREE_CODE (field) == FIELD_DECL
2588               && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2589                   || (TREE_CODE (type) == QUAL_UNION_TYPE
2590                       && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2591                   || type_contains_placeholder_p (TREE_TYPE (field))))
2592             return true;
2593
2594         return false;
2595       }
2596
2597     default:
2598       gcc_unreachable ();
2599     }
2600 }
2601
2602 bool
2603 type_contains_placeholder_p (tree type)
2604 {
2605   bool result;
2606
2607   /* If the contains_placeholder_bits field has been initialized,
2608      then we know the answer.  */
2609   if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2610     return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2611
2612   /* Indicate that we've seen this type node, and the answer is false.
2613      This is what we want to return if we run into recursion via fields.  */
2614   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2615
2616   /* Compute the real value.  */
2617   result = type_contains_placeholder_1 (type);
2618
2619   /* Store the real value.  */
2620   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2621
2622   return result;
2623 }
2624 \f
2625 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2626    return a tree with all occurrences of references to F in a
2627    PLACEHOLDER_EXPR replaced by R.   Note that we assume here that EXP
2628    contains only arithmetic expressions or a CALL_EXPR with a
2629    PLACEHOLDER_EXPR occurring only in its arglist.  */
2630
2631 tree
2632 substitute_in_expr (tree exp, tree f, tree r)
2633 {
2634   enum tree_code code = TREE_CODE (exp);
2635   tree op0, op1, op2, op3;
2636   tree new_tree, inner;
2637
2638   /* We handle TREE_LIST and COMPONENT_REF separately.  */
2639   if (code == TREE_LIST)
2640     {
2641       op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2642       op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2643       if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2644         return exp;
2645
2646       return tree_cons (TREE_PURPOSE (exp), op1, op0);
2647     }
2648   else if (code == COMPONENT_REF)
2649    {
2650      /* If this expression is getting a value from a PLACEHOLDER_EXPR
2651         and it is the right field, replace it with R.  */
2652      for (inner = TREE_OPERAND (exp, 0);
2653           REFERENCE_CLASS_P (inner);
2654           inner = TREE_OPERAND (inner, 0))
2655        ;
2656      if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2657          && TREE_OPERAND (exp, 1) == f)
2658        return r;
2659
2660      /* If this expression hasn't been completed let, leave it alone.  */
2661      if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2662        return exp;
2663
2664      op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2665      if (op0 == TREE_OPERAND (exp, 0))
2666        return exp;
2667
2668      new_tree = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2669                         op0, TREE_OPERAND (exp, 1), NULL_TREE);
2670    }
2671   else
2672     switch (TREE_CODE_CLASS (code))
2673       {
2674       case tcc_constant:
2675       case tcc_declaration:
2676         return exp;
2677
2678       case tcc_exceptional:
2679       case tcc_unary:
2680       case tcc_binary:
2681       case tcc_comparison:
2682       case tcc_expression:
2683       case tcc_reference:
2684         switch (TREE_CODE_LENGTH (code))
2685           {
2686           case 0:
2687             return exp;
2688
2689           case 1:
2690             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2691             if (op0 == TREE_OPERAND (exp, 0))
2692               return exp;
2693
2694             new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
2695             break;
2696
2697           case 2:
2698             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2699             op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2700
2701             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2702               return exp;
2703
2704             new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2705             break;
2706
2707           case 3:
2708             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2709             op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2710             op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2711
2712             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2713                 && op2 == TREE_OPERAND (exp, 2))
2714               return exp;
2715
2716             new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2717             break;
2718
2719           case 4:
2720             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2721             op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2722             op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2723             op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2724
2725             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2726                 && op2 == TREE_OPERAND (exp, 2)
2727                 && op3 == TREE_OPERAND (exp, 3))
2728               return exp;
2729
2730             new_tree = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2731             break;
2732
2733           default:
2734             gcc_unreachable ();
2735           }
2736         break;
2737
2738       case tcc_vl_exp:
2739         {
2740           tree copy = NULL_TREE;
2741           int i;
2742
2743           for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2744             {
2745               tree op = TREE_OPERAND (exp, i);
2746               tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
2747               if (new_op != op)
2748                 {
2749                   if (!copy)
2750                     copy = copy_node (exp);
2751                   TREE_OPERAND (copy, i) = new_op;
2752                 }
2753             }
2754
2755           if (copy)
2756             new_tree = fold (copy);
2757           else
2758             return exp;
2759         }
2760         break;
2761
2762       default:
2763         gcc_unreachable ();
2764       }
2765
2766   TREE_READONLY (new_tree) = TREE_READONLY (exp);
2767   return new_tree;
2768 }
2769
2770 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2771    for it within OBJ, a tree that is an object or a chain of references.  */
2772
2773 tree
2774 substitute_placeholder_in_expr (tree exp, tree obj)
2775 {
2776   enum tree_code code = TREE_CODE (exp);
2777   tree op0, op1, op2, op3;
2778
2779   /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2780      in the chain of OBJ.  */
2781   if (code == PLACEHOLDER_EXPR)
2782     {
2783       tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2784       tree elt;
2785
2786       for (elt = obj; elt != 0;
2787            elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2788                    || TREE_CODE (elt) == COND_EXPR)
2789                   ? TREE_OPERAND (elt, 1)
2790                   : (REFERENCE_CLASS_P (elt)
2791                      || UNARY_CLASS_P (elt)
2792                      || BINARY_CLASS_P (elt)
2793                      || VL_EXP_CLASS_P (elt)
2794                      || EXPRESSION_CLASS_P (elt))
2795                   ? TREE_OPERAND (elt, 0) : 0))
2796         if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2797           return elt;
2798
2799       for (elt = obj; elt != 0;
2800            elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2801                    || TREE_CODE (elt) == COND_EXPR)
2802                   ? TREE_OPERAND (elt, 1)
2803                   : (REFERENCE_CLASS_P (elt)
2804                      || UNARY_CLASS_P (elt)
2805                      || BINARY_CLASS_P (elt)
2806                      || VL_EXP_CLASS_P (elt)
2807                      || EXPRESSION_CLASS_P (elt))
2808                   ? TREE_OPERAND (elt, 0) : 0))
2809         if (POINTER_TYPE_P (TREE_TYPE (elt))
2810             && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2811                 == need_type))
2812           return fold_build1 (INDIRECT_REF, need_type, elt);
2813
2814       /* If we didn't find it, return the original PLACEHOLDER_EXPR.  If it
2815          survives until RTL generation, there will be an error.  */
2816       return exp;
2817     }
2818
2819   /* TREE_LIST is special because we need to look at TREE_VALUE
2820      and TREE_CHAIN, not TREE_OPERANDS.  */
2821   else if (code == TREE_LIST)
2822     {
2823       op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2824       op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2825       if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2826         return exp;
2827
2828       return tree_cons (TREE_PURPOSE (exp), op1, op0);
2829     }
2830   else
2831     switch (TREE_CODE_CLASS (code))
2832       {
2833       case tcc_constant:
2834       case tcc_declaration:
2835         return exp;
2836
2837       case tcc_exceptional:
2838       case tcc_unary:
2839       case tcc_binary:
2840       case tcc_comparison:
2841       case tcc_expression:
2842       case tcc_reference:
2843       case tcc_statement:
2844         switch (TREE_CODE_LENGTH (code))
2845           {
2846           case 0:
2847             return exp;
2848
2849           case 1:
2850             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2851             if (op0 == TREE_OPERAND (exp, 0))
2852               return exp;
2853             else
2854               return fold_build1 (code, TREE_TYPE (exp), op0);
2855
2856           case 2:
2857             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2858             op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2859
2860             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2861               return exp;
2862             else
2863               return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2864
2865           case 3:
2866             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2867             op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2868             op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2869
2870             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2871                 && op2 == TREE_OPERAND (exp, 2))
2872               return exp;
2873             else
2874               return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2875
2876           case 4:
2877             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2878             op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2879             op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2880             op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2881
2882             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2883                 && op2 == TREE_OPERAND (exp, 2)
2884                 && op3 == TREE_OPERAND (exp, 3))
2885               return exp;
2886             else
2887               return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2888
2889           default:
2890             gcc_unreachable ();
2891           }
2892         break;
2893
2894       case tcc_vl_exp:
2895         {
2896           tree copy = NULL_TREE;
2897           int i;
2898
2899           for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2900             {
2901               tree op = TREE_OPERAND (exp, i);
2902               tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
2903               if (new_op != op)
2904                 {
2905                   if (!copy)
2906                     copy = copy_node (exp);
2907                   TREE_OPERAND (copy, i) = new_op;
2908                 }
2909             }
2910
2911           if (copy)
2912             return fold (copy);
2913           else
2914             return exp;
2915         }
2916
2917       default:
2918         gcc_unreachable ();
2919       }
2920 }
2921 \f
2922 /* Stabilize a reference so that we can use it any number of times
2923    without causing its operands to be evaluated more than once.
2924    Returns the stabilized reference.  This works by means of save_expr,
2925    so see the caveats in the comments about save_expr.
2926
2927    Also allows conversion expressions whose operands are references.
2928    Any other kind of expression is returned unchanged.  */
2929
2930 tree
2931 stabilize_reference (tree ref)
2932 {
2933   tree result;
2934   enum tree_code code = TREE_CODE (ref);
2935
2936   switch (code)
2937     {
2938     case VAR_DECL:
2939     case PARM_DECL:
2940     case RESULT_DECL:
2941       /* No action is needed in this case.  */
2942       return ref;
2943
2944     CASE_CONVERT:
2945     case FLOAT_EXPR:
2946     case FIX_TRUNC_EXPR:
2947       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2948       break;
2949
2950     case INDIRECT_REF:
2951       result = build_nt (INDIRECT_REF,
2952                          stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2953       break;
2954
2955     case COMPONENT_REF:
2956       result = build_nt (COMPONENT_REF,
2957                          stabilize_reference (TREE_OPERAND (ref, 0)),
2958                          TREE_OPERAND (ref, 1), NULL_TREE);
2959       break;
2960
2961     case BIT_FIELD_REF:
2962       result = build_nt (BIT_FIELD_REF,
2963                          stabilize_reference (TREE_OPERAND (ref, 0)),
2964                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2965                          stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2966       break;
2967
2968     case ARRAY_REF:
2969       result = build_nt (ARRAY_REF,
2970                          stabilize_reference (TREE_OPERAND (ref, 0)),
2971                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2972                          TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2973       break;
2974
2975     case ARRAY_RANGE_REF:
2976       result = build_nt (ARRAY_RANGE_REF,
2977                          stabilize_reference (TREE_OPERAND (ref, 0)),
2978                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2979                          TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2980       break;
2981
2982     case COMPOUND_EXPR:
2983       /* We cannot wrap the first expression in a SAVE_EXPR, as then
2984          it wouldn't be ignored.  This matters when dealing with
2985          volatiles.  */
2986       return stabilize_reference_1 (ref);
2987
2988       /* If arg isn't a kind of lvalue we recognize, make no change.
2989          Caller should recognize the error for an invalid lvalue.  */
2990     default:
2991       return ref;
2992
2993     case ERROR_MARK:
2994       return error_mark_node;
2995     }
2996
2997   TREE_TYPE (result) = TREE_TYPE (ref);
2998   TREE_READONLY (result) = TREE_READONLY (ref);
2999   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3000   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3001
3002   return result;
3003 }
3004
3005 /* Subroutine of stabilize_reference; this is called for subtrees of
3006    references.  Any expression with side-effects must be put in a SAVE_EXPR
3007    to ensure that it is only evaluated once.
3008
3009    We don't put SAVE_EXPR nodes around everything, because assigning very
3010    simple expressions to temporaries causes us to miss good opportunities
3011    for optimizations.  Among other things, the opportunity to fold in the
3012    addition of a constant into an addressing mode often gets lost, e.g.
3013    "y[i+1] += x;".  In general, we take the approach that we should not make
3014    an assignment unless we are forced into it - i.e., that any non-side effect
3015    operator should be allowed, and that cse should take care of coalescing
3016    multiple utterances of the same expression should that prove fruitful.  */
3017
3018 tree
3019 stabilize_reference_1 (tree e)
3020 {
3021   tree result;
3022   enum tree_code code = TREE_CODE (e);
3023
3024   /* We cannot ignore const expressions because it might be a reference
3025      to a const array but whose index contains side-effects.  But we can
3026      ignore things that are actual constant or that already have been
3027      handled by this function.  */
3028
3029   if (tree_invariant_p (e))
3030     return e;
3031
3032   switch (TREE_CODE_CLASS (code))
3033     {
3034     case tcc_exceptional:
3035     case tcc_type:
3036     case tcc_declaration:
3037     case tcc_comparison:
3038     case tcc_statement:
3039     case tcc_expression:
3040     case tcc_reference:
3041     case tcc_vl_exp:
3042       /* If the expression has side-effects, then encase it in a SAVE_EXPR
3043          so that it will only be evaluated once.  */
3044       /* The reference (r) and comparison (<) classes could be handled as
3045          below, but it is generally faster to only evaluate them once.  */
3046       if (TREE_SIDE_EFFECTS (e))
3047         return save_expr (e);
3048       return e;
3049
3050     case tcc_constant:
3051       /* Constants need no processing.  In fact, we should never reach
3052          here.  */
3053       return e;
3054
3055     case tcc_binary:
3056       /* Division is slow and tends to be compiled with jumps,
3057          especially the division by powers of 2 that is often
3058          found inside of an array reference.  So do it just once.  */
3059       if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3060           || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3061           || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3062           || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3063         return save_expr (e);
3064       /* Recursively stabilize each operand.  */
3065       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3066                          stabilize_reference_1 (TREE_OPERAND (e, 1)));
3067       break;
3068
3069     case tcc_unary:
3070       /* Recursively stabilize each operand.  */
3071       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3072       break;
3073
3074     default:
3075       gcc_unreachable ();
3076     }
3077
3078   TREE_TYPE (result) = TREE_TYPE (e);
3079   TREE_READONLY (result) = TREE_READONLY (e);
3080   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3081   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3082
3083   return result;
3084 }
3085 \f
3086 /* Low-level constructors for expressions.  */
3087
3088 /* A helper function for build1 and constant folders.  Set TREE_CONSTANT,
3089    and TREE_SIDE_EFFECTS for an ADDR_EXPR.  */
3090
3091 void
3092 recompute_tree_invariant_for_addr_expr (tree t)
3093 {
3094   tree node;
3095   bool tc = true, se = false;
3096
3097   /* We started out assuming this address is both invariant and constant, but
3098      does not have side effects.  Now go down any handled components and see if
3099      any of them involve offsets that are either non-constant or non-invariant.
3100      Also check for side-effects.
3101
3102      ??? Note that this code makes no attempt to deal with the case where
3103      taking the address of something causes a copy due to misalignment.  */
3104
3105 #define UPDATE_FLAGS(NODE)  \
3106 do { tree _node = (NODE); \
3107      if (_node && !TREE_CONSTANT (_node)) tc = false; \
3108      if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3109
3110   for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3111        node = TREE_OPERAND (node, 0))
3112     {
3113       /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3114          array reference (probably made temporarily by the G++ front end),
3115          so ignore all the operands.  */
3116       if ((TREE_CODE (node) == ARRAY_REF
3117            || TREE_CODE (node) == ARRAY_RANGE_REF)
3118           && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3119         {
3120           UPDATE_FLAGS (TREE_OPERAND (node, 1));
3121           if (TREE_OPERAND (node, 2))
3122             UPDATE_FLAGS (TREE_OPERAND (node, 2));
3123           if (TREE_OPERAND (node, 3))
3124             UPDATE_FLAGS (TREE_OPERAND (node, 3));
3125         }
3126       /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3127          FIELD_DECL, apparently.  The G++ front end can put something else
3128          there, at least temporarily.  */
3129       else if (TREE_CODE (node) == COMPONENT_REF
3130                && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3131         {
3132           if (TREE_OPERAND (node, 2))
3133             UPDATE_FLAGS (TREE_OPERAND (node, 2));
3134         }
3135       else if (TREE_CODE (node) == BIT_FIELD_REF)
3136         UPDATE_FLAGS (TREE_OPERAND (node, 2));
3137     }
3138
3139   node = lang_hooks.expr_to_decl (node, &tc, &se);
3140
3141   /* Now see what's inside.  If it's an INDIRECT_REF, copy our properties from
3142      the address, since &(*a)->b is a form of addition.  If it's a constant, the
3143      address is constant too.  If it's a decl, its address is constant if the
3144      decl is static.  Everything else is not constant and, furthermore,
3145      taking the address of a volatile variable is not volatile.  */
3146   if (TREE_CODE (node) == INDIRECT_REF)
3147     UPDATE_FLAGS (TREE_OPERAND (node, 0));
3148   else if (CONSTANT_CLASS_P (node))
3149     ;
3150   else if (DECL_P (node))
3151     tc &= (staticp (node) != NULL_TREE);
3152   else
3153     {
3154       tc = false;
3155       se |= TREE_SIDE_EFFECTS (node);
3156     }
3157
3158
3159   TREE_CONSTANT (t) = tc;
3160   TREE_SIDE_EFFECTS (t) = se;
3161 #undef UPDATE_FLAGS
3162 }
3163
3164 /* Build an expression of code CODE, data type TYPE, and operands as
3165    specified.  Expressions and reference nodes can be created this way.
3166    Constants, decls, types and misc nodes cannot be.
3167
3168    We define 5 non-variadic functions, from 0 to 4 arguments.  This is
3169    enough for all extant tree codes.  */
3170
3171 tree
3172 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3173 {
3174   tree t;
3175
3176   gcc_assert (TREE_CODE_LENGTH (code) == 0);
3177
3178   t = make_node_stat (code PASS_MEM_STAT);
3179   TREE_TYPE (t) = tt;
3180
3181   return t;
3182 }
3183
3184 tree
3185 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3186 {
3187   int length = sizeof (struct tree_exp);
3188 #ifdef GATHER_STATISTICS
3189   tree_node_kind kind;
3190 #endif
3191   tree t;
3192
3193 #ifdef GATHER_STATISTICS
3194   switch (TREE_CODE_CLASS (code))
3195     {
3196     case tcc_statement:  /* an expression with side effects */
3197       kind = s_kind;
3198       break;
3199     case tcc_reference:  /* a reference */
3200       kind = r_kind;
3201       break;
3202     default:
3203       kind = e_kind;
3204       break;
3205     }
3206
3207   tree_node_counts[(int) kind]++;
3208   tree_node_sizes[(int) kind] += length;
3209 #endif
3210
3211   gcc_assert (TREE_CODE_LENGTH (code) == 1);
3212
3213   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3214
3215   memset (t, 0, sizeof (struct tree_common));
3216
3217   TREE_SET_CODE (t, code);
3218
3219   TREE_TYPE (t) = type;
3220   SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3221   TREE_OPERAND (t, 0) = node;
3222   TREE_BLOCK (t) = NULL_TREE;
3223   if (node && !TYPE_P (node))
3224     {
3225       TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3226       TREE_READONLY (t) = TREE_READONLY (node);
3227     }
3228
3229   if (TREE_CODE_CLASS (code) == tcc_statement)
3230     TREE_SIDE_EFFECTS (t) = 1;
3231   else switch (code)
3232     {
3233     case VA_ARG_EXPR:
3234       /* All of these have side-effects, no matter what their
3235          operands are.  */
3236       TREE_SIDE_EFFECTS (t) = 1;
3237       TREE_READONLY (t) = 0;
3238       break;
3239
3240     case MISALIGNED_INDIRECT_REF:
3241     case ALIGN_INDIRECT_REF:
3242     case INDIRECT_REF:
3243       /* Whether a dereference is readonly has nothing to do with whether
3244          its operand is readonly.  */
3245       TREE_READONLY (t) = 0;
3246       break;
3247
3248     case ADDR_EXPR:
3249       if (node)
3250         recompute_tree_invariant_for_addr_expr (t);
3251       break;
3252
3253     default:
3254       if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3255           && node && !TYPE_P (node)
3256           && TREE_CONSTANT (node))
3257         TREE_CONSTANT (t) = 1;
3258       if (TREE_CODE_CLASS (code) == tcc_reference
3259           && node && TREE_THIS_VOLATILE (node))
3260         TREE_THIS_VOLATILE (t) = 1;
3261       break;
3262     }
3263
3264   return t;
3265 }
3266
3267 #define PROCESS_ARG(N)                  \
3268   do {                                  \
3269     TREE_OPERAND (t, N) = arg##N;       \
3270     if (arg##N &&!TYPE_P (arg##N))      \
3271       {                                 \
3272         if (TREE_SIDE_EFFECTS (arg##N)) \
3273           side_effects = 1;             \
3274         if (!TREE_READONLY (arg##N))    \
3275           read_only = 0;                \
3276         if (!TREE_CONSTANT (arg##N))    \
3277           constant = 0;                 \
3278       }                                 \
3279   } while (0)
3280
3281 tree
3282 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3283 {
3284   bool constant, read_only, side_effects;
3285   tree t;
3286
3287   gcc_assert (TREE_CODE_LENGTH (code) == 2);
3288
3289   if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3290       && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3291       /* When sizetype precision doesn't match that of pointers
3292          we need to be able to build explicit extensions or truncations
3293          of the offset argument.  */
3294       && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3295     gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3296                 && TREE_CODE (arg1) == INTEGER_CST);
3297
3298   if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3299     gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3300                 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3301                 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3302
3303   t = make_node_stat (code PASS_MEM_STAT);
3304   TREE_TYPE (t) = tt;
3305
3306   /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3307      result based on those same flags for the arguments.  But if the
3308      arguments aren't really even `tree' expressions, we shouldn't be trying
3309      to do this.  */
3310
3311   /* Expressions without side effects may be constant if their
3312      arguments are as well.  */
3313   constant = (TREE_CODE_CLASS (code) == tcc_comparison
3314               || TREE_CODE_CLASS (code) == tcc_binary);
3315   read_only = 1;
3316   side_effects = TREE_SIDE_EFFECTS (t);
3317
3318   PROCESS_ARG(0);
3319   PROCESS_ARG(1);
3320
3321   TREE_READONLY (t) = read_only;
3322   TREE_CONSTANT (t) = constant;
3323   TREE_SIDE_EFFECTS (t) = side_effects;
3324   TREE_THIS_VOLATILE (t)
3325     = (TREE_CODE_CLASS (code) == tcc_reference
3326        && arg0 && TREE_THIS_VOLATILE (arg0));
3327
3328   return t;
3329 }
3330
3331
3332 tree
3333 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3334              tree arg2 MEM_STAT_DECL)
3335 {
3336   bool constant, read_only, side_effects;
3337   tree t;
3338
3339   gcc_assert (TREE_CODE_LENGTH (code) == 3);
3340   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3341
3342   t = make_node_stat (code PASS_MEM_STAT);
3343   TREE_TYPE (t) = tt;
3344
3345   /* As a special exception, if COND_EXPR has NULL branches, we
3346      assume that it is a gimple statement and always consider
3347      it to have side effects.  */
3348   if (code == COND_EXPR
3349       && tt == void_type_node
3350       && arg1 == NULL_TREE
3351       && arg2 == NULL_TREE)
3352     side_effects = true;
3353   else
3354     side_effects = TREE_SIDE_EFFECTS (t);
3355
3356   PROCESS_ARG(0);
3357   PROCESS_ARG(1);
3358   PROCESS_ARG(2);
3359
3360   TREE_SIDE_EFFECTS (t) = side_effects;
3361   TREE_THIS_VOLATILE (t)
3362     = (TREE_CODE_CLASS (code) == tcc_reference
3363        && arg0 && TREE_THIS_VOLATILE (arg0));
3364
3365   return t;
3366 }
3367
3368 tree
3369 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3370              tree arg2, tree arg3 MEM_STAT_DECL)
3371 {
3372   bool constant, read_only, side_effects;
3373   tree t;
3374
3375   gcc_assert (TREE_CODE_LENGTH (code) == 4);
3376
3377   t = make_node_stat (code PASS_MEM_STAT);
3378   TREE_TYPE (t) = tt;
3379
3380   side_effects = TREE_SIDE_EFFECTS (t);
3381
3382   PROCESS_ARG(0);
3383   PROCESS_ARG(1);
3384   PROCESS_ARG(2);
3385   PROCESS_ARG(3);
3386
3387   TREE_SIDE_EFFECTS (t) = side_effects;
3388   TREE_THIS_VOLATILE (t)
3389     = (TREE_CODE_CLASS (code) == tcc_reference
3390        && arg0 && TREE_THIS_VOLATILE (arg0));
3391
3392   return t;
3393 }
3394
3395 tree
3396 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3397              tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3398 {
3399   bool constant, read_only, side_effects;
3400   tree t;
3401
3402   gcc_assert (TREE_CODE_LENGTH (code) == 5);
3403
3404   t = make_node_stat (code PASS_MEM_STAT);
3405   TREE_TYPE (t) = tt;
3406
3407   side_effects = TREE_SIDE_EFFECTS (t);
3408
3409   PROCESS_ARG(0);
3410   PROCESS_ARG(1);
3411   PROCESS_ARG(2);
3412   PROCESS_ARG(3);
3413   PROCESS_ARG(4);
3414
3415   TREE_SIDE_EFFECTS (t) = side_effects;
3416   TREE_THIS_VOLATILE (t)
3417     = (TREE_CODE_CLASS (code) == tcc_reference
3418        && arg0 && TREE_THIS_VOLATILE (arg0));
3419
3420   return t;
3421 }
3422
3423 tree
3424 build7_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3425              tree arg2, tree arg3, tree arg4, tree arg5,
3426              tree arg6 MEM_STAT_DECL)
3427 {
3428   bool constant, read_only, side_effects;
3429   tree t;
3430
3431   gcc_assert (code == TARGET_MEM_REF);
3432
3433   t = make_node_stat (code PASS_MEM_STAT);
3434   TREE_TYPE (t) = tt;
3435
3436   side_effects = TREE_SIDE_EFFECTS (t);
3437
3438   PROCESS_ARG(0);
3439   PROCESS_ARG(1);
3440   PROCESS_ARG(2);
3441   PROCESS_ARG(3);
3442   PROCESS_ARG(4);
3443   PROCESS_ARG(5);
3444   PROCESS_ARG(6);
3445
3446   TREE_SIDE_EFFECTS (t) = side_effects;
3447   TREE_THIS_VOLATILE (t) = 0;
3448
3449   return t;
3450 }
3451
3452 /* Similar except don't specify the TREE_TYPE
3453    and leave the TREE_SIDE_EFFECTS as 0.
3454    It is permissible for arguments to be null,
3455    or even garbage if their values do not matter.  */
3456
3457 tree
3458 build_nt (enum tree_code code, ...)
3459 {
3460   tree t;
3461   int length;
3462   int i;
3463   va_list p;
3464
3465   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3466
3467   va_start (p, code);
3468
3469   t = make_node (code);
3470   length = TREE_CODE_LENGTH (code);
3471
3472   for (i = 0; i < length; i++)
3473     TREE_OPERAND (t, i) = va_arg (p, tree);
3474
3475   va_end (p);
3476   return t;
3477 }
3478
3479 /* Similar to build_nt, but for creating a CALL_EXPR object with
3480    ARGLIST passed as a list.  */
3481
3482 tree
3483 build_nt_call_list (tree fn, tree arglist)
3484 {
3485   tree t;
3486   int i;
3487
3488   t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3489   CALL_EXPR_FN (t) = fn;
3490   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3491   for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3492     CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3493   return t;
3494 }
3495 \f
3496 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3497    We do NOT enter this node in any sort of symbol table.
3498
3499    layout_decl is used to set up the decl's storage layout.
3500    Other slots are initialized to 0 or null pointers.  */
3501
3502 tree
3503 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3504 {
3505   tree t;
3506
3507   t = make_node_stat (code PASS_MEM_STAT);
3508
3509 /*  if (type == error_mark_node)
3510     type = integer_type_node; */
3511 /* That is not done, deliberately, so that having error_mark_node
3512    as the type can suppress useless errors in the use of this variable.  */
3513
3514   DECL_NAME (t) = name;
3515   TREE_TYPE (t) = type;
3516
3517   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3518     layout_decl (t, 0);
3519
3520   return t;
3521 }
3522
3523 /* Builds and returns function declaration with NAME and TYPE.  */
3524
3525 tree
3526 build_fn_decl (const char *name, tree type)
3527 {
3528   tree id = get_identifier (name);
3529   tree decl = build_decl (FUNCTION_DECL, id, type);
3530
3531   DECL_EXTERNAL (decl) = 1;
3532   TREE_PUBLIC (decl) = 1;
3533   DECL_ARTIFICIAL (decl) = 1;
3534   TREE_NOTHROW (decl) = 1;
3535
3536   return decl;
3537 }
3538
3539 \f
3540 /* BLOCK nodes are used to represent the structure of binding contours
3541    and declarations, once those contours have been exited and their contents
3542    compiled.  This information is used for outputting debugging info.  */
3543
3544 tree
3545 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3546 {
3547   tree block = make_node (BLOCK);
3548
3549   BLOCK_VARS (block) = vars;
3550   BLOCK_SUBBLOCKS (block) = subblocks;
3551   BLOCK_SUPERCONTEXT (block) = supercontext;
3552   BLOCK_CHAIN (block) = chain;
3553   return block;
3554 }
3555
3556 expanded_location
3557 expand_location (source_location loc)
3558 {
3559   expanded_location xloc;
3560   if (loc == 0)
3561     {
3562       xloc.file = NULL;
3563       xloc.line = 0;
3564       xloc.column = 0;
3565       xloc.sysp = 0;
3566     }
3567   else
3568     {
3569       const struct line_map *map = linemap_lookup (line_table, loc);
3570       xloc.file = map->to_file;
3571       xloc.line = SOURCE_LINE (map, loc);
3572       xloc.column = SOURCE_COLUMN (map, loc);
3573       xloc.sysp = map->sysp != 0;
3574     };
3575   return xloc;
3576 }
3577
3578 \f
3579 /* Source location accessor functions.  */
3580
3581
3582 void
3583 set_expr_locus (tree node, source_location *loc)
3584 {
3585   if (loc == NULL)
3586     EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3587   else
3588     EXPR_CHECK (node)->exp.locus = *loc;
3589 }
3590
3591 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
3592
3593    LOC is the location to use in tree T.  */
3594
3595 void protected_set_expr_location (tree t, location_t loc)
3596 {
3597   if (t && CAN_HAVE_LOCATION_P (t))
3598     SET_EXPR_LOCATION (t, loc);
3599 }
3600 \f
3601 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3602    is ATTRIBUTE.  */
3603
3604 tree
3605 build_decl_attribute_variant (tree ddecl, tree attribute)
3606 {
3607   DECL_ATTRIBUTES (ddecl) = attribute;
3608   return ddecl;
3609 }
3610
3611 /* Borrowed from hashtab.c iterative_hash implementation.  */
3612 #define mix(a,b,c) \
3613 { \
3614   a -= b; a -= c; a ^= (c>>13); \
3615   b -= c; b -= a; b ^= (a<< 8); \
3616   c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3617   a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3618   b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3619   c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3620   a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3621   b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3622   c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3623 }
3624
3625
3626 /* Produce good hash value combining VAL and VAL2.  */
3627 hashval_t
3628 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3629 {
3630   /* the golden ratio; an arbitrary value.  */
3631   hashval_t a = 0x9e3779b9;
3632
3633   mix (a, val, val2);
3634   return val2;
3635 }
3636
3637 /* Produce good hash value combining PTR and VAL2.  */
3638 static inline hashval_t
3639 iterative_hash_pointer (const void *ptr, hashval_t val2)
3640 {
3641   if (sizeof (ptr) == sizeof (hashval_t))
3642     return iterative_hash_hashval_t ((size_t) ptr, val2);
3643   else
3644     {
3645       hashval_t a = (hashval_t) (size_t) ptr;
3646       /* Avoid warnings about shifting of more than the width of the type on
3647          hosts that won't execute this path.  */
3648       int zero = 0;
3649       hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3650       mix (a, b, val2);
3651       return val2;
3652     }
3653 }
3654
3655 /* Produce good hash value combining VAL and VAL2.  */
3656 static inline hashval_t
3657 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3658 {
3659   if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3660     return iterative_hash_hashval_t (val, val2);
3661   else
3662     {
3663       hashval_t a = (hashval_t) val;
3664       /* Avoid warnings about shifting of more than the width of the type on
3665          hosts that won't execute this path.  */
3666       int zero = 0;
3667       hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3668       mix (a, b, val2);
3669       if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3670         {
3671           hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3672           hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3673           mix (a, b, val2);
3674         }
3675       return val2;
3676     }
3677 }
3678
3679 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3680    is ATTRIBUTE and its qualifiers are QUALS.
3681
3682    Record such modified types already made so we don't make duplicates.  */
3683
3684 static tree
3685 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3686 {
3687   if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3688     {
3689       hashval_t hashcode = 0;
3690       tree ntype;
3691       enum tree_code code = TREE_CODE (ttype);
3692
3693       /* Building a distinct copy of a tagged type is inappropriate; it
3694          causes breakage in code that expects there to be a one-to-one
3695          relationship between a struct and its fields.
3696          build_duplicate_type is another solution (as used in
3697          handle_transparent_union_attribute), but that doesn't play well
3698          with the stronger C++ type identity model.  */
3699       if (TREE_CODE (ttype) == RECORD_TYPE
3700           || TREE_CODE (ttype) == UNION_TYPE
3701           || TREE_CODE (ttype) == QUAL_UNION_TYPE
3702           || TREE_CODE (ttype) == ENUMERAL_TYPE)
3703         {
3704           warning (OPT_Wattributes,
3705                    "ignoring attributes applied to %qT after definition",
3706                    TYPE_MAIN_VARIANT (ttype));
3707           return build_qualified_type (ttype, quals);
3708         }
3709
3710       ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
3711       ntype = build_distinct_type_copy (ttype);
3712
3713       TYPE_ATTRIBUTES (ntype) = attribute;
3714
3715       hashcode = iterative_hash_object (code, hashcode);
3716       if (TREE_TYPE (ntype))
3717         hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3718                                           hashcode);
3719       hashcode = attribute_hash_list (attribute, hashcode);
3720
3721       switch (TREE_CODE (ntype))
3722         {
3723         case FUNCTION_TYPE:
3724           hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3725           break;
3726         case ARRAY_TYPE:
3727           if (TYPE_DOMAIN (ntype))
3728             hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3729                                               hashcode);
3730           break;
3731         case INTEGER_TYPE:
3732           hashcode = iterative_hash_object
3733             (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3734           hashcode = iterative_hash_object
3735             (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3736           break;
3737         case REAL_TYPE:
3738         case FIXED_POINT_TYPE:
3739           {
3740             unsigned int precision = TYPE_PRECISION (ntype);
3741             hashcode = iterative_hash_object (precision, hashcode);
3742           }
3743           break;
3744         default:
3745           break;
3746         }
3747
3748       ntype = type_hash_canon (hashcode, ntype);
3749
3750       /* If the target-dependent attributes make NTYPE different from
3751          its canonical type, we will need to use structural equality
3752          checks for this type. */
3753       if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
3754           || !targetm.comp_type_attributes (ntype, ttype))
3755         SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3756       else if (TYPE_CANONICAL (ntype) == ntype)
3757         TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
3758
3759       ttype = build_qualified_type (ntype, quals);
3760     }
3761   else if (TYPE_QUALS (ttype) != quals)
3762     ttype = build_qualified_type (ttype, quals);
3763
3764   return ttype;
3765 }
3766
3767
3768 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3769    is ATTRIBUTE.
3770
3771    Record such modified types already made so we don't make duplicates.  */
3772
3773 tree
3774 build_type_attribute_variant (tree ttype, tree attribute)
3775 {
3776   return build_type_attribute_qual_variant (ttype, attribute,
3777                                             TYPE_QUALS (ttype));
3778 }
3779
3780 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3781    or zero if not.
3782
3783    We try both `text' and `__text__', ATTR may be either one.  */
3784 /* ??? It might be a reasonable simplification to require ATTR to be only
3785    `text'.  One might then also require attribute lists to be stored in
3786    their canonicalized form.  */
3787
3788 static int
3789 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
3790 {
3791   int ident_len;
3792   const char *p;
3793
3794   if (TREE_CODE (ident) != IDENTIFIER_NODE)
3795     return 0;
3796   
3797   p = IDENTIFIER_POINTER (ident);
3798   ident_len = IDENTIFIER_LENGTH (ident);
3799   
3800   if (ident_len == attr_len
3801       && strcmp (attr, p) == 0)
3802     return 1;
3803
3804   /* If ATTR is `__text__', IDENT must be `text'; and vice versa.  */
3805   if (attr[0] == '_')
3806     {
3807       gcc_assert (attr[1] == '_');
3808       gcc_assert (attr[attr_len - 2] == '_');
3809       gcc_assert (attr[attr_len - 1] == '_');
3810       if (ident_len == attr_len - 4
3811           && strncmp (attr + 2, p, attr_len - 4) == 0)
3812         return 1;
3813     }
3814   else
3815     {
3816       if (ident_len == attr_len + 4
3817           && p[0] == '_' && p[1] == '_'
3818           && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3819           && strncmp (attr, p + 2, attr_len) == 0)
3820         return 1;
3821     }
3822
3823   return 0;
3824 }
3825
3826 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3827    or zero if not.
3828
3829    We try both `text' and `__text__', ATTR may be either one.  */
3830
3831 int
3832 is_attribute_p (const char *attr, const_tree ident)
3833 {
3834   return is_attribute_with_length_p (attr, strlen (attr), ident);
3835 }
3836
3837 /* Given an attribute name and a list of attributes, return a pointer to the
3838    attribute's list element if the attribute is part of the list, or NULL_TREE
3839    if not found.  If the attribute appears more than once, this only
3840    returns the first occurrence; the TREE_CHAIN of the return value should
3841    be passed back in if further occurrences are wanted.  */
3842
3843 tree
3844 lookup_attribute (const char *attr_name, tree list)
3845 {
3846   tree l;
3847   size_t attr_len = strlen (attr_name);
3848
3849   for (l = list; l; l = TREE_CHAIN (l))
3850     {
3851       gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3852       if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3853         return l;
3854     }
3855   return NULL_TREE;
3856 }
3857
3858 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3859    modified list.  */
3860
3861 tree
3862 remove_attribute (const char *attr_name, tree list)
3863 {
3864   tree *p;
3865   size_t attr_len = strlen (attr_name);
3866
3867   for (p = &list; *p; )
3868     {
3869       tree l = *p;
3870       gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3871       if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3872         *p = TREE_CHAIN (l);
3873       else
3874         p = &TREE_CHAIN (l);
3875     }
3876
3877   return list;
3878 }
3879
3880 /* Return an attribute list that is the union of a1 and a2.  */
3881
3882 tree
3883 merge_attributes (tree a1, tree a2)
3884 {
3885   tree attributes;
3886
3887   /* Either one unset?  Take the set one.  */
3888
3889   if ((attributes = a1) == 0)
3890     attributes = a2;
3891
3892   /* One that completely contains the other?  Take it.  */
3893
3894   else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3895     {
3896       if (attribute_list_contained (a2, a1))
3897         attributes = a2;
3898       else
3899         {
3900           /* Pick the longest list, and hang on the other list.  */
3901
3902           if (list_length (a1) < list_length (a2))
3903             attributes = a2, a2 = a1;
3904
3905           for (; a2 != 0; a2 = TREE_CHAIN (a2))
3906             {
3907               tree a;
3908               for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3909                                          attributes);
3910                    a != NULL_TREE;
3911                    a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3912                                          TREE_CHAIN (a)))
3913                 {
3914                   if (TREE_VALUE (a) != NULL
3915                       && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3916                       && TREE_VALUE (a2) != NULL
3917                       && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3918                     {
3919                       if (simple_cst_list_equal (TREE_VALUE (a),
3920                                                  TREE_VALUE (a2)) == 1)
3921                         break;
3922                     }
3923                   else if (simple_cst_equal (TREE_VALUE (a),
3924                                              TREE_VALUE (a2)) == 1)
3925                     break;
3926                 }
3927               if (a == NULL_TREE)
3928                 {
3929                   a1 = copy_node (a2);
3930                   TREE_CHAIN (a1) = attributes;
3931                   attributes = a1;
3932                 }
3933             }
3934         }
3935     }
3936   return attributes;
3937 }
3938
3939 /* Given types T1 and T2, merge their attributes and return
3940   the result.  */
3941
3942 tree
3943 merge_type_attributes (tree t1, tree t2)
3944 {
3945   return merge_attributes (TYPE_ATTRIBUTES (t1),
3946                            TYPE_ATTRIBUTES (t2));
3947 }
3948
3949 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3950    the result.  */
3951
3952 tree
3953 merge_decl_attributes (tree olddecl, tree newdecl)
3954 {
3955   return merge_attributes (DECL_ATTRIBUTES (olddecl),
3956                            DECL_ATTRIBUTES (newdecl));
3957 }
3958
3959 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3960
3961 /* Specialization of merge_decl_attributes for various Windows targets.
3962
3963    This handles the following situation:
3964
3965      __declspec (dllimport) int foo;
3966      int foo;
3967
3968    The second instance of `foo' nullifies the dllimport.  */
3969
3970 tree
3971 merge_dllimport_decl_attributes (tree old, tree new_tree)
3972 {
3973   tree a;
3974   int delete_dllimport_p = 1;
3975
3976   /* What we need to do here is remove from `old' dllimport if it doesn't
3977      appear in `new'.  dllimport behaves like extern: if a declaration is
3978      marked dllimport and a definition appears later, then the object
3979      is not dllimport'd.  We also remove a `new' dllimport if the old list
3980      contains dllexport:  dllexport always overrides dllimport, regardless
3981      of the order of declaration.  */     
3982   if (!VAR_OR_FUNCTION_DECL_P (new_tree))
3983     delete_dllimport_p = 0;
3984   else if (DECL_DLLIMPORT_P (new_tree)
3985            && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
3986     { 
3987       DECL_DLLIMPORT_P (new_tree) = 0;
3988       warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
3989               "dllimport ignored", new_tree);
3990     }
3991   else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
3992     {
3993       /* Warn about overriding a symbol that has already been used, e.g.:
3994            extern int __attribute__ ((dllimport)) foo;
3995            int* bar () {return &foo;}
3996            int foo;
3997       */
3998       if (TREE_USED (old))
3999         {
4000           warning (0, "%q+D redeclared without dllimport attribute "
4001                    "after being referenced with dll linkage", new_tree);
4002           /* If we have used a variable's address with dllimport linkage,
4003               keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
4004               decl may already have had TREE_CONSTANT computed.
4005               We still remove the attribute so that assembler code refers
4006               to '&foo rather than '_imp__foo'.  */
4007           if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4008             DECL_DLLIMPORT_P (new_tree) = 1;
4009         }
4010
4011       /* Let an inline definition silently override the external reference,
4012          but otherwise warn about attribute inconsistency.  */ 
4013       else if (TREE_CODE (new_tree) == VAR_DECL
4014                || !DECL_DECLARED_INLINE_P (new_tree))
4015         warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4016                   "previous dllimport ignored", new_tree);
4017     }
4018   else
4019     delete_dllimport_p = 0;
4020
4021   a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
4022
4023   if (delete_dllimport_p) 
4024     {
4025       tree prev, t;
4026       const size_t attr_len = strlen ("dllimport"); 
4027      
4028       /* Scan the list for dllimport and delete it.  */
4029       for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4030         {
4031           if (is_attribute_with_length_p ("dllimport", attr_len,
4032                                           TREE_PURPOSE (t)))
4033             {
4034               if (prev == NULL_TREE)
4035                 a = TREE_CHAIN (a);
4036               else
4037                 TREE_CHAIN (prev) = TREE_CHAIN (t);
4038               break;
4039             }
4040         }
4041     }
4042
4043   return a;
4044 }
4045
4046 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4047    struct attribute_spec.handler.  */
4048
4049 tree
4050 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4051                       bool *no_add_attrs)
4052 {
4053   tree node = *pnode;
4054
4055   /* These attributes may apply to structure and union types being created,
4056      but otherwise should pass to the declaration involved.  */
4057   if (!DECL_P (node))
4058     {
4059       if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4060                    | (int) ATTR_FLAG_ARRAY_NEXT))
4061         {
4062           *no_add_attrs = true;
4063           return tree_cons (name, args, NULL_TREE);
4064         }
4065       if (TREE_CODE (node) == RECORD_TYPE
4066           || TREE_CODE (node) == UNION_TYPE)
4067         {
4068           node = TYPE_NAME (node);
4069           if (!node)
4070             return NULL_TREE;
4071         }
4072       else
4073         {
4074           warning (OPT_Wattributes, "%qs attribute ignored",
4075                    IDENTIFIER_POINTER (name));
4076           *no_add_attrs = true;
4077           return NULL_TREE;
4078         }
4079     }
4080
4081   if (TREE_CODE (node) != FUNCTION_DECL
4082       && TREE_CODE (node) != VAR_DECL
4083       && TREE_CODE (node) != TYPE_DECL)
4084     {
4085       *no_add_attrs = true;
4086       warning (OPT_Wattributes, "%qs attribute ignored",
4087                IDENTIFIER_POINTER (name));
4088       return NULL_TREE;
4089     }
4090
4091   if (TREE_CODE (node) == TYPE_DECL
4092       && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4093       && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4094     {
4095       *no_add_attrs = true;
4096       warning (OPT_Wattributes, "%qs attribute ignored",
4097                IDENTIFIER_POINTER (name));
4098       return NULL_TREE;
4099     }
4100
4101   /* Report error on dllimport ambiguities seen now before they cause
4102      any damage.  */
4103   else if (is_attribute_p ("dllimport", name))
4104     {
4105       /* Honor any target-specific overrides. */ 
4106       if (!targetm.valid_dllimport_attribute_p (node))
4107         *no_add_attrs = true;
4108
4109      else if (TREE_CODE (node) == FUNCTION_DECL
4110                 && DECL_DECLARED_INLINE_P (node))
4111         {
4112           warning (OPT_Wattributes, "inline function %q+D declared as "
4113                   " dllimport: attribute ignored", node); 
4114           *no_add_attrs = true;
4115         }
4116       /* Like MS, treat definition of dllimported variables and
4117          non-inlined functions on declaration as syntax errors. */
4118      else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4119         {
4120           error ("function %q+D definition is marked dllimport", node);
4121           *no_add_attrs = true;
4122         }
4123
4124      else if (TREE_CODE (node) == VAR_DECL)
4125         {
4126           if (DECL_INITIAL (node))
4127             {
4128               error ("variable %q+D definition is marked dllimport",
4129                      node);
4130               *no_add_attrs = true;
4131             }
4132
4133           /* `extern' needn't be specified with dllimport.
4134              Specify `extern' now and hope for the best.  Sigh.  */
4135           DECL_EXTERNAL (node) = 1;
4136           /* Also, implicitly give dllimport'd variables declared within
4137              a function global scope, unless declared static.  */
4138           if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4139             TREE_PUBLIC (node) = 1;
4140         }
4141
4142       if (*no_add_attrs == false)
4143         DECL_DLLIMPORT_P (node) = 1;
4144     }
4145
4146   /*  Report error if symbol is not accessible at global scope.  */
4147   if (!TREE_PUBLIC (node)
4148       && (TREE_CODE (node) == VAR_DECL
4149           || TREE_CODE (node) == FUNCTION_DECL))
4150     {
4151       error ("external linkage required for symbol %q+D because of "
4152              "%qs attribute", node, IDENTIFIER_POINTER (name));
4153       *no_add_attrs = true;
4154     }
4155
4156   /* A dllexport'd entity must have default visibility so that other
4157      program units (shared libraries or the main executable) can see
4158      it.  A dllimport'd entity must have default visibility so that
4159      the linker knows that undefined references within this program
4160      unit can be resolved by the dynamic linker.  */
4161   if (!*no_add_attrs)
4162     {
4163       if (DECL_VISIBILITY_SPECIFIED (node)
4164           && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4165         error ("%qs implies default visibility, but %qD has already "
4166                "been declared with a different visibility", 
4167                IDENTIFIER_POINTER (name), node);
4168       DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4169       DECL_VISIBILITY_SPECIFIED (node) = 1;
4170     }
4171
4172   return NULL_TREE;
4173 }
4174
4175 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES  */
4176 \f
4177 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4178    of the various TYPE_QUAL values.  */
4179
4180 static void
4181 set_type_quals (tree type, int type_quals)
4182 {
4183   TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4184   TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4185   TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4186 }
4187
4188 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS.  */
4189
4190 bool
4191 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4192 {
4193   return (TYPE_QUALS (cand) == type_quals
4194           && TYPE_NAME (cand) == TYPE_NAME (base)
4195           /* Apparently this is needed for Objective-C.  */
4196           && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4197           && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4198                                    TYPE_ATTRIBUTES (base)));
4199 }
4200
4201 /* Return a version of the TYPE, qualified as indicated by the
4202    TYPE_QUALS, if one exists.  If no qualified version exists yet,
4203    return NULL_TREE.  */
4204
4205 tree
4206 get_qualified_type (tree type, int type_quals)
4207 {
4208   tree t;
4209
4210   if (TYPE_QUALS (type) == type_quals)
4211     return type;
4212
4213   /* Search the chain of variants to see if there is already one there just
4214      like the one we need to have.  If so, use that existing one.  We must
4215      preserve the TYPE_NAME, since there is code that depends on this.  */
4216   for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4217     if (check_qualified_type (t, type, type_quals))
4218       return t;
4219
4220   return NULL_TREE;
4221 }
4222
4223 /* Like get_qualified_type, but creates the type if it does not
4224    exist.  This function never returns NULL_TREE.  */
4225
4226 tree
4227 build_qualified_type (tree type, int type_quals)
4228 {
4229   tree t;
4230
4231   /* See if we already have the appropriate qualified variant.  */
4232   t = get_qualified_type (type, type_quals);
4233
4234   /* If not, build it.  */
4235   if (!t)
4236     {
4237       t = build_variant_type_copy (type);
4238       set_type_quals (t, type_quals);
4239
4240       if (TYPE_STRUCTURAL_EQUALITY_P (type))
4241         /* Propagate structural equality. */
4242         SET_TYPE_STRUCTURAL_EQUALITY (t);
4243       else if (TYPE_CANONICAL (type) != type)
4244         /* Build the underlying canonical type, since it is different
4245            from TYPE. */
4246         TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4247                                                    type_quals);
4248       else
4249         /* T is its own canonical type. */
4250         TYPE_CANONICAL (t) = t;
4251       
4252     }
4253
4254   return t;
4255 }
4256
4257 /* Create a new distinct copy of TYPE.  The new type is made its own
4258    MAIN_VARIANT. If TYPE requires structural equality checks, the
4259    resulting type requires structural equality checks; otherwise, its
4260    TYPE_CANONICAL points to itself. */
4261
4262 tree
4263 build_distinct_type_copy (tree type)
4264 {
4265   tree t = copy_node (type);
4266   
4267   TYPE_POINTER_TO (t) = 0;
4268   TYPE_REFERENCE_TO (t) = 0;
4269
4270   /* Set the canonical type either to a new equivalence class, or
4271      propagate the need for structural equality checks. */
4272   if (TYPE_STRUCTURAL_EQUALITY_P (type))
4273     SET_TYPE_STRUCTURAL_EQUALITY (t);
4274   else
4275     TYPE_CANONICAL (t) = t;
4276
4277   /* Make it its own variant.  */
4278   TYPE_MAIN_VARIANT (t) = t;
4279   TYPE_NEXT_VARIANT (t) = 0;
4280
4281   /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4282      whose TREE_TYPE is not t.  This can also happen in the Ada
4283      frontend when using subtypes.  */
4284
4285   return t;
4286 }
4287
4288 /* Create a new variant of TYPE, equivalent but distinct.  This is so
4289    the caller can modify it. TYPE_CANONICAL for the return type will
4290    be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4291    are considered equal by the language itself (or that both types
4292    require structural equality checks). */
4293
4294 tree
4295 build_variant_type_copy (tree type)
4296 {
4297   tree t, m = TYPE_MAIN_VARIANT (type);
4298
4299   t = build_distinct_type_copy (type);
4300
4301   /* Since we're building a variant, assume that it is a non-semantic
4302      variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4303   TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4304   
4305   /* Add the new type to the chain of variants of TYPE.  */
4306   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4307   TYPE_NEXT_VARIANT (m) = t;
4308   TYPE_MAIN_VARIANT (t) = m;
4309
4310   return t;
4311 }
4312 \f
4313 /* Return true if the from tree in both tree maps are equal.  */
4314
4315 int
4316 tree_map_base_eq (const void *va, const void *vb)
4317 {
4318   const struct tree_map_base  *const a = (const struct tree_map_base *) va,
4319     *const b = (const struct tree_map_base *) vb;
4320   return (a->from == b->from);
4321 }
4322
4323 /* Hash a from tree in a tree_map.  */
4324
4325 unsigned int
4326 tree_map_base_hash (const void *item)
4327 {
4328   return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4329 }
4330
4331 /* Return true if this tree map structure is marked for garbage collection
4332    purposes.  We simply return true if the from tree is marked, so that this
4333    structure goes away when the from tree goes away.  */
4334
4335 int
4336 tree_map_base_marked_p (const void *p)
4337 {
4338   return ggc_marked_p (((const struct tree_map_base *) p)->from);
4339 }
4340
4341 unsigned int
4342 tree_map_hash (const void *item)
4343 {
4344   return (((const struct tree_map *) item)->hash);
4345 }
4346
4347 /* Return the initialization priority for DECL.  */
4348
4349 priority_type
4350 decl_init_priority_lookup (tree decl)
4351 {
4352   struct tree_priority_map *h;
4353   struct tree_map_base in;
4354
4355   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4356   in.from = decl;
4357   h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4358   return h ? h->init : DEFAULT_INIT_PRIORITY;
4359 }
4360
4361 /* Return the finalization priority for DECL.  */
4362
4363 priority_type
4364 decl_fini_priority_lookup (tree decl)
4365 {
4366   struct tree_priority_map *h;
4367   struct tree_map_base in;
4368
4369   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4370   in.from = decl;
4371   h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4372   return h ? h->fini : DEFAULT_INIT_PRIORITY;
4373 }
4374
4375 /* Return the initialization and finalization priority information for
4376    DECL.  If there is no previous priority information, a freshly
4377    allocated structure is returned.  */
4378
4379 static struct tree_priority_map *
4380 decl_priority_info (tree decl)
4381 {
4382   struct tree_priority_map in;
4383   struct tree_priority_map *h;
4384   void **loc;
4385
4386   in.base.from = decl;
4387   loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4388   h = (struct tree_priority_map *) *loc;
4389   if (!h)
4390     {
4391       h = GGC_CNEW (struct tree_priority_map);
4392       *loc = h;
4393       h->base.from = decl;
4394       h->init = DEFAULT_INIT_PRIORITY;
4395       h->fini = DEFAULT_INIT_PRIORITY;
4396     }
4397
4398   return h;
4399 }
4400
4401 /* Set the initialization priority for DECL to PRIORITY.  */
4402
4403 void
4404 decl_init_priority_insert (tree decl, priority_type priority)
4405 {
4406   struct tree_priority_map *h;
4407
4408   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4409   h = decl_priority_info (decl);
4410   h->init = priority;
4411 }  
4412
4413 /* Set the finalization priority for DECL to PRIORITY.  */
4414
4415 void
4416 decl_fini_priority_insert (tree decl, priority_type priority)
4417 {
4418   struct tree_priority_map *h;
4419
4420   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4421   h = decl_priority_info (decl);
4422   h->fini = priority;
4423 }  
4424
4425 /* Look up a restrict qualified base decl for FROM.  */
4426
4427 tree
4428 decl_restrict_base_lookup (tree from)
4429 {
4430   struct tree_map *h;
4431   struct tree_map in;
4432
4433   in.base.from = from;
4434   h = (struct tree_map *) htab_find_with_hash (restrict_base_for_decl, &in,
4435                                                htab_hash_pointer (from));
4436   return h ? h->to : NULL_TREE;
4437 }
4438
4439 /* Record the restrict qualified base TO for FROM.  */
4440
4441 void
4442 decl_restrict_base_insert (tree from, tree to)
4443 {
4444   struct tree_map *h;
4445   void **loc;
4446
4447   h = GGC_NEW (struct tree_map);
4448   h->hash = htab_hash_pointer (from);
4449   h->base.from = from;
4450   h->to = to;
4451   loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4452   *(struct tree_map **) loc = h;
4453 }
4454
4455 /* Print out the statistics for the DECL_DEBUG_EXPR hash table.  */
4456
4457 static void
4458 print_debug_expr_statistics (void)
4459 {
4460   fprintf (stderr, "DECL_DEBUG_EXPR  hash: size %ld, %ld elements, %f collisions\n",
4461            (long) htab_size (debug_expr_for_decl),
4462            (long) htab_elements (debug_expr_for_decl),
4463            htab_collisions (debug_expr_for_decl));
4464 }
4465
4466 /* Print out the statistics for the DECL_VALUE_EXPR hash table.  */
4467
4468 static void
4469 print_value_expr_statistics (void)
4470 {
4471   fprintf (stderr, "DECL_VALUE_EXPR  hash: size %ld, %ld elements, %f collisions\n",
4472            (long) htab_size (value_expr_for_decl),
4473            (long) htab_elements (value_expr_for_decl),
4474            htab_collisions (value_expr_for_decl));
4475 }
4476
4477 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4478    don't print anything if the table is empty.  */
4479
4480 static void
4481 print_restrict_base_statistics (void)
4482 {
4483   if (htab_elements (restrict_base_for_decl) != 0)
4484     fprintf (stderr,
4485              "RESTRICT_BASE    hash: size %ld, %ld elements, %f collisions\n",
4486              (long) htab_size (restrict_base_for_decl),
4487              (long) htab_elements (restrict_base_for_decl),
4488              htab_collisions (restrict_base_for_decl));
4489 }
4490
4491 /* Lookup a debug expression for FROM, and return it if we find one.  */
4492
4493 tree 
4494 decl_debug_expr_lookup (tree from)
4495 {
4496   struct tree_map *h, in;
4497   in.base.from = from;
4498
4499   h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4500                                                htab_hash_pointer (from));
4501   if (h)
4502     return h->to;
4503   return NULL_TREE;
4504 }
4505
4506 /* Insert a mapping FROM->TO in the debug expression hashtable.  */
4507
4508 void
4509 decl_debug_expr_insert (tree from, tree to)
4510 {
4511   struct tree_map *h;
4512   void **loc;
4513
4514   h = GGC_NEW (struct tree_map);
4515   h->hash = htab_hash_pointer (from);
4516   h->base.from = from;
4517   h->to = to;
4518   loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4519   *(struct tree_map **) loc = h;
4520 }  
4521
4522 /* Lookup a value expression for FROM, and return it if we find one.  */
4523
4524 tree 
4525 decl_value_expr_lookup (tree from)
4526 {
4527   struct tree_map *h, in;
4528   in.base.from = from;
4529
4530   h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4531                                                htab_hash_pointer (from));
4532   if (h)
4533     return h->to;
4534   return NULL_TREE;
4535 }
4536
4537 /* Insert a mapping FROM->TO in the value expression hashtable.  */
4538
4539 void
4540 decl_value_expr_insert (tree from, tree to)
4541 {
4542   struct tree_map *h;
4543   void **loc;
4544
4545   h = GGC_NEW (struct tree_map);
4546   h->hash = htab_hash_pointer (from);
4547   h->base.from = from;
4548   h->to = to;
4549   loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4550   *(struct tree_map **) loc = h;
4551 }
4552
4553 /* Hashing of types so that we don't make duplicates.
4554    The entry point is `type_hash_canon'.  */
4555
4556 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4557    with types in the TREE_VALUE slots), by adding the hash codes
4558    of the individual types.  */
4559
4560 static unsigned int
4561 type_hash_list (const_tree list, hashval_t hashcode)
4562 {
4563   const_tree tail;
4564
4565   for (tail = list; tail; tail = TREE_CHAIN (tail))
4566     if (TREE_VALUE (tail) != error_mark_node)
4567       hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4568                                         hashcode);
4569
4570   return hashcode;
4571 }
4572
4573 /* These are the Hashtable callback functions.  */
4574
4575 /* Returns true iff the types are equivalent.  */
4576
4577 static int
4578 type_hash_eq (const void *va, const void *vb)
4579 {
4580   const struct type_hash *const a = (const struct type_hash *) va,
4581     *const b = (const struct type_hash *) vb;
4582
4583   /* First test the things that are the same for all types.  */
4584   if (a->hash != b->hash
4585       || TREE_CODE (a->type) != TREE_CODE (b->type)
4586       || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4587       || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4588                                  TYPE_ATTRIBUTES (b->type))
4589       || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4590       || TYPE_MODE (a->type) != TYPE_MODE (b->type)
4591       || (TREE_CODE (a->type) != COMPLEX_TYPE 
4592           && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
4593     return 0;
4594
4595   switch (TREE_CODE (a->type))
4596     {
4597     case VOID_TYPE:
4598     case COMPLEX_TYPE:
4599     case POINTER_TYPE:
4600     case REFERENCE_TYPE:
4601       return 1;
4602
4603     case VECTOR_TYPE:
4604       return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4605
4606     case ENUMERAL_TYPE:
4607       if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4608           && !(TYPE_VALUES (a->type)
4609                && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4610                && TYPE_VALUES (b->type)
4611                && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4612                && type_list_equal (TYPE_VALUES (a->type),
4613                                    TYPE_VALUES (b->type))))
4614         return 0;
4615
4616       /* ... fall through ... */
4617
4618     case INTEGER_TYPE:
4619     case REAL_TYPE:
4620     case BOOLEAN_TYPE:
4621       return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4622                || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4623                                       TYPE_MAX_VALUE (b->type)))
4624               && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4625                   || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4626                                          TYPE_MIN_VALUE (b->type))));
4627
4628     case FIXED_POINT_TYPE:
4629       return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4630
4631     case OFFSET_TYPE:
4632       return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4633
4634     case METHOD_TYPE:
4635       return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4636               && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4637                   || (TYPE_ARG_TYPES (a->type)
4638                       && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4639                       && TYPE_ARG_TYPES (b->type)
4640                       && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4641                       && type_list_equal (TYPE_ARG_TYPES (a->type),
4642                                           TYPE_ARG_TYPES (b->type)))));
4643
4644     case ARRAY_TYPE:
4645       return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4646
4647     case RECORD_TYPE:
4648     case UNION_TYPE:
4649     case QUAL_UNION_TYPE:
4650       return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4651               || (TYPE_FIELDS (a->type)
4652                   && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4653                   && TYPE_FIELDS (b->type)
4654                   && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4655                   && type_list_equal (TYPE_FIELDS (a->type),
4656                                       TYPE_FIELDS (b->type))));
4657
4658     case FUNCTION_TYPE:
4659       if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4660           || (TYPE_ARG_TYPES (a->type)
4661               && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4662               && TYPE_ARG_TYPES (b->type)
4663               && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4664               && type_list_equal (TYPE_ARG_TYPES (a->type),
4665                                   TYPE_ARG_TYPES (b->type))))
4666         break;
4667       return 0;
4668
4669     default:
4670       return 0;
4671     }
4672
4673   if (lang_hooks.types.type_hash_eq != NULL)
4674     return lang_hooks.types.type_hash_eq (a->type, b->type);
4675
4676   return 1;
4677 }
4678
4679 /* Return the cached hash value.  */
4680
4681 static hashval_t
4682 type_hash_hash (const void *item)
4683 {
4684   return ((const struct type_hash *) item)->hash;
4685 }
4686
4687 /* Look in the type hash table for a type isomorphic to TYPE.
4688    If one is found, return it.  Otherwise return 0.  */
4689
4690 tree
4691 type_hash_lookup (hashval_t hashcode, tree type)
4692 {
4693   struct type_hash *h, in;
4694
4695   /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4696      must call that routine before comparing TYPE_ALIGNs.  */
4697   layout_type (type);
4698
4699   in.hash = hashcode;
4700   in.type = type;
4701
4702   h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
4703                                                 hashcode);
4704   if (h)
4705     return h->type;
4706   return NULL_TREE;
4707 }
4708
4709 /* Add an entry to the type-hash-table
4710    for a type TYPE whose hash code is HASHCODE.  */
4711
4712 void
4713 type_hash_add (hashval_t hashcode, tree type)
4714 {
4715   struct type_hash *h;
4716   void **loc;
4717
4718   h = GGC_NEW (struct type_hash);
4719   h->hash = hashcode;
4720   h->type = type;
4721   loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4722   *loc = (void *)h;
4723 }
4724
4725 /* Given TYPE, and HASHCODE its hash code, return the canonical
4726    object for an identical type if one already exists.
4727    Otherwise, return TYPE, and record it as the canonical object.
4728
4729    To use this function, first create a type of the sort you want.
4730    Then compute its hash code from the fields of the type that
4731    make it different from other similar types.
4732    Then call this function and use the value.  */
4733
4734 tree
4735 type_hash_canon (unsigned int hashcode, tree type)
4736 {
4737   tree t1;
4738
4739   /* The hash table only contains main variants, so ensure that's what we're
4740      being passed.  */
4741   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4742
4743   if (!lang_hooks.types.hash_types)
4744     return type;
4745
4746   /* See if the type is in the hash table already.  If so, return it.
4747      Otherwise, add the type.  */
4748   t1 = type_hash_lookup (hashcode, type);
4749   if (t1 != 0)
4750     {
4751 #ifdef GATHER_STATISTICS
4752       tree_node_counts[(int) t_kind]--;
4753       tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4754 #endif
4755       return t1;
4756     }
4757   else
4758     {
4759       type_hash_add (hashcode, type);
4760       return type;
4761     }
4762 }
4763
4764 /* See if the data pointed to by the type hash table is marked.  We consider
4765    it marked if the type is marked or if a debug type number or symbol
4766    table entry has been made for the type.  This reduces the amount of
4767    debugging output and eliminates that dependency of the debug output on
4768    the number of garbage collections.  */
4769
4770 static int
4771 type_hash_marked_p (const void *p)
4772 {
4773   const_tree const type = ((const struct type_hash *) p)->type;
4774
4775   return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4776 }
4777
4778 static void
4779 print_type_hash_statistics (void)
4780 {
4781   fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4782            (long) htab_size (type_hash_table),
4783            (long) htab_elements (type_hash_table),
4784            htab_collisions (type_hash_table));
4785 }
4786
4787 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4788    with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4789    by adding the hash codes of the individual attributes.  */
4790
4791 static unsigned int
4792 attribute_hash_list (const_tree list, hashval_t hashcode)
4793 {
4794   const_tree tail;
4795
4796   for (tail = list; tail; tail = TREE_CHAIN (tail))
4797     /* ??? Do we want to add in TREE_VALUE too? */
4798     hashcode = iterative_hash_object
4799       (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4800   return hashcode;
4801 }
4802
4803 /* Given two lists of attributes, return true if list l2 is
4804    equivalent to l1.  */
4805
4806 int
4807 attribute_list_equal (const_tree l1, const_tree l2)
4808 {
4809   return attribute_list_contained (l1, l2)
4810          && attribute_list_contained (l2, l1);
4811 }
4812
4813 /* Given two lists of attributes, return true if list L2 is
4814    completely contained within L1.  */
4815 /* ??? This would be faster if attribute names were stored in a canonicalized
4816    form.  Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4817    must be used to show these elements are equivalent (which they are).  */
4818 /* ??? It's not clear that attributes with arguments will always be handled
4819    correctly.  */
4820
4821 int
4822 attribute_list_contained (const_tree l1, const_tree l2)
4823 {
4824   const_tree t1, t2;
4825
4826   /* First check the obvious, maybe the lists are identical.  */
4827   if (l1 == l2)
4828     return 1;
4829
4830   /* Maybe the lists are similar.  */
4831   for (t1 = l1, t2 = l2;
4832        t1 != 0 && t2 != 0
4833         && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4834         && TREE_VALUE (t1) == TREE_VALUE (t2);
4835        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4836
4837   /* Maybe the lists are equal.  */
4838   if (t1 == 0 && t2 == 0)
4839     return 1;
4840
4841   for (; t2 != 0; t2 = TREE_CHAIN (t2))
4842     {
4843       const_tree attr;
4844       /* This CONST_CAST is okay because lookup_attribute does not
4845          modify its argument and the return value is assigned to a
4846          const_tree.  */
4847       for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4848                                     CONST_CAST_TREE(l1));
4849            attr != NULL_TREE;
4850            attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4851                                     TREE_CHAIN (attr)))
4852         {
4853           if (TREE_VALUE (t2) != NULL
4854               && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4855               && TREE_VALUE (attr) != NULL
4856               && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4857             {
4858               if (simple_cst_list_equal (TREE_VALUE (t2),
4859                                          TREE_VALUE (attr)) == 1)
4860                 break;
4861             }
4862           else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4863             break;
4864         }
4865
4866       if (attr == 0)
4867         return 0;
4868     }
4869
4870   return 1;
4871 }
4872
4873 /* Given two lists of types
4874    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4875    return 1 if the lists contain the same types in the same order.
4876    Also, the TREE_PURPOSEs must match.  */
4877
4878 int
4879 type_list_equal (const_tree l1, const_tree l2)
4880 {
4881   const_tree t1, t2;
4882
4883   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4884     if (TREE_VALUE (t1) != TREE_VALUE (t2)
4885         || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4886             && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4887                   && (TREE_TYPE (TREE_PURPOSE (t1))
4888                       == TREE_TYPE (TREE_PURPOSE (t2))))))
4889       return 0;
4890
4891   return t1 == t2;
4892 }
4893
4894 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4895    given by TYPE.  If the argument list accepts variable arguments,
4896    then this function counts only the ordinary arguments.  */
4897
4898 int
4899 type_num_arguments (const_tree type)
4900 {
4901   int i = 0;
4902   tree t;
4903
4904   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4905     /* If the function does not take a variable number of arguments,
4906        the last element in the list will have type `void'.  */
4907     if (VOID_TYPE_P (TREE_VALUE (t)))
4908       break;
4909     else
4910       ++i;
4911
4912   return i;
4913 }
4914
4915 /* Nonzero if integer constants T1 and T2
4916    represent the same constant value.  */
4917
4918 int
4919 tree_int_cst_equal (const_tree t1, const_tree t2)
4920 {
4921   if (t1 == t2)
4922     return 1;
4923
4924   if (t1 == 0 || t2 == 0)
4925     return 0;
4926
4927   if (TREE_CODE (t1) == INTEGER_CST
4928       && TREE_CODE (t2) == INTEGER_CST
4929       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4930       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4931     return 1;
4932
4933   return 0;
4934 }
4935
4936 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4937    The precise way of comparison depends on their data type.  */
4938
4939 int
4940 tree_int_cst_lt (const_tree t1, const_tree t2)
4941 {
4942   if (t1 == t2)
4943     return 0;
4944
4945   if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4946     {
4947       int t1_sgn = tree_int_cst_sgn (t1);
4948       int t2_sgn = tree_int_cst_sgn (t2);
4949
4950       if (t1_sgn < t2_sgn)
4951         return 1;
4952       else if (t1_sgn > t2_sgn)
4953         return 0;
4954       /* Otherwise, both are non-negative, so we compare them as
4955          unsigned just in case one of them would overflow a signed
4956          type.  */
4957     }
4958   else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4959     return INT_CST_LT (t1, t2);
4960
4961   return INT_CST_LT_UNSIGNED (t1, t2);
4962 }
4963
4964 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2.  */
4965
4966 int
4967 tree_int_cst_compare (const_tree t1, const_tree t2)
4968 {
4969   if (tree_int_cst_lt (t1, t2))
4970     return -1;
4971   else if (tree_int_cst_lt (t2, t1))
4972     return 1;
4973   else
4974     return 0;
4975 }
4976
4977 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4978    the host.  If POS is zero, the value can be represented in a single
4979    HOST_WIDE_INT.  If POS is nonzero, the value must be non-negative and can
4980    be represented in a single unsigned HOST_WIDE_INT.  */
4981
4982 int
4983 host_integerp (const_tree t, int pos)
4984 {
4985   return (TREE_CODE (t) == INTEGER_CST
4986           && ((TREE_INT_CST_HIGH (t) == 0
4987                && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4988               || (! pos && TREE_INT_CST_HIGH (t) == -1
4989                   && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4990                   && (!TYPE_UNSIGNED (TREE_TYPE (t))
4991                       || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4992                           && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
4993               || (pos && TREE_INT_CST_HIGH (t) == 0)));
4994 }
4995
4996 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4997    INTEGER_CST and there is no overflow.  POS is nonzero if the result must
4998    be non-negative.  We must be able to satisfy the above conditions.  */
4999
5000 HOST_WIDE_INT
5001 tree_low_cst (const_tree t, int pos)
5002 {
5003   gcc_assert (host_integerp (t, pos));
5004   return TREE_INT_CST_LOW (t);
5005 }
5006
5007 /* Return the most significant bit of the integer constant T.  */
5008
5009 int
5010 tree_int_cst_msb (const_tree t)
5011 {
5012   int prec;
5013   HOST_WIDE_INT h;
5014   unsigned HOST_WIDE_INT l;
5015
5016   /* Note that using TYPE_PRECISION here is wrong.  We care about the
5017      actual bits, not the (arbitrary) range of the type.  */
5018   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5019   rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5020                  2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5021   return (l & 1) == 1;
5022 }
5023
5024 /* Return an indication of the sign of the integer constant T.
5025    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5026    Note that -1 will never be returned if T's type is unsigned.  */
5027
5028 int
5029 tree_int_cst_sgn (const_tree t)
5030 {
5031   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5032     return 0;
5033   else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5034     return 1;
5035   else if (TREE_INT_CST_HIGH (t) < 0)
5036     return -1;
5037   else
5038     return 1;
5039 }
5040
5041 /* Return the minimum number of bits needed to represent VALUE in a
5042    signed or unsigned type, UNSIGNEDP says which.  */
5043
5044 unsigned int
5045 tree_int_cst_min_precision (tree value, bool unsignedp)
5046 {
5047   int log;
5048
5049   /* If the value is negative, compute its negative minus 1.  The latter
5050      adjustment is because the absolute value of the largest negative value
5051      is one larger than the largest positive value.  This is equivalent to
5052      a bit-wise negation, so use that operation instead.  */
5053
5054   if (tree_int_cst_sgn (value) < 0)
5055     value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
5056
5057   /* Return the number of bits needed, taking into account the fact
5058      that we need one more bit for a signed than unsigned type.  */
5059
5060   if (integer_zerop (value))
5061     log = 0;
5062   else
5063     log = tree_floor_log2 (value);
5064
5065   return log + 1 + !unsignedp;
5066 }
5067
5068 /* Compare two constructor-element-type constants.  Return 1 if the lists
5069    are known to be equal; otherwise return 0.  */
5070
5071 int
5072 simple_cst_list_equal (const_tree l1, const_tree l2)
5073 {
5074   while (l1 != NULL_TREE && l2 != NULL_TREE)
5075     {
5076       if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5077         return 0;
5078
5079       l1 = TREE_CHAIN (l1);
5080       l2 = TREE_CHAIN (l2);
5081     }
5082
5083   return l1 == l2;
5084 }
5085
5086 /* Return truthvalue of whether T1 is the same tree structure as T2.
5087    Return 1 if they are the same.
5088    Return 0 if they are understandably different.
5089    Return -1 if either contains tree structure not understood by
5090    this function.  */
5091
5092 int
5093 simple_cst_equal (const_tree t1, const_tree t2)
5094 {
5095   enum tree_code code1, code2;
5096   int cmp;
5097   int i;
5098
5099   if (t1 == t2)
5100     return 1;
5101   if (t1 == 0 || t2 == 0)
5102     return 0;
5103
5104   code1 = TREE_CODE (t1);
5105   code2 = TREE_CODE (t2);
5106
5107   if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
5108     {
5109       if (CONVERT_EXPR_CODE_P (code2)
5110           || code2 == NON_LVALUE_EXPR)
5111         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5112       else
5113         return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5114     }
5115
5116   else if (CONVERT_EXPR_CODE_P (code2)
5117            || code2 == NON_LVALUE_EXPR)
5118     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5119
5120   if (code1 != code2)
5121     return 0;
5122
5123   switch (code1)
5124     {
5125     case INTEGER_CST:
5126       return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5127               && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5128
5129     case REAL_CST:
5130       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5131
5132     case FIXED_CST:
5133       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5134
5135     case STRING_CST:
5136       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5137               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5138                          TREE_STRING_LENGTH (t1)));
5139
5140     case CONSTRUCTOR:
5141       {
5142         unsigned HOST_WIDE_INT idx;
5143         VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5144         VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5145
5146         if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5147           return false;
5148
5149         for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5150           /* ??? Should we handle also fields here? */
5151           if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5152                                  VEC_index (constructor_elt, v2, idx)->value))
5153             return false;
5154         return true;
5155       }
5156
5157     case SAVE_EXPR:
5158       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5159
5160     case CALL_EXPR:
5161       cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5162       if (cmp <= 0)
5163         return cmp;
5164       if (call_expr_nargs (t1) != call_expr_nargs (t2))
5165         return 0;
5166       {
5167         const_tree arg1, arg2;
5168         const_call_expr_arg_iterator iter1, iter2;
5169         for (arg1 = first_const_call_expr_arg (t1, &iter1),
5170                arg2 = first_const_call_expr_arg (t2, &iter2);
5171              arg1 && arg2;
5172              arg1 = next_const_call_expr_arg (&iter1),
5173                arg2 = next_const_call_expr_arg (&iter2))
5174           {
5175             cmp = simple_cst_equal (arg1, arg2);
5176             if (cmp <= 0)
5177               return cmp;
5178           }
5179         return arg1 == arg2;
5180       }
5181
5182     case TARGET_EXPR:
5183       /* Special case: if either target is an unallocated VAR_DECL,
5184          it means that it's going to be unified with whatever the
5185          TARGET_EXPR is really supposed to initialize, so treat it
5186          as being equivalent to anything.  */
5187       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5188            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5189            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5190           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5191               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5192               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5193         cmp = 1;
5194       else
5195         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5196
5197       if (cmp <= 0)
5198         return cmp;
5199
5200       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5201
5202     case WITH_CLEANUP_EXPR:
5203       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5204       if (cmp <= 0)
5205         return cmp;
5206
5207       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5208
5209     case COMPONENT_REF:
5210       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5211         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5212
5213       return 0;
5214
5215     case VAR_DECL:
5216     case PARM_DECL:
5217     case CONST_DECL:
5218     case FUNCTION_DECL:
5219       return 0;
5220
5221     default:
5222       break;
5223     }
5224
5225   /* This general rule works for most tree codes.  All exceptions should be
5226      handled above.  If this is a language-specific tree code, we can't
5227      trust what might be in the operand, so say we don't know
5228      the situation.  */
5229   if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5230     return -1;
5231
5232   switch (TREE_CODE_CLASS (code1))
5233     {
5234     case tcc_unary:
5235     case tcc_binary:
5236     case tcc_comparison:
5237     case tcc_expression:
5238     case tcc_reference:
5239     case tcc_statement:
5240       cmp = 1;
5241       for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5242         {
5243           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5244           if (cmp <= 0)
5245             return cmp;
5246         }
5247
5248       return cmp;
5249
5250     default:
5251       return -1;
5252     }
5253 }
5254
5255 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5256    Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5257    than U, respectively.  */
5258
5259 int
5260 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5261 {
5262   if (tree_int_cst_sgn (t) < 0)
5263     return -1;
5264   else if (TREE_INT_CST_HIGH (t) != 0)
5265     return 1;
5266   else if (TREE_INT_CST_LOW (t) == u)
5267     return 0;
5268   else if (TREE_INT_CST_LOW (t) < u)
5269     return -1;
5270   else
5271     return 1;
5272 }
5273
5274 /* Return true if CODE represents an associative tree code.  Otherwise
5275    return false.  */
5276 bool
5277 associative_tree_code (enum tree_code code)
5278 {
5279   switch (code)
5280     {
5281     case BIT_IOR_EXPR:
5282     case BIT_AND_EXPR:
5283     case BIT_XOR_EXPR:
5284     case PLUS_EXPR:
5285     case MULT_EXPR:
5286     case MIN_EXPR:
5287     case MAX_EXPR:
5288       return true;
5289
5290     default:
5291       break;
5292     }
5293   return false;
5294 }
5295
5296 /* Return true if CODE represents a commutative tree code.  Otherwise
5297    return false.  */
5298 bool
5299 commutative_tree_code (enum tree_code code)
5300 {
5301   switch (code)
5302     {
5303     case PLUS_EXPR:
5304     case MULT_EXPR:
5305     case MIN_EXPR:
5306     case MAX_EXPR:
5307     case BIT_IOR_EXPR:
5308     case BIT_XOR_EXPR:
5309     case BIT_AND_EXPR:
5310     case NE_EXPR:
5311     case EQ_EXPR:
5312     case UNORDERED_EXPR:
5313     case ORDERED_EXPR:
5314     case UNEQ_EXPR:
5315     case LTGT_EXPR:
5316     case TRUTH_AND_EXPR:
5317     case TRUTH_XOR_EXPR:
5318     case TRUTH_OR_EXPR:
5319       return true;
5320
5321     default:
5322       break;
5323     }
5324   return false;
5325 }
5326
5327 /* Generate a hash value for an expression.  This can be used iteratively
5328    by passing a previous result as the VAL argument.
5329
5330    This function is intended to produce the same hash for expressions which
5331    would compare equal using operand_equal_p.  */
5332
5333 hashval_t
5334 iterative_hash_expr (const_tree t, hashval_t val)
5335 {
5336   int i;
5337   enum tree_code code;
5338   char tclass;
5339
5340   if (t == NULL_TREE)
5341     return iterative_hash_pointer (t, val);
5342
5343   code = TREE_CODE (t);
5344
5345   switch (code)
5346     {
5347     /* Alas, constants aren't shared, so we can't rely on pointer
5348        identity.  */
5349     case INTEGER_CST:
5350       val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5351       return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5352     case REAL_CST:
5353       {
5354         unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5355
5356         return iterative_hash_hashval_t (val2, val);
5357       }
5358     case FIXED_CST:
5359       {
5360         unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5361
5362         return iterative_hash_hashval_t (val2, val);
5363       }
5364     case STRING_CST:
5365       return iterative_hash (TREE_STRING_POINTER (t),
5366                              TREE_STRING_LENGTH (t), val);
5367     case COMPLEX_CST:
5368       val = iterative_hash_expr (TREE_REALPART (t), val);
5369       return iterative_hash_expr (TREE_IMAGPART (t), val);
5370     case VECTOR_CST:
5371       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5372
5373     case SSA_NAME:
5374       /* we can just compare by pointer.  */
5375       return iterative_hash_pointer (t, val);
5376
5377     case TREE_LIST:
5378       /* A list of expressions, for a CALL_EXPR or as the elements of a
5379          VECTOR_CST.  */
5380       for (; t; t = TREE_CHAIN (t))
5381         val = iterative_hash_expr (TREE_VALUE (t), val);
5382       return val;
5383     case CONSTRUCTOR:
5384       {
5385         unsigned HOST_WIDE_INT idx;
5386         tree field, value;
5387         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5388           {
5389             val = iterative_hash_expr (field, val);
5390             val = iterative_hash_expr (value, val);
5391           }
5392         return val;
5393       }
5394     case FUNCTION_DECL:
5395       /* When referring to a built-in FUNCTION_DECL, use the
5396          __builtin__ form.  Otherwise nodes that compare equal
5397          according to operand_equal_p might get different
5398          hash codes.  */
5399       if (DECL_BUILT_IN (t))
5400         {
5401           val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)], 
5402                                       val);
5403           return val;
5404         }
5405       /* else FALL THROUGH */
5406     default:
5407       tclass = TREE_CODE_CLASS (code);
5408
5409       if (tclass == tcc_declaration)
5410         {
5411           /* DECL's have a unique ID */
5412           val = iterative_hash_host_wide_int (DECL_UID (t), val);
5413         }
5414       else
5415         {
5416           gcc_assert (IS_EXPR_CODE_CLASS (tclass));
5417           
5418           val = iterative_hash_object (code, val);
5419
5420           /* Don't hash the type, that can lead to having nodes which
5421              compare equal according to operand_equal_p, but which
5422              have different hash codes.  */
5423           if (CONVERT_EXPR_CODE_P (code)
5424               || code == NON_LVALUE_EXPR)
5425             {
5426               /* Make sure to include signness in the hash computation.  */
5427               val += TYPE_UNSIGNED (TREE_TYPE (t));
5428               val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5429             }
5430
5431           else if (commutative_tree_code (code))
5432             {
5433               /* It's a commutative expression.  We want to hash it the same
5434                  however it appears.  We do this by first hashing both operands
5435                  and then rehashing based on the order of their independent
5436                  hashes.  */
5437               hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5438               hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5439               hashval_t t;
5440
5441               if (one > two)
5442                 t = one, one = two, two = t;
5443
5444               val = iterative_hash_hashval_t (one, val);
5445               val = iterative_hash_hashval_t (two, val);
5446             }
5447           else
5448             for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5449               val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5450         }
5451       return val;
5452       break;
5453     }
5454 }
5455
5456 /* Generate a hash value for a pair of expressions.  This can be used
5457    iteratively by passing a previous result as the VAL argument.
5458
5459    The same hash value is always returned for a given pair of expressions,
5460    regardless of the order in which they are presented.  This is useful in
5461    hashing the operands of commutative functions.  */
5462
5463 hashval_t
5464 iterative_hash_exprs_commutative (const_tree t1,
5465                                   const_tree t2, hashval_t val)
5466 {
5467   hashval_t one = iterative_hash_expr (t1, 0);
5468   hashval_t two = iterative_hash_expr (t2, 0);
5469   hashval_t t;
5470
5471   if (one > two)
5472     t = one, one = two, two = t;
5473   val = iterative_hash_hashval_t (one, val);
5474   val = iterative_hash_hashval_t (two, val);
5475
5476   return val;
5477 }
5478 \f
5479 /* Constructors for pointer, array and function types.
5480    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5481    constructed by language-dependent code, not here.)  */
5482
5483 /* Construct, lay out and return the type of pointers to TO_TYPE with
5484    mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
5485    reference all of memory. If such a type has already been
5486    constructed, reuse it.  */
5487
5488 tree
5489 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5490                              bool can_alias_all)
5491 {
5492   tree t;
5493
5494   if (to_type == error_mark_node)
5495     return error_mark_node;
5496
5497   /* If the pointed-to type has the may_alias attribute set, force
5498      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
5499   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5500     can_alias_all = true;
5501
5502   /* In some cases, languages will have things that aren't a POINTER_TYPE
5503      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5504      In that case, return that type without regard to the rest of our
5505      operands.
5506
5507      ??? This is a kludge, but consistent with the way this function has
5508      always operated and there doesn't seem to be a good way to avoid this
5509      at the moment.  */
5510   if (TYPE_POINTER_TO (to_type) != 0
5511       && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5512     return TYPE_POINTER_TO (to_type);
5513
5514   /* First, if we already have a type for pointers to TO_TYPE and it's
5515      the proper mode, use it.  */
5516   for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5517     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5518       return t;
5519
5520   t = make_node (POINTER_TYPE);
5521
5522   TREE_TYPE (t) = to_type;
5523   SET_TYPE_MODE (t, mode);
5524   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5525   TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5526   TYPE_POINTER_TO (to_type) = t;
5527
5528   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5529     SET_TYPE_STRUCTURAL_EQUALITY (t);
5530   else if (TYPE_CANONICAL (to_type) != to_type)
5531     TYPE_CANONICAL (t)
5532       = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5533                                      mode, can_alias_all);
5534
5535   /* Lay out the type.  This function has many callers that are concerned
5536      with expression-construction, and this simplifies them all.  */
5537   layout_type (t);
5538
5539   return t;
5540 }
5541
5542 /* By default build pointers in ptr_mode.  */
5543
5544 tree
5545 build_pointer_type (tree to_type)
5546 {
5547   return build_pointer_type_for_mode (to_type, ptr_mode, false);
5548 }
5549
5550 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
5551
5552 tree
5553 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5554                                bool can_alias_all)
5555 {
5556   tree t;
5557
5558   if (to_type == error_mark_node)
5559     return error_mark_node;
5560
5561   /* If the pointed-to type has the may_alias attribute set, force
5562      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
5563   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5564     can_alias_all = true;
5565
5566   /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5567      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5568      In that case, return that type without regard to the rest of our
5569      operands.
5570
5571      ??? This is a kludge, but consistent with the way this function has
5572      always operated and there doesn't seem to be a good way to avoid this
5573      at the moment.  */
5574   if (TYPE_REFERENCE_TO (to_type) != 0
5575       && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5576     return TYPE_REFERENCE_TO (to_type);
5577
5578   /* First, if we already have a type for pointers to TO_TYPE and it's
5579      the proper mode, use it.  */
5580   for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5581     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5582       return t;
5583
5584   t = make_node (REFERENCE_TYPE);
5585
5586   TREE_TYPE (t) = to_type;
5587   SET_TYPE_MODE (t, mode);
5588   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5589   TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5590   TYPE_REFERENCE_TO (to_type) = t;
5591
5592   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5593     SET_TYPE_STRUCTURAL_EQUALITY (t);
5594   else if (TYPE_CANONICAL (to_type) != to_type)
5595     TYPE_CANONICAL (t) 
5596       = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5597                                        mode, can_alias_all);
5598
5599   layout_type (t);
5600
5601   return t;
5602 }
5603
5604
5605 /* Build the node for the type of references-to-TO_TYPE by default
5606    in ptr_mode.  */
5607
5608 tree
5609 build_reference_type (tree to_type)
5610 {
5611   return build_reference_type_for_mode (to_type, ptr_mode, false);
5612 }
5613
5614 /* Build a type that is compatible with t but has no cv quals anywhere
5615    in its type, thus
5616
5617    const char *const *const *  ->  char ***.  */
5618
5619 tree
5620 build_type_no_quals (tree t)
5621 {
5622   switch (TREE_CODE (t))
5623     {
5624     case POINTER_TYPE:
5625       return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5626                                           TYPE_MODE (t),
5627                                           TYPE_REF_CAN_ALIAS_ALL (t));
5628     case REFERENCE_TYPE:
5629       return
5630         build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5631                                        TYPE_MODE (t),
5632                                        TYPE_REF_CAN_ALIAS_ALL (t));
5633     default:
5634       return TYPE_MAIN_VARIANT (t);
5635     }
5636 }
5637
5638 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5639    MAXVAL should be the maximum value in the domain
5640    (one less than the length of the array).
5641
5642    The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5643    We don't enforce this limit, that is up to caller (e.g. language front end).
5644    The limit exists because the result is a signed type and we don't handle
5645    sizes that use more than one HOST_WIDE_INT.  */
5646
5647 tree
5648 build_index_type (tree maxval)
5649 {
5650   tree itype = make_node (INTEGER_TYPE);
5651
5652   TREE_TYPE (itype) = sizetype;
5653   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5654   TYPE_MIN_VALUE (itype) = size_zero_node;
5655   TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5656   SET_TYPE_MODE (itype, TYPE_MODE (sizetype));
5657   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5658   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5659   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5660   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5661
5662   if (host_integerp (maxval, 1))
5663     return type_hash_canon (tree_low_cst (maxval, 1), itype);
5664   else
5665     {
5666       /* Since we cannot hash this type, we need to compare it using
5667          structural equality checks. */
5668       SET_TYPE_STRUCTURAL_EQUALITY (itype);
5669       return itype;
5670     }
5671 }
5672
5673 /* Builds a signed or unsigned integer type of precision PRECISION.
5674    Used for C bitfields whose precision does not match that of
5675    built-in target types.  */
5676 tree
5677 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5678                                 int unsignedp)
5679 {
5680   tree itype = make_node (INTEGER_TYPE);
5681
5682   TYPE_PRECISION (itype) = precision;
5683
5684   if (unsignedp)
5685     fixup_unsigned_type (itype);
5686   else
5687     fixup_signed_type (itype);
5688
5689   if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5690     return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5691
5692   return itype;
5693 }
5694
5695 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5696    ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5697    high bound HIGHVAL.  If TYPE is NULL, sizetype is used.  */
5698
5699 tree
5700 build_range_type (tree type, tree lowval, tree highval)
5701 {
5702   tree itype = make_node (INTEGER_TYPE);
5703
5704   TREE_TYPE (itype) = type;
5705   if (type == NULL_TREE)
5706     type = sizetype;
5707
5708   TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5709   TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5710
5711   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5712   SET_TYPE_MODE (itype, TYPE_MODE (type));
5713   TYPE_SIZE (itype) = TYPE_SIZE (type);
5714   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5715   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5716   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5717
5718   if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5719     return type_hash_canon (tree_low_cst (highval, 0)
5720                             - tree_low_cst (lowval, 0),
5721                             itype);
5722   else
5723     return itype;
5724 }
5725
5726 /* Just like build_index_type, but takes lowval and highval instead
5727    of just highval (maxval).  */
5728
5729 tree
5730 build_index_2_type (tree lowval, tree highval)
5731 {
5732   return build_range_type (sizetype, lowval, highval);
5733 }
5734
5735 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5736    and number of elements specified by the range of values of INDEX_TYPE.
5737    If such a type has already been constructed, reuse it.  */
5738
5739 tree
5740 build_array_type (tree elt_type, tree index_type)
5741 {
5742   tree t;
5743   hashval_t hashcode = 0;
5744
5745   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5746     {
5747       error ("arrays of functions are not meaningful");
5748       elt_type = integer_type_node;
5749     }
5750
5751   t = make_node (ARRAY_TYPE);
5752   TREE_TYPE (t) = elt_type;
5753   TYPE_DOMAIN (t) = index_type;
5754   
5755   if (index_type == 0)
5756     {
5757       tree save = t;
5758       hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5759       t = type_hash_canon (hashcode, t);
5760       if (save == t)
5761         layout_type (t);
5762
5763       if (TYPE_CANONICAL (t) == t)
5764         {
5765           if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5766             SET_TYPE_STRUCTURAL_EQUALITY (t);
5767           else if (TYPE_CANONICAL (elt_type) != elt_type)
5768             TYPE_CANONICAL (t) 
5769               = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5770         }
5771
5772       return t;
5773     }
5774
5775   hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5776   hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5777   t = type_hash_canon (hashcode, t);
5778
5779   if (!COMPLETE_TYPE_P (t))
5780     layout_type (t);
5781
5782   if (TYPE_CANONICAL (t) == t)
5783     {
5784       if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5785           || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5786         SET_TYPE_STRUCTURAL_EQUALITY (t);
5787       else if (TYPE_CANONICAL (elt_type) != elt_type
5788                || TYPE_CANONICAL (index_type) != index_type)
5789         TYPE_CANONICAL (t) 
5790           = build_array_type (TYPE_CANONICAL (elt_type),
5791                               TYPE_CANONICAL (index_type));
5792     }
5793
5794   return t;
5795 }
5796
5797 /* Recursively examines the array elements of TYPE, until a non-array
5798    element type is found.  */
5799
5800 tree
5801 strip_array_types (tree type)
5802 {
5803   while (TREE_CODE (type) == ARRAY_TYPE)
5804     type = TREE_TYPE (type);
5805
5806   return type;
5807 }
5808
5809 /* Computes the canonical argument types from the argument type list
5810    ARGTYPES. 
5811
5812    Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
5813    on entry to this function, or if any of the ARGTYPES are
5814    structural.
5815
5816    Upon return, *ANY_NONCANONICAL_P will be true iff either it was
5817    true on entry to this function, or if any of the ARGTYPES are
5818    non-canonical.
5819
5820    Returns a canonical argument list, which may be ARGTYPES when the
5821    canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
5822    true) or would not differ from ARGTYPES.  */
5823
5824 static tree 
5825 maybe_canonicalize_argtypes(tree argtypes, 
5826                             bool *any_structural_p,
5827                             bool *any_noncanonical_p)
5828 {
5829   tree arg;
5830   bool any_noncanonical_argtypes_p = false;
5831   
5832   for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
5833     {
5834       if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
5835         /* Fail gracefully by stating that the type is structural.  */
5836         *any_structural_p = true;
5837       else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
5838         *any_structural_p = true;
5839       else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
5840                || TREE_PURPOSE (arg))
5841         /* If the argument has a default argument, we consider it
5842            non-canonical even though the type itself is canonical.
5843            That way, different variants of function and method types
5844            with default arguments will all point to the variant with
5845            no defaults as their canonical type.  */
5846         any_noncanonical_argtypes_p = true;
5847     }
5848
5849   if (*any_structural_p)
5850     return argtypes;
5851
5852   if (any_noncanonical_argtypes_p)
5853     {
5854       /* Build the canonical list of argument types.  */
5855       tree canon_argtypes = NULL_TREE;
5856       bool is_void = false;
5857
5858       for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
5859         {
5860           if (arg == void_list_node)
5861             is_void = true;
5862           else
5863             canon_argtypes = tree_cons (NULL_TREE,
5864                                         TYPE_CANONICAL (TREE_VALUE (arg)),
5865                                         canon_argtypes);
5866         }
5867
5868       canon_argtypes = nreverse (canon_argtypes);
5869       if (is_void)
5870         canon_argtypes = chainon (canon_argtypes, void_list_node);
5871
5872       /* There is a non-canonical type.  */
5873       *any_noncanonical_p = true;
5874       return canon_argtypes;
5875     }
5876
5877   /* The canonical argument types are the same as ARGTYPES.  */
5878   return argtypes;
5879 }
5880
5881 /* Construct, lay out and return
5882    the type of functions returning type VALUE_TYPE
5883    given arguments of types ARG_TYPES.
5884    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5885    are data type nodes for the arguments of the function.
5886    If such a type has already been constructed, reuse it.  */
5887
5888 tree
5889 build_function_type (tree value_type, tree arg_types)
5890 {
5891   tree t;
5892   hashval_t hashcode = 0;
5893   bool any_structural_p, any_noncanonical_p;
5894   tree canon_argtypes;
5895
5896   if (TREE_CODE (value_type) == FUNCTION_TYPE)
5897     {
5898       error ("function return type cannot be function");
5899       value_type = integer_type_node;
5900     }
5901
5902   /* Make a node of the sort we want.  */
5903   t = make_node (FUNCTION_TYPE);
5904   TREE_TYPE (t) = value_type;
5905   TYPE_ARG_TYPES (t) = arg_types;
5906
5907   /* If we already have such a type, use the old one.  */
5908   hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5909   hashcode = type_hash_list (arg_types, hashcode);
5910   t = type_hash_canon (hashcode, t);
5911
5912   /* Set up the canonical type. */
5913   any_structural_p   = TYPE_STRUCTURAL_EQUALITY_P (value_type);
5914   any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
5915   canon_argtypes = maybe_canonicalize_argtypes (arg_types, 
5916                                                 &any_structural_p,
5917                                                 &any_noncanonical_p);
5918   if (any_structural_p)
5919     SET_TYPE_STRUCTURAL_EQUALITY (t);
5920   else if (any_noncanonical_p)
5921     TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
5922                                               canon_argtypes);
5923       
5924   if (!COMPLETE_TYPE_P (t))
5925     layout_type (t);
5926   return t;
5927 }
5928
5929 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.  */
5930
5931 tree
5932 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
5933 {
5934   tree new_type = NULL;
5935   tree args, new_args = NULL, t;
5936   tree new_reversed;
5937   int i = 0;
5938
5939   for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
5940        args = TREE_CHAIN (args), i++)
5941     if (!bitmap_bit_p (args_to_skip, i))
5942       new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
5943
5944   new_reversed = nreverse (new_args);
5945   if (args)
5946     {
5947       if (new_reversed)
5948         TREE_CHAIN (new_args) = void_list_node;
5949       else
5950         new_reversed = void_list_node;
5951     }
5952     gcc_assert (new_reversed);
5953
5954   /* Use copy_node to preserve as much as possible from original type
5955      (debug info, attribute lists etc.)
5956      Exception is METHOD_TYPEs must have THIS argument.
5957      When we are asked to remove it, we need to build new FUNCTION_TYPE
5958      instead.  */
5959   if (TREE_CODE (orig_type) != METHOD_TYPE
5960       || !bitmap_bit_p (args_to_skip, 0))
5961     {
5962       new_type = copy_node (orig_type);
5963       TYPE_ARG_TYPES (new_type) = new_reversed;
5964     }
5965   else
5966     {
5967       new_type
5968         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
5969                                                          new_reversed));
5970       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
5971     }
5972
5973   /* This is a new type, not a copy of an old type.  Need to reassociate
5974      variants.  We can handle everything except the main variant lazily.  */
5975   t = TYPE_MAIN_VARIANT (orig_type);
5976   if (orig_type != t)
5977     {
5978       TYPE_MAIN_VARIANT (new_type) = t;
5979       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
5980       TYPE_NEXT_VARIANT (t) = new_type;
5981     }
5982   else
5983     {
5984       TYPE_MAIN_VARIANT (new_type) = new_type;
5985       TYPE_NEXT_VARIANT (new_type) = NULL;
5986     }
5987   return new_type;
5988 }
5989
5990 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.  
5991   
5992    Arguments from DECL_ARGUMENTS list can't be removed now, since they are
5993    linked by TREE_CHAIN directly.  It is caller responsibility to eliminate
5994    them when they are being duplicated (i.e. copy_arguments_for_versioning).  */
5995
5996 tree
5997 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
5998 {
5999   tree new_decl = copy_node (orig_decl);
6000   tree new_type;
6001
6002   new_type = TREE_TYPE (orig_decl);
6003   if (prototype_p (new_type))
6004     new_type = build_function_type_skip_args (new_type, args_to_skip);
6005   TREE_TYPE (new_decl) = new_type;
6006
6007   /* For declarations setting DECL_VINDEX (i.e. methods)
6008      we expect first argument to be THIS pointer.   */
6009   if (bitmap_bit_p (args_to_skip, 0))
6010     DECL_VINDEX (new_decl) = NULL_TREE;
6011   return new_decl;
6012 }
6013
6014 /* Build a function type.  The RETURN_TYPE is the type returned by the
6015    function. If VAARGS is set, no void_type_node is appended to the
6016    the list. ARGP muse be alway be terminated be a NULL_TREE.  */
6017
6018 static tree
6019 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
6020 {
6021   tree t, args, last;
6022
6023   t = va_arg (argp, tree);
6024   for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
6025     args = tree_cons (NULL_TREE, t, args);
6026
6027   if (vaargs)
6028     {
6029       last = args;
6030       if (args != NULL_TREE)
6031         args = nreverse (args);
6032       gcc_assert (args != NULL_TREE && last != void_list_node);
6033     }
6034   else if (args == NULL_TREE)
6035     args = void_list_node;
6036   else
6037     {
6038       last = args;
6039       args = nreverse (args);
6040       TREE_CHAIN (last) = void_list_node;
6041     }
6042   args = build_function_type (return_type, args);
6043
6044   return args;
6045 }
6046
6047 /* Build a function type.  The RETURN_TYPE is the type returned by the
6048    function.  If additional arguments are provided, they are
6049    additional argument types.  The list of argument types must always
6050    be terminated by NULL_TREE.  */
6051
6052 tree
6053 build_function_type_list (tree return_type, ...)
6054 {
6055   tree args;
6056   va_list p;
6057
6058   va_start (p, return_type);
6059   args = build_function_type_list_1 (false, return_type, p);
6060   va_end (p);
6061   return args;
6062 }
6063
6064 /* Build a variable argument function type.  The RETURN_TYPE is the
6065    type returned by the function.  If additional arguments are provided,
6066    they are additional argument types.  The list of argument types must
6067    always be terminated by NULL_TREE.  */
6068
6069 tree
6070 build_varargs_function_type_list (tree return_type, ...)
6071 {
6072   tree args;
6073   va_list p;
6074
6075   va_start (p, return_type);
6076   args = build_function_type_list_1 (true, return_type, p);
6077   va_end (p);
6078
6079   return args;
6080 }
6081
6082 /* Build a METHOD_TYPE for a member of BASETYPE.  The RETTYPE (a TYPE)
6083    and ARGTYPES (a TREE_LIST) are the return type and arguments types
6084    for the method.  An implicit additional parameter (of type
6085    pointer-to-BASETYPE) is added to the ARGTYPES.  */
6086
6087 tree
6088 build_method_type_directly (tree basetype,
6089                             tree rettype,
6090                             tree argtypes)
6091 {
6092   tree t;
6093   tree ptype;
6094   int hashcode = 0;
6095   bool any_structural_p, any_noncanonical_p;
6096   tree canon_argtypes;
6097
6098   /* Make a node of the sort we want.  */
6099   t = make_node (METHOD_TYPE);
6100
6101   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6102   TREE_TYPE (t) = rettype;
6103   ptype = build_pointer_type (basetype);
6104
6105   /* The actual arglist for this function includes a "hidden" argument
6106      which is "this".  Put it into the list of argument types.  */
6107   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
6108   TYPE_ARG_TYPES (t) = argtypes;
6109
6110   /* If we already have such a type, use the old one.  */
6111   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6112   hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
6113   hashcode = type_hash_list (argtypes, hashcode);
6114   t = type_hash_canon (hashcode, t);
6115
6116   /* Set up the canonical type. */
6117   any_structural_p
6118     = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6119        || TYPE_STRUCTURAL_EQUALITY_P (rettype));
6120   any_noncanonical_p
6121     = (TYPE_CANONICAL (basetype) != basetype
6122        || TYPE_CANONICAL (rettype) != rettype);
6123   canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
6124                                                 &any_structural_p,
6125                                                 &any_noncanonical_p);
6126   if (any_structural_p)
6127     SET_TYPE_STRUCTURAL_EQUALITY (t);
6128   else if (any_noncanonical_p)
6129     TYPE_CANONICAL (t) 
6130       = build_method_type_directly (TYPE_CANONICAL (basetype),
6131                                     TYPE_CANONICAL (rettype),
6132                                     canon_argtypes);
6133   if (!COMPLETE_TYPE_P (t))
6134     layout_type (t);
6135
6136   return t;
6137 }
6138
6139 /* Construct, lay out and return the type of methods belonging to class
6140    BASETYPE and whose arguments and values are described by TYPE.
6141    If that type exists already, reuse it.
6142    TYPE must be a FUNCTION_TYPE node.  */
6143
6144 tree
6145 build_method_type (tree basetype, tree type)
6146 {
6147   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
6148
6149   return build_method_type_directly (basetype,
6150                                      TREE_TYPE (type),
6151                                      TYPE_ARG_TYPES (type));
6152 }
6153
6154 /* Construct, lay out and return the type of offsets to a value
6155    of type TYPE, within an object of type BASETYPE.
6156    If a suitable offset type exists already, reuse it.  */
6157
6158 tree
6159 build_offset_type (tree basetype, tree type)
6160 {
6161   tree t;
6162   hashval_t hashcode = 0;
6163
6164   /* Make a node of the sort we want.  */
6165   t = make_node (OFFSET_TYPE);
6166
6167   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6168   TREE_TYPE (t) = type;
6169
6170   /* If we already have such a type, use the old one.  */
6171   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6172   hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
6173   t = type_hash_canon (hashcode, t);
6174
6175   if (!COMPLETE_TYPE_P (t))
6176     layout_type (t);
6177
6178   if (TYPE_CANONICAL (t) == t)
6179     {
6180       if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6181           || TYPE_STRUCTURAL_EQUALITY_P (type))
6182         SET_TYPE_STRUCTURAL_EQUALITY (t);
6183       else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
6184                || TYPE_CANONICAL (type) != type)
6185         TYPE_CANONICAL (t) 
6186           = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6187                                TYPE_CANONICAL (type));
6188     }
6189
6190   return t;
6191 }
6192
6193 /* Create a complex type whose components are COMPONENT_TYPE.  */
6194
6195 tree
6196 build_complex_type (tree component_type)
6197 {
6198   tree t;
6199   hashval_t hashcode;
6200
6201   gcc_assert (INTEGRAL_TYPE_P (component_type)
6202               || SCALAR_FLOAT_TYPE_P (component_type)
6203               || FIXED_POINT_TYPE_P (component_type));
6204
6205   /* Make a node of the sort we want.  */
6206   t = make_node (COMPLEX_TYPE);
6207
6208   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6209
6210   /* If we already have such a type, use the old one.  */
6211   hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6212   t = type_hash_canon (hashcode, t);
6213
6214   if (!COMPLETE_TYPE_P (t))
6215     layout_type (t);
6216
6217   if (TYPE_CANONICAL (t) == t)
6218     {
6219       if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6220         SET_TYPE_STRUCTURAL_EQUALITY (t);
6221       else if (TYPE_CANONICAL (component_type) != component_type)
6222         TYPE_CANONICAL (t) 
6223           = build_complex_type (TYPE_CANONICAL (component_type));
6224     }
6225
6226   /* We need to create a name, since complex is a fundamental type.  */
6227   if (! TYPE_NAME (t))
6228     {
6229       const char *name;
6230       if (component_type == char_type_node)
6231         name = "complex char";
6232       else if (component_type == signed_char_type_node)
6233         name = "complex signed char";
6234       else if (component_type == unsigned_char_type_node)
6235         name = "complex unsigned char";
6236       else if (component_type == short_integer_type_node)
6237         name = "complex short int";
6238       else if (component_type == short_unsigned_type_node)
6239         name = "complex short unsigned int";
6240       else if (component_type == integer_type_node)
6241         name = "complex int";
6242       else if (component_type == unsigned_type_node)
6243         name = "complex unsigned int";
6244       else if (component_type == long_integer_type_node)
6245         name = "complex long int";
6246       else if (component_type == long_unsigned_type_node)
6247         name = "complex long unsigned int";
6248       else if (component_type == long_long_integer_type_node)
6249         name = "complex long long int";
6250       else if (component_type == long_long_unsigned_type_node)
6251         name = "complex long long unsigned int";
6252       else
6253         name = 0;
6254
6255       if (name != 0)
6256         TYPE_NAME (t) = build_decl (TYPE_DECL, get_identifier (name), t);
6257     }
6258
6259   return build_qualified_type (t, TYPE_QUALS (component_type));
6260 }
6261 \f
6262 /* Return OP, stripped of any conversions to wider types as much as is safe.
6263    Converting the value back to OP's type makes a value equivalent to OP.
6264
6265    If FOR_TYPE is nonzero, we return a value which, if converted to
6266    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6267
6268    OP must have integer, real or enumeral type.  Pointers are not allowed!
6269
6270    There are some cases where the obvious value we could return
6271    would regenerate to OP if converted to OP's type,
6272    but would not extend like OP to wider types.
6273    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6274    For example, if OP is (unsigned short)(signed char)-1,
6275    we avoid returning (signed char)-1 if FOR_TYPE is int,
6276    even though extending that to an unsigned short would regenerate OP,
6277    since the result of extending (signed char)-1 to (int)
6278    is different from (int) OP.  */
6279
6280 tree
6281 get_unwidened (tree op, tree for_type)
6282 {
6283   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
6284   tree type = TREE_TYPE (op);
6285   unsigned final_prec
6286     = TYPE_PRECISION (for_type != 0 ? for_type : type);
6287   int uns
6288     = (for_type != 0 && for_type != type
6289        && final_prec > TYPE_PRECISION (type)
6290        && TYPE_UNSIGNED (type));
6291   tree win = op;
6292
6293   while (CONVERT_EXPR_P (op))
6294     {
6295       int bitschange;
6296
6297       /* TYPE_PRECISION on vector types has different meaning
6298          (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6299          so avoid them here.  */
6300       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6301         break;
6302
6303       bitschange = TYPE_PRECISION (TREE_TYPE (op))
6304                    - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6305
6306       /* Truncations are many-one so cannot be removed.
6307          Unless we are later going to truncate down even farther.  */
6308       if (bitschange < 0
6309           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6310         break;
6311
6312       /* See what's inside this conversion.  If we decide to strip it,
6313          we will set WIN.  */
6314       op = TREE_OPERAND (op, 0);
6315
6316       /* If we have not stripped any zero-extensions (uns is 0),
6317          we can strip any kind of extension.
6318          If we have previously stripped a zero-extension,
6319          only zero-extensions can safely be stripped.
6320          Any extension can be stripped if the bits it would produce
6321          are all going to be discarded later by truncating to FOR_TYPE.  */
6322
6323       if (bitschange > 0)
6324         {
6325           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6326             win = op;
6327           /* TYPE_UNSIGNED says whether this is a zero-extension.
6328              Let's avoid computing it if it does not affect WIN
6329              and if UNS will not be needed again.  */
6330           if ((uns
6331                || CONVERT_EXPR_P (op))
6332               && TYPE_UNSIGNED (TREE_TYPE (op)))
6333             {
6334               uns = 1;
6335               win = op;
6336             }
6337         }
6338     }
6339
6340   return win;
6341 }
6342 \f
6343 /* Return OP or a simpler expression for a narrower value
6344    which can be sign-extended or zero-extended to give back OP.
6345    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6346    or 0 if the value should be sign-extended.  */
6347
6348 tree
6349 get_narrower (tree op, int *unsignedp_ptr)
6350 {
6351   int uns = 0;
6352   int first = 1;
6353   tree win = op;
6354   bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6355
6356   while (TREE_CODE (op) == NOP_EXPR)
6357     {
6358       int bitschange
6359         = (TYPE_PRECISION (TREE_TYPE (op))
6360            - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6361
6362       /* Truncations are many-one so cannot be removed.  */
6363       if (bitschange < 0)
6364         break;
6365
6366       /* See what's inside this conversion.  If we decide to strip it,
6367          we will set WIN.  */
6368
6369       if (bitschange > 0)
6370         {
6371           op = TREE_OPERAND (op, 0);
6372           /* An extension: the outermost one can be stripped,
6373              but remember whether it is zero or sign extension.  */
6374           if (first)
6375             uns = TYPE_UNSIGNED (TREE_TYPE (op));
6376           /* Otherwise, if a sign extension has been stripped,
6377              only sign extensions can now be stripped;
6378              if a zero extension has been stripped, only zero-extensions.  */
6379           else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6380             break;
6381           first = 0;
6382         }
6383       else /* bitschange == 0 */
6384         {
6385           /* A change in nominal type can always be stripped, but we must
6386              preserve the unsignedness.  */
6387           if (first)
6388             uns = TYPE_UNSIGNED (TREE_TYPE (op));
6389           first = 0;
6390           op = TREE_OPERAND (op, 0);
6391           /* Keep trying to narrow, but don't assign op to win if it
6392              would turn an integral type into something else.  */
6393           if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6394             continue;
6395         }
6396
6397       win = op;
6398     }
6399
6400   if (TREE_CODE (op) == COMPONENT_REF
6401       /* Since type_for_size always gives an integer type.  */
6402       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6403       && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6404       /* Ensure field is laid out already.  */
6405       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6406       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6407     {
6408       unsigned HOST_WIDE_INT innerprec
6409         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6410       int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6411                        || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6412       tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6413
6414       /* We can get this structure field in a narrower type that fits it,
6415          but the resulting extension to its nominal type (a fullword type)
6416          must satisfy the same conditions as for other extensions.
6417
6418          Do this only for fields that are aligned (not bit-fields),
6419          because when bit-field insns will be used there is no
6420          advantage in doing this.  */
6421
6422       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6423           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6424           && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6425           && type != 0)
6426         {
6427           if (first)
6428             uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6429           win = fold_convert (type, op);
6430         }
6431     }
6432
6433   *unsignedp_ptr = uns;
6434   return win;
6435 }
6436 \f
6437 /* Nonzero if integer constant C has a value that is permissible
6438    for type TYPE (an INTEGER_TYPE).  */
6439
6440 int
6441 int_fits_type_p (const_tree c, const_tree type)
6442 {
6443   tree type_low_bound, type_high_bound;
6444   bool ok_for_low_bound, ok_for_high_bound, unsc;
6445   double_int dc, dd;
6446
6447   dc = tree_to_double_int (c);
6448   unsc = TYPE_UNSIGNED (TREE_TYPE (c));
6449
6450   if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
6451       && TYPE_IS_SIZETYPE (TREE_TYPE (c))
6452       && unsc)
6453     /* So c is an unsigned integer whose type is sizetype and type is not.
6454        sizetype'd integers are sign extended even though they are
6455        unsigned. If the integer value fits in the lower end word of c,
6456        and if the higher end word has all its bits set to 1, that
6457        means the higher end bits are set to 1 only for sign extension.
6458        So let's convert c into an equivalent zero extended unsigned
6459        integer.  */
6460     dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
6461
6462 retry:
6463   type_low_bound = TYPE_MIN_VALUE (type);
6464   type_high_bound = TYPE_MAX_VALUE (type);
6465
6466   /* If at least one bound of the type is a constant integer, we can check
6467      ourselves and maybe make a decision. If no such decision is possible, but
6468      this type is a subtype, try checking against that.  Otherwise, use
6469      fit_double_type, which checks against the precision.
6470
6471      Compute the status for each possibly constant bound, and return if we see
6472      one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6473      for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6474      for "constant known to fit".  */
6475
6476   /* Check if c >= type_low_bound.  */
6477   if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6478     {
6479       dd = tree_to_double_int (type_low_bound);
6480       if (TREE_CODE (type) == INTEGER_TYPE
6481           && TYPE_IS_SIZETYPE (type)
6482           && TYPE_UNSIGNED (type))
6483         dd = double_int_zext (dd, TYPE_PRECISION (type));
6484       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
6485         {
6486           int c_neg = (!unsc && double_int_negative_p (dc));
6487           int t_neg = (unsc && double_int_negative_p (dd));
6488
6489           if (c_neg && !t_neg)
6490             return 0;
6491           if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
6492             return 0;
6493         }
6494       else if (double_int_cmp (dc, dd, unsc) < 0)
6495         return 0;
6496       ok_for_low_bound = true;
6497     }
6498   else
6499     ok_for_low_bound = false;
6500
6501   /* Check if c <= type_high_bound.  */
6502   if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6503     {
6504       dd = tree_to_double_int (type_high_bound);
6505       if (TREE_CODE (type) == INTEGER_TYPE
6506           && TYPE_IS_SIZETYPE (type)
6507           && TYPE_UNSIGNED (type))
6508         dd = double_int_zext (dd, TYPE_PRECISION (type));
6509       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
6510         {
6511           int c_neg = (!unsc && double_int_negative_p (dc));
6512           int t_neg = (unsc && double_int_negative_p (dd));
6513
6514           if (t_neg && !c_neg)
6515             return 0;
6516           if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
6517             return 0;
6518         }
6519       else if (double_int_cmp (dc, dd, unsc) > 0)
6520         return 0;
6521       ok_for_high_bound = true;
6522     }
6523   else
6524     ok_for_high_bound = false;
6525
6526   /* If the constant fits both bounds, the result is known.  */
6527   if (ok_for_low_bound && ok_for_high_bound)
6528     return 1;
6529
6530   /* Perform some generic filtering which may allow making a decision
6531      even if the bounds are not constant.  First, negative integers
6532      never fit in unsigned types, */
6533   if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
6534     return 0;
6535
6536   /* Second, narrower types always fit in wider ones.  */
6537   if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6538     return 1;
6539
6540   /* Third, unsigned integers with top bit set never fit signed types.  */
6541   if (! TYPE_UNSIGNED (type) && unsc)
6542     {
6543       int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
6544       if (prec < HOST_BITS_PER_WIDE_INT)
6545         {
6546           if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
6547             return 0;
6548         }
6549       else if (((((unsigned HOST_WIDE_INT) 1)
6550                  << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
6551         return 0;
6552     }
6553
6554   /* If we haven't been able to decide at this point, there nothing more we
6555      can check ourselves here.  Look at the base type if we have one and it
6556      has the same precision.  */
6557   if (TREE_CODE (type) == INTEGER_TYPE
6558       && TREE_TYPE (type) != 0
6559       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6560     {
6561       type = TREE_TYPE (type);
6562       goto retry;
6563     }
6564
6565   /* Or to fit_double_type, if nothing else.  */
6566   return !fit_double_type (dc.low, dc.high, &dc.low, &dc.high, type);
6567 }
6568
6569 /* Stores bounds of an integer TYPE in MIN and MAX.  If TYPE has non-constant
6570    bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6571    represented (assuming two's-complement arithmetic) within the bit
6572    precision of the type are returned instead.  */
6573
6574 void
6575 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6576 {
6577   if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6578       && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6579     mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6580                         TYPE_UNSIGNED (type));
6581   else
6582     {
6583       if (TYPE_UNSIGNED (type))
6584         mpz_set_ui (min, 0);
6585       else
6586         {
6587           double_int mn;
6588           mn = double_int_mask (TYPE_PRECISION (type) - 1);
6589           mn = double_int_sext (double_int_add (mn, double_int_one),
6590                                 TYPE_PRECISION (type));
6591           mpz_set_double_int (min, mn, false);
6592         }
6593     }
6594
6595   if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type) 
6596       && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
6597     mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
6598                         TYPE_UNSIGNED (type));
6599   else
6600     {
6601       if (TYPE_UNSIGNED (type))
6602         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
6603                             true);
6604       else
6605         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
6606                             true);
6607     }
6608 }
6609
6610 /* Return true if VAR is an automatic variable defined in function FN.  */
6611
6612 bool
6613 auto_var_in_fn_p (const_tree var, const_tree fn)
6614 {
6615   return (DECL_P (var) && DECL_CONTEXT (var) == fn
6616           && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
6617                && ! TREE_STATIC (var))
6618               || TREE_CODE (var) == LABEL_DECL
6619               || TREE_CODE (var) == RESULT_DECL));
6620 }
6621
6622 /* Subprogram of following function.  Called by walk_tree.
6623
6624    Return *TP if it is an automatic variable or parameter of the
6625    function passed in as DATA.  */
6626
6627 static tree
6628 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6629 {
6630   tree fn = (tree) data;
6631
6632   if (TYPE_P (*tp))
6633     *walk_subtrees = 0;
6634
6635   else if (DECL_P (*tp)
6636            && auto_var_in_fn_p (*tp, fn))
6637     return *tp;
6638
6639   return NULL_TREE;
6640 }
6641
6642 /* Returns true if T is, contains, or refers to a type with variable
6643    size.  For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6644    arguments, but not the return type.  If FN is nonzero, only return
6645    true if a modifier of the type or position of FN is a variable or
6646    parameter inside FN.
6647
6648    This concept is more general than that of C99 'variably modified types':
6649    in C99, a struct type is never variably modified because a VLA may not
6650    appear as a structure member.  However, in GNU C code like:
6651
6652      struct S { int i[f()]; };
6653
6654    is valid, and other languages may define similar constructs.  */
6655
6656 bool
6657 variably_modified_type_p (tree type, tree fn)
6658 {
6659   tree t;
6660
6661 /* Test if T is either variable (if FN is zero) or an expression containing
6662    a variable in FN.  */
6663 #define RETURN_TRUE_IF_VAR(T)                                           \
6664   do { tree _t = (T);                                                   \
6665     if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST    \
6666         && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL)))        \
6667       return true;  } while (0)
6668
6669   if (type == error_mark_node)
6670     return false;
6671
6672   /* If TYPE itself has variable size, it is variably modified.  */
6673   RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6674   RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6675
6676   switch (TREE_CODE (type))
6677     {
6678     case POINTER_TYPE:
6679     case REFERENCE_TYPE:
6680     case VECTOR_TYPE:
6681       if (variably_modified_type_p (TREE_TYPE (type), fn))
6682         return true;
6683       break;
6684
6685     case FUNCTION_TYPE:
6686     case METHOD_TYPE:
6687       /* If TYPE is a function type, it is variably modified if the
6688          return type is variably modified.  */
6689       if (variably_modified_type_p (TREE_TYPE (type), fn))
6690           return true;
6691       break;
6692
6693     case INTEGER_TYPE:
6694     case REAL_TYPE:
6695     case FIXED_POINT_TYPE:
6696     case ENUMERAL_TYPE:
6697     case BOOLEAN_TYPE:
6698       /* Scalar types are variably modified if their end points
6699          aren't constant.  */
6700       RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6701       RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6702       break;
6703
6704     case RECORD_TYPE:
6705     case UNION_TYPE:
6706     case QUAL_UNION_TYPE:
6707       /* We can't see if any of the fields are variably-modified by the
6708          definition we normally use, since that would produce infinite
6709          recursion via pointers.  */
6710       /* This is variably modified if some field's type is.  */
6711       for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6712         if (TREE_CODE (t) == FIELD_DECL)
6713           {
6714             RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6715             RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6716             RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6717
6718             if (TREE_CODE (type) == QUAL_UNION_TYPE)
6719               RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6720           }
6721         break;
6722
6723     case ARRAY_TYPE:
6724       /* Do not call ourselves to avoid infinite recursion.  This is
6725          variably modified if the element type is.  */
6726       RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6727       RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6728       break;
6729
6730     default:
6731       break;
6732     }
6733
6734   /* The current language may have other cases to check, but in general,
6735      all other types are not variably modified.  */
6736   return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6737
6738 #undef RETURN_TRUE_IF_VAR
6739 }
6740
6741 /* Given a DECL or TYPE, return the scope in which it was declared, or
6742    NULL_TREE if there is no containing scope.  */
6743
6744 tree
6745 get_containing_scope (const_tree t)
6746 {
6747   return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6748 }
6749
6750 /* Return the innermost context enclosing DECL that is
6751    a FUNCTION_DECL, or zero if none.  */
6752
6753 tree
6754 decl_function_context (const_tree decl)
6755 {
6756   tree context;
6757
6758   if (TREE_CODE (decl) == ERROR_MARK)
6759     return 0;
6760
6761   /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6762      where we look up the function at runtime.  Such functions always take
6763      a first argument of type 'pointer to real context'.
6764
6765      C++ should really be fixed to use DECL_CONTEXT for the real context,
6766      and use something else for the "virtual context".  */
6767   else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6768     context
6769       = TYPE_MAIN_VARIANT
6770         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6771   else
6772     context = DECL_CONTEXT (decl);
6773
6774   while (context && TREE_CODE (context) != FUNCTION_DECL)
6775     {
6776       if (TREE_CODE (context) == BLOCK)
6777         context = BLOCK_SUPERCONTEXT (context);
6778       else
6779         context = get_containing_scope (context);
6780     }
6781
6782   return context;
6783 }
6784
6785 /* Return the innermost context enclosing DECL that is
6786    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6787    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
6788
6789 tree
6790 decl_type_context (const_tree decl)
6791 {
6792   tree context = DECL_CONTEXT (decl);
6793
6794   while (context)
6795     switch (TREE_CODE (context))
6796       {
6797       case NAMESPACE_DECL:
6798       case TRANSLATION_UNIT_DECL:
6799         return NULL_TREE;
6800
6801       case RECORD_TYPE:
6802       case UNION_TYPE:
6803       case QUAL_UNION_TYPE:
6804         return context;
6805
6806       case TYPE_DECL:
6807       case FUNCTION_DECL:
6808         context = DECL_CONTEXT (context);
6809         break;
6810
6811       case BLOCK:
6812         context = BLOCK_SUPERCONTEXT (context);
6813         break;
6814
6815       default:
6816         gcc_unreachable ();
6817       }
6818
6819   return NULL_TREE;
6820 }
6821
6822 /* CALL is a CALL_EXPR.  Return the declaration for the function
6823    called, or NULL_TREE if the called function cannot be
6824    determined.  */
6825
6826 tree
6827 get_callee_fndecl (const_tree call)
6828 {
6829   tree addr;
6830
6831   if (call == error_mark_node)
6832     return error_mark_node;
6833
6834   /* It's invalid to call this function with anything but a
6835      CALL_EXPR.  */
6836   gcc_assert (TREE_CODE (call) == CALL_EXPR);
6837
6838   /* The first operand to the CALL is the address of the function
6839      called.  */
6840   addr = CALL_EXPR_FN (call);
6841
6842   STRIP_NOPS (addr);
6843
6844   /* If this is a readonly function pointer, extract its initial value.  */
6845   if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6846       && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6847       && DECL_INITIAL (addr))
6848     addr = DECL_INITIAL (addr);
6849
6850   /* If the address is just `&f' for some function `f', then we know
6851      that `f' is being called.  */
6852   if (TREE_CODE (addr) == ADDR_EXPR
6853       && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6854     return TREE_OPERAND (addr, 0);
6855
6856   /* We couldn't figure out what was being called.  */
6857   return NULL_TREE;
6858 }
6859
6860 /* Print debugging information about tree nodes generated during the compile,
6861    and any language-specific information.  */
6862
6863 void
6864 dump_tree_statistics (void)
6865 {
6866 #ifdef GATHER_STATISTICS
6867   int i;
6868   int total_nodes, total_bytes;
6869 #endif
6870
6871   fprintf (stderr, "\n??? tree nodes created\n\n");
6872 #ifdef GATHER_STATISTICS
6873   fprintf (stderr, "Kind                   Nodes      Bytes\n");
6874   fprintf (stderr, "---------------------------------------\n");
6875   total_nodes = total_bytes = 0;
6876   for (i = 0; i < (int) all_kinds; i++)
6877     {
6878       fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
6879                tree_node_counts[i], tree_node_sizes[i]);
6880       total_nodes += tree_node_counts[i];
6881       total_bytes += tree_node_sizes[i];
6882     }
6883   fprintf (stderr, "---------------------------------------\n");
6884   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
6885   fprintf (stderr, "---------------------------------------\n");
6886   ssanames_print_statistics ();
6887   phinodes_print_statistics ();
6888 #else
6889   fprintf (stderr, "(No per-node statistics)\n");
6890 #endif
6891   print_type_hash_statistics ();
6892   print_debug_expr_statistics ();
6893   print_value_expr_statistics ();
6894   print_restrict_base_statistics ();
6895   lang_hooks.print_statistics ();
6896 }
6897 \f
6898 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
6899
6900 /* Generate a crc32 of a string.  */
6901
6902 unsigned
6903 crc32_string (unsigned chksum, const char *string)
6904 {
6905   do
6906     {
6907       unsigned value = *string << 24;
6908       unsigned ix;
6909
6910       for (ix = 8; ix--; value <<= 1)
6911         {
6912           unsigned feedback;
6913
6914           feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
6915           chksum <<= 1;
6916           chksum ^= feedback;
6917         }
6918     }
6919   while (*string++);
6920   return chksum;
6921 }
6922
6923 /* P is a string that will be used in a symbol.  Mask out any characters
6924    that are not valid in that context.  */
6925
6926 void
6927 clean_symbol_name (char *p)
6928 {
6929   for (; *p; p++)
6930     if (! (ISALNUM (*p)
6931 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
6932             || *p == '$'
6933 #endif
6934 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but...  */
6935             || *p == '.'
6936 #endif
6937            ))
6938       *p = '_';
6939 }
6940
6941 /* Generate a name for a special-purpose function function.
6942    The generated name may need to be unique across the whole link.
6943    TYPE is some string to identify the purpose of this function to the
6944    linker or collect2; it must start with an uppercase letter,
6945    one of:
6946    I - for constructors
6947    D - for destructors
6948    N - for C++ anonymous namespaces
6949    F - for DWARF unwind frame information.  */
6950
6951 tree
6952 get_file_function_name (const char *type)
6953 {
6954   char *buf;
6955   const char *p;
6956   char *q;
6957
6958   /* If we already have a name we know to be unique, just use that.  */
6959   if (first_global_object_name)
6960     p = q = ASTRDUP (first_global_object_name);
6961   /* If the target is handling the constructors/destructors, they
6962      will be local to this file and the name is only necessary for
6963      debugging purposes.  */
6964   else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
6965     {
6966       const char *file = main_input_filename;
6967       if (! file)
6968         file = input_filename;
6969       /* Just use the file's basename, because the full pathname
6970          might be quite long.  */
6971       p = strrchr (file, '/');
6972       if (p)
6973         p++;
6974       else
6975         p = file;
6976       p = q = ASTRDUP (p);
6977     }
6978   else
6979     {
6980       /* Otherwise, the name must be unique across the entire link.
6981          We don't have anything that we know to be unique to this translation
6982          unit, so use what we do have and throw in some randomness.  */
6983       unsigned len;
6984       const char *name = weak_global_object_name;
6985       const char *file = main_input_filename;
6986
6987       if (! name)
6988         name = "";
6989       if (! file)
6990         file = input_filename;
6991
6992       len = strlen (file);
6993       q = (char *) alloca (9 * 2 + len + 1);
6994       memcpy (q, file, len + 1);
6995
6996       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
6997                crc32_string (0, get_random_seed (false)));
6998
6999       p = q;
7000     }
7001
7002   clean_symbol_name (q);
7003   buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
7004                          + strlen (type));
7005
7006   /* Set up the name of the file-level functions we may need.
7007      Use a global object (which is already required to be unique over
7008      the program) rather than the file name (which imposes extra
7009      constraints).  */
7010   sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
7011
7012   return get_identifier (buf);
7013 }
7014 \f
7015 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
7016
7017 /* Complain that the tree code of NODE does not match the expected 0
7018    terminated list of trailing codes. The trailing code list can be
7019    empty, for a more vague error message.  FILE, LINE, and FUNCTION
7020    are of the caller.  */
7021
7022 void
7023 tree_check_failed (const_tree node, const char *file,
7024                    int line, const char *function, ...)
7025 {
7026   va_list args;
7027   const char *buffer;
7028   unsigned length = 0;
7029   int code;
7030
7031   va_start (args, function);
7032   while ((code = va_arg (args, int)))
7033     length += 4 + strlen (tree_code_name[code]);
7034   va_end (args);
7035   if (length)
7036     {
7037       char *tmp;
7038       va_start (args, function);
7039       length += strlen ("expected ");
7040       buffer = tmp = (char *) alloca (length);
7041       length = 0;
7042       while ((code = va_arg (args, int)))
7043         {
7044           const char *prefix = length ? " or " : "expected ";
7045           
7046           strcpy (tmp + length, prefix);
7047           length += strlen (prefix);
7048           strcpy (tmp + length, tree_code_name[code]);
7049           length += strlen (tree_code_name[code]);
7050         }
7051       va_end (args);
7052     }
7053   else
7054     buffer = "unexpected node";
7055
7056   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7057                   buffer, tree_code_name[TREE_CODE (node)],
7058                   function, trim_filename (file), line);
7059 }
7060
7061 /* Complain that the tree code of NODE does match the expected 0
7062    terminated list of trailing codes. FILE, LINE, and FUNCTION are of
7063    the caller.  */
7064
7065 void
7066 tree_not_check_failed (const_tree node, const char *file,
7067                        int line, const char *function, ...)
7068 {
7069   va_list args;
7070   char *buffer;
7071   unsigned length = 0;
7072   int code;
7073
7074   va_start (args, function);
7075   while ((code = va_arg (args, int)))
7076     length += 4 + strlen (tree_code_name[code]);
7077   va_end (args);
7078   va_start (args, function);
7079   buffer = (char *) alloca (length);
7080   length = 0;
7081   while ((code = va_arg (args, int)))
7082     {
7083       if (length)
7084         {
7085           strcpy (buffer + length, " or ");
7086           length += 4;
7087         }
7088       strcpy (buffer + length, tree_code_name[code]);
7089       length += strlen (tree_code_name[code]);
7090     }
7091   va_end (args);
7092
7093   internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
7094                   buffer, tree_code_name[TREE_CODE (node)],
7095                   function, trim_filename (file), line);
7096 }
7097
7098 /* Similar to tree_check_failed, except that we check for a class of tree
7099    code, given in CL.  */
7100
7101 void
7102 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
7103                          const char *file, int line, const char *function)
7104 {
7105   internal_error
7106     ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
7107      TREE_CODE_CLASS_STRING (cl),
7108      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7109      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7110 }
7111
7112 /* Similar to tree_check_failed, except that instead of specifying a
7113    dozen codes, use the knowledge that they're all sequential.  */
7114
7115 void
7116 tree_range_check_failed (const_tree node, const char *file, int line,
7117                          const char *function, enum tree_code c1,
7118                          enum tree_code c2)
7119 {
7120   char *buffer;
7121   unsigned length = 0;
7122   enum tree_code c;
7123
7124   for (c = c1; c <= c2; ++c)
7125     length += 4 + strlen (tree_code_name[c]);
7126
7127   length += strlen ("expected ");
7128   buffer = (char *) alloca (length);
7129   length = 0;
7130
7131   for (c = c1; c <= c2; ++c)
7132     {
7133       const char *prefix = length ? " or " : "expected ";
7134
7135       strcpy (buffer + length, prefix);
7136       length += strlen (prefix);
7137       strcpy (buffer + length, tree_code_name[c]);
7138       length += strlen (tree_code_name[c]);
7139     }
7140
7141   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7142                   buffer, tree_code_name[TREE_CODE (node)],
7143                   function, trim_filename (file), line);
7144 }
7145
7146
7147 /* Similar to tree_check_failed, except that we check that a tree does
7148    not have the specified code, given in CL.  */
7149
7150 void
7151 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
7152                              const char *file, int line, const char *function)
7153 {
7154   internal_error
7155     ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
7156      TREE_CODE_CLASS_STRING (cl),
7157      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7158      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7159 }
7160
7161
7162 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes.  */
7163
7164 void
7165 omp_clause_check_failed (const_tree node, const char *file, int line,
7166                          const char *function, enum omp_clause_code code)
7167 {
7168   internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
7169                   omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
7170                   function, trim_filename (file), line);
7171 }
7172
7173
7174 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes.  */
7175
7176 void
7177 omp_clause_range_check_failed (const_tree node, const char *file, int line,
7178                                const char *function, enum omp_clause_code c1,
7179                                enum omp_clause_code c2)
7180 {
7181   char *buffer;
7182   unsigned length = 0;
7183   enum omp_clause_code c;
7184
7185   for (c = c1; c <= c2; ++c)
7186     length += 4 + strlen (omp_clause_code_name[c]);
7187
7188   length += strlen ("expected ");
7189   buffer = (char *) alloca (length);
7190   length = 0;
7191
7192   for (c = c1; c <= c2; ++c)
7193     {
7194       const char *prefix = length ? " or " : "expected ";
7195
7196       strcpy (buffer + length, prefix);
7197       length += strlen (prefix);
7198       strcpy (buffer + length, omp_clause_code_name[c]);
7199       length += strlen (omp_clause_code_name[c]);
7200     }
7201
7202   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7203                   buffer, omp_clause_code_name[TREE_CODE (node)],
7204                   function, trim_filename (file), line);
7205 }
7206
7207
7208 #undef DEFTREESTRUCT
7209 #define DEFTREESTRUCT(VAL, NAME) NAME,
7210
7211 static const char *ts_enum_names[] = {
7212 #include "treestruct.def"
7213 };
7214 #undef DEFTREESTRUCT
7215
7216 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
7217
7218 /* Similar to tree_class_check_failed, except that we check for
7219    whether CODE contains the tree structure identified by EN.  */
7220
7221 void
7222 tree_contains_struct_check_failed (const_tree node, 
7223                                    const enum tree_node_structure_enum en,
7224                                    const char *file, int line, 
7225                                    const char *function)
7226 {
7227   internal_error
7228     ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
7229      TS_ENUM_NAME(en),
7230      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7231 }
7232
7233
7234 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
7235    (dynamically sized) vector.  */
7236
7237 void
7238 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
7239                            const char *function)
7240 {
7241   internal_error
7242     ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
7243      idx + 1, len, function, trim_filename (file), line);
7244 }
7245
7246 /* Similar to above, except that the check is for the bounds of the operand
7247    vector of an expression node EXP.  */
7248
7249 void
7250 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7251                            int line, const char *function)
7252 {
7253   int code = TREE_CODE (exp);
7254   internal_error
7255     ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7256      idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7257      function, trim_filename (file), line);
7258 }
7259
7260 /* Similar to above, except that the check is for the number of
7261    operands of an OMP_CLAUSE node.  */
7262
7263 void
7264 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7265                                  int line, const char *function)
7266 {
7267   internal_error
7268     ("tree check: accessed operand %d of omp_clause %s with %d operands "
7269      "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7270      omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7271      trim_filename (file), line);
7272 }
7273 #endif /* ENABLE_TREE_CHECKING */
7274 \f
7275 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7276    and mapped to the machine mode MODE.  Initialize its fields and build
7277    the information necessary for debugging output.  */
7278
7279 static tree
7280 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7281 {
7282   tree t;
7283   hashval_t hashcode = 0;
7284
7285   /* Build a main variant, based on the main variant of the inner type, then
7286      use it to build the variant we return.  */
7287   if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7288       && TYPE_MAIN_VARIANT (innertype) != innertype)
7289     return build_type_attribute_qual_variant (
7290             make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7291             TYPE_ATTRIBUTES (innertype),
7292             TYPE_QUALS (innertype));
7293
7294   t = make_node (VECTOR_TYPE);
7295   TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7296   SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7297   SET_TYPE_MODE (t, mode);
7298   TYPE_READONLY (t) = TYPE_READONLY (innertype);
7299   TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
7300
7301   if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
7302     SET_TYPE_STRUCTURAL_EQUALITY (t);
7303   else if (TYPE_CANONICAL (innertype) != innertype
7304            || mode != VOIDmode)
7305     TYPE_CANONICAL (t) 
7306       = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
7307
7308   layout_type (t);
7309
7310   {
7311     tree index = build_int_cst (NULL_TREE, nunits - 1);
7312     tree array = build_array_type (innertype, build_index_type (index));
7313     tree rt = make_node (RECORD_TYPE);
7314
7315     TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
7316     DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
7317     layout_type (rt);
7318     TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
7319     /* In dwarfout.c, type lookup uses TYPE_UID numbers.  We want to output
7320        the representation type, and we want to find that die when looking up
7321        the vector type.  This is most easily achieved by making the TYPE_UID
7322        numbers equal.  */
7323     TYPE_UID (rt) = TYPE_UID (t);
7324   }
7325
7326   hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
7327   hashcode = iterative_hash_host_wide_int (mode, hashcode);
7328   hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7329   return type_hash_canon (hashcode, t);
7330 }
7331
7332 static tree
7333 make_or_reuse_type (unsigned size, int unsignedp)
7334 {
7335   if (size == INT_TYPE_SIZE)
7336     return unsignedp ? unsigned_type_node : integer_type_node;
7337   if (size == CHAR_TYPE_SIZE)
7338     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7339   if (size == SHORT_TYPE_SIZE)
7340     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7341   if (size == LONG_TYPE_SIZE)
7342     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7343   if (size == LONG_LONG_TYPE_SIZE)
7344     return (unsignedp ? long_long_unsigned_type_node
7345             : long_long_integer_type_node);
7346
7347   if (unsignedp)
7348     return make_unsigned_type (size);
7349   else
7350     return make_signed_type (size);
7351 }
7352
7353 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP.  */
7354
7355 static tree
7356 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7357 {
7358   if (satp)
7359     {
7360       if (size == SHORT_FRACT_TYPE_SIZE)
7361         return unsignedp ? sat_unsigned_short_fract_type_node
7362                          : sat_short_fract_type_node;
7363       if (size == FRACT_TYPE_SIZE)
7364         return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7365       if (size == LONG_FRACT_TYPE_SIZE)
7366         return unsignedp ? sat_unsigned_long_fract_type_node
7367                          : sat_long_fract_type_node;
7368       if (size == LONG_LONG_FRACT_TYPE_SIZE)
7369         return unsignedp ? sat_unsigned_long_long_fract_type_node
7370                          : sat_long_long_fract_type_node;
7371     }
7372   else
7373     {
7374       if (size == SHORT_FRACT_TYPE_SIZE)
7375         return unsignedp ? unsigned_short_fract_type_node
7376                          : short_fract_type_node;
7377       if (size == FRACT_TYPE_SIZE)
7378         return unsignedp ? unsigned_fract_type_node : fract_type_node;
7379       if (size == LONG_FRACT_TYPE_SIZE)
7380         return unsignedp ? unsigned_long_fract_type_node
7381                          : long_fract_type_node;
7382       if (size == LONG_LONG_FRACT_TYPE_SIZE)
7383         return unsignedp ? unsigned_long_long_fract_type_node
7384                          : long_long_fract_type_node;
7385     }
7386
7387   return make_fract_type (size, unsignedp, satp);
7388 }
7389
7390 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP.  */
7391
7392 static tree
7393 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7394 {
7395   if (satp)
7396     {
7397       if (size == SHORT_ACCUM_TYPE_SIZE)
7398         return unsignedp ? sat_unsigned_short_accum_type_node
7399                          : sat_short_accum_type_node;
7400       if (size == ACCUM_TYPE_SIZE)
7401         return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7402       if (size == LONG_ACCUM_TYPE_SIZE)
7403         return unsignedp ? sat_unsigned_long_accum_type_node
7404                          : sat_long_accum_type_node;
7405       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7406         return unsignedp ? sat_unsigned_long_long_accum_type_node
7407                          : sat_long_long_accum_type_node;
7408     }
7409   else
7410     {
7411       if (size == SHORT_ACCUM_TYPE_SIZE)
7412         return unsignedp ? unsigned_short_accum_type_node
7413                          : short_accum_type_node;
7414       if (size == ACCUM_TYPE_SIZE)
7415         return unsignedp ? unsigned_accum_type_node : accum_type_node;
7416       if (size == LONG_ACCUM_TYPE_SIZE)
7417         return unsignedp ? unsigned_long_accum_type_node
7418                          : long_accum_type_node;
7419       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7420         return unsignedp ? unsigned_long_long_accum_type_node
7421                          : long_long_accum_type_node;
7422     }
7423
7424   return make_accum_type (size, unsignedp, satp);
7425 }
7426
7427 /* Create nodes for all integer types (and error_mark_node) using the sizes
7428    of C datatypes.  The caller should call set_sizetype soon after calling
7429    this function to select one of the types as sizetype.  */
7430
7431 void
7432 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7433 {
7434   error_mark_node = make_node (ERROR_MARK);
7435   TREE_TYPE (error_mark_node) = error_mark_node;
7436
7437   initialize_sizetypes (signed_sizetype);
7438
7439   /* Define both `signed char' and `unsigned char'.  */
7440   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7441   TYPE_STRING_FLAG (signed_char_type_node) = 1;
7442   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7443   TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7444
7445   /* Define `char', which is like either `signed char' or `unsigned char'
7446      but not the same as either.  */
7447   char_type_node
7448     = (signed_char
7449        ? make_signed_type (CHAR_TYPE_SIZE)
7450        : make_unsigned_type (CHAR_TYPE_SIZE));
7451   TYPE_STRING_FLAG (char_type_node) = 1;
7452
7453   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7454   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7455   integer_type_node = make_signed_type (INT_TYPE_SIZE);
7456   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7457   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7458   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7459   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7460   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7461
7462   /* Define a boolean type.  This type only represents boolean values but
7463      may be larger than char depending on the value of BOOL_TYPE_SIZE.
7464      Front ends which want to override this size (i.e. Java) can redefine
7465      boolean_type_node before calling build_common_tree_nodes_2.  */
7466   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7467   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7468   TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7469   TYPE_PRECISION (boolean_type_node) = 1;
7470
7471   /* Fill in the rest of the sized types.  Reuse existing type nodes
7472      when possible.  */
7473   intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7474   intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7475   intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7476   intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7477   intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7478
7479   unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7480   unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7481   unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7482   unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7483   unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7484
7485   access_public_node = get_identifier ("public");
7486   access_protected_node = get_identifier ("protected");
7487   access_private_node = get_identifier ("private");
7488 }
7489
7490 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7491    It will create several other common tree nodes.  */
7492
7493 void
7494 build_common_tree_nodes_2 (int short_double)
7495 {
7496   /* Define these next since types below may used them.  */
7497   integer_zero_node = build_int_cst (NULL_TREE, 0);
7498   integer_one_node = build_int_cst (NULL_TREE, 1);
7499   integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7500
7501   size_zero_node = size_int (0);
7502   size_one_node = size_int (1);
7503   bitsize_zero_node = bitsize_int (0);
7504   bitsize_one_node = bitsize_int (1);
7505   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7506
7507   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7508   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7509
7510   void_type_node = make_node (VOID_TYPE);
7511   layout_type (void_type_node);
7512
7513   /* We are not going to have real types in C with less than byte alignment,
7514      so we might as well not have any types that claim to have it.  */
7515   TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7516   TYPE_USER_ALIGN (void_type_node) = 0;
7517
7518   null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7519   layout_type (TREE_TYPE (null_pointer_node));
7520
7521   ptr_type_node = build_pointer_type (void_type_node);
7522   const_ptr_type_node
7523     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7524   fileptr_type_node = ptr_type_node;
7525
7526   float_type_node = make_node (REAL_TYPE);
7527   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7528   layout_type (float_type_node);
7529
7530   double_type_node = make_node (REAL_TYPE);
7531   if (short_double)
7532     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7533   else
7534     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7535   layout_type (double_type_node);
7536
7537   long_double_type_node = make_node (REAL_TYPE);
7538   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7539   layout_type (long_double_type_node);
7540
7541   float_ptr_type_node = build_pointer_type (float_type_node);
7542   double_ptr_type_node = build_pointer_type (double_type_node);
7543   long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7544   integer_ptr_type_node = build_pointer_type (integer_type_node);
7545
7546   /* Fixed size integer types.  */
7547   uint32_type_node = build_nonstandard_integer_type (32, true);
7548   uint64_type_node = build_nonstandard_integer_type (64, true);
7549
7550   /* Decimal float types. */
7551   dfloat32_type_node = make_node (REAL_TYPE);
7552   TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE; 
7553   layout_type (dfloat32_type_node);
7554   SET_TYPE_MODE (dfloat32_type_node, SDmode);
7555   dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7556
7557   dfloat64_type_node = make_node (REAL_TYPE);
7558   TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7559   layout_type (dfloat64_type_node);
7560   SET_TYPE_MODE (dfloat64_type_node, DDmode);
7561   dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7562
7563   dfloat128_type_node = make_node (REAL_TYPE);
7564   TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE; 
7565   layout_type (dfloat128_type_node);
7566   SET_TYPE_MODE (dfloat128_type_node, TDmode);
7567   dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7568
7569   complex_integer_type_node = build_complex_type (integer_type_node);
7570   complex_float_type_node = build_complex_type (float_type_node);
7571   complex_double_type_node = build_complex_type (double_type_node);
7572   complex_long_double_type_node = build_complex_type (long_double_type_node);
7573
7574 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned.  */
7575 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
7576   sat_ ## KIND ## _type_node = \
7577     make_sat_signed_ ## KIND ## _type (SIZE); \
7578   sat_unsigned_ ## KIND ## _type_node = \
7579     make_sat_unsigned_ ## KIND ## _type (SIZE); \
7580   KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7581   unsigned_ ## KIND ## _type_node = \
7582     make_unsigned_ ## KIND ## _type (SIZE);
7583
7584 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
7585   sat_ ## WIDTH ## KIND ## _type_node = \
7586     make_sat_signed_ ## KIND ## _type (SIZE); \
7587   sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7588     make_sat_unsigned_ ## KIND ## _type (SIZE); \
7589   WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7590   unsigned_ ## WIDTH ## KIND ## _type_node = \
7591     make_unsigned_ ## KIND ## _type (SIZE);
7592
7593 /* Make fixed-point type nodes based on four different widths.  */
7594 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
7595   MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
7596   MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
7597   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
7598   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
7599
7600 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned.  */
7601 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
7602   NAME ## _type_node = \
7603     make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
7604   u ## NAME ## _type_node = \
7605     make_or_reuse_unsigned_ ## KIND ## _type \
7606       (GET_MODE_BITSIZE (U ## MODE ## mode)); \
7607   sat_ ## NAME ## _type_node = \
7608     make_or_reuse_sat_signed_ ## KIND ## _type \
7609       (GET_MODE_BITSIZE (MODE ## mode)); \
7610   sat_u ## NAME ## _type_node = \
7611     make_or_reuse_sat_unsigned_ ## KIND ## _type \
7612       (GET_MODE_BITSIZE (U ## MODE ## mode));
7613
7614   /* Fixed-point type and mode nodes.  */
7615   MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
7616   MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
7617   MAKE_FIXED_MODE_NODE (fract, qq, QQ)
7618   MAKE_FIXED_MODE_NODE (fract, hq, HQ)
7619   MAKE_FIXED_MODE_NODE (fract, sq, SQ)
7620   MAKE_FIXED_MODE_NODE (fract, dq, DQ)
7621   MAKE_FIXED_MODE_NODE (fract, tq, TQ)
7622   MAKE_FIXED_MODE_NODE (accum, ha, HA)
7623   MAKE_FIXED_MODE_NODE (accum, sa, SA)
7624   MAKE_FIXED_MODE_NODE (accum, da, DA)
7625   MAKE_FIXED_MODE_NODE (accum, ta, TA)
7626
7627   {
7628     tree t = targetm.build_builtin_va_list ();
7629
7630     /* Many back-ends define record types without setting TYPE_NAME.
7631        If we copied the record type here, we'd keep the original
7632        record type without a name.  This breaks name mangling.  So,
7633        don't copy record types and let c_common_nodes_and_builtins()
7634        declare the type to be __builtin_va_list.  */
7635     if (TREE_CODE (t) != RECORD_TYPE)
7636       t = build_variant_type_copy (t);
7637     
7638     va_list_type_node = t;
7639   }
7640 }
7641
7642 /* A subroutine of build_common_builtin_nodes.  Define a builtin function.  */
7643
7644 static void
7645 local_define_builtin (const char *name, tree type, enum built_in_function code,
7646                       const char *library_name, int ecf_flags)
7647 {
7648   tree decl;
7649
7650   decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
7651                                library_name, NULL_TREE);
7652   if (ecf_flags & ECF_CONST)
7653     TREE_READONLY (decl) = 1;
7654   if (ecf_flags & ECF_PURE)
7655     DECL_PURE_P (decl) = 1;
7656   if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
7657     DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
7658   if (ecf_flags & ECF_NORETURN)
7659     TREE_THIS_VOLATILE (decl) = 1;
7660   if (ecf_flags & ECF_NOTHROW)
7661     TREE_NOTHROW (decl) = 1;
7662   if (ecf_flags & ECF_MALLOC)
7663     DECL_IS_MALLOC (decl) = 1;
7664
7665   built_in_decls[code] = decl;
7666   implicit_built_in_decls[code] = decl;
7667 }
7668
7669 /* Call this function after instantiating all builtins that the language
7670    front end cares about.  This will build the rest of the builtins that
7671    are relied upon by the tree optimizers and the middle-end.  */
7672
7673 void
7674 build_common_builtin_nodes (void)
7675 {
7676   tree tmp, ftype;
7677
7678   if (built_in_decls[BUILT_IN_MEMCPY] == NULL
7679       || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7680     {
7681       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7682       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7683       tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7684       ftype = build_function_type (ptr_type_node, tmp);
7685
7686       if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
7687         local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
7688                               "memcpy", ECF_NOTHROW);
7689       if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7690         local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
7691                               "memmove", ECF_NOTHROW);
7692     }
7693
7694   if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
7695     {
7696       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7697       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7698       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7699       ftype = build_function_type (integer_type_node, tmp);
7700       local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7701                             "memcmp", ECF_PURE | ECF_NOTHROW);
7702     }
7703
7704   if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7705     {
7706       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7707       tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7708       tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7709       ftype = build_function_type (ptr_type_node, tmp);
7710       local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7711                             "memset", ECF_NOTHROW);
7712     }
7713
7714   if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7715     {
7716       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7717       ftype = build_function_type (ptr_type_node, tmp);
7718       local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7719                             "alloca", ECF_NOTHROW | ECF_MALLOC);
7720     }
7721
7722   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7723   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7724   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7725   ftype = build_function_type (void_type_node, tmp);
7726   local_define_builtin ("__builtin_init_trampoline", ftype,
7727                         BUILT_IN_INIT_TRAMPOLINE,
7728                         "__builtin_init_trampoline", ECF_NOTHROW);
7729
7730   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7731   ftype = build_function_type (ptr_type_node, tmp);
7732   local_define_builtin ("__builtin_adjust_trampoline", ftype,
7733                         BUILT_IN_ADJUST_TRAMPOLINE,
7734                         "__builtin_adjust_trampoline",
7735                         ECF_CONST | ECF_NOTHROW);
7736
7737   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7738   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7739   ftype = build_function_type (void_type_node, tmp);
7740   local_define_builtin ("__builtin_nonlocal_goto", ftype,
7741                         BUILT_IN_NONLOCAL_GOTO,
7742                         "__builtin_nonlocal_goto",
7743                         ECF_NORETURN | ECF_NOTHROW);
7744
7745   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7746   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7747   ftype = build_function_type (void_type_node, tmp);
7748   local_define_builtin ("__builtin_setjmp_setup", ftype,
7749                         BUILT_IN_SETJMP_SETUP,
7750                         "__builtin_setjmp_setup", ECF_NOTHROW);
7751
7752   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7753   ftype = build_function_type (ptr_type_node, tmp);
7754   local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7755                         BUILT_IN_SETJMP_DISPATCHER,
7756                         "__builtin_setjmp_dispatcher",
7757                         ECF_PURE | ECF_NOTHROW);
7758
7759   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7760   ftype = build_function_type (void_type_node, tmp);
7761   local_define_builtin ("__builtin_setjmp_receiver", ftype,
7762                         BUILT_IN_SETJMP_RECEIVER,
7763                         "__builtin_setjmp_receiver", ECF_NOTHROW);
7764
7765   ftype = build_function_type (ptr_type_node, void_list_node);
7766   local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7767                         "__builtin_stack_save", ECF_NOTHROW);
7768
7769   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7770   ftype = build_function_type (void_type_node, tmp);
7771   local_define_builtin ("__builtin_stack_restore", ftype,
7772                         BUILT_IN_STACK_RESTORE,
7773                         "__builtin_stack_restore", ECF_NOTHROW);
7774
7775   ftype = build_function_type (void_type_node, void_list_node);
7776   local_define_builtin ("__builtin_profile_func_enter", ftype,
7777                         BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7778   local_define_builtin ("__builtin_profile_func_exit", ftype,
7779                         BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7780
7781   /* Complex multiplication and division.  These are handled as builtins
7782      rather than optabs because emit_library_call_value doesn't support
7783      complex.  Further, we can do slightly better with folding these 
7784      beasties if the real and complex parts of the arguments are separate.  */
7785   {
7786     enum machine_mode mode;
7787
7788     for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7789       {
7790         char mode_name_buf[4], *q;
7791         const char *p;
7792         enum built_in_function mcode, dcode;
7793         tree type, inner_type;
7794
7795         type = lang_hooks.types.type_for_mode (mode, 0);
7796         if (type == NULL)
7797           continue;
7798         inner_type = TREE_TYPE (type);
7799
7800         tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7801         tmp = tree_cons (NULL_TREE, inner_type, tmp);
7802         tmp = tree_cons (NULL_TREE, inner_type, tmp);
7803         tmp = tree_cons (NULL_TREE, inner_type, tmp);
7804         ftype = build_function_type (type, tmp);
7805
7806         mcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7807         dcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7808
7809         for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7810           *q = TOLOWER (*p);
7811         *q = '\0';
7812
7813         built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7814         local_define_builtin (built_in_names[mcode], ftype, mcode,
7815                               built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7816
7817         built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7818         local_define_builtin (built_in_names[dcode], ftype, dcode,
7819                               built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7820       }
7821   }
7822 }
7823
7824 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
7825    better way.
7826
7827    If we requested a pointer to a vector, build up the pointers that
7828    we stripped off while looking for the inner type.  Similarly for
7829    return values from functions.
7830
7831    The argument TYPE is the top of the chain, and BOTTOM is the
7832    new type which we will point to.  */
7833
7834 tree
7835 reconstruct_complex_type (tree type, tree bottom)
7836 {
7837   tree inner, outer;
7838   
7839   if (TREE_CODE (type) == POINTER_TYPE)
7840     {
7841       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7842       outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
7843                                            TYPE_REF_CAN_ALIAS_ALL (type));
7844     }
7845   else if (TREE_CODE (type) == REFERENCE_TYPE)
7846     {
7847       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7848       outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
7849                                              TYPE_REF_CAN_ALIAS_ALL (type));
7850     }
7851   else if (TREE_CODE (type) == ARRAY_TYPE)
7852     {
7853       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7854       outer = build_array_type (inner, TYPE_DOMAIN (type));
7855     }
7856   else if (TREE_CODE (type) == FUNCTION_TYPE)
7857     {
7858       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7859       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
7860     }
7861   else if (TREE_CODE (type) == METHOD_TYPE)
7862     {
7863       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7864       /* The build_method_type_directly() routine prepends 'this' to argument list,
7865          so we must compensate by getting rid of it.  */
7866       outer 
7867         = build_method_type_directly 
7868             (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
7869              inner,
7870              TREE_CHAIN (TYPE_ARG_TYPES (type)));
7871     }
7872   else if (TREE_CODE (type) == OFFSET_TYPE)
7873     {
7874       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7875       outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
7876     }
7877   else
7878     return bottom;
7879
7880   return build_qualified_type (outer, TYPE_QUALS (type));
7881 }
7882
7883 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
7884    the inner type.  */
7885 tree
7886 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
7887 {
7888   int nunits;
7889
7890   switch (GET_MODE_CLASS (mode))
7891     {
7892     case MODE_VECTOR_INT:
7893     case MODE_VECTOR_FLOAT:
7894     case MODE_VECTOR_FRACT:
7895     case MODE_VECTOR_UFRACT:
7896     case MODE_VECTOR_ACCUM:
7897     case MODE_VECTOR_UACCUM:
7898       nunits = GET_MODE_NUNITS (mode);
7899       break;
7900
7901     case MODE_INT:
7902       /* Check that there are no leftover bits.  */
7903       gcc_assert (GET_MODE_BITSIZE (mode)
7904                   % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
7905
7906       nunits = GET_MODE_BITSIZE (mode)
7907                / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
7908       break;
7909
7910     default:
7911       gcc_unreachable ();
7912     }
7913
7914   return make_vector_type (innertype, nunits, mode);
7915 }
7916
7917 /* Similarly, but takes the inner type and number of units, which must be
7918    a power of two.  */
7919
7920 tree
7921 build_vector_type (tree innertype, int nunits)
7922 {
7923   return make_vector_type (innertype, nunits, VOIDmode);
7924 }
7925
7926
7927 /* Build RESX_EXPR with given REGION_NUMBER.  */
7928 tree
7929 build_resx (int region_number)
7930 {
7931   tree t;
7932   t = build1 (RESX_EXPR, void_type_node,
7933               build_int_cst (NULL_TREE, region_number));
7934   return t;
7935 }
7936
7937 /* Given an initializer INIT, return TRUE if INIT is zero or some
7938    aggregate of zeros.  Otherwise return FALSE.  */
7939 bool
7940 initializer_zerop (const_tree init)
7941 {
7942   tree elt;
7943
7944   STRIP_NOPS (init);
7945
7946   switch (TREE_CODE (init))
7947     {
7948     case INTEGER_CST:
7949       return integer_zerop (init);
7950
7951     case REAL_CST:
7952       /* ??? Note that this is not correct for C4X float formats.  There,
7953          a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
7954          negative exponent.  */
7955       return real_zerop (init)
7956         && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
7957
7958     case FIXED_CST:
7959       return fixed_zerop (init);
7960
7961     case COMPLEX_CST:
7962       return integer_zerop (init)
7963         || (real_zerop (init)
7964             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
7965             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
7966
7967     case VECTOR_CST:
7968       for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
7969         if (!initializer_zerop (TREE_VALUE (elt)))
7970           return false;
7971       return true;
7972
7973     case CONSTRUCTOR:
7974       {
7975         unsigned HOST_WIDE_INT idx;
7976
7977         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
7978           if (!initializer_zerop (elt))
7979             return false;
7980         return true;
7981       }
7982
7983     default:
7984       return false;
7985     }
7986 }
7987
7988 /* Build an empty statement.  */
7989
7990 tree
7991 build_empty_stmt (void)
7992 {
7993   return build1 (NOP_EXPR, void_type_node, size_zero_node);
7994 }
7995
7996
7997 /* Build an OpenMP clause with code CODE.  */
7998
7999 tree
8000 build_omp_clause (enum omp_clause_code code)
8001 {
8002   tree t;
8003   int size, length;
8004
8005   length = omp_clause_num_ops[code];
8006   size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
8007
8008   t = GGC_NEWVAR (union tree_node, size);
8009   memset (t, 0, size);
8010   TREE_SET_CODE (t, OMP_CLAUSE);
8011   OMP_CLAUSE_SET_CODE (t, code);
8012
8013 #ifdef GATHER_STATISTICS
8014   tree_node_counts[(int) omp_clause_kind]++;
8015   tree_node_sizes[(int) omp_clause_kind] += size;
8016 #endif
8017   
8018   return t;
8019 }
8020
8021 /* Set various status flags when building a CALL_EXPR object T.  */
8022
8023 static void
8024 process_call_operands (tree t)
8025 {
8026   bool side_effects;
8027
8028   side_effects = TREE_SIDE_EFFECTS (t);
8029   if (!side_effects)
8030     {
8031       int i, n;
8032       n = TREE_OPERAND_LENGTH (t);
8033       for (i = 1; i < n; i++)
8034         {
8035           tree op = TREE_OPERAND (t, i);
8036           if (op && TREE_SIDE_EFFECTS (op))
8037             {
8038               side_effects = 1;
8039               break;
8040             }
8041         }
8042     }
8043   if (!side_effects)
8044     {
8045       int i;
8046
8047       /* Calls have side-effects, except those to const or
8048          pure functions.  */
8049       i = call_expr_flags (t);
8050       if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
8051         side_effects = 1;
8052     }
8053   TREE_SIDE_EFFECTS (t) = side_effects;
8054 }
8055
8056 /* Build a tcc_vl_exp object with code CODE and room for LEN operands.  LEN
8057    includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
8058    Except for the CODE and operand count field, other storage for the
8059    object is initialized to zeros.  */
8060
8061 tree
8062 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
8063 {
8064   tree t;
8065   int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
8066
8067   gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
8068   gcc_assert (len >= 1);
8069
8070 #ifdef GATHER_STATISTICS
8071   tree_node_counts[(int) e_kind]++;
8072   tree_node_sizes[(int) e_kind] += length;
8073 #endif
8074
8075   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
8076
8077   memset (t, 0, length);
8078
8079   TREE_SET_CODE (t, code);
8080
8081   /* Can't use TREE_OPERAND to store the length because if checking is
8082      enabled, it will try to check the length before we store it.  :-P  */
8083   t->exp.operands[0] = build_int_cst (sizetype, len);
8084
8085   return t;
8086 }
8087
8088
8089 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
8090    and FN and a null static chain slot.  ARGLIST is a TREE_LIST of the
8091    arguments.  */
8092
8093 tree
8094 build_call_list (tree return_type, tree fn, tree arglist)
8095 {
8096   tree t;
8097   int i;
8098
8099   t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
8100   TREE_TYPE (t) = return_type;
8101   CALL_EXPR_FN (t) = fn;
8102   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8103   for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
8104     CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
8105   process_call_operands (t);
8106   return t;
8107 }
8108
8109 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8110    FN and a null static chain slot.  NARGS is the number of call arguments
8111    which are specified as "..." arguments.  */
8112
8113 tree
8114 build_call_nary (tree return_type, tree fn, int nargs, ...)
8115 {
8116   tree ret;
8117   va_list args;
8118   va_start (args, nargs);
8119   ret = build_call_valist (return_type, fn, nargs, args);
8120   va_end (args);
8121   return ret;
8122 }
8123
8124 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8125    FN and a null static chain slot.  NARGS is the number of call arguments
8126    which are specified as a va_list ARGS.  */
8127
8128 tree
8129 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
8130 {
8131   tree t;
8132   int i;
8133
8134   t = build_vl_exp (CALL_EXPR, nargs + 3);
8135   TREE_TYPE (t) = return_type;
8136   CALL_EXPR_FN (t) = fn;
8137   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8138   for (i = 0; i < nargs; i++)
8139     CALL_EXPR_ARG (t, i) = va_arg (args, tree);
8140   process_call_operands (t);
8141   return t;
8142 }
8143
8144 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8145    FN and a null static chain slot.  NARGS is the number of call arguments
8146    which are specified as a tree array ARGS.  */
8147
8148 tree
8149 build_call_array (tree return_type, tree fn, int nargs, tree *args)
8150 {
8151   tree t;
8152   int i;
8153
8154   t = build_vl_exp (CALL_EXPR, nargs + 3);
8155   TREE_TYPE (t) = return_type;
8156   CALL_EXPR_FN (t) = fn;
8157   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8158   for (i = 0; i < nargs; i++)
8159     CALL_EXPR_ARG (t, i) = args[i];
8160   process_call_operands (t);
8161   return t;
8162 }
8163
8164
8165 /* Returns true if it is possible to prove that the index of
8166    an array access REF (an ARRAY_REF expression) falls into the
8167    array bounds.  */
8168
8169 bool
8170 in_array_bounds_p (tree ref)
8171 {
8172   tree idx = TREE_OPERAND (ref, 1);
8173   tree min, max;
8174
8175   if (TREE_CODE (idx) != INTEGER_CST)
8176     return false;
8177
8178   min = array_ref_low_bound (ref);
8179   max = array_ref_up_bound (ref);
8180   if (!min
8181       || !max
8182       || TREE_CODE (min) != INTEGER_CST
8183       || TREE_CODE (max) != INTEGER_CST)
8184     return false;
8185
8186   if (tree_int_cst_lt (idx, min)
8187       || tree_int_cst_lt (max, idx))
8188     return false;
8189
8190   return true;
8191 }
8192
8193 /* Returns true if it is possible to prove that the range of
8194    an array access REF (an ARRAY_RANGE_REF expression) falls
8195    into the array bounds.  */
8196
8197 bool
8198 range_in_array_bounds_p (tree ref)
8199 {
8200   tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
8201   tree range_min, range_max, min, max;
8202
8203   range_min = TYPE_MIN_VALUE (domain_type);
8204   range_max = TYPE_MAX_VALUE (domain_type);
8205   if (!range_min
8206       || !range_max
8207       || TREE_CODE (range_min) != INTEGER_CST
8208       || TREE_CODE (range_max) != INTEGER_CST)
8209     return false;
8210
8211   min = array_ref_low_bound (ref);
8212   max = array_ref_up_bound (ref);
8213   if (!min
8214       || !max
8215       || TREE_CODE (min) != INTEGER_CST
8216       || TREE_CODE (max) != INTEGER_CST)
8217     return false;
8218
8219   if (tree_int_cst_lt (range_min, min)
8220       || tree_int_cst_lt (max, range_max))
8221     return false;
8222
8223   return true;
8224 }
8225
8226 /* Return true if T (assumed to be a DECL) must be assigned a memory
8227    location.  */
8228
8229 bool
8230 needs_to_live_in_memory (const_tree t)
8231 {
8232   if (TREE_CODE (t) == SSA_NAME)
8233     t = SSA_NAME_VAR (t);
8234
8235   return (TREE_ADDRESSABLE (t)
8236           || is_global_var (t)
8237           || (TREE_CODE (t) == RESULT_DECL
8238               && aggregate_value_p (t, current_function_decl)));
8239 }
8240
8241 /* There are situations in which a language considers record types
8242    compatible which have different field lists.  Decide if two fields
8243    are compatible.  It is assumed that the parent records are compatible.  */
8244
8245 bool
8246 fields_compatible_p (const_tree f1, const_tree f2)
8247 {
8248   if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8249                         DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8250     return false;
8251
8252   if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8253                         DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8254     return false;
8255
8256   if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8257     return false;
8258
8259   return true;
8260 }
8261
8262 /* Locate within RECORD a field that is compatible with ORIG_FIELD.  */
8263
8264 tree
8265 find_compatible_field (tree record, tree orig_field)
8266 {
8267   tree f;
8268
8269   for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8270     if (TREE_CODE (f) == FIELD_DECL
8271         && fields_compatible_p (f, orig_field))
8272       return f;
8273
8274   /* ??? Why isn't this on the main fields list?  */
8275   f = TYPE_VFIELD (record);
8276   if (f && TREE_CODE (f) == FIELD_DECL
8277       && fields_compatible_p (f, orig_field))
8278     return f;
8279
8280   /* ??? We should abort here, but Java appears to do Bad Things
8281      with inherited fields.  */
8282   return orig_field;
8283 }
8284
8285 /* Return value of a constant X and sign-extend it.  */
8286
8287 HOST_WIDE_INT
8288 int_cst_value (const_tree x)
8289 {
8290   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8291   unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8292
8293   /* Make sure the sign-extended value will fit in a HOST_WIDE_INT.  */
8294   gcc_assert (TREE_INT_CST_HIGH (x) == 0
8295               || TREE_INT_CST_HIGH (x) == -1);
8296
8297   if (bits < HOST_BITS_PER_WIDE_INT)
8298     {
8299       bool negative = ((val >> (bits - 1)) & 1) != 0;
8300       if (negative)
8301         val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8302       else
8303         val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8304     }
8305
8306   return val;
8307 }
8308
8309 /* If TYPE is an integral type, return an equivalent type which is
8310     unsigned iff UNSIGNEDP is true.  If TYPE is not an integral type,
8311     return TYPE itself.  */
8312
8313 tree
8314 signed_or_unsigned_type_for (int unsignedp, tree type)
8315 {
8316   tree t = type;
8317   if (POINTER_TYPE_P (type))
8318     t = size_type_node;
8319
8320   if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8321     return t;
8322   
8323   return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8324 }
8325
8326 /* Returns unsigned variant of TYPE.  */
8327
8328 tree
8329 unsigned_type_for (tree type)
8330 {
8331   return signed_or_unsigned_type_for (1, type);
8332 }
8333
8334 /* Returns signed variant of TYPE.  */
8335
8336 tree
8337 signed_type_for (tree type)
8338 {
8339   return signed_or_unsigned_type_for (0, type);
8340 }
8341
8342 /* Returns the largest value obtainable by casting something in INNER type to
8343    OUTER type.  */
8344
8345 tree
8346 upper_bound_in_type (tree outer, tree inner)
8347 {
8348   unsigned HOST_WIDE_INT lo, hi;
8349   unsigned int det = 0;
8350   unsigned oprec = TYPE_PRECISION (outer);
8351   unsigned iprec = TYPE_PRECISION (inner);
8352   unsigned prec;
8353
8354   /* Compute a unique number for every combination.  */
8355   det |= (oprec > iprec) ? 4 : 0;
8356   det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8357   det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8358
8359   /* Determine the exponent to use.  */
8360   switch (det)
8361     {
8362     case 0:
8363     case 1:
8364       /* oprec <= iprec, outer: signed, inner: don't care.  */
8365       prec = oprec - 1;
8366       break;
8367     case 2:
8368     case 3:
8369       /* oprec <= iprec, outer: unsigned, inner: don't care.  */
8370       prec = oprec;
8371       break;
8372     case 4:
8373       /* oprec > iprec, outer: signed, inner: signed.  */
8374       prec = iprec - 1;
8375       break;
8376     case 5:
8377       /* oprec > iprec, outer: signed, inner: unsigned.  */
8378       prec = iprec;
8379       break;
8380     case 6:
8381       /* oprec > iprec, outer: unsigned, inner: signed.  */
8382       prec = oprec;
8383       break;
8384     case 7:
8385       /* oprec > iprec, outer: unsigned, inner: unsigned.  */
8386       prec = iprec;
8387       break;
8388     default:
8389       gcc_unreachable ();
8390     }
8391
8392   /* Compute 2^^prec - 1.  */
8393   if (prec <= HOST_BITS_PER_WIDE_INT)
8394     {
8395       hi = 0;
8396       lo = ((~(unsigned HOST_WIDE_INT) 0)
8397             >> (HOST_BITS_PER_WIDE_INT - prec));
8398     }
8399   else
8400     {
8401       hi = ((~(unsigned HOST_WIDE_INT) 0)
8402             >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8403       lo = ~(unsigned HOST_WIDE_INT) 0;
8404     }
8405
8406   return build_int_cst_wide (outer, lo, hi);
8407 }
8408
8409 /* Returns the smallest value obtainable by casting something in INNER type to
8410    OUTER type.  */
8411
8412 tree
8413 lower_bound_in_type (tree outer, tree inner)
8414 {
8415   unsigned HOST_WIDE_INT lo, hi;
8416   unsigned oprec = TYPE_PRECISION (outer);
8417   unsigned iprec = TYPE_PRECISION (inner);
8418
8419   /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8420      and obtain 0.  */
8421   if (TYPE_UNSIGNED (outer)
8422       /* If we are widening something of an unsigned type, OUTER type
8423          contains all values of INNER type.  In particular, both INNER
8424          and OUTER types have zero in common.  */
8425       || (oprec > iprec && TYPE_UNSIGNED (inner)))
8426     lo = hi = 0;
8427   else
8428     {
8429       /* If we are widening a signed type to another signed type, we
8430          want to obtain -2^^(iprec-1).  If we are keeping the
8431          precision or narrowing to a signed type, we want to obtain
8432          -2^(oprec-1).  */
8433       unsigned prec = oprec > iprec ? iprec : oprec;
8434
8435       if (prec <= HOST_BITS_PER_WIDE_INT)
8436         {
8437           hi = ~(unsigned HOST_WIDE_INT) 0;
8438           lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8439         }
8440       else
8441         {
8442           hi = ((~(unsigned HOST_WIDE_INT) 0)
8443                 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8444           lo = 0;
8445         }
8446     }
8447
8448   return build_int_cst_wide (outer, lo, hi);
8449 }
8450
8451 /* Return nonzero if two operands that are suitable for PHI nodes are
8452    necessarily equal.  Specifically, both ARG0 and ARG1 must be either
8453    SSA_NAME or invariant.  Note that this is strictly an optimization.
8454    That is, callers of this function can directly call operand_equal_p
8455    and get the same result, only slower.  */
8456
8457 int
8458 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8459 {
8460   if (arg0 == arg1)
8461     return 1;
8462   if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8463     return 0;
8464   return operand_equal_p (arg0, arg1, 0);
8465 }
8466
8467 /* Returns number of zeros at the end of binary representation of X.
8468    
8469    ??? Use ffs if available?  */
8470
8471 tree
8472 num_ending_zeros (const_tree x)
8473 {
8474   unsigned HOST_WIDE_INT fr, nfr;
8475   unsigned num, abits;
8476   tree type = TREE_TYPE (x);
8477
8478   if (TREE_INT_CST_LOW (x) == 0)
8479     {
8480       num = HOST_BITS_PER_WIDE_INT;
8481       fr = TREE_INT_CST_HIGH (x);
8482     }
8483   else
8484     {
8485       num = 0;
8486       fr = TREE_INT_CST_LOW (x);
8487     }
8488
8489   for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8490     {
8491       nfr = fr >> abits;
8492       if (nfr << abits == fr)
8493         {
8494           num += abits;
8495           fr = nfr;
8496         }
8497     }
8498
8499   if (num > TYPE_PRECISION (type))
8500     num = TYPE_PRECISION (type);
8501
8502   return build_int_cst_type (type, num);
8503 }
8504
8505
8506 #define WALK_SUBTREE(NODE)                              \
8507   do                                                    \
8508     {                                                   \
8509       result = walk_tree_1 (&(NODE), func, data, pset, lh);     \
8510       if (result)                                       \
8511         return result;                                  \
8512     }                                                   \
8513   while (0)
8514
8515 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8516    be walked whenever a type is seen in the tree.  Rest of operands and return
8517    value are as for walk_tree.  */
8518
8519 static tree
8520 walk_type_fields (tree type, walk_tree_fn func, void *data,
8521                   struct pointer_set_t *pset, walk_tree_lh lh)
8522 {
8523   tree result = NULL_TREE;
8524
8525   switch (TREE_CODE (type))
8526     {
8527     case POINTER_TYPE:
8528     case REFERENCE_TYPE:
8529       /* We have to worry about mutually recursive pointers.  These can't
8530          be written in C.  They can in Ada.  It's pathological, but
8531          there's an ACATS test (c38102a) that checks it.  Deal with this
8532          by checking if we're pointing to another pointer, that one
8533          points to another pointer, that one does too, and we have no htab.
8534          If so, get a hash table.  We check three levels deep to avoid
8535          the cost of the hash table if we don't need one.  */
8536       if (POINTER_TYPE_P (TREE_TYPE (type))
8537           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8538           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8539           && !pset)
8540         {
8541           result = walk_tree_without_duplicates (&TREE_TYPE (type),
8542                                                  func, data);
8543           if (result)
8544             return result;
8545
8546           break;
8547         }
8548
8549       /* ... fall through ... */
8550
8551     case COMPLEX_TYPE:
8552       WALK_SUBTREE (TREE_TYPE (type));
8553       break;
8554
8555     case METHOD_TYPE:
8556       WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8557
8558       /* Fall through.  */
8559
8560     case FUNCTION_TYPE:
8561       WALK_SUBTREE (TREE_TYPE (type));
8562       {
8563         tree arg;
8564
8565         /* We never want to walk into default arguments.  */
8566         for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
8567           WALK_SUBTREE (TREE_VALUE (arg));
8568       }
8569       break;
8570
8571     case ARRAY_TYPE:
8572       /* Don't follow this nodes's type if a pointer for fear that
8573          we'll have infinite recursion.  If we have a PSET, then we
8574          need not fear.  */
8575       if (pset
8576           || (!POINTER_TYPE_P (TREE_TYPE (type))
8577               && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
8578         WALK_SUBTREE (TREE_TYPE (type));
8579       WALK_SUBTREE (TYPE_DOMAIN (type));
8580       break;
8581
8582     case OFFSET_TYPE:
8583       WALK_SUBTREE (TREE_TYPE (type));
8584       WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
8585       break;
8586
8587     default:
8588       break;
8589     }
8590
8591   return NULL_TREE;
8592 }
8593
8594 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.  FUNC is
8595    called with the DATA and the address of each sub-tree.  If FUNC returns a
8596    non-NULL value, the traversal is stopped, and the value returned by FUNC
8597    is returned.  If PSET is non-NULL it is used to record the nodes visited,
8598    and to avoid visiting a node more than once.  */
8599
8600 tree
8601 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
8602              struct pointer_set_t *pset, walk_tree_lh lh)
8603 {
8604   enum tree_code code;
8605   int walk_subtrees;
8606   tree result;
8607
8608 #define WALK_SUBTREE_TAIL(NODE)                         \
8609   do                                                    \
8610     {                                                   \
8611        tp = & (NODE);                                   \
8612        goto tail_recurse;                               \
8613     }                                                   \
8614   while (0)
8615
8616  tail_recurse:
8617   /* Skip empty subtrees.  */
8618   if (!*tp)
8619     return NULL_TREE;
8620
8621   /* Don't walk the same tree twice, if the user has requested
8622      that we avoid doing so.  */
8623   if (pset && pointer_set_insert (pset, *tp))
8624     return NULL_TREE;
8625
8626   /* Call the function.  */
8627   walk_subtrees = 1;
8628   result = (*func) (tp, &walk_subtrees, data);
8629
8630   /* If we found something, return it.  */
8631   if (result)
8632     return result;
8633
8634   code = TREE_CODE (*tp);
8635
8636   /* Even if we didn't, FUNC may have decided that there was nothing
8637      interesting below this point in the tree.  */
8638   if (!walk_subtrees)
8639     {
8640       /* But we still need to check our siblings.  */
8641       if (code == TREE_LIST)
8642         WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8643       else if (code == OMP_CLAUSE)
8644         WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8645       else
8646         return NULL_TREE;
8647     }
8648
8649   if (lh)
8650     {
8651       result = (*lh) (tp, &walk_subtrees, func, data, pset);
8652       if (result || !walk_subtrees)
8653         return result;
8654     }
8655
8656   switch (code)
8657     {
8658     case ERROR_MARK:
8659     case IDENTIFIER_NODE:
8660     case INTEGER_CST:
8661     case REAL_CST:
8662     case FIXED_CST:
8663     case VECTOR_CST:
8664     case STRING_CST:
8665     case BLOCK:
8666     case PLACEHOLDER_EXPR:
8667     case SSA_NAME:
8668     case FIELD_DECL:
8669     case RESULT_DECL:
8670       /* None of these have subtrees other than those already walked
8671          above.  */
8672       break;
8673
8674     case TREE_LIST:
8675       WALK_SUBTREE (TREE_VALUE (*tp));
8676       WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8677       break;
8678
8679     case TREE_VEC:
8680       {
8681         int len = TREE_VEC_LENGTH (*tp);
8682
8683         if (len == 0)
8684           break;
8685
8686         /* Walk all elements but the first.  */
8687         while (--len)
8688           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
8689
8690         /* Now walk the first one as a tail call.  */
8691         WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
8692       }
8693
8694     case COMPLEX_CST:
8695       WALK_SUBTREE (TREE_REALPART (*tp));
8696       WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
8697
8698     case CONSTRUCTOR:
8699       {
8700         unsigned HOST_WIDE_INT idx;
8701         constructor_elt *ce;
8702
8703         for (idx = 0;
8704              VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
8705              idx++)
8706           WALK_SUBTREE (ce->value);
8707       }
8708       break;
8709
8710     case SAVE_EXPR:
8711       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
8712
8713     case BIND_EXPR:
8714       {
8715         tree decl;
8716         for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
8717           {
8718             /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
8719                into declarations that are just mentioned, rather than
8720                declared; they don't really belong to this part of the tree.
8721                And, we can see cycles: the initializer for a declaration
8722                can refer to the declaration itself.  */
8723             WALK_SUBTREE (DECL_INITIAL (decl));
8724             WALK_SUBTREE (DECL_SIZE (decl));
8725             WALK_SUBTREE (DECL_SIZE_UNIT (decl));
8726           }
8727         WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
8728       }
8729
8730     case STATEMENT_LIST:
8731       {
8732         tree_stmt_iterator i;
8733         for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
8734           WALK_SUBTREE (*tsi_stmt_ptr (i));
8735       }
8736       break;
8737
8738     case OMP_CLAUSE:
8739       switch (OMP_CLAUSE_CODE (*tp))
8740         {
8741         case OMP_CLAUSE_PRIVATE:
8742         case OMP_CLAUSE_SHARED:
8743         case OMP_CLAUSE_FIRSTPRIVATE:
8744         case OMP_CLAUSE_COPYIN:
8745         case OMP_CLAUSE_COPYPRIVATE:
8746         case OMP_CLAUSE_IF:
8747         case OMP_CLAUSE_NUM_THREADS:
8748         case OMP_CLAUSE_SCHEDULE:
8749           WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
8750           /* FALLTHRU */
8751
8752         case OMP_CLAUSE_NOWAIT:
8753         case OMP_CLAUSE_ORDERED:
8754         case OMP_CLAUSE_DEFAULT:
8755         case OMP_CLAUSE_UNTIED:
8756           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8757
8758         case OMP_CLAUSE_LASTPRIVATE:
8759           WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
8760           WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
8761           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8762
8763         case OMP_CLAUSE_COLLAPSE:
8764           {
8765             int i;
8766             for (i = 0; i < 3; i++)
8767               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8768             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8769           }
8770
8771         case OMP_CLAUSE_REDUCTION:
8772           {
8773             int i;
8774             for (i = 0; i < 4; i++)
8775               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8776             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8777           }
8778
8779         default:
8780           gcc_unreachable ();
8781         }
8782       break;
8783
8784     case TARGET_EXPR:
8785       {
8786         int i, len;
8787
8788         /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
8789            But, we only want to walk once.  */
8790         len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
8791         for (i = 0; i < len; ++i)
8792           WALK_SUBTREE (TREE_OPERAND (*tp, i));
8793         WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
8794       }
8795
8796     case CHANGE_DYNAMIC_TYPE_EXPR:
8797       WALK_SUBTREE (CHANGE_DYNAMIC_TYPE_NEW_TYPE (*tp));
8798       WALK_SUBTREE_TAIL (CHANGE_DYNAMIC_TYPE_LOCATION (*tp));
8799
8800     case DECL_EXPR:
8801       /* If this is a TYPE_DECL, walk into the fields of the type that it's
8802          defining.  We only want to walk into these fields of a type in this
8803          case and not in the general case of a mere reference to the type.
8804
8805          The criterion is as follows: if the field can be an expression, it
8806          must be walked only here.  This should be in keeping with the fields
8807          that are directly gimplified in gimplify_type_sizes in order for the
8808          mark/copy-if-shared/unmark machinery of the gimplifier to work with
8809          variable-sized types.
8810   
8811          Note that DECLs get walked as part of processing the BIND_EXPR.  */
8812       if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
8813         {
8814           tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
8815           if (TREE_CODE (*type_p) == ERROR_MARK)
8816             return NULL_TREE;
8817
8818           /* Call the function for the type.  See if it returns anything or
8819              doesn't want us to continue.  If we are to continue, walk both
8820              the normal fields and those for the declaration case.  */
8821           result = (*func) (type_p, &walk_subtrees, data);
8822           if (result || !walk_subtrees)
8823             return result;
8824
8825           result = walk_type_fields (*type_p, func, data, pset, lh);
8826           if (result)
8827             return result;
8828
8829           /* If this is a record type, also walk the fields.  */
8830           if (TREE_CODE (*type_p) == RECORD_TYPE
8831               || TREE_CODE (*type_p) == UNION_TYPE
8832               || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8833             {
8834               tree field;
8835
8836               for (field = TYPE_FIELDS (*type_p); field;
8837                    field = TREE_CHAIN (field))
8838                 {
8839                   /* We'd like to look at the type of the field, but we can
8840                      easily get infinite recursion.  So assume it's pointed
8841                      to elsewhere in the tree.  Also, ignore things that
8842                      aren't fields.  */
8843                   if (TREE_CODE (field) != FIELD_DECL)
8844                     continue;
8845
8846                   WALK_SUBTREE (DECL_FIELD_OFFSET (field));
8847                   WALK_SUBTREE (DECL_SIZE (field));
8848                   WALK_SUBTREE (DECL_SIZE_UNIT (field));
8849                   if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8850                     WALK_SUBTREE (DECL_QUALIFIER (field));
8851                 }
8852             }
8853
8854           /* Same for scalar types.  */
8855           else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
8856                    || TREE_CODE (*type_p) == ENUMERAL_TYPE
8857                    || TREE_CODE (*type_p) == INTEGER_TYPE
8858                    || TREE_CODE (*type_p) == FIXED_POINT_TYPE
8859                    || TREE_CODE (*type_p) == REAL_TYPE)
8860             {
8861               WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
8862               WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
8863             }
8864
8865           WALK_SUBTREE (TYPE_SIZE (*type_p));
8866           WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
8867         }
8868       /* FALLTHRU */
8869
8870     default:
8871       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
8872         {
8873           int i, len;
8874
8875           /* Walk over all the sub-trees of this operand.  */
8876           len = TREE_OPERAND_LENGTH (*tp);
8877
8878           /* Go through the subtrees.  We need to do this in forward order so
8879              that the scope of a FOR_EXPR is handled properly.  */
8880           if (len)
8881             {
8882               for (i = 0; i < len - 1; ++i)
8883                 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8884               WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
8885             }
8886         }
8887       /* If this is a type, walk the needed fields in the type.  */
8888       else if (TYPE_P (*tp))
8889         return walk_type_fields (*tp, func, data, pset, lh);
8890       break;
8891     }
8892
8893   /* We didn't find what we were looking for.  */
8894   return NULL_TREE;
8895
8896 #undef WALK_SUBTREE_TAIL
8897 }
8898 #undef WALK_SUBTREE
8899
8900 /* Like walk_tree, but does not walk duplicate nodes more than once.  */
8901
8902 tree
8903 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
8904                                 walk_tree_lh lh)
8905 {
8906   tree result;
8907   struct pointer_set_t *pset;
8908
8909   pset = pointer_set_create ();
8910   result = walk_tree_1 (tp, func, data, pset, lh);
8911   pointer_set_destroy (pset);
8912   return result;
8913 }
8914
8915
8916 tree *
8917 tree_block (tree t)
8918 {
8919   char const c = TREE_CODE_CLASS (TREE_CODE (t));
8920
8921   if (IS_EXPR_CODE_CLASS (c))
8922     return &t->exp.block;
8923   gcc_unreachable ();
8924   return NULL;
8925 }
8926
8927 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
8928    FIXME: don't use this function.  It exists for compatibility with
8929    the old representation of CALL_EXPRs where a list was used to hold the
8930    arguments.  Places that currently extract the arglist from a CALL_EXPR
8931    ought to be rewritten to use the CALL_EXPR itself.  */
8932 tree
8933 call_expr_arglist (tree exp)
8934 {
8935   tree arglist = NULL_TREE;
8936   int i;
8937   for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
8938     arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
8939   return arglist;
8940 }
8941
8942
8943 /* Create a nameless artificial label and put it in the current function
8944    context.  Returns the newly created label.  */
8945
8946 tree
8947 create_artificial_label (void)
8948 {
8949   tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
8950
8951   DECL_ARTIFICIAL (lab) = 1;
8952   DECL_IGNORED_P (lab) = 1;
8953   DECL_CONTEXT (lab) = current_function_decl;
8954   return lab;
8955 }
8956
8957 /*  Given a tree, try to return a useful variable name that we can use
8958     to prefix a temporary that is being assigned the value of the tree.
8959     I.E. given  <temp> = &A, return A.  */
8960
8961 const char *
8962 get_name (tree t)
8963 {
8964   tree stripped_decl;
8965
8966   stripped_decl = t;
8967   STRIP_NOPS (stripped_decl);
8968   if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
8969     return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
8970   else
8971     {
8972       switch (TREE_CODE (stripped_decl))
8973         {
8974         case ADDR_EXPR:
8975           return get_name (TREE_OPERAND (stripped_decl, 0));
8976         default:
8977           return NULL;
8978         }
8979     }
8980 }
8981
8982 /* Return true if TYPE has a variable argument list.  */
8983
8984 bool
8985 stdarg_p (tree fntype)
8986 {
8987   function_args_iterator args_iter;
8988   tree n = NULL_TREE, t;
8989
8990   if (!fntype)
8991     return false;
8992
8993   FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
8994     {
8995       n = t;
8996     }
8997
8998   return n != NULL_TREE && n != void_type_node;
8999 }
9000
9001 /* Return true if TYPE has a prototype.  */
9002
9003 bool
9004 prototype_p (tree fntype)
9005 {
9006   tree t;
9007
9008   gcc_assert (fntype != NULL_TREE);
9009
9010   t = TYPE_ARG_TYPES (fntype);
9011   return (t != NULL_TREE);
9012 }
9013
9014 /* Return the number of arguments that a function has.  */
9015
9016 int
9017 function_args_count (tree fntype)
9018 {
9019   function_args_iterator args_iter;
9020   tree t;
9021   int num = 0;
9022
9023   if (fntype)
9024     {
9025       FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
9026         {
9027           num++;
9028         }
9029     }
9030
9031   return num;
9032 }
9033
9034 /* If BLOCK is inlined from an __attribute__((__artificial__))
9035    routine, return pointer to location from where it has been
9036    called.  */
9037 location_t *
9038 block_nonartificial_location (tree block)
9039 {
9040   location_t *ret = NULL;
9041
9042   while (block && TREE_CODE (block) == BLOCK
9043          && BLOCK_ABSTRACT_ORIGIN (block))
9044     {
9045       tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9046
9047       while (TREE_CODE (ao) == BLOCK
9048              && BLOCK_ABSTRACT_ORIGIN (ao)
9049              && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
9050         ao = BLOCK_ABSTRACT_ORIGIN (ao);
9051
9052       if (TREE_CODE (ao) == FUNCTION_DECL)
9053         {
9054           /* If AO is an artificial inline, point RET to the
9055              call site locus at which it has been inlined and continue
9056              the loop, in case AO's caller is also an artificial
9057              inline.  */
9058           if (DECL_DECLARED_INLINE_P (ao)
9059               && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9060             ret = &BLOCK_SOURCE_LOCATION (block);
9061           else
9062             break;
9063         }
9064       else if (TREE_CODE (ao) != BLOCK)
9065         break;
9066
9067       block = BLOCK_SUPERCONTEXT (block);
9068     }
9069   return ret;
9070 }
9071
9072
9073 /* If EXP is inlined from an __attribute__((__artificial__))
9074    function, return the location of the original call expression.  */
9075
9076 location_t
9077 tree_nonartificial_location (tree exp)
9078 {
9079   tree block = TREE_BLOCK (exp);
9080
9081   while (block
9082          && TREE_CODE (block) == BLOCK
9083          && BLOCK_ABSTRACT_ORIGIN (block))
9084     {
9085       tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9086
9087       do
9088         {
9089           if (TREE_CODE (ao) == FUNCTION_DECL
9090               && DECL_DECLARED_INLINE_P (ao)
9091               && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9092             return BLOCK_SOURCE_LOCATION (block);
9093           else if (TREE_CODE (ao) == BLOCK
9094                    && BLOCK_SUPERCONTEXT (ao) != ao)
9095             ao = BLOCK_SUPERCONTEXT (ao);
9096           else
9097             break;
9098         }
9099       while (ao);
9100
9101       block = BLOCK_SUPERCONTEXT (block);
9102     }
9103
9104   return EXPR_LOCATION (exp);
9105 }
9106
9107
9108 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
9109    nodes.  */
9110
9111 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code.  */
9112
9113 static hashval_t
9114 cl_option_hash_hash (const void *x)
9115 {
9116   const_tree const t = (const_tree) x;
9117   const char *p;
9118   size_t i;
9119   size_t len = 0;
9120   hashval_t hash = 0;
9121
9122   if (TREE_CODE (t) == OPTIMIZATION_NODE)
9123     {
9124       p = (const char *)TREE_OPTIMIZATION (t);
9125       len = sizeof (struct cl_optimization);
9126     }
9127
9128   else if (TREE_CODE (t) == TARGET_OPTION_NODE)
9129     {
9130       p = (const char *)TREE_TARGET_OPTION (t);
9131       len = sizeof (struct cl_target_option);
9132     }
9133
9134   else
9135     gcc_unreachable ();
9136
9137   /* assume most opt flags are just 0/1, some are 2-3, and a few might be
9138      something else.  */
9139   for (i = 0; i < len; i++)
9140     if (p[i])
9141       hash = (hash << 4) ^ ((i << 2) | p[i]);
9142
9143   return hash;
9144 }
9145
9146 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
9147    TARGET_OPTION tree node) is the same as that given by *Y, which is the
9148    same.  */
9149
9150 static int
9151 cl_option_hash_eq (const void *x, const void *y)
9152 {
9153   const_tree const xt = (const_tree) x;
9154   const_tree const yt = (const_tree) y;
9155   const char *xp;
9156   const char *yp;
9157   size_t len;
9158
9159   if (TREE_CODE (xt) != TREE_CODE (yt))
9160     return 0;
9161
9162   if (TREE_CODE (xt) == OPTIMIZATION_NODE)
9163     {
9164       xp = (const char *)TREE_OPTIMIZATION (xt);
9165       yp = (const char *)TREE_OPTIMIZATION (yt);
9166       len = sizeof (struct cl_optimization);
9167     }
9168
9169   else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
9170     {
9171       xp = (const char *)TREE_TARGET_OPTION (xt);
9172       yp = (const char *)TREE_TARGET_OPTION (yt);
9173       len = sizeof (struct cl_target_option);
9174     }
9175
9176   else
9177     gcc_unreachable ();
9178
9179   return (memcmp (xp, yp, len) == 0);
9180 }
9181
9182 /* Build an OPTIMIZATION_NODE based on the current options.  */
9183
9184 tree
9185 build_optimization_node (void)
9186 {
9187   tree t;
9188   void **slot;
9189
9190   /* Use the cache of optimization nodes.  */
9191
9192   cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node));
9193
9194   slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
9195   t = (tree) *slot;
9196   if (!t)
9197     {
9198       /* Insert this one into the hash table.  */
9199       t = cl_optimization_node;
9200       *slot = t;
9201
9202       /* Make a new node for next time round.  */
9203       cl_optimization_node = make_node (OPTIMIZATION_NODE);
9204     }
9205
9206   return t;
9207 }
9208
9209 /* Build a TARGET_OPTION_NODE based on the current options.  */
9210
9211 tree
9212 build_target_option_node (void)
9213 {
9214   tree t;
9215   void **slot;
9216
9217   /* Use the cache of optimization nodes.  */
9218
9219   cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node));
9220
9221   slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
9222   t = (tree) *slot;
9223   if (!t)
9224     {
9225       /* Insert this one into the hash table.  */
9226       t = cl_target_option_node;
9227       *slot = t;
9228
9229       /* Make a new node for next time round.  */
9230       cl_target_option_node = make_node (TARGET_OPTION_NODE);
9231     }
9232
9233   return t;
9234 }
9235
9236 /* Determine the "ultimate origin" of a block.  The block may be an inlined
9237    instance of an inlined instance of a block which is local to an inline
9238    function, so we have to trace all of the way back through the origin chain
9239    to find out what sort of node actually served as the original seed for the
9240    given block.  */
9241
9242 tree
9243 block_ultimate_origin (const_tree block)
9244 {
9245   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
9246
9247   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
9248      nodes in the function to point to themselves; ignore that if
9249      we're trying to output the abstract instance of this function.  */
9250   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
9251     return NULL_TREE;
9252
9253   if (immediate_origin == NULL_TREE)
9254     return NULL_TREE;
9255   else
9256     {
9257       tree ret_val;
9258       tree lookahead = immediate_origin;
9259
9260       do
9261         {
9262           ret_val = lookahead;
9263           lookahead = (TREE_CODE (ret_val) == BLOCK
9264                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
9265         }
9266       while (lookahead != NULL && lookahead != ret_val);
9267
9268       /* The block's abstract origin chain may not be the *ultimate* origin of
9269          the block. It could lead to a DECL that has an abstract origin set.
9270          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
9271          will give us if it has one).  Note that DECL's abstract origins are
9272          supposed to be the most distant ancestor (or so decl_ultimate_origin
9273          claims), so we don't need to loop following the DECL origins.  */
9274       if (DECL_P (ret_val))
9275         return DECL_ORIGIN (ret_val);
9276
9277       return ret_val;
9278     }
9279 }
9280
9281 #include "gt-tree.h"