2005-09-26 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / tree.c
blob4c4db96a3fe136f5713a5b9258915ad41bedd4a5
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
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.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
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"
54 /* Each tree code class has an associated string representation.
55 These must correspond to the tree_code_class entries. */
57 const char *const tree_code_class_strings[] =
59 "exceptional",
60 "constant",
61 "type",
62 "declaration",
63 "reference",
64 "comparison",
65 "unary",
66 "binary",
67 "statement",
68 "expression",
71 /* obstack.[ch] explicitly declined to prototype this. */
72 extern int _obstack_allocated_p (struct obstack *h, void *obj);
74 #ifdef GATHER_STATISTICS
75 /* Statistics-gathering stuff. */
77 int tree_node_counts[(int) all_kinds];
78 int tree_node_sizes[(int) all_kinds];
80 /* Keep in sync with tree.h:enum tree_node_kind. */
81 static const char * const tree_node_kind_names[] = {
82 "decls",
83 "types",
84 "blocks",
85 "stmts",
86 "refs",
87 "exprs",
88 "constants",
89 "identifiers",
90 "perm_tree_lists",
91 "temp_tree_lists",
92 "vecs",
93 "binfos",
94 "phi_nodes",
95 "ssa names",
96 "constructors",
97 "random kinds",
98 "lang_decl kinds",
99 "lang_type kinds"
101 #endif /* GATHER_STATISTICS */
103 /* Unique id for next decl created. */
104 static GTY(()) int next_decl_uid;
105 /* Unique id for next type created. */
106 static GTY(()) int next_type_uid = 1;
108 /* Since we cannot rehash a type after it is in the table, we have to
109 keep the hash code. */
111 struct type_hash GTY(())
113 unsigned long hash;
114 tree type;
117 /* Initial size of the hash table (rounded to next prime). */
118 #define TYPE_HASH_INITIAL_SIZE 1000
120 /* Now here is the hash table. When recording a type, it is added to
121 the slot whose index is the hash code. Note that the hash table is
122 used for several kinds of types (function types, array types and
123 array index range types, for now). While all these live in the
124 same table, they are completely independent, and the hash code is
125 computed differently for each of these. */
127 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
128 htab_t type_hash_table;
130 /* Hash table and temporary node for larger integer const values. */
131 static GTY (()) tree int_cst_node;
132 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
133 htab_t int_cst_hash_table;
135 /* General tree->tree mapping structure for use in hash tables. */
138 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
139 htab_t debug_expr_for_decl;
141 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
142 htab_t value_expr_for_decl;
144 static GTY ((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
145 htab_t init_priority_for_decl;
147 struct tree_int_map GTY(())
149 tree from;
150 unsigned short to;
152 static unsigned int tree_int_map_hash (const void *);
153 static int tree_int_map_eq (const void *, const void *);
154 static int tree_int_map_marked_p (const void *);
155 static void set_type_quals (tree, int);
156 static int type_hash_eq (const void *, const void *);
157 static hashval_t type_hash_hash (const void *);
158 static hashval_t int_cst_hash_hash (const void *);
159 static int int_cst_hash_eq (const void *, const void *);
160 static void print_type_hash_statistics (void);
161 static void print_debug_expr_statistics (void);
162 static void print_value_expr_statistics (void);
163 static tree make_vector_type (tree, int, enum machine_mode);
164 static int type_hash_marked_p (const void *);
165 static unsigned int type_hash_list (tree, hashval_t);
166 static unsigned int attribute_hash_list (tree, hashval_t);
168 tree global_trees[TI_MAX];
169 tree integer_types[itk_none];
171 unsigned char tree_contains_struct[256][64];
173 /* Init tree.c. */
175 void
176 init_ttree (void)
179 /* Initialize the hash table of types. */
180 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
181 type_hash_eq, 0);
183 debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
184 tree_map_eq, 0);
186 value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
187 tree_map_eq, 0);
188 init_priority_for_decl = htab_create_ggc (512, tree_int_map_hash,
189 tree_int_map_eq, 0);
191 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
192 int_cst_hash_eq, NULL);
194 int_cst_node = make_node (INTEGER_CST);
196 tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
197 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
198 tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
201 tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
202 tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
203 tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
204 tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
205 tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
206 tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
207 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
208 tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
209 tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
212 tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
213 tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
214 tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
215 tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
216 tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
217 tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1;
219 tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
220 tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
221 tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
222 tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
223 tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
224 tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
225 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
226 tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
227 tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
228 tree_contains_struct[STRUCT_FIELD_TAG][TS_DECL_MINIMAL] = 1;
229 tree_contains_struct[NAME_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
230 tree_contains_struct[TYPE_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
232 tree_contains_struct[STRUCT_FIELD_TAG][TS_MEMORY_TAG] = 1;
233 tree_contains_struct[NAME_MEMORY_TAG][TS_MEMORY_TAG] = 1;
234 tree_contains_struct[TYPE_MEMORY_TAG][TS_MEMORY_TAG] = 1;
236 tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
237 tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
238 tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
239 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
241 tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
242 tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
243 tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
244 tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
245 tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
246 tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
247 tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
248 tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
250 lang_hooks.init_ts ();
254 /* The name of the object as the assembler will see it (but before any
255 translations made by ASM_OUTPUT_LABELREF). Often this is the same
256 as DECL_NAME. It is an IDENTIFIER_NODE. */
257 tree
258 decl_assembler_name (tree decl)
260 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
261 lang_hooks.set_decl_assembler_name (decl);
262 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
265 /* Compute the number of bytes occupied by a tree with code CODE.
266 This function cannot be used for TREE_VEC, PHI_NODE, or STRING_CST
267 codes, which are of variable length. */
268 size_t
269 tree_code_size (enum tree_code code)
271 switch (TREE_CODE_CLASS (code))
273 case tcc_declaration: /* A decl node */
275 switch (code)
277 case FIELD_DECL:
278 return sizeof (struct tree_field_decl);
279 case PARM_DECL:
280 return sizeof (struct tree_parm_decl);
281 case VAR_DECL:
282 return sizeof (struct tree_var_decl);
283 case LABEL_DECL:
284 return sizeof (struct tree_label_decl);
285 case RESULT_DECL:
286 return sizeof (struct tree_result_decl);
287 case CONST_DECL:
288 return sizeof (struct tree_const_decl);
289 case TYPE_DECL:
290 return sizeof (struct tree_type_decl);
291 case FUNCTION_DECL:
292 return sizeof (struct tree_function_decl);
293 case NAME_MEMORY_TAG:
294 case TYPE_MEMORY_TAG:
295 case STRUCT_FIELD_TAG:
296 return sizeof (struct tree_memory_tag);
297 default:
298 return sizeof (struct tree_decl_non_common);
302 case tcc_type: /* a type node */
303 return sizeof (struct tree_type);
305 case tcc_reference: /* a reference */
306 case tcc_expression: /* an expression */
307 case tcc_statement: /* an expression with side effects */
308 case tcc_comparison: /* a comparison expression */
309 case tcc_unary: /* a unary arithmetic expression */
310 case tcc_binary: /* a binary arithmetic expression */
311 return (sizeof (struct tree_exp)
312 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
314 case tcc_constant: /* a constant */
315 switch (code)
317 case INTEGER_CST: return sizeof (struct tree_int_cst);
318 case REAL_CST: return sizeof (struct tree_real_cst);
319 case COMPLEX_CST: return sizeof (struct tree_complex);
320 case VECTOR_CST: return sizeof (struct tree_vector);
321 case STRING_CST: gcc_unreachable ();
322 default:
323 return lang_hooks.tree_size (code);
326 case tcc_exceptional: /* something random, like an identifier. */
327 switch (code)
329 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
330 case TREE_LIST: return sizeof (struct tree_list);
332 case ERROR_MARK:
333 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
335 case TREE_VEC:
336 case PHI_NODE: gcc_unreachable ();
338 case SSA_NAME: return sizeof (struct tree_ssa_name);
340 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
341 case BLOCK: return sizeof (struct tree_block);
342 case VALUE_HANDLE: return sizeof (struct tree_value_handle);
343 case CONSTRUCTOR: return sizeof (struct tree_constructor);
345 default:
346 return lang_hooks.tree_size (code);
349 default:
350 gcc_unreachable ();
354 /* Compute the number of bytes occupied by NODE. This routine only
355 looks at TREE_CODE, except for PHI_NODE and TREE_VEC nodes. */
356 size_t
357 tree_size (tree node)
359 enum tree_code code = TREE_CODE (node);
360 switch (code)
362 case PHI_NODE:
363 return (sizeof (struct tree_phi_node)
364 + (PHI_ARG_CAPACITY (node) - 1) * sizeof (struct phi_arg_d));
366 case TREE_BINFO:
367 return (offsetof (struct tree_binfo, base_binfos)
368 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
370 case TREE_VEC:
371 return (sizeof (struct tree_vec)
372 + (TREE_VEC_LENGTH (node) - 1) * sizeof(char *));
374 case STRING_CST:
375 return sizeof (struct tree_string) + TREE_STRING_LENGTH (node) - 1;
377 default:
378 return tree_code_size (code);
382 /* Return a newly allocated node of code CODE. For decl and type
383 nodes, some other fields are initialized. The rest of the node is
384 initialized to zero. This function cannot be used for PHI_NODE or
385 TREE_VEC nodes, which is enforced by asserts in tree_code_size.
387 Achoo! I got a code in the node. */
389 tree
390 make_node_stat (enum tree_code code MEM_STAT_DECL)
392 tree t;
393 enum tree_code_class type = TREE_CODE_CLASS (code);
394 size_t length = tree_code_size (code);
395 #ifdef GATHER_STATISTICS
396 tree_node_kind kind;
398 switch (type)
400 case tcc_declaration: /* A decl node */
401 kind = d_kind;
402 break;
404 case tcc_type: /* a type node */
405 kind = t_kind;
406 break;
408 case tcc_statement: /* an expression with side effects */
409 kind = s_kind;
410 break;
412 case tcc_reference: /* a reference */
413 kind = r_kind;
414 break;
416 case tcc_expression: /* an expression */
417 case tcc_comparison: /* a comparison expression */
418 case tcc_unary: /* a unary arithmetic expression */
419 case tcc_binary: /* a binary arithmetic expression */
420 kind = e_kind;
421 break;
423 case tcc_constant: /* a constant */
424 kind = c_kind;
425 break;
427 case tcc_exceptional: /* something random, like an identifier. */
428 switch (code)
430 case IDENTIFIER_NODE:
431 kind = id_kind;
432 break;
434 case TREE_VEC:
435 kind = vec_kind;
436 break;
438 case TREE_BINFO:
439 kind = binfo_kind;
440 break;
442 case PHI_NODE:
443 kind = phi_kind;
444 break;
446 case SSA_NAME:
447 kind = ssa_name_kind;
448 break;
450 case BLOCK:
451 kind = b_kind;
452 break;
454 case CONSTRUCTOR:
455 kind = constr_kind;
456 break;
458 default:
459 kind = x_kind;
460 break;
462 break;
464 default:
465 gcc_unreachable ();
468 tree_node_counts[(int) kind]++;
469 tree_node_sizes[(int) kind] += length;
470 #endif
472 if (code == IDENTIFIER_NODE)
473 t = ggc_alloc_zone_pass_stat (length, &tree_id_zone);
474 else
475 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
477 memset (t, 0, length);
479 TREE_SET_CODE (t, code);
481 switch (type)
483 case tcc_statement:
484 TREE_SIDE_EFFECTS (t) = 1;
485 break;
487 case tcc_declaration:
488 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
489 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
490 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
492 if (code != FUNCTION_DECL)
493 DECL_ALIGN (t) = 1;
494 DECL_USER_ALIGN (t) = 0;
495 /* We have not yet computed the alias set for this declaration. */
496 DECL_POINTER_ALIAS_SET (t) = -1;
498 DECL_SOURCE_LOCATION (t) = input_location;
499 DECL_UID (t) = next_decl_uid++;
501 break;
503 case tcc_type:
504 TYPE_UID (t) = next_type_uid++;
505 TYPE_ALIGN (t) = BITS_PER_UNIT;
506 TYPE_USER_ALIGN (t) = 0;
507 TYPE_MAIN_VARIANT (t) = t;
509 /* Default to no attributes for type, but let target change that. */
510 TYPE_ATTRIBUTES (t) = NULL_TREE;
511 targetm.set_default_type_attributes (t);
513 /* We have not yet computed the alias set for this type. */
514 TYPE_ALIAS_SET (t) = -1;
515 break;
517 case tcc_constant:
518 TREE_CONSTANT (t) = 1;
519 TREE_INVARIANT (t) = 1;
520 break;
522 case tcc_expression:
523 switch (code)
525 case INIT_EXPR:
526 case MODIFY_EXPR:
527 case VA_ARG_EXPR:
528 case PREDECREMENT_EXPR:
529 case PREINCREMENT_EXPR:
530 case POSTDECREMENT_EXPR:
531 case POSTINCREMENT_EXPR:
532 /* All of these have side-effects, no matter what their
533 operands are. */
534 TREE_SIDE_EFFECTS (t) = 1;
535 break;
537 default:
538 break;
540 break;
542 default:
543 /* Other classes need no special treatment. */
544 break;
547 return t;
550 /* Return a new node with the same contents as NODE except that its
551 TREE_CHAIN is zero and it has a fresh uid. */
553 tree
554 copy_node_stat (tree node MEM_STAT_DECL)
556 tree t;
557 enum tree_code code = TREE_CODE (node);
558 size_t length;
560 gcc_assert (code != STATEMENT_LIST);
562 length = tree_size (node);
563 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
564 memcpy (t, node, length);
566 TREE_CHAIN (t) = 0;
567 TREE_ASM_WRITTEN (t) = 0;
568 TREE_VISITED (t) = 0;
569 t->common.ann = 0;
571 if (TREE_CODE_CLASS (code) == tcc_declaration)
573 DECL_UID (t) = next_decl_uid++;
574 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
575 && DECL_HAS_VALUE_EXPR_P (node))
577 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
578 DECL_HAS_VALUE_EXPR_P (t) = 1;
580 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
582 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
583 DECL_HAS_INIT_PRIORITY_P (t) = 1;
587 else if (TREE_CODE_CLASS (code) == tcc_type)
589 TYPE_UID (t) = next_type_uid++;
590 /* The following is so that the debug code for
591 the copy is different from the original type.
592 The two statements usually duplicate each other
593 (because they clear fields of the same union),
594 but the optimizer should catch that. */
595 TYPE_SYMTAB_POINTER (t) = 0;
596 TYPE_SYMTAB_ADDRESS (t) = 0;
598 /* Do not copy the values cache. */
599 if (TYPE_CACHED_VALUES_P(t))
601 TYPE_CACHED_VALUES_P (t) = 0;
602 TYPE_CACHED_VALUES (t) = NULL_TREE;
606 return t;
609 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
610 For example, this can copy a list made of TREE_LIST nodes. */
612 tree
613 copy_list (tree list)
615 tree head;
616 tree prev, next;
618 if (list == 0)
619 return 0;
621 head = prev = copy_node (list);
622 next = TREE_CHAIN (list);
623 while (next)
625 TREE_CHAIN (prev) = copy_node (next);
626 prev = TREE_CHAIN (prev);
627 next = TREE_CHAIN (next);
629 return head;
633 /* Create an INT_CST node with a LOW value sign extended. */
635 tree
636 build_int_cst (tree type, HOST_WIDE_INT low)
638 return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
641 /* Create an INT_CST node with a LOW value zero extended. */
643 tree
644 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
646 return build_int_cst_wide (type, low, 0);
649 /* Create an INT_CST node with a LOW value in TYPE. The value is sign extended
650 if it is negative. This function is similar to build_int_cst, but
651 the extra bits outside of the type precision are cleared. Constants
652 with these extra bits may confuse the fold so that it detects overflows
653 even in cases when they do not occur, and in general should be avoided.
654 We cannot however make this a default behavior of build_int_cst without
655 more intrusive changes, since there are parts of gcc that rely on the extra
656 precision of the integer constants. */
658 tree
659 build_int_cst_type (tree type, HOST_WIDE_INT low)
661 unsigned HOST_WIDE_INT val = (unsigned HOST_WIDE_INT) low;
662 unsigned HOST_WIDE_INT hi, mask;
663 unsigned bits;
664 bool signed_p;
665 bool negative;
667 if (!type)
668 type = integer_type_node;
670 bits = TYPE_PRECISION (type);
671 signed_p = !TYPE_UNSIGNED (type);
673 if (bits >= HOST_BITS_PER_WIDE_INT)
674 negative = (low < 0);
675 else
677 /* If the sign bit is inside precision of LOW, use it to determine
678 the sign of the constant. */
679 negative = ((val >> (bits - 1)) & 1) != 0;
681 /* Mask out the bits outside of the precision of the constant. */
682 mask = (((unsigned HOST_WIDE_INT) 2) << (bits - 1)) - 1;
684 if (signed_p && negative)
685 val |= ~mask;
686 else
687 val &= mask;
690 /* Determine the high bits. */
691 hi = (negative ? ~(unsigned HOST_WIDE_INT) 0 : 0);
693 /* For unsigned type we need to mask out the bits outside of the type
694 precision. */
695 if (!signed_p)
697 if (bits <= HOST_BITS_PER_WIDE_INT)
698 hi = 0;
699 else
701 bits -= HOST_BITS_PER_WIDE_INT;
702 mask = (((unsigned HOST_WIDE_INT) 2) << (bits - 1)) - 1;
703 hi &= mask;
707 return build_int_cst_wide (type, val, hi);
710 /* These are the hash table functions for the hash table of INTEGER_CST
711 nodes of a sizetype. */
713 /* Return the hash code code X, an INTEGER_CST. */
715 static hashval_t
716 int_cst_hash_hash (const void *x)
718 tree t = (tree) x;
720 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
721 ^ htab_hash_pointer (TREE_TYPE (t)));
724 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
725 is the same as that given by *Y, which is the same. */
727 static int
728 int_cst_hash_eq (const void *x, const void *y)
730 tree xt = (tree) x;
731 tree yt = (tree) y;
733 return (TREE_TYPE (xt) == TREE_TYPE (yt)
734 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
735 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
738 /* Create an INT_CST node of TYPE and value HI:LOW. If TYPE is NULL,
739 integer_type_node is used. The returned node is always shared.
740 For small integers we use a per-type vector cache, for larger ones
741 we use a single hash table. */
743 tree
744 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
746 tree t;
747 int ix = -1;
748 int limit = 0;
750 if (!type)
751 type = integer_type_node;
753 switch (TREE_CODE (type))
755 case POINTER_TYPE:
756 case REFERENCE_TYPE:
757 /* Cache NULL pointer. */
758 if (!hi && !low)
760 limit = 1;
761 ix = 0;
763 break;
765 case BOOLEAN_TYPE:
766 /* Cache false or true. */
767 limit = 2;
768 if (!hi && low < 2)
769 ix = low;
770 break;
772 case INTEGER_TYPE:
773 case CHAR_TYPE:
774 case OFFSET_TYPE:
775 if (TYPE_UNSIGNED (type))
777 /* Cache 0..N */
778 limit = INTEGER_SHARE_LIMIT;
779 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
780 ix = low;
782 else
784 /* Cache -1..N */
785 limit = INTEGER_SHARE_LIMIT + 1;
786 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
787 ix = low + 1;
788 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
789 ix = 0;
791 break;
792 default:
793 break;
796 if (ix >= 0)
798 /* Look for it in the type's vector of small shared ints. */
799 if (!TYPE_CACHED_VALUES_P (type))
801 TYPE_CACHED_VALUES_P (type) = 1;
802 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
805 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
806 if (t)
808 /* Make sure no one is clobbering the shared constant. */
809 gcc_assert (TREE_TYPE (t) == type);
810 gcc_assert (TREE_INT_CST_LOW (t) == low);
811 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
813 else
815 /* Create a new shared int. */
816 t = make_node (INTEGER_CST);
818 TREE_INT_CST_LOW (t) = low;
819 TREE_INT_CST_HIGH (t) = hi;
820 TREE_TYPE (t) = type;
822 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
825 else
827 /* Use the cache of larger shared ints. */
828 void **slot;
830 TREE_INT_CST_LOW (int_cst_node) = low;
831 TREE_INT_CST_HIGH (int_cst_node) = hi;
832 TREE_TYPE (int_cst_node) = type;
834 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
835 t = *slot;
836 if (!t)
838 /* Insert this one into the hash table. */
839 t = int_cst_node;
840 *slot = t;
841 /* Make a new node for next time round. */
842 int_cst_node = make_node (INTEGER_CST);
846 return t;
849 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
850 and the rest are zeros. */
852 tree
853 build_low_bits_mask (tree type, unsigned bits)
855 unsigned HOST_WIDE_INT low;
856 HOST_WIDE_INT high;
857 unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
859 gcc_assert (bits <= TYPE_PRECISION (type));
861 if (bits == TYPE_PRECISION (type)
862 && !TYPE_UNSIGNED (type))
864 /* Sign extended all-ones mask. */
865 low = all_ones;
866 high = -1;
868 else if (bits <= HOST_BITS_PER_WIDE_INT)
870 low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
871 high = 0;
873 else
875 bits -= HOST_BITS_PER_WIDE_INT;
876 low = all_ones;
877 high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
880 return build_int_cst_wide (type, low, high);
883 /* Checks that X is integer constant that can be expressed in (unsigned)
884 HOST_WIDE_INT without loss of precision. */
886 bool
887 cst_and_fits_in_hwi (tree x)
889 if (TREE_CODE (x) != INTEGER_CST)
890 return false;
892 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
893 return false;
895 return (TREE_INT_CST_HIGH (x) == 0
896 || TREE_INT_CST_HIGH (x) == -1);
899 /* Return a new VECTOR_CST node whose type is TYPE and whose values
900 are in a list pointed to by VALS. */
902 tree
903 build_vector (tree type, tree vals)
905 tree v = make_node (VECTOR_CST);
906 int over1 = 0, over2 = 0;
907 tree link;
909 TREE_VECTOR_CST_ELTS (v) = vals;
910 TREE_TYPE (v) = type;
912 /* Iterate through elements and check for overflow. */
913 for (link = vals; link; link = TREE_CHAIN (link))
915 tree value = TREE_VALUE (link);
917 over1 |= TREE_OVERFLOW (value);
918 over2 |= TREE_CONSTANT_OVERFLOW (value);
921 TREE_OVERFLOW (v) = over1;
922 TREE_CONSTANT_OVERFLOW (v) = over2;
924 return v;
927 /* Return a new VECTOR_CST node whose type is TYPE and whose values
928 are extracted from V, a vector of CONSTRUCTOR_ELT. */
930 tree
931 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
933 tree list = NULL_TREE;
934 unsigned HOST_WIDE_INT idx;
935 tree value;
937 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
938 list = tree_cons (NULL_TREE, value, list);
939 return build_vector (type, nreverse (list));
942 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
943 are in the VEC pointed to by VALS. */
944 tree
945 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
947 tree c = make_node (CONSTRUCTOR);
948 TREE_TYPE (c) = type;
949 CONSTRUCTOR_ELTS (c) = vals;
950 return c;
953 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
954 INDEX and VALUE. */
955 tree
956 build_constructor_single (tree type, tree index, tree value)
958 VEC(constructor_elt,gc) *v;
959 constructor_elt *elt;
961 v = VEC_alloc (constructor_elt, gc, 1);
962 elt = VEC_quick_push (constructor_elt, v, NULL);
963 elt->index = index;
964 elt->value = value;
966 return build_constructor (type, v);
970 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
971 are in a list pointed to by VALS. */
972 tree
973 build_constructor_from_list (tree type, tree vals)
975 tree t;
976 VEC(constructor_elt,gc) *v = NULL;
978 if (vals)
980 v = VEC_alloc (constructor_elt, gc, list_length (vals));
981 for (t = vals; t; t = TREE_CHAIN (t))
983 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
984 elt->index = TREE_PURPOSE (t);
985 elt->value = TREE_VALUE (t);
989 return build_constructor (type, v);
993 /* Return a new REAL_CST node whose type is TYPE and value is D. */
995 tree
996 build_real (tree type, REAL_VALUE_TYPE d)
998 tree v;
999 REAL_VALUE_TYPE *dp;
1000 int overflow = 0;
1002 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1003 Consider doing it via real_convert now. */
1005 v = make_node (REAL_CST);
1006 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
1007 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1009 TREE_TYPE (v) = type;
1010 TREE_REAL_CST_PTR (v) = dp;
1011 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1012 return v;
1015 /* Return a new REAL_CST node whose type is TYPE
1016 and whose value is the integer value of the INTEGER_CST node I. */
1018 REAL_VALUE_TYPE
1019 real_value_from_int_cst (tree type, tree i)
1021 REAL_VALUE_TYPE d;
1023 /* Clear all bits of the real value type so that we can later do
1024 bitwise comparisons to see if two values are the same. */
1025 memset (&d, 0, sizeof d);
1027 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1028 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1029 TYPE_UNSIGNED (TREE_TYPE (i)));
1030 return d;
1033 /* Given a tree representing an integer constant I, return a tree
1034 representing the same value as a floating-point constant of type TYPE. */
1036 tree
1037 build_real_from_int_cst (tree type, tree i)
1039 tree v;
1040 int overflow = TREE_OVERFLOW (i);
1042 v = build_real (type, real_value_from_int_cst (type, i));
1044 TREE_OVERFLOW (v) |= overflow;
1045 TREE_CONSTANT_OVERFLOW (v) |= overflow;
1046 return v;
1049 /* Return a newly constructed STRING_CST node whose value is
1050 the LEN characters at STR.
1051 The TREE_TYPE is not initialized. */
1053 tree
1054 build_string (int len, const char *str)
1056 tree s;
1057 size_t length;
1059 length = len + sizeof (struct tree_string);
1061 #ifdef GATHER_STATISTICS
1062 tree_node_counts[(int) c_kind]++;
1063 tree_node_sizes[(int) c_kind] += length;
1064 #endif
1066 s = ggc_alloc_tree (length);
1068 memset (s, 0, sizeof (struct tree_common));
1069 TREE_SET_CODE (s, STRING_CST);
1070 TREE_CONSTANT (s) = 1;
1071 TREE_INVARIANT (s) = 1;
1072 TREE_STRING_LENGTH (s) = len;
1073 memcpy ((char *) TREE_STRING_POINTER (s), str, len);
1074 ((char *) TREE_STRING_POINTER (s))[len] = '\0';
1076 return s;
1079 /* Return a newly constructed COMPLEX_CST node whose value is
1080 specified by the real and imaginary parts REAL and IMAG.
1081 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1082 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1084 tree
1085 build_complex (tree type, tree real, tree imag)
1087 tree t = make_node (COMPLEX_CST);
1089 TREE_REALPART (t) = real;
1090 TREE_IMAGPART (t) = imag;
1091 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1092 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1093 TREE_CONSTANT_OVERFLOW (t)
1094 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1095 return t;
1098 /* Build a BINFO with LEN language slots. */
1100 tree
1101 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1103 tree t;
1104 size_t length = (offsetof (struct tree_binfo, base_binfos)
1105 + VEC_embedded_size (tree, base_binfos));
1107 #ifdef GATHER_STATISTICS
1108 tree_node_counts[(int) binfo_kind]++;
1109 tree_node_sizes[(int) binfo_kind] += length;
1110 #endif
1112 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
1114 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1116 TREE_SET_CODE (t, TREE_BINFO);
1118 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1120 return t;
1124 /* Build a newly constructed TREE_VEC node of length LEN. */
1126 tree
1127 make_tree_vec_stat (int len MEM_STAT_DECL)
1129 tree t;
1130 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1132 #ifdef GATHER_STATISTICS
1133 tree_node_counts[(int) vec_kind]++;
1134 tree_node_sizes[(int) vec_kind] += length;
1135 #endif
1137 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
1139 memset (t, 0, length);
1141 TREE_SET_CODE (t, TREE_VEC);
1142 TREE_VEC_LENGTH (t) = len;
1144 return t;
1147 /* Return 1 if EXPR is the integer constant zero or a complex constant
1148 of zero. */
1151 integer_zerop (tree expr)
1153 STRIP_NOPS (expr);
1155 return ((TREE_CODE (expr) == INTEGER_CST
1156 && ! TREE_CONSTANT_OVERFLOW (expr)
1157 && TREE_INT_CST_LOW (expr) == 0
1158 && TREE_INT_CST_HIGH (expr) == 0)
1159 || (TREE_CODE (expr) == COMPLEX_CST
1160 && integer_zerop (TREE_REALPART (expr))
1161 && integer_zerop (TREE_IMAGPART (expr))));
1164 /* Return 1 if EXPR is the integer constant one or the corresponding
1165 complex constant. */
1168 integer_onep (tree expr)
1170 STRIP_NOPS (expr);
1172 return ((TREE_CODE (expr) == INTEGER_CST
1173 && ! TREE_CONSTANT_OVERFLOW (expr)
1174 && TREE_INT_CST_LOW (expr) == 1
1175 && TREE_INT_CST_HIGH (expr) == 0)
1176 || (TREE_CODE (expr) == COMPLEX_CST
1177 && integer_onep (TREE_REALPART (expr))
1178 && integer_zerop (TREE_IMAGPART (expr))));
1181 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1182 it contains. Likewise for the corresponding complex constant. */
1185 integer_all_onesp (tree expr)
1187 int prec;
1188 int uns;
1190 STRIP_NOPS (expr);
1192 if (TREE_CODE (expr) == COMPLEX_CST
1193 && integer_all_onesp (TREE_REALPART (expr))
1194 && integer_zerop (TREE_IMAGPART (expr)))
1195 return 1;
1197 else if (TREE_CODE (expr) != INTEGER_CST
1198 || TREE_CONSTANT_OVERFLOW (expr))
1199 return 0;
1201 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1202 if (!uns)
1203 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1204 && TREE_INT_CST_HIGH (expr) == -1);
1206 /* Note that using TYPE_PRECISION here is wrong. We care about the
1207 actual bits, not the (arbitrary) range of the type. */
1208 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1209 if (prec >= HOST_BITS_PER_WIDE_INT)
1211 HOST_WIDE_INT high_value;
1212 int shift_amount;
1214 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1216 /* Can not handle precisions greater than twice the host int size. */
1217 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1218 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1219 /* Shifting by the host word size is undefined according to the ANSI
1220 standard, so we must handle this as a special case. */
1221 high_value = -1;
1222 else
1223 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1225 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1226 && TREE_INT_CST_HIGH (expr) == high_value);
1228 else
1229 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1232 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1233 one bit on). */
1236 integer_pow2p (tree expr)
1238 int prec;
1239 HOST_WIDE_INT high, low;
1241 STRIP_NOPS (expr);
1243 if (TREE_CODE (expr) == COMPLEX_CST
1244 && integer_pow2p (TREE_REALPART (expr))
1245 && integer_zerop (TREE_IMAGPART (expr)))
1246 return 1;
1248 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
1249 return 0;
1251 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1252 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1253 high = TREE_INT_CST_HIGH (expr);
1254 low = TREE_INT_CST_LOW (expr);
1256 /* First clear all bits that are beyond the type's precision in case
1257 we've been sign extended. */
1259 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1261 else if (prec > HOST_BITS_PER_WIDE_INT)
1262 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1263 else
1265 high = 0;
1266 if (prec < HOST_BITS_PER_WIDE_INT)
1267 low &= ~((HOST_WIDE_INT) (-1) << prec);
1270 if (high == 0 && low == 0)
1271 return 0;
1273 return ((high == 0 && (low & (low - 1)) == 0)
1274 || (low == 0 && (high & (high - 1)) == 0));
1277 /* Return 1 if EXPR is an integer constant other than zero or a
1278 complex constant other than zero. */
1281 integer_nonzerop (tree expr)
1283 STRIP_NOPS (expr);
1285 return ((TREE_CODE (expr) == INTEGER_CST
1286 && ! TREE_CONSTANT_OVERFLOW (expr)
1287 && (TREE_INT_CST_LOW (expr) != 0
1288 || TREE_INT_CST_HIGH (expr) != 0))
1289 || (TREE_CODE (expr) == COMPLEX_CST
1290 && (integer_nonzerop (TREE_REALPART (expr))
1291 || integer_nonzerop (TREE_IMAGPART (expr)))));
1294 /* Return the power of two represented by a tree node known to be a
1295 power of two. */
1298 tree_log2 (tree expr)
1300 int prec;
1301 HOST_WIDE_INT high, low;
1303 STRIP_NOPS (expr);
1305 if (TREE_CODE (expr) == COMPLEX_CST)
1306 return tree_log2 (TREE_REALPART (expr));
1308 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1309 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1311 high = TREE_INT_CST_HIGH (expr);
1312 low = TREE_INT_CST_LOW (expr);
1314 /* First clear all bits that are beyond the type's precision in case
1315 we've been sign extended. */
1317 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1319 else if (prec > HOST_BITS_PER_WIDE_INT)
1320 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1321 else
1323 high = 0;
1324 if (prec < HOST_BITS_PER_WIDE_INT)
1325 low &= ~((HOST_WIDE_INT) (-1) << prec);
1328 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1329 : exact_log2 (low));
1332 /* Similar, but return the largest integer Y such that 2 ** Y is less
1333 than or equal to EXPR. */
1336 tree_floor_log2 (tree expr)
1338 int prec;
1339 HOST_WIDE_INT high, low;
1341 STRIP_NOPS (expr);
1343 if (TREE_CODE (expr) == COMPLEX_CST)
1344 return tree_log2 (TREE_REALPART (expr));
1346 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1347 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1349 high = TREE_INT_CST_HIGH (expr);
1350 low = TREE_INT_CST_LOW (expr);
1352 /* First clear all bits that are beyond the type's precision in case
1353 we've been sign extended. Ignore if type's precision hasn't been set
1354 since what we are doing is setting it. */
1356 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1358 else if (prec > HOST_BITS_PER_WIDE_INT)
1359 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1360 else
1362 high = 0;
1363 if (prec < HOST_BITS_PER_WIDE_INT)
1364 low &= ~((HOST_WIDE_INT) (-1) << prec);
1367 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1368 : floor_log2 (low));
1371 /* Return 1 if EXPR is the real constant zero. */
1374 real_zerop (tree expr)
1376 STRIP_NOPS (expr);
1378 return ((TREE_CODE (expr) == REAL_CST
1379 && ! TREE_CONSTANT_OVERFLOW (expr)
1380 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1381 || (TREE_CODE (expr) == COMPLEX_CST
1382 && real_zerop (TREE_REALPART (expr))
1383 && real_zerop (TREE_IMAGPART (expr))));
1386 /* Return 1 if EXPR is the real constant one in real or complex form. */
1389 real_onep (tree expr)
1391 STRIP_NOPS (expr);
1393 return ((TREE_CODE (expr) == REAL_CST
1394 && ! TREE_CONSTANT_OVERFLOW (expr)
1395 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1396 || (TREE_CODE (expr) == COMPLEX_CST
1397 && real_onep (TREE_REALPART (expr))
1398 && real_zerop (TREE_IMAGPART (expr))));
1401 /* Return 1 if EXPR is the real constant two. */
1404 real_twop (tree expr)
1406 STRIP_NOPS (expr);
1408 return ((TREE_CODE (expr) == REAL_CST
1409 && ! TREE_CONSTANT_OVERFLOW (expr)
1410 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1411 || (TREE_CODE (expr) == COMPLEX_CST
1412 && real_twop (TREE_REALPART (expr))
1413 && real_zerop (TREE_IMAGPART (expr))));
1416 /* Return 1 if EXPR is the real constant minus one. */
1419 real_minus_onep (tree expr)
1421 STRIP_NOPS (expr);
1423 return ((TREE_CODE (expr) == REAL_CST
1424 && ! TREE_CONSTANT_OVERFLOW (expr)
1425 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1426 || (TREE_CODE (expr) == COMPLEX_CST
1427 && real_minus_onep (TREE_REALPART (expr))
1428 && real_zerop (TREE_IMAGPART (expr))));
1431 /* Nonzero if EXP is a constant or a cast of a constant. */
1434 really_constant_p (tree exp)
1436 /* This is not quite the same as STRIP_NOPS. It does more. */
1437 while (TREE_CODE (exp) == NOP_EXPR
1438 || TREE_CODE (exp) == CONVERT_EXPR
1439 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1440 exp = TREE_OPERAND (exp, 0);
1441 return TREE_CONSTANT (exp);
1444 /* Return first list element whose TREE_VALUE is ELEM.
1445 Return 0 if ELEM is not in LIST. */
1447 tree
1448 value_member (tree elem, tree list)
1450 while (list)
1452 if (elem == TREE_VALUE (list))
1453 return list;
1454 list = TREE_CHAIN (list);
1456 return NULL_TREE;
1459 /* Return first list element whose TREE_PURPOSE is ELEM.
1460 Return 0 if ELEM is not in LIST. */
1462 tree
1463 purpose_member (tree elem, tree list)
1465 while (list)
1467 if (elem == TREE_PURPOSE (list))
1468 return list;
1469 list = TREE_CHAIN (list);
1471 return NULL_TREE;
1474 /* Return nonzero if ELEM is part of the chain CHAIN. */
1477 chain_member (tree elem, tree chain)
1479 while (chain)
1481 if (elem == chain)
1482 return 1;
1483 chain = TREE_CHAIN (chain);
1486 return 0;
1489 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1490 We expect a null pointer to mark the end of the chain.
1491 This is the Lisp primitive `length'. */
1494 list_length (tree t)
1496 tree p = t;
1497 #ifdef ENABLE_TREE_CHECKING
1498 tree q = t;
1499 #endif
1500 int len = 0;
1502 while (p)
1504 p = TREE_CHAIN (p);
1505 #ifdef ENABLE_TREE_CHECKING
1506 if (len % 2)
1507 q = TREE_CHAIN (q);
1508 gcc_assert (p != q);
1509 #endif
1510 len++;
1513 return len;
1516 /* Returns the number of FIELD_DECLs in TYPE. */
1519 fields_length (tree type)
1521 tree t = TYPE_FIELDS (type);
1522 int count = 0;
1524 for (; t; t = TREE_CHAIN (t))
1525 if (TREE_CODE (t) == FIELD_DECL)
1526 ++count;
1528 return count;
1531 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1532 by modifying the last node in chain 1 to point to chain 2.
1533 This is the Lisp primitive `nconc'. */
1535 tree
1536 chainon (tree op1, tree op2)
1538 tree t1;
1540 if (!op1)
1541 return op2;
1542 if (!op2)
1543 return op1;
1545 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1546 continue;
1547 TREE_CHAIN (t1) = op2;
1549 #ifdef ENABLE_TREE_CHECKING
1551 tree t2;
1552 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1553 gcc_assert (t2 != t1);
1555 #endif
1557 return op1;
1560 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1562 tree
1563 tree_last (tree chain)
1565 tree next;
1566 if (chain)
1567 while ((next = TREE_CHAIN (chain)))
1568 chain = next;
1569 return chain;
1572 /* Reverse the order of elements in the chain T,
1573 and return the new head of the chain (old last element). */
1575 tree
1576 nreverse (tree t)
1578 tree prev = 0, decl, next;
1579 for (decl = t; decl; decl = next)
1581 next = TREE_CHAIN (decl);
1582 TREE_CHAIN (decl) = prev;
1583 prev = decl;
1585 return prev;
1588 /* Return a newly created TREE_LIST node whose
1589 purpose and value fields are PARM and VALUE. */
1591 tree
1592 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1594 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1595 TREE_PURPOSE (t) = parm;
1596 TREE_VALUE (t) = value;
1597 return t;
1600 /* Return a newly created TREE_LIST node whose
1601 purpose and value fields are PURPOSE and VALUE
1602 and whose TREE_CHAIN is CHAIN. */
1604 tree
1605 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1607 tree node;
1609 node = ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1611 memset (node, 0, sizeof (struct tree_common));
1613 #ifdef GATHER_STATISTICS
1614 tree_node_counts[(int) x_kind]++;
1615 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1616 #endif
1618 TREE_SET_CODE (node, TREE_LIST);
1619 TREE_CHAIN (node) = chain;
1620 TREE_PURPOSE (node) = purpose;
1621 TREE_VALUE (node) = value;
1622 return node;
1626 /* Return the size nominally occupied by an object of type TYPE
1627 when it resides in memory. The value is measured in units of bytes,
1628 and its data type is that normally used for type sizes
1629 (which is the first type created by make_signed_type or
1630 make_unsigned_type). */
1632 tree
1633 size_in_bytes (tree type)
1635 tree t;
1637 if (type == error_mark_node)
1638 return integer_zero_node;
1640 type = TYPE_MAIN_VARIANT (type);
1641 t = TYPE_SIZE_UNIT (type);
1643 if (t == 0)
1645 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1646 return size_zero_node;
1649 if (TREE_CODE (t) == INTEGER_CST)
1650 t = force_fit_type (t, 0, false, false);
1652 return t;
1655 /* Return the size of TYPE (in bytes) as a wide integer
1656 or return -1 if the size can vary or is larger than an integer. */
1658 HOST_WIDE_INT
1659 int_size_in_bytes (tree type)
1661 tree t;
1663 if (type == error_mark_node)
1664 return 0;
1666 type = TYPE_MAIN_VARIANT (type);
1667 t = TYPE_SIZE_UNIT (type);
1668 if (t == 0
1669 || TREE_CODE (t) != INTEGER_CST
1670 || TREE_OVERFLOW (t)
1671 || TREE_INT_CST_HIGH (t) != 0
1672 /* If the result would appear negative, it's too big to represent. */
1673 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1674 return -1;
1676 return TREE_INT_CST_LOW (t);
1679 /* Return the bit position of FIELD, in bits from the start of the record.
1680 This is a tree of type bitsizetype. */
1682 tree
1683 bit_position (tree field)
1685 return bit_from_pos (DECL_FIELD_OFFSET (field),
1686 DECL_FIELD_BIT_OFFSET (field));
1689 /* Likewise, but return as an integer. It must be representable in
1690 that way (since it could be a signed value, we don't have the
1691 option of returning -1 like int_size_in_byte can. */
1693 HOST_WIDE_INT
1694 int_bit_position (tree field)
1696 return tree_low_cst (bit_position (field), 0);
1699 /* Return the byte position of FIELD, in bytes from the start of the record.
1700 This is a tree of type sizetype. */
1702 tree
1703 byte_position (tree field)
1705 return byte_from_pos (DECL_FIELD_OFFSET (field),
1706 DECL_FIELD_BIT_OFFSET (field));
1709 /* Likewise, but return as an integer. It must be representable in
1710 that way (since it could be a signed value, we don't have the
1711 option of returning -1 like int_size_in_byte can. */
1713 HOST_WIDE_INT
1714 int_byte_position (tree field)
1716 return tree_low_cst (byte_position (field), 0);
1719 /* Return the strictest alignment, in bits, that T is known to have. */
1721 unsigned int
1722 expr_align (tree t)
1724 unsigned int align0, align1;
1726 switch (TREE_CODE (t))
1728 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1729 /* If we have conversions, we know that the alignment of the
1730 object must meet each of the alignments of the types. */
1731 align0 = expr_align (TREE_OPERAND (t, 0));
1732 align1 = TYPE_ALIGN (TREE_TYPE (t));
1733 return MAX (align0, align1);
1735 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1736 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1737 case CLEANUP_POINT_EXPR:
1738 /* These don't change the alignment of an object. */
1739 return expr_align (TREE_OPERAND (t, 0));
1741 case COND_EXPR:
1742 /* The best we can do is say that the alignment is the least aligned
1743 of the two arms. */
1744 align0 = expr_align (TREE_OPERAND (t, 1));
1745 align1 = expr_align (TREE_OPERAND (t, 2));
1746 return MIN (align0, align1);
1748 case LABEL_DECL: case CONST_DECL:
1749 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1750 if (DECL_ALIGN (t) != 0)
1751 return DECL_ALIGN (t);
1752 break;
1754 case FUNCTION_DECL:
1755 return FUNCTION_BOUNDARY;
1757 default:
1758 break;
1761 /* Otherwise take the alignment from that of the type. */
1762 return TYPE_ALIGN (TREE_TYPE (t));
1765 /* Return, as a tree node, the number of elements for TYPE (which is an
1766 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1768 tree
1769 array_type_nelts (tree type)
1771 tree index_type, min, max;
1773 /* If they did it with unspecified bounds, then we should have already
1774 given an error about it before we got here. */
1775 if (! TYPE_DOMAIN (type))
1776 return error_mark_node;
1778 index_type = TYPE_DOMAIN (type);
1779 min = TYPE_MIN_VALUE (index_type);
1780 max = TYPE_MAX_VALUE (index_type);
1782 return (integer_zerop (min)
1783 ? max
1784 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
1787 /* If arg is static -- a reference to an object in static storage -- then
1788 return the object. This is not the same as the C meaning of `static'.
1789 If arg isn't static, return NULL. */
1791 tree
1792 staticp (tree arg)
1794 switch (TREE_CODE (arg))
1796 case FUNCTION_DECL:
1797 /* Nested functions are static, even though taking their address will
1798 involve a trampoline as we unnest the nested function and create
1799 the trampoline on the tree level. */
1800 return arg;
1802 case VAR_DECL:
1803 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1804 && ! DECL_THREAD_LOCAL_P (arg)
1805 && ! DECL_NON_ADDR_CONST_P (arg)
1806 ? arg : NULL);
1808 case CONST_DECL:
1809 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1810 ? arg : NULL);
1812 case CONSTRUCTOR:
1813 return TREE_STATIC (arg) ? arg : NULL;
1815 case LABEL_DECL:
1816 case STRING_CST:
1817 return arg;
1819 case COMPONENT_REF:
1820 /* If the thing being referenced is not a field, then it is
1821 something language specific. */
1822 if (TREE_CODE (TREE_OPERAND (arg, 1)) != FIELD_DECL)
1823 return (*lang_hooks.staticp) (arg);
1825 /* If we are referencing a bitfield, we can't evaluate an
1826 ADDR_EXPR at compile time and so it isn't a constant. */
1827 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
1828 return NULL;
1830 return staticp (TREE_OPERAND (arg, 0));
1832 case BIT_FIELD_REF:
1833 return NULL;
1835 case MISALIGNED_INDIRECT_REF:
1836 case ALIGN_INDIRECT_REF:
1837 case INDIRECT_REF:
1838 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
1840 case ARRAY_REF:
1841 case ARRAY_RANGE_REF:
1842 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1843 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1844 return staticp (TREE_OPERAND (arg, 0));
1845 else
1846 return false;
1848 default:
1849 if ((unsigned int) TREE_CODE (arg)
1850 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1851 return lang_hooks.staticp (arg);
1852 else
1853 return NULL;
1857 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1858 Do this to any expression which may be used in more than one place,
1859 but must be evaluated only once.
1861 Normally, expand_expr would reevaluate the expression each time.
1862 Calling save_expr produces something that is evaluated and recorded
1863 the first time expand_expr is called on it. Subsequent calls to
1864 expand_expr just reuse the recorded value.
1866 The call to expand_expr that generates code that actually computes
1867 the value is the first call *at compile time*. Subsequent calls
1868 *at compile time* generate code to use the saved value.
1869 This produces correct result provided that *at run time* control
1870 always flows through the insns made by the first expand_expr
1871 before reaching the other places where the save_expr was evaluated.
1872 You, the caller of save_expr, must make sure this is so.
1874 Constants, and certain read-only nodes, are returned with no
1875 SAVE_EXPR because that is safe. Expressions containing placeholders
1876 are not touched; see tree.def for an explanation of what these
1877 are used for. */
1879 tree
1880 save_expr (tree expr)
1882 tree t = fold (expr);
1883 tree inner;
1885 /* If the tree evaluates to a constant, then we don't want to hide that
1886 fact (i.e. this allows further folding, and direct checks for constants).
1887 However, a read-only object that has side effects cannot be bypassed.
1888 Since it is no problem to reevaluate literals, we just return the
1889 literal node. */
1890 inner = skip_simple_arithmetic (t);
1892 if (TREE_INVARIANT (inner)
1893 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1894 || TREE_CODE (inner) == SAVE_EXPR
1895 || TREE_CODE (inner) == ERROR_MARK)
1896 return t;
1898 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1899 it means that the size or offset of some field of an object depends on
1900 the value within another field.
1902 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1903 and some variable since it would then need to be both evaluated once and
1904 evaluated more than once. Front-ends must assure this case cannot
1905 happen by surrounding any such subexpressions in their own SAVE_EXPR
1906 and forcing evaluation at the proper time. */
1907 if (contains_placeholder_p (inner))
1908 return t;
1910 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
1912 /* This expression might be placed ahead of a jump to ensure that the
1913 value was computed on both sides of the jump. So make sure it isn't
1914 eliminated as dead. */
1915 TREE_SIDE_EFFECTS (t) = 1;
1916 TREE_INVARIANT (t) = 1;
1917 return t;
1920 /* Look inside EXPR and into any simple arithmetic operations. Return
1921 the innermost non-arithmetic node. */
1923 tree
1924 skip_simple_arithmetic (tree expr)
1926 tree inner;
1928 /* We don't care about whether this can be used as an lvalue in this
1929 context. */
1930 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1931 expr = TREE_OPERAND (expr, 0);
1933 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1934 a constant, it will be more efficient to not make another SAVE_EXPR since
1935 it will allow better simplification and GCSE will be able to merge the
1936 computations if they actually occur. */
1937 inner = expr;
1938 while (1)
1940 if (UNARY_CLASS_P (inner))
1941 inner = TREE_OPERAND (inner, 0);
1942 else if (BINARY_CLASS_P (inner))
1944 if (TREE_INVARIANT (TREE_OPERAND (inner, 1)))
1945 inner = TREE_OPERAND (inner, 0);
1946 else if (TREE_INVARIANT (TREE_OPERAND (inner, 0)))
1947 inner = TREE_OPERAND (inner, 1);
1948 else
1949 break;
1951 else
1952 break;
1955 return inner;
1958 /* Return which tree structure is used by T. */
1960 enum tree_node_structure_enum
1961 tree_node_structure (tree t)
1963 enum tree_code code = TREE_CODE (t);
1965 switch (TREE_CODE_CLASS (code))
1967 case tcc_declaration:
1969 switch (code)
1971 case FIELD_DECL:
1972 return TS_FIELD_DECL;
1973 case PARM_DECL:
1974 return TS_PARM_DECL;
1975 case VAR_DECL:
1976 return TS_VAR_DECL;
1977 case LABEL_DECL:
1978 return TS_LABEL_DECL;
1979 case RESULT_DECL:
1980 return TS_RESULT_DECL;
1981 case CONST_DECL:
1982 return TS_CONST_DECL;
1983 case TYPE_DECL:
1984 return TS_TYPE_DECL;
1985 case FUNCTION_DECL:
1986 return TS_FUNCTION_DECL;
1987 case TYPE_MEMORY_TAG:
1988 case NAME_MEMORY_TAG:
1989 case STRUCT_FIELD_TAG:
1990 return TS_MEMORY_TAG;
1991 default:
1992 return TS_DECL_NON_COMMON;
1995 case tcc_type:
1996 return TS_TYPE;
1997 case tcc_reference:
1998 case tcc_comparison:
1999 case tcc_unary:
2000 case tcc_binary:
2001 case tcc_expression:
2002 case tcc_statement:
2003 return TS_EXP;
2004 default: /* tcc_constant and tcc_exceptional */
2005 break;
2007 switch (code)
2009 /* tcc_constant cases. */
2010 case INTEGER_CST: return TS_INT_CST;
2011 case REAL_CST: return TS_REAL_CST;
2012 case COMPLEX_CST: return TS_COMPLEX;
2013 case VECTOR_CST: return TS_VECTOR;
2014 case STRING_CST: return TS_STRING;
2015 /* tcc_exceptional cases. */
2016 case ERROR_MARK: return TS_COMMON;
2017 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2018 case TREE_LIST: return TS_LIST;
2019 case TREE_VEC: return TS_VEC;
2020 case PHI_NODE: return TS_PHI_NODE;
2021 case SSA_NAME: return TS_SSA_NAME;
2022 case PLACEHOLDER_EXPR: return TS_COMMON;
2023 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2024 case BLOCK: return TS_BLOCK;
2025 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2026 case TREE_BINFO: return TS_BINFO;
2027 case VALUE_HANDLE: return TS_VALUE_HANDLE;
2029 default:
2030 gcc_unreachable ();
2034 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2035 or offset that depends on a field within a record. */
2037 bool
2038 contains_placeholder_p (tree exp)
2040 enum tree_code code;
2042 if (!exp)
2043 return 0;
2045 code = TREE_CODE (exp);
2046 if (code == PLACEHOLDER_EXPR)
2047 return 1;
2049 switch (TREE_CODE_CLASS (code))
2051 case tcc_reference:
2052 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2053 position computations since they will be converted into a
2054 WITH_RECORD_EXPR involving the reference, which will assume
2055 here will be valid. */
2056 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2058 case tcc_exceptional:
2059 if (code == TREE_LIST)
2060 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2061 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2062 break;
2064 case tcc_unary:
2065 case tcc_binary:
2066 case tcc_comparison:
2067 case tcc_expression:
2068 switch (code)
2070 case COMPOUND_EXPR:
2071 /* Ignoring the first operand isn't quite right, but works best. */
2072 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2074 case COND_EXPR:
2075 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2076 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2077 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2079 case CALL_EXPR:
2080 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2082 default:
2083 break;
2086 switch (TREE_CODE_LENGTH (code))
2088 case 1:
2089 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2090 case 2:
2091 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2092 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2093 default:
2094 return 0;
2097 default:
2098 return 0;
2100 return 0;
2103 /* Return true if any part of the computation of TYPE involves a
2104 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2105 (for QUAL_UNION_TYPE) and field positions. */
2107 static bool
2108 type_contains_placeholder_1 (tree type)
2110 /* If the size contains a placeholder or the parent type (component type in
2111 the case of arrays) type involves a placeholder, this type does. */
2112 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2113 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2114 || (TREE_TYPE (type) != 0
2115 && type_contains_placeholder_p (TREE_TYPE (type))))
2116 return true;
2118 /* Now do type-specific checks. Note that the last part of the check above
2119 greatly limits what we have to do below. */
2120 switch (TREE_CODE (type))
2122 case VOID_TYPE:
2123 case COMPLEX_TYPE:
2124 case ENUMERAL_TYPE:
2125 case BOOLEAN_TYPE:
2126 case CHAR_TYPE:
2127 case POINTER_TYPE:
2128 case OFFSET_TYPE:
2129 case REFERENCE_TYPE:
2130 case METHOD_TYPE:
2131 case FUNCTION_TYPE:
2132 case VECTOR_TYPE:
2133 return false;
2135 case INTEGER_TYPE:
2136 case REAL_TYPE:
2137 /* Here we just check the bounds. */
2138 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2139 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2141 case ARRAY_TYPE:
2142 /* We're already checked the component type (TREE_TYPE), so just check
2143 the index type. */
2144 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2146 case RECORD_TYPE:
2147 case UNION_TYPE:
2148 case QUAL_UNION_TYPE:
2150 tree field;
2152 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2153 if (TREE_CODE (field) == FIELD_DECL
2154 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2155 || (TREE_CODE (type) == QUAL_UNION_TYPE
2156 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2157 || type_contains_placeholder_p (TREE_TYPE (field))))
2158 return true;
2160 return false;
2163 default:
2164 gcc_unreachable ();
2168 bool
2169 type_contains_placeholder_p (tree type)
2171 bool result;
2173 /* If the contains_placeholder_bits field has been initialized,
2174 then we know the answer. */
2175 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2176 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2178 /* Indicate that we've seen this type node, and the answer is false.
2179 This is what we want to return if we run into recursion via fields. */
2180 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2182 /* Compute the real value. */
2183 result = type_contains_placeholder_1 (type);
2185 /* Store the real value. */
2186 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2188 return result;
2191 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2192 return a tree with all occurrences of references to F in a
2193 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2194 contains only arithmetic expressions or a CALL_EXPR with a
2195 PLACEHOLDER_EXPR occurring only in its arglist. */
2197 tree
2198 substitute_in_expr (tree exp, tree f, tree r)
2200 enum tree_code code = TREE_CODE (exp);
2201 tree op0, op1, op2, op3;
2202 tree new;
2203 tree inner;
2205 /* We handle TREE_LIST and COMPONENT_REF separately. */
2206 if (code == TREE_LIST)
2208 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2209 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2210 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2211 return exp;
2213 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2215 else if (code == COMPONENT_REF)
2217 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2218 and it is the right field, replace it with R. */
2219 for (inner = TREE_OPERAND (exp, 0);
2220 REFERENCE_CLASS_P (inner);
2221 inner = TREE_OPERAND (inner, 0))
2223 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2224 && TREE_OPERAND (exp, 1) == f)
2225 return r;
2227 /* If this expression hasn't been completed let, leave it alone. */
2228 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2229 return exp;
2231 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2232 if (op0 == TREE_OPERAND (exp, 0))
2233 return exp;
2235 new = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2236 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2238 else
2239 switch (TREE_CODE_CLASS (code))
2241 case tcc_constant:
2242 case tcc_declaration:
2243 return exp;
2245 case tcc_exceptional:
2246 case tcc_unary:
2247 case tcc_binary:
2248 case tcc_comparison:
2249 case tcc_expression:
2250 case tcc_reference:
2251 switch (TREE_CODE_LENGTH (code))
2253 case 0:
2254 return exp;
2256 case 1:
2257 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2258 if (op0 == TREE_OPERAND (exp, 0))
2259 return exp;
2261 new = fold_build1 (code, TREE_TYPE (exp), op0);
2262 break;
2264 case 2:
2265 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2266 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2268 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2269 return exp;
2271 new = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2272 break;
2274 case 3:
2275 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2276 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2277 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2279 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2280 && op2 == TREE_OPERAND (exp, 2))
2281 return exp;
2283 new = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2284 break;
2286 case 4:
2287 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2288 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2289 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2290 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2292 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2293 && op2 == TREE_OPERAND (exp, 2)
2294 && op3 == TREE_OPERAND (exp, 3))
2295 return exp;
2297 new = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2298 break;
2300 default:
2301 gcc_unreachable ();
2303 break;
2305 default:
2306 gcc_unreachable ();
2309 TREE_READONLY (new) = TREE_READONLY (exp);
2310 return new;
2313 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2314 for it within OBJ, a tree that is an object or a chain of references. */
2316 tree
2317 substitute_placeholder_in_expr (tree exp, tree obj)
2319 enum tree_code code = TREE_CODE (exp);
2320 tree op0, op1, op2, op3;
2322 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2323 in the chain of OBJ. */
2324 if (code == PLACEHOLDER_EXPR)
2326 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2327 tree elt;
2329 for (elt = obj; elt != 0;
2330 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2331 || TREE_CODE (elt) == COND_EXPR)
2332 ? TREE_OPERAND (elt, 1)
2333 : (REFERENCE_CLASS_P (elt)
2334 || UNARY_CLASS_P (elt)
2335 || BINARY_CLASS_P (elt)
2336 || EXPRESSION_CLASS_P (elt))
2337 ? TREE_OPERAND (elt, 0) : 0))
2338 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2339 return elt;
2341 for (elt = obj; elt != 0;
2342 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2343 || TREE_CODE (elt) == COND_EXPR)
2344 ? TREE_OPERAND (elt, 1)
2345 : (REFERENCE_CLASS_P (elt)
2346 || UNARY_CLASS_P (elt)
2347 || BINARY_CLASS_P (elt)
2348 || EXPRESSION_CLASS_P (elt))
2349 ? TREE_OPERAND (elt, 0) : 0))
2350 if (POINTER_TYPE_P (TREE_TYPE (elt))
2351 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2352 == need_type))
2353 return fold_build1 (INDIRECT_REF, need_type, elt);
2355 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2356 survives until RTL generation, there will be an error. */
2357 return exp;
2360 /* TREE_LIST is special because we need to look at TREE_VALUE
2361 and TREE_CHAIN, not TREE_OPERANDS. */
2362 else if (code == TREE_LIST)
2364 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2365 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2366 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2367 return exp;
2369 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2371 else
2372 switch (TREE_CODE_CLASS (code))
2374 case tcc_constant:
2375 case tcc_declaration:
2376 return exp;
2378 case tcc_exceptional:
2379 case tcc_unary:
2380 case tcc_binary:
2381 case tcc_comparison:
2382 case tcc_expression:
2383 case tcc_reference:
2384 case tcc_statement:
2385 switch (TREE_CODE_LENGTH (code))
2387 case 0:
2388 return exp;
2390 case 1:
2391 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2392 if (op0 == TREE_OPERAND (exp, 0))
2393 return exp;
2394 else
2395 return fold_build1 (code, TREE_TYPE (exp), op0);
2397 case 2:
2398 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2399 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2401 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2402 return exp;
2403 else
2404 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2406 case 3:
2407 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2408 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2409 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2411 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2412 && op2 == TREE_OPERAND (exp, 2))
2413 return exp;
2414 else
2415 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2417 case 4:
2418 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2419 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2420 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2421 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2423 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2424 && op2 == TREE_OPERAND (exp, 2)
2425 && op3 == TREE_OPERAND (exp, 3))
2426 return exp;
2427 else
2428 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2430 default:
2431 gcc_unreachable ();
2433 break;
2435 default:
2436 gcc_unreachable ();
2440 /* Stabilize a reference so that we can use it any number of times
2441 without causing its operands to be evaluated more than once.
2442 Returns the stabilized reference. This works by means of save_expr,
2443 so see the caveats in the comments about save_expr.
2445 Also allows conversion expressions whose operands are references.
2446 Any other kind of expression is returned unchanged. */
2448 tree
2449 stabilize_reference (tree ref)
2451 tree result;
2452 enum tree_code code = TREE_CODE (ref);
2454 switch (code)
2456 case VAR_DECL:
2457 case PARM_DECL:
2458 case RESULT_DECL:
2459 /* No action is needed in this case. */
2460 return ref;
2462 case NOP_EXPR:
2463 case CONVERT_EXPR:
2464 case FLOAT_EXPR:
2465 case FIX_TRUNC_EXPR:
2466 case FIX_FLOOR_EXPR:
2467 case FIX_ROUND_EXPR:
2468 case FIX_CEIL_EXPR:
2469 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2470 break;
2472 case INDIRECT_REF:
2473 result = build_nt (INDIRECT_REF,
2474 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2475 break;
2477 case COMPONENT_REF:
2478 result = build_nt (COMPONENT_REF,
2479 stabilize_reference (TREE_OPERAND (ref, 0)),
2480 TREE_OPERAND (ref, 1), NULL_TREE);
2481 break;
2483 case BIT_FIELD_REF:
2484 result = build_nt (BIT_FIELD_REF,
2485 stabilize_reference (TREE_OPERAND (ref, 0)),
2486 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2487 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2488 break;
2490 case ARRAY_REF:
2491 result = build_nt (ARRAY_REF,
2492 stabilize_reference (TREE_OPERAND (ref, 0)),
2493 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2494 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2495 break;
2497 case ARRAY_RANGE_REF:
2498 result = build_nt (ARRAY_RANGE_REF,
2499 stabilize_reference (TREE_OPERAND (ref, 0)),
2500 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2501 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2502 break;
2504 case COMPOUND_EXPR:
2505 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2506 it wouldn't be ignored. This matters when dealing with
2507 volatiles. */
2508 return stabilize_reference_1 (ref);
2510 /* If arg isn't a kind of lvalue we recognize, make no change.
2511 Caller should recognize the error for an invalid lvalue. */
2512 default:
2513 return ref;
2515 case ERROR_MARK:
2516 return error_mark_node;
2519 TREE_TYPE (result) = TREE_TYPE (ref);
2520 TREE_READONLY (result) = TREE_READONLY (ref);
2521 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2522 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2524 return result;
2527 /* Subroutine of stabilize_reference; this is called for subtrees of
2528 references. Any expression with side-effects must be put in a SAVE_EXPR
2529 to ensure that it is only evaluated once.
2531 We don't put SAVE_EXPR nodes around everything, because assigning very
2532 simple expressions to temporaries causes us to miss good opportunities
2533 for optimizations. Among other things, the opportunity to fold in the
2534 addition of a constant into an addressing mode often gets lost, e.g.
2535 "y[i+1] += x;". In general, we take the approach that we should not make
2536 an assignment unless we are forced into it - i.e., that any non-side effect
2537 operator should be allowed, and that cse should take care of coalescing
2538 multiple utterances of the same expression should that prove fruitful. */
2540 tree
2541 stabilize_reference_1 (tree e)
2543 tree result;
2544 enum tree_code code = TREE_CODE (e);
2546 /* We cannot ignore const expressions because it might be a reference
2547 to a const array but whose index contains side-effects. But we can
2548 ignore things that are actual constant or that already have been
2549 handled by this function. */
2551 if (TREE_INVARIANT (e))
2552 return e;
2554 switch (TREE_CODE_CLASS (code))
2556 case tcc_exceptional:
2557 case tcc_type:
2558 case tcc_declaration:
2559 case tcc_comparison:
2560 case tcc_statement:
2561 case tcc_expression:
2562 case tcc_reference:
2563 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2564 so that it will only be evaluated once. */
2565 /* The reference (r) and comparison (<) classes could be handled as
2566 below, but it is generally faster to only evaluate them once. */
2567 if (TREE_SIDE_EFFECTS (e))
2568 return save_expr (e);
2569 return e;
2571 case tcc_constant:
2572 /* Constants need no processing. In fact, we should never reach
2573 here. */
2574 return e;
2576 case tcc_binary:
2577 /* Division is slow and tends to be compiled with jumps,
2578 especially the division by powers of 2 that is often
2579 found inside of an array reference. So do it just once. */
2580 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2581 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2582 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2583 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2584 return save_expr (e);
2585 /* Recursively stabilize each operand. */
2586 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2587 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2588 break;
2590 case tcc_unary:
2591 /* Recursively stabilize each operand. */
2592 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2593 break;
2595 default:
2596 gcc_unreachable ();
2599 TREE_TYPE (result) = TREE_TYPE (e);
2600 TREE_READONLY (result) = TREE_READONLY (e);
2601 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2602 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2603 TREE_INVARIANT (result) = 1;
2605 return result;
2608 /* Low-level constructors for expressions. */
2610 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
2611 TREE_INVARIANT, and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
2613 void
2614 recompute_tree_invarant_for_addr_expr (tree t)
2616 tree node;
2617 bool tc = true, ti = true, se = false;
2619 /* We started out assuming this address is both invariant and constant, but
2620 does not have side effects. Now go down any handled components and see if
2621 any of them involve offsets that are either non-constant or non-invariant.
2622 Also check for side-effects.
2624 ??? Note that this code makes no attempt to deal with the case where
2625 taking the address of something causes a copy due to misalignment. */
2627 #define UPDATE_TITCSE(NODE) \
2628 do { tree _node = (NODE); \
2629 if (_node && !TREE_INVARIANT (_node)) ti = false; \
2630 if (_node && !TREE_CONSTANT (_node)) tc = false; \
2631 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
2633 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
2634 node = TREE_OPERAND (node, 0))
2636 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
2637 array reference (probably made temporarily by the G++ front end),
2638 so ignore all the operands. */
2639 if ((TREE_CODE (node) == ARRAY_REF
2640 || TREE_CODE (node) == ARRAY_RANGE_REF)
2641 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
2643 UPDATE_TITCSE (TREE_OPERAND (node, 1));
2644 if (TREE_OPERAND (node, 2))
2645 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2646 if (TREE_OPERAND (node, 3))
2647 UPDATE_TITCSE (TREE_OPERAND (node, 3));
2649 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
2650 FIELD_DECL, apparently. The G++ front end can put something else
2651 there, at least temporarily. */
2652 else if (TREE_CODE (node) == COMPONENT_REF
2653 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
2655 if (TREE_OPERAND (node, 2))
2656 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2658 else if (TREE_CODE (node) == BIT_FIELD_REF)
2659 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2662 node = lang_hooks.expr_to_decl (node, &tc, &ti, &se);
2664 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
2665 the address, since &(*a)->b is a form of addition. If it's a decl, it's
2666 invariant and constant if the decl is static. It's also invariant if it's
2667 a decl in the current function. Taking the address of a volatile variable
2668 is not volatile. If it's a constant, the address is both invariant and
2669 constant. Otherwise it's neither. */
2670 if (TREE_CODE (node) == INDIRECT_REF)
2671 UPDATE_TITCSE (TREE_OPERAND (node, 0));
2672 else if (DECL_P (node))
2674 if (staticp (node))
2676 else if (decl_function_context (node) == current_function_decl
2677 /* Addresses of thread-local variables are invariant. */
2678 || (TREE_CODE (node) == VAR_DECL
2679 && DECL_THREAD_LOCAL_P (node)))
2680 tc = false;
2681 else
2682 ti = tc = false;
2684 else if (CONSTANT_CLASS_P (node))
2686 else
2688 ti = tc = false;
2689 se |= TREE_SIDE_EFFECTS (node);
2692 TREE_CONSTANT (t) = tc;
2693 TREE_INVARIANT (t) = ti;
2694 TREE_SIDE_EFFECTS (t) = se;
2695 #undef UPDATE_TITCSE
2698 /* Build an expression of code CODE, data type TYPE, and operands as
2699 specified. Expressions and reference nodes can be created this way.
2700 Constants, decls, types and misc nodes cannot be.
2702 We define 5 non-variadic functions, from 0 to 4 arguments. This is
2703 enough for all extant tree codes. These functions can be called
2704 directly (preferably!), but can also be obtained via GCC preprocessor
2705 magic within the build macro. */
2707 tree
2708 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
2710 tree t;
2712 gcc_assert (TREE_CODE_LENGTH (code) == 0);
2714 t = make_node_stat (code PASS_MEM_STAT);
2715 TREE_TYPE (t) = tt;
2717 return t;
2720 tree
2721 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
2723 int length = sizeof (struct tree_exp);
2724 #ifdef GATHER_STATISTICS
2725 tree_node_kind kind;
2726 #endif
2727 tree t;
2729 #ifdef GATHER_STATISTICS
2730 switch (TREE_CODE_CLASS (code))
2732 case tcc_statement: /* an expression with side effects */
2733 kind = s_kind;
2734 break;
2735 case tcc_reference: /* a reference */
2736 kind = r_kind;
2737 break;
2738 default:
2739 kind = e_kind;
2740 break;
2743 tree_node_counts[(int) kind]++;
2744 tree_node_sizes[(int) kind] += length;
2745 #endif
2747 gcc_assert (TREE_CODE_LENGTH (code) == 1);
2749 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
2751 memset (t, 0, sizeof (struct tree_common));
2753 TREE_SET_CODE (t, code);
2755 TREE_TYPE (t) = type;
2756 #ifdef USE_MAPPED_LOCATION
2757 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
2758 #else
2759 SET_EXPR_LOCUS (t, NULL);
2760 #endif
2761 TREE_COMPLEXITY (t) = 0;
2762 TREE_OPERAND (t, 0) = node;
2763 TREE_BLOCK (t) = NULL_TREE;
2764 if (node && !TYPE_P (node))
2766 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2767 TREE_READONLY (t) = TREE_READONLY (node);
2770 if (TREE_CODE_CLASS (code) == tcc_statement)
2771 TREE_SIDE_EFFECTS (t) = 1;
2772 else switch (code)
2774 case VA_ARG_EXPR:
2775 /* All of these have side-effects, no matter what their
2776 operands are. */
2777 TREE_SIDE_EFFECTS (t) = 1;
2778 TREE_READONLY (t) = 0;
2779 break;
2781 case MISALIGNED_INDIRECT_REF:
2782 case ALIGN_INDIRECT_REF:
2783 case INDIRECT_REF:
2784 /* Whether a dereference is readonly has nothing to do with whether
2785 its operand is readonly. */
2786 TREE_READONLY (t) = 0;
2787 break;
2789 case ADDR_EXPR:
2790 if (node)
2791 recompute_tree_invarant_for_addr_expr (t);
2792 break;
2794 default:
2795 if (TREE_CODE_CLASS (code) == tcc_unary
2796 && node && !TYPE_P (node)
2797 && TREE_CONSTANT (node))
2798 TREE_CONSTANT (t) = 1;
2799 if (TREE_CODE_CLASS (code) == tcc_unary
2800 && node && TREE_INVARIANT (node))
2801 TREE_INVARIANT (t) = 1;
2802 if (TREE_CODE_CLASS (code) == tcc_reference
2803 && node && TREE_THIS_VOLATILE (node))
2804 TREE_THIS_VOLATILE (t) = 1;
2805 break;
2808 return t;
2811 #define PROCESS_ARG(N) \
2812 do { \
2813 TREE_OPERAND (t, N) = arg##N; \
2814 if (arg##N &&!TYPE_P (arg##N)) \
2816 if (TREE_SIDE_EFFECTS (arg##N)) \
2817 side_effects = 1; \
2818 if (!TREE_READONLY (arg##N)) \
2819 read_only = 0; \
2820 if (!TREE_CONSTANT (arg##N)) \
2821 constant = 0; \
2822 if (!TREE_INVARIANT (arg##N)) \
2823 invariant = 0; \
2825 } while (0)
2827 tree
2828 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
2830 bool constant, read_only, side_effects, invariant;
2831 tree t;
2833 gcc_assert (TREE_CODE_LENGTH (code) == 2);
2835 t = make_node_stat (code PASS_MEM_STAT);
2836 TREE_TYPE (t) = tt;
2838 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2839 result based on those same flags for the arguments. But if the
2840 arguments aren't really even `tree' expressions, we shouldn't be trying
2841 to do this. */
2843 /* Expressions without side effects may be constant if their
2844 arguments are as well. */
2845 constant = (TREE_CODE_CLASS (code) == tcc_comparison
2846 || TREE_CODE_CLASS (code) == tcc_binary);
2847 read_only = 1;
2848 side_effects = TREE_SIDE_EFFECTS (t);
2849 invariant = constant;
2851 PROCESS_ARG(0);
2852 PROCESS_ARG(1);
2854 TREE_READONLY (t) = read_only;
2855 TREE_CONSTANT (t) = constant;
2856 TREE_INVARIANT (t) = invariant;
2857 TREE_SIDE_EFFECTS (t) = side_effects;
2858 TREE_THIS_VOLATILE (t)
2859 = (TREE_CODE_CLASS (code) == tcc_reference
2860 && arg0 && TREE_THIS_VOLATILE (arg0));
2862 return t;
2865 tree
2866 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
2867 tree arg2 MEM_STAT_DECL)
2869 bool constant, read_only, side_effects, invariant;
2870 tree t;
2872 gcc_assert (TREE_CODE_LENGTH (code) == 3);
2874 t = make_node_stat (code PASS_MEM_STAT);
2875 TREE_TYPE (t) = tt;
2877 side_effects = TREE_SIDE_EFFECTS (t);
2879 PROCESS_ARG(0);
2880 PROCESS_ARG(1);
2881 PROCESS_ARG(2);
2883 if (code == CALL_EXPR && !side_effects)
2885 tree node;
2886 int i;
2888 /* Calls have side-effects, except those to const or
2889 pure functions. */
2890 i = call_expr_flags (t);
2891 if (!(i & (ECF_CONST | ECF_PURE)))
2892 side_effects = 1;
2894 /* And even those have side-effects if their arguments do. */
2895 else for (node = arg1; node; node = TREE_CHAIN (node))
2896 if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
2898 side_effects = 1;
2899 break;
2903 TREE_SIDE_EFFECTS (t) = side_effects;
2904 TREE_THIS_VOLATILE (t)
2905 = (TREE_CODE_CLASS (code) == tcc_reference
2906 && arg0 && TREE_THIS_VOLATILE (arg0));
2908 return t;
2911 tree
2912 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
2913 tree arg2, tree arg3 MEM_STAT_DECL)
2915 bool constant, read_only, side_effects, invariant;
2916 tree t;
2918 gcc_assert (TREE_CODE_LENGTH (code) == 4);
2920 t = make_node_stat (code PASS_MEM_STAT);
2921 TREE_TYPE (t) = tt;
2923 side_effects = TREE_SIDE_EFFECTS (t);
2925 PROCESS_ARG(0);
2926 PROCESS_ARG(1);
2927 PROCESS_ARG(2);
2928 PROCESS_ARG(3);
2930 TREE_SIDE_EFFECTS (t) = side_effects;
2931 TREE_THIS_VOLATILE (t)
2932 = (TREE_CODE_CLASS (code) == tcc_reference
2933 && arg0 && TREE_THIS_VOLATILE (arg0));
2935 return t;
2938 tree
2939 build7_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
2940 tree arg2, tree arg3, tree arg4, tree arg5,
2941 tree arg6 MEM_STAT_DECL)
2943 bool constant, read_only, side_effects, invariant;
2944 tree t;
2946 gcc_assert (code == TARGET_MEM_REF);
2948 t = make_node_stat (code PASS_MEM_STAT);
2949 TREE_TYPE (t) = tt;
2951 side_effects = TREE_SIDE_EFFECTS (t);
2953 PROCESS_ARG(0);
2954 PROCESS_ARG(1);
2955 PROCESS_ARG(2);
2956 PROCESS_ARG(3);
2957 PROCESS_ARG(4);
2958 PROCESS_ARG(5);
2959 PROCESS_ARG(6);
2961 TREE_SIDE_EFFECTS (t) = side_effects;
2962 TREE_THIS_VOLATILE (t) = 0;
2964 return t;
2967 /* Backup definition for non-gcc build compilers. */
2969 tree
2970 (build) (enum tree_code code, tree tt, ...)
2972 tree t, arg0, arg1, arg2, arg3, arg4, arg5, arg6;
2973 int length = TREE_CODE_LENGTH (code);
2974 va_list p;
2976 va_start (p, tt);
2977 switch (length)
2979 case 0:
2980 t = build0 (code, tt);
2981 break;
2982 case 1:
2983 arg0 = va_arg (p, tree);
2984 t = build1 (code, tt, arg0);
2985 break;
2986 case 2:
2987 arg0 = va_arg (p, tree);
2988 arg1 = va_arg (p, tree);
2989 t = build2 (code, tt, arg0, arg1);
2990 break;
2991 case 3:
2992 arg0 = va_arg (p, tree);
2993 arg1 = va_arg (p, tree);
2994 arg2 = va_arg (p, tree);
2995 t = build3 (code, tt, arg0, arg1, arg2);
2996 break;
2997 case 4:
2998 arg0 = va_arg (p, tree);
2999 arg1 = va_arg (p, tree);
3000 arg2 = va_arg (p, tree);
3001 arg3 = va_arg (p, tree);
3002 t = build4 (code, tt, arg0, arg1, arg2, arg3);
3003 break;
3004 case 7:
3005 arg0 = va_arg (p, tree);
3006 arg1 = va_arg (p, tree);
3007 arg2 = va_arg (p, tree);
3008 arg3 = va_arg (p, tree);
3009 arg4 = va_arg (p, tree);
3010 arg5 = va_arg (p, tree);
3011 arg6 = va_arg (p, tree);
3012 t = build7 (code, tt, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
3013 break;
3014 default:
3015 gcc_unreachable ();
3017 va_end (p);
3019 return t;
3022 /* Similar except don't specify the TREE_TYPE
3023 and leave the TREE_SIDE_EFFECTS as 0.
3024 It is permissible for arguments to be null,
3025 or even garbage if their values do not matter. */
3027 tree
3028 build_nt (enum tree_code code, ...)
3030 tree t;
3031 int length;
3032 int i;
3033 va_list p;
3035 va_start (p, code);
3037 t = make_node (code);
3038 length = TREE_CODE_LENGTH (code);
3040 for (i = 0; i < length; i++)
3041 TREE_OPERAND (t, i) = va_arg (p, tree);
3043 va_end (p);
3044 return t;
3047 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3048 We do NOT enter this node in any sort of symbol table.
3050 layout_decl is used to set up the decl's storage layout.
3051 Other slots are initialized to 0 or null pointers. */
3053 tree
3054 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3056 tree t;
3058 t = make_node_stat (code PASS_MEM_STAT);
3060 /* if (type == error_mark_node)
3061 type = integer_type_node; */
3062 /* That is not done, deliberately, so that having error_mark_node
3063 as the type can suppress useless errors in the use of this variable. */
3065 DECL_NAME (t) = name;
3066 TREE_TYPE (t) = type;
3068 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3069 layout_decl (t, 0);
3070 else if (code == FUNCTION_DECL)
3071 DECL_MODE (t) = FUNCTION_MODE;
3073 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
3075 /* Set default visibility to whatever the user supplied with
3076 visibility_specified depending on #pragma GCC visibility. */
3077 DECL_VISIBILITY (t) = default_visibility;
3078 DECL_VISIBILITY_SPECIFIED (t) = visibility_options.inpragma;
3081 return t;
3084 /* Builds and returns function declaration with NAME and TYPE. */
3086 tree
3087 build_fn_decl (const char *name, tree type)
3089 tree id = get_identifier (name);
3090 tree decl = build_decl (FUNCTION_DECL, id, type);
3092 DECL_EXTERNAL (decl) = 1;
3093 TREE_PUBLIC (decl) = 1;
3094 DECL_ARTIFICIAL (decl) = 1;
3095 TREE_NOTHROW (decl) = 1;
3097 return decl;
3101 /* BLOCK nodes are used to represent the structure of binding contours
3102 and declarations, once those contours have been exited and their contents
3103 compiled. This information is used for outputting debugging info. */
3105 tree
3106 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3108 tree block = make_node (BLOCK);
3110 BLOCK_VARS (block) = vars;
3111 BLOCK_SUBBLOCKS (block) = subblocks;
3112 BLOCK_SUPERCONTEXT (block) = supercontext;
3113 BLOCK_CHAIN (block) = chain;
3114 return block;
3117 #if 1 /* ! defined(USE_MAPPED_LOCATION) */
3118 /* ??? gengtype doesn't handle conditionals */
3119 static GTY(()) tree last_annotated_node;
3120 #endif
3122 #ifdef USE_MAPPED_LOCATION
3124 expanded_location
3125 expand_location (source_location loc)
3127 expanded_location xloc;
3128 if (loc == 0) { xloc.file = NULL; xloc.line = 0; xloc.column = 0; }
3129 else
3131 const struct line_map *map = linemap_lookup (&line_table, loc);
3132 xloc.file = map->to_file;
3133 xloc.line = SOURCE_LINE (map, loc);
3134 xloc.column = SOURCE_COLUMN (map, loc);
3136 return xloc;
3139 #else
3141 /* Record the exact location where an expression or an identifier were
3142 encountered. */
3144 void
3145 annotate_with_file_line (tree node, const char *file, int line)
3147 /* Roughly one percent of the calls to this function are to annotate
3148 a node with the same information already attached to that node!
3149 Just return instead of wasting memory. */
3150 if (EXPR_LOCUS (node)
3151 && EXPR_LINENO (node) == line
3152 && (EXPR_FILENAME (node) == file
3153 || !strcmp (EXPR_FILENAME (node), file)))
3155 last_annotated_node = node;
3156 return;
3159 /* In heavily macroized code (such as GCC itself) this single
3160 entry cache can reduce the number of allocations by more
3161 than half. */
3162 if (last_annotated_node
3163 && EXPR_LOCUS (last_annotated_node)
3164 && EXPR_LINENO (last_annotated_node) == line
3165 && (EXPR_FILENAME (last_annotated_node) == file
3166 || !strcmp (EXPR_FILENAME (last_annotated_node), file)))
3168 SET_EXPR_LOCUS (node, EXPR_LOCUS (last_annotated_node));
3169 return;
3172 SET_EXPR_LOCUS (node, ggc_alloc (sizeof (location_t)));
3173 EXPR_LINENO (node) = line;
3174 EXPR_FILENAME (node) = file;
3175 last_annotated_node = node;
3178 void
3179 annotate_with_locus (tree node, location_t locus)
3181 annotate_with_file_line (node, locus.file, locus.line);
3183 #endif
3185 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3186 is ATTRIBUTE. */
3188 tree
3189 build_decl_attribute_variant (tree ddecl, tree attribute)
3191 DECL_ATTRIBUTES (ddecl) = attribute;
3192 return ddecl;
3195 /* Borrowed from hashtab.c iterative_hash implementation. */
3196 #define mix(a,b,c) \
3198 a -= b; a -= c; a ^= (c>>13); \
3199 b -= c; b -= a; b ^= (a<< 8); \
3200 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3201 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3202 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3203 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3204 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3205 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3206 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3210 /* Produce good hash value combining VAL and VAL2. */
3211 static inline hashval_t
3212 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3214 /* the golden ratio; an arbitrary value. */
3215 hashval_t a = 0x9e3779b9;
3217 mix (a, val, val2);
3218 return val2;
3221 /* Produce good hash value combining PTR and VAL2. */
3222 static inline hashval_t
3223 iterative_hash_pointer (void *ptr, hashval_t val2)
3225 if (sizeof (ptr) == sizeof (hashval_t))
3226 return iterative_hash_hashval_t ((size_t) ptr, val2);
3227 else
3229 hashval_t a = (hashval_t) (size_t) ptr;
3230 /* Avoid warnings about shifting of more than the width of the type on
3231 hosts that won't execute this path. */
3232 int zero = 0;
3233 hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3234 mix (a, b, val2);
3235 return val2;
3239 /* Produce good hash value combining VAL and VAL2. */
3240 static inline hashval_t
3241 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3243 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3244 return iterative_hash_hashval_t (val, val2);
3245 else
3247 hashval_t a = (hashval_t) val;
3248 /* Avoid warnings about shifting of more than the width of the type on
3249 hosts that won't execute this path. */
3250 int zero = 0;
3251 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3252 mix (a, b, val2);
3253 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3255 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3256 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3257 mix (a, b, val2);
3259 return val2;
3263 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3264 is ATTRIBUTE.
3266 Record such modified types already made so we don't make duplicates. */
3268 tree
3269 build_type_attribute_variant (tree ttype, tree attribute)
3271 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3273 hashval_t hashcode = 0;
3274 tree ntype;
3275 enum tree_code code = TREE_CODE (ttype);
3277 ntype = copy_node (ttype);
3279 TYPE_POINTER_TO (ntype) = 0;
3280 TYPE_REFERENCE_TO (ntype) = 0;
3281 TYPE_ATTRIBUTES (ntype) = attribute;
3283 /* Create a new main variant of TYPE. */
3284 TYPE_MAIN_VARIANT (ntype) = ntype;
3285 TYPE_NEXT_VARIANT (ntype) = 0;
3286 set_type_quals (ntype, TYPE_UNQUALIFIED);
3288 hashcode = iterative_hash_object (code, hashcode);
3289 if (TREE_TYPE (ntype))
3290 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3291 hashcode);
3292 hashcode = attribute_hash_list (attribute, hashcode);
3294 switch (TREE_CODE (ntype))
3296 case FUNCTION_TYPE:
3297 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3298 break;
3299 case ARRAY_TYPE:
3300 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3301 hashcode);
3302 break;
3303 case INTEGER_TYPE:
3304 hashcode = iterative_hash_object
3305 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3306 hashcode = iterative_hash_object
3307 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3308 break;
3309 case REAL_TYPE:
3311 unsigned int precision = TYPE_PRECISION (ntype);
3312 hashcode = iterative_hash_object (precision, hashcode);
3314 break;
3315 default:
3316 break;
3319 ntype = type_hash_canon (hashcode, ntype);
3320 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
3323 return ttype;
3327 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3328 or zero if not.
3330 We try both `text' and `__text__', ATTR may be either one. */
3331 /* ??? It might be a reasonable simplification to require ATTR to be only
3332 `text'. One might then also require attribute lists to be stored in
3333 their canonicalized form. */
3335 static int
3336 is_attribute_with_length_p (const char *attr, int attr_len, tree ident)
3338 int ident_len;
3339 const char *p;
3341 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3342 return 0;
3344 p = IDENTIFIER_POINTER (ident);
3345 ident_len = IDENTIFIER_LENGTH (ident);
3347 if (ident_len == attr_len
3348 && strcmp (attr, p) == 0)
3349 return 1;
3351 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3352 if (attr[0] == '_')
3354 gcc_assert (attr[1] == '_');
3355 gcc_assert (attr[attr_len - 2] == '_');
3356 gcc_assert (attr[attr_len - 1] == '_');
3357 gcc_assert (attr[1] == '_');
3358 if (ident_len == attr_len - 4
3359 && strncmp (attr + 2, p, attr_len - 4) == 0)
3360 return 1;
3362 else
3364 if (ident_len == attr_len + 4
3365 && p[0] == '_' && p[1] == '_'
3366 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3367 && strncmp (attr, p + 2, attr_len) == 0)
3368 return 1;
3371 return 0;
3374 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3375 or zero if not.
3377 We try both `text' and `__text__', ATTR may be either one. */
3380 is_attribute_p (const char *attr, tree ident)
3382 return is_attribute_with_length_p (attr, strlen (attr), ident);
3385 /* Given an attribute name and a list of attributes, return a pointer to the
3386 attribute's list element if the attribute is part of the list, or NULL_TREE
3387 if not found. If the attribute appears more than once, this only
3388 returns the first occurrence; the TREE_CHAIN of the return value should
3389 be passed back in if further occurrences are wanted. */
3391 tree
3392 lookup_attribute (const char *attr_name, tree list)
3394 tree l;
3395 size_t attr_len = strlen (attr_name);
3397 for (l = list; l; l = TREE_CHAIN (l))
3399 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3400 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3401 return l;
3404 return NULL_TREE;
3407 /* Return an attribute list that is the union of a1 and a2. */
3409 tree
3410 merge_attributes (tree a1, tree a2)
3412 tree attributes;
3414 /* Either one unset? Take the set one. */
3416 if ((attributes = a1) == 0)
3417 attributes = a2;
3419 /* One that completely contains the other? Take it. */
3421 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3423 if (attribute_list_contained (a2, a1))
3424 attributes = a2;
3425 else
3427 /* Pick the longest list, and hang on the other list. */
3429 if (list_length (a1) < list_length (a2))
3430 attributes = a2, a2 = a1;
3432 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3434 tree a;
3435 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3436 attributes);
3437 a != NULL_TREE;
3438 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3439 TREE_CHAIN (a)))
3441 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
3442 break;
3444 if (a == NULL_TREE)
3446 a1 = copy_node (a2);
3447 TREE_CHAIN (a1) = attributes;
3448 attributes = a1;
3453 return attributes;
3456 /* Given types T1 and T2, merge their attributes and return
3457 the result. */
3459 tree
3460 merge_type_attributes (tree t1, tree t2)
3462 return merge_attributes (TYPE_ATTRIBUTES (t1),
3463 TYPE_ATTRIBUTES (t2));
3466 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3467 the result. */
3469 tree
3470 merge_decl_attributes (tree olddecl, tree newdecl)
3472 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3473 DECL_ATTRIBUTES (newdecl));
3476 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3478 /* Specialization of merge_decl_attributes for various Windows targets.
3480 This handles the following situation:
3482 __declspec (dllimport) int foo;
3483 int foo;
3485 The second instance of `foo' nullifies the dllimport. */
3487 tree
3488 merge_dllimport_decl_attributes (tree old, tree new)
3490 tree a;
3491 int delete_dllimport_p;
3493 old = DECL_ATTRIBUTES (old);
3494 new = DECL_ATTRIBUTES (new);
3496 /* What we need to do here is remove from `old' dllimport if it doesn't
3497 appear in `new'. dllimport behaves like extern: if a declaration is
3498 marked dllimport and a definition appears later, then the object
3499 is not dllimport'd. */
3500 if (lookup_attribute ("dllimport", old) != NULL_TREE
3501 && lookup_attribute ("dllimport", new) == NULL_TREE)
3502 delete_dllimport_p = 1;
3503 else
3504 delete_dllimport_p = 0;
3506 a = merge_attributes (old, new);
3508 if (delete_dllimport_p)
3510 tree prev, t;
3512 /* Scan the list for dllimport and delete it. */
3513 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
3515 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
3517 if (prev == NULL_TREE)
3518 a = TREE_CHAIN (a);
3519 else
3520 TREE_CHAIN (prev) = TREE_CHAIN (t);
3521 break;
3526 return a;
3529 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
3530 struct attribute_spec.handler. */
3532 tree
3533 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
3534 bool *no_add_attrs)
3536 tree node = *pnode;
3538 /* These attributes may apply to structure and union types being created,
3539 but otherwise should pass to the declaration involved. */
3540 if (!DECL_P (node))
3542 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
3543 | (int) ATTR_FLAG_ARRAY_NEXT))
3545 *no_add_attrs = true;
3546 return tree_cons (name, args, NULL_TREE);
3548 if (TREE_CODE (node) != RECORD_TYPE && TREE_CODE (node) != UNION_TYPE)
3550 warning (OPT_Wattributes, "%qs attribute ignored",
3551 IDENTIFIER_POINTER (name));
3552 *no_add_attrs = true;
3555 return NULL_TREE;
3558 /* Report error on dllimport ambiguities seen now before they cause
3559 any damage. */
3560 if (is_attribute_p ("dllimport", name))
3562 /* Like MS, treat definition of dllimported variables and
3563 non-inlined functions on declaration as syntax errors. We
3564 allow the attribute for function definitions if declared
3565 inline. */
3566 if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node)
3567 && !DECL_DECLARED_INLINE_P (node))
3569 error ("function %q+D definition is marked dllimport", node);
3570 *no_add_attrs = true;
3573 else if (TREE_CODE (node) == VAR_DECL)
3575 if (DECL_INITIAL (node))
3577 error ("variable %q+D definition is marked dllimport",
3578 node);
3579 *no_add_attrs = true;
3582 /* `extern' needn't be specified with dllimport.
3583 Specify `extern' now and hope for the best. Sigh. */
3584 DECL_EXTERNAL (node) = 1;
3585 /* Also, implicitly give dllimport'd variables declared within
3586 a function global scope, unless declared static. */
3587 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
3588 TREE_PUBLIC (node) = 1;
3592 /* Report error if symbol is not accessible at global scope. */
3593 if (!TREE_PUBLIC (node)
3594 && (TREE_CODE (node) == VAR_DECL
3595 || TREE_CODE (node) == FUNCTION_DECL))
3597 error ("external linkage required for symbol %q+D because of "
3598 "%qs attribute", node, IDENTIFIER_POINTER (name));
3599 *no_add_attrs = true;
3602 return NULL_TREE;
3605 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
3607 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
3608 of the various TYPE_QUAL values. */
3610 static void
3611 set_type_quals (tree type, int type_quals)
3613 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
3614 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
3615 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
3618 /* Returns true iff cand is equivalent to base with type_quals. */
3620 bool
3621 check_qualified_type (tree cand, tree base, int type_quals)
3623 return (TYPE_QUALS (cand) == type_quals
3624 && TYPE_NAME (cand) == TYPE_NAME (base)
3625 /* Apparently this is needed for Objective-C. */
3626 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
3627 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
3628 TYPE_ATTRIBUTES (base)));
3631 /* Return a version of the TYPE, qualified as indicated by the
3632 TYPE_QUALS, if one exists. If no qualified version exists yet,
3633 return NULL_TREE. */
3635 tree
3636 get_qualified_type (tree type, int type_quals)
3638 tree t;
3640 if (TYPE_QUALS (type) == type_quals)
3641 return type;
3643 /* Search the chain of variants to see if there is already one there just
3644 like the one we need to have. If so, use that existing one. We must
3645 preserve the TYPE_NAME, since there is code that depends on this. */
3646 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3647 if (check_qualified_type (t, type, type_quals))
3648 return t;
3650 return NULL_TREE;
3653 /* Like get_qualified_type, but creates the type if it does not
3654 exist. This function never returns NULL_TREE. */
3656 tree
3657 build_qualified_type (tree type, int type_quals)
3659 tree t;
3661 /* See if we already have the appropriate qualified variant. */
3662 t = get_qualified_type (type, type_quals);
3664 /* If not, build it. */
3665 if (!t)
3667 t = build_variant_type_copy (type);
3668 set_type_quals (t, type_quals);
3671 return t;
3674 /* Create a new distinct copy of TYPE. The new type is made its own
3675 MAIN_VARIANT. */
3677 tree
3678 build_distinct_type_copy (tree type)
3680 tree t = copy_node (type);
3682 TYPE_POINTER_TO (t) = 0;
3683 TYPE_REFERENCE_TO (t) = 0;
3685 /* Make it its own variant. */
3686 TYPE_MAIN_VARIANT (t) = t;
3687 TYPE_NEXT_VARIANT (t) = 0;
3689 return t;
3692 /* Create a new variant of TYPE, equivalent but distinct.
3693 This is so the caller can modify it. */
3695 tree
3696 build_variant_type_copy (tree type)
3698 tree t, m = TYPE_MAIN_VARIANT (type);
3700 t = build_distinct_type_copy (type);
3702 /* Add the new type to the chain of variants of TYPE. */
3703 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3704 TYPE_NEXT_VARIANT (m) = t;
3705 TYPE_MAIN_VARIANT (t) = m;
3707 return t;
3710 /* Return true if the from tree in both tree maps are equal. */
3713 tree_map_eq (const void *va, const void *vb)
3715 const struct tree_map *a = va, *b = vb;
3716 return (a->from == b->from);
3719 /* Hash a from tree in a tree_map. */
3721 unsigned int
3722 tree_map_hash (const void *item)
3724 return (((const struct tree_map *) item)->hash);
3727 /* Return true if this tree map structure is marked for garbage collection
3728 purposes. We simply return true if the from tree is marked, so that this
3729 structure goes away when the from tree goes away. */
3732 tree_map_marked_p (const void *p)
3734 tree from = ((struct tree_map *) p)->from;
3736 return ggc_marked_p (from);
3739 /* Return true if the trees in the tree_int_map *'s VA and VB are equal. */
3741 static int
3742 tree_int_map_eq (const void *va, const void *vb)
3744 const struct tree_int_map *a = va, *b = vb;
3745 return (a->from == b->from);
3748 /* Hash a from tree in the tree_int_map * ITEM. */
3750 static unsigned int
3751 tree_int_map_hash (const void *item)
3753 return htab_hash_pointer (((const struct tree_int_map *)item)->from);
3756 /* Return true if this tree int map structure is marked for garbage collection
3757 purposes. We simply return true if the from tree_int_map *P's from tree is marked, so that this
3758 structure goes away when the from tree goes away. */
3760 static int
3761 tree_int_map_marked_p (const void *p)
3763 tree from = ((struct tree_int_map *) p)->from;
3765 return ggc_marked_p (from);
3767 /* Lookup an init priority for FROM, and return it if we find one. */
3769 unsigned short
3770 decl_init_priority_lookup (tree from)
3772 struct tree_int_map *h, in;
3773 in.from = from;
3775 h = htab_find_with_hash (init_priority_for_decl,
3776 &in, htab_hash_pointer (from));
3777 if (h)
3778 return h->to;
3779 return 0;
3782 /* Insert a mapping FROM->TO in the init priority hashtable. */
3784 void
3785 decl_init_priority_insert (tree from, unsigned short to)
3787 struct tree_int_map *h;
3788 void **loc;
3790 h = ggc_alloc (sizeof (struct tree_int_map));
3791 h->from = from;
3792 h->to = to;
3793 loc = htab_find_slot_with_hash (init_priority_for_decl, h,
3794 htab_hash_pointer (from), INSERT);
3795 *(struct tree_int_map **) loc = h;
3798 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
3800 static void
3801 print_debug_expr_statistics (void)
3803 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
3804 (long) htab_size (debug_expr_for_decl),
3805 (long) htab_elements (debug_expr_for_decl),
3806 htab_collisions (debug_expr_for_decl));
3809 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
3811 static void
3812 print_value_expr_statistics (void)
3814 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
3815 (long) htab_size (value_expr_for_decl),
3816 (long) htab_elements (value_expr_for_decl),
3817 htab_collisions (value_expr_for_decl));
3819 /* Lookup a debug expression for FROM, and return it if we find one. */
3821 tree
3822 decl_debug_expr_lookup (tree from)
3824 struct tree_map *h, in;
3825 in.from = from;
3827 h = htab_find_with_hash (debug_expr_for_decl, &in, htab_hash_pointer (from));
3828 if (h)
3829 return h->to;
3830 return NULL_TREE;
3833 /* Insert a mapping FROM->TO in the debug expression hashtable. */
3835 void
3836 decl_debug_expr_insert (tree from, tree to)
3838 struct tree_map *h;
3839 void **loc;
3841 h = ggc_alloc (sizeof (struct tree_map));
3842 h->hash = htab_hash_pointer (from);
3843 h->from = from;
3844 h->to = to;
3845 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
3846 *(struct tree_map **) loc = h;
3849 /* Lookup a value expression for FROM, and return it if we find one. */
3851 tree
3852 decl_value_expr_lookup (tree from)
3854 struct tree_map *h, in;
3855 in.from = from;
3857 h = htab_find_with_hash (value_expr_for_decl, &in, htab_hash_pointer (from));
3858 if (h)
3859 return h->to;
3860 return NULL_TREE;
3863 /* Insert a mapping FROM->TO in the value expression hashtable. */
3865 void
3866 decl_value_expr_insert (tree from, tree to)
3868 struct tree_map *h;
3869 void **loc;
3871 h = ggc_alloc (sizeof (struct tree_map));
3872 h->hash = htab_hash_pointer (from);
3873 h->from = from;
3874 h->to = to;
3875 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
3876 *(struct tree_map **) loc = h;
3879 /* Hashing of types so that we don't make duplicates.
3880 The entry point is `type_hash_canon'. */
3882 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3883 with types in the TREE_VALUE slots), by adding the hash codes
3884 of the individual types. */
3886 unsigned int
3887 type_hash_list (tree list, hashval_t hashcode)
3889 tree tail;
3891 for (tail = list; tail; tail = TREE_CHAIN (tail))
3892 if (TREE_VALUE (tail) != error_mark_node)
3893 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
3894 hashcode);
3896 return hashcode;
3899 /* These are the Hashtable callback functions. */
3901 /* Returns true iff the types are equivalent. */
3903 static int
3904 type_hash_eq (const void *va, const void *vb)
3906 const struct type_hash *a = va, *b = vb;
3908 /* First test the things that are the same for all types. */
3909 if (a->hash != b->hash
3910 || TREE_CODE (a->type) != TREE_CODE (b->type)
3911 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
3912 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3913 TYPE_ATTRIBUTES (b->type))
3914 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
3915 || TYPE_MODE (a->type) != TYPE_MODE (b->type))
3916 return 0;
3918 switch (TREE_CODE (a->type))
3920 case VOID_TYPE:
3921 case COMPLEX_TYPE:
3922 case POINTER_TYPE:
3923 case REFERENCE_TYPE:
3924 return 1;
3926 case VECTOR_TYPE:
3927 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
3929 case ENUMERAL_TYPE:
3930 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
3931 && !(TYPE_VALUES (a->type)
3932 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
3933 && TYPE_VALUES (b->type)
3934 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
3935 && type_list_equal (TYPE_VALUES (a->type),
3936 TYPE_VALUES (b->type))))
3937 return 0;
3939 /* ... fall through ... */
3941 case INTEGER_TYPE:
3942 case REAL_TYPE:
3943 case BOOLEAN_TYPE:
3944 case CHAR_TYPE:
3945 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3946 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3947 TYPE_MAX_VALUE (b->type)))
3948 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3949 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3950 TYPE_MIN_VALUE (b->type))));
3952 case OFFSET_TYPE:
3953 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
3955 case METHOD_TYPE:
3956 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
3957 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3958 || (TYPE_ARG_TYPES (a->type)
3959 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3960 && TYPE_ARG_TYPES (b->type)
3961 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3962 && type_list_equal (TYPE_ARG_TYPES (a->type),
3963 TYPE_ARG_TYPES (b->type)))));
3965 case ARRAY_TYPE:
3966 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
3968 case RECORD_TYPE:
3969 case UNION_TYPE:
3970 case QUAL_UNION_TYPE:
3971 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
3972 || (TYPE_FIELDS (a->type)
3973 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
3974 && TYPE_FIELDS (b->type)
3975 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
3976 && type_list_equal (TYPE_FIELDS (a->type),
3977 TYPE_FIELDS (b->type))));
3979 case FUNCTION_TYPE:
3980 return (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3981 || (TYPE_ARG_TYPES (a->type)
3982 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3983 && TYPE_ARG_TYPES (b->type)
3984 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3985 && type_list_equal (TYPE_ARG_TYPES (a->type),
3986 TYPE_ARG_TYPES (b->type))));
3988 default:
3989 return 0;
3993 /* Return the cached hash value. */
3995 static hashval_t
3996 type_hash_hash (const void *item)
3998 return ((const struct type_hash *) item)->hash;
4001 /* Look in the type hash table for a type isomorphic to TYPE.
4002 If one is found, return it. Otherwise return 0. */
4004 tree
4005 type_hash_lookup (hashval_t hashcode, tree type)
4007 struct type_hash *h, in;
4009 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4010 must call that routine before comparing TYPE_ALIGNs. */
4011 layout_type (type);
4013 in.hash = hashcode;
4014 in.type = type;
4016 h = htab_find_with_hash (type_hash_table, &in, hashcode);
4017 if (h)
4018 return h->type;
4019 return NULL_TREE;
4022 /* Add an entry to the type-hash-table
4023 for a type TYPE whose hash code is HASHCODE. */
4025 void
4026 type_hash_add (hashval_t hashcode, tree type)
4028 struct type_hash *h;
4029 void **loc;
4031 h = ggc_alloc (sizeof (struct type_hash));
4032 h->hash = hashcode;
4033 h->type = type;
4034 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4035 *(struct type_hash **) loc = h;
4038 /* Given TYPE, and HASHCODE its hash code, return the canonical
4039 object for an identical type if one already exists.
4040 Otherwise, return TYPE, and record it as the canonical object.
4042 To use this function, first create a type of the sort you want.
4043 Then compute its hash code from the fields of the type that
4044 make it different from other similar types.
4045 Then call this function and use the value. */
4047 tree
4048 type_hash_canon (unsigned int hashcode, tree type)
4050 tree t1;
4052 /* The hash table only contains main variants, so ensure that's what we're
4053 being passed. */
4054 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4056 if (!lang_hooks.types.hash_types)
4057 return type;
4059 /* See if the type is in the hash table already. If so, return it.
4060 Otherwise, add the type. */
4061 t1 = type_hash_lookup (hashcode, type);
4062 if (t1 != 0)
4064 #ifdef GATHER_STATISTICS
4065 tree_node_counts[(int) t_kind]--;
4066 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4067 #endif
4068 return t1;
4070 else
4072 type_hash_add (hashcode, type);
4073 return type;
4077 /* See if the data pointed to by the type hash table is marked. We consider
4078 it marked if the type is marked or if a debug type number or symbol
4079 table entry has been made for the type. This reduces the amount of
4080 debugging output and eliminates that dependency of the debug output on
4081 the number of garbage collections. */
4083 static int
4084 type_hash_marked_p (const void *p)
4086 tree type = ((struct type_hash *) p)->type;
4088 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4091 static void
4092 print_type_hash_statistics (void)
4094 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4095 (long) htab_size (type_hash_table),
4096 (long) htab_elements (type_hash_table),
4097 htab_collisions (type_hash_table));
4100 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4101 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4102 by adding the hash codes of the individual attributes. */
4104 unsigned int
4105 attribute_hash_list (tree list, hashval_t hashcode)
4107 tree tail;
4109 for (tail = list; tail; tail = TREE_CHAIN (tail))
4110 /* ??? Do we want to add in TREE_VALUE too? */
4111 hashcode = iterative_hash_object
4112 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4113 return hashcode;
4116 /* Given two lists of attributes, return true if list l2 is
4117 equivalent to l1. */
4120 attribute_list_equal (tree l1, tree l2)
4122 return attribute_list_contained (l1, l2)
4123 && attribute_list_contained (l2, l1);
4126 /* Given two lists of attributes, return true if list L2 is
4127 completely contained within L1. */
4128 /* ??? This would be faster if attribute names were stored in a canonicalized
4129 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4130 must be used to show these elements are equivalent (which they are). */
4131 /* ??? It's not clear that attributes with arguments will always be handled
4132 correctly. */
4135 attribute_list_contained (tree l1, tree l2)
4137 tree t1, t2;
4139 /* First check the obvious, maybe the lists are identical. */
4140 if (l1 == l2)
4141 return 1;
4143 /* Maybe the lists are similar. */
4144 for (t1 = l1, t2 = l2;
4145 t1 != 0 && t2 != 0
4146 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4147 && TREE_VALUE (t1) == TREE_VALUE (t2);
4148 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4150 /* Maybe the lists are equal. */
4151 if (t1 == 0 && t2 == 0)
4152 return 1;
4154 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4156 tree attr;
4157 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
4158 attr != NULL_TREE;
4159 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4160 TREE_CHAIN (attr)))
4162 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4163 break;
4166 if (attr == 0)
4167 return 0;
4169 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
4170 return 0;
4173 return 1;
4176 /* Given two lists of types
4177 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4178 return 1 if the lists contain the same types in the same order.
4179 Also, the TREE_PURPOSEs must match. */
4182 type_list_equal (tree l1, tree l2)
4184 tree t1, t2;
4186 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4187 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4188 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4189 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4190 && (TREE_TYPE (TREE_PURPOSE (t1))
4191 == TREE_TYPE (TREE_PURPOSE (t2))))))
4192 return 0;
4194 return t1 == t2;
4197 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4198 given by TYPE. If the argument list accepts variable arguments,
4199 then this function counts only the ordinary arguments. */
4202 type_num_arguments (tree type)
4204 int i = 0;
4205 tree t;
4207 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4208 /* If the function does not take a variable number of arguments,
4209 the last element in the list will have type `void'. */
4210 if (VOID_TYPE_P (TREE_VALUE (t)))
4211 break;
4212 else
4213 ++i;
4215 return i;
4218 /* Nonzero if integer constants T1 and T2
4219 represent the same constant value. */
4222 tree_int_cst_equal (tree t1, tree t2)
4224 if (t1 == t2)
4225 return 1;
4227 if (t1 == 0 || t2 == 0)
4228 return 0;
4230 if (TREE_CODE (t1) == INTEGER_CST
4231 && TREE_CODE (t2) == INTEGER_CST
4232 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4233 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4234 return 1;
4236 return 0;
4239 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4240 The precise way of comparison depends on their data type. */
4243 tree_int_cst_lt (tree t1, tree t2)
4245 if (t1 == t2)
4246 return 0;
4248 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4250 int t1_sgn = tree_int_cst_sgn (t1);
4251 int t2_sgn = tree_int_cst_sgn (t2);
4253 if (t1_sgn < t2_sgn)
4254 return 1;
4255 else if (t1_sgn > t2_sgn)
4256 return 0;
4257 /* Otherwise, both are non-negative, so we compare them as
4258 unsigned just in case one of them would overflow a signed
4259 type. */
4261 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4262 return INT_CST_LT (t1, t2);
4264 return INT_CST_LT_UNSIGNED (t1, t2);
4267 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
4270 tree_int_cst_compare (tree t1, tree t2)
4272 if (tree_int_cst_lt (t1, t2))
4273 return -1;
4274 else if (tree_int_cst_lt (t2, t1))
4275 return 1;
4276 else
4277 return 0;
4280 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4281 the host. If POS is zero, the value can be represented in a single
4282 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
4283 be represented in a single unsigned HOST_WIDE_INT. */
4286 host_integerp (tree t, int pos)
4288 return (TREE_CODE (t) == INTEGER_CST
4289 && ! TREE_OVERFLOW (t)
4290 && ((TREE_INT_CST_HIGH (t) == 0
4291 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4292 || (! pos && TREE_INT_CST_HIGH (t) == -1
4293 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4294 && !TYPE_UNSIGNED (TREE_TYPE (t)))
4295 || (pos && TREE_INT_CST_HIGH (t) == 0)));
4298 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4299 INTEGER_CST and there is no overflow. POS is nonzero if the result must
4300 be non-negative. We must be able to satisfy the above conditions. */
4302 HOST_WIDE_INT
4303 tree_low_cst (tree t, int pos)
4305 gcc_assert (host_integerp (t, pos));
4306 return TREE_INT_CST_LOW (t);
4309 /* Return the most significant bit of the integer constant T. */
4312 tree_int_cst_msb (tree t)
4314 int prec;
4315 HOST_WIDE_INT h;
4316 unsigned HOST_WIDE_INT l;
4318 /* Note that using TYPE_PRECISION here is wrong. We care about the
4319 actual bits, not the (arbitrary) range of the type. */
4320 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
4321 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
4322 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
4323 return (l & 1) == 1;
4326 /* Return an indication of the sign of the integer constant T.
4327 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
4328 Note that -1 will never be returned it T's type is unsigned. */
4331 tree_int_cst_sgn (tree t)
4333 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
4334 return 0;
4335 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
4336 return 1;
4337 else if (TREE_INT_CST_HIGH (t) < 0)
4338 return -1;
4339 else
4340 return 1;
4343 /* Compare two constructor-element-type constants. Return 1 if the lists
4344 are known to be equal; otherwise return 0. */
4347 simple_cst_list_equal (tree l1, tree l2)
4349 while (l1 != NULL_TREE && l2 != NULL_TREE)
4351 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4352 return 0;
4354 l1 = TREE_CHAIN (l1);
4355 l2 = TREE_CHAIN (l2);
4358 return l1 == l2;
4361 /* Return truthvalue of whether T1 is the same tree structure as T2.
4362 Return 1 if they are the same.
4363 Return 0 if they are understandably different.
4364 Return -1 if either contains tree structure not understood by
4365 this function. */
4368 simple_cst_equal (tree t1, tree t2)
4370 enum tree_code code1, code2;
4371 int cmp;
4372 int i;
4374 if (t1 == t2)
4375 return 1;
4376 if (t1 == 0 || t2 == 0)
4377 return 0;
4379 code1 = TREE_CODE (t1);
4380 code2 = TREE_CODE (t2);
4382 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
4384 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
4385 || code2 == NON_LVALUE_EXPR)
4386 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4387 else
4388 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
4391 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
4392 || code2 == NON_LVALUE_EXPR)
4393 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
4395 if (code1 != code2)
4396 return 0;
4398 switch (code1)
4400 case INTEGER_CST:
4401 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4402 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
4404 case REAL_CST:
4405 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
4407 case STRING_CST:
4408 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
4409 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
4410 TREE_STRING_LENGTH (t1)));
4412 case CONSTRUCTOR:
4414 unsigned HOST_WIDE_INT idx;
4415 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
4416 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
4418 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
4419 return false;
4421 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
4422 /* ??? Should we handle also fields here? */
4423 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
4424 VEC_index (constructor_elt, v2, idx)->value))
4425 return false;
4426 return true;
4429 case SAVE_EXPR:
4430 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4432 case CALL_EXPR:
4433 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4434 if (cmp <= 0)
4435 return cmp;
4436 return
4437 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4439 case TARGET_EXPR:
4440 /* Special case: if either target is an unallocated VAR_DECL,
4441 it means that it's going to be unified with whatever the
4442 TARGET_EXPR is really supposed to initialize, so treat it
4443 as being equivalent to anything. */
4444 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
4445 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
4446 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
4447 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
4448 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
4449 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
4450 cmp = 1;
4451 else
4452 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4454 if (cmp <= 0)
4455 return cmp;
4457 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4459 case WITH_CLEANUP_EXPR:
4460 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4461 if (cmp <= 0)
4462 return cmp;
4464 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
4466 case COMPONENT_REF:
4467 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
4468 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4470 return 0;
4472 case VAR_DECL:
4473 case PARM_DECL:
4474 case CONST_DECL:
4475 case FUNCTION_DECL:
4476 return 0;
4478 default:
4479 break;
4482 /* This general rule works for most tree codes. All exceptions should be
4483 handled above. If this is a language-specific tree code, we can't
4484 trust what might be in the operand, so say we don't know
4485 the situation. */
4486 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
4487 return -1;
4489 switch (TREE_CODE_CLASS (code1))
4491 case tcc_unary:
4492 case tcc_binary:
4493 case tcc_comparison:
4494 case tcc_expression:
4495 case tcc_reference:
4496 case tcc_statement:
4497 cmp = 1;
4498 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
4500 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
4501 if (cmp <= 0)
4502 return cmp;
4505 return cmp;
4507 default:
4508 return -1;
4512 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
4513 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
4514 than U, respectively. */
4517 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
4519 if (tree_int_cst_sgn (t) < 0)
4520 return -1;
4521 else if (TREE_INT_CST_HIGH (t) != 0)
4522 return 1;
4523 else if (TREE_INT_CST_LOW (t) == u)
4524 return 0;
4525 else if (TREE_INT_CST_LOW (t) < u)
4526 return -1;
4527 else
4528 return 1;
4531 /* Return true if CODE represents an associative tree code. Otherwise
4532 return false. */
4533 bool
4534 associative_tree_code (enum tree_code code)
4536 switch (code)
4538 case BIT_IOR_EXPR:
4539 case BIT_AND_EXPR:
4540 case BIT_XOR_EXPR:
4541 case PLUS_EXPR:
4542 case MULT_EXPR:
4543 case MIN_EXPR:
4544 case MAX_EXPR:
4545 return true;
4547 default:
4548 break;
4550 return false;
4553 /* Return true if CODE represents a commutative tree code. Otherwise
4554 return false. */
4555 bool
4556 commutative_tree_code (enum tree_code code)
4558 switch (code)
4560 case PLUS_EXPR:
4561 case MULT_EXPR:
4562 case MIN_EXPR:
4563 case MAX_EXPR:
4564 case BIT_IOR_EXPR:
4565 case BIT_XOR_EXPR:
4566 case BIT_AND_EXPR:
4567 case NE_EXPR:
4568 case EQ_EXPR:
4569 case UNORDERED_EXPR:
4570 case ORDERED_EXPR:
4571 case UNEQ_EXPR:
4572 case LTGT_EXPR:
4573 case TRUTH_AND_EXPR:
4574 case TRUTH_XOR_EXPR:
4575 case TRUTH_OR_EXPR:
4576 return true;
4578 default:
4579 break;
4581 return false;
4584 /* Generate a hash value for an expression. This can be used iteratively
4585 by passing a previous result as the "val" argument.
4587 This function is intended to produce the same hash for expressions which
4588 would compare equal using operand_equal_p. */
4590 hashval_t
4591 iterative_hash_expr (tree t, hashval_t val)
4593 int i;
4594 enum tree_code code;
4595 char class;
4597 if (t == NULL_TREE)
4598 return iterative_hash_pointer (t, val);
4600 code = TREE_CODE (t);
4602 switch (code)
4604 /* Alas, constants aren't shared, so we can't rely on pointer
4605 identity. */
4606 case INTEGER_CST:
4607 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
4608 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
4609 case REAL_CST:
4611 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
4613 return iterative_hash_hashval_t (val2, val);
4615 case STRING_CST:
4616 return iterative_hash (TREE_STRING_POINTER (t),
4617 TREE_STRING_LENGTH (t), val);
4618 case COMPLEX_CST:
4619 val = iterative_hash_expr (TREE_REALPART (t), val);
4620 return iterative_hash_expr (TREE_IMAGPART (t), val);
4621 case VECTOR_CST:
4622 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
4624 case SSA_NAME:
4625 case VALUE_HANDLE:
4626 /* we can just compare by pointer. */
4627 return iterative_hash_pointer (t, val);
4629 case TREE_LIST:
4630 /* A list of expressions, for a CALL_EXPR or as the elements of a
4631 VECTOR_CST. */
4632 for (; t; t = TREE_CHAIN (t))
4633 val = iterative_hash_expr (TREE_VALUE (t), val);
4634 return val;
4635 case CONSTRUCTOR:
4637 unsigned HOST_WIDE_INT idx;
4638 tree field, value;
4639 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4641 val = iterative_hash_expr (field, val);
4642 val = iterative_hash_expr (value, val);
4644 return val;
4646 case FUNCTION_DECL:
4647 /* When referring to a built-in FUNCTION_DECL, use the
4648 __builtin__ form. Otherwise nodes that compare equal
4649 according to operand_equal_p might get different
4650 hash codes. */
4651 if (DECL_BUILT_IN (t))
4653 val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)],
4654 val);
4655 return val;
4657 /* else FALL THROUGH */
4658 default:
4659 class = TREE_CODE_CLASS (code);
4661 if (class == tcc_declaration)
4663 /* Otherwise, we can just compare decls by pointer. */
4664 val = iterative_hash_pointer (t, val);
4666 else
4668 gcc_assert (IS_EXPR_CODE_CLASS (class));
4670 val = iterative_hash_object (code, val);
4672 /* Don't hash the type, that can lead to having nodes which
4673 compare equal according to operand_equal_p, but which
4674 have different hash codes. */
4675 if (code == NOP_EXPR
4676 || code == CONVERT_EXPR
4677 || code == NON_LVALUE_EXPR)
4679 /* Make sure to include signness in the hash computation. */
4680 val += TYPE_UNSIGNED (TREE_TYPE (t));
4681 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
4684 else if (commutative_tree_code (code))
4686 /* It's a commutative expression. We want to hash it the same
4687 however it appears. We do this by first hashing both operands
4688 and then rehashing based on the order of their independent
4689 hashes. */
4690 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
4691 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
4692 hashval_t t;
4694 if (one > two)
4695 t = one, one = two, two = t;
4697 val = iterative_hash_hashval_t (one, val);
4698 val = iterative_hash_hashval_t (two, val);
4700 else
4701 for (i = TREE_CODE_LENGTH (code) - 1; i >= 0; --i)
4702 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
4704 return val;
4705 break;
4709 /* Constructors for pointer, array and function types.
4710 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
4711 constructed by language-dependent code, not here.) */
4713 /* Construct, lay out and return the type of pointers to TO_TYPE with
4714 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
4715 reference all of memory. If such a type has already been
4716 constructed, reuse it. */
4718 tree
4719 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
4720 bool can_alias_all)
4722 tree t;
4724 if (to_type == error_mark_node)
4725 return error_mark_node;
4727 /* In some cases, languages will have things that aren't a POINTER_TYPE
4728 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
4729 In that case, return that type without regard to the rest of our
4730 operands.
4732 ??? This is a kludge, but consistent with the way this function has
4733 always operated and there doesn't seem to be a good way to avoid this
4734 at the moment. */
4735 if (TYPE_POINTER_TO (to_type) != 0
4736 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
4737 return TYPE_POINTER_TO (to_type);
4739 /* First, if we already have a type for pointers to TO_TYPE and it's
4740 the proper mode, use it. */
4741 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
4742 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
4743 return t;
4745 t = make_node (POINTER_TYPE);
4747 TREE_TYPE (t) = to_type;
4748 TYPE_MODE (t) = mode;
4749 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
4750 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
4751 TYPE_POINTER_TO (to_type) = t;
4753 /* Lay out the type. This function has many callers that are concerned
4754 with expression-construction, and this simplifies them all. */
4755 layout_type (t);
4757 return t;
4760 /* By default build pointers in ptr_mode. */
4762 tree
4763 build_pointer_type (tree to_type)
4765 return build_pointer_type_for_mode (to_type, ptr_mode, false);
4768 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
4770 tree
4771 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
4772 bool can_alias_all)
4774 tree t;
4776 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
4777 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
4778 In that case, return that type without regard to the rest of our
4779 operands.
4781 ??? This is a kludge, but consistent with the way this function has
4782 always operated and there doesn't seem to be a good way to avoid this
4783 at the moment. */
4784 if (TYPE_REFERENCE_TO (to_type) != 0
4785 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
4786 return TYPE_REFERENCE_TO (to_type);
4788 /* First, if we already have a type for pointers to TO_TYPE and it's
4789 the proper mode, use it. */
4790 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
4791 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
4792 return t;
4794 t = make_node (REFERENCE_TYPE);
4796 TREE_TYPE (t) = to_type;
4797 TYPE_MODE (t) = mode;
4798 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
4799 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
4800 TYPE_REFERENCE_TO (to_type) = t;
4802 layout_type (t);
4804 return t;
4808 /* Build the node for the type of references-to-TO_TYPE by default
4809 in ptr_mode. */
4811 tree
4812 build_reference_type (tree to_type)
4814 return build_reference_type_for_mode (to_type, ptr_mode, false);
4817 /* Build a type that is compatible with t but has no cv quals anywhere
4818 in its type, thus
4820 const char *const *const * -> char ***. */
4822 tree
4823 build_type_no_quals (tree t)
4825 switch (TREE_CODE (t))
4827 case POINTER_TYPE:
4828 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
4829 TYPE_MODE (t),
4830 TYPE_REF_CAN_ALIAS_ALL (t));
4831 case REFERENCE_TYPE:
4832 return
4833 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
4834 TYPE_MODE (t),
4835 TYPE_REF_CAN_ALIAS_ALL (t));
4836 default:
4837 return TYPE_MAIN_VARIANT (t);
4841 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
4842 MAXVAL should be the maximum value in the domain
4843 (one less than the length of the array).
4845 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
4846 We don't enforce this limit, that is up to caller (e.g. language front end).
4847 The limit exists because the result is a signed type and we don't handle
4848 sizes that use more than one HOST_WIDE_INT. */
4850 tree
4851 build_index_type (tree maxval)
4853 tree itype = make_node (INTEGER_TYPE);
4855 TREE_TYPE (itype) = sizetype;
4856 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
4857 TYPE_MIN_VALUE (itype) = size_zero_node;
4858 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
4859 TYPE_MODE (itype) = TYPE_MODE (sizetype);
4860 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
4861 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
4862 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
4863 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
4865 if (host_integerp (maxval, 1))
4866 return type_hash_canon (tree_low_cst (maxval, 1), itype);
4867 else
4868 return itype;
4871 /* Builds a signed or unsigned integer type of precision PRECISION.
4872 Used for C bitfields whose precision does not match that of
4873 built-in target types. */
4874 tree
4875 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
4876 int unsignedp)
4878 tree itype = make_node (INTEGER_TYPE);
4880 TYPE_PRECISION (itype) = precision;
4882 if (unsignedp)
4883 fixup_unsigned_type (itype);
4884 else
4885 fixup_signed_type (itype);
4887 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
4888 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
4890 return itype;
4893 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
4894 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
4895 low bound LOWVAL and high bound HIGHVAL.
4896 if TYPE==NULL_TREE, sizetype is used. */
4898 tree
4899 build_range_type (tree type, tree lowval, tree highval)
4901 tree itype = make_node (INTEGER_TYPE);
4903 TREE_TYPE (itype) = type;
4904 if (type == NULL_TREE)
4905 type = sizetype;
4907 TYPE_MIN_VALUE (itype) = convert (type, lowval);
4908 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
4910 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
4911 TYPE_MODE (itype) = TYPE_MODE (type);
4912 TYPE_SIZE (itype) = TYPE_SIZE (type);
4913 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
4914 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
4915 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
4917 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
4918 return type_hash_canon (tree_low_cst (highval, 0)
4919 - tree_low_cst (lowval, 0),
4920 itype);
4921 else
4922 return itype;
4925 /* Just like build_index_type, but takes lowval and highval instead
4926 of just highval (maxval). */
4928 tree
4929 build_index_2_type (tree lowval, tree highval)
4931 return build_range_type (sizetype, lowval, highval);
4934 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
4935 and number of elements specified by the range of values of INDEX_TYPE.
4936 If such a type has already been constructed, reuse it. */
4938 tree
4939 build_array_type (tree elt_type, tree index_type)
4941 tree t;
4942 hashval_t hashcode = 0;
4944 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
4946 error ("arrays of functions are not meaningful");
4947 elt_type = integer_type_node;
4950 t = make_node (ARRAY_TYPE);
4951 TREE_TYPE (t) = elt_type;
4952 TYPE_DOMAIN (t) = index_type;
4954 if (index_type == 0)
4956 layout_type (t);
4957 return t;
4960 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
4961 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
4962 t = type_hash_canon (hashcode, t);
4964 if (!COMPLETE_TYPE_P (t))
4965 layout_type (t);
4966 return t;
4969 /* Return the TYPE of the elements comprising
4970 the innermost dimension of ARRAY. */
4972 tree
4973 get_inner_array_type (tree array)
4975 tree type = TREE_TYPE (array);
4977 while (TREE_CODE (type) == ARRAY_TYPE)
4978 type = TREE_TYPE (type);
4980 return type;
4983 /* Construct, lay out and return
4984 the type of functions returning type VALUE_TYPE
4985 given arguments of types ARG_TYPES.
4986 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
4987 are data type nodes for the arguments of the function.
4988 If such a type has already been constructed, reuse it. */
4990 tree
4991 build_function_type (tree value_type, tree arg_types)
4993 tree t;
4994 hashval_t hashcode = 0;
4996 if (TREE_CODE (value_type) == FUNCTION_TYPE)
4998 error ("function return type cannot be function");
4999 value_type = integer_type_node;
5002 /* Make a node of the sort we want. */
5003 t = make_node (FUNCTION_TYPE);
5004 TREE_TYPE (t) = value_type;
5005 TYPE_ARG_TYPES (t) = arg_types;
5007 /* If we already have such a type, use the old one. */
5008 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5009 hashcode = type_hash_list (arg_types, hashcode);
5010 t = type_hash_canon (hashcode, t);
5012 if (!COMPLETE_TYPE_P (t))
5013 layout_type (t);
5014 return t;
5017 /* Build a function type. The RETURN_TYPE is the type returned by the
5018 function. If additional arguments are provided, they are
5019 additional argument types. The list of argument types must always
5020 be terminated by NULL_TREE. */
5022 tree
5023 build_function_type_list (tree return_type, ...)
5025 tree t, args, last;
5026 va_list p;
5028 va_start (p, return_type);
5030 t = va_arg (p, tree);
5031 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
5032 args = tree_cons (NULL_TREE, t, args);
5034 if (args == NULL_TREE)
5035 args = void_list_node;
5036 else
5038 last = args;
5039 args = nreverse (args);
5040 TREE_CHAIN (last) = void_list_node;
5042 args = build_function_type (return_type, args);
5044 va_end (p);
5045 return args;
5048 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
5049 and ARGTYPES (a TREE_LIST) are the return type and arguments types
5050 for the method. An implicit additional parameter (of type
5051 pointer-to-BASETYPE) is added to the ARGTYPES. */
5053 tree
5054 build_method_type_directly (tree basetype,
5055 tree rettype,
5056 tree argtypes)
5058 tree t;
5059 tree ptype;
5060 int hashcode = 0;
5062 /* Make a node of the sort we want. */
5063 t = make_node (METHOD_TYPE);
5065 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5066 TREE_TYPE (t) = rettype;
5067 ptype = build_pointer_type (basetype);
5069 /* The actual arglist for this function includes a "hidden" argument
5070 which is "this". Put it into the list of argument types. */
5071 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
5072 TYPE_ARG_TYPES (t) = argtypes;
5074 /* If we already have such a type, use the old one. */
5075 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5076 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
5077 hashcode = type_hash_list (argtypes, hashcode);
5078 t = type_hash_canon (hashcode, t);
5080 if (!COMPLETE_TYPE_P (t))
5081 layout_type (t);
5083 return t;
5086 /* Construct, lay out and return the type of methods belonging to class
5087 BASETYPE and whose arguments and values are described by TYPE.
5088 If that type exists already, reuse it.
5089 TYPE must be a FUNCTION_TYPE node. */
5091 tree
5092 build_method_type (tree basetype, tree type)
5094 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
5096 return build_method_type_directly (basetype,
5097 TREE_TYPE (type),
5098 TYPE_ARG_TYPES (type));
5101 /* Construct, lay out and return the type of offsets to a value
5102 of type TYPE, within an object of type BASETYPE.
5103 If a suitable offset type exists already, reuse it. */
5105 tree
5106 build_offset_type (tree basetype, tree type)
5108 tree t;
5109 hashval_t hashcode = 0;
5111 /* Make a node of the sort we want. */
5112 t = make_node (OFFSET_TYPE);
5114 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5115 TREE_TYPE (t) = type;
5117 /* If we already have such a type, use the old one. */
5118 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5119 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
5120 t = type_hash_canon (hashcode, t);
5122 if (!COMPLETE_TYPE_P (t))
5123 layout_type (t);
5125 return t;
5128 /* Create a complex type whose components are COMPONENT_TYPE. */
5130 tree
5131 build_complex_type (tree component_type)
5133 tree t;
5134 hashval_t hashcode;
5136 /* Make a node of the sort we want. */
5137 t = make_node (COMPLEX_TYPE);
5139 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
5141 /* If we already have such a type, use the old one. */
5142 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
5143 t = type_hash_canon (hashcode, t);
5145 if (!COMPLETE_TYPE_P (t))
5146 layout_type (t);
5148 /* If we are writing Dwarf2 output we need to create a name,
5149 since complex is a fundamental type. */
5150 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
5151 && ! TYPE_NAME (t))
5153 const char *name;
5154 if (component_type == char_type_node)
5155 name = "complex char";
5156 else if (component_type == signed_char_type_node)
5157 name = "complex signed char";
5158 else if (component_type == unsigned_char_type_node)
5159 name = "complex unsigned char";
5160 else if (component_type == short_integer_type_node)
5161 name = "complex short int";
5162 else if (component_type == short_unsigned_type_node)
5163 name = "complex short unsigned int";
5164 else if (component_type == integer_type_node)
5165 name = "complex int";
5166 else if (component_type == unsigned_type_node)
5167 name = "complex unsigned int";
5168 else if (component_type == long_integer_type_node)
5169 name = "complex long int";
5170 else if (component_type == long_unsigned_type_node)
5171 name = "complex long unsigned int";
5172 else if (component_type == long_long_integer_type_node)
5173 name = "complex long long int";
5174 else if (component_type == long_long_unsigned_type_node)
5175 name = "complex long long unsigned int";
5176 else
5177 name = 0;
5179 if (name != 0)
5180 TYPE_NAME (t) = get_identifier (name);
5183 return build_qualified_type (t, TYPE_QUALS (component_type));
5186 /* Return OP, stripped of any conversions to wider types as much as is safe.
5187 Converting the value back to OP's type makes a value equivalent to OP.
5189 If FOR_TYPE is nonzero, we return a value which, if converted to
5190 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
5192 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
5193 narrowest type that can hold the value, even if they don't exactly fit.
5194 Otherwise, bit-field references are changed to a narrower type
5195 only if they can be fetched directly from memory in that type.
5197 OP must have integer, real or enumeral type. Pointers are not allowed!
5199 There are some cases where the obvious value we could return
5200 would regenerate to OP if converted to OP's type,
5201 but would not extend like OP to wider types.
5202 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
5203 For example, if OP is (unsigned short)(signed char)-1,
5204 we avoid returning (signed char)-1 if FOR_TYPE is int,
5205 even though extending that to an unsigned short would regenerate OP,
5206 since the result of extending (signed char)-1 to (int)
5207 is different from (int) OP. */
5209 tree
5210 get_unwidened (tree op, tree for_type)
5212 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
5213 tree type = TREE_TYPE (op);
5214 unsigned final_prec
5215 = TYPE_PRECISION (for_type != 0 ? for_type : type);
5216 int uns
5217 = (for_type != 0 && for_type != type
5218 && final_prec > TYPE_PRECISION (type)
5219 && TYPE_UNSIGNED (type));
5220 tree win = op;
5222 while (TREE_CODE (op) == NOP_EXPR
5223 || TREE_CODE (op) == CONVERT_EXPR)
5225 int bitschange;
5227 /* TYPE_PRECISION on vector types has different meaning
5228 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
5229 so avoid them here. */
5230 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
5231 break;
5233 bitschange = TYPE_PRECISION (TREE_TYPE (op))
5234 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
5236 /* Truncations are many-one so cannot be removed.
5237 Unless we are later going to truncate down even farther. */
5238 if (bitschange < 0
5239 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
5240 break;
5242 /* See what's inside this conversion. If we decide to strip it,
5243 we will set WIN. */
5244 op = TREE_OPERAND (op, 0);
5246 /* If we have not stripped any zero-extensions (uns is 0),
5247 we can strip any kind of extension.
5248 If we have previously stripped a zero-extension,
5249 only zero-extensions can safely be stripped.
5250 Any extension can be stripped if the bits it would produce
5251 are all going to be discarded later by truncating to FOR_TYPE. */
5253 if (bitschange > 0)
5255 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
5256 win = op;
5257 /* TYPE_UNSIGNED says whether this is a zero-extension.
5258 Let's avoid computing it if it does not affect WIN
5259 and if UNS will not be needed again. */
5260 if ((uns
5261 || TREE_CODE (op) == NOP_EXPR
5262 || TREE_CODE (op) == CONVERT_EXPR)
5263 && TYPE_UNSIGNED (TREE_TYPE (op)))
5265 uns = 1;
5266 win = op;
5271 if (TREE_CODE (op) == COMPONENT_REF
5272 /* Since type_for_size always gives an integer type. */
5273 && TREE_CODE (type) != REAL_TYPE
5274 /* Don't crash if field not laid out yet. */
5275 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
5276 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
5278 unsigned int innerprec
5279 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
5280 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
5281 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
5282 type = lang_hooks.types.type_for_size (innerprec, unsignedp);
5284 /* We can get this structure field in the narrowest type it fits in.
5285 If FOR_TYPE is 0, do this only for a field that matches the
5286 narrower type exactly and is aligned for it
5287 The resulting extension to its nominal type (a fullword type)
5288 must fit the same conditions as for other extensions. */
5290 if (type != 0
5291 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
5292 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
5293 && (! uns || final_prec <= innerprec || unsignedp))
5295 win = build3 (COMPONENT_REF, type, TREE_OPERAND (op, 0),
5296 TREE_OPERAND (op, 1), NULL_TREE);
5297 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
5298 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
5302 return win;
5305 /* Return OP or a simpler expression for a narrower value
5306 which can be sign-extended or zero-extended to give back OP.
5307 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
5308 or 0 if the value should be sign-extended. */
5310 tree
5311 get_narrower (tree op, int *unsignedp_ptr)
5313 int uns = 0;
5314 int first = 1;
5315 tree win = op;
5316 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
5318 while (TREE_CODE (op) == NOP_EXPR)
5320 int bitschange
5321 = (TYPE_PRECISION (TREE_TYPE (op))
5322 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
5324 /* Truncations are many-one so cannot be removed. */
5325 if (bitschange < 0)
5326 break;
5328 /* See what's inside this conversion. If we decide to strip it,
5329 we will set WIN. */
5331 if (bitschange > 0)
5333 op = TREE_OPERAND (op, 0);
5334 /* An extension: the outermost one can be stripped,
5335 but remember whether it is zero or sign extension. */
5336 if (first)
5337 uns = TYPE_UNSIGNED (TREE_TYPE (op));
5338 /* Otherwise, if a sign extension has been stripped,
5339 only sign extensions can now be stripped;
5340 if a zero extension has been stripped, only zero-extensions. */
5341 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
5342 break;
5343 first = 0;
5345 else /* bitschange == 0 */
5347 /* A change in nominal type can always be stripped, but we must
5348 preserve the unsignedness. */
5349 if (first)
5350 uns = TYPE_UNSIGNED (TREE_TYPE (op));
5351 first = 0;
5352 op = TREE_OPERAND (op, 0);
5353 /* Keep trying to narrow, but don't assign op to win if it
5354 would turn an integral type into something else. */
5355 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
5356 continue;
5359 win = op;
5362 if (TREE_CODE (op) == COMPONENT_REF
5363 /* Since type_for_size always gives an integer type. */
5364 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
5365 /* Ensure field is laid out already. */
5366 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
5367 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
5369 unsigned HOST_WIDE_INT innerprec
5370 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
5371 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
5372 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
5373 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
5375 /* We can get this structure field in a narrower type that fits it,
5376 but the resulting extension to its nominal type (a fullword type)
5377 must satisfy the same conditions as for other extensions.
5379 Do this only for fields that are aligned (not bit-fields),
5380 because when bit-field insns will be used there is no
5381 advantage in doing this. */
5383 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
5384 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
5385 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
5386 && type != 0)
5388 if (first)
5389 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
5390 win = build3 (COMPONENT_REF, type, TREE_OPERAND (op, 0),
5391 TREE_OPERAND (op, 1), NULL_TREE);
5392 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
5393 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
5396 *unsignedp_ptr = uns;
5397 return win;
5400 /* Nonzero if integer constant C has a value that is permissible
5401 for type TYPE (an INTEGER_TYPE). */
5404 int_fits_type_p (tree c, tree type)
5406 tree type_low_bound = TYPE_MIN_VALUE (type);
5407 tree type_high_bound = TYPE_MAX_VALUE (type);
5408 bool ok_for_low_bound, ok_for_high_bound;
5409 tree tmp;
5411 /* If at least one bound of the type is a constant integer, we can check
5412 ourselves and maybe make a decision. If no such decision is possible, but
5413 this type is a subtype, try checking against that. Otherwise, use
5414 force_fit_type, which checks against the precision.
5416 Compute the status for each possibly constant bound, and return if we see
5417 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
5418 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
5419 for "constant known to fit". */
5421 /* Check if C >= type_low_bound. */
5422 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
5424 if (tree_int_cst_lt (c, type_low_bound))
5425 return 0;
5426 ok_for_low_bound = true;
5428 else
5429 ok_for_low_bound = false;
5431 /* Check if c <= type_high_bound. */
5432 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
5434 if (tree_int_cst_lt (type_high_bound, c))
5435 return 0;
5436 ok_for_high_bound = true;
5438 else
5439 ok_for_high_bound = false;
5441 /* If the constant fits both bounds, the result is known. */
5442 if (ok_for_low_bound && ok_for_high_bound)
5443 return 1;
5445 /* Perform some generic filtering which may allow making a decision
5446 even if the bounds are not constant. First, negative integers
5447 never fit in unsigned types, */
5448 if (TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
5449 return 0;
5451 /* Second, narrower types always fit in wider ones. */
5452 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
5453 return 1;
5455 /* Third, unsigned integers with top bit set never fit signed types. */
5456 if (! TYPE_UNSIGNED (type)
5457 && TYPE_UNSIGNED (TREE_TYPE (c))
5458 && tree_int_cst_msb (c))
5459 return 0;
5461 /* If we haven't been able to decide at this point, there nothing more we
5462 can check ourselves here. Look at the base type if we have one. */
5463 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
5464 return int_fits_type_p (c, TREE_TYPE (type));
5466 /* Or to force_fit_type, if nothing else. */
5467 tmp = copy_node (c);
5468 TREE_TYPE (tmp) = type;
5469 tmp = force_fit_type (tmp, -1, false, false);
5470 return TREE_INT_CST_HIGH (tmp) == TREE_INT_CST_HIGH (c)
5471 && TREE_INT_CST_LOW (tmp) == TREE_INT_CST_LOW (c);
5474 /* Subprogram of following function. Called by walk_tree.
5476 Return *TP if it is an automatic variable or parameter of the
5477 function passed in as DATA. */
5479 static tree
5480 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
5482 tree fn = (tree) data;
5484 if (TYPE_P (*tp))
5485 *walk_subtrees = 0;
5487 else if (DECL_P (*tp)
5488 && lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
5489 return *tp;
5491 return NULL_TREE;
5494 /* Returns true if T is, contains, or refers to a type with variable
5495 size. If FN is nonzero, only return true if a modifier of the type
5496 or position of FN is a variable or parameter inside FN.
5498 This concept is more general than that of C99 'variably modified types':
5499 in C99, a struct type is never variably modified because a VLA may not
5500 appear as a structure member. However, in GNU C code like:
5502 struct S { int i[f()]; };
5504 is valid, and other languages may define similar constructs. */
5506 bool
5507 variably_modified_type_p (tree type, tree fn)
5509 tree t;
5511 /* Test if T is either variable (if FN is zero) or an expression containing
5512 a variable in FN. */
5513 #define RETURN_TRUE_IF_VAR(T) \
5514 do { tree _t = (T); \
5515 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
5516 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
5517 return true; } while (0)
5519 if (type == error_mark_node)
5520 return false;
5522 /* If TYPE itself has variable size, it is variably modified.
5524 We do not yet have a representation of the C99 '[*]' syntax.
5525 When a representation is chosen, this function should be modified
5526 to test for that case as well. */
5527 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
5528 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT(type));
5530 switch (TREE_CODE (type))
5532 case POINTER_TYPE:
5533 case REFERENCE_TYPE:
5534 case ARRAY_TYPE:
5535 case VECTOR_TYPE:
5536 if (variably_modified_type_p (TREE_TYPE (type), fn))
5537 return true;
5538 break;
5540 case FUNCTION_TYPE:
5541 case METHOD_TYPE:
5542 /* If TYPE is a function type, it is variably modified if any of the
5543 parameters or the return type are variably modified. */
5544 if (variably_modified_type_p (TREE_TYPE (type), fn))
5545 return true;
5547 for (t = TYPE_ARG_TYPES (type);
5548 t && t != void_list_node;
5549 t = TREE_CHAIN (t))
5550 if (variably_modified_type_p (TREE_VALUE (t), fn))
5551 return true;
5552 break;
5554 case INTEGER_TYPE:
5555 case REAL_TYPE:
5556 case ENUMERAL_TYPE:
5557 case BOOLEAN_TYPE:
5558 case CHAR_TYPE:
5559 /* Scalar types are variably modified if their end points
5560 aren't constant. */
5561 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
5562 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
5563 break;
5565 case RECORD_TYPE:
5566 case UNION_TYPE:
5567 case QUAL_UNION_TYPE:
5568 /* We can't see if any of the field are variably-modified by the
5569 definition we normally use, since that would produce infinite
5570 recursion via pointers. */
5571 /* This is variably modified if some field's type is. */
5572 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5573 if (TREE_CODE (t) == FIELD_DECL)
5575 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
5576 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
5577 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
5579 if (TREE_CODE (type) == QUAL_UNION_TYPE)
5580 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
5582 break;
5584 default:
5585 break;
5588 /* The current language may have other cases to check, but in general,
5589 all other types are not variably modified. */
5590 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
5592 #undef RETURN_TRUE_IF_VAR
5595 /* Given a DECL or TYPE, return the scope in which it was declared, or
5596 NULL_TREE if there is no containing scope. */
5598 tree
5599 get_containing_scope (tree t)
5601 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
5604 /* Return the innermost context enclosing DECL that is
5605 a FUNCTION_DECL, or zero if none. */
5607 tree
5608 decl_function_context (tree decl)
5610 tree context;
5612 if (TREE_CODE (decl) == ERROR_MARK)
5613 return 0;
5615 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
5616 where we look up the function at runtime. Such functions always take
5617 a first argument of type 'pointer to real context'.
5619 C++ should really be fixed to use DECL_CONTEXT for the real context,
5620 and use something else for the "virtual context". */
5621 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
5622 context
5623 = TYPE_MAIN_VARIANT
5624 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5625 else
5626 context = DECL_CONTEXT (decl);
5628 while (context && TREE_CODE (context) != FUNCTION_DECL)
5630 if (TREE_CODE (context) == BLOCK)
5631 context = BLOCK_SUPERCONTEXT (context);
5632 else
5633 context = get_containing_scope (context);
5636 return context;
5639 /* Return the innermost context enclosing DECL that is
5640 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
5641 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
5643 tree
5644 decl_type_context (tree decl)
5646 tree context = DECL_CONTEXT (decl);
5648 while (context)
5649 switch (TREE_CODE (context))
5651 case NAMESPACE_DECL:
5652 case TRANSLATION_UNIT_DECL:
5653 return NULL_TREE;
5655 case RECORD_TYPE:
5656 case UNION_TYPE:
5657 case QUAL_UNION_TYPE:
5658 return context;
5660 case TYPE_DECL:
5661 case FUNCTION_DECL:
5662 context = DECL_CONTEXT (context);
5663 break;
5665 case BLOCK:
5666 context = BLOCK_SUPERCONTEXT (context);
5667 break;
5669 default:
5670 gcc_unreachable ();
5673 return NULL_TREE;
5676 /* CALL is a CALL_EXPR. Return the declaration for the function
5677 called, or NULL_TREE if the called function cannot be
5678 determined. */
5680 tree
5681 get_callee_fndecl (tree call)
5683 tree addr;
5685 /* It's invalid to call this function with anything but a
5686 CALL_EXPR. */
5687 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5689 /* The first operand to the CALL is the address of the function
5690 called. */
5691 addr = TREE_OPERAND (call, 0);
5693 STRIP_NOPS (addr);
5695 /* If this is a readonly function pointer, extract its initial value. */
5696 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
5697 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
5698 && DECL_INITIAL (addr))
5699 addr = DECL_INITIAL (addr);
5701 /* If the address is just `&f' for some function `f', then we know
5702 that `f' is being called. */
5703 if (TREE_CODE (addr) == ADDR_EXPR
5704 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
5705 return TREE_OPERAND (addr, 0);
5707 /* We couldn't figure out what was being called. Maybe the front
5708 end has some idea. */
5709 return lang_hooks.lang_get_callee_fndecl (call);
5712 /* Print debugging information about tree nodes generated during the compile,
5713 and any language-specific information. */
5715 void
5716 dump_tree_statistics (void)
5718 #ifdef GATHER_STATISTICS
5719 int i;
5720 int total_nodes, total_bytes;
5721 #endif
5723 fprintf (stderr, "\n??? tree nodes created\n\n");
5724 #ifdef GATHER_STATISTICS
5725 fprintf (stderr, "Kind Nodes Bytes\n");
5726 fprintf (stderr, "---------------------------------------\n");
5727 total_nodes = total_bytes = 0;
5728 for (i = 0; i < (int) all_kinds; i++)
5730 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
5731 tree_node_counts[i], tree_node_sizes[i]);
5732 total_nodes += tree_node_counts[i];
5733 total_bytes += tree_node_sizes[i];
5735 fprintf (stderr, "---------------------------------------\n");
5736 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
5737 fprintf (stderr, "---------------------------------------\n");
5738 ssanames_print_statistics ();
5739 phinodes_print_statistics ();
5740 #else
5741 fprintf (stderr, "(No per-node statistics)\n");
5742 #endif
5743 print_type_hash_statistics ();
5744 print_debug_expr_statistics ();
5745 print_value_expr_statistics ();
5746 lang_hooks.print_statistics ();
5749 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
5751 /* Generate a crc32 of a string. */
5753 unsigned
5754 crc32_string (unsigned chksum, const char *string)
5758 unsigned value = *string << 24;
5759 unsigned ix;
5761 for (ix = 8; ix--; value <<= 1)
5763 unsigned feedback;
5765 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
5766 chksum <<= 1;
5767 chksum ^= feedback;
5770 while (*string++);
5771 return chksum;
5774 /* P is a string that will be used in a symbol. Mask out any characters
5775 that are not valid in that context. */
5777 void
5778 clean_symbol_name (char *p)
5780 for (; *p; p++)
5781 if (! (ISALNUM (*p)
5782 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
5783 || *p == '$'
5784 #endif
5785 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
5786 || *p == '.'
5787 #endif
5789 *p = '_';
5792 /* Generate a name for a function unique to this translation unit.
5793 TYPE is some string to identify the purpose of this function to the
5794 linker or collect2. */
5796 tree
5797 get_file_function_name_long (const char *type)
5799 char *buf;
5800 const char *p;
5801 char *q;
5803 if (first_global_object_name)
5804 p = first_global_object_name;
5805 else
5807 /* We don't have anything that we know to be unique to this translation
5808 unit, so use what we do have and throw in some randomness. */
5809 unsigned len;
5810 const char *name = weak_global_object_name;
5811 const char *file = main_input_filename;
5813 if (! name)
5814 name = "";
5815 if (! file)
5816 file = input_filename;
5818 len = strlen (file);
5819 q = alloca (9 * 2 + len + 1);
5820 memcpy (q, file, len + 1);
5821 clean_symbol_name (q);
5823 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
5824 crc32_string (0, flag_random_seed));
5826 p = q;
5829 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
5831 /* Set up the name of the file-level functions we may need.
5832 Use a global object (which is already required to be unique over
5833 the program) rather than the file name (which imposes extra
5834 constraints). */
5835 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
5837 return get_identifier (buf);
5840 /* If KIND=='I', return a suitable global initializer (constructor) name.
5841 If KIND=='D', return a suitable global clean-up (destructor) name. */
5843 tree
5844 get_file_function_name (int kind)
5846 char p[2];
5848 p[0] = kind;
5849 p[1] = 0;
5851 return get_file_function_name_long (p);
5854 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5856 /* Complain that the tree code of NODE does not match the expected 0
5857 terminated list of trailing codes. The trailing code list can be
5858 empty, for a more vague error message. FILE, LINE, and FUNCTION
5859 are of the caller. */
5861 void
5862 tree_check_failed (const tree node, const char *file,
5863 int line, const char *function, ...)
5865 va_list args;
5866 char *buffer;
5867 unsigned length = 0;
5868 int code;
5870 va_start (args, function);
5871 while ((code = va_arg (args, int)))
5872 length += 4 + strlen (tree_code_name[code]);
5873 va_end (args);
5874 if (length)
5876 va_start (args, function);
5877 length += strlen ("expected ");
5878 buffer = alloca (length);
5879 length = 0;
5880 while ((code = va_arg (args, int)))
5882 const char *prefix = length ? " or " : "expected ";
5884 strcpy (buffer + length, prefix);
5885 length += strlen (prefix);
5886 strcpy (buffer + length, tree_code_name[code]);
5887 length += strlen (tree_code_name[code]);
5889 va_end (args);
5891 else
5892 buffer = (char *)"unexpected node";
5894 internal_error ("tree check: %s, have %s in %s, at %s:%d",
5895 buffer, tree_code_name[TREE_CODE (node)],
5896 function, trim_filename (file), line);
5899 /* Complain that the tree code of NODE does match the expected 0
5900 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
5901 the caller. */
5903 void
5904 tree_not_check_failed (const tree node, const char *file,
5905 int line, const char *function, ...)
5907 va_list args;
5908 char *buffer;
5909 unsigned length = 0;
5910 int code;
5912 va_start (args, function);
5913 while ((code = va_arg (args, int)))
5914 length += 4 + strlen (tree_code_name[code]);
5915 va_end (args);
5916 va_start (args, function);
5917 buffer = alloca (length);
5918 length = 0;
5919 while ((code = va_arg (args, int)))
5921 if (length)
5923 strcpy (buffer + length, " or ");
5924 length += 4;
5926 strcpy (buffer + length, tree_code_name[code]);
5927 length += strlen (tree_code_name[code]);
5929 va_end (args);
5931 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
5932 buffer, tree_code_name[TREE_CODE (node)],
5933 function, trim_filename (file), line);
5936 /* Similar to tree_check_failed, except that we check for a class of tree
5937 code, given in CL. */
5939 void
5940 tree_class_check_failed (const tree node, const enum tree_code_class cl,
5941 const char *file, int line, const char *function)
5943 internal_error
5944 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
5945 TREE_CODE_CLASS_STRING (cl),
5946 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
5947 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
5949 #undef DEFTREESTRUCT
5950 #define DEFTREESTRUCT(VAL, NAME) NAME,
5952 static const char *ts_enum_names[] = {
5953 #include "treestruct.def"
5955 #undef DEFTREESTRUCT
5957 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
5959 /* Similar to tree_class_check_failed, except that we check for
5960 whether CODE contains the tree structure identified by EN. */
5962 void
5963 tree_contains_struct_check_failed (const tree node,
5964 const enum tree_node_structure_enum en,
5965 const char *file, int line,
5966 const char *function)
5968 internal_error
5969 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
5970 TS_ENUM_NAME(en),
5971 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
5975 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
5976 (dynamically sized) vector. */
5978 void
5979 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
5980 const char *function)
5982 internal_error
5983 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
5984 idx + 1, len, function, trim_filename (file), line);
5987 /* Similar to above, except that the check is for the bounds of a PHI_NODE's
5988 (dynamically sized) vector. */
5990 void
5991 phi_node_elt_check_failed (int idx, int len, const char *file, int line,
5992 const char *function)
5994 internal_error
5995 ("tree check: accessed elt %d of phi_node with %d elts in %s, at %s:%d",
5996 idx + 1, len, function, trim_filename (file), line);
5999 /* Similar to above, except that the check is for the bounds of the operand
6000 vector of an expression node. */
6002 void
6003 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
6004 int line, const char *function)
6006 internal_error
6007 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
6008 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
6009 function, trim_filename (file), line);
6011 #endif /* ENABLE_TREE_CHECKING */
6013 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
6014 and mapped to the machine mode MODE. Initialize its fields and build
6015 the information necessary for debugging output. */
6017 static tree
6018 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
6020 tree t = make_node (VECTOR_TYPE);
6022 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
6023 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
6024 TYPE_MODE (t) = mode;
6025 TYPE_READONLY (t) = TYPE_READONLY (innertype);
6026 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
6028 layout_type (t);
6031 tree index = build_int_cst (NULL_TREE, nunits - 1);
6032 tree array = build_array_type (innertype, build_index_type (index));
6033 tree rt = make_node (RECORD_TYPE);
6035 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
6036 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
6037 layout_type (rt);
6038 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
6039 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
6040 the representation type, and we want to find that die when looking up
6041 the vector type. This is most easily achieved by making the TYPE_UID
6042 numbers equal. */
6043 TYPE_UID (rt) = TYPE_UID (t);
6046 /* Build our main variant, based on the main variant of the inner type. */
6047 if (TYPE_MAIN_VARIANT (innertype) != innertype)
6049 tree innertype_main_variant = TYPE_MAIN_VARIANT (innertype);
6050 unsigned int hash = TYPE_HASH (innertype_main_variant);
6051 TYPE_MAIN_VARIANT (t)
6052 = type_hash_canon (hash, make_vector_type (innertype_main_variant,
6053 nunits, mode));
6056 return t;
6059 static tree
6060 make_or_reuse_type (unsigned size, int unsignedp)
6062 if (size == INT_TYPE_SIZE)
6063 return unsignedp ? unsigned_type_node : integer_type_node;
6064 if (size == CHAR_TYPE_SIZE)
6065 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
6066 if (size == SHORT_TYPE_SIZE)
6067 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
6068 if (size == LONG_TYPE_SIZE)
6069 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
6070 if (size == LONG_LONG_TYPE_SIZE)
6071 return (unsignedp ? long_long_unsigned_type_node
6072 : long_long_integer_type_node);
6074 if (unsignedp)
6075 return make_unsigned_type (size);
6076 else
6077 return make_signed_type (size);
6080 /* Create nodes for all integer types (and error_mark_node) using the sizes
6081 of C datatypes. The caller should call set_sizetype soon after calling
6082 this function to select one of the types as sizetype. */
6084 void
6085 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
6087 error_mark_node = make_node (ERROR_MARK);
6088 TREE_TYPE (error_mark_node) = error_mark_node;
6090 initialize_sizetypes (signed_sizetype);
6092 /* Define both `signed char' and `unsigned char'. */
6093 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6094 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6096 /* Define `char', which is like either `signed char' or `unsigned char'
6097 but not the same as either. */
6098 char_type_node
6099 = (signed_char
6100 ? make_signed_type (CHAR_TYPE_SIZE)
6101 : make_unsigned_type (CHAR_TYPE_SIZE));
6103 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6104 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6105 integer_type_node = make_signed_type (INT_TYPE_SIZE);
6106 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6107 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6108 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6109 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6110 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6112 /* Define a boolean type. This type only represents boolean values but
6113 may be larger than char depending on the value of BOOL_TYPE_SIZE.
6114 Front ends which want to override this size (i.e. Java) can redefine
6115 boolean_type_node before calling build_common_tree_nodes_2. */
6116 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6117 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6118 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
6119 TYPE_PRECISION (boolean_type_node) = 1;
6121 /* Fill in the rest of the sized types. Reuse existing type nodes
6122 when possible. */
6123 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
6124 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
6125 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
6126 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
6127 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
6129 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
6130 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
6131 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
6132 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
6133 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
6135 access_public_node = get_identifier ("public");
6136 access_protected_node = get_identifier ("protected");
6137 access_private_node = get_identifier ("private");
6140 /* Call this function after calling build_common_tree_nodes and set_sizetype.
6141 It will create several other common tree nodes. */
6143 void
6144 build_common_tree_nodes_2 (int short_double)
6146 /* Define these next since types below may used them. */
6147 integer_zero_node = build_int_cst (NULL_TREE, 0);
6148 integer_one_node = build_int_cst (NULL_TREE, 1);
6149 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
6151 size_zero_node = size_int (0);
6152 size_one_node = size_int (1);
6153 bitsize_zero_node = bitsize_int (0);
6154 bitsize_one_node = bitsize_int (1);
6155 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
6157 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
6158 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
6160 void_type_node = make_node (VOID_TYPE);
6161 layout_type (void_type_node);
6163 /* We are not going to have real types in C with less than byte alignment,
6164 so we might as well not have any types that claim to have it. */
6165 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
6166 TYPE_USER_ALIGN (void_type_node) = 0;
6168 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
6169 layout_type (TREE_TYPE (null_pointer_node));
6171 ptr_type_node = build_pointer_type (void_type_node);
6172 const_ptr_type_node
6173 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
6174 fileptr_type_node = ptr_type_node;
6176 float_type_node = make_node (REAL_TYPE);
6177 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6178 layout_type (float_type_node);
6180 double_type_node = make_node (REAL_TYPE);
6181 if (short_double)
6182 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6183 else
6184 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6185 layout_type (double_type_node);
6187 long_double_type_node = make_node (REAL_TYPE);
6188 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6189 layout_type (long_double_type_node);
6191 float_ptr_type_node = build_pointer_type (float_type_node);
6192 double_ptr_type_node = build_pointer_type (double_type_node);
6193 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
6194 integer_ptr_type_node = build_pointer_type (integer_type_node);
6196 complex_integer_type_node = make_node (COMPLEX_TYPE);
6197 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6198 layout_type (complex_integer_type_node);
6200 complex_float_type_node = make_node (COMPLEX_TYPE);
6201 TREE_TYPE (complex_float_type_node) = float_type_node;
6202 layout_type (complex_float_type_node);
6204 complex_double_type_node = make_node (COMPLEX_TYPE);
6205 TREE_TYPE (complex_double_type_node) = double_type_node;
6206 layout_type (complex_double_type_node);
6208 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6209 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6210 layout_type (complex_long_double_type_node);
6213 tree t = targetm.build_builtin_va_list ();
6215 /* Many back-ends define record types without setting TYPE_NAME.
6216 If we copied the record type here, we'd keep the original
6217 record type without a name. This breaks name mangling. So,
6218 don't copy record types and let c_common_nodes_and_builtins()
6219 declare the type to be __builtin_va_list. */
6220 if (TREE_CODE (t) != RECORD_TYPE)
6221 t = build_variant_type_copy (t);
6223 va_list_type_node = t;
6227 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
6229 static void
6230 local_define_builtin (const char *name, tree type, enum built_in_function code,
6231 const char *library_name, int ecf_flags)
6233 tree decl;
6235 decl = lang_hooks.builtin_function (name, type, code, BUILT_IN_NORMAL,
6236 library_name, NULL_TREE);
6237 if (ecf_flags & ECF_CONST)
6238 TREE_READONLY (decl) = 1;
6239 if (ecf_flags & ECF_PURE)
6240 DECL_IS_PURE (decl) = 1;
6241 if (ecf_flags & ECF_NORETURN)
6242 TREE_THIS_VOLATILE (decl) = 1;
6243 if (ecf_flags & ECF_NOTHROW)
6244 TREE_NOTHROW (decl) = 1;
6245 if (ecf_flags & ECF_MALLOC)
6246 DECL_IS_MALLOC (decl) = 1;
6248 built_in_decls[code] = decl;
6249 implicit_built_in_decls[code] = decl;
6252 /* Call this function after instantiating all builtins that the language
6253 front end cares about. This will build the rest of the builtins that
6254 are relied upon by the tree optimizers and the middle-end. */
6256 void
6257 build_common_builtin_nodes (void)
6259 tree tmp, ftype;
6261 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
6262 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
6264 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6265 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
6266 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6267 ftype = build_function_type (ptr_type_node, tmp);
6269 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
6270 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
6271 "memcpy", ECF_NOTHROW);
6272 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
6273 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
6274 "memmove", ECF_NOTHROW);
6277 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
6279 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6280 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
6281 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
6282 ftype = build_function_type (integer_type_node, tmp);
6283 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
6284 "memcmp", ECF_PURE | ECF_NOTHROW);
6287 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
6289 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6290 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
6291 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6292 ftype = build_function_type (ptr_type_node, tmp);
6293 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
6294 "memset", ECF_NOTHROW);
6297 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
6299 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6300 ftype = build_function_type (ptr_type_node, tmp);
6301 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
6302 "alloca", ECF_NOTHROW | ECF_MALLOC);
6305 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
6306 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6307 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6308 ftype = build_function_type (void_type_node, tmp);
6309 local_define_builtin ("__builtin_init_trampoline", ftype,
6310 BUILT_IN_INIT_TRAMPOLINE,
6311 "__builtin_init_trampoline", ECF_NOTHROW);
6313 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
6314 ftype = build_function_type (ptr_type_node, tmp);
6315 local_define_builtin ("__builtin_adjust_trampoline", ftype,
6316 BUILT_IN_ADJUST_TRAMPOLINE,
6317 "__builtin_adjust_trampoline",
6318 ECF_CONST | ECF_NOTHROW);
6320 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
6321 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6322 ftype = build_function_type (void_type_node, tmp);
6323 local_define_builtin ("__builtin_nonlocal_goto", ftype,
6324 BUILT_IN_NONLOCAL_GOTO,
6325 "__builtin_nonlocal_goto",
6326 ECF_NORETURN | ECF_NOTHROW);
6328 ftype = build_function_type (ptr_type_node, void_list_node);
6329 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
6330 "__builtin_stack_save", ECF_NOTHROW);
6332 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
6333 ftype = build_function_type (void_type_node, tmp);
6334 local_define_builtin ("__builtin_stack_restore", ftype,
6335 BUILT_IN_STACK_RESTORE,
6336 "__builtin_stack_restore", ECF_NOTHROW);
6338 ftype = build_function_type (void_type_node, void_list_node);
6339 local_define_builtin ("__builtin_profile_func_enter", ftype,
6340 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
6341 local_define_builtin ("__builtin_profile_func_exit", ftype,
6342 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
6344 /* Complex multiplication and division. These are handled as builtins
6345 rather than optabs because emit_library_call_value doesn't support
6346 complex. Further, we can do slightly better with folding these
6347 beasties if the real and complex parts of the arguments are separate. */
6349 enum machine_mode mode;
6351 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
6353 char mode_name_buf[4], *q;
6354 const char *p;
6355 enum built_in_function mcode, dcode;
6356 tree type, inner_type;
6358 type = lang_hooks.types.type_for_mode (mode, 0);
6359 if (type == NULL)
6360 continue;
6361 inner_type = TREE_TYPE (type);
6363 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
6364 tmp = tree_cons (NULL_TREE, inner_type, tmp);
6365 tmp = tree_cons (NULL_TREE, inner_type, tmp);
6366 tmp = tree_cons (NULL_TREE, inner_type, tmp);
6367 ftype = build_function_type (type, tmp);
6369 mcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
6370 dcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
6372 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
6373 *q = TOLOWER (*p);
6374 *q = '\0';
6376 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
6377 local_define_builtin (built_in_names[mcode], ftype, mcode,
6378 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
6380 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
6381 local_define_builtin (built_in_names[dcode], ftype, dcode,
6382 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
6387 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
6388 better way.
6390 If we requested a pointer to a vector, build up the pointers that
6391 we stripped off while looking for the inner type. Similarly for
6392 return values from functions.
6394 The argument TYPE is the top of the chain, and BOTTOM is the
6395 new type which we will point to. */
6397 tree
6398 reconstruct_complex_type (tree type, tree bottom)
6400 tree inner, outer;
6402 if (POINTER_TYPE_P (type))
6404 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
6405 outer = build_pointer_type (inner);
6407 else if (TREE_CODE (type) == ARRAY_TYPE)
6409 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
6410 outer = build_array_type (inner, TYPE_DOMAIN (type));
6412 else if (TREE_CODE (type) == FUNCTION_TYPE)
6414 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
6415 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
6417 else if (TREE_CODE (type) == METHOD_TYPE)
6419 tree argtypes;
6420 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
6421 /* The build_method_type_directly() routine prepends 'this' to argument list,
6422 so we must compensate by getting rid of it. */
6423 argtypes = TYPE_ARG_TYPES (type);
6424 outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
6425 inner,
6426 TYPE_ARG_TYPES (type));
6427 TYPE_ARG_TYPES (outer) = argtypes;
6429 else
6430 return bottom;
6432 TYPE_READONLY (outer) = TYPE_READONLY (type);
6433 TYPE_VOLATILE (outer) = TYPE_VOLATILE (type);
6435 return outer;
6438 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
6439 the inner type. */
6440 tree
6441 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
6443 int nunits;
6445 switch (GET_MODE_CLASS (mode))
6447 case MODE_VECTOR_INT:
6448 case MODE_VECTOR_FLOAT:
6449 nunits = GET_MODE_NUNITS (mode);
6450 break;
6452 case MODE_INT:
6453 /* Check that there are no leftover bits. */
6454 gcc_assert (GET_MODE_BITSIZE (mode)
6455 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
6457 nunits = GET_MODE_BITSIZE (mode)
6458 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
6459 break;
6461 default:
6462 gcc_unreachable ();
6465 return make_vector_type (innertype, nunits, mode);
6468 /* Similarly, but takes the inner type and number of units, which must be
6469 a power of two. */
6471 tree
6472 build_vector_type (tree innertype, int nunits)
6474 return make_vector_type (innertype, nunits, VOIDmode);
6477 /* Build RESX_EXPR with given REGION_NUMBER. */
6478 tree
6479 build_resx (int region_number)
6481 tree t;
6482 t = build1 (RESX_EXPR, void_type_node,
6483 build_int_cst (NULL_TREE, region_number));
6484 return t;
6487 /* Given an initializer INIT, return TRUE if INIT is zero or some
6488 aggregate of zeros. Otherwise return FALSE. */
6489 bool
6490 initializer_zerop (tree init)
6492 tree elt;
6494 STRIP_NOPS (init);
6496 switch (TREE_CODE (init))
6498 case INTEGER_CST:
6499 return integer_zerop (init);
6501 case REAL_CST:
6502 /* ??? Note that this is not correct for C4X float formats. There,
6503 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
6504 negative exponent. */
6505 return real_zerop (init)
6506 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
6508 case COMPLEX_CST:
6509 return integer_zerop (init)
6510 || (real_zerop (init)
6511 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
6512 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
6514 case VECTOR_CST:
6515 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
6516 if (!initializer_zerop (TREE_VALUE (elt)))
6517 return false;
6518 return true;
6520 case CONSTRUCTOR:
6522 unsigned HOST_WIDE_INT idx;
6524 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
6525 if (!initializer_zerop (elt))
6526 return false;
6527 return true;
6530 default:
6531 return false;
6535 void
6536 add_var_to_bind_expr (tree bind_expr, tree var)
6538 BIND_EXPR_VARS (bind_expr)
6539 = chainon (BIND_EXPR_VARS (bind_expr), var);
6540 if (BIND_EXPR_BLOCK (bind_expr))
6541 BLOCK_VARS (BIND_EXPR_BLOCK (bind_expr))
6542 = BIND_EXPR_VARS (bind_expr);
6545 /* Build an empty statement. */
6547 tree
6548 build_empty_stmt (void)
6550 return build1 (NOP_EXPR, void_type_node, size_zero_node);
6554 /* Returns true if it is possible to prove that the index of
6555 an array access REF (an ARRAY_REF expression) falls into the
6556 array bounds. */
6558 bool
6559 in_array_bounds_p (tree ref)
6561 tree idx = TREE_OPERAND (ref, 1);
6562 tree min, max;
6564 if (TREE_CODE (idx) != INTEGER_CST)
6565 return false;
6567 min = array_ref_low_bound (ref);
6568 max = array_ref_up_bound (ref);
6569 if (!min
6570 || !max
6571 || TREE_CODE (min) != INTEGER_CST
6572 || TREE_CODE (max) != INTEGER_CST)
6573 return false;
6575 if (tree_int_cst_lt (idx, min)
6576 || tree_int_cst_lt (max, idx))
6577 return false;
6579 return true;
6582 /* Return true if T (assumed to be a DECL) is a global variable. */
6584 bool
6585 is_global_var (tree t)
6587 if (MTAG_P (t))
6588 return (TREE_STATIC (t) || MTAG_GLOBAL (t));
6589 else
6590 return (TREE_STATIC (t) || DECL_EXTERNAL (t));
6593 /* Return true if T (assumed to be a DECL) must be assigned a memory
6594 location. */
6596 bool
6597 needs_to_live_in_memory (tree t)
6599 return (TREE_ADDRESSABLE (t)
6600 || is_global_var (t)
6601 || (TREE_CODE (t) == RESULT_DECL
6602 && aggregate_value_p (t, current_function_decl)));
6605 /* There are situations in which a language considers record types
6606 compatible which have different field lists. Decide if two fields
6607 are compatible. It is assumed that the parent records are compatible. */
6609 bool
6610 fields_compatible_p (tree f1, tree f2)
6612 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
6613 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
6614 return false;
6616 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
6617 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
6618 return false;
6620 if (!lang_hooks.types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
6621 return false;
6623 return true;
6626 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
6628 tree
6629 find_compatible_field (tree record, tree orig_field)
6631 tree f;
6633 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
6634 if (TREE_CODE (f) == FIELD_DECL
6635 && fields_compatible_p (f, orig_field))
6636 return f;
6638 /* ??? Why isn't this on the main fields list? */
6639 f = TYPE_VFIELD (record);
6640 if (f && TREE_CODE (f) == FIELD_DECL
6641 && fields_compatible_p (f, orig_field))
6642 return f;
6644 /* ??? We should abort here, but Java appears to do Bad Things
6645 with inherited fields. */
6646 return orig_field;
6649 /* Return value of a constant X. */
6651 HOST_WIDE_INT
6652 int_cst_value (tree x)
6654 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
6655 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
6656 bool negative = ((val >> (bits - 1)) & 1) != 0;
6658 gcc_assert (bits <= HOST_BITS_PER_WIDE_INT);
6660 if (negative)
6661 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
6662 else
6663 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
6665 return val;
6668 /* Returns the greatest common divisor of A and B, which must be
6669 INTEGER_CSTs. */
6671 tree
6672 tree_fold_gcd (tree a, tree b)
6674 tree a_mod_b;
6675 tree type = TREE_TYPE (a);
6677 gcc_assert (TREE_CODE (a) == INTEGER_CST);
6678 gcc_assert (TREE_CODE (b) == INTEGER_CST);
6680 if (integer_zerop (a))
6681 return b;
6683 if (integer_zerop (b))
6684 return a;
6686 if (tree_int_cst_sgn (a) == -1)
6687 a = fold_build2 (MULT_EXPR, type, a,
6688 convert (type, integer_minus_one_node));
6690 if (tree_int_cst_sgn (b) == -1)
6691 b = fold_build2 (MULT_EXPR, type, b,
6692 convert (type, integer_minus_one_node));
6694 while (1)
6696 a_mod_b = fold_build2 (FLOOR_MOD_EXPR, type, a, b);
6698 if (!TREE_INT_CST_LOW (a_mod_b)
6699 && !TREE_INT_CST_HIGH (a_mod_b))
6700 return b;
6702 a = b;
6703 b = a_mod_b;
6707 /* Returns unsigned variant of TYPE. */
6709 tree
6710 unsigned_type_for (tree type)
6712 return lang_hooks.types.unsigned_type (type);
6715 /* Returns signed variant of TYPE. */
6717 tree
6718 signed_type_for (tree type)
6720 return lang_hooks.types.signed_type (type);
6723 /* Returns the largest value obtainable by casting something in INNER type to
6724 OUTER type. */
6726 tree
6727 upper_bound_in_type (tree outer, tree inner)
6729 unsigned HOST_WIDE_INT lo, hi;
6730 unsigned int det = 0;
6731 unsigned oprec = TYPE_PRECISION (outer);
6732 unsigned iprec = TYPE_PRECISION (inner);
6733 unsigned prec;
6735 /* Compute a unique number for every combination. */
6736 det |= (oprec > iprec) ? 4 : 0;
6737 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
6738 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
6740 /* Determine the exponent to use. */
6741 switch (det)
6743 case 0:
6744 case 1:
6745 /* oprec <= iprec, outer: signed, inner: don't care. */
6746 prec = oprec - 1;
6747 break;
6748 case 2:
6749 case 3:
6750 /* oprec <= iprec, outer: unsigned, inner: don't care. */
6751 prec = oprec;
6752 break;
6753 case 4:
6754 /* oprec > iprec, outer: signed, inner: signed. */
6755 prec = iprec - 1;
6756 break;
6757 case 5:
6758 /* oprec > iprec, outer: signed, inner: unsigned. */
6759 prec = iprec;
6760 break;
6761 case 6:
6762 /* oprec > iprec, outer: unsigned, inner: signed. */
6763 prec = oprec;
6764 break;
6765 case 7:
6766 /* oprec > iprec, outer: unsigned, inner: unsigned. */
6767 prec = iprec;
6768 break;
6769 default:
6770 gcc_unreachable ();
6773 /* Compute 2^^prec - 1. */
6774 if (prec <= HOST_BITS_PER_WIDE_INT)
6776 hi = 0;
6777 lo = ((~(unsigned HOST_WIDE_INT) 0)
6778 >> (HOST_BITS_PER_WIDE_INT - prec));
6780 else
6782 hi = ((~(unsigned HOST_WIDE_INT) 0)
6783 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
6784 lo = ~(unsigned HOST_WIDE_INT) 0;
6787 return build_int_cst_wide (outer, lo, hi);
6790 /* Returns the smallest value obtainable by casting something in INNER type to
6791 OUTER type. */
6793 tree
6794 lower_bound_in_type (tree outer, tree inner)
6796 unsigned HOST_WIDE_INT lo, hi;
6797 unsigned oprec = TYPE_PRECISION (outer);
6798 unsigned iprec = TYPE_PRECISION (inner);
6800 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
6801 and obtain 0. */
6802 if (TYPE_UNSIGNED (outer)
6803 /* If we are widening something of an unsigned type, OUTER type
6804 contains all values of INNER type. In particular, both INNER
6805 and OUTER types have zero in common. */
6806 || (oprec > iprec && TYPE_UNSIGNED (inner)))
6807 lo = hi = 0;
6808 else
6810 /* If we are widening a signed type to another signed type, we
6811 want to obtain -2^^(iprec-1). If we are keeping the
6812 precision or narrowing to a signed type, we want to obtain
6813 -2^(oprec-1). */
6814 unsigned prec = oprec > iprec ? iprec : oprec;
6816 if (prec <= HOST_BITS_PER_WIDE_INT)
6818 hi = ~(unsigned HOST_WIDE_INT) 0;
6819 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
6821 else
6823 hi = ((~(unsigned HOST_WIDE_INT) 0)
6824 << (prec - HOST_BITS_PER_WIDE_INT - 1));
6825 lo = 0;
6829 return build_int_cst_wide (outer, lo, hi);
6832 /* Return nonzero if two operands that are suitable for PHI nodes are
6833 necessarily equal. Specifically, both ARG0 and ARG1 must be either
6834 SSA_NAME or invariant. Note that this is strictly an optimization.
6835 That is, callers of this function can directly call operand_equal_p
6836 and get the same result, only slower. */
6839 operand_equal_for_phi_arg_p (tree arg0, tree arg1)
6841 if (arg0 == arg1)
6842 return 1;
6843 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
6844 return 0;
6845 return operand_equal_p (arg0, arg1, 0);
6848 /* Returns number of zeros at the end of binary representation of X.
6850 ??? Use ffs if available? */
6852 tree
6853 num_ending_zeros (tree x)
6855 unsigned HOST_WIDE_INT fr, nfr;
6856 unsigned num, abits;
6857 tree type = TREE_TYPE (x);
6859 if (TREE_INT_CST_LOW (x) == 0)
6861 num = HOST_BITS_PER_WIDE_INT;
6862 fr = TREE_INT_CST_HIGH (x);
6864 else
6866 num = 0;
6867 fr = TREE_INT_CST_LOW (x);
6870 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
6872 nfr = fr >> abits;
6873 if (nfr << abits == fr)
6875 num += abits;
6876 fr = nfr;
6880 if (num > TYPE_PRECISION (type))
6881 num = TYPE_PRECISION (type);
6883 return build_int_cst_type (type, num);
6887 #define WALK_SUBTREE(NODE) \
6888 do \
6890 result = walk_tree (&(NODE), func, data, pset); \
6891 if (result) \
6892 return result; \
6894 while (0)
6896 /* This is a subroutine of walk_tree that walks field of TYPE that are to
6897 be walked whenever a type is seen in the tree. Rest of operands and return
6898 value are as for walk_tree. */
6900 static tree
6901 walk_type_fields (tree type, walk_tree_fn func, void *data,
6902 struct pointer_set_t *pset)
6904 tree result = NULL_TREE;
6906 switch (TREE_CODE (type))
6908 case POINTER_TYPE:
6909 case REFERENCE_TYPE:
6910 /* We have to worry about mutually recursive pointers. These can't
6911 be written in C. They can in Ada. It's pathological, but
6912 there's an ACATS test (c38102a) that checks it. Deal with this
6913 by checking if we're pointing to another pointer, that one
6914 points to another pointer, that one does too, and we have no htab.
6915 If so, get a hash table. We check three levels deep to avoid
6916 the cost of the hash table if we don't need one. */
6917 if (POINTER_TYPE_P (TREE_TYPE (type))
6918 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
6919 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
6920 && !pset)
6922 result = walk_tree_without_duplicates (&TREE_TYPE (type),
6923 func, data);
6924 if (result)
6925 return result;
6927 break;
6930 /* ... fall through ... */
6932 case COMPLEX_TYPE:
6933 WALK_SUBTREE (TREE_TYPE (type));
6934 break;
6936 case METHOD_TYPE:
6937 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
6939 /* Fall through. */
6941 case FUNCTION_TYPE:
6942 WALK_SUBTREE (TREE_TYPE (type));
6944 tree arg;
6946 /* We never want to walk into default arguments. */
6947 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
6948 WALK_SUBTREE (TREE_VALUE (arg));
6950 break;
6952 case ARRAY_TYPE:
6953 /* Don't follow this nodes's type if a pointer for fear that we'll
6954 have infinite recursion. Those types are uninteresting anyway. */
6955 if (!POINTER_TYPE_P (TREE_TYPE (type))
6956 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE)
6957 WALK_SUBTREE (TREE_TYPE (type));
6958 WALK_SUBTREE (TYPE_DOMAIN (type));
6959 break;
6961 case BOOLEAN_TYPE:
6962 case ENUMERAL_TYPE:
6963 case INTEGER_TYPE:
6964 case CHAR_TYPE:
6965 case REAL_TYPE:
6966 WALK_SUBTREE (TYPE_MIN_VALUE (type));
6967 WALK_SUBTREE (TYPE_MAX_VALUE (type));
6968 break;
6970 case OFFSET_TYPE:
6971 WALK_SUBTREE (TREE_TYPE (type));
6972 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
6973 break;
6975 default:
6976 break;
6979 return NULL_TREE;
6982 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
6983 called with the DATA and the address of each sub-tree. If FUNC returns a
6984 non-NULL value, the traversal is stopped, and the value returned by FUNC
6985 is returned. If PSET is non-NULL it is used to record the nodes visited,
6986 and to avoid visiting a node more than once. */
6988 tree
6989 walk_tree (tree *tp, walk_tree_fn func, void *data, struct pointer_set_t *pset)
6991 enum tree_code code;
6992 int walk_subtrees;
6993 tree result;
6995 #define WALK_SUBTREE_TAIL(NODE) \
6996 do \
6998 tp = & (NODE); \
6999 goto tail_recurse; \
7001 while (0)
7003 tail_recurse:
7004 /* Skip empty subtrees. */
7005 if (!*tp)
7006 return NULL_TREE;
7008 /* Don't walk the same tree twice, if the user has requested
7009 that we avoid doing so. */
7010 if (pset && pointer_set_insert (pset, *tp))
7011 return NULL_TREE;
7013 /* Call the function. */
7014 walk_subtrees = 1;
7015 result = (*func) (tp, &walk_subtrees, data);
7017 /* If we found something, return it. */
7018 if (result)
7019 return result;
7021 code = TREE_CODE (*tp);
7023 /* Even if we didn't, FUNC may have decided that there was nothing
7024 interesting below this point in the tree. */
7025 if (!walk_subtrees)
7027 if (code == TREE_LIST)
7028 /* But we still need to check our siblings. */
7029 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
7030 else
7031 return NULL_TREE;
7034 result = lang_hooks.tree_inlining.walk_subtrees (tp, &walk_subtrees, func,
7035 data, pset);
7036 if (result || ! walk_subtrees)
7037 return result;
7039 /* If this is a DECL_EXPR, walk into various fields of the type that it's
7040 defining. We only want to walk into these fields of a type in this
7041 case. Note that decls get walked as part of the processing of a
7042 BIND_EXPR.
7044 ??? Precisely which fields of types that we are supposed to walk in
7045 this case vs. the normal case aren't well defined. */
7046 if (code == DECL_EXPR
7047 && TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL
7048 && TREE_CODE (TREE_TYPE (DECL_EXPR_DECL (*tp))) != ERROR_MARK)
7050 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
7052 /* Call the function for the type. See if it returns anything or
7053 doesn't want us to continue. If we are to continue, walk both
7054 the normal fields and those for the declaration case. */
7055 result = (*func) (type_p, &walk_subtrees, data);
7056 if (result || !walk_subtrees)
7057 return NULL_TREE;
7059 result = walk_type_fields (*type_p, func, data, pset);
7060 if (result)
7061 return result;
7063 WALK_SUBTREE (TYPE_SIZE (*type_p));
7064 WALK_SUBTREE (TYPE_SIZE_UNIT (*type_p));
7066 /* If this is a record type, also walk the fields. */
7067 if (TREE_CODE (*type_p) == RECORD_TYPE
7068 || TREE_CODE (*type_p) == UNION_TYPE
7069 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
7071 tree field;
7073 for (field = TYPE_FIELDS (*type_p); field;
7074 field = TREE_CHAIN (field))
7076 /* We'd like to look at the type of the field, but we can easily
7077 get infinite recursion. So assume it's pointed to elsewhere
7078 in the tree. Also, ignore things that aren't fields. */
7079 if (TREE_CODE (field) != FIELD_DECL)
7080 continue;
7082 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
7083 WALK_SUBTREE (DECL_SIZE (field));
7084 WALK_SUBTREE (DECL_SIZE_UNIT (field));
7085 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
7086 WALK_SUBTREE (DECL_QUALIFIER (field));
7091 else if (code != SAVE_EXPR
7092 && code != BIND_EXPR
7093 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
7095 int i, len;
7097 /* Walk over all the sub-trees of this operand. */
7098 len = TREE_CODE_LENGTH (code);
7099 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
7100 But, we only want to walk once. */
7101 if (code == TARGET_EXPR
7102 && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
7103 --len;
7105 /* Go through the subtrees. We need to do this in forward order so
7106 that the scope of a FOR_EXPR is handled properly. */
7107 #ifdef DEBUG_WALK_TREE
7108 for (i = 0; i < len; ++i)
7109 WALK_SUBTREE (TREE_OPERAND (*tp, i));
7110 #else
7111 for (i = 0; i < len - 1; ++i)
7112 WALK_SUBTREE (TREE_OPERAND (*tp, i));
7114 if (len)
7116 /* The common case is that we may tail recurse here. */
7117 if (code != BIND_EXPR
7118 && !TREE_CHAIN (*tp))
7119 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
7120 else
7121 WALK_SUBTREE (TREE_OPERAND (*tp, len - 1));
7123 #endif
7126 /* If this is a type, walk the needed fields in the type. */
7127 else if (TYPE_P (*tp))
7129 result = walk_type_fields (*tp, func, data, pset);
7130 if (result)
7131 return result;
7133 else
7135 /* Not one of the easy cases. We must explicitly go through the
7136 children. */
7137 switch (code)
7139 case ERROR_MARK:
7140 case IDENTIFIER_NODE:
7141 case INTEGER_CST:
7142 case REAL_CST:
7143 case VECTOR_CST:
7144 case STRING_CST:
7145 case BLOCK:
7146 case PLACEHOLDER_EXPR:
7147 case SSA_NAME:
7148 case FIELD_DECL:
7149 case RESULT_DECL:
7150 /* None of these have subtrees other than those already walked
7151 above. */
7152 break;
7154 case TREE_LIST:
7155 WALK_SUBTREE (TREE_VALUE (*tp));
7156 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
7157 break;
7159 case TREE_VEC:
7161 int len = TREE_VEC_LENGTH (*tp);
7163 if (len == 0)
7164 break;
7166 /* Walk all elements but the first. */
7167 while (--len)
7168 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
7170 /* Now walk the first one as a tail call. */
7171 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
7174 case COMPLEX_CST:
7175 WALK_SUBTREE (TREE_REALPART (*tp));
7176 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
7178 case CONSTRUCTOR:
7180 unsigned HOST_WIDE_INT idx;
7181 constructor_elt *ce;
7183 for (idx = 0;
7184 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
7185 idx++)
7186 WALK_SUBTREE (ce->value);
7188 break;
7190 case SAVE_EXPR:
7191 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
7193 case BIND_EXPR:
7195 tree decl;
7196 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
7198 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
7199 into declarations that are just mentioned, rather than
7200 declared; they don't really belong to this part of the tree.
7201 And, we can see cycles: the initializer for a declaration
7202 can refer to the declaration itself. */
7203 WALK_SUBTREE (DECL_INITIAL (decl));
7204 WALK_SUBTREE (DECL_SIZE (decl));
7205 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
7207 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
7210 case STATEMENT_LIST:
7212 tree_stmt_iterator i;
7213 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
7214 WALK_SUBTREE (*tsi_stmt_ptr (i));
7216 break;
7218 default:
7219 /* ??? This could be a language-defined node. We really should make
7220 a hook for it, but right now just ignore it. */
7221 break;
7225 /* We didn't find what we were looking for. */
7226 return NULL_TREE;
7228 #undef WALK_SUBTREE_TAIL
7230 #undef WALK_SUBTREE
7232 /* Like walk_tree, but does not walk duplicate nodes more than once. */
7234 tree
7235 walk_tree_without_duplicates (tree *tp, walk_tree_fn func, void *data)
7237 tree result;
7238 struct pointer_set_t *pset;
7240 pset = pointer_set_create ();
7241 result = walk_tree (tp, func, data, pset);
7242 pointer_set_destroy (pset);
7243 return result;
7246 #include "gt-tree.h"