OSDN Git Service

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