OSDN Git Service

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